blob: cffb1fd8ba33fdb66aab0c076cf24e3f4f2e6387 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/inode.c - basic sysfs inode and dentry operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heo6d66f5c2007-09-20 17:31:38 +09004 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
11 */
12
13#undef DEBUG
14
15#include <linux/pagemap.h>
16#include <linux/namei.h>
17#include <linux/backing-dev.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018#include <linux/capability.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070019#include <linux/errno.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
David P. Quigleyddd29ec2009-09-09 14:25:37 -040022#include <linux/xattr.h>
23#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "sysfs.h"
25
26extern struct super_block * sysfs_sb;
27
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070028static const struct address_space_operations sysfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .readpage = simple_readpage,
Nick Piggin800d15a2007-10-16 01:25:03 -070030 .write_begin = simple_write_begin,
31 .write_end = simple_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34static struct backing_dev_info sysfs_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +020035 .name = "sysfs",
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .ra_pages = 0, /* No readahead */
Miklos Szeredie4ad08f2008-04-30 00:54:37 -070037 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080040static const struct inode_operations sysfs_inode_operations ={
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -080041 .permission = sysfs_permission,
Maneesh Soni988d1862005-05-31 10:39:14 +053042 .setattr = sysfs_setattr,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -080043 .getattr = sysfs_getattr,
David P. Quigleyddd29ec2009-09-09 14:25:37 -040044 .setxattr = sysfs_setxattr,
Maneesh Soni988d1862005-05-31 10:39:14 +053045};
46
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070047int __init sysfs_inode_init(void)
48{
49 return bdi_init(&sysfs_backing_dev_info);
50}
51
Stefan Richterf38506c2009-10-14 20:47:32 +020052static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
David P. Quigleyddd29ec2009-09-09 14:25:37 -040053{
54 struct sysfs_inode_attrs *attrs;
55 struct iattr *iattrs;
56
57 attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
58 if (!attrs)
59 return NULL;
60 iattrs = &attrs->ia_iattr;
61
62 /* assign default attributes */
63 iattrs->ia_mode = sd->s_mode;
64 iattrs->ia_uid = 0;
65 iattrs->ia_gid = 0;
66 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
67
68 return attrs;
69}
Stefan Richterf38506c2009-10-14 20:47:32 +020070
Eric W. Biederman35df63c2009-11-20 16:08:50 -080071int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
Maneesh Soni988d1862005-05-31 10:39:14 +053072{
David P. Quigleyddd29ec2009-09-09 14:25:37 -040073 struct sysfs_inode_attrs *sd_attrs;
74 struct iattr *iattrs;
Maneesh Soni988d1862005-05-31 10:39:14 +053075 unsigned int ia_valid = iattr->ia_valid;
Maneesh Soni988d1862005-05-31 10:39:14 +053076
David P. Quigleyddd29ec2009-09-09 14:25:37 -040077 sd_attrs = sd->s_iattr;
Maneesh Soni988d1862005-05-31 10:39:14 +053078
David P. Quigleyddd29ec2009-09-09 14:25:37 -040079 if (!sd_attrs) {
Maneesh Soni988d1862005-05-31 10:39:14 +053080 /* setting attributes for the first time, allocate now */
David P. Quigleyddd29ec2009-09-09 14:25:37 -040081 sd_attrs = sysfs_init_inode_attrs(sd);
82 if (!sd_attrs)
Maneesh Soni988d1862005-05-31 10:39:14 +053083 return -ENOMEM;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040084 sd->s_iattr = sd_attrs;
Eric W. Biederman7c0ff872010-02-03 23:13:24 -080085 }
86 /* attributes were changed at least once in past */
87 iattrs = &sd_attrs->ia_iattr;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040088
Eric W. Biederman7c0ff872010-02-03 23:13:24 -080089 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;
Maneesh Soni988d1862005-05-31 10:39:14 +0530102 }
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800103 return 0;
104}
105
106int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
107{
108 struct inode *inode = dentry->d_inode;
109 struct sysfs_dirent *sd = dentry->d_fsdata;
110 int error;
111
112 if (!sd)
113 return -EINVAL;
114
Eric W. Biedermanf8d4f612010-02-12 19:22:23 -0800115 mutex_lock(&sysfs_mutex);
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800116 error = inode_change_ok(inode, iattr);
117 if (error)
Eric W. Biedermanf8d4f612010-02-12 19:22:23 -0800118 goto out;
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800119
Nick Piggin75de46b2010-05-31 17:58:02 +1000120 error = sysfs_sd_setattr(sd, iattr);
121 if (error)
122 goto out;
123
Nick Piggin3322e792010-05-27 22:42:19 +1000124 /* this ignores size changes */
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200125 setattr_copy(inode, iattr);
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800126
Eric W. Biedermanf8d4f612010-02-12 19:22:23 -0800127out:
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800128 mutex_unlock(&sysfs_mutex);
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400129 return error;
130}
Maneesh Soni988d1862005-05-31 10:39:14 +0530131
Eric W. Biedermanf44d3e72009-11-07 23:26:59 -0800132static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len)
133{
134 struct sysfs_inode_attrs *iattrs;
135 void *old_secdata;
136 size_t old_secdata_len;
137
138 iattrs = sd->s_iattr;
139 if (!iattrs)
140 iattrs = sysfs_init_inode_attrs(sd);
141 if (!iattrs)
142 return -ENOMEM;
143
144 old_secdata = iattrs->ia_secdata;
145 old_secdata_len = iattrs->ia_secdata_len;
146
147 iattrs->ia_secdata = *secdata;
148 iattrs->ia_secdata_len = *secdata_len;
149
150 *secdata = old_secdata;
151 *secdata_len = old_secdata_len;
152 return 0;
153}
154
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400155int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
156 size_t size, int flags)
157{
158 struct sysfs_dirent *sd = dentry->d_fsdata;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400159 void *secdata;
160 int error;
161 u32 secdata_len = 0;
Maneesh Soni988d1862005-05-31 10:39:14 +0530162
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400163 if (!sd)
164 return -EINVAL;
Maneesh Soni988d1862005-05-31 10:39:14 +0530165
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400166 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,
169 value, size, flags);
170 if (error)
171 goto out;
172 error = security_inode_getsecctx(dentry->d_inode,
173 &secdata, &secdata_len);
174 if (error)
175 goto out;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400176
Eric W. Biedermanf44d3e72009-11-07 23:26:59 -0800177 mutex_lock(&sysfs_mutex);
178 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
179 mutex_unlock(&sysfs_mutex);
180
181 if (secdata)
182 security_release_secctx(secdata, secdata_len);
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400183 } else
184 return -EINVAL;
185out:
Maneesh Soni988d1862005-05-31 10:39:14 +0530186 return error;
187}
188
Maneesh Soni82155342005-05-31 10:39:52 +0530189static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
190{
191 inode->i_mode = mode;
Maneesh Soni82155342005-05-31 10:39:52 +0530192 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
193}
194
195static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
196{
Maneesh Soni82155342005-05-31 10:39:52 +0530197 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;
202}
203
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900204static int sysfs_count_nlink(struct sysfs_dirent *sd)
205{
206 struct sysfs_dirent *child;
207 int nr = 0;
208
Tejun Heobc747f32007-09-20 16:05:12 +0900209 for (child = sd->s_dir.children; child; child = child->s_sibling)
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900210 if (sysfs_type(child) == SYSFS_DIR)
211 nr++;
212
213 return nr + 2;
214}
215
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800216static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
217{
218 struct sysfs_inode_attrs *iattrs = sd->s_iattr;
219
220 inode->i_mode = sd->s_mode;
221 if (iattrs) {
222 /* sysfs_dirent has non-default attributes
223 * get them from persistent copy in sysfs_dirent
224 */
225 set_inode_attr(inode, &iattrs->ia_iattr);
226 security_inode_notifysecctx(inode,
227 iattrs->ia_secdata,
228 iattrs->ia_secdata_len);
229 }
230
231 if (sysfs_type(sd) == SYSFS_DIR)
232 inode->i_nlink = sysfs_count_nlink(sd);
233}
234
235int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
236{
237 struct sysfs_dirent *sd = dentry->d_fsdata;
238 struct inode *inode = dentry->d_inode;
239
240 mutex_lock(&sysfs_mutex);
241 sysfs_refresh_inode(sd, inode);
242 mutex_unlock(&sysfs_mutex);
243
244 generic_fillattr(inode, stat);
245 return 0;
246}
247
Tejun Heobc37e282007-07-18 14:30:28 +0900248static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900250 struct bin_attribute *bin_attr;
251
Eric W. Biederman04256b42009-02-11 13:20:23 -0800252 inode->i_private = sysfs_get(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900253 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;
Maneesh Soni82155342005-05-31 10:39:52 +0530256
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800257 set_default_inode_attr(inode, sd->s_mode);
258 sysfs_refresh_inode(sd, inode);
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900259
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900260 /* initialize inode according to type */
261 switch (sysfs_type(sd)) {
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900262 case SYSFS_DIR:
263 inode->i_op = &sysfs_dir_inode_operations;
264 inode->i_fop = &sysfs_dir_operations;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900265 break;
266 case SYSFS_KOBJ_ATTR:
267 inode->i_size = PAGE_SIZE;
268 inode->i_fop = &sysfs_file_operations;
269 break;
270 case SYSFS_KOBJ_BIN_ATTR:
Tejun Heob1fc3d62007-09-20 16:05:11 +0900271 bin_attr = sd->s_bin_attr.bin_attr;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900272 inode->i_size = bin_attr->size;
273 inode->i_fop = &bin_fops;
274 break;
275 case SYSFS_KOBJ_LINK:
276 inode->i_op = &sysfs_symlink_inode_operations;
277 break;
278 default:
279 BUG();
280 }
281
282 unlock_new_inode(inode);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900283}
284
285/**
Tejun Heo8312a8d2007-06-14 03:45:17 +0900286 * sysfs_get_inode - get inode for sysfs_dirent
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800287 * @sb: super block
Tejun Heofc9f54b2007-06-14 03:45:17 +0900288 * @sd: sysfs_dirent to allocate inode for
289 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900290 * Get inode for @sd. If such inode doesn't exist, a new inode
291 * is allocated and basics are initialized. New inode is
292 * returned locked.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900293 *
294 * LOCKING:
295 * Kernel thread context (may sleep).
296 *
297 * RETURNS:
298 * Pointer to allocated inode on success, NULL on failure.
299 */
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800300struct inode * sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
Tejun Heofc9f54b2007-06-14 03:45:17 +0900301{
302 struct inode *inode;
303
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800304 inode = iget_locked(sb, sd->s_ino);
Tejun Heo8312a8d2007-06-14 03:45:17 +0900305 if (inode && (inode->i_state & I_NEW))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900306 sysfs_init_inode(sd, inode);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return inode;
309}
310
Eric W. Biederman04256b42009-02-11 13:20:23 -0800311/*
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
Al Viro01cd9fe2010-06-04 22:21:54 -0400315 * super_operations.evict_inode() implementation is needed to drop that
Eric W. Biederman04256b42009-02-11 13:20:23 -0800316 * reference upon inode destruction.
317 */
Al Viro01cd9fe2010-06-04 22:21:54 -0400318void sysfs_evict_inode(struct inode *inode)
Eric W. Biederman04256b42009-02-11 13:20:23 -0800319{
320 struct sysfs_dirent *sd = inode->i_private;
321
322 truncate_inode_pages(&inode->i_data, 0);
Al Viro01cd9fe2010-06-04 22:21:54 -0400323 end_writeback(inode);
Eric W. Biederman04256b42009-02-11 13:20:23 -0800324 sysfs_put(sd);
325}
326
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700327int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Tejun Heofb6896d2007-06-14 04:27:24 +0900329 struct sysfs_addrm_cxt acxt;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900330 struct sysfs_dirent *sd;
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800331
Tejun Heo608e2662007-06-14 04:27:22 +0900332 if (!dir_sd)
Randy.Dunlap995982c2006-07-10 23:05:25 -0700333 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Tejun Heofb6896d2007-06-14 04:27:24 +0900335 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900336
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700337 sd = sysfs_find_dirent(dir_sd, ns, name);
Eric W. Biedermanaf10ec72010-03-30 11:31:27 -0700338 if (sd && (sd->s_ns != ns))
339 sd = NULL;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900340 if (sd)
341 sysfs_remove_one(&acxt, sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900342
Tejun Heo990e53f2007-08-02 21:38:03 +0900343 sysfs_addrm_finish(&acxt);
344
345 if (sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900346 return 0;
Tejun Heo990e53f2007-08-02 21:38:03 +0900347 else
348 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800350
351int sysfs_permission(struct inode *inode, int mask)
352{
353 struct sysfs_dirent *sd = inode->i_private;
354
355 mutex_lock(&sysfs_mutex);
356 sysfs_refresh_inode(sd, inode);
357 mutex_unlock(&sysfs_mutex);
358
359 return generic_permission(inode, mask, NULL);
360}