]>
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 | ||
8215534c | 136 | struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd) |
1da177e4 LT |
137 | { |
138 | struct inode * inode = new_inode(sysfs_sb); | |
139 | if (inode) { | |
1da177e4 | 140 | inode->i_blocks = 0; |
1da177e4 LT |
141 | inode->i_mapping->a_ops = &sysfs_aops; |
142 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | |
8215534c | 143 | inode->i_op = &sysfs_inode_operations; |
dc351252 | 144 | inode->i_ino = sd->s_ino; |
232ba9db | 145 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); |
8215534c MS |
146 | |
147 | if (sd->s_iattr) { | |
148 | /* sysfs_dirent has non-default attributes | |
149 | * get them for the new inode from persistent copy | |
150 | * in sysfs_dirent | |
151 | */ | |
152 | set_inode_attr(inode, sd->s_iattr); | |
153 | } else | |
154 | set_default_inode_attr(inode, mode); | |
1da177e4 LT |
155 | } |
156 | return inode; | |
157 | } | |
158 | ||
159 | int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *)) | |
160 | { | |
161 | int error = 0; | |
162 | struct inode * inode = NULL; | |
163 | if (dentry) { | |
164 | if (!dentry->d_inode) { | |
8215534c MS |
165 | struct sysfs_dirent * sd = dentry->d_fsdata; |
166 | if ((inode = sysfs_new_inode(mode, sd))) { | |
1da177e4 LT |
167 | if (dentry->d_parent && dentry->d_parent->d_inode) { |
168 | struct inode *p_inode = dentry->d_parent->d_inode; | |
169 | p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME; | |
170 | } | |
171 | goto Proceed; | |
172 | } | |
173 | else | |
174 | error = -ENOMEM; | |
175 | } else | |
176 | error = -EEXIST; | |
177 | } else | |
178 | error = -ENOENT; | |
179 | goto Done; | |
180 | ||
181 | Proceed: | |
182 | if (init) | |
183 | error = init(inode); | |
184 | if (!error) { | |
185 | d_instantiate(dentry, inode); | |
186 | if (S_ISDIR(mode)) | |
187 | dget(dentry); /* pin only directory dentry in core */ | |
188 | } else | |
189 | iput(inode); | |
190 | Done: | |
191 | return error; | |
192 | } | |
193 | ||
94bebf4d ON |
194 | static inline void orphan_all_buffers(struct inode *node) |
195 | { | |
e7b0d26a | 196 | struct sysfs_buffer_collection *set; |
94bebf4d ON |
197 | struct sysfs_buffer *buf; |
198 | ||
d3fc373a | 199 | mutex_lock_nested(&node->i_mutex, I_MUTEX_CHILD); |
e7b0d26a AS |
200 | set = node->i_private; |
201 | if (set) { | |
202 | list_for_each_entry(buf, &set->associates, associates) { | |
203 | down(&buf->sem); | |
94bebf4d | 204 | buf->orphaned = 1; |
e7b0d26a AS |
205 | up(&buf->sem); |
206 | } | |
94bebf4d ON |
207 | } |
208 | mutex_unlock(&node->i_mutex); | |
209 | } | |
210 | ||
1da177e4 LT |
211 | |
212 | /* | |
213 | * Unhashes the dentry corresponding to given sysfs_dirent | |
1b1dcc1b | 214 | * Called with parent inode's i_mutex held. |
1da177e4 LT |
215 | */ |
216 | void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent) | |
217 | { | |
dd14cbc9 | 218 | struct dentry *dentry = NULL; |
94bebf4d | 219 | struct inode *inode; |
1da177e4 | 220 | |
dd14cbc9 TH |
221 | /* We're not holding a reference to ->s_dentry dentry but the |
222 | * field will stay valid as long as sysfs_lock is held. | |
223 | */ | |
224 | spin_lock(&sysfs_lock); | |
225 | spin_lock(&dcache_lock); | |
226 | ||
227 | /* dget dentry if it's still alive */ | |
228 | if (sd->s_dentry && sd->s_dentry->d_inode) | |
229 | dentry = dget_locked(sd->s_dentry); | |
230 | ||
231 | spin_unlock(&dcache_lock); | |
232 | spin_unlock(&sysfs_lock); | |
233 | ||
234 | /* drop dentry */ | |
1da177e4 LT |
235 | if (dentry) { |
236 | spin_lock(&dcache_lock); | |
237 | spin_lock(&dentry->d_lock); | |
6aa054aa | 238 | if (!d_unhashed(dentry) && dentry->d_inode) { |
94bebf4d ON |
239 | inode = dentry->d_inode; |
240 | spin_lock(&inode->i_lock); | |
241 | __iget(inode); | |
242 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
243 | dget_locked(dentry); |
244 | __d_drop(dentry); | |
245 | spin_unlock(&dentry->d_lock); | |
246 | spin_unlock(&dcache_lock); | |
247 | simple_unlink(parent->d_inode, dentry); | |
94bebf4d ON |
248 | orphan_all_buffers(inode); |
249 | iput(inode); | |
1da177e4 LT |
250 | } else { |
251 | spin_unlock(&dentry->d_lock); | |
252 | spin_unlock(&dcache_lock); | |
253 | } | |
dd14cbc9 TH |
254 | |
255 | dput(dentry); | |
1da177e4 LT |
256 | } |
257 | } | |
258 | ||
995982ca | 259 | int sysfs_hash_and_remove(struct dentry * dir, const char * name) |
1da177e4 LT |
260 | { |
261 | struct sysfs_dirent * sd; | |
641e6f30 | 262 | struct sysfs_dirent * parent_sd; |
995982ca | 263 | int found = 0; |
641e6f30 GKH |
264 | |
265 | if (!dir) | |
995982ca | 266 | return -ENOENT; |
1da177e4 | 267 | |
36676bcb JB |
268 | if (dir->d_inode == NULL) |
269 | /* no inode means this hasn't been made visible yet */ | |
995982ca | 270 | return -ENOENT; |
36676bcb | 271 | |
641e6f30 | 272 | parent_sd = dir->d_fsdata; |
d3fc373a | 273 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
1da177e4 | 274 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
3e519038 | 275 | if (!sd->s_type) |
1da177e4 | 276 | continue; |
0c096b50 | 277 | if (!strcmp(sd->s_name, name)) { |
1da177e4 LT |
278 | list_del_init(&sd->s_sibling); |
279 | sysfs_drop_dentry(sd, dir); | |
995982ca | 280 | found = 1; |
1da177e4 LT |
281 | break; |
282 | } | |
283 | } | |
1b1dcc1b | 284 | mutex_unlock(&dir->d_inode->i_mutex); |
995982ca | 285 | |
0ab66088 TH |
286 | if (!found) |
287 | return -ENOENT; | |
288 | ||
289 | sysfs_deactivate(sd); | |
290 | sysfs_put(sd); | |
291 | return 0; | |
1da177e4 | 292 | } |