]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * inode.c - basic inode and dentry operations. | |
3 | * | |
4 | * sysfs is Copyright (c) 2001-3 Patrick Mochel | |
5 | * | |
6 | * Please see Documentation/filesystems/sysfs.txt for more information. | |
7 | */ | |
8 | ||
9 | #undef DEBUG | |
10 | ||
11 | #include <linux/pagemap.h> | |
12 | #include <linux/namei.h> | |
13 | #include <linux/backing-dev.h> | |
16f7e0fe | 14 | #include <linux/capability.h> |
995982ca | 15 | #include <linux/errno.h> |
e8edc6e0 | 16 | #include <linux/sched.h> |
94bebf4d | 17 | #include <asm/semaphore.h> |
1da177e4 LT |
18 | #include "sysfs.h" |
19 | ||
20 | extern struct super_block * sysfs_sb; | |
21 | ||
f5e54d6e | 22 | static const struct address_space_operations sysfs_aops = { |
1da177e4 LT |
23 | .readpage = simple_readpage, |
24 | .prepare_write = simple_prepare_write, | |
25 | .commit_write = simple_commit_write | |
26 | }; | |
27 | ||
28 | static struct backing_dev_info sysfs_backing_dev_info = { | |
29 | .ra_pages = 0, /* No readahead */ | |
30 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, | |
31 | }; | |
32 | ||
c5ef1c42 | 33 | static const struct inode_operations sysfs_inode_operations ={ |
988d186d MS |
34 | .setattr = sysfs_setattr, |
35 | }; | |
36 | ||
b592fcfe EB |
37 | void sysfs_delete_inode(struct inode *inode) |
38 | { | |
39 | /* Free the shadowed directory inode operations */ | |
40 | if (sysfs_is_shadowed_inode(inode)) { | |
41 | kfree(inode->i_op); | |
42 | inode->i_op = NULL; | |
43 | } | |
44 | return generic_delete_inode(inode); | |
45 | } | |
46 | ||
988d186d MS |
47 | int sysfs_setattr(struct dentry * dentry, struct iattr * iattr) |
48 | { | |
49 | struct inode * inode = dentry->d_inode; | |
50 | struct sysfs_dirent * sd = dentry->d_fsdata; | |
51 | struct iattr * sd_iattr; | |
52 | unsigned int ia_valid = iattr->ia_valid; | |
53 | int error; | |
54 | ||
55 | if (!sd) | |
56 | return -EINVAL; | |
57 | ||
58 | sd_iattr = sd->s_iattr; | |
59 | ||
60 | error = inode_change_ok(inode, iattr); | |
61 | if (error) | |
62 | return error; | |
63 | ||
64 | error = inode_setattr(inode, iattr); | |
65 | if (error) | |
66 | return error; | |
67 | ||
68 | if (!sd_iattr) { | |
69 | /* setting attributes for the first time, allocate now */ | |
58d49283 | 70 | sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL); |
988d186d MS |
71 | if (!sd_iattr) |
72 | return -ENOMEM; | |
73 | /* assign default attributes */ | |
988d186d MS |
74 | sd_iattr->ia_mode = sd->s_mode; |
75 | sd_iattr->ia_uid = 0; | |
76 | sd_iattr->ia_gid = 0; | |
77 | sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME; | |
78 | sd->s_iattr = sd_iattr; | |
79 | } | |
80 | ||
81 | /* attributes were changed atleast once in past */ | |
82 | ||
83 | if (ia_valid & ATTR_UID) | |
84 | sd_iattr->ia_uid = iattr->ia_uid; | |
85 | if (ia_valid & ATTR_GID) | |
86 | sd_iattr->ia_gid = iattr->ia_gid; | |
87 | if (ia_valid & ATTR_ATIME) | |
88 | sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime, | |
89 | inode->i_sb->s_time_gran); | |
90 | if (ia_valid & ATTR_MTIME) | |
91 | sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime, | |
92 | inode->i_sb->s_time_gran); | |
93 | if (ia_valid & ATTR_CTIME) | |
94 | sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime, | |
95 | inode->i_sb->s_time_gran); | |
96 | if (ia_valid & ATTR_MODE) { | |
97 | umode_t mode = iattr->ia_mode; | |
98 | ||
99 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | |
100 | mode &= ~S_ISGID; | |
9ca1eb32 | 101 | sd_iattr->ia_mode = sd->s_mode = mode; |
988d186d MS |
102 | } |
103 | ||
104 | return error; | |
105 | } | |
106 | ||
8215534c MS |
107 | static inline void set_default_inode_attr(struct inode * inode, mode_t mode) |
108 | { | |
109 | inode->i_mode = mode; | |
110 | inode->i_uid = 0; | |
111 | inode->i_gid = 0; | |
112 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
113 | } | |
114 | ||
115 | static inline void set_inode_attr(struct inode * inode, struct iattr * iattr) | |
116 | { | |
117 | inode->i_mode = iattr->ia_mode; | |
118 | inode->i_uid = iattr->ia_uid; | |
119 | inode->i_gid = iattr->ia_gid; | |
120 | inode->i_atime = iattr->ia_atime; | |
121 | inode->i_mtime = iattr->ia_mtime; | |
122 | inode->i_ctime = iattr->ia_ctime; | |
123 | } | |
124 | ||
232ba9db AV |
125 | |
126 | /* | |
127 | * sysfs has a different i_mutex lock order behavior for i_mutex than other | |
128 | * filesystems; sysfs i_mutex is called in many places with subsystem locks | |
129 | * held. At the same time, many of the VFS locking rules do not apply to | |
130 | * sysfs at all (cross directory rename for example). To untangle this mess | |
131 | * (which gives false positives in lockdep), we're giving sysfs inodes their | |
132 | * own class for i_mutex. | |
133 | */ | |
134 | static struct lock_class_key sysfs_inode_imutex_key; | |
135 | ||
fc9f54b9 | 136 | void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
1da177e4 | 137 | { |
fc9f54b9 TH |
138 | inode->i_blocks = 0; |
139 | inode->i_mapping->a_ops = &sysfs_aops; | |
140 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | |
141 | inode->i_op = &sysfs_inode_operations; | |
142 | inode->i_ino = sd->s_ino; | |
143 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); | |
144 | ||
145 | if (sd->s_iattr) { | |
146 | /* sysfs_dirent has non-default attributes | |
147 | * get them for the new inode from persistent copy | |
148 | * in sysfs_dirent | |
149 | */ | |
150 | set_inode_attr(inode, sd->s_iattr); | |
151 | } else | |
152 | set_default_inode_attr(inode, sd->s_mode); | |
153 | } | |
154 | ||
155 | /** | |
8312a8d7 | 156 | * sysfs_get_inode - get inode for sysfs_dirent |
fc9f54b9 TH |
157 | * @sd: sysfs_dirent to allocate inode for |
158 | * | |
8312a8d7 TH |
159 | * Get inode for @sd. If such inode doesn't exist, a new inode |
160 | * is allocated and basics are initialized. New inode is | |
161 | * returned locked. | |
fc9f54b9 TH |
162 | * |
163 | * LOCKING: | |
164 | * Kernel thread context (may sleep). | |
165 | * | |
166 | * RETURNS: | |
167 | * Pointer to allocated inode on success, NULL on failure. | |
168 | */ | |
8312a8d7 | 169 | struct inode * sysfs_get_inode(struct sysfs_dirent *sd) |
fc9f54b9 TH |
170 | { |
171 | struct inode *inode; | |
172 | ||
8312a8d7 TH |
173 | inode = iget_locked(sysfs_sb, sd->s_ino); |
174 | if (inode && (inode->i_state & I_NEW)) | |
fc9f54b9 TH |
175 | sysfs_init_inode(sd, inode); |
176 | ||
1da177e4 LT |
177 | return inode; |
178 | } | |
179 | ||
fc9f54b9 TH |
180 | /** |
181 | * sysfs_instantiate - instantiate dentry | |
182 | * @dentry: dentry to be instantiated | |
183 | * @inode: inode associated with @sd | |
184 | * | |
8312a8d7 | 185 | * Unlock @inode if locked and instantiate @dentry with @inode. |
fc9f54b9 TH |
186 | * |
187 | * LOCKING: | |
188 | * None. | |
189 | */ | |
190 | void sysfs_instantiate(struct dentry *dentry, struct inode *inode) | |
1da177e4 | 191 | { |
fc9f54b9 TH |
192 | BUG_ON(!dentry || dentry->d_inode); |
193 | ||
fb6896da | 194 | if (inode->i_state & I_NEW) |
8312a8d7 TH |
195 | unlock_new_inode(inode); |
196 | ||
fc9f54b9 | 197 | d_instantiate(dentry, inode); |
1da177e4 LT |
198 | } |
199 | ||
dbde0fcf TH |
200 | /** |
201 | * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent | |
202 | * @sd: target sysfs_dirent | |
203 | * | |
204 | * Drop dentry for @sd. @sd must have been unlinked from its | |
205 | * parent on entry to this function such that it can't be looked | |
206 | * up anymore. | |
207 | * | |
5f995323 TH |
208 | * @sd->s_dentry which is protected with sysfs_assoc_lock points |
209 | * to the currently associated dentry but we're not holding a | |
210 | * reference to it and racing with dput(). Grab dcache_lock and | |
211 | * verify dentry before dropping it. If @sd->s_dentry is NULL or | |
212 | * dput() beats us, no need to bother. | |
1da177e4 | 213 | */ |
dbde0fcf | 214 | void sysfs_drop_dentry(struct sysfs_dirent *sd) |
1da177e4 | 215 | { |
d0bcb568 | 216 | struct dentry *dentry = NULL; |
d0bcb568 | 217 | struct inode *inode; |
1da177e4 | 218 | |
dd14cbc9 | 219 | /* We're not holding a reference to ->s_dentry dentry but the |
5f995323 | 220 | * field will stay valid as long as sysfs_assoc_lock is held. |
dd14cbc9 | 221 | */ |
5f995323 | 222 | spin_lock(&sysfs_assoc_lock); |
dd14cbc9 TH |
223 | spin_lock(&dcache_lock); |
224 | ||
d0bcb568 | 225 | /* drop dentry if it's there and dput() didn't kill it yet */ |
dbde0fcf | 226 | if (sd->s_dentry && sd->s_dentry->d_inode) { |
dd14cbc9 | 227 | dentry = dget_locked(sd->s_dentry); |
dbde0fcf TH |
228 | spin_lock(&dentry->d_lock); |
229 | __d_drop(dentry); | |
230 | spin_unlock(&dentry->d_lock); | |
231 | } | |
dd14cbc9 TH |
232 | |
233 | spin_unlock(&dcache_lock); | |
5f995323 | 234 | spin_unlock(&sysfs_assoc_lock); |
dd14cbc9 | 235 | |
d0bcb568 TH |
236 | dput(dentry); |
237 | /* XXX: unpin if directory, this will go away soon */ | |
b402d72c | 238 | if (sysfs_type(sd) == SYSFS_DIR) |
d0bcb568 | 239 | dput(dentry); |
dbde0fcf TH |
240 | |
241 | /* adjust nlink and update timestamp */ | |
d0bcb568 TH |
242 | inode = ilookup(sysfs_sb, sd->s_ino); |
243 | if (inode) { | |
244 | mutex_lock(&inode->i_mutex); | |
dbde0fcf | 245 | |
fb6896da | 246 | inode->i_ctime = CURRENT_TIME; |
d0bcb568 | 247 | drop_nlink(inode); |
b402d72c | 248 | if (sysfs_type(sd) == SYSFS_DIR) |
d0bcb568 TH |
249 | drop_nlink(inode); |
250 | ||
251 | mutex_unlock(&inode->i_mutex); | |
252 | iput(inode); | |
dbde0fcf | 253 | } |
1da177e4 LT |
254 | } |
255 | ||
608e266a | 256 | int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name) |
1da177e4 | 257 | { |
fb6896da | 258 | struct sysfs_addrm_cxt acxt; |
0c73f18b | 259 | struct sysfs_dirent **pos, *sd; |
641e6f30 | 260 | |
608e266a | 261 | if (!dir_sd) |
995982ca | 262 | return -ENOENT; |
1da177e4 | 263 | |
fb6896da | 264 | sysfs_addrm_start(&acxt, dir_sd); |
608e266a | 265 | |
608e266a | 266 | for (pos = &dir_sd->s_children; *pos; pos = &(*pos)->s_sibling) { |
0c73f18b TH |
267 | sd = *pos; |
268 | ||
b402d72c | 269 | if (!sysfs_type(sd)) |
1da177e4 | 270 | continue; |
0c096b50 | 271 | if (!strcmp(sd->s_name, name)) { |
0c73f18b TH |
272 | *pos = sd->s_sibling; |
273 | sd->s_sibling = NULL; | |
fb6896da | 274 | sysfs_remove_one(&acxt, sd); |
1da177e4 LT |
275 | break; |
276 | } | |
277 | } | |
3007e997 | 278 | |
fb6896da TH |
279 | if (sysfs_addrm_finish(&acxt)) |
280 | return 0; | |
281 | return -ENOENT; | |
1da177e4 | 282 | } |