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