sysctl: Move the implementation into fs/proc/proc_sysctl.c
[linux-2.6/btrfs-unstable.git] / fs / proc / proc_sysctl.c
blob06e6f10ee8ec6611175fcb982e146a0923b3f75b
1 /*
2 * /proc/sys support
3 */
4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/poll.h>
7 #include <linux/proc_fs.h>
8 #include <linux/security.h>
9 #include <linux/namei.h>
10 #include <linux/module.h>
11 #include "internal.h"
13 static const struct dentry_operations proc_sys_dentry_operations;
14 static const struct file_operations proc_sys_file_operations;
15 static const struct inode_operations proc_sys_inode_operations;
16 static const struct file_operations proc_sys_dir_file_operations;
17 static const struct inode_operations proc_sys_dir_operations;
19 void proc_sys_poll_notify(struct ctl_table_poll *poll)
21 if (!poll)
22 return;
24 atomic_inc(&poll->event);
25 wake_up_interruptible(&poll->wait);
28 static struct ctl_table root_table[1];
29 static struct ctl_table_root sysctl_table_root;
30 static struct ctl_table_header root_table_header = {
31 {{.count = 1,
32 .ctl_table = root_table,
33 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
34 .root = &sysctl_table_root,
35 .set = &sysctl_table_root.default_set,
37 static struct ctl_table_root sysctl_table_root = {
38 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
39 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
42 static DEFINE_SPINLOCK(sysctl_lock);
44 /* called under sysctl_lock */
45 static int use_table(struct ctl_table_header *p)
47 if (unlikely(p->unregistering))
48 return 0;
49 p->used++;
50 return 1;
53 /* called under sysctl_lock */
54 static void unuse_table(struct ctl_table_header *p)
56 if (!--p->used)
57 if (unlikely(p->unregistering))
58 complete(p->unregistering);
61 /* called under sysctl_lock, will reacquire if has to wait */
62 static void start_unregistering(struct ctl_table_header *p)
65 * if p->used is 0, nobody will ever touch that entry again;
66 * we'll eliminate all paths to it before dropping sysctl_lock
68 if (unlikely(p->used)) {
69 struct completion wait;
70 init_completion(&wait);
71 p->unregistering = &wait;
72 spin_unlock(&sysctl_lock);
73 wait_for_completion(&wait);
74 spin_lock(&sysctl_lock);
75 } else {
76 /* anything non-NULL; we'll never dereference it */
77 p->unregistering = ERR_PTR(-EINVAL);
80 * do not remove from the list until nobody holds it; walking the
81 * list in do_sysctl() relies on that.
83 list_del_init(&p->ctl_entry);
86 static void sysctl_head_get(struct ctl_table_header *head)
88 spin_lock(&sysctl_lock);
89 head->count++;
90 spin_unlock(&sysctl_lock);
93 void sysctl_head_put(struct ctl_table_header *head)
95 spin_lock(&sysctl_lock);
96 if (!--head->count)
97 kfree_rcu(head, rcu);
98 spin_unlock(&sysctl_lock);
101 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
103 if (!head)
104 BUG();
105 spin_lock(&sysctl_lock);
106 if (!use_table(head))
107 head = ERR_PTR(-ENOENT);
108 spin_unlock(&sysctl_lock);
109 return head;
112 static void sysctl_head_finish(struct ctl_table_header *head)
114 if (!head)
115 return;
116 spin_lock(&sysctl_lock);
117 unuse_table(head);
118 spin_unlock(&sysctl_lock);
121 static struct ctl_table_set *
122 lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
124 struct ctl_table_set *set = &root->default_set;
125 if (root->lookup)
126 set = root->lookup(root, namespaces);
127 return set;
130 static struct list_head *
131 lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
133 struct ctl_table_set *set = lookup_header_set(root, namespaces);
134 return &set->list;
137 static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
138 struct ctl_table_header *prev)
140 struct ctl_table_root *root;
141 struct list_head *header_list;
142 struct ctl_table_header *head;
143 struct list_head *tmp;
145 spin_lock(&sysctl_lock);
146 if (prev) {
147 head = prev;
148 tmp = &prev->ctl_entry;
149 unuse_table(prev);
150 goto next;
152 tmp = &root_table_header.ctl_entry;
153 for (;;) {
154 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
156 if (!use_table(head))
157 goto next;
158 spin_unlock(&sysctl_lock);
159 return head;
160 next:
161 root = head->root;
162 tmp = tmp->next;
163 header_list = lookup_header_list(root, namespaces);
164 if (tmp != header_list)
165 continue;
167 do {
168 root = list_entry(root->root_list.next,
169 struct ctl_table_root, root_list);
170 if (root == &sysctl_table_root)
171 goto out;
172 header_list = lookup_header_list(root, namespaces);
173 } while (list_empty(header_list));
174 tmp = header_list->next;
176 out:
177 spin_unlock(&sysctl_lock);
178 return NULL;
181 static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
183 return __sysctl_head_next(current->nsproxy, prev);
186 void register_sysctl_root(struct ctl_table_root *root)
188 spin_lock(&sysctl_lock);
189 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
190 spin_unlock(&sysctl_lock);
194 * sysctl_perm does NOT grant the superuser all rights automatically, because
195 * some sysctl variables are readonly even to root.
198 static int test_perm(int mode, int op)
200 if (!current_euid())
201 mode >>= 6;
202 else if (in_egroup_p(0))
203 mode >>= 3;
204 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
205 return 0;
206 return -EACCES;
209 static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
211 int mode;
213 if (root->permissions)
214 mode = root->permissions(root, current->nsproxy, table);
215 else
216 mode = table->mode;
218 return test_perm(mode, op);
221 static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
223 for (; table->procname; table++) {
224 table->parent = parent;
225 if (table->child)
226 sysctl_set_parent(table, table->child);
231 static struct inode *proc_sys_make_inode(struct super_block *sb,
232 struct ctl_table_header *head, struct ctl_table *table)
234 struct inode *inode;
235 struct proc_inode *ei;
237 inode = new_inode(sb);
238 if (!inode)
239 goto out;
241 inode->i_ino = get_next_ino();
243 sysctl_head_get(head);
244 ei = PROC_I(inode);
245 ei->sysctl = head;
246 ei->sysctl_entry = table;
248 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
249 inode->i_mode = table->mode;
250 if (!table->child) {
251 inode->i_mode |= S_IFREG;
252 inode->i_op = &proc_sys_inode_operations;
253 inode->i_fop = &proc_sys_file_operations;
254 } else {
255 inode->i_mode |= S_IFDIR;
256 clear_nlink(inode);
257 inode->i_op = &proc_sys_dir_operations;
258 inode->i_fop = &proc_sys_dir_file_operations;
260 out:
261 return inode;
264 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
266 for ( ; p->procname; p++) {
267 if (strlen(p->procname) != name->len)
268 continue;
270 if (memcmp(p->procname, name->name, name->len) != 0)
271 continue;
273 /* I have a match */
274 return p;
276 return NULL;
279 static struct ctl_table_header *grab_header(struct inode *inode)
281 if (PROC_I(inode)->sysctl)
282 return sysctl_head_grab(PROC_I(inode)->sysctl);
283 else
284 return sysctl_head_next(NULL);
287 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
288 struct nameidata *nd)
290 struct ctl_table_header *head = grab_header(dir);
291 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
292 struct ctl_table_header *h = NULL;
293 struct qstr *name = &dentry->d_name;
294 struct ctl_table *p;
295 struct inode *inode;
296 struct dentry *err = ERR_PTR(-ENOENT);
298 if (IS_ERR(head))
299 return ERR_CAST(head);
301 if (table && !table->child) {
302 WARN_ON(1);
303 goto out;
306 table = table ? table->child : head->ctl_table;
308 p = find_in_table(table, name);
309 if (!p) {
310 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
311 if (h->attached_to != table)
312 continue;
313 p = find_in_table(h->attached_by, name);
314 if (p)
315 break;
319 if (!p)
320 goto out;
322 err = ERR_PTR(-ENOMEM);
323 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
324 if (h)
325 sysctl_head_finish(h);
327 if (!inode)
328 goto out;
330 err = NULL;
331 d_set_d_op(dentry, &proc_sys_dentry_operations);
332 d_add(dentry, inode);
334 out:
335 sysctl_head_finish(head);
336 return err;
339 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
340 size_t count, loff_t *ppos, int write)
342 struct inode *inode = filp->f_path.dentry->d_inode;
343 struct ctl_table_header *head = grab_header(inode);
344 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
345 ssize_t error;
346 size_t res;
348 if (IS_ERR(head))
349 return PTR_ERR(head);
352 * At this point we know that the sysctl was not unregistered
353 * and won't be until we finish.
355 error = -EPERM;
356 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
357 goto out;
359 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
360 error = -EINVAL;
361 if (!table->proc_handler)
362 goto out;
364 /* careful: calling conventions are nasty here */
365 res = count;
366 error = table->proc_handler(table, write, buf, &res, ppos);
367 if (!error)
368 error = res;
369 out:
370 sysctl_head_finish(head);
372 return error;
375 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
376 size_t count, loff_t *ppos)
378 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
381 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
382 size_t count, loff_t *ppos)
384 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
387 static int proc_sys_open(struct inode *inode, struct file *filp)
389 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
391 if (table->poll)
392 filp->private_data = proc_sys_poll_event(table->poll);
394 return 0;
397 static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
399 struct inode *inode = filp->f_path.dentry->d_inode;
400 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
401 unsigned long event = (unsigned long)filp->private_data;
402 unsigned int ret = DEFAULT_POLLMASK;
404 if (!table->proc_handler)
405 goto out;
407 if (!table->poll)
408 goto out;
410 poll_wait(filp, &table->poll->wait, wait);
412 if (event != atomic_read(&table->poll->event)) {
413 filp->private_data = proc_sys_poll_event(table->poll);
414 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
417 out:
418 return ret;
421 static int proc_sys_fill_cache(struct file *filp, void *dirent,
422 filldir_t filldir,
423 struct ctl_table_header *head,
424 struct ctl_table *table)
426 struct dentry *child, *dir = filp->f_path.dentry;
427 struct inode *inode;
428 struct qstr qname;
429 ino_t ino = 0;
430 unsigned type = DT_UNKNOWN;
432 qname.name = table->procname;
433 qname.len = strlen(table->procname);
434 qname.hash = full_name_hash(qname.name, qname.len);
436 child = d_lookup(dir, &qname);
437 if (!child) {
438 child = d_alloc(dir, &qname);
439 if (child) {
440 inode = proc_sys_make_inode(dir->d_sb, head, table);
441 if (!inode) {
442 dput(child);
443 return -ENOMEM;
444 } else {
445 d_set_d_op(child, &proc_sys_dentry_operations);
446 d_add(child, inode);
448 } else {
449 return -ENOMEM;
452 inode = child->d_inode;
453 ino = inode->i_ino;
454 type = inode->i_mode >> 12;
455 dput(child);
456 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
459 static int scan(struct ctl_table_header *head, ctl_table *table,
460 unsigned long *pos, struct file *file,
461 void *dirent, filldir_t filldir)
464 for (; table->procname; table++, (*pos)++) {
465 int res;
467 if (*pos < file->f_pos)
468 continue;
470 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
471 if (res)
472 return res;
474 file->f_pos = *pos + 1;
476 return 0;
479 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
481 struct dentry *dentry = filp->f_path.dentry;
482 struct inode *inode = dentry->d_inode;
483 struct ctl_table_header *head = grab_header(inode);
484 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
485 struct ctl_table_header *h = NULL;
486 unsigned long pos;
487 int ret = -EINVAL;
489 if (IS_ERR(head))
490 return PTR_ERR(head);
492 if (table && !table->child) {
493 WARN_ON(1);
494 goto out;
497 table = table ? table->child : head->ctl_table;
499 ret = 0;
500 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
501 if (filp->f_pos == 0) {
502 if (filldir(dirent, ".", 1, filp->f_pos,
503 inode->i_ino, DT_DIR) < 0)
504 goto out;
505 filp->f_pos++;
507 if (filp->f_pos == 1) {
508 if (filldir(dirent, "..", 2, filp->f_pos,
509 parent_ino(dentry), DT_DIR) < 0)
510 goto out;
511 filp->f_pos++;
513 pos = 2;
515 ret = scan(head, table, &pos, filp, dirent, filldir);
516 if (ret)
517 goto out;
519 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
520 if (h->attached_to != table)
521 continue;
522 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
523 if (ret) {
524 sysctl_head_finish(h);
525 break;
528 ret = 1;
529 out:
530 sysctl_head_finish(head);
531 return ret;
534 static int proc_sys_permission(struct inode *inode, int mask)
537 * sysctl entries that are not writeable,
538 * are _NOT_ writeable, capabilities or not.
540 struct ctl_table_header *head;
541 struct ctl_table *table;
542 int error;
544 /* Executable files are not allowed under /proc/sys/ */
545 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
546 return -EACCES;
548 head = grab_header(inode);
549 if (IS_ERR(head))
550 return PTR_ERR(head);
552 table = PROC_I(inode)->sysctl_entry;
553 if (!table) /* global root - r-xr-xr-x */
554 error = mask & MAY_WRITE ? -EACCES : 0;
555 else /* Use the permissions on the sysctl table entry */
556 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
558 sysctl_head_finish(head);
559 return error;
562 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
564 struct inode *inode = dentry->d_inode;
565 int error;
567 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
568 return -EPERM;
570 error = inode_change_ok(inode, attr);
571 if (error)
572 return error;
574 if ((attr->ia_valid & ATTR_SIZE) &&
575 attr->ia_size != i_size_read(inode)) {
576 error = vmtruncate(inode, attr->ia_size);
577 if (error)
578 return error;
581 setattr_copy(inode, attr);
582 mark_inode_dirty(inode);
583 return 0;
586 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
588 struct inode *inode = dentry->d_inode;
589 struct ctl_table_header *head = grab_header(inode);
590 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
592 if (IS_ERR(head))
593 return PTR_ERR(head);
595 generic_fillattr(inode, stat);
596 if (table)
597 stat->mode = (stat->mode & S_IFMT) | table->mode;
599 sysctl_head_finish(head);
600 return 0;
603 static const struct file_operations proc_sys_file_operations = {
604 .open = proc_sys_open,
605 .poll = proc_sys_poll,
606 .read = proc_sys_read,
607 .write = proc_sys_write,
608 .llseek = default_llseek,
611 static const struct file_operations proc_sys_dir_file_operations = {
612 .read = generic_read_dir,
613 .readdir = proc_sys_readdir,
614 .llseek = generic_file_llseek,
617 static const struct inode_operations proc_sys_inode_operations = {
618 .permission = proc_sys_permission,
619 .setattr = proc_sys_setattr,
620 .getattr = proc_sys_getattr,
623 static const struct inode_operations proc_sys_dir_operations = {
624 .lookup = proc_sys_lookup,
625 .permission = proc_sys_permission,
626 .setattr = proc_sys_setattr,
627 .getattr = proc_sys_getattr,
630 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
632 if (nd->flags & LOOKUP_RCU)
633 return -ECHILD;
634 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
637 static int proc_sys_delete(const struct dentry *dentry)
639 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
642 static int sysctl_is_seen(struct ctl_table_header *p)
644 struct ctl_table_set *set = p->set;
645 int res;
646 spin_lock(&sysctl_lock);
647 if (p->unregistering)
648 res = 0;
649 else if (!set->is_seen)
650 res = 1;
651 else
652 res = set->is_seen(set);
653 spin_unlock(&sysctl_lock);
654 return res;
657 static int proc_sys_compare(const struct dentry *parent,
658 const struct inode *pinode,
659 const struct dentry *dentry, const struct inode *inode,
660 unsigned int len, const char *str, const struct qstr *name)
662 struct ctl_table_header *head;
663 /* Although proc doesn't have negative dentries, rcu-walk means
664 * that inode here can be NULL */
665 /* AV: can it, indeed? */
666 if (!inode)
667 return 1;
668 if (name->len != len)
669 return 1;
670 if (memcmp(name->name, str, len))
671 return 1;
672 head = rcu_dereference(PROC_I(inode)->sysctl);
673 return !head || !sysctl_is_seen(head);
676 static const struct dentry_operations proc_sys_dentry_operations = {
677 .d_revalidate = proc_sys_revalidate,
678 .d_delete = proc_sys_delete,
679 .d_compare = proc_sys_compare,
682 static struct ctl_table *is_branch_in(struct ctl_table *branch,
683 struct ctl_table *table)
685 struct ctl_table *p;
686 const char *s = branch->procname;
688 /* branch should have named subdirectory as its first element */
689 if (!s || !branch->child)
690 return NULL;
692 /* ... and nothing else */
693 if (branch[1].procname)
694 return NULL;
696 /* table should contain subdirectory with the same name */
697 for (p = table; p->procname; p++) {
698 if (!p->child)
699 continue;
700 if (p->procname && strcmp(p->procname, s) == 0)
701 return p;
703 return NULL;
706 /* see if attaching q to p would be an improvement */
707 static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
709 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
710 struct ctl_table *next;
711 int is_better = 0;
712 int not_in_parent = !p->attached_by;
714 while ((next = is_branch_in(by, to)) != NULL) {
715 if (by == q->attached_by)
716 is_better = 1;
717 if (to == p->attached_by)
718 not_in_parent = 1;
719 by = by->child;
720 to = next->child;
723 if (is_better && not_in_parent) {
724 q->attached_by = by;
725 q->attached_to = to;
726 q->parent = p;
730 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
731 static int sysctl_depth(struct ctl_table *table)
733 struct ctl_table *tmp;
734 int depth;
736 depth = 0;
737 for (tmp = table; tmp->parent; tmp = tmp->parent)
738 depth++;
740 return depth;
743 static struct ctl_table *sysctl_parent(struct ctl_table *table, int n)
745 int i;
747 for (i = 0; table && i < n; i++)
748 table = table->parent;
750 return table;
754 static void sysctl_print_path(struct ctl_table *table)
756 struct ctl_table *tmp;
757 int depth, i;
758 depth = sysctl_depth(table);
759 if (table->procname) {
760 for (i = depth; i >= 0; i--) {
761 tmp = sysctl_parent(table, i);
762 printk("/%s", tmp->procname?tmp->procname:"");
765 printk(" ");
768 static struct ctl_table *sysctl_check_lookup(struct nsproxy *namespaces,
769 struct ctl_table *table)
771 struct ctl_table_header *head;
772 struct ctl_table *ref, *test;
773 int depth, cur_depth;
775 depth = sysctl_depth(table);
777 for (head = __sysctl_head_next(namespaces, NULL); head;
778 head = __sysctl_head_next(namespaces, head)) {
779 cur_depth = depth;
780 ref = head->ctl_table;
781 repeat:
782 test = sysctl_parent(table, cur_depth);
783 for (; ref->procname; ref++) {
784 int match = 0;
785 if (cur_depth && !ref->child)
786 continue;
788 if (test->procname && ref->procname &&
789 (strcmp(test->procname, ref->procname) == 0))
790 match++;
792 if (match) {
793 if (cur_depth != 0) {
794 cur_depth--;
795 ref = ref->child;
796 goto repeat;
798 goto out;
802 ref = NULL;
803 out:
804 sysctl_head_finish(head);
805 return ref;
808 static void set_fail(const char **fail, struct ctl_table *table, const char *str)
810 if (*fail) {
811 printk(KERN_ERR "sysctl table check failed: ");
812 sysctl_print_path(table);
813 printk(" %s\n", *fail);
814 dump_stack();
816 *fail = str;
819 static void sysctl_check_leaf(struct nsproxy *namespaces,
820 struct ctl_table *table, const char **fail)
822 struct ctl_table *ref;
824 ref = sysctl_check_lookup(namespaces, table);
825 if (ref && (ref != table))
826 set_fail(fail, table, "Sysctl already exists");
829 static int sysctl_check_table(struct nsproxy *namespaces, struct ctl_table *table)
831 int error = 0;
832 for (; table->procname; table++) {
833 const char *fail = NULL;
835 if (table->parent) {
836 if (!table->parent->procname)
837 set_fail(&fail, table, "Parent without procname");
839 if (table->child) {
840 if (table->data)
841 set_fail(&fail, table, "Directory with data?");
842 if (table->maxlen)
843 set_fail(&fail, table, "Directory with maxlen?");
844 if ((table->mode & (S_IRUGO|S_IXUGO)) != table->mode)
845 set_fail(&fail, table, "Writable sysctl directory");
846 if (table->proc_handler)
847 set_fail(&fail, table, "Directory with proc_handler");
848 if (table->extra1)
849 set_fail(&fail, table, "Directory with extra1");
850 if (table->extra2)
851 set_fail(&fail, table, "Directory with extra2");
852 } else {
853 if ((table->proc_handler == proc_dostring) ||
854 (table->proc_handler == proc_dointvec) ||
855 (table->proc_handler == proc_dointvec_minmax) ||
856 (table->proc_handler == proc_dointvec_jiffies) ||
857 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
858 (table->proc_handler == proc_dointvec_ms_jiffies) ||
859 (table->proc_handler == proc_doulongvec_minmax) ||
860 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
861 if (!table->data)
862 set_fail(&fail, table, "No data");
863 if (!table->maxlen)
864 set_fail(&fail, table, "No maxlen");
866 #ifdef CONFIG_PROC_SYSCTL
867 if (!table->proc_handler)
868 set_fail(&fail, table, "No proc_handler");
869 #endif
870 sysctl_check_leaf(namespaces, table, &fail);
872 if (table->mode > 0777)
873 set_fail(&fail, table, "bogus .mode");
874 if (fail) {
875 set_fail(&fail, table, NULL);
876 error = -EINVAL;
878 if (table->child)
879 error |= sysctl_check_table(namespaces, table->child);
881 return error;
883 #endif /* CONFIG_SYSCTL_SYSCALL_CHECK */
886 * __register_sysctl_paths - register a sysctl hierarchy
887 * @root: List of sysctl headers to register on
888 * @namespaces: Data to compute which lists of sysctl entries are visible
889 * @path: The path to the directory the sysctl table is in.
890 * @table: the top-level table structure
892 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
893 * array. A completely 0 filled entry terminates the table.
895 * The members of the &struct ctl_table structure are used as follows:
897 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
898 * enter a sysctl file
900 * data - a pointer to data for use by proc_handler
902 * maxlen - the maximum size in bytes of the data
904 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
906 * child - a pointer to the child sysctl table if this entry is a directory, or
907 * %NULL.
909 * proc_handler - the text handler routine (described below)
911 * de - for internal use by the sysctl routines
913 * extra1, extra2 - extra pointers usable by the proc handler routines
915 * Leaf nodes in the sysctl tree will be represented by a single file
916 * under /proc; non-leaf nodes will be represented by directories.
918 * sysctl(2) can automatically manage read and write requests through
919 * the sysctl table. The data and maxlen fields of the ctl_table
920 * struct enable minimal validation of the values being written to be
921 * performed, and the mode field allows minimal authentication.
923 * There must be a proc_handler routine for any terminal nodes
924 * mirrored under /proc/sys (non-terminals are handled by a built-in
925 * directory handler). Several default handlers are available to
926 * cover common cases -
928 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
929 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
930 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
932 * It is the handler's job to read the input buffer from user memory
933 * and process it. The handler should return 0 on success.
935 * This routine returns %NULL on a failure to register, and a pointer
936 * to the table header on success.
938 struct ctl_table_header *__register_sysctl_paths(
939 struct ctl_table_root *root,
940 struct nsproxy *namespaces,
941 const struct ctl_path *path, struct ctl_table *table)
943 struct ctl_table_header *header;
944 struct ctl_table *new, **prevp;
945 unsigned int n, npath;
946 struct ctl_table_set *set;
948 /* Count the path components */
949 for (npath = 0; path[npath].procname; ++npath)
953 * For each path component, allocate a 2-element ctl_table array.
954 * The first array element will be filled with the sysctl entry
955 * for this, the second will be the sentinel (procname == 0).
957 * We allocate everything in one go so that we don't have to
958 * worry about freeing additional memory in unregister_sysctl_table.
960 header = kzalloc(sizeof(struct ctl_table_header) +
961 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
962 if (!header)
963 return NULL;
965 new = (struct ctl_table *) (header + 1);
967 /* Now connect the dots */
968 prevp = &header->ctl_table;
969 for (n = 0; n < npath; ++n, ++path) {
970 /* Copy the procname */
971 new->procname = path->procname;
972 new->mode = 0555;
974 *prevp = new;
975 prevp = &new->child;
977 new += 2;
979 *prevp = table;
980 header->ctl_table_arg = table;
982 INIT_LIST_HEAD(&header->ctl_entry);
983 header->used = 0;
984 header->unregistering = NULL;
985 header->root = root;
986 sysctl_set_parent(NULL, header->ctl_table);
987 header->count = 1;
988 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
989 if (sysctl_check_table(namespaces, header->ctl_table)) {
990 kfree(header);
991 return NULL;
993 #endif
994 spin_lock(&sysctl_lock);
995 header->set = lookup_header_set(root, namespaces);
996 header->attached_by = header->ctl_table;
997 header->attached_to = root_table;
998 header->parent = &root_table_header;
999 for (set = header->set; set; set = set->parent) {
1000 struct ctl_table_header *p;
1001 list_for_each_entry(p, &set->list, ctl_entry) {
1002 if (p->unregistering)
1003 continue;
1004 try_attach(p, header);
1007 header->parent->count++;
1008 list_add_tail(&header->ctl_entry, &header->set->list);
1009 spin_unlock(&sysctl_lock);
1011 return header;
1015 * register_sysctl_table_path - register a sysctl table hierarchy
1016 * @path: The path to the directory the sysctl table is in.
1017 * @table: the top-level table structure
1019 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1020 * array. A completely 0 filled entry terminates the table.
1022 * See __register_sysctl_paths for more details.
1024 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1025 struct ctl_table *table)
1027 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1028 path, table);
1030 EXPORT_SYMBOL(register_sysctl_paths);
1033 * register_sysctl_table - register a sysctl table hierarchy
1034 * @table: the top-level table structure
1036 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1037 * array. A completely 0 filled entry terminates the table.
1039 * See register_sysctl_paths for more details.
1041 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1043 static const struct ctl_path null_path[] = { {} };
1045 return register_sysctl_paths(null_path, table);
1047 EXPORT_SYMBOL(register_sysctl_table);
1050 * unregister_sysctl_table - unregister a sysctl table hierarchy
1051 * @header: the header returned from register_sysctl_table
1053 * Unregisters the sysctl table and all children. proc entries may not
1054 * actually be removed until they are no longer used by anyone.
1056 void unregister_sysctl_table(struct ctl_table_header * header)
1058 might_sleep();
1060 if (header == NULL)
1061 return;
1063 spin_lock(&sysctl_lock);
1064 start_unregistering(header);
1065 if (!--header->parent->count) {
1066 WARN_ON(1);
1067 kfree_rcu(header->parent, rcu);
1069 if (!--header->count)
1070 kfree_rcu(header, rcu);
1071 spin_unlock(&sysctl_lock);
1073 EXPORT_SYMBOL(unregister_sysctl_table);
1075 void setup_sysctl_set(struct ctl_table_set *p,
1076 struct ctl_table_set *parent,
1077 int (*is_seen)(struct ctl_table_set *))
1079 INIT_LIST_HEAD(&p->list);
1080 p->parent = parent ? parent : &sysctl_table_root.default_set;
1081 p->is_seen = is_seen;
1085 int __init proc_sys_init(void)
1087 struct proc_dir_entry *proc_sys_root;
1089 proc_sys_root = proc_mkdir("sys", NULL);
1090 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1091 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
1092 proc_sys_root->nlink = 0;
1094 return sysctl_init();