]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/proc/proc_sysctl.c
e5601dc240883d552bbb889e02d43279b8b05c64
[mirror_ubuntu-bionic-kernel.git] / fs / proc / proc_sysctl.c
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"
12
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;
18
19 void proc_sys_poll_notify(struct ctl_table_poll *poll)
20 {
21 if (!poll)
22 return;
23
24 atomic_inc(&poll->event);
25 wake_up_interruptible(&poll->wait);
26 }
27
28 static struct ctl_table root_table[] = {
29 {
30 .procname = "",
31 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
32 },
33 { }
34 };
35 static struct ctl_table_root sysctl_table_root = {
36 .default_set.dir.header = {
37 {{.count = 1,
38 .nreg = 1,
39 .ctl_table = root_table }},
40 .ctl_table_arg = root_table,
41 .root = &sysctl_table_root,
42 .set = &sysctl_table_root.default_set,
43 },
44 };
45
46 static DEFINE_SPINLOCK(sysctl_lock);
47
48 static void drop_sysctl_table(struct ctl_table_header *header);
49 static int sysctl_follow_link(struct ctl_table_header **phead,
50 struct ctl_table **pentry, struct nsproxy *namespaces);
51 static int insert_links(struct ctl_table_header *head);
52 static void put_links(struct ctl_table_header *header);
53
54 static void sysctl_print_dir(struct ctl_dir *dir)
55 {
56 if (dir->header.parent)
57 sysctl_print_dir(dir->header.parent);
58 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
59 }
60
61 static int namecmp(const char *name1, int len1, const char *name2, int len2)
62 {
63 int minlen;
64 int cmp;
65
66 minlen = len1;
67 if (minlen > len2)
68 minlen = len2;
69
70 cmp = memcmp(name1, name2, minlen);
71 if (cmp == 0)
72 cmp = len1 - len2;
73 return cmp;
74 }
75
76 /* Called under sysctl_lock */
77 static struct ctl_table *find_entry(struct ctl_table_header **phead,
78 struct ctl_dir *dir, const char *name, int namelen)
79 {
80 struct ctl_table_header *head;
81 struct ctl_table *entry;
82 struct rb_node *node = dir->root.rb_node;
83
84 while (node)
85 {
86 struct ctl_node *ctl_node;
87 const char *procname;
88 int cmp;
89
90 ctl_node = rb_entry(node, struct ctl_node, node);
91 head = ctl_node->header;
92 entry = &head->ctl_table[ctl_node - head->node];
93 procname = entry->procname;
94
95 cmp = namecmp(name, namelen, procname, strlen(procname));
96 if (cmp < 0)
97 node = node->rb_left;
98 else if (cmp > 0)
99 node = node->rb_right;
100 else {
101 *phead = head;
102 return entry;
103 }
104 }
105 return NULL;
106 }
107
108 static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
109 {
110 struct rb_node *node = &head->node[entry - head->ctl_table].node;
111 struct rb_node **p = &head->parent->root.rb_node;
112 struct rb_node *parent = NULL;
113 const char *name = entry->procname;
114 int namelen = strlen(name);
115
116 while (*p) {
117 struct ctl_table_header *parent_head;
118 struct ctl_table *parent_entry;
119 struct ctl_node *parent_node;
120 const char *parent_name;
121 int cmp;
122
123 parent = *p;
124 parent_node = rb_entry(parent, struct ctl_node, node);
125 parent_head = parent_node->header;
126 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
127 parent_name = parent_entry->procname;
128
129 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
130 if (cmp < 0)
131 p = &(*p)->rb_left;
132 else if (cmp > 0)
133 p = &(*p)->rb_right;
134 else {
135 printk(KERN_ERR "sysctl duplicate entry: ");
136 sysctl_print_dir(head->parent);
137 printk(KERN_CONT "/%s\n", entry->procname);
138 return -EEXIST;
139 }
140 }
141
142 rb_link_node(node, parent, p);
143 return 0;
144 }
145
146 static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
147 {
148 struct rb_node *node = &head->node[entry - head->ctl_table].node;
149
150 rb_erase(node, &head->parent->root);
151 }
152
153 static void init_header(struct ctl_table_header *head,
154 struct ctl_table_root *root, struct ctl_table_set *set,
155 struct ctl_node *node, struct ctl_table *table)
156 {
157 head->ctl_table = table;
158 head->ctl_table_arg = table;
159 head->used = 0;
160 head->count = 1;
161 head->nreg = 1;
162 head->unregistering = NULL;
163 head->root = root;
164 head->set = set;
165 head->parent = NULL;
166 head->node = node;
167 if (node) {
168 struct ctl_table *entry;
169 for (entry = table; entry->procname; entry++, node++) {
170 rb_init_node(&node->node);
171 node->header = head;
172 }
173 }
174 }
175
176 static void erase_header(struct ctl_table_header *head)
177 {
178 struct ctl_table *entry;
179 for (entry = head->ctl_table; entry->procname; entry++)
180 erase_entry(head, entry);
181 }
182
183 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
184 {
185 struct ctl_table *entry;
186 int err;
187
188 dir->header.nreg++;
189 header->parent = dir;
190 err = insert_links(header);
191 if (err)
192 goto fail_links;
193 for (entry = header->ctl_table; entry->procname; entry++) {
194 err = insert_entry(header, entry);
195 if (err)
196 goto fail;
197 }
198 return 0;
199 fail:
200 erase_header(header);
201 put_links(header);
202 fail_links:
203 header->parent = NULL;
204 drop_sysctl_table(&dir->header);
205 return err;
206 }
207
208 /* called under sysctl_lock */
209 static int use_table(struct ctl_table_header *p)
210 {
211 if (unlikely(p->unregistering))
212 return 0;
213 p->used++;
214 return 1;
215 }
216
217 /* called under sysctl_lock */
218 static void unuse_table(struct ctl_table_header *p)
219 {
220 if (!--p->used)
221 if (unlikely(p->unregistering))
222 complete(p->unregistering);
223 }
224
225 /* called under sysctl_lock, will reacquire if has to wait */
226 static void start_unregistering(struct ctl_table_header *p)
227 {
228 /*
229 * if p->used is 0, nobody will ever touch that entry again;
230 * we'll eliminate all paths to it before dropping sysctl_lock
231 */
232 if (unlikely(p->used)) {
233 struct completion wait;
234 init_completion(&wait);
235 p->unregistering = &wait;
236 spin_unlock(&sysctl_lock);
237 wait_for_completion(&wait);
238 spin_lock(&sysctl_lock);
239 } else {
240 /* anything non-NULL; we'll never dereference it */
241 p->unregistering = ERR_PTR(-EINVAL);
242 }
243 /*
244 * do not remove from the list until nobody holds it; walking the
245 * list in do_sysctl() relies on that.
246 */
247 erase_header(p);
248 }
249
250 static void sysctl_head_get(struct ctl_table_header *head)
251 {
252 spin_lock(&sysctl_lock);
253 head->count++;
254 spin_unlock(&sysctl_lock);
255 }
256
257 void sysctl_head_put(struct ctl_table_header *head)
258 {
259 spin_lock(&sysctl_lock);
260 if (!--head->count)
261 kfree_rcu(head, rcu);
262 spin_unlock(&sysctl_lock);
263 }
264
265 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
266 {
267 if (!head)
268 BUG();
269 spin_lock(&sysctl_lock);
270 if (!use_table(head))
271 head = ERR_PTR(-ENOENT);
272 spin_unlock(&sysctl_lock);
273 return head;
274 }
275
276 static void sysctl_head_finish(struct ctl_table_header *head)
277 {
278 if (!head)
279 return;
280 spin_lock(&sysctl_lock);
281 unuse_table(head);
282 spin_unlock(&sysctl_lock);
283 }
284
285 static struct ctl_table_set *
286 lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
287 {
288 struct ctl_table_set *set = &root->default_set;
289 if (root->lookup)
290 set = root->lookup(root, namespaces);
291 return set;
292 }
293
294 static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
295 struct ctl_dir *dir,
296 const char *name, int namelen)
297 {
298 struct ctl_table_header *head;
299 struct ctl_table *entry;
300
301 spin_lock(&sysctl_lock);
302 entry = find_entry(&head, dir, name, namelen);
303 if (entry && use_table(head))
304 *phead = head;
305 else
306 entry = NULL;
307 spin_unlock(&sysctl_lock);
308 return entry;
309 }
310
311 static struct ctl_node *first_usable_entry(struct rb_node *node)
312 {
313 struct ctl_node *ctl_node;
314
315 for (;node; node = rb_next(node)) {
316 ctl_node = rb_entry(node, struct ctl_node, node);
317 if (use_table(ctl_node->header))
318 return ctl_node;
319 }
320 return NULL;
321 }
322
323 static void first_entry(struct ctl_dir *dir,
324 struct ctl_table_header **phead, struct ctl_table **pentry)
325 {
326 struct ctl_table_header *head = NULL;
327 struct ctl_table *entry = NULL;
328 struct ctl_node *ctl_node;
329
330 spin_lock(&sysctl_lock);
331 ctl_node = first_usable_entry(rb_first(&dir->root));
332 spin_unlock(&sysctl_lock);
333 if (ctl_node) {
334 head = ctl_node->header;
335 entry = &head->ctl_table[ctl_node - head->node];
336 }
337 *phead = head;
338 *pentry = entry;
339 }
340
341 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
342 {
343 struct ctl_table_header *head = *phead;
344 struct ctl_table *entry = *pentry;
345 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
346
347 spin_lock(&sysctl_lock);
348 unuse_table(head);
349
350 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
351 spin_unlock(&sysctl_lock);
352 head = NULL;
353 if (ctl_node) {
354 head = ctl_node->header;
355 entry = &head->ctl_table[ctl_node - head->node];
356 }
357 *phead = head;
358 *pentry = entry;
359 }
360
361 void register_sysctl_root(struct ctl_table_root *root)
362 {
363 }
364
365 /*
366 * sysctl_perm does NOT grant the superuser all rights automatically, because
367 * some sysctl variables are readonly even to root.
368 */
369
370 static int test_perm(int mode, int op)
371 {
372 if (!current_euid())
373 mode >>= 6;
374 else if (in_egroup_p(0))
375 mode >>= 3;
376 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
377 return 0;
378 return -EACCES;
379 }
380
381 static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
382 {
383 int mode;
384
385 if (root->permissions)
386 mode = root->permissions(root, current->nsproxy, table);
387 else
388 mode = table->mode;
389
390 return test_perm(mode, op);
391 }
392
393 static struct inode *proc_sys_make_inode(struct super_block *sb,
394 struct ctl_table_header *head, struct ctl_table *table)
395 {
396 struct inode *inode;
397 struct proc_inode *ei;
398
399 inode = new_inode(sb);
400 if (!inode)
401 goto out;
402
403 inode->i_ino = get_next_ino();
404
405 sysctl_head_get(head);
406 ei = PROC_I(inode);
407 ei->sysctl = head;
408 ei->sysctl_entry = table;
409
410 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
411 inode->i_mode = table->mode;
412 if (!S_ISDIR(table->mode)) {
413 inode->i_mode |= S_IFREG;
414 inode->i_op = &proc_sys_inode_operations;
415 inode->i_fop = &proc_sys_file_operations;
416 } else {
417 inode->i_mode |= S_IFDIR;
418 inode->i_op = &proc_sys_dir_operations;
419 inode->i_fop = &proc_sys_dir_file_operations;
420 }
421 out:
422 return inode;
423 }
424
425 static struct ctl_table_header *grab_header(struct inode *inode)
426 {
427 struct ctl_table_header *head = PROC_I(inode)->sysctl;
428 if (!head)
429 head = &sysctl_table_root.default_set.dir.header;
430 return sysctl_head_grab(head);
431 }
432
433 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
434 struct nameidata *nd)
435 {
436 struct ctl_table_header *head = grab_header(dir);
437 struct ctl_table_header *h = NULL;
438 struct qstr *name = &dentry->d_name;
439 struct ctl_table *p;
440 struct inode *inode;
441 struct dentry *err = ERR_PTR(-ENOENT);
442 struct ctl_dir *ctl_dir;
443 int ret;
444
445 if (IS_ERR(head))
446 return ERR_CAST(head);
447
448 ctl_dir = container_of(head, struct ctl_dir, header);
449
450 p = lookup_entry(&h, ctl_dir, name->name, name->len);
451 if (!p)
452 goto out;
453
454 ret = sysctl_follow_link(&h, &p, current->nsproxy);
455 err = ERR_PTR(ret);
456 if (ret)
457 goto out;
458
459 err = ERR_PTR(-ENOMEM);
460 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
461 if (h)
462 sysctl_head_finish(h);
463
464 if (!inode)
465 goto out;
466
467 err = NULL;
468 d_set_d_op(dentry, &proc_sys_dentry_operations);
469 d_add(dentry, inode);
470
471 out:
472 sysctl_head_finish(head);
473 return err;
474 }
475
476 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
477 size_t count, loff_t *ppos, int write)
478 {
479 struct inode *inode = filp->f_path.dentry->d_inode;
480 struct ctl_table_header *head = grab_header(inode);
481 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
482 ssize_t error;
483 size_t res;
484
485 if (IS_ERR(head))
486 return PTR_ERR(head);
487
488 /*
489 * At this point we know that the sysctl was not unregistered
490 * and won't be until we finish.
491 */
492 error = -EPERM;
493 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
494 goto out;
495
496 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
497 error = -EINVAL;
498 if (!table->proc_handler)
499 goto out;
500
501 /* careful: calling conventions are nasty here */
502 res = count;
503 error = table->proc_handler(table, write, buf, &res, ppos);
504 if (!error)
505 error = res;
506 out:
507 sysctl_head_finish(head);
508
509 return error;
510 }
511
512 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
513 size_t count, loff_t *ppos)
514 {
515 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
516 }
517
518 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
519 size_t count, loff_t *ppos)
520 {
521 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
522 }
523
524 static int proc_sys_open(struct inode *inode, struct file *filp)
525 {
526 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
527
528 if (table->poll)
529 filp->private_data = proc_sys_poll_event(table->poll);
530
531 return 0;
532 }
533
534 static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
535 {
536 struct inode *inode = filp->f_path.dentry->d_inode;
537 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
538 unsigned long event = (unsigned long)filp->private_data;
539 unsigned int ret = DEFAULT_POLLMASK;
540
541 if (!table->proc_handler)
542 goto out;
543
544 if (!table->poll)
545 goto out;
546
547 poll_wait(filp, &table->poll->wait, wait);
548
549 if (event != atomic_read(&table->poll->event)) {
550 filp->private_data = proc_sys_poll_event(table->poll);
551 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
552 }
553
554 out:
555 return ret;
556 }
557
558 static int proc_sys_fill_cache(struct file *filp, void *dirent,
559 filldir_t filldir,
560 struct ctl_table_header *head,
561 struct ctl_table *table)
562 {
563 struct dentry *child, *dir = filp->f_path.dentry;
564 struct inode *inode;
565 struct qstr qname;
566 ino_t ino = 0;
567 unsigned type = DT_UNKNOWN;
568
569 qname.name = table->procname;
570 qname.len = strlen(table->procname);
571 qname.hash = full_name_hash(qname.name, qname.len);
572
573 child = d_lookup(dir, &qname);
574 if (!child) {
575 child = d_alloc(dir, &qname);
576 if (child) {
577 inode = proc_sys_make_inode(dir->d_sb, head, table);
578 if (!inode) {
579 dput(child);
580 return -ENOMEM;
581 } else {
582 d_set_d_op(child, &proc_sys_dentry_operations);
583 d_add(child, inode);
584 }
585 } else {
586 return -ENOMEM;
587 }
588 }
589 inode = child->d_inode;
590 ino = inode->i_ino;
591 type = inode->i_mode >> 12;
592 dput(child);
593 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
594 }
595
596 static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
597 filldir_t filldir,
598 struct ctl_table_header *head,
599 struct ctl_table *table)
600 {
601 int err, ret = 0;
602 head = sysctl_head_grab(head);
603
604 /* It is not an error if we can not follow the link ignore it */
605 err = sysctl_follow_link(&head, &table, current->nsproxy);
606 if (err)
607 goto out;
608
609 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
610 out:
611 sysctl_head_finish(head);
612 return ret;
613 }
614
615 static int scan(struct ctl_table_header *head, ctl_table *table,
616 unsigned long *pos, struct file *file,
617 void *dirent, filldir_t filldir)
618 {
619 int res;
620
621 if ((*pos)++ < file->f_pos)
622 return 0;
623
624 if (unlikely(S_ISLNK(table->mode)))
625 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
626 else
627 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
628
629 if (res == 0)
630 file->f_pos = *pos;
631
632 return res;
633 }
634
635 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
636 {
637 struct dentry *dentry = filp->f_path.dentry;
638 struct inode *inode = dentry->d_inode;
639 struct ctl_table_header *head = grab_header(inode);
640 struct ctl_table_header *h = NULL;
641 struct ctl_table *entry;
642 struct ctl_dir *ctl_dir;
643 unsigned long pos;
644 int ret = -EINVAL;
645
646 if (IS_ERR(head))
647 return PTR_ERR(head);
648
649 ctl_dir = container_of(head, struct ctl_dir, header);
650
651 ret = 0;
652 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
653 if (filp->f_pos == 0) {
654 if (filldir(dirent, ".", 1, filp->f_pos,
655 inode->i_ino, DT_DIR) < 0)
656 goto out;
657 filp->f_pos++;
658 }
659 if (filp->f_pos == 1) {
660 if (filldir(dirent, "..", 2, filp->f_pos,
661 parent_ino(dentry), DT_DIR) < 0)
662 goto out;
663 filp->f_pos++;
664 }
665 pos = 2;
666
667 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
668 ret = scan(h, entry, &pos, filp, dirent, filldir);
669 if (ret) {
670 sysctl_head_finish(h);
671 break;
672 }
673 }
674 ret = 1;
675 out:
676 sysctl_head_finish(head);
677 return ret;
678 }
679
680 static int proc_sys_permission(struct inode *inode, int mask)
681 {
682 /*
683 * sysctl entries that are not writeable,
684 * are _NOT_ writeable, capabilities or not.
685 */
686 struct ctl_table_header *head;
687 struct ctl_table *table;
688 int error;
689
690 /* Executable files are not allowed under /proc/sys/ */
691 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
692 return -EACCES;
693
694 head = grab_header(inode);
695 if (IS_ERR(head))
696 return PTR_ERR(head);
697
698 table = PROC_I(inode)->sysctl_entry;
699 if (!table) /* global root - r-xr-xr-x */
700 error = mask & MAY_WRITE ? -EACCES : 0;
701 else /* Use the permissions on the sysctl table entry */
702 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
703
704 sysctl_head_finish(head);
705 return error;
706 }
707
708 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
709 {
710 struct inode *inode = dentry->d_inode;
711 int error;
712
713 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
714 return -EPERM;
715
716 error = inode_change_ok(inode, attr);
717 if (error)
718 return error;
719
720 if ((attr->ia_valid & ATTR_SIZE) &&
721 attr->ia_size != i_size_read(inode)) {
722 error = vmtruncate(inode, attr->ia_size);
723 if (error)
724 return error;
725 }
726
727 setattr_copy(inode, attr);
728 mark_inode_dirty(inode);
729 return 0;
730 }
731
732 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
733 {
734 struct inode *inode = dentry->d_inode;
735 struct ctl_table_header *head = grab_header(inode);
736 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
737
738 if (IS_ERR(head))
739 return PTR_ERR(head);
740
741 generic_fillattr(inode, stat);
742 if (table)
743 stat->mode = (stat->mode & S_IFMT) | table->mode;
744
745 sysctl_head_finish(head);
746 return 0;
747 }
748
749 static const struct file_operations proc_sys_file_operations = {
750 .open = proc_sys_open,
751 .poll = proc_sys_poll,
752 .read = proc_sys_read,
753 .write = proc_sys_write,
754 .llseek = default_llseek,
755 };
756
757 static const struct file_operations proc_sys_dir_file_operations = {
758 .read = generic_read_dir,
759 .readdir = proc_sys_readdir,
760 .llseek = generic_file_llseek,
761 };
762
763 static const struct inode_operations proc_sys_inode_operations = {
764 .permission = proc_sys_permission,
765 .setattr = proc_sys_setattr,
766 .getattr = proc_sys_getattr,
767 };
768
769 static const struct inode_operations proc_sys_dir_operations = {
770 .lookup = proc_sys_lookup,
771 .permission = proc_sys_permission,
772 .setattr = proc_sys_setattr,
773 .getattr = proc_sys_getattr,
774 };
775
776 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
777 {
778 if (nd->flags & LOOKUP_RCU)
779 return -ECHILD;
780 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
781 }
782
783 static int proc_sys_delete(const struct dentry *dentry)
784 {
785 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
786 }
787
788 static int sysctl_is_seen(struct ctl_table_header *p)
789 {
790 struct ctl_table_set *set = p->set;
791 int res;
792 spin_lock(&sysctl_lock);
793 if (p->unregistering)
794 res = 0;
795 else if (!set->is_seen)
796 res = 1;
797 else
798 res = set->is_seen(set);
799 spin_unlock(&sysctl_lock);
800 return res;
801 }
802
803 static int proc_sys_compare(const struct dentry *parent,
804 const struct inode *pinode,
805 const struct dentry *dentry, const struct inode *inode,
806 unsigned int len, const char *str, const struct qstr *name)
807 {
808 struct ctl_table_header *head;
809 /* Although proc doesn't have negative dentries, rcu-walk means
810 * that inode here can be NULL */
811 /* AV: can it, indeed? */
812 if (!inode)
813 return 1;
814 if (name->len != len)
815 return 1;
816 if (memcmp(name->name, str, len))
817 return 1;
818 head = rcu_dereference(PROC_I(inode)->sysctl);
819 return !head || !sysctl_is_seen(head);
820 }
821
822 static const struct dentry_operations proc_sys_dentry_operations = {
823 .d_revalidate = proc_sys_revalidate,
824 .d_delete = proc_sys_delete,
825 .d_compare = proc_sys_compare,
826 };
827
828 static struct ctl_dir *find_subdir(struct ctl_dir *dir,
829 const char *name, int namelen)
830 {
831 struct ctl_table_header *head;
832 struct ctl_table *entry;
833
834 entry = find_entry(&head, dir, name, namelen);
835 if (!entry)
836 return ERR_PTR(-ENOENT);
837 if (!S_ISDIR(entry->mode))
838 return ERR_PTR(-ENOTDIR);
839 return container_of(head, struct ctl_dir, header);
840 }
841
842 static struct ctl_dir *new_dir(struct ctl_table_set *set,
843 const char *name, int namelen)
844 {
845 struct ctl_table *table;
846 struct ctl_dir *new;
847 struct ctl_node *node;
848 char *new_name;
849
850 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
851 sizeof(struct ctl_table)*2 + namelen + 1,
852 GFP_KERNEL);
853 if (!new)
854 return NULL;
855
856 node = (struct ctl_node *)(new + 1);
857 table = (struct ctl_table *)(node + 1);
858 new_name = (char *)(table + 2);
859 memcpy(new_name, name, namelen);
860 new_name[namelen] = '\0';
861 table[0].procname = new_name;
862 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
863 init_header(&new->header, set->dir.header.root, set, node, table);
864
865 return new;
866 }
867
868 /**
869 * get_subdir - find or create a subdir with the specified name.
870 * @dir: Directory to create the subdirectory in
871 * @name: The name of the subdirectory to find or create
872 * @namelen: The length of name
873 *
874 * Takes a directory with an elevated reference count so we know that
875 * if we drop the lock the directory will not go away. Upon success
876 * the reference is moved from @dir to the returned subdirectory.
877 * Upon error an error code is returned and the reference on @dir is
878 * simply dropped.
879 */
880 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
881 const char *name, int namelen)
882 {
883 struct ctl_table_set *set = dir->header.set;
884 struct ctl_dir *subdir, *new = NULL;
885 int err;
886
887 spin_lock(&sysctl_lock);
888 subdir = find_subdir(dir, name, namelen);
889 if (!IS_ERR(subdir))
890 goto found;
891 if (PTR_ERR(subdir) != -ENOENT)
892 goto failed;
893
894 spin_unlock(&sysctl_lock);
895 new = new_dir(set, name, namelen);
896 spin_lock(&sysctl_lock);
897 subdir = ERR_PTR(-ENOMEM);
898 if (!new)
899 goto failed;
900
901 /* Was the subdir added while we dropped the lock? */
902 subdir = find_subdir(dir, name, namelen);
903 if (!IS_ERR(subdir))
904 goto found;
905 if (PTR_ERR(subdir) != -ENOENT)
906 goto failed;
907
908 /* Nope. Use the our freshly made directory entry. */
909 err = insert_header(dir, &new->header);
910 subdir = ERR_PTR(err);
911 if (err)
912 goto failed;
913 subdir = new;
914 found:
915 subdir->header.nreg++;
916 failed:
917 if (unlikely(IS_ERR(subdir))) {
918 printk(KERN_ERR "sysctl could not get directory: ");
919 sysctl_print_dir(dir);
920 printk(KERN_CONT "/%*.*s %ld\n",
921 namelen, namelen, name, PTR_ERR(subdir));
922 }
923 drop_sysctl_table(&dir->header);
924 if (new)
925 drop_sysctl_table(&new->header);
926 spin_unlock(&sysctl_lock);
927 return subdir;
928 }
929
930 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
931 {
932 struct ctl_dir *parent;
933 const char *procname;
934 if (!dir->header.parent)
935 return &set->dir;
936 parent = xlate_dir(set, dir->header.parent);
937 if (IS_ERR(parent))
938 return parent;
939 procname = dir->header.ctl_table[0].procname;
940 return find_subdir(parent, procname, strlen(procname));
941 }
942
943 static int sysctl_follow_link(struct ctl_table_header **phead,
944 struct ctl_table **pentry, struct nsproxy *namespaces)
945 {
946 struct ctl_table_header *head;
947 struct ctl_table_root *root;
948 struct ctl_table_set *set;
949 struct ctl_table *entry;
950 struct ctl_dir *dir;
951 int ret;
952
953 /* Get out quickly if not a link */
954 if (!S_ISLNK((*pentry)->mode))
955 return 0;
956
957 ret = 0;
958 spin_lock(&sysctl_lock);
959 root = (*pentry)->data;
960 set = lookup_header_set(root, namespaces);
961 dir = xlate_dir(set, (*phead)->parent);
962 if (IS_ERR(dir))
963 ret = PTR_ERR(dir);
964 else {
965 const char *procname = (*pentry)->procname;
966 head = NULL;
967 entry = find_entry(&head, dir, procname, strlen(procname));
968 ret = -ENOENT;
969 if (entry && use_table(head)) {
970 unuse_table(*phead);
971 *phead = head;
972 *pentry = entry;
973 ret = 0;
974 }
975 }
976
977 spin_unlock(&sysctl_lock);
978 return ret;
979 }
980
981 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
982 {
983 struct va_format vaf;
984 va_list args;
985
986 va_start(args, fmt);
987 vaf.fmt = fmt;
988 vaf.va = &args;
989
990 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
991 path, table->procname, &vaf);
992
993 va_end(args);
994 return -EINVAL;
995 }
996
997 static int sysctl_check_table(const char *path, struct ctl_table *table)
998 {
999 int err = 0;
1000 for (; table->procname; table++) {
1001 if (table->child)
1002 err = sysctl_err(path, table, "Not a file");
1003
1004 if ((table->proc_handler == proc_dostring) ||
1005 (table->proc_handler == proc_dointvec) ||
1006 (table->proc_handler == proc_dointvec_minmax) ||
1007 (table->proc_handler == proc_dointvec_jiffies) ||
1008 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
1009 (table->proc_handler == proc_dointvec_ms_jiffies) ||
1010 (table->proc_handler == proc_doulongvec_minmax) ||
1011 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1012 if (!table->data)
1013 err = sysctl_err(path, table, "No data");
1014 if (!table->maxlen)
1015 err = sysctl_err(path, table, "No maxlen");
1016 }
1017 if (!table->proc_handler)
1018 err = sysctl_err(path, table, "No proc_handler");
1019
1020 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1021 err = sysctl_err(path, table, "bogus .mode 0%o",
1022 table->mode);
1023 }
1024 return err;
1025 }
1026
1027 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1028 struct ctl_table_root *link_root)
1029 {
1030 struct ctl_table *link_table, *entry, *link;
1031 struct ctl_table_header *links;
1032 struct ctl_node *node;
1033 char *link_name;
1034 int nr_entries, name_bytes;
1035
1036 name_bytes = 0;
1037 nr_entries = 0;
1038 for (entry = table; entry->procname; entry++) {
1039 nr_entries++;
1040 name_bytes += strlen(entry->procname) + 1;
1041 }
1042
1043 links = kzalloc(sizeof(struct ctl_table_header) +
1044 sizeof(struct ctl_node)*nr_entries +
1045 sizeof(struct ctl_table)*(nr_entries + 1) +
1046 name_bytes,
1047 GFP_KERNEL);
1048
1049 if (!links)
1050 return NULL;
1051
1052 node = (struct ctl_node *)(links + 1);
1053 link_table = (struct ctl_table *)(node + nr_entries);
1054 link_name = (char *)&link_table[nr_entries + 1];
1055
1056 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1057 int len = strlen(entry->procname) + 1;
1058 memcpy(link_name, entry->procname, len);
1059 link->procname = link_name;
1060 link->mode = S_IFLNK|S_IRWXUGO;
1061 link->data = link_root;
1062 link_name += len;
1063 }
1064 init_header(links, dir->header.root, dir->header.set, node, link_table);
1065 links->nreg = nr_entries;
1066
1067 return links;
1068 }
1069
1070 static bool get_links(struct ctl_dir *dir,
1071 struct ctl_table *table, struct ctl_table_root *link_root)
1072 {
1073 struct ctl_table_header *head;
1074 struct ctl_table *entry, *link;
1075
1076 /* Are there links available for every entry in table? */
1077 for (entry = table; entry->procname; entry++) {
1078 const char *procname = entry->procname;
1079 link = find_entry(&head, dir, procname, strlen(procname));
1080 if (!link)
1081 return false;
1082 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1083 continue;
1084 if (S_ISLNK(link->mode) && (link->data == link_root))
1085 continue;
1086 return false;
1087 }
1088
1089 /* The checks passed. Increase the registration count on the links */
1090 for (entry = table; entry->procname; entry++) {
1091 const char *procname = entry->procname;
1092 link = find_entry(&head, dir, procname, strlen(procname));
1093 head->nreg++;
1094 }
1095 return true;
1096 }
1097
1098 static int insert_links(struct ctl_table_header *head)
1099 {
1100 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1101 struct ctl_dir *core_parent = NULL;
1102 struct ctl_table_header *links;
1103 int err;
1104
1105 if (head->set == root_set)
1106 return 0;
1107
1108 core_parent = xlate_dir(root_set, head->parent);
1109 if (IS_ERR(core_parent))
1110 return 0;
1111
1112 if (get_links(core_parent, head->ctl_table, head->root))
1113 return 0;
1114
1115 core_parent->header.nreg++;
1116 spin_unlock(&sysctl_lock);
1117
1118 links = new_links(core_parent, head->ctl_table, head->root);
1119
1120 spin_lock(&sysctl_lock);
1121 err = -ENOMEM;
1122 if (!links)
1123 goto out;
1124
1125 err = 0;
1126 if (get_links(core_parent, head->ctl_table, head->root)) {
1127 kfree(links);
1128 goto out;
1129 }
1130
1131 err = insert_header(core_parent, links);
1132 if (err)
1133 kfree(links);
1134 out:
1135 drop_sysctl_table(&core_parent->header);
1136 return err;
1137 }
1138
1139 /**
1140 * __register_sysctl_table - register a leaf sysctl table
1141 * @set: Sysctl tree to register on
1142 * @path: The path to the directory the sysctl table is in.
1143 * @table: the top-level table structure
1144 *
1145 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1146 * array. A completely 0 filled entry terminates the table.
1147 *
1148 * The members of the &struct ctl_table structure are used as follows:
1149 *
1150 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1151 * enter a sysctl file
1152 *
1153 * data - a pointer to data for use by proc_handler
1154 *
1155 * maxlen - the maximum size in bytes of the data
1156 *
1157 * mode - the file permissions for the /proc/sys file
1158 *
1159 * child - must be %NULL.
1160 *
1161 * proc_handler - the text handler routine (described below)
1162 *
1163 * extra1, extra2 - extra pointers usable by the proc handler routines
1164 *
1165 * Leaf nodes in the sysctl tree will be represented by a single file
1166 * under /proc; non-leaf nodes will be represented by directories.
1167 *
1168 * There must be a proc_handler routine for any terminal nodes.
1169 * Several default handlers are available to cover common cases -
1170 *
1171 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1172 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1173 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1174 *
1175 * It is the handler's job to read the input buffer from user memory
1176 * and process it. The handler should return 0 on success.
1177 *
1178 * This routine returns %NULL on a failure to register, and a pointer
1179 * to the table header on success.
1180 */
1181 struct ctl_table_header *__register_sysctl_table(
1182 struct ctl_table_set *set,
1183 const char *path, struct ctl_table *table)
1184 {
1185 struct ctl_table_root *root = set->dir.header.root;
1186 struct ctl_table_header *header;
1187 const char *name, *nextname;
1188 struct ctl_dir *dir;
1189 struct ctl_table *entry;
1190 struct ctl_node *node;
1191 int nr_entries = 0;
1192
1193 for (entry = table; entry->procname; entry++)
1194 nr_entries++;
1195
1196 header = kzalloc(sizeof(struct ctl_table_header) +
1197 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
1198 if (!header)
1199 return NULL;
1200
1201 node = (struct ctl_node *)(header + 1);
1202 init_header(header, root, set, node, table);
1203 if (sysctl_check_table(path, table))
1204 goto fail;
1205
1206 spin_lock(&sysctl_lock);
1207 dir = &set->dir;
1208 /* Reference moved down the diretory tree get_subdir */
1209 dir->header.nreg++;
1210 spin_unlock(&sysctl_lock);
1211
1212 /* Find the directory for the ctl_table */
1213 for (name = path; name; name = nextname) {
1214 int namelen;
1215 nextname = strchr(name, '/');
1216 if (nextname) {
1217 namelen = nextname - name;
1218 nextname++;
1219 } else {
1220 namelen = strlen(name);
1221 }
1222 if (namelen == 0)
1223 continue;
1224
1225 dir = get_subdir(dir, name, namelen);
1226 if (IS_ERR(dir))
1227 goto fail;
1228 }
1229
1230 spin_lock(&sysctl_lock);
1231 if (insert_header(dir, header))
1232 goto fail_put_dir_locked;
1233
1234 drop_sysctl_table(&dir->header);
1235 spin_unlock(&sysctl_lock);
1236
1237 return header;
1238
1239 fail_put_dir_locked:
1240 drop_sysctl_table(&dir->header);
1241 spin_unlock(&sysctl_lock);
1242 fail:
1243 kfree(header);
1244 dump_stack();
1245 return NULL;
1246 }
1247
1248 /**
1249 * register_sysctl - register a sysctl table
1250 * @path: The path to the directory the sysctl table is in.
1251 * @table: the table structure
1252 *
1253 * Register a sysctl table. @table should be a filled in ctl_table
1254 * array. A completely 0 filled entry terminates the table.
1255 *
1256 * See __register_sysctl_table for more details.
1257 */
1258 struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1259 {
1260 return __register_sysctl_table(&sysctl_table_root.default_set,
1261 path, table);
1262 }
1263 EXPORT_SYMBOL(register_sysctl);
1264
1265 static char *append_path(const char *path, char *pos, const char *name)
1266 {
1267 int namelen;
1268 namelen = strlen(name);
1269 if (((pos - path) + namelen + 2) >= PATH_MAX)
1270 return NULL;
1271 memcpy(pos, name, namelen);
1272 pos[namelen] = '/';
1273 pos[namelen + 1] = '\0';
1274 pos += namelen + 1;
1275 return pos;
1276 }
1277
1278 static int count_subheaders(struct ctl_table *table)
1279 {
1280 int has_files = 0;
1281 int nr_subheaders = 0;
1282 struct ctl_table *entry;
1283
1284 /* special case: no directory and empty directory */
1285 if (!table || !table->procname)
1286 return 1;
1287
1288 for (entry = table; entry->procname; entry++) {
1289 if (entry->child)
1290 nr_subheaders += count_subheaders(entry->child);
1291 else
1292 has_files = 1;
1293 }
1294 return nr_subheaders + has_files;
1295 }
1296
1297 static int register_leaf_sysctl_tables(const char *path, char *pos,
1298 struct ctl_table_header ***subheader, struct ctl_table_set *set,
1299 struct ctl_table *table)
1300 {
1301 struct ctl_table *ctl_table_arg = NULL;
1302 struct ctl_table *entry, *files;
1303 int nr_files = 0;
1304 int nr_dirs = 0;
1305 int err = -ENOMEM;
1306
1307 for (entry = table; entry->procname; entry++) {
1308 if (entry->child)
1309 nr_dirs++;
1310 else
1311 nr_files++;
1312 }
1313
1314 files = table;
1315 /* If there are mixed files and directories we need a new table */
1316 if (nr_dirs && nr_files) {
1317 struct ctl_table *new;
1318 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1319 GFP_KERNEL);
1320 if (!files)
1321 goto out;
1322
1323 ctl_table_arg = files;
1324 for (new = files, entry = table; entry->procname; entry++) {
1325 if (entry->child)
1326 continue;
1327 *new = *entry;
1328 new++;
1329 }
1330 }
1331
1332 /* Register everything except a directory full of subdirectories */
1333 if (nr_files || !nr_dirs) {
1334 struct ctl_table_header *header;
1335 header = __register_sysctl_table(set, path, files);
1336 if (!header) {
1337 kfree(ctl_table_arg);
1338 goto out;
1339 }
1340
1341 /* Remember if we need to free the file table */
1342 header->ctl_table_arg = ctl_table_arg;
1343 **subheader = header;
1344 (*subheader)++;
1345 }
1346
1347 /* Recurse into the subdirectories. */
1348 for (entry = table; entry->procname; entry++) {
1349 char *child_pos;
1350
1351 if (!entry->child)
1352 continue;
1353
1354 err = -ENAMETOOLONG;
1355 child_pos = append_path(path, pos, entry->procname);
1356 if (!child_pos)
1357 goto out;
1358
1359 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1360 set, entry->child);
1361 pos[0] = '\0';
1362 if (err)
1363 goto out;
1364 }
1365 err = 0;
1366 out:
1367 /* On failure our caller will unregister all registered subheaders */
1368 return err;
1369 }
1370
1371 /**
1372 * __register_sysctl_paths - register a sysctl table hierarchy
1373 * @set: Sysctl tree to register on
1374 * @path: The path to the directory the sysctl table is in.
1375 * @table: the top-level table structure
1376 *
1377 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1378 * array. A completely 0 filled entry terminates the table.
1379 *
1380 * See __register_sysctl_table for more details.
1381 */
1382 struct ctl_table_header *__register_sysctl_paths(
1383 struct ctl_table_set *set,
1384 const struct ctl_path *path, struct ctl_table *table)
1385 {
1386 struct ctl_table *ctl_table_arg = table;
1387 int nr_subheaders = count_subheaders(table);
1388 struct ctl_table_header *header = NULL, **subheaders, **subheader;
1389 const struct ctl_path *component;
1390 char *new_path, *pos;
1391
1392 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1393 if (!new_path)
1394 return NULL;
1395
1396 pos[0] = '\0';
1397 for (component = path; component->procname; component++) {
1398 pos = append_path(new_path, pos, component->procname);
1399 if (!pos)
1400 goto out;
1401 }
1402 while (table->procname && table->child && !table[1].procname) {
1403 pos = append_path(new_path, pos, table->procname);
1404 if (!pos)
1405 goto out;
1406 table = table->child;
1407 }
1408 if (nr_subheaders == 1) {
1409 header = __register_sysctl_table(set, new_path, table);
1410 if (header)
1411 header->ctl_table_arg = ctl_table_arg;
1412 } else {
1413 header = kzalloc(sizeof(*header) +
1414 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1415 if (!header)
1416 goto out;
1417
1418 subheaders = (struct ctl_table_header **) (header + 1);
1419 subheader = subheaders;
1420 header->ctl_table_arg = ctl_table_arg;
1421
1422 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1423 set, table))
1424 goto err_register_leaves;
1425 }
1426
1427 out:
1428 kfree(new_path);
1429 return header;
1430
1431 err_register_leaves:
1432 while (subheader > subheaders) {
1433 struct ctl_table_header *subh = *(--subheader);
1434 struct ctl_table *table = subh->ctl_table_arg;
1435 unregister_sysctl_table(subh);
1436 kfree(table);
1437 }
1438 kfree(header);
1439 header = NULL;
1440 goto out;
1441 }
1442
1443 /**
1444 * register_sysctl_table_path - register a sysctl table hierarchy
1445 * @path: The path to the directory the sysctl table is in.
1446 * @table: the top-level table structure
1447 *
1448 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1449 * array. A completely 0 filled entry terminates the table.
1450 *
1451 * See __register_sysctl_paths for more details.
1452 */
1453 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1454 struct ctl_table *table)
1455 {
1456 return __register_sysctl_paths(&sysctl_table_root.default_set,
1457 path, table);
1458 }
1459 EXPORT_SYMBOL(register_sysctl_paths);
1460
1461 /**
1462 * register_sysctl_table - register a sysctl table hierarchy
1463 * @table: the top-level table structure
1464 *
1465 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1466 * array. A completely 0 filled entry terminates the table.
1467 *
1468 * See register_sysctl_paths for more details.
1469 */
1470 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1471 {
1472 static const struct ctl_path null_path[] = { {} };
1473
1474 return register_sysctl_paths(null_path, table);
1475 }
1476 EXPORT_SYMBOL(register_sysctl_table);
1477
1478 static void put_links(struct ctl_table_header *header)
1479 {
1480 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1481 struct ctl_table_root *root = header->root;
1482 struct ctl_dir *parent = header->parent;
1483 struct ctl_dir *core_parent;
1484 struct ctl_table *entry;
1485
1486 if (header->set == root_set)
1487 return;
1488
1489 core_parent = xlate_dir(root_set, parent);
1490 if (IS_ERR(core_parent))
1491 return;
1492
1493 for (entry = header->ctl_table; entry->procname; entry++) {
1494 struct ctl_table_header *link_head;
1495 struct ctl_table *link;
1496 const char *name = entry->procname;
1497
1498 link = find_entry(&link_head, core_parent, name, strlen(name));
1499 if (link &&
1500 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1501 (S_ISLNK(link->mode) && (link->data == root)))) {
1502 drop_sysctl_table(link_head);
1503 }
1504 else {
1505 printk(KERN_ERR "sysctl link missing during unregister: ");
1506 sysctl_print_dir(parent);
1507 printk(KERN_CONT "/%s\n", name);
1508 }
1509 }
1510 }
1511
1512 static void drop_sysctl_table(struct ctl_table_header *header)
1513 {
1514 struct ctl_dir *parent = header->parent;
1515
1516 if (--header->nreg)
1517 return;
1518
1519 put_links(header);
1520 start_unregistering(header);
1521 if (!--header->count)
1522 kfree_rcu(header, rcu);
1523
1524 if (parent)
1525 drop_sysctl_table(&parent->header);
1526 }
1527
1528 /**
1529 * unregister_sysctl_table - unregister a sysctl table hierarchy
1530 * @header: the header returned from register_sysctl_table
1531 *
1532 * Unregisters the sysctl table and all children. proc entries may not
1533 * actually be removed until they are no longer used by anyone.
1534 */
1535 void unregister_sysctl_table(struct ctl_table_header * header)
1536 {
1537 int nr_subheaders;
1538 might_sleep();
1539
1540 if (header == NULL)
1541 return;
1542
1543 nr_subheaders = count_subheaders(header->ctl_table_arg);
1544 if (unlikely(nr_subheaders > 1)) {
1545 struct ctl_table_header **subheaders;
1546 int i;
1547
1548 subheaders = (struct ctl_table_header **)(header + 1);
1549 for (i = nr_subheaders -1; i >= 0; i--) {
1550 struct ctl_table_header *subh = subheaders[i];
1551 struct ctl_table *table = subh->ctl_table_arg;
1552 unregister_sysctl_table(subh);
1553 kfree(table);
1554 }
1555 kfree(header);
1556 return;
1557 }
1558
1559 spin_lock(&sysctl_lock);
1560 drop_sysctl_table(header);
1561 spin_unlock(&sysctl_lock);
1562 }
1563 EXPORT_SYMBOL(unregister_sysctl_table);
1564
1565 void setup_sysctl_set(struct ctl_table_set *set,
1566 struct ctl_table_root *root,
1567 int (*is_seen)(struct ctl_table_set *))
1568 {
1569 memset(set, 0, sizeof(*set));
1570 set->is_seen = is_seen;
1571 init_header(&set->dir.header, root, set, NULL, root_table);
1572 }
1573
1574 void retire_sysctl_set(struct ctl_table_set *set)
1575 {
1576 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
1577 }
1578
1579 int __init proc_sys_init(void)
1580 {
1581 struct proc_dir_entry *proc_sys_root;
1582
1583 proc_sys_root = proc_mkdir("sys", NULL);
1584 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1585 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
1586 proc_sys_root->nlink = 0;
1587
1588 return sysctl_init();
1589 }