Blame view

fs/configfs/dir.c 49.4 KB
7063fbf22   Joel Becker   [PATCH] configfs:...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  /* -*- mode: c; c-basic-offset: 8; -*-
   * vim: noexpandtab sw=8 ts=8 sts=0:
   *
   * dir.c - Operations for configfs directories.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License as published by the Free Software Foundation; either
   * version 2 of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   *
   * Based on sysfs:
   * 	sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
   *
   * configfs Copyright (C) 2005 Oracle.  All rights reserved.
   */
  
  #undef DEBUG
  
  #include <linux/fs.h>
  #include <linux/mount.h>
  #include <linux/module.h>
  #include <linux/slab.h>
107ed40bd   Louis Rilling   configfs: Make co...
33
  #include <linux/err.h>
7063fbf22   Joel Becker   [PATCH] configfs:...
34
35
36
37
38
  
  #include <linux/configfs.h>
  #include "configfs_internal.h"
  
  DECLARE_RWSEM(configfs_rename_sem);
6f6107640   Louis Rilling   configfs: Introdu...
39
40
  /*
   * Protects mutations of configfs_dirent linkage together with proper i_mutex
5301a77da   Louis Rilling   configfs: Protect...
41
   * Also protects mutations of symlinks linkage to target configfs_dirent
6f6107640   Louis Rilling   configfs: Introdu...
42
43
   * Mutators of configfs_dirent linkage must *both* have the proper inode locked
   * and configfs_dirent_lock locked, in that order.
5301a77da   Louis Rilling   configfs: Protect...
44
45
   * This allows one to safely traverse configfs_dirent trees and symlinks without
   * having to lock inodes.
b3e76af87   Louis Rilling   configfs: Fix dea...
46
47
48
49
   *
   * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
   * unlocked is not reliable unless in detach_groups() called from
   * rmdir()/unregister() and from configfs_attach_group()
6f6107640   Louis Rilling   configfs: Introdu...
50
51
   */
  DEFINE_SPINLOCK(configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
52
53
54
55
  
  static void configfs_d_iput(struct dentry * dentry,
  			    struct inode * inode)
  {
24307aa1e   Joel Becker   configfs: Fix rac...
56
  	struct configfs_dirent *sd = dentry->d_fsdata;
7063fbf22   Joel Becker   [PATCH] configfs:...
57
58
  
  	if (sd) {
24307aa1e   Joel Becker   configfs: Fix rac...
59
60
  		/* Coordinate with configfs_readdir */
  		spin_lock(&configfs_dirent_lock);
76ae281f6   Junxiao Bi   configfs: fix rac...
61
62
63
64
65
66
67
68
69
70
  		/* Coordinate with configfs_attach_attr where will increase
  		 * sd->s_count and update sd->s_dentry to new allocated one.
  		 * Only set sd->dentry to null when this dentry is the only
  		 * sd owner.
  		 * If not do so, configfs_d_iput may run just after
  		 * configfs_attach_attr and set sd->s_dentry to null
  		 * even it's still in use.
  		 */
  		if (atomic_read(&sd->s_count) <= 2)
  			sd->s_dentry = NULL;
24307aa1e   Joel Becker   configfs: Fix rac...
71
  		spin_unlock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
72
73
74
75
  		configfs_put(sd);
  	}
  	iput(inode);
  }
d463a0c4b   Al Viro   switch configfs
76
  const struct dentry_operations configfs_dentry_ops = {
7063fbf22   Joel Becker   [PATCH] configfs:...
77
  	.d_iput		= configfs_d_iput,
b26d4cd38   Al Viro   consolidate simpl...
78
  	.d_delete	= always_delete_dentry,
7063fbf22   Joel Becker   [PATCH] configfs:...
79
  };
e74cc06df   Louis Rilling   configfs: Silence...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  #ifdef CONFIG_LOCKDEP
  
  /*
   * Helpers to make lockdep happy with our recursive locking of default groups'
   * inodes (see configfs_attach_group() and configfs_detach_group()).
   * We put default groups i_mutexes in separate classes according to their depth
   * from the youngest non-default group ancestor.
   *
   * For a non-default group A having default groups A/B, A/C, and A/C/D, default
   * groups A/B and A/C will have their inode's mutex in class
   * default_group_class[0], and default group A/C/D will be in
   * default_group_class[1].
   *
   * The lock classes are declared and assigned in inode.c, according to the
   * s_depth value.
   * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
   * default groups, and reset to -1 when all default groups are attached. During
   * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
   * inode's mutex is set to default_group_class[s_depth - 1].
   */
  
  static void configfs_init_dirent_depth(struct configfs_dirent *sd)
  {
  	sd->s_depth = -1;
  }
  
  static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
  					  struct configfs_dirent *sd)
  {
  	int parent_depth = parent_sd->s_depth;
  
  	if (parent_depth >= 0)
  		sd->s_depth = parent_depth + 1;
  }
  
  static void
  configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
  {
  	/*
  	 * item's i_mutex class is already setup, so s_depth is now only
  	 * used to set new sub-directories s_depth, which is always done
  	 * with item's i_mutex locked.
  	 */
  	/*
  	 *  sd->s_depth == -1 iff we are a non default group.
  	 *  else (we are a default group) sd->s_depth > 0 (see
  	 *  create_dir()).
  	 */
  	if (sd->s_depth == -1)
  		/*
  		 * We are a non default group and we are going to create
  		 * default groups.
  		 */
  		sd->s_depth = 0;
  }
  
  static void
  configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
  {
  	/* We will not create default groups anymore. */
  	sd->s_depth = -1;
  }
  
  #else /* CONFIG_LOCKDEP */
  
  static void configfs_init_dirent_depth(struct configfs_dirent *sd)
  {
  }
  
  static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
  					  struct configfs_dirent *sd)
  {
  }
  
  static void
  configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
  {
  }
  
  static void
  configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
  {
  }
  
  #endif /* CONFIG_LOCKDEP */
7063fbf22   Joel Becker   [PATCH] configfs:...
165
166
167
  /*
   * Allocates a new configfs_dirent and links it to the parent configfs_dirent
   */
420118caa   Louis Rilling   configfs: Rework ...
168
169
  static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
  						   void *element, int type)
7063fbf22   Joel Becker   [PATCH] configfs:...
170
171
  {
  	struct configfs_dirent * sd;
c37622296   Robert P. J. Day   [PATCH] Transform...
172
  	sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
7063fbf22   Joel Becker   [PATCH] configfs:...
173
  	if (!sd)
107ed40bd   Louis Rilling   configfs: Make co...
174
  		return ERR_PTR(-ENOMEM);
7063fbf22   Joel Becker   [PATCH] configfs:...
175

7063fbf22   Joel Becker   [PATCH] configfs:...
176
177
178
  	atomic_set(&sd->s_count, 1);
  	INIT_LIST_HEAD(&sd->s_links);
  	INIT_LIST_HEAD(&sd->s_children);
7063fbf22   Joel Becker   [PATCH] configfs:...
179
  	sd->s_element = element;
420118caa   Louis Rilling   configfs: Rework ...
180
  	sd->s_type = type;
e74cc06df   Louis Rilling   configfs: Silence...
181
  	configfs_init_dirent_depth(sd);
6f6107640   Louis Rilling   configfs: Introdu...
182
  	spin_lock(&configfs_dirent_lock);
b3e76af87   Louis Rilling   configfs: Fix dea...
183
184
185
186
187
  	if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
  		spin_unlock(&configfs_dirent_lock);
  		kmem_cache_free(configfs_dir_cachep, sd);
  		return ERR_PTR(-ENOENT);
  	}
6f6107640   Louis Rilling   configfs: Introdu...
188
189
  	list_add(&sd->s_sibling, &parent_sd->s_children);
  	spin_unlock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
190
191
192
  
  	return sd;
  }
b4c98f625   Joel Becker   configfs: Prevent...
193
194
195
196
197
198
199
  /*
   *
   * Return -EEXIST if there is already a configfs element with the same
   * name for the same parent.
   *
   * called with parent inode's i_mutex held
   */
58d206c2f   Adrian Bunk   configfs: make co...
200
201
  static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
  				  const unsigned char *new)
b4c98f625   Joel Becker   configfs: Prevent...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  {
  	struct configfs_dirent * sd;
  
  	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  		if (sd->s_element) {
  			const unsigned char *existing = configfs_get_name(sd);
  			if (strcmp(existing, new))
  				continue;
  			else
  				return -EEXIST;
  		}
  	}
  
  	return 0;
  }
7063fbf22   Joel Becker   [PATCH] configfs:...
217
218
219
220
221
  int configfs_make_dirent(struct configfs_dirent * parent_sd,
  			 struct dentry * dentry, void * element,
  			 umode_t mode, int type)
  {
  	struct configfs_dirent * sd;
420118caa   Louis Rilling   configfs: Rework ...
222
  	sd = configfs_new_dirent(parent_sd, element, type);
107ed40bd   Louis Rilling   configfs: Make co...
223
224
  	if (IS_ERR(sd))
  		return PTR_ERR(sd);
7063fbf22   Joel Becker   [PATCH] configfs:...
225
226
  
  	sd->s_mode = mode;
7063fbf22   Joel Becker   [PATCH] configfs:...
227
  	sd->s_dentry = dentry;
fbc8d4c04   Nick Piggin   config fs: avoid ...
228
  	if (dentry)
7063fbf22   Joel Becker   [PATCH] configfs:...
229
  		dentry->d_fsdata = configfs_get(sd);
7063fbf22   Joel Becker   [PATCH] configfs:...
230
231
232
  
  	return 0;
  }
c88b1e70a   Al Viro   configfs: configf...
233
  static void init_dir(struct inode * inode)
7063fbf22   Joel Becker   [PATCH] configfs:...
234
235
236
237
238
  {
  	inode->i_op = &configfs_dir_inode_operations;
  	inode->i_fop = &configfs_dir_operations;
  
  	/* directory inodes start off with i_nlink == 2 (for "." entry) */
d8c76e6f4   Dave Hansen   [PATCH] r/o bind ...
239
  	inc_nlink(inode);
7063fbf22   Joel Becker   [PATCH] configfs:...
240
  }
c88b1e70a   Al Viro   configfs: configf...
241
  static void configfs_init_file(struct inode * inode)
7063fbf22   Joel Becker   [PATCH] configfs:...
242
243
244
  {
  	inode->i_size = PAGE_SIZE;
  	inode->i_fop = &configfs_file_operations;
7063fbf22   Joel Becker   [PATCH] configfs:...
245
  }
03607ace8   Pantelis Antoniou   configfs: impleme...
246
247
248
249
250
  static void configfs_init_bin_file(struct inode *inode)
  {
  	inode->i_size = 0;
  	inode->i_fop = &configfs_bin_file_operations;
  }
c88b1e70a   Al Viro   configfs: configf...
251
  static void init_symlink(struct inode * inode)
7063fbf22   Joel Becker   [PATCH] configfs:...
252
253
  {
  	inode->i_op = &configfs_symlink_inode_operations;
7063fbf22   Joel Becker   [PATCH] configfs:...
254
  }
7063fbf22   Joel Becker   [PATCH] configfs:...
255
256
257
258
  /**
   *	configfs_create_dir - create a directory for an config_item.
   *	@item:		config_itemwe're creating directory for.
   *	@dentry:	config_item's dentry.
2a109f2a4   Louis Rilling   [PATCH] configfs:...
259
260
261
   *
   *	Note: user-created entries won't be allowed under this new directory
   *	until it is validated by configfs_dir_set_ready()
7063fbf22   Joel Becker   [PATCH] configfs:...
262
   */
