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