blob: e57f98e54fce2439393243099bd24a2fc00ec991 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "sysfs.h"
22
23extern struct super_block * sysfs_sb;
24
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070025static const struct address_space_operations sysfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 .readpage = simple_readpage,
Nick Piggin800d15a2007-10-16 01:25:03 -070027 .write_begin = simple_write_begin,
28 .write_end = simple_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
31static struct backing_dev_info sysfs_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +020032 .name = "sysfs",
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .ra_pages = 0, /* No readahead */
Miklos Szeredie4ad08f2008-04-30 00:54:37 -070034 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080037static const struct inode_operations sysfs_inode_operations ={
Maneesh Soni988d1862005-05-31 10:39:14 +053038 .setattr = sysfs_setattr,
39};
40
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070041int __init sysfs_inode_init(void)
42{
43 return bdi_init(&sysfs_backing_dev_info);
44}
45
Maneesh Soni988d1862005-05-31 10:39:14 +053046int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
47{
48 struct inode * inode = dentry->d_inode;
49 struct sysfs_dirent * sd = dentry->d_fsdata;
50 struct iattr * sd_iattr;
51 unsigned int ia_valid = iattr->ia_valid;
52 int error;
53
54 if (!sd)
55 return -EINVAL;
56
57 sd_iattr = sd->s_iattr;
58
59 error = inode_change_ok(inode, iattr);
60 if (error)
61 return error;
62
Ben Hutchings40a21592008-04-28 15:59:58 +010063 iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
64
Maneesh Soni988d1862005-05-31 10:39:14 +053065 error = inode_setattr(inode, iattr);
66 if (error)
67 return error;
68
69 if (!sd_iattr) {
70 /* setting attributes for the first time, allocate now */
Eric Sesterhenn58d49282006-02-22 11:18:15 +010071 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
Maneesh Soni988d1862005-05-31 10:39:14 +053072 if (!sd_iattr)
73 return -ENOMEM;
74 /* assign default attributes */
Maneesh Soni988d1862005-05-31 10:39:14 +053075 sd_iattr->ia_mode = sd->s_mode;
76 sd_iattr->ia_uid = 0;
77 sd_iattr->ia_gid = 0;
78 sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
79 sd->s_iattr = sd_iattr;
80 }
81
82 /* attributes were changed atleast once in past */
83
84 if (ia_valid & ATTR_UID)
85 sd_iattr->ia_uid = iattr->ia_uid;
86 if (ia_valid & ATTR_GID)
87 sd_iattr->ia_gid = iattr->ia_gid;
88 if (ia_valid & ATTR_ATIME)
89 sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
90 inode->i_sb->s_time_gran);
91 if (ia_valid & ATTR_MTIME)
92 sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
93 inode->i_sb->s_time_gran);
94 if (ia_valid & ATTR_CTIME)
95 sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
96 inode->i_sb->s_time_gran);
97 if (ia_valid & ATTR_MODE) {
98 umode_t mode = iattr->ia_mode;
99
100 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
101 mode &= ~S_ISGID;
Maneesh Soni9ca1eb32005-07-29 12:14:19 -0700102 sd_iattr->ia_mode = sd->s_mode = mode;
Maneesh Soni988d1862005-05-31 10:39:14 +0530103 }
104
105 return error;
106}
107
Maneesh Soni82155342005-05-31 10:39:52 +0530108static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
109{
110 inode->i_mode = mode;
Maneesh Soni82155342005-05-31 10:39:52 +0530111 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
112}
113
114static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
115{
116 inode->i_mode = iattr->ia_mode;
117 inode->i_uid = iattr->ia_uid;
118 inode->i_gid = iattr->ia_gid;
119 inode->i_atime = iattr->ia_atime;
120 inode->i_mtime = iattr->ia_mtime;
121 inode->i_ctime = iattr->ia_ctime;
122}
123
Arjan van de Ven232ba9d2006-07-12 09:03:06 -0700124
125/*
126 * sysfs has a different i_mutex lock order behavior for i_mutex than other
127 * filesystems; sysfs i_mutex is called in many places with subsystem locks
128 * held. At the same time, many of the VFS locking rules do not apply to
129 * sysfs at all (cross directory rename for example). To untangle this mess
130 * (which gives false positives in lockdep), we're giving sysfs inodes their
131 * own class for i_mutex.
132 */
133static struct lock_class_key sysfs_inode_imutex_key;
134
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900135static int sysfs_count_nlink(struct sysfs_dirent *sd)
136{
137 struct sysfs_dirent *child;
138 int nr = 0;
139
Tejun Heobc747f32007-09-20 16:05:12 +0900140 for (child = sd->s_dir.children; child; child = child->s_sibling)
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900141 if (sysfs_type(child) == SYSFS_DIR)
142 nr++;
143
144 return nr + 2;
145}
146
Tejun Heobc37e282007-07-18 14:30:28 +0900147static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900149 struct bin_attribute *bin_attr;
150
Eric W. Biederman04256b42009-02-11 13:20:23 -0800151 inode->i_private = sysfs_get(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900152 inode->i_mapping->a_ops = &sysfs_aops;
153 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
154 inode->i_op = &sysfs_inode_operations;
155 inode->i_ino = sd->s_ino;
156 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
Maneesh Soni82155342005-05-31 10:39:52 +0530157
Tejun Heofc9f54b2007-06-14 03:45:17 +0900158 if (sd->s_iattr) {
159 /* sysfs_dirent has non-default attributes
160 * get them for the new inode from persistent copy
161 * in sysfs_dirent
162 */
163 set_inode_attr(inode, sd->s_iattr);
164 } else
165 set_default_inode_attr(inode, sd->s_mode);
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900166
167
168 /* initialize inode according to type */
169 switch (sysfs_type(sd)) {
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900170 case SYSFS_DIR:
171 inode->i_op = &sysfs_dir_inode_operations;
172 inode->i_fop = &sysfs_dir_operations;
173 inode->i_nlink = sysfs_count_nlink(sd);
174 break;
175 case SYSFS_KOBJ_ATTR:
176 inode->i_size = PAGE_SIZE;
177 inode->i_fop = &sysfs_file_operations;
178 break;
179 case SYSFS_KOBJ_BIN_ATTR:
Tejun Heob1fc3d62007-09-20 16:05:11 +0900180 bin_attr = sd->s_bin_attr.bin_attr;
Eric W. Biederman372e88b2007-08-20 21:36:29 +0900181 inode->i_size = bin_attr->size;
182 inode->i_fop = &bin_fops;
183 break;
184 case SYSFS_KOBJ_LINK:
185 inode->i_op = &sysfs_symlink_inode_operations;
186 break;
187 default:
188 BUG();
189 }
190
191 unlock_new_inode(inode);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900192}
193
194/**
Tejun Heo8312a8d2007-06-14 03:45:17 +0900195 * sysfs_get_inode - get inode for sysfs_dirent
Tejun Heofc9f54b2007-06-14 03:45:17 +0900196 * @sd: sysfs_dirent to allocate inode for
197 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900198 * Get inode for @sd. If such inode doesn't exist, a new inode
199 * is allocated and basics are initialized. New inode is
200 * returned locked.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900201 *
202 * LOCKING:
203 * Kernel thread context (may sleep).
204 *
205 * RETURNS:
206 * Pointer to allocated inode on success, NULL on failure.
207 */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900208struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
Tejun Heofc9f54b2007-06-14 03:45:17 +0900209{
210 struct inode *inode;
211
Tejun Heo8312a8d2007-06-14 03:45:17 +0900212 inode = iget_locked(sysfs_sb, sd->s_ino);
213 if (inode && (inode->i_state & I_NEW))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900214 sysfs_init_inode(sd, inode);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return inode;
217}
218
Eric W. Biederman04256b42009-02-11 13:20:23 -0800219/*
220 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
221 * To prevent the sysfs inode numbers from being freed prematurely we take a
222 * reference to sysfs_dirent from the sysfs inode. A
223 * super_operations.delete_inode() implementation is needed to drop that
224 * reference upon inode destruction.
225 */
226void sysfs_delete_inode(struct inode *inode)
227{
228 struct sysfs_dirent *sd = inode->i_private;
229
230 truncate_inode_pages(&inode->i_data, 0);
231 clear_inode(inode);
232 sysfs_put(sd);
233}
234
Tejun Heo608e2662007-06-14 04:27:22 +0900235int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Tejun Heofb6896d2007-06-14 04:27:24 +0900237 struct sysfs_addrm_cxt acxt;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900238 struct sysfs_dirent *sd;
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800239
Tejun Heo608e2662007-06-14 04:27:22 +0900240 if (!dir_sd)
Randy.Dunlap995982c2006-07-10 23:05:25 -0700241 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Tejun Heofb6896d2007-06-14 04:27:24 +0900243 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900244
Tejun Heo41fc1c22007-08-02 21:38:03 +0900245 sd = sysfs_find_dirent(dir_sd, name);
246 if (sd)
247 sysfs_remove_one(&acxt, sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900248
Tejun Heo990e53f2007-08-02 21:38:03 +0900249 sysfs_addrm_finish(&acxt);
250
251 if (sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900252 return 0;
Tejun Heo990e53f2007-08-02 21:38:03 +0900253 else
254 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}