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