blob: fc20bd8a63372fbda09efb846abd4cec5f4e40a0 [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masahiro Yamadafa60ce22021-05-06 18:06:44 -07002/*
Joel Becker7063fbf2005-12-15 14:29:43 -08003 * dir.c - Operations for configfs directories.
4 *
Joel Becker7063fbf2005-12-15 14:29:43 -08005 * Based on sysfs:
6 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
7 *
8 * configfs Copyright (C) 2005 Oracle. All rights reserved.
9 */
10
11#undef DEBUG
12
13#include <linux/fs.h>
Amir Goldstein6146e782019-05-26 17:34:09 +030014#include <linux/fsnotify.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080015#include <linux/mount.h>
16#include <linux/module.h>
17#include <linux/slab.h>
Louis Rilling107ed402008-06-16 19:01:00 +020018#include <linux/err.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080019
20#include <linux/configfs.h>
21#include "configfs_internal.h"
22
Louis Rilling6f610762008-06-16 19:00:58 +020023/*
24 * Protects mutations of configfs_dirent linkage together with proper i_mutex
Louis Rilling5301a772008-06-16 19:00:59 +020025 * Also protects mutations of symlinks linkage to target configfs_dirent
Louis Rilling6f610762008-06-16 19:00:58 +020026 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
27 * and configfs_dirent_lock locked, in that order.
Louis Rilling5301a772008-06-16 19:00:59 +020028 * This allows one to safely traverse configfs_dirent trees and symlinks without
29 * having to lock inodes.
Louis Rillingb3e76af2008-06-16 19:01:01 +020030 *
31 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
32 * unlocked is not reliable unless in detach_groups() called from
33 * rmdir()/unregister() and from configfs_attach_group()
Louis Rilling6f610762008-06-16 19:00:58 +020034 */
35DEFINE_SPINLOCK(configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080036
37static void configfs_d_iput(struct dentry * dentry,
38 struct inode * inode)
39{
Joel Becker24307aa2011-05-18 04:08:16 -070040 struct configfs_dirent *sd = dentry->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -080041
42 if (sd) {
Joel Becker24307aa2011-05-18 04:08:16 -070043 /* Coordinate with configfs_readdir */
44 spin_lock(&configfs_dirent_lock);
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053045 /*
46 * Set sd->s_dentry to null only when this dentry is the one
47 * that is going to be killed. Otherwise configfs_d_iput may
Christoph Hellwigd07f1322021-08-25 07:50:32 +020048 * run just after configfs_lookup and set sd->s_dentry to
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053049 * NULL even it's still in use.
Junxiao Bi76ae2812013-11-21 14:31:56 -080050 */
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053051 if (sd->s_dentry == dentry)
Junxiao Bi76ae2812013-11-21 14:31:56 -080052 sd->s_dentry = NULL;
53
Joel Becker24307aa2011-05-18 04:08:16 -070054 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080055 configfs_put(sd);
56 }
57 iput(inode);
58}
59
Al Virod463a0c2011-01-12 16:41:05 -050060const struct dentry_operations configfs_dentry_ops = {
Joel Becker7063fbf2005-12-15 14:29:43 -080061 .d_iput = configfs_d_iput,
Al Virob26d4cd2013-10-25 18:47:37 -040062 .d_delete = always_delete_dentry,
Joel Becker7063fbf2005-12-15 14:29:43 -080063};
64
Louis Rillinge74cc062009-01-28 19:18:32 +010065#ifdef CONFIG_LOCKDEP
66
67/*
68 * Helpers to make lockdep happy with our recursive locking of default groups'
69 * inodes (see configfs_attach_group() and configfs_detach_group()).
70 * We put default groups i_mutexes in separate classes according to their depth
71 * from the youngest non-default group ancestor.
72 *
73 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
74 * groups A/B and A/C will have their inode's mutex in class
75 * default_group_class[0], and default group A/C/D will be in
76 * default_group_class[1].
77 *
78 * The lock classes are declared and assigned in inode.c, according to the
79 * s_depth value.
80 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
81 * default groups, and reset to -1 when all default groups are attached. During
82 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
83 * inode's mutex is set to default_group_class[s_depth - 1].
84 */
85
86static void configfs_init_dirent_depth(struct configfs_dirent *sd)
87{
88 sd->s_depth = -1;
89}
90
91static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
92 struct configfs_dirent *sd)
93{
94 int parent_depth = parent_sd->s_depth;
95
96 if (parent_depth >= 0)
97 sd->s_depth = parent_depth + 1;
98}
99
100static void
101configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
102{
103 /*
104 * item's i_mutex class is already setup, so s_depth is now only
105 * used to set new sub-directories s_depth, which is always done
106 * with item's i_mutex locked.
107 */
108 /*
109 * sd->s_depth == -1 iff we are a non default group.
110 * else (we are a default group) sd->s_depth > 0 (see
111 * create_dir()).
112 */
113 if (sd->s_depth == -1)
114 /*
115 * We are a non default group and we are going to create
116 * default groups.
117 */
118 sd->s_depth = 0;
119}
120
121static void
122configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
123{
124 /* We will not create default groups anymore. */
125 sd->s_depth = -1;
126}
127
128#else /* CONFIG_LOCKDEP */
129
130static void configfs_init_dirent_depth(struct configfs_dirent *sd)
131{
132}
133
134static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
135 struct configfs_dirent *sd)
136{
137}
138
139static void
140configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
141{
142}
143
144static void
145configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
146{
147}
148
149#endif /* CONFIG_LOCKDEP */
150
Al Viro47320fb2019-08-25 19:56:13 -0400151static struct configfs_fragment *new_fragment(void)
152{
153 struct configfs_fragment *p;
154
155 p = kmalloc(sizeof(struct configfs_fragment), GFP_KERNEL);
156 if (p) {
157 atomic_set(&p->frag_count, 1);
158 init_rwsem(&p->frag_sem);
159 p->frag_dead = false;
160 }
161 return p;
162}
163
164void put_fragment(struct configfs_fragment *frag)
165{
166 if (frag && atomic_dec_and_test(&frag->frag_count))
167 kfree(frag);
168}
169
170struct configfs_fragment *get_fragment(struct configfs_fragment *frag)
171{
172 if (likely(frag))
173 atomic_inc(&frag->frag_count);
174 return frag;
175}
176
Joel Becker7063fbf2005-12-15 14:29:43 -0800177/*
178 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
179 */
Louis Rilling420118c2009-01-28 19:18:33 +0100180static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
Al Viro47320fb2019-08-25 19:56:13 -0400181 void *element, int type,
182 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800183{
184 struct configfs_dirent * sd;
185
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800186 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -0800187 if (!sd)
Louis Rilling107ed402008-06-16 19:01:00 +0200188 return ERR_PTR(-ENOMEM);
Joel Becker7063fbf2005-12-15 14:29:43 -0800189
Joel Becker7063fbf2005-12-15 14:29:43 -0800190 atomic_set(&sd->s_count, 1);
Joel Becker7063fbf2005-12-15 14:29:43 -0800191 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -0800192 sd->s_element = element;
Louis Rilling420118c2009-01-28 19:18:33 +0100193 sd->s_type = type;
Louis Rillinge74cc062009-01-28 19:18:32 +0100194 configfs_init_dirent_depth(sd);
Louis Rilling6f610762008-06-16 19:00:58 +0200195 spin_lock(&configfs_dirent_lock);
Louis Rillingb3e76af2008-06-16 19:01:01 +0200196 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
197 spin_unlock(&configfs_dirent_lock);
198 kmem_cache_free(configfs_dir_cachep, sd);
199 return ERR_PTR(-ENOENT);
200 }
Al Viro47320fb2019-08-25 19:56:13 -0400201 sd->s_frag = get_fragment(frag);
Louis Rilling6f610762008-06-16 19:00:58 +0200202 list_add(&sd->s_sibling, &parent_sd->s_children);
203 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800204
205 return sd;
206}
207
Joel Beckerb4c98f62006-09-13 11:01:19 -0700208/*
209 *
210 * Return -EEXIST if there is already a configfs element with the same
211 * name for the same parent.
212 *
213 * called with parent inode's i_mutex held
214 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100215static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
216 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700217{
218 struct configfs_dirent * sd;
219
220 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
221 if (sd->s_element) {
222 const unsigned char *existing = configfs_get_name(sd);
223 if (strcmp(existing, new))
224 continue;
225 else
226 return -EEXIST;
227 }
228 }
229
230 return 0;
231}
232
233
Joel Becker7063fbf2005-12-15 14:29:43 -0800234int configfs_make_dirent(struct configfs_dirent * parent_sd,
235 struct dentry * dentry, void * element,
Al Viro47320fb2019-08-25 19:56:13 -0400236 umode_t mode, int type, struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800237{
238 struct configfs_dirent * sd;
239
Al Viro47320fb2019-08-25 19:56:13 -0400240 sd = configfs_new_dirent(parent_sd, element, type, frag);
Louis Rilling107ed402008-06-16 19:01:00 +0200241 if (IS_ERR(sd))
242 return PTR_ERR(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800243
244 sd->s_mode = mode;
Joel Becker7063fbf2005-12-15 14:29:43 -0800245 sd->s_dentry = dentry;
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100246 if (dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800247 dentry->d_fsdata = configfs_get(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800248
249 return 0;
250}
251
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200252static void configfs_remove_dirent(struct dentry *dentry)
253{
254 struct configfs_dirent *sd = dentry->d_fsdata;
255
256 if (!sd)
257 return;
258 spin_lock(&configfs_dirent_lock);
259 list_del_init(&sd->s_sibling);
260 spin_unlock(&configfs_dirent_lock);
261 configfs_put(sd);
262}
263
Joel Becker7063fbf2005-12-15 14:29:43 -0800264/**
265 * configfs_create_dir - create a directory for an config_item.
266 * @item: config_itemwe're creating directory for.
267 * @dentry: config_item's dentry.
Alex Shi65cdb4a2020-11-13 16:58:13 +0800268 * @frag: config_item's fragment.
Louis Rilling2a109f22008-07-04 16:56:05 +0200269 *
270 * Note: user-created entries won't be allowed under this new directory
271 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800272 */
273
Al Viro47320fb2019-08-25 19:56:13 -0400274static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
275 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800276{
Al Viro1cf97d02015-01-29 00:20:49 -0500277 int error;
278 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
279 struct dentry *p = dentry->d_parent;
Al Viro2743c512019-09-11 08:57:38 +0200280 struct inode *inode;
Al Viro1cf97d02015-01-29 00:20:49 -0500281
282 BUG_ON(!item);
283
284 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
285 if (unlikely(error))
286 return error;
287
288 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
Al Viro47320fb2019-08-25 19:56:13 -0400289 CONFIGFS_DIR | CONFIGFS_USET_CREATING,
290 frag);
Al Viro1cf97d02015-01-29 00:20:49 -0500291 if (unlikely(error))
292 return error;
293
294 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
Al Viro2743c512019-09-11 08:57:38 +0200295 inode = configfs_create(dentry, mode);
296 if (IS_ERR(inode))
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200297 goto out_remove;
298
Al Viro2743c512019-09-11 08:57:38 +0200299 inode->i_op = &configfs_dir_inode_operations;
300 inode->i_fop = &configfs_dir_operations;
301 /* directory inodes start off with i_nlink == 2 (for "." entry) */
302 inc_nlink(inode);
303 d_instantiate(dentry, inode);
304 /* already hashed */
305 dget(dentry); /* pin directory dentries in core */
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200306 inc_nlink(d_inode(p));
307 item->ci_dentry = dentry;
308 return 0;
309
310out_remove:
311 configfs_remove_dirent(dentry);
Al Viro2743c512019-09-11 08:57:38 +0200312 return PTR_ERR(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800313}
314
Louis Rilling2a109f22008-07-04 16:56:05 +0200315/*
316 * Allow userspace to create new entries under a new directory created with
317 * configfs_create_dir(), and under all of its chidlren directories recursively.
318 * @sd configfs_dirent of the new directory to validate
319 *
320 * Caller must hold configfs_dirent_lock.
321 */
322static void configfs_dir_set_ready(struct configfs_dirent *sd)
323{
324 struct configfs_dirent *child_sd;
325
326 sd->s_type &= ~CONFIGFS_USET_CREATING;
327 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
328 if (child_sd->s_type & CONFIGFS_USET_CREATING)
329 configfs_dir_set_ready(child_sd);
330}
331
332/*
333 * Check that a directory does not belong to a directory hierarchy being
334 * attached and not validated yet.
335 * @sd configfs_dirent of the directory to check
336 *
337 * @return non-zero iff the directory was validated
338 *
339 * Note: takes configfs_dirent_lock, so the result may change from false to true
340 * in two consecutive calls, but never from true to false.
341 */
342int configfs_dirent_is_ready(struct configfs_dirent *sd)
343{
344 int ret;
345
346 spin_lock(&configfs_dirent_lock);
347 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
348 spin_unlock(&configfs_dirent_lock);
349
350 return ret;
351}
352
Al Viroe9c03af2019-09-11 09:00:01 +0200353int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
354 struct dentry *dentry, char *body)
Joel Becker7063fbf2005-12-15 14:29:43 -0800355{
356 int err = 0;
357 umode_t mode = S_IFLNK | S_IRWXUGO;
Al Viro47320fb2019-08-25 19:56:13 -0400358 struct configfs_dirent *p = parent->d_fsdata;
Al Viro2743c512019-09-11 08:57:38 +0200359 struct inode *inode;
Joel Becker7063fbf2005-12-15 14:29:43 -0800360
Al Viroe9c03af2019-09-11 09:00:01 +0200361 err = configfs_make_dirent(p, dentry, target, mode, CONFIGFS_ITEM_LINK,
362 p->s_frag);
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200363 if (err)
364 return err;
365
Al Viro2743c512019-09-11 08:57:38 +0200366 inode = configfs_create(dentry, mode);
367 if (IS_ERR(inode))
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200368 goto out_remove;
Al Viro2743c512019-09-11 08:57:38 +0200369
Al Viroe9c03af2019-09-11 09:00:01 +0200370 inode->i_link = body;
Al Viro2743c512019-09-11 08:57:38 +0200371 inode->i_op = &configfs_symlink_inode_operations;
372 d_instantiate(dentry, inode);
373 dget(dentry); /* pin link dentries in core */
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200374 return 0;
375
376out_remove:
377 configfs_remove_dirent(dentry);
Al Viro2743c512019-09-11 08:57:38 +0200378 return PTR_ERR(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800379}
380
381static void remove_dir(struct dentry * d)
382{
383 struct dentry * parent = dget(d->d_parent);
Joel Becker7063fbf2005-12-15 14:29:43 -0800384
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200385 configfs_remove_dirent(d);
386
David Howells2b0143b2015-03-17 22:25:59 +0000387 if (d_really_is_positive(d))
388 simple_rmdir(d_inode(parent),d);
Joel Becker7063fbf2005-12-15 14:29:43 -0800389
Al Viroa4555892014-10-21 20:11:25 -0400390 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800391
392 dput(parent);
393}
394
395/**
396 * configfs_remove_dir - remove an config_item's directory.
397 * @item: config_item we're removing.
398 *
399 * The only thing special about this is that we remove any files in
400 * the directory before we remove the directory, and we've inlined
401 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200402 *
403 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800404 */
405
406static void configfs_remove_dir(struct config_item * item)
407{
408 struct dentry * dentry = dget(item->ci_dentry);
409
410 if (!dentry)
411 return;
412
413 remove_dir(dentry);
414 /**
415 * Drop reference from dget() on entrance.
416 */
417 dput(dentry);
418}
419
Joel Becker7063fbf2005-12-15 14:29:43 -0800420static struct dentry * configfs_lookup(struct inode *dir,
421 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400422 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800423{
424 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
425 struct configfs_dirent * sd;
Christoph Hellwigd07f1322021-08-25 07:50:32 +0200426 struct inode *inode = NULL;
Louis Rilling2a109f22008-07-04 16:56:05 +0200427
Christoph Hellwig417b9622021-08-25 07:42:44 +0200428 if (dentry->d_name.len > NAME_MAX)
429 return ERR_PTR(-ENAMETOOLONG);
430
Louis Rilling2a109f22008-07-04 16:56:05 +0200431 /*
432 * Fake invisibility if dir belongs to a group/default groups hierarchy
433 * being attached
434 *
435 * This forbids userspace to read/write attributes of items which may
436 * not complete their initialization, since the dentries of the
437 * attributes won't be instantiated.
438 */
Louis Rilling2a109f22008-07-04 16:56:05 +0200439 if (!configfs_dirent_is_ready(parent_sd))
Christoph Hellwig899587c2021-08-25 07:43:55 +0200440 return ERR_PTR(-ENOENT);
Joel Becker7063fbf2005-12-15 14:29:43 -0800441
442 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Christoph Hellwigd07f1322021-08-25 07:50:32 +0200443 if ((sd->s_type & CONFIGFS_NOT_PINNED) &&
444 !strcmp(configfs_get_name(sd), dentry->d_name.name)) {
445 struct configfs_attribute *attr = sd->s_element;
446 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
Joel Becker7063fbf2005-12-15 14:29:43 -0800447
Christoph Hellwigd07f1322021-08-25 07:50:32 +0200448 spin_lock(&configfs_dirent_lock);
449 dentry->d_fsdata = configfs_get(sd);
450 sd->s_dentry = dentry;
451 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800452
Christoph Hellwigd07f1322021-08-25 07:50:32 +0200453 inode = configfs_create(dentry, mode);
454 if (IS_ERR(inode)) {
455 configfs_put(sd);
456 return ERR_CAST(inode);
457 }
458 if (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) {
459 inode->i_size = 0;
460 inode->i_fop = &configfs_bin_file_operations;
461 } else {
462 inode->i_size = PAGE_SIZE;
463 inode->i_fop = &configfs_file_operations;
464 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800465 break;
466 }
467 }
468
Christoph Hellwigd07f1322021-08-25 07:50:32 +0200469 d_add(dentry, inode);
470 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800471}
472
473/*
474 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200475 * attributes and are removed by rmdir(). We recurse, setting
476 * CONFIGFS_USET_DROPPING on all children that are candidates for
477 * default detach.
478 * If there is an error, the caller will reset the flags via
479 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800480 */
Al Viro48f35b72016-04-12 00:37:59 -0400481static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
Joel Becker7063fbf2005-12-15 14:29:43 -0800482{
483 struct configfs_dirent *parent_sd = dentry->d_fsdata;
484 struct configfs_dirent *sd;
485 int ret;
486
Louis Rilling4768e9b2008-06-23 14:16:17 +0200487 /* Mark that we're trying to drop the group */
488 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
489
Joel Becker7063fbf2005-12-15 14:29:43 -0800490 ret = -EBUSY;
Al Viroe9c03af2019-09-11 09:00:01 +0200491 if (parent_sd->s_links)
Joel Becker7063fbf2005-12-15 14:29:43 -0800492 goto out;
493
494 ret = 0;
495 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200496 if (!sd->s_element ||
497 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800498 continue;
499 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200500 /* Abort if racing with mkdir() */
501 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
Al Viro48f35b72016-04-12 00:37:59 -0400502 if (wait)
503 *wait= dget(sd->s_dentry);
Louis Rilling6d8344b2008-06-16 19:01:02 +0200504 return -EAGAIN;
505 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800506
Joel Becker631d1fe2007-06-18 18:06:09 -0700507 /*
508 * Yup, recursive. If there's a problem, blame
509 * deep nesting of default_groups
510 */
Al Viro48f35b72016-04-12 00:37:59 -0400511 ret = configfs_detach_prep(sd->s_dentry, wait);
Joel Becker7063fbf2005-12-15 14:29:43 -0800512 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800513 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800514 } else
515 ret = -ENOTEMPTY;
516
517 break;
518 }
519
520out:
521 return ret;
522}
523
524/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200525 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800526 * set.
527 */
528static void configfs_detach_rollback(struct dentry *dentry)
529{
530 struct configfs_dirent *parent_sd = dentry->d_fsdata;
531 struct configfs_dirent *sd;
532
Louis Rilling4768e9b2008-06-23 14:16:17 +0200533 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
534
535 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
536 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800537 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800538}
539
540static void detach_attrs(struct config_item * item)
541{
542 struct dentry * dentry = dget(item->ci_dentry);
543 struct configfs_dirent * parent_sd;
544 struct configfs_dirent * sd, * tmp;
545
546 if (!dentry)
547 return;
548
549 pr_debug("configfs %s: dropping attrs for dir\n",
550 dentry->d_name.name);
551
552 parent_sd = dentry->d_fsdata;
553 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
554 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
555 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200556 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800557 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200558 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800559 configfs_drop_dentry(sd, dentry);
560 configfs_put(sd);
561 }
562
563 /**
564 * Drop reference from dget() on entrance.
565 */
566 dput(dentry);
567}
568
569static int populate_attrs(struct config_item *item)
570{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200571 const struct config_item_type *t = item->ci_type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800572 struct configfs_attribute *attr;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300573 struct configfs_bin_attribute *bin_attr;
Joel Becker7063fbf2005-12-15 14:29:43 -0800574 int error = 0;
575 int i;
576
577 if (!t)
578 return -EINVAL;
579 if (t->ct_attrs) {
580 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
581 if ((error = configfs_create_file(item, attr)))
582 break;
583 }
584 }
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300585 if (t->ct_bin_attrs) {
586 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
587 error = configfs_create_bin_file(item, bin_attr);
588 if (error)
589 break;
590 }
591 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800592
593 if (error)
594 detach_attrs(item);
595
596 return error;
597}
598
599static int configfs_attach_group(struct config_item *parent_item,
600 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400601 struct dentry *dentry,
602 struct configfs_fragment *frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800603static void configfs_detach_group(struct config_item *item);
604
605static void detach_groups(struct config_group *group)
606{
607 struct dentry * dentry = dget(group->cg_item.ci_dentry);
608 struct dentry *child;
609 struct configfs_dirent *parent_sd;
610 struct configfs_dirent *sd, *tmp;
611
612 if (!dentry)
613 return;
614
615 parent_sd = dentry->d_fsdata;
616 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
617 if (!sd->s_element ||
618 !(sd->s_type & CONFIGFS_USET_DEFAULT))
619 continue;
620
621 child = sd->s_dentry;
622
Al Viro59551022016-01-22 15:40:57 -0500623 inode_lock(d_inode(child));
Louis Rillingb3e76af2008-06-16 19:01:01 +0200624
Joel Becker7063fbf2005-12-15 14:29:43 -0800625 configfs_detach_group(sd->s_element);
David Howells2b0143b2015-03-17 22:25:59 +0000626 d_inode(child)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400627 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800628
Al Viro59551022016-01-22 15:40:57 -0500629 inode_unlock(d_inode(child));
Joel Becker7063fbf2005-12-15 14:29:43 -0800630
631 d_delete(child);
632 dput(child);
633 }
634
635 /**
636 * Drop reference from dget() on entrance.
637 */
638 dput(dentry);
639}
640
641/*
642 * This fakes mkdir(2) on a default_groups[] entry. It
643 * creates a dentry, attachs it, and then does fixup
644 * on the sd->s_type.
645 *
646 * We could, perhaps, tweak our parent's ->mkdir for a minute and
647 * try using vfs_mkdir. Just a thought.
648 */
649static int create_default_group(struct config_group *parent_group,
Al Viro47320fb2019-08-25 19:56:13 -0400650 struct config_group *group,
651 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800652{
653 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800654 struct configfs_dirent *sd;
655 /* We trust the caller holds a reference to parent */
656 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
657
658 if (!group->cg_item.ci_name)
659 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800660
661 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400662 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800663 if (child) {
664 d_add(child, NULL);
665
666 ret = configfs_attach_group(&parent_group->cg_item,
Al Viro47320fb2019-08-25 19:56:13 -0400667 &group->cg_item, child, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800668 if (!ret) {
669 sd = child->d_fsdata;
670 sd->s_type |= CONFIGFS_USET_DEFAULT;
671 } else {
David Howells2b0143b2015-03-17 22:25:59 +0000672 BUG_ON(d_inode(child));
Joel Beckerdf7f9962011-02-22 01:09:49 -0800673 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800674 dput(child);
675 }
676 }
677
678 return ret;
679}
680
Al Viro47320fb2019-08-25 19:56:13 -0400681static int populate_groups(struct config_group *group,
682 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800683{
684 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800685 int ret = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800686
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100687 list_for_each_entry(new_group, &group->default_groups, group_entry) {
Al Viro47320fb2019-08-25 19:56:13 -0400688 ret = create_default_group(group, new_group, frag);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100689 if (ret) {
690 detach_groups(group);
691 break;
Joel Becker7063fbf2005-12-15 14:29:43 -0800692 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800693 }
694
Joel Becker7063fbf2005-12-15 14:29:43 -0800695 return ret;
696}
697
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100698void configfs_remove_default_groups(struct config_group *group)
699{
700 struct config_group *g, *n;
701
702 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
703 list_del(&g->group_entry);
704 config_item_put(&g->cg_item);
705 }
706}
707EXPORT_SYMBOL(configfs_remove_default_groups);
708
Joel Becker7063fbf2005-12-15 14:29:43 -0800709/*
710 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700711 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800712 */
713
714static void unlink_obj(struct config_item *item)
715{
716 struct config_group *group;
717
718 group = item->ci_group;
719 if (group) {
720 list_del_init(&item->ci_entry);
721
722 item->ci_group = NULL;
723 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700724
725 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800726 config_item_put(item);
727
Joel Beckereed7a0d2006-04-11 21:37:20 -0700728 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800729 config_group_put(group);
730 }
731}
732
733static void link_obj(struct config_item *parent_item, struct config_item *item)
734{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700735 /*
736 * Parent seems redundant with group, but it makes certain
737 * traversals much nicer.
738 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800739 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700740
741 /*
742 * We hold a reference on the parent for the child's ci_parent
743 * link.
744 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800745 item->ci_group = config_group_get(to_config_group(parent_item));
746 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
747
Joel Beckereed7a0d2006-04-11 21:37:20 -0700748 /*
749 * We hold a reference on the child for ci_entry on the parent's
750 * cg_children
751 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800752 config_item_get(item);
753}
754
755static void unlink_group(struct config_group *group)
756{
Joel Becker7063fbf2005-12-15 14:29:43 -0800757 struct config_group *new_group;
758
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100759 list_for_each_entry(new_group, &group->default_groups, group_entry)
760 unlink_group(new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800761
762 group->cg_subsys = NULL;
763 unlink_obj(&group->cg_item);
764}
765
766static void link_group(struct config_group *parent_group, struct config_group *group)
767{
Joel Becker7063fbf2005-12-15 14:29:43 -0800768 struct config_group *new_group;
769 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
770
771 link_obj(&parent_group->cg_item, &group->cg_item);
772
773 if (parent_group->cg_subsys)
774 subsys = parent_group->cg_subsys;
775 else if (configfs_is_root(&parent_group->cg_item))
776 subsys = to_configfs_subsystem(group);
777 else
778 BUG();
779 group->cg_subsys = subsys;
780
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100781 list_for_each_entry(new_group, &group->default_groups, group_entry)
782 link_group(group, new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800783}
784
785/*
786 * The goal is that configfs_attach_item() (and
787 * configfs_attach_group()) can be called from either the VFS or this
788 * module. That is, they assume that the items have been created,
789 * the dentry allocated, and the dcache is all ready to go.
790 *
791 * If they fail, they must clean up after themselves as if they
792 * had never been called. The caller (VFS or local function) will
793 * handle cleaning up the dcache bits.
794 *
795 * configfs_detach_group() and configfs_detach_item() behave similarly on
796 * the way out. They assume that the proper semaphores are held, they
797 * clean up the configfs items, and they expect their callers will
798 * handle the dcache bits.
799 */
800static int configfs_attach_item(struct config_item *parent_item,
801 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400802 struct dentry *dentry,
803 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800804{
805 int ret;
806
Al Viro47320fb2019-08-25 19:56:13 -0400807 ret = configfs_create_dir(item, dentry, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800808 if (!ret) {
809 ret = populate_attrs(item);
810 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200811 /*
812 * We are going to remove an inode and its dentry but
813 * the VFS may already have hit and used them. Thus,
814 * we must lock them as rmdir() would.
815 */
Al Viro59551022016-01-22 15:40:57 -0500816 inode_lock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800817 configfs_remove_dir(item);
David Howells2b0143b2015-03-17 22:25:59 +0000818 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400819 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -0500820 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800821 d_delete(dentry);
822 }
823 }
824
825 return ret;
826}
827
Louis Rilling2e2ce172008-07-04 16:56:06 +0200828/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800829static void configfs_detach_item(struct config_item *item)
830{
831 detach_attrs(item);
832 configfs_remove_dir(item);
833}
834
835static int configfs_attach_group(struct config_item *parent_item,
836 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400837 struct dentry *dentry,
838 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800839{
840 int ret;
841 struct configfs_dirent *sd;
842
Al Viro47320fb2019-08-25 19:56:13 -0400843 ret = configfs_attach_item(parent_item, item, dentry, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800844 if (!ret) {
845 sd = dentry->d_fsdata;
846 sd->s_type |= CONFIGFS_USET_DIR;
847
Louis Rilling2e2ce172008-07-04 16:56:06 +0200848 /*
849 * FYI, we're faking mkdir in populate_groups()
850 * We must lock the group's inode to avoid races with the VFS
851 * which can already hit the inode and try to add/remove entries
852 * under it.
853 *
854 * We must also lock the inode to remove it safely in case of
855 * error, as rmdir() would.
856 */
Al Viro59551022016-01-22 15:40:57 -0500857 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100858 configfs_adjust_dir_dirent_depth_before_populate(sd);
Al Viro47320fb2019-08-25 19:56:13 -0400859 ret = populate_groups(to_config_group(item), frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800860 if (ret) {
861 configfs_detach_item(item);
David Howells2b0143b2015-03-17 22:25:59 +0000862 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400863 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800864 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100865 configfs_adjust_dir_dirent_depth_after_populate(sd);
Al Viro59551022016-01-22 15:40:57 -0500866 inode_unlock(d_inode(dentry));
Louis Rilling2e2ce172008-07-04 16:56:06 +0200867 if (ret)
868 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800869 }
870
871 return ret;
872}
873
Louis Rilling2e2ce172008-07-04 16:56:06 +0200874/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800875static void configfs_detach_group(struct config_item *item)
876{
877 detach_groups(to_config_group(item));
878 configfs_detach_item(item);
879}
880
881/*
Joel Becker299894c2006-10-06 17:33:23 -0700882 * After the item has been detached from the filesystem view, we are
883 * ready to tear it out of the hierarchy. Notify the client before
884 * we do that so they can perform any cleanup that requires
885 * navigating the hierarchy. A client does not need to provide this
886 * callback. The subsystem semaphore MUST be held by the caller, and
887 * references must be valid for both items. It also assumes the
888 * caller has validated ci_type.
889 */
890static void client_disconnect_notify(struct config_item *parent_item,
891 struct config_item *item)
892{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200893 const struct config_item_type *type;
Joel Becker299894c2006-10-06 17:33:23 -0700894
895 type = parent_item->ci_type;
896 BUG_ON(!type);
897
898 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
899 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
900 item);
901}
902
903/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800904 * Drop the initial reference from make_item()/make_group()
905 * This function assumes that reference is held on item
906 * and that item holds a valid reference to the parent. Also, it
907 * assumes the caller has validated ci_type.
908 */
909static void client_drop_item(struct config_item *parent_item,
910 struct config_item *item)
911{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200912 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800913
914 type = parent_item->ci_type;
915 BUG_ON(!type);
916
Joel Beckereed7a0d2006-04-11 21:37:20 -0700917 /*
918 * If ->drop_item() exists, it is responsible for the
919 * config_item_put().
920 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800921 if (type->ct_group_ops && type->ct_group_ops->drop_item)
922 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700923 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800924 else
925 config_item_put(item);
926}
927
Joel Becker631d1fe2007-06-18 18:06:09 -0700928#ifdef DEBUG
929static void configfs_dump_one(struct configfs_dirent *sd, int level)
930{
Fabian Frederickc6686932014-06-04 16:05:58 -0700931 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700932
Fabian Frederickc6686932014-06-04 16:05:58 -0700933#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700934 type_print(CONFIGFS_ROOT);
935 type_print(CONFIGFS_DIR);
936 type_print(CONFIGFS_ITEM_ATTR);
937 type_print(CONFIGFS_ITEM_LINK);
938 type_print(CONFIGFS_USET_DIR);
939 type_print(CONFIGFS_USET_DEFAULT);
940 type_print(CONFIGFS_USET_DROPPING);
941#undef type_print
942}
943
944static int configfs_dump(struct configfs_dirent *sd, int level)
945{
946 struct configfs_dirent *child_sd;
947 int ret = 0;
948
949 configfs_dump_one(sd, level);
950
951 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
952 return 0;
953
954 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
955 ret = configfs_dump(child_sd, level + 2);
956 if (ret)
957 break;
958 }
959
960 return ret;
961}
962#endif
963
964
965/*
966 * configfs_depend_item() and configfs_undepend_item()
967 *
968 * WARNING: Do not call these from a configfs callback!
969 *
970 * This describes these functions and their helpers.
971 *
972 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300973 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -0700974 * it. The idea is to give client modules as simple an interface as
975 * possible. When a system asks them to depend on an item, they just
976 * call configfs_depend_item(). If the item is live and the client
977 * driver is in good shape, we'll happily do the work for them.
978 *
979 * Why is the locking complex? Because configfs uses the VFS to handle
980 * all locking, but this function is called outside the normal
981 * VFS->configfs path. So it must take VFS locks to prevent the
982 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
983 * why you can't call these functions underneath configfs callbacks.
984 *
985 * Note, btw, that this can be called at *any* time, even when a configfs
986 * subsystem isn't registered, or when configfs is loading or unloading.
987 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +0100988 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
989 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -0700990 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +0100991 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
992 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -0700993 */
994
995/*
996 * configfs_depend_prep()
997 *
998 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
999 * attributes. This is similar but not the same to configfs_detach_prep().
1000 * Note that configfs_detach_prep() expects the parent to be locked when it
1001 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1002 * do that so we can unlock it if we find nothing.
1003 *
1004 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001005 * our object.
1006 * We deliberately ignore items tagged as dropping since they are virtually
1007 * dead, as well as items in the middle of attachment since they virtually
1008 * do not exist yet. This completes the locking out of racing mkdir() and
1009 * rmdir().
1010 * Note: subdirectories in the middle of attachment start with s_type =
1011 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1012 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1013 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001014 *
Louis Rilling420118c2009-01-28 19:18:33 +01001015 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001016 *
1017 * This adds a requirement that all config_items be unique!
1018 *
Louis Rilling420118c2009-01-28 19:18:33 +01001019 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001020 * much on the stack, though, so folks that need this function - be careful
1021 * about your stack! Patches will be accepted to make it iterative.
1022 */
1023static int configfs_depend_prep(struct dentry *origin,
1024 struct config_item *target)
1025{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001026 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001027 int ret = 0;
1028
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001029 BUG_ON(!origin || !origin->d_fsdata);
1030 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001031
Joel Becker631d1fe2007-06-18 18:06:09 -07001032 if (sd->s_element == target) /* Boo-yah */
1033 goto out;
1034
1035 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001036 if ((child_sd->s_type & CONFIGFS_DIR) &&
1037 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1038 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001039 ret = configfs_depend_prep(child_sd->s_dentry,
1040 target);
1041 if (!ret)
1042 goto out; /* Child path boo-yah */
1043 }
1044 }
1045
1046 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001047 ret = -ENOENT;
1048
1049out:
1050 return ret;
1051}
1052
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001053static int configfs_do_depend_item(struct dentry *subsys_dentry,
1054 struct config_item *target)
1055{
1056 struct configfs_dirent *p;
1057 int ret;
1058
1059 spin_lock(&configfs_dirent_lock);
1060 /* Scan the tree, return 0 if found */
1061 ret = configfs_depend_prep(subsys_dentry, target);
1062 if (ret)
1063 goto out_unlock_dirent_lock;
1064
1065 /*
1066 * We are sure that the item is not about to be removed by rmdir(), and
1067 * not in the middle of attachment by mkdir().
1068 */
1069 p = target->ci_dentry->d_fsdata;
1070 p->s_dependent_count += 1;
1071
1072out_unlock_dirent_lock:
1073 spin_unlock(&configfs_dirent_lock);
1074
1075 return ret;
1076}
1077
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001078static inline struct configfs_dirent *
1079configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1080 struct config_item *subsys_item)
1081{
1082 struct configfs_dirent *p;
1083 struct configfs_dirent *ret = NULL;
1084
1085 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1086 if (p->s_type & CONFIGFS_DIR &&
1087 p->s_element == subsys_item) {
1088 ret = p;
1089 break;
1090 }
1091 }
1092
1093 return ret;
1094}
1095
1096
Joel Becker631d1fe2007-06-18 18:06:09 -07001097int configfs_depend_item(struct configfs_subsystem *subsys,
1098 struct config_item *target)
1099{
1100 int ret;
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001101 struct configfs_dirent *subsys_sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001102 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001103 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001104
1105 /*
1106 * Pin the configfs filesystem. This means we can safely access
1107 * the root of the configfs filesystem.
1108 */
Al Viro2a152ad2012-03-17 16:53:29 -04001109 root = configfs_pin_fs();
1110 if (IS_ERR(root))
1111 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001112
1113 /*
1114 * Next, lock the root directory. We're going to check that the
1115 * subsystem is really registered, and so we need to lock out
1116 * configfs_[un]register_subsystem().
1117 */
Al Viro59551022016-01-22 15:40:57 -05001118 inode_lock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001119
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001120 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
Joel Becker631d1fe2007-06-18 18:06:09 -07001121 if (!subsys_sd) {
1122 ret = -ENOENT;
1123 goto out_unlock_fs;
1124 }
1125
1126 /* Ok, now we can trust subsys/s_item */
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001127 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
Joel Becker631d1fe2007-06-18 18:06:09 -07001128
Joel Becker631d1fe2007-06-18 18:06:09 -07001129out_unlock_fs:
Al Viro59551022016-01-22 15:40:57 -05001130 inode_unlock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001131
1132 /*
1133 * If we succeeded, the fs is pinned via other methods. If not,
1134 * we're done with it anyway. So release_fs() is always right.
1135 */
1136 configfs_release_fs();
1137
1138 return ret;
1139}
1140EXPORT_SYMBOL(configfs_depend_item);
1141
1142/*
1143 * Release the dependent linkage. This is much simpler than
Randy Dunlapce9bebe2020-10-15 20:10:18 -07001144 * configfs_depend_item() because we know that the client driver is
Joel Becker631d1fe2007-06-18 18:06:09 -07001145 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1146 */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +01001147void configfs_undepend_item(struct config_item *target)
Joel Becker631d1fe2007-06-18 18:06:09 -07001148{
1149 struct configfs_dirent *sd;
1150
1151 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001152 * Since we can trust everything is pinned, we just need
1153 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001154 */
Louis Rilling420118c2009-01-28 19:18:33 +01001155 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001156
1157 sd = target->ci_dentry->d_fsdata;
1158 BUG_ON(sd->s_dependent_count < 1);
1159
1160 sd->s_dependent_count -= 1;
1161
1162 /*
1163 * After this unlock, we cannot trust the item to stay alive!
1164 * DO NOT REFERENCE item after this unlock.
1165 */
Louis Rilling420118c2009-01-28 19:18:33 +01001166 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001167}
1168EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001169
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001170/*
1171 * caller_subsys is a caller's subsystem not target's. This is used to
1172 * determine if we should lock root and check subsys or not. When we are
1173 * in the same subsystem as our target there is no need to do locking as
1174 * we know that subsys is valid and is not unregistered during this function
1175 * as we are called from callback of one of his children and VFS holds a lock
1176 * on some inode. Otherwise we have to lock our root to ensure that target's
1177 * subsystem it is not unregistered during this function.
1178 */
1179int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1180 struct config_item *target)
1181{
1182 struct configfs_subsystem *target_subsys;
1183 struct config_group *root, *parent;
1184 struct configfs_dirent *subsys_sd;
1185 int ret = -ENOENT;
1186
1187 /* Disallow this function for configfs root */
1188 if (configfs_is_root(target))
1189 return -EINVAL;
1190
1191 parent = target->ci_group;
1192 /*
1193 * This may happen when someone is trying to depend root
1194 * directory of some subsystem
1195 */
1196 if (configfs_is_root(&parent->cg_item)) {
1197 target_subsys = to_configfs_subsystem(to_config_group(target));
1198 root = parent;
1199 } else {
1200 target_subsys = parent->cg_subsys;
1201 /* Find a cofnigfs root as we may need it for locking */
1202 for (root = parent; !configfs_is_root(&root->cg_item);
1203 root = root->cg_item.ci_group)
1204 ;
1205 }
1206
1207 if (target_subsys != caller_subsys) {
1208 /*
1209 * We are in other configfs subsystem, so we have to do
1210 * additional locking to prevent other subsystem from being
1211 * unregistered
1212 */
Al Viro59551022016-01-22 15:40:57 -05001213 inode_lock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001214
1215 /*
1216 * As we are trying to depend item from other subsystem
1217 * we have to check if this subsystem is still registered
1218 */
1219 subsys_sd = configfs_find_subsys_dentry(
1220 root->cg_item.ci_dentry->d_fsdata,
1221 &target_subsys->su_group.cg_item);
1222 if (!subsys_sd)
1223 goto out_root_unlock;
1224 } else {
1225 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1226 }
1227
1228 /* Now we can execute core of depend item */
1229 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1230
1231 if (target_subsys != caller_subsys)
1232out_root_unlock:
1233 /*
1234 * We were called from subsystem other than our target so we
1235 * took some locks so now it's time to release them
1236 */
Al Viro59551022016-01-22 15:40:57 -05001237 inode_unlock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001238
1239 return ret;
1240}
1241EXPORT_SYMBOL(configfs_depend_item_unlocked);
1242
Christian Brauner549c7292021-01-21 14:19:43 +01001243static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1244 struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001245{
Joel Beckera6795e92008-07-17 15:21:29 -07001246 int ret = 0;
1247 int module_got = 0;
1248 struct config_group *group = NULL;
1249 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001250 struct config_item *parent_item;
1251 struct configfs_subsystem *subsys;
1252 struct configfs_dirent *sd;
Bhumika Goyalaa293582017-10-16 17:18:40 +02001253 const struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001254 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Al Viro47320fb2019-08-25 19:56:13 -04001255 struct configfs_fragment *frag;
Joel Becker7063fbf2005-12-15 14:29:43 -08001256 char *name;
1257
Joel Becker7063fbf2005-12-15 14:29:43 -08001258 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001259
1260 /*
1261 * Fake invisibility if dir belongs to a group/default groups hierarchy
1262 * being attached
1263 */
1264 if (!configfs_dirent_is_ready(sd)) {
1265 ret = -ENOENT;
1266 goto out;
1267 }
1268
Joel Becker84efad12006-03-27 18:46:09 -08001269 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1270 ret = -EPERM;
1271 goto out;
1272 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001273
Al Viro47320fb2019-08-25 19:56:13 -04001274 frag = new_fragment();
1275 if (!frag) {
1276 ret = -ENOMEM;
1277 goto out;
1278 }
1279
Joel Becker84efad12006-03-27 18:46:09 -08001280 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001281 parent_item = configfs_get_config_item(dentry->d_parent);
1282 type = parent_item->ci_type;
1283 subsys = to_config_group(parent_item)->cg_subsys;
1284 BUG_ON(!subsys);
1285
1286 if (!type || !type->ct_group_ops ||
1287 (!type->ct_group_ops->make_group &&
1288 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001289 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1290 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001291 }
1292
Joel Becker70526b62008-06-17 15:34:32 -07001293 /*
1294 * The subsystem may belong to a different module than the item
1295 * being created. We don't want to safely pin the new item but
1296 * fail to pin the subsystem it sits under.
1297 */
1298 if (!subsys->su_group.cg_item.ci_type) {
1299 ret = -EINVAL;
1300 goto out_put;
1301 }
1302 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1303 if (!try_module_get(subsys_owner)) {
1304 ret = -EINVAL;
1305 goto out_put;
1306 }
1307
Joel Becker7063fbf2005-12-15 14:29:43 -08001308 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1309 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001310 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001311 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001312 }
Joel Becker84efad12006-03-27 18:46:09 -08001313
Joel Becker7063fbf2005-12-15 14:29:43 -08001314 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1315
Joel Beckere6bd07a2007-07-06 23:33:17 -07001316 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001317 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001318 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001319 if (!group)
1320 group = ERR_PTR(-ENOMEM);
1321 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001322 link_group(to_config_group(parent_item), group);
1323 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001324 } else
1325 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001326 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001327 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001328 if (!item)
1329 item = ERR_PTR(-ENOMEM);
1330 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001331 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001332 else
1333 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001334 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001335 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001336
1337 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001338 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001339 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001340 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001341 * There are no extra references to clean up.
1342 */
Joel Becker70526b62008-06-17 15:34:32 -07001343 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001344 }
1345
Joel Beckereed7a0d2006-04-11 21:37:20 -07001346 /*
1347 * link_obj() has been called (via link_group() for groups).
1348 * From here on out, errors must clean that up.
1349 */
1350
Joel Becker7063fbf2005-12-15 14:29:43 -08001351 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001352 if (!type) {
1353 ret = -EINVAL;
1354 goto out_unlink;
1355 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001356
Joel Becker70526b62008-06-17 15:34:32 -07001357 new_item_owner = type->ct_owner;
1358 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001359 ret = -EINVAL;
1360 goto out_unlink;
1361 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001362
Joel Beckereed7a0d2006-04-11 21:37:20 -07001363 /*
1364 * I hate doing it this way, but if there is
1365 * an error, module_put() probably should
1366 * happen after any cleanup.
1367 */
1368 module_got = 1;
1369
Louis Rilling6d8344b2008-06-16 19:01:02 +02001370 /*
1371 * Make racing rmdir() fail if it did not tag parent with
1372 * CONFIGFS_USET_DROPPING
1373 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1374 * fail and let rmdir() terminate correctly
1375 */
1376 spin_lock(&configfs_dirent_lock);
1377 /* This will make configfs_detach_prep() fail */
1378 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1379 spin_unlock(&configfs_dirent_lock);
1380
Joel Beckereed7a0d2006-04-11 21:37:20 -07001381 if (group)
Al Viro47320fb2019-08-25 19:56:13 -04001382 ret = configfs_attach_group(parent_item, item, dentry, frag);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001383 else
Al Viro47320fb2019-08-25 19:56:13 -04001384 ret = configfs_attach_item(parent_item, item, dentry, frag);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001385
Louis Rilling6d8344b2008-06-16 19:01:02 +02001386 spin_lock(&configfs_dirent_lock);
1387 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001388 if (!ret)
1389 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001390 spin_unlock(&configfs_dirent_lock);
1391
Joel Beckereed7a0d2006-04-11 21:37:20 -07001392out_unlink:
1393 if (ret) {
1394 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001395 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001396
1397 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001398 if (group)
1399 unlink_group(group);
1400 else
1401 unlink_obj(item);
1402 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001403
Joel Beckere6bd07a2007-07-06 23:33:17 -07001404 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001405
1406 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001407 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001408 }
1409
Joel Becker70526b62008-06-17 15:34:32 -07001410out_subsys_put:
1411 if (ret)
1412 module_put(subsys_owner);
1413
Joel Becker84efad12006-03-27 18:46:09 -08001414out_put:
1415 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001416 * link_obj()/link_group() took a reference from child->parent,
1417 * so the parent is safely pinned. We can drop our working
1418 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001419 */
1420 config_item_put(parent_item);
Al Viro47320fb2019-08-25 19:56:13 -04001421 put_fragment(frag);
Joel Becker84efad12006-03-27 18:46:09 -08001422
1423out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001424 return ret;
1425}
1426
1427static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1428{
1429 struct config_item *parent_item;
1430 struct config_item *item;
1431 struct configfs_subsystem *subsys;
1432 struct configfs_dirent *sd;
Al Virob0841ee2019-08-31 09:43:43 +02001433 struct configfs_fragment *frag;
Joel Becker70526b62008-06-17 15:34:32 -07001434 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001435 int ret;
1436
Joel Becker7063fbf2005-12-15 14:29:43 -08001437 sd = dentry->d_fsdata;
1438 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1439 return -EPERM;
1440
Joel Becker84efad12006-03-27 18:46:09 -08001441 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001442 parent_item = configfs_get_config_item(dentry->d_parent);
1443 subsys = to_config_group(parent_item)->cg_subsys;
1444 BUG_ON(!subsys);
1445
1446 if (!parent_item->ci_type) {
1447 config_item_put(parent_item);
1448 return -EINVAL;
1449 }
1450
Joel Becker70526b62008-06-17 15:34:32 -07001451 /* configfs_mkdir() shouldn't have allowed this */
1452 BUG_ON(!subsys->su_group.cg_item.ci_type);
1453 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1454
Louis Rilling9a73d782008-06-20 14:09:22 +02001455 /*
1456 * Ensure that no racing symlink() will make detach_prep() fail while
1457 * the new link is temporarily attached
1458 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001459 do {
Al Viro48f35b72016-04-12 00:37:59 -04001460 struct dentry *wait;
Louis Rilling6d8344b2008-06-16 19:01:02 +02001461
Louis Rillingde6bf182008-08-15 12:37:23 -07001462 mutex_lock(&configfs_symlink_mutex);
1463 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001464 /*
1465 * Here's where we check for dependents. We're protected by
1466 * configfs_dirent_lock.
1467 * If no dependent, atomically tag the item as dropping.
1468 */
1469 ret = sd->s_dependent_count ? -EBUSY : 0;
1470 if (!ret) {
Al Viro48f35b72016-04-12 00:37:59 -04001471 ret = configfs_detach_prep(dentry, &wait);
Louis Rilling420118c2009-01-28 19:18:33 +01001472 if (ret)
1473 configfs_detach_rollback(dentry);
1474 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001475 spin_unlock(&configfs_dirent_lock);
1476 mutex_unlock(&configfs_symlink_mutex);
1477
1478 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001479 if (ret != -EAGAIN) {
1480 config_item_put(parent_item);
1481 return ret;
1482 }
1483
1484 /* Wait until the racing operation terminates */
Al Viro48f35b72016-04-12 00:37:59 -04001485 inode_lock(d_inode(wait));
1486 inode_unlock(d_inode(wait));
1487 dput(wait);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001488 }
1489 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001490
Al Virob0841ee2019-08-31 09:43:43 +02001491 frag = sd->s_frag;
1492 if (down_write_killable(&frag->frag_sem)) {
1493 spin_lock(&configfs_dirent_lock);
1494 configfs_detach_rollback(dentry);
1495 spin_unlock(&configfs_dirent_lock);
Xiyu Yang8aebfff2020-04-25 20:52:26 +08001496 config_item_put(parent_item);
Al Virob0841ee2019-08-31 09:43:43 +02001497 return -EINTR;
1498 }
1499 frag->frag_dead = true;
1500 up_write(&frag->frag_sem);
1501
Joel Becker84efad12006-03-27 18:46:09 -08001502 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001503 item = configfs_get_config_item(dentry);
1504
1505 /* Drop reference from above, item already holds one. */
1506 config_item_put(parent_item);
1507
1508 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001509 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001510
1511 if (sd->s_type & CONFIGFS_USET_DIR) {
1512 configfs_detach_group(item);
1513
Joel Beckere6bd07a2007-07-06 23:33:17 -07001514 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001515 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001516 unlink_group(to_config_group(item));
1517 } else {
1518 configfs_detach_item(item);
1519
Joel Beckere6bd07a2007-07-06 23:33:17 -07001520 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001521 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001522 unlink_obj(item);
1523 }
1524
1525 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001526 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001527
1528 /* Drop our reference from above */
1529 config_item_put(item);
1530
Joel Becker70526b62008-06-17 15:34:32 -07001531 module_put(dead_item_owner);
1532 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001533
1534 return 0;
1535}
1536
Arjan van de Ven754661f2007-02-12 00:55:38 -08001537const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001538 .mkdir = configfs_mkdir,
1539 .rmdir = configfs_rmdir,
1540 .symlink = configfs_symlink,
1541 .unlink = configfs_unlink,
1542 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001543 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001544};
1545
Al Viro81d44ed12012-03-17 16:13:25 -04001546const struct inode_operations configfs_root_inode_operations = {
1547 .lookup = configfs_lookup,
1548 .setattr = configfs_setattr,
1549};
1550
Joel Becker7063fbf2005-12-15 14:29:43 -08001551static int configfs_dir_open(struct inode *inode, struct file *file)
1552{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001553 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001554 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001555 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001556
Al Viro59551022016-01-22 15:40:57 -05001557 inode_lock(d_inode(dentry));
Louis Rilling2a109f22008-07-04 16:56:05 +02001558 /*
1559 * Fake invisibility if dir belongs to a group/default groups hierarchy
1560 * being attached
1561 */
1562 err = -ENOENT;
1563 if (configfs_dirent_is_ready(parent_sd)) {
Al Viro47320fb2019-08-25 19:56:13 -04001564 file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
Louis Rilling2a109f22008-07-04 16:56:05 +02001565 if (IS_ERR(file->private_data))
1566 err = PTR_ERR(file->private_data);
1567 else
1568 err = 0;
1569 }
Al Viro59551022016-01-22 15:40:57 -05001570 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001571
Louis Rilling2a109f22008-07-04 16:56:05 +02001572 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001573}
1574
1575static int configfs_dir_close(struct inode *inode, struct file *file)
1576{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001577 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001578 struct configfs_dirent * cursor = file->private_data;
1579
Al Viro59551022016-01-22 15:40:57 -05001580 inode_lock(d_inode(dentry));
Louis Rilling6f610762008-06-16 19:00:58 +02001581 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001582 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001583 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001584 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001585
1586 release_configfs_dirent(cursor);
1587
1588 return 0;
1589}
1590
1591/* Relationship between s_mode and the DT_xxx types */
1592static inline unsigned char dt_type(struct configfs_dirent *sd)
1593{
1594 return (sd->s_mode >> 12) & 15;
1595}
1596
Al Viro52018852013-05-16 01:28:34 -04001597static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001598{
Al Viro52018852013-05-16 01:28:34 -04001599 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001600 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001601 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001602 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001603 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001604 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001605
Al Viro52018852013-05-16 01:28:34 -04001606 if (!dir_emit_dots(file, ctx))
1607 return 0;
Al Viroa01b3002016-04-20 19:50:05 -04001608 spin_lock(&configfs_dirent_lock);
1609 if (ctx->pos == 2)
Al Viro52018852013-05-16 01:28:34 -04001610 list_move(q, &parent_sd->s_children);
Al Viro52018852013-05-16 01:28:34 -04001611 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1612 struct configfs_dirent *next;
1613 const char *name;
1614 int len;
1615 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001616
Al Viro52018852013-05-16 01:28:34 -04001617 next = list_entry(p, struct configfs_dirent, s_sibling);
1618 if (!next->s_element)
1619 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001620
Al Viro52018852013-05-16 01:28:34 -04001621 /*
1622 * We'll have a dentry and an inode for
1623 * PINNED items and for open attribute
1624 * files. We lock here to prevent a race
1625 * with configfs_d_iput() clearing
1626 * s_dentry before calling iput().
1627 *
1628 * Why do we go to the trouble? If
1629 * someone has an attribute file open,
1630 * the inode number should match until
1631 * they close it. Beyond that, we don't
1632 * care.
1633 */
Al Viro52018852013-05-16 01:28:34 -04001634 dentry = next->s_dentry;
1635 if (dentry)
David Howells2b0143b2015-03-17 22:25:59 +00001636 inode = d_inode(dentry);
Al Viro52018852013-05-16 01:28:34 -04001637 if (inode)
1638 ino = inode->i_ino;
1639 spin_unlock(&configfs_dirent_lock);
1640 if (!inode)
1641 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001642
Al Viroa01b3002016-04-20 19:50:05 -04001643 name = configfs_get_name(next);
1644 len = strlen(name);
1645
Al Viro52018852013-05-16 01:28:34 -04001646 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1647 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001648
Al Viro52018852013-05-16 01:28:34 -04001649 spin_lock(&configfs_dirent_lock);
1650 list_move(q, p);
Al Viro52018852013-05-16 01:28:34 -04001651 p = q;
1652 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001653 }
Al Viroa01b3002016-04-20 19:50:05 -04001654 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001655 return 0;
1656}
1657
Andrew Morton965c8e52012-12-17 15:59:39 -08001658static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001659{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001660 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001661
Andrew Morton965c8e52012-12-17 15:59:39 -08001662 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001663 case 1:
1664 offset += file->f_pos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001665 fallthrough;
Joel Becker7063fbf2005-12-15 14:29:43 -08001666 case 0:
1667 if (offset >= 0)
1668 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001669 fallthrough;
Joel Becker7063fbf2005-12-15 14:29:43 -08001670 default:
Joel Becker7063fbf2005-12-15 14:29:43 -08001671 return -EINVAL;
1672 }
1673 if (offset != file->f_pos) {
1674 file->f_pos = offset;
1675 if (file->f_pos >= 2) {
1676 struct configfs_dirent *sd = dentry->d_fsdata;
1677 struct configfs_dirent *cursor = file->private_data;
1678 struct list_head *p;
1679 loff_t n = file->f_pos - 2;
1680
Louis Rilling6f610762008-06-16 19:00:58 +02001681 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001682 list_del(&cursor->s_sibling);
1683 p = sd->s_children.next;
1684 while (n && p != &sd->s_children) {
1685 struct configfs_dirent *next;
1686 next = list_entry(p, struct configfs_dirent,
1687 s_sibling);
1688 if (next->s_element)
1689 n--;
1690 p = p->next;
1691 }
1692 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001693 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001694 }
1695 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001696 return offset;
1697}
1698
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001699const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001700 .open = configfs_dir_open,
1701 .release = configfs_dir_close,
1702 .llseek = configfs_dir_lseek,
1703 .read = generic_read_dir,
Al Viroa01b3002016-04-20 19:50:05 -04001704 .iterate_shared = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001705};
1706
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001707/**
1708 * configfs_register_group - creates a parent-child relation between two groups
1709 * @parent_group: parent group
1710 * @group: child group
1711 *
1712 * link groups, creates dentry for the child and attaches it to the
1713 * parent dentry.
1714 *
1715 * Return: 0 on success, negative errno code on error
1716 */
1717int configfs_register_group(struct config_group *parent_group,
1718 struct config_group *group)
1719{
1720 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1721 struct dentry *parent;
Al Viro47320fb2019-08-25 19:56:13 -04001722 struct configfs_fragment *frag;
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001723 int ret;
1724
Al Viro47320fb2019-08-25 19:56:13 -04001725 frag = new_fragment();
1726 if (!frag)
1727 return -ENOMEM;
1728
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001729 mutex_lock(&subsys->su_mutex);
1730 link_group(parent_group, group);
1731 mutex_unlock(&subsys->su_mutex);
1732
1733 parent = parent_group->cg_item.ci_dentry;
1734
Al Viro59551022016-01-22 15:40:57 -05001735 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Al Viro47320fb2019-08-25 19:56:13 -04001736 ret = create_default_group(parent_group, group, frag);
YueHaibing35399f82019-05-05 11:03:12 +08001737 if (ret)
1738 goto err_out;
1739
1740 spin_lock(&configfs_dirent_lock);
1741 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1742 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001743 inode_unlock(d_inode(parent));
Al Viro47320fb2019-08-25 19:56:13 -04001744 put_fragment(frag);
YueHaibing35399f82019-05-05 11:03:12 +08001745 return 0;
1746err_out:
1747 inode_unlock(d_inode(parent));
1748 mutex_lock(&subsys->su_mutex);
1749 unlink_group(group);
1750 mutex_unlock(&subsys->su_mutex);
Al Viro47320fb2019-08-25 19:56:13 -04001751 put_fragment(frag);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001752 return ret;
1753}
1754EXPORT_SYMBOL(configfs_register_group);
1755
1756/**
1757 * configfs_unregister_group() - unregisters a child group from its parent
1758 * @group: parent group to be unregistered
1759 *
1760 * Undoes configfs_register_group()
1761 */
1762void configfs_unregister_group(struct config_group *group)
1763{
1764 struct configfs_subsystem *subsys = group->cg_subsys;
1765 struct dentry *dentry = group->cg_item.ci_dentry;
1766 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
Al Virob0841ee2019-08-31 09:43:43 +02001767 struct configfs_dirent *sd = dentry->d_fsdata;
1768 struct configfs_fragment *frag = sd->s_frag;
1769
1770 down_write(&frag->frag_sem);
1771 frag->frag_dead = true;
1772 up_write(&frag->frag_sem);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001773
Al Viro59551022016-01-22 15:40:57 -05001774 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001775 spin_lock(&configfs_dirent_lock);
1776 configfs_detach_prep(dentry, NULL);
1777 spin_unlock(&configfs_dirent_lock);
1778
1779 configfs_detach_group(&group->cg_item);
1780 d_inode(dentry)->i_flags |= S_DEAD;
1781 dont_mount(dentry);
Amir Goldstein6146e782019-05-26 17:34:09 +03001782 fsnotify_rmdir(d_inode(parent), dentry);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001783 d_delete(dentry);
Al Viro59551022016-01-22 15:40:57 -05001784 inode_unlock(d_inode(parent));
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001785
1786 dput(dentry);
1787
1788 mutex_lock(&subsys->su_mutex);
1789 unlink_group(group);
1790 mutex_unlock(&subsys->su_mutex);
1791}
1792EXPORT_SYMBOL(configfs_unregister_group);
1793
1794/**
1795 * configfs_register_default_group() - allocates and registers a child group
1796 * @parent_group: parent group
1797 * @name: child group name
1798 * @item_type: child item type description
1799 *
1800 * boilerplate to allocate and register a child group with its parent. We need
1801 * kzalloc'ed memory because child's default_group is initially empty.
1802 *
1803 * Return: allocated config group or ERR_PTR() on error
1804 */
1805struct config_group *
1806configfs_register_default_group(struct config_group *parent_group,
1807 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +02001808 const struct config_item_type *item_type)
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001809{
1810 int ret;
1811 struct config_group *group;
1812
1813 group = kzalloc(sizeof(*group), GFP_KERNEL);
1814 if (!group)
1815 return ERR_PTR(-ENOMEM);
1816 config_group_init_type_name(group, name, item_type);
1817
1818 ret = configfs_register_group(parent_group, group);
1819 if (ret) {
1820 kfree(group);
1821 return ERR_PTR(ret);
1822 }
1823 return group;
1824}
1825EXPORT_SYMBOL(configfs_register_default_group);
1826
1827/**
1828 * configfs_unregister_default_group() - unregisters and frees a child group
1829 * @group: the group to act on
1830 */
1831void configfs_unregister_default_group(struct config_group *group)
1832{
1833 configfs_unregister_group(group);
1834 kfree(group);
1835}
1836EXPORT_SYMBOL(configfs_unregister_default_group);
1837
Joel Becker7063fbf2005-12-15 14:29:43 -08001838int configfs_register_subsystem(struct configfs_subsystem *subsys)
1839{
1840 int err;
1841 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001842 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001843 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001844 struct configfs_dirent *sd;
Al Viro47320fb2019-08-25 19:56:13 -04001845 struct configfs_fragment *frag;
1846
1847 frag = new_fragment();
1848 if (!frag)
1849 return -ENOMEM;
Joel Becker7063fbf2005-12-15 14:29:43 -08001850
Al Viro2a152ad2012-03-17 16:53:29 -04001851 root = configfs_pin_fs();
Al Viro47320fb2019-08-25 19:56:13 -04001852 if (IS_ERR(root)) {
1853 put_fragment(frag);
Al Viro2a152ad2012-03-17 16:53:29 -04001854 return PTR_ERR(root);
Al Viro47320fb2019-08-25 19:56:13 -04001855 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001856
1857 if (!group->cg_item.ci_name)
1858 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1859
Al Virob7c177f2012-03-17 16:24:54 -04001860 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001861 link_group(to_config_group(sd->s_element), group);
1862
Al Viro59551022016-01-22 15:40:57 -05001863 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001864
Joel Becker7063fbf2005-12-15 14:29:43 -08001865 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001866 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001867 if (dentry) {
1868 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001869
Joel Beckerafdf04e2007-03-05 15:49:49 -08001870 err = configfs_attach_group(sd->s_element, &group->cg_item,
Al Viro47320fb2019-08-25 19:56:13 -04001871 dentry, frag);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001872 if (err) {
David Howells2b0143b2015-03-17 22:25:59 +00001873 BUG_ON(d_inode(dentry));
Joel Beckerdf7f9962011-02-22 01:09:49 -08001874 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001875 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001876 } else {
1877 spin_lock(&configfs_dirent_lock);
1878 configfs_dir_set_ready(dentry->d_fsdata);
1879 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001880 }
1881 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001882
Al Viro59551022016-01-22 15:40:57 -05001883 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001884
Joel Beckerafdf04e2007-03-05 15:49:49 -08001885 if (err) {
1886 unlink_group(group);
1887 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001888 }
Al Viro47320fb2019-08-25 19:56:13 -04001889 put_fragment(frag);
Joel Becker7063fbf2005-12-15 14:29:43 -08001890
1891 return err;
1892}
1893
1894void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1895{
1896 struct config_group *group = &subsys->su_group;
1897 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001898 struct dentry *root = dentry->d_sb->s_root;
Al Virob0841ee2019-08-31 09:43:43 +02001899 struct configfs_dirent *sd = dentry->d_fsdata;
1900 struct configfs_fragment *frag = sd->s_frag;
Joel Becker7063fbf2005-12-15 14:29:43 -08001901
Al Virob7c177f2012-03-17 16:24:54 -04001902 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001903 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001904 return;
1905 }
1906
Al Virob0841ee2019-08-31 09:43:43 +02001907 down_write(&frag->frag_sem);
1908 frag->frag_dead = true;
1909 up_write(&frag->frag_sem);
1910
Al Viro59551022016-01-22 15:40:57 -05001911 inode_lock_nested(d_inode(root),
Mark Fasheh55ed1602006-10-20 14:55:54 -07001912 I_MUTEX_PARENT);
Al Viro59551022016-01-22 15:40:57 -05001913 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001914 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001915 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001916 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001917 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001918 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001919 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001920 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001921 configfs_detach_group(&group->cg_item);
David Howells2b0143b2015-03-17 22:25:59 +00001922 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001923 dont_mount(dentry);
Amir Goldstein6146e782019-05-26 17:34:09 +03001924 fsnotify_rmdir(d_inode(root), dentry);
Al Viro59551022016-01-22 15:40:57 -05001925 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001926
1927 d_delete(dentry);
1928
Al Viro59551022016-01-22 15:40:57 -05001929 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001930
1931 dput(dentry);
1932
1933 unlink_group(group);
1934 configfs_release_fs();
1935}
1936
1937EXPORT_SYMBOL(configfs_register_subsystem);
1938EXPORT_SYMBOL(configfs_unregister_subsystem);