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