xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 85fe4025)
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>
877b14db5SEric W. Biederman #include "internal.h"
977b14db5SEric W. Biederman 
10d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1177b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1203a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
139043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
149043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
1577b14db5SEric W. Biederman 
169043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
179043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
1877b14db5SEric W. Biederman {
1977b14db5SEric W. Biederman 	struct inode *inode;
209043476fSAl Viro 	struct proc_inode *ei;
2177b14db5SEric W. Biederman 
229043476fSAl Viro 	inode = new_inode(sb);
2377b14db5SEric W. Biederman 	if (!inode)
2477b14db5SEric W. Biederman 		goto out;
2577b14db5SEric W. Biederman 
2685fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2785fe4025SChristoph Hellwig 
289043476fSAl Viro 	sysctl_head_get(head);
2977b14db5SEric W. Biederman 	ei = PROC_I(inode);
309043476fSAl Viro 	ei->sysctl = head;
319043476fSAl Viro 	ei->sysctl_entry = table;
329043476fSAl Viro 
3377b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
349043476fSAl Viro 	inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
359043476fSAl Viro 	inode->i_mode = table->mode;
369043476fSAl Viro 	if (!table->child) {
379043476fSAl Viro 		inode->i_mode |= S_IFREG;
3877b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
3977b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
409043476fSAl Viro 	} else {
419043476fSAl Viro 		inode->i_mode |= S_IFDIR;
429043476fSAl Viro 		inode->i_nlink = 0;
439043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
449043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
459043476fSAl Viro 	}
4677b14db5SEric W. Biederman out:
4777b14db5SEric W. Biederman 	return inode;
4877b14db5SEric W. Biederman }
4977b14db5SEric W. Biederman 
509043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
5177b14db5SEric W. Biederman {
5277b14db5SEric W. Biederman 	int len;
532315ffa0SEric W. Biederman 	for ( ; p->procname; p++) {
5477b14db5SEric W. Biederman 
559043476fSAl Viro 		if (!p->procname)
5677b14db5SEric W. Biederman 			continue;
5777b14db5SEric W. Biederman 
589043476fSAl Viro 		len = strlen(p->procname);
5977b14db5SEric W. Biederman 		if (len != name->len)
6077b14db5SEric W. Biederman 			continue;
6177b14db5SEric W. Biederman 
629043476fSAl Viro 		if (memcmp(p->procname, name->name, len) != 0)
6377b14db5SEric W. Biederman 			continue;
6477b14db5SEric W. Biederman 
6577b14db5SEric W. Biederman 		/* I have a match */
669043476fSAl Viro 		return p;
6777b14db5SEric W. Biederman 	}
6877b14db5SEric W. Biederman 	return NULL;
6977b14db5SEric W. Biederman }
7077b14db5SEric W. Biederman 
7181324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
7277b14db5SEric W. Biederman {
739043476fSAl Viro 	if (PROC_I(inode)->sysctl)
749043476fSAl Viro 		return sysctl_head_grab(PROC_I(inode)->sysctl);
759043476fSAl Viro 	else
769043476fSAl Viro 		return sysctl_head_next(NULL);
7777b14db5SEric W. Biederman }
7877b14db5SEric W. Biederman 
7977b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
8077b14db5SEric W. Biederman 					struct nameidata *nd)
8177b14db5SEric W. Biederman {
829043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
839043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
849043476fSAl Viro 	struct ctl_table_header *h = NULL;
859043476fSAl Viro 	struct qstr *name = &dentry->d_name;
869043476fSAl Viro 	struct ctl_table *p;
8777b14db5SEric W. Biederman 	struct inode *inode;
889043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
8977b14db5SEric W. Biederman 
909043476fSAl Viro 	if (IS_ERR(head))
919043476fSAl Viro 		return ERR_CAST(head);
929043476fSAl Viro 
939043476fSAl Viro 	if (table && !table->child) {
949043476fSAl Viro 		WARN_ON(1);
959043476fSAl Viro 		goto out;
969043476fSAl Viro 	}
979043476fSAl Viro 
989043476fSAl Viro 	table = table ? table->child : head->ctl_table;
999043476fSAl Viro 
1009043476fSAl Viro 	p = find_in_table(table, name);
1019043476fSAl Viro 	if (!p) {
1029043476fSAl Viro 		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
1039043476fSAl Viro 			if (h->attached_to != table)
1049043476fSAl Viro 				continue;
1059043476fSAl Viro 			p = find_in_table(h->attached_by, name);
1069043476fSAl Viro 			if (p)
1079043476fSAl Viro 				break;
1089043476fSAl Viro 		}
1099043476fSAl Viro 	}
1109043476fSAl Viro 
1119043476fSAl Viro 	if (!p)
11277b14db5SEric W. Biederman 		goto out;
11377b14db5SEric W. Biederman 
11477b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
1159043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
1169043476fSAl Viro 	if (h)
1179043476fSAl Viro 		sysctl_head_finish(h);
1189043476fSAl Viro 
11977b14db5SEric W. Biederman 	if (!inode)
12077b14db5SEric W. Biederman 		goto out;
12177b14db5SEric W. Biederman 
12277b14db5SEric W. Biederman 	err = NULL;
12377b14db5SEric W. Biederman 	dentry->d_op = &proc_sys_dentry_operations;
12477b14db5SEric W. Biederman 	d_add(dentry, inode);
12577b14db5SEric W. Biederman 
12677b14db5SEric W. Biederman out:
12777b14db5SEric W. Biederman 	sysctl_head_finish(head);
12877b14db5SEric W. Biederman 	return err;
12977b14db5SEric W. Biederman }
13077b14db5SEric W. Biederman 
1317708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
1327708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
13377b14db5SEric W. Biederman {
1349043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
1359043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
1369043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
1372a2da53bSDavid Howells 	ssize_t error;
1382a2da53bSDavid Howells 	size_t res;
13977b14db5SEric W. Biederman 
1409043476fSAl Viro 	if (IS_ERR(head))
1419043476fSAl Viro 		return PTR_ERR(head);
14277b14db5SEric W. Biederman 
14377b14db5SEric W. Biederman 	/*
14477b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
14577b14db5SEric W. Biederman 	 * and won't be until we finish.
14677b14db5SEric W. Biederman 	 */
14777b14db5SEric W. Biederman 	error = -EPERM;
148d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
14977b14db5SEric W. Biederman 		goto out;
15077b14db5SEric W. Biederman 
1519043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
1529043476fSAl Viro 	error = -EINVAL;
1539043476fSAl Viro 	if (!table->proc_handler)
1549043476fSAl Viro 		goto out;
1559043476fSAl Viro 
15677b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
15777b14db5SEric W. Biederman 	res = count;
1588d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
15977b14db5SEric W. Biederman 	if (!error)
16077b14db5SEric W. Biederman 		error = res;
16177b14db5SEric W. Biederman out:
16277b14db5SEric W. Biederman 	sysctl_head_finish(head);
16377b14db5SEric W. Biederman 
16477b14db5SEric W. Biederman 	return error;
16577b14db5SEric W. Biederman }
16677b14db5SEric W. Biederman 
1677708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
1687708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
1697708bfb1SPavel Emelyanov {
1707708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
1717708bfb1SPavel Emelyanov }
1727708bfb1SPavel Emelyanov 
17377b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
17477b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
17577b14db5SEric W. Biederman {
1767708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
17777b14db5SEric W. Biederman }
17877b14db5SEric W. Biederman 
17977b14db5SEric W. Biederman 
18077b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
1819043476fSAl Viro 				filldir_t filldir,
1829043476fSAl Viro 				struct ctl_table_header *head,
1839043476fSAl Viro 				struct ctl_table *table)
18477b14db5SEric W. Biederman {
18577b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
18677b14db5SEric W. Biederman 	struct inode *inode;
18777b14db5SEric W. Biederman 	struct qstr qname;
18877b14db5SEric W. Biederman 	ino_t ino = 0;
18977b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
19077b14db5SEric W. Biederman 
19177b14db5SEric W. Biederman 	qname.name = table->procname;
19277b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
19377b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
19477b14db5SEric W. Biederman 
19577b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
19677b14db5SEric W. Biederman 	if (!child) {
1979043476fSAl Viro 		child = d_alloc(dir, &qname);
1989043476fSAl Viro 		if (child) {
1999043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
2009043476fSAl Viro 			if (!inode) {
2019043476fSAl Viro 				dput(child);
2029043476fSAl Viro 				return -ENOMEM;
2039043476fSAl Viro 			} else {
2049043476fSAl Viro 				child->d_op = &proc_sys_dentry_operations;
2059043476fSAl Viro 				d_add(child, inode);
20677b14db5SEric W. Biederman 			}
2079043476fSAl Viro 		} else {
2089043476fSAl Viro 			return -ENOMEM;
20977b14db5SEric W. Biederman 		}
21077b14db5SEric W. Biederman 	}
21177b14db5SEric W. Biederman 	inode = child->d_inode;
21277b14db5SEric W. Biederman 	ino  = inode->i_ino;
21377b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
21477b14db5SEric W. Biederman 	dput(child);
2159043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
2169043476fSAl Viro }
2179043476fSAl Viro 
2189043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
2199043476fSAl Viro 		unsigned long *pos, struct file *file,
2209043476fSAl Viro 		void *dirent, filldir_t filldir)
2219043476fSAl Viro {
2229043476fSAl Viro 
2232315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
2249043476fSAl Viro 		int res;
2259043476fSAl Viro 
2269043476fSAl Viro 		/* Can't do anything without a proc name */
2279043476fSAl Viro 		if (!table->procname)
2289043476fSAl Viro 			continue;
2299043476fSAl Viro 
2309043476fSAl Viro 		if (*pos < file->f_pos)
2319043476fSAl Viro 			continue;
2329043476fSAl Viro 
2339043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
2349043476fSAl Viro 		if (res)
2359043476fSAl Viro 			return res;
2369043476fSAl Viro 
2379043476fSAl Viro 		file->f_pos = *pos + 1;
2389043476fSAl Viro 	}
2399043476fSAl Viro 	return 0;
24077b14db5SEric W. Biederman }
24177b14db5SEric W. Biederman 
24277b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
24377b14db5SEric W. Biederman {
2449043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
24577b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
2469043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
2479043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2489043476fSAl Viro 	struct ctl_table_header *h = NULL;
24977b14db5SEric W. Biederman 	unsigned long pos;
2509043476fSAl Viro 	int ret = -EINVAL;
25177b14db5SEric W. Biederman 
2529043476fSAl Viro 	if (IS_ERR(head))
2539043476fSAl Viro 		return PTR_ERR(head);
2549043476fSAl Viro 
2559043476fSAl Viro 	if (table && !table->child) {
2569043476fSAl Viro 		WARN_ON(1);
25777b14db5SEric W. Biederman 		goto out;
2589043476fSAl Viro 	}
2599043476fSAl Viro 
2609043476fSAl Viro 	table = table ? table->child : head->ctl_table;
26177b14db5SEric W. Biederman 
26277b14db5SEric W. Biederman 	ret = 0;
26377b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
26477b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
26577b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
26677b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
26777b14db5SEric W. Biederman 			goto out;
26877b14db5SEric W. Biederman 		filp->f_pos++;
26977b14db5SEric W. Biederman 	}
27077b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
27177b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
27277b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
27377b14db5SEric W. Biederman 			goto out;
27477b14db5SEric W. Biederman 		filp->f_pos++;
27577b14db5SEric W. Biederman 	}
27677b14db5SEric W. Biederman 	pos = 2;
27777b14db5SEric W. Biederman 
2789043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
2799043476fSAl Viro 	if (ret)
28077b14db5SEric W. Biederman 		goto out;
2819043476fSAl Viro 
2829043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
2839043476fSAl Viro 		if (h->attached_to != table)
2849043476fSAl Viro 			continue;
2859043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
2869043476fSAl Viro 		if (ret) {
2879043476fSAl Viro 			sysctl_head_finish(h);
2889043476fSAl Viro 			break;
28977b14db5SEric W. Biederman 		}
29077b14db5SEric W. Biederman 	}
29177b14db5SEric W. Biederman 	ret = 1;
29277b14db5SEric W. Biederman out:
29377b14db5SEric W. Biederman 	sysctl_head_finish(head);
29477b14db5SEric W. Biederman 	return ret;
29577b14db5SEric W. Biederman }
29677b14db5SEric W. Biederman 
297e6305c43SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
29877b14db5SEric W. Biederman {
29977b14db5SEric W. Biederman 	/*
30077b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
30177b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
30277b14db5SEric W. Biederman 	 */
303f696a365SMiklos Szeredi 	struct ctl_table_header *head;
304f696a365SMiklos Szeredi 	struct ctl_table *table;
30577b14db5SEric W. Biederman 	int error;
30677b14db5SEric W. Biederman 
307f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
308f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
309f696a365SMiklos Szeredi 		return -EACCES;
310f696a365SMiklos Szeredi 
311f696a365SMiklos Szeredi 	head = grab_header(inode);
3129043476fSAl Viro 	if (IS_ERR(head))
3139043476fSAl Viro 		return PTR_ERR(head);
31477b14db5SEric W. Biederman 
315f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
3169043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
3179043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
3189043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
319d7321cd6SPavel Emelyanov 		error = sysctl_perm(head->root, table, mask);
3209043476fSAl Viro 
32177b14db5SEric W. Biederman 	sysctl_head_finish(head);
32277b14db5SEric W. Biederman 	return error;
32377b14db5SEric W. Biederman }
32477b14db5SEric W. Biederman 
32577b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
32677b14db5SEric W. Biederman {
32777b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
32877b14db5SEric W. Biederman 	int error;
32977b14db5SEric W. Biederman 
33077b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
33177b14db5SEric W. Biederman 		return -EPERM;
33277b14db5SEric W. Biederman 
33377b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
3341025774cSChristoph Hellwig 	if (error)
33577b14db5SEric W. Biederman 		return error;
3361025774cSChristoph Hellwig 
3371025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
3381025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
3391025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
3401025774cSChristoph Hellwig 		if (error)
3411025774cSChristoph Hellwig 			return error;
3421025774cSChristoph Hellwig 	}
3431025774cSChristoph Hellwig 
3441025774cSChristoph Hellwig 	setattr_copy(inode, attr);
3451025774cSChristoph Hellwig 	mark_inode_dirty(inode);
3461025774cSChristoph Hellwig 	return 0;
34777b14db5SEric W. Biederman }
34877b14db5SEric W. Biederman 
3499043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3509043476fSAl Viro {
3519043476fSAl Viro 	struct inode *inode = dentry->d_inode;
3529043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
3539043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
3549043476fSAl Viro 
3559043476fSAl Viro 	if (IS_ERR(head))
3569043476fSAl Viro 		return PTR_ERR(head);
3579043476fSAl Viro 
3589043476fSAl Viro 	generic_fillattr(inode, stat);
3599043476fSAl Viro 	if (table)
3609043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
3619043476fSAl Viro 
3629043476fSAl Viro 	sysctl_head_finish(head);
3639043476fSAl Viro 	return 0;
3649043476fSAl Viro }
3659043476fSAl Viro 
36677b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
36777b14db5SEric W. Biederman 	.read		= proc_sys_read,
36877b14db5SEric W. Biederman 	.write		= proc_sys_write,
3696038f373SArnd Bergmann 	.llseek		= default_llseek,
3709043476fSAl Viro };
3719043476fSAl Viro 
3729043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
37377b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
3743222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
37577b14db5SEric W. Biederman };
37677b14db5SEric W. Biederman 
37703a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
3789043476fSAl Viro 	.permission	= proc_sys_permission,
3799043476fSAl Viro 	.setattr	= proc_sys_setattr,
3809043476fSAl Viro 	.getattr	= proc_sys_getattr,
3819043476fSAl Viro };
3829043476fSAl Viro 
3839043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
38477b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
38577b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
38677b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
3879043476fSAl Viro 	.getattr	= proc_sys_getattr,
38877b14db5SEric W. Biederman };
38977b14db5SEric W. Biederman 
39077b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
39177b14db5SEric W. Biederman {
3929043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
3939043476fSAl Viro }
3949043476fSAl Viro 
3959043476fSAl Viro static int proc_sys_delete(struct dentry *dentry)
3969043476fSAl Viro {
3979043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
3989043476fSAl Viro }
3999043476fSAl Viro 
4009043476fSAl Viro static int proc_sys_compare(struct dentry *dir, struct qstr *qstr,
4019043476fSAl Viro 			    struct qstr *name)
4029043476fSAl Viro {
4039043476fSAl Viro 	struct dentry *dentry = container_of(qstr, struct dentry, d_name);
4049043476fSAl Viro 	if (qstr->len != name->len)
4059043476fSAl Viro 		return 1;
4069043476fSAl Viro 	if (memcmp(qstr->name, name->name, name->len))
4079043476fSAl Viro 		return 1;
4089043476fSAl Viro 	return !sysctl_is_seen(PROC_I(dentry->d_inode)->sysctl);
40977b14db5SEric W. Biederman }
41077b14db5SEric W. Biederman 
411d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
41277b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
4139043476fSAl Viro 	.d_delete	= proc_sys_delete,
4149043476fSAl Viro 	.d_compare	= proc_sys_compare,
41577b14db5SEric W. Biederman };
41677b14db5SEric W. Biederman 
4171e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
41877b14db5SEric W. Biederman {
419e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
420e1675231SAlexey Dobriyan 
42177b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
4229043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
4239043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
42477b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
42577b14db5SEric W. Biederman 	return 0;
42677b14db5SEric W. Biederman }
427