]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - fs/proc/proc_sysctl.c
sysctl: Remove the now unused ctl_table parent field.
[mirror_ubuntu-disco-kernel.git] / fs / proc / proc_sysctl.c
CommitLineData
77b14db5
EB
1/*
2 * /proc/sys support
3 */
1e0edd3f 4#include <linux/init.h>
77b14db5 5#include <linux/sysctl.h>
f1ecf068 6#include <linux/poll.h>
77b14db5
EB
7#include <linux/proc_fs.h>
8#include <linux/security.h>
34286d66 9#include <linux/namei.h>
1f87f0b5 10#include <linux/module.h>
77b14db5
EB
11#include "internal.h"
12
d72f71eb 13static const struct dentry_operations proc_sys_dentry_operations;
77b14db5 14static const struct file_operations proc_sys_file_operations;
03a44825 15static const struct inode_operations proc_sys_inode_operations;
9043476f
AV
16static const struct file_operations proc_sys_dir_file_operations;
17static const struct inode_operations proc_sys_dir_operations;
77b14db5 18
f1ecf068
LDM
19void 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
1f87f0b5
EB
28static struct ctl_table root_table[1];
29static struct ctl_table_root sysctl_table_root;
30static struct ctl_table_header root_table_header = {
31 {{.count = 1,
32 .ctl_table = root_table,
33 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
34 .root = &sysctl_table_root,
35 .set = &sysctl_table_root.default_set,
36};
37static struct ctl_table_root sysctl_table_root = {
38 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
39 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
40};
41
42static DEFINE_SPINLOCK(sysctl_lock);
43
44/* called under sysctl_lock */
45static int use_table(struct ctl_table_header *p)
46{
47 if (unlikely(p->unregistering))
48 return 0;
49 p->used++;
50 return 1;
51}
52
53/* called under sysctl_lock */
54static void unuse_table(struct ctl_table_header *p)
55{
56 if (!--p->used)
57 if (unlikely(p->unregistering))
58 complete(p->unregistering);
59}
60
61/* called under sysctl_lock, will reacquire if has to wait */
62static void start_unregistering(struct ctl_table_header *p)
63{
64 /*
65 * if p->used is 0, nobody will ever touch that entry again;
66 * we'll eliminate all paths to it before dropping sysctl_lock
67 */
68 if (unlikely(p->used)) {
69 struct completion wait;
70 init_completion(&wait);
71 p->unregistering = &wait;
72 spin_unlock(&sysctl_lock);
73 wait_for_completion(&wait);
74 spin_lock(&sysctl_lock);
75 } else {
76 /* anything non-NULL; we'll never dereference it */
77 p->unregistering = ERR_PTR(-EINVAL);
78 }
79 /*
80 * do not remove from the list until nobody holds it; walking the
81 * list in do_sysctl() relies on that.
82 */
83 list_del_init(&p->ctl_entry);
84}
85
86static void sysctl_head_get(struct ctl_table_header *head)
87{
88 spin_lock(&sysctl_lock);
89 head->count++;
90 spin_unlock(&sysctl_lock);
91}
92
93void sysctl_head_put(struct ctl_table_header *head)
94{
95 spin_lock(&sysctl_lock);
96 if (!--head->count)
97 kfree_rcu(head, rcu);
98 spin_unlock(&sysctl_lock);
99}
100
101static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
102{
103 if (!head)
104 BUG();
105 spin_lock(&sysctl_lock);
106 if (!use_table(head))
107 head = ERR_PTR(-ENOENT);
108 spin_unlock(&sysctl_lock);
109 return head;
110}
111
112static void sysctl_head_finish(struct ctl_table_header *head)
113{
114 if (!head)
115 return;
116 spin_lock(&sysctl_lock);
117 unuse_table(head);
118 spin_unlock(&sysctl_lock);
119}
120
121static struct ctl_table_set *
122lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
123{
124 struct ctl_table_set *set = &root->default_set;
125 if (root->lookup)
126 set = root->lookup(root, namespaces);
127 return set;
128}
129
130static struct list_head *
131lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
132{
133 struct ctl_table_set *set = lookup_header_set(root, namespaces);
134 return &set->list;
135}
136
137static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
138 struct ctl_table_header *prev)
139{
140 struct ctl_table_root *root;
141 struct list_head *header_list;
142 struct ctl_table_header *head;
143 struct list_head *tmp;
144
145 spin_lock(&sysctl_lock);
146 if (prev) {
147 head = prev;
148 tmp = &prev->ctl_entry;
149 unuse_table(prev);
150 goto next;
151 }
152 tmp = &root_table_header.ctl_entry;
153 for (;;) {
154 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
155
156 if (!use_table(head))
157 goto next;
158 spin_unlock(&sysctl_lock);
159 return head;
160 next:
161 root = head->root;
162 tmp = tmp->next;
163 header_list = lookup_header_list(root, namespaces);
164 if (tmp != header_list)
165 continue;
166
167 do {
168 root = list_entry(root->root_list.next,
169 struct ctl_table_root, root_list);
170 if (root == &sysctl_table_root)
171 goto out;
172 header_list = lookup_header_list(root, namespaces);
173 } while (list_empty(header_list));
174 tmp = header_list->next;
175 }
176out:
177 spin_unlock(&sysctl_lock);
178 return NULL;
179}
180
181static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
182{
183 return __sysctl_head_next(current->nsproxy, prev);
184}
185
186void register_sysctl_root(struct ctl_table_root *root)
187{
188 spin_lock(&sysctl_lock);
189 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
190 spin_unlock(&sysctl_lock);
191}
192
193/*
194 * sysctl_perm does NOT grant the superuser all rights automatically, because
195 * some sysctl variables are readonly even to root.
196 */
197
198static int test_perm(int mode, int op)
199{
200 if (!current_euid())
201 mode >>= 6;
202 else if (in_egroup_p(0))
203 mode >>= 3;
204 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
205 return 0;
206 return -EACCES;
207}
208
209static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
210{
211 int mode;
212
213 if (root->permissions)
214 mode = root->permissions(root, current->nsproxy, table);
215 else
216 mode = table->mode;
217
218 return test_perm(mode, op);
219}
220
9043476f
AV
221static struct inode *proc_sys_make_inode(struct super_block *sb,
222 struct ctl_table_header *head, struct ctl_table *table)
77b14db5
EB
223{
224 struct inode *inode;
9043476f 225 struct proc_inode *ei;
77b14db5 226
9043476f 227 inode = new_inode(sb);
77b14db5
EB
228 if (!inode)
229 goto out;
230
85fe4025
CH
231 inode->i_ino = get_next_ino();
232
9043476f 233 sysctl_head_get(head);
77b14db5 234 ei = PROC_I(inode);
9043476f
AV
235 ei->sysctl = head;
236 ei->sysctl_entry = table;
237
77b14db5 238 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
9043476f
AV
239 inode->i_mode = table->mode;
240 if (!table->child) {
241 inode->i_mode |= S_IFREG;
242 inode->i_op = &proc_sys_inode_operations;
243 inode->i_fop = &proc_sys_file_operations;
244 } else {
245 inode->i_mode |= S_IFDIR;
9043476f
AV
246 inode->i_op = &proc_sys_dir_operations;
247 inode->i_fop = &proc_sys_dir_file_operations;
248 }
77b14db5
EB
249out:
250 return inode;
251}
252
9043476f 253static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
77b14db5 254{
2315ffa0 255 for ( ; p->procname; p++) {
36885d7b 256 if (strlen(p->procname) != name->len)
77b14db5
EB
257 continue;
258
36885d7b 259 if (memcmp(p->procname, name->name, name->len) != 0)
77b14db5
EB
260 continue;
261
262 /* I have a match */
9043476f 263 return p;
77b14db5
EB
264 }
265 return NULL;
266}
267
81324364 268static struct ctl_table_header *grab_header(struct inode *inode)
77b14db5 269{
9043476f
AV
270 if (PROC_I(inode)->sysctl)
271 return sysctl_head_grab(PROC_I(inode)->sysctl);
272 else
273 return sysctl_head_next(NULL);
274}
77b14db5 275
9043476f
AV
276static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
277 struct nameidata *nd)
278{
279 struct ctl_table_header *head = grab_header(dir);
280 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
281 struct ctl_table_header *h = NULL;
282 struct qstr *name = &dentry->d_name;
283 struct ctl_table *p;
284 struct inode *inode;
285 struct dentry *err = ERR_PTR(-ENOENT);
77b14db5 286
9043476f
AV
287 if (IS_ERR(head))
288 return ERR_CAST(head);
77b14db5 289
9043476f
AV
290 if (table && !table->child) {
291 WARN_ON(1);
292 goto out;
77b14db5 293 }
77b14db5 294
9043476f 295 table = table ? table->child : head->ctl_table;
77b14db5 296
9043476f
AV
297 p = find_in_table(table, name);
298 if (!p) {
299 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
300 if (h->attached_to != table)
301 continue;
302 p = find_in_table(h->attached_by, name);
303 if (p)
304 break;
305 }
77b14db5 306 }
77b14db5 307
9043476f 308 if (!p)
77b14db5
EB
309 goto out;
310
311 err = ERR_PTR(-ENOMEM);
9043476f
AV
312 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
313 if (h)
314 sysctl_head_finish(h);
315
77b14db5
EB
316 if (!inode)
317 goto out;
318
319 err = NULL;
fb045adb 320 d_set_d_op(dentry, &proc_sys_dentry_operations);
77b14db5
EB
321 d_add(dentry, inode);
322
323out:
324 sysctl_head_finish(head);
325 return err;
326}
327
7708bfb1
PE
328static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
329 size_t count, loff_t *ppos, int write)
77b14db5 330{
9043476f
AV
331 struct inode *inode = filp->f_path.dentry->d_inode;
332 struct ctl_table_header *head = grab_header(inode);
333 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2a2da53b
DH
334 ssize_t error;
335 size_t res;
77b14db5 336
9043476f
AV
337 if (IS_ERR(head))
338 return PTR_ERR(head);
77b14db5
EB
339
340 /*
341 * At this point we know that the sysctl was not unregistered
342 * and won't be until we finish.
343 */
344 error = -EPERM;
d7321cd6 345 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
77b14db5
EB
346 goto out;
347
9043476f
AV
348 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
349 error = -EINVAL;
350 if (!table->proc_handler)
351 goto out;
352
77b14db5
EB
353 /* careful: calling conventions are nasty here */
354 res = count;
8d65af78 355 error = table->proc_handler(table, write, buf, &res, ppos);
77b14db5
EB
356 if (!error)
357 error = res;
358out:
359 sysctl_head_finish(head);
360
361 return error;
362}
363
7708bfb1 364static ssize_t proc_sys_read(struct file *filp, char __user *buf,
77b14db5
EB
365 size_t count, loff_t *ppos)
366{
7708bfb1
PE
367 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
368}
77b14db5 369
7708bfb1
PE
370static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
371 size_t count, loff_t *ppos)
372{
373 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
77b14db5
EB
374}
375
f1ecf068
LDM
376static int proc_sys_open(struct inode *inode, struct file *filp)
377{
378 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
379
380 if (table->poll)
381 filp->private_data = proc_sys_poll_event(table->poll);
382
383 return 0;
384}
385
386static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
387{
388 struct inode *inode = filp->f_path.dentry->d_inode;
389 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
390 unsigned long event = (unsigned long)filp->private_data;
391 unsigned int ret = DEFAULT_POLLMASK;
392
393 if (!table->proc_handler)
394 goto out;
395
396 if (!table->poll)
397 goto out;
398
399 poll_wait(filp, &table->poll->wait, wait);
400
401 if (event != atomic_read(&table->poll->event)) {
402 filp->private_data = proc_sys_poll_event(table->poll);
403 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
404 }
405
406out:
407 return ret;
408}
77b14db5
EB
409
410static int proc_sys_fill_cache(struct file *filp, void *dirent,
9043476f
AV
411 filldir_t filldir,
412 struct ctl_table_header *head,
413 struct ctl_table *table)
77b14db5 414{
77b14db5
EB
415 struct dentry *child, *dir = filp->f_path.dentry;
416 struct inode *inode;
417 struct qstr qname;
418 ino_t ino = 0;
419 unsigned type = DT_UNKNOWN;
77b14db5
EB
420
421 qname.name = table->procname;
422 qname.len = strlen(table->procname);
423 qname.hash = full_name_hash(qname.name, qname.len);
424
77b14db5
EB
425 child = d_lookup(dir, &qname);
426 if (!child) {
9043476f
AV
427 child = d_alloc(dir, &qname);
428 if (child) {
429 inode = proc_sys_make_inode(dir->d_sb, head, table);
430 if (!inode) {
431 dput(child);
432 return -ENOMEM;
433 } else {
fb045adb 434 d_set_d_op(child, &proc_sys_dentry_operations);
9043476f 435 d_add(child, inode);
77b14db5 436 }
9043476f
AV
437 } else {
438 return -ENOMEM;
77b14db5
EB
439 }
440 }
77b14db5 441 inode = child->d_inode;
9043476f
AV
442 ino = inode->i_ino;
443 type = inode->i_mode >> 12;
77b14db5 444 dput(child);
9043476f
AV
445 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
446}
447
448static int scan(struct ctl_table_header *head, ctl_table *table,
449 unsigned long *pos, struct file *file,
450 void *dirent, filldir_t filldir)
451{
452
2315ffa0 453 for (; table->procname; table++, (*pos)++) {
9043476f
AV
454 int res;
455
9043476f
AV
456 if (*pos < file->f_pos)
457 continue;
458
459 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
460 if (res)
461 return res;
462
463 file->f_pos = *pos + 1;
464 }
465 return 0;
77b14db5
EB
466}
467
468static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
469{
9043476f 470 struct dentry *dentry = filp->f_path.dentry;
77b14db5 471 struct inode *inode = dentry->d_inode;
9043476f
AV
472 struct ctl_table_header *head = grab_header(inode);
473 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
474 struct ctl_table_header *h = NULL;
77b14db5 475 unsigned long pos;
9043476f
AV
476 int ret = -EINVAL;
477
478 if (IS_ERR(head))
479 return PTR_ERR(head);
77b14db5 480
9043476f
AV
481 if (table && !table->child) {
482 WARN_ON(1);
77b14db5 483 goto out;
9043476f
AV
484 }
485
486 table = table ? table->child : head->ctl_table;
77b14db5
EB
487
488 ret = 0;
489 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
490 if (filp->f_pos == 0) {
491 if (filldir(dirent, ".", 1, filp->f_pos,
492 inode->i_ino, DT_DIR) < 0)
493 goto out;
494 filp->f_pos++;
495 }
496 if (filp->f_pos == 1) {
497 if (filldir(dirent, "..", 2, filp->f_pos,
498 parent_ino(dentry), DT_DIR) < 0)
499 goto out;
500 filp->f_pos++;
501 }
502 pos = 2;
503
9043476f
AV
504 ret = scan(head, table, &pos, filp, dirent, filldir);
505 if (ret)
506 goto out;
77b14db5 507
9043476f
AV
508 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
509 if (h->attached_to != table)
77b14db5 510 continue;
9043476f
AV
511 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
512 if (ret) {
513 sysctl_head_finish(h);
514 break;
77b14db5
EB
515 }
516 }
517 ret = 1;
518out:
519 sysctl_head_finish(head);
520 return ret;
521}
522
10556cb2 523static int proc_sys_permission(struct inode *inode, int mask)
77b14db5
EB
524{
525 /*
526 * sysctl entries that are not writeable,
527 * are _NOT_ writeable, capabilities or not.
528 */
f696a365
MS
529 struct ctl_table_header *head;
530 struct ctl_table *table;
77b14db5
EB
531 int error;
532
f696a365
MS
533 /* Executable files are not allowed under /proc/sys/ */
534 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
535 return -EACCES;
536
537 head = grab_header(inode);
9043476f
AV
538 if (IS_ERR(head))
539 return PTR_ERR(head);
77b14db5 540
f696a365 541 table = PROC_I(inode)->sysctl_entry;
9043476f
AV
542 if (!table) /* global root - r-xr-xr-x */
543 error = mask & MAY_WRITE ? -EACCES : 0;
544 else /* Use the permissions on the sysctl table entry */
1fc0f78c 545 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
77b14db5 546
77b14db5
EB
547 sysctl_head_finish(head);
548 return error;
549}
550
551static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
552{
553 struct inode *inode = dentry->d_inode;
554 int error;
555
556 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
557 return -EPERM;
558
559 error = inode_change_ok(inode, attr);
1025774c
CH
560 if (error)
561 return error;
562
563 if ((attr->ia_valid & ATTR_SIZE) &&
564 attr->ia_size != i_size_read(inode)) {
565 error = vmtruncate(inode, attr->ia_size);
566 if (error)
567 return error;
568 }
77b14db5 569
1025774c
CH
570 setattr_copy(inode, attr);
571 mark_inode_dirty(inode);
572 return 0;
77b14db5
EB
573}
574
9043476f
AV
575static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
576{
577 struct inode *inode = dentry->d_inode;
578 struct ctl_table_header *head = grab_header(inode);
579 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
580
581 if (IS_ERR(head))
582 return PTR_ERR(head);
583
584 generic_fillattr(inode, stat);
585 if (table)
586 stat->mode = (stat->mode & S_IFMT) | table->mode;
587
588 sysctl_head_finish(head);
589 return 0;
590}
591
77b14db5 592static const struct file_operations proc_sys_file_operations = {
f1ecf068
LDM
593 .open = proc_sys_open,
594 .poll = proc_sys_poll,
77b14db5
EB
595 .read = proc_sys_read,
596 .write = proc_sys_write,
6038f373 597 .llseek = default_llseek,
9043476f
AV
598};
599
600static const struct file_operations proc_sys_dir_file_operations = {
887df078 601 .read = generic_read_dir,
77b14db5 602 .readdir = proc_sys_readdir,
3222a3e5 603 .llseek = generic_file_llseek,
77b14db5
EB
604};
605
03a44825 606static const struct inode_operations proc_sys_inode_operations = {
9043476f
AV
607 .permission = proc_sys_permission,
608 .setattr = proc_sys_setattr,
609 .getattr = proc_sys_getattr,
610};
611
612static const struct inode_operations proc_sys_dir_operations = {
77b14db5
EB
613 .lookup = proc_sys_lookup,
614 .permission = proc_sys_permission,
615 .setattr = proc_sys_setattr,
9043476f 616 .getattr = proc_sys_getattr,
77b14db5
EB
617};
618
619static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
620{
34286d66
NP
621 if (nd->flags & LOOKUP_RCU)
622 return -ECHILD;
9043476f
AV
623 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
624}
625
fe15ce44 626static int proc_sys_delete(const struct dentry *dentry)
9043476f
AV
627{
628 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
629}
630
1f87f0b5
EB
631static int sysctl_is_seen(struct ctl_table_header *p)
632{
633 struct ctl_table_set *set = p->set;
634 int res;
635 spin_lock(&sysctl_lock);
636 if (p->unregistering)
637 res = 0;
638 else if (!set->is_seen)
639 res = 1;
640 else
641 res = set->is_seen(set);
642 spin_unlock(&sysctl_lock);
643 return res;
644}
645
621e155a
NP
646static int proc_sys_compare(const struct dentry *parent,
647 const struct inode *pinode,
648 const struct dentry *dentry, const struct inode *inode,
649 unsigned int len, const char *str, const struct qstr *name)
9043476f 650{
dfef6dcd 651 struct ctl_table_header *head;
31e6b01f
NP
652 /* Although proc doesn't have negative dentries, rcu-walk means
653 * that inode here can be NULL */
dfef6dcd 654 /* AV: can it, indeed? */
31e6b01f 655 if (!inode)
dfef6dcd 656 return 1;
621e155a 657 if (name->len != len)
9043476f 658 return 1;
621e155a 659 if (memcmp(name->name, str, len))
9043476f 660 return 1;
dfef6dcd
AV
661 head = rcu_dereference(PROC_I(inode)->sysctl);
662 return !head || !sysctl_is_seen(head);
77b14db5
EB
663}
664
d72f71eb 665static const struct dentry_operations proc_sys_dentry_operations = {
77b14db5 666 .d_revalidate = proc_sys_revalidate,
9043476f
AV
667 .d_delete = proc_sys_delete,
668 .d_compare = proc_sys_compare,
77b14db5
EB
669};
670
1f87f0b5
EB
671static struct ctl_table *is_branch_in(struct ctl_table *branch,
672 struct ctl_table *table)
673{
674 struct ctl_table *p;
675 const char *s = branch->procname;
676
677 /* branch should have named subdirectory as its first element */
678 if (!s || !branch->child)
679 return NULL;
680
681 /* ... and nothing else */
682 if (branch[1].procname)
683 return NULL;
684
685 /* table should contain subdirectory with the same name */
686 for (p = table; p->procname; p++) {
687 if (!p->child)
688 continue;
689 if (p->procname && strcmp(p->procname, s) == 0)
690 return p;
691 }
692 return NULL;
693}
694
695/* see if attaching q to p would be an improvement */
696static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
697{
698 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
699 struct ctl_table *next;
700 int is_better = 0;
701 int not_in_parent = !p->attached_by;
702
703 while ((next = is_branch_in(by, to)) != NULL) {
704 if (by == q->attached_by)
705 is_better = 1;
706 if (to == p->attached_by)
707 not_in_parent = 1;
708 by = by->child;
709 to = next->child;
710 }
711
712 if (is_better && not_in_parent) {
713 q->attached_by = by;
714 q->attached_to = to;
715 q->parent = p;
716 }
717}
718
7c60c48f
EB
719static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
720 struct ctl_table *table)
1f87f0b5 721{
7c60c48f
EB
722 struct ctl_table *entry, *test;
723 int error = 0;
1f87f0b5 724
7c60c48f
EB
725 for (entry = old; entry->procname; entry++) {
726 for (test = table; test->procname; test++) {
727 if (strcmp(entry->procname, test->procname) == 0) {
728 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
729 path, test->procname);
730 error = -EEXIST;
731 }
732 }
733 }
734 return error;
1f87f0b5
EB
735}
736
7c60c48f
EB
737static int sysctl_check_dups(struct nsproxy *namespaces,
738 struct ctl_table_header *header,
739 const char *path, struct ctl_table *table)
1f87f0b5 740{
7c60c48f
EB
741 struct ctl_table_root *root;
742 struct ctl_table_set *set;
743 struct ctl_table_header *dir_head, *head;
744 struct ctl_table *dir_table;
745 int error = 0;
1f87f0b5 746
7c60c48f
EB
747 /* No dups if we are the only member of our directory */
748 if (header->attached_by != table)
749 return 0;
1f87f0b5 750
7c60c48f
EB
751 dir_head = header->parent;
752 dir_table = header->attached_to;
1f87f0b5 753
7c60c48f 754 error = sysctl_check_table_dups(path, dir_table, table);
1f87f0b5 755
7c60c48f
EB
756 root = &sysctl_table_root;
757 do {
758 set = lookup_header_set(root, namespaces);
1f87f0b5 759
7c60c48f
EB
760 list_for_each_entry(head, &set->list, ctl_entry) {
761 if (head->unregistering)
1f87f0b5 762 continue;
7c60c48f
EB
763 if (head->attached_to != dir_table)
764 continue;
765 error = sysctl_check_table_dups(path, head->attached_by,
766 table);
1f87f0b5 767 }
7c60c48f
EB
768 root = list_entry(root->root_list.next,
769 struct ctl_table_root, root_list);
770 } while (root != &sysctl_table_root);
771 return error;
1f87f0b5
EB
772}
773
7c60c48f 774static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1f87f0b5 775{
7c60c48f
EB
776 struct va_format vaf;
777 va_list args;
1f87f0b5 778
7c60c48f
EB
779 va_start(args, fmt);
780 vaf.fmt = fmt;
781 vaf.va = &args;
782
783 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
784 path, table->procname, &vaf);
1f87f0b5 785
7c60c48f
EB
786 va_end(args);
787 return -EINVAL;
1f87f0b5
EB
788}
789
7c60c48f 790static int sysctl_check_table(const char *path, struct ctl_table *table)
1f87f0b5 791{
7c60c48f 792 int err = 0;
1f87f0b5 793 for (; table->procname; table++) {
1f87f0b5 794 if (table->child)
7c60c48f
EB
795 err = sysctl_err(path, table, "Not a file");
796
797 if ((table->proc_handler == proc_dostring) ||
798 (table->proc_handler == proc_dointvec) ||
799 (table->proc_handler == proc_dointvec_minmax) ||
800 (table->proc_handler == proc_dointvec_jiffies) ||
801 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
802 (table->proc_handler == proc_dointvec_ms_jiffies) ||
803 (table->proc_handler == proc_doulongvec_minmax) ||
804 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
805 if (!table->data)
806 err = sysctl_err(path, table, "No data");
807 if (!table->maxlen)
808 err = sysctl_err(path, table, "No maxlen");
809 }
810 if (!table->proc_handler)
811 err = sysctl_err(path, table, "No proc_handler");
812
813 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
814 err = sysctl_err(path, table, "bogus .mode 0%o",
815 table->mode);
1f87f0b5 816 }
7c60c48f 817 return err;
1f87f0b5 818}
1f87f0b5
EB
819
820/**
f728019b 821 * __register_sysctl_table - register a leaf sysctl table
1f87f0b5
EB
822 * @root: List of sysctl headers to register on
823 * @namespaces: Data to compute which lists of sysctl entries are visible
824 * @path: The path to the directory the sysctl table is in.
825 * @table: the top-level table structure
826 *
827 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
828 * array. A completely 0 filled entry terminates the table.
829 *
830 * The members of the &struct ctl_table structure are used as follows:
831 *
832 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
833 * enter a sysctl file
834 *
835 * data - a pointer to data for use by proc_handler
836 *
837 * maxlen - the maximum size in bytes of the data
838 *
f728019b 839 * mode - the file permissions for the /proc/sys file
1f87f0b5 840 *
f728019b 841 * child - must be %NULL.
1f87f0b5
EB
842 *
843 * proc_handler - the text handler routine (described below)
844 *
1f87f0b5
EB
845 * extra1, extra2 - extra pointers usable by the proc handler routines
846 *
847 * Leaf nodes in the sysctl tree will be represented by a single file
848 * under /proc; non-leaf nodes will be represented by directories.
849 *
f728019b
EB
850 * There must be a proc_handler routine for any terminal nodes.
851 * Several default handlers are available to cover common cases -
1f87f0b5
EB
852 *
853 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
854 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
855 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
856 *
857 * It is the handler's job to read the input buffer from user memory
858 * and process it. The handler should return 0 on success.
859 *
860 * This routine returns %NULL on a failure to register, and a pointer
861 * to the table header on success.
862 */
6e9d5164 863struct ctl_table_header *__register_sysctl_table(
1f87f0b5
EB
864 struct ctl_table_root *root,
865 struct nsproxy *namespaces,
6e9d5164 866 const char *path, struct ctl_table *table)
1f87f0b5
EB
867{
868 struct ctl_table_header *header;
869 struct ctl_table *new, **prevp;
6e9d5164
EB
870 const char *name, *nextname;
871 unsigned int npath = 0;
1f87f0b5 872 struct ctl_table_set *set;
f05e53a7
EB
873 size_t path_bytes = 0;
874 char *new_name;
1f87f0b5
EB
875
876 /* Count the path components */
6e9d5164
EB
877 for (name = path; name; name = nextname) {
878 int namelen;
879 nextname = strchr(name, '/');
880 if (nextname) {
881 namelen = nextname - name;
882 nextname++;
883 } else {
884 namelen = strlen(name);
885 }
886 if (namelen == 0)
887 continue;
888 path_bytes += namelen + 1;
889 npath++;
890 }
1f87f0b5
EB
891
892 /*
893 * For each path component, allocate a 2-element ctl_table array.
894 * The first array element will be filled with the sysctl entry
895 * for this, the second will be the sentinel (procname == 0).
896 *
897 * We allocate everything in one go so that we don't have to
898 * worry about freeing additional memory in unregister_sysctl_table.
899 */
f05e53a7 900 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
1f87f0b5
EB
901 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
902 if (!header)
903 return NULL;
904
905 new = (struct ctl_table *) (header + 1);
f05e53a7 906 new_name = (char *)(new + (2 * npath));
1f87f0b5
EB
907
908 /* Now connect the dots */
909 prevp = &header->ctl_table;
6e9d5164
EB
910 for (name = path; name; name = nextname) {
911 int namelen;
912 nextname = strchr(name, '/');
913 if (nextname) {
914 namelen = nextname - name;
915 nextname++;
916 } else {
917 namelen = strlen(name);
918 }
919 if (namelen == 0)
920 continue;
921 memcpy(new_name, name, namelen);
922 new_name[namelen] = '\0';
923
f05e53a7 924 new->procname = new_name;
1f87f0b5
EB
925 new->mode = 0555;
926
927 *prevp = new;
928 prevp = &new->child;
929
930 new += 2;
6e9d5164 931 new_name += namelen + 1;
1f87f0b5
EB
932 }
933 *prevp = table;
934 header->ctl_table_arg = table;
935
936 INIT_LIST_HEAD(&header->ctl_entry);
937 header->used = 0;
938 header->unregistering = NULL;
939 header->root = root;
1f87f0b5 940 header->count = 1;
7c60c48f
EB
941 if (sysctl_check_table(path, table))
942 goto fail;
8d6ecfcc 943
1f87f0b5
EB
944 spin_lock(&sysctl_lock);
945 header->set = lookup_header_set(root, namespaces);
946 header->attached_by = header->ctl_table;
947 header->attached_to = root_table;
948 header->parent = &root_table_header;
bd295b56
EB
949 set = header->set;
950 root = header->root;
951 for (;;) {
1f87f0b5
EB
952 struct ctl_table_header *p;
953 list_for_each_entry(p, &set->list, ctl_entry) {
954 if (p->unregistering)
955 continue;
956 try_attach(p, header);
957 }
bd295b56
EB
958 if (root == &sysctl_table_root)
959 break;
960 root = list_entry(root->root_list.prev,
961 struct ctl_table_root, root_list);
962 set = lookup_header_set(root, namespaces);
1f87f0b5 963 }
7c60c48f
EB
964 if (sysctl_check_dups(namespaces, header, path, table))
965 goto fail_locked;
1f87f0b5
EB
966 header->parent->count++;
967 list_add_tail(&header->ctl_entry, &header->set->list);
968 spin_unlock(&sysctl_lock);
969
970 return header;
7c60c48f
EB
971fail_locked:
972 spin_unlock(&sysctl_lock);
973fail:
974 kfree(header);
975 dump_stack();
976 return NULL;
1f87f0b5
EB
977}
978
6e9d5164
EB
979static char *append_path(const char *path, char *pos, const char *name)
980{
981 int namelen;
982 namelen = strlen(name);
983 if (((pos - path) + namelen + 2) >= PATH_MAX)
984 return NULL;
985 memcpy(pos, name, namelen);
986 pos[namelen] = '/';
987 pos[namelen + 1] = '\0';
988 pos += namelen + 1;
989 return pos;
990}
991
f728019b
EB
992static int count_subheaders(struct ctl_table *table)
993{
994 int has_files = 0;
995 int nr_subheaders = 0;
996 struct ctl_table *entry;
997
998 /* special case: no directory and empty directory */
999 if (!table || !table->procname)
1000 return 1;
1001
1002 for (entry = table; entry->procname; entry++) {
1003 if (entry->child)
1004 nr_subheaders += count_subheaders(entry->child);
1005 else
1006 has_files = 1;
1007 }
1008 return nr_subheaders + has_files;
1009}
1010
1011static int register_leaf_sysctl_tables(const char *path, char *pos,
1012 struct ctl_table_header ***subheader,
1013 struct ctl_table_root *root, struct nsproxy *namespaces,
1014 struct ctl_table *table)
1015{
1016 struct ctl_table *ctl_table_arg = NULL;
1017 struct ctl_table *entry, *files;
1018 int nr_files = 0;
1019 int nr_dirs = 0;
1020 int err = -ENOMEM;
1021
1022 for (entry = table; entry->procname; entry++) {
1023 if (entry->child)
1024 nr_dirs++;
1025 else
1026 nr_files++;
1027 }
1028
1029 files = table;
1030 /* If there are mixed files and directories we need a new table */
1031 if (nr_dirs && nr_files) {
1032 struct ctl_table *new;
1033 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1034 GFP_KERNEL);
1035 if (!files)
1036 goto out;
1037
1038 ctl_table_arg = files;
1039 for (new = files, entry = table; entry->procname; entry++) {
1040 if (entry->child)
1041 continue;
1042 *new = *entry;
1043 new++;
1044 }
1045 }
1046
1047 /* Register everything except a directory full of subdirectories */
1048 if (nr_files || !nr_dirs) {
1049 struct ctl_table_header *header;
1050 header = __register_sysctl_table(root, namespaces, path, files);
1051 if (!header) {
1052 kfree(ctl_table_arg);
1053 goto out;
1054 }
1055
1056 /* Remember if we need to free the file table */
1057 header->ctl_table_arg = ctl_table_arg;
1058 **subheader = header;
1059 (*subheader)++;
1060 }
1061
1062 /* Recurse into the subdirectories. */
1063 for (entry = table; entry->procname; entry++) {
1064 char *child_pos;
1065
1066 if (!entry->child)
1067 continue;
1068
1069 err = -ENAMETOOLONG;
1070 child_pos = append_path(path, pos, entry->procname);
1071 if (!child_pos)
1072 goto out;
1073
1074 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1075 root, namespaces, entry->child);
1076 pos[0] = '\0';
1077 if (err)
1078 goto out;
1079 }
1080 err = 0;
1081out:
1082 /* On failure our caller will unregister all registered subheaders */
1083 return err;
1084}
1085
6e9d5164
EB
1086/**
1087 * __register_sysctl_paths - register a sysctl table hierarchy
1088 * @root: List of sysctl headers to register on
1089 * @namespaces: Data to compute which lists of sysctl entries are visible
1090 * @path: The path to the directory the sysctl table is in.
1091 * @table: the top-level table structure
1092 *
1093 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1094 * array. A completely 0 filled entry terminates the table.
1095 *
1096 * See __register_sysctl_table for more details.
1097 */
1098struct ctl_table_header *__register_sysctl_paths(
1099 struct ctl_table_root *root,
1100 struct nsproxy *namespaces,
1101 const struct ctl_path *path, struct ctl_table *table)
1102{
ec6a5266 1103 struct ctl_table *ctl_table_arg = table;
f728019b
EB
1104 int nr_subheaders = count_subheaders(table);
1105 struct ctl_table_header *header = NULL, **subheaders, **subheader;
6e9d5164
EB
1106 const struct ctl_path *component;
1107 char *new_path, *pos;
1108
1109 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1110 if (!new_path)
1111 return NULL;
1112
1113 pos[0] = '\0';
1114 for (component = path; component->procname; component++) {
1115 pos = append_path(new_path, pos, component->procname);
1116 if (!pos)
1117 goto out;
1118 }
ec6a5266
EB
1119 while (table->procname && table->child && !table[1].procname) {
1120 pos = append_path(new_path, pos, table->procname);
1121 if (!pos)
1122 goto out;
1123 table = table->child;
1124 }
f728019b
EB
1125 if (nr_subheaders == 1) {
1126 header = __register_sysctl_table(root, namespaces, new_path, table);
1127 if (header)
1128 header->ctl_table_arg = ctl_table_arg;
1129 } else {
1130 header = kzalloc(sizeof(*header) +
1131 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1132 if (!header)
1133 goto out;
1134
1135 subheaders = (struct ctl_table_header **) (header + 1);
1136 subheader = subheaders;
ec6a5266 1137 header->ctl_table_arg = ctl_table_arg;
f728019b
EB
1138
1139 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1140 root, namespaces, table))
1141 goto err_register_leaves;
1142 }
1143
6e9d5164
EB
1144out:
1145 kfree(new_path);
1146 return header;
f728019b
EB
1147
1148err_register_leaves:
1149 while (subheader > subheaders) {
1150 struct ctl_table_header *subh = *(--subheader);
1151 struct ctl_table *table = subh->ctl_table_arg;
1152 unregister_sysctl_table(subh);
1153 kfree(table);
1154 }
1155 kfree(header);
1156 header = NULL;
1157 goto out;
6e9d5164
EB
1158}
1159
1f87f0b5
EB
1160/**
1161 * register_sysctl_table_path - register a sysctl table hierarchy
1162 * @path: The path to the directory the sysctl table is in.
1163 * @table: the top-level table structure
1164 *
1165 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1166 * array. A completely 0 filled entry terminates the table.
1167 *
1168 * See __register_sysctl_paths for more details.
1169 */
1170struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1171 struct ctl_table *table)
1172{
1173 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1174 path, table);
1175}
1176EXPORT_SYMBOL(register_sysctl_paths);
1177
1178/**
1179 * register_sysctl_table - register a sysctl table hierarchy
1180 * @table: the top-level table structure
1181 *
1182 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1183 * array. A completely 0 filled entry terminates the table.
1184 *
1185 * See register_sysctl_paths for more details.
1186 */
1187struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1188{
1189 static const struct ctl_path null_path[] = { {} };
1190
1191 return register_sysctl_paths(null_path, table);
1192}
1193EXPORT_SYMBOL(register_sysctl_table);
1194
1195/**
1196 * unregister_sysctl_table - unregister a sysctl table hierarchy
1197 * @header: the header returned from register_sysctl_table
1198 *
1199 * Unregisters the sysctl table and all children. proc entries may not
1200 * actually be removed until they are no longer used by anyone.
1201 */
1202void unregister_sysctl_table(struct ctl_table_header * header)
1203{
f728019b 1204 int nr_subheaders;
1f87f0b5
EB
1205 might_sleep();
1206
1207 if (header == NULL)
1208 return;
1209
f728019b
EB
1210 nr_subheaders = count_subheaders(header->ctl_table_arg);
1211 if (unlikely(nr_subheaders > 1)) {
1212 struct ctl_table_header **subheaders;
1213 int i;
1214
1215 subheaders = (struct ctl_table_header **)(header + 1);
1216 for (i = nr_subheaders -1; i >= 0; i--) {
1217 struct ctl_table_header *subh = subheaders[i];
1218 struct ctl_table *table = subh->ctl_table_arg;
1219 unregister_sysctl_table(subh);
1220 kfree(table);
1221 }
1222 kfree(header);
1223 return;
1224 }
1225
1f87f0b5
EB
1226 spin_lock(&sysctl_lock);
1227 start_unregistering(header);
1228 if (!--header->parent->count) {
1229 WARN_ON(1);
1230 kfree_rcu(header->parent, rcu);
1231 }
1232 if (!--header->count)
1233 kfree_rcu(header, rcu);
1234 spin_unlock(&sysctl_lock);
1235}
1236EXPORT_SYMBOL(unregister_sysctl_table);
1237
1238void setup_sysctl_set(struct ctl_table_set *p,
1f87f0b5
EB
1239 int (*is_seen)(struct ctl_table_set *))
1240{
1241 INIT_LIST_HEAD(&p->list);
1f87f0b5
EB
1242 p->is_seen = is_seen;
1243}
1244
97324cd8
EB
1245void retire_sysctl_set(struct ctl_table_set *set)
1246{
1247 WARN_ON(!list_empty(&set->list));
1248}
1f87f0b5 1249
1e0edd3f 1250int __init proc_sys_init(void)
77b14db5 1251{
e1675231
AD
1252 struct proc_dir_entry *proc_sys_root;
1253
77b14db5 1254 proc_sys_root = proc_mkdir("sys", NULL);
9043476f
AV
1255 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1256 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
77b14db5 1257 proc_sys_root->nlink = 0;
de4e83bd
EB
1258
1259 return sysctl_init();
77b14db5 1260}