xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 34286d66)
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>
677b14db5SEric W. Biederman #include <linux/proc_fs.h>
777b14db5SEric W. Biederman #include <linux/security.h>
834286d66SNick Piggin #include <linux/namei.h>
977b14db5SEric W. Biederman #include "internal.h"
1077b14db5SEric W. Biederman 
11d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1277b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1303a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
149043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
159043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
1677b14db5SEric W. Biederman 
179043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
189043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
1977b14db5SEric W. Biederman {
2077b14db5SEric W. Biederman 	struct inode *inode;
219043476fSAl Viro 	struct proc_inode *ei;
2277b14db5SEric W. Biederman 
239043476fSAl Viro 	inode = new_inode(sb);
2477b14db5SEric W. Biederman 	if (!inode)
2577b14db5SEric W. Biederman 		goto out;
2677b14db5SEric W. Biederman 
2785fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2885fe4025SChristoph Hellwig 
299043476fSAl Viro 	sysctl_head_get(head);
3077b14db5SEric W. Biederman 	ei = PROC_I(inode);
319043476fSAl Viro 	ei->sysctl = head;
329043476fSAl Viro 	ei->sysctl_entry = table;
339043476fSAl Viro 
3477b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
359043476fSAl Viro 	inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
369043476fSAl Viro 	inode->i_mode = table->mode;
379043476fSAl Viro 	if (!table->child) {
389043476fSAl Viro 		inode->i_mode |= S_IFREG;
3977b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
4077b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
419043476fSAl Viro 	} else {
429043476fSAl Viro 		inode->i_mode |= S_IFDIR;
439043476fSAl Viro 		inode->i_nlink = 0;
449043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
459043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
469043476fSAl Viro 	}
4777b14db5SEric W. Biederman out:
4877b14db5SEric W. Biederman 	return inode;
4977b14db5SEric W. Biederman }
5077b14db5SEric W. Biederman 
519043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
5277b14db5SEric W. Biederman {
5377b14db5SEric W. Biederman 	int len;
542315ffa0SEric W. Biederman 	for ( ; p->procname; p++) {
5577b14db5SEric W. Biederman 
569043476fSAl Viro 		if (!p->procname)
5777b14db5SEric W. Biederman 			continue;
5877b14db5SEric W. Biederman 
599043476fSAl Viro 		len = strlen(p->procname);
6077b14db5SEric W. Biederman 		if (len != name->len)
6177b14db5SEric W. Biederman 			continue;
6277b14db5SEric W. Biederman 
639043476fSAl Viro 		if (memcmp(p->procname, name->name, len) != 0)
6477b14db5SEric W. Biederman 			continue;
6577b14db5SEric W. Biederman 
6677b14db5SEric W. Biederman 		/* I have a match */
679043476fSAl Viro 		return p;
6877b14db5SEric W. Biederman 	}
6977b14db5SEric W. Biederman 	return NULL;
7077b14db5SEric W. Biederman }
7177b14db5SEric W. Biederman 
7281324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
7377b14db5SEric W. Biederman {
749043476fSAl Viro 	if (PROC_I(inode)->sysctl)
759043476fSAl Viro 		return sysctl_head_grab(PROC_I(inode)->sysctl);
769043476fSAl Viro 	else
779043476fSAl Viro 		return sysctl_head_next(NULL);
7877b14db5SEric W. Biederman }
7977b14db5SEric W. Biederman 
8077b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
8177b14db5SEric W. Biederman 					struct nameidata *nd)
8277b14db5SEric W. Biederman {
839043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
849043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
859043476fSAl Viro 	struct ctl_table_header *h = NULL;
869043476fSAl Viro 	struct qstr *name = &dentry->d_name;
879043476fSAl Viro 	struct ctl_table *p;
8877b14db5SEric W. Biederman 	struct inode *inode;
899043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
9077b14db5SEric W. Biederman 
919043476fSAl Viro 	if (IS_ERR(head))
929043476fSAl Viro 		return ERR_CAST(head);
939043476fSAl Viro 
949043476fSAl Viro 	if (table && !table->child) {
959043476fSAl Viro 		WARN_ON(1);
969043476fSAl Viro 		goto out;
979043476fSAl Viro 	}
989043476fSAl Viro 
999043476fSAl Viro 	table = table ? table->child : head->ctl_table;
1009043476fSAl Viro 
1019043476fSAl Viro 	p = find_in_table(table, name);
1029043476fSAl Viro 	if (!p) {
1039043476fSAl Viro 		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
1049043476fSAl Viro 			if (h->attached_to != table)
1059043476fSAl Viro 				continue;
1069043476fSAl Viro 			p = find_in_table(h->attached_by, name);
1079043476fSAl Viro 			if (p)
1089043476fSAl Viro 				break;
1099043476fSAl Viro 		}
1109043476fSAl Viro 	}
1119043476fSAl Viro 
1129043476fSAl Viro 	if (!p)
11377b14db5SEric W. Biederman 		goto out;
11477b14db5SEric W. Biederman 
11577b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
1169043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
1179043476fSAl Viro 	if (h)
1189043476fSAl Viro 		sysctl_head_finish(h);
1199043476fSAl Viro 
12077b14db5SEric W. Biederman 	if (!inode)
12177b14db5SEric W. Biederman 		goto out;
12277b14db5SEric W. Biederman 
12377b14db5SEric W. Biederman 	err = NULL;
124fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
12577b14db5SEric W. Biederman 	d_add(dentry, inode);
12677b14db5SEric W. Biederman 
12777b14db5SEric W. Biederman out:
12877b14db5SEric W. Biederman 	sysctl_head_finish(head);
12977b14db5SEric W. Biederman 	return err;
13077b14db5SEric W. Biederman }
13177b14db5SEric W. Biederman 
1327708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
1337708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
13477b14db5SEric W. Biederman {
1359043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
1369043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
1379043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
1382a2da53bSDavid Howells 	ssize_t error;
1392a2da53bSDavid Howells 	size_t res;
14077b14db5SEric W. Biederman 
1419043476fSAl Viro 	if (IS_ERR(head))
1429043476fSAl Viro 		return PTR_ERR(head);
14377b14db5SEric W. Biederman 
14477b14db5SEric W. Biederman 	/*
14577b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
14677b14db5SEric W. Biederman 	 * and won't be until we finish.
14777b14db5SEric W. Biederman 	 */
14877b14db5SEric W. Biederman 	error = -EPERM;
149d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
15077b14db5SEric W. Biederman 		goto out;
15177b14db5SEric W. Biederman 
1529043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
1539043476fSAl Viro 	error = -EINVAL;
1549043476fSAl Viro 	if (!table->proc_handler)
1559043476fSAl Viro 		goto out;
1569043476fSAl Viro 
15777b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
15877b14db5SEric W. Biederman 	res = count;
1598d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
16077b14db5SEric W. Biederman 	if (!error)
16177b14db5SEric W. Biederman 		error = res;
16277b14db5SEric W. Biederman out:
16377b14db5SEric W. Biederman 	sysctl_head_finish(head);
16477b14db5SEric W. Biederman 
16577b14db5SEric W. Biederman 	return error;
16677b14db5SEric W. Biederman }
16777b14db5SEric W. Biederman 
1687708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
1697708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
1707708bfb1SPavel Emelyanov {
1717708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
1727708bfb1SPavel Emelyanov }
1737708bfb1SPavel Emelyanov 
17477b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
17577b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
17677b14db5SEric W. Biederman {
1777708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
17877b14db5SEric W. Biederman }
17977b14db5SEric W. Biederman 
18077b14db5SEric W. Biederman 
18177b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
1829043476fSAl Viro 				filldir_t filldir,
1839043476fSAl Viro 				struct ctl_table_header *head,
1849043476fSAl Viro 				struct ctl_table *table)
18577b14db5SEric W. Biederman {
18677b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
18777b14db5SEric W. Biederman 	struct inode *inode;
18877b14db5SEric W. Biederman 	struct qstr qname;
18977b14db5SEric W. Biederman 	ino_t ino = 0;
19077b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
19177b14db5SEric W. Biederman 
19277b14db5SEric W. Biederman 	qname.name = table->procname;
19377b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
19477b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
19577b14db5SEric W. Biederman 
19677b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
19777b14db5SEric W. Biederman 	if (!child) {
1989043476fSAl Viro 		child = d_alloc(dir, &qname);
1999043476fSAl Viro 		if (child) {
2009043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
2019043476fSAl Viro 			if (!inode) {
2029043476fSAl Viro 				dput(child);
2039043476fSAl Viro 				return -ENOMEM;
2049043476fSAl Viro 			} else {
205fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
2069043476fSAl Viro 				d_add(child, inode);
20777b14db5SEric W. Biederman 			}
2089043476fSAl Viro 		} else {
2099043476fSAl Viro 			return -ENOMEM;
21077b14db5SEric W. Biederman 		}
21177b14db5SEric W. Biederman 	}
21277b14db5SEric W. Biederman 	inode = child->d_inode;
21377b14db5SEric W. Biederman 	ino  = inode->i_ino;
21477b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
21577b14db5SEric W. Biederman 	dput(child);
2169043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
2179043476fSAl Viro }
2189043476fSAl Viro 
2199043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
2209043476fSAl Viro 		unsigned long *pos, struct file *file,
2219043476fSAl Viro 		void *dirent, filldir_t filldir)
2229043476fSAl Viro {
2239043476fSAl Viro 
2242315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
2259043476fSAl Viro 		int res;
2269043476fSAl Viro 
2279043476fSAl Viro 		/* Can't do anything without a proc name */
2289043476fSAl Viro 		if (!table->procname)
2299043476fSAl Viro 			continue;
2309043476fSAl Viro 
2319043476fSAl Viro 		if (*pos < file->f_pos)
2329043476fSAl Viro 			continue;
2339043476fSAl Viro 
2349043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
2359043476fSAl Viro 		if (res)
2369043476fSAl Viro 			return res;
2379043476fSAl Viro 
2389043476fSAl Viro 		file->f_pos = *pos + 1;
2399043476fSAl Viro 	}
2409043476fSAl Viro 	return 0;
24177b14db5SEric W. Biederman }
24277b14db5SEric W. Biederman 
24377b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
24477b14db5SEric W. Biederman {
2459043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
24677b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
2479043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
2489043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2499043476fSAl Viro 	struct ctl_table_header *h = NULL;
25077b14db5SEric W. Biederman 	unsigned long pos;
2519043476fSAl Viro 	int ret = -EINVAL;
25277b14db5SEric W. Biederman 
2539043476fSAl Viro 	if (IS_ERR(head))
2549043476fSAl Viro 		return PTR_ERR(head);
2559043476fSAl Viro 
2569043476fSAl Viro 	if (table && !table->child) {
2579043476fSAl Viro 		WARN_ON(1);
25877b14db5SEric W. Biederman 		goto out;
2599043476fSAl Viro 	}
2609043476fSAl Viro 
2619043476fSAl Viro 	table = table ? table->child : head->ctl_table;
26277b14db5SEric W. Biederman 
26377b14db5SEric W. Biederman 	ret = 0;
26477b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
26577b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
26677b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
26777b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
26877b14db5SEric W. Biederman 			goto out;
26977b14db5SEric W. Biederman 		filp->f_pos++;
27077b14db5SEric W. Biederman 	}
27177b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
27277b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
27377b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
27477b14db5SEric W. Biederman 			goto out;
27577b14db5SEric W. Biederman 		filp->f_pos++;
27677b14db5SEric W. Biederman 	}
27777b14db5SEric W. Biederman 	pos = 2;
27877b14db5SEric W. Biederman 
2799043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
2809043476fSAl Viro 	if (ret)
28177b14db5SEric W. Biederman 		goto out;
2829043476fSAl Viro 
2839043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
2849043476fSAl Viro 		if (h->attached_to != table)
2859043476fSAl Viro 			continue;
2869043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
2879043476fSAl Viro 		if (ret) {
2889043476fSAl Viro 			sysctl_head_finish(h);
2899043476fSAl Viro 			break;
29077b14db5SEric W. Biederman 		}
29177b14db5SEric W. Biederman 	}
29277b14db5SEric W. Biederman 	ret = 1;
29377b14db5SEric W. Biederman out:
29477b14db5SEric W. Biederman 	sysctl_head_finish(head);
29577b14db5SEric W. Biederman 	return ret;
29677b14db5SEric W. Biederman }
29777b14db5SEric W. Biederman 
298e6305c43SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
29977b14db5SEric W. Biederman {
30077b14db5SEric W. Biederman 	/*
30177b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
30277b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
30377b14db5SEric W. Biederman 	 */
304f696a365SMiklos Szeredi 	struct ctl_table_header *head;
305f696a365SMiklos Szeredi 	struct ctl_table *table;
30677b14db5SEric W. Biederman 	int error;
30777b14db5SEric W. Biederman 
308f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
309f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
310f696a365SMiklos Szeredi 		return -EACCES;
311f696a365SMiklos Szeredi 
312f696a365SMiklos Szeredi 	head = grab_header(inode);
3139043476fSAl Viro 	if (IS_ERR(head))
3149043476fSAl Viro 		return PTR_ERR(head);
31577b14db5SEric W. Biederman 
316f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
3179043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
3189043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
3199043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
320d7321cd6SPavel Emelyanov 		error = sysctl_perm(head->root, table, mask);
3219043476fSAl Viro 
32277b14db5SEric W. Biederman 	sysctl_head_finish(head);
32377b14db5SEric W. Biederman 	return error;
32477b14db5SEric W. Biederman }
32577b14db5SEric W. Biederman 
32677b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
32777b14db5SEric W. Biederman {
32877b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
32977b14db5SEric W. Biederman 	int error;
33077b14db5SEric W. Biederman 
33177b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
33277b14db5SEric W. Biederman 		return -EPERM;
33377b14db5SEric W. Biederman 
33477b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
3351025774cSChristoph Hellwig 	if (error)
33677b14db5SEric W. Biederman 		return error;
3371025774cSChristoph Hellwig 
3381025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
3391025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
3401025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
3411025774cSChristoph Hellwig 		if (error)
3421025774cSChristoph Hellwig 			return error;
3431025774cSChristoph Hellwig 	}
3441025774cSChristoph Hellwig 
3451025774cSChristoph Hellwig 	setattr_copy(inode, attr);
3461025774cSChristoph Hellwig 	mark_inode_dirty(inode);
3471025774cSChristoph Hellwig 	return 0;
34877b14db5SEric W. Biederman }
34977b14db5SEric W. Biederman 
3509043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3519043476fSAl Viro {
3529043476fSAl Viro 	struct inode *inode = dentry->d_inode;
3539043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
3549043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
3559043476fSAl Viro 
3569043476fSAl Viro 	if (IS_ERR(head))
3579043476fSAl Viro 		return PTR_ERR(head);
3589043476fSAl Viro 
3599043476fSAl Viro 	generic_fillattr(inode, stat);
3609043476fSAl Viro 	if (table)
3619043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
3629043476fSAl Viro 
3639043476fSAl Viro 	sysctl_head_finish(head);
3649043476fSAl Viro 	return 0;
3659043476fSAl Viro }
3669043476fSAl Viro 
36777b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
36877b14db5SEric W. Biederman 	.read		= proc_sys_read,
36977b14db5SEric W. Biederman 	.write		= proc_sys_write,
3706038f373SArnd Bergmann 	.llseek		= default_llseek,
3719043476fSAl Viro };
3729043476fSAl Viro 
3739043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
37477b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
3753222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
37677b14db5SEric W. Biederman };
37777b14db5SEric W. Biederman 
37803a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
3799043476fSAl Viro 	.permission	= proc_sys_permission,
3809043476fSAl Viro 	.setattr	= proc_sys_setattr,
3819043476fSAl Viro 	.getattr	= proc_sys_getattr,
3829043476fSAl Viro };
3839043476fSAl Viro 
3849043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
38577b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
38677b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
38777b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
3889043476fSAl Viro 	.getattr	= proc_sys_getattr,
38977b14db5SEric W. Biederman };
39077b14db5SEric W. Biederman 
39177b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
39277b14db5SEric W. Biederman {
39334286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
39434286d66SNick Piggin 		return -ECHILD;
3959043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
3969043476fSAl Viro }
3979043476fSAl Viro 
398fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
3999043476fSAl Viro {
4009043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
4019043476fSAl Viro }
4029043476fSAl Viro 
403621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
404621e155aSNick Piggin 		const struct inode *pinode,
405621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
406621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
4079043476fSAl Viro {
40831e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
40931e6b01fSNick Piggin 	 * that inode here can be NULL */
41031e6b01fSNick Piggin 	if (!inode)
41131e6b01fSNick Piggin 		return 0;
412621e155aSNick Piggin 	if (name->len != len)
4139043476fSAl Viro 		return 1;
414621e155aSNick Piggin 	if (memcmp(name->name, str, len))
4159043476fSAl Viro 		return 1;
416621e155aSNick Piggin 	return !sysctl_is_seen(PROC_I(inode)->sysctl);
41777b14db5SEric W. Biederman }
41877b14db5SEric W. Biederman 
419d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
42077b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
4219043476fSAl Viro 	.d_delete	= proc_sys_delete,
4229043476fSAl Viro 	.d_compare	= proc_sys_compare,
42377b14db5SEric W. Biederman };
42477b14db5SEric W. Biederman 
4251e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
42677b14db5SEric W. Biederman {
427e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
428e1675231SAlexey Dobriyan 
42977b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
4309043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
4319043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
43277b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
43377b14db5SEric W. Biederman 	return 0;
43477b14db5SEric W. Biederman }
435