switch ll_lookup_finish_locks() and ll_revalidate_it_finish() to inode
[deliverable/linux.git] / fs / configfs / dir.c
CommitLineData
7063fbf2
JB
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dir.c - Operations for configfs directories.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
20 *
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
23 *
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
25 */
26
27#undef DEBUG
28
29#include <linux/fs.h>
30#include <linux/mount.h>
31#include <linux/module.h>
32#include <linux/slab.h>
107ed40b 33#include <linux/err.h>
7063fbf2
JB
34
35#include <linux/configfs.h>
36#include "configfs_internal.h"
37
38DECLARE_RWSEM(configfs_rename_sem);
6f610764
LR
39/*
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
5301a77d 41 * Also protects mutations of symlinks linkage to target configfs_dirent
6f610764
LR
42 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
5301a77d
LR
44 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
b3e76af8
LR
46 *
47 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
48 * unlocked is not reliable unless in detach_groups() called from
49 * rmdir()/unregister() and from configfs_attach_group()
6f610764
LR
50 */
51DEFINE_SPINLOCK(configfs_dirent_lock);
7063fbf2
JB
52
53static void configfs_d_iput(struct dentry * dentry,
54 struct inode * inode)
55{
24307aa1 56 struct configfs_dirent *sd = dentry->d_fsdata;
7063fbf2
JB
57
58 if (sd) {
24307aa1
JB
59 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock);
76ae281f
JB
61 /* Coordinate with configfs_attach_attr where will increase
62 * sd->s_count and update sd->s_dentry to new allocated one.
63 * Only set sd->dentry to null when this dentry is the only
64 * sd owner.
65 * If not do so, configfs_d_iput may run just after
66 * configfs_attach_attr and set sd->s_dentry to null
67 * even it's still in use.
68 */
69 if (atomic_read(&sd->s_count) <= 2)
70 sd->s_dentry = NULL;
71
24307aa1 72 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
73 configfs_put(sd);
74 }
75 iput(inode);
76}
77
d463a0c4 78const struct dentry_operations configfs_dentry_ops = {
7063fbf2 79 .d_iput = configfs_d_iput,
b26d4cd3 80 .d_delete = always_delete_dentry,
7063fbf2
JB
81};
82
e74cc06d
LR
83#ifdef CONFIG_LOCKDEP
84
85/*
86 * Helpers to make lockdep happy with our recursive locking of default groups'
87 * inodes (see configfs_attach_group() and configfs_detach_group()).
88 * We put default groups i_mutexes in separate classes according to their depth
89 * from the youngest non-default group ancestor.
90 *
91 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
92 * groups A/B and A/C will have their inode's mutex in class
93 * default_group_class[0], and default group A/C/D will be in
94 * default_group_class[1].
95 *
96 * The lock classes are declared and assigned in inode.c, according to the
97 * s_depth value.
98 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
99 * default groups, and reset to -1 when all default groups are attached. During
100 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
101 * inode's mutex is set to default_group_class[s_depth - 1].
102 */
103
104static void configfs_init_dirent_depth(struct configfs_dirent *sd)
105{
106 sd->s_depth = -1;
107}
108
109static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
110 struct configfs_dirent *sd)
111{
112 int parent_depth = parent_sd->s_depth;
113
114 if (parent_depth >= 0)
115 sd->s_depth = parent_depth + 1;
116}
117
118static void
119configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
120{
121 /*
122 * item's i_mutex class is already setup, so s_depth is now only
123 * used to set new sub-directories s_depth, which is always done
124 * with item's i_mutex locked.
125 */
126 /*
127 * sd->s_depth == -1 iff we are a non default group.
128 * else (we are a default group) sd->s_depth > 0 (see
129 * create_dir()).
130 */
131 if (sd->s_depth == -1)
132 /*
133 * We are a non default group and we are going to create
134 * default groups.
135 */
136 sd->s_depth = 0;
137}
138
139static void
140configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
141{
142 /* We will not create default groups anymore. */
143 sd->s_depth = -1;
144}
145
146#else /* CONFIG_LOCKDEP */
147
148static void configfs_init_dirent_depth(struct configfs_dirent *sd)
149{
150}
151
152static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
153 struct configfs_dirent *sd)
154{
155}
156
157static void
158configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
159{
160}
161
162static void
163configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
164{
165}
166
167#endif /* CONFIG_LOCKDEP */
168
7063fbf2
JB
169/*
170 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
171 */
420118ca
LR
172static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
173 void *element, int type)
7063fbf2
JB
174{
175 struct configfs_dirent * sd;
176
c3762229 177 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
7063fbf2 178 if (!sd)
107ed40b 179 return ERR_PTR(-ENOMEM);
7063fbf2 180
7063fbf2
JB
181 atomic_set(&sd->s_count, 1);
182 INIT_LIST_HEAD(&sd->s_links);
183 INIT_LIST_HEAD(&sd->s_children);
7063fbf2 184 sd->s_element = element;
420118ca 185 sd->s_type = type;
e74cc06d 186 configfs_init_dirent_depth(sd);
6f610764 187 spin_lock(&configfs_dirent_lock);
b3e76af8
LR
188 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
189 spin_unlock(&configfs_dirent_lock);
190 kmem_cache_free(configfs_dir_cachep, sd);
191 return ERR_PTR(-ENOENT);
192 }
6f610764
LR
193 list_add(&sd->s_sibling, &parent_sd->s_children);
194 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
195
196 return sd;
197}
198
b4c98f62
JB
199/*
200 *
201 * Return -EEXIST if there is already a configfs element with the same
202 * name for the same parent.
203 *
204 * called with parent inode's i_mutex held
205 */
58d206c2
AB
206static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
207 const unsigned char *new)
b4c98f62
JB
208{
209 struct configfs_dirent * sd;
210
211 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
212 if (sd->s_element) {
213 const unsigned char *existing = configfs_get_name(sd);
214 if (strcmp(existing, new))
215 continue;
216 else
217 return -EEXIST;
218 }
219 }
220
221 return 0;
222}
223
224
7063fbf2
JB
225int configfs_make_dirent(struct configfs_dirent * parent_sd,
226 struct dentry * dentry, void * element,
227 umode_t mode, int type)
228{
229 struct configfs_dirent * sd;
230
420118ca 231 sd = configfs_new_dirent(parent_sd, element, type);
107ed40b
LR
232 if (IS_ERR(sd))
233 return PTR_ERR(sd);
7063fbf2
JB
234
235 sd->s_mode = mode;
7063fbf2 236 sd->s_dentry = dentry;
fbc8d4c0 237 if (dentry)
7063fbf2 238 dentry->d_fsdata = configfs_get(sd);
7063fbf2
JB
239
240 return 0;
241}
242
243static int init_dir(struct inode * inode)
244{
245 inode->i_op = &configfs_dir_inode_operations;
246 inode->i_fop = &configfs_dir_operations;
247
248 /* directory inodes start off with i_nlink == 2 (for "." entry) */
d8c76e6f 249 inc_nlink(inode);
7063fbf2
JB
250 return 0;
251}
252
ce8d2cdf 253static int configfs_init_file(struct inode * inode)
7063fbf2
JB
254{
255 inode->i_size = PAGE_SIZE;
256 inode->i_fop = &configfs_file_operations;
257 return 0;
258}
259
260static int init_symlink(struct inode * inode)
261{
262 inode->i_op = &configfs_symlink_inode_operations;
263 return 0;
264}
265
0dd6c08a 266static int create_dir(struct config_item *k, struct dentry *d)
7063fbf2
JB
267{
268 int error;
269 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
0dd6c08a
AV
270 struct dentry *p = d->d_parent;
271
272 BUG_ON(!k);
7063fbf2 273
b4c98f62
JB
274 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
275 if (!error)
276 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
2a109f2a 277 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
7063fbf2 278 if (!error) {
e74cc06d 279 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
3d0f89bb 280 error = configfs_create(d, mode, init_dir);
7063fbf2 281 if (!error) {
d8c76e6f 282 inc_nlink(p->d_inode);
3d0f89bb
JB
283 } else {
284 struct configfs_dirent *sd = d->d_fsdata;
285 if (sd) {
6f610764 286 spin_lock(&configfs_dirent_lock);
3d0f89bb 287 list_del_init(&sd->s_sibling);
6f610764 288 spin_unlock(&configfs_dirent_lock);
3d0f89bb
JB
289 configfs_put(sd);
290 }
7063fbf2
JB
291 }
292 }
293 return error;
294}
295
296
297/**
298 * configfs_create_dir - create a directory for an config_item.
299 * @item: config_itemwe're creating directory for.
300 * @dentry: config_item's dentry.
2a109f2a
LR
301 *
302 * Note: user-created entries won't be allowed under this new directory
303 * until it is validated by configfs_dir_set_ready()
7063fbf2
JB
304 */
305
306static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
307{
0dd6c08a 308 int error = create_dir(item, dentry);
7063fbf2
JB
309 if (!error)
310 item->ci_dentry = dentry;
311 return error;
312}
313
2a109f2a
LR
314/*
315 * Allow userspace to create new entries under a new directory created with
316 * configfs_create_dir(), and under all of its chidlren directories recursively.
317 * @sd configfs_dirent of the new directory to validate
318 *
319 * Caller must hold configfs_dirent_lock.
320 */
321static void configfs_dir_set_ready(struct configfs_dirent *sd)
322{
323 struct configfs_dirent *child_sd;
324
325 sd->s_type &= ~CONFIGFS_USET_CREATING;
326 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
327 if (child_sd->s_type & CONFIGFS_USET_CREATING)
328 configfs_dir_set_ready(child_sd);
329}
330
331/*
332 * Check that a directory does not belong to a directory hierarchy being
333 * attached and not validated yet.
334 * @sd configfs_dirent of the directory to check
335 *
336 * @return non-zero iff the directory was validated
337 *
338 * Note: takes configfs_dirent_lock, so the result may change from false to true
339 * in two consecutive calls, but never from true to false.
340 */
341int configfs_dirent_is_ready(struct configfs_dirent *sd)
342{
343 int ret;
344
345 spin_lock(&configfs_dirent_lock);
346 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
347 spin_unlock(&configfs_dirent_lock);
348
349 return ret;
350}
351
7063fbf2
JB
352int configfs_create_link(struct configfs_symlink *sl,
353 struct dentry *parent,
354 struct dentry *dentry)
355{
356 int err = 0;
357 umode_t mode = S_IFLNK | S_IRWXUGO;
358
3d0f89bb
JB
359 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
360 CONFIGFS_ITEM_LINK);
7063fbf2 361 if (!err) {
3d0f89bb 362 err = configfs_create(dentry, mode, init_symlink);
fbc8d4c0 363 if (err) {
3d0f89bb
JB
364 struct configfs_dirent *sd = dentry->d_fsdata;
365 if (sd) {
6f610764 366 spin_lock(&configfs_dirent_lock);
3d0f89bb 367 list_del_init(&sd->s_sibling);
6f610764 368 spin_unlock(&configfs_dirent_lock);
3d0f89bb
JB
369 configfs_put(sd);
370 }
371 }
7063fbf2
JB
372 }
373 return err;
374}
375
376static void remove_dir(struct dentry * d)
377{
378 struct dentry * parent = dget(d->d_parent);
379 struct configfs_dirent * sd;
380
381 sd = d->d_fsdata;
6f610764 382 spin_lock(&configfs_dirent_lock);
e7515d06 383 list_del_init(&sd->s_sibling);
6f610764 384 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
385 configfs_put(sd);
386 if (d->d_inode)
387 simple_rmdir(parent->d_inode,d);
388
a455589f 389 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
7063fbf2
JB
390
391 dput(parent);
392}
393
394/**
395 * configfs_remove_dir - remove an config_item's directory.
396 * @item: config_item we're removing.
397 *
398 * The only thing special about this is that we remove any files in
399 * the directory before we remove the directory, and we've inlined
400 * what used to be configfs_rmdir() below, instead of calling separately.
2e2ce171
LR
401 *
402 * Caller holds the mutex of the item's inode
7063fbf2
JB
403 */
404
405static void configfs_remove_dir(struct config_item * item)
406{
407 struct dentry * dentry = dget(item->ci_dentry);
408
409 if (!dentry)
410 return;
411
412 remove_dir(dentry);
413 /**
414 * Drop reference from dget() on entrance.
415 */
416 dput(dentry);
417}
418
419
420/* attaches attribute's configfs_dirent to the dentry corresponding to the
421 * attribute file
422 */
423static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
424{
425 struct configfs_attribute * attr = sd->s_element;
426 int error;
427
76ae281f 428 spin_lock(&configfs_dirent_lock);
3d0f89bb
JB
429 dentry->d_fsdata = configfs_get(sd);
430 sd->s_dentry = dentry;
76ae281f
JB
431 spin_unlock(&configfs_dirent_lock);
432
ce8d2cdf
DH
433 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
434 configfs_init_file);
3d0f89bb
JB
435 if (error) {
436 configfs_put(sd);
7063fbf2 437 return error;
3d0f89bb 438 }
7063fbf2 439
7063fbf2
JB
440 d_rehash(dentry);
441
442 return 0;
443}
444
445static struct dentry * configfs_lookup(struct inode *dir,
446 struct dentry *dentry,
00cd8dd3 447 unsigned int flags)
7063fbf2
JB
448{
449 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
450 struct configfs_dirent * sd;
451 int found = 0;
2a109f2a
LR
452 int err;
453
454 /*
455 * Fake invisibility if dir belongs to a group/default groups hierarchy
456 * being attached
457 *
458 * This forbids userspace to read/write attributes of items which may
459 * not complete their initialization, since the dentries of the
460 * attributes won't be instantiated.
461 */
462 err = -ENOENT;
463 if (!configfs_dirent_is_ready(parent_sd))
464 goto out;
7063fbf2
JB
465
466 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
467 if (sd->s_type & CONFIGFS_NOT_PINNED) {
468 const unsigned char * name = configfs_get_name(sd);
469
470 if (strcmp(name, dentry->d_name.name))
471 continue;
472
473 found = 1;
474 err = configfs_attach_attr(sd, dentry);
475 break;
476 }
477 }
478
479 if (!found) {
480 /*
481 * If it doesn't exist and it isn't a NOT_PINNED item,
482 * it must be negative.
483 */
fbc8d4c0
NP
484 if (dentry->d_name.len > NAME_MAX)
485 return ERR_PTR(-ENAMETOOLONG);
fbc8d4c0
NP
486 d_add(dentry, NULL);
487 return NULL;
7063fbf2
JB
488 }
489
2a109f2a 490out:
7063fbf2
JB
491 return ERR_PTR(err);
492}
493
494/*
495 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
b3e76af8
LR
496 * attributes and are removed by rmdir(). We recurse, setting
497 * CONFIGFS_USET_DROPPING on all children that are candidates for
498 * default detach.
499 * If there is an error, the caller will reset the flags via
500 * configfs_detach_rollback().
7063fbf2 501 */
6d8344ba 502static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
7063fbf2
JB
503{
504 struct configfs_dirent *parent_sd = dentry->d_fsdata;
505 struct configfs_dirent *sd;
506 int ret;
507
4768e9b1
LR
508 /* Mark that we're trying to drop the group */
509 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
510
7063fbf2
JB
511 ret = -EBUSY;
512 if (!list_empty(&parent_sd->s_links))
513 goto out;
514
515 ret = 0;
516 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
99cefda4
LR
517 if (!sd->s_element ||
518 (sd->s_type & CONFIGFS_NOT_PINNED))
7063fbf2
JB
519 continue;
520 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
6d8344ba
LR
521 /* Abort if racing with mkdir() */
522 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
523 if (wait_mutex)
524 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
525 return -EAGAIN;
526 }
7063fbf2 527
631d1feb
JB
528 /*
529 * Yup, recursive. If there's a problem, blame
530 * deep nesting of default_groups
531 */
6d8344ba 532 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
7063fbf2 533 if (!ret)
e7515d06 534 continue;
7063fbf2
JB
535 } else
536 ret = -ENOTEMPTY;
537
538 break;
539 }
540
541out:
542 return ret;
543}
544
545/*
b3e76af8 546 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
7063fbf2
JB
547 * set.
548 */
549static void configfs_detach_rollback(struct dentry *dentry)
550{
551 struct configfs_dirent *parent_sd = dentry->d_fsdata;
552 struct configfs_dirent *sd;
553
4768e9b1
LR
554 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
555
556 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
557 if (sd->s_type & CONFIGFS_USET_DEFAULT)
7063fbf2 558 configfs_detach_rollback(sd->s_dentry);
7063fbf2
JB
559}
560
561static void detach_attrs(struct config_item * item)
562{
563 struct dentry * dentry = dget(item->ci_dentry);
564 struct configfs_dirent * parent_sd;
565 struct configfs_dirent * sd, * tmp;
566
567 if (!dentry)
568 return;
569
570 pr_debug("configfs %s: dropping attrs for dir\n",
571 dentry->d_name.name);
572
573 parent_sd = dentry->d_fsdata;
574 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
575 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
576 continue;
6f610764 577 spin_lock(&configfs_dirent_lock);
7063fbf2 578 list_del_init(&sd->s_sibling);
6f610764 579 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
580 configfs_drop_dentry(sd, dentry);
581 configfs_put(sd);
582 }
583
584 /**
585 * Drop reference from dget() on entrance.
586 */
587 dput(dentry);
588}
589
590static int populate_attrs(struct config_item *item)
591{
592 struct config_item_type *t = item->ci_type;
593 struct configfs_attribute *attr;
594 int error = 0;
595 int i;
596
597 if (!t)
598 return -EINVAL;
599 if (t->ct_attrs) {
600 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
601 if ((error = configfs_create_file(item, attr)))
602 break;
603 }
604 }
605
606 if (error)
607 detach_attrs(item);
608
609 return error;
610}
611
612static int configfs_attach_group(struct config_item *parent_item,
613 struct config_item *item,
614 struct dentry *dentry);
615static void configfs_detach_group(struct config_item *item);
616
617static void detach_groups(struct config_group *group)
618{
619 struct dentry * dentry = dget(group->cg_item.ci_dentry);
620 struct dentry *child;
621 struct configfs_dirent *parent_sd;
622 struct configfs_dirent *sd, *tmp;
623
624 if (!dentry)
625 return;
626
627 parent_sd = dentry->d_fsdata;
628 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
629 if (!sd->s_element ||
630 !(sd->s_type & CONFIGFS_USET_DEFAULT))
631 continue;
632
633 child = sd->s_dentry;
634
b3e76af8
LR
635 mutex_lock(&child->d_inode->i_mutex);
636
7063fbf2
JB
637 configfs_detach_group(sd->s_element);
638 child->d_inode->i_flags |= S_DEAD;
d83c49f3 639 dont_mount(child);
7063fbf2 640
b3e76af8 641 mutex_unlock(&child->d_inode->i_mutex);
7063fbf2
JB
642
643 d_delete(child);
644 dput(child);
645 }
646
647 /**
648 * Drop reference from dget() on entrance.
649 */
650 dput(dentry);
651}
652
653/*
654 * This fakes mkdir(2) on a default_groups[] entry. It
655 * creates a dentry, attachs it, and then does fixup
656 * on the sd->s_type.
657 *
658 * We could, perhaps, tweak our parent's ->mkdir for a minute and
659 * try using vfs_mkdir. Just a thought.
660 */
661static int create_default_group(struct config_group *parent_group,
662 struct config_group *group)
663{
664 int ret;
7063fbf2
JB
665 struct configfs_dirent *sd;
666 /* We trust the caller holds a reference to parent */
667 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
668
669 if (!group->cg_item.ci_name)
670 group->cg_item.ci_name = group->cg_item.ci_namebuf;
7063fbf2
JB
671
672 ret = -ENOMEM;
ec193cf5 673 child = d_alloc_name(parent, group->cg_item.ci_name);
7063fbf2
JB
674 if (child) {
675 d_add(child, NULL);
676
677 ret = configfs_attach_group(&parent_group->cg_item,
678 &group->cg_item, child);
679 if (!ret) {
680 sd = child->d_fsdata;
681 sd->s_type |= CONFIGFS_USET_DEFAULT;
682 } else {
df7f9967
JB
683 BUG_ON(child->d_inode);
684 d_drop(child);
7063fbf2
JB
685 dput(child);
686 }
687 }
688
689 return ret;
690}
691
692static int populate_groups(struct config_group *group)
693{
694 struct config_group *new_group;
7063fbf2
JB
695 int ret = 0;
696 int i;
697
cbca692c 698 if (group->default_groups) {
7063fbf2
JB
699 for (i = 0; group->default_groups[i]; i++) {
700 new_group = group->default_groups[i];
701
702 ret = create_default_group(group, new_group);
2e2ce171
LR
703 if (ret) {
704 detach_groups(group);
7063fbf2 705 break;
2e2ce171 706 }
7063fbf2 707 }
7063fbf2
JB
708 }
709
7063fbf2
JB
710 return ret;
711}
712
713/*
714 * All of link_obj/unlink_obj/link_group/unlink_group require that
e6bd07ae 715 * subsys->su_mutex is held.
7063fbf2
JB
716 */
717
718static void unlink_obj(struct config_item *item)
719{
720 struct config_group *group;
721
722 group = item->ci_group;
723 if (group) {
724 list_del_init(&item->ci_entry);
725
726 item->ci_group = NULL;
727 item->ci_parent = NULL;
eed7a0db
JB
728
729 /* Drop the reference for ci_entry */
7063fbf2
JB
730 config_item_put(item);
731
eed7a0db 732 /* Drop the reference for ci_parent */
7063fbf2
JB
733 config_group_put(group);
734 }
735}
736
737static void link_obj(struct config_item *parent_item, struct config_item *item)
738{
eed7a0db
JB
739 /*
740 * Parent seems redundant with group, but it makes certain
741 * traversals much nicer.
742 */
7063fbf2 743 item->ci_parent = parent_item;
eed7a0db
JB
744
745 /*
746 * We hold a reference on the parent for the child's ci_parent
747 * link.
748 */
7063fbf2
JB
749 item->ci_group = config_group_get(to_config_group(parent_item));
750 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
751
eed7a0db
JB
752 /*
753 * We hold a reference on the child for ci_entry on the parent's
754 * cg_children
755 */
7063fbf2
JB
756 config_item_get(item);
757}
758
759static void unlink_group(struct config_group *group)
760{
761 int i;
762 struct config_group *new_group;
763
764 if (group->default_groups) {
765 for (i = 0; group->default_groups[i]; i++) {
766 new_group = group->default_groups[i];
767 unlink_group(new_group);
768 }
769 }
770
771 group->cg_subsys = NULL;
772 unlink_obj(&group->cg_item);
773}
774
775static void link_group(struct config_group *parent_group, struct config_group *group)
776{
777 int i;
778 struct config_group *new_group;
779 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
780
781 link_obj(&parent_group->cg_item, &group->cg_item);
782
783 if (parent_group->cg_subsys)
784 subsys = parent_group->cg_subsys;
785 else if (configfs_is_root(&parent_group->cg_item))
786 subsys = to_configfs_subsystem(group);
787 else
788 BUG();
789 group->cg_subsys = subsys;
790
791 if (group->default_groups) {
792 for (i = 0; group->default_groups[i]; i++) {
793 new_group = group->default_groups[i];
794 link_group(group, new_group);
795 }
796 }
797}
798
799/*
800 * The goal is that configfs_attach_item() (and
801 * configfs_attach_group()) can be called from either the VFS or this
802 * module. That is, they assume that the items have been created,
803 * the dentry allocated, and the dcache is all ready to go.
804 *
805 * If they fail, they must clean up after themselves as if they
806 * had never been called. The caller (VFS or local function) will
807 * handle cleaning up the dcache bits.
808 *
809 * configfs_detach_group() and configfs_detach_item() behave similarly on
810 * the way out. They assume that the proper semaphores are held, they
811 * clean up the configfs items, and they expect their callers will
812 * handle the dcache bits.
813 */
814static int configfs_attach_item(struct config_item *parent_item,
815 struct config_item *item,
816 struct dentry *dentry)
817{
818 int ret;
819
820 ret = configfs_create_dir(item, dentry);
821 if (!ret) {
822 ret = populate_attrs(item);
823 if (ret) {
2e2ce171
LR
824 /*
825 * We are going to remove an inode and its dentry but
826 * the VFS may already have hit and used them. Thus,
827 * we must lock them as rmdir() would.
828 */
829 mutex_lock(&dentry->d_inode->i_mutex);
7063fbf2 830 configfs_remove_dir(item);
2e2ce171 831 dentry->d_inode->i_flags |= S_DEAD;
d83c49f3 832 dont_mount(dentry);
2e2ce171 833 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
834 d_delete(dentry);
835 }
836 }
837
838 return ret;
839}
840
2e2ce171 841/* Caller holds the mutex of the item's inode */
7063fbf2
JB
842static void configfs_detach_item(struct config_item *item)
843{
844 detach_attrs(item);
845 configfs_remove_dir(item);
846}
847
848static int configfs_attach_group(struct config_item *parent_item,
849 struct config_item *item,
850 struct dentry *dentry)
851{
852 int ret;
853 struct configfs_dirent *sd;
854
855 ret = configfs_attach_item(parent_item, item, dentry);
856 if (!ret) {
857 sd = dentry->d_fsdata;
858 sd->s_type |= CONFIGFS_USET_DIR;
859
2e2ce171
LR
860 /*
861 * FYI, we're faking mkdir in populate_groups()
862 * We must lock the group's inode to avoid races with the VFS
863 * which can already hit the inode and try to add/remove entries
864 * under it.
865 *
866 * We must also lock the inode to remove it safely in case of
867 * error, as rmdir() would.
868 */
869 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
e74cc06d 870 configfs_adjust_dir_dirent_depth_before_populate(sd);
7063fbf2
JB
871 ret = populate_groups(to_config_group(item));
872 if (ret) {
873 configfs_detach_item(item);
2e2ce171 874 dentry->d_inode->i_flags |= S_DEAD;
d83c49f3 875 dont_mount(dentry);
7063fbf2 876 }
e74cc06d 877 configfs_adjust_dir_dirent_depth_after_populate(sd);
2e2ce171
LR
878 mutex_unlock(&dentry->d_inode->i_mutex);
879 if (ret)
880 d_delete(dentry);
7063fbf2
JB
881 }
882
883 return ret;
884}
885
2e2ce171 886/* Caller holds the mutex of the group's inode */
7063fbf2
JB
887static void configfs_detach_group(struct config_item *item)
888{
889 detach_groups(to_config_group(item));
890 configfs_detach_item(item);
891}
892
299894cc
JB
893/*
894 * After the item has been detached from the filesystem view, we are
895 * ready to tear it out of the hierarchy. Notify the client before
896 * we do that so they can perform any cleanup that requires
897 * navigating the hierarchy. A client does not need to provide this
898 * callback. The subsystem semaphore MUST be held by the caller, and
899 * references must be valid for both items. It also assumes the
900 * caller has validated ci_type.
901 */
902static void client_disconnect_notify(struct config_item *parent_item,
903 struct config_item *item)
904{
905 struct config_item_type *type;
906
907 type = parent_item->ci_type;
908 BUG_ON(!type);
909
910 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
911 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
912 item);
913}
914
7063fbf2
JB
915/*
916 * Drop the initial reference from make_item()/make_group()
917 * This function assumes that reference is held on item
918 * and that item holds a valid reference to the parent. Also, it
919 * assumes the caller has validated ci_type.
920 */
921static void client_drop_item(struct config_item *parent_item,
922 struct config_item *item)
923{
924 struct config_item_type *type;
925
926 type = parent_item->ci_type;
927 BUG_ON(!type);
928
eed7a0db
JB
929 /*
930 * If ->drop_item() exists, it is responsible for the
931 * config_item_put().
932 */
7063fbf2
JB
933 if (type->ct_group_ops && type->ct_group_ops->drop_item)
934 type->ct_group_ops->drop_item(to_config_group(parent_item),
299894cc 935 item);
7063fbf2
JB
936 else
937 config_item_put(item);
938}
939
631d1feb
JB
940#ifdef DEBUG
941static void configfs_dump_one(struct configfs_dirent *sd, int level)
942{
c6686931 943 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
631d1feb 944
c6686931 945#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
631d1feb
JB
946 type_print(CONFIGFS_ROOT);
947 type_print(CONFIGFS_DIR);
948 type_print(CONFIGFS_ITEM_ATTR);
949 type_print(CONFIGFS_ITEM_LINK);
950 type_print(CONFIGFS_USET_DIR);
951 type_print(CONFIGFS_USET_DEFAULT);
952 type_print(CONFIGFS_USET_DROPPING);
953#undef type_print
954}
955
956static int configfs_dump(struct configfs_dirent *sd, int level)
957{
958 struct configfs_dirent *child_sd;
959 int ret = 0;
960
961 configfs_dump_one(sd, level);
962
963 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
964 return 0;
965
966 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
967 ret = configfs_dump(child_sd, level + 2);
968 if (ret)
969 break;
970 }
971
972 return ret;
973}
974#endif
975
976
977/*
978 * configfs_depend_item() and configfs_undepend_item()
979 *
980 * WARNING: Do not call these from a configfs callback!
981 *
982 * This describes these functions and their helpers.
983 *
984 * Allow another kernel system to depend on a config_item. If this
25985edc 985 * happens, the item cannot go away until the dependent can live without
631d1feb
JB
986 * it. The idea is to give client modules as simple an interface as
987 * possible. When a system asks them to depend on an item, they just
988 * call configfs_depend_item(). If the item is live and the client
989 * driver is in good shape, we'll happily do the work for them.
990 *
991 * Why is the locking complex? Because configfs uses the VFS to handle
992 * all locking, but this function is called outside the normal
993 * VFS->configfs path. So it must take VFS locks to prevent the
994 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
995 * why you can't call these functions underneath configfs callbacks.
996 *
997 * Note, btw, that this can be called at *any* time, even when a configfs
998 * subsystem isn't registered, or when configfs is loading or unloading.
999 * Just like configfs_register_subsystem(). So we take the same
420118ca
LR
1000 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1001 * If we can find the target item in the
631d1feb 1002 * configfs tree, it must be part of the subsystem tree as well, so we
420118ca
LR
1003 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1004 * locking out mkdir() and rmdir(), who might be racing us.
631d1feb
JB
1005 */
1006
1007/*
1008 * configfs_depend_prep()
1009 *
1010 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1011 * attributes. This is similar but not the same to configfs_detach_prep().
1012 * Note that configfs_detach_prep() expects the parent to be locked when it
1013 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1014 * do that so we can unlock it if we find nothing.
1015 *
1016 * Here we do a depth-first search of the dentry hierarchy looking for
420118ca
LR
1017 * our object.
1018 * We deliberately ignore items tagged as dropping since they are virtually
1019 * dead, as well as items in the middle of attachment since they virtually
1020 * do not exist yet. This completes the locking out of racing mkdir() and
1021 * rmdir().
1022 * Note: subdirectories in the middle of attachment start with s_type =
1023 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1024 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1025 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
631d1feb 1026 *
420118ca 1027 * If the target is not found, -ENOENT is bubbled up.
631d1feb
JB
1028 *
1029 * This adds a requirement that all config_items be unique!
1030 *
420118ca 1031 * This is recursive. There isn't
631d1feb
JB
1032 * much on the stack, though, so folks that need this function - be careful
1033 * about your stack! Patches will be accepted to make it iterative.
1034 */
1035static int configfs_depend_prep(struct dentry *origin,
1036 struct config_item *target)
1037{
49deb4bc 1038 struct configfs_dirent *child_sd, *sd;
631d1feb
JB
1039 int ret = 0;
1040
49deb4bc
WY
1041 BUG_ON(!origin || !origin->d_fsdata);
1042 sd = origin->d_fsdata;
631d1feb 1043
631d1feb
JB
1044 if (sd->s_element == target) /* Boo-yah */
1045 goto out;
1046
1047 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
420118ca
LR
1048 if ((child_sd->s_type & CONFIGFS_DIR) &&
1049 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1050 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
631d1feb
JB
1051 ret = configfs_depend_prep(child_sd->s_dentry,
1052 target);
1053 if (!ret)
1054 goto out; /* Child path boo-yah */
1055 }
1056 }
1057
1058 /* We looped all our children and didn't find target */
631d1feb
JB
1059 ret = -ENOENT;
1060
1061out:
1062 return ret;
1063}
1064
631d1feb
JB
1065int configfs_depend_item(struct configfs_subsystem *subsys,
1066 struct config_item *target)
1067{
1068 int ret;
1069 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1070 struct config_item *s_item = &subsys->su_group.cg_item;
b7c177fc 1071 struct dentry *root;
631d1feb
JB
1072
1073 /*
1074 * Pin the configfs filesystem. This means we can safely access
1075 * the root of the configfs filesystem.
1076 */
2a152ad3
AV
1077 root = configfs_pin_fs();
1078 if (IS_ERR(root))
1079 return PTR_ERR(root);
631d1feb
JB
1080
1081 /*
1082 * Next, lock the root directory. We're going to check that the
1083 * subsystem is really registered, and so we need to lock out
1084 * configfs_[un]register_subsystem().
1085 */
b7c177fc 1086 mutex_lock(&root->d_inode->i_mutex);
631d1feb 1087
b7c177fc 1088 root_sd = root->d_fsdata;
631d1feb
JB
1089
1090 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1091 if (p->s_type & CONFIGFS_DIR) {
1092 if (p->s_element == s_item) {
1093 subsys_sd = p;
1094 break;
1095 }
1096 }
1097 }
1098
1099 if (!subsys_sd) {
1100 ret = -ENOENT;
1101 goto out_unlock_fs;
1102 }
1103
1104 /* Ok, now we can trust subsys/s_item */
1105
420118ca
LR
1106 spin_lock(&configfs_dirent_lock);
1107 /* Scan the tree, return 0 if found */
631d1feb
JB
1108 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1109 if (ret)
420118ca 1110 goto out_unlock_dirent_lock;
631d1feb 1111
420118ca
LR
1112 /*
1113 * We are sure that the item is not about to be removed by rmdir(), and
1114 * not in the middle of attachment by mkdir().
1115 */
631d1feb
JB
1116 p = target->ci_dentry->d_fsdata;
1117 p->s_dependent_count += 1;
1118
420118ca
LR
1119out_unlock_dirent_lock:
1120 spin_unlock(&configfs_dirent_lock);
631d1feb 1121out_unlock_fs:
b7c177fc 1122 mutex_unlock(&root->d_inode->i_mutex);
631d1feb
JB
1123
1124 /*
1125 * If we succeeded, the fs is pinned via other methods. If not,
1126 * we're done with it anyway. So release_fs() is always right.
1127 */
1128 configfs_release_fs();
1129
1130 return ret;
1131}
1132EXPORT_SYMBOL(configfs_depend_item);
1133
1134/*
1135 * Release the dependent linkage. This is much simpler than
1136 * configfs_depend_item() because we know that that the client driver is
1137 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1138 */
1139void configfs_undepend_item(struct configfs_subsystem *subsys,
1140 struct config_item *target)
1141{
1142 struct configfs_dirent *sd;
1143
1144 /*
420118ca
LR
1145 * Since we can trust everything is pinned, we just need
1146 * configfs_dirent_lock.
631d1feb 1147 */
420118ca 1148 spin_lock(&configfs_dirent_lock);
631d1feb
JB
1149
1150 sd = target->ci_dentry->d_fsdata;
1151 BUG_ON(sd->s_dependent_count < 1);
1152
1153 sd->s_dependent_count -= 1;
1154
1155 /*
1156 * After this unlock, we cannot trust the item to stay alive!
1157 * DO NOT REFERENCE item after this unlock.
1158 */
420118ca 1159 spin_unlock(&configfs_dirent_lock);
631d1feb
JB
1160}
1161EXPORT_SYMBOL(configfs_undepend_item);
7063fbf2 1162
18bb1db3 1163static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
7063fbf2 1164{
a6795e9e
JB
1165 int ret = 0;
1166 int module_got = 0;
1167 struct config_group *group = NULL;
1168 struct config_item *item = NULL;
7063fbf2
JB
1169 struct config_item *parent_item;
1170 struct configfs_subsystem *subsys;
1171 struct configfs_dirent *sd;
1172 struct config_item_type *type;
70526b67 1173 struct module *subsys_owner = NULL, *new_item_owner = NULL;
7063fbf2
JB
1174 char *name;
1175
7063fbf2 1176 sd = dentry->d_parent->d_fsdata;
2a109f2a
LR
1177
1178 /*
1179 * Fake invisibility if dir belongs to a group/default groups hierarchy
1180 * being attached
1181 */
1182 if (!configfs_dirent_is_ready(sd)) {
1183 ret = -ENOENT;
1184 goto out;
1185 }
1186
84efad1a
JB
1187 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1188 ret = -EPERM;
1189 goto out;
1190 }
7063fbf2 1191
84efad1a 1192 /* Get a working ref for the duration of this function */
7063fbf2
JB
1193 parent_item = configfs_get_config_item(dentry->d_parent);
1194 type = parent_item->ci_type;
1195 subsys = to_config_group(parent_item)->cg_subsys;
1196 BUG_ON(!subsys);
1197
1198 if (!type || !type->ct_group_ops ||
1199 (!type->ct_group_ops->make_group &&
1200 !type->ct_group_ops->make_item)) {
84efad1a
JB
1201 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1202 goto out_put;
7063fbf2
JB
1203 }
1204
70526b67
JB
1205 /*
1206 * The subsystem may belong to a different module than the item
1207 * being created. We don't want to safely pin the new item but
1208 * fail to pin the subsystem it sits under.
1209 */
1210 if (!subsys->su_group.cg_item.ci_type) {
1211 ret = -EINVAL;
1212 goto out_put;
1213 }
1214 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1215 if (!try_module_get(subsys_owner)) {
1216 ret = -EINVAL;
1217 goto out_put;
1218 }
1219
7063fbf2
JB
1220 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1221 if (!name) {
84efad1a 1222 ret = -ENOMEM;
70526b67 1223 goto out_subsys_put;
7063fbf2 1224 }
84efad1a 1225
7063fbf2
JB
1226 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1227
e6bd07ae 1228 mutex_lock(&subsys->su_mutex);
7063fbf2 1229 if (type->ct_group_ops->make_group) {
f89ab861 1230 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
a6795e9e
JB
1231 if (!group)
1232 group = ERR_PTR(-ENOMEM);
1233 if (!IS_ERR(group)) {
7063fbf2
JB
1234 link_group(to_config_group(parent_item), group);
1235 item = &group->cg_item;
a6795e9e
JB
1236 } else
1237 ret = PTR_ERR(group);
7063fbf2 1238 } else {
f89ab861 1239 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
a6795e9e
JB
1240 if (!item)
1241 item = ERR_PTR(-ENOMEM);
1242 if (!IS_ERR(item))
7063fbf2 1243 link_obj(parent_item, item);
a6795e9e
JB
1244 else
1245 ret = PTR_ERR(item);
7063fbf2 1246 }
e6bd07ae 1247 mutex_unlock(&subsys->su_mutex);
7063fbf2
JB
1248
1249 kfree(name);
a6795e9e 1250 if (ret) {
eed7a0db 1251 /*
dacdd0e0 1252 * If ret != 0, then link_obj() was never called.
eed7a0db
JB
1253 * There are no extra references to clean up.
1254 */
70526b67 1255 goto out_subsys_put;
7063fbf2
JB
1256 }
1257
eed7a0db
JB
1258 /*
1259 * link_obj() has been called (via link_group() for groups).
1260 * From here on out, errors must clean that up.
1261 */
1262
7063fbf2 1263 type = item->ci_type;
eed7a0db
JB
1264 if (!type) {
1265 ret = -EINVAL;
1266 goto out_unlink;
1267 }
7063fbf2 1268
70526b67
JB
1269 new_item_owner = type->ct_owner;
1270 if (!try_module_get(new_item_owner)) {
eed7a0db
JB
1271 ret = -EINVAL;
1272 goto out_unlink;
1273 }
7063fbf2 1274
eed7a0db
JB
1275 /*
1276 * I hate doing it this way, but if there is
1277 * an error, module_put() probably should
1278 * happen after any cleanup.
1279 */
1280 module_got = 1;
1281
6d8344ba
LR
1282 /*
1283 * Make racing rmdir() fail if it did not tag parent with
1284 * CONFIGFS_USET_DROPPING
1285 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1286 * fail and let rmdir() terminate correctly
1287 */
1288 spin_lock(&configfs_dirent_lock);
1289 /* This will make configfs_detach_prep() fail */
1290 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1291 spin_unlock(&configfs_dirent_lock);
1292
eed7a0db
JB
1293 if (group)
1294 ret = configfs_attach_group(parent_item, item, dentry);
1295 else
1296 ret = configfs_attach_item(parent_item, item, dentry);
1297
6d8344ba
LR
1298 spin_lock(&configfs_dirent_lock);
1299 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
2a109f2a
LR
1300 if (!ret)
1301 configfs_dir_set_ready(dentry->d_fsdata);
6d8344ba
LR
1302 spin_unlock(&configfs_dirent_lock);
1303
eed7a0db
JB
1304out_unlink:
1305 if (ret) {
1306 /* Tear down everything we built up */
e6bd07ae 1307 mutex_lock(&subsys->su_mutex);
299894cc
JB
1308
1309 client_disconnect_notify(parent_item, item);
eed7a0db
JB
1310 if (group)
1311 unlink_group(group);
1312 else
1313 unlink_obj(item);
1314 client_drop_item(parent_item, item);
299894cc 1315
e6bd07ae 1316 mutex_unlock(&subsys->su_mutex);
eed7a0db
JB
1317
1318 if (module_got)
70526b67 1319 module_put(new_item_owner);
7063fbf2
JB
1320 }
1321
70526b67
JB
1322out_subsys_put:
1323 if (ret)
1324 module_put(subsys_owner);
1325
84efad1a
JB
1326out_put:
1327 /*
eed7a0db
JB
1328 * link_obj()/link_group() took a reference from child->parent,
1329 * so the parent is safely pinned. We can drop our working
1330 * reference.
84efad1a
JB
1331 */
1332 config_item_put(parent_item);
1333
1334out:
7063fbf2
JB
1335 return ret;
1336}
1337
1338static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1339{
1340 struct config_item *parent_item;
1341 struct config_item *item;
1342 struct configfs_subsystem *subsys;
1343 struct configfs_dirent *sd;
70526b67 1344 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
7063fbf2
JB
1345 int ret;
1346
7063fbf2
JB
1347 sd = dentry->d_fsdata;
1348 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1349 return -EPERM;
1350
84efad1a 1351 /* Get a working ref until we have the child */
7063fbf2
JB
1352 parent_item = configfs_get_config_item(dentry->d_parent);
1353 subsys = to_config_group(parent_item)->cg_subsys;
1354 BUG_ON(!subsys);
1355
1356 if (!parent_item->ci_type) {
1357 config_item_put(parent_item);
1358 return -EINVAL;
1359 }
1360
70526b67
JB
1361 /* configfs_mkdir() shouldn't have allowed this */
1362 BUG_ON(!subsys->su_group.cg_item.ci_type);
1363 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1364
9a73d78c
LR
1365 /*
1366 * Ensure that no racing symlink() will make detach_prep() fail while
1367 * the new link is temporarily attached
1368 */
6d8344ba
LR
1369 do {
1370 struct mutex *wait_mutex;
1371
de6bf18e
LR
1372 mutex_lock(&configfs_symlink_mutex);
1373 spin_lock(&configfs_dirent_lock);
420118ca
LR
1374 /*
1375 * Here's where we check for dependents. We're protected by
1376 * configfs_dirent_lock.
1377 * If no dependent, atomically tag the item as dropping.
1378 */
1379 ret = sd->s_dependent_count ? -EBUSY : 0;
1380 if (!ret) {
1381 ret = configfs_detach_prep(dentry, &wait_mutex);
1382 if (ret)
1383 configfs_detach_rollback(dentry);
1384 }
de6bf18e
LR
1385 spin_unlock(&configfs_dirent_lock);
1386 mutex_unlock(&configfs_symlink_mutex);
1387
1388 if (ret) {
6d8344ba
LR
1389 if (ret != -EAGAIN) {
1390 config_item_put(parent_item);
1391 return ret;
1392 }
1393
1394 /* Wait until the racing operation terminates */
1395 mutex_lock(wait_mutex);
1396 mutex_unlock(wait_mutex);
6d8344ba
LR
1397 }
1398 } while (ret == -EAGAIN);
7063fbf2 1399
84efad1a 1400 /* Get a working ref for the duration of this function */
7063fbf2
JB
1401 item = configfs_get_config_item(dentry);
1402
1403 /* Drop reference from above, item already holds one. */
1404 config_item_put(parent_item);
1405
1406 if (item->ci_type)
70526b67 1407 dead_item_owner = item->ci_type->ct_owner;
7063fbf2
JB
1408
1409 if (sd->s_type & CONFIGFS_USET_DIR) {
1410 configfs_detach_group(item);
1411
e6bd07ae 1412 mutex_lock(&subsys->su_mutex);
299894cc 1413 client_disconnect_notify(parent_item, item);
7063fbf2
JB
1414 unlink_group(to_config_group(item));
1415 } else {
1416 configfs_detach_item(item);
1417
e6bd07ae 1418 mutex_lock(&subsys->su_mutex);
299894cc 1419 client_disconnect_notify(parent_item, item);
7063fbf2
JB
1420 unlink_obj(item);
1421 }
1422
1423 client_drop_item(parent_item, item);
e6bd07ae 1424 mutex_unlock(&subsys->su_mutex);
7063fbf2
JB
1425
1426 /* Drop our reference from above */
1427 config_item_put(item);
1428
70526b67
JB
1429 module_put(dead_item_owner);
1430 module_put(subsys_owner);
7063fbf2
JB
1431
1432 return 0;
1433}
1434
754661f1 1435const struct inode_operations configfs_dir_inode_operations = {
7063fbf2
JB
1436 .mkdir = configfs_mkdir,
1437 .rmdir = configfs_rmdir,
1438 .symlink = configfs_symlink,
1439 .unlink = configfs_unlink,
1440 .lookup = configfs_lookup,
3d0f89bb 1441 .setattr = configfs_setattr,
7063fbf2
JB
1442};
1443
81d44ed1
AV
1444const struct inode_operations configfs_root_inode_operations = {
1445 .lookup = configfs_lookup,
1446 .setattr = configfs_setattr,
1447};
1448
7063fbf2
JB
1449#if 0
1450int configfs_rename_dir(struct config_item * item, const char *new_name)
1451{
1452 int error = 0;
1453 struct dentry * new_dentry, * parent;
1454
1455 if (!strcmp(config_item_name(item), new_name))
1456 return -EINVAL;
1457
1458 if (!item->parent)
1459 return -EINVAL;
1460
1461 down_write(&configfs_rename_sem);
1462 parent = item->parent->dentry;
1463
1b1dcc1b 1464 mutex_lock(&parent->d_inode->i_mutex);
7063fbf2
JB
1465
1466 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1467 if (!IS_ERR(new_dentry)) {
e7515d06 1468 if (!new_dentry->d_inode) {
7063fbf2
JB
1469 error = config_item_set_name(item, "%s", new_name);
1470 if (!error) {
1471 d_add(new_dentry, NULL);
1472 d_move(item->dentry, new_dentry);
1473 }
1474 else
1475 d_delete(new_dentry);
1476 } else
1477 error = -EEXIST;
1478 dput(new_dentry);
1479 }
1b1dcc1b 1480 mutex_unlock(&parent->d_inode->i_mutex);
7063fbf2
JB
1481 up_write(&configfs_rename_sem);
1482
1483 return error;
1484}
1485#endif
1486
1487static int configfs_dir_open(struct inode *inode, struct file *file)
1488{
867fa491 1489 struct dentry * dentry = file->f_path.dentry;
7063fbf2 1490 struct configfs_dirent * parent_sd = dentry->d_fsdata;
2a109f2a 1491 int err;
7063fbf2 1492
1b1dcc1b 1493 mutex_lock(&dentry->d_inode->i_mutex);
2a109f2a
LR
1494 /*
1495 * Fake invisibility if dir belongs to a group/default groups hierarchy
1496 * being attached
1497 */
1498 err = -ENOENT;
1499 if (configfs_dirent_is_ready(parent_sd)) {
420118ca 1500 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
2a109f2a
LR
1501 if (IS_ERR(file->private_data))
1502 err = PTR_ERR(file->private_data);
1503 else
1504 err = 0;
1505 }
1b1dcc1b 1506 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2 1507
2a109f2a 1508 return err;
7063fbf2
JB
1509}
1510
1511static int configfs_dir_close(struct inode *inode, struct file *file)
1512{
867fa491 1513 struct dentry * dentry = file->f_path.dentry;
7063fbf2
JB
1514 struct configfs_dirent * cursor = file->private_data;
1515
1b1dcc1b 1516 mutex_lock(&dentry->d_inode->i_mutex);
6f610764 1517 spin_lock(&configfs_dirent_lock);
7063fbf2 1518 list_del_init(&cursor->s_sibling);
6f610764 1519 spin_unlock(&configfs_dirent_lock);
1b1dcc1b 1520 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
1521
1522 release_configfs_dirent(cursor);
1523
1524 return 0;
1525}
1526
1527/* Relationship between s_mode and the DT_xxx types */
1528static inline unsigned char dt_type(struct configfs_dirent *sd)
1529{
1530 return (sd->s_mode >> 12) & 15;
1531}
1532
52018855 1533static int configfs_readdir(struct file *file, struct dir_context *ctx)
7063fbf2 1534{
52018855 1535 struct dentry *dentry = file->f_path.dentry;
b7c177fc 1536 struct super_block *sb = dentry->d_sb;
7063fbf2 1537 struct configfs_dirent * parent_sd = dentry->d_fsdata;
52018855 1538 struct configfs_dirent *cursor = file->private_data;
7063fbf2 1539 struct list_head *p, *q = &cursor->s_sibling;
24307aa1 1540 ino_t ino = 0;
7063fbf2 1541
52018855
AV
1542 if (!dir_emit_dots(file, ctx))
1543 return 0;
1544 if (ctx->pos == 2) {
1545 spin_lock(&configfs_dirent_lock);
1546 list_move(q, &parent_sd->s_children);
1547 spin_unlock(&configfs_dirent_lock);
1548 }
1549 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1550 struct configfs_dirent *next;
1551 const char *name;
1552 int len;
1553 struct inode *inode = NULL;
1554
1555 next = list_entry(p, struct configfs_dirent, s_sibling);
1556 if (!next->s_element)
1557 continue;
7063fbf2 1558
52018855
AV
1559 name = configfs_get_name(next);
1560 len = strlen(name);
1561
1562 /*
1563 * We'll have a dentry and an inode for
1564 * PINNED items and for open attribute
1565 * files. We lock here to prevent a race
1566 * with configfs_d_iput() clearing
1567 * s_dentry before calling iput().
1568 *
1569 * Why do we go to the trouble? If
1570 * someone has an attribute file open,
1571 * the inode number should match until
1572 * they close it. Beyond that, we don't
1573 * care.
1574 */
1575 spin_lock(&configfs_dirent_lock);
1576 dentry = next->s_dentry;
1577 if (dentry)
1578 inode = dentry->d_inode;
1579 if (inode)
1580 ino = inode->i_ino;
1581 spin_unlock(&configfs_dirent_lock);
1582 if (!inode)
1583 ino = iunique(sb, 2);
7063fbf2 1584
52018855
AV
1585 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1586 return 0;
7063fbf2 1587
52018855
AV
1588 spin_lock(&configfs_dirent_lock);
1589 list_move(q, p);
1590 spin_unlock(&configfs_dirent_lock);
1591 p = q;
1592 ctx->pos++;
7063fbf2
JB
1593 }
1594 return 0;
1595}
1596
965c8e59 1597static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
7063fbf2 1598{
867fa491 1599 struct dentry * dentry = file->f_path.dentry;
7063fbf2 1600
1b1dcc1b 1601 mutex_lock(&dentry->d_inode->i_mutex);
965c8e59 1602 switch (whence) {
7063fbf2
JB
1603 case 1:
1604 offset += file->f_pos;
1605 case 0:
1606 if (offset >= 0)
1607 break;
1608 default:
496ad9aa 1609 mutex_unlock(&file_inode(file)->i_mutex);
7063fbf2
JB
1610 return -EINVAL;
1611 }
1612 if (offset != file->f_pos) {
1613 file->f_pos = offset;
1614 if (file->f_pos >= 2) {
1615 struct configfs_dirent *sd = dentry->d_fsdata;
1616 struct configfs_dirent *cursor = file->private_data;
1617 struct list_head *p;
1618 loff_t n = file->f_pos - 2;
1619
6f610764 1620 spin_lock(&configfs_dirent_lock);
7063fbf2
JB
1621 list_del(&cursor->s_sibling);
1622 p = sd->s_children.next;
1623 while (n && p != &sd->s_children) {
1624 struct configfs_dirent *next;
1625 next = list_entry(p, struct configfs_dirent,
1626 s_sibling);
1627 if (next->s_element)
1628 n--;
1629 p = p->next;
1630 }
1631 list_add_tail(&cursor->s_sibling, p);
6f610764 1632 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
1633 }
1634 }
1b1dcc1b 1635 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
1636 return offset;
1637}
1638
4b6f5d20 1639const struct file_operations configfs_dir_operations = {
7063fbf2
JB
1640 .open = configfs_dir_open,
1641 .release = configfs_dir_close,
1642 .llseek = configfs_dir_lseek,
1643 .read = generic_read_dir,
52018855 1644 .iterate = configfs_readdir,
7063fbf2
JB
1645};
1646
1647int configfs_register_subsystem(struct configfs_subsystem *subsys)
1648{
1649 int err;
1650 struct config_group *group = &subsys->su_group;
7063fbf2 1651 struct dentry *dentry;
b7c177fc 1652 struct dentry *root;
7063fbf2
JB
1653 struct configfs_dirent *sd;
1654
2a152ad3
AV
1655 root = configfs_pin_fs();
1656 if (IS_ERR(root))
1657 return PTR_ERR(root);
7063fbf2
JB
1658
1659 if (!group->cg_item.ci_name)
1660 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1661
b7c177fc 1662 sd = root->d_fsdata;
7063fbf2
JB
1663 link_group(to_config_group(sd->s_element), group);
1664
b7c177fc 1665 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
7063fbf2 1666
7063fbf2 1667 err = -ENOMEM;
ec193cf5 1668 dentry = d_alloc_name(root, group->cg_item.ci_name);
afdf04ea
JB
1669 if (dentry) {
1670 d_add(dentry, NULL);
7063fbf2 1671
afdf04ea
JB
1672 err = configfs_attach_group(sd->s_element, &group->cg_item,
1673 dentry);
1674 if (err) {
df7f9967
JB
1675 BUG_ON(dentry->d_inode);
1676 d_drop(dentry);
afdf04ea 1677 dput(dentry);
2a109f2a
LR
1678 } else {
1679 spin_lock(&configfs_dirent_lock);
1680 configfs_dir_set_ready(dentry->d_fsdata);
1681 spin_unlock(&configfs_dirent_lock);
afdf04ea
JB
1682 }
1683 }
7063fbf2 1684
b7c177fc 1685 mutex_unlock(&root->d_inode->i_mutex);
7063fbf2 1686
afdf04ea
JB
1687 if (err) {
1688 unlink_group(group);
1689 configfs_release_fs();
7063fbf2
JB
1690 }
1691
1692 return err;
1693}
1694
1695void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1696{
1697 struct config_group *group = &subsys->su_group;
1698 struct dentry *dentry = group->cg_item.ci_dentry;
b7c177fc 1699 struct dentry *root = dentry->d_sb->s_root;
7063fbf2 1700
b7c177fc 1701 if (dentry->d_parent != root) {
1d88aa44 1702 pr_err("Tried to unregister non-subsystem!\n");
7063fbf2
JB
1703 return;
1704 }
1705
b7c177fc 1706 mutex_lock_nested(&root->d_inode->i_mutex,
55ed1602
MF
1707 I_MUTEX_PARENT);
1708 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
9a73d78c 1709 mutex_lock(&configfs_symlink_mutex);
b3e76af8 1710 spin_lock(&configfs_dirent_lock);
6d8344ba 1711 if (configfs_detach_prep(dentry, NULL)) {
1d88aa44 1712 pr_err("Tried to unregister non-empty subsystem!\n");
7063fbf2 1713 }
b3e76af8 1714 spin_unlock(&configfs_dirent_lock);
9a73d78c 1715 mutex_unlock(&configfs_symlink_mutex);
7063fbf2
JB
1716 configfs_detach_group(&group->cg_item);
1717 dentry->d_inode->i_flags |= S_DEAD;
d83c49f3 1718 dont_mount(dentry);
1b1dcc1b 1719 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
1720
1721 d_delete(dentry);
1722
b7c177fc 1723 mutex_unlock(&root->d_inode->i_mutex);
7063fbf2
JB
1724
1725 dput(dentry);
1726
1727 unlink_group(group);
1728 configfs_release_fs();
1729}
1730
1731EXPORT_SYMBOL(configfs_register_subsystem);
1732EXPORT_SYMBOL(configfs_unregister_subsystem);
This page took 0.740154 seconds and 5 git commands to generate.