RDS: Heap OOB write in rds_message_alloc_sgs()
[linux/fpc-iii.git] / fs / configfs / dir.c
bloba7a1b218f308af56ac3094fa5f2ddb6b2749dd4a
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);
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.
69 if (atomic_read(&sd->s_count) <= 2)
70 sd->s_dentry = NULL;
72 spin_unlock(&configfs_dirent_lock);
73 configfs_put(sd);
75 iput(inode);
78 const struct dentry_operations configfs_dentry_ops = {
79 .d_iput = configfs_d_iput,
80 .d_delete = always_delete_dentry,
83 #ifdef CONFIG_LOCKDEP
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.
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].
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].
104 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
106 sd->s_depth = -1;
109 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
110 struct configfs_dirent *sd)
112 int parent_depth = parent_sd->s_depth;
114 if (parent_depth >= 0)
115 sd->s_depth = parent_depth + 1;
118 static void
119 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
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.
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()).
131 if (sd->s_depth == -1)
133 * We are a non default group and we are going to create
134 * default groups.
136 sd->s_depth = 0;
139 static void
140 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
142 /* We will not create default groups anymore. */
143 sd->s_depth = -1;
146 #else /* CONFIG_LOCKDEP */
148 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
152 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
153 struct configfs_dirent *sd)
157 static void
158 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
162 static void
163 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
167 #endif /* CONFIG_LOCKDEP */
170 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
172 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
173 void *element, int type)
175 struct configfs_dirent * sd;
177 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
178 if (!sd)
179 return ERR_PTR(-ENOMEM);
181 atomic_set(&sd->s_count, 1);
182 INIT_LIST_HEAD(&sd->s_links);
183 INIT_LIST_HEAD(&sd->s_children);
184 sd->s_element = element;
185 sd->s_type = type;
186 configfs_init_dirent_depth(sd);
187 spin_lock(&configfs_dirent_lock);
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);
193 list_add(&sd->s_sibling, &parent_sd->s_children);
194 spin_unlock(&configfs_dirent_lock);
196 return sd;
201 * Return -EEXIST if there is already a configfs element with the same
202 * name for the same parent.
204 * called with parent inode's i_mutex held
206 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
207 const unsigned char *new)
209 struct configfs_dirent * sd;
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;
221 return 0;
225 int configfs_make_dirent(struct configfs_dirent * parent_sd,
226 struct dentry * dentry, void * element,
227 umode_t mode, int type)
229 struct configfs_dirent * sd;
231 sd = configfs_new_dirent(parent_sd, element, type);
232 if (IS_ERR(sd))
233 return PTR_ERR(sd);
235 sd->s_mode = mode;
236 sd->s_dentry = dentry;
237 if (dentry)
238 dentry->d_fsdata = configfs_get(sd);
240 return 0;
243 static void init_dir(struct inode * inode)
245 inode->i_op = &configfs_dir_inode_operations;
246 inode->i_fop = &configfs_dir_operations;
248 /* directory inodes start off with i_nlink == 2 (for "." entry) */
249 inc_nlink(inode);
252 static void configfs_init_file(struct inode * inode)
254 inode->i_size = PAGE_SIZE;
255 inode->i_fop = &configfs_file_operations;
258 static void init_symlink(struct inode * inode)
260 inode->i_op = &configfs_symlink_inode_operations;
264 * configfs_create_dir - create a directory for an config_item.
265 * @item: config_itemwe're creating directory for.
266 * @dentry: config_item's dentry.
268 * Note: user-created entries won't be allowed under this new directory
269 * until it is validated by configfs_dir_set_ready()
272 static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
274 int error;
275 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
276 struct dentry *p = dentry->d_parent;
278 BUG_ON(!item);
280 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
281 if (unlikely(error))
282 return error;
284 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
285 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
286 if (unlikely(error))
287 return error;
289 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
290 error = configfs_create(dentry, mode, init_dir);
291 if (!error) {
292 inc_nlink(d_inode(p));
293 item->ci_dentry = dentry;
294 } else {
295 struct configfs_dirent *sd = dentry->d_fsdata;
296 if (sd) {
297 spin_lock(&configfs_dirent_lock);
298 list_del_init(&sd->s_sibling);
299 spin_unlock(&configfs_dirent_lock);
300 configfs_put(sd);
303 return error;
307 * Allow userspace to create new entries under a new directory created with
308 * configfs_create_dir(), and under all of its chidlren directories recursively.
309 * @sd configfs_dirent of the new directory to validate
311 * Caller must hold configfs_dirent_lock.
313 static void configfs_dir_set_ready(struct configfs_dirent *sd)
315 struct configfs_dirent *child_sd;
317 sd->s_type &= ~CONFIGFS_USET_CREATING;
318 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
319 if (child_sd->s_type & CONFIGFS_USET_CREATING)
320 configfs_dir_set_ready(child_sd);
324 * Check that a directory does not belong to a directory hierarchy being
325 * attached and not validated yet.
326 * @sd configfs_dirent of the directory to check
328 * @return non-zero iff the directory was validated
330 * Note: takes configfs_dirent_lock, so the result may change from false to true
331 * in two consecutive calls, but never from true to false.
333 int configfs_dirent_is_ready(struct configfs_dirent *sd)
335 int ret;
337 spin_lock(&configfs_dirent_lock);
338 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
339 spin_unlock(&configfs_dirent_lock);
341 return ret;
344 int configfs_create_link(struct configfs_symlink *sl,
345 struct dentry *parent,
346 struct dentry *dentry)
348 int err = 0;
349 umode_t mode = S_IFLNK | S_IRWXUGO;
351 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
352 CONFIGFS_ITEM_LINK);
353 if (!err) {
354 err = configfs_create(dentry, mode, init_symlink);
355 if (err) {
356 struct configfs_dirent *sd = dentry->d_fsdata;
357 if (sd) {
358 spin_lock(&configfs_dirent_lock);
359 list_del_init(&sd->s_sibling);
360 spin_unlock(&configfs_dirent_lock);
361 configfs_put(sd);
365 return err;
368 static void remove_dir(struct dentry * d)
370 struct dentry * parent = dget(d->d_parent);
371 struct configfs_dirent * sd;
373 sd = d->d_fsdata;
374 spin_lock(&configfs_dirent_lock);
375 list_del_init(&sd->s_sibling);
376 spin_unlock(&configfs_dirent_lock);
377 configfs_put(sd);
378 if (d_really_is_positive(d))
379 simple_rmdir(d_inode(parent),d);
381 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
383 dput(parent);
387 * configfs_remove_dir - remove an config_item's directory.
388 * @item: config_item we're removing.
390 * The only thing special about this is that we remove any files in
391 * the directory before we remove the directory, and we've inlined
392 * what used to be configfs_rmdir() below, instead of calling separately.
394 * Caller holds the mutex of the item's inode
397 static void configfs_remove_dir(struct config_item * item)
399 struct dentry * dentry = dget(item->ci_dentry);
401 if (!dentry)
402 return;
404 remove_dir(dentry);
406 * Drop reference from dget() on entrance.
408 dput(dentry);
412 /* attaches attribute's configfs_dirent to the dentry corresponding to the
413 * attribute file
415 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
417 struct configfs_attribute * attr = sd->s_element;
418 int error;
420 spin_lock(&configfs_dirent_lock);
421 dentry->d_fsdata = configfs_get(sd);
422 sd->s_dentry = dentry;
423 spin_unlock(&configfs_dirent_lock);
425 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
426 configfs_init_file);
427 if (error) {
428 configfs_put(sd);
429 return error;
432 d_rehash(dentry);
434 return 0;
437 static struct dentry * configfs_lookup(struct inode *dir,
438 struct dentry *dentry,
439 unsigned int flags)
441 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
442 struct configfs_dirent * sd;
443 int found = 0;
444 int err;
447 * Fake invisibility if dir belongs to a group/default groups hierarchy
448 * being attached
450 * This forbids userspace to read/write attributes of items which may
451 * not complete their initialization, since the dentries of the
452 * attributes won't be instantiated.
454 err = -ENOENT;
455 if (!configfs_dirent_is_ready(parent_sd))
456 goto out;
458 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
459 if (sd->s_type & CONFIGFS_NOT_PINNED) {
460 const unsigned char * name = configfs_get_name(sd);
462 if (strcmp(name, dentry->d_name.name))
463 continue;
465 found = 1;
466 err = configfs_attach_attr(sd, dentry);
467 break;
471 if (!found) {
473 * If it doesn't exist and it isn't a NOT_PINNED item,
474 * it must be negative.
476 if (dentry->d_name.len > NAME_MAX)
477 return ERR_PTR(-ENAMETOOLONG);
478 d_add(dentry, NULL);
479 return NULL;
482 out:
483 return ERR_PTR(err);
487 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
488 * attributes and are removed by rmdir(). We recurse, setting
489 * CONFIGFS_USET_DROPPING on all children that are candidates for
490 * default detach.
491 * If there is an error, the caller will reset the flags via
492 * configfs_detach_rollback().
494 static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
496 struct configfs_dirent *parent_sd = dentry->d_fsdata;
497 struct configfs_dirent *sd;
498 int ret;
500 /* Mark that we're trying to drop the group */
501 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
503 ret = -EBUSY;
504 if (!list_empty(&parent_sd->s_links))
505 goto out;
507 ret = 0;
508 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
509 if (!sd->s_element ||
510 (sd->s_type & CONFIGFS_NOT_PINNED))
511 continue;
512 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
513 /* Abort if racing with mkdir() */
514 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
515 if (wait_mutex)
516 *wait_mutex = &d_inode(sd->s_dentry)->i_mutex;
517 return -EAGAIN;
521 * Yup, recursive. If there's a problem, blame
522 * deep nesting of default_groups
524 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
525 if (!ret)
526 continue;
527 } else
528 ret = -ENOTEMPTY;
530 break;
533 out:
534 return ret;
538 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
539 * set.
541 static void configfs_detach_rollback(struct dentry *dentry)
543 struct configfs_dirent *parent_sd = dentry->d_fsdata;
544 struct configfs_dirent *sd;
546 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
548 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
549 if (sd->s_type & CONFIGFS_USET_DEFAULT)
550 configfs_detach_rollback(sd->s_dentry);
553 static void detach_attrs(struct config_item * item)
555 struct dentry * dentry = dget(item->ci_dentry);
556 struct configfs_dirent * parent_sd;
557 struct configfs_dirent * sd, * tmp;
559 if (!dentry)
560 return;
562 pr_debug("configfs %s: dropping attrs for dir\n",
563 dentry->d_name.name);
565 parent_sd = dentry->d_fsdata;
566 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
567 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
568 continue;
569 spin_lock(&configfs_dirent_lock);
570 list_del_init(&sd->s_sibling);
571 spin_unlock(&configfs_dirent_lock);
572 configfs_drop_dentry(sd, dentry);
573 configfs_put(sd);
577 * Drop reference from dget() on entrance.
579 dput(dentry);
582 static int populate_attrs(struct config_item *item)
584 struct config_item_type *t = item->ci_type;
585 struct configfs_attribute *attr;
586 int error = 0;
587 int i;
589 if (!t)
590 return -EINVAL;
591 if (t->ct_attrs) {
592 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
593 if ((error = configfs_create_file(item, attr)))
594 break;
598 if (error)
599 detach_attrs(item);
601 return error;
604 static int configfs_attach_group(struct config_item *parent_item,
605 struct config_item *item,
606 struct dentry *dentry);
607 static void configfs_detach_group(struct config_item *item);
609 static void detach_groups(struct config_group *group)
611 struct dentry * dentry = dget(group->cg_item.ci_dentry);
612 struct dentry *child;
613 struct configfs_dirent *parent_sd;
614 struct configfs_dirent *sd, *tmp;
616 if (!dentry)
617 return;
619 parent_sd = dentry->d_fsdata;
620 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
621 if (!sd->s_element ||
622 !(sd->s_type & CONFIGFS_USET_DEFAULT))
623 continue;
625 child = sd->s_dentry;
627 mutex_lock(&d_inode(child)->i_mutex);
629 configfs_detach_group(sd->s_element);
630 d_inode(child)->i_flags |= S_DEAD;
631 dont_mount(child);
633 mutex_unlock(&d_inode(child)->i_mutex);
635 d_delete(child);
636 dput(child);
640 * Drop reference from dget() on entrance.
642 dput(dentry);
646 * This fakes mkdir(2) on a default_groups[] entry. It
647 * creates a dentry, attachs it, and then does fixup
648 * on the sd->s_type.
650 * We could, perhaps, tweak our parent's ->mkdir for a minute and
651 * try using vfs_mkdir. Just a thought.
653 static int create_default_group(struct config_group *parent_group,
654 struct config_group *group)
656 int ret;
657 struct configfs_dirent *sd;
658 /* We trust the caller holds a reference to parent */
659 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
661 if (!group->cg_item.ci_name)
662 group->cg_item.ci_name = group->cg_item.ci_namebuf;
664 ret = -ENOMEM;
665 child = d_alloc_name(parent, group->cg_item.ci_name);
666 if (child) {
667 d_add(child, NULL);
669 ret = configfs_attach_group(&parent_group->cg_item,
670 &group->cg_item, child);
671 if (!ret) {
672 sd = child->d_fsdata;
673 sd->s_type |= CONFIGFS_USET_DEFAULT;
674 } else {
675 BUG_ON(d_inode(child));
676 d_drop(child);
677 dput(child);
681 return ret;
684 static int populate_groups(struct config_group *group)
686 struct config_group *new_group;
687 int ret = 0;
688 int i;
690 if (group->default_groups) {
691 for (i = 0; group->default_groups[i]; i++) {
692 new_group = group->default_groups[i];
694 ret = create_default_group(group, new_group);
695 if (ret) {
696 detach_groups(group);
697 break;
702 return ret;
706 * All of link_obj/unlink_obj/link_group/unlink_group require that
707 * subsys->su_mutex is held.
710 static void unlink_obj(struct config_item *item)
712 struct config_group *group;
714 group = item->ci_group;
715 if (group) {
716 list_del_init(&item->ci_entry);
718 item->ci_group = NULL;
719 item->ci_parent = NULL;
721 /* Drop the reference for ci_entry */
722 config_item_put(item);
724 /* Drop the reference for ci_parent */
725 config_group_put(group);
729 static void link_obj(struct config_item *parent_item, struct config_item *item)
732 * Parent seems redundant with group, but it makes certain
733 * traversals much nicer.
735 item->ci_parent = parent_item;
738 * We hold a reference on the parent for the child's ci_parent
739 * link.
741 item->ci_group = config_group_get(to_config_group(parent_item));
742 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
745 * We hold a reference on the child for ci_entry on the parent's
746 * cg_children
748 config_item_get(item);
751 static void unlink_group(struct config_group *group)
753 int i;
754 struct config_group *new_group;
756 if (group->default_groups) {
757 for (i = 0; group->default_groups[i]; i++) {
758 new_group = group->default_groups[i];
759 unlink_group(new_group);
763 group->cg_subsys = NULL;
764 unlink_obj(&group->cg_item);
767 static void link_group(struct config_group *parent_group, struct config_group *group)
769 int i;
770 struct config_group *new_group;
771 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
773 link_obj(&parent_group->cg_item, &group->cg_item);
775 if (parent_group->cg_subsys)
776 subsys = parent_group->cg_subsys;
777 else if (configfs_is_root(&parent_group->cg_item))
778 subsys = to_configfs_subsystem(group);
779 else
780 BUG();
781 group->cg_subsys = subsys;
783 if (group->default_groups) {
784 for (i = 0; group->default_groups[i]; i++) {
785 new_group = group->default_groups[i];
786 link_group(group, new_group);
792 * The goal is that configfs_attach_item() (and
793 * configfs_attach_group()) can be called from either the VFS or this
794 * module. That is, they assume that the items have been created,
795 * the dentry allocated, and the dcache is all ready to go.
797 * If they fail, they must clean up after themselves as if they
798 * had never been called. The caller (VFS or local function) will
799 * handle cleaning up the dcache bits.
801 * configfs_detach_group() and configfs_detach_item() behave similarly on
802 * the way out. They assume that the proper semaphores are held, they
803 * clean up the configfs items, and they expect their callers will
804 * handle the dcache bits.
806 static int configfs_attach_item(struct config_item *parent_item,
807 struct config_item *item,
808 struct dentry *dentry)
810 int ret;
812 ret = configfs_create_dir(item, dentry);
813 if (!ret) {
814 ret = populate_attrs(item);
815 if (ret) {
817 * We are going to remove an inode and its dentry but
818 * the VFS may already have hit and used them. Thus,
819 * we must lock them as rmdir() would.
821 mutex_lock(&d_inode(dentry)->i_mutex);
822 configfs_remove_dir(item);
823 d_inode(dentry)->i_flags |= S_DEAD;
824 dont_mount(dentry);
825 mutex_unlock(&d_inode(dentry)->i_mutex);
826 d_delete(dentry);
830 return ret;
833 /* Caller holds the mutex of the item's inode */
834 static void configfs_detach_item(struct config_item *item)
836 detach_attrs(item);
837 configfs_remove_dir(item);
840 static int configfs_attach_group(struct config_item *parent_item,
841 struct config_item *item,
842 struct dentry *dentry)
844 int ret;
845 struct configfs_dirent *sd;
847 ret = configfs_attach_item(parent_item, item, dentry);
848 if (!ret) {
849 sd = dentry->d_fsdata;
850 sd->s_type |= CONFIGFS_USET_DIR;
853 * FYI, we're faking mkdir in populate_groups()
854 * We must lock the group's inode to avoid races with the VFS
855 * which can already hit the inode and try to add/remove entries
856 * under it.
858 * We must also lock the inode to remove it safely in case of
859 * error, as rmdir() would.
861 mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_CHILD);
862 configfs_adjust_dir_dirent_depth_before_populate(sd);
863 ret = populate_groups(to_config_group(item));
864 if (ret) {
865 configfs_detach_item(item);
866 d_inode(dentry)->i_flags |= S_DEAD;
867 dont_mount(dentry);
869 configfs_adjust_dir_dirent_depth_after_populate(sd);
870 mutex_unlock(&d_inode(dentry)->i_mutex);
871 if (ret)
872 d_delete(dentry);
875 return ret;
878 /* Caller holds the mutex of the group's inode */
879 static void configfs_detach_group(struct config_item *item)
881 detach_groups(to_config_group(item));
882 configfs_detach_item(item);
886 * After the item has been detached from the filesystem view, we are
887 * ready to tear it out of the hierarchy. Notify the client before
888 * we do that so they can perform any cleanup that requires
889 * navigating the hierarchy. A client does not need to provide this
890 * callback. The subsystem semaphore MUST be held by the caller, and
891 * references must be valid for both items. It also assumes the
892 * caller has validated ci_type.
894 static void client_disconnect_notify(struct config_item *parent_item,
895 struct config_item *item)
897 struct config_item_type *type;
899 type = parent_item->ci_type;
900 BUG_ON(!type);
902 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
903 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
904 item);
908 * Drop the initial reference from make_item()/make_group()
909 * This function assumes that reference is held on item
910 * and that item holds a valid reference to the parent. Also, it
911 * assumes the caller has validated ci_type.
913 static void client_drop_item(struct config_item *parent_item,
914 struct config_item *item)
916 struct config_item_type *type;
918 type = parent_item->ci_type;
919 BUG_ON(!type);
922 * If ->drop_item() exists, it is responsible for the
923 * config_item_put().
925 if (type->ct_group_ops && type->ct_group_ops->drop_item)
926 type->ct_group_ops->drop_item(to_config_group(parent_item),
927 item);
928 else
929 config_item_put(item);
932 #ifdef DEBUG
933 static void configfs_dump_one(struct configfs_dirent *sd, int level)
935 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
937 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
938 type_print(CONFIGFS_ROOT);
939 type_print(CONFIGFS_DIR);
940 type_print(CONFIGFS_ITEM_ATTR);
941 type_print(CONFIGFS_ITEM_LINK);
942 type_print(CONFIGFS_USET_DIR);
943 type_print(CONFIGFS_USET_DEFAULT);
944 type_print(CONFIGFS_USET_DROPPING);
945 #undef type_print
948 static int configfs_dump(struct configfs_dirent *sd, int level)
950 struct configfs_dirent *child_sd;
951 int ret = 0;
953 configfs_dump_one(sd, level);
955 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
956 return 0;
958 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
959 ret = configfs_dump(child_sd, level + 2);
960 if (ret)
961 break;
964 return ret;
966 #endif
970 * configfs_depend_item() and configfs_undepend_item()
972 * WARNING: Do not call these from a configfs callback!
974 * This describes these functions and their helpers.
976 * Allow another kernel system to depend on a config_item. If this
977 * happens, the item cannot go away until the dependent can live without
978 * it. The idea is to give client modules as simple an interface as
979 * possible. When a system asks them to depend on an item, they just
980 * call configfs_depend_item(). If the item is live and the client
981 * driver is in good shape, we'll happily do the work for them.
983 * Why is the locking complex? Because configfs uses the VFS to handle
984 * all locking, but this function is called outside the normal
985 * VFS->configfs path. So it must take VFS locks to prevent the
986 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
987 * why you can't call these functions underneath configfs callbacks.
989 * Note, btw, that this can be called at *any* time, even when a configfs
990 * subsystem isn't registered, or when configfs is loading or unloading.
991 * Just like configfs_register_subsystem(). So we take the same
992 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
993 * If we can find the target item in the
994 * configfs tree, it must be part of the subsystem tree as well, so we
995 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
996 * locking out mkdir() and rmdir(), who might be racing us.
1000 * configfs_depend_prep()
1002 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1003 * attributes. This is similar but not the same to configfs_detach_prep().
1004 * Note that configfs_detach_prep() expects the parent to be locked when it
1005 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1006 * do that so we can unlock it if we find nothing.
1008 * Here we do a depth-first search of the dentry hierarchy looking for
1009 * our object.
1010 * We deliberately ignore items tagged as dropping since they are virtually
1011 * dead, as well as items in the middle of attachment since they virtually
1012 * do not exist yet. This completes the locking out of racing mkdir() and
1013 * rmdir().
1014 * Note: subdirectories in the middle of attachment start with s_type =
1015 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1016 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1017 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1019 * If the target is not found, -ENOENT is bubbled up.
1021 * This adds a requirement that all config_items be unique!
1023 * This is recursive. There isn't
1024 * much on the stack, though, so folks that need this function - be careful
1025 * about your stack! Patches will be accepted to make it iterative.
1027 static int configfs_depend_prep(struct dentry *origin,
1028 struct config_item *target)
1030 struct configfs_dirent *child_sd, *sd;
1031 int ret = 0;
1033 BUG_ON(!origin || !origin->d_fsdata);
1034 sd = origin->d_fsdata;
1036 if (sd->s_element == target) /* Boo-yah */
1037 goto out;
1039 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1040 if ((child_sd->s_type & CONFIGFS_DIR) &&
1041 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1042 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1043 ret = configfs_depend_prep(child_sd->s_dentry,
1044 target);
1045 if (!ret)
1046 goto out; /* Child path boo-yah */
1050 /* We looped all our children and didn't find target */
1051 ret = -ENOENT;
1053 out:
1054 return ret;
1057 int configfs_depend_item(struct configfs_subsystem *subsys,
1058 struct config_item *target)
1060 int ret;
1061 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1062 struct config_item *s_item = &subsys->su_group.cg_item;
1063 struct dentry *root;
1066 * Pin the configfs filesystem. This means we can safely access
1067 * the root of the configfs filesystem.
1069 root = configfs_pin_fs();
1070 if (IS_ERR(root))
1071 return PTR_ERR(root);
1074 * Next, lock the root directory. We're going to check that the
1075 * subsystem is really registered, and so we need to lock out
1076 * configfs_[un]register_subsystem().
1078 mutex_lock(&d_inode(root)->i_mutex);
1080 root_sd = root->d_fsdata;
1082 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1083 if (p->s_type & CONFIGFS_DIR) {
1084 if (p->s_element == s_item) {
1085 subsys_sd = p;
1086 break;
1091 if (!subsys_sd) {
1092 ret = -ENOENT;
1093 goto out_unlock_fs;
1096 /* Ok, now we can trust subsys/s_item */
1098 spin_lock(&configfs_dirent_lock);
1099 /* Scan the tree, return 0 if found */
1100 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1101 if (ret)
1102 goto out_unlock_dirent_lock;
1105 * We are sure that the item is not about to be removed by rmdir(), and
1106 * not in the middle of attachment by mkdir().
1108 p = target->ci_dentry->d_fsdata;
1109 p->s_dependent_count += 1;
1111 out_unlock_dirent_lock:
1112 spin_unlock(&configfs_dirent_lock);
1113 out_unlock_fs:
1114 mutex_unlock(&d_inode(root)->i_mutex);
1117 * If we succeeded, the fs is pinned via other methods. If not,
1118 * we're done with it anyway. So release_fs() is always right.
1120 configfs_release_fs();
1122 return ret;
1124 EXPORT_SYMBOL(configfs_depend_item);
1127 * Release the dependent linkage. This is much simpler than
1128 * configfs_depend_item() because we know that that the client driver is
1129 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1131 void configfs_undepend_item(struct configfs_subsystem *subsys,
1132 struct config_item *target)
1134 struct configfs_dirent *sd;
1137 * Since we can trust everything is pinned, we just need
1138 * configfs_dirent_lock.
1140 spin_lock(&configfs_dirent_lock);
1142 sd = target->ci_dentry->d_fsdata;
1143 BUG_ON(sd->s_dependent_count < 1);
1145 sd->s_dependent_count -= 1;
1148 * After this unlock, we cannot trust the item to stay alive!
1149 * DO NOT REFERENCE item after this unlock.
1151 spin_unlock(&configfs_dirent_lock);
1153 EXPORT_SYMBOL(configfs_undepend_item);
1155 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1157 int ret = 0;
1158 int module_got = 0;
1159 struct config_group *group = NULL;
1160 struct config_item *item = NULL;
1161 struct config_item *parent_item;
1162 struct configfs_subsystem *subsys;
1163 struct configfs_dirent *sd;
1164 struct config_item_type *type;
1165 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1166 char *name;
1168 sd = dentry->d_parent->d_fsdata;
1171 * Fake invisibility if dir belongs to a group/default groups hierarchy
1172 * being attached
1174 if (!configfs_dirent_is_ready(sd)) {
1175 ret = -ENOENT;
1176 goto out;
1179 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1180 ret = -EPERM;
1181 goto out;
1184 /* Get a working ref for the duration of this function */
1185 parent_item = configfs_get_config_item(dentry->d_parent);
1186 type = parent_item->ci_type;
1187 subsys = to_config_group(parent_item)->cg_subsys;
1188 BUG_ON(!subsys);
1190 if (!type || !type->ct_group_ops ||
1191 (!type->ct_group_ops->make_group &&
1192 !type->ct_group_ops->make_item)) {
1193 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1194 goto out_put;
1198 * The subsystem may belong to a different module than the item
1199 * being created. We don't want to safely pin the new item but
1200 * fail to pin the subsystem it sits under.
1202 if (!subsys->su_group.cg_item.ci_type) {
1203 ret = -EINVAL;
1204 goto out_put;
1206 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1207 if (!try_module_get(subsys_owner)) {
1208 ret = -EINVAL;
1209 goto out_put;
1212 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1213 if (!name) {
1214 ret = -ENOMEM;
1215 goto out_subsys_put;
1218 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1220 mutex_lock(&subsys->su_mutex);
1221 if (type->ct_group_ops->make_group) {
1222 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1223 if (!group)
1224 group = ERR_PTR(-ENOMEM);
1225 if (!IS_ERR(group)) {
1226 link_group(to_config_group(parent_item), group);
1227 item = &group->cg_item;
1228 } else
1229 ret = PTR_ERR(group);
1230 } else {
1231 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1232 if (!item)
1233 item = ERR_PTR(-ENOMEM);
1234 if (!IS_ERR(item))
1235 link_obj(parent_item, item);
1236 else
1237 ret = PTR_ERR(item);
1239 mutex_unlock(&subsys->su_mutex);
1241 kfree(name);
1242 if (ret) {
1244 * If ret != 0, then link_obj() was never called.
1245 * There are no extra references to clean up.
1247 goto out_subsys_put;
1251 * link_obj() has been called (via link_group() for groups).
1252 * From here on out, errors must clean that up.
1255 type = item->ci_type;
1256 if (!type) {
1257 ret = -EINVAL;
1258 goto out_unlink;
1261 new_item_owner = type->ct_owner;
1262 if (!try_module_get(new_item_owner)) {
1263 ret = -EINVAL;
1264 goto out_unlink;
1268 * I hate doing it this way, but if there is
1269 * an error, module_put() probably should
1270 * happen after any cleanup.
1272 module_got = 1;
1275 * Make racing rmdir() fail if it did not tag parent with
1276 * CONFIGFS_USET_DROPPING
1277 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1278 * fail and let rmdir() terminate correctly
1280 spin_lock(&configfs_dirent_lock);
1281 /* This will make configfs_detach_prep() fail */
1282 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1283 spin_unlock(&configfs_dirent_lock);
1285 if (group)
1286 ret = configfs_attach_group(parent_item, item, dentry);
1287 else
1288 ret = configfs_attach_item(parent_item, item, dentry);
1290 spin_lock(&configfs_dirent_lock);
1291 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1292 if (!ret)
1293 configfs_dir_set_ready(dentry->d_fsdata);
1294 spin_unlock(&configfs_dirent_lock);
1296 out_unlink:
1297 if (ret) {
1298 /* Tear down everything we built up */
1299 mutex_lock(&subsys->su_mutex);
1301 client_disconnect_notify(parent_item, item);
1302 if (group)
1303 unlink_group(group);
1304 else
1305 unlink_obj(item);
1306 client_drop_item(parent_item, item);
1308 mutex_unlock(&subsys->su_mutex);
1310 if (module_got)
1311 module_put(new_item_owner);
1314 out_subsys_put:
1315 if (ret)
1316 module_put(subsys_owner);
1318 out_put:
1320 * link_obj()/link_group() took a reference from child->parent,
1321 * so the parent is safely pinned. We can drop our working
1322 * reference.
1324 config_item_put(parent_item);
1326 out:
1327 return ret;
1330 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1332 struct config_item *parent_item;
1333 struct config_item *item;
1334 struct configfs_subsystem *subsys;
1335 struct configfs_dirent *sd;
1336 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1337 int ret;
1339 sd = dentry->d_fsdata;
1340 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1341 return -EPERM;
1343 /* Get a working ref until we have the child */
1344 parent_item = configfs_get_config_item(dentry->d_parent);
1345 subsys = to_config_group(parent_item)->cg_subsys;
1346 BUG_ON(!subsys);
1348 if (!parent_item->ci_type) {
1349 config_item_put(parent_item);
1350 return -EINVAL;
1353 /* configfs_mkdir() shouldn't have allowed this */
1354 BUG_ON(!subsys->su_group.cg_item.ci_type);
1355 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1358 * Ensure that no racing symlink() will make detach_prep() fail while
1359 * the new link is temporarily attached
1361 do {
1362 struct mutex *wait_mutex;
1364 mutex_lock(&configfs_symlink_mutex);
1365 spin_lock(&configfs_dirent_lock);
1367 * Here's where we check for dependents. We're protected by
1368 * configfs_dirent_lock.
1369 * If no dependent, atomically tag the item as dropping.
1371 ret = sd->s_dependent_count ? -EBUSY : 0;
1372 if (!ret) {
1373 ret = configfs_detach_prep(dentry, &wait_mutex);
1374 if (ret)
1375 configfs_detach_rollback(dentry);
1377 spin_unlock(&configfs_dirent_lock);
1378 mutex_unlock(&configfs_symlink_mutex);
1380 if (ret) {
1381 if (ret != -EAGAIN) {
1382 config_item_put(parent_item);
1383 return ret;
1386 /* Wait until the racing operation terminates */
1387 mutex_lock(wait_mutex);
1388 mutex_unlock(wait_mutex);
1390 } while (ret == -EAGAIN);
1392 /* Get a working ref for the duration of this function */
1393 item = configfs_get_config_item(dentry);
1395 /* Drop reference from above, item already holds one. */
1396 config_item_put(parent_item);
1398 if (item->ci_type)
1399 dead_item_owner = item->ci_type->ct_owner;
1401 if (sd->s_type & CONFIGFS_USET_DIR) {
1402 configfs_detach_group(item);
1404 mutex_lock(&subsys->su_mutex);
1405 client_disconnect_notify(parent_item, item);
1406 unlink_group(to_config_group(item));
1407 } else {
1408 configfs_detach_item(item);
1410 mutex_lock(&subsys->su_mutex);
1411 client_disconnect_notify(parent_item, item);
1412 unlink_obj(item);
1415 client_drop_item(parent_item, item);
1416 mutex_unlock(&subsys->su_mutex);
1418 /* Drop our reference from above */
1419 config_item_put(item);
1421 module_put(dead_item_owner);
1422 module_put(subsys_owner);
1424 return 0;
1427 const struct inode_operations configfs_dir_inode_operations = {
1428 .mkdir = configfs_mkdir,
1429 .rmdir = configfs_rmdir,
1430 .symlink = configfs_symlink,
1431 .unlink = configfs_unlink,
1432 .lookup = configfs_lookup,
1433 .setattr = configfs_setattr,
1436 const struct inode_operations configfs_root_inode_operations = {
1437 .lookup = configfs_lookup,
1438 .setattr = configfs_setattr,
1441 #if 0
1442 int configfs_rename_dir(struct config_item * item, const char *new_name)
1444 int error = 0;
1445 struct dentry * new_dentry, * parent;
1447 if (!strcmp(config_item_name(item), new_name))
1448 return -EINVAL;
1450 if (!item->parent)
1451 return -EINVAL;
1453 down_write(&configfs_rename_sem);
1454 parent = item->parent->dentry;
1456 mutex_lock(&d_inode(parent)->i_mutex);
1458 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1459 if (!IS_ERR(new_dentry)) {
1460 if (d_really_is_negative(new_dentry)) {
1461 error = config_item_set_name(item, "%s", new_name);
1462 if (!error) {
1463 d_add(new_dentry, NULL);
1464 d_move(item->dentry, new_dentry);
1466 else
1467 d_delete(new_dentry);
1468 } else
1469 error = -EEXIST;
1470 dput(new_dentry);
1472 mutex_unlock(&d_inode(parent)->i_mutex);
1473 up_write(&configfs_rename_sem);
1475 return error;
1477 #endif
1479 static int configfs_dir_open(struct inode *inode, struct file *file)
1481 struct dentry * dentry = file->f_path.dentry;
1482 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1483 int err;
1485 mutex_lock(&d_inode(dentry)->i_mutex);
1487 * Fake invisibility if dir belongs to a group/default groups hierarchy
1488 * being attached
1490 err = -ENOENT;
1491 if (configfs_dirent_is_ready(parent_sd)) {
1492 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1493 if (IS_ERR(file->private_data))
1494 err = PTR_ERR(file->private_data);
1495 else
1496 err = 0;
1498 mutex_unlock(&d_inode(dentry)->i_mutex);
1500 return err;
1503 static int configfs_dir_close(struct inode *inode, struct file *file)
1505 struct dentry * dentry = file->f_path.dentry;
1506 struct configfs_dirent * cursor = file->private_data;
1508 mutex_lock(&d_inode(dentry)->i_mutex);
1509 spin_lock(&configfs_dirent_lock);
1510 list_del_init(&cursor->s_sibling);
1511 spin_unlock(&configfs_dirent_lock);
1512 mutex_unlock(&d_inode(dentry)->i_mutex);
1514 release_configfs_dirent(cursor);
1516 return 0;
1519 /* Relationship between s_mode and the DT_xxx types */
1520 static inline unsigned char dt_type(struct configfs_dirent *sd)
1522 return (sd->s_mode >> 12) & 15;
1525 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1527 struct dentry *dentry = file->f_path.dentry;
1528 struct super_block *sb = dentry->d_sb;
1529 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1530 struct configfs_dirent *cursor = file->private_data;
1531 struct list_head *p, *q = &cursor->s_sibling;
1532 ino_t ino = 0;
1534 if (!dir_emit_dots(file, ctx))
1535 return 0;
1536 if (ctx->pos == 2) {
1537 spin_lock(&configfs_dirent_lock);
1538 list_move(q, &parent_sd->s_children);
1539 spin_unlock(&configfs_dirent_lock);
1541 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1542 struct configfs_dirent *next;
1543 const char *name;
1544 int len;
1545 struct inode *inode = NULL;
1547 next = list_entry(p, struct configfs_dirent, s_sibling);
1548 if (!next->s_element)
1549 continue;
1551 name = configfs_get_name(next);
1552 len = strlen(name);
1555 * We'll have a dentry and an inode for
1556 * PINNED items and for open attribute
1557 * files. We lock here to prevent a race
1558 * with configfs_d_iput() clearing
1559 * s_dentry before calling iput().
1561 * Why do we go to the trouble? If
1562 * someone has an attribute file open,
1563 * the inode number should match until
1564 * they close it. Beyond that, we don't
1565 * care.
1567 spin_lock(&configfs_dirent_lock);
1568 dentry = next->s_dentry;
1569 if (dentry)
1570 inode = d_inode(dentry);
1571 if (inode)
1572 ino = inode->i_ino;
1573 spin_unlock(&configfs_dirent_lock);
1574 if (!inode)
1575 ino = iunique(sb, 2);
1577 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1578 return 0;
1580 spin_lock(&configfs_dirent_lock);
1581 list_move(q, p);
1582 spin_unlock(&configfs_dirent_lock);
1583 p = q;
1584 ctx->pos++;
1586 return 0;
1589 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1591 struct dentry * dentry = file->f_path.dentry;
1593 mutex_lock(&d_inode(dentry)->i_mutex);
1594 switch (whence) {
1595 case 1:
1596 offset += file->f_pos;
1597 case 0:
1598 if (offset >= 0)
1599 break;
1600 default:
1601 mutex_unlock(&d_inode(dentry)->i_mutex);
1602 return -EINVAL;
1604 if (offset != file->f_pos) {
1605 file->f_pos = offset;
1606 if (file->f_pos >= 2) {
1607 struct configfs_dirent *sd = dentry->d_fsdata;
1608 struct configfs_dirent *cursor = file->private_data;
1609 struct list_head *p;
1610 loff_t n = file->f_pos - 2;
1612 spin_lock(&configfs_dirent_lock);
1613 list_del(&cursor->s_sibling);
1614 p = sd->s_children.next;
1615 while (n && p != &sd->s_children) {
1616 struct configfs_dirent *next;
1617 next = list_entry(p, struct configfs_dirent,
1618 s_sibling);
1619 if (next->s_element)
1620 n--;
1621 p = p->next;
1623 list_add_tail(&cursor->s_sibling, p);
1624 spin_unlock(&configfs_dirent_lock);
1627 mutex_unlock(&d_inode(dentry)->i_mutex);
1628 return offset;
1631 const struct file_operations configfs_dir_operations = {
1632 .open = configfs_dir_open,
1633 .release = configfs_dir_close,
1634 .llseek = configfs_dir_lseek,
1635 .read = generic_read_dir,
1636 .iterate = configfs_readdir,
1640 * configfs_register_group - creates a parent-child relation between two groups
1641 * @parent_group: parent group
1642 * @group: child group
1644 * link groups, creates dentry for the child and attaches it to the
1645 * parent dentry.
1647 * Return: 0 on success, negative errno code on error
1649 int configfs_register_group(struct config_group *parent_group,
1650 struct config_group *group)
1652 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1653 struct dentry *parent;
1654 int ret;
1656 mutex_lock(&subsys->su_mutex);
1657 link_group(parent_group, group);
1658 mutex_unlock(&subsys->su_mutex);
1660 parent = parent_group->cg_item.ci_dentry;
1662 mutex_lock_nested(&d_inode(parent)->i_mutex, I_MUTEX_PARENT);
1663 ret = create_default_group(parent_group, group);
1664 if (!ret) {
1665 spin_lock(&configfs_dirent_lock);
1666 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1667 spin_unlock(&configfs_dirent_lock);
1669 mutex_unlock(&d_inode(parent)->i_mutex);
1670 return ret;
1672 EXPORT_SYMBOL(configfs_register_group);
1675 * configfs_unregister_group() - unregisters a child group from its parent
1676 * @group: parent group to be unregistered
1678 * Undoes configfs_register_group()
1680 void configfs_unregister_group(struct config_group *group)
1682 struct configfs_subsystem *subsys = group->cg_subsys;
1683 struct dentry *dentry = group->cg_item.ci_dentry;
1684 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1686 mutex_lock_nested(&d_inode(parent)->i_mutex, I_MUTEX_PARENT);
1687 spin_lock(&configfs_dirent_lock);
1688 configfs_detach_prep(dentry, NULL);
1689 spin_unlock(&configfs_dirent_lock);
1691 configfs_detach_group(&group->cg_item);
1692 d_inode(dentry)->i_flags |= S_DEAD;
1693 dont_mount(dentry);
1694 d_delete(dentry);
1695 mutex_unlock(&d_inode(parent)->i_mutex);
1697 dput(dentry);
1699 mutex_lock(&subsys->su_mutex);
1700 unlink_group(group);
1701 mutex_unlock(&subsys->su_mutex);
1703 EXPORT_SYMBOL(configfs_unregister_group);
1706 * configfs_register_default_group() - allocates and registers a child group
1707 * @parent_group: parent group
1708 * @name: child group name
1709 * @item_type: child item type description
1711 * boilerplate to allocate and register a child group with its parent. We need
1712 * kzalloc'ed memory because child's default_group is initially empty.
1714 * Return: allocated config group or ERR_PTR() on error
1716 struct config_group *
1717 configfs_register_default_group(struct config_group *parent_group,
1718 const char *name,
1719 struct config_item_type *item_type)
1721 int ret;
1722 struct config_group *group;
1724 group = kzalloc(sizeof(*group), GFP_KERNEL);
1725 if (!group)
1726 return ERR_PTR(-ENOMEM);
1727 config_group_init_type_name(group, name, item_type);
1729 ret = configfs_register_group(parent_group, group);
1730 if (ret) {
1731 kfree(group);
1732 return ERR_PTR(ret);
1734 return group;
1736 EXPORT_SYMBOL(configfs_register_default_group);
1739 * configfs_unregister_default_group() - unregisters and frees a child group
1740 * @group: the group to act on
1742 void configfs_unregister_default_group(struct config_group *group)
1744 configfs_unregister_group(group);
1745 kfree(group);
1747 EXPORT_SYMBOL(configfs_unregister_default_group);
1749 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1751 int err;
1752 struct config_group *group = &subsys->su_group;
1753 struct dentry *dentry;
1754 struct dentry *root;
1755 struct configfs_dirent *sd;
1757 root = configfs_pin_fs();
1758 if (IS_ERR(root))
1759 return PTR_ERR(root);
1761 if (!group->cg_item.ci_name)
1762 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1764 sd = root->d_fsdata;
1765 link_group(to_config_group(sd->s_element), group);
1767 mutex_lock_nested(&d_inode(root)->i_mutex, I_MUTEX_PARENT);
1769 err = -ENOMEM;
1770 dentry = d_alloc_name(root, group->cg_item.ci_name);
1771 if (dentry) {
1772 d_add(dentry, NULL);
1774 err = configfs_attach_group(sd->s_element, &group->cg_item,
1775 dentry);
1776 if (err) {
1777 BUG_ON(d_inode(dentry));
1778 d_drop(dentry);
1779 dput(dentry);
1780 } else {
1781 spin_lock(&configfs_dirent_lock);
1782 configfs_dir_set_ready(dentry->d_fsdata);
1783 spin_unlock(&configfs_dirent_lock);
1787 mutex_unlock(&d_inode(root)->i_mutex);
1789 if (err) {
1790 unlink_group(group);
1791 configfs_release_fs();
1794 return err;
1797 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1799 struct config_group *group = &subsys->su_group;
1800 struct dentry *dentry = group->cg_item.ci_dentry;
1801 struct dentry *root = dentry->d_sb->s_root;
1803 if (dentry->d_parent != root) {
1804 pr_err("Tried to unregister non-subsystem!\n");
1805 return;
1808 mutex_lock_nested(&d_inode(root)->i_mutex,
1809 I_MUTEX_PARENT);
1810 mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_CHILD);
1811 mutex_lock(&configfs_symlink_mutex);
1812 spin_lock(&configfs_dirent_lock);
1813 if (configfs_detach_prep(dentry, NULL)) {
1814 pr_err("Tried to unregister non-empty subsystem!\n");
1816 spin_unlock(&configfs_dirent_lock);
1817 mutex_unlock(&configfs_symlink_mutex);
1818 configfs_detach_group(&group->cg_item);
1819 d_inode(dentry)->i_flags |= S_DEAD;
1820 dont_mount(dentry);
1821 mutex_unlock(&d_inode(dentry)->i_mutex);
1823 d_delete(dentry);
1825 mutex_unlock(&d_inode(root)->i_mutex);
1827 dput(dentry);
1829 unlink_group(group);
1830 configfs_release_fs();
1833 EXPORT_SYMBOL(configfs_register_subsystem);
1834 EXPORT_SYMBOL(configfs_unregister_subsystem);