HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / configfs / dir.c
blob8e709b641b559726210836c4966b4c78af67cbb5
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dir.c - Operations for configfs directories.
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.
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.
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.
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
27 #undef DEBUG
29 #include <linux/fs.h>
30 #include <linux/mount.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/err.h>
35 #include <linux/configfs.h>
36 #include "configfs_internal.h"
38 DECLARE_RWSEM(configfs_rename_sem);
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
41 * Also protects mutations of symlinks linkage to target configfs_dirent
42 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
44 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
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()
51 DEFINE_SPINLOCK(configfs_dirent_lock);
53 static void configfs_d_iput(struct dentry * dentry,
54 struct inode * inode)
56 struct configfs_dirent *sd = dentry->d_fsdata;
58 if (sd) {
59 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock);
62 * Set sd->s_dentry to null only when this dentry is the one
63 * that is going to be killed. Otherwise configfs_d_iput may
64 * run just after configfs_attach_attr and set sd->s_dentry to
65 * NULL even it's still in use.
67 if (sd->s_dentry == dentry)
68 sd->s_dentry = NULL;
70 spin_unlock(&configfs_dirent_lock);
71 configfs_put(sd);
73 iput(inode);
76 const struct dentry_operations configfs_dentry_ops = {
77 .d_iput = configfs_d_iput,
78 .d_delete = always_delete_dentry,
81 #ifdef CONFIG_LOCKDEP
84 * Helpers to make lockdep happy with our recursive locking of default groups'
85 * inodes (see configfs_attach_group() and configfs_detach_group()).
86 * We put default groups i_mutexes in separate classes according to their depth
87 * from the youngest non-default group ancestor.
89 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
90 * groups A/B and A/C will have their inode's mutex in class
91 * default_group_class[0], and default group A/C/D will be in
92 * default_group_class[1].
94 * The lock classes are declared and assigned in inode.c, according to the
95 * s_depth value.
96 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
97 * default groups, and reset to -1 when all default groups are attached. During
98 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
99 * inode's mutex is set to default_group_class[s_depth - 1].
102 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
104 sd->s_depth = -1;
107 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
108 struct configfs_dirent *sd)
110 int parent_depth = parent_sd->s_depth;
112 if (parent_depth >= 0)
113 sd->s_depth = parent_depth + 1;
116 static void
117 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
120 * item's i_mutex class is already setup, so s_depth is now only
121 * used to set new sub-directories s_depth, which is always done
122 * with item's i_mutex locked.
125 * sd->s_depth == -1 iff we are a non default group.
126 * else (we are a default group) sd->s_depth > 0 (see
127 * create_dir()).
129 if (sd->s_depth == -1)
131 * We are a non default group and we are going to create
132 * default groups.
134 sd->s_depth = 0;
137 static void
138 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
140 /* We will not create default groups anymore. */
141 sd->s_depth = -1;
144 #else /* CONFIG_LOCKDEP */
146 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
150 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
151 struct configfs_dirent *sd)
155 static void
156 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
160 static void
161 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
165 #endif /* CONFIG_LOCKDEP */
168 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
170 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
171 void *element, int type)
173 struct configfs_dirent * sd;
175 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
176 if (!sd)
177 return ERR_PTR(-ENOMEM);
179 atomic_set(&sd->s_count, 1);
180 INIT_LIST_HEAD(&sd->s_links);
181 INIT_LIST_HEAD(&sd->s_children);
182 sd->s_element = element;
183 sd->s_type = type;
184 configfs_init_dirent_depth(sd);
185 spin_lock(&configfs_dirent_lock);
186 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
187 spin_unlock(&configfs_dirent_lock);
188 kmem_cache_free(configfs_dir_cachep, sd);
189 return ERR_PTR(-ENOENT);
191 list_add(&sd->s_sibling, &parent_sd->s_children);
192 spin_unlock(&configfs_dirent_lock);
194 return sd;
199 * Return -EEXIST if there is already a configfs element with the same
200 * name for the same parent.
202 * called with parent inode's i_mutex held
204 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
205 const unsigned char *new)
207 struct configfs_dirent * sd;
209 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
210 if (sd->s_element) {
211 const unsigned char *existing = configfs_get_name(sd);
212 if (strcmp(existing, new))
213 continue;
214 else
215 return -EEXIST;
219 return 0;
223 int configfs_make_dirent(struct configfs_dirent * parent_sd,
224 struct dentry * dentry, void * element,
225 umode_t mode, int type)
227 struct configfs_dirent * sd;
229 sd = configfs_new_dirent(parent_sd, element, type);
230 if (IS_ERR(sd))
231 return PTR_ERR(sd);
233 sd->s_mode = mode;
234 sd->s_dentry = dentry;
235 if (dentry)
236 dentry->d_fsdata = configfs_get(sd);
238 return 0;
241 static void init_dir(struct inode * inode)
243 inode->i_op = &configfs_dir_inode_operations;
244 inode->i_fop = &configfs_dir_operations;
246 /* directory inodes start off with i_nlink == 2 (for "." entry) */
247 inc_nlink(inode);
250 static void configfs_init_file(struct inode * inode)
252 inode->i_size = PAGE_SIZE;
253 inode->i_fop = &configfs_file_operations;
256 static void init_symlink(struct inode * inode)
258 inode->i_op = &configfs_symlink_inode_operations;
262 * configfs_create_dir - create a directory for an config_item.
263 * @item: config_itemwe're creating directory for.
264 * @dentry: config_item's dentry.
266 * Note: user-created entries won't be allowed under this new directory
267 * until it is validated by configfs_dir_set_ready()
270 static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
272 int error;
273 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
274 struct dentry *p = dentry->d_parent;
276 BUG_ON(!item);
278 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
279 if (unlikely(error))
280 return error;
282 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
283 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
284 if (unlikely(error))
285 return error;
287 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
288 error = configfs_create(dentry, mode, init_dir);
289 if (!error) {
290 inc_nlink(d_inode(p));
291 item->ci_dentry = dentry;
292 } else {
293 struct configfs_dirent *sd = dentry->d_fsdata;
294 if (sd) {
295 spin_lock(&configfs_dirent_lock);
296 list_del_init(&sd->s_sibling);
297 spin_unlock(&configfs_dirent_lock);
298 configfs_put(sd);
301 return error;
305 * Allow userspace to create new entries under a new directory created with
306 * configfs_create_dir(), and under all of its chidlren directories recursively.
307 * @sd configfs_dirent of the new directory to validate
309 * Caller must hold configfs_dirent_lock.
311 static void configfs_dir_set_ready(struct configfs_dirent *sd)
313 struct configfs_dirent *child_sd;
315 sd->s_type &= ~CONFIGFS_USET_CREATING;
316 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
317 if (child_sd->s_type & CONFIGFS_USET_CREATING)
318 configfs_dir_set_ready(child_sd);
322 * Check that a directory does not belong to a directory hierarchy being
323 * attached and not validated yet.
324 * @sd configfs_dirent of the directory to check
326 * @return non-zero iff the directory was validated
328 * Note: takes configfs_dirent_lock, so the result may change from false to true
329 * in two consecutive calls, but never from true to false.
331 int configfs_dirent_is_ready(struct configfs_dirent *sd)
333 int ret;
335 spin_lock(&configfs_dirent_lock);
336 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
337 spin_unlock(&configfs_dirent_lock);
339 return ret;
342 int configfs_create_link(struct configfs_symlink *sl,
343 struct dentry *parent,
344 struct dentry *dentry)
346 int err = 0;
347 umode_t mode = S_IFLNK | S_IRWXUGO;
349 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
350 CONFIGFS_ITEM_LINK);
351 if (!err) {
352 err = configfs_create(dentry, mode, init_symlink);
353 if (err) {
354 struct configfs_dirent *sd = dentry->d_fsdata;
355 if (sd) {
356 spin_lock(&configfs_dirent_lock);
357 list_del_init(&sd->s_sibling);
358 spin_unlock(&configfs_dirent_lock);
359 configfs_put(sd);
363 return err;
366 static void remove_dir(struct dentry * d)
368 struct dentry * parent = dget(d->d_parent);
369 struct configfs_dirent * sd;
371 sd = d->d_fsdata;
372 spin_lock(&configfs_dirent_lock);
373 list_del_init(&sd->s_sibling);
374 spin_unlock(&configfs_dirent_lock);
375 configfs_put(sd);
376 if (d_really_is_positive(d))
377 simple_rmdir(d_inode(parent),d);
379 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
381 dput(parent);
385 * configfs_remove_dir - remove an config_item's directory.
386 * @item: config_item we're removing.
388 * The only thing special about this is that we remove any files in
389 * the directory before we remove the directory, and we've inlined
390 * what used to be configfs_rmdir() below, instead of calling separately.
392 * Caller holds the mutex of the item's inode
395 static void configfs_remove_dir(struct config_item * item)
397 struct dentry * dentry = dget(item->ci_dentry);
399 if (!dentry)
400 return;
402 remove_dir(dentry);
404 * Drop reference from dget() on entrance.
406 dput(dentry);
410 /* attaches attribute's configfs_dirent to the dentry corresponding to the
411 * attribute file
413 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
415 struct configfs_attribute * attr = sd->s_element;
416 int error;
418 spin_lock(&configfs_dirent_lock);
419 dentry->d_fsdata = configfs_get(sd);
420 sd->s_dentry = dentry;
421 spin_unlock(&configfs_dirent_lock);
423 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
424 configfs_init_file);
425 if (error) {
426 configfs_put(sd);
427 return error;
430 d_rehash(dentry);
432 return 0;
435 static struct dentry * configfs_lookup(struct inode *dir,
436 struct dentry *dentry,
437 unsigned int flags)
439 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
440 struct configfs_dirent * sd;
441 int found = 0;
442 int err;
445 * Fake invisibility if dir belongs to a group/default groups hierarchy
446 * being attached
448 * This forbids userspace to read/write attributes of items which may
449 * not complete their initialization, since the dentries of the
450 * attributes won't be instantiated.
452 err = -ENOENT;
453 if (!configfs_dirent_is_ready(parent_sd))
454 goto out;
456 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
457 if (sd->s_type & CONFIGFS_NOT_PINNED) {
458 const unsigned char * name = configfs_get_name(sd);
460 if (strcmp(name, dentry->d_name.name))
461 continue;
463 found = 1;
464 err = configfs_attach_attr(sd, dentry);
465 break;
469 if (!found) {
471 * If it doesn't exist and it isn't a NOT_PINNED item,
472 * it must be negative.
474 if (dentry->d_name.len > NAME_MAX)
475 return ERR_PTR(-ENAMETOOLONG);
476 d_add(dentry, NULL);
477 return NULL;
480 out:
481 return ERR_PTR(err);
485 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
486 * attributes and are removed by rmdir(). We recurse, setting
487 * CONFIGFS_USET_DROPPING on all children that are candidates for
488 * default detach.
489 * If there is an error, the caller will reset the flags via
490 * configfs_detach_rollback().
492 static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
494 struct configfs_dirent *parent_sd = dentry->d_fsdata;
495 struct configfs_dirent *sd;
496 int ret;
498 /* Mark that we're trying to drop the group */
499 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
501 ret = -EBUSY;
502 if (!list_empty(&parent_sd->s_links))
503 goto out;
505 ret = 0;
506 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
507 if (!sd->s_element ||
508 (sd->s_type & CONFIGFS_NOT_PINNED))
509 continue;
510 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
511 /* Abort if racing with mkdir() */
512 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
513 if (wait_mutex)
514 *wait_mutex = &d_inode(sd->s_dentry)->i_mutex;
515 return -EAGAIN;
519 * Yup, recursive. If there's a problem, blame
520 * deep nesting of default_groups
522 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
523 if (!ret)
524 continue;
525 } else
526 ret = -ENOTEMPTY;
528 break;
531 out:
532 return ret;
536 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
537 * set.
539 static void configfs_detach_rollback(struct dentry *dentry)
541 struct configfs_dirent *parent_sd = dentry->d_fsdata;
542 struct configfs_dirent *sd;
544 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
546 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
547 if (sd->s_type & CONFIGFS_USET_DEFAULT)
548 configfs_detach_rollback(sd->s_dentry);
551 static void detach_attrs(struct config_item * item)
553 struct dentry * dentry = dget(item->ci_dentry);
554 struct configfs_dirent * parent_sd;
555 struct configfs_dirent * sd, * tmp;
557 if (!dentry)
558 return;
560 pr_debug("configfs %s: dropping attrs for dir\n",
561 dentry->d_name.name);
563 parent_sd = dentry->d_fsdata;
564 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
565 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
566 continue;
567 spin_lock(&configfs_dirent_lock);
568 list_del_init(&sd->s_sibling);
569 spin_unlock(&configfs_dirent_lock);
570 configfs_drop_dentry(sd, dentry);
571 configfs_put(sd);
575 * Drop reference from dget() on entrance.
577 dput(dentry);
580 static int populate_attrs(struct config_item *item)
582 struct config_item_type *t = item->ci_type;
583 struct configfs_attribute *attr;
584 int error = 0;
585 int i;
587 if (!t)
588 return -EINVAL;
589 if (t->ct_attrs) {
590 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
591 if ((error = configfs_create_file(item, attr)))
592 break;
596 if (error)
597 detach_attrs(item);
599 return error;
602 static int configfs_attach_group(struct config_item *parent_item,
603 struct config_item *item,
604 struct dentry *dentry);
605 static void configfs_detach_group(struct config_item *item);
607 static void detach_groups(struct config_group *group)
609 struct dentry * dentry = dget(group->cg_item.ci_dentry);
610 struct dentry *child;
611 struct configfs_dirent *parent_sd;
612 struct configfs_dirent *sd, *tmp;
614 if (!dentry)
615 return;
617 parent_sd = dentry->d_fsdata;
618 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
619 if (!sd->s_element ||
620 !(sd->s_type & CONFIGFS_USET_DEFAULT))
621 continue;
623 child = sd->s_dentry;
625 mutex_lock(&d_inode(child)->i_mutex);
627 configfs_detach_group(sd->s_element);
628 d_inode(child)->i_flags |= S_DEAD;
629 dont_mount(child);
631 mutex_unlock(&d_inode(child)->i_mutex);
633 d_delete(child);
634 dput(child);
638 * Drop reference from dget() on entrance.
640 dput(dentry);
644 * This fakes mkdir(2) on a default_groups[] entry. It
645 * creates a dentry, attachs it, and then does fixup
646 * on the sd->s_type.
648 * We could, perhaps, tweak our parent's ->mkdir for a minute and
649 * try using vfs_mkdir. Just a thought.
651 static int create_default_group(struct config_group *parent_group,
652 struct config_group *group)
654 int ret;
655 struct configfs_dirent *sd;
656 /* We trust the caller holds a reference to parent */
657 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
659 if (!group->cg_item.ci_name)
660 group->cg_item.ci_name = group->cg_item.ci_namebuf;
662 ret = -ENOMEM;
663 child = d_alloc_name(parent, group->cg_item.ci_name);
664 if (child) {
665 d_add(child, NULL);
667 ret = configfs_attach_group(&parent_group->cg_item,
668 &group->cg_item, child);
669 if (!ret) {
670 sd = child->d_fsdata;
671 sd->s_type |= CONFIGFS_USET_DEFAULT;
672 } else {
673 BUG_ON(d_inode(child));
674 d_drop(child);
675 dput(child);
679 return ret;
682 static int populate_groups(struct config_group *group)
684 struct config_group *new_group;
685 int ret = 0;
686 int i;
688 if (group->default_groups) {
689 for (i = 0; group->default_groups[i]; i++) {
690 new_group = group->default_groups[i];
692 ret = create_default_group(group, new_group);
693 if (ret) {
694 detach_groups(group);
695 break;
700 return ret;
704 * All of link_obj/unlink_obj/link_group/unlink_group require that
705 * subsys->su_mutex is held.
708 static void unlink_obj(struct config_item *item)
710 struct config_group *group;
712 group = item->ci_group;
713 if (group) {
714 list_del_init(&item->ci_entry);
716 item->ci_group = NULL;
717 item->ci_parent = NULL;
719 /* Drop the reference for ci_entry */
720 config_item_put(item);
722 /* Drop the reference for ci_parent */
723 config_group_put(group);
727 static void link_obj(struct config_item *parent_item, struct config_item *item)
730 * Parent seems redundant with group, but it makes certain
731 * traversals much nicer.
733 item->ci_parent = parent_item;
736 * We hold a reference on the parent for the child's ci_parent
737 * link.
739 item->ci_group = config_group_get(to_config_group(parent_item));
740 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
743 * We hold a reference on the child for ci_entry on the parent's
744 * cg_children
746 config_item_get(item);
749 static void unlink_group(struct config_group *group)
751 int i;
752 struct config_group *new_group;
754 if (group->default_groups) {
755 for (i = 0; group->default_groups[i]; i++) {
756 new_group = group->default_groups[i];
757 unlink_group(new_group);
761 group->cg_subsys = NULL;
762 unlink_obj(&group->cg_item);
765 static void link_group(struct config_group *parent_group, struct config_group *group)
767 int i;
768 struct config_group *new_group;
769 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
771 link_obj(&parent_group->cg_item, &group->cg_item);
773 if (parent_group->cg_subsys)
774 subsys = parent_group->cg_subsys;
775 else if (configfs_is_root(&parent_group->cg_item))
776 subsys = to_configfs_subsystem(group);
777 else
778 BUG();
779 group->cg_subsys = subsys;
781 if (group->default_groups) {
782 for (i = 0; group->default_groups[i]; i++) {
783 new_group = group->default_groups[i];
784 link_group(group, new_group);
790 * The goal is that configfs_attach_item() (and
791 * configfs_attach_group()) can be called from either the VFS or this
792 * module. That is, they assume that the items have been created,
793 * the dentry allocated, and the dcache is all ready to go.
795 * If they fail, they must clean up after themselves as if they
796 * had never been called. The caller (VFS or local function) will
797 * handle cleaning up the dcache bits.
799 * configfs_detach_group() and configfs_detach_item() behave similarly on
800 * the way out. They assume that the proper semaphores are held, they
801 * clean up the configfs items, and they expect their callers will
802 * handle the dcache bits.
804 static int configfs_attach_item(struct config_item *parent_item,
805 struct config_item *item,
806 struct dentry *dentry)
808 int ret;
810 ret = configfs_create_dir(item, dentry);
811 if (!ret) {
812 ret = populate_attrs(item);
813 if (ret) {
815 * We are going to remove an inode and its dentry but
816 * the VFS may already have hit and used them. Thus,
817 * we must lock them as rmdir() would.
819 mutex_lock(&d_inode(dentry)->i_mutex);
820 configfs_remove_dir(item);
821 d_inode(dentry)->i_flags |= S_DEAD;
822 dont_mount(dentry);
823 mutex_unlock(&d_inode(dentry)->i_mutex);
824 d_delete(dentry);
828 return ret;
831 /* Caller holds the mutex of the item's inode */
832 static void configfs_detach_item(struct config_item *item)
834 detach_attrs(item);
835 configfs_remove_dir(item);
838 static int configfs_attach_group(struct config_item *parent_item,
839 struct config_item *item,
840 struct dentry *dentry)
842 int ret;
843 struct configfs_dirent *sd;
845 ret = configfs_attach_item(parent_item, item, dentry);
846 if (!ret) {
847 sd = dentry->d_fsdata;
848 sd->s_type |= CONFIGFS_USET_DIR;
851 * FYI, we're faking mkdir in populate_groups()
852 * We must lock the group's inode to avoid races with the VFS
853 * which can already hit the inode and try to add/remove entries
854 * under it.
856 * We must also lock the inode to remove it safely in case of
857 * error, as rmdir() would.
859 mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_CHILD);
860 configfs_adjust_dir_dirent_depth_before_populate(sd);
861 ret = populate_groups(to_config_group(item));
862 if (ret) {
863 configfs_detach_item(item);
864 d_inode(dentry)->i_flags |= S_DEAD;
865 dont_mount(dentry);
867 configfs_adjust_dir_dirent_depth_after_populate(sd);
868 mutex_unlock(&d_inode(dentry)->i_mutex);
869 if (ret)
870 d_delete(dentry);
873 return ret;
876 /* Caller holds the mutex of the group's inode */
877 static void configfs_detach_group(struct config_item *item)
879 detach_groups(to_config_group(item));
880 configfs_detach_item(item);
884 * After the item has been detached from the filesystem view, we are
885 * ready to tear it out of the hierarchy. Notify the client before
886 * we do that so they can perform any cleanup that requires
887 * navigating the hierarchy. A client does not need to provide this
888 * callback. The subsystem semaphore MUST be held by the caller, and
889 * references must be valid for both items. It also assumes the
890 * caller has validated ci_type.
892 static void client_disconnect_notify(struct config_item *parent_item,
893 struct config_item *item)
895 struct config_item_type *type;
897 type = parent_item->ci_type;
898 BUG_ON(!type);
900 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
901 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
902 item);
906 * Drop the initial reference from make_item()/make_group()
907 * This function assumes that reference is held on item
908 * and that item holds a valid reference to the parent. Also, it
909 * assumes the caller has validated ci_type.
911 static void client_drop_item(struct config_item *parent_item,
912 struct config_item *item)
914 struct config_item_type *type;
916 type = parent_item->ci_type;
917 BUG_ON(!type);
920 * If ->drop_item() exists, it is responsible for the
921 * config_item_put().
923 if (type->ct_group_ops && type->ct_group_ops->drop_item)
924 type->ct_group_ops->drop_item(to_config_group(parent_item),
925 item);
926 else
927 config_item_put(item);
930 #ifdef DEBUG
931 static void configfs_dump_one(struct configfs_dirent *sd, int level)
933 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
935 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
936 type_print(CONFIGFS_ROOT);
937 type_print(CONFIGFS_DIR);
938 type_print(CONFIGFS_ITEM_ATTR);
939 type_print(CONFIGFS_ITEM_LINK);
940 type_print(CONFIGFS_USET_DIR);
941 type_print(CONFIGFS_USET_DEFAULT);
942 type_print(CONFIGFS_USET_DROPPING);
943 #undef type_print
946 static int configfs_dump(struct configfs_dirent *sd, int level)
948 struct configfs_dirent *child_sd;
949 int ret = 0;
951 configfs_dump_one(sd, level);
953 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
954 return 0;
956 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
957 ret = configfs_dump(child_sd, level + 2);
958 if (ret)
959 break;
962 return ret;
964 #endif
968 * configfs_depend_item() and configfs_undepend_item()
970 * WARNING: Do not call these from a configfs callback!
972 * This describes these functions and their helpers.
974 * Allow another kernel system to depend on a config_item. If this
975 * happens, the item cannot go away until the dependent can live without
976 * it. The idea is to give client modules as simple an interface as
977 * possible. When a system asks them to depend on an item, they just
978 * call configfs_depend_item(). If the item is live and the client
979 * driver is in good shape, we'll happily do the work for them.
981 * Why is the locking complex? Because configfs uses the VFS to handle
982 * all locking, but this function is called outside the normal
983 * VFS->configfs path. So it must take VFS locks to prevent the
984 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
985 * why you can't call these functions underneath configfs callbacks.
987 * Note, btw, that this can be called at *any* time, even when a configfs
988 * subsystem isn't registered, or when configfs is loading or unloading.
989 * Just like configfs_register_subsystem(). So we take the same
990 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
991 * If we can find the target item in the
992 * configfs tree, it must be part of the subsystem tree as well, so we
993 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
994 * locking out mkdir() and rmdir(), who might be racing us.
998 * configfs_depend_prep()
1000 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1001 * attributes. This is similar but not the same to configfs_detach_prep().
1002 * Note that configfs_detach_prep() expects the parent to be locked when it
1003 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1004 * do that so we can unlock it if we find nothing.
1006 * Here we do a depth-first search of the dentry hierarchy looking for
1007 * our object.
1008 * We deliberately ignore items tagged as dropping since they are virtually
1009 * dead, as well as items in the middle of attachment since they virtually
1010 * do not exist yet. This completes the locking out of racing mkdir() and
1011 * rmdir().
1012 * Note: subdirectories in the middle of attachment start with s_type =
1013 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1014 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1015 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1017 * If the target is not found, -ENOENT is bubbled up.
1019 * This adds a requirement that all config_items be unique!
1021 * This is recursive. There isn't
1022 * much on the stack, though, so folks that need this function - be careful
1023 * about your stack! Patches will be accepted to make it iterative.
1025 static int configfs_depend_prep(struct dentry *origin,
1026 struct config_item *target)
1028 struct configfs_dirent *child_sd, *sd;
1029 int ret = 0;
1031 BUG_ON(!origin || !origin->d_fsdata);
1032 sd = origin->d_fsdata;
1034 if (sd->s_element == target) /* Boo-yah */
1035 goto out;
1037 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1038 if ((child_sd->s_type & CONFIGFS_DIR) &&
1039 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1040 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1041 ret = configfs_depend_prep(child_sd->s_dentry,
1042 target);
1043 if (!ret)
1044 goto out; /* Child path boo-yah */
1048 /* We looped all our children and didn't find target */
1049 ret = -ENOENT;
1051 out:
1052 return ret;
1055 int configfs_depend_item(struct configfs_subsystem *subsys,
1056 struct config_item *target)
1058 int ret;
1059 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1060 struct config_item *s_item = &subsys->su_group.cg_item;
1061 struct dentry *root;
1064 * Pin the configfs filesystem. This means we can safely access
1065 * the root of the configfs filesystem.
1067 root = configfs_pin_fs();
1068 if (IS_ERR(root))
1069 return PTR_ERR(root);
1072 * Next, lock the root directory. We're going to check that the
1073 * subsystem is really registered, and so we need to lock out
1074 * configfs_[un]register_subsystem().
1076 mutex_lock(&d_inode(root)->i_mutex);
1078 root_sd = root->d_fsdata;
1080 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1081 if (p->s_type & CONFIGFS_DIR) {
1082 if (p->s_element == s_item) {
1083 subsys_sd = p;
1084 break;
1089 if (!subsys_sd) {
1090 ret = -ENOENT;
1091 goto out_unlock_fs;
1094 /* Ok, now we can trust subsys/s_item */
1096 spin_lock(&configfs_dirent_lock);
1097 /* Scan the tree, return 0 if found */
1098 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1099 if (ret)
1100 goto out_unlock_dirent_lock;
1103 * We are sure that the item is not about to be removed by rmdir(), and
1104 * not in the middle of attachment by mkdir().
1106 p = target->ci_dentry->d_fsdata;
1107 p->s_dependent_count += 1;
1109 out_unlock_dirent_lock:
1110 spin_unlock(&configfs_dirent_lock);
1111 out_unlock_fs:
1112 mutex_unlock(&d_inode(root)->i_mutex);
1115 * If we succeeded, the fs is pinned via other methods. If not,
1116 * we're done with it anyway. So release_fs() is always right.
1118 configfs_release_fs();
1120 return ret;
1122 EXPORT_SYMBOL(configfs_depend_item);
1125 * Release the dependent linkage. This is much simpler than
1126 * configfs_depend_item() because we know that that the client driver is
1127 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1129 void configfs_undepend_item(struct configfs_subsystem *subsys,
1130 struct config_item *target)
1132 struct configfs_dirent *sd;
1135 * Since we can trust everything is pinned, we just need
1136 * configfs_dirent_lock.
1138 spin_lock(&configfs_dirent_lock);
1140 sd = target->ci_dentry->d_fsdata;
1141 BUG_ON(sd->s_dependent_count < 1);
1143 sd->s_dependent_count -= 1;
1146 * After this unlock, we cannot trust the item to stay alive!
1147 * DO NOT REFERENCE item after this unlock.
1149 spin_unlock(&configfs_dirent_lock);
1151 EXPORT_SYMBOL(configfs_undepend_item);
1153 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1155 int ret = 0;
1156 int module_got = 0;
1157 struct config_group *group = NULL;
1158 struct config_item *item = NULL;
1159 struct config_item *parent_item;
1160 struct configfs_subsystem *subsys;
1161 struct configfs_dirent *sd;
1162 struct config_item_type *type;
1163 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1164 char *name;
1166 sd = dentry->d_parent->d_fsdata;
1169 * Fake invisibility if dir belongs to a group/default groups hierarchy
1170 * being attached
1172 if (!configfs_dirent_is_ready(sd)) {
1173 ret = -ENOENT;
1174 goto out;
1177 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1178 ret = -EPERM;
1179 goto out;
1182 /* Get a working ref for the duration of this function */
1183 parent_item = configfs_get_config_item(dentry->d_parent);
1184 type = parent_item->ci_type;
1185 subsys = to_config_group(parent_item)->cg_subsys;
1186 BUG_ON(!subsys);
1188 if (!type || !type->ct_group_ops ||
1189 (!type->ct_group_ops->make_group &&
1190 !type->ct_group_ops->make_item)) {
1191 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1192 goto out_put;
1196 * The subsystem may belong to a different module than the item
1197 * being created. We don't want to safely pin the new item but
1198 * fail to pin the subsystem it sits under.
1200 if (!subsys->su_group.cg_item.ci_type) {
1201 ret = -EINVAL;
1202 goto out_put;
1204 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1205 if (!try_module_get(subsys_owner)) {
1206 ret = -EINVAL;
1207 goto out_put;
1210 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1211 if (!name) {
1212 ret = -ENOMEM;
1213 goto out_subsys_put;
1216 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1218 mutex_lock(&subsys->su_mutex);
1219 if (type->ct_group_ops->make_group) {
1220 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1221 if (!group)
1222 group = ERR_PTR(-ENOMEM);
1223 if (!IS_ERR(group)) {
1224 link_group(to_config_group(parent_item), group);
1225 item = &group->cg_item;
1226 } else
1227 ret = PTR_ERR(group);
1228 } else {
1229 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1230 if (!item)
1231 item = ERR_PTR(-ENOMEM);
1232 if (!IS_ERR(item))
1233 link_obj(parent_item, item);
1234 else
1235 ret = PTR_ERR(item);
1237 mutex_unlock(&subsys->su_mutex);
1239 kfree(name);
1240 if (ret) {
1242 * If ret != 0, then link_obj() was never called.
1243 * There are no extra references to clean up.
1245 goto out_subsys_put;
1249 * link_obj() has been called (via link_group() for groups).
1250 * From here on out, errors must clean that up.
1253 type = item->ci_type;
1254 if (!type) {
1255 ret = -EINVAL;
1256 goto out_unlink;
1259 new_item_owner = type->ct_owner;
1260 if (!try_module_get(new_item_owner)) {
1261 ret = -EINVAL;
1262 goto out_unlink;
1266 * I hate doing it this way, but if there is
1267 * an error, module_put() probably should
1268 * happen after any cleanup.
1270 module_got = 1;
1273 * Make racing rmdir() fail if it did not tag parent with
1274 * CONFIGFS_USET_DROPPING
1275 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1276 * fail and let rmdir() terminate correctly
1278 spin_lock(&configfs_dirent_lock);
1279 /* This will make configfs_detach_prep() fail */
1280 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1281 spin_unlock(&configfs_dirent_lock);
1283 if (group)
1284 ret = configfs_attach_group(parent_item, item, dentry);
1285 else
1286 ret = configfs_attach_item(parent_item, item, dentry);
1288 spin_lock(&configfs_dirent_lock);
1289 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1290 if (!ret)
1291 configfs_dir_set_ready(dentry->d_fsdata);
1292 spin_unlock(&configfs_dirent_lock);
1294 out_unlink:
1295 if (ret) {
1296 /* Tear down everything we built up */
1297 mutex_lock(&subsys->su_mutex);
1299 client_disconnect_notify(parent_item, item);
1300 if (group)
1301 unlink_group(group);
1302 else
1303 unlink_obj(item);
1304 client_drop_item(parent_item, item);
1306 mutex_unlock(&subsys->su_mutex);
1308 if (module_got)
1309 module_put(new_item_owner);
1312 out_subsys_put:
1313 if (ret)
1314 module_put(subsys_owner);
1316 out_put:
1318 * link_obj()/link_group() took a reference from child->parent,
1319 * so the parent is safely pinned. We can drop our working
1320 * reference.
1322 config_item_put(parent_item);
1324 out:
1325 return ret;
1328 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1330 struct config_item *parent_item;
1331 struct config_item *item;
1332 struct configfs_subsystem *subsys;
1333 struct configfs_dirent *sd;
1334 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1335 int ret;
1337 sd = dentry->d_fsdata;
1338 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1339 return -EPERM;
1341 /* Get a working ref until we have the child */
1342 parent_item = configfs_get_config_item(dentry->d_parent);
1343 subsys = to_config_group(parent_item)->cg_subsys;
1344 BUG_ON(!subsys);
1346 if (!parent_item->ci_type) {
1347 config_item_put(parent_item);
1348 return -EINVAL;
1351 /* configfs_mkdir() shouldn't have allowed this */
1352 BUG_ON(!subsys->su_group.cg_item.ci_type);
1353 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1356 * Ensure that no racing symlink() will make detach_prep() fail while
1357 * the new link is temporarily attached
1359 do {
1360 struct mutex *wait_mutex;
1362 mutex_lock(&configfs_symlink_mutex);
1363 spin_lock(&configfs_dirent_lock);
1365 * Here's where we check for dependents. We're protected by
1366 * configfs_dirent_lock.
1367 * If no dependent, atomically tag the item as dropping.
1369 ret = sd->s_dependent_count ? -EBUSY : 0;
1370 if (!ret) {
1371 ret = configfs_detach_prep(dentry, &wait_mutex);
1372 if (ret)
1373 configfs_detach_rollback(dentry);
1375 spin_unlock(&configfs_dirent_lock);
1376 mutex_unlock(&configfs_symlink_mutex);
1378 if (ret) {
1379 if (ret != -EAGAIN) {
1380 config_item_put(parent_item);
1381 return ret;
1384 /* Wait until the racing operation terminates */
1385 mutex_lock(wait_mutex);
1386 mutex_unlock(wait_mutex);
1388 } while (ret == -EAGAIN);
1390 /* Get a working ref for the duration of this function */
1391 item = configfs_get_config_item(dentry);
1393 /* Drop reference from above, item already holds one. */
1394 config_item_put(parent_item);
1396 if (item->ci_type)
1397 dead_item_owner = item->ci_type->ct_owner;
1399 if (sd->s_type & CONFIGFS_USET_DIR) {
1400 configfs_detach_group(item);
1402 mutex_lock(&subsys->su_mutex);
1403 client_disconnect_notify(parent_item, item);
1404 unlink_group(to_config_group(item));
1405 } else {
1406 configfs_detach_item(item);
1408 mutex_lock(&subsys->su_mutex);
1409 client_disconnect_notify(parent_item, item);
1410 unlink_obj(item);
1413 client_drop_item(parent_item, item);
1414 mutex_unlock(&subsys->su_mutex);
1416 /* Drop our reference from above */
1417 config_item_put(item);
1419 module_put(dead_item_owner);
1420 module_put(subsys_owner);
1422 return 0;
1425 const struct inode_operations configfs_dir_inode_operations = {
1426 .mkdir = configfs_mkdir,
1427 .rmdir = configfs_rmdir,
1428 .symlink = configfs_symlink,
1429 .unlink = configfs_unlink,
1430 .lookup = configfs_lookup,
1431 .setattr = configfs_setattr,
1434 const struct inode_operations configfs_root_inode_operations = {
1435 .lookup = configfs_lookup,
1436 .setattr = configfs_setattr,
1439 #if 0
1440 int configfs_rename_dir(struct config_item * item, const char *new_name)
1442 int error = 0;
1443 struct dentry * new_dentry, * parent;
1445 if (!strcmp(config_item_name(item), new_name))
1446 return -EINVAL;
1448 if (!item->parent)
1449 return -EINVAL;
1451 down_write(&configfs_rename_sem);
1452 parent = item->parent->dentry;
1454 mutex_lock(&d_inode(parent)->i_mutex);
1456 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1457 if (!IS_ERR(new_dentry)) {
1458 if (d_really_is_negative(new_dentry)) {
1459 error = config_item_set_name(item, "%s", new_name);
1460 if (!error) {
1461 d_add(new_dentry, NULL);
1462 d_move(item->dentry, new_dentry);
1464 else
1465 d_delete(new_dentry);
1466 } else
1467 error = -EEXIST;
1468 dput(new_dentry);
1470 mutex_unlock(&d_inode(parent)->i_mutex);
1471 up_write(&configfs_rename_sem);
1473 return error;
1475 #endif
1477 static int configfs_dir_open(struct inode *inode, struct file *file)
1479 struct dentry * dentry = file->f_path.dentry;
1480 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1481 int err;
1483 mutex_lock(&d_inode(dentry)->i_mutex);
1485 * Fake invisibility if dir belongs to a group/default groups hierarchy
1486 * being attached
1488 err = -ENOENT;
1489 if (configfs_dirent_is_ready(parent_sd)) {
1490 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1491 if (IS_ERR(file->private_data))
1492 err = PTR_ERR(file->private_data);
1493 else
1494 err = 0;
1496 mutex_unlock(&d_inode(dentry)->i_mutex);
1498 return err;
1501 static int configfs_dir_close(struct inode *inode, struct file *file)
1503 struct dentry * dentry = file->f_path.dentry;
1504 struct configfs_dirent * cursor = file->private_data;
1506 mutex_lock(&d_inode(dentry)->i_mutex);
1507 spin_lock(&configfs_dirent_lock);
1508 list_del_init(&cursor->s_sibling);
1509 spin_unlock(&configfs_dirent_lock);
1510 mutex_unlock(&d_inode(dentry)->i_mutex);
1512 release_configfs_dirent(cursor);
1514 return 0;
1517 /* Relationship between s_mode and the DT_xxx types */
1518 static inline unsigned char dt_type(struct configfs_dirent *sd)
1520 return (sd->s_mode >> 12) & 15;
1523 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1525 struct dentry *dentry = file->f_path.dentry;
1526 struct super_block *sb = dentry->d_sb;
1527 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1528 struct configfs_dirent *cursor = file->private_data;
1529 struct list_head *p, *q = &cursor->s_sibling;
1530 ino_t ino = 0;
1532 if (!dir_emit_dots(file, ctx))
1533 return 0;
1534 if (ctx->pos == 2) {
1535 spin_lock(&configfs_dirent_lock);
1536 list_move(q, &parent_sd->s_children);
1537 spin_unlock(&configfs_dirent_lock);
1539 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1540 struct configfs_dirent *next;
1541 const char *name;
1542 int len;
1543 struct inode *inode = NULL;
1545 next = list_entry(p, struct configfs_dirent, s_sibling);
1546 if (!next->s_element)
1547 continue;
1549 name = configfs_get_name(next);
1550 len = strlen(name);
1553 * We'll have a dentry and an inode for
1554 * PINNED items and for open attribute
1555 * files. We lock here to prevent a race
1556 * with configfs_d_iput() clearing
1557 * s_dentry before calling iput().
1559 * Why do we go to the trouble? If
1560 * someone has an attribute file open,
1561 * the inode number should match until
1562 * they close it. Beyond that, we don't
1563 * care.
1565 spin_lock(&configfs_dirent_lock);
1566 dentry = next->s_dentry;
1567 if (dentry)
1568 inode = d_inode(dentry);
1569 if (inode)
1570 ino = inode->i_ino;
1571 spin_unlock(&configfs_dirent_lock);
1572 if (!inode)
1573 ino = iunique(sb, 2);
1575 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1576 return 0;
1578 spin_lock(&configfs_dirent_lock);
1579 list_move(q, p);
1580 spin_unlock(&configfs_dirent_lock);
1581 p = q;
1582 ctx->pos++;
1584 return 0;
1587 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1589 struct dentry * dentry = file->f_path.dentry;
1591 mutex_lock(&d_inode(dentry)->i_mutex);
1592 switch (whence) {
1593 case 1:
1594 offset += file->f_pos;
1595 case 0:
1596 if (offset >= 0)
1597 break;
1598 default:
1599 mutex_unlock(&d_inode(dentry)->i_mutex);
1600 return -EINVAL;
1602 if (offset != file->f_pos) {
1603 file->f_pos = offset;
1604 if (file->f_pos >= 2) {
1605 struct configfs_dirent *sd = dentry->d_fsdata;
1606 struct configfs_dirent *cursor = file->private_data;
1607 struct list_head *p;
1608 loff_t n = file->f_pos - 2;
1610 spin_lock(&configfs_dirent_lock);
1611 list_del(&cursor->s_sibling);
1612 p = sd->s_children.next;
1613 while (n && p != &sd->s_children) {
1614 struct configfs_dirent *next;
1615 next = list_entry(p, struct configfs_dirent,
1616 s_sibling);
1617 if (next->s_element)
1618 n--;
1619 p = p->next;
1621 list_add_tail(&cursor->s_sibling, p);
1622 spin_unlock(&configfs_dirent_lock);
1625 mutex_unlock(&d_inode(dentry)->i_mutex);
1626 return offset;
1629 const struct file_operations configfs_dir_operations = {
1630 .open = configfs_dir_open,
1631 .release = configfs_dir_close,
1632 .llseek = configfs_dir_lseek,
1633 .read = generic_read_dir,
1634 .iterate = configfs_readdir,
1638 * configfs_register_group - creates a parent-child relation between two groups
1639 * @parent_group: parent group
1640 * @group: child group
1642 * link groups, creates dentry for the child and attaches it to the
1643 * parent dentry.
1645 * Return: 0 on success, negative errno code on error
1647 int configfs_register_group(struct config_group *parent_group,
1648 struct config_group *group)
1650 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1651 struct dentry *parent;
1652 int ret;
1654 mutex_lock(&subsys->su_mutex);
1655 link_group(parent_group, group);
1656 mutex_unlock(&subsys->su_mutex);
1658 parent = parent_group->cg_item.ci_dentry;
1660 mutex_lock_nested(&d_inode(parent)->i_mutex, I_MUTEX_PARENT);
1661 ret = create_default_group(parent_group, group);
1662 if (!ret) {
1663 spin_lock(&configfs_dirent_lock);
1664 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1665 spin_unlock(&configfs_dirent_lock);
1667 mutex_unlock(&d_inode(parent)->i_mutex);
1668 return ret;
1670 EXPORT_SYMBOL(configfs_register_group);
1673 * configfs_unregister_group() - unregisters a child group from its parent
1674 * @group: parent group to be unregistered
1676 * Undoes configfs_register_group()
1678 void configfs_unregister_group(struct config_group *group)
1680 struct configfs_subsystem *subsys = group->cg_subsys;
1681 struct dentry *dentry = group->cg_item.ci_dentry;
1682 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1684 mutex_lock_nested(&d_inode(parent)->i_mutex, I_MUTEX_PARENT);
1685 spin_lock(&configfs_dirent_lock);
1686 configfs_detach_prep(dentry, NULL);
1687 spin_unlock(&configfs_dirent_lock);
1689 configfs_detach_group(&group->cg_item);
1690 d_inode(dentry)->i_flags |= S_DEAD;
1691 dont_mount(dentry);
1692 d_delete(dentry);
1693 mutex_unlock(&d_inode(parent)->i_mutex);
1695 dput(dentry);
1697 mutex_lock(&subsys->su_mutex);
1698 unlink_group(group);
1699 mutex_unlock(&subsys->su_mutex);
1701 EXPORT_SYMBOL(configfs_unregister_group);
1704 * configfs_register_default_group() - allocates and registers a child group
1705 * @parent_group: parent group
1706 * @name: child group name
1707 * @item_type: child item type description
1709 * boilerplate to allocate and register a child group with its parent. We need
1710 * kzalloc'ed memory because child's default_group is initially empty.
1712 * Return: allocated config group or ERR_PTR() on error
1714 struct config_group *
1715 configfs_register_default_group(struct config_group *parent_group,
1716 const char *name,
1717 struct config_item_type *item_type)
1719 int ret;
1720 struct config_group *group;
1722 group = kzalloc(sizeof(*group), GFP_KERNEL);
1723 if (!group)
1724 return ERR_PTR(-ENOMEM);
1725 config_group_init_type_name(group, name, item_type);
1727 ret = configfs_register_group(parent_group, group);
1728 if (ret) {
1729 kfree(group);
1730 return ERR_PTR(ret);
1732 return group;
1734 EXPORT_SYMBOL(configfs_register_default_group);
1737 * configfs_unregister_default_group() - unregisters and frees a child group
1738 * @group: the group to act on
1740 void configfs_unregister_default_group(struct config_group *group)
1742 configfs_unregister_group(group);
1743 kfree(group);
1745 EXPORT_SYMBOL(configfs_unregister_default_group);
1747 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1749 int err;
1750 struct config_group *group = &subsys->su_group;
1751 struct dentry *dentry;
1752 struct dentry *root;
1753 struct configfs_dirent *sd;
1755 root = configfs_pin_fs();
1756 if (IS_ERR(root))
1757 return PTR_ERR(root);
1759 if (!group->cg_item.ci_name)
1760 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1762 sd = root->d_fsdata;
1763 link_group(to_config_group(sd->s_element), group);
1765 mutex_lock_nested(&d_inode(root)->i_mutex, I_MUTEX_PARENT);
1767 err = -ENOMEM;
1768 dentry = d_alloc_name(root, group->cg_item.ci_name);
1769 if (dentry) {
1770 d_add(dentry, NULL);
1772 err = configfs_attach_group(sd->s_element, &group->cg_item,
1773 dentry);
1774 if (err) {
1775 BUG_ON(d_inode(dentry));
1776 d_drop(dentry);
1777 dput(dentry);
1778 } else {
1779 spin_lock(&configfs_dirent_lock);
1780 configfs_dir_set_ready(dentry->d_fsdata);
1781 spin_unlock(&configfs_dirent_lock);
1785 mutex_unlock(&d_inode(root)->i_mutex);
1787 if (err) {
1788 unlink_group(group);
1789 configfs_release_fs();
1792 return err;
1795 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1797 struct config_group *group = &subsys->su_group;
1798 struct dentry *dentry = group->cg_item.ci_dentry;
1799 struct dentry *root = dentry->d_sb->s_root;
1801 if (dentry->d_parent != root) {
1802 pr_err("Tried to unregister non-subsystem!\n");
1803 return;
1806 mutex_lock_nested(&d_inode(root)->i_mutex,
1807 I_MUTEX_PARENT);
1808 mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_CHILD);
1809 mutex_lock(&configfs_symlink_mutex);
1810 spin_lock(&configfs_dirent_lock);
1811 if (configfs_detach_prep(dentry, NULL)) {
1812 pr_err("Tried to unregister non-empty subsystem!\n");
1814 spin_unlock(&configfs_dirent_lock);
1815 mutex_unlock(&configfs_symlink_mutex);
1816 configfs_detach_group(&group->cg_item);
1817 d_inode(dentry)->i_flags |= S_DEAD;
1818 dont_mount(dentry);
1819 mutex_unlock(&d_inode(dentry)->i_mutex);
1821 d_delete(dentry);
1823 mutex_unlock(&d_inode(root)->i_mutex);
1825 dput(dentry);
1827 unlink_group(group);
1828 configfs_release_fs();
1831 EXPORT_SYMBOL(configfs_register_subsystem);
1832 EXPORT_SYMBOL(configfs_unregister_subsystem);