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