1cf97d0d3   Al Viro   configfs: fold cr...
263
  static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
7063fbf22   Joel Becker   [PATCH] configfs:...
264
  {
1cf97d0d3   Al Viro   configfs: fold cr...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  	int error;
  	umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  	struct dentry *p = dentry->d_parent;
  
  	BUG_ON(!item);
  
  	error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
  	if (unlikely(error))
  		return error;
  
  	error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
  				     CONFIGFS_DIR | CONFIGFS_USET_CREATING);
  	if (unlikely(error))
  		return error;
  
  	configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
  	error = configfs_create(dentry, mode, init_dir);
  	if (!error) {
2b0143b5c   David Howells   VFS: normal files...
283
  		inc_nlink(d_inode(p));
7063fbf22   Joel Becker   [PATCH] configfs:...
284
  		item->ci_dentry = dentry;
1cf97d0d3   Al Viro   configfs: fold cr...
285
286
287
288
289
290
291
292
293
  	} else {
  		struct configfs_dirent *sd = dentry->d_fsdata;
  		if (sd) {
  			spin_lock(&configfs_dirent_lock);
  			list_del_init(&sd->s_sibling);
  			spin_unlock(&configfs_dirent_lock);
  			configfs_put(sd);
  		}
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
294
295
  	return error;
  }
2a109f2a4   Louis Rilling   [PATCH] configfs:...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  /*
   * Allow userspace to create new entries under a new directory created with
   * configfs_create_dir(), and under all of its chidlren directories recursively.
   * @sd		configfs_dirent of the new directory to validate
   *
   * Caller must hold configfs_dirent_lock.
   */
  static void configfs_dir_set_ready(struct configfs_dirent *sd)
  {
  	struct configfs_dirent *child_sd;
  
  	sd->s_type &= ~CONFIGFS_USET_CREATING;
  	list_for_each_entry(child_sd, &sd->s_children, s_sibling)
  		if (child_sd->s_type & CONFIGFS_USET_CREATING)
  			configfs_dir_set_ready(child_sd);
  }
  
  /*
   * Check that a directory does not belong to a directory hierarchy being
   * attached and not validated yet.
   * @sd		configfs_dirent of the directory to check
   *
   * @return	non-zero iff the directory was validated
   *
   * Note: takes configfs_dirent_lock, so the result may change from false to true
   * in two consecutive calls, but never from true to false.
   */
  int configfs_dirent_is_ready(struct configfs_dirent *sd)
  {
  	int ret;
  
  	spin_lock(&configfs_dirent_lock);
  	ret = !(sd->s_type & CONFIGFS_USET_CREATING);
  	spin_unlock(&configfs_dirent_lock);
  
  	return ret;
  }
7063fbf22   Joel Becker   [PATCH] configfs:...
333
334
335
336
337
338
  int configfs_create_link(struct configfs_symlink *sl,
  			 struct dentry *parent,
  			 struct dentry *dentry)
  {
  	int err = 0;
  	umode_t mode = S_IFLNK | S_IRWXUGO;
3d0f89bb1   Joel Becker   configfs: Add per...
339
340
  	err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
  				   CONFIGFS_ITEM_LINK);
7063fbf22   Joel Becker   [PATCH] configfs:...
341
  	if (!err) {
3d0f89bb1   Joel Becker   configfs: Add per...
342
  		err = configfs_create(dentry, mode, init_symlink);
fbc8d4c04   Nick Piggin   config fs: avoid ...
343
  		if (err) {
3d0f89bb1   Joel Becker   configfs: Add per...
344
345
  			struct configfs_dirent *sd = dentry->d_fsdata;
  			if (sd) {
6f6107640   Louis Rilling   configfs: Introdu...
346
  				spin_lock(&configfs_dirent_lock);
3d0f89bb1   Joel Becker   configfs: Add per...
347
  				list_del_init(&sd->s_sibling);
6f6107640   Louis Rilling   configfs: Introdu...
348
  				spin_unlock(&configfs_dirent_lock);
3d0f89bb1   Joel Becker   configfs: Add per...
349
350
351
  				configfs_put(sd);
  			}
  		}
7063fbf22   Joel Becker   [PATCH] configfs:...
352
353
354
355
356
357
358
359
360
361
  	}
  	return err;
  }
  
  static void remove_dir(struct dentry * d)
  {
  	struct dentry * parent = dget(d->d_parent);
  	struct configfs_dirent * sd;
  
  	sd = d->d_fsdata;
6f6107640   Louis Rilling   configfs: Introdu...
362
  	spin_lock(&configfs_dirent_lock);
e7515d065   Joel Becker   configfs: Clear u...
363
  	list_del_init(&sd->s_sibling);
6f6107640   Louis Rilling   configfs: Introdu...
364
  	spin_unlock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
365
  	configfs_put(sd);
2b0143b5c   David Howells   VFS: normal files...
366
367
  	if (d_really_is_positive(d))
  		simple_rmdir(d_inode(parent),d);
7063fbf22   Joel Becker   [PATCH] configfs:...
368

a455589f1   Al Viro   assorted conversi...
369
370
  	pr_debug(" o %pd removing done (%d)
  ", d, d_count(d));
7063fbf22   Joel Becker   [PATCH] configfs:...
371
372
373
374
375
376
377
378
379
380
381
  
  	dput(parent);
  }
  
  /**
   * configfs_remove_dir - remove an config_item's directory.
   * @item:	config_item we're removing.
   *
   * The only thing special about this is that we remove any files in
   * the directory before we remove the directory, and we've inlined
   * what used to be configfs_rmdir() below, instead of calling separately.
2e2ce171c   Louis Rilling   [PATCH] configfs:...
382
383
   *
   * Caller holds the mutex of the item's inode
7063fbf22   Joel Becker   [PATCH] configfs:...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
   */
  
  static void configfs_remove_dir(struct config_item * item)
  {
  	struct dentry * dentry = dget(item->ci_dentry);
  
  	if (!dentry)
  		return;
  
  	remove_dir(dentry);
  	/**
  	 * Drop reference from dget() on entrance.
  	 */
  	dput(dentry);
  }
  
  
  /* attaches attribute's configfs_dirent to the dentry corresponding to the
   * attribute file
   */
  static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
  {
  	struct configfs_attribute * attr = sd->s_element;
  	int error;
76ae281f6   Junxiao Bi   configfs: fix rac...
408
  	spin_lock(&configfs_dirent_lock);
3d0f89bb1   Joel Becker   configfs: Add per...
409
410
  	dentry->d_fsdata = configfs_get(sd);
  	sd->s_dentry = dentry;
76ae281f6   Junxiao Bi   configfs: fix rac...
411
  	spin_unlock(&configfs_dirent_lock);
ce8d2cdf3   Dave Hansen   r/o bind mounts: ...
412
  	error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
03607ace8   Pantelis Antoniou   configfs: impleme...
413
414
415
  				(sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
  					configfs_init_bin_file :
  					configfs_init_file);
5cf3b560a   Al Viro   configfs: move d_...
416
  	if (error)
3d0f89bb1   Joel Becker   configfs: Add per...
417
  		configfs_put(sd);
5cf3b560a   Al Viro   configfs: move d_...
418
  	return error;
7063fbf22   Joel Becker   [PATCH] configfs:...
419
420
421
422
  }
  
  static struct dentry * configfs_lookup(struct inode *dir,
  				       struct dentry *dentry,
00cd8dd3b   Al Viro   stop passing name...
423
  				       unsigned int flags)
7063fbf22   Joel Becker   [PATCH] configfs:...
424
425
426
427
  {
  	struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
  	struct configfs_dirent * sd;
  	int found = 0;
2a109f2a4   Louis Rilling   [PATCH] configfs:...
428
429
430
431
432
433
434
435
436
437
438
439
440
  	int err;
  
  	/*
  	 * Fake invisibility if dir belongs to a group/default groups hierarchy
  	 * being attached
  	 *
  	 * This forbids userspace to read/write attributes of items which may
  	 * not complete their initialization, since the dentries of the
  	 * attributes won't be instantiated.
  	 */
  	err = -ENOENT;
  	if (!configfs_dirent_is_ready(parent_sd))
  		goto out;
7063fbf22   Joel Becker   [PATCH] configfs:...
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  
  	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  		if (sd->s_type & CONFIGFS_NOT_PINNED) {
  			const unsigned char * name = configfs_get_name(sd);
  
  			if (strcmp(name, dentry->d_name.name))
  				continue;
  
  			found = 1;
  			err = configfs_attach_attr(sd, dentry);
  			break;
  		}
  	}
  
  	if (!found) {
  		/*
  		 * If it doesn't exist and it isn't a NOT_PINNED item,
  		 * it must be negative.
  		 */
fbc8d4c04   Nick Piggin   config fs: avoid ...
460
461
  		if (dentry->d_name.len > NAME_MAX)
  			return ERR_PTR(-ENAMETOOLONG);
fbc8d4c04   Nick Piggin   config fs: avoid ...
462
463
  		d_add(dentry, NULL);
  		return NULL;
7063fbf22   Joel Becker   [PATCH] configfs:...
464
  	}
2a109f2a4   Louis Rilling   [PATCH] configfs:...
465
  out:
7063fbf22   Joel Becker   [PATCH] configfs:...
466
467
468
469
470
  	return ERR_PTR(err);
  }
  
  /*
   * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
b3e76af87   Louis Rilling   configfs: Fix dea...
471
472
473
474
475
   * attributes and are removed by rmdir().  We recurse, setting
   * CONFIGFS_USET_DROPPING on all children that are candidates for
   * default detach.
   * If there is an error, the caller will reset the flags via
   * configfs_detach_rollback().
7063fbf22   Joel Becker   [PATCH] configfs:...
476
   */
48f35b7b7   Al Viro   configfs_detach_p...
477
  static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
