2 * proc/fs/generic.c --- generic routines for the proc-fs
4 * This file contains generic proc-fs routines for handling
5 * directories and files.
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
11 #include <linux/errno.h>
12 #include <linux/time.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.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>
30 static DEFINE_SPINLOCK(proc_subdir_lock);
32 static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
34 if (len < de->namelen)
36 if (len > de->namelen)
39 return memcmp(name, de->name, len);
42 static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
44 struct rb_node *node = rb_first(&dir->subdir);
49 return rb_entry(node, struct proc_dir_entry, subdir_node);
52 static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
54 struct rb_node *node = rb_next(&dir->subdir_node);
59 return rb_entry(node, struct proc_dir_entry, subdir_node);
62 static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
66 struct rb_node *node = dir->subdir.rb_node;
69 struct proc_dir_entry *de = container_of(node,
70 struct proc_dir_entry,
72 int result = proc_match(len, name, de);
77 node = node->rb_right;
84 static bool pde_subdir_insert(struct proc_dir_entry *dir,
85 struct proc_dir_entry *de)
87 struct rb_root *root = &dir->subdir;
88 struct rb_node **new = &root->rb_node, *parent = NULL;
90 /* Figure out where to put new node */
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);
98 new = &(*new)->rb_left;
100 new = &(*new)->rb_right;
105 /* Add new node and rebalance tree. */
106 rb_link_node(&de->subdir_node, parent, new);
107 rb_insert_color(&de->subdir_node, root);
111 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
113 struct inode *inode = dentry->d_inode;
114 struct proc_dir_entry *de = PDE(inode);
117 error = inode_change_ok(inode, iattr);
121 setattr_copy(inode, iattr);
122 mark_inode_dirty(inode);
124 proc_set_user(de, inode->i_uid, inode->i_gid);
125 de->mode = inode->i_mode;
129 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
132 struct inode *inode = dentry->d_inode;
133 struct proc_dir_entry *de = PROC_I(inode)->pde;
135 set_nlink(inode, de->nlink);
137 generic_fillattr(inode, stat);
141 static const struct inode_operations proc_file_inode_operations = {
142 .setattr = proc_notify_change,
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.
150 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
151 const char **residual)
153 const char *cp = name, *next;
154 struct proc_dir_entry *de;
162 next = strchr(cp, '/');
167 de = pde_subdir_find(de, cp, len);
169 WARN(1, "name '%s'\n", name);
179 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
180 const char **residual)
184 spin_lock(&proc_subdir_lock);
185 rv = __xlate_proc_name(name, ret, residual);
186 spin_unlock(&proc_subdir_lock);
190 static DEFINE_IDA(proc_inum_ida);
191 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
193 #define PROC_DYNAMIC_FIRST 0xF0000000U
196 * Return an inode number between PROC_DYNAMIC_FIRST and
197 * 0xffffffff, or zero on failure.
199 int proc_alloc_inum(unsigned int *inum)
205 if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
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)
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);
222 *inum = PROC_DYNAMIC_FIRST + i;
226 void proc_free_inum(unsigned int inum)
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);
234 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
236 nd_set_link(nd, __PDE_DATA(dentry->d_inode));
240 static const struct inode_operations proc_link_inode_operations = {
241 .readlink = generic_readlink,
242 .follow_link = proc_follow_link,
246 * Don't create negative dentries here, return -ENOENT by hand
249 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
250 struct dentry *dentry)
254 spin_lock(&proc_subdir_lock);
255 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
258 spin_unlock(&proc_subdir_lock);
259 inode = proc_get_inode(dir->i_sb, de);
261 return ERR_PTR(-ENOMEM);
262 d_set_d_op(dentry, &simple_dentry_operations);
263 d_add(dentry, inode);
266 spin_unlock(&proc_subdir_lock);
267 return ERR_PTR(-ENOENT);
270 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
273 return proc_lookup_de(PDE(dir), dir, dentry);
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..
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
285 int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
286 struct dir_context *ctx)
290 if (!dir_emit_dots(file, ctx))
293 spin_lock(&proc_subdir_lock);
294 de = pde_subdir_first(de);
298 spin_unlock(&proc_subdir_lock);
303 de = pde_subdir_next(de);
308 struct proc_dir_entry *next;
310 spin_unlock(&proc_subdir_lock);
311 if (!dir_emit(ctx, de->name, de->namelen,
312 de->low_ino, de->mode >> 12)) {
316 spin_lock(&proc_subdir_lock);
318 next = pde_subdir_next(de);
322 spin_unlock(&proc_subdir_lock);
326 int proc_readdir(struct file *file, struct dir_context *ctx)
328 struct inode *inode = file_inode(file);
330 return proc_readdir_de(PDE(inode), file, ctx);
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.
338 static const struct file_operations proc_dir_operations = {
339 .llseek = generic_file_llseek,
340 .read = generic_read_dir,
341 .iterate = proc_readdir,
345 * proc directories can do almost nothing..
347 static const struct inode_operations proc_dir_inode_operations = {
348 .lookup = proc_lookup,
349 .getattr = proc_getattr,
350 .setattr = proc_notify_change,
353 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
357 ret = proc_alloc_inum(&dp->low_ino);
361 if (S_ISDIR(dp->mode)) {
362 dp->proc_fops = &proc_dir_operations;
363 dp->proc_iops = &proc_dir_inode_operations;
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;
375 spin_lock(&proc_subdir_lock);
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);
385 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
390 struct proc_dir_entry *ent = NULL;
394 if (xlate_proc_name(name, parent, &fn) != 0)
397 qstr.len = strlen(fn);
398 if (qstr.len == 0 || qstr.len >= 256) {
399 WARN(1, "name len %u\n", qstr.len);
402 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
403 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
407 ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
411 memcpy(ent->name, fn, qstr.len + 1);
412 ent->namelen = qstr.len;
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);
423 struct proc_dir_entry *proc_symlink(const char *name,
424 struct proc_dir_entry *parent, const char *dest)
426 struct proc_dir_entry *ent;
428 ent = __proc_create(&parent, name,
429 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
432 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
434 strcpy((char*)ent->data,dest);
435 if (proc_register(parent, ent) < 0) {
447 EXPORT_SYMBOL(proc_symlink);
449 struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
450 struct proc_dir_entry *parent, void *data)
452 struct proc_dir_entry *ent;
455 mode = S_IRUGO | S_IXUGO;
457 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
460 if (proc_register(parent, ent) < 0) {
467 EXPORT_SYMBOL_GPL(proc_mkdir_data);
469 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
470 struct proc_dir_entry *parent)
472 return proc_mkdir_data(name, mode, parent, NULL);
474 EXPORT_SYMBOL(proc_mkdir_mode);
476 struct proc_dir_entry *proc_mkdir(const char *name,
477 struct proc_dir_entry *parent)
479 return proc_mkdir_data(name, 0, parent, NULL);
481 EXPORT_SYMBOL(proc_mkdir);
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,
488 struct proc_dir_entry *pde;
489 if ((mode & S_IFMT) == 0)
492 if (!S_ISREG(mode)) {
493 WARN_ON(1); /* use proc_mkdir() */
497 if ((mode & S_IALLUGO) == 0)
499 pde = __proc_create(&parent, name, mode, 1);
502 pde->proc_fops = proc_fops;
504 if (proc_register(parent, pde) < 0)
512 EXPORT_SYMBOL(proc_create_data);
514 void proc_set_size(struct proc_dir_entry *de, loff_t size)
518 EXPORT_SYMBOL(proc_set_size);
520 void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
525 EXPORT_SYMBOL(proc_set_user);
527 static void free_proc_entry(struct proc_dir_entry *de)
529 proc_free_inum(de->low_ino);
531 if (S_ISLNK(de->mode))
536 void pde_put(struct proc_dir_entry *pde)
538 if (atomic_dec_and_test(&pde->count))
539 free_proc_entry(pde);
543 * Remove a /proc entry and free it if it's not currently in use.
545 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
547 struct proc_dir_entry *de = NULL;
548 const char *fn = name;
551 spin_lock(&proc_subdir_lock);
552 if (__xlate_proc_name(name, &parent, &fn) != 0) {
553 spin_unlock(&proc_subdir_lock);
558 de = pde_subdir_find(parent, fn, len);
560 rb_erase(&de->subdir_node, &parent->subdir);
561 spin_unlock(&proc_subdir_lock);
563 WARN(1, "name '%s'\n", name);
567 proc_entry_rundown(de);
569 if (S_ISDIR(de->mode))
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);
577 EXPORT_SYMBOL(remove_proc_entry);
579 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
581 struct proc_dir_entry *root = NULL, *de, *next;
582 const char *fn = name;
585 spin_lock(&proc_subdir_lock);
586 if (__xlate_proc_name(name, &parent, &fn) != 0) {
587 spin_unlock(&proc_subdir_lock);
592 root = pde_subdir_find(parent, fn, len);
594 spin_unlock(&proc_subdir_lock);
597 rb_erase(&root->subdir_node, &parent->subdir);
601 next = pde_subdir_first(de);
603 rb_erase(&next->subdir_node, &de->subdir);
607 spin_unlock(&proc_subdir_lock);
609 proc_entry_rundown(de);
611 if (S_ISDIR(de->mode))
618 spin_lock(&proc_subdir_lock);
624 EXPORT_SYMBOL(remove_proc_subtree);
626 void *proc_get_parent_data(const struct inode *inode)
628 struct proc_dir_entry *de = PDE(inode);
629 return de->parent->data;
631 EXPORT_SYMBOL_GPL(proc_get_parent_data);
633 void proc_remove(struct proc_dir_entry *de)
636 remove_proc_subtree(de->name, de->parent);
638 EXPORT_SYMBOL(proc_remove);
640 void *PDE_DATA(const struct inode *inode)
642 return __PDE_DATA(inode);
644 EXPORT_SYMBOL(PDE_DATA);