xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision f1ecf068)
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>
1077b14db5SEric W. Biederman #include "internal.h"
1177b14db5SEric W. Biederman 
12d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1377b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1403a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
159043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
169043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
1777b14db5SEric W. Biederman 
18f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
19f1ecf068SLucas De Marchi {
20f1ecf068SLucas De Marchi 	if (!poll)
21f1ecf068SLucas De Marchi 		return;
22f1ecf068SLucas De Marchi 
23f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
24f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
25f1ecf068SLucas De Marchi }
26f1ecf068SLucas De Marchi 
279043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
289043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
2977b14db5SEric W. Biederman {
3077b14db5SEric W. Biederman 	struct inode *inode;
319043476fSAl Viro 	struct proc_inode *ei;
3277b14db5SEric W. Biederman 
339043476fSAl Viro 	inode = new_inode(sb);
3477b14db5SEric W. Biederman 	if (!inode)
3577b14db5SEric W. Biederman 		goto out;
3677b14db5SEric W. Biederman 
3785fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
3885fe4025SChristoph Hellwig 
399043476fSAl Viro 	sysctl_head_get(head);
4077b14db5SEric W. Biederman 	ei = PROC_I(inode);
419043476fSAl Viro 	ei->sysctl = head;
429043476fSAl Viro 	ei->sysctl_entry = table;
439043476fSAl Viro 
4477b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
459043476fSAl Viro 	inode->i_mode = table->mode;
469043476fSAl Viro 	if (!table->child) {
479043476fSAl Viro 		inode->i_mode |= S_IFREG;
4877b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
4977b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
509043476fSAl Viro 	} else {
519043476fSAl Viro 		inode->i_mode |= S_IFDIR;
529043476fSAl Viro 		inode->i_nlink = 0;
539043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
549043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
559043476fSAl Viro 	}
5677b14db5SEric W. Biederman out:
5777b14db5SEric W. Biederman 	return inode;
5877b14db5SEric W. Biederman }
5977b14db5SEric W. Biederman 
609043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
6177b14db5SEric W. Biederman {
6277b14db5SEric W. Biederman 	int len;
632315ffa0SEric W. Biederman 	for ( ; p->procname; p++) {
6477b14db5SEric W. Biederman 
659043476fSAl Viro 		if (!p->procname)
6677b14db5SEric W. Biederman 			continue;
6777b14db5SEric W. Biederman 
689043476fSAl Viro 		len = strlen(p->procname);
6977b14db5SEric W. Biederman 		if (len != name->len)
7077b14db5SEric W. Biederman 			continue;
7177b14db5SEric W. Biederman 
729043476fSAl Viro 		if (memcmp(p->procname, name->name, len) != 0)
7377b14db5SEric W. Biederman 			continue;
7477b14db5SEric W. Biederman 
7577b14db5SEric W. Biederman 		/* I have a match */
769043476fSAl Viro 		return p;
7777b14db5SEric W. Biederman 	}
7877b14db5SEric W. Biederman 	return NULL;
7977b14db5SEric W. Biederman }
8077b14db5SEric W. Biederman 
8181324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
8277b14db5SEric W. Biederman {
839043476fSAl Viro 	if (PROC_I(inode)->sysctl)
849043476fSAl Viro 		return sysctl_head_grab(PROC_I(inode)->sysctl);
859043476fSAl Viro 	else
869043476fSAl Viro 		return sysctl_head_next(NULL);
8777b14db5SEric W. Biederman }
8877b14db5SEric W. Biederman 
8977b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
9077b14db5SEric W. Biederman 					struct nameidata *nd)
9177b14db5SEric W. Biederman {
929043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
939043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
949043476fSAl Viro 	struct ctl_table_header *h = NULL;
959043476fSAl Viro 	struct qstr *name = &dentry->d_name;
969043476fSAl Viro 	struct ctl_table *p;
9777b14db5SEric W. Biederman 	struct inode *inode;
989043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
9977b14db5SEric W. Biederman 
1009043476fSAl Viro 	if (IS_ERR(head))
1019043476fSAl Viro 		return ERR_CAST(head);
1029043476fSAl Viro 
1039043476fSAl Viro 	if (table && !table->child) {
1049043476fSAl Viro 		WARN_ON(1);
1059043476fSAl Viro 		goto out;
1069043476fSAl Viro 	}
1079043476fSAl Viro 
1089043476fSAl Viro 	table = table ? table->child : head->ctl_table;
1099043476fSAl Viro 
1109043476fSAl Viro 	p = find_in_table(table, name);
1119043476fSAl Viro 	if (!p) {
1129043476fSAl Viro 		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
1139043476fSAl Viro 			if (h->attached_to != table)
1149043476fSAl Viro 				continue;
1159043476fSAl Viro 			p = find_in_table(h->attached_by, name);
1169043476fSAl Viro 			if (p)
1179043476fSAl Viro 				break;
1189043476fSAl Viro 		}
1199043476fSAl Viro 	}
1209043476fSAl Viro 
1219043476fSAl Viro 	if (!p)
12277b14db5SEric W. Biederman 		goto out;
12377b14db5SEric W. Biederman 
12477b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
1259043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
1269043476fSAl Viro 	if (h)
1279043476fSAl Viro 		sysctl_head_finish(h);
1289043476fSAl Viro 
12977b14db5SEric W. Biederman 	if (!inode)
13077b14db5SEric W. Biederman 		goto out;
13177b14db5SEric W. Biederman 
13277b14db5SEric W. Biederman 	err = NULL;
133fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
13477b14db5SEric W. Biederman 	d_add(dentry, inode);
13577b14db5SEric W. Biederman 
13677b14db5SEric W. Biederman out:
13777b14db5SEric W. Biederman 	sysctl_head_finish(head);
13877b14db5SEric W. Biederman 	return err;
13977b14db5SEric W. Biederman }
14077b14db5SEric W. Biederman 
1417708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
1427708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
14377b14db5SEric W. Biederman {
1449043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
1459043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
1469043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
1472a2da53bSDavid Howells 	ssize_t error;
1482a2da53bSDavid Howells 	size_t res;
14977b14db5SEric W. Biederman 
1509043476fSAl Viro 	if (IS_ERR(head))
1519043476fSAl Viro 		return PTR_ERR(head);
15277b14db5SEric W. Biederman 
15377b14db5SEric W. Biederman 	/*
15477b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
15577b14db5SEric W. Biederman 	 * and won't be until we finish.
15677b14db5SEric W. Biederman 	 */
15777b14db5SEric W. Biederman 	error = -EPERM;
158d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
15977b14db5SEric W. Biederman 		goto out;
16077b14db5SEric W. Biederman 
1619043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
1629043476fSAl Viro 	error = -EINVAL;
1639043476fSAl Viro 	if (!table->proc_handler)
1649043476fSAl Viro 		goto out;
1659043476fSAl Viro 
16677b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
16777b14db5SEric W. Biederman 	res = count;
1688d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
16977b14db5SEric W. Biederman 	if (!error)
17077b14db5SEric W. Biederman 		error = res;
17177b14db5SEric W. Biederman out:
17277b14db5SEric W. Biederman 	sysctl_head_finish(head);
17377b14db5SEric W. Biederman 
17477b14db5SEric W. Biederman 	return error;
17577b14db5SEric W. Biederman }
17677b14db5SEric W. Biederman 
1777708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
1787708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
1797708bfb1SPavel Emelyanov {
1807708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
1817708bfb1SPavel Emelyanov }
1827708bfb1SPavel Emelyanov 
18377b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
18477b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
18577b14db5SEric W. Biederman {
1867708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
18777b14db5SEric W. Biederman }
18877b14db5SEric W. Biederman 
189f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
190f1ecf068SLucas De Marchi {
191f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
192f1ecf068SLucas De Marchi 
193f1ecf068SLucas De Marchi 	if (table->poll)
194f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
195f1ecf068SLucas De Marchi 
196f1ecf068SLucas De Marchi 	return 0;
197f1ecf068SLucas De Marchi }
198f1ecf068SLucas De Marchi 
199f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
200f1ecf068SLucas De Marchi {
201f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
202f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
203f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
204f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
205f1ecf068SLucas De Marchi 
206f1ecf068SLucas De Marchi 	if (!table->proc_handler)
207f1ecf068SLucas De Marchi 		goto out;
208f1ecf068SLucas De Marchi 
209f1ecf068SLucas De Marchi 	if (!table->poll)
210f1ecf068SLucas De Marchi 		goto out;
211f1ecf068SLucas De Marchi 
212f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
213f1ecf068SLucas De Marchi 
214f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
215f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
216f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
217f1ecf068SLucas De Marchi 	}
218f1ecf068SLucas De Marchi 
219f1ecf068SLucas De Marchi out:
220f1ecf068SLucas De Marchi 	return ret;
221f1ecf068SLucas De Marchi }
22277b14db5SEric W. Biederman 
22377b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
2249043476fSAl Viro 				filldir_t filldir,
2259043476fSAl Viro 				struct ctl_table_header *head,
2269043476fSAl Viro 				struct ctl_table *table)
22777b14db5SEric W. Biederman {
22877b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
22977b14db5SEric W. Biederman 	struct inode *inode;
23077b14db5SEric W. Biederman 	struct qstr qname;
23177b14db5SEric W. Biederman 	ino_t ino = 0;
23277b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
23377b14db5SEric W. Biederman 
23477b14db5SEric W. Biederman 	qname.name = table->procname;
23577b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
23677b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
23777b14db5SEric W. Biederman 
23877b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
23977b14db5SEric W. Biederman 	if (!child) {
2409043476fSAl Viro 		child = d_alloc(dir, &qname);
2419043476fSAl Viro 		if (child) {
2429043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
2439043476fSAl Viro 			if (!inode) {
2449043476fSAl Viro 				dput(child);
2459043476fSAl Viro 				return -ENOMEM;
2469043476fSAl Viro 			} else {
247fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
2489043476fSAl Viro 				d_add(child, inode);
24977b14db5SEric W. Biederman 			}
2509043476fSAl Viro 		} else {
2519043476fSAl Viro 			return -ENOMEM;
25277b14db5SEric W. Biederman 		}
25377b14db5SEric W. Biederman 	}
25477b14db5SEric W. Biederman 	inode = child->d_inode;
25577b14db5SEric W. Biederman 	ino  = inode->i_ino;
25677b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
25777b14db5SEric W. Biederman 	dput(child);
2589043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
2599043476fSAl Viro }
2609043476fSAl Viro 
2619043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
2629043476fSAl Viro 		unsigned long *pos, struct file *file,
2639043476fSAl Viro 		void *dirent, filldir_t filldir)
2649043476fSAl Viro {
2659043476fSAl Viro 
2662315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
2679043476fSAl Viro 		int res;
2689043476fSAl Viro 
2699043476fSAl Viro 		/* Can't do anything without a proc name */
2709043476fSAl Viro 		if (!table->procname)
2719043476fSAl Viro 			continue;
2729043476fSAl Viro 
2739043476fSAl Viro 		if (*pos < file->f_pos)
2749043476fSAl Viro 			continue;
2759043476fSAl Viro 
2769043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
2779043476fSAl Viro 		if (res)
2789043476fSAl Viro 			return res;
2799043476fSAl Viro 
2809043476fSAl Viro 		file->f_pos = *pos + 1;
2819043476fSAl Viro 	}
2829043476fSAl Viro 	return 0;
28377b14db5SEric W. Biederman }
28477b14db5SEric W. Biederman 
28577b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
28677b14db5SEric W. Biederman {
2879043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
28877b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
2899043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
2909043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2919043476fSAl Viro 	struct ctl_table_header *h = NULL;
29277b14db5SEric W. Biederman 	unsigned long pos;
2939043476fSAl Viro 	int ret = -EINVAL;
29477b14db5SEric W. Biederman 
2959043476fSAl Viro 	if (IS_ERR(head))
2969043476fSAl Viro 		return PTR_ERR(head);
2979043476fSAl Viro 
2989043476fSAl Viro 	if (table && !table->child) {
2999043476fSAl Viro 		WARN_ON(1);
30077b14db5SEric W. Biederman 		goto out;
3019043476fSAl Viro 	}
3029043476fSAl Viro 
3039043476fSAl Viro 	table = table ? table->child : head->ctl_table;
30477b14db5SEric W. Biederman 
30577b14db5SEric W. Biederman 	ret = 0;
30677b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
30777b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
30877b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
30977b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
31077b14db5SEric W. Biederman 			goto out;
31177b14db5SEric W. Biederman 		filp->f_pos++;
31277b14db5SEric W. Biederman 	}
31377b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
31477b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
31577b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
31677b14db5SEric W. Biederman 			goto out;
31777b14db5SEric W. Biederman 		filp->f_pos++;
31877b14db5SEric W. Biederman 	}
31977b14db5SEric W. Biederman 	pos = 2;
32077b14db5SEric W. Biederman 
3219043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
3229043476fSAl Viro 	if (ret)
32377b14db5SEric W. Biederman 		goto out;
3249043476fSAl Viro 
3259043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
3269043476fSAl Viro 		if (h->attached_to != table)
3279043476fSAl Viro 			continue;
3289043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
3299043476fSAl Viro 		if (ret) {
3309043476fSAl Viro 			sysctl_head_finish(h);
3319043476fSAl Viro 			break;
33277b14db5SEric W. Biederman 		}
33377b14db5SEric W. Biederman 	}
33477b14db5SEric W. Biederman 	ret = 1;
33577b14db5SEric W. Biederman out:
33677b14db5SEric W. Biederman 	sysctl_head_finish(head);
33777b14db5SEric W. Biederman 	return ret;
33877b14db5SEric W. Biederman }
33977b14db5SEric W. Biederman 
34010556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
34177b14db5SEric W. Biederman {
34277b14db5SEric W. Biederman 	/*
34377b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
34477b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
34577b14db5SEric W. Biederman 	 */
346f696a365SMiklos Szeredi 	struct ctl_table_header *head;
347f696a365SMiklos Szeredi 	struct ctl_table *table;
34877b14db5SEric W. Biederman 	int error;
34977b14db5SEric W. Biederman 
350f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
351f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
352f696a365SMiklos Szeredi 		return -EACCES;
353f696a365SMiklos Szeredi 
354f696a365SMiklos Szeredi 	head = grab_header(inode);
3559043476fSAl Viro 	if (IS_ERR(head))
3569043476fSAl Viro 		return PTR_ERR(head);
35777b14db5SEric W. Biederman 
358f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
3599043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
3609043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
3619043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
3621fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
3639043476fSAl Viro 
36477b14db5SEric W. Biederman 	sysctl_head_finish(head);
36577b14db5SEric W. Biederman 	return error;
36677b14db5SEric W. Biederman }
36777b14db5SEric W. Biederman 
36877b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
36977b14db5SEric W. Biederman {
37077b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
37177b14db5SEric W. Biederman 	int error;
37277b14db5SEric W. Biederman 
37377b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
37477b14db5SEric W. Biederman 		return -EPERM;
37577b14db5SEric W. Biederman 
37677b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
3771025774cSChristoph Hellwig 	if (error)
37877b14db5SEric W. Biederman 		return error;
3791025774cSChristoph Hellwig 
3801025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
3811025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
3821025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
3831025774cSChristoph Hellwig 		if (error)
3841025774cSChristoph Hellwig 			return error;
3851025774cSChristoph Hellwig 	}
3861025774cSChristoph Hellwig 
3871025774cSChristoph Hellwig 	setattr_copy(inode, attr);
3881025774cSChristoph Hellwig 	mark_inode_dirty(inode);
3891025774cSChristoph Hellwig 	return 0;
39077b14db5SEric W. Biederman }
39177b14db5SEric W. Biederman 
3929043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3939043476fSAl Viro {
3949043476fSAl Viro 	struct inode *inode = dentry->d_inode;
3959043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
3969043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
3979043476fSAl Viro 
3989043476fSAl Viro 	if (IS_ERR(head))
3999043476fSAl Viro 		return PTR_ERR(head);
4009043476fSAl Viro 
4019043476fSAl Viro 	generic_fillattr(inode, stat);
4029043476fSAl Viro 	if (table)
4039043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
4049043476fSAl Viro 
4059043476fSAl Viro 	sysctl_head_finish(head);
4069043476fSAl Viro 	return 0;
4079043476fSAl Viro }
4089043476fSAl Viro 
40977b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
410f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
411f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
41277b14db5SEric W. Biederman 	.read		= proc_sys_read,
41377b14db5SEric W. Biederman 	.write		= proc_sys_write,
4146038f373SArnd Bergmann 	.llseek		= default_llseek,
4159043476fSAl Viro };
4169043476fSAl Viro 
4179043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
418887df078SPavel Emelyanov 	.read		= generic_read_dir,
41977b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
4203222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
42177b14db5SEric W. Biederman };
42277b14db5SEric W. Biederman 
42303a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
4249043476fSAl Viro 	.permission	= proc_sys_permission,
4259043476fSAl Viro 	.setattr	= proc_sys_setattr,
4269043476fSAl Viro 	.getattr	= proc_sys_getattr,
4279043476fSAl Viro };
4289043476fSAl Viro 
4299043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
43077b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
43177b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
43277b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
4339043476fSAl Viro 	.getattr	= proc_sys_getattr,
43477b14db5SEric W. Biederman };
43577b14db5SEric W. Biederman 
43677b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
43777b14db5SEric W. Biederman {
43834286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
43934286d66SNick Piggin 		return -ECHILD;
4409043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
4419043476fSAl Viro }
4429043476fSAl Viro 
443fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
4449043476fSAl Viro {
4459043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
4469043476fSAl Viro }
4479043476fSAl Viro 
448621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
449621e155aSNick Piggin 		const struct inode *pinode,
450621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
451621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
4529043476fSAl Viro {
453dfef6dcdSAl Viro 	struct ctl_table_header *head;
45431e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
45531e6b01fSNick Piggin 	 * that inode here can be NULL */
456dfef6dcdSAl Viro 	/* AV: can it, indeed? */
45731e6b01fSNick Piggin 	if (!inode)
458dfef6dcdSAl Viro 		return 1;
459621e155aSNick Piggin 	if (name->len != len)
4609043476fSAl Viro 		return 1;
461621e155aSNick Piggin 	if (memcmp(name->name, str, len))
4629043476fSAl Viro 		return 1;
463dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
464dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
46577b14db5SEric W. Biederman }
46677b14db5SEric W. Biederman 
467d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
46877b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
4699043476fSAl Viro 	.d_delete	= proc_sys_delete,
4709043476fSAl Viro 	.d_compare	= proc_sys_compare,
47177b14db5SEric W. Biederman };
47277b14db5SEric W. Biederman 
4731e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
47477b14db5SEric W. Biederman {
475e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
476e1675231SAlexey Dobriyan 
47777b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
4789043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
4799043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
48077b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
48177b14db5SEric W. Biederman 	return 0;
48277b14db5SEric W. Biederman }
483