xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision e79c6a4f)
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>
887ebdc00SAndrew Morton #include <linux/printk.h>
977b14db5SEric W. Biederman #include <linux/security.h>
1040401530SAl Viro #include <linux/sched.h>
1134286d66SNick Piggin #include <linux/namei.h>
1240401530SAl Viro #include <linux/mm.h>
131f87f0b5SEric W. Biederman #include <linux/module.h>
1477b14db5SEric W. Biederman #include "internal.h"
1577b14db5SEric W. Biederman 
16d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1777b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1803a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
199043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
209043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
2177b14db5SEric W. Biederman 
22f9bd6733SEric W. Biederman /* Support for permanently empty directories */
23f9bd6733SEric W. Biederman 
24f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = {
25f9bd6733SEric W. Biederman 	{ }
26f9bd6733SEric W. Biederman };
27f9bd6733SEric W. Biederman 
28f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head)
29f9bd6733SEric W. Biederman {
30f9bd6733SEric W. Biederman 	return head->ctl_table[0].child == sysctl_mount_point;
31f9bd6733SEric W. Biederman }
32f9bd6733SEric W. Biederman 
33f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir)
34f9bd6733SEric W. Biederman {
35f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = sysctl_mount_point;
36f9bd6733SEric W. Biederman }
37f9bd6733SEric W. Biederman 
38f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir)
39f9bd6733SEric W. Biederman 
40f9bd6733SEric W. Biederman {
41f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = NULL;
42f9bd6733SEric W. Biederman }
43f9bd6733SEric W. Biederman 
44f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
45f1ecf068SLucas De Marchi {
46f1ecf068SLucas De Marchi 	if (!poll)
47f1ecf068SLucas De Marchi 		return;
48f1ecf068SLucas De Marchi 
49f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
50f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
51f1ecf068SLucas De Marchi }
52f1ecf068SLucas De Marchi 
53a194558eSEric W. Biederman static struct ctl_table root_table[] = {
54a194558eSEric W. Biederman 	{
55a194558eSEric W. Biederman 		.procname = "",
567ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
57a194558eSEric W. Biederman 	},
58a194558eSEric W. Biederman 	{ }
59a194558eSEric W. Biederman };
600e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
610e47c99dSEric W. Biederman 	.default_set.dir.header = {
621f87f0b5SEric W. Biederman 		{{.count = 1,
63938aaa4fSEric W. Biederman 		  .nreg = 1,
64ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
650e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
661f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
671f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
687ec66d06SEric W. Biederman 	},
691f87f0b5SEric W. Biederman };
701f87f0b5SEric W. Biederman 
711f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
721f87f0b5SEric W. Biederman 
737ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
740e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
750e47c99dSEric W. Biederman 	struct ctl_table **pentry, struct nsproxy *namespaces);
760e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
770e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
787ec66d06SEric W. Biederman 
796980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
806980128fSEric W. Biederman {
816980128fSEric W. Biederman 	if (dir->header.parent)
826980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
8387ebdc00SAndrew Morton 	pr_cont("%s/", dir->header.ctl_table[0].procname);
846980128fSEric W. Biederman }
856980128fSEric W. Biederman 
86076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
87076c3eedSEric W. Biederman {
88076c3eedSEric W. Biederman 	int minlen;
89076c3eedSEric W. Biederman 	int cmp;
90076c3eedSEric W. Biederman 
91076c3eedSEric W. Biederman 	minlen = len1;
92076c3eedSEric W. Biederman 	if (minlen > len2)
93076c3eedSEric W. Biederman 		minlen = len2;
94076c3eedSEric W. Biederman 
95076c3eedSEric W. Biederman 	cmp = memcmp(name1, name2, minlen);
96076c3eedSEric W. Biederman 	if (cmp == 0)
97076c3eedSEric W. Biederman 		cmp = len1 - len2;
98076c3eedSEric W. Biederman 	return cmp;
99076c3eedSEric W. Biederman }
100076c3eedSEric W. Biederman 
10160f126d9SEric W. Biederman /* Called under sysctl_lock */
102076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
1030e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
104076c3eedSEric W. Biederman {
105076c3eedSEric W. Biederman 	struct ctl_table_header *head;
106076c3eedSEric W. Biederman 	struct ctl_table *entry;
107ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
108076c3eedSEric W. Biederman 
109ac13ac6fSEric W. Biederman 	while (node)
110ac13ac6fSEric W. Biederman 	{
111ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
112ac13ac6fSEric W. Biederman 		const char *procname;
113ac13ac6fSEric W. Biederman 		int cmp;
114ac13ac6fSEric W. Biederman 
115ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
116ac13ac6fSEric W. Biederman 		head = ctl_node->header;
117ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
118ac13ac6fSEric W. Biederman 		procname = entry->procname;
119ac13ac6fSEric W. Biederman 
120ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
121ac13ac6fSEric W. Biederman 		if (cmp < 0)
122ac13ac6fSEric W. Biederman 			node = node->rb_left;
123ac13ac6fSEric W. Biederman 		else if (cmp > 0)
124ac13ac6fSEric W. Biederman 			node = node->rb_right;
125ac13ac6fSEric W. Biederman 		else {
126076c3eedSEric W. Biederman 			*phead = head;
127076c3eedSEric W. Biederman 			return entry;
128076c3eedSEric W. Biederman 		}
129076c3eedSEric W. Biederman 	}
130076c3eedSEric W. Biederman 	return NULL;
131076c3eedSEric W. Biederman }
132076c3eedSEric W. Biederman 
133ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
134ac13ac6fSEric W. Biederman {
135ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
136ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
137ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
138ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
139ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
140ac13ac6fSEric W. Biederman 
141ac13ac6fSEric W. Biederman 	while (*p) {
142ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
143ac13ac6fSEric W. Biederman 		struct ctl_table *parent_entry;
144ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
145ac13ac6fSEric W. Biederman 		const char *parent_name;
146ac13ac6fSEric W. Biederman 		int cmp;
147ac13ac6fSEric W. Biederman 
148ac13ac6fSEric W. Biederman 		parent = *p;
149ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
150ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
151ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
152ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
153ac13ac6fSEric W. Biederman 
154ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
155ac13ac6fSEric W. Biederman 		if (cmp < 0)
156ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
157ac13ac6fSEric W. Biederman 		else if (cmp > 0)
158ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
159ac13ac6fSEric W. Biederman 		else {
16087ebdc00SAndrew Morton 			pr_err("sysctl duplicate entry: ");
161ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
16287ebdc00SAndrew Morton 			pr_cont("/%s\n", entry->procname);
163ac13ac6fSEric W. Biederman 			return -EEXIST;
164ac13ac6fSEric W. Biederman 		}
165ac13ac6fSEric W. Biederman 	}
166ac13ac6fSEric W. Biederman 
167ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
168ea5272f5SMichel Lespinasse 	rb_insert_color(node, &head->parent->root);
169ac13ac6fSEric W. Biederman 	return 0;
170ac13ac6fSEric W. Biederman }
171ac13ac6fSEric W. Biederman 
172ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
173ac13ac6fSEric W. Biederman {
174ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
175ac13ac6fSEric W. Biederman 
176ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
177ac13ac6fSEric W. Biederman }
178ac13ac6fSEric W. Biederman 
179e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
180e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
181ac13ac6fSEric W. Biederman 	struct ctl_node *node, struct ctl_table *table)
182e0d04529SEric W. Biederman {
1837ec66d06SEric W. Biederman 	head->ctl_table = table;
184e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
185e0d04529SEric W. Biederman 	head->used = 0;
186e0d04529SEric W. Biederman 	head->count = 1;
187e0d04529SEric W. Biederman 	head->nreg = 1;
188e0d04529SEric W. Biederman 	head->unregistering = NULL;
189e0d04529SEric W. Biederman 	head->root = root;
190e0d04529SEric W. Biederman 	head->set = set;
191e0d04529SEric W. Biederman 	head->parent = NULL;
192ac13ac6fSEric W. Biederman 	head->node = node;
193ac13ac6fSEric W. Biederman 	if (node) {
194ac13ac6fSEric W. Biederman 		struct ctl_table *entry;
1954c199a93SMichel Lespinasse 		for (entry = table; entry->procname; entry++, node++)
196ac13ac6fSEric W. Biederman 			node->header = head;
197ac13ac6fSEric W. Biederman 	}
198ac13ac6fSEric W. Biederman }
199e0d04529SEric W. Biederman 
2008425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
2018425d6aaSEric W. Biederman {
202ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
203ac13ac6fSEric W. Biederman 	for (entry = head->ctl_table; entry->procname; entry++)
204ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
2058425d6aaSEric W. Biederman }
2068425d6aaSEric W. Biederman 
2070e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
2088425d6aaSEric W. Biederman {
209ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
2100e47c99dSEric W. Biederman 	int err;
2110e47c99dSEric W. Biederman 
212f9bd6733SEric W. Biederman 	/* Is this a permanently empty directory? */
213f9bd6733SEric W. Biederman 	if (is_empty_dir(&dir->header))
214f9bd6733SEric W. Biederman 		return -EROFS;
215f9bd6733SEric W. Biederman 
216f9bd6733SEric W. Biederman 	/* Am I creating a permanently empty directory? */
217f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point) {
218f9bd6733SEric W. Biederman 		if (!RB_EMPTY_ROOT(&dir->root))
219f9bd6733SEric W. Biederman 			return -EINVAL;
220f9bd6733SEric W. Biederman 		set_empty_dir(dir);
221f9bd6733SEric W. Biederman 	}
222f9bd6733SEric W. Biederman 
2230e47c99dSEric W. Biederman 	dir->header.nreg++;
2247ec66d06SEric W. Biederman 	header->parent = dir;
2250e47c99dSEric W. Biederman 	err = insert_links(header);
2260e47c99dSEric W. Biederman 	if (err)
2270e47c99dSEric W. Biederman 		goto fail_links;
228ac13ac6fSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
229ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
230ac13ac6fSEric W. Biederman 		if (err)
231ac13ac6fSEric W. Biederman 			goto fail;
232ac13ac6fSEric W. Biederman 	}
2330e47c99dSEric W. Biederman 	return 0;
234ac13ac6fSEric W. Biederman fail:
235ac13ac6fSEric W. Biederman 	erase_header(header);
236ac13ac6fSEric W. Biederman 	put_links(header);
2370e47c99dSEric W. Biederman fail_links:
238f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point)
239f9bd6733SEric W. Biederman 		clear_empty_dir(dir);
2400e47c99dSEric W. Biederman 	header->parent = NULL;
2410e47c99dSEric W. Biederman 	drop_sysctl_table(&dir->header);
2420e47c99dSEric W. Biederman 	return err;
2438425d6aaSEric W. Biederman }
2448425d6aaSEric W. Biederman 
2451f87f0b5SEric W. Biederman /* called under sysctl_lock */
2461f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2471f87f0b5SEric W. Biederman {
2481f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2491f87f0b5SEric W. Biederman 		return 0;
2501f87f0b5SEric W. Biederman 	p->used++;
2511f87f0b5SEric W. Biederman 	return 1;
2521f87f0b5SEric W. Biederman }
2531f87f0b5SEric W. Biederman 
2541f87f0b5SEric W. Biederman /* called under sysctl_lock */
2551f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2561f87f0b5SEric W. Biederman {
2571f87f0b5SEric W. Biederman 	if (!--p->used)
2581f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2591f87f0b5SEric W. Biederman 			complete(p->unregistering);
2601f87f0b5SEric W. Biederman }
2611f87f0b5SEric W. Biederman 
2621f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
2631f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
2641f87f0b5SEric W. Biederman {
2651f87f0b5SEric W. Biederman 	/*
2661f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
2671f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
2681f87f0b5SEric W. Biederman 	 */
2691f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
2701f87f0b5SEric W. Biederman 		struct completion wait;
2711f87f0b5SEric W. Biederman 		init_completion(&wait);
2721f87f0b5SEric W. Biederman 		p->unregistering = &wait;
2731f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
2741f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
2751f87f0b5SEric W. Biederman 		spin_lock(&sysctl_lock);
2761f87f0b5SEric W. Biederman 	} else {
2771f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
2781f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
2791f87f0b5SEric W. Biederman 	}
2801f87f0b5SEric W. Biederman 	/*
2811f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
2821f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
2831f87f0b5SEric W. Biederman 	 */
2848425d6aaSEric W. Biederman 	erase_header(p);
2851f87f0b5SEric W. Biederman }
2861f87f0b5SEric W. Biederman 
2871f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head)
2881f87f0b5SEric W. Biederman {
2891f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2901f87f0b5SEric W. Biederman 	head->count++;
2911f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2921f87f0b5SEric W. Biederman }
2931f87f0b5SEric W. Biederman 
2941f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head)
2951f87f0b5SEric W. Biederman {
2961f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2971f87f0b5SEric W. Biederman 	if (!--head->count)
2981f87f0b5SEric W. Biederman 		kfree_rcu(head, rcu);
2991f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3001f87f0b5SEric W. Biederman }
3011f87f0b5SEric W. Biederman 
3021f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
3031f87f0b5SEric W. Biederman {
304ab4a1f24SPrasad Joshi 	BUG_ON(!head);
3051f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3061f87f0b5SEric W. Biederman 	if (!use_table(head))
3071f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
3081f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3091f87f0b5SEric W. Biederman 	return head;
3101f87f0b5SEric W. Biederman }
3111f87f0b5SEric W. Biederman 
3121f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
3131f87f0b5SEric W. Biederman {
3141f87f0b5SEric W. Biederman 	if (!head)
3151f87f0b5SEric W. Biederman 		return;
3161f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3171f87f0b5SEric W. Biederman 	unuse_table(head);
3181f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3191f87f0b5SEric W. Biederman }
3201f87f0b5SEric W. Biederman 
3211f87f0b5SEric W. Biederman static struct ctl_table_set *
3221f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
3231f87f0b5SEric W. Biederman {
3241f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
3251f87f0b5SEric W. Biederman 	if (root->lookup)
3261f87f0b5SEric W. Biederman 		set = root->lookup(root, namespaces);
3271f87f0b5SEric W. Biederman 	return set;
3281f87f0b5SEric W. Biederman }
3291f87f0b5SEric W. Biederman 
330076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
3317ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
332076c3eedSEric W. Biederman 				      const char *name, int namelen)
333076c3eedSEric W. Biederman {
334076c3eedSEric W. Biederman 	struct ctl_table_header *head;
335076c3eedSEric W. Biederman 	struct ctl_table *entry;
336076c3eedSEric W. Biederman 
337076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3380e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
339076c3eedSEric W. Biederman 	if (entry && use_table(head))
340076c3eedSEric W. Biederman 		*phead = head;
341076c3eedSEric W. Biederman 	else
342076c3eedSEric W. Biederman 		entry = NULL;
343076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
344076c3eedSEric W. Biederman 	return entry;
345076c3eedSEric W. Biederman }
346076c3eedSEric W. Biederman 
347ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3481f87f0b5SEric W. Biederman {
349ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3501f87f0b5SEric W. Biederman 
351ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
352ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
353ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
354ac13ac6fSEric W. Biederman 			return ctl_node;
3551f87f0b5SEric W. Biederman 	}
3561f87f0b5SEric W. Biederman 	return NULL;
3571f87f0b5SEric W. Biederman }
3581f87f0b5SEric W. Biederman 
3597ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
3606a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
3611f87f0b5SEric W. Biederman {
362ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
3637ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
364ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3656a75ce16SEric W. Biederman 
3666a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
367ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
3686a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
369ac13ac6fSEric W. Biederman 	if (ctl_node) {
370ac13ac6fSEric W. Biederman 		head = ctl_node->header;
371ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
372ac13ac6fSEric W. Biederman 	}
3736a75ce16SEric W. Biederman 	*phead = head;
3746a75ce16SEric W. Biederman 	*pentry = entry;
3756a75ce16SEric W. Biederman }
3766a75ce16SEric W. Biederman 
3777ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
3786a75ce16SEric W. Biederman {
3796a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
3806a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
381ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
3826a75ce16SEric W. Biederman 
3836a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
3846a75ce16SEric W. Biederman 	unuse_table(head);
385ac13ac6fSEric W. Biederman 
386ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
3876a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
388ac13ac6fSEric W. Biederman 	head = NULL;
389ac13ac6fSEric W. Biederman 	if (ctl_node) {
390ac13ac6fSEric W. Biederman 		head = ctl_node->header;
391ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
3926a75ce16SEric W. Biederman 	}
3936a75ce16SEric W. Biederman 	*phead = head;
3946a75ce16SEric W. Biederman 	*pentry = entry;
3951f87f0b5SEric W. Biederman }
3961f87f0b5SEric W. Biederman 
3971f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root)
3981f87f0b5SEric W. Biederman {
3991f87f0b5SEric W. Biederman }
4001f87f0b5SEric W. Biederman 
4011f87f0b5SEric W. Biederman /*
4021f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
4031f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
4041f87f0b5SEric W. Biederman  */
4051f87f0b5SEric W. Biederman 
4061f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
4071f87f0b5SEric W. Biederman {
408091bd3eaSEric W. Biederman 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
4091f87f0b5SEric W. Biederman 		mode >>= 6;
410091bd3eaSEric W. Biederman 	else if (in_egroup_p(GLOBAL_ROOT_GID))
4111f87f0b5SEric W. Biederman 		mode >>= 3;
4121f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
4131f87f0b5SEric W. Biederman 		return 0;
4141f87f0b5SEric W. Biederman 	return -EACCES;
4151f87f0b5SEric W. Biederman }
4161f87f0b5SEric W. Biederman 
41773f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
4181f87f0b5SEric W. Biederman {
41973f7ef43SEric W. Biederman 	struct ctl_table_root *root = head->root;
4201f87f0b5SEric W. Biederman 	int mode;
4211f87f0b5SEric W. Biederman 
4221f87f0b5SEric W. Biederman 	if (root->permissions)
42373f7ef43SEric W. Biederman 		mode = root->permissions(head, table);
4241f87f0b5SEric W. Biederman 	else
4251f87f0b5SEric W. Biederman 		mode = table->mode;
4261f87f0b5SEric W. Biederman 
4271f87f0b5SEric W. Biederman 	return test_perm(mode, op);
4281f87f0b5SEric W. Biederman }
4291f87f0b5SEric W. Biederman 
4309043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
4319043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
43277b14db5SEric W. Biederman {
433e79c6a4fSDmitry Torokhov 	struct ctl_table_root *root = head->root;
43477b14db5SEric W. Biederman 	struct inode *inode;
4359043476fSAl Viro 	struct proc_inode *ei;
43677b14db5SEric W. Biederman 
4379043476fSAl Viro 	inode = new_inode(sb);
43877b14db5SEric W. Biederman 	if (!inode)
43977b14db5SEric W. Biederman 		goto out;
44077b14db5SEric W. Biederman 
44185fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
44285fe4025SChristoph Hellwig 
4439043476fSAl Viro 	sysctl_head_get(head);
44477b14db5SEric W. Biederman 	ei = PROC_I(inode);
4459043476fSAl Viro 	ei->sysctl = head;
4469043476fSAl Viro 	ei->sysctl_entry = table;
4479043476fSAl Viro 
44877b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4499043476fSAl Viro 	inode->i_mode = table->mode;
4507ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4519043476fSAl Viro 		inode->i_mode |= S_IFREG;
45277b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
45377b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4549043476fSAl Viro 	} else {
4559043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4569043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4579043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
458f9bd6733SEric W. Biederman 		if (is_empty_dir(head))
459f9bd6733SEric W. Biederman 			make_empty_dir_inode(inode);
4609043476fSAl Viro 	}
461e79c6a4fSDmitry Torokhov 
462e79c6a4fSDmitry Torokhov 	if (root->set_ownership)
463e79c6a4fSDmitry Torokhov 		root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
464e79c6a4fSDmitry Torokhov 
46577b14db5SEric W. Biederman out:
46677b14db5SEric W. Biederman 	return inode;
46777b14db5SEric W. Biederman }
46877b14db5SEric W. Biederman 
46981324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
47077b14db5SEric W. Biederman {
4713cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
4723cc3e046SEric W. Biederman 	if (!head)
4730e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
4743cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
47577b14db5SEric W. Biederman }
47677b14db5SEric W. Biederman 
47777b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
47800cd8dd3SAl Viro 					unsigned int flags)
47977b14db5SEric W. Biederman {
4809043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
4819043476fSAl Viro 	struct ctl_table_header *h = NULL;
4829043476fSAl Viro 	struct qstr *name = &dentry->d_name;
4839043476fSAl Viro 	struct ctl_table *p;
48477b14db5SEric W. Biederman 	struct inode *inode;
4859043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
4867ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
4870e47c99dSEric W. Biederman 	int ret;
48877b14db5SEric W. Biederman 
4899043476fSAl Viro 	if (IS_ERR(head))
4909043476fSAl Viro 		return ERR_CAST(head);
4919043476fSAl Viro 
4927ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
4939043476fSAl Viro 
4947ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
4959043476fSAl Viro 	if (!p)
49677b14db5SEric W. Biederman 		goto out;
49777b14db5SEric W. Biederman 
4984e757320SEric W. Biederman 	if (S_ISLNK(p->mode)) {
4990e47c99dSEric W. Biederman 		ret = sysctl_follow_link(&h, &p, current->nsproxy);
5000e47c99dSEric W. Biederman 		err = ERR_PTR(ret);
5010e47c99dSEric W. Biederman 		if (ret)
5020e47c99dSEric W. Biederman 			goto out;
5034e757320SEric W. Biederman 	}
5040e47c99dSEric W. Biederman 
50577b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
5069043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
50777b14db5SEric W. Biederman 	if (!inode)
50877b14db5SEric W. Biederman 		goto out;
50977b14db5SEric W. Biederman 
51077b14db5SEric W. Biederman 	err = NULL;
511fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
51277b14db5SEric W. Biederman 	d_add(dentry, inode);
51377b14db5SEric W. Biederman 
51477b14db5SEric W. Biederman out:
5156bf61045SFrancesco Ruggeri 	if (h)
5166bf61045SFrancesco Ruggeri 		sysctl_head_finish(h);
51777b14db5SEric W. Biederman 	sysctl_head_finish(head);
51877b14db5SEric W. Biederman 	return err;
51977b14db5SEric W. Biederman }
52077b14db5SEric W. Biederman 
5217708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
5227708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
52377b14db5SEric W. Biederman {
524496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
5259043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5269043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5272a2da53bSDavid Howells 	ssize_t error;
5282a2da53bSDavid Howells 	size_t res;
52977b14db5SEric W. Biederman 
5309043476fSAl Viro 	if (IS_ERR(head))
5319043476fSAl Viro 		return PTR_ERR(head);
53277b14db5SEric W. Biederman 
53377b14db5SEric W. Biederman 	/*
53477b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
53577b14db5SEric W. Biederman 	 * and won't be until we finish.
53677b14db5SEric W. Biederman 	 */
53777b14db5SEric W. Biederman 	error = -EPERM;
53873f7ef43SEric W. Biederman 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
53977b14db5SEric W. Biederman 		goto out;
54077b14db5SEric W. Biederman 
5419043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
5429043476fSAl Viro 	error = -EINVAL;
5439043476fSAl Viro 	if (!table->proc_handler)
5449043476fSAl Viro 		goto out;
5459043476fSAl Viro 
54677b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
54777b14db5SEric W. Biederman 	res = count;
5488d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
54977b14db5SEric W. Biederman 	if (!error)
55077b14db5SEric W. Biederman 		error = res;
55177b14db5SEric W. Biederman out:
55277b14db5SEric W. Biederman 	sysctl_head_finish(head);
55377b14db5SEric W. Biederman 
55477b14db5SEric W. Biederman 	return error;
55577b14db5SEric W. Biederman }
55677b14db5SEric W. Biederman 
5577708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
5587708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
5597708bfb1SPavel Emelyanov {
5607708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
5617708bfb1SPavel Emelyanov }
5627708bfb1SPavel Emelyanov 
56377b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
56477b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
56577b14db5SEric W. Biederman {
5667708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
56777b14db5SEric W. Biederman }
56877b14db5SEric W. Biederman 
569f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
570f1ecf068SLucas De Marchi {
5714e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
572f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
573f1ecf068SLucas De Marchi 
5744e474a00SLucas De Marchi 	/* sysctl was unregistered */
5754e474a00SLucas De Marchi 	if (IS_ERR(head))
5764e474a00SLucas De Marchi 		return PTR_ERR(head);
5774e474a00SLucas De Marchi 
578f1ecf068SLucas De Marchi 	if (table->poll)
579f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
580f1ecf068SLucas De Marchi 
5814e474a00SLucas De Marchi 	sysctl_head_finish(head);
5824e474a00SLucas De Marchi 
583f1ecf068SLucas De Marchi 	return 0;
584f1ecf068SLucas De Marchi }
585f1ecf068SLucas De Marchi 
586f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
587f1ecf068SLucas De Marchi {
588496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
5894e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
590f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
591f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
5924e474a00SLucas De Marchi 	unsigned long event;
5934e474a00SLucas De Marchi 
5944e474a00SLucas De Marchi 	/* sysctl was unregistered */
5954e474a00SLucas De Marchi 	if (IS_ERR(head))
5964e474a00SLucas De Marchi 		return POLLERR | POLLHUP;
597f1ecf068SLucas De Marchi 
598f1ecf068SLucas De Marchi 	if (!table->proc_handler)
599f1ecf068SLucas De Marchi 		goto out;
600f1ecf068SLucas De Marchi 
601f1ecf068SLucas De Marchi 	if (!table->poll)
602f1ecf068SLucas De Marchi 		goto out;
603f1ecf068SLucas De Marchi 
6044e474a00SLucas De Marchi 	event = (unsigned long)filp->private_data;
605f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
606f1ecf068SLucas De Marchi 
607f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
608f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
609f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
610f1ecf068SLucas De Marchi 	}
611f1ecf068SLucas De Marchi 
612f1ecf068SLucas De Marchi out:
6134e474a00SLucas De Marchi 	sysctl_head_finish(head);
6144e474a00SLucas De Marchi 
615f1ecf068SLucas De Marchi 	return ret;
616f1ecf068SLucas De Marchi }
61777b14db5SEric W. Biederman 
618f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file,
619f0c3b509SAl Viro 				struct dir_context *ctx,
6209043476fSAl Viro 				struct ctl_table_header *head,
6219043476fSAl Viro 				struct ctl_table *table)
62277b14db5SEric W. Biederman {
623f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
62477b14db5SEric W. Biederman 	struct inode *inode;
62577b14db5SEric W. Biederman 	struct qstr qname;
62677b14db5SEric W. Biederman 	ino_t ino = 0;
62777b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
62877b14db5SEric W. Biederman 
62977b14db5SEric W. Biederman 	qname.name = table->procname;
63077b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
6318387ff25SLinus Torvalds 	qname.hash = full_name_hash(dir, qname.name, qname.len);
63277b14db5SEric W. Biederman 
63377b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
63477b14db5SEric W. Biederman 	if (!child) {
63576aab3abSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
63676aab3abSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
63776aab3abSAl Viro 		if (IS_ERR(child))
63876aab3abSAl Viro 			return false;
63976aab3abSAl Viro 		if (d_in_lookup(child)) {
6409043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
6419043476fSAl Viro 			if (!inode) {
64276aab3abSAl Viro 				d_lookup_done(child);
6439043476fSAl Viro 				dput(child);
644f0c3b509SAl Viro 				return false;
64576aab3abSAl Viro 			}
646fb045adbSNick Piggin 			d_set_d_op(child, &proc_sys_dentry_operations);
6479043476fSAl Viro 			d_add(child, inode);
64877b14db5SEric W. Biederman 		}
64977b14db5SEric W. Biederman 	}
6502b0143b5SDavid Howells 	inode = d_inode(child);
65177b14db5SEric W. Biederman 	ino  = inode->i_ino;
65277b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
65377b14db5SEric W. Biederman 	dput(child);
654f0c3b509SAl Viro 	return dir_emit(ctx, qname.name, qname.len, ino, type);
6559043476fSAl Viro }
6569043476fSAl Viro 
657f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file,
658f0c3b509SAl Viro 				    struct dir_context *ctx,
6590e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
6600e47c99dSEric W. Biederman 				    struct ctl_table *table)
6610e47c99dSEric W. Biederman {
662f0c3b509SAl Viro 	bool ret = true;
6630e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
6640e47c99dSEric W. Biederman 
6654e757320SEric W. Biederman 	if (S_ISLNK(table->mode)) {
6660e47c99dSEric W. Biederman 		/* It is not an error if we can not follow the link ignore it */
667f0c3b509SAl Viro 		int err = sysctl_follow_link(&head, &table, current->nsproxy);
6680e47c99dSEric W. Biederman 		if (err)
6690e47c99dSEric W. Biederman 			goto out;
6704e757320SEric W. Biederman 	}
6710e47c99dSEric W. Biederman 
672f0c3b509SAl Viro 	ret = proc_sys_fill_cache(file, ctx, head, table);
6730e47c99dSEric W. Biederman out:
6740e47c99dSEric W. Biederman 	sysctl_head_finish(head);
6750e47c99dSEric W. Biederman 	return ret;
6760e47c99dSEric W. Biederman }
6770e47c99dSEric W. Biederman 
678e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table,
6799043476fSAl Viro 		unsigned long *pos, struct file *file,
680f0c3b509SAl Viro 		struct dir_context *ctx)
6819043476fSAl Viro {
682f0c3b509SAl Viro 	bool res;
6839043476fSAl Viro 
684f0c3b509SAl Viro 	if ((*pos)++ < ctx->pos)
685f0c3b509SAl Viro 		return true;
6869043476fSAl Viro 
6870e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
688f0c3b509SAl Viro 		res = proc_sys_link_fill_cache(file, ctx, head, table);
6890e47c99dSEric W. Biederman 	else
690f0c3b509SAl Viro 		res = proc_sys_fill_cache(file, ctx, head, table);
6919043476fSAl Viro 
692f0c3b509SAl Viro 	if (res)
693f0c3b509SAl Viro 		ctx->pos = *pos;
6946a75ce16SEric W. Biederman 
6956a75ce16SEric W. Biederman 	return res;
69677b14db5SEric W. Biederman }
69777b14db5SEric W. Biederman 
698f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
69977b14db5SEric W. Biederman {
700f0c3b509SAl Viro 	struct ctl_table_header *head = grab_header(file_inode(file));
7019043476fSAl Viro 	struct ctl_table_header *h = NULL;
7026a75ce16SEric W. Biederman 	struct ctl_table *entry;
7037ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
70477b14db5SEric W. Biederman 	unsigned long pos;
70577b14db5SEric W. Biederman 
7069043476fSAl Viro 	if (IS_ERR(head))
7079043476fSAl Viro 		return PTR_ERR(head);
7089043476fSAl Viro 
7097ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
71077b14db5SEric W. Biederman 
711f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
712f0c3b509SAl Viro 		return 0;
713f0c3b509SAl Viro 
71477b14db5SEric W. Biederman 	pos = 2;
71577b14db5SEric W. Biederman 
7167ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
717f0c3b509SAl Viro 		if (!scan(h, entry, &pos, file, ctx)) {
7189043476fSAl Viro 			sysctl_head_finish(h);
7199043476fSAl Viro 			break;
72077b14db5SEric W. Biederman 		}
72177b14db5SEric W. Biederman 	}
72277b14db5SEric W. Biederman 	sysctl_head_finish(head);
723f0c3b509SAl Viro 	return 0;
72477b14db5SEric W. Biederman }
72577b14db5SEric W. Biederman 
72610556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
72777b14db5SEric W. Biederman {
72877b14db5SEric W. Biederman 	/*
72977b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
73077b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
73177b14db5SEric W. Biederman 	 */
732f696a365SMiklos Szeredi 	struct ctl_table_header *head;
733f696a365SMiklos Szeredi 	struct ctl_table *table;
73477b14db5SEric W. Biederman 	int error;
73577b14db5SEric W. Biederman 
736f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
737f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
738f696a365SMiklos Szeredi 		return -EACCES;
739f696a365SMiklos Szeredi 
740f696a365SMiklos Szeredi 	head = grab_header(inode);
7419043476fSAl Viro 	if (IS_ERR(head))
7429043476fSAl Viro 		return PTR_ERR(head);
74377b14db5SEric W. Biederman 
744f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
7459043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
7469043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
7479043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
74873f7ef43SEric W. Biederman 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
7499043476fSAl Viro 
75077b14db5SEric W. Biederman 	sysctl_head_finish(head);
75177b14db5SEric W. Biederman 	return error;
75277b14db5SEric W. Biederman }
75377b14db5SEric W. Biederman 
75477b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
75577b14db5SEric W. Biederman {
7562b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
75777b14db5SEric W. Biederman 	int error;
75877b14db5SEric W. Biederman 
75977b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
76077b14db5SEric W. Biederman 		return -EPERM;
76177b14db5SEric W. Biederman 
76277b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
7631025774cSChristoph Hellwig 	if (error)
76477b14db5SEric W. Biederman 		return error;
7651025774cSChristoph Hellwig 
7661025774cSChristoph Hellwig 	setattr_copy(inode, attr);
7671025774cSChristoph Hellwig 	mark_inode_dirty(inode);
7681025774cSChristoph Hellwig 	return 0;
76977b14db5SEric W. Biederman }
77077b14db5SEric W. Biederman 
7719043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
7729043476fSAl Viro {
7732b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
7749043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
7759043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
7769043476fSAl Viro 
7779043476fSAl Viro 	if (IS_ERR(head))
7789043476fSAl Viro 		return PTR_ERR(head);
7799043476fSAl Viro 
7809043476fSAl Viro 	generic_fillattr(inode, stat);
7819043476fSAl Viro 	if (table)
7829043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
7839043476fSAl Viro 
7849043476fSAl Viro 	sysctl_head_finish(head);
7859043476fSAl Viro 	return 0;
7869043476fSAl Viro }
7879043476fSAl Viro 
78877b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
789f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
790f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
79177b14db5SEric W. Biederman 	.read		= proc_sys_read,
79277b14db5SEric W. Biederman 	.write		= proc_sys_write,
7936038f373SArnd Bergmann 	.llseek		= default_llseek,
7949043476fSAl Viro };
7959043476fSAl Viro 
7969043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
797887df078SPavel Emelyanov 	.read		= generic_read_dir,
798f50752eaSAl Viro 	.iterate_shared	= proc_sys_readdir,
7993222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
80077b14db5SEric W. Biederman };
80177b14db5SEric W. Biederman 
80203a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
8039043476fSAl Viro 	.permission	= proc_sys_permission,
8049043476fSAl Viro 	.setattr	= proc_sys_setattr,
8059043476fSAl Viro 	.getattr	= proc_sys_getattr,
8069043476fSAl Viro };
8079043476fSAl Viro 
8089043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
80977b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
81077b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
81177b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
8129043476fSAl Viro 	.getattr	= proc_sys_getattr,
81377b14db5SEric W. Biederman };
81477b14db5SEric W. Biederman 
8150b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
81677b14db5SEric W. Biederman {
8170b728e19SAl Viro 	if (flags & LOOKUP_RCU)
81834286d66SNick Piggin 		return -ECHILD;
8192b0143b5SDavid Howells 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
8209043476fSAl Viro }
8219043476fSAl Viro 
822fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
8239043476fSAl Viro {
8242b0143b5SDavid Howells 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
8259043476fSAl Viro }
8269043476fSAl Viro 
8271f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
8281f87f0b5SEric W. Biederman {
8291f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
8301f87f0b5SEric W. Biederman 	int res;
8311f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
8321f87f0b5SEric W. Biederman 	if (p->unregistering)
8331f87f0b5SEric W. Biederman 		res = 0;
8341f87f0b5SEric W. Biederman 	else if (!set->is_seen)
8351f87f0b5SEric W. Biederman 		res = 1;
8361f87f0b5SEric W. Biederman 	else
8371f87f0b5SEric W. Biederman 		res = set->is_seen(set);
8381f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
8391f87f0b5SEric W. Biederman 	return res;
8401f87f0b5SEric W. Biederman }
8411f87f0b5SEric W. Biederman 
842da53be12SLinus Torvalds static int proc_sys_compare(const struct dentry *parent, const struct dentry *dentry,
843621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
8449043476fSAl Viro {
845dfef6dcdSAl Viro 	struct ctl_table_header *head;
846da53be12SLinus Torvalds 	struct inode *inode;
847da53be12SLinus Torvalds 
84831e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
84931e6b01fSNick Piggin 	 * that inode here can be NULL */
850dfef6dcdSAl Viro 	/* AV: can it, indeed? */
8512b0143b5SDavid Howells 	inode = d_inode_rcu(dentry);
85231e6b01fSNick Piggin 	if (!inode)
853dfef6dcdSAl Viro 		return 1;
854621e155aSNick Piggin 	if (name->len != len)
8559043476fSAl Viro 		return 1;
856621e155aSNick Piggin 	if (memcmp(name->name, str, len))
8579043476fSAl Viro 		return 1;
858dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
859dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
86077b14db5SEric W. Biederman }
86177b14db5SEric W. Biederman 
862d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
86377b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
8649043476fSAl Viro 	.d_delete	= proc_sys_delete,
8659043476fSAl Viro 	.d_compare	= proc_sys_compare,
86677b14db5SEric W. Biederman };
86777b14db5SEric W. Biederman 
8680e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
8697ec66d06SEric W. Biederman 				   const char *name, int namelen)
8701f87f0b5SEric W. Biederman {
8717ec66d06SEric W. Biederman 	struct ctl_table_header *head;
8727ec66d06SEric W. Biederman 	struct ctl_table *entry;
8731f87f0b5SEric W. Biederman 
8740e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
8757ec66d06SEric W. Biederman 	if (!entry)
8767ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
87751f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
8787ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
87951f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
8801f87f0b5SEric W. Biederman }
8811f87f0b5SEric W. Biederman 
8827ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
8837ec66d06SEric W. Biederman 			       const char *name, int namelen)
8841f87f0b5SEric W. Biederman {
8857ec66d06SEric W. Biederman 	struct ctl_table *table;
8867ec66d06SEric W. Biederman 	struct ctl_dir *new;
887ac13ac6fSEric W. Biederman 	struct ctl_node *node;
8887ec66d06SEric W. Biederman 	char *new_name;
8891f87f0b5SEric W. Biederman 
890ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
891ac13ac6fSEric W. Biederman 		      sizeof(struct ctl_table)*2 +  namelen + 1,
892ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
8937ec66d06SEric W. Biederman 	if (!new)
8947ec66d06SEric W. Biederman 		return NULL;
8957ec66d06SEric W. Biederman 
896ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
897ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
8987ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
8997ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
9007ec66d06SEric W. Biederman 	new_name[namelen] = '\0';
9017ec66d06SEric W. Biederman 	table[0].procname = new_name;
9027ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
903ac13ac6fSEric W. Biederman 	init_header(&new->header, set->dir.header.root, set, node, table);
9047ec66d06SEric W. Biederman 
9057ec66d06SEric W. Biederman 	return new;
9061f87f0b5SEric W. Biederman }
9071f87f0b5SEric W. Biederman 
90860f126d9SEric W. Biederman /**
90960f126d9SEric W. Biederman  * get_subdir - find or create a subdir with the specified name.
91060f126d9SEric W. Biederman  * @dir:  Directory to create the subdirectory in
91160f126d9SEric W. Biederman  * @name: The name of the subdirectory to find or create
91260f126d9SEric W. Biederman  * @namelen: The length of name
91360f126d9SEric W. Biederman  *
91460f126d9SEric W. Biederman  * Takes a directory with an elevated reference count so we know that
91560f126d9SEric W. Biederman  * if we drop the lock the directory will not go away.  Upon success
91660f126d9SEric W. Biederman  * the reference is moved from @dir to the returned subdirectory.
91760f126d9SEric W. Biederman  * Upon error an error code is returned and the reference on @dir is
91860f126d9SEric W. Biederman  * simply dropped.
91960f126d9SEric W. Biederman  */
9200e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
9210e47c99dSEric W. Biederman 				  const char *name, int namelen)
9227ec66d06SEric W. Biederman {
9230e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
9247ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
9250eb97f38SEric W. Biederman 	int err;
9267ec66d06SEric W. Biederman 
9277ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
9280e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
9297ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
9307ec66d06SEric W. Biederman 		goto found;
9317ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
9327ec66d06SEric W. Biederman 		goto failed;
9337ec66d06SEric W. Biederman 
9347ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
9357ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
9367ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
9377ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
9387ec66d06SEric W. Biederman 	if (!new)
9397ec66d06SEric W. Biederman 		goto failed;
9407ec66d06SEric W. Biederman 
94160f126d9SEric W. Biederman 	/* Was the subdir added while we dropped the lock? */
9420e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
9437ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
9447ec66d06SEric W. Biederman 		goto found;
9457ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
9467ec66d06SEric W. Biederman 		goto failed;
9477ec66d06SEric W. Biederman 
94860f126d9SEric W. Biederman 	/* Nope.  Use the our freshly made directory entry. */
9490eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
9500eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
9510eb97f38SEric W. Biederman 	if (err)
9520e47c99dSEric W. Biederman 		goto failed;
9537ec66d06SEric W. Biederman 	subdir = new;
9547ec66d06SEric W. Biederman found:
9557ec66d06SEric W. Biederman 	subdir->header.nreg++;
9567ec66d06SEric W. Biederman failed:
957a1c83681SViresh Kumar 	if (IS_ERR(subdir)) {
95887ebdc00SAndrew Morton 		pr_err("sysctl could not get directory: ");
9596980128fSEric W. Biederman 		sysctl_print_dir(dir);
96087ebdc00SAndrew Morton 		pr_cont("/%*.*s %ld\n",
9617ec66d06SEric W. Biederman 			namelen, namelen, name, PTR_ERR(subdir));
9621f87f0b5SEric W. Biederman 	}
9637ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
9647ec66d06SEric W. Biederman 	if (new)
9657ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
9667ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
9677ec66d06SEric W. Biederman 	return subdir;
9681f87f0b5SEric W. Biederman }
9691f87f0b5SEric W. Biederman 
9700e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
9710e47c99dSEric W. Biederman {
9720e47c99dSEric W. Biederman 	struct ctl_dir *parent;
9730e47c99dSEric W. Biederman 	const char *procname;
9740e47c99dSEric W. Biederman 	if (!dir->header.parent)
9750e47c99dSEric W. Biederman 		return &set->dir;
9760e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
9770e47c99dSEric W. Biederman 	if (IS_ERR(parent))
9780e47c99dSEric W. Biederman 		return parent;
9790e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
9800e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
9810e47c99dSEric W. Biederman }
9820e47c99dSEric W. Biederman 
9830e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
9840e47c99dSEric W. Biederman 	struct ctl_table **pentry, struct nsproxy *namespaces)
9850e47c99dSEric W. Biederman {
9860e47c99dSEric W. Biederman 	struct ctl_table_header *head;
9870e47c99dSEric W. Biederman 	struct ctl_table_root *root;
9880e47c99dSEric W. Biederman 	struct ctl_table_set *set;
9890e47c99dSEric W. Biederman 	struct ctl_table *entry;
9900e47c99dSEric W. Biederman 	struct ctl_dir *dir;
9910e47c99dSEric W. Biederman 	int ret;
9920e47c99dSEric W. Biederman 
9930e47c99dSEric W. Biederman 	ret = 0;
9940e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
9950e47c99dSEric W. Biederman 	root = (*pentry)->data;
9960e47c99dSEric W. Biederman 	set = lookup_header_set(root, namespaces);
9970e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
9980e47c99dSEric W. Biederman 	if (IS_ERR(dir))
9990e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
10000e47c99dSEric W. Biederman 	else {
10010e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
10020e47c99dSEric W. Biederman 		head = NULL;
10030e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
10040e47c99dSEric W. Biederman 		ret = -ENOENT;
10050e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
10060e47c99dSEric W. Biederman 			unuse_table(*phead);
10070e47c99dSEric W. Biederman 			*phead = head;
10080e47c99dSEric W. Biederman 			*pentry = entry;
10090e47c99dSEric W. Biederman 			ret = 0;
10100e47c99dSEric W. Biederman 		}
10110e47c99dSEric W. Biederman 	}
10120e47c99dSEric W. Biederman 
10130e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
10140e47c99dSEric W. Biederman 	return ret;
10150e47c99dSEric W. Biederman }
10160e47c99dSEric W. Biederman 
10177c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
10187c60c48fSEric W. Biederman {
10197c60c48fSEric W. Biederman 	struct va_format vaf;
10207c60c48fSEric W. Biederman 	va_list args;
10217c60c48fSEric W. Biederman 
10227c60c48fSEric W. Biederman 	va_start(args, fmt);
10237c60c48fSEric W. Biederman 	vaf.fmt = fmt;
10247c60c48fSEric W. Biederman 	vaf.va = &args;
10257c60c48fSEric W. Biederman 
102687ebdc00SAndrew Morton 	pr_err("sysctl table check failed: %s/%s %pV\n",
10277c60c48fSEric W. Biederman 	       path, table->procname, &vaf);
10287c60c48fSEric W. Biederman 
10297c60c48fSEric W. Biederman 	va_end(args);
10307c60c48fSEric W. Biederman 	return -EINVAL;
10317c60c48fSEric W. Biederman }
10327c60c48fSEric W. Biederman 
10337c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
10347c60c48fSEric W. Biederman {
10357c60c48fSEric W. Biederman 	int err = 0;
10367c60c48fSEric W. Biederman 	for (; table->procname; table++) {
10377c60c48fSEric W. Biederman 		if (table->child)
10387c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "Not a file");
10397c60c48fSEric W. Biederman 
10401f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
10411f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
10421f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
10431f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
10441f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
10451f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
10461f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
10471f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
10481f87f0b5SEric W. Biederman 			if (!table->data)
10497c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No data");
10501f87f0b5SEric W. Biederman 			if (!table->maxlen)
10517c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No maxlen");
10521f87f0b5SEric W. Biederman 		}
10531f87f0b5SEric W. Biederman 		if (!table->proc_handler)
10547c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "No proc_handler");
10557c60c48fSEric W. Biederman 
10567c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
10577c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "bogus .mode 0%o",
10587c60c48fSEric W. Biederman 				table->mode);
10591f87f0b5SEric W. Biederman 	}
10607c60c48fSEric W. Biederman 	return err;
10611f87f0b5SEric W. Biederman }
10621f87f0b5SEric W. Biederman 
10630e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
10640e47c99dSEric W. Biederman 	struct ctl_table_root *link_root)
10650e47c99dSEric W. Biederman {
10660e47c99dSEric W. Biederman 	struct ctl_table *link_table, *entry, *link;
10670e47c99dSEric W. Biederman 	struct ctl_table_header *links;
1068ac13ac6fSEric W. Biederman 	struct ctl_node *node;
10690e47c99dSEric W. Biederman 	char *link_name;
10700e47c99dSEric W. Biederman 	int nr_entries, name_bytes;
10710e47c99dSEric W. Biederman 
10720e47c99dSEric W. Biederman 	name_bytes = 0;
10730e47c99dSEric W. Biederman 	nr_entries = 0;
10740e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
10750e47c99dSEric W. Biederman 		nr_entries++;
10760e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
10770e47c99dSEric W. Biederman 	}
10780e47c99dSEric W. Biederman 
10790e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
1080ac13ac6fSEric W. Biederman 			sizeof(struct ctl_node)*nr_entries +
10810e47c99dSEric W. Biederman 			sizeof(struct ctl_table)*(nr_entries + 1) +
10820e47c99dSEric W. Biederman 			name_bytes,
10830e47c99dSEric W. Biederman 			GFP_KERNEL);
10840e47c99dSEric W. Biederman 
10850e47c99dSEric W. Biederman 	if (!links)
10860e47c99dSEric W. Biederman 		return NULL;
10870e47c99dSEric W. Biederman 
1088ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
1089ac13ac6fSEric W. Biederman 	link_table = (struct ctl_table *)(node + nr_entries);
10900e47c99dSEric W. Biederman 	link_name = (char *)&link_table[nr_entries + 1];
10910e47c99dSEric W. Biederman 
10920e47c99dSEric W. Biederman 	for (link = link_table, entry = table; entry->procname; link++, entry++) {
10930e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
10940e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
10950e47c99dSEric W. Biederman 		link->procname = link_name;
10960e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
10970e47c99dSEric W. Biederman 		link->data = link_root;
10980e47c99dSEric W. Biederman 		link_name += len;
10990e47c99dSEric W. Biederman 	}
1100ac13ac6fSEric W. Biederman 	init_header(links, dir->header.root, dir->header.set, node, link_table);
11010e47c99dSEric W. Biederman 	links->nreg = nr_entries;
11020e47c99dSEric W. Biederman 
11030e47c99dSEric W. Biederman 	return links;
11040e47c99dSEric W. Biederman }
11050e47c99dSEric W. Biederman 
11060e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
11070e47c99dSEric W. Biederman 	struct ctl_table *table, struct ctl_table_root *link_root)
11080e47c99dSEric W. Biederman {
11090e47c99dSEric W. Biederman 	struct ctl_table_header *head;
11100e47c99dSEric W. Biederman 	struct ctl_table *entry, *link;
11110e47c99dSEric W. Biederman 
11120e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
11130e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
11140e47c99dSEric W. Biederman 		const char *procname = entry->procname;
11150e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
11160e47c99dSEric W. Biederman 		if (!link)
11170e47c99dSEric W. Biederman 			return false;
11180e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
11190e47c99dSEric W. Biederman 			continue;
11200e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
11210e47c99dSEric W. Biederman 			continue;
11220e47c99dSEric W. Biederman 		return false;
11230e47c99dSEric W. Biederman 	}
11240e47c99dSEric W. Biederman 
11250e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
11260e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
11270e47c99dSEric W. Biederman 		const char *procname = entry->procname;
11280e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
11290e47c99dSEric W. Biederman 		head->nreg++;
11300e47c99dSEric W. Biederman 	}
11310e47c99dSEric W. Biederman 	return true;
11320e47c99dSEric W. Biederman }
11330e47c99dSEric W. Biederman 
11340e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
11350e47c99dSEric W. Biederman {
11360e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
11370e47c99dSEric W. Biederman 	struct ctl_dir *core_parent = NULL;
11380e47c99dSEric W. Biederman 	struct ctl_table_header *links;
11390e47c99dSEric W. Biederman 	int err;
11400e47c99dSEric W. Biederman 
11410e47c99dSEric W. Biederman 	if (head->set == root_set)
11420e47c99dSEric W. Biederman 		return 0;
11430e47c99dSEric W. Biederman 
11440e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
11450e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
11460e47c99dSEric W. Biederman 		return 0;
11470e47c99dSEric W. Biederman 
11480e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root))
11490e47c99dSEric W. Biederman 		return 0;
11500e47c99dSEric W. Biederman 
11510e47c99dSEric W. Biederman 	core_parent->header.nreg++;
11520e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
11530e47c99dSEric W. Biederman 
11540e47c99dSEric W. Biederman 	links = new_links(core_parent, head->ctl_table, head->root);
11550e47c99dSEric W. Biederman 
11560e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
11570e47c99dSEric W. Biederman 	err = -ENOMEM;
11580e47c99dSEric W. Biederman 	if (!links)
11590e47c99dSEric W. Biederman 		goto out;
11600e47c99dSEric W. Biederman 
11610e47c99dSEric W. Biederman 	err = 0;
11620e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root)) {
11630e47c99dSEric W. Biederman 		kfree(links);
11640e47c99dSEric W. Biederman 		goto out;
11650e47c99dSEric W. Biederman 	}
11660e47c99dSEric W. Biederman 
11670e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
11680e47c99dSEric W. Biederman 	if (err)
11690e47c99dSEric W. Biederman 		kfree(links);
11700e47c99dSEric W. Biederman out:
11710e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
11720e47c99dSEric W. Biederman 	return err;
11730e47c99dSEric W. Biederman }
11740e47c99dSEric W. Biederman 
11751f87f0b5SEric W. Biederman /**
1176f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
117760a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
11781f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
11791f87f0b5SEric W. Biederman  * @table: the top-level table structure
11801f87f0b5SEric W. Biederman  *
11811f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
11821f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
11831f87f0b5SEric W. Biederman  *
11841f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
11851f87f0b5SEric W. Biederman  *
11861f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
11871f87f0b5SEric W. Biederman  *            enter a sysctl file
11881f87f0b5SEric W. Biederman  *
11891f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
11901f87f0b5SEric W. Biederman  *
11911f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
11921f87f0b5SEric W. Biederman  *
1193f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
11941f87f0b5SEric W. Biederman  *
1195f728019bSEric W. Biederman  * child - must be %NULL.
11961f87f0b5SEric W. Biederman  *
11971f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
11981f87f0b5SEric W. Biederman  *
11991f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
12001f87f0b5SEric W. Biederman  *
12011f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
12021f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
12031f87f0b5SEric W. Biederman  *
1204f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1205f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
12061f87f0b5SEric W. Biederman  *
12071f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
12081f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
12091f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
12101f87f0b5SEric W. Biederman  *
12111f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
12121f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
12131f87f0b5SEric W. Biederman  *
12141f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
12151f87f0b5SEric W. Biederman  * to the table header on success.
12161f87f0b5SEric W. Biederman  */
12176e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
121860a47a2eSEric W. Biederman 	struct ctl_table_set *set,
12196e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
12201f87f0b5SEric W. Biederman {
122160a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
12221f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
12236e9d5164SEric W. Biederman 	const char *name, *nextname;
12247ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1225ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1226ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1227ac13ac6fSEric W. Biederman 	int nr_entries = 0;
12281f87f0b5SEric W. Biederman 
1229ac13ac6fSEric W. Biederman 	for (entry = table; entry->procname; entry++)
1230ac13ac6fSEric W. Biederman 		nr_entries++;
1231ac13ac6fSEric W. Biederman 
1232ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1233ac13ac6fSEric W. Biederman 			 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
12341f87f0b5SEric W. Biederman 	if (!header)
12351f87f0b5SEric W. Biederman 		return NULL;
12361f87f0b5SEric W. Biederman 
1237ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1238ac13ac6fSEric W. Biederman 	init_header(header, root, set, node, table);
12397c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
12407c60c48fSEric W. Biederman 		goto fail;
12418d6ecfccSEric W. Biederman 
12421f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
12430e47c99dSEric W. Biederman 	dir = &set->dir;
124460f126d9SEric W. Biederman 	/* Reference moved down the diretory tree get_subdir */
12457ec66d06SEric W. Biederman 	dir->header.nreg++;
12467ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
12477ec66d06SEric W. Biederman 
12487ec66d06SEric W. Biederman 	/* Find the directory for the ctl_table */
12497ec66d06SEric W. Biederman 	for (name = path; name; name = nextname) {
12507ec66d06SEric W. Biederman 		int namelen;
12517ec66d06SEric W. Biederman 		nextname = strchr(name, '/');
12527ec66d06SEric W. Biederman 		if (nextname) {
12537ec66d06SEric W. Biederman 			namelen = nextname - name;
12547ec66d06SEric W. Biederman 			nextname++;
12557ec66d06SEric W. Biederman 		} else {
12567ec66d06SEric W. Biederman 			namelen = strlen(name);
12577ec66d06SEric W. Biederman 		}
12587ec66d06SEric W. Biederman 		if (namelen == 0)
12591f87f0b5SEric W. Biederman 			continue;
12607ec66d06SEric W. Biederman 
12610e47c99dSEric W. Biederman 		dir = get_subdir(dir, name, namelen);
12627ec66d06SEric W. Biederman 		if (IS_ERR(dir))
12637ec66d06SEric W. Biederman 			goto fail;
12641f87f0b5SEric W. Biederman 	}
12650e47c99dSEric W. Biederman 
12667ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
12670e47c99dSEric W. Biederman 	if (insert_header(dir, header))
12680e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
12690e47c99dSEric W. Biederman 
12707ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
12711f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
12721f87f0b5SEric W. Biederman 
12731f87f0b5SEric W. Biederman 	return header;
12740e47c99dSEric W. Biederman 
12757ec66d06SEric W. Biederman fail_put_dir_locked:
12767ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
12777c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
12787c60c48fSEric W. Biederman fail:
12797c60c48fSEric W. Biederman 	kfree(header);
12807c60c48fSEric W. Biederman 	dump_stack();
12817c60c48fSEric W. Biederman 	return NULL;
12821f87f0b5SEric W. Biederman }
12831f87f0b5SEric W. Biederman 
1284fea478d4SEric W. Biederman /**
1285fea478d4SEric W. Biederman  * register_sysctl - register a sysctl table
1286fea478d4SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1287fea478d4SEric W. Biederman  * @table: the table structure
1288fea478d4SEric W. Biederman  *
1289fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1290fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1291fea478d4SEric W. Biederman  *
1292fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1293fea478d4SEric W. Biederman  */
1294fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1295fea478d4SEric W. Biederman {
1296fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
1297fea478d4SEric W. Biederman 					path, table);
1298fea478d4SEric W. Biederman }
1299fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl);
1300fea478d4SEric W. Biederman 
13016e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
13026e9d5164SEric W. Biederman {
13036e9d5164SEric W. Biederman 	int namelen;
13046e9d5164SEric W. Biederman 	namelen = strlen(name);
13056e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
13066e9d5164SEric W. Biederman 		return NULL;
13076e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
13086e9d5164SEric W. Biederman 	pos[namelen] = '/';
13096e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
13106e9d5164SEric W. Biederman 	pos += namelen + 1;
13116e9d5164SEric W. Biederman 	return pos;
13126e9d5164SEric W. Biederman }
13136e9d5164SEric W. Biederman 
1314f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1315f728019bSEric W. Biederman {
1316f728019bSEric W. Biederman 	int has_files = 0;
1317f728019bSEric W. Biederman 	int nr_subheaders = 0;
1318f728019bSEric W. Biederman 	struct ctl_table *entry;
1319f728019bSEric W. Biederman 
1320f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1321f728019bSEric W. Biederman 	if (!table || !table->procname)
1322f728019bSEric W. Biederman 		return 1;
1323f728019bSEric W. Biederman 
1324f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1325f728019bSEric W. Biederman 		if (entry->child)
1326f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1327f728019bSEric W. Biederman 		else
1328f728019bSEric W. Biederman 			has_files = 1;
1329f728019bSEric W. Biederman 	}
1330f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1331f728019bSEric W. Biederman }
1332f728019bSEric W. Biederman 
1333f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
133460a47a2eSEric W. Biederman 	struct ctl_table_header ***subheader, struct ctl_table_set *set,
1335f728019bSEric W. Biederman 	struct ctl_table *table)
1336f728019bSEric W. Biederman {
1337f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1338f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1339f728019bSEric W. Biederman 	int nr_files = 0;
1340f728019bSEric W. Biederman 	int nr_dirs = 0;
1341f728019bSEric W. Biederman 	int err = -ENOMEM;
1342f728019bSEric W. Biederman 
1343f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1344f728019bSEric W. Biederman 		if (entry->child)
1345f728019bSEric W. Biederman 			nr_dirs++;
1346f728019bSEric W. Biederman 		else
1347f728019bSEric W. Biederman 			nr_files++;
1348f728019bSEric W. Biederman 	}
1349f728019bSEric W. Biederman 
1350f728019bSEric W. Biederman 	files = table;
1351f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1352f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1353f728019bSEric W. Biederman 		struct ctl_table *new;
1354f728019bSEric W. Biederman 		files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1355f728019bSEric W. Biederman 				GFP_KERNEL);
1356f728019bSEric W. Biederman 		if (!files)
1357f728019bSEric W. Biederman 			goto out;
1358f728019bSEric W. Biederman 
1359f728019bSEric W. Biederman 		ctl_table_arg = files;
1360f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1361f728019bSEric W. Biederman 			if (entry->child)
1362f728019bSEric W. Biederman 				continue;
1363f728019bSEric W. Biederman 			*new = *entry;
1364f728019bSEric W. Biederman 			new++;
1365f728019bSEric W. Biederman 		}
1366f728019bSEric W. Biederman 	}
1367f728019bSEric W. Biederman 
1368f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1369f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1370f728019bSEric W. Biederman 		struct ctl_table_header *header;
137160a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, path, files);
1372f728019bSEric W. Biederman 		if (!header) {
1373f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1374f728019bSEric W. Biederman 			goto out;
1375f728019bSEric W. Biederman 		}
1376f728019bSEric W. Biederman 
1377f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1378f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1379f728019bSEric W. Biederman 		**subheader = header;
1380f728019bSEric W. Biederman 		(*subheader)++;
1381f728019bSEric W. Biederman 	}
1382f728019bSEric W. Biederman 
1383f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1384f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1385f728019bSEric W. Biederman 		char *child_pos;
1386f728019bSEric W. Biederman 
1387f728019bSEric W. Biederman 		if (!entry->child)
1388f728019bSEric W. Biederman 			continue;
1389f728019bSEric W. Biederman 
1390f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1391f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1392f728019bSEric W. Biederman 		if (!child_pos)
1393f728019bSEric W. Biederman 			goto out;
1394f728019bSEric W. Biederman 
1395f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
139660a47a2eSEric W. Biederman 						  set, entry->child);
1397f728019bSEric W. Biederman 		pos[0] = '\0';
1398f728019bSEric W. Biederman 		if (err)
1399f728019bSEric W. Biederman 			goto out;
1400f728019bSEric W. Biederman 	}
1401f728019bSEric W. Biederman 	err = 0;
1402f728019bSEric W. Biederman out:
1403f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1404f728019bSEric W. Biederman 	return err;
1405f728019bSEric W. Biederman }
1406f728019bSEric W. Biederman 
14076e9d5164SEric W. Biederman /**
14086e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
140960a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
14106e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
14116e9d5164SEric W. Biederman  * @table: the top-level table structure
14126e9d5164SEric W. Biederman  *
14136e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
14146e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
14156e9d5164SEric W. Biederman  *
14166e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
14176e9d5164SEric W. Biederman  */
14186e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
141960a47a2eSEric W. Biederman 	struct ctl_table_set *set,
14206e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
14216e9d5164SEric W. Biederman {
1422ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1423f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1424f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
14256e9d5164SEric W. Biederman 	const struct ctl_path *component;
14266e9d5164SEric W. Biederman 	char *new_path, *pos;
14276e9d5164SEric W. Biederman 
14286e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
14296e9d5164SEric W. Biederman 	if (!new_path)
14306e9d5164SEric W. Biederman 		return NULL;
14316e9d5164SEric W. Biederman 
14326e9d5164SEric W. Biederman 	pos[0] = '\0';
14336e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
14346e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
14356e9d5164SEric W. Biederman 		if (!pos)
14366e9d5164SEric W. Biederman 			goto out;
14376e9d5164SEric W. Biederman 	}
1438ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1439ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1440ec6a5266SEric W. Biederman 		if (!pos)
1441ec6a5266SEric W. Biederman 			goto out;
1442ec6a5266SEric W. Biederman 		table = table->child;
1443ec6a5266SEric W. Biederman 	}
1444f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
144560a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, new_path, table);
1446ec6a5266SEric W. Biederman 		if (header)
1447ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1448f728019bSEric W. Biederman 	} else {
1449f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1450f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1451f728019bSEric W. Biederman 		if (!header)
1452f728019bSEric W. Biederman 			goto out;
1453f728019bSEric W. Biederman 
1454f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1455f728019bSEric W. Biederman 		subheader = subheaders;
1456f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1457f728019bSEric W. Biederman 
1458f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
145960a47a2eSEric W. Biederman 						set, table))
1460f728019bSEric W. Biederman 			goto err_register_leaves;
1461f728019bSEric W. Biederman 	}
1462f728019bSEric W. Biederman 
14636e9d5164SEric W. Biederman out:
14646e9d5164SEric W. Biederman 	kfree(new_path);
14656e9d5164SEric W. Biederman 	return header;
1466f728019bSEric W. Biederman 
1467f728019bSEric W. Biederman err_register_leaves:
1468f728019bSEric W. Biederman 	while (subheader > subheaders) {
1469f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1470f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1471f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1472f728019bSEric W. Biederman 		kfree(table);
1473f728019bSEric W. Biederman 	}
1474f728019bSEric W. Biederman 	kfree(header);
1475f728019bSEric W. Biederman 	header = NULL;
1476f728019bSEric W. Biederman 	goto out;
14776e9d5164SEric W. Biederman }
14786e9d5164SEric W. Biederman 
14791f87f0b5SEric W. Biederman /**
14801f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
14811f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
14821f87f0b5SEric W. Biederman  * @table: the top-level table structure
14831f87f0b5SEric W. Biederman  *
14841f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
14851f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
14861f87f0b5SEric W. Biederman  *
14871f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
14881f87f0b5SEric W. Biederman  */
14891f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
14901f87f0b5SEric W. Biederman 						struct ctl_table *table)
14911f87f0b5SEric W. Biederman {
149260a47a2eSEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root.default_set,
14931f87f0b5SEric W. Biederman 					path, table);
14941f87f0b5SEric W. Biederman }
14951f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
14961f87f0b5SEric W. Biederman 
14971f87f0b5SEric W. Biederman /**
14981f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
14991f87f0b5SEric W. Biederman  * @table: the top-level table structure
15001f87f0b5SEric W. Biederman  *
15011f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
15021f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
15031f87f0b5SEric W. Biederman  *
15041f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
15051f87f0b5SEric W. Biederman  */
15061f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
15071f87f0b5SEric W. Biederman {
15081f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
15091f87f0b5SEric W. Biederman 
15101f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
15111f87f0b5SEric W. Biederman }
15121f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
15131f87f0b5SEric W. Biederman 
15140e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
15150e47c99dSEric W. Biederman {
15160e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
15170e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
15180e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
15190e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
15200e47c99dSEric W. Biederman 	struct ctl_table *entry;
15210e47c99dSEric W. Biederman 
15220e47c99dSEric W. Biederman 	if (header->set == root_set)
15230e47c99dSEric W. Biederman 		return;
15240e47c99dSEric W. Biederman 
15250e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
15260e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
15270e47c99dSEric W. Biederman 		return;
15280e47c99dSEric W. Biederman 
15290e47c99dSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
15300e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
15310e47c99dSEric W. Biederman 		struct ctl_table *link;
15320e47c99dSEric W. Biederman 		const char *name = entry->procname;
15330e47c99dSEric W. Biederman 
15340e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
15350e47c99dSEric W. Biederman 		if (link &&
15360e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
15370e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
15380e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
15390e47c99dSEric W. Biederman 		}
15400e47c99dSEric W. Biederman 		else {
154187ebdc00SAndrew Morton 			pr_err("sysctl link missing during unregister: ");
15420e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
154387ebdc00SAndrew Morton 			pr_cont("/%s\n", name);
15440e47c99dSEric W. Biederman 		}
15450e47c99dSEric W. Biederman 	}
15460e47c99dSEric W. Biederman }
15470e47c99dSEric W. Biederman 
1548938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1549938aaa4fSEric W. Biederman {
15507ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
15517ec66d06SEric W. Biederman 
1552938aaa4fSEric W. Biederman 	if (--header->nreg)
1553938aaa4fSEric W. Biederman 		return;
1554938aaa4fSEric W. Biederman 
15550e47c99dSEric W. Biederman 	put_links(header);
1556938aaa4fSEric W. Biederman 	start_unregistering(header);
1557938aaa4fSEric W. Biederman 	if (!--header->count)
1558938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
15597ec66d06SEric W. Biederman 
15607ec66d06SEric W. Biederman 	if (parent)
15617ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1562938aaa4fSEric W. Biederman }
1563938aaa4fSEric W. Biederman 
15641f87f0b5SEric W. Biederman /**
15651f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
15661f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
15671f87f0b5SEric W. Biederman  *
15681f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
15691f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
15701f87f0b5SEric W. Biederman  */
15711f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
15721f87f0b5SEric W. Biederman {
1573f728019bSEric W. Biederman 	int nr_subheaders;
15741f87f0b5SEric W. Biederman 	might_sleep();
15751f87f0b5SEric W. Biederman 
15761f87f0b5SEric W. Biederman 	if (header == NULL)
15771f87f0b5SEric W. Biederman 		return;
15781f87f0b5SEric W. Biederman 
1579f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1580f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1581f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1582f728019bSEric W. Biederman 		int i;
1583f728019bSEric W. Biederman 
1584f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1585f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1586f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1587f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1588f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1589f728019bSEric W. Biederman 			kfree(table);
1590f728019bSEric W. Biederman 		}
1591f728019bSEric W. Biederman 		kfree(header);
1592f728019bSEric W. Biederman 		return;
1593f728019bSEric W. Biederman 	}
1594f728019bSEric W. Biederman 
15951f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1596938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
15971f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
15981f87f0b5SEric W. Biederman }
15991f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
16001f87f0b5SEric W. Biederman 
16010e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
16029eb47c26SEric W. Biederman 	struct ctl_table_root *root,
16031f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
16041f87f0b5SEric W. Biederman {
16051347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
16060e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1607ac13ac6fSEric W. Biederman 	init_header(&set->dir.header, root, set, NULL, root_table);
16081f87f0b5SEric W. Biederman }
16091f87f0b5SEric W. Biederman 
161097324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
161197324cd8SEric W. Biederman {
1612ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
161397324cd8SEric W. Biederman }
16141f87f0b5SEric W. Biederman 
16151e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
161677b14db5SEric W. Biederman {
1617e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1618e1675231SAlexey Dobriyan 
161977b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
16209043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
16219043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
162277b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1623de4e83bdSEric W. Biederman 
1624de4e83bdSEric W. Biederman 	return sysctl_init();
162577b14db5SEric W. Biederman }
1626