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>
17 extern struct super_block
* sysfs_sb
;
19 static struct address_space_operations sysfs_aops
= {
20 .readpage
= simple_readpage
,
21 .prepare_write
= simple_prepare_write
,
22 .commit_write
= simple_commit_write
25 static struct backing_dev_info sysfs_backing_dev_info
= {
26 .ra_pages
= 0, /* No readahead */
27 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
30 static struct inode_operations sysfs_inode_operations
={
31 .setattr
= sysfs_setattr
,
34 int sysfs_setattr(struct dentry
* dentry
, struct iattr
* iattr
)
36 struct inode
* inode
= dentry
->d_inode
;
37 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
38 struct iattr
* sd_iattr
;
39 unsigned int ia_valid
= iattr
->ia_valid
;
45 sd_iattr
= sd
->s_iattr
;
47 error
= inode_change_ok(inode
, iattr
);
51 error
= inode_setattr(inode
, iattr
);
56 /* setting attributes for the first time, allocate now */
57 sd_iattr
= kmalloc(sizeof(struct iattr
), GFP_KERNEL
);
60 /* assign default attributes */
61 memset(sd_iattr
, 0, sizeof(struct iattr
));
62 sd_iattr
->ia_mode
= sd
->s_mode
;
65 sd_iattr
->ia_atime
= sd_iattr
->ia_mtime
= sd_iattr
->ia_ctime
= CURRENT_TIME
;
66 sd
->s_iattr
= sd_iattr
;
69 /* attributes were changed atleast once in past */
71 if (ia_valid
& ATTR_UID
)
72 sd_iattr
->ia_uid
= iattr
->ia_uid
;
73 if (ia_valid
& ATTR_GID
)
74 sd_iattr
->ia_gid
= iattr
->ia_gid
;
75 if (ia_valid
& ATTR_ATIME
)
76 sd_iattr
->ia_atime
= timespec_trunc(iattr
->ia_atime
,
77 inode
->i_sb
->s_time_gran
);
78 if (ia_valid
& ATTR_MTIME
)
79 sd_iattr
->ia_mtime
= timespec_trunc(iattr
->ia_mtime
,
80 inode
->i_sb
->s_time_gran
);
81 if (ia_valid
& ATTR_CTIME
)
82 sd_iattr
->ia_ctime
= timespec_trunc(iattr
->ia_ctime
,
83 inode
->i_sb
->s_time_gran
);
84 if (ia_valid
& ATTR_MODE
) {
85 umode_t mode
= iattr
->ia_mode
;
87 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
89 sd_iattr
->ia_mode
= sd
->s_mode
= mode
;
95 static inline void set_default_inode_attr(struct inode
* inode
, mode_t mode
)
100 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
103 static inline void set_inode_attr(struct inode
* inode
, struct iattr
* iattr
)
105 inode
->i_mode
= iattr
->ia_mode
;
106 inode
->i_uid
= iattr
->ia_uid
;
107 inode
->i_gid
= iattr
->ia_gid
;
108 inode
->i_atime
= iattr
->ia_atime
;
109 inode
->i_mtime
= iattr
->ia_mtime
;
110 inode
->i_ctime
= iattr
->ia_ctime
;
113 struct inode
* sysfs_new_inode(mode_t mode
, struct sysfs_dirent
* sd
)
115 struct inode
* inode
= new_inode(sysfs_sb
);
117 inode
->i_blksize
= PAGE_CACHE_SIZE
;
119 inode
->i_mapping
->a_ops
= &sysfs_aops
;
120 inode
->i_mapping
->backing_dev_info
= &sysfs_backing_dev_info
;
121 inode
->i_op
= &sysfs_inode_operations
;
124 /* sysfs_dirent has non-default attributes
125 * get them for the new inode from persistent copy
128 set_inode_attr(inode
, sd
->s_iattr
);
130 set_default_inode_attr(inode
, mode
);
135 int sysfs_create(struct dentry
* dentry
, int mode
, int (*init
)(struct inode
*))
138 struct inode
* inode
= NULL
;
140 if (!dentry
->d_inode
) {
141 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
142 if ((inode
= sysfs_new_inode(mode
, sd
))) {
143 if (dentry
->d_parent
&& dentry
->d_parent
->d_inode
) {
144 struct inode
*p_inode
= dentry
->d_parent
->d_inode
;
145 p_inode
->i_mtime
= p_inode
->i_ctime
= CURRENT_TIME
;
161 d_instantiate(dentry
, inode
);
163 dget(dentry
); /* pin only directory dentry in core */
171 * Get the name for corresponding element represented by the given sysfs_dirent
173 const unsigned char * sysfs_get_name(struct sysfs_dirent
*sd
)
175 struct attribute
* attr
;
176 struct bin_attribute
* bin_attr
;
177 struct sysfs_symlink
* sl
;
179 if (!sd
|| !sd
->s_element
)
182 switch (sd
->s_type
) {
184 /* Always have a dentry so use that */
185 return sd
->s_dentry
->d_name
.name
;
187 case SYSFS_KOBJ_ATTR
:
188 attr
= sd
->s_element
;
191 case SYSFS_KOBJ_BIN_ATTR
:
192 bin_attr
= sd
->s_element
;
193 return bin_attr
->attr
.name
;
195 case SYSFS_KOBJ_LINK
:
197 return sl
->link_name
;
204 * Unhashes the dentry corresponding to given sysfs_dirent
205 * Called with parent inode's i_mutex held.
207 void sysfs_drop_dentry(struct sysfs_dirent
* sd
, struct dentry
* parent
)
209 struct dentry
* dentry
= sd
->s_dentry
;
212 spin_lock(&dcache_lock
);
213 spin_lock(&dentry
->d_lock
);
214 if (!(d_unhashed(dentry
) && dentry
->d_inode
)) {
217 spin_unlock(&dentry
->d_lock
);
218 spin_unlock(&dcache_lock
);
219 simple_unlink(parent
->d_inode
, dentry
);
221 spin_unlock(&dentry
->d_lock
);
222 spin_unlock(&dcache_lock
);
227 void sysfs_hash_and_remove(struct dentry
* dir
, const char * name
)
229 struct sysfs_dirent
* sd
;
230 struct sysfs_dirent
* parent_sd
= dir
->d_fsdata
;
232 if (dir
->d_inode
== NULL
)
233 /* no inode means this hasn't been made visible yet */
236 mutex_lock(&dir
->d_inode
->i_mutex
);
237 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
240 if (!strcmp(sysfs_get_name(sd
), name
)) {
241 list_del_init(&sd
->s_sibling
);
242 sysfs_drop_dentry(sd
, dir
);
247 mutex_unlock(&dir
->d_inode
->i_mutex
);