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