perf record: Fix segfault with --no-mmap-pages
[linux/fpc-iii.git] / fs / sysfs / inode.c
blob963f910c8034f84d414e625c092d45d2ca564123
1 /*
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.
13 #undef DEBUG
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/sysfs.h>
23 #include <linux/xattr.h>
24 #include <linux/security.h>
25 #include "sysfs.h"
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 = {
34 .name = "sysfs",
35 .ra_pages = 0, /* No readahead */
36 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
39 static const struct inode_operations sysfs_inode_operations = {
40 .permission = sysfs_permission,
41 .setattr = sysfs_setattr,
42 .getattr = sysfs_getattr,
43 .setxattr = sysfs_setxattr,
46 int __init sysfs_inode_init(void)
48 return bdi_init(&sysfs_backing_dev_info);
51 static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
53 struct sysfs_inode_attrs *attrs;
54 struct iattr *iattrs;
56 attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
57 if (!attrs)
58 return NULL;
59 iattrs = &attrs->ia_iattr;
61 /* assign default attributes */
62 iattrs->ia_mode = sd->s_mode;
63 iattrs->ia_uid = GLOBAL_ROOT_UID;
64 iattrs->ia_gid = GLOBAL_ROOT_GID;
65 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
67 return attrs;
70 int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr)
72 struct sysfs_inode_attrs *sd_attrs;
73 struct iattr *iattrs;
74 unsigned int ia_valid = iattr->ia_valid;
76 sd_attrs = sd->s_iattr;
78 if (!sd_attrs) {
79 /* setting attributes for the first time, allocate now */
80 sd_attrs = sysfs_init_inode_attrs(sd);
81 if (!sd_attrs)
82 return -ENOMEM;
83 sd->s_iattr = sd_attrs;
85 /* attributes were changed at least once in past */
86 iattrs = &sd_attrs->ia_iattr;
88 if (ia_valid & ATTR_UID)
89 iattrs->ia_uid = iattr->ia_uid;
90 if (ia_valid & ATTR_GID)
91 iattrs->ia_gid = iattr->ia_gid;
92 if (ia_valid & ATTR_ATIME)
93 iattrs->ia_atime = iattr->ia_atime;
94 if (ia_valid & ATTR_MTIME)
95 iattrs->ia_mtime = iattr->ia_mtime;
96 if (ia_valid & ATTR_CTIME)
97 iattrs->ia_ctime = iattr->ia_ctime;
98 if (ia_valid & ATTR_MODE) {
99 umode_t mode = iattr->ia_mode;
100 iattrs->ia_mode = sd->s_mode = mode;
102 return 0;
105 int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
107 struct inode *inode = dentry->d_inode;
108 struct sysfs_dirent *sd = dentry->d_fsdata;
109 int error;
111 if (!sd)
112 return -EINVAL;
114 mutex_lock(&sysfs_mutex);
115 error = inode_change_ok(inode, iattr);
116 if (error)
117 goto out;
119 error = sysfs_sd_setattr(sd, iattr);
120 if (error)
121 goto out;
123 /* this ignores size changes */
124 setattr_copy(inode, iattr);
126 out:
127 mutex_unlock(&sysfs_mutex);
128 return error;
131 static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata,
132 u32 *secdata_len)
134 struct sysfs_inode_attrs *iattrs;
135 void *old_secdata;
136 size_t old_secdata_len;
138 if (!sd->s_iattr) {
139 sd->s_iattr = sysfs_init_inode_attrs(sd);
140 if (!sd->s_iattr)
141 return -ENOMEM;
144 iattrs = sd->s_iattr;
145 old_secdata = iattrs->ia_secdata;
146 old_secdata_len = iattrs->ia_secdata_len;
148 iattrs->ia_secdata = *secdata;
149 iattrs->ia_secdata_len = *secdata_len;
151 *secdata = old_secdata;
152 *secdata_len = old_secdata_len;
153 return 0;
156 int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
157 size_t size, int flags)
159 struct sysfs_dirent *sd = dentry->d_fsdata;
160 void *secdata;
161 int error;
162 u32 secdata_len = 0;
164 if (!sd)
165 return -EINVAL;
167 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
168 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
169 error = security_inode_setsecurity(dentry->d_inode, suffix,
170 value, size, flags);
171 if (error)
172 goto out;
173 error = security_inode_getsecctx(dentry->d_inode,
174 &secdata, &secdata_len);
175 if (error)
176 goto out;
178 mutex_lock(&sysfs_mutex);
179 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
180 mutex_unlock(&sysfs_mutex);
182 if (secdata)
183 security_release_secctx(secdata, secdata_len);
184 } else
185 return -EINVAL;
186 out:
187 return error;
190 static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
192 inode->i_mode = mode;
193 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
196 static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
198 inode->i_uid = iattr->ia_uid;
199 inode->i_gid = iattr->ia_gid;
200 inode->i_atime = iattr->ia_atime;
201 inode->i_mtime = iattr->ia_mtime;
202 inode->i_ctime = iattr->ia_ctime;
205 static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
207 struct sysfs_inode_attrs *iattrs = sd->s_iattr;
209 inode->i_mode = sd->s_mode;
210 if (iattrs) {
211 /* sysfs_dirent has non-default attributes
212 * get them from persistent copy in sysfs_dirent
214 set_inode_attr(inode, &iattrs->ia_iattr);
215 security_inode_notifysecctx(inode,
216 iattrs->ia_secdata,
217 iattrs->ia_secdata_len);
220 if (sysfs_type(sd) == SYSFS_DIR)
221 set_nlink(inode, sd->s_dir.subdirs + 2);
224 int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
225 struct kstat *stat)
227 struct sysfs_dirent *sd = dentry->d_fsdata;
228 struct inode *inode = dentry->d_inode;
230 mutex_lock(&sysfs_mutex);
231 sysfs_refresh_inode(sd, inode);
232 mutex_unlock(&sysfs_mutex);
234 generic_fillattr(inode, stat);
235 return 0;
238 static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
240 struct bin_attribute *bin_attr;
242 inode->i_private = sysfs_get(sd);
243 inode->i_mapping->a_ops = &sysfs_aops;
244 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
245 inode->i_op = &sysfs_inode_operations;
247 set_default_inode_attr(inode, sd->s_mode);
248 sysfs_refresh_inode(sd, inode);
250 /* initialize inode according to type */
251 switch (sysfs_type(sd)) {
252 case SYSFS_DIR:
253 inode->i_op = &sysfs_dir_inode_operations;
254 inode->i_fop = &sysfs_dir_operations;
255 break;
256 case SYSFS_KOBJ_ATTR:
257 inode->i_size = PAGE_SIZE;
258 inode->i_fop = &sysfs_file_operations;
259 break;
260 case SYSFS_KOBJ_BIN_ATTR:
261 bin_attr = sd->s_bin_attr.bin_attr;
262 inode->i_size = bin_attr->size;
263 inode->i_fop = &bin_fops;
264 break;
265 case SYSFS_KOBJ_LINK:
266 inode->i_op = &sysfs_symlink_inode_operations;
267 break;
268 default:
269 BUG();
272 unlock_new_inode(inode);
276 * sysfs_get_inode - get inode for sysfs_dirent
277 * @sb: super block
278 * @sd: sysfs_dirent to allocate inode for
280 * Get inode for @sd. If such inode doesn't exist, a new inode
281 * is allocated and basics are initialized. New inode is
282 * returned locked.
284 * LOCKING:
285 * Kernel thread context (may sleep).
287 * RETURNS:
288 * Pointer to allocated inode on success, NULL on failure.
290 struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
292 struct inode *inode;
294 inode = iget_locked(sb, sd->s_ino);
295 if (inode && (inode->i_state & I_NEW))
296 sysfs_init_inode(sd, inode);
298 return inode;
302 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
303 * To prevent the sysfs inode numbers from being freed prematurely we take a
304 * reference to sysfs_dirent from the sysfs inode. A
305 * super_operations.evict_inode() implementation is needed to drop that
306 * reference upon inode destruction.
308 void sysfs_evict_inode(struct inode *inode)
310 struct sysfs_dirent *sd = inode->i_private;
312 truncate_inode_pages(&inode->i_data, 0);
313 clear_inode(inode);
314 sysfs_put(sd);
317 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns,
318 const char *name)
320 struct sysfs_addrm_cxt acxt;
321 struct sysfs_dirent *sd;
323 if (!dir_sd) {
324 WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
325 name);
326 return -ENOENT;
329 sysfs_addrm_start(&acxt, dir_sd);
331 sd = sysfs_find_dirent(dir_sd, ns, name);
332 if (sd)
333 sysfs_remove_one(&acxt, sd);
335 sysfs_addrm_finish(&acxt);
337 if (sd)
338 return 0;
339 else
340 return -ENOENT;
343 int sysfs_permission(struct inode *inode, int mask)
345 struct sysfs_dirent *sd;
347 if (mask & MAY_NOT_BLOCK)
348 return -ECHILD;
350 sd = inode->i_private;
352 mutex_lock(&sysfs_mutex);
353 sysfs_refresh_inode(sd, inode);
354 mutex_unlock(&sysfs_mutex);
356 return generic_permission(inode, mask);