blob: 79fc25aaa8cd5e8168d32a7ada8bb0ec40af4c1d [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
Al Viroc88b1e72015-01-29 00:17:57 -0500256static void init_dir(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800257{
258 inode->i_op = &configfs_dir_inode_operations;
259 inode->i_fop = &configfs_dir_operations;
260
261 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700262 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800263}
264
Al Viroc88b1e72015-01-29 00:17:57 -0500265static void configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800266{
267 inode->i_size = PAGE_SIZE;
268 inode->i_fop = &configfs_file_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800269}
270
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300271static void configfs_init_bin_file(struct inode *inode)
272{
273 inode->i_size = 0;
274 inode->i_fop = &configfs_bin_file_operations;
275}
276
Al Viroc88b1e72015-01-29 00:17:57 -0500277static void init_symlink(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800278{
279 inode->i_op = &configfs_symlink_inode_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800280}
281
Joel Becker7063fbf2005-12-15 14:29:43 -0800282/**
283 * configfs_create_dir - create a directory for an config_item.
284 * @item: config_itemwe're creating directory for.
285 * @dentry: config_item's dentry.
Louis Rilling2a109f22008-07-04 16:56:05 +0200286 *
287 * Note: user-created entries won't be allowed under this new directory
288 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800289 */
290
Al Viro47320fb2019-08-25 19:56:13 -0400291static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
292 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800293{
Al Viro1cf97d02015-01-29 00:20:49 -0500294 int error;
295 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
296 struct dentry *p = dentry->d_parent;
297
298 BUG_ON(!item);
299
300 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
301 if (unlikely(error))
302 return error;
303
304 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
Al Viro47320fb2019-08-25 19:56:13 -0400305 CONFIGFS_DIR | CONFIGFS_USET_CREATING,
306 frag);
Al Viro1cf97d02015-01-29 00:20:49 -0500307 if (unlikely(error))
308 return error;
309
310 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
311 error = configfs_create(dentry, mode, init_dir);
312 if (!error) {
David Howells2b0143b2015-03-17 22:25:59 +0000313 inc_nlink(d_inode(p));
Joel Becker7063fbf2005-12-15 14:29:43 -0800314 item->ci_dentry = dentry;
Al Viro1cf97d02015-01-29 00:20:49 -0500315 } else {
316 struct configfs_dirent *sd = dentry->d_fsdata;
317 if (sd) {
318 spin_lock(&configfs_dirent_lock);
319 list_del_init(&sd->s_sibling);
320 spin_unlock(&configfs_dirent_lock);
321 configfs_put(sd);
322 }
323 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800324 return error;
325}
326
Louis Rilling2a109f22008-07-04 16:56:05 +0200327/*
328 * Allow userspace to create new entries under a new directory created with
329 * configfs_create_dir(), and under all of its chidlren directories recursively.
330 * @sd configfs_dirent of the new directory to validate
331 *
332 * Caller must hold configfs_dirent_lock.
333 */
334static void configfs_dir_set_ready(struct configfs_dirent *sd)
335{
336 struct configfs_dirent *child_sd;
337
338 sd->s_type &= ~CONFIGFS_USET_CREATING;
339 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
340 if (child_sd->s_type & CONFIGFS_USET_CREATING)
341 configfs_dir_set_ready(child_sd);
342}
343
344/*
345 * Check that a directory does not belong to a directory hierarchy being
346 * attached and not validated yet.
347 * @sd configfs_dirent of the directory to check
348 *
349 * @return non-zero iff the directory was validated
350 *
351 * Note: takes configfs_dirent_lock, so the result may change from false to true
352 * in two consecutive calls, but never from true to false.
353 */
354int configfs_dirent_is_ready(struct configfs_dirent *sd)
355{
356 int ret;
357
358 spin_lock(&configfs_dirent_lock);
359 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
360 spin_unlock(&configfs_dirent_lock);
361
362 return ret;
363}
364
Joel Becker7063fbf2005-12-15 14:29:43 -0800365int configfs_create_link(struct configfs_symlink *sl,
366 struct dentry *parent,
367 struct dentry *dentry)
368{
369 int err = 0;
370 umode_t mode = S_IFLNK | S_IRWXUGO;
Al Viro47320fb2019-08-25 19:56:13 -0400371 struct configfs_dirent *p = parent->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -0800372
Al Viro47320fb2019-08-25 19:56:13 -0400373 err = configfs_make_dirent(p, dentry, sl, mode,
374 CONFIGFS_ITEM_LINK, p->s_frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800375 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800376 err = configfs_create(dentry, mode, init_symlink);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100377 if (err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800378 struct configfs_dirent *sd = dentry->d_fsdata;
379 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200380 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800381 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200382 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800383 configfs_put(sd);
384 }
385 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800386 }
387 return err;
388}
389
390static void remove_dir(struct dentry * d)
391{
392 struct dentry * parent = dget(d->d_parent);
393 struct configfs_dirent * sd;
394
395 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200396 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800397 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200398 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800399 configfs_put(sd);
David Howells2b0143b2015-03-17 22:25:59 +0000400 if (d_really_is_positive(d))
401 simple_rmdir(d_inode(parent),d);
Joel Becker7063fbf2005-12-15 14:29:43 -0800402
Al Viroa4555892014-10-21 20:11:25 -0400403 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800404
405 dput(parent);
406}
407
408/**
409 * configfs_remove_dir - remove an config_item's directory.
410 * @item: config_item we're removing.
411 *
412 * The only thing special about this is that we remove any files in
413 * the directory before we remove the directory, and we've inlined
414 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200415 *
416 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800417 */
418
419static void configfs_remove_dir(struct config_item * item)
420{
421 struct dentry * dentry = dget(item->ci_dentry);
422
423 if (!dentry)
424 return;
425
426 remove_dir(dentry);
427 /**
428 * Drop reference from dget() on entrance.
429 */
430 dput(dentry);
431}
432
433
434/* attaches attribute's configfs_dirent to the dentry corresponding to the
435 * attribute file
436 */
437static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
438{
439 struct configfs_attribute * attr = sd->s_element;
440 int error;
441
Junxiao Bi76ae2812013-11-21 14:31:56 -0800442 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800443 dentry->d_fsdata = configfs_get(sd);
444 sd->s_dentry = dentry;
Junxiao Bi76ae2812013-11-21 14:31:56 -0800445 spin_unlock(&configfs_dirent_lock);
446
Dave Hansence8d2cd2007-10-16 23:31:13 -0700447 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300448 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
449 configfs_init_bin_file :
450 configfs_init_file);
Al Viro5cf3b562016-03-07 14:25:46 -0500451 if (error)
Joel Becker3d0f89b2006-01-25 13:31:07 -0800452 configfs_put(sd);
Al Viro5cf3b562016-03-07 14:25:46 -0500453 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800454}
455
456static struct dentry * configfs_lookup(struct inode *dir,
457 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400458 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800459{
460 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
461 struct configfs_dirent * sd;
462 int found = 0;
Louis Rilling2a109f22008-07-04 16:56:05 +0200463 int err;
464
465 /*
466 * Fake invisibility if dir belongs to a group/default groups hierarchy
467 * being attached
468 *
469 * This forbids userspace to read/write attributes of items which may
470 * not complete their initialization, since the dentries of the
471 * attributes won't be instantiated.
472 */
473 err = -ENOENT;
474 if (!configfs_dirent_is_ready(parent_sd))
475 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800476
477 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
478 if (sd->s_type & CONFIGFS_NOT_PINNED) {
479 const unsigned char * name = configfs_get_name(sd);
480
481 if (strcmp(name, dentry->d_name.name))
482 continue;
483
484 found = 1;
485 err = configfs_attach_attr(sd, dentry);
486 break;
487 }
488 }
489
490 if (!found) {
491 /*
492 * If it doesn't exist and it isn't a NOT_PINNED item,
493 * it must be negative.
494 */
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100495 if (dentry->d_name.len > NAME_MAX)
496 return ERR_PTR(-ENAMETOOLONG);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100497 d_add(dentry, NULL);
498 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800499 }
500
Louis Rilling2a109f22008-07-04 16:56:05 +0200501out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800502 return ERR_PTR(err);
503}
504
505/*
506 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200507 * attributes and are removed by rmdir(). We recurse, setting
508 * CONFIGFS_USET_DROPPING on all children that are candidates for
509 * default detach.
510 * If there is an error, the caller will reset the flags via
511 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800512 */
Al Viro48f35b72016-04-12 00:37:59 -0400513static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
Joel Becker7063fbf2005-12-15 14:29:43 -0800514{
515 struct configfs_dirent *parent_sd = dentry->d_fsdata;
516 struct configfs_dirent *sd;
517 int ret;
518
Louis Rilling4768e9b2008-06-23 14:16:17 +0200519 /* Mark that we're trying to drop the group */
520 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
521
Joel Becker7063fbf2005-12-15 14:29:43 -0800522 ret = -EBUSY;
523 if (!list_empty(&parent_sd->s_links))
524 goto out;
525
526 ret = 0;
527 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200528 if (!sd->s_element ||
529 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800530 continue;
531 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200532 /* Abort if racing with mkdir() */
533 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
Al Viro48f35b72016-04-12 00:37:59 -0400534 if (wait)
535 *wait= dget(sd->s_dentry);
Louis Rilling6d8344b2008-06-16 19:01:02 +0200536 return -EAGAIN;
537 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800538
Joel Becker631d1fe2007-06-18 18:06:09 -0700539 /*
540 * Yup, recursive. If there's a problem, blame
541 * deep nesting of default_groups
542 */
Al Viro48f35b72016-04-12 00:37:59 -0400543 ret = configfs_detach_prep(sd->s_dentry, wait);
Joel Becker7063fbf2005-12-15 14:29:43 -0800544 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800545 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800546 } else
547 ret = -ENOTEMPTY;
548
549 break;
550 }
551
552out:
553 return ret;
554}
555
556/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200557 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800558 * set.
559 */
560static void configfs_detach_rollback(struct dentry *dentry)
561{
562 struct configfs_dirent *parent_sd = dentry->d_fsdata;
563 struct configfs_dirent *sd;
564
Louis Rilling4768e9b2008-06-23 14:16:17 +0200565 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
566
567 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
568 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800569 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800570}
571
572static void detach_attrs(struct config_item * item)
573{
574 struct dentry * dentry = dget(item->ci_dentry);
575 struct configfs_dirent * parent_sd;
576 struct configfs_dirent * sd, * tmp;
577
578 if (!dentry)
579 return;
580
581 pr_debug("configfs %s: dropping attrs for dir\n",
582 dentry->d_name.name);
583
584 parent_sd = dentry->d_fsdata;
585 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
586 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
587 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200588 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800589 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200590 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800591 configfs_drop_dentry(sd, dentry);
592 configfs_put(sd);
593 }
594
595 /**
596 * Drop reference from dget() on entrance.
597 */
598 dput(dentry);
599}
600
601static int populate_attrs(struct config_item *item)
602{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200603 const struct config_item_type *t = item->ci_type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800604 struct configfs_attribute *attr;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300605 struct configfs_bin_attribute *bin_attr;
Joel Becker7063fbf2005-12-15 14:29:43 -0800606 int error = 0;
607 int i;
608
609 if (!t)
610 return -EINVAL;
611 if (t->ct_attrs) {
612 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
613 if ((error = configfs_create_file(item, attr)))
614 break;
615 }
616 }
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300617 if (t->ct_bin_attrs) {
618 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
619 error = configfs_create_bin_file(item, bin_attr);
620 if (error)
621 break;
622 }
623 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800624
625 if (error)
626 detach_attrs(item);
627
628 return error;
629}
630
631static int configfs_attach_group(struct config_item *parent_item,
632 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400633 struct dentry *dentry,
634 struct configfs_fragment *frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800635static void configfs_detach_group(struct config_item *item);
636
637static void detach_groups(struct config_group *group)
638{
639 struct dentry * dentry = dget(group->cg_item.ci_dentry);
640 struct dentry *child;
641 struct configfs_dirent *parent_sd;
642 struct configfs_dirent *sd, *tmp;
643
644 if (!dentry)
645 return;
646
647 parent_sd = dentry->d_fsdata;
648 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
649 if (!sd->s_element ||
650 !(sd->s_type & CONFIGFS_USET_DEFAULT))
651 continue;
652
653 child = sd->s_dentry;
654
Al Viro59551022016-01-22 15:40:57 -0500655 inode_lock(d_inode(child));
Louis Rillingb3e76af2008-06-16 19:01:01 +0200656
Joel Becker7063fbf2005-12-15 14:29:43 -0800657 configfs_detach_group(sd->s_element);
David Howells2b0143b2015-03-17 22:25:59 +0000658 d_inode(child)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400659 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800660
Al Viro59551022016-01-22 15:40:57 -0500661 inode_unlock(d_inode(child));
Joel Becker7063fbf2005-12-15 14:29:43 -0800662
663 d_delete(child);
664 dput(child);
665 }
666
667 /**
668 * Drop reference from dget() on entrance.
669 */
670 dput(dentry);
671}
672
673/*
674 * This fakes mkdir(2) on a default_groups[] entry. It
675 * creates a dentry, attachs it, and then does fixup
676 * on the sd->s_type.
677 *
678 * We could, perhaps, tweak our parent's ->mkdir for a minute and
679 * try using vfs_mkdir. Just a thought.
680 */
681static int create_default_group(struct config_group *parent_group,
Al Viro47320fb2019-08-25 19:56:13 -0400682 struct config_group *group,
683 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800684{
685 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800686 struct configfs_dirent *sd;
687 /* We trust the caller holds a reference to parent */
688 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
689
690 if (!group->cg_item.ci_name)
691 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800692
693 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400694 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800695 if (child) {
696 d_add(child, NULL);
697
698 ret = configfs_attach_group(&parent_group->cg_item,
Al Viro47320fb2019-08-25 19:56:13 -0400699 &group->cg_item, child, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800700 if (!ret) {
701 sd = child->d_fsdata;
702 sd->s_type |= CONFIGFS_USET_DEFAULT;
703 } else {
David Howells2b0143b2015-03-17 22:25:59 +0000704 BUG_ON(d_inode(child));
Joel Beckerdf7f9962011-02-22 01:09:49 -0800705 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800706 dput(child);
707 }
708 }
709
710 return ret;
711}
712
Al Viro47320fb2019-08-25 19:56:13 -0400713static int populate_groups(struct config_group *group,
714 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800715{
716 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800717 int ret = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800718
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100719 list_for_each_entry(new_group, &group->default_groups, group_entry) {
Al Viro47320fb2019-08-25 19:56:13 -0400720 ret = create_default_group(group, new_group, frag);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100721 if (ret) {
722 detach_groups(group);
723 break;
Joel Becker7063fbf2005-12-15 14:29:43 -0800724 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800725 }
726
Joel Becker7063fbf2005-12-15 14:29:43 -0800727 return ret;
728}
729
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100730void configfs_remove_default_groups(struct config_group *group)
731{
732 struct config_group *g, *n;
733
734 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
735 list_del(&g->group_entry);
736 config_item_put(&g->cg_item);
737 }
738}
739EXPORT_SYMBOL(configfs_remove_default_groups);
740
Joel Becker7063fbf2005-12-15 14:29:43 -0800741/*
742 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700743 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800744 */
745
746static void unlink_obj(struct config_item *item)
747{
748 struct config_group *group;
749
750 group = item->ci_group;
751 if (group) {
752 list_del_init(&item->ci_entry);
753
754 item->ci_group = NULL;
755 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700756
757 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800758 config_item_put(item);
759
Joel Beckereed7a0d2006-04-11 21:37:20 -0700760 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800761 config_group_put(group);
762 }
763}
764
765static void link_obj(struct config_item *parent_item, struct config_item *item)
766{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700767 /*
768 * Parent seems redundant with group, but it makes certain
769 * traversals much nicer.
770 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800771 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700772
773 /*
774 * We hold a reference on the parent for the child's ci_parent
775 * link.
776 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800777 item->ci_group = config_group_get(to_config_group(parent_item));
778 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
779
Joel Beckereed7a0d2006-04-11 21:37:20 -0700780 /*
781 * We hold a reference on the child for ci_entry on the parent's
782 * cg_children
783 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800784 config_item_get(item);
785}
786
787static void unlink_group(struct config_group *group)
788{
Joel Becker7063fbf2005-12-15 14:29:43 -0800789 struct config_group *new_group;
790
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100791 list_for_each_entry(new_group, &group->default_groups, group_entry)
792 unlink_group(new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800793
794 group->cg_subsys = NULL;
795 unlink_obj(&group->cg_item);
796}
797
798static void link_group(struct config_group *parent_group, struct config_group *group)
799{
Joel Becker7063fbf2005-12-15 14:29:43 -0800800 struct config_group *new_group;
801 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
802
803 link_obj(&parent_group->cg_item, &group->cg_item);
804
805 if (parent_group->cg_subsys)
806 subsys = parent_group->cg_subsys;
807 else if (configfs_is_root(&parent_group->cg_item))
808 subsys = to_configfs_subsystem(group);
809 else
810 BUG();
811 group->cg_subsys = subsys;
812
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100813 list_for_each_entry(new_group, &group->default_groups, group_entry)
814 link_group(group, new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800815}
816
817/*
818 * The goal is that configfs_attach_item() (and
819 * configfs_attach_group()) can be called from either the VFS or this
820 * module. That is, they assume that the items have been created,
821 * the dentry allocated, and the dcache is all ready to go.
822 *
823 * If they fail, they must clean up after themselves as if they
824 * had never been called. The caller (VFS or local function) will
825 * handle cleaning up the dcache bits.
826 *
827 * configfs_detach_group() and configfs_detach_item() behave similarly on
828 * the way out. They assume that the proper semaphores are held, they
829 * clean up the configfs items, and they expect their callers will
830 * handle the dcache bits.
831 */
832static int configfs_attach_item(struct config_item *parent_item,
833 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400834 struct dentry *dentry,
835 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800836{
837 int ret;
838
Al Viro47320fb2019-08-25 19:56:13 -0400839 ret = configfs_create_dir(item, dentry, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800840 if (!ret) {
841 ret = populate_attrs(item);
842 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200843 /*
844 * We are going to remove an inode and its dentry but
845 * the VFS may already have hit and used them. Thus,
846 * we must lock them as rmdir() would.
847 */
Al Viro59551022016-01-22 15:40:57 -0500848 inode_lock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800849 configfs_remove_dir(item);
David Howells2b0143b2015-03-17 22:25:59 +0000850 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400851 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -0500852 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800853 d_delete(dentry);
854 }
855 }
856
857 return ret;
858}
859
Louis Rilling2e2ce172008-07-04 16:56:06 +0200860/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800861static void configfs_detach_item(struct config_item *item)
862{
863 detach_attrs(item);
864 configfs_remove_dir(item);
865}
866
867static int configfs_attach_group(struct config_item *parent_item,
868 struct config_item *item,
Al Viro47320fb2019-08-25 19:56:13 -0400869 struct dentry *dentry,
870 struct configfs_fragment *frag)
Joel Becker7063fbf2005-12-15 14:29:43 -0800871{
872 int ret;
873 struct configfs_dirent *sd;
874
Al Viro47320fb2019-08-25 19:56:13 -0400875 ret = configfs_attach_item(parent_item, item, dentry, frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800876 if (!ret) {
877 sd = dentry->d_fsdata;
878 sd->s_type |= CONFIGFS_USET_DIR;
879
Louis Rilling2e2ce172008-07-04 16:56:06 +0200880 /*
881 * FYI, we're faking mkdir in populate_groups()
882 * We must lock the group's inode to avoid races with the VFS
883 * which can already hit the inode and try to add/remove entries
884 * under it.
885 *
886 * We must also lock the inode to remove it safely in case of
887 * error, as rmdir() would.
888 */
Al Viro59551022016-01-22 15:40:57 -0500889 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100890 configfs_adjust_dir_dirent_depth_before_populate(sd);
Al Viro47320fb2019-08-25 19:56:13 -0400891 ret = populate_groups(to_config_group(item), frag);
Joel Becker7063fbf2005-12-15 14:29:43 -0800892 if (ret) {
893 configfs_detach_item(item);
David Howells2b0143b2015-03-17 22:25:59 +0000894 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400895 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800896 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100897 configfs_adjust_dir_dirent_depth_after_populate(sd);
Al Viro59551022016-01-22 15:40:57 -0500898 inode_unlock(d_inode(dentry));
Louis Rilling2e2ce172008-07-04 16:56:06 +0200899 if (ret)
900 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800901 }
902
903 return ret;
904}
905
Louis Rilling2e2ce172008-07-04 16:56:06 +0200906/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800907static void configfs_detach_group(struct config_item *item)
908{
909 detach_groups(to_config_group(item));
910 configfs_detach_item(item);
911}
912
913/*
Joel Becker299894c2006-10-06 17:33:23 -0700914 * After the item has been detached from the filesystem view, we are
915 * ready to tear it out of the hierarchy. Notify the client before
916 * we do that so they can perform any cleanup that requires
917 * navigating the hierarchy. A client does not need to provide this
918 * callback. The subsystem semaphore MUST be held by the caller, and
919 * references must be valid for both items. It also assumes the
920 * caller has validated ci_type.
921 */
922static void client_disconnect_notify(struct config_item *parent_item,
923 struct config_item *item)
924{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200925 const struct config_item_type *type;
Joel Becker299894c2006-10-06 17:33:23 -0700926
927 type = parent_item->ci_type;
928 BUG_ON(!type);
929
930 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
931 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
932 item);
933}
934
935/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800936 * Drop the initial reference from make_item()/make_group()
937 * This function assumes that reference is held on item
938 * and that item holds a valid reference to the parent. Also, it
939 * assumes the caller has validated ci_type.
940 */
941static void client_drop_item(struct config_item *parent_item,
942 struct config_item *item)
943{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200944 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800945
946 type = parent_item->ci_type;
947 BUG_ON(!type);
948
Joel Beckereed7a0d2006-04-11 21:37:20 -0700949 /*
950 * If ->drop_item() exists, it is responsible for the
951 * config_item_put().
952 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800953 if (type->ct_group_ops && type->ct_group_ops->drop_item)
954 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700955 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800956 else
957 config_item_put(item);
958}
959
Joel Becker631d1fe2007-06-18 18:06:09 -0700960#ifdef DEBUG
961static void configfs_dump_one(struct configfs_dirent *sd, int level)
962{
Fabian Frederickc6686932014-06-04 16:05:58 -0700963 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700964
Fabian Frederickc6686932014-06-04 16:05:58 -0700965#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700966 type_print(CONFIGFS_ROOT);
967 type_print(CONFIGFS_DIR);
968 type_print(CONFIGFS_ITEM_ATTR);
969 type_print(CONFIGFS_ITEM_LINK);
970 type_print(CONFIGFS_USET_DIR);
971 type_print(CONFIGFS_USET_DEFAULT);
972 type_print(CONFIGFS_USET_DROPPING);
973#undef type_print
974}
975
976static int configfs_dump(struct configfs_dirent *sd, int level)
977{
978 struct configfs_dirent *child_sd;
979 int ret = 0;
980
981 configfs_dump_one(sd, level);
982
983 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
984 return 0;
985
986 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
987 ret = configfs_dump(child_sd, level + 2);
988 if (ret)
989 break;
990 }
991
992 return ret;
993}
994#endif
995
996
997/*
998 * configfs_depend_item() and configfs_undepend_item()
999 *
1000 * WARNING: Do not call these from a configfs callback!
1001 *
1002 * This describes these functions and their helpers.
1003 *
1004 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001005 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -07001006 * it. The idea is to give client modules as simple an interface as
1007 * possible. When a system asks them to depend on an item, they just
1008 * call configfs_depend_item(). If the item is live and the client
1009 * driver is in good shape, we'll happily do the work for them.
1010 *
1011 * Why is the locking complex? Because configfs uses the VFS to handle
1012 * all locking, but this function is called outside the normal
1013 * VFS->configfs path. So it must take VFS locks to prevent the
1014 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1015 * why you can't call these functions underneath configfs callbacks.
1016 *
1017 * Note, btw, that this can be called at *any* time, even when a configfs
1018 * subsystem isn't registered, or when configfs is loading or unloading.
1019 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +01001020 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1021 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -07001022 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +01001023 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1024 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -07001025 */
1026
1027/*
1028 * configfs_depend_prep()
1029 *
1030 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1031 * attributes. This is similar but not the same to configfs_detach_prep().
1032 * Note that configfs_detach_prep() expects the parent to be locked when it
1033 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1034 * do that so we can unlock it if we find nothing.
1035 *
1036 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001037 * our object.
1038 * We deliberately ignore items tagged as dropping since they are virtually
1039 * dead, as well as items in the middle of attachment since they virtually
1040 * do not exist yet. This completes the locking out of racing mkdir() and
1041 * rmdir().
1042 * Note: subdirectories in the middle of attachment start with s_type =
1043 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1044 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1045 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001046 *
Louis Rilling420118c2009-01-28 19:18:33 +01001047 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001048 *
1049 * This adds a requirement that all config_items be unique!
1050 *
Louis Rilling420118c2009-01-28 19:18:33 +01001051 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001052 * much on the stack, though, so folks that need this function - be careful
1053 * about your stack! Patches will be accepted to make it iterative.
1054 */
1055static int configfs_depend_prep(struct dentry *origin,
1056 struct config_item *target)
1057{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001058 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001059 int ret = 0;
1060
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001061 BUG_ON(!origin || !origin->d_fsdata);
1062 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001063
Joel Becker631d1fe2007-06-18 18:06:09 -07001064 if (sd->s_element == target) /* Boo-yah */
1065 goto out;
1066
1067 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001068 if ((child_sd->s_type & CONFIGFS_DIR) &&
1069 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1070 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001071 ret = configfs_depend_prep(child_sd->s_dentry,
1072 target);
1073 if (!ret)
1074 goto out; /* Child path boo-yah */
1075 }
1076 }
1077
1078 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001079 ret = -ENOENT;
1080
1081out:
1082 return ret;
1083}
1084
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001085static int configfs_do_depend_item(struct dentry *subsys_dentry,
1086 struct config_item *target)
1087{
1088 struct configfs_dirent *p;
1089 int ret;
1090
1091 spin_lock(&configfs_dirent_lock);
1092 /* Scan the tree, return 0 if found */
1093 ret = configfs_depend_prep(subsys_dentry, target);
1094 if (ret)
1095 goto out_unlock_dirent_lock;
1096
1097 /*
1098 * We are sure that the item is not about to be removed by rmdir(), and
1099 * not in the middle of attachment by mkdir().
1100 */
1101 p = target->ci_dentry->d_fsdata;
1102 p->s_dependent_count += 1;
1103
1104out_unlock_dirent_lock:
1105 spin_unlock(&configfs_dirent_lock);
1106
1107 return ret;
1108}
1109
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001110static inline struct configfs_dirent *
1111configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1112 struct config_item *subsys_item)
1113{
1114 struct configfs_dirent *p;
1115 struct configfs_dirent *ret = NULL;
1116
1117 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1118 if (p->s_type & CONFIGFS_DIR &&
1119 p->s_element == subsys_item) {
1120 ret = p;
1121 break;
1122 }
1123 }
1124
1125 return ret;
1126}
1127
1128
Joel Becker631d1fe2007-06-18 18:06:09 -07001129int configfs_depend_item(struct configfs_subsystem *subsys,
1130 struct config_item *target)
1131{
1132 int ret;
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001133 struct configfs_dirent *subsys_sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001134 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001135 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001136
1137 /*
1138 * Pin the configfs filesystem. This means we can safely access
1139 * the root of the configfs filesystem.
1140 */
Al Viro2a152ad2012-03-17 16:53:29 -04001141 root = configfs_pin_fs();
1142 if (IS_ERR(root))
1143 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001144
1145 /*
1146 * Next, lock the root directory. We're going to check that the
1147 * subsystem is really registered, and so we need to lock out
1148 * configfs_[un]register_subsystem().
1149 */
Al Viro59551022016-01-22 15:40:57 -05001150 inode_lock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001151
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001152 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
Joel Becker631d1fe2007-06-18 18:06:09 -07001153 if (!subsys_sd) {
1154 ret = -ENOENT;
1155 goto out_unlock_fs;
1156 }
1157
1158 /* Ok, now we can trust subsys/s_item */
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001159 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
Joel Becker631d1fe2007-06-18 18:06:09 -07001160
Joel Becker631d1fe2007-06-18 18:06:09 -07001161out_unlock_fs:
Al Viro59551022016-01-22 15:40:57 -05001162 inode_unlock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001163
1164 /*
1165 * If we succeeded, the fs is pinned via other methods. If not,
1166 * we're done with it anyway. So release_fs() is always right.
1167 */
1168 configfs_release_fs();
1169
1170 return ret;
1171}
1172EXPORT_SYMBOL(configfs_depend_item);
1173
1174/*
1175 * Release the dependent linkage. This is much simpler than
1176 * configfs_depend_item() because we know that that the client driver is
1177 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1178 */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +01001179void configfs_undepend_item(struct config_item *target)
Joel Becker631d1fe2007-06-18 18:06:09 -07001180{
1181 struct configfs_dirent *sd;
1182
1183 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001184 * Since we can trust everything is pinned, we just need
1185 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001186 */
Louis Rilling420118c2009-01-28 19:18:33 +01001187 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001188
1189 sd = target->ci_dentry->d_fsdata;
1190 BUG_ON(sd->s_dependent_count < 1);
1191
1192 sd->s_dependent_count -= 1;
1193
1194 /*
1195 * After this unlock, we cannot trust the item to stay alive!
1196 * DO NOT REFERENCE item after this unlock.
1197 */
Louis Rilling420118c2009-01-28 19:18:33 +01001198 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001199}
1200EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001201
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001202/*
1203 * caller_subsys is a caller's subsystem not target's. This is used to
1204 * determine if we should lock root and check subsys or not. When we are
1205 * in the same subsystem as our target there is no need to do locking as
1206 * we know that subsys is valid and is not unregistered during this function
1207 * as we are called from callback of one of his children and VFS holds a lock
1208 * on some inode. Otherwise we have to lock our root to ensure that target's
1209 * subsystem it is not unregistered during this function.
1210 */
1211int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1212 struct config_item *target)
1213{
1214 struct configfs_subsystem *target_subsys;
1215 struct config_group *root, *parent;
1216 struct configfs_dirent *subsys_sd;
1217 int ret = -ENOENT;
1218
1219 /* Disallow this function for configfs root */
1220 if (configfs_is_root(target))
1221 return -EINVAL;
1222
1223 parent = target->ci_group;
1224 /*
1225 * This may happen when someone is trying to depend root
1226 * directory of some subsystem
1227 */
1228 if (configfs_is_root(&parent->cg_item)) {
1229 target_subsys = to_configfs_subsystem(to_config_group(target));
1230 root = parent;
1231 } else {
1232 target_subsys = parent->cg_subsys;
1233 /* Find a cofnigfs root as we may need it for locking */
1234 for (root = parent; !configfs_is_root(&root->cg_item);
1235 root = root->cg_item.ci_group)
1236 ;
1237 }
1238
1239 if (target_subsys != caller_subsys) {
1240 /*
1241 * We are in other configfs subsystem, so we have to do
1242 * additional locking to prevent other subsystem from being
1243 * unregistered
1244 */
Al Viro59551022016-01-22 15:40:57 -05001245 inode_lock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001246
1247 /*
1248 * As we are trying to depend item from other subsystem
1249 * we have to check if this subsystem is still registered
1250 */
1251 subsys_sd = configfs_find_subsys_dentry(
1252 root->cg_item.ci_dentry->d_fsdata,
1253 &target_subsys->su_group.cg_item);
1254 if (!subsys_sd)
1255 goto out_root_unlock;
1256 } else {
1257 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1258 }
1259
1260 /* Now we can execute core of depend item */
1261 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1262
1263 if (target_subsys != caller_subsys)
1264out_root_unlock:
1265 /*
1266 * We were called from subsystem other than our target so we
1267 * took some locks so now it's time to release them
1268 */
Al Viro59551022016-01-22 15:40:57 -05001269 inode_unlock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001270
1271 return ret;
1272}
1273EXPORT_SYMBOL(configfs_depend_item_unlocked);
1274
Al Viro18bb1db2011-07-26 01:41:39 -04001275static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001276{
Joel Beckera6795e92008-07-17 15:21:29 -07001277 int ret = 0;
1278 int module_got = 0;
1279 struct config_group *group = NULL;
1280 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001281 struct config_item *parent_item;
1282 struct configfs_subsystem *subsys;
1283 struct configfs_dirent *sd;
Bhumika Goyalaa293582017-10-16 17:18:40 +02001284 const struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001285 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Al Viro47320fb2019-08-25 19:56:13 -04001286 struct configfs_fragment *frag;
Joel Becker7063fbf2005-12-15 14:29:43 -08001287 char *name;
1288
Joel Becker7063fbf2005-12-15 14:29:43 -08001289 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001290
1291 /*
1292 * Fake invisibility if dir belongs to a group/default groups hierarchy
1293 * being attached
1294 */
1295 if (!configfs_dirent_is_ready(sd)) {
1296 ret = -ENOENT;
1297 goto out;
1298 }
1299
Joel Becker84efad12006-03-27 18:46:09 -08001300 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1301 ret = -EPERM;
1302 goto out;
1303 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001304
Al Viro47320fb2019-08-25 19:56:13 -04001305 frag = new_fragment();
1306 if (!frag) {
1307 ret = -ENOMEM;
1308 goto out;
1309 }
1310
Joel Becker84efad12006-03-27 18:46:09 -08001311 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001312 parent_item = configfs_get_config_item(dentry->d_parent);
1313 type = parent_item->ci_type;
1314 subsys = to_config_group(parent_item)->cg_subsys;
1315 BUG_ON(!subsys);
1316
1317 if (!type || !type->ct_group_ops ||
1318 (!type->ct_group_ops->make_group &&
1319 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001320 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1321 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001322 }
1323
Joel Becker70526b62008-06-17 15:34:32 -07001324 /*
1325 * The subsystem may belong to a different module than the item
1326 * being created. We don't want to safely pin the new item but
1327 * fail to pin the subsystem it sits under.
1328 */
1329 if (!subsys->su_group.cg_item.ci_type) {
1330 ret = -EINVAL;
1331 goto out_put;
1332 }
1333 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1334 if (!try_module_get(subsys_owner)) {
1335 ret = -EINVAL;
1336 goto out_put;
1337 }
1338
Joel Becker7063fbf2005-12-15 14:29:43 -08001339 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1340 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001341 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001342 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001343 }
Joel Becker84efad12006-03-27 18:46:09 -08001344
Joel Becker7063fbf2005-12-15 14:29:43 -08001345 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1346
Joel Beckere6bd07a2007-07-06 23:33:17 -07001347 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001348 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001349 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001350 if (!group)
1351 group = ERR_PTR(-ENOMEM);
1352 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001353 link_group(to_config_group(parent_item), group);
1354 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001355 } else
1356 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001357 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001358 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001359 if (!item)
1360 item = ERR_PTR(-ENOMEM);
1361 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001362 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001363 else
1364 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001365 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001366 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001367
1368 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001369 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001370 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001371 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001372 * There are no extra references to clean up.
1373 */
Joel Becker70526b62008-06-17 15:34:32 -07001374 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001375 }
1376
Joel Beckereed7a0d2006-04-11 21:37:20 -07001377 /*
1378 * link_obj() has been called (via link_group() for groups).
1379 * From here on out, errors must clean that up.
1380 */
1381
Joel Becker7063fbf2005-12-15 14:29:43 -08001382 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001383 if (!type) {
1384 ret = -EINVAL;
1385 goto out_unlink;
1386 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001387
Joel Becker70526b62008-06-17 15:34:32 -07001388 new_item_owner = type->ct_owner;
1389 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001390 ret = -EINVAL;
1391 goto out_unlink;
1392 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001393
Joel Beckereed7a0d2006-04-11 21:37:20 -07001394 /*
1395 * I hate doing it this way, but if there is
1396 * an error, module_put() probably should
1397 * happen after any cleanup.
1398 */
1399 module_got = 1;
1400
Louis Rilling6d8344b2008-06-16 19:01:02 +02001401 /*
1402 * Make racing rmdir() fail if it did not tag parent with
1403 * CONFIGFS_USET_DROPPING
1404 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1405 * fail and let rmdir() terminate correctly
1406 */
1407 spin_lock(&configfs_dirent_lock);
1408 /* This will make configfs_detach_prep() fail */
1409 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1410 spin_unlock(&configfs_dirent_lock);
1411
Joel Beckereed7a0d2006-04-11 21:37:20 -07001412 if (group)
Al Viro47320fb2019-08-25 19:56:13 -04001413 ret = configfs_attach_group(parent_item, item, dentry, frag);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001414 else
Al Viro47320fb2019-08-25 19:56:13 -04001415 ret = configfs_attach_item(parent_item, item, dentry, frag);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001416
Louis Rilling6d8344b2008-06-16 19:01:02 +02001417 spin_lock(&configfs_dirent_lock);
1418 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001419 if (!ret)
1420 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001421 spin_unlock(&configfs_dirent_lock);
1422
Joel Beckereed7a0d2006-04-11 21:37:20 -07001423out_unlink:
1424 if (ret) {
1425 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001426 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001427
1428 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001429 if (group)
1430 unlink_group(group);
1431 else
1432 unlink_obj(item);
1433 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001434
Joel Beckere6bd07a2007-07-06 23:33:17 -07001435 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001436
1437 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001438 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001439 }
1440
Joel Becker70526b62008-06-17 15:34:32 -07001441out_subsys_put:
1442 if (ret)
1443 module_put(subsys_owner);
1444
Joel Becker84efad12006-03-27 18:46:09 -08001445out_put:
1446 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001447 * link_obj()/link_group() took a reference from child->parent,
1448 * so the parent is safely pinned. We can drop our working
1449 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001450 */
1451 config_item_put(parent_item);
Al Viro47320fb2019-08-25 19:56:13 -04001452 put_fragment(frag);
Joel Becker84efad12006-03-27 18:46:09 -08001453
1454out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001455 return ret;
1456}
1457
1458static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1459{
1460 struct config_item *parent_item;
1461 struct config_item *item;
1462 struct configfs_subsystem *subsys;
1463 struct configfs_dirent *sd;
Al Virob0841ee2019-08-31 09:43:43 +02001464 struct configfs_fragment *frag;
Joel Becker70526b62008-06-17 15:34:32 -07001465 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001466 int ret;
1467
Joel Becker7063fbf2005-12-15 14:29:43 -08001468 sd = dentry->d_fsdata;
1469 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1470 return -EPERM;
1471
Joel Becker84efad12006-03-27 18:46:09 -08001472 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001473 parent_item = configfs_get_config_item(dentry->d_parent);
1474 subsys = to_config_group(parent_item)->cg_subsys;
1475 BUG_ON(!subsys);
1476
1477 if (!parent_item->ci_type) {
1478 config_item_put(parent_item);
1479 return -EINVAL;
1480 }
1481
Joel Becker70526b62008-06-17 15:34:32 -07001482 /* configfs_mkdir() shouldn't have allowed this */
1483 BUG_ON(!subsys->su_group.cg_item.ci_type);
1484 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1485
Louis Rilling9a73d782008-06-20 14:09:22 +02001486 /*
1487 * Ensure that no racing symlink() will make detach_prep() fail while
1488 * the new link is temporarily attached
1489 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001490 do {
Al Viro48f35b72016-04-12 00:37:59 -04001491 struct dentry *wait;
Louis Rilling6d8344b2008-06-16 19:01:02 +02001492
Louis Rillingde6bf182008-08-15 12:37:23 -07001493 mutex_lock(&configfs_symlink_mutex);
1494 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001495 /*
1496 * Here's where we check for dependents. We're protected by
1497 * configfs_dirent_lock.
1498 * If no dependent, atomically tag the item as dropping.
1499 */
1500 ret = sd->s_dependent_count ? -EBUSY : 0;
1501 if (!ret) {
Al Viro48f35b72016-04-12 00:37:59 -04001502 ret = configfs_detach_prep(dentry, &wait);
Louis Rilling420118c2009-01-28 19:18:33 +01001503 if (ret)
1504 configfs_detach_rollback(dentry);
1505 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001506 spin_unlock(&configfs_dirent_lock);
1507 mutex_unlock(&configfs_symlink_mutex);
1508
1509 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001510 if (ret != -EAGAIN) {
1511 config_item_put(parent_item);
1512 return ret;
1513 }
1514
1515 /* Wait until the racing operation terminates */
Al Viro48f35b72016-04-12 00:37:59 -04001516 inode_lock(d_inode(wait));
1517 inode_unlock(d_inode(wait));
1518 dput(wait);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001519 }
1520 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001521
Al Virob0841ee2019-08-31 09:43:43 +02001522 frag = sd->s_frag;
1523 if (down_write_killable(&frag->frag_sem)) {
1524 spin_lock(&configfs_dirent_lock);
1525 configfs_detach_rollback(dentry);
1526 spin_unlock(&configfs_dirent_lock);
1527 return -EINTR;
1528 }
1529 frag->frag_dead = true;
1530 up_write(&frag->frag_sem);
1531
Joel Becker84efad12006-03-27 18:46:09 -08001532 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001533 item = configfs_get_config_item(dentry);
1534
1535 /* Drop reference from above, item already holds one. */
1536 config_item_put(parent_item);
1537
1538 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001539 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001540
1541 if (sd->s_type & CONFIGFS_USET_DIR) {
1542 configfs_detach_group(item);
1543
Joel Beckere6bd07a2007-07-06 23:33:17 -07001544 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001545 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001546 unlink_group(to_config_group(item));
1547 } else {
1548 configfs_detach_item(item);
1549
Joel Beckere6bd07a2007-07-06 23:33:17 -07001550 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001551 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001552 unlink_obj(item);
1553 }
1554
1555 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001556 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001557
1558 /* Drop our reference from above */
1559 config_item_put(item);
1560
Joel Becker70526b62008-06-17 15:34:32 -07001561 module_put(dead_item_owner);
1562 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001563
1564 return 0;
1565}
1566
Arjan van de Ven754661f2007-02-12 00:55:38 -08001567const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001568 .mkdir = configfs_mkdir,
1569 .rmdir = configfs_rmdir,
1570 .symlink = configfs_symlink,
1571 .unlink = configfs_unlink,
1572 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001573 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001574};
1575
Al Viro81d44ed12012-03-17 16:13:25 -04001576const struct inode_operations configfs_root_inode_operations = {
1577 .lookup = configfs_lookup,
1578 .setattr = configfs_setattr,
1579};
1580
Joel Becker7063fbf2005-12-15 14:29:43 -08001581#if 0
1582int configfs_rename_dir(struct config_item * item, const char *new_name)
1583{
1584 int error = 0;
1585 struct dentry * new_dentry, * parent;
1586
1587 if (!strcmp(config_item_name(item), new_name))
1588 return -EINVAL;
1589
1590 if (!item->parent)
1591 return -EINVAL;
1592
1593 down_write(&configfs_rename_sem);
1594 parent = item->parent->dentry;
1595
Al Viro59551022016-01-22 15:40:57 -05001596 inode_lock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001597
1598 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1599 if (!IS_ERR(new_dentry)) {
David Howells2b0143b2015-03-17 22:25:59 +00001600 if (d_really_is_negative(new_dentry)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001601 error = config_item_set_name(item, "%s", new_name);
1602 if (!error) {
1603 d_add(new_dentry, NULL);
1604 d_move(item->dentry, new_dentry);
1605 }
1606 else
1607 d_delete(new_dentry);
1608 } else
1609 error = -EEXIST;
1610 dput(new_dentry);
1611 }
Al Viro59551022016-01-22 15:40:57 -05001612 inode_unlock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001613 up_write(&configfs_rename_sem);
1614
1615 return error;
1616}
1617#endif
1618
1619static int configfs_dir_open(struct inode *inode, struct file *file)
1620{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001621 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001622 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001623 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001624
Al Viro59551022016-01-22 15:40:57 -05001625 inode_lock(d_inode(dentry));
Louis Rilling2a109f22008-07-04 16:56:05 +02001626 /*
1627 * Fake invisibility if dir belongs to a group/default groups hierarchy
1628 * being attached
1629 */
1630 err = -ENOENT;
1631 if (configfs_dirent_is_ready(parent_sd)) {
Al Viro47320fb2019-08-25 19:56:13 -04001632 file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
Louis Rilling2a109f22008-07-04 16:56:05 +02001633 if (IS_ERR(file->private_data))
1634 err = PTR_ERR(file->private_data);
1635 else
1636 err = 0;
1637 }
Al Viro59551022016-01-22 15:40:57 -05001638 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001639
Louis Rilling2a109f22008-07-04 16:56:05 +02001640 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001641}
1642
1643static int configfs_dir_close(struct inode *inode, struct file *file)
1644{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001645 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001646 struct configfs_dirent * cursor = file->private_data;
1647
Al Viro59551022016-01-22 15:40:57 -05001648 inode_lock(d_inode(dentry));
Louis Rilling6f610762008-06-16 19:00:58 +02001649 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001650 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001651 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001652 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001653
1654 release_configfs_dirent(cursor);
1655
1656 return 0;
1657}
1658
1659/* Relationship between s_mode and the DT_xxx types */
1660static inline unsigned char dt_type(struct configfs_dirent *sd)
1661{
1662 return (sd->s_mode >> 12) & 15;
1663}
1664
Al Viro52018852013-05-16 01:28:34 -04001665static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001666{
Al Viro52018852013-05-16 01:28:34 -04001667 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001668 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001669 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001670 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001671 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001672 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001673
Al Viro52018852013-05-16 01:28:34 -04001674 if (!dir_emit_dots(file, ctx))
1675 return 0;
Al Viroa01b3002016-04-20 19:50:05 -04001676 spin_lock(&configfs_dirent_lock);
1677 if (ctx->pos == 2)
Al Viro52018852013-05-16 01:28:34 -04001678 list_move(q, &parent_sd->s_children);
Al Viro52018852013-05-16 01:28:34 -04001679 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1680 struct configfs_dirent *next;
1681 const char *name;
1682 int len;
1683 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001684
Al Viro52018852013-05-16 01:28:34 -04001685 next = list_entry(p, struct configfs_dirent, s_sibling);
1686 if (!next->s_element)
1687 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001688
Al Viro52018852013-05-16 01:28:34 -04001689 /*
1690 * We'll have a dentry and an inode for
1691 * PINNED items and for open attribute
1692 * files. We lock here to prevent a race
1693 * with configfs_d_iput() clearing
1694 * s_dentry before calling iput().
1695 *
1696 * Why do we go to the trouble? If
1697 * someone has an attribute file open,
1698 * the inode number should match until
1699 * they close it. Beyond that, we don't
1700 * care.
1701 */
Al Viro52018852013-05-16 01:28:34 -04001702 dentry = next->s_dentry;
1703 if (dentry)
David Howells2b0143b2015-03-17 22:25:59 +00001704 inode = d_inode(dentry);
Al Viro52018852013-05-16 01:28:34 -04001705 if (inode)
1706 ino = inode->i_ino;
1707 spin_unlock(&configfs_dirent_lock);
1708 if (!inode)
1709 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001710
Al Viroa01b3002016-04-20 19:50:05 -04001711 name = configfs_get_name(next);
1712 len = strlen(name);
1713
Al Viro52018852013-05-16 01:28:34 -04001714 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1715 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001716
Al Viro52018852013-05-16 01:28:34 -04001717 spin_lock(&configfs_dirent_lock);
1718 list_move(q, p);
Al Viro52018852013-05-16 01:28:34 -04001719 p = q;
1720 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001721 }
Al Viroa01b3002016-04-20 19:50:05 -04001722 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001723 return 0;
1724}
1725
Andrew Morton965c8e52012-12-17 15:59:39 -08001726static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001727{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001728 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001729
Andrew Morton965c8e52012-12-17 15:59:39 -08001730 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001731 case 1:
1732 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001733 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001734 case 0:
1735 if (offset >= 0)
1736 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001737 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001738 default:
Joel Becker7063fbf2005-12-15 14:29:43 -08001739 return -EINVAL;
1740 }
1741 if (offset != file->f_pos) {
1742 file->f_pos = offset;
1743 if (file->f_pos >= 2) {
1744 struct configfs_dirent *sd = dentry->d_fsdata;
1745 struct configfs_dirent *cursor = file->private_data;
1746 struct list_head *p;
1747 loff_t n = file->f_pos - 2;
1748
Louis Rilling6f610762008-06-16 19:00:58 +02001749 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001750 list_del(&cursor->s_sibling);
1751 p = sd->s_children.next;
1752 while (n && p != &sd->s_children) {
1753 struct configfs_dirent *next;
1754 next = list_entry(p, struct configfs_dirent,
1755 s_sibling);
1756 if (next->s_element)
1757 n--;
1758 p = p->next;
1759 }
1760 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001761 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001762 }
1763 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001764 return offset;
1765}
1766
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001767const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001768 .open = configfs_dir_open,
1769 .release = configfs_dir_close,
1770 .llseek = configfs_dir_lseek,
1771 .read = generic_read_dir,
Al Viroa01b3002016-04-20 19:50:05 -04001772 .iterate_shared = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001773};
1774
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001775/**
1776 * configfs_register_group - creates a parent-child relation between two groups
1777 * @parent_group: parent group
1778 * @group: child group
1779 *
1780 * link groups, creates dentry for the child and attaches it to the
1781 * parent dentry.
1782 *
1783 * Return: 0 on success, negative errno code on error
1784 */
1785int configfs_register_group(struct config_group *parent_group,
1786 struct config_group *group)
1787{
1788 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1789 struct dentry *parent;
Al Viro47320fb2019-08-25 19:56:13 -04001790 struct configfs_fragment *frag;
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001791 int ret;
1792
Al Viro47320fb2019-08-25 19:56:13 -04001793 frag = new_fragment();
1794 if (!frag)
1795 return -ENOMEM;
1796
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001797 mutex_lock(&subsys->su_mutex);
1798 link_group(parent_group, group);
1799 mutex_unlock(&subsys->su_mutex);
1800
1801 parent = parent_group->cg_item.ci_dentry;
1802
Al Viro59551022016-01-22 15:40:57 -05001803 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Al Viro47320fb2019-08-25 19:56:13 -04001804 ret = create_default_group(parent_group, group, frag);
YueHaibing35399f82019-05-05 11:03:12 +08001805 if (ret)
1806 goto err_out;
1807
1808 spin_lock(&configfs_dirent_lock);
1809 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1810 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001811 inode_unlock(d_inode(parent));
Al Viro47320fb2019-08-25 19:56:13 -04001812 put_fragment(frag);
YueHaibing35399f82019-05-05 11:03:12 +08001813 return 0;
1814err_out:
1815 inode_unlock(d_inode(parent));
1816 mutex_lock(&subsys->su_mutex);
1817 unlink_group(group);
1818 mutex_unlock(&subsys->su_mutex);
Al Viro47320fb2019-08-25 19:56:13 -04001819 put_fragment(frag);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001820 return ret;
1821}
1822EXPORT_SYMBOL(configfs_register_group);
1823
1824/**
1825 * configfs_unregister_group() - unregisters a child group from its parent
1826 * @group: parent group to be unregistered
1827 *
1828 * Undoes configfs_register_group()
1829 */
1830void configfs_unregister_group(struct config_group *group)
1831{
1832 struct configfs_subsystem *subsys = group->cg_subsys;
1833 struct dentry *dentry = group->cg_item.ci_dentry;
1834 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
Al Virob0841ee2019-08-31 09:43:43 +02001835 struct configfs_dirent *sd = dentry->d_fsdata;
1836 struct configfs_fragment *frag = sd->s_frag;
1837
1838 down_write(&frag->frag_sem);
1839 frag->frag_dead = true;
1840 up_write(&frag->frag_sem);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001841
Al Viro59551022016-01-22 15:40:57 -05001842 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001843 spin_lock(&configfs_dirent_lock);
1844 configfs_detach_prep(dentry, NULL);
1845 spin_unlock(&configfs_dirent_lock);
1846
1847 configfs_detach_group(&group->cg_item);
1848 d_inode(dentry)->i_flags |= S_DEAD;
1849 dont_mount(dentry);
Amir Goldstein6146e782019-05-26 17:34:09 +03001850 fsnotify_rmdir(d_inode(parent), dentry);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001851 d_delete(dentry);
Al Viro59551022016-01-22 15:40:57 -05001852 inode_unlock(d_inode(parent));
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001853
1854 dput(dentry);
1855
1856 mutex_lock(&subsys->su_mutex);
1857 unlink_group(group);
1858 mutex_unlock(&subsys->su_mutex);
1859}
1860EXPORT_SYMBOL(configfs_unregister_group);
1861
1862/**
1863 * configfs_register_default_group() - allocates and registers a child group
1864 * @parent_group: parent group
1865 * @name: child group name
1866 * @item_type: child item type description
1867 *
1868 * boilerplate to allocate and register a child group with its parent. We need
1869 * kzalloc'ed memory because child's default_group is initially empty.
1870 *
1871 * Return: allocated config group or ERR_PTR() on error
1872 */
1873struct config_group *
1874configfs_register_default_group(struct config_group *parent_group,
1875 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +02001876 const struct config_item_type *item_type)
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001877{
1878 int ret;
1879 struct config_group *group;
1880
1881 group = kzalloc(sizeof(*group), GFP_KERNEL);
1882 if (!group)
1883 return ERR_PTR(-ENOMEM);
1884 config_group_init_type_name(group, name, item_type);
1885
1886 ret = configfs_register_group(parent_group, group);
1887 if (ret) {
1888 kfree(group);
1889 return ERR_PTR(ret);
1890 }
1891 return group;
1892}
1893EXPORT_SYMBOL(configfs_register_default_group);
1894
1895/**
1896 * configfs_unregister_default_group() - unregisters and frees a child group
1897 * @group: the group to act on
1898 */
1899void configfs_unregister_default_group(struct config_group *group)
1900{
1901 configfs_unregister_group(group);
1902 kfree(group);
1903}
1904EXPORT_SYMBOL(configfs_unregister_default_group);
1905
Joel Becker7063fbf2005-12-15 14:29:43 -08001906int configfs_register_subsystem(struct configfs_subsystem *subsys)
1907{
1908 int err;
1909 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001910 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001911 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001912 struct configfs_dirent *sd;
Al Viro47320fb2019-08-25 19:56:13 -04001913 struct configfs_fragment *frag;
1914
1915 frag = new_fragment();
1916 if (!frag)
1917 return -ENOMEM;
Joel Becker7063fbf2005-12-15 14:29:43 -08001918
Al Viro2a152ad2012-03-17 16:53:29 -04001919 root = configfs_pin_fs();
Al Viro47320fb2019-08-25 19:56:13 -04001920 if (IS_ERR(root)) {
1921 put_fragment(frag);
Al Viro2a152ad2012-03-17 16:53:29 -04001922 return PTR_ERR(root);
Al Viro47320fb2019-08-25 19:56:13 -04001923 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001924
1925 if (!group->cg_item.ci_name)
1926 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1927
Al Virob7c177f2012-03-17 16:24:54 -04001928 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001929 link_group(to_config_group(sd->s_element), group);
1930
Al Viro59551022016-01-22 15:40:57 -05001931 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001932
Joel Becker7063fbf2005-12-15 14:29:43 -08001933 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001934 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001935 if (dentry) {
1936 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001937
Joel Beckerafdf04e2007-03-05 15:49:49 -08001938 err = configfs_attach_group(sd->s_element, &group->cg_item,
Al Viro47320fb2019-08-25 19:56:13 -04001939 dentry, frag);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001940 if (err) {
David Howells2b0143b2015-03-17 22:25:59 +00001941 BUG_ON(d_inode(dentry));
Joel Beckerdf7f9962011-02-22 01:09:49 -08001942 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001943 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001944 } else {
1945 spin_lock(&configfs_dirent_lock);
1946 configfs_dir_set_ready(dentry->d_fsdata);
1947 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001948 }
1949 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001950
Al Viro59551022016-01-22 15:40:57 -05001951 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001952
Joel Beckerafdf04e2007-03-05 15:49:49 -08001953 if (err) {
1954 unlink_group(group);
1955 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001956 }
Al Viro47320fb2019-08-25 19:56:13 -04001957 put_fragment(frag);
Joel Becker7063fbf2005-12-15 14:29:43 -08001958
1959 return err;
1960}
1961
1962void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1963{
1964 struct config_group *group = &subsys->su_group;
1965 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001966 struct dentry *root = dentry->d_sb->s_root;
Al Virob0841ee2019-08-31 09:43:43 +02001967 struct configfs_dirent *sd = dentry->d_fsdata;
1968 struct configfs_fragment *frag = sd->s_frag;
Joel Becker7063fbf2005-12-15 14:29:43 -08001969
Al Virob7c177f2012-03-17 16:24:54 -04001970 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001971 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001972 return;
1973 }
1974
Al Virob0841ee2019-08-31 09:43:43 +02001975 down_write(&frag->frag_sem);
1976 frag->frag_dead = true;
1977 up_write(&frag->frag_sem);
1978
Al Viro59551022016-01-22 15:40:57 -05001979 inode_lock_nested(d_inode(root),
Mark Fasheh55ed1602006-10-20 14:55:54 -07001980 I_MUTEX_PARENT);
Al Viro59551022016-01-22 15:40:57 -05001981 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001982 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001983 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001984 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001985 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001986 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001987 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001988 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001989 configfs_detach_group(&group->cg_item);
David Howells2b0143b2015-03-17 22:25:59 +00001990 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001991 dont_mount(dentry);
Amir Goldstein6146e782019-05-26 17:34:09 +03001992 fsnotify_rmdir(d_inode(root), dentry);
Al Viro59551022016-01-22 15:40:57 -05001993 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001994
1995 d_delete(dentry);
1996
Al Viro59551022016-01-22 15:40:57 -05001997 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001998
1999 dput(dentry);
2000
2001 unlink_group(group);
2002 configfs_release_fs();
2003}
2004
2005EXPORT_SYMBOL(configfs_register_subsystem);
2006EXPORT_SYMBOL(configfs_unregister_subsystem);