7063fbf22   Joel Becker   [PATCH] configfs:...
478
479
480
481
  {
  	struct configfs_dirent *parent_sd = dentry->d_fsdata;
  	struct configfs_dirent *sd;
  	int ret;
4768e9b18   Louis Rilling   [PATCH] configfs:...
482
483
  	/* Mark that we're trying to drop the group */
  	parent_sd->s_type |= CONFIGFS_USET_DROPPING;
7063fbf22   Joel Becker   [PATCH] configfs:...
484
485
486
487
488
489
  	ret = -EBUSY;
  	if (!list_empty(&parent_sd->s_links))
  		goto out;
  
  	ret = 0;
  	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
99cefda42   Louis Rilling   [PATCH] configfs:...
490
491
  		if (!sd->s_element ||
  		    (sd->s_type & CONFIGFS_NOT_PINNED))
7063fbf22   Joel Becker   [PATCH] configfs:...
492
493
  			continue;
  		if (sd->s_type & CONFIGFS_USET_DEFAULT) {
6d8344bae   Louis Rilling   configfs: Fix fai...
494
495
  			/* Abort if racing with mkdir() */
  			if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
48f35b7b7   Al Viro   configfs_detach_p...
496
497
  				if (wait)
  					*wait= dget(sd->s_dentry);
6d8344bae   Louis Rilling   configfs: Fix fai...
498
499
  				return -EAGAIN;
  			}
7063fbf22   Joel Becker   [PATCH] configfs:...
500

631d1feba   Joel Becker   configfs: config ...
501
502
503
504
  			/*
  			 * Yup, recursive.  If there's a problem, blame
  			 * deep nesting of default_groups
  			 */
48f35b7b7   Al Viro   configfs_detach_p...
505
  			ret = configfs_detach_prep(sd->s_dentry, wait);
7063fbf22   Joel Becker   [PATCH] configfs:...
506
  			if (!ret)
e7515d065   Joel Becker   configfs: Clear u...
507
  				continue;
7063fbf22   Joel Becker   [PATCH] configfs:...
508
509
510
511
512
513
514
515
516
517
518
  		} else
  			ret = -ENOTEMPTY;
  
  		break;
  	}
  
  out:
  	return ret;
  }
  
  /*
b3e76af87   Louis Rilling   configfs: Fix dea...
519
   * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
7063fbf22   Joel Becker   [PATCH] configfs:...
520
521
522
523
524
525
   * set.
   */
  static void configfs_detach_rollback(struct dentry *dentry)
  {
  	struct configfs_dirent *parent_sd = dentry->d_fsdata;
  	struct configfs_dirent *sd;
4768e9b18   Louis Rilling   [PATCH] configfs:...
526
527
528
529
  	parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
  
  	list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
  		if (sd->s_type & CONFIGFS_USET_DEFAULT)
7063fbf22   Joel Becker   [PATCH] configfs:...
530
  			configfs_detach_rollback(sd->s_dentry);
7063fbf22   Joel Becker   [PATCH] configfs:...
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  }
  
  static void detach_attrs(struct config_item * item)
  {
  	struct dentry * dentry = dget(item->ci_dentry);
  	struct configfs_dirent * parent_sd;
  	struct configfs_dirent * sd, * tmp;
  
  	if (!dentry)
  		return;
  
  	pr_debug("configfs %s: dropping attrs for  dir
  ",
  		 dentry->d_name.name);
  
  	parent_sd = dentry->d_fsdata;
  	list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  		if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
  			continue;
6f6107640   Louis Rilling   configfs: Introdu...
550
  		spin_lock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
551
  		list_del_init(&sd->s_sibling);
6f6107640   Louis Rilling   configfs: Introdu...
552
  		spin_unlock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
  		configfs_drop_dentry(sd, dentry);
  		configfs_put(sd);
  	}
  
  	/**
  	 * Drop reference from dget() on entrance.
  	 */
  	dput(dentry);
  }
  
  static int populate_attrs(struct config_item *item)
  {
  	struct config_item_type *t = item->ci_type;
  	struct configfs_attribute *attr;
03607ace8   Pantelis Antoniou   configfs: impleme...
567
  	struct configfs_bin_attribute *bin_attr;
7063fbf22   Joel Becker   [PATCH] configfs:...
568
569
570
571
572
573
574
575
576
577
578
  	int error = 0;
  	int i;
  
  	if (!t)
  		return -EINVAL;
  	if (t->ct_attrs) {
  		for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
  			if ((error = configfs_create_file(item, attr)))
  				break;
  		}
  	}
03607ace8   Pantelis Antoniou   configfs: impleme...
579
580
581
582
583
584
585
  	if (t->ct_bin_attrs) {
  		for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
  			error = configfs_create_bin_file(item, bin_attr);
  			if (error)
  				break;
  		}
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
  
  	if (error)
  		detach_attrs(item);
  
  	return error;
  }
  
  static int configfs_attach_group(struct config_item *parent_item,
  				 struct config_item *item,
  				 struct dentry *dentry);
  static void configfs_detach_group(struct config_item *item);
  
  static void detach_groups(struct config_group *group)
  {
  	struct dentry * dentry = dget(group->cg_item.ci_dentry);
  	struct dentry *child;
  	struct configfs_dirent *parent_sd;
  	struct configfs_dirent *sd, *tmp;
  
  	if (!dentry)
  		return;
  
  	parent_sd = dentry->d_fsdata;
  	list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  		if (!sd->s_element ||
  		    !(sd->s_type & CONFIGFS_USET_DEFAULT))
  			continue;
  
  		child = sd->s_dentry;
5955102c9   Al Viro   wrappers for ->i_...
615
  		inode_lock(d_inode(child));
b3e76af87   Louis Rilling   configfs: Fix dea...
616

7063fbf22   Joel Becker   [PATCH] configfs:...
617
  		configfs_detach_group(sd->s_element);
2b0143b5c   David Howells   VFS: normal files...
618
  		d_inode(child)->i_flags |= S_DEAD;
d83c49f3e   Al Viro   Fix the regressio...
619
  		dont_mount(child);
7063fbf22   Joel Becker   [PATCH] configfs:...
620

5955102c9   Al Viro   wrappers for ->i_...
621
  		inode_unlock(d_inode(child));
7063fbf22   Joel Becker   [PATCH] configfs:...
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
  
  		d_delete(child);
  		dput(child);
  	}
  
  	/**
  	 * Drop reference from dget() on entrance.
  	 */
  	dput(dentry);
  }
  
  /*
   * This fakes mkdir(2) on a default_groups[] entry.  It
   * creates a dentry, attachs it, and then does fixup
   * on the sd->s_type.
   *
   * We could, perhaps, tweak our parent's ->mkdir for a minute and
   * try using vfs_mkdir.  Just a thought.
   */
  static int create_default_group(struct config_group *parent_group,
  				struct config_group *group)
  {
  	int ret;
7063fbf22   Joel Becker   [PATCH] configfs:...
645
646
647
648
649
650
  	struct configfs_dirent *sd;
  	/* We trust the caller holds a reference to parent */
  	struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
  
  	if (!group->cg_item.ci_name)
  		group->cg_item.ci_name = group->cg_item.ci_namebuf;
7063fbf22   Joel Becker   [PATCH] configfs:...
651
652
  
  	ret = -ENOMEM;
ec193cf5a   Al Viro   configfs: don't o...
653
  	child = d_alloc_name(parent, group->cg_item.ci_name);
7063fbf22   Joel Becker   [PATCH] configfs:...
654
655
656
657
658
659
660
661
662
  	if (child) {
  		d_add(child, NULL);
  
  		ret = configfs_attach_group(&parent_group->cg_item,
  					    &group->cg_item, child);
  		if (!ret) {
  			sd = child->d_fsdata;
  			sd->s_type |= CONFIGFS_USET_DEFAULT;
  		} else {
2b0143b5c   David Howells   VFS: normal files...
663
  			BUG_ON(d_inode(child));
df7f99670   Joel Becker   configfs: Don't t...
664
  			d_drop(child);
7063fbf22   Joel Becker   [PATCH] configfs:...
665
666
667
668
669
670
671
672
673
674
  			dput(child);
  		}
  	}
  
  	return ret;
  }
  
  static int populate_groups(struct config_group *group)
  {
  	struct config_group *new_group;
7063fbf22   Joel Becker   [PATCH] configfs:...
675
  	int ret = 0;
7063fbf22   Joel Becker   [PATCH] configfs:...
676

1ae1602de   Christoph Hellwig   configfs: switch ...
677
678
679
680
681
  	list_for_each_entry(new_group, &group->default_groups, group_entry) {
  		ret = create_default_group(group, new_group);
  		if (ret) {
  			detach_groups(group);
  			break;
7063fbf22   Joel Becker   [PATCH] configfs:...
682
  		}
7063fbf22   Joel Becker   [PATCH] configfs:...
683
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
684
685
  	return ret;
  }
1ae1602de   Christoph Hellwig   configfs: switch ...
686
687
688
689
690
691
692
693
694
695
  void configfs_remove_default_groups(struct config_group *group)
  {
  	struct config_group *g, *n;
  
  	list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
  		list_del(&g->group_entry);
  		config_item_put(&g->cg_item);
  	}
  }
  EXPORT_SYMBOL(configfs_remove_default_groups);
7063fbf22   Joel Becker   [PATCH] configfs:...
696
697
  /*
   * All of link_obj/unlink_obj/link_group/unlink_group require that
e6bd07aee   Joel Becker   configfs: Convert...
698
   * subsys->su_mutex is held.
7063fbf22   Joel Becker   [PATCH] configfs:...
699
700
701
702
703
704
705
706
707
708
709
710
   */
  
  static void unlink_obj(struct config_item *item)
  {
  	struct config_group *group;
  
  	group = item->ci_group;
  	if (group) {
  		list_del_init(&item->ci_entry);
  
  		item->ci_group = NULL;
  		item->ci_parent = NULL;
eed7a0db4   Joel Becker   configfs: configf...
711
712
  
  		/* Drop the reference for ci_entry */
7063fbf22   Joel Becker   [PATCH] configfs:...
713
  		config_item_put(item);
eed7a0db4   Joel Becker   configfs: configf...
714
  		/* Drop the reference for ci_parent */
7063fbf22   Joel Becker   [PATCH] configfs:...
715
716
717
718
719
720
  		config_group_put(group);
  	}
  }
  
  static void link_obj(struct config_item *parent_item, struct config_item *item)
  {
eed7a0db4   Joel Becker   configfs: configf...
721
722
723
724
  	/*
  	 * Parent seems redundant with group, but it makes certain
  	 * traversals much nicer.
  	 */
7063fbf22   Joel Becker   [PATCH] configfs:...
725
  	item->ci_parent = parent_item;
eed7a0db4   Joel Becker   configfs: configf...
726
727
728
729
730
  
  	/*
  	 * We hold a reference on the parent for the child's ci_parent
  	 * link.
  	 */
7063fbf22   Joel Becker   [PATCH] configfs:...
731
732
  	item->ci_group = config_group_get(to_config_group(parent_item));
  	list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
eed7a0db4   Joel Becker   configfs: configf...
733
734
735
736
  	/*
  	 * We hold a reference on the child for ci_entry on the parent's
  	 * cg_children
  	 */
7063fbf22   Joel Becker   [PATCH] configfs:...
737
738
739
740
741
  	config_item_get(item);
  }
  
  static void unlink_group(struct config_group *group)
  {
7063fbf22   Joel Becker   [PATCH] configfs:...
742
  	struct config_group *new_group;
1ae1602de   Christoph Hellwig   configfs: switch ...
743
744
  	list_for_each_entry(new_group, &group->default_groups, group_entry)
  		unlink_group(new_group);
7063fbf22   Joel Becker   [PATCH] configfs:...
745
746
747
748
749
750
751
  
  	group->cg_subsys = NULL;
  	unlink_obj(&group->cg_item);
  }
  
  static void link_group(struct config_group *parent_group, struct config_group *group)
  {
7063fbf22   Joel Becker   [PATCH] configfs:...
752
753
754
755
756
757
758
759
760
761
762
763
  	struct config_group *new_group;
  	struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
  
  	link_obj(&parent_group->cg_item, &group->cg_item);
  
  	if (parent_group->cg_subsys)
  		subsys = parent_group->cg_subsys;
  	else if (configfs_is_root(&parent_group->cg_item))
  		subsys = to_configfs_subsystem(group);
  	else
  		BUG();
  	group->cg_subsys = subsys;
1ae1602de   Christoph Hellwig   configfs: switch ...
764
765
  	list_for_each_entry(new_group, &group->default_groups, group_entry)
  		link_group(group, new_group);
7063fbf22   Joel Becker   [PATCH] configfs:...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
  }
  
  /*
   * The goal is that configfs_attach_item() (and
   * configfs_attach_group()) can be called from either the VFS or this
   * module.  That is, they assume that the items have been created,
   * the dentry allocated, and the dcache is all ready to go.
   *
   * If they fail, they must clean up after themselves as if they
   * had never been called.  The caller (VFS or local function) will
   * handle cleaning up the dcache bits.
   *
   * configfs_detach_group() and configfs_detach_item() behave similarly on
   * the way out.  They assume that the proper semaphores are held, they
   * clean up the configfs items, and they expect their callers will
   * handle the dcache bits.
   */
  static int configfs_attach_item(struct config_item *parent_item,
  				struct config_item *item,
  				struct dentry *dentry)
  {
  	int ret;
  
  	ret = configfs_create_dir(item, dentry);
  	if (!ret) {
  		ret = populate_attrs(item);
  		if (ret) {
2e2ce171c   Louis Rilling   [PATCH] configfs:...
793
794
795
796
797
  			/*
  			 * We are going to remove an inode and its dentry but
  			 * the VFS may already have hit and used them. Thus,
  			 * we must lock them as rmdir() would.
  			 */
5955102c9   Al Viro   wrappers for ->i_...
798
  			inode_lock(d_inode(dentry));
7063fbf22   Joel Becker   [PATCH] configfs:...
799
  			configfs_remove_dir(item);
2b0143b5c   David Howells   VFS: normal files...
800
  			d_inode(dentry)->i_flags |= S_DEAD;
d83c49f3e   Al Viro   Fix the regressio...
801
  			dont_mount(dentry);
5955102c9   Al Viro   wrappers for ->i_...
802
  			inode_unlock(d_inode(dentry));
7063fbf22   Joel Becker   [PATCH] configfs:...
803
804
805
806
807
808
  			d_delete(dentry);
  		}
  	}
  
  	return ret;
  }
