blob: 1be853706e9991b0b4f270479acde63f5cff527a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c - basic inode and dentry operations.
3 *
4 * sysfs is Copyright (c) 2001-3 Patrick Mochel
5 *
6 * Please see Documentation/filesystems/sysfs.txt for more information.
7 */
8
9#undef DEBUG
10
11#include <linux/pagemap.h>
12#include <linux/namei.h>
13#include <linux/backing-dev.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080014#include <linux/capability.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070015#include <linux/errno.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010017#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "sysfs.h"
19
20extern struct super_block * sysfs_sb;
21
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070022static const struct address_space_operations sysfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 .readpage = simple_readpage,
24 .prepare_write = simple_prepare_write,
25 .commit_write = simple_commit_write
26};
27
28static struct backing_dev_info sysfs_backing_dev_info = {
29 .ra_pages = 0, /* No readahead */
30 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
31};
32
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080033static const struct inode_operations sysfs_inode_operations ={
Maneesh Soni988d1862005-05-31 10:39:14 +053034 .setattr = sysfs_setattr,
35};
36
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070037void sysfs_delete_inode(struct inode *inode)
38{
39 /* Free the shadowed directory inode operations */
40 if (sysfs_is_shadowed_inode(inode)) {
41 kfree(inode->i_op);
42 inode->i_op = NULL;
43 }
44 return generic_delete_inode(inode);
45}
46
Maneesh Soni988d1862005-05-31 10:39:14 +053047int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
48{
49 struct inode * inode = dentry->d_inode;
50 struct sysfs_dirent * sd = dentry->d_fsdata;
51 struct iattr * sd_iattr;
52 unsigned int ia_valid = iattr->ia_valid;
53 int error;
54
55 if (!sd)
56 return -EINVAL;
57
58 sd_iattr = sd->s_iattr;
59
60 error = inode_change_ok(inode, iattr);
61 if (error)
62 return error;
63
64 error = inode_setattr(inode, iattr);
65 if (error)
66 return error;
67
68 if (!sd_iattr) {
69 /* setting attributes for the first time, allocate now */
Eric Sesterhenn58d49282006-02-22 11:18:15 +010070 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
Maneesh Soni988d1862005-05-31 10:39:14 +053071 if (!sd_iattr)
72 return -ENOMEM;
73 /* assign default attributes */
Maneesh Soni988d1862005-05-31 10:39:14 +053074 sd_iattr->ia_mode = sd->s_mode;
75 sd_iattr->ia_uid = 0;
76 sd_iattr->ia_gid = 0;
77 sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
78 sd->s_iattr = sd_iattr;
79 }
80
81 /* attributes were changed atleast once in past */
82
83 if (ia_valid & ATTR_UID)
84 sd_iattr->ia_uid = iattr->ia_uid;
85 if (ia_valid & ATTR_GID)
86 sd_iattr->ia_gid = iattr->ia_gid;
87 if (ia_valid & ATTR_ATIME)
88 sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
89 inode->i_sb->s_time_gran);
90 if (ia_valid & ATTR_MTIME)
91 sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
92 inode->i_sb->s_time_gran);
93 if (ia_valid & ATTR_CTIME)
94 sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
95 inode->i_sb->s_time_gran);
96 if (ia_valid & ATTR_MODE) {
97 umode_t mode = iattr->ia_mode;
98
99 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
100 mode &= ~S_ISGID;
Maneesh Soni9ca1eb32005-07-29 12:14:19 -0700101 sd_iattr->ia_mode = sd->s_mode = mode;
Maneesh Soni988d1862005-05-31 10:39:14 +0530102 }
103
104 return error;
105}
106
Maneesh Soni82155342005-05-31 10:39:52 +0530107static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
108{
109 inode->i_mode = mode;
110 inode->i_uid = 0;
111 inode->i_gid = 0;
112 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
113}
114
115static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
116{
117 inode->i_mode = iattr->ia_mode;
118 inode->i_uid = iattr->ia_uid;
119 inode->i_gid = iattr->ia_gid;
120 inode->i_atime = iattr->ia_atime;
121 inode->i_mtime = iattr->ia_mtime;
122 inode->i_ctime = iattr->ia_ctime;
123}
124
Arjan van de Ven232ba9d2006-07-12 09:03:06 -0700125
126/*
127 * sysfs has a different i_mutex lock order behavior for i_mutex than other
128 * filesystems; sysfs i_mutex is called in many places with subsystem locks
129 * held. At the same time, many of the VFS locking rules do not apply to
130 * sysfs at all (cross directory rename for example). To untangle this mess
131 * (which gives false positives in lockdep), we're giving sysfs inodes their
132 * own class for i_mutex.
133 */
134static struct lock_class_key sysfs_inode_imutex_key;
135
Tejun Heofc9f54b2007-06-14 03:45:17 +0900136void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Tejun Heofc9f54b2007-06-14 03:45:17 +0900138 inode->i_blocks = 0;
139 inode->i_mapping->a_ops = &sysfs_aops;
140 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
141 inode->i_op = &sysfs_inode_operations;
142 inode->i_ino = sd->s_ino;
143 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
Maneesh Soni82155342005-05-31 10:39:52 +0530144
Tejun Heofc9f54b2007-06-14 03:45:17 +0900145 if (sd->s_iattr) {
146 /* sysfs_dirent has non-default attributes
147 * get them for the new inode from persistent copy
148 * in sysfs_dirent
149 */
150 set_inode_attr(inode, sd->s_iattr);
151 } else
152 set_default_inode_attr(inode, sd->s_mode);
153}
154
155/**
Tejun Heo8312a8d2007-06-14 03:45:17 +0900156 * sysfs_get_inode - get inode for sysfs_dirent
Tejun Heofc9f54b2007-06-14 03:45:17 +0900157 * @sd: sysfs_dirent to allocate inode for
158 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900159 * Get inode for @sd. If such inode doesn't exist, a new inode
160 * is allocated and basics are initialized. New inode is
161 * returned locked.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900162 *
163 * LOCKING:
164 * Kernel thread context (may sleep).
165 *
166 * RETURNS:
167 * Pointer to allocated inode on success, NULL on failure.
168 */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900169struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
Tejun Heofc9f54b2007-06-14 03:45:17 +0900170{
171 struct inode *inode;
172
Tejun Heo8312a8d2007-06-14 03:45:17 +0900173 inode = iget_locked(sysfs_sb, sd->s_ino);
174 if (inode && (inode->i_state & I_NEW))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900175 sysfs_init_inode(sd, inode);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return inode;
178}
179
Tejun Heofc9f54b2007-06-14 03:45:17 +0900180/**
181 * sysfs_instantiate - instantiate dentry
182 * @dentry: dentry to be instantiated
183 * @inode: inode associated with @sd
184 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900185 * Unlock @inode if locked and instantiate @dentry with @inode.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900186 *
187 * LOCKING:
188 * None.
189 */
190void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Tejun Heofc9f54b2007-06-14 03:45:17 +0900192 BUG_ON(!dentry || dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Tejun Heo8312a8d2007-06-14 03:45:17 +0900194 if (inode->i_state & I_NEW) {
195 unlock_new_inode(inode);
196
197 if (dentry->d_parent && dentry->d_parent->d_inode) {
198 struct inode *p_inode = dentry->d_parent->d_inode;
199 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
200 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900201 }
202
203 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Tejun Heodbde0fc2007-06-14 03:45:16 +0900206/**
207 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
208 * @sd: target sysfs_dirent
209 *
210 * Drop dentry for @sd. @sd must have been unlinked from its
211 * parent on entry to this function such that it can't be looked
212 * up anymore.
213 *
214 * @sd->s_dentry which is protected with sysfs_lock points to the
215 * currently associated dentry but we're not holding a reference
216 * to it and racing with dput(). Grab dcache_lock and verify
217 * dentry before dropping it. If @sd->s_dentry is NULL or dput()
218 * beats us, no need to bother.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Tejun Heodbde0fc2007-06-14 03:45:16 +0900220void sysfs_drop_dentry(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Tejun Heod0bcb562007-06-14 04:27:21 +0900222 struct dentry *dentry = NULL;
Tejun Heodbde0fc2007-06-14 03:45:16 +0900223 struct timespec curtime;
Tejun Heod0bcb562007-06-14 04:27:21 +0900224 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heodd14cbc2007-06-11 14:04:01 +0900226 /* We're not holding a reference to ->s_dentry dentry but the
227 * field will stay valid as long as sysfs_lock is held.
228 */
229 spin_lock(&sysfs_lock);
230 spin_lock(&dcache_lock);
231
Tejun Heod0bcb562007-06-14 04:27:21 +0900232 /* drop dentry if it's there and dput() didn't kill it yet */
Tejun Heodbde0fc2007-06-14 03:45:16 +0900233 if (sd->s_dentry && sd->s_dentry->d_inode) {
Tejun Heodd14cbc2007-06-11 14:04:01 +0900234 dentry = dget_locked(sd->s_dentry);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900235 spin_lock(&dentry->d_lock);
236 __d_drop(dentry);
237 spin_unlock(&dentry->d_lock);
238 }
Tejun Heodd14cbc2007-06-11 14:04:01 +0900239
240 spin_unlock(&dcache_lock);
241 spin_unlock(&sysfs_lock);
242
Tejun Heod0bcb562007-06-14 04:27:21 +0900243 dput(dentry);
244 /* XXX: unpin if directory, this will go away soon */
Tejun Heob402d722007-06-14 04:27:21 +0900245 if (sysfs_type(sd) == SYSFS_DIR)
Tejun Heod0bcb562007-06-14 04:27:21 +0900246 dput(dentry);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900247
Tejun Heodbde0fc2007-06-14 03:45:16 +0900248 /* adjust nlink and update timestamp */
Tejun Heodbde0fc2007-06-14 03:45:16 +0900249 curtime = CURRENT_TIME;
250
Tejun Heod0bcb562007-06-14 04:27:21 +0900251 inode = ilookup(sysfs_sb, sd->s_ino);
252 if (inode) {
253 mutex_lock(&inode->i_mutex);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900254
Tejun Heod0bcb562007-06-14 04:27:21 +0900255 inode->i_ctime = curtime;
256 drop_nlink(inode);
Tejun Heob402d722007-06-14 04:27:21 +0900257 if (sysfs_type(sd) == SYSFS_DIR)
Tejun Heod0bcb562007-06-14 04:27:21 +0900258 drop_nlink(inode);
259
260 mutex_unlock(&inode->i_mutex);
261 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Tejun Heodbde0fc2007-06-14 03:45:16 +0900263
Tejun Heod0bcb562007-06-14 04:27:21 +0900264 /* adjust nlink and udpate timestamp of the parent */
265 inode = ilookup(sysfs_sb, sd->s_parent->s_ino);
266 if (inode) {
267 mutex_lock(&inode->i_mutex);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900268
Tejun Heod0bcb562007-06-14 04:27:21 +0900269 inode->i_ctime = inode->i_mtime = curtime;
Tejun Heob402d722007-06-14 04:27:21 +0900270 if (sysfs_type(sd) == SYSFS_DIR)
Tejun Heod0bcb562007-06-14 04:27:21 +0900271 drop_nlink(inode);
272
273 mutex_unlock(&inode->i_mutex);
274 iput(inode);
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Tejun Heo608e2662007-06-14 04:27:22 +0900278int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Tejun Heo608e2662007-06-14 04:27:22 +0900280 struct dentry *dir;
Tejun Heo0c73f182007-06-14 03:45:18 +0900281 struct sysfs_dirent **pos, *sd;
Randy.Dunlap995982c2006-07-10 23:05:25 -0700282 int found = 0;
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800283
Tejun Heo608e2662007-06-14 04:27:22 +0900284 if (!dir_sd)
Randy.Dunlap995982c2006-07-10 23:05:25 -0700285 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Tejun Heo608e2662007-06-14 04:27:22 +0900287 dir = dir_sd->s_dentry;
288
James Bottomley36676bc2005-08-26 18:34:17 -0700289 if (dir->d_inode == NULL)
290 /* no inode means this hasn't been made visible yet */
Randy.Dunlap995982c2006-07-10 23:05:25 -0700291 return -ENOENT;
James Bottomley36676bc2005-08-26 18:34:17 -0700292
Frederik Deweerdtd3fc3732007-01-05 12:04:33 -0800293 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
Tejun Heo608e2662007-06-14 04:27:22 +0900294 for (pos = &dir_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
Tejun Heo0c73f182007-06-14 03:45:18 +0900295 sd = *pos;
296
Tejun Heob402d722007-06-14 04:27:21 +0900297 if (!sysfs_type(sd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 continue;
Tejun Heo0c096b52007-06-14 03:45:15 +0900299 if (!strcmp(sd->s_name, name)) {
Tejun Heo380e6fb2007-06-14 04:27:22 +0900300 sd->s_flags |= SYSFS_FLAG_REMOVED;
Tejun Heo0c73f182007-06-14 03:45:18 +0900301 *pos = sd->s_sibling;
302 sd->s_sibling = NULL;
Randy.Dunlap995982c2006-07-10 23:05:25 -0700303 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 }
306 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800307 mutex_unlock(&dir->d_inode->i_mutex);
Randy.Dunlap995982c2006-07-10 23:05:25 -0700308
Tejun Heo0ab66082007-06-14 03:45:16 +0900309 if (!found)
310 return -ENOENT;
311
Tejun Heodbde0fc2007-06-14 03:45:16 +0900312 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900313 sysfs_deactivate(sd);
314 sysfs_put(sd);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900315
Tejun Heo0ab66082007-06-14 03:45:16 +0900316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}