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 <asm/semaphore.h>
19 extern struct super_block
* sysfs_sb
;
21 static const struct address_space_operations sysfs_aops
= {
22 .readpage
= simple_readpage
,
23 .prepare_write
= simple_prepare_write
,
24 .commit_write
= simple_commit_write
27 static struct backing_dev_info sysfs_backing_dev_info
= {
28 .ra_pages
= 0, /* No readahead */
29 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
32 static const struct inode_operations sysfs_inode_operations
={
33 .setattr
= sysfs_setattr
,
36 void sysfs_delete_inode(struct inode
*inode
)
38 /* Free the shadowed directory inode operations */
39 if (sysfs_is_shadowed_inode(inode
)) {
43 return generic_delete_inode(inode
);
46 int sysfs_setattr(struct dentry
* dentry
, struct iattr
* iattr
)
48 struct inode
* inode
= dentry
->d_inode
;
49 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
50 struct iattr
* sd_iattr
;
51 unsigned int ia_valid
= iattr
->ia_valid
;
57 sd_iattr
= sd
->s_iattr
;
59 error
= inode_change_ok(inode
, iattr
);
63 error
= inode_setattr(inode
, iattr
);
68 /* setting attributes for the first time, allocate now */
69 sd_iattr
= kzalloc(sizeof(struct iattr
), GFP_KERNEL
);
72 /* assign default attributes */
73 sd_iattr
->ia_mode
= sd
->s_mode
;
76 sd_iattr
->ia_atime
= sd_iattr
->ia_mtime
= sd_iattr
->ia_ctime
= CURRENT_TIME
;
77 sd
->s_iattr
= sd_iattr
;
80 /* attributes were changed atleast once in past */
82 if (ia_valid
& ATTR_UID
)
83 sd_iattr
->ia_uid
= iattr
->ia_uid
;
84 if (ia_valid
& ATTR_GID
)
85 sd_iattr
->ia_gid
= iattr
->ia_gid
;
86 if (ia_valid
& ATTR_ATIME
)
87 sd_iattr
->ia_atime
= timespec_trunc(iattr
->ia_atime
,
88 inode
->i_sb
->s_time_gran
);
89 if (ia_valid
& ATTR_MTIME
)
90 sd_iattr
->ia_mtime
= timespec_trunc(iattr
->ia_mtime
,
91 inode
->i_sb
->s_time_gran
);
92 if (ia_valid
& ATTR_CTIME
)
93 sd_iattr
->ia_ctime
= timespec_trunc(iattr
->ia_ctime
,
94 inode
->i_sb
->s_time_gran
);
95 if (ia_valid
& ATTR_MODE
) {
96 umode_t mode
= iattr
->ia_mode
;
98 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
100 sd_iattr
->ia_mode
= sd
->s_mode
= mode
;
106 static inline void set_default_inode_attr(struct inode
* inode
, mode_t mode
)
108 inode
->i_mode
= mode
;
111 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
114 static inline void set_inode_attr(struct inode
* inode
, struct iattr
* iattr
)
116 inode
->i_mode
= iattr
->ia_mode
;
117 inode
->i_uid
= iattr
->ia_uid
;
118 inode
->i_gid
= iattr
->ia_gid
;
119 inode
->i_atime
= iattr
->ia_atime
;
120 inode
->i_mtime
= iattr
->ia_mtime
;
121 inode
->i_ctime
= iattr
->ia_ctime
;
126 * sysfs has a different i_mutex lock order behavior for i_mutex than other
127 * filesystems; sysfs i_mutex is called in many places with subsystem locks
128 * held. At the same time, many of the VFS locking rules do not apply to
129 * sysfs at all (cross directory rename for example). To untangle this mess
130 * (which gives false positives in lockdep), we're giving sysfs inodes their
131 * own class for i_mutex.
133 static struct lock_class_key sysfs_inode_imutex_key
;
135 struct inode
* sysfs_new_inode(mode_t mode
, struct sysfs_dirent
* sd
)
137 struct inode
* inode
= new_inode(sysfs_sb
);
140 inode
->i_mapping
->a_ops
= &sysfs_aops
;
141 inode
->i_mapping
->backing_dev_info
= &sysfs_backing_dev_info
;
142 inode
->i_op
= &sysfs_inode_operations
;
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
, mode
);
157 int sysfs_create(struct dentry
* dentry
, int mode
, int (*init
)(struct inode
*))
160 struct inode
* inode
= NULL
;
162 if (!dentry
->d_inode
) {
163 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
164 if ((inode
= sysfs_new_inode(mode
, sd
))) {
165 if (dentry
->d_parent
&& dentry
->d_parent
->d_inode
) {
166 struct inode
*p_inode
= dentry
->d_parent
->d_inode
;
167 p_inode
->i_mtime
= p_inode
->i_ctime
= CURRENT_TIME
;
183 d_instantiate(dentry
, inode
);
185 dget(dentry
); /* pin only directory dentry in core */
193 * Get the name for corresponding element represented by the given sysfs_dirent
195 const unsigned char * sysfs_get_name(struct sysfs_dirent
*sd
)
197 struct attribute
* attr
;
198 struct bin_attribute
* bin_attr
;
199 struct sysfs_symlink
* sl
;
201 BUG_ON(!sd
|| !sd
->s_element
);
203 switch (sd
->s_type
) {
205 /* Always have a dentry so use that */
206 return sd
->s_dentry
->d_name
.name
;
208 case SYSFS_KOBJ_ATTR
:
209 attr
= sd
->s_element
;
212 case SYSFS_KOBJ_BIN_ATTR
:
213 bin_attr
= sd
->s_element
;
214 return bin_attr
->attr
.name
;
216 case SYSFS_KOBJ_LINK
:
218 return sl
->link_name
;
223 static inline void orphan_all_buffers(struct inode
*node
)
225 struct sysfs_buffer_collection
*set
= node
->i_private
;
226 struct sysfs_buffer
*buf
;
228 mutex_lock_nested(&node
->i_mutex
, I_MUTEX_CHILD
);
229 if (node
->i_private
) {
230 list_for_each_entry(buf
, &set
->associates
, associates
) {
236 mutex_unlock(&node
->i_mutex
);
241 * Unhashes the dentry corresponding to given sysfs_dirent
242 * Called with parent inode's i_mutex held.
244 void sysfs_drop_dentry(struct sysfs_dirent
* sd
, struct dentry
* parent
)
246 struct dentry
* dentry
= sd
->s_dentry
;
250 spin_lock(&dcache_lock
);
251 spin_lock(&dentry
->d_lock
);
252 if (!(d_unhashed(dentry
) && dentry
->d_inode
)) {
253 inode
= dentry
->d_inode
;
254 spin_lock(&inode
->i_lock
);
256 spin_unlock(&inode
->i_lock
);
259 spin_unlock(&dentry
->d_lock
);
260 spin_unlock(&dcache_lock
);
261 simple_unlink(parent
->d_inode
, dentry
);
262 orphan_all_buffers(inode
);
265 spin_unlock(&dentry
->d_lock
);
266 spin_unlock(&dcache_lock
);
271 int sysfs_hash_and_remove(struct dentry
* dir
, const char * name
)
273 struct sysfs_dirent
* sd
;
274 struct sysfs_dirent
* parent_sd
;
280 if (dir
->d_inode
== NULL
)
281 /* no inode means this hasn't been made visible yet */
284 parent_sd
= dir
->d_fsdata
;
285 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
286 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
289 if (!strcmp(sysfs_get_name(sd
), name
)) {
290 list_del_init(&sd
->s_sibling
);
291 sysfs_drop_dentry(sd
, dir
);
297 mutex_unlock(&dir
->d_inode
->i_mutex
);
299 return found
? 0 : -ENOENT
;