2e2ce171c   Louis Rilling   [PATCH] configfs:...
809
  /* Caller holds the mutex of the item's inode */
7063fbf22   Joel Becker   [PATCH] configfs:...
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  static void configfs_detach_item(struct config_item *item)
  {
  	detach_attrs(item);
  	configfs_remove_dir(item);
  }
  
  static int configfs_attach_group(struct config_item *parent_item,
  				 struct config_item *item,
  				 struct dentry *dentry)
  {
  	int ret;
  	struct configfs_dirent *sd;
  
  	ret = configfs_attach_item(parent_item, item, dentry);
  	if (!ret) {
  		sd = dentry->d_fsdata;
  		sd->s_type |= CONFIGFS_USET_DIR;
2e2ce171c   Louis Rilling   [PATCH] configfs:...
827
828
829
830
831
832
833
834
835
  		/*
  		 * FYI, we're faking mkdir in populate_groups()
  		 * We must lock the group's inode to avoid races with the VFS
  		 * which can already hit the inode and try to add/remove entries
  		 * under it.
  		 *
  		 * We must also lock the inode to remove it safely in case of
  		 * error, as rmdir() would.
  		 */
5955102c9   Al Viro   wrappers for ->i_...
836
  		inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
e74cc06df   Louis Rilling   configfs: Silence...
837
  		configfs_adjust_dir_dirent_depth_before_populate(sd);
7063fbf22   Joel Becker   [PATCH] configfs:...
838
839
840
  		ret = populate_groups(to_config_group(item));
  		if (ret) {
  			configfs_detach_item(item);
2b0143b5c   David Howells   VFS: normal files...
841
  			d_inode(dentry)->i_flags |= S_DEAD;
d83c49f3e   Al Viro   Fix the regressio...
842
  			dont_mount(dentry);
7063fbf22   Joel Becker   [PATCH] configfs:...
843
  		}
e74cc06df   Louis Rilling   configfs: Silence...
844
  		configfs_adjust_dir_dirent_depth_after_populate(sd);
5955102c9   Al Viro   wrappers for ->i_...
845
  		inode_unlock(d_inode(dentry));
2e2ce171c   Louis Rilling   [PATCH] configfs:...
846
847
  		if (ret)
  			d_delete(dentry);
7063fbf22   Joel Becker   [PATCH] configfs:...
848
849
850
851
  	}
  
  	return ret;
  }
2e2ce171c   Louis Rilling   [PATCH] configfs:...
852
  /* Caller holds the mutex of the group's inode */
7063fbf22   Joel Becker   [PATCH] configfs:...
853
854
855
856
857
858
859
  static void configfs_detach_group(struct config_item *item)
  {
  	detach_groups(to_config_group(item));
  	configfs_detach_item(item);
  }
  
  /*
299894cc9   Joel Becker   configfs: accessi...
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
   * After the item has been detached from the filesystem view, we are
   * ready to tear it out of the hierarchy.  Notify the client before
   * we do that so they can perform any cleanup that requires
   * navigating the hierarchy.  A client does not need to provide this
   * callback.  The subsystem semaphore MUST be held by the caller, and
   * references must be valid for both items.  It also assumes the
   * caller has validated ci_type.
   */
  static void client_disconnect_notify(struct config_item *parent_item,
  				     struct config_item *item)
  {
  	struct config_item_type *type;
  
  	type = parent_item->ci_type;
  	BUG_ON(!type);
  
  	if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
  		type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
  						      item);
  }
  
  /*
7063fbf22   Joel Becker   [PATCH] configfs:...
882
883
884
885
886
887
888
889
890
891
892
893
   * Drop the initial reference from make_item()/make_group()
   * This function assumes that reference is held on item
   * and that item holds a valid reference to the parent.  Also, it
   * assumes the caller has validated ci_type.
   */
  static void client_drop_item(struct config_item *parent_item,
  			     struct config_item *item)
  {
  	struct config_item_type *type;
  
  	type = parent_item->ci_type;
  	BUG_ON(!type);
eed7a0db4   Joel Becker   configfs: configf...
894
895
896
897
  	/*
  	 * If ->drop_item() exists, it is responsible for the
  	 * config_item_put().
  	 */
7063fbf22   Joel Becker   [PATCH] configfs:...
898
899
  	if (type->ct_group_ops && type->ct_group_ops->drop_item)
  		type->ct_group_ops->drop_item(to_config_group(parent_item),
299894cc9   Joel Becker   configfs: accessi...
900
  					      item);
7063fbf22   Joel Becker   [PATCH] configfs:...
901
902
903
  	else
  		config_item_put(item);
  }
631d1feba   Joel Becker   configfs: config ...
904
905
906
  #ifdef DEBUG
  static void configfs_dump_one(struct configfs_dirent *sd, int level)
  {
c66869313   Fabian Frederick   fs/configfs: conv...
907
908
  	pr_info("%*s\"%s\":
  ", level, " ", configfs_get_name(sd));
631d1feba   Joel Becker   configfs: config ...
909

c66869313   Fabian Frederick   fs/configfs: conv...
910
911
  #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s
  ", level, " ", #_type);
631d1feba   Joel Becker   configfs: config ...
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
  	type_print(CONFIGFS_ROOT);
  	type_print(CONFIGFS_DIR);
  	type_print(CONFIGFS_ITEM_ATTR);
  	type_print(CONFIGFS_ITEM_LINK);
  	type_print(CONFIGFS_USET_DIR);
  	type_print(CONFIGFS_USET_DEFAULT);
  	type_print(CONFIGFS_USET_DROPPING);
  #undef type_print
  }
  
  static int configfs_dump(struct configfs_dirent *sd, int level)
  {
  	struct configfs_dirent *child_sd;
  	int ret = 0;
  
  	configfs_dump_one(sd, level);
  
  	if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
  		return 0;
  
  	list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
  		ret = configfs_dump(child_sd, level + 2);
  		if (ret)
  			break;
  	}
  
  	return ret;
  }
  #endif
  
  
  /*
   * configfs_depend_item() and configfs_undepend_item()
   *
   * WARNING: Do not call these from a configfs callback!
   *
   * This describes these functions and their helpers.
   *
   * Allow another kernel system to depend on a config_item.  If this
25985edce   Lucas De Marchi   Fix common misspe...
951
   * happens, the item cannot go away until the dependent can live without
631d1feba   Joel Becker   configfs: config ...
952
953
954
955
956
957
958
959
960
961
962
963
964
965
   * it.  The idea is to give client modules as simple an interface as
   * possible.  When a system asks them to depend on an item, they just
   * call configfs_depend_item().  If the item is live and the client
   * driver is in good shape, we'll happily do the work for them.
   *
   * Why is the locking complex?  Because configfs uses the VFS to handle
   * all locking, but this function is called outside the normal
   * VFS->configfs path.  So it must take VFS locks to prevent the
   * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc).  This is
   * why you can't call these functions underneath configfs callbacks.
   *
   * Note, btw, that this can be called at *any* time, even when a configfs
   * subsystem isn't registered, or when configfs is loading or unloading.
   * Just like configfs_register_subsystem().  So we take the same
420118caa   Louis Rilling   configfs: Rework ...
966
967
   * precautions.  We pin the filesystem.  We lock configfs_dirent_lock.
   * If we can find the target item in the
631d1feba   Joel Becker   configfs: config ...
968
   * configfs tree, it must be part of the subsystem tree as well, so we
420118caa   Louis Rilling   configfs: Rework ...
969
970
   * do not need the subsystem semaphore.  Holding configfs_dirent_lock helps
   * locking out mkdir() and rmdir(), who might be racing us.
631d1feba   Joel Becker   configfs: config ...
971
972
973
974
975
976
977
978
979
980
981
982
   */
  
  /*
   * configfs_depend_prep()
   *
   * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
   * attributes.  This is similar but not the same to configfs_detach_prep().
   * Note that configfs_detach_prep() expects the parent to be locked when it
   * is called, but we lock the parent *inside* configfs_depend_prep().  We
   * do that so we can unlock it if we find nothing.
   *
   * Here we do a depth-first search of the dentry hierarchy looking for
420118caa   Louis Rilling   configfs: Rework ...
983
984
985
986
987
988
989
990
991
   * our object.
   * We deliberately ignore items tagged as dropping since they are virtually
   * dead, as well as items in the middle of attachment since they virtually
   * do not exist yet. This completes the locking out of racing mkdir() and
   * rmdir().
   * Note: subdirectories in the middle of attachment start with s_type =
   * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir().  When
   * CONFIGFS_USET_CREATING is set, we ignore the item.  The actual set of
   * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
631d1feba   Joel Becker   configfs: config ...
992
   *
420118caa   Louis Rilling   configfs: Rework ...
993
   * If the target is not found, -ENOENT is bubbled up.
631d1feba   Joel Becker   configfs: config ...
994
995
996
   *
   * This adds a requirement that all config_items be unique!
   *
420118caa   Louis Rilling   configfs: Rework ...
997
   * This is recursive.  There isn't
631d1feba   Joel Becker   configfs: config ...
998
999
1000
1001
1002
1003
   * much on the stack, though, so folks that need this function - be careful
   * about your stack!  Patches will be accepted to make it iterative.
   */
  static int configfs_depend_prep(struct dentry *origin,
  				struct config_item *target)
  {
49deb4bc2   Wei Yongjun   configfs: move th...
1004
  	struct configfs_dirent *child_sd, *sd;
631d1feba   Joel Becker   configfs: config ...
1005
  	int ret = 0;
49deb4bc2   Wei Yongjun   configfs: move th...
1006
1007
  	BUG_ON(!origin || !origin->d_fsdata);
  	sd = origin->d_fsdata;
631d1feba   Joel Becker   configfs: config ...
1008

631d1feba   Joel Becker   configfs: config ...
1009
1010
1011
1012
  	if (sd->s_element == target)  /* Boo-yah */
  		goto out;
  
  	list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
420118caa   Louis Rilling   configfs: Rework ...
1013
1014
1015
  		if ((child_sd->s_type & CONFIGFS_DIR) &&
  		    !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
  		    !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
631d1feba   Joel Becker   configfs: config ...
1016
1017
1018
1019
1020
1021
1022
1023
  			ret = configfs_depend_prep(child_sd->s_dentry,
  						   target);
  			if (!ret)
  				goto out;  /* Child path boo-yah */
  		}
  	}
  
  	/* We looped all our children and didn't find target */
