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