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