blob: fbd6f9dbe7a6bcb5a28bf49a1cea182d3ca2c18c [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joel Becker7063fbf2005-12-15 14:29:43 -08002/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * dir.c - Operations for configfs directories.
6 *
Joel Becker7063fbf2005-12-15 14:29:43 -08007 * Based on sysfs:
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
9 *
10 * configfs Copyright (C) 2005 Oracle. All rights reserved.
11 */
12
13#undef DEBUG
14
15#include <linux/fs.h>
Amir Goldstein6146e782019-05-26 17:34:09 +030016#include <linux/fsnotify.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080017#include <linux/mount.h>
18#include <linux/module.h>
19#include <linux/slab.h>
Louis Rilling107ed402008-06-16 19:01:00 +020020#include <linux/err.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080021
22#include <linux/configfs.h>
23#include "configfs_internal.h"
24
25DECLARE_RWSEM(configfs_rename_sem);
Louis Rilling6f610762008-06-16 19:00:58 +020026/*
27 * Protects mutations of configfs_dirent linkage together with proper i_mutex
Louis Rilling5301a772008-06-16 19:00:59 +020028 * Also protects mutations of symlinks linkage to target configfs_dirent
Louis Rilling6f610762008-06-16 19:00:58 +020029 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
30 * and configfs_dirent_lock locked, in that order.
Louis Rilling5301a772008-06-16 19:00:59 +020031 * This allows one to safely traverse configfs_dirent trees and symlinks without
32 * having to lock inodes.
Louis Rillingb3e76af2008-06-16 19:01:01 +020033 *
34 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
35 * unlocked is not reliable unless in detach_groups() called from
36 * rmdir()/unregister() and from configfs_attach_group()
Louis Rilling6f610762008-06-16 19:00:58 +020037 */
38DEFINE_SPINLOCK(configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080039
40static void configfs_d_iput(struct dentry * dentry,
41 struct inode * inode)
42{
Joel Becker24307aa2011-05-18 04:08:16 -070043 struct configfs_dirent *sd = dentry->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -080044
45 if (sd) {
Joel Becker24307aa2011-05-18 04:08:16 -070046 /* Coordinate with configfs_readdir */
47 spin_lock(&configfs_dirent_lock);
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053048 /*
49 * Set sd->s_dentry to null only when this dentry is the one
50 * that is going to be killed. Otherwise configfs_d_iput may
51 * run just after configfs_attach_attr and set sd->s_dentry to
52 * NULL even it's still in use.
Junxiao Bi76ae2812013-11-21 14:31:56 -080053 */
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053054 if (sd->s_dentry == dentry)
Junxiao Bi76ae2812013-11-21 14:31:56 -080055 sd->s_dentry = NULL;
56
Joel Becker24307aa2011-05-18 04:08:16 -070057 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080058 configfs_put(sd);
59 }
60 iput(inode);
61}
62
Al Virod463a0c2011-01-12 16:41:05 -050063const struct dentry_operations configfs_dentry_ops = {
Joel Becker7063fbf2005-12-15 14:29:43 -080064 .d_iput = configfs_d_iput,
Al Virob26d4cd2013-10-25 18:47:37 -040065 .d_delete = always_delete_dentry,
Joel Becker7063fbf2005-12-15 14:29:43 -080066};
67
Louis Rillinge74cc062009-01-28 19:18:32 +010068#ifdef CONFIG_LOCKDEP
69
70/*
71 * Helpers to make lockdep happy with our recursive locking of default groups'
72 * inodes (see configfs_attach_group() and configfs_detach_group()).
73 * We put default groups i_mutexes in separate classes according to their depth
74 * from the youngest non-default group ancestor.
75 *
76 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
77 * groups A/B and A/C will have their inode's mutex in class
78 * default_group_class[0], and default group A/C/D will be in
79 * default_group_class[1].
80 *
81 * The lock classes are declared and assigned in inode.c, according to the
82 * s_depth value.
83 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
84 * default groups, and reset to -1 when all default groups are attached. During
85 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
86 * inode's mutex is set to default_group_class[s_depth - 1].
87 */
88
89static void configfs_init_dirent_depth(struct configfs_dirent *sd)
90{
91 sd->s_depth = -1;
92}
93
94static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
95 struct configfs_dirent *sd)
96{
97 int parent_depth = parent_sd->s_depth;
98
99 if (parent_depth >= 0)
100 sd->s_depth = parent_depth + 1;
101}
102
103static void
104configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
105{
106 /*
107 * item's i_mutex class is already setup, so s_depth is now only
108 * used to set new sub-directories s_depth, which is always done
109 * with item's i_mutex locked.
110 */
111 /*
112 * sd->s_depth == -1 iff we are a non default group.
113 * else (we are a default group) sd->s_depth > 0 (see
114 * create_dir()).
115 */
116 if (sd->s_depth == -1)
117 /*
118 * We are a non default group and we are going to create
119 * default groups.
120 */
121 sd->s_depth = 0;
122}
123
124static void
125configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
126{
127 /* We will not create default groups anymore. */
128 sd->s_depth = -1;
129}
130
131#else /* CONFIG_LOCKDEP */
132
133static void configfs_init_dirent_depth(struct configfs_dirent *sd)
134{
135}
136
137static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
138 struct configfs_dirent *sd)
139{
140}
141
142static void
143configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
144{
145}
146
147static void
148configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
149{
150}
151
152#endif /* CONFIG_LOCKDEP */
153
Al Viro47320fb2019-08-25 19:56:13 -0400154static struct configfs_fragment *new_fragment(void)
155{
156 struct configfs_fragment *p;
157
158 p = kmalloc(sizeof(struct configfs_fragment), GFP_KERNEL);
159 if (p) {
160 atomic_set(&p->frag_count, 1);
161 init_rwsem(&p->frag_sem);
162 p->frag_dead = false;
163 }
164 return p;
165}
166
167void put_fragment(struct configfs_fragment *frag)
168{
169 if (frag && atomic_dec_and_test(&frag->frag_count))
170 kfree(frag);
171}
172
173struct configfs_fragment *get_fragment(struct configfs_fragment *frag)
174{
175 if (likely(frag))
176 atomic_inc(&frag->frag_count);
177 return frag;
178}
179
Joel Becker7063fbf2005-12-15 14:29:43 -0800180/*
181 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
182 */
Louis Rilling420118c2009-01-28 19:18:33 +0100183static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
Al Viro47320fb2019-08-25 19:56:13 -0400184 void *element, int type,
185 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800186{
187 struct configfs_dirent * sd;
188
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800189 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -0800190 if (!sd)
Louis Rilling107ed402008-06-16 19:01:00 +0200191 return ERR_PTR(-ENOMEM);
Joel Becker7063fbf2005-12-15 14:29:43 -0800192
Joel Becker7063fbf2005-12-15 14:29:43 -0800193 atomic_set(&sd->s_count, 1);
194 INIT_LIST_HEAD(&sd->s_links);
195 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -0800196 sd->s_element = element;
Louis Rilling420118c2009-01-28 19:18:33 +0100197 sd->s_type = type;
Louis Rillinge74cc062009-01-28 19:18:32 +0100198 configfs_init_dirent_depth(sd);
Louis Rilling6f610762008-06-16 19:00:58 +0200199 spin_lock(&configfs_dirent_lock);
Louis Rillingb3e76af2008-06-16 19:01:01 +0200200 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
201 spin_unlock(&configfs_dirent_lock);
202 kmem_cache_free(configfs_dir_cachep, sd);
203 return ERR_PTR(-ENOENT);
204 }
Al Viro47320fb2019-08-25 19:56:13 -0400205 sd->s_frag = get_fragment(frag);
Louis Rilling6f610762008-06-16 19:00:58 +0200206 list_add(&sd->s_sibling, &parent_sd->s_children);
207 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800208
209 return sd;
210}
211
Joel Beckerb4c98f62006-09-13 11:01:19 -0700212/*
213 *
214 * Return -EEXIST if there is already a configfs element with the same
215 * name for the same parent.
216 *
217 * called with parent inode's i_mutex held
218 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100219static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
220 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700221{
222 struct configfs_dirent * sd;
223
224 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
225 if (sd->s_element) {
226 const unsigned char *existing = configfs_get_name(sd);
227 if (strcmp(existing, new))
228 continue;
229 else
230 return -EEXIST;
231 }
232 }
233
234 return 0;
235}
236
237
Joel Becker7063fbf2005-12-15 14:29:43 -0800238int configfs_make_dirent(struct configfs_dirent * parent_sd,
239 struct dentry * dentry, void * element,
Al Viro47320fb2019-08-25 19:56:13 -0400240 umode_t mode, int type, struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800241{
242 struct configfs_dirent * sd;
243
Al Viro47320fb2019-08-25 19:56:13 -0400244 sd = configfs_new_dirent(parent_sd, element, type, frag);
Louis Rilling107ed402008-06-16 19:01:00 +0200245 if (IS_ERR(sd))
246 return PTR_ERR(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800247
248 sd->s_mode = mode;
Joel Becker7063fbf2005-12-15 14:29:43 -0800249 sd->s_dentry = dentry;
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100250 if (dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800251 dentry->d_fsdata = configfs_get(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800252
253 return 0;
254}
255
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200256static void configfs_remove_dirent(struct dentry *dentry)
257{
258 struct configfs_dirent *sd = dentry->d_fsdata;
259
260 if (!sd)
261 return;
262 spin_lock(&configfs_dirent_lock);
263 list_del_init(&sd->s_sibling);
264 spin_unlock(&configfs_dirent_lock);
265 configfs_put(sd);
266}
267
Al Viroc88b1e72015-01-29 00:17:57 -0500268static void init_dir(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800269{
270 inode->i_op = &configfs_dir_inode_operations;
271 inode->i_fop = &configfs_dir_operations;
272
273 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700274 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800275}
276
Al Viroc88b1e72015-01-29 00:17:57 -0500277static void configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800278{
279 inode->i_size = PAGE_SIZE;
280 inode->i_fop = &configfs_file_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800281}
282
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300283static void configfs_init_bin_file(struct inode *inode)
284{
285 inode->i_size = 0;
286 inode->i_fop = &configfs_bin_file_operations;
287}
288
Al Viroc88b1e72015-01-29 00:17:57 -0500289static void init_symlink(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800290{
291 inode->i_op = &configfs_symlink_inode_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800292}
293
Joel Becker7063fbf2005-12-15 14:29:43 -0800294/**
295 * configfs_create_dir - create a directory for an config_item.
296 * @item: config_itemwe're creating directory for.
297 * @dentry: config_item's dentry.
Louis Rilling2a109f22008-07-04 16:56:05 +0200298 *
299 * Note: user-created entries won't be allowed under this new directory
300 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800301 */
302
Al Viro47320fb2019-08-25 19:56:13 -0400303static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
304 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800305{
Al Viro1cf97d02015-01-29 00:20:49 -0500306 int error;
307 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
308 struct dentry *p = dentry->d_parent;
309
310 BUG_ON(!item);
311
312 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
313 if (unlikely(error))
314 return error;
315
316 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
Al Viro47320fb2019-08-25 19:56:13 -0400317 CONFIGFS_DIR | CONFIGFS_USET_CREATING,
318 frag);
Al Viro1cf97d02015-01-29 00:20:49 -0500319 if (unlikely(error))
320 return error;
321
322 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
323 error = configfs_create(dentry, mode, init_dir);
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200324 if (error)
325 goto out_remove;
326
327 inc_nlink(d_inode(p));
328 item->ci_dentry = dentry;
329 return 0;
330
331out_remove:
332 configfs_remove_dirent(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800333 return error;
334}
335
Louis Rilling2a109f22008-07-04 16:56:05 +0200336/*
337 * Allow userspace to create new entries under a new directory created with
338 * configfs_create_dir(), and under all of its chidlren directories recursively.
339 * @sd configfs_dirent of the new directory to validate
340 *
341 * Caller must hold configfs_dirent_lock.
342 */
343static void configfs_dir_set_ready(struct configfs_dirent *sd)
344{
345 struct configfs_dirent *child_sd;
346
347 sd->s_type &= ~CONFIGFS_USET_CREATING;
348 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
349 if (child_sd->s_type & CONFIGFS_USET_CREATING)
350 configfs_dir_set_ready(child_sd);
351}
352
353/*
354 * Check that a directory does not belong to a directory hierarchy being
355 * attached and not validated yet.
356 * @sd configfs_dirent of the directory to check
357 *
358 * @return non-zero iff the directory was validated
359 *
360 * Note: takes configfs_dirent_lock, so the result may change from false to true
361 * in two consecutive calls, but never from true to false.
362 */
363int configfs_dirent_is_ready(struct configfs_dirent *sd)
364{
365 int ret;
366
367 spin_lock(&configfs_dirent_lock);
368 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
369 spin_unlock(&configfs_dirent_lock);
370
371 return ret;
372}
373
Joel Becker7063fbf2005-12-15 14:29:43 -0800374int configfs_create_link(struct configfs_symlink *sl,
375 struct dentry *parent,
376 struct dentry *dentry)
377{
378 int err = 0;
379 umode_t mode = S_IFLNK | S_IRWXUGO;
Al Viro47320fb2019-08-25 19:56:13 -0400380 struct configfs_dirent *p = parent->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -0800381
Al Viro47320fb2019-08-25 19:56:13 -0400382 err = configfs_make_dirent(p, dentry, sl, mode,
383 CONFIGFS_ITEM_LINK, p->s_frag);
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200384 if (err)
385 return err;
386
387 err = configfs_create(dentry, mode, init_symlink);
388 if (err)
389 goto out_remove;
390 return 0;
391
392out_remove:
393 configfs_remove_dirent(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800394 return err;
395}
396
397static void remove_dir(struct dentry * d)
398{
399 struct dentry * parent = dget(d->d_parent);
Joel Becker7063fbf2005-12-15 14:29:43 -0800400
Christoph Hellwig1cf7a002019-09-11 08:50:46 +0200401 configfs_remove_dirent(d);
402
David Howells2b0143b2015-03-17 22:25:59 +0000403 if (d_really_is_positive(d))
404 simple_rmdir(d_inode(parent),d);
Joel Becker7063fbf2005-12-15 14:29:43 -0800405
Al Viroa4555892014-10-21 20:11:25 -0400406 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800407
408 dput(parent);
409}
410
411/**
412 * configfs_remove_dir - remove an config_item's directory.
413 * @item: config_item we're removing.
414 *
415 * The only thing special about this is that we remove any files in
416 * the directory before we remove the directory, and we've inlined
417 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200418 *
419 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800420 */
421
422static void configfs_remove_dir(struct config_item * item)
423{
424 struct dentry * dentry = dget(item->ci_dentry);
425
426 if (!dentry)
427 return;
428
429 remove_dir(dentry);
430 /**
431 * Drop reference from dget() on entrance.
432 */
433 dput(dentry);
434}
435
436
437/* attaches attribute's configfs_dirent to the dentry corresponding to the
438 * attribute file
439 */
440static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
441{
442 struct configfs_attribute * attr = sd->s_element;
443 int error;
444
Junxiao Bi76ae2812013-11-21 14:31:56 -0800445 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800446 dentry->d_fsdata = configfs_get(sd);
447 sd->s_dentry = dentry;
Junxiao Bi76ae2812013-11-21 14:31:56 -0800448 spin_unlock(&configfs_dirent_lock);
449
Dave Hansence8d2cd2007-10-16 23:31:13 -0700450 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300451 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
452 configfs_init_bin_file :
453 configfs_init_file);
Al Viro5cf3b562016-03-07 14:25:46 -0500454 if (error)
Joel Becker3d0f89b2006-01-25 13:31:07 -0800455 configfs_put(sd);
Al Viro5cf3b562016-03-07 14:25:46 -0500456 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800457}
458
459static struct dentry * configfs_lookup(struct inode *dir,
460 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400461 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800462{
463 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
464 struct configfs_dirent * sd;
465 int found = 0;
Louis Rilling2a109f22008-07-04 16:56:05 +0200466 int err;
467
468 /*
469 * Fake invisibility if dir belongs to a group/default groups hierarchy
470 * being attached
471 *
472 * This forbids userspace to read/write attributes of items which may
473 * not complete their initialization, since the dentries of the
474 * attributes won't be instantiated.
475 */
476 err = -ENOENT;
477 if (!configfs_dirent_is_ready(parent_sd))
478 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800479
480 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
481 if (sd->s_type & CONFIGFS_NOT_PINNED) {
482 const unsigned char * name = configfs_get_name(sd);
483
484 if (strcmp(name, dentry->d_name.name))
485 continue;
486
487 found = 1;
488 err = configfs_attach_attr(sd, dentry);
489 break;
490 }
491 }
492
493 if (!found) {
494 /*
495 * If it doesn't exist and it isn't a NOT_PINNED item,
496 * it must be negative.
497 */
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100498 if (dentry->d_name.len > NAME_MAX)
499 return ERR_PTR(-ENAMETOOLONG);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100500 d_add(dentry, NULL);
501 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800502 }
503
Louis Rilling2a109f22008-07-04 16:56:05 +0200504out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800505 return ERR_PTR(err);
506}
507
508/*
509 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200510 * attributes and are removed by rmdir(). We recurse, setting
511 * CONFIGFS_USET_DROPPING on all children that are candidates for
512 * default detach.
513 * If there is an error, the caller will reset the flags via
514 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800515 */
Al Viro48f35b72016-04-12 00:37:59 -0400516static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
Joel Becker7063fbf2005-12-15 14:29:43 -0800517{
518 struct configfs_dirent *parent_sd = dentry->d_fsdata;
519 struct configfs_dirent *sd;
520 int ret;
521
Louis Rilling4768e9b2008-06-23 14:16:17 +0200522 /* Mark that we're trying to drop the group */
523 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
524
Joel Becker7063fbf2005-12-15 14:29:43 -0800525 ret = -EBUSY;
526 if (!list_empty(&parent_sd->s_links))
527 goto out;
528
529 ret = 0;
530 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200531 if (!sd->s_element ||
532 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800533 continue;
534 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200535 /* Abort if racing with mkdir() */
536 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
Al Viro48f35b72016-04-12 00:37:59 -0400537 if (wait)
538 *wait= dget(sd->s_dentry);
Louis Rilling6d8344b2008-06-16 19:01:02 +0200539 return -EAGAIN;
540 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800541
Joel Becker631d1fe2007-06-18 18:06:09 -0700542 /*
543 * Yup, recursive. If there's a problem, blame
544 * deep nesting of default_groups
545 */
Al Viro48f35b72016-04-12 00:37:59 -0400546 ret = configfs_detach_prep(sd->s_dentry, wait);
Joel Becker7063fbf2005-12-15 14:29:43 -0800547 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800548 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800549 } else
550 ret = -ENOTEMPTY;
551
552 break;
553 }
554
555out:
556 return ret;
557}
558
559/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200560 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800561 * set.
562 */
563static void configfs_detach_rollback(struct dentry *dentry)
564{
565 struct configfs_dirent *parent_sd = dentry->d_fsdata;
566 struct configfs_dirent *sd;
567
Louis Rilling4768e9b2008-06-23 14:16:17 +0200568 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
569
570 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
571 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800572 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800573}
574
575static void detach_attrs(struct config_item * item)
576{
577 struct dentry * dentry = dget(item->ci_dentry);
578 struct configfs_dirent * parent_sd;
579 struct configfs_dirent * sd, * tmp;
580
581 if (!dentry)
582 return;
583
584 pr_debug("configfs %s: dropping attrs for dir\n",
585 dentry->d_name.name);
586
587 parent_sd = dentry->d_fsdata;
588 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
589 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
590 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200591 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800592 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200593 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800594 configfs_drop_dentry(sd, dentry);
595 configfs_put(sd);
596 }
597
598 /**
599 * Drop reference from dget() on entrance.
600 */
601 dput(dentry);
602}
603
604static int populate_attrs(struct config_item *item)
605{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200606 const struct config_item_type *t = item->ci_type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800607 struct configfs_attribute *attr;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300608 struct configfs_bin_attribute *bin_attr;
Joel Becker7063fbf2005-12-15 14:29:43 -0800609 int error = 0;
610 int i;
611
612 if (!t)
613 return -EINVAL;
614 if (t->ct_attrs) {
615 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
616 if ((error = configfs_create_file(item, attr)))
617 break;
618 }
619 }
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300620 if (t->ct_bin_attrs) {
621 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
622 error = configfs_create_bin_file(item, bin_attr);
623 if (error)
624 break;
625 }
626 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800627
628 if (error)
629 detach_attrs(item);
630
631 return error;
632}
633
634static int configfs_attach_group(struct config_item *parent_item,
635 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400636 struct dentry *dentry,
637 struct configfs_fragment *frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800638static void configfs_detach_group(struct config_item *item);
639
640static void detach_groups(struct config_group *group)
641{
642 struct dentry * dentry = dget(group->cg_item.ci_dentry);
643 struct dentry *child;
644 struct configfs_dirent *parent_sd;
645 struct configfs_dirent *sd, *tmp;
646
647 if (!dentry)
648 return;
649
650 parent_sd = dentry->d_fsdata;
651 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
652 if (!sd->s_element ||
653 !(sd->s_type & CONFIGFS_USET_DEFAULT))
654 continue;
655
656 child = sd->s_dentry;
657
Al Viro59551022016-01-22 15:40:57 -0500658 inode_lock(d_inode(child));
Louis Rillingb3e76af2008-06-16 19:01:01 +0200659
Joel Becker7063fbf2005-12-15 14:29:43 -0800660 configfs_detach_group(sd->s_element);
David Howells2b0143b2015-03-17 22:25:59 +0000661 d_inode(child)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400662 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800663
Al Viro59551022016-01-22 15:40:57 -0500664 inode_unlock(d_inode(child));
Joel Becker7063fbf2005-12-15 14:29:43 -0800665
666 d_delete(child);
667 dput(child);
668 }
669
670 /**
671 * Drop reference from dget() on entrance.
672 */
673 dput(dentry);
674}
675
676/*
677 * This fakes mkdir(2) on a default_groups[] entry. It
678 * creates a dentry, attachs it, and then does fixup
679 * on the sd->s_type.
680 *
681 * We could, perhaps, tweak our parent's ->mkdir for a minute and
682 * try using vfs_mkdir. Just a thought.
683 */
684static int create_default_group(struct config_group *parent_group,
Al Viro47320fb2019-08-25 19:56:13 -0400685 struct config_group *group,
686 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800687{
688 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800689 struct configfs_dirent *sd;
690 /* We trust the caller holds a reference to parent */
691 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
692
693 if (!group->cg_item.ci_name)
694 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800695
696 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400697 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800698 if (child) {
699 d_add(child, NULL);
700
701 ret = configfs_attach_group(&parent_group->cg_item,
Al Viro47320fb2019-08-25 19:56:13 -0400702 &group->cg_item, child, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800703 if (!ret) {
704 sd = child->d_fsdata;
705 sd->s_type |= CONFIGFS_USET_DEFAULT;
706 } else {
David Howells2b0143b2015-03-17 22:25:59 +0000707 BUG_ON(d_inode(child));
Joel Beckerdf7f9962011-02-22 01:09:49 -0800708 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800709 dput(child);
710 }
711 }
712
713 return ret;
714}
715
Al Viro47320fb2019-08-25 19:56:13 -0400716static int populate_groups(struct config_group *group,
717 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800718{
719 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800720 int ret = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800721
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100722 list_for_each_entry(new_group, &group->default_groups, group_entry) {
Al Viro47320fb2019-08-25 19:56:13 -0400723 ret = create_default_group(group, new_group, frag);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100724 if (ret) {
725 detach_groups(group);
726 break;
Joel Becker7063fbf2005-12-15 14:29:43 -0800727 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800728 }
729
Joel Becker7063fbf2005-12-15 14:29:43 -0800730 return ret;
731}
732
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100733void configfs_remove_default_groups(struct config_group *group)
734{
735 struct config_group *g, *n;
736
737 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
738 list_del(&g->group_entry);
739 config_item_put(&g->cg_item);
740 }
741}
742EXPORT_SYMBOL(configfs_remove_default_groups);
743
Joel Becker7063fbf2005-12-15 14:29:43 -0800744/*
745 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700746 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800747 */
748
749static void unlink_obj(struct config_item *item)
750{
751 struct config_group *group;
752
753 group = item->ci_group;
754 if (group) {
755 list_del_init(&item->ci_entry);
756
757 item->ci_group = NULL;
758 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700759
760 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800761 config_item_put(item);
762
Joel Beckereed7a0d2006-04-11 21:37:20 -0700763 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800764 config_group_put(group);
765 }
766}
767
768static void link_obj(struct config_item *parent_item, struct config_item *item)
769{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700770 /*
771 * Parent seems redundant with group, but it makes certain
772 * traversals much nicer.
773 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800774 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700775
776 /*
777 * We hold a reference on the parent for the child's ci_parent
778 * link.
779 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800780 item->ci_group = config_group_get(to_config_group(parent_item));
781 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
782
Joel Beckereed7a0d2006-04-11 21:37:20 -0700783 /*
784 * We hold a reference on the child for ci_entry on the parent's
785 * cg_children
786 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800787 config_item_get(item);
788}
789
790static void unlink_group(struct config_group *group)
791{
Joel Becker7063fbf2005-12-15 14:29:43 -0800792 struct config_group *new_group;
793
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100794 list_for_each_entry(new_group, &group->default_groups, group_entry)
795 unlink_group(new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800796
797 group->cg_subsys = NULL;
798 unlink_obj(&group->cg_item);
799}
800
801static void link_group(struct config_group *parent_group, struct config_group *group)
802{
Joel Becker7063fbf2005-12-15 14:29:43 -0800803 struct config_group *new_group;
804 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
805
806 link_obj(&parent_group->cg_item, &group->cg_item);
807
808 if (parent_group->cg_subsys)
809 subsys = parent_group->cg_subsys;
810 else if (configfs_is_root(&parent_group->cg_item))
811 subsys = to_configfs_subsystem(group);
812 else
813 BUG();
814 group->cg_subsys = subsys;
815
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100816 list_for_each_entry(new_group, &group->default_groups, group_entry)
817 link_group(group, new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800818}
819
820/*
821 * The goal is that configfs_attach_item() (and
822 * configfs_attach_group()) can be called from either the VFS or this
823 * module. That is, they assume that the items have been created,
824 * the dentry allocated, and the dcache is all ready to go.
825 *
826 * If they fail, they must clean up after themselves as if they
827 * had never been called. The caller (VFS or local function) will
828 * handle cleaning up the dcache bits.
829 *
830 * configfs_detach_group() and configfs_detach_item() behave similarly on
831 * the way out. They assume that the proper semaphores are held, they
832 * clean up the configfs items, and they expect their callers will
833 * handle the dcache bits.
834 */
835static int configfs_attach_item(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
Al Viro47320fb2019-08-25 19:56:13 -0400842 ret = configfs_create_dir(item, dentry, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800843 if (!ret) {
844 ret = populate_attrs(item);
845 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200846 /*
847 * We are going to remove an inode and its dentry but
848 * the VFS may already have hit and used them. Thus,
849 * we must lock them as rmdir() would.
850 */
Al Viro59551022016-01-22 15:40:57 -0500851 inode_lock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800852 configfs_remove_dir(item);
David Howells2b0143b2015-03-17 22:25:59 +0000853 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400854 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -0500855 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800856 d_delete(dentry);
857 }
858 }
859
860 return ret;
861}
862
Louis Rilling2e2ce172008-07-04 16:56:06 +0200863/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800864static void configfs_detach_item(struct config_item *item)
865{
866 detach_attrs(item);
867 configfs_remove_dir(item);
868}
869
870static int configfs_attach_group(struct config_item *parent_item,
871 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400872 struct dentry *dentry,
873 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800874{
875 int ret;
876 struct configfs_dirent *sd;
877
Al Viro47320fb2019-08-25 19:56:13 -0400878 ret = configfs_attach_item(parent_item, item, dentry, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800879 if (!ret) {
880 sd = dentry->d_fsdata;
881 sd->s_type |= CONFIGFS_USET_DIR;
882
Louis Rilling2e2ce172008-07-04 16:56:06 +0200883 /*
884 * FYI, we're faking mkdir in populate_groups()
885 * We must lock the group's inode to avoid races with the VFS
886 * which can already hit the inode and try to add/remove entries
887 * under it.
888 *
889 * We must also lock the inode to remove it safely in case of
890 * error, as rmdir() would.
891 */
Al Viro59551022016-01-22 15:40:57 -0500892 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100893 configfs_adjust_dir_dirent_depth_before_populate(sd);
Al Viro47320fb2019-08-25 19:56:13 -0400894 ret = populate_groups(to_config_group(item), frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800895 if (ret) {
896 configfs_detach_item(item);
David Howells2b0143b2015-03-17 22:25:59 +0000897 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400898 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800899 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100900 configfs_adjust_dir_dirent_depth_after_populate(sd);
Al Viro59551022016-01-22 15:40:57 -0500901 inode_unlock(d_inode(dentry));
Louis Rilling2e2ce172008-07-04 16:56:06 +0200902 if (ret)
903 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800904 }
905
906 return ret;
907}
908
Louis Rilling2e2ce172008-07-04 16:56:06 +0200909/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800910static void configfs_detach_group(struct config_item *item)
911{
912 detach_groups(to_config_group(item));
913 configfs_detach_item(item);
914}
915
916/*
Joel Becker299894c2006-10-06 17:33:23 -0700917 * After the item has been detached from the filesystem view, we are
918 * ready to tear it out of the hierarchy. Notify the client before
919 * we do that so they can perform any cleanup that requires
920 * navigating the hierarchy. A client does not need to provide this
921 * callback. The subsystem semaphore MUST be held by the caller, and
922 * references must be valid for both items. It also assumes the
923 * caller has validated ci_type.
924 */
925static void client_disconnect_notify(struct config_item *parent_item,
926 struct config_item *item)
927{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200928 const struct config_item_type *type;
Joel Becker299894c2006-10-06 17:33:23 -0700929
930 type = parent_item->ci_type;
931 BUG_ON(!type);
932
933 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
934 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
935 item);
936}
937
938/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800939 * Drop the initial reference from make_item()/make_group()
940 * This function assumes that reference is held on item
941 * and that item holds a valid reference to the parent. Also, it
942 * assumes the caller has validated ci_type.
943 */
944static void client_drop_item(struct config_item *parent_item,
945 struct config_item *item)
946{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200947 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800948
949 type = parent_item->ci_type;
950 BUG_ON(!type);
951
Joel Beckereed7a0d2006-04-11 21:37:20 -0700952 /*
953 * If ->drop_item() exists, it is responsible for the
954 * config_item_put().
955 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800956 if (type->ct_group_ops && type->ct_group_ops->drop_item)
957 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700958 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800959 else
960 config_item_put(item);
961}
962
Joel Becker631d1fe2007-06-18 18:06:09 -0700963#ifdef DEBUG
964static void configfs_dump_one(struct configfs_dirent *sd, int level)
965{
Fabian Frederickc6686932014-06-04 16:05:58 -0700966 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700967
Fabian Frederickc6686932014-06-04 16:05:58 -0700968#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700969 type_print(CONFIGFS_ROOT);
970 type_print(CONFIGFS_DIR);
971 type_print(CONFIGFS_ITEM_ATTR);
972 type_print(CONFIGFS_ITEM_LINK);
973 type_print(CONFIGFS_USET_DIR);
974 type_print(CONFIGFS_USET_DEFAULT);
975 type_print(CONFIGFS_USET_DROPPING);
976#undef type_print
977}
978
979static int configfs_dump(struct configfs_dirent *sd, int level)
980{
981 struct configfs_dirent *child_sd;
982 int ret = 0;
983
984 configfs_dump_one(sd, level);
985
986 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
987 return 0;
988
989 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
990 ret = configfs_dump(child_sd, level + 2);
991 if (ret)
992 break;
993 }
994
995 return ret;
996}
997#endif
998
999
1000/*
1001 * configfs_depend_item() and configfs_undepend_item()
1002 *
1003 * WARNING: Do not call these from a configfs callback!
1004 *
1005 * This describes these functions and their helpers.
1006 *
1007 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001008 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -07001009 * it. The idea is to give client modules as simple an interface as
1010 * possible. When a system asks them to depend on an item, they just
1011 * call configfs_depend_item(). If the item is live and the client
1012 * driver is in good shape, we'll happily do the work for them.
1013 *
1014 * Why is the locking complex? Because configfs uses the VFS to handle
1015 * all locking, but this function is called outside the normal
1016 * VFS->configfs path. So it must take VFS locks to prevent the
1017 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1018 * why you can't call these functions underneath configfs callbacks.
1019 *
1020 * Note, btw, that this can be called at *any* time, even when a configfs
1021 * subsystem isn't registered, or when configfs is loading or unloading.
1022 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +01001023 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1024 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -07001025 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +01001026 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1027 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -07001028 */
1029
1030/*
1031 * configfs_depend_prep()
1032 *
1033 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1034 * attributes. This is similar but not the same to configfs_detach_prep().
1035 * Note that configfs_detach_prep() expects the parent to be locked when it
1036 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1037 * do that so we can unlock it if we find nothing.
1038 *
1039 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001040 * our object.
1041 * We deliberately ignore items tagged as dropping since they are virtually
1042 * dead, as well as items in the middle of attachment since they virtually
1043 * do not exist yet. This completes the locking out of racing mkdir() and
1044 * rmdir().
1045 * Note: subdirectories in the middle of attachment start with s_type =
1046 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1047 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1048 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001049 *
Louis Rilling420118c2009-01-28 19:18:33 +01001050 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001051 *
1052 * This adds a requirement that all config_items be unique!
1053 *
Louis Rilling420118c2009-01-28 19:18:33 +01001054 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001055 * much on the stack, though, so folks that need this function - be careful
1056 * about your stack! Patches will be accepted to make it iterative.
1057 */
1058static int configfs_depend_prep(struct dentry *origin,
1059 struct config_item *target)
1060{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001061 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001062 int ret = 0;
1063
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001064 BUG_ON(!origin || !origin->d_fsdata);
1065 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001066
Joel Becker631d1fe2007-06-18 18:06:09 -07001067 if (sd->s_element == target) /* Boo-yah */
1068 goto out;
1069
1070 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001071 if ((child_sd->s_type & CONFIGFS_DIR) &&
1072 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1073 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001074 ret = configfs_depend_prep(child_sd->s_dentry,
1075 target);
1076 if (!ret)
1077 goto out; /* Child path boo-yah */
1078 }
1079 }
1080
1081 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001082 ret = -ENOENT;
1083
1084out:
1085 return ret;
1086}
1087
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001088static int configfs_do_depend_item(struct dentry *subsys_dentry,
1089 struct config_item *target)
1090{
1091 struct configfs_dirent *p;
1092 int ret;
1093
1094 spin_lock(&configfs_dirent_lock);
1095 /* Scan the tree, return 0 if found */
1096 ret = configfs_depend_prep(subsys_dentry, target);
1097 if (ret)
1098 goto out_unlock_dirent_lock;
1099
1100 /*
1101 * We are sure that the item is not about to be removed by rmdir(), and
1102 * not in the middle of attachment by mkdir().
1103 */
1104 p = target->ci_dentry->d_fsdata;
1105 p->s_dependent_count += 1;
1106
1107out_unlock_dirent_lock:
1108 spin_unlock(&configfs_dirent_lock);
1109
1110 return ret;
1111}
1112
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001113static inline struct configfs_dirent *
1114configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1115 struct config_item *subsys_item)
1116{
1117 struct configfs_dirent *p;
1118 struct configfs_dirent *ret = NULL;
1119
1120 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1121 if (p->s_type & CONFIGFS_DIR &&
1122 p->s_element == subsys_item) {
1123 ret = p;
1124 break;
1125 }
1126 }
1127
1128 return ret;
1129}
1130
1131
Joel Becker631d1fe2007-06-18 18:06:09 -07001132int configfs_depend_item(struct configfs_subsystem *subsys,
1133 struct config_item *target)
1134{
1135 int ret;
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001136 struct configfs_dirent *subsys_sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001137 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001138 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001139
1140 /*
1141 * Pin the configfs filesystem. This means we can safely access
1142 * the root of the configfs filesystem.
1143 */
Al Viro2a152ad2012-03-17 16:53:29 -04001144 root = configfs_pin_fs();
1145 if (IS_ERR(root))
1146 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001147
1148 /*
1149 * Next, lock the root directory. We're going to check that the
1150 * subsystem is really registered, and so we need to lock out
1151 * configfs_[un]register_subsystem().
1152 */
Al Viro59551022016-01-22 15:40:57 -05001153 inode_lock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001154
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001155 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
Joel Becker631d1fe2007-06-18 18:06:09 -07001156 if (!subsys_sd) {
1157 ret = -ENOENT;
1158 goto out_unlock_fs;
1159 }
1160
1161 /* Ok, now we can trust subsys/s_item */
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001162 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
Joel Becker631d1fe2007-06-18 18:06:09 -07001163
Joel Becker631d1fe2007-06-18 18:06:09 -07001164out_unlock_fs:
Al Viro59551022016-01-22 15:40:57 -05001165 inode_unlock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001166
1167 /*
1168 * If we succeeded, the fs is pinned via other methods. If not,
1169 * we're done with it anyway. So release_fs() is always right.
1170 */
1171 configfs_release_fs();
1172
1173 return ret;
1174}
1175EXPORT_SYMBOL(configfs_depend_item);
1176
1177/*
1178 * Release the dependent linkage. This is much simpler than
1179 * configfs_depend_item() because we know that that the client driver is
1180 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1181 */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +01001182void configfs_undepend_item(struct config_item *target)
Joel Becker631d1fe2007-06-18 18:06:09 -07001183{
1184 struct configfs_dirent *sd;
1185
1186 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001187 * Since we can trust everything is pinned, we just need
1188 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001189 */
Louis Rilling420118c2009-01-28 19:18:33 +01001190 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001191
1192 sd = target->ci_dentry->d_fsdata;
1193 BUG_ON(sd->s_dependent_count < 1);
1194
1195 sd->s_dependent_count -= 1;
1196
1197 /*
1198 * After this unlock, we cannot trust the item to stay alive!
1199 * DO NOT REFERENCE item after this unlock.
1200 */
Louis Rilling420118c2009-01-28 19:18:33 +01001201 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001202}
1203EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001204
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001205/*
1206 * caller_subsys is a caller's subsystem not target's. This is used to
1207 * determine if we should lock root and check subsys or not. When we are
1208 * in the same subsystem as our target there is no need to do locking as
1209 * we know that subsys is valid and is not unregistered during this function
1210 * as we are called from callback of one of his children and VFS holds a lock
1211 * on some inode. Otherwise we have to lock our root to ensure that target's
1212 * subsystem it is not unregistered during this function.
1213 */
1214int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1215 struct config_item *target)
1216{
1217 struct configfs_subsystem *target_subsys;
1218 struct config_group *root, *parent;
1219 struct configfs_dirent *subsys_sd;
1220 int ret = -ENOENT;
1221
1222 /* Disallow this function for configfs root */
1223 if (configfs_is_root(target))
1224 return -EINVAL;
1225
1226 parent = target->ci_group;
1227 /*
1228 * This may happen when someone is trying to depend root
1229 * directory of some subsystem
1230 */
1231 if (configfs_is_root(&parent->cg_item)) {
1232 target_subsys = to_configfs_subsystem(to_config_group(target));
1233 root = parent;
1234 } else {
1235 target_subsys = parent->cg_subsys;
1236 /* Find a cofnigfs root as we may need it for locking */
1237 for (root = parent; !configfs_is_root(&root->cg_item);
1238 root = root->cg_item.ci_group)
1239 ;
1240 }
1241
1242 if (target_subsys != caller_subsys) {
1243 /*
1244 * We are in other configfs subsystem, so we have to do
1245 * additional locking to prevent other subsystem from being
1246 * unregistered
1247 */
Al Viro59551022016-01-22 15:40:57 -05001248 inode_lock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001249
1250 /*
1251 * As we are trying to depend item from other subsystem
1252 * we have to check if this subsystem is still registered
1253 */
1254 subsys_sd = configfs_find_subsys_dentry(
1255 root->cg_item.ci_dentry->d_fsdata,
1256 &target_subsys->su_group.cg_item);
1257 if (!subsys_sd)
1258 goto out_root_unlock;
1259 } else {
1260 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1261 }
1262
1263 /* Now we can execute core of depend item */
1264 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1265
1266 if (target_subsys != caller_subsys)
1267out_root_unlock:
1268 /*
1269 * We were called from subsystem other than our target so we
1270 * took some locks so now it's time to release them
1271 */
Al Viro59551022016-01-22 15:40:57 -05001272 inode_unlock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001273
1274 return ret;
1275}
1276EXPORT_SYMBOL(configfs_depend_item_unlocked);
1277
Al Viro18bb1db2011-07-26 01:41:39 -04001278static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001279{
Joel Beckera6795e92008-07-17 15:21:29 -07001280 int ret = 0;
1281 int module_got = 0;
1282 struct config_group *group = NULL;
1283 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001284 struct config_item *parent_item;
1285 struct configfs_subsystem *subsys;
1286 struct configfs_dirent *sd;
Bhumika Goyalaa293582017-10-16 17:18:40 +02001287 const struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001288 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Al Viro47320fb2019-08-25 19:56:13 -04001289 struct configfs_fragment *frag;
Joel Becker7063fbf2005-12-15 14:29:43 -08001290 char *name;
1291
Joel Becker7063fbf2005-12-15 14:29:43 -08001292 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001293
1294 /*
1295 * Fake invisibility if dir belongs to a group/default groups hierarchy
1296 * being attached
1297 */
1298 if (!configfs_dirent_is_ready(sd)) {
1299 ret = -ENOENT;
1300 goto out;
1301 }
1302
Joel Becker84efad12006-03-27 18:46:09 -08001303 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1304 ret = -EPERM;
1305 goto out;
1306 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001307
Al Viro47320fb2019-08-25 19:56:13 -04001308 frag = new_fragment();
1309 if (!frag) {
1310 ret = -ENOMEM;
1311 goto out;
1312 }
1313
Joel Becker84efad12006-03-27 18:46:09 -08001314 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001315 parent_item = configfs_get_config_item(dentry->d_parent);
1316 type = parent_item->ci_type;
1317 subsys = to_config_group(parent_item)->cg_subsys;
1318 BUG_ON(!subsys);
1319
1320 if (!type || !type->ct_group_ops ||
1321 (!type->ct_group_ops->make_group &&
1322 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001323 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1324 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001325 }
1326
Joel Becker70526b62008-06-17 15:34:32 -07001327 /*
1328 * The subsystem may belong to a different module than the item
1329 * being created. We don't want to safely pin the new item but
1330 * fail to pin the subsystem it sits under.
1331 */
1332 if (!subsys->su_group.cg_item.ci_type) {
1333 ret = -EINVAL;
1334 goto out_put;
1335 }
1336 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1337 if (!try_module_get(subsys_owner)) {
1338 ret = -EINVAL;
1339 goto out_put;
1340 }
1341
Joel Becker7063fbf2005-12-15 14:29:43 -08001342 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1343 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001344 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001345 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001346 }
Joel Becker84efad12006-03-27 18:46:09 -08001347
Joel Becker7063fbf2005-12-15 14:29:43 -08001348 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1349
Joel Beckere6bd07a2007-07-06 23:33:17 -07001350 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001351 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001352 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001353 if (!group)
1354 group = ERR_PTR(-ENOMEM);
1355 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001356 link_group(to_config_group(parent_item), group);
1357 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001358 } else
1359 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001360 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001361 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001362 if (!item)
1363 item = ERR_PTR(-ENOMEM);
1364 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001365 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001366 else
1367 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001368 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001369 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001370
1371 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001372 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001373 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001374 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001375 * There are no extra references to clean up.
1376 */
Joel Becker70526b62008-06-17 15:34:32 -07001377 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001378 }
1379
Joel Beckereed7a0d2006-04-11 21:37:20 -07001380 /*
1381 * link_obj() has been called (via link_group() for groups).
1382 * From here on out, errors must clean that up.
1383 */
1384
Joel Becker7063fbf2005-12-15 14:29:43 -08001385 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001386 if (!type) {
1387 ret = -EINVAL;
1388 goto out_unlink;
1389 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001390
Joel Becker70526b62008-06-17 15:34:32 -07001391 new_item_owner = type->ct_owner;
1392 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001393 ret = -EINVAL;
1394 goto out_unlink;
1395 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001396
Joel Beckereed7a0d2006-04-11 21:37:20 -07001397 /*
1398 * I hate doing it this way, but if there is
1399 * an error, module_put() probably should
1400 * happen after any cleanup.
1401 */
1402 module_got = 1;
1403
Louis Rilling6d8344b2008-06-16 19:01:02 +02001404 /*
1405 * Make racing rmdir() fail if it did not tag parent with
1406 * CONFIGFS_USET_DROPPING
1407 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1408 * fail and let rmdir() terminate correctly
1409 */
1410 spin_lock(&configfs_dirent_lock);
1411 /* This will make configfs_detach_prep() fail */
1412 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1413 spin_unlock(&configfs_dirent_lock);
1414
Joel Beckereed7a0d2006-04-11 21:37:20 -07001415 if (group)
Al Viro47320fb2019-08-25 19:56:13 -04001416 ret = configfs_attach_group(parent_item, item, dentry, frag);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001417 else
Al Viro47320fb2019-08-25 19:56:13 -04001418 ret = configfs_attach_item(parent_item, item, dentry, frag);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001419
Louis Rilling6d8344b2008-06-16 19:01:02 +02001420 spin_lock(&configfs_dirent_lock);
1421 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001422 if (!ret)
1423 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001424 spin_unlock(&configfs_dirent_lock);
1425
Joel Beckereed7a0d2006-04-11 21:37:20 -07001426out_unlink:
1427 if (ret) {
1428 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001429 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001430
1431 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001432 if (group)
1433 unlink_group(group);
1434 else
1435 unlink_obj(item);
1436 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001437
Joel Beckere6bd07a2007-07-06 23:33:17 -07001438 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001439
1440 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001441 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001442 }
1443
Joel Becker70526b62008-06-17 15:34:32 -07001444out_subsys_put:
1445 if (ret)
1446 module_put(subsys_owner);
1447
Joel Becker84efad12006-03-27 18:46:09 -08001448out_put:
1449 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001450 * link_obj()/link_group() took a reference from child->parent,
1451 * so the parent is safely pinned. We can drop our working
1452 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001453 */
1454 config_item_put(parent_item);
Al Viro47320fb2019-08-25 19:56:13 -04001455 put_fragment(frag);
Joel Becker84efad12006-03-27 18:46:09 -08001456
1457out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001458 return ret;
1459}
1460
1461static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1462{
1463 struct config_item *parent_item;
1464 struct config_item *item;
1465 struct configfs_subsystem *subsys;
1466 struct configfs_dirent *sd;
Al Virob0841ee2019-08-31 09:43:43 +02001467 struct configfs_fragment *frag;
Joel Becker70526b62008-06-17 15:34:32 -07001468 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001469 int ret;
1470
Joel Becker7063fbf2005-12-15 14:29:43 -08001471 sd = dentry->d_fsdata;
1472 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1473 return -EPERM;
1474
Joel Becker84efad12006-03-27 18:46:09 -08001475 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001476 parent_item = configfs_get_config_item(dentry->d_parent);
1477 subsys = to_config_group(parent_item)->cg_subsys;
1478 BUG_ON(!subsys);
1479
1480 if (!parent_item->ci_type) {
1481 config_item_put(parent_item);
1482 return -EINVAL;
1483 }
1484
Joel Becker70526b62008-06-17 15:34:32 -07001485 /* configfs_mkdir() shouldn't have allowed this */
1486 BUG_ON(!subsys->su_group.cg_item.ci_type);
1487 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1488
Louis Rilling9a73d782008-06-20 14:09:22 +02001489 /*
1490 * Ensure that no racing symlink() will make detach_prep() fail while
1491 * the new link is temporarily attached
1492 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001493 do {
Al Viro48f35b72016-04-12 00:37:59 -04001494 struct dentry *wait;
Louis Rilling6d8344b2008-06-16 19:01:02 +02001495
Louis Rillingde6bf182008-08-15 12:37:23 -07001496 mutex_lock(&configfs_symlink_mutex);
1497 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001498 /*
1499 * Here's where we check for dependents. We're protected by
1500 * configfs_dirent_lock.
1501 * If no dependent, atomically tag the item as dropping.
1502 */
1503 ret = sd->s_dependent_count ? -EBUSY : 0;
1504 if (!ret) {
Al Viro48f35b72016-04-12 00:37:59 -04001505 ret = configfs_detach_prep(dentry, &wait);
Louis Rilling420118c2009-01-28 19:18:33 +01001506 if (ret)
1507 configfs_detach_rollback(dentry);
1508 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001509 spin_unlock(&configfs_dirent_lock);
1510 mutex_unlock(&configfs_symlink_mutex);
1511
1512 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001513 if (ret != -EAGAIN) {
1514 config_item_put(parent_item);
1515 return ret;
1516 }
1517
1518 /* Wait until the racing operation terminates */
Al Viro48f35b72016-04-12 00:37:59 -04001519 inode_lock(d_inode(wait));
1520 inode_unlock(d_inode(wait));
1521 dput(wait);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001522 }
1523 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001524
Al Virob0841ee2019-08-31 09:43:43 +02001525 frag = sd->s_frag;
1526 if (down_write_killable(&frag->frag_sem)) {
1527 spin_lock(&configfs_dirent_lock);
1528 configfs_detach_rollback(dentry);
1529 spin_unlock(&configfs_dirent_lock);
1530 return -EINTR;
1531 }
1532 frag->frag_dead = true;
1533 up_write(&frag->frag_sem);
1534
Joel Becker84efad12006-03-27 18:46:09 -08001535 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001536 item = configfs_get_config_item(dentry);
1537
1538 /* Drop reference from above, item already holds one. */
1539 config_item_put(parent_item);
1540
1541 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001542 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001543
1544 if (sd->s_type & CONFIGFS_USET_DIR) {
1545 configfs_detach_group(item);
1546
Joel Beckere6bd07a2007-07-06 23:33:17 -07001547 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001548 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001549 unlink_group(to_config_group(item));
1550 } else {
1551 configfs_detach_item(item);
1552
Joel Beckere6bd07a2007-07-06 23:33:17 -07001553 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001554 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001555 unlink_obj(item);
1556 }
1557
1558 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001559 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001560
1561 /* Drop our reference from above */
1562 config_item_put(item);
1563
Joel Becker70526b62008-06-17 15:34:32 -07001564 module_put(dead_item_owner);
1565 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001566
1567 return 0;
1568}
1569
Arjan van de Ven754661f2007-02-12 00:55:38 -08001570const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001571 .mkdir = configfs_mkdir,
1572 .rmdir = configfs_rmdir,
1573 .symlink = configfs_symlink,
1574 .unlink = configfs_unlink,
1575 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001576 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001577};
1578
Al Viro81d44ed12012-03-17 16:13:25 -04001579const struct inode_operations configfs_root_inode_operations = {
1580 .lookup = configfs_lookup,
1581 .setattr = configfs_setattr,
1582};
1583
Joel Becker7063fbf2005-12-15 14:29:43 -08001584#if 0
1585int configfs_rename_dir(struct config_item * item, const char *new_name)
1586{
1587 int error = 0;
1588 struct dentry * new_dentry, * parent;
1589
1590 if (!strcmp(config_item_name(item), new_name))
1591 return -EINVAL;
1592
1593 if (!item->parent)
1594 return -EINVAL;
1595
1596 down_write(&configfs_rename_sem);
1597 parent = item->parent->dentry;
1598
Al Viro59551022016-01-22 15:40:57 -05001599 inode_lock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001600
1601 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1602 if (!IS_ERR(new_dentry)) {
David Howells2b0143b2015-03-17 22:25:59 +00001603 if (d_really_is_negative(new_dentry)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001604 error = config_item_set_name(item, "%s", new_name);
1605 if (!error) {
1606 d_add(new_dentry, NULL);
1607 d_move(item->dentry, new_dentry);
1608 }
1609 else
1610 d_delete(new_dentry);
1611 } else
1612 error = -EEXIST;
1613 dput(new_dentry);
1614 }
Al Viro59551022016-01-22 15:40:57 -05001615 inode_unlock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001616 up_write(&configfs_rename_sem);
1617
1618 return error;
1619}
1620#endif
1621
1622static int configfs_dir_open(struct inode *inode, struct file *file)
1623{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001624 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001625 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001626 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001627
Al Viro59551022016-01-22 15:40:57 -05001628 inode_lock(d_inode(dentry));
Louis Rilling2a109f22008-07-04 16:56:05 +02001629 /*
1630 * Fake invisibility if dir belongs to a group/default groups hierarchy
1631 * being attached
1632 */
1633 err = -ENOENT;
1634 if (configfs_dirent_is_ready(parent_sd)) {
Al Viro47320fb2019-08-25 19:56:13 -04001635 file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
Louis Rilling2a109f22008-07-04 16:56:05 +02001636 if (IS_ERR(file->private_data))
1637 err = PTR_ERR(file->private_data);
1638 else
1639 err = 0;
1640 }
Al Viro59551022016-01-22 15:40:57 -05001641 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001642
Louis Rilling2a109f22008-07-04 16:56:05 +02001643 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001644}
1645
1646static int configfs_dir_close(struct inode *inode, struct file *file)
1647{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001648 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001649 struct configfs_dirent * cursor = file->private_data;
1650
Al Viro59551022016-01-22 15:40:57 -05001651 inode_lock(d_inode(dentry));
Louis Rilling6f610762008-06-16 19:00:58 +02001652 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001653 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001654 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001655 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001656
1657 release_configfs_dirent(cursor);
1658
1659 return 0;
1660}
1661
1662/* Relationship between s_mode and the DT_xxx types */
1663static inline unsigned char dt_type(struct configfs_dirent *sd)
1664{
1665 return (sd->s_mode >> 12) & 15;
1666}
1667
Al Viro52018852013-05-16 01:28:34 -04001668static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001669{
Al Viro52018852013-05-16 01:28:34 -04001670 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001671 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001672 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001673 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001674 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001675 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001676
Al Viro52018852013-05-16 01:28:34 -04001677 if (!dir_emit_dots(file, ctx))
1678 return 0;
Al Viroa01b3002016-04-20 19:50:05 -04001679 spin_lock(&configfs_dirent_lock);
1680 if (ctx->pos == 2)
Al Viro52018852013-05-16 01:28:34 -04001681 list_move(q, &parent_sd->s_children);
Al Viro52018852013-05-16 01:28:34 -04001682 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1683 struct configfs_dirent *next;
1684 const char *name;
1685 int len;
1686 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001687
Al Viro52018852013-05-16 01:28:34 -04001688 next = list_entry(p, struct configfs_dirent, s_sibling);
1689 if (!next->s_element)
1690 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001691
Al Viro52018852013-05-16 01:28:34 -04001692 /*
1693 * We'll have a dentry and an inode for
1694 * PINNED items and for open attribute
1695 * files. We lock here to prevent a race
1696 * with configfs_d_iput() clearing
1697 * s_dentry before calling iput().
1698 *
1699 * Why do we go to the trouble? If
1700 * someone has an attribute file open,
1701 * the inode number should match until
1702 * they close it. Beyond that, we don't
1703 * care.
1704 */
Al Viro52018852013-05-16 01:28:34 -04001705 dentry = next->s_dentry;
1706 if (dentry)
David Howells2b0143b2015-03-17 22:25:59 +00001707 inode = d_inode(dentry);
Al Viro52018852013-05-16 01:28:34 -04001708 if (inode)
1709 ino = inode->i_ino;
1710 spin_unlock(&configfs_dirent_lock);
1711 if (!inode)
1712 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001713
Al Viroa01b3002016-04-20 19:50:05 -04001714 name = configfs_get_name(next);
1715 len = strlen(name);
1716
Al Viro52018852013-05-16 01:28:34 -04001717 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1718 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001719
Al Viro52018852013-05-16 01:28:34 -04001720 spin_lock(&configfs_dirent_lock);
1721 list_move(q, p);
Al Viro52018852013-05-16 01:28:34 -04001722 p = q;
1723 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001724 }
Al Viroa01b3002016-04-20 19:50:05 -04001725 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001726 return 0;
1727}
1728
Andrew Morton965c8e52012-12-17 15:59:39 -08001729static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001730{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001731 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001732
Andrew Morton965c8e52012-12-17 15:59:39 -08001733 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001734 case 1:
1735 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001736 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001737 case 0:
1738 if (offset >= 0)
1739 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001740 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001741 default:
Joel Becker7063fbf2005-12-15 14:29:43 -08001742 return -EINVAL;
1743 }
1744 if (offset != file->f_pos) {
1745 file->f_pos = offset;
1746 if (file->f_pos >= 2) {
1747 struct configfs_dirent *sd = dentry->d_fsdata;
1748 struct configfs_dirent *cursor = file->private_data;
1749 struct list_head *p;
1750 loff_t n = file->f_pos - 2;
1751
Louis Rilling6f610762008-06-16 19:00:58 +02001752 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001753 list_del(&cursor->s_sibling);
1754 p = sd->s_children.next;
1755 while (n && p != &sd->s_children) {
1756 struct configfs_dirent *next;
1757 next = list_entry(p, struct configfs_dirent,
1758 s_sibling);
1759 if (next->s_element)
1760 n--;
1761 p = p->next;
1762 }
1763 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001764 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001765 }
1766 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001767 return offset;
1768}
1769
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001770const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001771 .open = configfs_dir_open,
1772 .release = configfs_dir_close,
1773 .llseek = configfs_dir_lseek,
1774 .read = generic_read_dir,
Al Viroa01b3002016-04-20 19:50:05 -04001775 .iterate_shared = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001776};
1777
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001778/**
1779 * configfs_register_group - creates a parent-child relation between two groups
1780 * @parent_group: parent group
1781 * @group: child group
1782 *
1783 * link groups, creates dentry for the child and attaches it to the
1784 * parent dentry.
1785 *
1786 * Return: 0 on success, negative errno code on error
1787 */
1788int configfs_register_group(struct config_group *parent_group,
1789 struct config_group *group)
1790{
1791 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1792 struct dentry *parent;
Al Viro47320fb2019-08-25 19:56:13 -04001793 struct configfs_fragment *frag;
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001794 int ret;
1795
Al Viro47320fb2019-08-25 19:56:13 -04001796 frag = new_fragment();
1797 if (!frag)
1798 return -ENOMEM;
1799
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001800 mutex_lock(&subsys->su_mutex);
1801 link_group(parent_group, group);
1802 mutex_unlock(&subsys->su_mutex);
1803
1804 parent = parent_group->cg_item.ci_dentry;
1805
Al Viro59551022016-01-22 15:40:57 -05001806 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Al Viro47320fb2019-08-25 19:56:13 -04001807 ret = create_default_group(parent_group, group, frag);
YueHaibing35399f82019-05-05 11:03:12 +08001808 if (ret)
1809 goto err_out;
1810
1811 spin_lock(&configfs_dirent_lock);
1812 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1813 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001814 inode_unlock(d_inode(parent));
Al Viro47320fb2019-08-25 19:56:13 -04001815 put_fragment(frag);
YueHaibing35399f82019-05-05 11:03:12 +08001816 return 0;
1817err_out:
1818 inode_unlock(d_inode(parent));
1819 mutex_lock(&subsys->su_mutex);
1820 unlink_group(group);
1821 mutex_unlock(&subsys->su_mutex);
Al Viro47320fb2019-08-25 19:56:13 -04001822 put_fragment(frag);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001823 return ret;
1824}
1825EXPORT_SYMBOL(configfs_register_group);
1826
1827/**
1828 * configfs_unregister_group() - unregisters a child group from its parent
1829 * @group: parent group to be unregistered
1830 *
1831 * Undoes configfs_register_group()
1832 */
1833void configfs_unregister_group(struct config_group *group)
1834{
1835 struct configfs_subsystem *subsys = group->cg_subsys;
1836 struct dentry *dentry = group->cg_item.ci_dentry;
1837 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
Al Virob0841ee2019-08-31 09:43:43 +02001838 struct configfs_dirent *sd = dentry->d_fsdata;
1839 struct configfs_fragment *frag = sd->s_frag;
1840
1841 down_write(&frag->frag_sem);
1842 frag->frag_dead = true;
1843 up_write(&frag->frag_sem);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001844
Al Viro59551022016-01-22 15:40:57 -05001845 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001846 spin_lock(&configfs_dirent_lock);
1847 configfs_detach_prep(dentry, NULL);
1848 spin_unlock(&configfs_dirent_lock);
1849
1850 configfs_detach_group(&group->cg_item);
1851 d_inode(dentry)->i_flags |= S_DEAD;
1852 dont_mount(dentry);
Amir Goldstein6146e782019-05-26 17:34:09 +03001853 fsnotify_rmdir(d_inode(parent), dentry);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001854 d_delete(dentry);
Al Viro59551022016-01-22 15:40:57 -05001855 inode_unlock(d_inode(parent));
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001856
1857 dput(dentry);
1858
1859 mutex_lock(&subsys->su_mutex);
1860 unlink_group(group);
1861 mutex_unlock(&subsys->su_mutex);
1862}
1863EXPORT_SYMBOL(configfs_unregister_group);
1864
1865/**
1866 * configfs_register_default_group() - allocates and registers a child group
1867 * @parent_group: parent group
1868 * @name: child group name
1869 * @item_type: child item type description
1870 *
1871 * boilerplate to allocate and register a child group with its parent. We need
1872 * kzalloc'ed memory because child's default_group is initially empty.
1873 *
1874 * Return: allocated config group or ERR_PTR() on error
1875 */
1876struct config_group *
1877configfs_register_default_group(struct config_group *parent_group,
1878 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +02001879 const struct config_item_type *item_type)
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001880{
1881 int ret;
1882 struct config_group *group;
1883
1884 group = kzalloc(sizeof(*group), GFP_KERNEL);
1885 if (!group)
1886 return ERR_PTR(-ENOMEM);
1887 config_group_init_type_name(group, name, item_type);
1888
1889 ret = configfs_register_group(parent_group, group);
1890 if (ret) {
1891 kfree(group);
1892 return ERR_PTR(ret);
1893 }
1894 return group;
1895}
1896EXPORT_SYMBOL(configfs_register_default_group);
1897
1898/**
1899 * configfs_unregister_default_group() - unregisters and frees a child group
1900 * @group: the group to act on
1901 */
1902void configfs_unregister_default_group(struct config_group *group)
1903{
1904 configfs_unregister_group(group);
1905 kfree(group);
1906}
1907EXPORT_SYMBOL(configfs_unregister_default_group);
1908
Joel Becker7063fbf2005-12-15 14:29:43 -08001909int configfs_register_subsystem(struct configfs_subsystem *subsys)
1910{
1911 int err;
1912 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001913 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001914 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001915 struct configfs_dirent *sd;
Al Viro47320fb2019-08-25 19:56:13 -04001916 struct configfs_fragment *frag;
1917
1918 frag = new_fragment();
1919 if (!frag)
1920 return -ENOMEM;
Joel Becker7063fbf2005-12-15 14:29:43 -08001921
Al Viro2a152ad2012-03-17 16:53:29 -04001922 root = configfs_pin_fs();
Al Viro47320fb2019-08-25 19:56:13 -04001923 if (IS_ERR(root)) {
1924 put_fragment(frag);
Al Viro2a152ad2012-03-17 16:53:29 -04001925 return PTR_ERR(root);
Al Viro47320fb2019-08-25 19:56:13 -04001926 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001927
1928 if (!group->cg_item.ci_name)
1929 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1930
Al Virob7c177f2012-03-17 16:24:54 -04001931 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001932 link_group(to_config_group(sd->s_element), group);
1933
Al Viro59551022016-01-22 15:40:57 -05001934 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001935
Joel Becker7063fbf2005-12-15 14:29:43 -08001936 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001937 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001938 if (dentry) {
1939 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001940
Joel Beckerafdf04e2007-03-05 15:49:49 -08001941 err = configfs_attach_group(sd->s_element, &group->cg_item,
Al Viro47320fb2019-08-25 19:56:13 -04001942 dentry, frag);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001943 if (err) {
David Howells2b0143b2015-03-17 22:25:59 +00001944 BUG_ON(d_inode(dentry));
Joel Beckerdf7f9962011-02-22 01:09:49 -08001945 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001946 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001947 } else {
1948 spin_lock(&configfs_dirent_lock);
1949 configfs_dir_set_ready(dentry->d_fsdata);
1950 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001951 }
1952 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001953
Al Viro59551022016-01-22 15:40:57 -05001954 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001955
Joel Beckerafdf04e2007-03-05 15:49:49 -08001956 if (err) {
1957 unlink_group(group);
1958 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001959 }
Al Viro47320fb2019-08-25 19:56:13 -04001960 put_fragment(frag);
Joel Becker7063fbf2005-12-15 14:29:43 -08001961
1962 return err;
1963}
1964
1965void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1966{
1967 struct config_group *group = &subsys->su_group;
1968 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001969 struct dentry *root = dentry->d_sb->s_root;
Al Virob0841ee2019-08-31 09:43:43 +02001970 struct configfs_dirent *sd = dentry->d_fsdata;
1971 struct configfs_fragment *frag = sd->s_frag;
Joel Becker7063fbf2005-12-15 14:29:43 -08001972
Al Virob7c177f2012-03-17 16:24:54 -04001973 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001974 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001975 return;
1976 }
1977
Al Virob0841ee2019-08-31 09:43:43 +02001978 down_write(&frag->frag_sem);
1979 frag->frag_dead = true;
1980 up_write(&frag->frag_sem);
1981
Al Viro59551022016-01-22 15:40:57 -05001982 inode_lock_nested(d_inode(root),
Mark Fasheh55ed1602006-10-20 14:55:54 -07001983 I_MUTEX_PARENT);
Al Viro59551022016-01-22 15:40:57 -05001984 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001985 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001986 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001987 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001988 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001989 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001990 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001991 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001992 configfs_detach_group(&group->cg_item);
David Howells2b0143b2015-03-17 22:25:59 +00001993 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001994 dont_mount(dentry);
Amir Goldstein6146e782019-05-26 17:34:09 +03001995 fsnotify_rmdir(d_inode(root), dentry);
Al Viro59551022016-01-22 15:40:57 -05001996 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001997
1998 d_delete(dentry);
1999
Al Viro59551022016-01-22 15:40:57 -05002000 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08002001
2002 dput(dentry);
2003
2004 unlink_group(group);
2005 configfs_release_fs();
2006}
2007
2008EXPORT_SYMBOL(configfs_register_subsystem);
2009EXPORT_SYMBOL(configfs_unregister_subsystem);