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