ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / fs / sysfs / inode.c
bloba494413e4865a1726e81a6caa4e8087f0c7be048
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 extern struct super_block * sysfs_sb;
29 static const struct address_space_operations sysfs_aops = {
30 .readpage = simple_readpage,
31 .write_begin = simple_write_begin,
32 .write_end = simple_write_end,
35 static struct backing_dev_info sysfs_backing_dev_info = {
36 .name = "sysfs",
37 .ra_pages = 0, /* No readahead */
38 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
41 static const struct inode_operations sysfs_inode_operations ={
42 .permission = sysfs_permission,
43 .setattr = sysfs_setattr,
44 .getattr = sysfs_getattr,
45 .setxattr = sysfs_setxattr,
48 int __init sysfs_inode_init(void)
50 return bdi_init(&sysfs_backing_dev_info);
53 static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
55 struct sysfs_inode_attrs *attrs;
56 struct iattr *iattrs;
58 attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
59 if (!attrs)
60 return NULL;
61 iattrs = &attrs->ia_iattr;
63 /* assign default attributes */
64 iattrs->ia_mode = sd->s_mode;
65 iattrs->ia_uid = 0;
66 iattrs->ia_gid = 0;
67 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
69 return attrs;
72 int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
74 struct sysfs_inode_attrs *sd_attrs;
75 struct iattr *iattrs;
76 unsigned int ia_valid = iattr->ia_valid;
78 sd_attrs = sd->s_iattr;
80 if (!sd_attrs) {
81 /* setting attributes for the first time, allocate now */
82 sd_attrs = sysfs_init_inode_attrs(sd);
83 if (!sd_attrs)
84 return -ENOMEM;
85 sd->s_iattr = sd_attrs;
87 /* attributes were changed at least once in past */
88 iattrs = &sd_attrs->ia_iattr;
90 if (ia_valid & ATTR_UID)
91 iattrs->ia_uid = iattr->ia_uid;
92 if (ia_valid & ATTR_GID)
93 iattrs->ia_gid = iattr->ia_gid;
94 if (ia_valid & ATTR_ATIME)
95 iattrs->ia_atime = iattr->ia_atime;
96 if (ia_valid & ATTR_MTIME)
97 iattrs->ia_mtime = iattr->ia_mtime;
98 if (ia_valid & ATTR_CTIME)
99 iattrs->ia_ctime = iattr->ia_ctime;
100 if (ia_valid & ATTR_MODE) {
101 umode_t mode = iattr->ia_mode;
102 iattrs->ia_mode = sd->s_mode = mode;
104 return 0;
107 int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
109 struct inode *inode = dentry->d_inode;
110 struct sysfs_dirent *sd = dentry->d_fsdata;
111 int error;
113 if (!sd)
114 return -EINVAL;
116 mutex_lock(&sysfs_mutex);
117 error = inode_change_ok(inode, iattr);
118 if (error)
119 goto out;
121 error = sysfs_sd_setattr(sd, iattr);
122 if (error)
123 goto out;
125 /* this ignores size changes */
126 setattr_copy(inode, iattr);
128 out:
129 mutex_unlock(&sysfs_mutex);
130 return error;
133 static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len)
135 struct sysfs_inode_attrs *iattrs;
136 void *old_secdata;
137 size_t old_secdata_len;
139 if (!sd->s_iattr) {
140 sd->s_iattr = sysfs_init_inode_attrs(sd);
141 if (!sd->s_iattr)
142 return -ENOMEM;
145 iattrs = sd->s_iattr;
146 old_secdata = iattrs->ia_secdata;
147 old_secdata_len = iattrs->ia_secdata_len;
149 iattrs->ia_secdata = *secdata;
150 iattrs->ia_secdata_len = *secdata_len;
152 *secdata = old_secdata;
153 *secdata_len = old_secdata_len;
154 return 0;
157 int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
158 size_t size, int flags)
160 struct sysfs_dirent *sd = dentry->d_fsdata;
161 void *secdata;
162 int error;
163 u32 secdata_len = 0;
165 if (!sd)
166 return -EINVAL;
168 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
169 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
170 error = security_inode_setsecurity(dentry->d_inode, suffix,
171 value, size, flags);
172 if (error)
173 goto out;
174 error = security_inode_getsecctx(dentry->d_inode,
175 &secdata, &secdata_len);
176 if (error)
177 goto out;
179 mutex_lock(&sysfs_mutex);
180 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
181 mutex_unlock(&sysfs_mutex);
183 if (secdata)
184 security_release_secctx(secdata, secdata_len);
185 } else
186 return -EINVAL;
187 out:
188 return error;
191 static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
193 inode->i_mode = mode;
194 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
197 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
199 inode->i_uid = iattr->ia_uid;
200 inode->i_gid = iattr->ia_gid;
201 inode->i_atime = iattr->ia_atime;
202 inode->i_mtime = iattr->ia_mtime;
203 inode->i_ctime = iattr->ia_ctime;
206 static int sysfs_count_nlink(struct sysfs_dirent *sd)
208 struct sysfs_dirent *child;
209 int nr = 0;
211 for (child = sd->s_dir.children; child; child = child->s_sibling)
212 if (sysfs_type(child) == SYSFS_DIR)
213 nr++;
215 return nr + 2;
218 static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
220 struct sysfs_inode_attrs *iattrs = sd->s_iattr;
222 inode->i_mode = sd->s_mode;
223 if (iattrs) {
224 /* sysfs_dirent has non-default attributes
225 * get them from persistent copy in sysfs_dirent
227 set_inode_attr(inode, &iattrs->ia_iattr);
228 security_inode_notifysecctx(inode,
229 iattrs->ia_secdata,
230 iattrs->ia_secdata_len);
233 if (sysfs_type(sd) == SYSFS_DIR)
234 inode->i_nlink = sysfs_count_nlink(sd);
237 int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
239 struct sysfs_dirent *sd = dentry->d_fsdata;
240 struct inode *inode = dentry->d_inode;
242 mutex_lock(&sysfs_mutex);
243 sysfs_refresh_inode(sd, inode);
244 mutex_unlock(&sysfs_mutex);
246 generic_fillattr(inode, stat);
247 return 0;
250 static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
252 struct bin_attribute *bin_attr;
254 inode->i_private = sysfs_get(sd);
255 inode->i_mapping->a_ops = &sysfs_aops;
256 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
257 inode->i_op = &sysfs_inode_operations;
259 set_default_inode_attr(inode, sd->s_mode);
260 sysfs_refresh_inode(sd, inode);
262 /* initialize inode according to type */
263 switch (sysfs_type(sd)) {
264 case SYSFS_DIR:
265 inode->i_op = &sysfs_dir_inode_operations;
266 inode->i_fop = &sysfs_dir_operations;
267 break;
268 case SYSFS_KOBJ_ATTR:
269 inode->i_size = PAGE_SIZE;
270 inode->i_fop = &sysfs_file_operations;
271 break;
272 case SYSFS_KOBJ_BIN_ATTR:
273 bin_attr = sd->s_bin_attr.bin_attr;
274 inode->i_size = bin_attr->size;
275 inode->i_fop = &bin_fops;
276 break;
277 case SYSFS_KOBJ_LINK:
278 inode->i_op = &sysfs_symlink_inode_operations;
279 break;
280 default:
281 BUG();
284 unlock_new_inode(inode);
288 * sysfs_get_inode - get inode for sysfs_dirent
289 * @sb: super block
290 * @sd: sysfs_dirent to allocate inode for
292 * Get inode for @sd. If such inode doesn't exist, a new inode
293 * is allocated and basics are initialized. New inode is
294 * returned locked.
296 * LOCKING:
297 * Kernel thread context (may sleep).
299 * RETURNS:
300 * Pointer to allocated inode on success, NULL on failure.
302 struct inode * sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
304 struct inode *inode;
306 inode = iget_locked(sb, sd->s_ino);
307 if (inode && (inode->i_state & I_NEW))
308 sysfs_init_inode(sd, inode);
310 return inode;
314 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
315 * To prevent the sysfs inode numbers from being freed prematurely we take a
316 * reference to sysfs_dirent from the sysfs inode. A
317 * super_operations.evict_inode() implementation is needed to drop that
318 * reference upon inode destruction.
320 void sysfs_evict_inode(struct inode *inode)
322 struct sysfs_dirent *sd = inode->i_private;
324 truncate_inode_pages(&inode->i_data, 0);
325 end_writeback(inode);
326 sysfs_put(sd);
329 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, const char *name)
331 struct sysfs_addrm_cxt acxt;
332 struct sysfs_dirent *sd;
334 if (!dir_sd)
335 return -ENOENT;
337 sysfs_addrm_start(&acxt, dir_sd);
339 sd = sysfs_find_dirent(dir_sd, ns, name);
340 if (sd && (sd->s_ns != ns))
341 sd = NULL;
342 if (sd)
343 sysfs_remove_one(&acxt, sd);
345 sysfs_addrm_finish(&acxt);
347 if (sd)
348 return 0;
349 else
350 return -ENOENT;
353 int sysfs_permission(struct inode *inode, int mask, unsigned int flags)
355 struct sysfs_dirent *sd;
357 if (flags & IPERM_FLAG_RCU)
358 return -ECHILD;
360 sd = inode->i_private;
362 mutex_lock(&sysfs_mutex);
363 sysfs_refresh_inode(sd, inode);
364 mutex_unlock(&sysfs_mutex);
366 return generic_permission(inode, mask, flags, NULL);