blob: 7731c459cfc30c56dd9741dd00ef14e55ea7b1bb [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>
16#include <linux/mount.h>
17#include <linux/module.h>
18#include <linux/slab.h>
Louis Rilling107ed402008-06-16 19:01:00 +020019#include <linux/err.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080020
21#include <linux/configfs.h>
22#include "configfs_internal.h"
23
24DECLARE_RWSEM(configfs_rename_sem);
Louis Rilling6f610762008-06-16 19:00:58 +020025/*
26 * Protects mutations of configfs_dirent linkage together with proper i_mutex
Louis Rilling5301a772008-06-16 19:00:59 +020027 * Also protects mutations of symlinks linkage to target configfs_dirent
Louis Rilling6f610762008-06-16 19:00:58 +020028 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
29 * and configfs_dirent_lock locked, in that order.
Louis Rilling5301a772008-06-16 19:00:59 +020030 * This allows one to safely traverse configfs_dirent trees and symlinks without
31 * having to lock inodes.
Louis Rillingb3e76af2008-06-16 19:01:01 +020032 *
33 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
34 * unlocked is not reliable unless in detach_groups() called from
35 * rmdir()/unregister() and from configfs_attach_group()
Louis Rilling6f610762008-06-16 19:00:58 +020036 */
37DEFINE_SPINLOCK(configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080038
39static void configfs_d_iput(struct dentry * dentry,
40 struct inode * inode)
41{
Joel Becker24307aa2011-05-18 04:08:16 -070042 struct configfs_dirent *sd = dentry->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -080043
44 if (sd) {
Joel Becker24307aa2011-05-18 04:08:16 -070045 /* Coordinate with configfs_readdir */
46 spin_lock(&configfs_dirent_lock);
Junxiao Bi76ae2812013-11-21 14:31:56 -080047 /* Coordinate with configfs_attach_attr where will increase
48 * sd->s_count and update sd->s_dentry to new allocated one.
49 * Only set sd->dentry to null when this dentry is the only
50 * sd owner.
51 * If not do so, configfs_d_iput may run just after
52 * configfs_attach_attr and set sd->s_dentry to null
53 * even it's still in use.
54 */
55 if (atomic_read(&sd->s_count) <= 2)
56 sd->s_dentry = NULL;
57
Joel Becker24307aa2011-05-18 04:08:16 -070058 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080059 configfs_put(sd);
60 }
61 iput(inode);
62}
63
Al Virod463a0c2011-01-12 16:41:05 -050064const struct dentry_operations configfs_dentry_ops = {
Joel Becker7063fbf2005-12-15 14:29:43 -080065 .d_iput = configfs_d_iput,
Al Virob26d4cd2013-10-25 18:47:37 -040066 .d_delete = always_delete_dentry,
Joel Becker7063fbf2005-12-15 14:29:43 -080067};
68
Louis Rillinge74cc062009-01-28 19:18:32 +010069#ifdef CONFIG_LOCKDEP
70
71/*
72 * Helpers to make lockdep happy with our recursive locking of default groups'
73 * inodes (see configfs_attach_group() and configfs_detach_group()).
74 * We put default groups i_mutexes in separate classes according to their depth
75 * from the youngest non-default group ancestor.
76 *
77 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
78 * groups A/B and A/C will have their inode's mutex in class
79 * default_group_class[0], and default group A/C/D will be in
80 * default_group_class[1].
81 *
82 * The lock classes are declared and assigned in inode.c, according to the
83 * s_depth value.
84 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
85 * default groups, and reset to -1 when all default groups are attached. During
86 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
87 * inode's mutex is set to default_group_class[s_depth - 1].
88 */
89
90static void configfs_init_dirent_depth(struct configfs_dirent *sd)
91{
92 sd->s_depth = -1;
93}
94
95static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
96 struct configfs_dirent *sd)
97{
98 int parent_depth = parent_sd->s_depth;
99
100 if (parent_depth >= 0)
101 sd->s_depth = parent_depth + 1;
102}
103
104static void
105configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
106{
107 /*
108 * item's i_mutex class is already setup, so s_depth is now only
109 * used to set new sub-directories s_depth, which is always done
110 * with item's i_mutex locked.
111 */
112 /*
113 * sd->s_depth == -1 iff we are a non default group.
114 * else (we are a default group) sd->s_depth > 0 (see
115 * create_dir()).
116 */
117 if (sd->s_depth == -1)
118 /*
119 * We are a non default group and we are going to create
120 * default groups.
121 */
122 sd->s_depth = 0;
123}
124
125static void
126configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
127{
128 /* We will not create default groups anymore. */
129 sd->s_depth = -1;
130}
131
132#else /* CONFIG_LOCKDEP */
133
134static void configfs_init_dirent_depth(struct configfs_dirent *sd)
135{
136}
137
138static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
139 struct configfs_dirent *sd)
140{
141}
142
143static void
144configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
145{
146}
147
148static void
149configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
150{
151}
152
153#endif /* CONFIG_LOCKDEP */
154
Joel Becker7063fbf2005-12-15 14:29:43 -0800155/*
156 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
157 */
Louis Rilling420118c2009-01-28 19:18:33 +0100158static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
159 void *element, int type)
Joel Becker7063fbf2005-12-15 14:29:43 -0800160{
161 struct configfs_dirent * sd;
162
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800163 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -0800164 if (!sd)
Louis Rilling107ed402008-06-16 19:01:00 +0200165 return ERR_PTR(-ENOMEM);
Joel Becker7063fbf2005-12-15 14:29:43 -0800166
Joel Becker7063fbf2005-12-15 14:29:43 -0800167 atomic_set(&sd->s_count, 1);
168 INIT_LIST_HEAD(&sd->s_links);
169 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -0800170 sd->s_element = element;
Louis Rilling420118c2009-01-28 19:18:33 +0100171 sd->s_type = type;
Louis Rillinge74cc062009-01-28 19:18:32 +0100172 configfs_init_dirent_depth(sd);
Louis Rilling6f610762008-06-16 19:00:58 +0200173 spin_lock(&configfs_dirent_lock);
Louis Rillingb3e76af2008-06-16 19:01:01 +0200174 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
175 spin_unlock(&configfs_dirent_lock);
176 kmem_cache_free(configfs_dir_cachep, sd);
177 return ERR_PTR(-ENOENT);
178 }
Louis Rilling6f610762008-06-16 19:00:58 +0200179 list_add(&sd->s_sibling, &parent_sd->s_children);
180 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800181
182 return sd;
183}
184
Joel Beckerb4c98f62006-09-13 11:01:19 -0700185/*
186 *
187 * Return -EEXIST if there is already a configfs element with the same
188 * name for the same parent.
189 *
190 * called with parent inode's i_mutex held
191 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100192static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
193 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700194{
195 struct configfs_dirent * sd;
196
197 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
198 if (sd->s_element) {
199 const unsigned char *existing = configfs_get_name(sd);
200 if (strcmp(existing, new))
201 continue;
202 else
203 return -EEXIST;
204 }
205 }
206
207 return 0;
208}
209
210
Joel Becker7063fbf2005-12-15 14:29:43 -0800211int configfs_make_dirent(struct configfs_dirent * parent_sd,
212 struct dentry * dentry, void * element,
213 umode_t mode, int type)
214{
215 struct configfs_dirent * sd;
216
Louis Rilling420118c2009-01-28 19:18:33 +0100217 sd = configfs_new_dirent(parent_sd, element, type);
Louis Rilling107ed402008-06-16 19:01:00 +0200218 if (IS_ERR(sd))
219 return PTR_ERR(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800220
221 sd->s_mode = mode;
Joel Becker7063fbf2005-12-15 14:29:43 -0800222 sd->s_dentry = dentry;
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100223 if (dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800224 dentry->d_fsdata = configfs_get(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800225
226 return 0;
227}
228
Al Viroc88b1e72015-01-29 00:17:57 -0500229static void init_dir(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800230{
231 inode->i_op = &configfs_dir_inode_operations;
232 inode->i_fop = &configfs_dir_operations;
233
234 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700235 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800236}
237
Al Viroc88b1e72015-01-29 00:17:57 -0500238static void configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800239{
240 inode->i_size = PAGE_SIZE;
241 inode->i_fop = &configfs_file_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800242}
243
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300244static void configfs_init_bin_file(struct inode *inode)
245{
246 inode->i_size = 0;
247 inode->i_fop = &configfs_bin_file_operations;
248}
249
Al Viroc88b1e72015-01-29 00:17:57 -0500250static void init_symlink(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800251{
252 inode->i_op = &configfs_symlink_inode_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800253}
254
Joel Becker7063fbf2005-12-15 14:29:43 -0800255/**
256 * configfs_create_dir - create a directory for an config_item.
257 * @item: config_itemwe're creating directory for.
258 * @dentry: config_item's dentry.
Louis Rilling2a109f22008-07-04 16:56:05 +0200259 *
260 * Note: user-created entries won't be allowed under this new directory
261 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800262 */
263
Al Viro1cf97d02015-01-29 00:20:49 -0500264static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800265{
Al Viro1cf97d02015-01-29 00:20:49 -0500266 int error;
267 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
268 struct dentry *p = dentry->d_parent;
269
270 BUG_ON(!item);
271
272 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
273 if (unlikely(error))
274 return error;
275
276 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
277 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
278 if (unlikely(error))
279 return error;
280
281 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
282 error = configfs_create(dentry, mode, init_dir);
283 if (!error) {
David Howells2b0143b2015-03-17 22:25:59 +0000284 inc_nlink(d_inode(p));
Joel Becker7063fbf2005-12-15 14:29:43 -0800285 item->ci_dentry = dentry;
Al Viro1cf97d02015-01-29 00:20:49 -0500286 } else {
287 struct configfs_dirent *sd = dentry->d_fsdata;
288 if (sd) {
289 spin_lock(&configfs_dirent_lock);
290 list_del_init(&sd->s_sibling);
291 spin_unlock(&configfs_dirent_lock);
292 configfs_put(sd);
293 }
294 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800295 return error;
296}
297
Louis Rilling2a109f22008-07-04 16:56:05 +0200298/*
299 * Allow userspace to create new entries under a new directory created with
300 * configfs_create_dir(), and under all of its chidlren directories recursively.
301 * @sd configfs_dirent of the new directory to validate
302 *
303 * Caller must hold configfs_dirent_lock.
304 */
305static void configfs_dir_set_ready(struct configfs_dirent *sd)
306{
307 struct configfs_dirent *child_sd;
308
309 sd->s_type &= ~CONFIGFS_USET_CREATING;
310 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
311 if (child_sd->s_type & CONFIGFS_USET_CREATING)
312 configfs_dir_set_ready(child_sd);
313}
314
315/*
316 * Check that a directory does not belong to a directory hierarchy being
317 * attached and not validated yet.
318 * @sd configfs_dirent of the directory to check
319 *
320 * @return non-zero iff the directory was validated
321 *
322 * Note: takes configfs_dirent_lock, so the result may change from false to true
323 * in two consecutive calls, but never from true to false.
324 */
325int configfs_dirent_is_ready(struct configfs_dirent *sd)
326{
327 int ret;
328
329 spin_lock(&configfs_dirent_lock);
330 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
331 spin_unlock(&configfs_dirent_lock);
332
333 return ret;
334}
335
Joel Becker7063fbf2005-12-15 14:29:43 -0800336int configfs_create_link(struct configfs_symlink *sl,
337 struct dentry *parent,
338 struct dentry *dentry)
339{
340 int err = 0;
341 umode_t mode = S_IFLNK | S_IRWXUGO;
342
Joel Becker3d0f89b2006-01-25 13:31:07 -0800343 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
344 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800345 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800346 err = configfs_create(dentry, mode, init_symlink);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100347 if (err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800348 struct configfs_dirent *sd = dentry->d_fsdata;
349 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200350 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800351 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200352 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800353 configfs_put(sd);
354 }
355 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800356 }
357 return err;
358}
359
360static void remove_dir(struct dentry * d)
361{
362 struct dentry * parent = dget(d->d_parent);
363 struct configfs_dirent * sd;
364
365 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200366 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800367 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200368 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800369 configfs_put(sd);
David Howells2b0143b2015-03-17 22:25:59 +0000370 if (d_really_is_positive(d))
371 simple_rmdir(d_inode(parent),d);
Joel Becker7063fbf2005-12-15 14:29:43 -0800372
Al Viroa4555892014-10-21 20:11:25 -0400373 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800374
375 dput(parent);
376}
377
378/**
379 * configfs_remove_dir - remove an config_item's directory.
380 * @item: config_item we're removing.
381 *
382 * The only thing special about this is that we remove any files in
383 * the directory before we remove the directory, and we've inlined
384 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200385 *
386 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800387 */
388
389static void configfs_remove_dir(struct config_item * item)
390{
391 struct dentry * dentry = dget(item->ci_dentry);
392
393 if (!dentry)
394 return;
395
396 remove_dir(dentry);
397 /**
398 * Drop reference from dget() on entrance.
399 */
400 dput(dentry);
401}
402
403
404/* attaches attribute's configfs_dirent to the dentry corresponding to the
405 * attribute file
406 */
407static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
408{
409 struct configfs_attribute * attr = sd->s_element;
410 int error;
411
Junxiao Bi76ae2812013-11-21 14:31:56 -0800412 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800413 dentry->d_fsdata = configfs_get(sd);
414 sd->s_dentry = dentry;
Junxiao Bi76ae2812013-11-21 14:31:56 -0800415 spin_unlock(&configfs_dirent_lock);
416
Dave Hansence8d2cd2007-10-16 23:31:13 -0700417 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300418 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
419 configfs_init_bin_file :
420 configfs_init_file);
Al Viro5cf3b562016-03-07 14:25:46 -0500421 if (error)
Joel Becker3d0f89b2006-01-25 13:31:07 -0800422 configfs_put(sd);
Al Viro5cf3b562016-03-07 14:25:46 -0500423 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800424}
425
426static struct dentry * configfs_lookup(struct inode *dir,
427 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400428 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800429{
430 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
431 struct configfs_dirent * sd;
432 int found = 0;
Louis Rilling2a109f22008-07-04 16:56:05 +0200433 int err;
434
435 /*
436 * Fake invisibility if dir belongs to a group/default groups hierarchy
437 * being attached
438 *
439 * This forbids userspace to read/write attributes of items which may
440 * not complete their initialization, since the dentries of the
441 * attributes won't be instantiated.
442 */
443 err = -ENOENT;
444 if (!configfs_dirent_is_ready(parent_sd))
445 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800446
447 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
448 if (sd->s_type & CONFIGFS_NOT_PINNED) {
449 const unsigned char * name = configfs_get_name(sd);
450
451 if (strcmp(name, dentry->d_name.name))
452 continue;
453
454 found = 1;
455 err = configfs_attach_attr(sd, dentry);
456 break;
457 }
458 }
459
460 if (!found) {
461 /*
462 * If it doesn't exist and it isn't a NOT_PINNED item,
463 * it must be negative.
464 */
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100465 if (dentry->d_name.len > NAME_MAX)
466 return ERR_PTR(-ENAMETOOLONG);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100467 d_add(dentry, NULL);
468 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800469 }
470
Louis Rilling2a109f22008-07-04 16:56:05 +0200471out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800472 return ERR_PTR(err);
473}
474
475/*
476 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200477 * attributes and are removed by rmdir(). We recurse, setting
478 * CONFIGFS_USET_DROPPING on all children that are candidates for
479 * default detach.
480 * If there is an error, the caller will reset the flags via
481 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800482 */
Al Viro48f35b72016-04-12 00:37:59 -0400483static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
Joel Becker7063fbf2005-12-15 14:29:43 -0800484{
485 struct configfs_dirent *parent_sd = dentry->d_fsdata;
486 struct configfs_dirent *sd;
487 int ret;
488
Louis Rilling4768e9b2008-06-23 14:16:17 +0200489 /* Mark that we're trying to drop the group */
490 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
491
Joel Becker7063fbf2005-12-15 14:29:43 -0800492 ret = -EBUSY;
493 if (!list_empty(&parent_sd->s_links))
494 goto out;
495
496 ret = 0;
497 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200498 if (!sd->s_element ||
499 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800500 continue;
501 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200502 /* Abort if racing with mkdir() */
503 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
Al Viro48f35b72016-04-12 00:37:59 -0400504 if (wait)
505 *wait= dget(sd->s_dentry);
Louis Rilling6d8344b2008-06-16 19:01:02 +0200506 return -EAGAIN;
507 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800508
Joel Becker631d1fe2007-06-18 18:06:09 -0700509 /*
510 * Yup, recursive. If there's a problem, blame
511 * deep nesting of default_groups
512 */
Al Viro48f35b72016-04-12 00:37:59 -0400513 ret = configfs_detach_prep(sd->s_dentry, wait);
Joel Becker7063fbf2005-12-15 14:29:43 -0800514 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800515 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800516 } else
517 ret = -ENOTEMPTY;
518
519 break;
520 }
521
522out:
523 return ret;
524}
525
526/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200527 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800528 * set.
529 */
530static void configfs_detach_rollback(struct dentry *dentry)
531{
532 struct configfs_dirent *parent_sd = dentry->d_fsdata;
533 struct configfs_dirent *sd;
534
Louis Rilling4768e9b2008-06-23 14:16:17 +0200535 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
536
537 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
538 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800539 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800540}
541
542static void detach_attrs(struct config_item * item)
543{
544 struct dentry * dentry = dget(item->ci_dentry);
545 struct configfs_dirent * parent_sd;
546 struct configfs_dirent * sd, * tmp;
547
548 if (!dentry)
549 return;
550
551 pr_debug("configfs %s: dropping attrs for dir\n",
552 dentry->d_name.name);
553
554 parent_sd = dentry->d_fsdata;
555 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
556 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
557 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200558 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800559 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200560 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800561 configfs_drop_dentry(sd, dentry);
562 configfs_put(sd);
563 }
564
565 /**
566 * Drop reference from dget() on entrance.
567 */
568 dput(dentry);
569}
570
571static int populate_attrs(struct config_item *item)
572{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200573 const struct config_item_type *t = item->ci_type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800574 struct configfs_attribute *attr;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300575 struct configfs_bin_attribute *bin_attr;
Joel Becker7063fbf2005-12-15 14:29:43 -0800576 int error = 0;
577 int i;
578
579 if (!t)
580 return -EINVAL;
581 if (t->ct_attrs) {
582 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
583 if ((error = configfs_create_file(item, attr)))
584 break;
585 }
586 }
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300587 if (t->ct_bin_attrs) {
588 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
589 error = configfs_create_bin_file(item, bin_attr);
590 if (error)
591 break;
592 }
593 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800594
595 if (error)
596 detach_attrs(item);
597
598 return error;
599}
600
601static int configfs_attach_group(struct config_item *parent_item,
602 struct config_item *item,
603 struct dentry *dentry);
604static void configfs_detach_group(struct config_item *item);
605
606static void detach_groups(struct config_group *group)
607{
608 struct dentry * dentry = dget(group->cg_item.ci_dentry);
609 struct dentry *child;
610 struct configfs_dirent *parent_sd;
611 struct configfs_dirent *sd, *tmp;
612
613 if (!dentry)
614 return;
615
616 parent_sd = dentry->d_fsdata;
617 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
618 if (!sd->s_element ||
619 !(sd->s_type & CONFIGFS_USET_DEFAULT))
620 continue;
621
622 child = sd->s_dentry;
623
Al Viro59551022016-01-22 15:40:57 -0500624 inode_lock(d_inode(child));
Louis Rillingb3e76af2008-06-16 19:01:01 +0200625
Joel Becker7063fbf2005-12-15 14:29:43 -0800626 configfs_detach_group(sd->s_element);
David Howells2b0143b2015-03-17 22:25:59 +0000627 d_inode(child)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400628 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800629
Al Viro59551022016-01-22 15:40:57 -0500630 inode_unlock(d_inode(child));
Joel Becker7063fbf2005-12-15 14:29:43 -0800631
632 d_delete(child);
633 dput(child);
634 }
635
636 /**
637 * Drop reference from dget() on entrance.
638 */
639 dput(dentry);
640}
641
642/*
643 * This fakes mkdir(2) on a default_groups[] entry. It
644 * creates a dentry, attachs it, and then does fixup
645 * on the sd->s_type.
646 *
647 * We could, perhaps, tweak our parent's ->mkdir for a minute and
648 * try using vfs_mkdir. Just a thought.
649 */
650static int create_default_group(struct config_group *parent_group,
651 struct config_group *group)
652{
653 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800654 struct configfs_dirent *sd;
655 /* We trust the caller holds a reference to parent */
656 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
657
658 if (!group->cg_item.ci_name)
659 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800660
661 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400662 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800663 if (child) {
664 d_add(child, NULL);
665
666 ret = configfs_attach_group(&parent_group->cg_item,
667 &group->cg_item, child);
668 if (!ret) {
669 sd = child->d_fsdata;
670 sd->s_type |= CONFIGFS_USET_DEFAULT;
671 } else {
David Howells2b0143b2015-03-17 22:25:59 +0000672 BUG_ON(d_inode(child));
Joel Beckerdf7f9962011-02-22 01:09:49 -0800673 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800674 dput(child);
675 }
676 }
677
678 return ret;
679}
680
681static int populate_groups(struct config_group *group)
682{
683 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800684 int ret = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800685
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100686 list_for_each_entry(new_group, &group->default_groups, group_entry) {
687 ret = create_default_group(group, new_group);
688 if (ret) {
689 detach_groups(group);
690 break;
Joel Becker7063fbf2005-12-15 14:29:43 -0800691 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800692 }
693
Joel Becker7063fbf2005-12-15 14:29:43 -0800694 return ret;
695}
696
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100697void configfs_remove_default_groups(struct config_group *group)
698{
699 struct config_group *g, *n;
700
701 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
702 list_del(&g->group_entry);
703 config_item_put(&g->cg_item);
704 }
705}
706EXPORT_SYMBOL(configfs_remove_default_groups);
707
Joel Becker7063fbf2005-12-15 14:29:43 -0800708/*
709 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700710 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800711 */
712
713static void unlink_obj(struct config_item *item)
714{
715 struct config_group *group;
716
717 group = item->ci_group;
718 if (group) {
719 list_del_init(&item->ci_entry);
720
721 item->ci_group = NULL;
722 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700723
724 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800725 config_item_put(item);
726
Joel Beckereed7a0d2006-04-11 21:37:20 -0700727 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800728 config_group_put(group);
729 }
730}
731
732static void link_obj(struct config_item *parent_item, struct config_item *item)
733{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700734 /*
735 * Parent seems redundant with group, but it makes certain
736 * traversals much nicer.
737 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800738 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700739
740 /*
741 * We hold a reference on the parent for the child's ci_parent
742 * link.
743 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800744 item->ci_group = config_group_get(to_config_group(parent_item));
745 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
746
Joel Beckereed7a0d2006-04-11 21:37:20 -0700747 /*
748 * We hold a reference on the child for ci_entry on the parent's
749 * cg_children
750 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800751 config_item_get(item);
752}
753
754static void unlink_group(struct config_group *group)
755{
Joel Becker7063fbf2005-12-15 14:29:43 -0800756 struct config_group *new_group;
757
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100758 list_for_each_entry(new_group, &group->default_groups, group_entry)
759 unlink_group(new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800760
761 group->cg_subsys = NULL;
762 unlink_obj(&group->cg_item);
763}
764
765static void link_group(struct config_group *parent_group, struct config_group *group)
766{
Joel Becker7063fbf2005-12-15 14:29:43 -0800767 struct config_group *new_group;
768 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
769
770 link_obj(&parent_group->cg_item, &group->cg_item);
771
772 if (parent_group->cg_subsys)
773 subsys = parent_group->cg_subsys;
774 else if (configfs_is_root(&parent_group->cg_item))
775 subsys = to_configfs_subsystem(group);
776 else
777 BUG();
778 group->cg_subsys = subsys;
779
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100780 list_for_each_entry(new_group, &group->default_groups, group_entry)
781 link_group(group, new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800782}
783
784/*
785 * The goal is that configfs_attach_item() (and
786 * configfs_attach_group()) can be called from either the VFS or this
787 * module. That is, they assume that the items have been created,
788 * the dentry allocated, and the dcache is all ready to go.
789 *
790 * If they fail, they must clean up after themselves as if they
791 * had never been called. The caller (VFS or local function) will
792 * handle cleaning up the dcache bits.
793 *
794 * configfs_detach_group() and configfs_detach_item() behave similarly on
795 * the way out. They assume that the proper semaphores are held, they
796 * clean up the configfs items, and they expect their callers will
797 * handle the dcache bits.
798 */
799static int configfs_attach_item(struct config_item *parent_item,
800 struct config_item *item,
801 struct dentry *dentry)
802{
803 int ret;
804
805 ret = configfs_create_dir(item, dentry);
806 if (!ret) {
807 ret = populate_attrs(item);
808 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200809 /*
810 * We are going to remove an inode and its dentry but
811 * the VFS may already have hit and used them. Thus,
812 * we must lock them as rmdir() would.
813 */
Al Viro59551022016-01-22 15:40:57 -0500814 inode_lock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800815 configfs_remove_dir(item);
David Howells2b0143b2015-03-17 22:25:59 +0000816 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400817 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -0500818 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800819 d_delete(dentry);
820 }
821 }
822
823 return ret;
824}
825
Louis Rilling2e2ce172008-07-04 16:56:06 +0200826/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800827static void configfs_detach_item(struct config_item *item)
828{
829 detach_attrs(item);
830 configfs_remove_dir(item);
831}
832
833static int configfs_attach_group(struct config_item *parent_item,
834 struct config_item *item,
835 struct dentry *dentry)
836{
837 int ret;
838 struct configfs_dirent *sd;
839
840 ret = configfs_attach_item(parent_item, item, dentry);
841 if (!ret) {
842 sd = dentry->d_fsdata;
843 sd->s_type |= CONFIGFS_USET_DIR;
844
Louis Rilling2e2ce172008-07-04 16:56:06 +0200845 /*
846 * FYI, we're faking mkdir in populate_groups()
847 * We must lock the group's inode to avoid races with the VFS
848 * which can already hit the inode and try to add/remove entries
849 * under it.
850 *
851 * We must also lock the inode to remove it safely in case of
852 * error, as rmdir() would.
853 */
Al Viro59551022016-01-22 15:40:57 -0500854 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100855 configfs_adjust_dir_dirent_depth_before_populate(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800856 ret = populate_groups(to_config_group(item));
857 if (ret) {
858 configfs_detach_item(item);
David Howells2b0143b2015-03-17 22:25:59 +0000859 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400860 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800861 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100862 configfs_adjust_dir_dirent_depth_after_populate(sd);
Al Viro59551022016-01-22 15:40:57 -0500863 inode_unlock(d_inode(dentry));
Louis Rilling2e2ce172008-07-04 16:56:06 +0200864 if (ret)
865 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800866 }
867
868 return ret;
869}
870
Louis Rilling2e2ce172008-07-04 16:56:06 +0200871/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800872static void configfs_detach_group(struct config_item *item)
873{
874 detach_groups(to_config_group(item));
875 configfs_detach_item(item);
876}
877
878/*
Joel Becker299894c2006-10-06 17:33:23 -0700879 * After the item has been detached from the filesystem view, we are
880 * ready to tear it out of the hierarchy. Notify the client before
881 * we do that so they can perform any cleanup that requires
882 * navigating the hierarchy. A client does not need to provide this
883 * callback. The subsystem semaphore MUST be held by the caller, and
884 * references must be valid for both items. It also assumes the
885 * caller has validated ci_type.
886 */
887static void client_disconnect_notify(struct config_item *parent_item,
888 struct config_item *item)
889{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200890 const struct config_item_type *type;
Joel Becker299894c2006-10-06 17:33:23 -0700891
892 type = parent_item->ci_type;
893 BUG_ON(!type);
894
895 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
896 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
897 item);
898}
899
900/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800901 * Drop the initial reference from make_item()/make_group()
902 * This function assumes that reference is held on item
903 * and that item holds a valid reference to the parent. Also, it
904 * assumes the caller has validated ci_type.
905 */
906static void client_drop_item(struct config_item *parent_item,
907 struct config_item *item)
908{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200909 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800910
911 type = parent_item->ci_type;
912 BUG_ON(!type);
913
Joel Beckereed7a0d2006-04-11 21:37:20 -0700914 /*
915 * If ->drop_item() exists, it is responsible for the
916 * config_item_put().
917 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800918 if (type->ct_group_ops && type->ct_group_ops->drop_item)
919 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700920 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800921 else
922 config_item_put(item);
923}
924
Joel Becker631d1fe2007-06-18 18:06:09 -0700925#ifdef DEBUG
926static void configfs_dump_one(struct configfs_dirent *sd, int level)
927{
Fabian Frederickc6686932014-06-04 16:05:58 -0700928 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700929
Fabian Frederickc6686932014-06-04 16:05:58 -0700930#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700931 type_print(CONFIGFS_ROOT);
932 type_print(CONFIGFS_DIR);
933 type_print(CONFIGFS_ITEM_ATTR);
934 type_print(CONFIGFS_ITEM_LINK);
935 type_print(CONFIGFS_USET_DIR);
936 type_print(CONFIGFS_USET_DEFAULT);
937 type_print(CONFIGFS_USET_DROPPING);
938#undef type_print
939}
940
941static int configfs_dump(struct configfs_dirent *sd, int level)
942{
943 struct configfs_dirent *child_sd;
944 int ret = 0;
945
946 configfs_dump_one(sd, level);
947
948 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
949 return 0;
950
951 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
952 ret = configfs_dump(child_sd, level + 2);
953 if (ret)
954 break;
955 }
956
957 return ret;
958}
959#endif
960
961
962/*
963 * configfs_depend_item() and configfs_undepend_item()
964 *
965 * WARNING: Do not call these from a configfs callback!
966 *
967 * This describes these functions and their helpers.
968 *
969 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300970 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -0700971 * it. The idea is to give client modules as simple an interface as
972 * possible. When a system asks them to depend on an item, they just
973 * call configfs_depend_item(). If the item is live and the client
974 * driver is in good shape, we'll happily do the work for them.
975 *
976 * Why is the locking complex? Because configfs uses the VFS to handle
977 * all locking, but this function is called outside the normal
978 * VFS->configfs path. So it must take VFS locks to prevent the
979 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
980 * why you can't call these functions underneath configfs callbacks.
981 *
982 * Note, btw, that this can be called at *any* time, even when a configfs
983 * subsystem isn't registered, or when configfs is loading or unloading.
984 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +0100985 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
986 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -0700987 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +0100988 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
989 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -0700990 */
991
992/*
993 * configfs_depend_prep()
994 *
995 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
996 * attributes. This is similar but not the same to configfs_detach_prep().
997 * Note that configfs_detach_prep() expects the parent to be locked when it
998 * is called, but we lock the parent *inside* configfs_depend_prep(). We
999 * do that so we can unlock it if we find nothing.
1000 *
1001 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001002 * our object.
1003 * We deliberately ignore items tagged as dropping since they are virtually
1004 * dead, as well as items in the middle of attachment since they virtually
1005 * do not exist yet. This completes the locking out of racing mkdir() and
1006 * rmdir().
1007 * Note: subdirectories in the middle of attachment start with s_type =
1008 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1009 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1010 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001011 *
Louis Rilling420118c2009-01-28 19:18:33 +01001012 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001013 *
1014 * This adds a requirement that all config_items be unique!
1015 *
Louis Rilling420118c2009-01-28 19:18:33 +01001016 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001017 * much on the stack, though, so folks that need this function - be careful
1018 * about your stack! Patches will be accepted to make it iterative.
1019 */
1020static int configfs_depend_prep(struct dentry *origin,
1021 struct config_item *target)
1022{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001023 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001024 int ret = 0;
1025
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001026 BUG_ON(!origin || !origin->d_fsdata);
1027 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001028
Joel Becker631d1fe2007-06-18 18:06:09 -07001029 if (sd->s_element == target) /* Boo-yah */
1030 goto out;
1031
1032 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001033 if ((child_sd->s_type & CONFIGFS_DIR) &&
1034 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1035 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001036 ret = configfs_depend_prep(child_sd->s_dentry,
1037 target);
1038 if (!ret)
1039 goto out; /* Child path boo-yah */
1040 }
1041 }
1042
1043 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001044 ret = -ENOENT;
1045
1046out:
1047 return ret;
1048}
1049
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001050static int configfs_do_depend_item(struct dentry *subsys_dentry,
1051 struct config_item *target)
1052{
1053 struct configfs_dirent *p;
1054 int ret;
1055
1056 spin_lock(&configfs_dirent_lock);
1057 /* Scan the tree, return 0 if found */
1058 ret = configfs_depend_prep(subsys_dentry, target);
1059 if (ret)
1060 goto out_unlock_dirent_lock;
1061
1062 /*
1063 * We are sure that the item is not about to be removed by rmdir(), and
1064 * not in the middle of attachment by mkdir().
1065 */
1066 p = target->ci_dentry->d_fsdata;
1067 p->s_dependent_count += 1;
1068
1069out_unlock_dirent_lock:
1070 spin_unlock(&configfs_dirent_lock);
1071
1072 return ret;
1073}
1074
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001075static inline struct configfs_dirent *
1076configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1077 struct config_item *subsys_item)
1078{
1079 struct configfs_dirent *p;
1080 struct configfs_dirent *ret = NULL;
1081
1082 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1083 if (p->s_type & CONFIGFS_DIR &&
1084 p->s_element == subsys_item) {
1085 ret = p;
1086 break;
1087 }
1088 }
1089
1090 return ret;
1091}
1092
1093
Joel Becker631d1fe2007-06-18 18:06:09 -07001094int configfs_depend_item(struct configfs_subsystem *subsys,
1095 struct config_item *target)
1096{
1097 int ret;
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001098 struct configfs_dirent *subsys_sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001099 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001100 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001101
1102 /*
1103 * Pin the configfs filesystem. This means we can safely access
1104 * the root of the configfs filesystem.
1105 */
Al Viro2a152ad2012-03-17 16:53:29 -04001106 root = configfs_pin_fs();
1107 if (IS_ERR(root))
1108 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001109
1110 /*
1111 * Next, lock the root directory. We're going to check that the
1112 * subsystem is really registered, and so we need to lock out
1113 * configfs_[un]register_subsystem().
1114 */
Al Viro59551022016-01-22 15:40:57 -05001115 inode_lock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001116
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001117 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
Joel Becker631d1fe2007-06-18 18:06:09 -07001118 if (!subsys_sd) {
1119 ret = -ENOENT;
1120 goto out_unlock_fs;
1121 }
1122
1123 /* Ok, now we can trust subsys/s_item */
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001124 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
Joel Becker631d1fe2007-06-18 18:06:09 -07001125
Joel Becker631d1fe2007-06-18 18:06:09 -07001126out_unlock_fs:
Al Viro59551022016-01-22 15:40:57 -05001127 inode_unlock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001128
1129 /*
1130 * If we succeeded, the fs is pinned via other methods. If not,
1131 * we're done with it anyway. So release_fs() is always right.
1132 */
1133 configfs_release_fs();
1134
1135 return ret;
1136}
1137EXPORT_SYMBOL(configfs_depend_item);
1138
1139/*
1140 * Release the dependent linkage. This is much simpler than
1141 * configfs_depend_item() because we know that that the client driver is
1142 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1143 */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +01001144void configfs_undepend_item(struct config_item *target)
Joel Becker631d1fe2007-06-18 18:06:09 -07001145{
1146 struct configfs_dirent *sd;
1147
1148 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001149 * Since we can trust everything is pinned, we just need
1150 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001151 */
Louis Rilling420118c2009-01-28 19:18:33 +01001152 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001153
1154 sd = target->ci_dentry->d_fsdata;
1155 BUG_ON(sd->s_dependent_count < 1);
1156
1157 sd->s_dependent_count -= 1;
1158
1159 /*
1160 * After this unlock, we cannot trust the item to stay alive!
1161 * DO NOT REFERENCE item after this unlock.
1162 */
Louis Rilling420118c2009-01-28 19:18:33 +01001163 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001164}
1165EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001166
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001167/*
1168 * caller_subsys is a caller's subsystem not target's. This is used to
1169 * determine if we should lock root and check subsys or not. When we are
1170 * in the same subsystem as our target there is no need to do locking as
1171 * we know that subsys is valid and is not unregistered during this function
1172 * as we are called from callback of one of his children and VFS holds a lock
1173 * on some inode. Otherwise we have to lock our root to ensure that target's
1174 * subsystem it is not unregistered during this function.
1175 */
1176int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1177 struct config_item *target)
1178{
1179 struct configfs_subsystem *target_subsys;
1180 struct config_group *root, *parent;
1181 struct configfs_dirent *subsys_sd;
1182 int ret = -ENOENT;
1183
1184 /* Disallow this function for configfs root */
1185 if (configfs_is_root(target))
1186 return -EINVAL;
1187
1188 parent = target->ci_group;
1189 /*
1190 * This may happen when someone is trying to depend root
1191 * directory of some subsystem
1192 */
1193 if (configfs_is_root(&parent->cg_item)) {
1194 target_subsys = to_configfs_subsystem(to_config_group(target));
1195 root = parent;
1196 } else {
1197 target_subsys = parent->cg_subsys;
1198 /* Find a cofnigfs root as we may need it for locking */
1199 for (root = parent; !configfs_is_root(&root->cg_item);
1200 root = root->cg_item.ci_group)
1201 ;
1202 }
1203
1204 if (target_subsys != caller_subsys) {
1205 /*
1206 * We are in other configfs subsystem, so we have to do
1207 * additional locking to prevent other subsystem from being
1208 * unregistered
1209 */
Al Viro59551022016-01-22 15:40:57 -05001210 inode_lock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001211
1212 /*
1213 * As we are trying to depend item from other subsystem
1214 * we have to check if this subsystem is still registered
1215 */
1216 subsys_sd = configfs_find_subsys_dentry(
1217 root->cg_item.ci_dentry->d_fsdata,
1218 &target_subsys->su_group.cg_item);
1219 if (!subsys_sd)
1220 goto out_root_unlock;
1221 } else {
1222 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1223 }
1224
1225 /* Now we can execute core of depend item */
1226 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1227
1228 if (target_subsys != caller_subsys)
1229out_root_unlock:
1230 /*
1231 * We were called from subsystem other than our target so we
1232 * took some locks so now it's time to release them
1233 */
Al Viro59551022016-01-22 15:40:57 -05001234 inode_unlock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001235
1236 return ret;
1237}
1238EXPORT_SYMBOL(configfs_depend_item_unlocked);
1239
Al Viro18bb1db2011-07-26 01:41:39 -04001240static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001241{
Joel Beckera6795e92008-07-17 15:21:29 -07001242 int ret = 0;
1243 int module_got = 0;
1244 struct config_group *group = NULL;
1245 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001246 struct config_item *parent_item;
1247 struct configfs_subsystem *subsys;
1248 struct configfs_dirent *sd;
Bhumika Goyalaa293582017-10-16 17:18:40 +02001249 const struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001250 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001251 char *name;
1252
Joel Becker7063fbf2005-12-15 14:29:43 -08001253 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001254
1255 /*
1256 * Fake invisibility if dir belongs to a group/default groups hierarchy
1257 * being attached
1258 */
1259 if (!configfs_dirent_is_ready(sd)) {
1260 ret = -ENOENT;
1261 goto out;
1262 }
1263
Joel Becker84efad12006-03-27 18:46:09 -08001264 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1265 ret = -EPERM;
1266 goto out;
1267 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001268
Joel Becker84efad12006-03-27 18:46:09 -08001269 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001270 parent_item = configfs_get_config_item(dentry->d_parent);
1271 type = parent_item->ci_type;
1272 subsys = to_config_group(parent_item)->cg_subsys;
1273 BUG_ON(!subsys);
1274
1275 if (!type || !type->ct_group_ops ||
1276 (!type->ct_group_ops->make_group &&
1277 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001278 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1279 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001280 }
1281
Joel Becker70526b62008-06-17 15:34:32 -07001282 /*
1283 * The subsystem may belong to a different module than the item
1284 * being created. We don't want to safely pin the new item but
1285 * fail to pin the subsystem it sits under.
1286 */
1287 if (!subsys->su_group.cg_item.ci_type) {
1288 ret = -EINVAL;
1289 goto out_put;
1290 }
1291 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1292 if (!try_module_get(subsys_owner)) {
1293 ret = -EINVAL;
1294 goto out_put;
1295 }
1296
Joel Becker7063fbf2005-12-15 14:29:43 -08001297 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1298 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001299 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001300 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001301 }
Joel Becker84efad12006-03-27 18:46:09 -08001302
Joel Becker7063fbf2005-12-15 14:29:43 -08001303 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1304
Joel Beckere6bd07a2007-07-06 23:33:17 -07001305 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001306 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001307 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001308 if (!group)
1309 group = ERR_PTR(-ENOMEM);
1310 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001311 link_group(to_config_group(parent_item), group);
1312 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001313 } else
1314 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001315 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001316 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001317 if (!item)
1318 item = ERR_PTR(-ENOMEM);
1319 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001320 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001321 else
1322 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001323 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001324 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001325
1326 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001327 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001328 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001329 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001330 * There are no extra references to clean up.
1331 */
Joel Becker70526b62008-06-17 15:34:32 -07001332 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001333 }
1334
Joel Beckereed7a0d2006-04-11 21:37:20 -07001335 /*
1336 * link_obj() has been called (via link_group() for groups).
1337 * From here on out, errors must clean that up.
1338 */
1339
Joel Becker7063fbf2005-12-15 14:29:43 -08001340 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001341 if (!type) {
1342 ret = -EINVAL;
1343 goto out_unlink;
1344 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001345
Joel Becker70526b62008-06-17 15:34:32 -07001346 new_item_owner = type->ct_owner;
1347 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001348 ret = -EINVAL;
1349 goto out_unlink;
1350 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001351
Joel Beckereed7a0d2006-04-11 21:37:20 -07001352 /*
1353 * I hate doing it this way, but if there is
1354 * an error, module_put() probably should
1355 * happen after any cleanup.
1356 */
1357 module_got = 1;
1358
Louis Rilling6d8344b2008-06-16 19:01:02 +02001359 /*
1360 * Make racing rmdir() fail if it did not tag parent with
1361 * CONFIGFS_USET_DROPPING
1362 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1363 * fail and let rmdir() terminate correctly
1364 */
1365 spin_lock(&configfs_dirent_lock);
1366 /* This will make configfs_detach_prep() fail */
1367 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1368 spin_unlock(&configfs_dirent_lock);
1369
Joel Beckereed7a0d2006-04-11 21:37:20 -07001370 if (group)
1371 ret = configfs_attach_group(parent_item, item, dentry);
1372 else
1373 ret = configfs_attach_item(parent_item, item, dentry);
1374
Louis Rilling6d8344b2008-06-16 19:01:02 +02001375 spin_lock(&configfs_dirent_lock);
1376 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001377 if (!ret)
1378 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001379 spin_unlock(&configfs_dirent_lock);
1380
Joel Beckereed7a0d2006-04-11 21:37:20 -07001381out_unlink:
1382 if (ret) {
1383 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001384 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001385
1386 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001387 if (group)
1388 unlink_group(group);
1389 else
1390 unlink_obj(item);
1391 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001392
Joel Beckere6bd07a2007-07-06 23:33:17 -07001393 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001394
1395 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001396 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001397 }
1398
Joel Becker70526b62008-06-17 15:34:32 -07001399out_subsys_put:
1400 if (ret)
1401 module_put(subsys_owner);
1402
Joel Becker84efad12006-03-27 18:46:09 -08001403out_put:
1404 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001405 * link_obj()/link_group() took a reference from child->parent,
1406 * so the parent is safely pinned. We can drop our working
1407 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001408 */
1409 config_item_put(parent_item);
1410
1411out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001412 return ret;
1413}
1414
1415static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1416{
1417 struct config_item *parent_item;
1418 struct config_item *item;
1419 struct configfs_subsystem *subsys;
1420 struct configfs_dirent *sd;
Joel Becker70526b62008-06-17 15:34:32 -07001421 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001422 int ret;
1423
Joel Becker7063fbf2005-12-15 14:29:43 -08001424 sd = dentry->d_fsdata;
1425 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1426 return -EPERM;
1427
Joel Becker84efad12006-03-27 18:46:09 -08001428 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001429 parent_item = configfs_get_config_item(dentry->d_parent);
1430 subsys = to_config_group(parent_item)->cg_subsys;
1431 BUG_ON(!subsys);
1432
1433 if (!parent_item->ci_type) {
1434 config_item_put(parent_item);
1435 return -EINVAL;
1436 }
1437
Joel Becker70526b62008-06-17 15:34:32 -07001438 /* configfs_mkdir() shouldn't have allowed this */
1439 BUG_ON(!subsys->su_group.cg_item.ci_type);
1440 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1441
Louis Rilling9a73d782008-06-20 14:09:22 +02001442 /*
1443 * Ensure that no racing symlink() will make detach_prep() fail while
1444 * the new link is temporarily attached
1445 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001446 do {
Al Viro48f35b72016-04-12 00:37:59 -04001447 struct dentry *wait;
Louis Rilling6d8344b2008-06-16 19:01:02 +02001448
Louis Rillingde6bf182008-08-15 12:37:23 -07001449 mutex_lock(&configfs_symlink_mutex);
1450 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001451 /*
1452 * Here's where we check for dependents. We're protected by
1453 * configfs_dirent_lock.
1454 * If no dependent, atomically tag the item as dropping.
1455 */
1456 ret = sd->s_dependent_count ? -EBUSY : 0;
1457 if (!ret) {
Al Viro48f35b72016-04-12 00:37:59 -04001458 ret = configfs_detach_prep(dentry, &wait);
Louis Rilling420118c2009-01-28 19:18:33 +01001459 if (ret)
1460 configfs_detach_rollback(dentry);
1461 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001462 spin_unlock(&configfs_dirent_lock);
1463 mutex_unlock(&configfs_symlink_mutex);
1464
1465 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001466 if (ret != -EAGAIN) {
1467 config_item_put(parent_item);
1468 return ret;
1469 }
1470
1471 /* Wait until the racing operation terminates */
Al Viro48f35b72016-04-12 00:37:59 -04001472 inode_lock(d_inode(wait));
1473 inode_unlock(d_inode(wait));
1474 dput(wait);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001475 }
1476 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001477
Joel Becker84efad12006-03-27 18:46:09 -08001478 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001479 item = configfs_get_config_item(dentry);
1480
1481 /* Drop reference from above, item already holds one. */
1482 config_item_put(parent_item);
1483
1484 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001485 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001486
1487 if (sd->s_type & CONFIGFS_USET_DIR) {
1488 configfs_detach_group(item);
1489
Joel Beckere6bd07a2007-07-06 23:33:17 -07001490 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001491 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001492 unlink_group(to_config_group(item));
1493 } else {
1494 configfs_detach_item(item);
1495
Joel Beckere6bd07a2007-07-06 23:33:17 -07001496 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001497 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001498 unlink_obj(item);
1499 }
1500
1501 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001502 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001503
1504 /* Drop our reference from above */
1505 config_item_put(item);
1506
Joel Becker70526b62008-06-17 15:34:32 -07001507 module_put(dead_item_owner);
1508 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001509
1510 return 0;
1511}
1512
Arjan van de Ven754661f2007-02-12 00:55:38 -08001513const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001514 .mkdir = configfs_mkdir,
1515 .rmdir = configfs_rmdir,
1516 .symlink = configfs_symlink,
1517 .unlink = configfs_unlink,
1518 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001519 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001520};
1521
Al Viro81d44ed12012-03-17 16:13:25 -04001522const struct inode_operations configfs_root_inode_operations = {
1523 .lookup = configfs_lookup,
1524 .setattr = configfs_setattr,
1525};
1526
Joel Becker7063fbf2005-12-15 14:29:43 -08001527#if 0
1528int configfs_rename_dir(struct config_item * item, const char *new_name)
1529{
1530 int error = 0;
1531 struct dentry * new_dentry, * parent;
1532
1533 if (!strcmp(config_item_name(item), new_name))
1534 return -EINVAL;
1535
1536 if (!item->parent)
1537 return -EINVAL;
1538
1539 down_write(&configfs_rename_sem);
1540 parent = item->parent->dentry;
1541
Al Viro59551022016-01-22 15:40:57 -05001542 inode_lock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001543
1544 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1545 if (!IS_ERR(new_dentry)) {
David Howells2b0143b2015-03-17 22:25:59 +00001546 if (d_really_is_negative(new_dentry)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001547 error = config_item_set_name(item, "%s", new_name);
1548 if (!error) {
1549 d_add(new_dentry, NULL);
1550 d_move(item->dentry, new_dentry);
1551 }
1552 else
1553 d_delete(new_dentry);
1554 } else
1555 error = -EEXIST;
1556 dput(new_dentry);
1557 }
Al Viro59551022016-01-22 15:40:57 -05001558 inode_unlock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001559 up_write(&configfs_rename_sem);
1560
1561 return error;
1562}
1563#endif
1564
1565static int configfs_dir_open(struct inode *inode, struct file *file)
1566{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001567 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001568 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001569 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001570
Al Viro59551022016-01-22 15:40:57 -05001571 inode_lock(d_inode(dentry));
Louis Rilling2a109f22008-07-04 16:56:05 +02001572 /*
1573 * Fake invisibility if dir belongs to a group/default groups hierarchy
1574 * being attached
1575 */
1576 err = -ENOENT;
1577 if (configfs_dirent_is_ready(parent_sd)) {
Louis Rilling420118c2009-01-28 19:18:33 +01001578 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
Louis Rilling2a109f22008-07-04 16:56:05 +02001579 if (IS_ERR(file->private_data))
1580 err = PTR_ERR(file->private_data);
1581 else
1582 err = 0;
1583 }
Al Viro59551022016-01-22 15:40:57 -05001584 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001585
Louis Rilling2a109f22008-07-04 16:56:05 +02001586 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001587}
1588
1589static int configfs_dir_close(struct inode *inode, struct file *file)
1590{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001591 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001592 struct configfs_dirent * cursor = file->private_data;
1593
Al Viro59551022016-01-22 15:40:57 -05001594 inode_lock(d_inode(dentry));
Louis Rilling6f610762008-06-16 19:00:58 +02001595 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001596 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001597 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001598 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001599
1600 release_configfs_dirent(cursor);
1601
1602 return 0;
1603}
1604
1605/* Relationship between s_mode and the DT_xxx types */
1606static inline unsigned char dt_type(struct configfs_dirent *sd)
1607{
1608 return (sd->s_mode >> 12) & 15;
1609}
1610
Al Viro52018852013-05-16 01:28:34 -04001611static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001612{
Al Viro52018852013-05-16 01:28:34 -04001613 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001614 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001615 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001616 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001617 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001618 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001619
Al Viro52018852013-05-16 01:28:34 -04001620 if (!dir_emit_dots(file, ctx))
1621 return 0;
Al Viroa01b3002016-04-20 19:50:05 -04001622 spin_lock(&configfs_dirent_lock);
1623 if (ctx->pos == 2)
Al Viro52018852013-05-16 01:28:34 -04001624 list_move(q, &parent_sd->s_children);
Al Viro52018852013-05-16 01:28:34 -04001625 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1626 struct configfs_dirent *next;
1627 const char *name;
1628 int len;
1629 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001630
Al Viro52018852013-05-16 01:28:34 -04001631 next = list_entry(p, struct configfs_dirent, s_sibling);
1632 if (!next->s_element)
1633 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001634
Al Viro52018852013-05-16 01:28:34 -04001635 /*
1636 * We'll have a dentry and an inode for
1637 * PINNED items and for open attribute
1638 * files. We lock here to prevent a race
1639 * with configfs_d_iput() clearing
1640 * s_dentry before calling iput().
1641 *
1642 * Why do we go to the trouble? If
1643 * someone has an attribute file open,
1644 * the inode number should match until
1645 * they close it. Beyond that, we don't
1646 * care.
1647 */
Al Viro52018852013-05-16 01:28:34 -04001648 dentry = next->s_dentry;
1649 if (dentry)
David Howells2b0143b2015-03-17 22:25:59 +00001650 inode = d_inode(dentry);
Al Viro52018852013-05-16 01:28:34 -04001651 if (inode)
1652 ino = inode->i_ino;
1653 spin_unlock(&configfs_dirent_lock);
1654 if (!inode)
1655 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001656
Al Viroa01b3002016-04-20 19:50:05 -04001657 name = configfs_get_name(next);
1658 len = strlen(name);
1659
Al Viro52018852013-05-16 01:28:34 -04001660 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1661 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001662
Al Viro52018852013-05-16 01:28:34 -04001663 spin_lock(&configfs_dirent_lock);
1664 list_move(q, p);
Al Viro52018852013-05-16 01:28:34 -04001665 p = q;
1666 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001667 }
Al Viroa01b3002016-04-20 19:50:05 -04001668 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001669 return 0;
1670}
1671
Andrew Morton965c8e52012-12-17 15:59:39 -08001672static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001673{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001674 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001675
Andrew Morton965c8e52012-12-17 15:59:39 -08001676 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001677 case 1:
1678 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001679 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001680 case 0:
1681 if (offset >= 0)
1682 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001683 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001684 default:
Joel Becker7063fbf2005-12-15 14:29:43 -08001685 return -EINVAL;
1686 }
1687 if (offset != file->f_pos) {
1688 file->f_pos = offset;
1689 if (file->f_pos >= 2) {
1690 struct configfs_dirent *sd = dentry->d_fsdata;
1691 struct configfs_dirent *cursor = file->private_data;
1692 struct list_head *p;
1693 loff_t n = file->f_pos - 2;
1694
Louis Rilling6f610762008-06-16 19:00:58 +02001695 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001696 list_del(&cursor->s_sibling);
1697 p = sd->s_children.next;
1698 while (n && p != &sd->s_children) {
1699 struct configfs_dirent *next;
1700 next = list_entry(p, struct configfs_dirent,
1701 s_sibling);
1702 if (next->s_element)
1703 n--;
1704 p = p->next;
1705 }
1706 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001707 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001708 }
1709 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001710 return offset;
1711}
1712
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001713const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001714 .open = configfs_dir_open,
1715 .release = configfs_dir_close,
1716 .llseek = configfs_dir_lseek,
1717 .read = generic_read_dir,
Al Viroa01b3002016-04-20 19:50:05 -04001718 .iterate_shared = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001719};
1720
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001721/**
1722 * configfs_register_group - creates a parent-child relation between two groups
1723 * @parent_group: parent group
1724 * @group: child group
1725 *
1726 * link groups, creates dentry for the child and attaches it to the
1727 * parent dentry.
1728 *
1729 * Return: 0 on success, negative errno code on error
1730 */
1731int configfs_register_group(struct config_group *parent_group,
1732 struct config_group *group)
1733{
1734 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1735 struct dentry *parent;
1736 int ret;
1737
1738 mutex_lock(&subsys->su_mutex);
1739 link_group(parent_group, group);
1740 mutex_unlock(&subsys->su_mutex);
1741
1742 parent = parent_group->cg_item.ci_dentry;
1743
Al Viro59551022016-01-22 15:40:57 -05001744 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001745 ret = create_default_group(parent_group, group);
YueHaibing35399f82019-05-05 11:03:12 +08001746 if (ret)
1747 goto err_out;
1748
1749 spin_lock(&configfs_dirent_lock);
1750 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1751 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001752 inode_unlock(d_inode(parent));
YueHaibing35399f82019-05-05 11:03:12 +08001753 return 0;
1754err_out:
1755 inode_unlock(d_inode(parent));
1756 mutex_lock(&subsys->su_mutex);
1757 unlink_group(group);
1758 mutex_unlock(&subsys->su_mutex);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001759 return ret;
1760}
1761EXPORT_SYMBOL(configfs_register_group);
1762
1763/**
1764 * configfs_unregister_group() - unregisters a child group from its parent
1765 * @group: parent group to be unregistered
1766 *
1767 * Undoes configfs_register_group()
1768 */
1769void configfs_unregister_group(struct config_group *group)
1770{
1771 struct configfs_subsystem *subsys = group->cg_subsys;
1772 struct dentry *dentry = group->cg_item.ci_dentry;
1773 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1774
Mike Christiecc57c072018-07-15 18:16:17 -05001775 mutex_lock(&subsys->su_mutex);
1776 if (!group->cg_item.ci_parent->ci_group) {
1777 /*
1778 * The parent has already been unlinked and detached
1779 * due to a rmdir.
1780 */
1781 goto unlink_group;
1782 }
1783 mutex_unlock(&subsys->su_mutex);
1784
Al Viro59551022016-01-22 15:40:57 -05001785 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001786 spin_lock(&configfs_dirent_lock);
1787 configfs_detach_prep(dentry, NULL);
1788 spin_unlock(&configfs_dirent_lock);
1789
1790 configfs_detach_group(&group->cg_item);
1791 d_inode(dentry)->i_flags |= S_DEAD;
1792 dont_mount(dentry);
1793 d_delete(dentry);
Al Viro59551022016-01-22 15:40:57 -05001794 inode_unlock(d_inode(parent));
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001795
1796 dput(dentry);
1797
1798 mutex_lock(&subsys->su_mutex);
Mike Christiecc57c072018-07-15 18:16:17 -05001799unlink_group:
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001800 unlink_group(group);
1801 mutex_unlock(&subsys->su_mutex);
1802}
1803EXPORT_SYMBOL(configfs_unregister_group);
1804
1805/**
1806 * configfs_register_default_group() - allocates and registers a child group
1807 * @parent_group: parent group
1808 * @name: child group name
1809 * @item_type: child item type description
1810 *
1811 * boilerplate to allocate and register a child group with its parent. We need
1812 * kzalloc'ed memory because child's default_group is initially empty.
1813 *
1814 * Return: allocated config group or ERR_PTR() on error
1815 */
1816struct config_group *
1817configfs_register_default_group(struct config_group *parent_group,
1818 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +02001819 const struct config_item_type *item_type)
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001820{
1821 int ret;
1822 struct config_group *group;
1823
1824 group = kzalloc(sizeof(*group), GFP_KERNEL);
1825 if (!group)
1826 return ERR_PTR(-ENOMEM);
1827 config_group_init_type_name(group, name, item_type);
1828
1829 ret = configfs_register_group(parent_group, group);
1830 if (ret) {
1831 kfree(group);
1832 return ERR_PTR(ret);
1833 }
1834 return group;
1835}
1836EXPORT_SYMBOL(configfs_register_default_group);
1837
1838/**
1839 * configfs_unregister_default_group() - unregisters and frees a child group
1840 * @group: the group to act on
1841 */
1842void configfs_unregister_default_group(struct config_group *group)
1843{
1844 configfs_unregister_group(group);
1845 kfree(group);
1846}
1847EXPORT_SYMBOL(configfs_unregister_default_group);
1848
Joel Becker7063fbf2005-12-15 14:29:43 -08001849int configfs_register_subsystem(struct configfs_subsystem *subsys)
1850{
1851 int err;
1852 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001853 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001854 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001855 struct configfs_dirent *sd;
1856
Al Viro2a152ad2012-03-17 16:53:29 -04001857 root = configfs_pin_fs();
1858 if (IS_ERR(root))
1859 return PTR_ERR(root);
Joel Becker7063fbf2005-12-15 14:29:43 -08001860
1861 if (!group->cg_item.ci_name)
1862 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1863
Al Virob7c177f2012-03-17 16:24:54 -04001864 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001865 link_group(to_config_group(sd->s_element), group);
1866
Al Viro59551022016-01-22 15:40:57 -05001867 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001868
Joel Becker7063fbf2005-12-15 14:29:43 -08001869 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001870 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001871 if (dentry) {
1872 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001873
Joel Beckerafdf04e2007-03-05 15:49:49 -08001874 err = configfs_attach_group(sd->s_element, &group->cg_item,
1875 dentry);
1876 if (err) {
David Howells2b0143b2015-03-17 22:25:59 +00001877 BUG_ON(d_inode(dentry));
Joel Beckerdf7f9962011-02-22 01:09:49 -08001878 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001879 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001880 } else {
1881 spin_lock(&configfs_dirent_lock);
1882 configfs_dir_set_ready(dentry->d_fsdata);
1883 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001884 }
1885 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001886
Al Viro59551022016-01-22 15:40:57 -05001887 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001888
Joel Beckerafdf04e2007-03-05 15:49:49 -08001889 if (err) {
1890 unlink_group(group);
1891 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001892 }
1893
1894 return err;
1895}
1896
1897void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1898{
1899 struct config_group *group = &subsys->su_group;
1900 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001901 struct dentry *root = dentry->d_sb->s_root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001902
Al Virob7c177f2012-03-17 16:24:54 -04001903 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001904 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001905 return;
1906 }
1907
Al Viro59551022016-01-22 15:40:57 -05001908 inode_lock_nested(d_inode(root),
Mark Fasheh55ed1602006-10-20 14:55:54 -07001909 I_MUTEX_PARENT);
Al Viro59551022016-01-22 15:40:57 -05001910 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001911 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001912 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001913 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001914 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001915 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001916 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001917 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001918 configfs_detach_group(&group->cg_item);
David Howells2b0143b2015-03-17 22:25:59 +00001919 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001920 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -05001921 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001922
1923 d_delete(dentry);
1924
Al Viro59551022016-01-22 15:40:57 -05001925 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001926
1927 dput(dentry);
1928
1929 unlink_group(group);
1930 configfs_release_fs();
1931}
1932
1933EXPORT_SYMBOL(configfs_register_subsystem);
1934EXPORT_SYMBOL(configfs_unregister_subsystem);