[PATCH] sysfs: sysfs_remove_dir() needs to invalidate the dentry
[linux-2.6.git] / fs / sysfs / dir.c
1 /*
2  * dir.c - Operations for sysfs directories.
3  */
4
5 #undef DEBUG
6
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/module.h>
10 #include <linux/kobject.h>
11 #include <linux/namei.h>
12 #include "sysfs.h"
13
14 DECLARE_RWSEM(sysfs_rename_sem);
15
16 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
17 {
18         struct sysfs_dirent * sd = dentry->d_fsdata;
19
20         if (sd) {
21                 BUG_ON(sd->s_dentry != dentry);
22                 sd->s_dentry = NULL;
23                 sysfs_put(sd);
24         }
25         iput(inode);
26 }
27
28 static struct dentry_operations sysfs_dentry_ops = {
29         .d_iput         = sysfs_d_iput,
30 };
31
32 /*
33  * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
34  */
35 static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent * parent_sd,
36                                                 void * element)
37 {
38         struct sysfs_dirent * sd;
39
40         sd = kmem_cache_alloc(sysfs_dir_cachep, GFP_KERNEL);
41         if (!sd)
42                 return NULL;
43
44         memset(sd, 0, sizeof(*sd));
45         atomic_set(&sd->s_count, 1);
46         INIT_LIST_HEAD(&sd->s_children);
47         list_add(&sd->s_sibling, &parent_sd->s_children);
48         sd->s_element = element;
49
50         return sd;
51 }
52
53 int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
54                         void * element, umode_t mode, int type)
55 {
56         struct sysfs_dirent * sd;
57
58         sd = sysfs_new_dirent(parent_sd, element);
59         if (!sd)
60                 return -ENOMEM;
61
62         sd->s_mode = mode;
63         sd->s_type = type;
64         sd->s_dentry = dentry;
65         if (dentry) {
66                 dentry->d_fsdata = sysfs_get(sd);
67                 dentry->d_op = &sysfs_dentry_ops;
68         }
69
70         return 0;
71 }
72
73 static int init_dir(struct inode * inode)
74 {
75         inode->i_op = &sysfs_dir_inode_operations;
76         inode->i_fop = &sysfs_dir_operations;
77
78         /* directory inodes start off with i_nlink == 2 (for "." entry) */
79         inode->i_nlink++;
80         return 0;
81 }
82
83 static int init_file(struct inode * inode)
84 {
85         inode->i_size = PAGE_SIZE;
86         inode->i_fop = &sysfs_file_operations;
87         return 0;
88 }
89
90 static int init_symlink(struct inode * inode)
91 {
92         inode->i_op = &sysfs_symlink_inode_operations;
93         return 0;
94 }
95
96 static int create_dir(struct kobject * k, struct dentry * p,
97                       const char * n, struct dentry ** d)
98 {
99         int error;
100         umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
101
102         mutex_lock(&p->d_inode->i_mutex);
103         *d = lookup_one_len(n, p, strlen(n));
104         if (!IS_ERR(*d)) {
105                 error = sysfs_make_dirent(p->d_fsdata, *d, k, mode, SYSFS_DIR);
106                 if (!error) {
107                         error = sysfs_create(*d, mode, init_dir);
108                         if (!error) {
109                                 p->d_inode->i_nlink++;
110                                 (*d)->d_op = &sysfs_dentry_ops;
111                                 d_rehash(*d);
112                         }
113                 }
114                 if (error && (error != -EEXIST)) {
115                         struct sysfs_dirent *sd = (*d)->d_fsdata;
116                         if (sd) {
117                                 list_del_init(&sd->s_sibling);
118                                 sysfs_put(sd);
119                         }
120                         d_drop(*d);
121                 }
122                 dput(*d);
123         } else
124                 error = PTR_ERR(*d);
125         mutex_unlock(&p->d_inode->i_mutex);
126         return error;
127 }
128
129
130 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
131 {
132         return create_dir(k,k->dentry,n,d);
133 }
134
135 /**
136  *      sysfs_create_dir - create a directory for an object.
137  *      @parent:        parent parent object.
138  *      @kobj:          object we're creating directory for. 
139  */
140
141 int sysfs_create_dir(struct kobject * kobj)
142 {
143         struct dentry * dentry = NULL;
144         struct dentry * parent;
145         int error = 0;
146
147         BUG_ON(!kobj);
148
149         if (kobj->parent)
150                 parent = kobj->parent->dentry;
151         else if (sysfs_mount && sysfs_mount->mnt_sb)
152                 parent = sysfs_mount->mnt_sb->s_root;
153         else
154                 return -EFAULT;
155
156         error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
157         if (!error)
158                 kobj->dentry = dentry;
159         return error;
160 }
161
162 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
163  * attribute file
164  */
165 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
166 {
167         struct attribute * attr = NULL;
168         struct bin_attribute * bin_attr = NULL;
169         int (* init) (struct inode *) = NULL;
170         int error = 0;
171
172         if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
173                 bin_attr = sd->s_element;
174                 attr = &bin_attr->attr;
175         } else {
176                 attr = sd->s_element;
177                 init = init_file;
178         }
179
180         dentry->d_fsdata = sysfs_get(sd);
181         sd->s_dentry = dentry;
182         error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
183         if (error) {
184                 sysfs_put(sd);
185                 return error;
186         }
187
188         if (bin_attr) {
189                 dentry->d_inode->i_size = bin_attr->size;
190                 dentry->d_inode->i_fop = &bin_fops;
191         }
192         dentry->d_op = &sysfs_dentry_ops;
193         d_rehash(dentry);
194
195         return 0;
196 }
197
198 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
199 {
200         int err = 0;
201
202         dentry->d_fsdata = sysfs_get(sd);
203         sd->s_dentry = dentry;
204         err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
205         if (!err) {
206                 dentry->d_op = &sysfs_dentry_ops;
207                 d_rehash(dentry);
208         } else
209                 sysfs_put(sd);
210
211         return err;
212 }
213
214 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
215                                 struct nameidata *nd)
216 {
217         struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
218         struct sysfs_dirent * sd;
219         int err = 0;
220
221         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
222                 if (sd->s_type & SYSFS_NOT_PINNED) {
223                         const unsigned char * name = sysfs_get_name(sd);
224
225                         if (strcmp(name, dentry->d_name.name))
226                                 continue;
227
228                         if (sd->s_type & SYSFS_KOBJ_LINK)
229                                 err = sysfs_attach_link(sd, dentry);
230                         else
231                                 err = sysfs_attach_attr(sd, dentry);
232                         break;
233                 }
234         }
235
236         return ERR_PTR(err);
237 }
238
239 struct inode_operations sysfs_dir_inode_operations = {
240         .lookup         = sysfs_lookup,
241         .setattr        = sysfs_setattr,
242 };
243
244 static void remove_dir(struct dentry * d)
245 {
246         struct dentry * parent = dget(d->d_parent);
247         struct sysfs_dirent * sd;
248
249         mutex_lock(&parent->d_inode->i_mutex);
250         d_delete(d);
251         sd = d->d_fsdata;
252         list_del_init(&sd->s_sibling);
253         sysfs_put(sd);
254         if (d->d_inode)
255                 simple_rmdir(parent->d_inode,d);
256
257         pr_debug(" o %s removing done (%d)\n",d->d_name.name,
258                  atomic_read(&d->d_count));
259
260         mutex_unlock(&parent->d_inode->i_mutex);
261         dput(parent);
262 }
263
264 void sysfs_remove_subdir(struct dentry * d)
265 {
266         remove_dir(d);
267 }
268
269
270 /**
271  *      sysfs_remove_dir - remove an object's directory.
272  *      @kobj:  object. 
273  *
274  *      The only thing special about this is that we remove any files in 
275  *      the directory before we remove the directory, and we've inlined
276  *      what used to be sysfs_rmdir() below, instead of calling separately.
277  */
278
279 void sysfs_remove_dir(struct kobject * kobj)
280 {
281         struct dentry * dentry = dget(kobj->dentry);
282         struct sysfs_dirent * parent_sd;
283         struct sysfs_dirent * sd, * tmp;
284
285         if (!dentry)
286                 return;
287
288         pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
289         mutex_lock(&dentry->d_inode->i_mutex);
290         parent_sd = dentry->d_fsdata;
291         list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
292                 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
293                         continue;
294                 list_del_init(&sd->s_sibling);
295                 sysfs_drop_dentry(sd, dentry);
296                 sysfs_put(sd);
297         }
298         mutex_unlock(&dentry->d_inode->i_mutex);
299
300         remove_dir(dentry);
301         /**
302          * Drop reference from dget() on entrance.
303          */
304         dput(dentry);
305         kobj->dentry = NULL;
306 }
307
308 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
309 {
310         int error = 0;
311         struct dentry * new_dentry, * parent;
312
313         if (!strcmp(kobject_name(kobj), new_name))
314                 return -EINVAL;
315
316         if (!kobj->parent)
317                 return -EINVAL;
318
319         down_write(&sysfs_rename_sem);
320         parent = kobj->parent->dentry;
321
322         mutex_lock(&parent->d_inode->i_mutex);
323
324         new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
325         if (!IS_ERR(new_dentry)) {
326                 if (!new_dentry->d_inode) {
327                         error = kobject_set_name(kobj, "%s", new_name);
328                         if (!error) {
329                                 d_add(new_dentry, NULL);
330                                 d_move(kobj->dentry, new_dentry);
331                         }
332                         else
333                                 d_drop(new_dentry);
334                 } else
335                         error = -EEXIST;
336                 dput(new_dentry);
337         }
338         mutex_unlock(&parent->d_inode->i_mutex);
339         up_write(&sysfs_rename_sem);
340
341         return error;
342 }
343
344 static int sysfs_dir_open(struct inode *inode, struct file *file)
345 {
346         struct dentry * dentry = file->f_dentry;
347         struct sysfs_dirent * parent_sd = dentry->d_fsdata;
348
349         mutex_lock(&dentry->d_inode->i_mutex);
350         file->private_data = sysfs_new_dirent(parent_sd, NULL);
351         mutex_unlock(&dentry->d_inode->i_mutex);
352
353         return file->private_data ? 0 : -ENOMEM;
354
355 }
356
357 static int sysfs_dir_close(struct inode *inode, struct file *file)
358 {
359         struct dentry * dentry = file->f_dentry;
360         struct sysfs_dirent * cursor = file->private_data;
361
362         mutex_lock(&dentry->d_inode->i_mutex);
363         list_del_init(&cursor->s_sibling);
364         mutex_unlock(&dentry->d_inode->i_mutex);
365
366         release_sysfs_dirent(cursor);
367
368         return 0;
369 }
370
371 /* Relationship between s_mode and the DT_xxx types */
372 static inline unsigned char dt_type(struct sysfs_dirent *sd)
373 {
374         return (sd->s_mode >> 12) & 15;
375 }
376
377 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
378 {
379         struct dentry *dentry = filp->f_dentry;
380         struct sysfs_dirent * parent_sd = dentry->d_fsdata;
381         struct sysfs_dirent *cursor = filp->private_data;
382         struct list_head *p, *q = &cursor->s_sibling;
383         ino_t ino;
384         int i = filp->f_pos;
385
386         switch (i) {
387                 case 0:
388                         ino = dentry->d_inode->i_ino;
389                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
390                                 break;
391                         filp->f_pos++;
392                         i++;
393                         /* fallthrough */
394                 case 1:
395                         ino = parent_ino(dentry);
396                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
397                                 break;
398                         filp->f_pos++;
399                         i++;
400                         /* fallthrough */
401                 default:
402                         if (filp->f_pos == 2) {
403                                 list_del(q);
404                                 list_add(q, &parent_sd->s_children);
405                         }
406                         for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
407                                 struct sysfs_dirent *next;
408                                 const char * name;
409                                 int len;
410
411                                 next = list_entry(p, struct sysfs_dirent,
412                                                    s_sibling);
413                                 if (!next->s_element)
414                                         continue;
415
416                                 name = sysfs_get_name(next);
417                                 len = strlen(name);
418                                 if (next->s_dentry)
419                                         ino = next->s_dentry->d_inode->i_ino;
420                                 else
421                                         ino = iunique(sysfs_sb, 2);
422
423                                 if (filldir(dirent, name, len, filp->f_pos, ino,
424                                                  dt_type(next)) < 0)
425                                         return 0;
426
427                                 list_del(q);
428                                 list_add(q, p);
429                                 p = q;
430                                 filp->f_pos++;
431                         }
432         }
433         return 0;
434 }
435
436 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
437 {
438         struct dentry * dentry = file->f_dentry;
439
440         mutex_lock(&dentry->d_inode->i_mutex);
441         switch (origin) {
442                 case 1:
443                         offset += file->f_pos;
444                 case 0:
445                         if (offset >= 0)
446                                 break;
447                 default:
448                         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
449                         return -EINVAL;
450         }
451         if (offset != file->f_pos) {
452                 file->f_pos = offset;
453                 if (file->f_pos >= 2) {
454                         struct sysfs_dirent *sd = dentry->d_fsdata;
455                         struct sysfs_dirent *cursor = file->private_data;
456                         struct list_head *p;
457                         loff_t n = file->f_pos - 2;
458
459                         list_del(&cursor->s_sibling);
460                         p = sd->s_children.next;
461                         while (n && p != &sd->s_children) {
462                                 struct sysfs_dirent *next;
463                                 next = list_entry(p, struct sysfs_dirent,
464                                                    s_sibling);
465                                 if (next->s_element)
466                                         n--;
467                                 p = p->next;
468                         }
469                         list_add_tail(&cursor->s_sibling, p);
470                 }
471         }
472         mutex_unlock(&dentry->d_inode->i_mutex);
473         return offset;
474 }
475
476 struct file_operations sysfs_dir_operations = {
477         .open           = sysfs_dir_open,
478         .release        = sysfs_dir_close,
479         .llseek         = sysfs_dir_lseek,
480         .read           = generic_read_dir,
481         .readdir        = sysfs_readdir,
482 };
483
484 EXPORT_SYMBOL_GPL(sysfs_create_dir);
485 EXPORT_SYMBOL_GPL(sysfs_remove_dir);
486 EXPORT_SYMBOL_GPL(sysfs_rename_dir);