2 * inode.c - basic inode and dentry operations.
4 * sysfs is Copyright (c) 2001-3 Patrick Mochel
6 * Please see Documentation/filesystems/sysfs.txt for more information.
11 #include <linux/pagemap.h>
12 #include <linux/namei.h>
13 #include <linux/backing-dev.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <asm/semaphore.h>
20 extern struct super_block
* sysfs_sb
;
22 static const struct address_space_operations sysfs_aops
= {
23 .readpage
= simple_readpage
,
24 .prepare_write
= simple_prepare_write
,
25 .commit_write
= simple_commit_write
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
,
33 static const struct inode_operations sysfs_inode_operations
={
34 .setattr
= sysfs_setattr
,
37 void sysfs_delete_inode(struct inode
*inode
)
39 /* Free the shadowed directory inode operations */
40 if (sysfs_is_shadowed_inode(inode
)) {
44 return generic_delete_inode(inode
);
47 int sysfs_setattr(struct dentry
* dentry
, struct iattr
* iattr
)
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
;
58 sd_iattr
= sd
->s_iattr
;
60 error
= inode_change_ok(inode
, iattr
);
64 error
= inode_setattr(inode
, iattr
);
69 /* setting attributes for the first time, allocate now */
70 sd_iattr
= kzalloc(sizeof(struct iattr
), GFP_KERNEL
);
73 /* assign default attributes */
74 sd_iattr
->ia_mode
= sd
->s_mode
;
77 sd_iattr
->ia_atime
= sd_iattr
->ia_mtime
= sd_iattr
->ia_ctime
= CURRENT_TIME
;
78 sd
->s_iattr
= sd_iattr
;
81 /* attributes were changed atleast once in past */
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
;
99 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
101 sd_iattr
->ia_mode
= sd
->s_mode
= mode
;
107 static inline void set_default_inode_attr(struct inode
* inode
, mode_t mode
)
109 inode
->i_mode
= mode
;
112 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
115 static inline void set_inode_attr(struct inode
* inode
, struct iattr
* iattr
)
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
;
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.
134 static struct lock_class_key sysfs_inode_imutex_key
;
136 void sysfs_init_inode(struct sysfs_dirent
*sd
, struct inode
*inode
)
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
);
146 /* sysfs_dirent has non-default attributes
147 * get them for the new inode from persistent copy
150 set_inode_attr(inode
, sd
->s_iattr
);
152 set_default_inode_attr(inode
, sd
->s_mode
);
156 * sysfs_get_inode - get inode for sysfs_dirent
157 * @sd: sysfs_dirent to allocate inode for
159 * Get inode for @sd. If such inode doesn't exist, a new inode
160 * is allocated and basics are initialized. New inode is
164 * Kernel thread context (may sleep).
167 * Pointer to allocated inode on success, NULL on failure.
169 struct inode
* sysfs_get_inode(struct sysfs_dirent
*sd
)
173 inode
= iget_locked(sysfs_sb
, sd
->s_ino
);
174 if (inode
&& (inode
->i_state
& I_NEW
))
175 sysfs_init_inode(sd
, inode
);
181 * sysfs_instantiate - instantiate dentry
182 * @dentry: dentry to be instantiated
183 * @inode: inode associated with @sd
185 * Unlock @inode if locked and instantiate @dentry with @inode.
190 void sysfs_instantiate(struct dentry
*dentry
, struct inode
*inode
)
192 BUG_ON(!dentry
|| dentry
->d_inode
);
194 if (inode
->i_state
& I_NEW
) {
195 unlock_new_inode(inode
);
197 if (dentry
->d_parent
&& dentry
->d_parent
->d_inode
) {
198 struct inode
*p_inode
= dentry
->d_parent
->d_inode
;
199 p_inode
->i_mtime
= p_inode
->i_ctime
= CURRENT_TIME
;
203 d_instantiate(dentry
, inode
);
207 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
208 * @sd: target sysfs_dirent
210 * Drop dentry for @sd. @sd must have been unlinked from its
211 * parent on entry to this function such that it can't be looked
214 * @sd->s_dentry which is protected with sysfs_lock points to the
215 * currently associated dentry but we're not holding a reference
216 * to it and racing with dput(). Grab dcache_lock and verify
217 * dentry before dropping it. If @sd->s_dentry is NULL or dput()
218 * beats us, no need to bother.
220 void sysfs_drop_dentry(struct sysfs_dirent
*sd
)
222 struct dentry
*dentry
= NULL
;
223 struct timespec curtime
;
226 /* We're not holding a reference to ->s_dentry dentry but the
227 * field will stay valid as long as sysfs_lock is held.
229 spin_lock(&sysfs_lock
);
230 spin_lock(&dcache_lock
);
232 /* drop dentry if it's there and dput() didn't kill it yet */
233 if (sd
->s_dentry
&& sd
->s_dentry
->d_inode
) {
234 dentry
= dget_locked(sd
->s_dentry
);
235 spin_lock(&dentry
->d_lock
);
237 spin_unlock(&dentry
->d_lock
);
240 spin_unlock(&dcache_lock
);
241 spin_unlock(&sysfs_lock
);
244 /* XXX: unpin if directory, this will go away soon */
245 if (sysfs_type(sd
) == SYSFS_DIR
)
248 /* adjust nlink and update timestamp */
249 curtime
= CURRENT_TIME
;
251 inode
= ilookup(sysfs_sb
, sd
->s_ino
);
253 mutex_lock(&inode
->i_mutex
);
255 inode
->i_ctime
= curtime
;
257 if (sysfs_type(sd
) == SYSFS_DIR
)
260 mutex_unlock(&inode
->i_mutex
);
264 /* adjust nlink and udpate timestamp of the parent */
265 inode
= ilookup(sysfs_sb
, sd
->s_parent
->s_ino
);
267 mutex_lock(&inode
->i_mutex
);
269 inode
->i_ctime
= inode
->i_mtime
= curtime
;
270 if (sysfs_type(sd
) == SYSFS_DIR
)
273 mutex_unlock(&inode
->i_mutex
);
278 int sysfs_hash_and_remove(struct dentry
* dir
, const char * name
)
280 struct sysfs_dirent
**pos
, *sd
;
281 struct sysfs_dirent
*parent_sd
;
287 if (dir
->d_inode
== NULL
)
288 /* no inode means this hasn't been made visible yet */
291 parent_sd
= dir
->d_fsdata
;
292 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
293 for (pos
= &parent_sd
->s_children
; *pos
; pos
= &(*pos
)->s_sibling
) {
298 if (!strcmp(sd
->s_name
, name
)) {
299 *pos
= sd
->s_sibling
;
300 sd
->s_sibling
= NULL
;
305 mutex_unlock(&dir
->d_inode
->i_mutex
);
310 sysfs_drop_dentry(sd
);
311 sysfs_deactivate(sd
);