2 * fs/sysfs/inode.c - basic sysfs inode and dentry operations
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
15 #include <linux/pagemap.h>
16 #include <linux/namei.h>
17 #include <linux/backing-dev.h>
18 #include <linux/capability.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/xattr.h>
23 #include <linux/security.h>
26 extern struct super_block
* sysfs_sb
;
28 static const struct address_space_operations sysfs_aops
= {
29 .readpage
= simple_readpage
,
30 .write_begin
= simple_write_begin
,
31 .write_end
= simple_write_end
,
34 static struct backing_dev_info sysfs_backing_dev_info
= {
36 .ra_pages
= 0, /* No readahead */
37 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
40 static const struct inode_operations sysfs_inode_operations
={
41 .permission
= sysfs_permission
,
42 .setattr
= sysfs_setattr
,
43 .getattr
= sysfs_getattr
,
44 .setxattr
= sysfs_setxattr
,
47 int __init
sysfs_inode_init(void)
49 return bdi_init(&sysfs_backing_dev_info
);
52 static struct sysfs_inode_attrs
*sysfs_init_inode_attrs(struct sysfs_dirent
*sd
)
54 struct sysfs_inode_attrs
*attrs
;
57 attrs
= kzalloc(sizeof(struct sysfs_inode_attrs
), GFP_KERNEL
);
60 iattrs
= &attrs
->ia_iattr
;
62 /* assign default attributes */
63 iattrs
->ia_mode
= sd
->s_mode
;
66 iattrs
->ia_atime
= iattrs
->ia_mtime
= iattrs
->ia_ctime
= CURRENT_TIME
;
71 int sysfs_sd_setattr(struct sysfs_dirent
*sd
, struct iattr
* iattr
)
73 struct sysfs_inode_attrs
*sd_attrs
;
75 unsigned int ia_valid
= iattr
->ia_valid
;
77 sd_attrs
= sd
->s_iattr
;
80 /* setting attributes for the first time, allocate now */
81 sd_attrs
= sysfs_init_inode_attrs(sd
);
84 sd
->s_iattr
= sd_attrs
;
86 /* attributes were changed at least once in past */
87 iattrs
= &sd_attrs
->ia_iattr
;
89 if (ia_valid
& ATTR_UID
)
90 iattrs
->ia_uid
= iattr
->ia_uid
;
91 if (ia_valid
& ATTR_GID
)
92 iattrs
->ia_gid
= iattr
->ia_gid
;
93 if (ia_valid
& ATTR_ATIME
)
94 iattrs
->ia_atime
= iattr
->ia_atime
;
95 if (ia_valid
& ATTR_MTIME
)
96 iattrs
->ia_mtime
= iattr
->ia_mtime
;
97 if (ia_valid
& ATTR_CTIME
)
98 iattrs
->ia_ctime
= iattr
->ia_ctime
;
99 if (ia_valid
& ATTR_MODE
) {
100 umode_t mode
= iattr
->ia_mode
;
101 iattrs
->ia_mode
= sd
->s_mode
= mode
;
106 int sysfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
108 struct inode
*inode
= dentry
->d_inode
;
109 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
115 mutex_lock(&sysfs_mutex
);
116 error
= inode_change_ok(inode
, iattr
);
120 error
= sysfs_sd_setattr(sd
, iattr
);
124 /* this ignores size changes */
125 generic_setattr(inode
, iattr
);
128 mutex_unlock(&sysfs_mutex
);
132 static int sysfs_sd_setsecdata(struct sysfs_dirent
*sd
, void **secdata
, u32
*secdata_len
)
134 struct sysfs_inode_attrs
*iattrs
;
136 size_t old_secdata_len
;
138 iattrs
= sd
->s_iattr
;
140 iattrs
= sysfs_init_inode_attrs(sd
);
144 old_secdata
= iattrs
->ia_secdata
;
145 old_secdata_len
= iattrs
->ia_secdata_len
;
147 iattrs
->ia_secdata
= *secdata
;
148 iattrs
->ia_secdata_len
= *secdata_len
;
150 *secdata
= old_secdata
;
151 *secdata_len
= old_secdata_len
;
155 int sysfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
156 size_t size
, int flags
)
158 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
166 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
)) {
167 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
168 error
= security_inode_setsecurity(dentry
->d_inode
, suffix
,
172 error
= security_inode_getsecctx(dentry
->d_inode
,
173 &secdata
, &secdata_len
);
177 mutex_lock(&sysfs_mutex
);
178 error
= sysfs_sd_setsecdata(sd
, &secdata
, &secdata_len
);
179 mutex_unlock(&sysfs_mutex
);
182 security_release_secctx(secdata
, secdata_len
);
189 static inline void set_default_inode_attr(struct inode
* inode
, mode_t mode
)
191 inode
->i_mode
= mode
;
192 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
195 static inline void set_inode_attr(struct inode
* inode
, struct iattr
* iattr
)
197 inode
->i_uid
= iattr
->ia_uid
;
198 inode
->i_gid
= iattr
->ia_gid
;
199 inode
->i_atime
= iattr
->ia_atime
;
200 inode
->i_mtime
= iattr
->ia_mtime
;
201 inode
->i_ctime
= iattr
->ia_ctime
;
204 static int sysfs_count_nlink(struct sysfs_dirent
*sd
)
206 struct sysfs_dirent
*child
;
209 for (child
= sd
->s_dir
.children
; child
; child
= child
->s_sibling
)
210 if (sysfs_type(child
) == SYSFS_DIR
)
216 static void sysfs_refresh_inode(struct sysfs_dirent
*sd
, struct inode
*inode
)
218 struct sysfs_inode_attrs
*iattrs
= sd
->s_iattr
;
220 inode
->i_mode
= sd
->s_mode
;
222 /* sysfs_dirent has non-default attributes
223 * get them from persistent copy in sysfs_dirent
225 set_inode_attr(inode
, &iattrs
->ia_iattr
);
226 security_inode_notifysecctx(inode
,
228 iattrs
->ia_secdata_len
);
231 if (sysfs_type(sd
) == SYSFS_DIR
)
232 inode
->i_nlink
= sysfs_count_nlink(sd
);
235 int sysfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
237 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
238 struct inode
*inode
= dentry
->d_inode
;
240 mutex_lock(&sysfs_mutex
);
241 sysfs_refresh_inode(sd
, inode
);
242 mutex_unlock(&sysfs_mutex
);
244 generic_fillattr(inode
, stat
);
248 static void sysfs_init_inode(struct sysfs_dirent
*sd
, struct inode
*inode
)
250 struct bin_attribute
*bin_attr
;
252 inode
->i_private
= sysfs_get(sd
);
253 inode
->i_mapping
->a_ops
= &sysfs_aops
;
254 inode
->i_mapping
->backing_dev_info
= &sysfs_backing_dev_info
;
255 inode
->i_op
= &sysfs_inode_operations
;
257 set_default_inode_attr(inode
, sd
->s_mode
);
258 sysfs_refresh_inode(sd
, inode
);
260 /* initialize inode according to type */
261 switch (sysfs_type(sd
)) {
263 inode
->i_op
= &sysfs_dir_inode_operations
;
264 inode
->i_fop
= &sysfs_dir_operations
;
266 case SYSFS_KOBJ_ATTR
:
267 inode
->i_size
= PAGE_SIZE
;
268 inode
->i_fop
= &sysfs_file_operations
;
270 case SYSFS_KOBJ_BIN_ATTR
:
271 bin_attr
= sd
->s_bin_attr
.bin_attr
;
272 inode
->i_size
= bin_attr
->size
;
273 inode
->i_fop
= &bin_fops
;
275 case SYSFS_KOBJ_LINK
:
276 inode
->i_op
= &sysfs_symlink_inode_operations
;
282 unlock_new_inode(inode
);
286 * sysfs_get_inode - get inode for sysfs_dirent
288 * @sd: sysfs_dirent to allocate inode for
290 * Get inode for @sd. If such inode doesn't exist, a new inode
291 * is allocated and basics are initialized. New inode is
295 * Kernel thread context (may sleep).
298 * Pointer to allocated inode on success, NULL on failure.
300 struct inode
* sysfs_get_inode(struct super_block
*sb
, struct sysfs_dirent
*sd
)
304 inode
= iget_locked(sb
, sd
->s_ino
);
305 if (inode
&& (inode
->i_state
& I_NEW
))
306 sysfs_init_inode(sd
, inode
);
312 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
313 * To prevent the sysfs inode numbers from being freed prematurely we take a
314 * reference to sysfs_dirent from the sysfs inode. A
315 * super_operations.delete_inode() implementation is needed to drop that
316 * reference upon inode destruction.
318 void sysfs_delete_inode(struct inode
*inode
)
320 struct sysfs_dirent
*sd
= inode
->i_private
;
322 truncate_inode_pages(&inode
->i_data
, 0);
327 int sysfs_hash_and_remove(struct sysfs_dirent
*dir_sd
, const void *ns
, const char *name
)
329 struct sysfs_addrm_cxt acxt
;
330 struct sysfs_dirent
*sd
;
335 sysfs_addrm_start(&acxt
, dir_sd
);
337 sd
= sysfs_find_dirent(dir_sd
, ns
, name
);
338 if (sd
&& (sd
->s_ns
!= ns
))
341 sysfs_remove_one(&acxt
, sd
);
343 sysfs_addrm_finish(&acxt
);
351 int sysfs_permission(struct inode
*inode
, int mask
)
353 struct sysfs_dirent
*sd
= inode
->i_private
;
355 mutex_lock(&sysfs_mutex
);
356 sysfs_refresh_inode(sd
, inode
);
357 mutex_unlock(&sysfs_mutex
);
359 return generic_permission(inode
, mask
, NULL
);