xref: /linux/fs/configfs/inode.c (revision a48b0872e69428d3d02994dcfad3519f01def7fa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * inode.c - basic inode and dentry operations.
4  *
5  * Based on sysfs:
6  * 	sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
7  *
8  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
9  *
10  * Please see Documentation/filesystems/configfs.rst for more
11  * information.
12  */
13 
14 #undef DEBUG
15 
16 #include <linux/pagemap.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/capability.h>
20 #include <linux/sched.h>
21 #include <linux/lockdep.h>
22 #include <linux/slab.h>
23 
24 #include <linux/configfs.h>
25 #include "configfs_internal.h"
26 
27 #ifdef CONFIG_LOCKDEP
28 static struct lock_class_key default_group_class[MAX_LOCK_DEPTH];
29 #endif
30 
31 static const struct address_space_operations configfs_aops = {
32 	.readpage	= simple_readpage,
33 	.write_begin	= simple_write_begin,
34 	.write_end	= simple_write_end,
35 };
36 
37 static const struct inode_operations configfs_inode_operations ={
38 	.setattr	= configfs_setattr,
39 };
40 
41 int configfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
42 		     struct iattr *iattr)
43 {
44 	struct inode * inode = d_inode(dentry);
45 	struct configfs_dirent * sd = dentry->d_fsdata;
46 	struct iattr * sd_iattr;
47 	unsigned int ia_valid = iattr->ia_valid;
48 	int error;
49 
50 	if (!sd)
51 		return -EINVAL;
52 
53 	sd_iattr = sd->s_iattr;
54 	if (!sd_iattr) {
55 		/* setting attributes for the first time, allocate now */
56 		sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
57 		if (!sd_iattr)
58 			return -ENOMEM;
59 		/* assign default attributes */
60 		sd_iattr->ia_mode = sd->s_mode;
61 		sd_iattr->ia_uid = GLOBAL_ROOT_UID;
62 		sd_iattr->ia_gid = GLOBAL_ROOT_GID;
63 		sd_iattr->ia_atime = sd_iattr->ia_mtime =
64 			sd_iattr->ia_ctime = current_time(inode);
65 		sd->s_iattr = sd_iattr;
66 	}
67 	/* attributes were changed atleast once in past */
68 
69 	error = simple_setattr(mnt_userns, dentry, iattr);
70 	if (error)
71 		return error;
72 
73 	if (ia_valid & ATTR_UID)
74 		sd_iattr->ia_uid = iattr->ia_uid;
75 	if (ia_valid & ATTR_GID)
76 		sd_iattr->ia_gid = iattr->ia_gid;
77 	if (ia_valid & ATTR_ATIME)
78 		sd_iattr->ia_atime = iattr->ia_atime;
79 	if (ia_valid & ATTR_MTIME)
80 		sd_iattr->ia_mtime = iattr->ia_mtime;
81 	if (ia_valid & ATTR_CTIME)
82 		sd_iattr->ia_ctime = iattr->ia_ctime;
83 	if (ia_valid & ATTR_MODE) {
84 		umode_t mode = iattr->ia_mode;
85 
86 		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
87 			mode &= ~S_ISGID;
88 		sd_iattr->ia_mode = sd->s_mode = mode;
89 	}
90 
91 	return error;
92 }
93 
94 static inline void set_default_inode_attr(struct inode * inode, umode_t mode)
95 {
96 	inode->i_mode = mode;
97 	inode->i_atime = inode->i_mtime =
98 		inode->i_ctime = current_time(inode);
99 }
100 
101 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
102 {
103 	inode->i_mode = iattr->ia_mode;
104 	inode->i_uid = iattr->ia_uid;
105 	inode->i_gid = iattr->ia_gid;
106 	inode->i_atime = iattr->ia_atime;
107 	inode->i_mtime = iattr->ia_mtime;
108 	inode->i_ctime = iattr->ia_ctime;
109 }
110 
111 struct inode *configfs_new_inode(umode_t mode, struct configfs_dirent *sd,
112 				 struct super_block *s)
113 {
114 	struct inode * inode = new_inode(s);
115 	if (inode) {
116 		inode->i_ino = get_next_ino();
117 		inode->i_mapping->a_ops = &configfs_aops;
118 		inode->i_op = &configfs_inode_operations;
119 
120 		if (sd->s_iattr) {
121 			/* sysfs_dirent has non-default attributes
122 			 * get them for the new inode from persistent copy
123 			 * in sysfs_dirent
124 			 */
125 			set_inode_attr(inode, sd->s_iattr);
126 		} else
127 			set_default_inode_attr(inode, mode);
128 	}
129 	return inode;
130 }
131 
132 #ifdef CONFIG_LOCKDEP
133 
134 static void configfs_set_inode_lock_class(struct configfs_dirent *sd,
135 					  struct inode *inode)
136 {
137 	int depth = sd->s_depth;
138 
139 	if (depth > 0) {
140 		if (depth <= ARRAY_SIZE(default_group_class)) {
141 			lockdep_set_class(&inode->i_rwsem,
142 					  &default_group_class[depth - 1]);
143 		} else {
144 			/*
145 			 * In practice the maximum level of locking depth is
146 			 * already reached. Just inform about possible reasons.
147 			 */
148 			pr_info("Too many levels of inodes for the locking correctness validator.\n");
149 			pr_info("Spurious warnings may appear.\n");
150 		}
151 	}
152 }
153 
154 #else /* CONFIG_LOCKDEP */
155 
156 static void configfs_set_inode_lock_class(struct configfs_dirent *sd,
157 					  struct inode *inode)
158 {
159 }
160 
161 #endif /* CONFIG_LOCKDEP */
162 
163 struct inode *configfs_create(struct dentry *dentry, umode_t mode)
164 {
165 	struct inode *inode = NULL;
166 	struct configfs_dirent *sd;
167 	struct inode *p_inode;
168 
169 	if (!dentry)
170 		return ERR_PTR(-ENOENT);
171 
172 	if (d_really_is_positive(dentry))
173 		return ERR_PTR(-EEXIST);
174 
175 	sd = dentry->d_fsdata;
176 	inode = configfs_new_inode(mode, sd, dentry->d_sb);
177 	if (!inode)
178 		return ERR_PTR(-ENOMEM);
179 
180 	p_inode = d_inode(dentry->d_parent);
181 	p_inode->i_mtime = p_inode->i_ctime = current_time(p_inode);
182 	configfs_set_inode_lock_class(sd, inode);
183 	return inode;
184 }
185 
186 /*
187  * Get the name for corresponding element represented by the given configfs_dirent
188  */
189 const unsigned char * configfs_get_name(struct configfs_dirent *sd)
190 {
191 	struct configfs_attribute *attr;
192 
193 	BUG_ON(!sd || !sd->s_element);
194 
195 	/* These always have a dentry, so use that */
196 	if (sd->s_type & (CONFIGFS_DIR | CONFIGFS_ITEM_LINK))
197 		return sd->s_dentry->d_name.name;
198 
199 	if (sd->s_type & (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)) {
200 		attr = sd->s_element;
201 		return attr->ca_name;
202 	}
203 	return NULL;
204 }
205 
206 
207 /*
208  * Unhashes the dentry corresponding to given configfs_dirent
209  * Called with parent inode's i_mutex held.
210  */
211 void configfs_drop_dentry(struct configfs_dirent * sd, struct dentry * parent)
212 {
213 	struct dentry * dentry = sd->s_dentry;
214 
215 	if (dentry) {
216 		spin_lock(&dentry->d_lock);
217 		if (simple_positive(dentry)) {
218 			dget_dlock(dentry);
219 			__d_drop(dentry);
220 			spin_unlock(&dentry->d_lock);
221 			simple_unlink(d_inode(parent), dentry);
222 		} else
223 			spin_unlock(&dentry->d_lock);
224 	}
225 }
226 
227 void configfs_hash_and_remove(struct dentry * dir, const char * name)
228 {
229 	struct configfs_dirent * sd;
230 	struct configfs_dirent * parent_sd = dir->d_fsdata;
231 
232 	if (d_really_is_negative(dir))
233 		/* no inode means this hasn't been made visible yet */
234 		return;
235 
236 	inode_lock(d_inode(dir));
237 	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
238 		if (!sd->s_element)
239 			continue;
240 		if (!strcmp(configfs_get_name(sd), name)) {
241 			spin_lock(&configfs_dirent_lock);
242 			list_del_init(&sd->s_sibling);
243 			spin_unlock(&configfs_dirent_lock);
244 			configfs_drop_dentry(sd, dir);
245 			configfs_put(sd);
246 			break;
247 		}
248 	}
249 	inode_unlock(d_inode(dir));
250 }
251