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