v2.6.22.24-op1
[linux-2.6.22.y-op.git] / fs / sysfs / inode.c
blob5266eec15f6e35766c84939f1c7663690d6c3930
1 /*
2 * inode.c - basic inode and dentry operations.
4 * sysfs is Copyright (c) 2001-3 Patrick Mochel
6 * Please see Documentation/filesystems/sysfs.txt for more information.
7 */
9 #undef DEBUG
11 #include <linux/pagemap.h>
12 #include <linux/namei.h>
13 #include <linux/backing-dev.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <asm/semaphore.h>
18 #include "sysfs.h"
20 extern struct super_block * sysfs_sb;
22 static const struct address_space_operations sysfs_aops = {
23 .readpage = simple_readpage,
24 .prepare_write = simple_prepare_write,
25 .commit_write = simple_commit_write
28 static 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,
33 static const struct inode_operations sysfs_inode_operations ={
34 .setattr = sysfs_setattr,
37 void sysfs_delete_inode(struct inode *inode)
39 /* Free the shadowed directory inode operations */
40 if (sysfs_is_shadowed_inode(inode)) {
41 kfree(inode->i_op);
42 inode->i_op = NULL;
44 return generic_delete_inode(inode);
47 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
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;
55 if (!sd)
56 return -EINVAL;
58 sd_iattr = sd->s_iattr;
60 error = inode_change_ok(inode, iattr);
61 if (error)
62 return error;
64 error = inode_setattr(inode, iattr);
65 if (error)
66 return error;
68 if (!sd_iattr) {
69 /* setting attributes for the first time, allocate now */
70 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
71 if (!sd_iattr)
72 return -ENOMEM;
73 /* assign default attributes */
74 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;
81 /* attributes were changed atleast once in past */
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;
99 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
100 mode &= ~S_ISGID;
101 sd_iattr->ia_mode = sd->s_mode = mode;
104 return error;
107 static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
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;
115 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
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;
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.
134 static struct lock_class_key sysfs_inode_imutex_key;
136 struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd)
138 struct inode * inode = new_inode(sysfs_sb);
139 if (inode) {
140 inode->i_blocks = 0;
141 inode->i_mapping->a_ops = &sysfs_aops;
142 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
143 inode->i_op = &sysfs_inode_operations;
144 inode->i_ino = sd->s_ino;
145 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
147 if (sd->s_iattr) {
148 /* sysfs_dirent has non-default attributes
149 * get them for the new inode from persistent copy
150 * in sysfs_dirent
152 set_inode_attr(inode, sd->s_iattr);
153 } else
154 set_default_inode_attr(inode, mode);
156 return inode;
159 int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
161 int error = 0;
162 struct inode * inode = NULL;
163 if (dentry) {
164 if (!dentry->d_inode) {
165 struct sysfs_dirent * sd = dentry->d_fsdata;
166 if ((inode = sysfs_new_inode(mode, sd))) {
167 if (dentry->d_parent && dentry->d_parent->d_inode) {
168 struct inode *p_inode = dentry->d_parent->d_inode;
169 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
171 goto Proceed;
173 else
174 error = -ENOMEM;
175 } else
176 error = -EEXIST;
177 } else
178 error = -ENOENT;
179 goto Done;
181 Proceed:
182 if (init)
183 error = init(inode);
184 if (!error) {
185 d_instantiate(dentry, inode);
186 if (S_ISDIR(mode))
187 dget(dentry); /* pin only directory dentry in core */
188 } else
189 iput(inode);
190 Done:
191 return error;
195 * Get the name for corresponding element represented by the given sysfs_dirent
197 const unsigned char * sysfs_get_name(struct sysfs_dirent *sd)
199 struct attribute * attr;
200 struct bin_attribute * bin_attr;
201 struct sysfs_symlink * sl;
203 BUG_ON(!sd || !sd->s_element);
205 switch (sd->s_type) {
206 case SYSFS_DIR:
207 /* Always have a dentry so use that */
208 return sd->s_dentry->d_name.name;
210 case SYSFS_KOBJ_ATTR:
211 attr = sd->s_element;
212 return attr->name;
214 case SYSFS_KOBJ_BIN_ATTR:
215 bin_attr = sd->s_element;
216 return bin_attr->attr.name;
218 case SYSFS_KOBJ_LINK:
219 sl = sd->s_element;
220 return sl->link_name;
222 return NULL;
225 static inline void orphan_all_buffers(struct inode *node)
227 struct sysfs_buffer_collection *set;
228 struct sysfs_buffer *buf;
230 mutex_lock_nested(&node->i_mutex, I_MUTEX_CHILD);
231 set = node->i_private;
232 if (set) {
233 list_for_each_entry(buf, &set->associates, associates) {
234 down(&buf->sem);
235 buf->orphaned = 1;
236 up(&buf->sem);
239 mutex_unlock(&node->i_mutex);
244 * Unhashes the dentry corresponding to given sysfs_dirent
245 * Called with parent inode's i_mutex held.
247 void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent)
249 struct dentry *dentry = NULL;
250 struct inode *inode;
252 /* We're not holding a reference to ->s_dentry dentry but the
253 * field will stay valid as long as sysfs_lock is held.
255 spin_lock(&sysfs_lock);
256 spin_lock(&dcache_lock);
258 /* dget dentry if it's still alive */
259 if (sd->s_dentry && sd->s_dentry->d_inode)
260 dentry = dget_locked(sd->s_dentry);
262 spin_unlock(&dcache_lock);
263 spin_unlock(&sysfs_lock);
265 /* drop dentry */
266 if (dentry) {
267 spin_lock(&dcache_lock);
268 spin_lock(&dentry->d_lock);
269 if (!d_unhashed(dentry) && dentry->d_inode) {
270 inode = dentry->d_inode;
271 spin_lock(&inode->i_lock);
272 __iget(inode);
273 spin_unlock(&inode->i_lock);
274 dget_locked(dentry);
275 __d_drop(dentry);
276 spin_unlock(&dentry->d_lock);
277 spin_unlock(&dcache_lock);
278 simple_unlink(parent->d_inode, dentry);
279 orphan_all_buffers(inode);
280 iput(inode);
281 } else {
282 spin_unlock(&dentry->d_lock);
283 spin_unlock(&dcache_lock);
286 dput(dentry);
290 int sysfs_hash_and_remove(struct dentry * dir, const char * name)
292 struct sysfs_dirent * sd;
293 struct sysfs_dirent * parent_sd;
294 int found = 0;
296 if (!dir)
297 return -ENOENT;
299 if (dir->d_inode == NULL)
300 /* no inode means this hasn't been made visible yet */
301 return -ENOENT;
303 parent_sd = dir->d_fsdata;
304 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
305 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
306 if (!sd->s_element)
307 continue;
308 if (!strcmp(sysfs_get_name(sd), name)) {
309 list_del_init(&sd->s_sibling);
310 sysfs_drop_dentry(sd, dir);
311 sysfs_put(sd);
312 found = 1;
313 break;
316 mutex_unlock(&dir->d_inode->i_mutex);
318 return found ? 0 : -ENOENT;