xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 0eb97f38)
177b14db5SEric W. Biederman /*
277b14db5SEric W. Biederman  * /proc/sys support
377b14db5SEric W. Biederman  */
41e0edd3fSAlexey Dobriyan #include <linux/init.h>
577b14db5SEric W. Biederman #include <linux/sysctl.h>
6f1ecf068SLucas De Marchi #include <linux/poll.h>
777b14db5SEric W. Biederman #include <linux/proc_fs.h>
877b14db5SEric W. Biederman #include <linux/security.h>
934286d66SNick Piggin #include <linux/namei.h>
101f87f0b5SEric W. Biederman #include <linux/module.h>
1177b14db5SEric W. Biederman #include "internal.h"
1277b14db5SEric W. Biederman 
13d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1477b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
169043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
179043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
1877b14db5SEric W. Biederman 
19f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
20f1ecf068SLucas De Marchi {
21f1ecf068SLucas De Marchi 	if (!poll)
22f1ecf068SLucas De Marchi 		return;
23f1ecf068SLucas De Marchi 
24f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
25f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
26f1ecf068SLucas De Marchi }
27f1ecf068SLucas De Marchi 
28a194558eSEric W. Biederman static struct ctl_table root_table[] = {
29a194558eSEric W. Biederman 	{
30a194558eSEric W. Biederman 		.procname = "",
317ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
32a194558eSEric W. Biederman 	},
33a194558eSEric W. Biederman 	{ }
34a194558eSEric W. Biederman };
350e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
360e47c99dSEric W. Biederman 	.default_set.dir.header = {
371f87f0b5SEric W. Biederman 		{{.count = 1,
38938aaa4fSEric W. Biederman 		  .nreg = 1,
39ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
400e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
411f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
421f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
437ec66d06SEric W. Biederman 	},
441f87f0b5SEric W. Biederman };
451f87f0b5SEric W. Biederman 
461f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
471f87f0b5SEric W. Biederman 
487ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
490e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
500e47c99dSEric W. Biederman 	struct ctl_table **pentry, struct nsproxy *namespaces);
510e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
520e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
537ec66d06SEric W. Biederman 
546980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
556980128fSEric W. Biederman {
566980128fSEric W. Biederman 	if (dir->header.parent)
576980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
586980128fSEric W. Biederman 	printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
596980128fSEric W. Biederman }
606980128fSEric W. Biederman 
61076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
62076c3eedSEric W. Biederman {
63076c3eedSEric W. Biederman 	int minlen;
64076c3eedSEric W. Biederman 	int cmp;
65076c3eedSEric W. Biederman 
66076c3eedSEric W. Biederman 	minlen = len1;
67076c3eedSEric W. Biederman 	if (minlen > len2)
68076c3eedSEric W. Biederman 		minlen = len2;
69076c3eedSEric W. Biederman 
70076c3eedSEric W. Biederman 	cmp = memcmp(name1, name2, minlen);
71076c3eedSEric W. Biederman 	if (cmp == 0)
72076c3eedSEric W. Biederman 		cmp = len1 - len2;
73076c3eedSEric W. Biederman 	return cmp;
74076c3eedSEric W. Biederman }
75076c3eedSEric W. Biederman 
76076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
770e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
78076c3eedSEric W. Biederman {
79076c3eedSEric W. Biederman 	struct ctl_table_header *head;
80076c3eedSEric W. Biederman 	struct ctl_table *entry;
81ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
82076c3eedSEric W. Biederman 
83ac13ac6fSEric W. Biederman 	while (node)
84ac13ac6fSEric W. Biederman 	{
85ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
86ac13ac6fSEric W. Biederman 		const char *procname;
87ac13ac6fSEric W. Biederman 		int cmp;
88ac13ac6fSEric W. Biederman 
89ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
90ac13ac6fSEric W. Biederman 		head = ctl_node->header;
91ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
92ac13ac6fSEric W. Biederman 		procname = entry->procname;
93ac13ac6fSEric W. Biederman 
94ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
95ac13ac6fSEric W. Biederman 		if (cmp < 0)
96ac13ac6fSEric W. Biederman 			node = node->rb_left;
97ac13ac6fSEric W. Biederman 		else if (cmp > 0)
98ac13ac6fSEric W. Biederman 			node = node->rb_right;
99ac13ac6fSEric W. Biederman 		else {
100076c3eedSEric W. Biederman 			*phead = head;
101076c3eedSEric W. Biederman 			return entry;
102076c3eedSEric W. Biederman 		}
103076c3eedSEric W. Biederman 	}
104076c3eedSEric W. Biederman 	return NULL;
105076c3eedSEric W. Biederman }
106076c3eedSEric W. Biederman 
107ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
108ac13ac6fSEric W. Biederman {
109ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
110ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
111ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
112ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
113ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
114ac13ac6fSEric W. Biederman 
115ac13ac6fSEric W. Biederman 	while (*p) {
116ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
117ac13ac6fSEric W. Biederman 		struct ctl_table *parent_entry;
118ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
119ac13ac6fSEric W. Biederman 		const char *parent_name;
120ac13ac6fSEric W. Biederman 		int cmp;
121ac13ac6fSEric W. Biederman 
122ac13ac6fSEric W. Biederman 		parent = *p;
123ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
124ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
125ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
126ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
127ac13ac6fSEric W. Biederman 
128ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
129ac13ac6fSEric W. Biederman 		if (cmp < 0)
130ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
131ac13ac6fSEric W. Biederman 		else if (cmp > 0)
132ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
133ac13ac6fSEric W. Biederman 		else {
134ac13ac6fSEric W. Biederman 			printk(KERN_ERR "sysctl duplicate entry: ");
135ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
136ac13ac6fSEric W. Biederman 			printk(KERN_CONT "/%s\n", entry->procname);
137ac13ac6fSEric W. Biederman 			return -EEXIST;
138ac13ac6fSEric W. Biederman 		}
139ac13ac6fSEric W. Biederman 	}
140ac13ac6fSEric W. Biederman 
141ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
142ac13ac6fSEric W. Biederman 	return 0;
143ac13ac6fSEric W. Biederman }
144ac13ac6fSEric W. Biederman 
145ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
146ac13ac6fSEric W. Biederman {
147ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
148ac13ac6fSEric W. Biederman 
149ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
150ac13ac6fSEric W. Biederman }
151ac13ac6fSEric W. Biederman 
152e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
153e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
154ac13ac6fSEric W. Biederman 	struct ctl_node *node, struct ctl_table *table)
155e0d04529SEric W. Biederman {
1567ec66d06SEric W. Biederman 	head->ctl_table = table;
157e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
158e0d04529SEric W. Biederman 	head->used = 0;
159e0d04529SEric W. Biederman 	head->count = 1;
160e0d04529SEric W. Biederman 	head->nreg = 1;
161e0d04529SEric W. Biederman 	head->unregistering = NULL;
162e0d04529SEric W. Biederman 	head->root = root;
163e0d04529SEric W. Biederman 	head->set = set;
164e0d04529SEric W. Biederman 	head->parent = NULL;
165ac13ac6fSEric W. Biederman 	head->node = node;
166ac13ac6fSEric W. Biederman 	if (node) {
167ac13ac6fSEric W. Biederman 		struct ctl_table *entry;
168ac13ac6fSEric W. Biederman 		for (entry = table; entry->procname; entry++, node++) {
169ac13ac6fSEric W. Biederman 			rb_init_node(&node->node);
170ac13ac6fSEric W. Biederman 			node->header = head;
171ac13ac6fSEric W. Biederman 		}
172ac13ac6fSEric W. Biederman 	}
173e0d04529SEric W. Biederman }
174e0d04529SEric W. Biederman 
1758425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
1768425d6aaSEric W. Biederman {
177ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
178ac13ac6fSEric W. Biederman 	for (entry = head->ctl_table; entry->procname; entry++)
179ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
1808425d6aaSEric W. Biederman }
1818425d6aaSEric W. Biederman 
1820e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
1838425d6aaSEric W. Biederman {
184ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1850e47c99dSEric W. Biederman 	int err;
1860e47c99dSEric W. Biederman 
1870e47c99dSEric W. Biederman 	dir->header.nreg++;
1887ec66d06SEric W. Biederman 	header->parent = dir;
1890e47c99dSEric W. Biederman 	err = insert_links(header);
1900e47c99dSEric W. Biederman 	if (err)
1910e47c99dSEric W. Biederman 		goto fail_links;
192ac13ac6fSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
193ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
194ac13ac6fSEric W. Biederman 		if (err)
195ac13ac6fSEric W. Biederman 			goto fail;
196ac13ac6fSEric W. Biederman 	}
1970e47c99dSEric W. Biederman 	return 0;
198ac13ac6fSEric W. Biederman fail:
199ac13ac6fSEric W. Biederman 	erase_header(header);
200ac13ac6fSEric W. Biederman 	put_links(header);
2010e47c99dSEric W. Biederman fail_links:
2020e47c99dSEric W. Biederman 	header->parent = NULL;
2030e47c99dSEric W. Biederman 	drop_sysctl_table(&dir->header);
2040e47c99dSEric W. Biederman 	return err;
2058425d6aaSEric W. Biederman }
2068425d6aaSEric W. Biederman 
2071f87f0b5SEric W. Biederman /* called under sysctl_lock */
2081f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2091f87f0b5SEric W. Biederman {
2101f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2111f87f0b5SEric W. Biederman 		return 0;
2121f87f0b5SEric W. Biederman 	p->used++;
2131f87f0b5SEric W. Biederman 	return 1;
2141f87f0b5SEric W. Biederman }
2151f87f0b5SEric W. Biederman 
2161f87f0b5SEric W. Biederman /* called under sysctl_lock */
2171f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2181f87f0b5SEric W. Biederman {
2191f87f0b5SEric W. Biederman 	if (!--p->used)
2201f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2211f87f0b5SEric W. Biederman 			complete(p->unregistering);
2221f87f0b5SEric W. Biederman }
2231f87f0b5SEric W. Biederman 
2241f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
2251f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
2261f87f0b5SEric W. Biederman {
2271f87f0b5SEric W. Biederman 	/*
2281f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
2291f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
2301f87f0b5SEric W. Biederman 	 */
2311f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
2321f87f0b5SEric W. Biederman 		struct completion wait;
2331f87f0b5SEric W. Biederman 		init_completion(&wait);
2341f87f0b5SEric W. Biederman 		p->unregistering = &wait;
2351f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
2361f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
2371f87f0b5SEric W. Biederman 		spin_lock(&sysctl_lock);
2381f87f0b5SEric W. Biederman 	} else {
2391f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
2401f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
2411f87f0b5SEric W. Biederman 	}
2421f87f0b5SEric W. Biederman 	/*
2431f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
2441f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
2451f87f0b5SEric W. Biederman 	 */
2468425d6aaSEric W. Biederman 	erase_header(p);
2471f87f0b5SEric W. Biederman }
2481f87f0b5SEric W. Biederman 
2491f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head)
2501f87f0b5SEric W. Biederman {
2511f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2521f87f0b5SEric W. Biederman 	head->count++;
2531f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2541f87f0b5SEric W. Biederman }
2551f87f0b5SEric W. Biederman 
2561f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head)
2571f87f0b5SEric W. Biederman {
2581f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2591f87f0b5SEric W. Biederman 	if (!--head->count)
2601f87f0b5SEric W. Biederman 		kfree_rcu(head, rcu);
2611f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2621f87f0b5SEric W. Biederman }
2631f87f0b5SEric W. Biederman 
2641f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
2651f87f0b5SEric W. Biederman {
2661f87f0b5SEric W. Biederman 	if (!head)
2671f87f0b5SEric W. Biederman 		BUG();
2681f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2691f87f0b5SEric W. Biederman 	if (!use_table(head))
2701f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
2711f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2721f87f0b5SEric W. Biederman 	return head;
2731f87f0b5SEric W. Biederman }
2741f87f0b5SEric W. Biederman 
2751f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
2761f87f0b5SEric W. Biederman {
2771f87f0b5SEric W. Biederman 	if (!head)
2781f87f0b5SEric W. Biederman 		return;
2791f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2801f87f0b5SEric W. Biederman 	unuse_table(head);
2811f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2821f87f0b5SEric W. Biederman }
2831f87f0b5SEric W. Biederman 
2841f87f0b5SEric W. Biederman static struct ctl_table_set *
2851f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
2861f87f0b5SEric W. Biederman {
2871f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
2881f87f0b5SEric W. Biederman 	if (root->lookup)
2891f87f0b5SEric W. Biederman 		set = root->lookup(root, namespaces);
2901f87f0b5SEric W. Biederman 	return set;
2911f87f0b5SEric W. Biederman }
2921f87f0b5SEric W. Biederman 
293076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
2947ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
295076c3eedSEric W. Biederman 				      const char *name, int namelen)
296076c3eedSEric W. Biederman {
297076c3eedSEric W. Biederman 	struct ctl_table_header *head;
298076c3eedSEric W. Biederman 	struct ctl_table *entry;
299076c3eedSEric W. Biederman 
300076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3010e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
302076c3eedSEric W. Biederman 	if (entry && use_table(head))
303076c3eedSEric W. Biederman 		*phead = head;
304076c3eedSEric W. Biederman 	else
305076c3eedSEric W. Biederman 		entry = NULL;
306076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
307076c3eedSEric W. Biederman 	return entry;
308076c3eedSEric W. Biederman }
309076c3eedSEric W. Biederman 
310ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3111f87f0b5SEric W. Biederman {
312ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3131f87f0b5SEric W. Biederman 
314ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
315ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
316ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
317ac13ac6fSEric W. Biederman 			return ctl_node;
3181f87f0b5SEric W. Biederman 	}
3191f87f0b5SEric W. Biederman 	return NULL;
3201f87f0b5SEric W. Biederman }
3211f87f0b5SEric W. Biederman 
3227ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
3236a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
3241f87f0b5SEric W. Biederman {
325ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
3267ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
327ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3286a75ce16SEric W. Biederman 
3296a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
330ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
3316a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
332ac13ac6fSEric W. Biederman 	if (ctl_node) {
333ac13ac6fSEric W. Biederman 		head = ctl_node->header;
334ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
335ac13ac6fSEric W. Biederman 	}
3366a75ce16SEric W. Biederman 	*phead = head;
3376a75ce16SEric W. Biederman 	*pentry = entry;
3386a75ce16SEric W. Biederman }
3396a75ce16SEric W. Biederman 
3407ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
3416a75ce16SEric W. Biederman {
3426a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
3436a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
344ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
3456a75ce16SEric W. Biederman 
3466a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
3476a75ce16SEric W. Biederman 	unuse_table(head);
348ac13ac6fSEric W. Biederman 
349ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
3506a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
351ac13ac6fSEric W. Biederman 	head = NULL;
352ac13ac6fSEric W. Biederman 	if (ctl_node) {
353ac13ac6fSEric W. Biederman 		head = ctl_node->header;
354ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
3556a75ce16SEric W. Biederman 	}
3566a75ce16SEric W. Biederman 	*phead = head;
3576a75ce16SEric W. Biederman 	*pentry = entry;
3581f87f0b5SEric W. Biederman }
3591f87f0b5SEric W. Biederman 
3601f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root)
3611f87f0b5SEric W. Biederman {
3621f87f0b5SEric W. Biederman }
3631f87f0b5SEric W. Biederman 
3641f87f0b5SEric W. Biederman /*
3651f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
3661f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
3671f87f0b5SEric W. Biederman  */
3681f87f0b5SEric W. Biederman 
3691f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
3701f87f0b5SEric W. Biederman {
3711f87f0b5SEric W. Biederman 	if (!current_euid())
3721f87f0b5SEric W. Biederman 		mode >>= 6;
3731f87f0b5SEric W. Biederman 	else if (in_egroup_p(0))
3741f87f0b5SEric W. Biederman 		mode >>= 3;
3751f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
3761f87f0b5SEric W. Biederman 		return 0;
3771f87f0b5SEric W. Biederman 	return -EACCES;
3781f87f0b5SEric W. Biederman }
3791f87f0b5SEric W. Biederman 
3801f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
3811f87f0b5SEric W. Biederman {
3821f87f0b5SEric W. Biederman 	int mode;
3831f87f0b5SEric W. Biederman 
3841f87f0b5SEric W. Biederman 	if (root->permissions)
3851f87f0b5SEric W. Biederman 		mode = root->permissions(root, current->nsproxy, table);
3861f87f0b5SEric W. Biederman 	else
3871f87f0b5SEric W. Biederman 		mode = table->mode;
3881f87f0b5SEric W. Biederman 
3891f87f0b5SEric W. Biederman 	return test_perm(mode, op);
3901f87f0b5SEric W. Biederman }
3911f87f0b5SEric W. Biederman 
3929043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
3939043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
39477b14db5SEric W. Biederman {
39577b14db5SEric W. Biederman 	struct inode *inode;
3969043476fSAl Viro 	struct proc_inode *ei;
39777b14db5SEric W. Biederman 
3989043476fSAl Viro 	inode = new_inode(sb);
39977b14db5SEric W. Biederman 	if (!inode)
40077b14db5SEric W. Biederman 		goto out;
40177b14db5SEric W. Biederman 
40285fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
40385fe4025SChristoph Hellwig 
4049043476fSAl Viro 	sysctl_head_get(head);
40577b14db5SEric W. Biederman 	ei = PROC_I(inode);
4069043476fSAl Viro 	ei->sysctl = head;
4079043476fSAl Viro 	ei->sysctl_entry = table;
4089043476fSAl Viro 
40977b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4109043476fSAl Viro 	inode->i_mode = table->mode;
4117ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4129043476fSAl Viro 		inode->i_mode |= S_IFREG;
41377b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
41477b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4159043476fSAl Viro 	} else {
4169043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4179043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4189043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
4199043476fSAl Viro 	}
42077b14db5SEric W. Biederman out:
42177b14db5SEric W. Biederman 	return inode;
42277b14db5SEric W. Biederman }
42377b14db5SEric W. Biederman 
42481324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
42577b14db5SEric W. Biederman {
4263cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
4273cc3e046SEric W. Biederman 	if (!head)
4280e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
4293cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
43077b14db5SEric W. Biederman }
43177b14db5SEric W. Biederman 
43277b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
43377b14db5SEric W. Biederman 					struct nameidata *nd)
43477b14db5SEric W. Biederman {
4359043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
4369043476fSAl Viro 	struct ctl_table_header *h = NULL;
4379043476fSAl Viro 	struct qstr *name = &dentry->d_name;
4389043476fSAl Viro 	struct ctl_table *p;
43977b14db5SEric W. Biederman 	struct inode *inode;
4409043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
4417ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
4420e47c99dSEric W. Biederman 	int ret;
44377b14db5SEric W. Biederman 
4449043476fSAl Viro 	if (IS_ERR(head))
4459043476fSAl Viro 		return ERR_CAST(head);
4469043476fSAl Viro 
4477ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
4489043476fSAl Viro 
4497ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
4509043476fSAl Viro 	if (!p)
45177b14db5SEric W. Biederman 		goto out;
45277b14db5SEric W. Biederman 
4530e47c99dSEric W. Biederman 	ret = sysctl_follow_link(&h, &p, current->nsproxy);
4540e47c99dSEric W. Biederman 	err = ERR_PTR(ret);
4550e47c99dSEric W. Biederman 	if (ret)
4560e47c99dSEric W. Biederman 		goto out;
4570e47c99dSEric W. Biederman 
45877b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
4599043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
4609043476fSAl Viro 	if (h)
4619043476fSAl Viro 		sysctl_head_finish(h);
4629043476fSAl Viro 
46377b14db5SEric W. Biederman 	if (!inode)
46477b14db5SEric W. Biederman 		goto out;
46577b14db5SEric W. Biederman 
46677b14db5SEric W. Biederman 	err = NULL;
467fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
46877b14db5SEric W. Biederman 	d_add(dentry, inode);
46977b14db5SEric W. Biederman 
47077b14db5SEric W. Biederman out:
47177b14db5SEric W. Biederman 	sysctl_head_finish(head);
47277b14db5SEric W. Biederman 	return err;
47377b14db5SEric W. Biederman }
47477b14db5SEric W. Biederman 
4757708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
4767708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
47777b14db5SEric W. Biederman {
4789043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
4799043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
4809043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
4812a2da53bSDavid Howells 	ssize_t error;
4822a2da53bSDavid Howells 	size_t res;
48377b14db5SEric W. Biederman 
4849043476fSAl Viro 	if (IS_ERR(head))
4859043476fSAl Viro 		return PTR_ERR(head);
48677b14db5SEric W. Biederman 
48777b14db5SEric W. Biederman 	/*
48877b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
48977b14db5SEric W. Biederman 	 * and won't be until we finish.
49077b14db5SEric W. Biederman 	 */
49177b14db5SEric W. Biederman 	error = -EPERM;
492d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
49377b14db5SEric W. Biederman 		goto out;
49477b14db5SEric W. Biederman 
4959043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
4969043476fSAl Viro 	error = -EINVAL;
4979043476fSAl Viro 	if (!table->proc_handler)
4989043476fSAl Viro 		goto out;
4999043476fSAl Viro 
50077b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
50177b14db5SEric W. Biederman 	res = count;
5028d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
50377b14db5SEric W. Biederman 	if (!error)
50477b14db5SEric W. Biederman 		error = res;
50577b14db5SEric W. Biederman out:
50677b14db5SEric W. Biederman 	sysctl_head_finish(head);
50777b14db5SEric W. Biederman 
50877b14db5SEric W. Biederman 	return error;
50977b14db5SEric W. Biederman }
51077b14db5SEric W. Biederman 
5117708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
5127708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
5137708bfb1SPavel Emelyanov {
5147708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
5157708bfb1SPavel Emelyanov }
5167708bfb1SPavel Emelyanov 
51777b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
51877b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
51977b14db5SEric W. Biederman {
5207708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
52177b14db5SEric W. Biederman }
52277b14db5SEric W. Biederman 
523f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
524f1ecf068SLucas De Marchi {
525f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
526f1ecf068SLucas De Marchi 
527f1ecf068SLucas De Marchi 	if (table->poll)
528f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
529f1ecf068SLucas De Marchi 
530f1ecf068SLucas De Marchi 	return 0;
531f1ecf068SLucas De Marchi }
532f1ecf068SLucas De Marchi 
533f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
534f1ecf068SLucas De Marchi {
535f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
536f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
537f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
538f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
539f1ecf068SLucas De Marchi 
540f1ecf068SLucas De Marchi 	if (!table->proc_handler)
541f1ecf068SLucas De Marchi 		goto out;
542f1ecf068SLucas De Marchi 
543f1ecf068SLucas De Marchi 	if (!table->poll)
544f1ecf068SLucas De Marchi 		goto out;
545f1ecf068SLucas De Marchi 
546f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
547f1ecf068SLucas De Marchi 
548f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
549f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
550f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
551f1ecf068SLucas De Marchi 	}
552f1ecf068SLucas De Marchi 
553f1ecf068SLucas De Marchi out:
554f1ecf068SLucas De Marchi 	return ret;
555f1ecf068SLucas De Marchi }
55677b14db5SEric W. Biederman 
55777b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
5589043476fSAl Viro 				filldir_t filldir,
5599043476fSAl Viro 				struct ctl_table_header *head,
5609043476fSAl Viro 				struct ctl_table *table)
56177b14db5SEric W. Biederman {
56277b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
56377b14db5SEric W. Biederman 	struct inode *inode;
56477b14db5SEric W. Biederman 	struct qstr qname;
56577b14db5SEric W. Biederman 	ino_t ino = 0;
56677b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
56777b14db5SEric W. Biederman 
56877b14db5SEric W. Biederman 	qname.name = table->procname;
56977b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
57077b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
57177b14db5SEric W. Biederman 
57277b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
57377b14db5SEric W. Biederman 	if (!child) {
5749043476fSAl Viro 		child = d_alloc(dir, &qname);
5759043476fSAl Viro 		if (child) {
5769043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
5779043476fSAl Viro 			if (!inode) {
5789043476fSAl Viro 				dput(child);
5799043476fSAl Viro 				return -ENOMEM;
5809043476fSAl Viro 			} else {
581fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
5829043476fSAl Viro 				d_add(child, inode);
58377b14db5SEric W. Biederman 			}
5849043476fSAl Viro 		} else {
5859043476fSAl Viro 			return -ENOMEM;
58677b14db5SEric W. Biederman 		}
58777b14db5SEric W. Biederman 	}
58877b14db5SEric W. Biederman 	inode = child->d_inode;
58977b14db5SEric W. Biederman 	ino  = inode->i_ino;
59077b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
59177b14db5SEric W. Biederman 	dput(child);
5929043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
5939043476fSAl Viro }
5949043476fSAl Viro 
5950e47c99dSEric W. Biederman static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
5960e47c99dSEric W. Biederman 				    filldir_t filldir,
5970e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
5980e47c99dSEric W. Biederman 				    struct ctl_table *table)
5990e47c99dSEric W. Biederman {
6000e47c99dSEric W. Biederman 	int err, ret = 0;
6010e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
6020e47c99dSEric W. Biederman 
6030e47c99dSEric W. Biederman 	/* It is not an error if we can not follow the link ignore it */
6040e47c99dSEric W. Biederman 	err = sysctl_follow_link(&head, &table, current->nsproxy);
6050e47c99dSEric W. Biederman 	if (err)
6060e47c99dSEric W. Biederman 		goto out;
6070e47c99dSEric W. Biederman 
6080e47c99dSEric W. Biederman 	ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
6090e47c99dSEric W. Biederman out:
6100e47c99dSEric W. Biederman 	sysctl_head_finish(head);
6110e47c99dSEric W. Biederman 	return ret;
6120e47c99dSEric W. Biederman }
6130e47c99dSEric W. Biederman 
6149043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
6159043476fSAl Viro 		unsigned long *pos, struct file *file,
6169043476fSAl Viro 		void *dirent, filldir_t filldir)
6179043476fSAl Viro {
6189043476fSAl Viro 	int res;
6199043476fSAl Viro 
6206a75ce16SEric W. Biederman 	if ((*pos)++ < file->f_pos)
6216a75ce16SEric W. Biederman 		return 0;
6229043476fSAl Viro 
6230e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
6240e47c99dSEric W. Biederman 		res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
6250e47c99dSEric W. Biederman 	else
6269043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
6279043476fSAl Viro 
6286a75ce16SEric W. Biederman 	if (res == 0)
6296a75ce16SEric W. Biederman 		file->f_pos = *pos;
6306a75ce16SEric W. Biederman 
6316a75ce16SEric W. Biederman 	return res;
63277b14db5SEric W. Biederman }
63377b14db5SEric W. Biederman 
63477b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
63577b14db5SEric W. Biederman {
6369043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
63777b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
6389043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
6399043476fSAl Viro 	struct ctl_table_header *h = NULL;
6406a75ce16SEric W. Biederman 	struct ctl_table *entry;
6417ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
64277b14db5SEric W. Biederman 	unsigned long pos;
6439043476fSAl Viro 	int ret = -EINVAL;
64477b14db5SEric W. Biederman 
6459043476fSAl Viro 	if (IS_ERR(head))
6469043476fSAl Viro 		return PTR_ERR(head);
6479043476fSAl Viro 
6487ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
64977b14db5SEric W. Biederman 
65077b14db5SEric W. Biederman 	ret = 0;
65177b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
65277b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
65377b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
65477b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
65577b14db5SEric W. Biederman 			goto out;
65677b14db5SEric W. Biederman 		filp->f_pos++;
65777b14db5SEric W. Biederman 	}
65877b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
65977b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
66077b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
66177b14db5SEric W. Biederman 			goto out;
66277b14db5SEric W. Biederman 		filp->f_pos++;
66377b14db5SEric W. Biederman 	}
66477b14db5SEric W. Biederman 	pos = 2;
66577b14db5SEric W. Biederman 
6667ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
6676a75ce16SEric W. Biederman 		ret = scan(h, entry, &pos, filp, dirent, filldir);
6689043476fSAl Viro 		if (ret) {
6699043476fSAl Viro 			sysctl_head_finish(h);
6709043476fSAl Viro 			break;
67177b14db5SEric W. Biederman 		}
67277b14db5SEric W. Biederman 	}
67377b14db5SEric W. Biederman 	ret = 1;
67477b14db5SEric W. Biederman out:
67577b14db5SEric W. Biederman 	sysctl_head_finish(head);
67677b14db5SEric W. Biederman 	return ret;
67777b14db5SEric W. Biederman }
67877b14db5SEric W. Biederman 
67910556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
68077b14db5SEric W. Biederman {
68177b14db5SEric W. Biederman 	/*
68277b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
68377b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
68477b14db5SEric W. Biederman 	 */
685f696a365SMiklos Szeredi 	struct ctl_table_header *head;
686f696a365SMiklos Szeredi 	struct ctl_table *table;
68777b14db5SEric W. Biederman 	int error;
68877b14db5SEric W. Biederman 
689f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
690f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
691f696a365SMiklos Szeredi 		return -EACCES;
692f696a365SMiklos Szeredi 
693f696a365SMiklos Szeredi 	head = grab_header(inode);
6949043476fSAl Viro 	if (IS_ERR(head))
6959043476fSAl Viro 		return PTR_ERR(head);
69677b14db5SEric W. Biederman 
697f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
6989043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
6999043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
7009043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
7011fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
7029043476fSAl Viro 
70377b14db5SEric W. Biederman 	sysctl_head_finish(head);
70477b14db5SEric W. Biederman 	return error;
70577b14db5SEric W. Biederman }
70677b14db5SEric W. Biederman 
70777b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
70877b14db5SEric W. Biederman {
70977b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
71077b14db5SEric W. Biederman 	int error;
71177b14db5SEric W. Biederman 
71277b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
71377b14db5SEric W. Biederman 		return -EPERM;
71477b14db5SEric W. Biederman 
71577b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
7161025774cSChristoph Hellwig 	if (error)
71777b14db5SEric W. Biederman 		return error;
7181025774cSChristoph Hellwig 
7191025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
7201025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
7211025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
7221025774cSChristoph Hellwig 		if (error)
7231025774cSChristoph Hellwig 			return error;
7241025774cSChristoph Hellwig 	}
7251025774cSChristoph Hellwig 
7261025774cSChristoph Hellwig 	setattr_copy(inode, attr);
7271025774cSChristoph Hellwig 	mark_inode_dirty(inode);
7281025774cSChristoph Hellwig 	return 0;
72977b14db5SEric W. Biederman }
73077b14db5SEric W. Biederman 
7319043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
7329043476fSAl Viro {
7339043476fSAl Viro 	struct inode *inode = dentry->d_inode;
7349043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
7359043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
7369043476fSAl Viro 
7379043476fSAl Viro 	if (IS_ERR(head))
7389043476fSAl Viro 		return PTR_ERR(head);
7399043476fSAl Viro 
7409043476fSAl Viro 	generic_fillattr(inode, stat);
7419043476fSAl Viro 	if (table)
7429043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
7439043476fSAl Viro 
7449043476fSAl Viro 	sysctl_head_finish(head);
7459043476fSAl Viro 	return 0;
7469043476fSAl Viro }
7479043476fSAl Viro 
74877b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
749f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
750f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
75177b14db5SEric W. Biederman 	.read		= proc_sys_read,
75277b14db5SEric W. Biederman 	.write		= proc_sys_write,
7536038f373SArnd Bergmann 	.llseek		= default_llseek,
7549043476fSAl Viro };
7559043476fSAl Viro 
7569043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
757887df078SPavel Emelyanov 	.read		= generic_read_dir,
75877b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
7593222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
76077b14db5SEric W. Biederman };
76177b14db5SEric W. Biederman 
76203a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
7639043476fSAl Viro 	.permission	= proc_sys_permission,
7649043476fSAl Viro 	.setattr	= proc_sys_setattr,
7659043476fSAl Viro 	.getattr	= proc_sys_getattr,
7669043476fSAl Viro };
7679043476fSAl Viro 
7689043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
76977b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
77077b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
77177b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
7729043476fSAl Viro 	.getattr	= proc_sys_getattr,
77377b14db5SEric W. Biederman };
77477b14db5SEric W. Biederman 
77577b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
77677b14db5SEric W. Biederman {
77734286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
77834286d66SNick Piggin 		return -ECHILD;
7799043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
7809043476fSAl Viro }
7819043476fSAl Viro 
782fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
7839043476fSAl Viro {
7849043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
7859043476fSAl Viro }
7869043476fSAl Viro 
7871f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
7881f87f0b5SEric W. Biederman {
7891f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
7901f87f0b5SEric W. Biederman 	int res;
7911f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
7921f87f0b5SEric W. Biederman 	if (p->unregistering)
7931f87f0b5SEric W. Biederman 		res = 0;
7941f87f0b5SEric W. Biederman 	else if (!set->is_seen)
7951f87f0b5SEric W. Biederman 		res = 1;
7961f87f0b5SEric W. Biederman 	else
7971f87f0b5SEric W. Biederman 		res = set->is_seen(set);
7981f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
7991f87f0b5SEric W. Biederman 	return res;
8001f87f0b5SEric W. Biederman }
8011f87f0b5SEric W. Biederman 
802621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
803621e155aSNick Piggin 		const struct inode *pinode,
804621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
805621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
8069043476fSAl Viro {
807dfef6dcdSAl Viro 	struct ctl_table_header *head;
80831e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
80931e6b01fSNick Piggin 	 * that inode here can be NULL */
810dfef6dcdSAl Viro 	/* AV: can it, indeed? */
81131e6b01fSNick Piggin 	if (!inode)
812dfef6dcdSAl Viro 		return 1;
813621e155aSNick Piggin 	if (name->len != len)
8149043476fSAl Viro 		return 1;
815621e155aSNick Piggin 	if (memcmp(name->name, str, len))
8169043476fSAl Viro 		return 1;
817dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
818dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
81977b14db5SEric W. Biederman }
82077b14db5SEric W. Biederman 
821d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
82277b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
8239043476fSAl Viro 	.d_delete	= proc_sys_delete,
8249043476fSAl Viro 	.d_compare	= proc_sys_compare,
82577b14db5SEric W. Biederman };
82677b14db5SEric W. Biederman 
8270e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
8287ec66d06SEric W. Biederman 				   const char *name, int namelen)
8291f87f0b5SEric W. Biederman {
8307ec66d06SEric W. Biederman 	struct ctl_table_header *head;
8317ec66d06SEric W. Biederman 	struct ctl_table *entry;
8321f87f0b5SEric W. Biederman 
8330e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
8347ec66d06SEric W. Biederman 	if (!entry)
8357ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
83651f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
8377ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
83851f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
8391f87f0b5SEric W. Biederman }
8401f87f0b5SEric W. Biederman 
8417ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
8427ec66d06SEric W. Biederman 			       const char *name, int namelen)
8431f87f0b5SEric W. Biederman {
8447ec66d06SEric W. Biederman 	struct ctl_table *table;
8457ec66d06SEric W. Biederman 	struct ctl_dir *new;
846ac13ac6fSEric W. Biederman 	struct ctl_node *node;
8477ec66d06SEric W. Biederman 	char *new_name;
8481f87f0b5SEric W. Biederman 
849ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
850ac13ac6fSEric W. Biederman 		      sizeof(struct ctl_table)*2 +  namelen + 1,
851ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
8527ec66d06SEric W. Biederman 	if (!new)
8537ec66d06SEric W. Biederman 		return NULL;
8547ec66d06SEric W. Biederman 
855ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
856ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
8577ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
8587ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
8597ec66d06SEric W. Biederman 	new_name[namelen] = '\0';
8607ec66d06SEric W. Biederman 	table[0].procname = new_name;
8617ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
862ac13ac6fSEric W. Biederman 	init_header(&new->header, set->dir.header.root, set, node, table);
8637ec66d06SEric W. Biederman 
8647ec66d06SEric W. Biederman 	return new;
8651f87f0b5SEric W. Biederman }
8661f87f0b5SEric W. Biederman 
8670e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
8680e47c99dSEric W. Biederman 				  const char *name, int namelen)
8697ec66d06SEric W. Biederman {
8700e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
8717ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
8720eb97f38SEric W. Biederman 	int err;
8737ec66d06SEric W. Biederman 
8747ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
8750e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
8767ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
8777ec66d06SEric W. Biederman 		goto found;
8787ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
8797ec66d06SEric W. Biederman 		goto failed;
8807ec66d06SEric W. Biederman 
8817ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
8827ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
8837ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
8847ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
8857ec66d06SEric W. Biederman 	if (!new)
8867ec66d06SEric W. Biederman 		goto failed;
8877ec66d06SEric W. Biederman 
8880e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
8897ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
8907ec66d06SEric W. Biederman 		goto found;
8917ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
8927ec66d06SEric W. Biederman 		goto failed;
8937ec66d06SEric W. Biederman 
8940eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
8950eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
8960eb97f38SEric W. Biederman 	if (err)
8970e47c99dSEric W. Biederman 		goto failed;
8987ec66d06SEric W. Biederman 	subdir = new;
8997ec66d06SEric W. Biederman found:
9007ec66d06SEric W. Biederman 	subdir->header.nreg++;
9017ec66d06SEric W. Biederman failed:
9027ec66d06SEric W. Biederman 	if (unlikely(IS_ERR(subdir))) {
9036980128fSEric W. Biederman 		printk(KERN_ERR "sysctl could not get directory: ");
9046980128fSEric W. Biederman 		sysctl_print_dir(dir);
9056980128fSEric W. Biederman 		printk(KERN_CONT "/%*.*s %ld\n",
9067ec66d06SEric W. Biederman 			namelen, namelen, name, PTR_ERR(subdir));
9071f87f0b5SEric W. Biederman 	}
9087ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
9097ec66d06SEric W. Biederman 	if (new)
9107ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
9117ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
9127ec66d06SEric W. Biederman 	return subdir;
9131f87f0b5SEric W. Biederman }
9141f87f0b5SEric W. Biederman 
9150e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
9160e47c99dSEric W. Biederman {
9170e47c99dSEric W. Biederman 	struct ctl_dir *parent;
9180e47c99dSEric W. Biederman 	const char *procname;
9190e47c99dSEric W. Biederman 	if (!dir->header.parent)
9200e47c99dSEric W. Biederman 		return &set->dir;
9210e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
9220e47c99dSEric W. Biederman 	if (IS_ERR(parent))
9230e47c99dSEric W. Biederman 		return parent;
9240e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
9250e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
9260e47c99dSEric W. Biederman }
9270e47c99dSEric W. Biederman 
9280e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
9290e47c99dSEric W. Biederman 	struct ctl_table **pentry, struct nsproxy *namespaces)
9300e47c99dSEric W. Biederman {
9310e47c99dSEric W. Biederman 	struct ctl_table_header *head;
9320e47c99dSEric W. Biederman 	struct ctl_table_root *root;
9330e47c99dSEric W. Biederman 	struct ctl_table_set *set;
9340e47c99dSEric W. Biederman 	struct ctl_table *entry;
9350e47c99dSEric W. Biederman 	struct ctl_dir *dir;
9360e47c99dSEric W. Biederman 	int ret;
9370e47c99dSEric W. Biederman 
9380e47c99dSEric W. Biederman 	/* Get out quickly if not a link */
9390e47c99dSEric W. Biederman 	if (!S_ISLNK((*pentry)->mode))
9400e47c99dSEric W. Biederman 		return 0;
9410e47c99dSEric W. Biederman 
9420e47c99dSEric W. Biederman 	ret = 0;
9430e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
9440e47c99dSEric W. Biederman 	root = (*pentry)->data;
9450e47c99dSEric W. Biederman 	set = lookup_header_set(root, namespaces);
9460e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
9470e47c99dSEric W. Biederman 	if (IS_ERR(dir))
9480e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
9490e47c99dSEric W. Biederman 	else {
9500e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
9510e47c99dSEric W. Biederman 		head = NULL;
9520e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
9530e47c99dSEric W. Biederman 		ret = -ENOENT;
9540e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
9550e47c99dSEric W. Biederman 			unuse_table(*phead);
9560e47c99dSEric W. Biederman 			*phead = head;
9570e47c99dSEric W. Biederman 			*pentry = entry;
9580e47c99dSEric W. Biederman 			ret = 0;
9590e47c99dSEric W. Biederman 		}
9600e47c99dSEric W. Biederman 	}
9610e47c99dSEric W. Biederman 
9620e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
9630e47c99dSEric W. Biederman 	return ret;
9640e47c99dSEric W. Biederman }
9650e47c99dSEric W. Biederman 
9667c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
9677c60c48fSEric W. Biederman {
9687c60c48fSEric W. Biederman 	struct va_format vaf;
9697c60c48fSEric W. Biederman 	va_list args;
9707c60c48fSEric W. Biederman 
9717c60c48fSEric W. Biederman 	va_start(args, fmt);
9727c60c48fSEric W. Biederman 	vaf.fmt = fmt;
9737c60c48fSEric W. Biederman 	vaf.va = &args;
9747c60c48fSEric W. Biederman 
9757c60c48fSEric W. Biederman 	printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
9767c60c48fSEric W. Biederman 		path, table->procname, &vaf);
9777c60c48fSEric W. Biederman 
9787c60c48fSEric W. Biederman 	va_end(args);
9797c60c48fSEric W. Biederman 	return -EINVAL;
9807c60c48fSEric W. Biederman }
9817c60c48fSEric W. Biederman 
9827c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
9837c60c48fSEric W. Biederman {
9847c60c48fSEric W. Biederman 	int err = 0;
9857c60c48fSEric W. Biederman 	for (; table->procname; table++) {
9867c60c48fSEric W. Biederman 		if (table->child)
9877c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "Not a file");
9887c60c48fSEric W. Biederman 
9891f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
9901f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
9911f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
9921f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
9931f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
9941f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
9951f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
9961f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
9971f87f0b5SEric W. Biederman 			if (!table->data)
9987c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No data");
9991f87f0b5SEric W. Biederman 			if (!table->maxlen)
10007c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No maxlen");
10011f87f0b5SEric W. Biederman 		}
10021f87f0b5SEric W. Biederman 		if (!table->proc_handler)
10037c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "No proc_handler");
10047c60c48fSEric W. Biederman 
10057c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
10067c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "bogus .mode 0%o",
10077c60c48fSEric W. Biederman 				table->mode);
10081f87f0b5SEric W. Biederman 	}
10097c60c48fSEric W. Biederman 	return err;
10101f87f0b5SEric W. Biederman }
10111f87f0b5SEric W. Biederman 
10120e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
10130e47c99dSEric W. Biederman 	struct ctl_table_root *link_root)
10140e47c99dSEric W. Biederman {
10150e47c99dSEric W. Biederman 	struct ctl_table *link_table, *entry, *link;
10160e47c99dSEric W. Biederman 	struct ctl_table_header *links;
1017ac13ac6fSEric W. Biederman 	struct ctl_node *node;
10180e47c99dSEric W. Biederman 	char *link_name;
10190e47c99dSEric W. Biederman 	int nr_entries, name_bytes;
10200e47c99dSEric W. Biederman 
10210e47c99dSEric W. Biederman 	name_bytes = 0;
10220e47c99dSEric W. Biederman 	nr_entries = 0;
10230e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
10240e47c99dSEric W. Biederman 		nr_entries++;
10250e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
10260e47c99dSEric W. Biederman 	}
10270e47c99dSEric W. Biederman 
10280e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
1029ac13ac6fSEric W. Biederman 			sizeof(struct ctl_node)*nr_entries +
10300e47c99dSEric W. Biederman 			sizeof(struct ctl_table)*(nr_entries + 1) +
10310e47c99dSEric W. Biederman 			name_bytes,
10320e47c99dSEric W. Biederman 			GFP_KERNEL);
10330e47c99dSEric W. Biederman 
10340e47c99dSEric W. Biederman 	if (!links)
10350e47c99dSEric W. Biederman 		return NULL;
10360e47c99dSEric W. Biederman 
1037ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
1038ac13ac6fSEric W. Biederman 	link_table = (struct ctl_table *)(node + nr_entries);
10390e47c99dSEric W. Biederman 	link_name = (char *)&link_table[nr_entries + 1];
10400e47c99dSEric W. Biederman 
10410e47c99dSEric W. Biederman 	for (link = link_table, entry = table; entry->procname; link++, entry++) {
10420e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
10430e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
10440e47c99dSEric W. Biederman 		link->procname = link_name;
10450e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
10460e47c99dSEric W. Biederman 		link->data = link_root;
10470e47c99dSEric W. Biederman 		link_name += len;
10480e47c99dSEric W. Biederman 	}
1049ac13ac6fSEric W. Biederman 	init_header(links, dir->header.root, dir->header.set, node, link_table);
10500e47c99dSEric W. Biederman 	links->nreg = nr_entries;
10510e47c99dSEric W. Biederman 
10520e47c99dSEric W. Biederman 	return links;
10530e47c99dSEric W. Biederman }
10540e47c99dSEric W. Biederman 
10550e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
10560e47c99dSEric W. Biederman 	struct ctl_table *table, struct ctl_table_root *link_root)
10570e47c99dSEric W. Biederman {
10580e47c99dSEric W. Biederman 	struct ctl_table_header *head;
10590e47c99dSEric W. Biederman 	struct ctl_table *entry, *link;
10600e47c99dSEric W. Biederman 
10610e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
10620e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
10630e47c99dSEric W. Biederman 		const char *procname = entry->procname;
10640e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
10650e47c99dSEric W. Biederman 		if (!link)
10660e47c99dSEric W. Biederman 			return false;
10670e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
10680e47c99dSEric W. Biederman 			continue;
10690e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
10700e47c99dSEric W. Biederman 			continue;
10710e47c99dSEric W. Biederman 		return false;
10720e47c99dSEric W. Biederman 	}
10730e47c99dSEric W. Biederman 
10740e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
10750e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
10760e47c99dSEric W. Biederman 		const char *procname = entry->procname;
10770e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
10780e47c99dSEric W. Biederman 		head->nreg++;
10790e47c99dSEric W. Biederman 	}
10800e47c99dSEric W. Biederman 	return true;
10810e47c99dSEric W. Biederman }
10820e47c99dSEric W. Biederman 
10830e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
10840e47c99dSEric W. Biederman {
10850e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
10860e47c99dSEric W. Biederman 	struct ctl_dir *core_parent = NULL;
10870e47c99dSEric W. Biederman 	struct ctl_table_header *links;
10880e47c99dSEric W. Biederman 	int err;
10890e47c99dSEric W. Biederman 
10900e47c99dSEric W. Biederman 	if (head->set == root_set)
10910e47c99dSEric W. Biederman 		return 0;
10920e47c99dSEric W. Biederman 
10930e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
10940e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
10950e47c99dSEric W. Biederman 		return 0;
10960e47c99dSEric W. Biederman 
10970e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root))
10980e47c99dSEric W. Biederman 		return 0;
10990e47c99dSEric W. Biederman 
11000e47c99dSEric W. Biederman 	core_parent->header.nreg++;
11010e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
11020e47c99dSEric W. Biederman 
11030e47c99dSEric W. Biederman 	links = new_links(core_parent, head->ctl_table, head->root);
11040e47c99dSEric W. Biederman 
11050e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
11060e47c99dSEric W. Biederman 	err = -ENOMEM;
11070e47c99dSEric W. Biederman 	if (!links)
11080e47c99dSEric W. Biederman 		goto out;
11090e47c99dSEric W. Biederman 
11100e47c99dSEric W. Biederman 	err = 0;
11110e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root)) {
11120e47c99dSEric W. Biederman 		kfree(links);
11130e47c99dSEric W. Biederman 		goto out;
11140e47c99dSEric W. Biederman 	}
11150e47c99dSEric W. Biederman 
11160e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
11170e47c99dSEric W. Biederman 	if (err)
11180e47c99dSEric W. Biederman 		kfree(links);
11190e47c99dSEric W. Biederman out:
11200e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
11210e47c99dSEric W. Biederman 	return err;
11220e47c99dSEric W. Biederman }
11230e47c99dSEric W. Biederman 
11241f87f0b5SEric W. Biederman /**
1125f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
112660a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
11271f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
11281f87f0b5SEric W. Biederman  * @table: the top-level table structure
11291f87f0b5SEric W. Biederman  *
11301f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
11311f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
11321f87f0b5SEric W. Biederman  *
11331f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
11341f87f0b5SEric W. Biederman  *
11351f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
11361f87f0b5SEric W. Biederman  *            enter a sysctl file
11371f87f0b5SEric W. Biederman  *
11381f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
11391f87f0b5SEric W. Biederman  *
11401f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
11411f87f0b5SEric W. Biederman  *
1142f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
11431f87f0b5SEric W. Biederman  *
1144f728019bSEric W. Biederman  * child - must be %NULL.
11451f87f0b5SEric W. Biederman  *
11461f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
11471f87f0b5SEric W. Biederman  *
11481f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
11491f87f0b5SEric W. Biederman  *
11501f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
11511f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
11521f87f0b5SEric W. Biederman  *
1153f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1154f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
11551f87f0b5SEric W. Biederman  *
11561f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
11571f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
11581f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
11591f87f0b5SEric W. Biederman  *
11601f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
11611f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
11621f87f0b5SEric W. Biederman  *
11631f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
11641f87f0b5SEric W. Biederman  * to the table header on success.
11651f87f0b5SEric W. Biederman  */
11666e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
116760a47a2eSEric W. Biederman 	struct ctl_table_set *set,
11686e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
11691f87f0b5SEric W. Biederman {
117060a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
11711f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
11726e9d5164SEric W. Biederman 	const char *name, *nextname;
11737ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1174ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1175ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1176ac13ac6fSEric W. Biederman 	int nr_entries = 0;
11771f87f0b5SEric W. Biederman 
1178ac13ac6fSEric W. Biederman 	for (entry = table; entry->procname; entry++)
1179ac13ac6fSEric W. Biederman 		nr_entries++;
1180ac13ac6fSEric W. Biederman 
1181ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1182ac13ac6fSEric W. Biederman 			 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
11831f87f0b5SEric W. Biederman 	if (!header)
11841f87f0b5SEric W. Biederman 		return NULL;
11851f87f0b5SEric W. Biederman 
1186ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1187ac13ac6fSEric W. Biederman 	init_header(header, root, set, node, table);
11887c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
11897c60c48fSEric W. Biederman 		goto fail;
11908d6ecfccSEric W. Biederman 
11911f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
11920e47c99dSEric W. Biederman 	dir = &set->dir;
11937ec66d06SEric W. Biederman 	dir->header.nreg++;
11947ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
11957ec66d06SEric W. Biederman 
11967ec66d06SEric W. Biederman 	/* Find the directory for the ctl_table */
11977ec66d06SEric W. Biederman 	for (name = path; name; name = nextname) {
11987ec66d06SEric W. Biederman 		int namelen;
11997ec66d06SEric W. Biederman 		nextname = strchr(name, '/');
12007ec66d06SEric W. Biederman 		if (nextname) {
12017ec66d06SEric W. Biederman 			namelen = nextname - name;
12027ec66d06SEric W. Biederman 			nextname++;
12037ec66d06SEric W. Biederman 		} else {
12047ec66d06SEric W. Biederman 			namelen = strlen(name);
12057ec66d06SEric W. Biederman 		}
12067ec66d06SEric W. Biederman 		if (namelen == 0)
12071f87f0b5SEric W. Biederman 			continue;
12087ec66d06SEric W. Biederman 
12090e47c99dSEric W. Biederman 		dir = get_subdir(dir, name, namelen);
12107ec66d06SEric W. Biederman 		if (IS_ERR(dir))
12117ec66d06SEric W. Biederman 			goto fail;
12121f87f0b5SEric W. Biederman 	}
12130e47c99dSEric W. Biederman 
12147ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
12150e47c99dSEric W. Biederman 	if (insert_header(dir, header))
12160e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
12170e47c99dSEric W. Biederman 
12187ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
12191f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
12201f87f0b5SEric W. Biederman 
12211f87f0b5SEric W. Biederman 	return header;
12220e47c99dSEric W. Biederman 
12237ec66d06SEric W. Biederman fail_put_dir_locked:
12247ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
12257c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
12267c60c48fSEric W. Biederman fail:
12277c60c48fSEric W. Biederman 	kfree(header);
12287c60c48fSEric W. Biederman 	dump_stack();
12297c60c48fSEric W. Biederman 	return NULL;
12301f87f0b5SEric W. Biederman }
12311f87f0b5SEric W. Biederman 
1232fea478d4SEric W. Biederman /**
1233fea478d4SEric W. Biederman  * register_sysctl - register a sysctl table
1234fea478d4SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1235fea478d4SEric W. Biederman  * @table: the table structure
1236fea478d4SEric W. Biederman  *
1237fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1238fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1239fea478d4SEric W. Biederman  *
1240fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1241fea478d4SEric W. Biederman  */
1242fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1243fea478d4SEric W. Biederman {
1244fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
1245fea478d4SEric W. Biederman 					path, table);
1246fea478d4SEric W. Biederman }
1247fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl);
1248fea478d4SEric W. Biederman 
12496e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
12506e9d5164SEric W. Biederman {
12516e9d5164SEric W. Biederman 	int namelen;
12526e9d5164SEric W. Biederman 	namelen = strlen(name);
12536e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
12546e9d5164SEric W. Biederman 		return NULL;
12556e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
12566e9d5164SEric W. Biederman 	pos[namelen] = '/';
12576e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
12586e9d5164SEric W. Biederman 	pos += namelen + 1;
12596e9d5164SEric W. Biederman 	return pos;
12606e9d5164SEric W. Biederman }
12616e9d5164SEric W. Biederman 
1262f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1263f728019bSEric W. Biederman {
1264f728019bSEric W. Biederman 	int has_files = 0;
1265f728019bSEric W. Biederman 	int nr_subheaders = 0;
1266f728019bSEric W. Biederman 	struct ctl_table *entry;
1267f728019bSEric W. Biederman 
1268f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1269f728019bSEric W. Biederman 	if (!table || !table->procname)
1270f728019bSEric W. Biederman 		return 1;
1271f728019bSEric W. Biederman 
1272f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1273f728019bSEric W. Biederman 		if (entry->child)
1274f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1275f728019bSEric W. Biederman 		else
1276f728019bSEric W. Biederman 			has_files = 1;
1277f728019bSEric W. Biederman 	}
1278f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1279f728019bSEric W. Biederman }
1280f728019bSEric W. Biederman 
1281f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
128260a47a2eSEric W. Biederman 	struct ctl_table_header ***subheader, struct ctl_table_set *set,
1283f728019bSEric W. Biederman 	struct ctl_table *table)
1284f728019bSEric W. Biederman {
1285f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1286f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1287f728019bSEric W. Biederman 	int nr_files = 0;
1288f728019bSEric W. Biederman 	int nr_dirs = 0;
1289f728019bSEric W. Biederman 	int err = -ENOMEM;
1290f728019bSEric W. Biederman 
1291f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1292f728019bSEric W. Biederman 		if (entry->child)
1293f728019bSEric W. Biederman 			nr_dirs++;
1294f728019bSEric W. Biederman 		else
1295f728019bSEric W. Biederman 			nr_files++;
1296f728019bSEric W. Biederman 	}
1297f728019bSEric W. Biederman 
1298f728019bSEric W. Biederman 	files = table;
1299f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1300f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1301f728019bSEric W. Biederman 		struct ctl_table *new;
1302f728019bSEric W. Biederman 		files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1303f728019bSEric W. Biederman 				GFP_KERNEL);
1304f728019bSEric W. Biederman 		if (!files)
1305f728019bSEric W. Biederman 			goto out;
1306f728019bSEric W. Biederman 
1307f728019bSEric W. Biederman 		ctl_table_arg = files;
1308f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1309f728019bSEric W. Biederman 			if (entry->child)
1310f728019bSEric W. Biederman 				continue;
1311f728019bSEric W. Biederman 			*new = *entry;
1312f728019bSEric W. Biederman 			new++;
1313f728019bSEric W. Biederman 		}
1314f728019bSEric W. Biederman 	}
1315f728019bSEric W. Biederman 
1316f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1317f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1318f728019bSEric W. Biederman 		struct ctl_table_header *header;
131960a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, path, files);
1320f728019bSEric W. Biederman 		if (!header) {
1321f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1322f728019bSEric W. Biederman 			goto out;
1323f728019bSEric W. Biederman 		}
1324f728019bSEric W. Biederman 
1325f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1326f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1327f728019bSEric W. Biederman 		**subheader = header;
1328f728019bSEric W. Biederman 		(*subheader)++;
1329f728019bSEric W. Biederman 	}
1330f728019bSEric W. Biederman 
1331f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1332f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1333f728019bSEric W. Biederman 		char *child_pos;
1334f728019bSEric W. Biederman 
1335f728019bSEric W. Biederman 		if (!entry->child)
1336f728019bSEric W. Biederman 			continue;
1337f728019bSEric W. Biederman 
1338f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1339f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1340f728019bSEric W. Biederman 		if (!child_pos)
1341f728019bSEric W. Biederman 			goto out;
1342f728019bSEric W. Biederman 
1343f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
134460a47a2eSEric W. Biederman 						  set, entry->child);
1345f728019bSEric W. Biederman 		pos[0] = '\0';
1346f728019bSEric W. Biederman 		if (err)
1347f728019bSEric W. Biederman 			goto out;
1348f728019bSEric W. Biederman 	}
1349f728019bSEric W. Biederman 	err = 0;
1350f728019bSEric W. Biederman out:
1351f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1352f728019bSEric W. Biederman 	return err;
1353f728019bSEric W. Biederman }
1354f728019bSEric W. Biederman 
13556e9d5164SEric W. Biederman /**
13566e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
135760a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
13586e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
13596e9d5164SEric W. Biederman  * @table: the top-level table structure
13606e9d5164SEric W. Biederman  *
13616e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
13626e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
13636e9d5164SEric W. Biederman  *
13646e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
13656e9d5164SEric W. Biederman  */
13666e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
136760a47a2eSEric W. Biederman 	struct ctl_table_set *set,
13686e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
13696e9d5164SEric W. Biederman {
1370ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1371f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1372f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
13736e9d5164SEric W. Biederman 	const struct ctl_path *component;
13746e9d5164SEric W. Biederman 	char *new_path, *pos;
13756e9d5164SEric W. Biederman 
13766e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
13776e9d5164SEric W. Biederman 	if (!new_path)
13786e9d5164SEric W. Biederman 		return NULL;
13796e9d5164SEric W. Biederman 
13806e9d5164SEric W. Biederman 	pos[0] = '\0';
13816e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
13826e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
13836e9d5164SEric W. Biederman 		if (!pos)
13846e9d5164SEric W. Biederman 			goto out;
13856e9d5164SEric W. Biederman 	}
1386ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1387ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1388ec6a5266SEric W. Biederman 		if (!pos)
1389ec6a5266SEric W. Biederman 			goto out;
1390ec6a5266SEric W. Biederman 		table = table->child;
1391ec6a5266SEric W. Biederman 	}
1392f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
139360a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, new_path, table);
1394ec6a5266SEric W. Biederman 		if (header)
1395ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1396f728019bSEric W. Biederman 	} else {
1397f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1398f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1399f728019bSEric W. Biederman 		if (!header)
1400f728019bSEric W. Biederman 			goto out;
1401f728019bSEric W. Biederman 
1402f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1403f728019bSEric W. Biederman 		subheader = subheaders;
1404f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1405f728019bSEric W. Biederman 
1406f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
140760a47a2eSEric W. Biederman 						set, table))
1408f728019bSEric W. Biederman 			goto err_register_leaves;
1409f728019bSEric W. Biederman 	}
1410f728019bSEric W. Biederman 
14116e9d5164SEric W. Biederman out:
14126e9d5164SEric W. Biederman 	kfree(new_path);
14136e9d5164SEric W. Biederman 	return header;
1414f728019bSEric W. Biederman 
1415f728019bSEric W. Biederman err_register_leaves:
1416f728019bSEric W. Biederman 	while (subheader > subheaders) {
1417f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1418f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1419f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1420f728019bSEric W. Biederman 		kfree(table);
1421f728019bSEric W. Biederman 	}
1422f728019bSEric W. Biederman 	kfree(header);
1423f728019bSEric W. Biederman 	header = NULL;
1424f728019bSEric W. Biederman 	goto out;
14256e9d5164SEric W. Biederman }
14266e9d5164SEric W. Biederman 
14271f87f0b5SEric W. Biederman /**
14281f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
14291f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
14301f87f0b5SEric W. Biederman  * @table: the top-level table structure
14311f87f0b5SEric W. Biederman  *
14321f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
14331f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
14341f87f0b5SEric W. Biederman  *
14351f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
14361f87f0b5SEric W. Biederman  */
14371f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
14381f87f0b5SEric W. Biederman 						struct ctl_table *table)
14391f87f0b5SEric W. Biederman {
144060a47a2eSEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root.default_set,
14411f87f0b5SEric W. Biederman 					path, table);
14421f87f0b5SEric W. Biederman }
14431f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
14441f87f0b5SEric W. Biederman 
14451f87f0b5SEric W. Biederman /**
14461f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
14471f87f0b5SEric W. Biederman  * @table: the top-level table structure
14481f87f0b5SEric W. Biederman  *
14491f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
14501f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
14511f87f0b5SEric W. Biederman  *
14521f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
14531f87f0b5SEric W. Biederman  */
14541f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
14551f87f0b5SEric W. Biederman {
14561f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
14571f87f0b5SEric W. Biederman 
14581f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
14591f87f0b5SEric W. Biederman }
14601f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
14611f87f0b5SEric W. Biederman 
14620e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
14630e47c99dSEric W. Biederman {
14640e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
14650e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
14660e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
14670e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
14680e47c99dSEric W. Biederman 	struct ctl_table *entry;
14690e47c99dSEric W. Biederman 
14700e47c99dSEric W. Biederman 	if (header->set == root_set)
14710e47c99dSEric W. Biederman 		return;
14720e47c99dSEric W. Biederman 
14730e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
14740e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
14750e47c99dSEric W. Biederman 		return;
14760e47c99dSEric W. Biederman 
14770e47c99dSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
14780e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
14790e47c99dSEric W. Biederman 		struct ctl_table *link;
14800e47c99dSEric W. Biederman 		const char *name = entry->procname;
14810e47c99dSEric W. Biederman 
14820e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
14830e47c99dSEric W. Biederman 		if (link &&
14840e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
14850e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
14860e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
14870e47c99dSEric W. Biederman 		}
14880e47c99dSEric W. Biederman 		else {
14890e47c99dSEric W. Biederman 			printk(KERN_ERR "sysctl link missing during unregister: ");
14900e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
14910e47c99dSEric W. Biederman 			printk(KERN_CONT "/%s\n", name);
14920e47c99dSEric W. Biederman 		}
14930e47c99dSEric W. Biederman 	}
14940e47c99dSEric W. Biederman }
14950e47c99dSEric W. Biederman 
1496938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1497938aaa4fSEric W. Biederman {
14987ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
14997ec66d06SEric W. Biederman 
1500938aaa4fSEric W. Biederman 	if (--header->nreg)
1501938aaa4fSEric W. Biederman 		return;
1502938aaa4fSEric W. Biederman 
15030e47c99dSEric W. Biederman 	put_links(header);
1504938aaa4fSEric W. Biederman 	start_unregistering(header);
1505938aaa4fSEric W. Biederman 	if (!--header->count)
1506938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
15077ec66d06SEric W. Biederman 
15087ec66d06SEric W. Biederman 	if (parent)
15097ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1510938aaa4fSEric W. Biederman }
1511938aaa4fSEric W. Biederman 
15121f87f0b5SEric W. Biederman /**
15131f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
15141f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
15151f87f0b5SEric W. Biederman  *
15161f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
15171f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
15181f87f0b5SEric W. Biederman  */
15191f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
15201f87f0b5SEric W. Biederman {
1521f728019bSEric W. Biederman 	int nr_subheaders;
15221f87f0b5SEric W. Biederman 	might_sleep();
15231f87f0b5SEric W. Biederman 
15241f87f0b5SEric W. Biederman 	if (header == NULL)
15251f87f0b5SEric W. Biederman 		return;
15261f87f0b5SEric W. Biederman 
1527f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1528f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1529f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1530f728019bSEric W. Biederman 		int i;
1531f728019bSEric W. Biederman 
1532f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1533f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1534f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1535f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1536f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1537f728019bSEric W. Biederman 			kfree(table);
1538f728019bSEric W. Biederman 		}
1539f728019bSEric W. Biederman 		kfree(header);
1540f728019bSEric W. Biederman 		return;
1541f728019bSEric W. Biederman 	}
1542f728019bSEric W. Biederman 
15431f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1544938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
15451f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
15461f87f0b5SEric W. Biederman }
15471f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
15481f87f0b5SEric W. Biederman 
15490e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
15509eb47c26SEric W. Biederman 	struct ctl_table_root *root,
15511f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
15521f87f0b5SEric W. Biederman {
15531347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
15540e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1555ac13ac6fSEric W. Biederman 	init_header(&set->dir.header, root, set, NULL, root_table);
15561f87f0b5SEric W. Biederman }
15571f87f0b5SEric W. Biederman 
155897324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
155997324cd8SEric W. Biederman {
1560ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
156197324cd8SEric W. Biederman }
15621f87f0b5SEric W. Biederman 
15631e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
156477b14db5SEric W. Biederman {
1565e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1566e1675231SAlexey Dobriyan 
156777b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
15689043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
15699043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
157077b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1571de4e83bdSEric W. Biederman 
1572de4e83bdSEric W. Biederman 	return sysctl_init();
157377b14db5SEric W. Biederman }
1574