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