sysfs: reimplement sysfs_drop_dentry()
[linux-2.6] / fs / sysfs / inode.c
1 /*
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>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <asm/semaphore.h>
18 #include "sysfs.h"
19
20 extern struct super_block * sysfs_sb;
21
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
26 };
27
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,
31 };
32
33 static const struct inode_operations sysfs_inode_operations ={
34         .setattr        = sysfs_setattr,
35 };
36
37 void 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
47 int 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 */
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;
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;
101                 sd_iattr->ia_mode = sd->s_mode = mode;
102         }
103
104         return error;
105 }
106
107 static 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
115 static 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
125
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  */
134 static struct lock_class_key sysfs_inode_imutex_key;
135
136 struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd)
137 {
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);
146
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
151                          */
152                         set_inode_attr(inode, sd->s_iattr);
153                 } else
154                         set_default_inode_attr(inode, mode);
155         }
156         return inode;
157 }
158
159 int sysfs_create(struct sysfs_dirent *sd, struct dentry *dentry, int mode,
160                  int (*init)(struct inode *))
161 {
162         int error = 0;
163         struct inode * inode = NULL;
164         if (dentry) {
165                 if (!dentry->d_inode) {
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;
170                                 }
171                                 goto Proceed;
172                         }
173                         else 
174                                 error = -ENOMEM;
175                 } else
176                         error = -EEXIST;
177         } else 
178                 error = -ENOENT;
179         goto Done;
180
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;
192 }
193
194 /**
195  *      sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
196  *      @sd: target sysfs_dirent
197  *
198  *      Drop dentry for @sd.  @sd must have been unlinked from its
199  *      parent on entry to this function such that it can't be looked
200  *      up anymore.
201  *
202  *      @sd->s_dentry which is protected with sysfs_lock points to the
203  *      currently associated dentry but we're not holding a reference
204  *      to it and racing with dput().  Grab dcache_lock and verify
205  *      dentry before dropping it.  If @sd->s_dentry is NULL or dput()
206  *      beats us, no need to bother.
207  */
208 void sysfs_drop_dentry(struct sysfs_dirent *sd)
209 {
210         struct dentry *dentry = NULL, *parent = NULL;
211         struct inode *dir;
212         struct timespec curtime;
213
214         /* We're not holding a reference to ->s_dentry dentry but the
215          * field will stay valid as long as sysfs_lock is held.
216          */
217         spin_lock(&sysfs_lock);
218         spin_lock(&dcache_lock);
219
220         if (sd->s_dentry && sd->s_dentry->d_inode) {
221                 /* get dentry if it's there and dput() didn't kill it yet */
222                 dentry = dget_locked(sd->s_dentry);
223                 parent = dentry->d_parent;
224         } else if (sd->s_parent->s_dentry->d_inode) {
225                 /* We need to update the parent even if dentry for the
226                  * victim itself doesn't exist.
227                  */
228                 parent = dget_locked(sd->s_parent->s_dentry);
229         }
230
231         /* drop */
232         if (dentry) {
233                 spin_lock(&dentry->d_lock);
234                 __d_drop(dentry);
235                 spin_unlock(&dentry->d_lock);
236         }
237
238         spin_unlock(&dcache_lock);
239         spin_unlock(&sysfs_lock);
240
241         /* nothing to do if the parent isn't in dcache */
242         if (!parent)
243                 return;
244
245         /* adjust nlink and update timestamp */
246         dir = parent->d_inode;
247         mutex_lock(&dir->i_mutex);
248
249         curtime = CURRENT_TIME;
250
251         dir->i_ctime = dir->i_mtime = curtime;
252
253         if (dentry) {
254                 dentry->d_inode->i_ctime = curtime;
255                 drop_nlink(dentry->d_inode);
256                 if (sd->s_type & SYSFS_DIR) {
257                         drop_nlink(dentry->d_inode);
258                         drop_nlink(dir);
259                         /* XXX: unpin if directory, this will go away soon */
260                         dput(dentry);
261                 }
262         }
263
264         mutex_unlock(&dir->i_mutex);
265
266         /* bye bye */
267         if (dentry)
268                 dput(dentry);
269         else
270                 dput(parent);
271 }
272
273 int sysfs_hash_and_remove(struct dentry * dir, const char * name)
274 {
275         struct sysfs_dirent * sd;
276         struct sysfs_dirent * parent_sd;
277         int found = 0;
278
279         if (!dir)
280                 return -ENOENT;
281
282         if (dir->d_inode == NULL)
283                 /* no inode means this hasn't been made visible yet */
284                 return -ENOENT;
285
286         parent_sd = dir->d_fsdata;
287         mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
288         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
289                 if (!sd->s_type)
290                         continue;
291                 if (!strcmp(sd->s_name, name)) {
292                         list_del_init(&sd->s_sibling);
293                         found = 1;
294                         break;
295                 }
296         }
297         mutex_unlock(&dir->d_inode->i_mutex);
298
299         if (!found)
300                 return -ENOENT;
301
302         sysfs_drop_dentry(sd);
303         sysfs_deactivate(sd);
304         sysfs_put(sd);
305
306         return 0;
307 }