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