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