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