blob: e3f091a81c724bcf830184e1b8db4d66c40b4c5d [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>
Alexey Dobriyan57cc7212011-01-10 08:18:25 +020022#include <linux/sysfs.h>
David P. Quigleyddd29ec2009-09-09 14:25:37 -040023#include <linux/xattr.h>
24#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "sysfs.h"
26
27extern struct super_block * sysfs_sb;
28
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070029static const struct address_space_operations sysfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .readpage = simple_readpage,
Nick Piggin800d15a2007-10-16 01:25:03 -070031 .write_begin = simple_write_begin,
32 .write_end = simple_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35static struct backing_dev_info sysfs_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +020036 .name = "sysfs",
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .ra_pages = 0, /* No readahead */
Miklos Szeredie4ad08f2008-04-30 00:54:37 -070038 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080041static const struct inode_operations sysfs_inode_operations ={
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -080042 .permission = sysfs_permission,
Maneesh Soni988d1862005-05-31 10:39:14 +053043 .setattr = sysfs_setattr,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -080044 .getattr = sysfs_getattr,
David P. Quigleyddd29ec2009-09-09 14:25:37 -040045 .setxattr = sysfs_setxattr,
Maneesh Soni988d1862005-05-31 10:39:14 +053046};
47
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070048int __init sysfs_inode_init(void)
49{
50 return bdi_init(&sysfs_backing_dev_info);
51}
52
Stefan Richterf38506c2009-10-14 20:47:32 +020053static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
David P. Quigleyddd29ec2009-09-09 14:25:37 -040054{
55 struct sysfs_inode_attrs *attrs;
56 struct iattr *iattrs;
57
58 attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
59 if (!attrs)
60 return NULL;
61 iattrs = &attrs->ia_iattr;
62
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;
68
69 return attrs;
70}
Stefan Richterf38506c2009-10-14 20:47:32 +020071
Eric W. Biederman35df63c2009-11-20 16:08:50 -080072int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
Maneesh Soni988d1862005-05-31 10:39:14 +053073{
David P. Quigleyddd29ec2009-09-09 14:25:37 -040074 struct sysfs_inode_attrs *sd_attrs;
75 struct iattr *iattrs;
Maneesh Soni988d1862005-05-31 10:39:14 +053076 unsigned int ia_valid = iattr->ia_valid;
Maneesh Soni988d1862005-05-31 10:39:14 +053077
David P. Quigleyddd29ec2009-09-09 14:25:37 -040078 sd_attrs = sd->s_iattr;
Maneesh Soni988d1862005-05-31 10:39:14 +053079
David P. Quigleyddd29ec2009-09-09 14:25:37 -040080 if (!sd_attrs) {
Maneesh Soni988d1862005-05-31 10:39:14 +053081 /* setting attributes for the first time, allocate now */
David P. Quigleyddd29ec2009-09-09 14:25:37 -040082 sd_attrs = sysfs_init_inode_attrs(sd);
83 if (!sd_attrs)
Maneesh Soni988d1862005-05-31 10:39:14 +053084 return -ENOMEM;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040085 sd->s_iattr = sd_attrs;
Eric W. Biederman7c0ff872010-02-03 23:13:24 -080086 }
87 /* attributes were changed at least once in past */
88 iattrs = &sd_attrs->ia_iattr;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040089
Eric W. Biederman7c0ff872010-02-03 23:13:24 -080090 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;
Maneesh Soni988d1862005-05-31 10:39:14 +0530103 }
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800104 return 0;
105}
106
107int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
108{
109 struct inode *inode = dentry->d_inode;
110 struct sysfs_dirent *sd = dentry->d_fsdata;
111 int error;
112
113 if (!sd)
114 return -EINVAL;
115
Eric W. Biedermanf8d4f612010-02-12 19:22:23 -0800116 mutex_lock(&sysfs_mutex);
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800117 error = inode_change_ok(inode, iattr);
118 if (error)
Eric W. Biedermanf8d4f612010-02-12 19:22:23 -0800119 goto out;
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800120
Nick Piggin75de46b2010-05-31 17:58:02 +1000121 error = sysfs_sd_setattr(sd, iattr);
122 if (error)
123 goto out;
124
Nick Piggin3322e792010-05-27 22:42:19 +1000125 /* this ignores size changes */
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200126 setattr_copy(inode, iattr);
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800127
Eric W. Biedermanf8d4f612010-02-12 19:22:23 -0800128out:
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800129 mutex_unlock(&sysfs_mutex);
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400130 return error;
131}
Maneesh Soni988d1862005-05-31 10:39:14 +0530132
Eric W. Biedermanf44d3e72009-11-07 23:26:59 -0800133static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len)
134{
135 struct sysfs_inode_attrs *iattrs;
136 void *old_secdata;
137 size_t old_secdata_len;
138
139 iattrs = sd->s_iattr;
140 if (!iattrs)
141 iattrs = sysfs_init_inode_attrs(sd);
142 if (!iattrs)
143 return -ENOMEM;
144
145 old_secdata = iattrs->ia_secdata;
146 old_secdata_len = iattrs->ia_secdata_len;
147
148 iattrs->ia_secdata = *secdata;
149 iattrs->ia_secdata_len = *secdata_len;
150
151 *secdata = old_secdata;
152 *secdata_len = old_secdata_len;
153 return 0;
154}
155
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400156int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
157 size_t size, int flags)
158{
159 struct sysfs_dirent *sd = dentry->d_fsdata;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400160 void *secdata;
161 int error;
162 u32 secdata_len = 0;
Maneesh Soni988d1862005-05-31 10:39:14 +0530163
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400164 if (!sd)
165 return -EINVAL;
Maneesh Soni988d1862005-05-31 10:39:14 +0530166
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400167 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;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400177
Eric W. Biedermanf44d3e72009-11-07 23:26:59 -0800178 mutex_lock(&sysfs_mutex);
179 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
180 mutex_unlock(&sysfs_mutex);
181
182 if (secdata)
183 security_release_secctx(secdata, secdata_len);
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400184 } else
185 return -EINVAL;
186out:
Maneesh Soni988d1862005-05-31 10:39:14 +0530187 return error;
188}
189
Maneesh Soni82155342005-05-31 10:39:52 +0530190static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
191{
192 inode->i_mode = mode;
Maneesh Soni82155342005-05-31 10:39:52 +0530193 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
194}
195
196static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
197{
Maneesh Soni82155342005-05-31 10:39:52 +0530198 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;
203}
204
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900205static int sysfs_count_nlink(struct sysfs_dirent *sd)
206{
207 struct sysfs_dirent *child;
208 int nr = 0;
209
Tejun Heobc747f32007-09-20 16:05:12 +0900210 for (child = sd->s_dir.children; child; child = child->s_sibling)
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900211 if (sysfs_type(child) == SYSFS_DIR)
212 nr++;
213
214 return nr + 2;
215}
216
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800217static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
218{
219 struct sysfs_inode_attrs *iattrs = sd->s_iattr;
220
221 inode->i_mode = sd->s_mode;
222 if (iattrs) {
223 /* sysfs_dirent has non-default attributes
224 * get them from persistent copy in sysfs_dirent
225 */
226 set_inode_attr(inode, &iattrs->ia_iattr);
227 security_inode_notifysecctx(inode,
228 iattrs->ia_secdata,
229 iattrs->ia_secdata_len);
230 }
231
232 if (sysfs_type(sd) == SYSFS_DIR)
233 inode->i_nlink = sysfs_count_nlink(sd);
234}
235
236int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
237{
238 struct sysfs_dirent *sd = dentry->d_fsdata;
239 struct inode *inode = dentry->d_inode;
240
241 mutex_lock(&sysfs_mutex);
242 sysfs_refresh_inode(sd, inode);
243 mutex_unlock(&sysfs_mutex);
244
245 generic_fillattr(inode, stat);
246 return 0;
247}
248
Tejun Heobc37e282007-07-18 14:30:28 +0900249static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900251 struct bin_attribute *bin_attr;
252
Eric W. Biederman04256b42009-02-11 13:20:23 -0800253 inode->i_private = sysfs_get(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900254 inode->i_mapping->a_ops = &sysfs_aops;
255 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
256 inode->i_op = &sysfs_inode_operations;
Maneesh Soni82155342005-05-31 10:39:52 +0530257
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800258 set_default_inode_attr(inode, sd->s_mode);
259 sysfs_refresh_inode(sd, inode);
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900260
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900261 /* initialize inode according to type */
262 switch (sysfs_type(sd)) {
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900263 case SYSFS_DIR:
264 inode->i_op = &sysfs_dir_inode_operations;
265 inode->i_fop = &sysfs_dir_operations;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900266 break;
267 case SYSFS_KOBJ_ATTR:
268 inode->i_size = PAGE_SIZE;
269 inode->i_fop = &sysfs_file_operations;
270 break;
271 case SYSFS_KOBJ_BIN_ATTR:
Tejun Heob1fc3d62007-09-20 16:05:11 +0900272 bin_attr = sd->s_bin_attr.bin_attr;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900273 inode->i_size = bin_attr->size;
274 inode->i_fop = &bin_fops;
275 break;
276 case SYSFS_KOBJ_LINK:
277 inode->i_op = &sysfs_symlink_inode_operations;
278 break;
279 default:
280 BUG();
281 }
282
283 unlock_new_inode(inode);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900284}
285
286/**
Tejun Heo8312a8d2007-06-14 03:45:17 +0900287 * sysfs_get_inode - get inode for sysfs_dirent
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800288 * @sb: super block
Tejun Heofc9f54b2007-06-14 03:45:17 +0900289 * @sd: sysfs_dirent to allocate inode for
290 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900291 * Get inode for @sd. If such inode doesn't exist, a new inode
292 * is allocated and basics are initialized. New inode is
293 * returned locked.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900294 *
295 * LOCKING:
296 * Kernel thread context (may sleep).
297 *
298 * RETURNS:
299 * Pointer to allocated inode on success, NULL on failure.
300 */
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800301struct inode * sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
Tejun Heofc9f54b2007-06-14 03:45:17 +0900302{
303 struct inode *inode;
304
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800305 inode = iget_locked(sb, sd->s_ino);
Tejun Heo8312a8d2007-06-14 03:45:17 +0900306 if (inode && (inode->i_state & I_NEW))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900307 sysfs_init_inode(sd, inode);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return inode;
310}
311
Eric W. Biederman04256b42009-02-11 13:20:23 -0800312/*
313 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
314 * To prevent the sysfs inode numbers from being freed prematurely we take a
315 * reference to sysfs_dirent from the sysfs inode. A
Al Viro01cd9fe2010-06-04 22:21:54 -0400316 * super_operations.evict_inode() implementation is needed to drop that
Eric W. Biederman04256b42009-02-11 13:20:23 -0800317 * reference upon inode destruction.
318 */
Al Viro01cd9fe2010-06-04 22:21:54 -0400319void sysfs_evict_inode(struct inode *inode)
Eric W. Biederman04256b42009-02-11 13:20:23 -0800320{
321 struct sysfs_dirent *sd = inode->i_private;
322
323 truncate_inode_pages(&inode->i_data, 0);
Al Viro01cd9fe2010-06-04 22:21:54 -0400324 end_writeback(inode);
Eric W. Biederman04256b42009-02-11 13:20:23 -0800325 sysfs_put(sd);
326}
327
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700328int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Tejun Heofb6896d2007-06-14 04:27:24 +0900330 struct sysfs_addrm_cxt acxt;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900331 struct sysfs_dirent *sd;
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800332
Tejun Heo608e2662007-06-14 04:27:22 +0900333 if (!dir_sd)
Randy.Dunlap995982c2006-07-10 23:05:25 -0700334 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Tejun Heofb6896d2007-06-14 04:27:24 +0900336 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900337
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700338 sd = sysfs_find_dirent(dir_sd, ns, name);
Eric W. Biedermanaf10ec72010-03-30 11:31:27 -0700339 if (sd && (sd->s_ns != ns))
340 sd = NULL;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900341 if (sd)
342 sysfs_remove_one(&acxt, sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900343
Tejun Heo990e53f2007-08-02 21:38:03 +0900344 sysfs_addrm_finish(&acxt);
345
346 if (sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900347 return 0;
Tejun Heo990e53f2007-08-02 21:38:03 +0900348 else
349 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800351
Al Viro10556cb2011-06-20 19:28:19 -0400352int sysfs_permission(struct inode *inode, int mask)
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800353{
Nick Pigginb74c79e2011-01-07 17:49:58 +1100354 struct sysfs_dirent *sd;
355
Al Viro10556cb2011-06-20 19:28:19 -0400356 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100357 return -ECHILD;
358
359 sd = inode->i_private;
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800360
361 mutex_lock(&sysfs_mutex);
362 sysfs_refresh_inode(sd, inode);
363 mutex_unlock(&sysfs_mutex);
364
Al Viro2830ba72011-06-20 19:16:29 -0400365 return generic_permission(inode, mask);
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800366}