631d1feba   Joel Becker   configfs: config ...
1024
1025
1026
1027
1028
  	ret = -ENOENT;
  
  out:
  	return ret;
  }
9fb434e75   Krzysztof Opasiak   fs: configfs: Fac...
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
  static int configfs_do_depend_item(struct dentry *subsys_dentry,
  				   struct config_item *target)
  {
  	struct configfs_dirent *p;
  	int ret;
  
  	spin_lock(&configfs_dirent_lock);
  	/* Scan the tree, return 0 if found */
  	ret = configfs_depend_prep(subsys_dentry, target);
  	if (ret)
  		goto out_unlock_dirent_lock;
  
  	/*
  	 * We are sure that the item is not about to be removed by rmdir(), and
  	 * not in the middle of attachment by mkdir().
  	 */
  	p = target->ci_dentry->d_fsdata;
  	p->s_dependent_count += 1;
  
  out_unlock_dirent_lock:
  	spin_unlock(&configfs_dirent_lock);
  
  	return ret;
  }
9a70adfff   Krzysztof Opasiak   fs: configfs: Fac...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
  static inline struct configfs_dirent *
  configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
  			    struct config_item *subsys_item)
  {
  	struct configfs_dirent *p;
  	struct configfs_dirent *ret = NULL;
  
  	list_for_each_entry(p, &root_sd->s_children, s_sibling) {
  		if (p->s_type & CONFIGFS_DIR &&
  		    p->s_element == subsys_item) {
  			ret = p;
  			break;
  		}
  	}
  
  	return ret;
  }
631d1feba   Joel Becker   configfs: config ...
1070
1071
1072
1073
  int configfs_depend_item(struct configfs_subsystem *subsys,
  			 struct config_item *target)
  {
  	int ret;
9a70adfff   Krzysztof Opasiak   fs: configfs: Fac...
1074
  	struct configfs_dirent *subsys_sd;
631d1feba   Joel Becker   configfs: config ...
1075
  	struct config_item *s_item = &subsys->su_group.cg_item;
b7c177fcd   Al Viro   configfs: kill co...
1076
  	struct dentry *root;
631d1feba   Joel Becker   configfs: config ...
1077
1078
1079
1080
1081
  
  	/*
  	 * Pin the configfs filesystem.  This means we can safely access
  	 * the root of the configfs filesystem.
  	 */
2a152ad3a   Al Viro   make configfs_pin...
1082
1083
1084
  	root = configfs_pin_fs();
  	if (IS_ERR(root))
  		return PTR_ERR(root);
631d1feba   Joel Becker   configfs: config ...
1085
1086
1087
1088
1089
1090
  
  	/*
  	 * Next, lock the root directory.  We're going to check that the
  	 * subsystem is really registered, and so we need to lock out
  	 * configfs_[un]register_subsystem().
  	 */
5955102c9   Al Viro   wrappers for ->i_...
1091
  	inode_lock(d_inode(root));
631d1feba   Joel Becker   configfs: config ...
1092

9a70adfff   Krzysztof Opasiak   fs: configfs: Fac...
1093
  	subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
631d1feba   Joel Becker   configfs: config ...
1094
1095
1096
1097
1098
1099
  	if (!subsys_sd) {
  		ret = -ENOENT;
  		goto out_unlock_fs;
  	}
  
  	/* Ok, now we can trust subsys/s_item */
9fb434e75   Krzysztof Opasiak   fs: configfs: Fac...
1100
  	ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
631d1feba   Joel Becker   configfs: config ...
1101

631d1feba   Joel Becker   configfs: config ...
1102
  out_unlock_fs:
5955102c9   Al Viro   wrappers for ->i_...
1103
  	inode_unlock(d_inode(root));
631d1feba   Joel Becker   configfs: config ...
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  
  	/*
  	 * If we succeeded, the fs is pinned via other methods.  If not,
  	 * we're done with it anyway.  So release_fs() is always right.
  	 */
  	configfs_release_fs();
  
  	return ret;
  }
  EXPORT_SYMBOL(configfs_depend_item);
  
  /*
   * Release the dependent linkage.  This is much simpler than
   * configfs_depend_item() because we know that that the client driver is
   * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
   */
9a9e3415e   Krzysztof Opasiak   fs: configfs: Dro...
1120
  void configfs_undepend_item(struct config_item *target)
631d1feba   Joel Becker   configfs: config ...
1121
1122
1123
1124
  {
  	struct configfs_dirent *sd;
  
  	/*
420118caa   Louis Rilling   configfs: Rework ...
1125
1126
  	 * Since we can trust everything is pinned, we just need
  	 * configfs_dirent_lock.
631d1feba   Joel Becker   configfs: config ...
1127
  	 */
420118caa   Louis Rilling   configfs: Rework ...
1128
  	spin_lock(&configfs_dirent_lock);
631d1feba   Joel Becker   configfs: config ...
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
  
  	sd = target->ci_dentry->d_fsdata;
  	BUG_ON(sd->s_dependent_count < 1);
  
  	sd->s_dependent_count -= 1;
  
  	/*
  	 * After this unlock, we cannot trust the item to stay alive!
  	 * DO NOT REFERENCE item after this unlock.
  	 */
420118caa   Louis Rilling   configfs: Rework ...
1139
  	spin_unlock(&configfs_dirent_lock);
631d1feba   Joel Becker   configfs: config ...
1140
1141
  }
  EXPORT_SYMBOL(configfs_undepend_item);
7063fbf22   Joel Becker   [PATCH] configfs:...
1142

d79d75b5c   Krzysztof Opasiak   fs: configfs: Add...
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
  /*
   * caller_subsys is a caller's subsystem not target's. This is used to
   * determine if we should lock root and check subsys or not. When we are
   * in the same subsystem as our target there is no need to do locking as
   * we know that subsys is valid and is not unregistered during this function
   * as we are called from callback of one of his children and VFS holds a lock
   * on some inode. Otherwise we have to lock our root to  ensure that target's
   * subsystem it is not unregistered during this function.
   */
  int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
  				  struct config_item *target)
  {
  	struct configfs_subsystem *target_subsys;
  	struct config_group *root, *parent;
  	struct configfs_dirent *subsys_sd;
  	int ret = -ENOENT;
  
  	/* Disallow this function for configfs root */
  	if (configfs_is_root(target))
  		return -EINVAL;
  
  	parent = target->ci_group;
  	/*
  	 * This may happen when someone is trying to depend root
  	 * directory of some subsystem
  	 */
  	if (configfs_is_root(&parent->cg_item)) {
  		target_subsys = to_configfs_subsystem(to_config_group(target));
  		root = parent;
  	} else {
  		target_subsys = parent->cg_subsys;
  		/* Find a cofnigfs root as we may need it for locking */
  		for (root = parent; !configfs_is_root(&root->cg_item);
  		     root = root->cg_item.ci_group)
  			;
  	}
  
  	if (target_subsys != caller_subsys) {
  		/*
  		 * We are in other configfs subsystem, so we have to do
  		 * additional locking to prevent other subsystem from being
  		 * unregistered
  		 */
5955102c9   Al Viro   wrappers for ->i_...
1186
  		inode_lock(d_inode(root->cg_item.ci_dentry));
d79d75b5c   Krzysztof Opasiak   fs: configfs: Add...
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
  
  		/*
  		 * As we are trying to depend item from other subsystem
  		 * we have to check if this subsystem is still registered
  		 */
  		subsys_sd = configfs_find_subsys_dentry(
  				root->cg_item.ci_dentry->d_fsdata,
  				&target_subsys->su_group.cg_item);
  		if (!subsys_sd)
  			goto out_root_unlock;
  	} else {
  		subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
  	}
  
  	/* Now we can execute core of depend item */
  	ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
  
  	if (target_subsys != caller_subsys)
  out_root_unlock:
  		/*
  		 * We were called from subsystem other than our target so we
  		 * took some locks so now it's time to release them
  		 */
5955102c9   Al Viro   wrappers for ->i_...
1210
  		inode_unlock(d_inode(root->cg_item.ci_dentry));
d79d75b5c   Krzysztof Opasiak   fs: configfs: Add...
1211
1212
1213
1214
  
  	return ret;
  }
  EXPORT_SYMBOL(configfs_depend_item_unlocked);
