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/xattr.h>
22 #include <linux/security.h>
25 extern struct super_block
* sysfs_sb
;
27 static const struct address_space_operations sysfs_aops
= {
28 .readpage
= simple_readpage
,
29 .write_begin
= simple_write_begin
,
30 .write_end
= simple_write_end
,
33 static struct backing_dev_info sysfs_backing_dev_info
= {
35 .ra_pages
= 0, /* No readahead */
36 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
39 static const struct inode_operations sysfs_inode_operations
={
40 .setattr
= sysfs_setattr
,
41 .setxattr
= sysfs_setxattr
,
44 int __init
sysfs_inode_init(void)
46 return bdi_init(&sysfs_backing_dev_info
);
49 struct sysfs_inode_attrs
*sysfs_init_inode_attrs(struct sysfs_dirent
*sd
)
51 struct sysfs_inode_attrs
*attrs
;
54 attrs
= kzalloc(sizeof(struct sysfs_inode_attrs
), GFP_KERNEL
);
57 iattrs
= &attrs
->ia_iattr
;
59 /* assign default attributes */
60 iattrs
->ia_mode
= sd
->s_mode
;
63 iattrs
->ia_atime
= iattrs
->ia_mtime
= iattrs
->ia_ctime
= CURRENT_TIME
;
67 int sysfs_setattr(struct dentry
* dentry
, struct iattr
* iattr
)
69 struct inode
* inode
= dentry
->d_inode
;
70 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
71 struct sysfs_inode_attrs
*sd_attrs
;
73 unsigned int ia_valid
= iattr
->ia_valid
;
79 sd_attrs
= sd
->s_iattr
;
81 error
= inode_change_ok(inode
, iattr
);
85 iattr
->ia_valid
&= ~ATTR_SIZE
; /* ignore size changes */
87 error
= inode_setattr(inode
, iattr
);
92 /* setting attributes for the first time, allocate now */
93 sd_attrs
= sysfs_init_inode_attrs(sd
);
96 sd
->s_iattr
= sd_attrs
;
98 /* attributes were changed at least once in past */
99 iattrs
= &sd_attrs
->ia_iattr
;
101 if (ia_valid
& ATTR_UID
)
102 iattrs
->ia_uid
= iattr
->ia_uid
;
103 if (ia_valid
& ATTR_GID
)
104 iattrs
->ia_gid
= iattr
->ia_gid
;
105 if (ia_valid
& ATTR_ATIME
)
106 iattrs
->ia_atime
= timespec_trunc(iattr
->ia_atime
,
107 inode
->i_sb
->s_time_gran
);
108 if (ia_valid
& ATTR_MTIME
)
109 iattrs
->ia_mtime
= timespec_trunc(iattr
->ia_mtime
,
110 inode
->i_sb
->s_time_gran
);
111 if (ia_valid
& ATTR_CTIME
)
112 iattrs
->ia_ctime
= timespec_trunc(iattr
->ia_ctime
,
113 inode
->i_sb
->s_time_gran
);
114 if (ia_valid
& ATTR_MODE
) {
115 umode_t mode
= iattr
->ia_mode
;
117 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
119 iattrs
->ia_mode
= sd
->s_mode
= mode
;
124 int sysfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
125 size_t size
, int flags
)
127 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
128 struct sysfs_inode_attrs
*iattrs
;
136 sd
->s_iattr
= sysfs_init_inode_attrs(sd
);
140 iattrs
= sd
->s_iattr
;
142 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
)) {
143 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
144 error
= security_inode_setsecurity(dentry
->d_inode
, suffix
,
148 error
= security_inode_getsecctx(dentry
->d_inode
,
149 &secdata
, &secdata_len
);
152 if (iattrs
->ia_secdata
)
153 security_release_secctx(iattrs
->ia_secdata
,
154 iattrs
->ia_secdata_len
);
155 iattrs
->ia_secdata
= secdata
;
156 iattrs
->ia_secdata_len
= secdata_len
;
164 static inline void set_default_inode_attr(struct inode
* inode
, mode_t mode
)
166 inode
->i_mode
= mode
;
167 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
170 static inline void set_inode_attr(struct inode
* inode
, struct iattr
* iattr
)
172 inode
->i_mode
= iattr
->ia_mode
;
173 inode
->i_uid
= iattr
->ia_uid
;
174 inode
->i_gid
= iattr
->ia_gid
;
175 inode
->i_atime
= iattr
->ia_atime
;
176 inode
->i_mtime
= iattr
->ia_mtime
;
177 inode
->i_ctime
= iattr
->ia_ctime
;
182 * sysfs has a different i_mutex lock order behavior for i_mutex than other
183 * filesystems; sysfs i_mutex is called in many places with subsystem locks
184 * held. At the same time, many of the VFS locking rules do not apply to
185 * sysfs at all (cross directory rename for example). To untangle this mess
186 * (which gives false positives in lockdep), we're giving sysfs inodes their
187 * own class for i_mutex.
189 static struct lock_class_key sysfs_inode_imutex_key
;
191 static int sysfs_count_nlink(struct sysfs_dirent
*sd
)
193 struct sysfs_dirent
*child
;
196 for (child
= sd
->s_dir
.children
; child
; child
= child
->s_sibling
)
197 if (sysfs_type(child
) == SYSFS_DIR
)
203 static void sysfs_init_inode(struct sysfs_dirent
*sd
, struct inode
*inode
)
205 struct bin_attribute
*bin_attr
;
206 struct sysfs_inode_attrs
*iattrs
;
208 inode
->i_private
= sysfs_get(sd
);
209 inode
->i_mapping
->a_ops
= &sysfs_aops
;
210 inode
->i_mapping
->backing_dev_info
= &sysfs_backing_dev_info
;
211 inode
->i_op
= &sysfs_inode_operations
;
212 inode
->i_ino
= sd
->s_ino
;
213 lockdep_set_class(&inode
->i_mutex
, &sysfs_inode_imutex_key
);
215 iattrs
= sd
->s_iattr
;
217 /* sysfs_dirent has non-default attributes
218 * get them for the new inode from persistent copy
221 set_inode_attr(inode
, &iattrs
->ia_iattr
);
222 if (iattrs
->ia_secdata
)
223 security_inode_notifysecctx(inode
,
225 iattrs
->ia_secdata_len
);
227 set_default_inode_attr(inode
, sd
->s_mode
);
229 /* initialize inode according to type */
230 switch (sysfs_type(sd
)) {
232 inode
->i_op
= &sysfs_dir_inode_operations
;
233 inode
->i_fop
= &sysfs_dir_operations
;
234 inode
->i_nlink
= sysfs_count_nlink(sd
);
236 case SYSFS_KOBJ_ATTR
:
237 inode
->i_size
= PAGE_SIZE
;
238 inode
->i_fop
= &sysfs_file_operations
;
240 case SYSFS_KOBJ_BIN_ATTR
:
241 bin_attr
= sd
->s_bin_attr
.bin_attr
;
242 inode
->i_size
= bin_attr
->size
;
243 inode
->i_fop
= &bin_fops
;
245 case SYSFS_KOBJ_LINK
:
246 inode
->i_op
= &sysfs_symlink_inode_operations
;
252 unlock_new_inode(inode
);
256 * sysfs_get_inode - get inode for sysfs_dirent
257 * @sd: sysfs_dirent to allocate inode for
259 * Get inode for @sd. If such inode doesn't exist, a new inode
260 * is allocated and basics are initialized. New inode is
264 * Kernel thread context (may sleep).
267 * Pointer to allocated inode on success, NULL on failure.
269 struct inode
* sysfs_get_inode(struct sysfs_dirent
*sd
)
273 inode
= iget_locked(sysfs_sb
, sd
->s_ino
);
274 if (inode
&& (inode
->i_state
& I_NEW
))
275 sysfs_init_inode(sd
, inode
);
281 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
282 * To prevent the sysfs inode numbers from being freed prematurely we take a
283 * reference to sysfs_dirent from the sysfs inode. A
284 * super_operations.delete_inode() implementation is needed to drop that
285 * reference upon inode destruction.
287 void sysfs_delete_inode(struct inode
*inode
)
289 struct sysfs_dirent
*sd
= inode
->i_private
;
291 truncate_inode_pages(&inode
->i_data
, 0);
296 int sysfs_hash_and_remove(struct sysfs_dirent
*dir_sd
, const char *name
)
298 struct sysfs_addrm_cxt acxt
;
299 struct sysfs_dirent
*sd
;
304 sysfs_addrm_start(&acxt
, dir_sd
);
306 sd
= sysfs_find_dirent(dir_sd
, name
);
308 sysfs_remove_one(&acxt
, sd
);
310 sysfs_addrm_finish(&acxt
);