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