blob: 76e977c300f7436269eeefb133af5a0449f56b30 [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>
David P. Quigleyddd29ec2009-09-09 14:25:37 -040021#include <linux/xattr.h>
22#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "sysfs.h"
24
25extern struct super_block * sysfs_sb;
26
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070027static const struct address_space_operations sysfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 .readpage = simple_readpage,
Nick Piggin800d15a2007-10-16 01:25:03 -070029 .write_begin = simple_write_begin,
30 .write_end = simple_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33static struct backing_dev_info sysfs_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +020034 .name = "sysfs",
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .ra_pages = 0, /* No readahead */
Miklos Szeredie4ad08f2008-04-30 00:54:37 -070036 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080039static const struct inode_operations sysfs_inode_operations ={
Maneesh Soni988d1862005-05-31 10:39:14 +053040 .setattr = sysfs_setattr,
David P. Quigleyddd29ec2009-09-09 14:25:37 -040041 .setxattr = sysfs_setxattr,
Maneesh Soni988d1862005-05-31 10:39:14 +053042};
43
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070044int __init sysfs_inode_init(void)
45{
46 return bdi_init(&sysfs_backing_dev_info);
47}
48
Stefan Richterf38506c2009-10-14 20:47:32 +020049static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
David P. Quigleyddd29ec2009-09-09 14:25:37 -040050{
51 struct sysfs_inode_attrs *attrs;
52 struct iattr *iattrs;
53
54 attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
55 if (!attrs)
56 return NULL;
57 iattrs = &attrs->ia_iattr;
58
59 /* assign default attributes */
60 iattrs->ia_mode = sd->s_mode;
61 iattrs->ia_uid = 0;
62 iattrs->ia_gid = 0;
63 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
64
65 return attrs;
66}
Stefan Richterf38506c2009-10-14 20:47:32 +020067
Eric W. Biederman35df63c2009-11-20 16:08:50 -080068int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
Maneesh Soni988d1862005-05-31 10:39:14 +053069{
David P. Quigleyddd29ec2009-09-09 14:25:37 -040070 struct sysfs_inode_attrs *sd_attrs;
71 struct iattr *iattrs;
Maneesh Soni988d1862005-05-31 10:39:14 +053072 unsigned int ia_valid = iattr->ia_valid;
Maneesh Soni988d1862005-05-31 10:39:14 +053073
David P. Quigleyddd29ec2009-09-09 14:25:37 -040074 sd_attrs = sd->s_iattr;
Maneesh Soni988d1862005-05-31 10:39:14 +053075
David P. Quigleyddd29ec2009-09-09 14:25:37 -040076 if (!sd_attrs) {
Maneesh Soni988d1862005-05-31 10:39:14 +053077 /* setting attributes for the first time, allocate now */
David P. Quigleyddd29ec2009-09-09 14:25:37 -040078 sd_attrs = sysfs_init_inode_attrs(sd);
79 if (!sd_attrs)
Maneesh Soni988d1862005-05-31 10:39:14 +053080 return -ENOMEM;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040081 sd->s_iattr = sd_attrs;
82 } else {
83 /* attributes were changed at least once in past */
84 iattrs = &sd_attrs->ia_iattr;
85
86 if (ia_valid & ATTR_UID)
87 iattrs->ia_uid = iattr->ia_uid;
88 if (ia_valid & ATTR_GID)
89 iattrs->ia_gid = iattr->ia_gid;
90 if (ia_valid & ATTR_ATIME)
Eric W. Biederman4be3df22009-11-07 23:27:03 -080091 iattrs->ia_atime = iattr->ia_atime;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040092 if (ia_valid & ATTR_MTIME)
Eric W. Biederman4be3df22009-11-07 23:27:03 -080093 iattrs->ia_mtime = iattr->ia_mtime;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040094 if (ia_valid & ATTR_CTIME)
Eric W. Biederman4be3df22009-11-07 23:27:03 -080095 iattrs->ia_ctime = iattr->ia_ctime;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040096 if (ia_valid & ATTR_MODE) {
97 umode_t mode = iattr->ia_mode;
David P. Quigleyddd29ec2009-09-09 14:25:37 -040098 iattrs->ia_mode = sd->s_mode = mode;
99 }
Maneesh Soni988d1862005-05-31 10:39:14 +0530100 }
Eric W. Biederman35df63c2009-11-20 16:08:50 -0800101 return 0;
102}
103
104int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
105{
106 struct inode *inode = dentry->d_inode;
107 struct sysfs_dirent *sd = dentry->d_fsdata;
108 int error;
109
110 if (!sd)
111 return -EINVAL;
112
113 error = inode_change_ok(inode, iattr);
114 if (error)
115 return error;
116
117 iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
118 if (iattr->ia_valid & ATTR_MODE) {
119 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
120 iattr->ia_mode &= ~S_ISGID;
121 }
122
123 error = inode_setattr(inode, iattr);
124 if (error)
125 return error;
126
127 mutex_lock(&sysfs_mutex);
128 error = sysfs_sd_setattr(sd, iattr);
129 mutex_unlock(&sysfs_mutex);
130
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400131 return error;
132}
Maneesh Soni988d1862005-05-31 10:39:14 +0530133
Eric W. Biedermanf44d3e72009-11-07 23:26:59 -0800134static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len)
135{
136 struct sysfs_inode_attrs *iattrs;
137 void *old_secdata;
138 size_t old_secdata_len;
139
140 iattrs = sd->s_iattr;
141 if (!iattrs)
142 iattrs = sysfs_init_inode_attrs(sd);
143 if (!iattrs)
144 return -ENOMEM;
145
146 old_secdata = iattrs->ia_secdata;
147 old_secdata_len = iattrs->ia_secdata_len;
148
149 iattrs->ia_secdata = *secdata;
150 iattrs->ia_secdata_len = *secdata_len;
151
152 *secdata = old_secdata;
153 *secdata_len = old_secdata_len;
154 return 0;
155}
156
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400157int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
158 size_t size, int flags)
159{
160 struct sysfs_dirent *sd = dentry->d_fsdata;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400161 void *secdata;
162 int error;
163 u32 secdata_len = 0;
Maneesh Soni988d1862005-05-31 10:39:14 +0530164
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400165 if (!sd)
166 return -EINVAL;
Maneesh Soni988d1862005-05-31 10:39:14 +0530167
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400168 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;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400178
Eric W. Biedermanf44d3e72009-11-07 23:26:59 -0800179 mutex_lock(&sysfs_mutex);
180 error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
181 mutex_unlock(&sysfs_mutex);
182
183 if (secdata)
184 security_release_secctx(secdata, secdata_len);
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400185 } else
186 return -EINVAL;
187out:
Maneesh Soni988d1862005-05-31 10:39:14 +0530188 return error;
189}
190
Maneesh Soni82155342005-05-31 10:39:52 +0530191static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
192{
193 inode->i_mode = mode;
Maneesh Soni82155342005-05-31 10:39:52 +0530194 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
195}
196
197static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
198{
199 inode->i_mode = iattr->ia_mode;
200 inode->i_uid = iattr->ia_uid;
201 inode->i_gid = iattr->ia_gid;
202 inode->i_atime = iattr->ia_atime;
203 inode->i_mtime = iattr->ia_mtime;
204 inode->i_ctime = iattr->ia_ctime;
205}
206
Arjan van de Ven232ba9d2006-07-12 09:03:06 -0700207
208/*
209 * sysfs has a different i_mutex lock order behavior for i_mutex than other
210 * filesystems; sysfs i_mutex is called in many places with subsystem locks
211 * held. At the same time, many of the VFS locking rules do not apply to
212 * sysfs at all (cross directory rename for example). To untangle this mess
213 * (which gives false positives in lockdep), we're giving sysfs inodes their
214 * own class for i_mutex.
215 */
216static struct lock_class_key sysfs_inode_imutex_key;
217
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900218static int sysfs_count_nlink(struct sysfs_dirent *sd)
219{
220 struct sysfs_dirent *child;
221 int nr = 0;
222
Tejun Heobc747f32007-09-20 16:05:12 +0900223 for (child = sd->s_dir.children; child; child = child->s_sibling)
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900224 if (sysfs_type(child) == SYSFS_DIR)
225 nr++;
226
227 return nr + 2;
228}
229
Tejun Heobc37e282007-07-18 14:30:28 +0900230static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900232 struct bin_attribute *bin_attr;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400233 struct sysfs_inode_attrs *iattrs;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900234
Eric W. Biederman04256b42009-02-11 13:20:23 -0800235 inode->i_private = sysfs_get(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900236 inode->i_mapping->a_ops = &sysfs_aops;
237 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
238 inode->i_op = &sysfs_inode_operations;
239 inode->i_ino = sd->s_ino;
240 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
Maneesh Soni82155342005-05-31 10:39:52 +0530241
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400242 iattrs = sd->s_iattr;
243 if (iattrs) {
Tejun Heofc9f54b2007-06-14 03:45:17 +0900244 /* sysfs_dirent has non-default attributes
245 * get them for the new inode from persistent copy
246 * in sysfs_dirent
247 */
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400248 set_inode_attr(inode, &iattrs->ia_iattr);
249 if (iattrs->ia_secdata)
250 security_inode_notifysecctx(inode,
251 iattrs->ia_secdata,
252 iattrs->ia_secdata_len);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900253 } else
254 set_default_inode_attr(inode, sd->s_mode);
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900255
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900256 /* initialize inode according to type */
257 switch (sysfs_type(sd)) {
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900258 case SYSFS_DIR:
259 inode->i_op = &sysfs_dir_inode_operations;
260 inode->i_fop = &sysfs_dir_operations;
261 inode->i_nlink = sysfs_count_nlink(sd);
262 break;
263 case SYSFS_KOBJ_ATTR:
264 inode->i_size = PAGE_SIZE;
265 inode->i_fop = &sysfs_file_operations;
266 break;
267 case SYSFS_KOBJ_BIN_ATTR:
Tejun Heob1fc3d62007-09-20 16:05:11 +0900268 bin_attr = sd->s_bin_attr.bin_attr;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900269 inode->i_size = bin_attr->size;
270 inode->i_fop = &bin_fops;
271 break;
272 case SYSFS_KOBJ_LINK:
273 inode->i_op = &sysfs_symlink_inode_operations;
274 break;
275 default:
276 BUG();
277 }
278
279 unlock_new_inode(inode);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900280}
281
282/**
Tejun Heo8312a8d2007-06-14 03:45:17 +0900283 * sysfs_get_inode - get inode for sysfs_dirent
Tejun Heofc9f54b2007-06-14 03:45:17 +0900284 * @sd: sysfs_dirent to allocate inode for
285 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900286 * Get inode for @sd. If such inode doesn't exist, a new inode
287 * is allocated and basics are initialized. New inode is
288 * returned locked.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900289 *
290 * LOCKING:
291 * Kernel thread context (may sleep).
292 *
293 * RETURNS:
294 * Pointer to allocated inode on success, NULL on failure.
295 */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900296struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
Tejun Heofc9f54b2007-06-14 03:45:17 +0900297{
298 struct inode *inode;
299
Tejun Heo8312a8d2007-06-14 03:45:17 +0900300 inode = iget_locked(sysfs_sb, sd->s_ino);
301 if (inode && (inode->i_state & I_NEW))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900302 sysfs_init_inode(sd, inode);
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return inode;
305}
306
Eric W. Biederman04256b42009-02-11 13:20:23 -0800307/*
308 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
309 * To prevent the sysfs inode numbers from being freed prematurely we take a
310 * reference to sysfs_dirent from the sysfs inode. A
311 * super_operations.delete_inode() implementation is needed to drop that
312 * reference upon inode destruction.
313 */
314void sysfs_delete_inode(struct inode *inode)
315{
316 struct sysfs_dirent *sd = inode->i_private;
317
318 truncate_inode_pages(&inode->i_data, 0);
319 clear_inode(inode);
320 sysfs_put(sd);
321}
322
Tejun Heo608e2662007-06-14 04:27:22 +0900323int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Tejun Heofb6896d2007-06-14 04:27:24 +0900325 struct sysfs_addrm_cxt acxt;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900326 struct sysfs_dirent *sd;
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800327
Tejun Heo608e2662007-06-14 04:27:22 +0900328 if (!dir_sd)
Randy.Dunlap995982c2006-07-10 23:05:25 -0700329 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Tejun Heofb6896d2007-06-14 04:27:24 +0900331 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900332
Tejun Heo41fc1c22007-08-02 21:38:03 +0900333 sd = sysfs_find_dirent(dir_sd, name);
334 if (sd)
335 sysfs_remove_one(&acxt, sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900336
Tejun Heo990e53f2007-08-02 21:38:03 +0900337 sysfs_addrm_finish(&acxt);
338
339 if (sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900340 return 0;
Tejun Heo990e53f2007-08-02 21:38:03 +0900341 else
342 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}