18bb1db3e   Al Viro   switch vfs_mkdir(...
1215
  static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
7063fbf22   Joel Becker   [PATCH] configfs:...
1216
  {
a6795e9eb   Joel Becker   configfs: Allow -...
1217
1218
1219
1220
  	int ret = 0;
  	int module_got = 0;
  	struct config_group *group = NULL;
  	struct config_item *item = NULL;
7063fbf22   Joel Becker   [PATCH] configfs:...
1221
1222
1223
1224
  	struct config_item *parent_item;
  	struct configfs_subsystem *subsys;
  	struct configfs_dirent *sd;
  	struct config_item_type *type;
70526b674   Joel Becker   [PATCH] configfs:...
1225
  	struct module *subsys_owner = NULL, *new_item_owner = NULL;
7063fbf22   Joel Becker   [PATCH] configfs:...
1226
  	char *name;
7063fbf22   Joel Becker   [PATCH] configfs:...
1227
  	sd = dentry->d_parent->d_fsdata;
2a109f2a4   Louis Rilling   [PATCH] configfs:...
1228
1229
1230
1231
1232
1233
1234
1235
1236
  
  	/*
  	 * Fake invisibility if dir belongs to a group/default groups hierarchy
  	 * being attached
  	 */
  	if (!configfs_dirent_is_ready(sd)) {
  		ret = -ENOENT;
  		goto out;
  	}
84efad1a5   Joel Becker   configfs: Fix a r...
1237
1238
1239
1240
  	if (!(sd->s_type & CONFIGFS_USET_DIR)) {
  		ret = -EPERM;
  		goto out;
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
1241

84efad1a5   Joel Becker   configfs: Fix a r...
1242
  	/* Get a working ref for the duration of this function */
7063fbf22   Joel Becker   [PATCH] configfs:...
1243
1244
1245
1246
1247
1248
1249
1250
  	parent_item = configfs_get_config_item(dentry->d_parent);
  	type = parent_item->ci_type;
  	subsys = to_config_group(parent_item)->cg_subsys;
  	BUG_ON(!subsys);
  
  	if (!type || !type->ct_group_ops ||
  	    (!type->ct_group_ops->make_group &&
  	     !type->ct_group_ops->make_item)) {
84efad1a5   Joel Becker   configfs: Fix a r...
1251
1252
  		ret = -EPERM;  /* Lack-of-mkdir returns -EPERM */
  		goto out_put;
7063fbf22   Joel Becker   [PATCH] configfs:...
1253
  	}
70526b674   Joel Becker   [PATCH] configfs:...
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
  	/*
  	 * The subsystem may belong to a different module than the item
  	 * being created.  We don't want to safely pin the new item but
  	 * fail to pin the subsystem it sits under.
  	 */
  	if (!subsys->su_group.cg_item.ci_type) {
  		ret = -EINVAL;
  		goto out_put;
  	}
  	subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
  	if (!try_module_get(subsys_owner)) {
  		ret = -EINVAL;
  		goto out_put;
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
1268
1269
  	name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
  	if (!name) {
84efad1a5   Joel Becker   configfs: Fix a r...
1270
  		ret = -ENOMEM;
70526b674   Joel Becker   [PATCH] configfs:...
1271
  		goto out_subsys_put;
7063fbf22   Joel Becker   [PATCH] configfs:...
1272
  	}
84efad1a5   Joel Becker   configfs: Fix a r...
1273

7063fbf22   Joel Becker   [PATCH] configfs:...
1274
  	snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
e6bd07aee   Joel Becker   configfs: Convert...
1275
  	mutex_lock(&subsys->su_mutex);
7063fbf22   Joel Becker   [PATCH] configfs:...
1276
  	if (type->ct_group_ops->make_group) {
f89ab8619   Joel Becker   Revert "configfs:...
1277
  		group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
a6795e9eb   Joel Becker   configfs: Allow -...
1278
1279
1280
  		if (!group)
  			group = ERR_PTR(-ENOMEM);
  		if (!IS_ERR(group)) {
7063fbf22   Joel Becker   [PATCH] configfs:...
1281
1282
  			link_group(to_config_group(parent_item), group);
  			item = &group->cg_item;
a6795e9eb   Joel Becker   configfs: Allow -...
1283
1284
  		} else
  			ret = PTR_ERR(group);
7063fbf22   Joel Becker   [PATCH] configfs:...
1285
  	} else {
f89ab8619   Joel Becker   Revert "configfs:...
1286
  		item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
a6795e9eb   Joel Becker   configfs: Allow -...
1287
1288
1289
  		if (!item)
  			item = ERR_PTR(-ENOMEM);
  		if (!IS_ERR(item))
7063fbf22   Joel Becker   [PATCH] configfs:...
1290
  			link_obj(parent_item, item);
a6795e9eb   Joel Becker   configfs: Allow -...
1291
1292
  		else
  			ret = PTR_ERR(item);
7063fbf22   Joel Becker   [PATCH] configfs:...
1293
  	}
e6bd07aee   Joel Becker   configfs: Convert...
1294
  	mutex_unlock(&subsys->su_mutex);
7063fbf22   Joel Becker   [PATCH] configfs:...
1295
1296
  
  	kfree(name);
a6795e9eb   Joel Becker   configfs: Allow -...
1297
  	if (ret) {
eed7a0db4   Joel Becker   configfs: configf...
1298
  		/*
dacdd0e04   Joel Becker   [PATCH] configfs:...
1299
  		 * If ret != 0, then link_obj() was never called.
eed7a0db4   Joel Becker   configfs: configf...
1300
1301
  		 * There are no extra references to clean up.
  		 */
70526b674   Joel Becker   [PATCH] configfs:...
1302
  		goto out_subsys_put;
7063fbf22   Joel Becker   [PATCH] configfs:...
1303
  	}
eed7a0db4   Joel Becker   configfs: configf...
1304
1305
1306
1307
  	/*
  	 * link_obj() has been called (via link_group() for groups).
  	 * From here on out, errors must clean that up.
  	 */
7063fbf22   Joel Becker   [PATCH] configfs:...
1308
  	type = item->ci_type;
eed7a0db4   Joel Becker   configfs: configf...
1309
1310
1311
1312
  	if (!type) {
  		ret = -EINVAL;
  		goto out_unlink;
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
1313

70526b674   Joel Becker   [PATCH] configfs:...
1314
1315
  	new_item_owner = type->ct_owner;
  	if (!try_module_get(new_item_owner)) {
eed7a0db4   Joel Becker   configfs: configf...
1316
1317
1318
  		ret = -EINVAL;
  		goto out_unlink;
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
1319

eed7a0db4   Joel Becker   configfs: configf...
1320
1321
1322
1323
1324
1325
  	/*
  	 * I hate doing it this way, but if there is
  	 * an error,  module_put() probably should
  	 * happen after any cleanup.
  	 */
  	module_got = 1;
6d8344bae   Louis Rilling   configfs: Fix fai...
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
  	/*
  	 * Make racing rmdir() fail if it did not tag parent with
  	 * CONFIGFS_USET_DROPPING
  	 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
  	 * fail and let rmdir() terminate correctly
  	 */
  	spin_lock(&configfs_dirent_lock);
  	/* This will make configfs_detach_prep() fail */
  	sd->s_type |= CONFIGFS_USET_IN_MKDIR;
  	spin_unlock(&configfs_dirent_lock);
eed7a0db4   Joel Becker   configfs: configf...
1336
1337
1338
1339
  	if (group)
  		ret = configfs_attach_group(parent_item, item, dentry);
  	else
  		ret = configfs_attach_item(parent_item, item, dentry);
6d8344bae   Louis Rilling   configfs: Fix fai...
1340
1341
  	spin_lock(&configfs_dirent_lock);
  	sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
2a109f2a4   Louis Rilling   [PATCH] configfs:...
1342
1343
  	if (!ret)
  		configfs_dir_set_ready(dentry->d_fsdata);
6d8344bae   Louis Rilling   configfs: Fix fai...
1344
  	spin_unlock(&configfs_dirent_lock);
eed7a0db4   Joel Becker   configfs: configf...
1345
1346
1347
  out_unlink:
  	if (ret) {
  		/* Tear down everything we built up */
e6bd07aee   Joel Becker   configfs: Convert...
1348
  		mutex_lock(&subsys->su_mutex);
299894cc9   Joel Becker   configfs: accessi...
1349
1350
  
  		client_disconnect_notify(parent_item, item);
eed7a0db4   Joel Becker   configfs: configf...
1351
1352
1353
1354
1355
  		if (group)
  			unlink_group(group);
  		else
  			unlink_obj(item);
  		client_drop_item(parent_item, item);
299894cc9   Joel Becker   configfs: accessi...
1356

e6bd07aee   Joel Becker   configfs: Convert...
1357
  		mutex_unlock(&subsys->su_mutex);
eed7a0db4   Joel Becker   configfs: configf...
1358
1359
  
  		if (module_got)
70526b674   Joel Becker   [PATCH] configfs:...
1360
  			module_put(new_item_owner);
7063fbf22   Joel Becker   [PATCH] configfs:...
1361
  	}
70526b674   Joel Becker   [PATCH] configfs:...
1362
1363
1364
  out_subsys_put:
  	if (ret)
  		module_put(subsys_owner);
84efad1a5   Joel Becker   configfs: Fix a r...
1365
1366
  out_put:
  	/*
eed7a0db4   Joel Becker   configfs: configf...
1367
1368
1369
  	 * link_obj()/link_group() took a reference from child->parent,
  	 * so the parent is safely pinned.  We can drop our working
  	 * reference.
84efad1a5   Joel Becker   configfs: Fix a r...
1370
1371
1372
1373
  	 */
  	config_item_put(parent_item);
  
  out:
7063fbf22   Joel Becker   [PATCH] configfs:...
1374
1375
1376
1377
1378
1379
1380
1381
1382
  	return ret;
  }
  
  static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
  {
  	struct config_item *parent_item;
  	struct config_item *item;
  	struct configfs_subsystem *subsys;
  	struct configfs_dirent *sd;
70526b674   Joel Becker   [PATCH] configfs:...
1383
  	struct module *subsys_owner = NULL, *dead_item_owner = NULL;
7063fbf22   Joel Becker   [PATCH] configfs:...
1384
  	int ret;
7063fbf22   Joel Becker   [PATCH] configfs:...
1385
1386
1387
  	sd = dentry->d_fsdata;
  	if (sd->s_type & CONFIGFS_USET_DEFAULT)
  		return -EPERM;
84efad1a5   Joel Becker   configfs: Fix a r...
1388
  	/* Get a working ref until we have the child */
7063fbf22   Joel Becker   [PATCH] configfs:...
1389
1390
1391
1392
1393
1394
1395
1396
  	parent_item = configfs_get_config_item(dentry->d_parent);
  	subsys = to_config_group(parent_item)->cg_subsys;
  	BUG_ON(!subsys);
  
  	if (!parent_item->ci_type) {
  		config_item_put(parent_item);
  		return -EINVAL;
  	}
70526b674   Joel Becker   [PATCH] configfs:...
1397
1398
1399
  	/* configfs_mkdir() shouldn't have allowed this */
  	BUG_ON(!subsys->su_group.cg_item.ci_type);
  	subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
9a73d78cd   Louis Rilling   [PATCH] configfs:...
1400
1401
1402
1403
  	/*
  	 * Ensure that no racing symlink() will make detach_prep() fail while
  	 * the new link is temporarily attached
  	 */
6d8344bae   Louis Rilling   configfs: Fix fai...
1404
  	do {
48f35b7b7   Al Viro   configfs_detach_p...
1405
  		struct dentry *wait;
6d8344bae   Louis Rilling   configfs: Fix fai...
1406

de6bf18e9   Louis Rilling   [PATCH] configfs:...
1407
1408
  		mutex_lock(&configfs_symlink_mutex);
  		spin_lock(&configfs_dirent_lock);
420118caa   Louis Rilling   configfs: Rework ...
1409
1410
1411
1412
1413
1414
1415
  		/*
  		 * Here's where we check for dependents.  We're protected by
  		 * configfs_dirent_lock.
  		 * If no dependent, atomically tag the item as dropping.
  		 */
  		ret = sd->s_dependent_count ? -EBUSY : 0;
  		if (!ret) {
48f35b7b7   Al Viro   configfs_detach_p...
1416
  			ret = configfs_detach_prep(dentry, &wait);
420118caa   Louis Rilling   configfs: Rework ...
1417
1418
1419
  			if (ret)
  				configfs_detach_rollback(dentry);
  		}
de6bf18e9   Louis Rilling   [PATCH] configfs:...
1420
1421
1422
1423
  		spin_unlock(&configfs_dirent_lock);
  		mutex_unlock(&configfs_symlink_mutex);
  
  		if (ret) {
6d8344bae   Louis Rilling   configfs: Fix fai...
1424
1425
1426
1427
1428
1429
  			if (ret != -EAGAIN) {
  				config_item_put(parent_item);
  				return ret;
  			}
  
  			/* Wait until the racing operation terminates */
48f35b7b7   Al Viro   configfs_detach_p...
1430
1431
1432
  			inode_lock(d_inode(wait));
  			inode_unlock(d_inode(wait));
  			dput(wait);
6d8344bae   Louis Rilling   configfs: Fix fai...
1433
1434
  		}
  	} while (ret == -EAGAIN);
7063fbf22   Joel Becker   [PATCH] configfs:...
1435

84efad1a5   Joel Becker   configfs: Fix a r...
1436
  	/* Get a working ref for the duration of this function */
7063fbf22   Joel Becker   [PATCH] configfs:...
1437
1438
1439
1440
1441
1442
  	item = configfs_get_config_item(dentry);
  
  	/* Drop reference from above, item already holds one. */
  	config_item_put(parent_item);
  
  	if (item->ci_type)
70526b674   Joel Becker   [PATCH] configfs:...
1443
  		dead_item_owner = item->ci_type->ct_owner;
7063fbf22   Joel Becker   [PATCH] configfs:...
1444
1445
1446
  
  	if (sd->s_type & CONFIGFS_USET_DIR) {
  		configfs_detach_group(item);
e6bd07aee   Joel Becker   configfs: Convert...
1447
  		mutex_lock(&subsys->su_mutex);
299894cc9   Joel Becker   configfs: accessi...
1448
  		client_disconnect_notify(parent_item, item);
7063fbf22   Joel Becker   [PATCH] configfs:...
1449
1450
1451
  		unlink_group(to_config_group(item));
  	} else {
  		configfs_detach_item(item);
e6bd07aee   Joel Becker   configfs: Convert...
1452
  		mutex_lock(&subsys->su_mutex);
299894cc9   Joel Becker   configfs: accessi...
1453
  		client_disconnect_notify(parent_item, item);
7063fbf22   Joel Becker   [PATCH] configfs:...
1454
1455
1456
1457
  		unlink_obj(item);
  	}
  
  	client_drop_item(parent_item, item);
e6bd07aee   Joel Becker   configfs: Convert...
1458
  	mutex_unlock(&subsys->su_mutex);
7063fbf22   Joel Becker   [PATCH] configfs:...
1459
1460
1461
  
  	/* Drop our reference from above */
  	config_item_put(item);
70526b674   Joel Becker   [PATCH] configfs:...
1462
1463
  	module_put(dead_item_owner);
  	module_put(subsys_owner);
7063fbf22   Joel Becker   [PATCH] configfs:...
1464
1465
1466
  
  	return 0;
  }
754661f14   Arjan van de Ven   [PATCH] mark stru...
1467
  const struct inode_operations configfs_dir_inode_operations = {
7063fbf22   Joel Becker   [PATCH] configfs:...
1468
1469
1470
1471
1472
  	.mkdir		= configfs_mkdir,
  	.rmdir		= configfs_rmdir,
  	.symlink	= configfs_symlink,
  	.unlink		= configfs_unlink,
  	.lookup		= configfs_lookup,
3d0f89bb1   Joel Becker   configfs: Add per...
1473
  	.setattr	= configfs_setattr,
7063fbf22   Joel Becker   [PATCH] configfs:...
1474
  };
81d44ed15   Al Viro   configfs: don't b...
1475
1476
1477
1478
  const struct inode_operations configfs_root_inode_operations = {
  	.lookup		= configfs_lookup,
  	.setattr	= configfs_setattr,
  };
7063fbf22   Joel Becker   [PATCH] configfs:...
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
  #if 0
  int configfs_rename_dir(struct config_item * item, const char *new_name)
  {
  	int error = 0;
  	struct dentry * new_dentry, * parent;
  
  	if (!strcmp(config_item_name(item), new_name))
  		return -EINVAL;
  
  	if (!item->parent)
  		return -EINVAL;
  
  	down_write(&configfs_rename_sem);
  	parent = item->parent->dentry;
5955102c9   Al Viro   wrappers for ->i_...
1493
  	inode_lock(d_inode(parent));
7063fbf22   Joel Becker   [PATCH] configfs:...
1494
1495
1496
  
  	new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
  	if (!IS_ERR(new_dentry)) {
2b0143b5c   David Howells   VFS: normal files...
1497
  		if (d_really_is_negative(new_dentry)) {
7063fbf22   Joel Becker   [PATCH] configfs:...
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
  			error = config_item_set_name(item, "%s", new_name);
  			if (!error) {
  				d_add(new_dentry, NULL);
  				d_move(item->dentry, new_dentry);
  			}
  			else
  				d_delete(new_dentry);
  		} else
  			error = -EEXIST;
  		dput(new_dentry);
  	}
5955102c9   Al Viro   wrappers for ->i_...
1509
  	inode_unlock(d_inode(parent));
7063fbf22   Joel Becker   [PATCH] configfs:...
1510
1511
1512
1513
1514
1515
1516
1517
  	up_write(&configfs_rename_sem);
  
  	return error;
  }
  #endif
  
  static int configfs_dir_open(struct inode *inode, struct file *file)
  {
867fa491a   Josef "Jeff" Sipek   [PATCH] configfs:...
1518
  	struct dentry * dentry = file->f_path.dentry;
7063fbf22   Joel Becker   [PATCH] configfs:...
1519
  	struct configfs_dirent * parent_sd = dentry->d_fsdata;
2a109f2a4   Louis Rilling   [PATCH] configfs:...
1520
  	int err;
7063fbf22   Joel Becker   [PATCH] configfs:...
1521

5955102c9   Al Viro   wrappers for ->i_...
1522
  	inode_lock(d_inode(dentry));
2a109f2a4   Louis Rilling   [PATCH] configfs:...
1523
1524
1525
1526
1527
1528
  	/*
  	 * Fake invisibility if dir belongs to a group/default groups hierarchy
  	 * being attached
  	 */
  	err = -ENOENT;
  	if (configfs_dirent_is_ready(parent_sd)) {
420118caa   Louis Rilling   configfs: Rework ...
1529
  		file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
2a109f2a4   Louis Rilling   [PATCH] configfs:...
1530
1531
1532
1533
1534
  		if (IS_ERR(file->private_data))
  			err = PTR_ERR(file->private_data);
  		else
  			err = 0;
  	}
5955102c9   Al Viro   wrappers for ->i_...
1535
  	inode_unlock(d_inode(dentry));
7063fbf22   Joel Becker   [PATCH] configfs:...
1536

2a109f2a4   Louis Rilling   [PATCH] configfs:...
1537
  	return err;
7063fbf22   Joel Becker   [PATCH] configfs:...
1538
1539
1540
1541
  }
  
  static int configfs_dir_close(struct inode *inode, struct file *file)
  {
867fa491a   Josef "Jeff" Sipek   [PATCH] configfs:...
1542
  	struct dentry * dentry = file->f_path.dentry;
7063fbf22   Joel Becker   [PATCH] configfs:...
1543
  	struct configfs_dirent * cursor = file->private_data;
5955102c9   Al Viro   wrappers for ->i_...
1544
  	inode_lock(d_inode(dentry));
6f6107640   Louis Rilling   configfs: Introdu...
1545
  	spin_lock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
1546
  	list_del_init(&cursor->s_sibling);
6f6107640   Louis Rilling   configfs: Introdu...
1547
  	spin_unlock(&configfs_dirent_lock);
5955102c9   Al Viro   wrappers for ->i_...
1548
  	inode_unlock(d_inode(dentry));
7063fbf22   Joel Becker   [PATCH] configfs:...
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
  
  	release_configfs_dirent(cursor);
  
  	return 0;
  }
  
  /* Relationship between s_mode and the DT_xxx types */
  static inline unsigned char dt_type(struct configfs_dirent *sd)
  {
  	return (sd->s_mode >> 12) & 15;
  }
52018855e   Al Viro   [readdir] convert...
1560
  static int configfs_readdir(struct file *file, struct dir_context *ctx)
7063fbf22   Joel Becker   [PATCH] configfs:...
1561
  {
52018855e   Al Viro   [readdir] convert...
1562
  	struct dentry *dentry = file->f_path.dentry;
b7c177fcd   Al Viro   configfs: kill co...
1563
  	struct super_block *sb = dentry->d_sb;
7063fbf22   Joel Becker   [PATCH] configfs:...
1564
  	struct configfs_dirent * parent_sd = dentry->d_fsdata;
52018855e   Al Viro   [readdir] convert...
1565
  	struct configfs_dirent *cursor = file->private_data;
7063fbf22   Joel Becker   [PATCH] configfs:...
1566
  	struct list_head *p, *q = &cursor->s_sibling;
24307aa1e   Joel Becker   configfs: Fix rac...
1567
  	ino_t ino = 0;
7063fbf22   Joel Becker   [PATCH] configfs:...
1568

52018855e   Al Viro   [readdir] convert...
1569
1570
  	if (!dir_emit_dots(file, ctx))
  		return 0;
a01b3007f   Al Viro   configfs_readdir(...
1571
1572
  	spin_lock(&configfs_dirent_lock);
  	if (ctx->pos == 2)
52018855e   Al Viro   [readdir] convert...
1573
  		list_move(q, &parent_sd->s_children);
52018855e   Al Viro   [readdir] convert...
1574
1575
1576
1577
1578
1579
1580
1581
1582
  	for (p = q->next; p != &parent_sd->s_children; p = p->next) {
  		struct configfs_dirent *next;
  		const char *name;
  		int len;
  		struct inode *inode = NULL;
  
  		next = list_entry(p, struct configfs_dirent, s_sibling);
  		if (!next->s_element)
  			continue;
7063fbf22   Joel Becker   [PATCH] configfs:...
1583

52018855e   Al Viro   [readdir] convert...
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
  		/*
  		 * We'll have a dentry and an inode for
  		 * PINNED items and for open attribute
  		 * files.  We lock here to prevent a race
  		 * with configfs_d_iput() clearing
  		 * s_dentry before calling iput().
  		 *
  		 * Why do we go to the trouble?  If
  		 * someone has an attribute file open,
  		 * the inode number should match until
  		 * they close it.  Beyond that, we don't
  		 * care.
  		 */
52018855e   Al Viro   [readdir] convert...
1597
1598
  		dentry = next->s_dentry;
  		if (dentry)
2b0143b5c   David Howells   VFS: normal files...
1599
  			inode = d_inode(dentry);
52018855e   Al Viro   [readdir] convert...
1600
1601
1602
1603
1604
  		if (inode)
  			ino = inode->i_ino;
  		spin_unlock(&configfs_dirent_lock);
  		if (!inode)
  			ino = iunique(sb, 2);
7063fbf22   Joel Becker   [PATCH] configfs:...
1605

a01b3007f   Al Viro   configfs_readdir(...
1606
1607
  		name = configfs_get_name(next);
  		len = strlen(name);
52018855e   Al Viro   [readdir] convert...
1608
1609
  		if (!dir_emit(ctx, name, len, ino, dt_type(next)))
  			return 0;
7063fbf22   Joel Becker   [PATCH] configfs:...
1610

52018855e   Al Viro   [readdir] convert...
1611
1612
  		spin_lock(&configfs_dirent_lock);
  		list_move(q, p);
52018855e   Al Viro   [readdir] convert...
1613
1614
  		p = q;
  		ctx->pos++;
7063fbf22   Joel Becker   [PATCH] configfs:...
1615
  	}
a01b3007f   Al Viro   configfs_readdir(...
1616
  	spin_unlock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
1617
1618
  	return 0;
  }
965c8e59c   Andrew Morton   lseek: the "whenc...
1619
  static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
7063fbf22   Joel Becker   [PATCH] configfs:...
1620
  {
867fa491a   Josef "Jeff" Sipek   [PATCH] configfs:...
1621
  	struct dentry * dentry = file->f_path.dentry;
7063fbf22   Joel Becker   [PATCH] configfs:...
1622

965c8e59c   Andrew Morton   lseek: the "whenc...
1623
  	switch (whence) {
7063fbf22   Joel Becker   [PATCH] configfs:...
1624
1625
1626
1627
1628
1629
  		case 1:
  			offset += file->f_pos;
  		case 0:
  			if (offset >= 0)
  				break;
  		default:
7063fbf22   Joel Becker   [PATCH] configfs:...
1630
1631
1632
1633
1634
1635
1636
1637
1638
  			return -EINVAL;
  	}
  	if (offset != file->f_pos) {
  		file->f_pos = offset;
  		if (file->f_pos >= 2) {
  			struct configfs_dirent *sd = dentry->d_fsdata;
  			struct configfs_dirent *cursor = file->private_data;
  			struct list_head *p;
  			loff_t n = file->f_pos - 2;
6f6107640   Louis Rilling   configfs: Introdu...
1639
  			spin_lock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
  			list_del(&cursor->s_sibling);
  			p = sd->s_children.next;
  			while (n && p != &sd->s_children) {
  				struct configfs_dirent *next;
  				next = list_entry(p, struct configfs_dirent,
  						   s_sibling);
  				if (next->s_element)
  					n--;
  				p = p->next;
  			}
  			list_add_tail(&cursor->s_sibling, p);
6f6107640   Louis Rilling   configfs: Introdu...
1651
  			spin_unlock(&configfs_dirent_lock);
7063fbf22   Joel Becker   [PATCH] configfs:...
1652
1653
  		}
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
1654
1655
  	return offset;
  }
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
1656
  const struct file_operations configfs_dir_operations = {
7063fbf22   Joel Becker   [PATCH] configfs:...
1657
1658
1659
1660
  	.open		= configfs_dir_open,
  	.release	= configfs_dir_close,
  	.llseek		= configfs_dir_lseek,
  	.read		= generic_read_dir,
a01b3007f   Al Viro   configfs_readdir(...
1661
  	.iterate_shared	= configfs_readdir,
7063fbf22   Joel Becker   [PATCH] configfs:...
1662
  };
5cf6a51e6   Daniel Baluta   configfs: allow d...
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
  /**
   * configfs_register_group - creates a parent-child relation between two groups
   * @parent_group:	parent group
   * @group:		child group
   *
   * link groups, creates dentry for the child and attaches it to the
   * parent dentry.
   *
   * Return: 0 on success, negative errno code on error
   */
  int configfs_register_group(struct config_group *parent_group,
  			    struct config_group *group)
  {
  	struct configfs_subsystem *subsys = parent_group->cg_subsys;
  	struct dentry *parent;
  	int ret;
  
  	mutex_lock(&subsys->su_mutex);
  	link_group(parent_group, group);
  	mutex_unlock(&subsys->su_mutex);
  
  	parent = parent_group->cg_item.ci_dentry;
5955102c9   Al Viro   wrappers for ->i_...
1685
  	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
5cf6a51e6   Daniel Baluta   configfs: allow d...
1686
1687
1688
1689
1690
1691
  	ret = create_default_group(parent_group, group);
  	if (!ret) {
  		spin_lock(&configfs_dirent_lock);
  		configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
  		spin_unlock(&configfs_dirent_lock);
  	}
5955102c9   Al Viro   wrappers for ->i_...
1692
  	inode_unlock(d_inode(parent));
5cf6a51e6   Daniel Baluta   configfs: allow d...
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
  	return ret;
  }
  EXPORT_SYMBOL(configfs_register_group);
  
  /**
   * configfs_unregister_group() - unregisters a child group from its parent
   * @group: parent group to be unregistered
   *
   * Undoes configfs_register_group()
   */
  void configfs_unregister_group(struct config_group *group)
  {
  	struct configfs_subsystem *subsys = group->cg_subsys;
  	struct dentry *dentry = group->cg_item.ci_dentry;
  	struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
bc87baee1   Mike Christie   configfs: fix reg...
1708
1709
1710
1711
1712
1713
1714
1715
1716
  	mutex_lock(&subsys->su_mutex);
  	if (!group->cg_item.ci_parent->ci_group) {
  		/*
  		 * The parent has already been unlinked and detached
  		 * due to a rmdir.
  		 */
  		goto unlink_group;
  	}
  	mutex_unlock(&subsys->su_mutex);
5955102c9   Al Viro   wrappers for ->i_...
1717
  	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
5cf6a51e6   Daniel Baluta   configfs: allow d...
1718
1719
1720
1721
1722
1723
1724
1725
  	spin_lock(&configfs_dirent_lock);
  	configfs_detach_prep(dentry, NULL);
  	spin_unlock(&configfs_dirent_lock);
  
  	configfs_detach_group(&group->cg_item);
  	d_inode(dentry)->i_flags |= S_DEAD;
  	dont_mount(dentry);
  	d_delete(dentry);
5955102c9   Al Viro   wrappers for ->i_...
1726
  	inode_unlock(d_inode(parent));
5cf6a51e6   Daniel Baluta   configfs: allow d...
1727
1728
1729
1730
  
  	dput(dentry);
  
  	mutex_lock(&subsys->su_mutex);
bc87baee1   Mike Christie   configfs: fix reg...
1731
  unlink_group:
5cf6a51e6   Daniel Baluta   configfs: allow d...
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
  	unlink_group(group);
  	mutex_unlock(&subsys->su_mutex);
  }
  EXPORT_SYMBOL(configfs_unregister_group);
  
  /**
   * configfs_register_default_group() - allocates and registers a child group
   * @parent_group:	parent group
   * @name:		child group name
   * @item_type:		child item type description
   *
   * boilerplate to allocate and register a child group with its parent. We need
   * kzalloc'ed memory because child's default_group is initially empty.
   *
   * Return: allocated config group or ERR_PTR() on error
   */
  struct config_group *
  configfs_register_default_group(struct config_group *parent_group,
  				const char *name,
  				struct config_item_type *item_type)
  {
  	int ret;
  	struct config_group *group;
  
  	group = kzalloc(sizeof(*group), GFP_KERNEL);
  	if (!group)
  		return ERR_PTR(-ENOMEM);
  	config_group_init_type_name(group, name, item_type);
  
  	ret = configfs_register_group(parent_group, group);
  	if (ret) {
  		kfree(group);
  		return ERR_PTR(ret);
  	}
  	return group;
  }
  EXPORT_SYMBOL(configfs_register_default_group);
  
  /**
   * configfs_unregister_default_group() - unregisters and frees a child group
   * @group:	the group to act on
   */
  void configfs_unregister_default_group(struct config_group *group)
  {
  	configfs_unregister_group(group);
  	kfree(group);
  }
  EXPORT_SYMBOL(configfs_unregister_default_group);
7063fbf22   Joel Becker   [PATCH] configfs:...
1780
1781
1782
1783
  int configfs_register_subsystem(struct configfs_subsystem *subsys)
  {
  	int err;
  	struct config_group *group = &subsys->su_group;
7063fbf22   Joel Becker   [PATCH] configfs:...
1784
  	struct dentry *dentry;
b7c177fcd   Al Viro   configfs: kill co...
1785
  	struct dentry *root;
7063fbf22   Joel Becker   [PATCH] configfs:...
1786
  	struct configfs_dirent *sd;
2a152ad3a   Al Viro   make configfs_pin...
1787
1788
1789
  	root = configfs_pin_fs();
  	if (IS_ERR(root))
  		return PTR_ERR(root);
7063fbf22   Joel Becker   [PATCH] configfs:...
1790
1791
1792
  
  	if (!group->cg_item.ci_name)
  		group->cg_item.ci_name = group->cg_item.ci_namebuf;
b7c177fcd   Al Viro   configfs: kill co...
1793
  	sd = root->d_fsdata;
7063fbf22   Joel Becker   [PATCH] configfs:...
1794
  	link_group(to_config_group(sd->s_element), group);
5955102c9   Al Viro   wrappers for ->i_...
1795
  	inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
7063fbf22   Joel Becker   [PATCH] configfs:...
1796

7063fbf22   Joel Becker   [PATCH] configfs:...
1797
  	err = -ENOMEM;
ec193cf5a   Al Viro   configfs: don't o...
1798
  	dentry = d_alloc_name(root, group->cg_item.ci_name);
afdf04ea0   Joel Becker   configfs: add mis...
1799
1800
  	if (dentry) {
  		d_add(dentry, NULL);
7063fbf22   Joel Becker   [PATCH] configfs:...
1801

afdf04ea0   Joel Becker   configfs: add mis...
1802
1803
1804
  		err = configfs_attach_group(sd->s_element, &group->cg_item,
  					    dentry);
  		if (err) {
2b0143b5c   David Howells   VFS: normal files...
1805
  			BUG_ON(d_inode(dentry));
df7f99670   Joel Becker   configfs: Don't t...
1806
  			d_drop(dentry);
afdf04ea0   Joel Becker   configfs: add mis...
1807
  			dput(dentry);
2a109f2a4   Louis Rilling   [PATCH] configfs:...
1808
1809
1810
1811
  		} else {
  			spin_lock(&configfs_dirent_lock);
  			configfs_dir_set_ready(dentry->d_fsdata);
  			spin_unlock(&configfs_dirent_lock);
afdf04ea0   Joel Becker   configfs: add mis...
1812
1813
  		}
  	}
7063fbf22   Joel Becker   [PATCH] configfs:...
1814

5955102c9   Al Viro   wrappers for ->i_...
1815
  	inode_unlock(d_inode(root));
7063fbf22   Joel Becker   [PATCH] configfs:...
1816

afdf04ea0   Joel Becker   configfs: add mis...
1817
1818
1819
  	if (err) {
  		unlink_group(group);
  		configfs_release_fs();
7063fbf22   Joel Becker   [PATCH] configfs:...
1820
1821
1822
1823
1824
1825
1826
1827
1828
  	}
  
  	return err;
  }
  
  void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
  {
  	struct config_group *group = &subsys->su_group;
  	struct dentry *dentry = group->cg_item.ci_dentry;
b7c177fcd   Al Viro   configfs: kill co...
1829
  	struct dentry *root = dentry->d_sb->s_root;
7063fbf22   Joel Becker   [PATCH] configfs:...
1830

b7c177fcd   Al Viro   configfs: kill co...
1831
  	if (dentry->d_parent != root) {
1d88aa441   Fabian Frederick   fs/configfs: use ...
1832
1833
  		pr_err("Tried to unregister non-subsystem!
  ");
7063fbf22   Joel Becker   [PATCH] configfs:...
1834
1835
  		return;
  	}
5955102c9   Al Viro   wrappers for ->i_...
1836
  	inode_lock_nested(d_inode(root),
55ed16029   Mark Fasheh   configfs: mutex_l...
1837
  			  I_MUTEX_PARENT);
5955102c9   Al Viro   wrappers for ->i_...
1838
  	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
9a73d78cd   Louis Rilling   [PATCH] configfs:...
1839
  	mutex_lock(&configfs_symlink_mutex);
b3e76af87   Louis Rilling   configfs: Fix dea...
1840
  	spin_lock(&configfs_dirent_lock);
6d8344bae   Louis Rilling   configfs: Fix fai...
1841
  	if (configfs_detach_prep(dentry, NULL)) {
1d88aa441   Fabian Frederick   fs/configfs: use ...
1842
1843
  		pr_err("Tried to unregister non-empty subsystem!
  ");
7063fbf22   Joel Becker   [PATCH] configfs:...
1844
  	}
b3e76af87   Louis Rilling   configfs: Fix dea...
1845
  	spin_unlock(&configfs_dirent_lock);
9a73d78cd   Louis Rilling   [PATCH] configfs:...
1846
  	mutex_unlock(&configfs_symlink_mutex);
7063fbf22   Joel Becker   [PATCH] configfs:...
1847
  	configfs_detach_group(&group->cg_item);
2b0143b5c   David Howells   VFS: normal files...
1848
  	d_inode(dentry)->i_flags |= S_DEAD;
d83c49f3e   Al Viro   Fix the regressio...
1849
  	dont_mount(dentry);
5955102c9   Al Viro   wrappers for ->i_...
1850
  	inode_unlock(d_inode(dentry));
7063fbf22   Joel Becker   [PATCH] configfs:...
1851
1852
  
  	d_delete(dentry);
5955102c9   Al Viro   wrappers for ->i_...
1853
  	inode_unlock(d_inode(root));
7063fbf22   Joel Becker   [PATCH] configfs:...
1854
1855
1856
1857
1858
1859
1860
1861
1862
  
  	dput(dentry);
  
  	unlink_group(group);
  	configfs_release_fs();
  }
  
  EXPORT_SYMBOL(configfs_register_subsystem);
  EXPORT_SYMBOL(configfs_unregister_subsystem);