]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/proc/generic.c
x86/xen: Reset VCPU0 info pointer after shared_info remap
[mirror_ubuntu-artful-kernel.git] / fs / proc / generic.c
1 /*
2 * proc/fs/generic.c --- generic routines for the proc-fs
3 *
4 * This file contains generic proc-fs routines for handling
5 * directories and files.
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
9 */
10
11 #include <linux/errno.h>
12 #include <linux/time.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.h>
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/printk.h>
19 #include <linux/mount.h>
20 #include <linux/init.h>
21 #include <linux/idr.h>
22 #include <linux/bitops.h>
23 #include <linux/spinlock.h>
24 #include <linux/completion.h>
25 #include <linux/uaccess.h>
26
27 #include "internal.h"
28
29 static DEFINE_RWLOCK(proc_subdir_lock);
30
31 static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
32 {
33 if (len < de->namelen)
34 return -1;
35 if (len > de->namelen)
36 return 1;
37
38 return memcmp(name, de->name, len);
39 }
40
41 static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
42 {
43 return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
44 subdir_node);
45 }
46
47 static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
48 {
49 return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
50 subdir_node);
51 }
52
53 static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
54 const char *name,
55 unsigned int len)
56 {
57 struct rb_node *node = dir->subdir.rb_node;
58
59 while (node) {
60 struct proc_dir_entry *de = rb_entry(node,
61 struct proc_dir_entry,
62 subdir_node);
63 int result = proc_match(len, name, de);
64
65 if (result < 0)
66 node = node->rb_left;
67 else if (result > 0)
68 node = node->rb_right;
69 else
70 return de;
71 }
72 return NULL;
73 }
74
75 static bool pde_subdir_insert(struct proc_dir_entry *dir,
76 struct proc_dir_entry *de)
77 {
78 struct rb_root *root = &dir->subdir;
79 struct rb_node **new = &root->rb_node, *parent = NULL;
80
81 /* Figure out where to put new node */
82 while (*new) {
83 struct proc_dir_entry *this = rb_entry(*new,
84 struct proc_dir_entry,
85 subdir_node);
86 int result = proc_match(de->namelen, de->name, this);
87
88 parent = *new;
89 if (result < 0)
90 new = &(*new)->rb_left;
91 else if (result > 0)
92 new = &(*new)->rb_right;
93 else
94 return false;
95 }
96
97 /* Add new node and rebalance tree. */
98 rb_link_node(&de->subdir_node, parent, new);
99 rb_insert_color(&de->subdir_node, root);
100 return true;
101 }
102
103 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
104 {
105 struct inode *inode = d_inode(dentry);
106 struct proc_dir_entry *de = PDE(inode);
107 struct user_namespace *s_user_ns;
108 int error;
109
110 /* Don't let anyone mess with weird proc files */
111 s_user_ns = inode->i_sb->s_user_ns;
112 if (!kuid_has_mapping(s_user_ns, inode->i_uid) ||
113 !kgid_has_mapping(s_user_ns, inode->i_gid))
114 return -EPERM;
115
116 error = setattr_prepare(dentry, iattr);
117 if (error)
118 return error;
119
120 setattr_copy(inode, iattr);
121 mark_inode_dirty(inode);
122
123 proc_set_user(de, inode->i_uid, inode->i_gid);
124 de->mode = inode->i_mode;
125 return 0;
126 }
127
128 static int proc_getattr(const struct path *path, struct kstat *stat,
129 u32 request_mask, unsigned int query_flags)
130 {
131 struct inode *inode = d_inode(path->dentry);
132 struct proc_dir_entry *de = PDE(inode);
133 if (de && de->nlink)
134 set_nlink(inode, de->nlink);
135
136 generic_fillattr(inode, stat);
137 return 0;
138 }
139
140 static const struct inode_operations proc_file_inode_operations = {
141 .setattr = proc_notify_change,
142 };
143
144 /*
145 * This function parses a name such as "tty/driver/serial", and
146 * returns the struct proc_dir_entry for "/proc/tty/driver", and
147 * returns "serial" in residual.
148 */
149 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
150 const char **residual)
151 {
152 const char *cp = name, *next;
153 struct proc_dir_entry *de;
154 unsigned int len;
155
156 de = *ret;
157 if (!de)
158 de = &proc_root;
159
160 while (1) {
161 next = strchr(cp, '/');
162 if (!next)
163 break;
164
165 len = next - cp;
166 de = pde_subdir_find(de, cp, len);
167 if (!de) {
168 WARN(1, "name '%s'\n", name);
169 return -ENOENT;
170 }
171 cp += len + 1;
172 }
173 *residual = cp;
174 *ret = de;
175 return 0;
176 }
177
178 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
179 const char **residual)
180 {
181 int rv;
182
183 read_lock(&proc_subdir_lock);
184 rv = __xlate_proc_name(name, ret, residual);
185 read_unlock(&proc_subdir_lock);
186 return rv;
187 }
188
189 static DEFINE_IDA(proc_inum_ida);
190
191 #define PROC_DYNAMIC_FIRST 0xF0000000U
192
193 /*
194 * Return an inode number between PROC_DYNAMIC_FIRST and
195 * 0xffffffff, or zero on failure.
196 */
197 int proc_alloc_inum(unsigned int *inum)
198 {
199 int i;
200
201 i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1,
202 GFP_KERNEL);
203 if (i < 0)
204 return i;
205
206 *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
207 return 0;
208 }
209
210 void proc_free_inum(unsigned int inum)
211 {
212 ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
213 }
214
215 /*
216 * Don't create negative dentries here, return -ENOENT by hand
217 * instead.
218 */
219 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
220 struct dentry *dentry)
221 {
222 struct inode *inode;
223
224 read_lock(&proc_subdir_lock);
225 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
226 if (de) {
227 pde_get(de);
228 read_unlock(&proc_subdir_lock);
229 inode = proc_get_inode(dir->i_sb, de);
230 if (!inode)
231 return ERR_PTR(-ENOMEM);
232 d_set_d_op(dentry, &simple_dentry_operations);
233 d_add(dentry, inode);
234 return NULL;
235 }
236 read_unlock(&proc_subdir_lock);
237 return ERR_PTR(-ENOENT);
238 }
239
240 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
241 unsigned int flags)
242 {
243 return proc_lookup_de(PDE(dir), dir, dentry);
244 }
245
246 /*
247 * This returns non-zero if at EOF, so that the /proc
248 * root directory can use this and check if it should
249 * continue with the <pid> entries..
250 *
251 * Note that the VFS-layer doesn't care about the return
252 * value of the readdir() call, as long as it's non-negative
253 * for success..
254 */
255 int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
256 struct dir_context *ctx)
257 {
258 int i;
259
260 if (!dir_emit_dots(file, ctx))
261 return 0;
262
263 read_lock(&proc_subdir_lock);
264 de = pde_subdir_first(de);
265 i = ctx->pos - 2;
266 for (;;) {
267 if (!de) {
268 read_unlock(&proc_subdir_lock);
269 return 0;
270 }
271 if (!i)
272 break;
273 de = pde_subdir_next(de);
274 i--;
275 }
276
277 do {
278 struct proc_dir_entry *next;
279 pde_get(de);
280 read_unlock(&proc_subdir_lock);
281 if (!dir_emit(ctx, de->name, de->namelen,
282 de->low_ino, de->mode >> 12)) {
283 pde_put(de);
284 return 0;
285 }
286 read_lock(&proc_subdir_lock);
287 ctx->pos++;
288 next = pde_subdir_next(de);
289 pde_put(de);
290 de = next;
291 } while (de);
292 read_unlock(&proc_subdir_lock);
293 return 1;
294 }
295
296 int proc_readdir(struct file *file, struct dir_context *ctx)
297 {
298 struct inode *inode = file_inode(file);
299
300 return proc_readdir_de(PDE(inode), file, ctx);
301 }
302
303 /*
304 * These are the generic /proc directory operations. They
305 * use the in-memory "struct proc_dir_entry" tree to parse
306 * the /proc directory.
307 */
308 static const struct file_operations proc_dir_operations = {
309 .llseek = generic_file_llseek,
310 .read = generic_read_dir,
311 .iterate_shared = proc_readdir,
312 };
313
314 /*
315 * proc directories can do almost nothing..
316 */
317 static const struct inode_operations proc_dir_inode_operations = {
318 .lookup = proc_lookup,
319 .getattr = proc_getattr,
320 .setattr = proc_notify_change,
321 };
322
323 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
324 {
325 int ret;
326
327 ret = proc_alloc_inum(&dp->low_ino);
328 if (ret)
329 return ret;
330
331 write_lock(&proc_subdir_lock);
332 dp->parent = dir;
333 if (pde_subdir_insert(dir, dp) == false) {
334 WARN(1, "proc_dir_entry '%s/%s' already registered\n",
335 dir->name, dp->name);
336 write_unlock(&proc_subdir_lock);
337 proc_free_inum(dp->low_ino);
338 return -EEXIST;
339 }
340 write_unlock(&proc_subdir_lock);
341
342 return 0;
343 }
344
345 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
346 const char *name,
347 umode_t mode,
348 nlink_t nlink)
349 {
350 struct proc_dir_entry *ent = NULL;
351 const char *fn;
352 struct qstr qstr;
353
354 if (xlate_proc_name(name, parent, &fn) != 0)
355 goto out;
356 qstr.name = fn;
357 qstr.len = strlen(fn);
358 if (qstr.len == 0 || qstr.len >= 256) {
359 WARN(1, "name len %u\n", qstr.len);
360 return NULL;
361 }
362 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
363 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
364 return NULL;
365 }
366 if (is_empty_pde(*parent)) {
367 WARN(1, "attempt to add to permanently empty directory");
368 return NULL;
369 }
370
371 ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
372 if (!ent)
373 goto out;
374
375 memcpy(ent->name, fn, qstr.len + 1);
376 ent->namelen = qstr.len;
377 ent->mode = mode;
378 ent->nlink = nlink;
379 ent->subdir = RB_ROOT;
380 atomic_set(&ent->count, 1);
381 spin_lock_init(&ent->pde_unload_lock);
382 INIT_LIST_HEAD(&ent->pde_openers);
383 proc_set_user(ent, (*parent)->uid, (*parent)->gid);
384
385 out:
386 return ent;
387 }
388
389 struct proc_dir_entry *proc_symlink(const char *name,
390 struct proc_dir_entry *parent, const char *dest)
391 {
392 struct proc_dir_entry *ent;
393
394 ent = __proc_create(&parent, name,
395 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
396
397 if (ent) {
398 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
399 if (ent->data) {
400 strcpy((char*)ent->data,dest);
401 ent->proc_iops = &proc_link_inode_operations;
402 if (proc_register(parent, ent) < 0) {
403 kfree(ent->data);
404 kfree(ent);
405 ent = NULL;
406 }
407 } else {
408 kfree(ent);
409 ent = NULL;
410 }
411 }
412 return ent;
413 }
414 EXPORT_SYMBOL(proc_symlink);
415
416 struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
417 struct proc_dir_entry *parent, void *data)
418 {
419 struct proc_dir_entry *ent;
420
421 if (mode == 0)
422 mode = S_IRUGO | S_IXUGO;
423
424 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
425 if (ent) {
426 ent->data = data;
427 ent->proc_fops = &proc_dir_operations;
428 ent->proc_iops = &proc_dir_inode_operations;
429 parent->nlink++;
430 if (proc_register(parent, ent) < 0) {
431 kfree(ent);
432 parent->nlink--;
433 ent = NULL;
434 }
435 }
436 return ent;
437 }
438 EXPORT_SYMBOL_GPL(proc_mkdir_data);
439
440 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
441 struct proc_dir_entry *parent)
442 {
443 return proc_mkdir_data(name, mode, parent, NULL);
444 }
445 EXPORT_SYMBOL(proc_mkdir_mode);
446
447 struct proc_dir_entry *proc_mkdir(const char *name,
448 struct proc_dir_entry *parent)
449 {
450 return proc_mkdir_data(name, 0, parent, NULL);
451 }
452 EXPORT_SYMBOL(proc_mkdir);
453
454 struct proc_dir_entry *proc_create_mount_point(const char *name)
455 {
456 umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
457 struct proc_dir_entry *ent, *parent = NULL;
458
459 ent = __proc_create(&parent, name, mode, 2);
460 if (ent) {
461 ent->data = NULL;
462 ent->proc_fops = NULL;
463 ent->proc_iops = NULL;
464 parent->nlink++;
465 if (proc_register(parent, ent) < 0) {
466 kfree(ent);
467 parent->nlink--;
468 ent = NULL;
469 }
470 }
471 return ent;
472 }
473 EXPORT_SYMBOL(proc_create_mount_point);
474
475 struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
476 struct proc_dir_entry *parent,
477 const struct file_operations *proc_fops,
478 void *data)
479 {
480 struct proc_dir_entry *pde;
481 if ((mode & S_IFMT) == 0)
482 mode |= S_IFREG;
483
484 if (!S_ISREG(mode)) {
485 WARN_ON(1); /* use proc_mkdir() */
486 return NULL;
487 }
488
489 BUG_ON(proc_fops == NULL);
490
491 if ((mode & S_IALLUGO) == 0)
492 mode |= S_IRUGO;
493 pde = __proc_create(&parent, name, mode, 1);
494 if (!pde)
495 goto out;
496 pde->proc_fops = proc_fops;
497 pde->data = data;
498 pde->proc_iops = &proc_file_inode_operations;
499 if (proc_register(parent, pde) < 0)
500 goto out_free;
501 return pde;
502 out_free:
503 kfree(pde);
504 out:
505 return NULL;
506 }
507 EXPORT_SYMBOL(proc_create_data);
508
509 void proc_set_size(struct proc_dir_entry *de, loff_t size)
510 {
511 de->size = size;
512 }
513 EXPORT_SYMBOL(proc_set_size);
514
515 void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
516 {
517 de->uid = uid;
518 de->gid = gid;
519 }
520 EXPORT_SYMBOL(proc_set_user);
521
522 static void free_proc_entry(struct proc_dir_entry *de)
523 {
524 proc_free_inum(de->low_ino);
525
526 if (S_ISLNK(de->mode))
527 kfree(de->data);
528 kfree(de);
529 }
530
531 void pde_put(struct proc_dir_entry *pde)
532 {
533 if (atomic_dec_and_test(&pde->count))
534 free_proc_entry(pde);
535 }
536
537 /*
538 * Remove a /proc entry and free it if it's not currently in use.
539 */
540 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
541 {
542 struct proc_dir_entry *de = NULL;
543 const char *fn = name;
544 unsigned int len;
545
546 write_lock(&proc_subdir_lock);
547 if (__xlate_proc_name(name, &parent, &fn) != 0) {
548 write_unlock(&proc_subdir_lock);
549 return;
550 }
551 len = strlen(fn);
552
553 de = pde_subdir_find(parent, fn, len);
554 if (de)
555 rb_erase(&de->subdir_node, &parent->subdir);
556 write_unlock(&proc_subdir_lock);
557 if (!de) {
558 WARN(1, "name '%s'\n", name);
559 return;
560 }
561
562 proc_entry_rundown(de);
563
564 if (S_ISDIR(de->mode))
565 parent->nlink--;
566 de->nlink = 0;
567 WARN(pde_subdir_first(de),
568 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
569 __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
570 pde_put(de);
571 }
572 EXPORT_SYMBOL(remove_proc_entry);
573
574 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
575 {
576 struct proc_dir_entry *root = NULL, *de, *next;
577 const char *fn = name;
578 unsigned int len;
579
580 write_lock(&proc_subdir_lock);
581 if (__xlate_proc_name(name, &parent, &fn) != 0) {
582 write_unlock(&proc_subdir_lock);
583 return -ENOENT;
584 }
585 len = strlen(fn);
586
587 root = pde_subdir_find(parent, fn, len);
588 if (!root) {
589 write_unlock(&proc_subdir_lock);
590 return -ENOENT;
591 }
592 rb_erase(&root->subdir_node, &parent->subdir);
593
594 de = root;
595 while (1) {
596 next = pde_subdir_first(de);
597 if (next) {
598 rb_erase(&next->subdir_node, &de->subdir);
599 de = next;
600 continue;
601 }
602 write_unlock(&proc_subdir_lock);
603
604 proc_entry_rundown(de);
605 next = de->parent;
606 if (S_ISDIR(de->mode))
607 next->nlink--;
608 de->nlink = 0;
609 if (de == root)
610 break;
611 pde_put(de);
612
613 write_lock(&proc_subdir_lock);
614 de = next;
615 }
616 pde_put(root);
617 return 0;
618 }
619 EXPORT_SYMBOL(remove_proc_subtree);
620
621 void *proc_get_parent_data(const struct inode *inode)
622 {
623 struct proc_dir_entry *de = PDE(inode);
624 return de->parent->data;
625 }
626 EXPORT_SYMBOL_GPL(proc_get_parent_data);
627
628 void proc_remove(struct proc_dir_entry *de)
629 {
630 if (de)
631 remove_proc_subtree(de->name, de->parent);
632 }
633 EXPORT_SYMBOL(proc_remove);
634
635 void *PDE_DATA(const struct inode *inode)
636 {
637 return __PDE_DATA(inode);
638 }
639 EXPORT_SYMBOL(PDE_DATA);