blob: d2ca5287762d8129998979296bf9c7fd97c6360d [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);
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053047 /*
48 * Set sd->s_dentry to null only when this dentry is the one
49 * that is going to be killed. Otherwise configfs_d_iput may
50 * run just after configfs_attach_attr and set sd->s_dentry to
51 * NULL even it's still in use.
Junxiao Bi76ae2812013-11-21 14:31:56 -080052 */
Sahitya Tummalaf6122ed2019-01-03 16:48:15 +053053 if (sd->s_dentry == dentry)
Junxiao Bi76ae2812013-11-21 14:31:56 -080054 sd->s_dentry = NULL;
55
Joel Becker24307aa2011-05-18 04:08:16 -070056 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080057 configfs_put(sd);
58 }
59 iput(inode);
60}
61
Al Virod463a0c2011-01-12 16:41:05 -050062const struct dentry_operations configfs_dentry_ops = {
Joel Becker7063fbf2005-12-15 14:29:43 -080063 .d_iput = configfs_d_iput,
Al Virob26d4cd2013-10-25 18:47:37 -040064 .d_delete = always_delete_dentry,
Joel Becker7063fbf2005-12-15 14:29:43 -080065};
66
Louis Rillinge74cc062009-01-28 19:18:32 +010067#ifdef CONFIG_LOCKDEP
68
69/*
70 * Helpers to make lockdep happy with our recursive locking of default groups'
71 * inodes (see configfs_attach_group() and configfs_detach_group()).
72 * We put default groups i_mutexes in separate classes according to their depth
73 * from the youngest non-default group ancestor.
74 *
75 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
76 * groups A/B and A/C will have their inode's mutex in class
77 * default_group_class[0], and default group A/C/D will be in
78 * default_group_class[1].
79 *
80 * The lock classes are declared and assigned in inode.c, according to the
81 * s_depth value.
82 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
83 * default groups, and reset to -1 when all default groups are attached. During
84 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
85 * inode's mutex is set to default_group_class[s_depth - 1].
86 */
87
88static void configfs_init_dirent_depth(struct configfs_dirent *sd)
89{
90 sd->s_depth = -1;
91}
92
93static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
94 struct configfs_dirent *sd)
95{
96 int parent_depth = parent_sd->s_depth;
97
98 if (parent_depth >= 0)
99 sd->s_depth = parent_depth + 1;
100}
101
102static void
103configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
104{
105 /*
106 * item's i_mutex class is already setup, so s_depth is now only
107 * used to set new sub-directories s_depth, which is always done
108 * with item's i_mutex locked.
109 */
110 /*
111 * sd->s_depth == -1 iff we are a non default group.
112 * else (we are a default group) sd->s_depth > 0 (see
113 * create_dir()).
114 */
115 if (sd->s_depth == -1)
116 /*
117 * We are a non default group and we are going to create
118 * default groups.
119 */
120 sd->s_depth = 0;
121}
122
123static void
124configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
125{
126 /* We will not create default groups anymore. */
127 sd->s_depth = -1;
128}
129
130#else /* CONFIG_LOCKDEP */
131
132static void configfs_init_dirent_depth(struct configfs_dirent *sd)
133{
134}
135
136static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
137 struct configfs_dirent *sd)
138{
139}
140
141static void
142configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
143{
144}
145
146static void
147configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
148{
149}
150
151#endif /* CONFIG_LOCKDEP */
152
Joel Becker7063fbf2005-12-15 14:29:43 -0800153/*
154 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
155 */
Louis Rilling420118c2009-01-28 19:18:33 +0100156static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
157 void *element, int type)
Joel Becker7063fbf2005-12-15 14:29:43 -0800158{
159 struct configfs_dirent * sd;
160
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800161 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -0800162 if (!sd)
Louis Rilling107ed402008-06-16 19:01:00 +0200163 return ERR_PTR(-ENOMEM);
Joel Becker7063fbf2005-12-15 14:29:43 -0800164
Joel Becker7063fbf2005-12-15 14:29:43 -0800165 atomic_set(&sd->s_count, 1);
166 INIT_LIST_HEAD(&sd->s_links);
167 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -0800168 sd->s_element = element;
Louis Rilling420118c2009-01-28 19:18:33 +0100169 sd->s_type = type;
Louis Rillinge74cc062009-01-28 19:18:32 +0100170 configfs_init_dirent_depth(sd);
Louis Rilling6f610762008-06-16 19:00:58 +0200171 spin_lock(&configfs_dirent_lock);
Louis Rillingb3e76af2008-06-16 19:01:01 +0200172 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
173 spin_unlock(&configfs_dirent_lock);
174 kmem_cache_free(configfs_dir_cachep, sd);
175 return ERR_PTR(-ENOENT);
176 }
Louis Rilling6f610762008-06-16 19:00:58 +0200177 list_add(&sd->s_sibling, &parent_sd->s_children);
178 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800179
180 return sd;
181}
182
Joel Beckerb4c98f62006-09-13 11:01:19 -0700183/*
184 *
185 * Return -EEXIST if there is already a configfs element with the same
186 * name for the same parent.
187 *
188 * called with parent inode's i_mutex held
189 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100190static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
191 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700192{
193 struct configfs_dirent * sd;
194
195 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
196 if (sd->s_element) {
197 const unsigned char *existing = configfs_get_name(sd);
198 if (strcmp(existing, new))
199 continue;
200 else
201 return -EEXIST;
202 }
203 }
204
205 return 0;
206}
207
208
Joel Becker7063fbf2005-12-15 14:29:43 -0800209int configfs_make_dirent(struct configfs_dirent * parent_sd,
210 struct dentry * dentry, void * element,
211 umode_t mode, int type)
212{
213 struct configfs_dirent * sd;
214
Louis Rilling420118c2009-01-28 19:18:33 +0100215 sd = configfs_new_dirent(parent_sd, element, type);
Louis Rilling107ed402008-06-16 19:01:00 +0200216 if (IS_ERR(sd))
217 return PTR_ERR(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800218
219 sd->s_mode = mode;
Joel Becker7063fbf2005-12-15 14:29:43 -0800220 sd->s_dentry = dentry;
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100221 if (dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800222 dentry->d_fsdata = configfs_get(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800223
224 return 0;
225}
226
Al Viroc88b1e72015-01-29 00:17:57 -0500227static void init_dir(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800228{
229 inode->i_op = &configfs_dir_inode_operations;
230 inode->i_fop = &configfs_dir_operations;
231
232 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700233 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800234}
235
Al Viroc88b1e72015-01-29 00:17:57 -0500236static void configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800237{
238 inode->i_size = PAGE_SIZE;
239 inode->i_fop = &configfs_file_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800240}
241
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300242static void configfs_init_bin_file(struct inode *inode)
243{
244 inode->i_size = 0;
245 inode->i_fop = &configfs_bin_file_operations;
246}
247
Al Viroc88b1e72015-01-29 00:17:57 -0500248static void init_symlink(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800249{
250 inode->i_op = &configfs_symlink_inode_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800251}
252
Joel Becker7063fbf2005-12-15 14:29:43 -0800253/**
254 * configfs_create_dir - create a directory for an config_item.
255 * @item: config_itemwe're creating directory for.
256 * @dentry: config_item's dentry.
Louis Rilling2a109f22008-07-04 16:56:05 +0200257 *
258 * Note: user-created entries won't be allowed under this new directory
259 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800260 */
261
Al Viro1cf97d02015-01-29 00:20:49 -0500262static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800263{
Al Viro1cf97d02015-01-29 00:20:49 -0500264 int error;
265 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
266 struct dentry *p = dentry->d_parent;
267
268 BUG_ON(!item);
269
270 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
271 if (unlikely(error))
272 return error;
273
274 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
275 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
276 if (unlikely(error))
277 return error;
278
279 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
280 error = configfs_create(dentry, mode, init_dir);
281 if (!error) {
David Howells2b0143b2015-03-17 22:25:59 +0000282 inc_nlink(d_inode(p));
Joel Becker7063fbf2005-12-15 14:29:43 -0800283 item->ci_dentry = dentry;
Al Viro1cf97d02015-01-29 00:20:49 -0500284 } else {
285 struct configfs_dirent *sd = dentry->d_fsdata;
286 if (sd) {
287 spin_lock(&configfs_dirent_lock);
288 list_del_init(&sd->s_sibling);
289 spin_unlock(&configfs_dirent_lock);
290 configfs_put(sd);
291 }
292 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800293 return error;
294}
295
Louis Rilling2a109f22008-07-04 16:56:05 +0200296/*
297 * Allow userspace to create new entries under a new directory created with
298 * configfs_create_dir(), and under all of its chidlren directories recursively.
299 * @sd configfs_dirent of the new directory to validate
300 *
301 * Caller must hold configfs_dirent_lock.
302 */
303static void configfs_dir_set_ready(struct configfs_dirent *sd)
304{
305 struct configfs_dirent *child_sd;
306
307 sd->s_type &= ~CONFIGFS_USET_CREATING;
308 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
309 if (child_sd->s_type & CONFIGFS_USET_CREATING)
310 configfs_dir_set_ready(child_sd);
311}
312
313/*
314 * Check that a directory does not belong to a directory hierarchy being
315 * attached and not validated yet.
316 * @sd configfs_dirent of the directory to check
317 *
318 * @return non-zero iff the directory was validated
319 *
320 * Note: takes configfs_dirent_lock, so the result may change from false to true
321 * in two consecutive calls, but never from true to false.
322 */
323int configfs_dirent_is_ready(struct configfs_dirent *sd)
324{
325 int ret;
326
327 spin_lock(&configfs_dirent_lock);
328 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
329 spin_unlock(&configfs_dirent_lock);
330
331 return ret;
332}
333
Joel Becker7063fbf2005-12-15 14:29:43 -0800334int configfs_create_link(struct configfs_symlink *sl,
335 struct dentry *parent,
336 struct dentry *dentry)
337{
338 int err = 0;
339 umode_t mode = S_IFLNK | S_IRWXUGO;
340
Joel Becker3d0f89b2006-01-25 13:31:07 -0800341 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
342 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800343 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800344 err = configfs_create(dentry, mode, init_symlink);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100345 if (err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800346 struct configfs_dirent *sd = dentry->d_fsdata;
347 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200348 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800349 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200350 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800351 configfs_put(sd);
352 }
353 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800354 }
355 return err;
356}
357
358static void remove_dir(struct dentry * d)
359{
360 struct dentry * parent = dget(d->d_parent);
361 struct configfs_dirent * sd;
362
363 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200364 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800365 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200366 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800367 configfs_put(sd);
David Howells2b0143b2015-03-17 22:25:59 +0000368 if (d_really_is_positive(d))
369 simple_rmdir(d_inode(parent),d);
Joel Becker7063fbf2005-12-15 14:29:43 -0800370
Al Viroa4555892014-10-21 20:11:25 -0400371 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800372
373 dput(parent);
374}
375
376/**
377 * configfs_remove_dir - remove an config_item's directory.
378 * @item: config_item we're removing.
379 *
380 * The only thing special about this is that we remove any files in
381 * the directory before we remove the directory, and we've inlined
382 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200383 *
384 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800385 */
386
387static void configfs_remove_dir(struct config_item * item)
388{
389 struct dentry * dentry = dget(item->ci_dentry);
390
391 if (!dentry)
392 return;
393
394 remove_dir(dentry);
395 /**
396 * Drop reference from dget() on entrance.
397 */
398 dput(dentry);
399}
400
401
402/* attaches attribute's configfs_dirent to the dentry corresponding to the
403 * attribute file
404 */
405static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
406{
407 struct configfs_attribute * attr = sd->s_element;
408 int error;
409
Junxiao Bi76ae2812013-11-21 14:31:56 -0800410 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800411 dentry->d_fsdata = configfs_get(sd);
412 sd->s_dentry = dentry;
Junxiao Bi76ae2812013-11-21 14:31:56 -0800413 spin_unlock(&configfs_dirent_lock);
414
Dave Hansence8d2cd2007-10-16 23:31:13 -0700415 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300416 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
417 configfs_init_bin_file :
418 configfs_init_file);
Al Viro5cf3b562016-03-07 14:25:46 -0500419 if (error)
Joel Becker3d0f89b2006-01-25 13:31:07 -0800420 configfs_put(sd);
Al Viro5cf3b562016-03-07 14:25:46 -0500421 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800422}
423
424static struct dentry * configfs_lookup(struct inode *dir,
425 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400426 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800427{
428 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
429 struct configfs_dirent * sd;
430 int found = 0;
Louis Rilling2a109f22008-07-04 16:56:05 +0200431 int err;
432
433 /*
434 * Fake invisibility if dir belongs to a group/default groups hierarchy
435 * being attached
436 *
437 * This forbids userspace to read/write attributes of items which may
438 * not complete their initialization, since the dentries of the
439 * attributes won't be instantiated.
440 */
441 err = -ENOENT;
442 if (!configfs_dirent_is_ready(parent_sd))
443 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800444
445 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
446 if (sd->s_type & CONFIGFS_NOT_PINNED) {
447 const unsigned char * name = configfs_get_name(sd);
448
449 if (strcmp(name, dentry->d_name.name))
450 continue;
451
452 found = 1;
453 err = configfs_attach_attr(sd, dentry);
454 break;
455 }
456 }
457
458 if (!found) {
459 /*
460 * If it doesn't exist and it isn't a NOT_PINNED item,
461 * it must be negative.
462 */
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100463 if (dentry->d_name.len > NAME_MAX)
464 return ERR_PTR(-ENAMETOOLONG);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100465 d_add(dentry, NULL);
466 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800467 }
468
Louis Rilling2a109f22008-07-04 16:56:05 +0200469out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800470 return ERR_PTR(err);
471}
472
473/*
474 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200475 * attributes and are removed by rmdir(). We recurse, setting
476 * CONFIGFS_USET_DROPPING on all children that are candidates for
477 * default detach.
478 * If there is an error, the caller will reset the flags via
479 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800480 */
Al Viro48f35b72016-04-12 00:37:59 -0400481static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
Joel Becker7063fbf2005-12-15 14:29:43 -0800482{
483 struct configfs_dirent *parent_sd = dentry->d_fsdata;
484 struct configfs_dirent *sd;
485 int ret;
486
Louis Rilling4768e9b2008-06-23 14:16:17 +0200487 /* Mark that we're trying to drop the group */
488 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
489
Joel Becker7063fbf2005-12-15 14:29:43 -0800490 ret = -EBUSY;
491 if (!list_empty(&parent_sd->s_links))
492 goto out;
493
494 ret = 0;
495 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200496 if (!sd->s_element ||
497 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800498 continue;
499 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200500 /* Abort if racing with mkdir() */
501 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
Al Viro48f35b72016-04-12 00:37:59 -0400502 if (wait)
503 *wait= dget(sd->s_dentry);
Louis Rilling6d8344b2008-06-16 19:01:02 +0200504 return -EAGAIN;
505 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800506
Joel Becker631d1fe2007-06-18 18:06:09 -0700507 /*
508 * Yup, recursive. If there's a problem, blame
509 * deep nesting of default_groups
510 */
Al Viro48f35b72016-04-12 00:37:59 -0400511 ret = configfs_detach_prep(sd->s_dentry, wait);
Joel Becker7063fbf2005-12-15 14:29:43 -0800512 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800513 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800514 } else
515 ret = -ENOTEMPTY;
516
517 break;
518 }
519
520out:
521 return ret;
522}
523
524/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200525 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800526 * set.
527 */
528static void configfs_detach_rollback(struct dentry *dentry)
529{
530 struct configfs_dirent *parent_sd = dentry->d_fsdata;
531 struct configfs_dirent *sd;
532
Louis Rilling4768e9b2008-06-23 14:16:17 +0200533 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
534
535 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
536 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800537 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800538}
539
540static void detach_attrs(struct config_item * item)
541{
542 struct dentry * dentry = dget(item->ci_dentry);
543 struct configfs_dirent * parent_sd;
544 struct configfs_dirent * sd, * tmp;
545
546 if (!dentry)
547 return;
548
549 pr_debug("configfs %s: dropping attrs for dir\n",
550 dentry->d_name.name);
551
552 parent_sd = dentry->d_fsdata;
553 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
554 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
555 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200556 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800557 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200558 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800559 configfs_drop_dentry(sd, dentry);
560 configfs_put(sd);
561 }
562
563 /**
564 * Drop reference from dget() on entrance.
565 */
566 dput(dentry);
567}
568
569static int populate_attrs(struct config_item *item)
570{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200571 const struct config_item_type *t = item->ci_type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800572 struct configfs_attribute *attr;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300573 struct configfs_bin_attribute *bin_attr;
Joel Becker7063fbf2005-12-15 14:29:43 -0800574 int error = 0;
575 int i;
576
577 if (!t)
578 return -EINVAL;
579 if (t->ct_attrs) {
580 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
581 if ((error = configfs_create_file(item, attr)))
582 break;
583 }
584 }
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300585 if (t->ct_bin_attrs) {
586 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
587 error = configfs_create_bin_file(item, bin_attr);
588 if (error)
589 break;
590 }
591 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800592
593 if (error)
594 detach_attrs(item);
595
596 return error;
597}
598
599static int configfs_attach_group(struct config_item *parent_item,
600 struct config_item *item,
601 struct dentry *dentry);
602static void configfs_detach_group(struct config_item *item);
603
604static void detach_groups(struct config_group *group)
605{
606 struct dentry * dentry = dget(group->cg_item.ci_dentry);
607 struct dentry *child;
608 struct configfs_dirent *parent_sd;
609 struct configfs_dirent *sd, *tmp;
610
611 if (!dentry)
612 return;
613
614 parent_sd = dentry->d_fsdata;
615 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
616 if (!sd->s_element ||
617 !(sd->s_type & CONFIGFS_USET_DEFAULT))
618 continue;
619
620 child = sd->s_dentry;
621
Al Viro59551022016-01-22 15:40:57 -0500622 inode_lock(d_inode(child));
Louis Rillingb3e76af2008-06-16 19:01:01 +0200623
Joel Becker7063fbf2005-12-15 14:29:43 -0800624 configfs_detach_group(sd->s_element);
David Howells2b0143b2015-03-17 22:25:59 +0000625 d_inode(child)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400626 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800627
Al Viro59551022016-01-22 15:40:57 -0500628 inode_unlock(d_inode(child));
Joel Becker7063fbf2005-12-15 14:29:43 -0800629
630 d_delete(child);
631 dput(child);
632 }
633
634 /**
635 * Drop reference from dget() on entrance.
636 */
637 dput(dentry);
638}
639
640/*
641 * This fakes mkdir(2) on a default_groups[] entry. It
642 * creates a dentry, attachs it, and then does fixup
643 * on the sd->s_type.
644 *
645 * We could, perhaps, tweak our parent's ->mkdir for a minute and
646 * try using vfs_mkdir. Just a thought.
647 */
648static int create_default_group(struct config_group *parent_group,
649 struct config_group *group)
650{
651 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800652 struct configfs_dirent *sd;
653 /* We trust the caller holds a reference to parent */
654 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
655
656 if (!group->cg_item.ci_name)
657 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800658
659 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400660 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800661 if (child) {
662 d_add(child, NULL);
663
664 ret = configfs_attach_group(&parent_group->cg_item,
665 &group->cg_item, child);
666 if (!ret) {
667 sd = child->d_fsdata;
668 sd->s_type |= CONFIGFS_USET_DEFAULT;
669 } else {
David Howells2b0143b2015-03-17 22:25:59 +0000670 BUG_ON(d_inode(child));
Joel Beckerdf7f9962011-02-22 01:09:49 -0800671 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800672 dput(child);
673 }
674 }
675
676 return ret;
677}
678
679static int populate_groups(struct config_group *group)
680{
681 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800682 int ret = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800683
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100684 list_for_each_entry(new_group, &group->default_groups, group_entry) {
685 ret = create_default_group(group, new_group);
686 if (ret) {
687 detach_groups(group);
688 break;
Joel Becker7063fbf2005-12-15 14:29:43 -0800689 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800690 }
691
Joel Becker7063fbf2005-12-15 14:29:43 -0800692 return ret;
693}
694
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100695void configfs_remove_default_groups(struct config_group *group)
696{
697 struct config_group *g, *n;
698
699 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
700 list_del(&g->group_entry);
701 config_item_put(&g->cg_item);
702 }
703}
704EXPORT_SYMBOL(configfs_remove_default_groups);
705
Joel Becker7063fbf2005-12-15 14:29:43 -0800706/*
707 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700708 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800709 */
710
711static void unlink_obj(struct config_item *item)
712{
713 struct config_group *group;
714
715 group = item->ci_group;
716 if (group) {
717 list_del_init(&item->ci_entry);
718
719 item->ci_group = NULL;
720 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700721
722 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800723 config_item_put(item);
724
Joel Beckereed7a0d2006-04-11 21:37:20 -0700725 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800726 config_group_put(group);
727 }
728}
729
730static void link_obj(struct config_item *parent_item, struct config_item *item)
731{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700732 /*
733 * Parent seems redundant with group, but it makes certain
734 * traversals much nicer.
735 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800736 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700737
738 /*
739 * We hold a reference on the parent for the child's ci_parent
740 * link.
741 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800742 item->ci_group = config_group_get(to_config_group(parent_item));
743 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
744
Joel Beckereed7a0d2006-04-11 21:37:20 -0700745 /*
746 * We hold a reference on the child for ci_entry on the parent's
747 * cg_children
748 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800749 config_item_get(item);
750}
751
752static void unlink_group(struct config_group *group)
753{
Joel Becker7063fbf2005-12-15 14:29:43 -0800754 struct config_group *new_group;
755
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100756 list_for_each_entry(new_group, &group->default_groups, group_entry)
757 unlink_group(new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800758
759 group->cg_subsys = NULL;
760 unlink_obj(&group->cg_item);
761}
762
763static void link_group(struct config_group *parent_group, struct config_group *group)
764{
Joel Becker7063fbf2005-12-15 14:29:43 -0800765 struct config_group *new_group;
766 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
767
768 link_obj(&parent_group->cg_item, &group->cg_item);
769
770 if (parent_group->cg_subsys)
771 subsys = parent_group->cg_subsys;
772 else if (configfs_is_root(&parent_group->cg_item))
773 subsys = to_configfs_subsystem(group);
774 else
775 BUG();
776 group->cg_subsys = subsys;
777
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100778 list_for_each_entry(new_group, &group->default_groups, group_entry)
779 link_group(group, new_group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800780}
781
782/*
783 * The goal is that configfs_attach_item() (and
784 * configfs_attach_group()) can be called from either the VFS or this
785 * module. That is, they assume that the items have been created,
786 * the dentry allocated, and the dcache is all ready to go.
787 *
788 * If they fail, they must clean up after themselves as if they
789 * had never been called. The caller (VFS or local function) will
790 * handle cleaning up the dcache bits.
791 *
792 * configfs_detach_group() and configfs_detach_item() behave similarly on
793 * the way out. They assume that the proper semaphores are held, they
794 * clean up the configfs items, and they expect their callers will
795 * handle the dcache bits.
796 */
797static int configfs_attach_item(struct config_item *parent_item,
798 struct config_item *item,
799 struct dentry *dentry)
800{
801 int ret;
802
803 ret = configfs_create_dir(item, dentry);
804 if (!ret) {
805 ret = populate_attrs(item);
806 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200807 /*
808 * We are going to remove an inode and its dentry but
809 * the VFS may already have hit and used them. Thus,
810 * we must lock them as rmdir() would.
811 */
Al Viro59551022016-01-22 15:40:57 -0500812 inode_lock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800813 configfs_remove_dir(item);
David Howells2b0143b2015-03-17 22:25:59 +0000814 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400815 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -0500816 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -0800817 d_delete(dentry);
818 }
819 }
820
821 return ret;
822}
823
Louis Rilling2e2ce172008-07-04 16:56:06 +0200824/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800825static void configfs_detach_item(struct config_item *item)
826{
827 detach_attrs(item);
828 configfs_remove_dir(item);
829}
830
831static int configfs_attach_group(struct config_item *parent_item,
832 struct config_item *item,
833 struct dentry *dentry)
834{
835 int ret;
836 struct configfs_dirent *sd;
837
838 ret = configfs_attach_item(parent_item, item, dentry);
839 if (!ret) {
840 sd = dentry->d_fsdata;
841 sd->s_type |= CONFIGFS_USET_DIR;
842
Louis Rilling2e2ce172008-07-04 16:56:06 +0200843 /*
844 * FYI, we're faking mkdir in populate_groups()
845 * We must lock the group's inode to avoid races with the VFS
846 * which can already hit the inode and try to add/remove entries
847 * under it.
848 *
849 * We must also lock the inode to remove it safely in case of
850 * error, as rmdir() would.
851 */
Al Viro59551022016-01-22 15:40:57 -0500852 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100853 configfs_adjust_dir_dirent_depth_before_populate(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800854 ret = populate_groups(to_config_group(item));
855 if (ret) {
856 configfs_detach_item(item);
David Howells2b0143b2015-03-17 22:25:59 +0000857 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400858 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800859 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100860 configfs_adjust_dir_dirent_depth_after_populate(sd);
Al Viro59551022016-01-22 15:40:57 -0500861 inode_unlock(d_inode(dentry));
Louis Rilling2e2ce172008-07-04 16:56:06 +0200862 if (ret)
863 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800864 }
865
866 return ret;
867}
868
Louis Rilling2e2ce172008-07-04 16:56:06 +0200869/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800870static void configfs_detach_group(struct config_item *item)
871{
872 detach_groups(to_config_group(item));
873 configfs_detach_item(item);
874}
875
876/*
Joel Becker299894c2006-10-06 17:33:23 -0700877 * After the item has been detached from the filesystem view, we are
878 * ready to tear it out of the hierarchy. Notify the client before
879 * we do that so they can perform any cleanup that requires
880 * navigating the hierarchy. A client does not need to provide this
881 * callback. The subsystem semaphore MUST be held by the caller, and
882 * references must be valid for both items. It also assumes the
883 * caller has validated ci_type.
884 */
885static void client_disconnect_notify(struct config_item *parent_item,
886 struct config_item *item)
887{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200888 const struct config_item_type *type;
Joel Becker299894c2006-10-06 17:33:23 -0700889
890 type = parent_item->ci_type;
891 BUG_ON(!type);
892
893 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
894 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
895 item);
896}
897
898/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800899 * Drop the initial reference from make_item()/make_group()
900 * This function assumes that reference is held on item
901 * and that item holds a valid reference to the parent. Also, it
902 * assumes the caller has validated ci_type.
903 */
904static void client_drop_item(struct config_item *parent_item,
905 struct config_item *item)
906{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200907 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800908
909 type = parent_item->ci_type;
910 BUG_ON(!type);
911
Joel Beckereed7a0d2006-04-11 21:37:20 -0700912 /*
913 * If ->drop_item() exists, it is responsible for the
914 * config_item_put().
915 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800916 if (type->ct_group_ops && type->ct_group_ops->drop_item)
917 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700918 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800919 else
920 config_item_put(item);
921}
922
Joel Becker631d1fe2007-06-18 18:06:09 -0700923#ifdef DEBUG
924static void configfs_dump_one(struct configfs_dirent *sd, int level)
925{
Fabian Frederickc6686932014-06-04 16:05:58 -0700926 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700927
Fabian Frederickc6686932014-06-04 16:05:58 -0700928#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700929 type_print(CONFIGFS_ROOT);
930 type_print(CONFIGFS_DIR);
931 type_print(CONFIGFS_ITEM_ATTR);
932 type_print(CONFIGFS_ITEM_LINK);
933 type_print(CONFIGFS_USET_DIR);
934 type_print(CONFIGFS_USET_DEFAULT);
935 type_print(CONFIGFS_USET_DROPPING);
936#undef type_print
937}
938
939static int configfs_dump(struct configfs_dirent *sd, int level)
940{
941 struct configfs_dirent *child_sd;
942 int ret = 0;
943
944 configfs_dump_one(sd, level);
945
946 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
947 return 0;
948
949 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
950 ret = configfs_dump(child_sd, level + 2);
951 if (ret)
952 break;
953 }
954
955 return ret;
956}
957#endif
958
959
960/*
961 * configfs_depend_item() and configfs_undepend_item()
962 *
963 * WARNING: Do not call these from a configfs callback!
964 *
965 * This describes these functions and their helpers.
966 *
967 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300968 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -0700969 * it. The idea is to give client modules as simple an interface as
970 * possible. When a system asks them to depend on an item, they just
971 * call configfs_depend_item(). If the item is live and the client
972 * driver is in good shape, we'll happily do the work for them.
973 *
974 * Why is the locking complex? Because configfs uses the VFS to handle
975 * all locking, but this function is called outside the normal
976 * VFS->configfs path. So it must take VFS locks to prevent the
977 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
978 * why you can't call these functions underneath configfs callbacks.
979 *
980 * Note, btw, that this can be called at *any* time, even when a configfs
981 * subsystem isn't registered, or when configfs is loading or unloading.
982 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +0100983 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
984 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -0700985 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +0100986 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
987 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -0700988 */
989
990/*
991 * configfs_depend_prep()
992 *
993 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
994 * attributes. This is similar but not the same to configfs_detach_prep().
995 * Note that configfs_detach_prep() expects the parent to be locked when it
996 * is called, but we lock the parent *inside* configfs_depend_prep(). We
997 * do that so we can unlock it if we find nothing.
998 *
999 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001000 * our object.
1001 * We deliberately ignore items tagged as dropping since they are virtually
1002 * dead, as well as items in the middle of attachment since they virtually
1003 * do not exist yet. This completes the locking out of racing mkdir() and
1004 * rmdir().
1005 * Note: subdirectories in the middle of attachment start with s_type =
1006 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1007 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1008 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001009 *
Louis Rilling420118c2009-01-28 19:18:33 +01001010 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001011 *
1012 * This adds a requirement that all config_items be unique!
1013 *
Louis Rilling420118c2009-01-28 19:18:33 +01001014 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001015 * much on the stack, though, so folks that need this function - be careful
1016 * about your stack! Patches will be accepted to make it iterative.
1017 */
1018static int configfs_depend_prep(struct dentry *origin,
1019 struct config_item *target)
1020{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001021 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001022 int ret = 0;
1023
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001024 BUG_ON(!origin || !origin->d_fsdata);
1025 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001026
Joel Becker631d1fe2007-06-18 18:06:09 -07001027 if (sd->s_element == target) /* Boo-yah */
1028 goto out;
1029
1030 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001031 if ((child_sd->s_type & CONFIGFS_DIR) &&
1032 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1033 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001034 ret = configfs_depend_prep(child_sd->s_dentry,
1035 target);
1036 if (!ret)
1037 goto out; /* Child path boo-yah */
1038 }
1039 }
1040
1041 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001042 ret = -ENOENT;
1043
1044out:
1045 return ret;
1046}
1047
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001048static int configfs_do_depend_item(struct dentry *subsys_dentry,
1049 struct config_item *target)
1050{
1051 struct configfs_dirent *p;
1052 int ret;
1053
1054 spin_lock(&configfs_dirent_lock);
1055 /* Scan the tree, return 0 if found */
1056 ret = configfs_depend_prep(subsys_dentry, target);
1057 if (ret)
1058 goto out_unlock_dirent_lock;
1059
1060 /*
1061 * We are sure that the item is not about to be removed by rmdir(), and
1062 * not in the middle of attachment by mkdir().
1063 */
1064 p = target->ci_dentry->d_fsdata;
1065 p->s_dependent_count += 1;
1066
1067out_unlock_dirent_lock:
1068 spin_unlock(&configfs_dirent_lock);
1069
1070 return ret;
1071}
1072
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001073static inline struct configfs_dirent *
1074configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1075 struct config_item *subsys_item)
1076{
1077 struct configfs_dirent *p;
1078 struct configfs_dirent *ret = NULL;
1079
1080 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1081 if (p->s_type & CONFIGFS_DIR &&
1082 p->s_element == subsys_item) {
1083 ret = p;
1084 break;
1085 }
1086 }
1087
1088 return ret;
1089}
1090
1091
Joel Becker631d1fe2007-06-18 18:06:09 -07001092int configfs_depend_item(struct configfs_subsystem *subsys,
1093 struct config_item *target)
1094{
1095 int ret;
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001096 struct configfs_dirent *subsys_sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001097 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001098 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001099
1100 /*
1101 * Pin the configfs filesystem. This means we can safely access
1102 * the root of the configfs filesystem.
1103 */
Al Viro2a152ad2012-03-17 16:53:29 -04001104 root = configfs_pin_fs();
1105 if (IS_ERR(root))
1106 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001107
1108 /*
1109 * Next, lock the root directory. We're going to check that the
1110 * subsystem is really registered, and so we need to lock out
1111 * configfs_[un]register_subsystem().
1112 */
Al Viro59551022016-01-22 15:40:57 -05001113 inode_lock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001114
Krzysztof Opasiak9a70adf2015-12-11 16:06:11 +01001115 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
Joel Becker631d1fe2007-06-18 18:06:09 -07001116 if (!subsys_sd) {
1117 ret = -ENOENT;
1118 goto out_unlock_fs;
1119 }
1120
1121 /* Ok, now we can trust subsys/s_item */
Krzysztof Opasiak9fb434e2015-12-11 16:06:10 +01001122 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
Joel Becker631d1fe2007-06-18 18:06:09 -07001123
Joel Becker631d1fe2007-06-18 18:06:09 -07001124out_unlock_fs:
Al Viro59551022016-01-22 15:40:57 -05001125 inode_unlock(d_inode(root));
Joel Becker631d1fe2007-06-18 18:06:09 -07001126
1127 /*
1128 * If we succeeded, the fs is pinned via other methods. If not,
1129 * we're done with it anyway. So release_fs() is always right.
1130 */
1131 configfs_release_fs();
1132
1133 return ret;
1134}
1135EXPORT_SYMBOL(configfs_depend_item);
1136
1137/*
1138 * Release the dependent linkage. This is much simpler than
1139 * configfs_depend_item() because we know that that the client driver is
1140 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1141 */
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +01001142void configfs_undepend_item(struct config_item *target)
Joel Becker631d1fe2007-06-18 18:06:09 -07001143{
1144 struct configfs_dirent *sd;
1145
1146 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001147 * Since we can trust everything is pinned, we just need
1148 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001149 */
Louis Rilling420118c2009-01-28 19:18:33 +01001150 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001151
1152 sd = target->ci_dentry->d_fsdata;
1153 BUG_ON(sd->s_dependent_count < 1);
1154
1155 sd->s_dependent_count -= 1;
1156
1157 /*
1158 * After this unlock, we cannot trust the item to stay alive!
1159 * DO NOT REFERENCE item after this unlock.
1160 */
Louis Rilling420118c2009-01-28 19:18:33 +01001161 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001162}
1163EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001164
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001165/*
1166 * caller_subsys is a caller's subsystem not target's. This is used to
1167 * determine if we should lock root and check subsys or not. When we are
1168 * in the same subsystem as our target there is no need to do locking as
1169 * we know that subsys is valid and is not unregistered during this function
1170 * as we are called from callback of one of his children and VFS holds a lock
1171 * on some inode. Otherwise we have to lock our root to ensure that target's
1172 * subsystem it is not unregistered during this function.
1173 */
1174int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1175 struct config_item *target)
1176{
1177 struct configfs_subsystem *target_subsys;
1178 struct config_group *root, *parent;
1179 struct configfs_dirent *subsys_sd;
1180 int ret = -ENOENT;
1181
1182 /* Disallow this function for configfs root */
1183 if (configfs_is_root(target))
1184 return -EINVAL;
1185
1186 parent = target->ci_group;
1187 /*
1188 * This may happen when someone is trying to depend root
1189 * directory of some subsystem
1190 */
1191 if (configfs_is_root(&parent->cg_item)) {
1192 target_subsys = to_configfs_subsystem(to_config_group(target));
1193 root = parent;
1194 } else {
1195 target_subsys = parent->cg_subsys;
1196 /* Find a cofnigfs root as we may need it for locking */
1197 for (root = parent; !configfs_is_root(&root->cg_item);
1198 root = root->cg_item.ci_group)
1199 ;
1200 }
1201
1202 if (target_subsys != caller_subsys) {
1203 /*
1204 * We are in other configfs subsystem, so we have to do
1205 * additional locking to prevent other subsystem from being
1206 * unregistered
1207 */
Al Viro59551022016-01-22 15:40:57 -05001208 inode_lock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001209
1210 /*
1211 * As we are trying to depend item from other subsystem
1212 * we have to check if this subsystem is still registered
1213 */
1214 subsys_sd = configfs_find_subsys_dentry(
1215 root->cg_item.ci_dentry->d_fsdata,
1216 &target_subsys->su_group.cg_item);
1217 if (!subsys_sd)
1218 goto out_root_unlock;
1219 } else {
1220 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1221 }
1222
1223 /* Now we can execute core of depend item */
1224 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1225
1226 if (target_subsys != caller_subsys)
1227out_root_unlock:
1228 /*
1229 * We were called from subsystem other than our target so we
1230 * took some locks so now it's time to release them
1231 */
Al Viro59551022016-01-22 15:40:57 -05001232 inode_unlock(d_inode(root->cg_item.ci_dentry));
Krzysztof Opasiakd79d75b2015-12-11 16:06:12 +01001233
1234 return ret;
1235}
1236EXPORT_SYMBOL(configfs_depend_item_unlocked);
1237
Al Viro18bb1db2011-07-26 01:41:39 -04001238static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001239{
Joel Beckera6795e92008-07-17 15:21:29 -07001240 int ret = 0;
1241 int module_got = 0;
1242 struct config_group *group = NULL;
1243 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001244 struct config_item *parent_item;
1245 struct configfs_subsystem *subsys;
1246 struct configfs_dirent *sd;
Bhumika Goyalaa293582017-10-16 17:18:40 +02001247 const struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001248 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001249 char *name;
1250
Joel Becker7063fbf2005-12-15 14:29:43 -08001251 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001252
1253 /*
1254 * Fake invisibility if dir belongs to a group/default groups hierarchy
1255 * being attached
1256 */
1257 if (!configfs_dirent_is_ready(sd)) {
1258 ret = -ENOENT;
1259 goto out;
1260 }
1261
Joel Becker84efad12006-03-27 18:46:09 -08001262 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1263 ret = -EPERM;
1264 goto out;
1265 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001266
Joel Becker84efad12006-03-27 18:46:09 -08001267 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001268 parent_item = configfs_get_config_item(dentry->d_parent);
1269 type = parent_item->ci_type;
1270 subsys = to_config_group(parent_item)->cg_subsys;
1271 BUG_ON(!subsys);
1272
1273 if (!type || !type->ct_group_ops ||
1274 (!type->ct_group_ops->make_group &&
1275 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001276 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1277 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001278 }
1279
Joel Becker70526b62008-06-17 15:34:32 -07001280 /*
1281 * The subsystem may belong to a different module than the item
1282 * being created. We don't want to safely pin the new item but
1283 * fail to pin the subsystem it sits under.
1284 */
1285 if (!subsys->su_group.cg_item.ci_type) {
1286 ret = -EINVAL;
1287 goto out_put;
1288 }
1289 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1290 if (!try_module_get(subsys_owner)) {
1291 ret = -EINVAL;
1292 goto out_put;
1293 }
1294
Joel Becker7063fbf2005-12-15 14:29:43 -08001295 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1296 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001297 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001298 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001299 }
Joel Becker84efad12006-03-27 18:46:09 -08001300
Joel Becker7063fbf2005-12-15 14:29:43 -08001301 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1302
Joel Beckere6bd07a2007-07-06 23:33:17 -07001303 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001304 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001305 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001306 if (!group)
1307 group = ERR_PTR(-ENOMEM);
1308 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001309 link_group(to_config_group(parent_item), group);
1310 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001311 } else
1312 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001313 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001314 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001315 if (!item)
1316 item = ERR_PTR(-ENOMEM);
1317 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001318 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001319 else
1320 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001321 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001322 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001323
1324 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001325 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001326 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001327 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001328 * There are no extra references to clean up.
1329 */
Joel Becker70526b62008-06-17 15:34:32 -07001330 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001331 }
1332
Joel Beckereed7a0d2006-04-11 21:37:20 -07001333 /*
1334 * link_obj() has been called (via link_group() for groups).
1335 * From here on out, errors must clean that up.
1336 */
1337
Joel Becker7063fbf2005-12-15 14:29:43 -08001338 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001339 if (!type) {
1340 ret = -EINVAL;
1341 goto out_unlink;
1342 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001343
Joel Becker70526b62008-06-17 15:34:32 -07001344 new_item_owner = type->ct_owner;
1345 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001346 ret = -EINVAL;
1347 goto out_unlink;
1348 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001349
Joel Beckereed7a0d2006-04-11 21:37:20 -07001350 /*
1351 * I hate doing it this way, but if there is
1352 * an error, module_put() probably should
1353 * happen after any cleanup.
1354 */
1355 module_got = 1;
1356
Louis Rilling6d8344b2008-06-16 19:01:02 +02001357 /*
1358 * Make racing rmdir() fail if it did not tag parent with
1359 * CONFIGFS_USET_DROPPING
1360 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1361 * fail and let rmdir() terminate correctly
1362 */
1363 spin_lock(&configfs_dirent_lock);
1364 /* This will make configfs_detach_prep() fail */
1365 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1366 spin_unlock(&configfs_dirent_lock);
1367
Joel Beckereed7a0d2006-04-11 21:37:20 -07001368 if (group)
1369 ret = configfs_attach_group(parent_item, item, dentry);
1370 else
1371 ret = configfs_attach_item(parent_item, item, dentry);
1372
Louis Rilling6d8344b2008-06-16 19:01:02 +02001373 spin_lock(&configfs_dirent_lock);
1374 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001375 if (!ret)
1376 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001377 spin_unlock(&configfs_dirent_lock);
1378
Joel Beckereed7a0d2006-04-11 21:37:20 -07001379out_unlink:
1380 if (ret) {
1381 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001382 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001383
1384 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001385 if (group)
1386 unlink_group(group);
1387 else
1388 unlink_obj(item);
1389 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001390
Joel Beckere6bd07a2007-07-06 23:33:17 -07001391 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001392
1393 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001394 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001395 }
1396
Joel Becker70526b62008-06-17 15:34:32 -07001397out_subsys_put:
1398 if (ret)
1399 module_put(subsys_owner);
1400
Joel Becker84efad12006-03-27 18:46:09 -08001401out_put:
1402 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001403 * link_obj()/link_group() took a reference from child->parent,
1404 * so the parent is safely pinned. We can drop our working
1405 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001406 */
1407 config_item_put(parent_item);
1408
1409out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001410 return ret;
1411}
1412
1413static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1414{
1415 struct config_item *parent_item;
1416 struct config_item *item;
1417 struct configfs_subsystem *subsys;
1418 struct configfs_dirent *sd;
Joel Becker70526b62008-06-17 15:34:32 -07001419 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001420 int ret;
1421
Joel Becker7063fbf2005-12-15 14:29:43 -08001422 sd = dentry->d_fsdata;
1423 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1424 return -EPERM;
1425
Joel Becker84efad12006-03-27 18:46:09 -08001426 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001427 parent_item = configfs_get_config_item(dentry->d_parent);
1428 subsys = to_config_group(parent_item)->cg_subsys;
1429 BUG_ON(!subsys);
1430
1431 if (!parent_item->ci_type) {
1432 config_item_put(parent_item);
1433 return -EINVAL;
1434 }
1435
Joel Becker70526b62008-06-17 15:34:32 -07001436 /* configfs_mkdir() shouldn't have allowed this */
1437 BUG_ON(!subsys->su_group.cg_item.ci_type);
1438 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1439
Louis Rilling9a73d782008-06-20 14:09:22 +02001440 /*
1441 * Ensure that no racing symlink() will make detach_prep() fail while
1442 * the new link is temporarily attached
1443 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001444 do {
Al Viro48f35b72016-04-12 00:37:59 -04001445 struct dentry *wait;
Louis Rilling6d8344b2008-06-16 19:01:02 +02001446
Louis Rillingde6bf182008-08-15 12:37:23 -07001447 mutex_lock(&configfs_symlink_mutex);
1448 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001449 /*
1450 * Here's where we check for dependents. We're protected by
1451 * configfs_dirent_lock.
1452 * If no dependent, atomically tag the item as dropping.
1453 */
1454 ret = sd->s_dependent_count ? -EBUSY : 0;
1455 if (!ret) {
Al Viro48f35b72016-04-12 00:37:59 -04001456 ret = configfs_detach_prep(dentry, &wait);
Louis Rilling420118c2009-01-28 19:18:33 +01001457 if (ret)
1458 configfs_detach_rollback(dentry);
1459 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001460 spin_unlock(&configfs_dirent_lock);
1461 mutex_unlock(&configfs_symlink_mutex);
1462
1463 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001464 if (ret != -EAGAIN) {
1465 config_item_put(parent_item);
1466 return ret;
1467 }
1468
1469 /* Wait until the racing operation terminates */
Al Viro48f35b72016-04-12 00:37:59 -04001470 inode_lock(d_inode(wait));
1471 inode_unlock(d_inode(wait));
1472 dput(wait);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001473 }
1474 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001475
Joel Becker84efad12006-03-27 18:46:09 -08001476 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001477 item = configfs_get_config_item(dentry);
1478
1479 /* Drop reference from above, item already holds one. */
1480 config_item_put(parent_item);
1481
1482 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001483 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001484
1485 if (sd->s_type & CONFIGFS_USET_DIR) {
1486 configfs_detach_group(item);
1487
Joel Beckere6bd07a2007-07-06 23:33:17 -07001488 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001489 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001490 unlink_group(to_config_group(item));
1491 } else {
1492 configfs_detach_item(item);
1493
Joel Beckere6bd07a2007-07-06 23:33:17 -07001494 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001495 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001496 unlink_obj(item);
1497 }
1498
1499 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001500 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001501
1502 /* Drop our reference from above */
1503 config_item_put(item);
1504
Joel Becker70526b62008-06-17 15:34:32 -07001505 module_put(dead_item_owner);
1506 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001507
1508 return 0;
1509}
1510
Arjan van de Ven754661f2007-02-12 00:55:38 -08001511const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001512 .mkdir = configfs_mkdir,
1513 .rmdir = configfs_rmdir,
1514 .symlink = configfs_symlink,
1515 .unlink = configfs_unlink,
1516 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001517 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001518};
1519
Al Viro81d44ed12012-03-17 16:13:25 -04001520const struct inode_operations configfs_root_inode_operations = {
1521 .lookup = configfs_lookup,
1522 .setattr = configfs_setattr,
1523};
1524
Joel Becker7063fbf2005-12-15 14:29:43 -08001525#if 0
1526int configfs_rename_dir(struct config_item * item, const char *new_name)
1527{
1528 int error = 0;
1529 struct dentry * new_dentry, * parent;
1530
1531 if (!strcmp(config_item_name(item), new_name))
1532 return -EINVAL;
1533
1534 if (!item->parent)
1535 return -EINVAL;
1536
1537 down_write(&configfs_rename_sem);
1538 parent = item->parent->dentry;
1539
Al Viro59551022016-01-22 15:40:57 -05001540 inode_lock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001541
1542 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1543 if (!IS_ERR(new_dentry)) {
David Howells2b0143b2015-03-17 22:25:59 +00001544 if (d_really_is_negative(new_dentry)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001545 error = config_item_set_name(item, "%s", new_name);
1546 if (!error) {
1547 d_add(new_dentry, NULL);
1548 d_move(item->dentry, new_dentry);
1549 }
1550 else
1551 d_delete(new_dentry);
1552 } else
1553 error = -EEXIST;
1554 dput(new_dentry);
1555 }
Al Viro59551022016-01-22 15:40:57 -05001556 inode_unlock(d_inode(parent));
Joel Becker7063fbf2005-12-15 14:29:43 -08001557 up_write(&configfs_rename_sem);
1558
1559 return error;
1560}
1561#endif
1562
1563static int configfs_dir_open(struct inode *inode, struct file *file)
1564{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001565 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001566 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001567 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001568
Al Viro59551022016-01-22 15:40:57 -05001569 inode_lock(d_inode(dentry));
Louis Rilling2a109f22008-07-04 16:56:05 +02001570 /*
1571 * Fake invisibility if dir belongs to a group/default groups hierarchy
1572 * being attached
1573 */
1574 err = -ENOENT;
1575 if (configfs_dirent_is_ready(parent_sd)) {
Louis Rilling420118c2009-01-28 19:18:33 +01001576 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
Louis Rilling2a109f22008-07-04 16:56:05 +02001577 if (IS_ERR(file->private_data))
1578 err = PTR_ERR(file->private_data);
1579 else
1580 err = 0;
1581 }
Al Viro59551022016-01-22 15:40:57 -05001582 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001583
Louis Rilling2a109f22008-07-04 16:56:05 +02001584 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001585}
1586
1587static int configfs_dir_close(struct inode *inode, struct file *file)
1588{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001589 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001590 struct configfs_dirent * cursor = file->private_data;
1591
Al Viro59551022016-01-22 15:40:57 -05001592 inode_lock(d_inode(dentry));
Louis Rilling6f610762008-06-16 19:00:58 +02001593 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001594 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001595 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001596 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001597
1598 release_configfs_dirent(cursor);
1599
1600 return 0;
1601}
1602
1603/* Relationship between s_mode and the DT_xxx types */
1604static inline unsigned char dt_type(struct configfs_dirent *sd)
1605{
1606 return (sd->s_mode >> 12) & 15;
1607}
1608
Al Viro52018852013-05-16 01:28:34 -04001609static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001610{
Al Viro52018852013-05-16 01:28:34 -04001611 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001612 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001613 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001614 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001615 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001616 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001617
Al Viro52018852013-05-16 01:28:34 -04001618 if (!dir_emit_dots(file, ctx))
1619 return 0;
Al Viroa01b3002016-04-20 19:50:05 -04001620 spin_lock(&configfs_dirent_lock);
1621 if (ctx->pos == 2)
Al Viro52018852013-05-16 01:28:34 -04001622 list_move(q, &parent_sd->s_children);
Al Viro52018852013-05-16 01:28:34 -04001623 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1624 struct configfs_dirent *next;
1625 const char *name;
1626 int len;
1627 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001628
Al Viro52018852013-05-16 01:28:34 -04001629 next = list_entry(p, struct configfs_dirent, s_sibling);
1630 if (!next->s_element)
1631 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001632
Al Viro52018852013-05-16 01:28:34 -04001633 /*
1634 * We'll have a dentry and an inode for
1635 * PINNED items and for open attribute
1636 * files. We lock here to prevent a race
1637 * with configfs_d_iput() clearing
1638 * s_dentry before calling iput().
1639 *
1640 * Why do we go to the trouble? If
1641 * someone has an attribute file open,
1642 * the inode number should match until
1643 * they close it. Beyond that, we don't
1644 * care.
1645 */
Al Viro52018852013-05-16 01:28:34 -04001646 dentry = next->s_dentry;
1647 if (dentry)
David Howells2b0143b2015-03-17 22:25:59 +00001648 inode = d_inode(dentry);
Al Viro52018852013-05-16 01:28:34 -04001649 if (inode)
1650 ino = inode->i_ino;
1651 spin_unlock(&configfs_dirent_lock);
1652 if (!inode)
1653 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001654
Al Viroa01b3002016-04-20 19:50:05 -04001655 name = configfs_get_name(next);
1656 len = strlen(name);
1657
Al Viro52018852013-05-16 01:28:34 -04001658 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1659 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001660
Al Viro52018852013-05-16 01:28:34 -04001661 spin_lock(&configfs_dirent_lock);
1662 list_move(q, p);
Al Viro52018852013-05-16 01:28:34 -04001663 p = q;
1664 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001665 }
Al Viroa01b3002016-04-20 19:50:05 -04001666 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001667 return 0;
1668}
1669
Andrew Morton965c8e52012-12-17 15:59:39 -08001670static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001671{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001672 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001673
Andrew Morton965c8e52012-12-17 15:59:39 -08001674 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001675 case 1:
1676 offset += file->f_pos;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001677 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001678 case 0:
1679 if (offset >= 0)
1680 break;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06001681 /* fall through */
Joel Becker7063fbf2005-12-15 14:29:43 -08001682 default:
Joel Becker7063fbf2005-12-15 14:29:43 -08001683 return -EINVAL;
1684 }
1685 if (offset != file->f_pos) {
1686 file->f_pos = offset;
1687 if (file->f_pos >= 2) {
1688 struct configfs_dirent *sd = dentry->d_fsdata;
1689 struct configfs_dirent *cursor = file->private_data;
1690 struct list_head *p;
1691 loff_t n = file->f_pos - 2;
1692
Louis Rilling6f610762008-06-16 19:00:58 +02001693 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001694 list_del(&cursor->s_sibling);
1695 p = sd->s_children.next;
1696 while (n && p != &sd->s_children) {
1697 struct configfs_dirent *next;
1698 next = list_entry(p, struct configfs_dirent,
1699 s_sibling);
1700 if (next->s_element)
1701 n--;
1702 p = p->next;
1703 }
1704 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001705 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001706 }
1707 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001708 return offset;
1709}
1710
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001711const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001712 .open = configfs_dir_open,
1713 .release = configfs_dir_close,
1714 .llseek = configfs_dir_lseek,
1715 .read = generic_read_dir,
Al Viroa01b3002016-04-20 19:50:05 -04001716 .iterate_shared = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001717};
1718
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001719/**
1720 * configfs_register_group - creates a parent-child relation between two groups
1721 * @parent_group: parent group
1722 * @group: child group
1723 *
1724 * link groups, creates dentry for the child and attaches it to the
1725 * parent dentry.
1726 *
1727 * Return: 0 on success, negative errno code on error
1728 */
1729int configfs_register_group(struct config_group *parent_group,
1730 struct config_group *group)
1731{
1732 struct configfs_subsystem *subsys = parent_group->cg_subsys;
1733 struct dentry *parent;
1734 int ret;
1735
1736 mutex_lock(&subsys->su_mutex);
1737 link_group(parent_group, group);
1738 mutex_unlock(&subsys->su_mutex);
1739
1740 parent = parent_group->cg_item.ci_dentry;
1741
Al Viro59551022016-01-22 15:40:57 -05001742 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001743 ret = create_default_group(parent_group, group);
YueHaibing35399f82019-05-05 11:03:12 +08001744 if (ret)
1745 goto err_out;
1746
1747 spin_lock(&configfs_dirent_lock);
1748 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1749 spin_unlock(&configfs_dirent_lock);
Al Viro59551022016-01-22 15:40:57 -05001750 inode_unlock(d_inode(parent));
YueHaibing35399f82019-05-05 11:03:12 +08001751 return 0;
1752err_out:
1753 inode_unlock(d_inode(parent));
1754 mutex_lock(&subsys->su_mutex);
1755 unlink_group(group);
1756 mutex_unlock(&subsys->su_mutex);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001757 return ret;
1758}
1759EXPORT_SYMBOL(configfs_register_group);
1760
1761/**
1762 * configfs_unregister_group() - unregisters a child group from its parent
1763 * @group: parent group to be unregistered
1764 *
1765 * Undoes configfs_register_group()
1766 */
1767void configfs_unregister_group(struct config_group *group)
1768{
1769 struct configfs_subsystem *subsys = group->cg_subsys;
1770 struct dentry *dentry = group->cg_item.ci_dentry;
1771 struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1772
Mike Christiecc57c072018-07-15 18:16:17 -05001773 mutex_lock(&subsys->su_mutex);
1774 if (!group->cg_item.ci_parent->ci_group) {
1775 /*
1776 * The parent has already been unlinked and detached
1777 * due to a rmdir.
1778 */
1779 goto unlink_group;
1780 }
1781 mutex_unlock(&subsys->su_mutex);
1782
Al Viro59551022016-01-22 15:40:57 -05001783 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001784 spin_lock(&configfs_dirent_lock);
1785 configfs_detach_prep(dentry, NULL);
1786 spin_unlock(&configfs_dirent_lock);
1787
1788 configfs_detach_group(&group->cg_item);
1789 d_inode(dentry)->i_flags |= S_DEAD;
1790 dont_mount(dentry);
1791 d_delete(dentry);
Al Viro59551022016-01-22 15:40:57 -05001792 inode_unlock(d_inode(parent));
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001793
1794 dput(dentry);
1795
1796 mutex_lock(&subsys->su_mutex);
Mike Christiecc57c072018-07-15 18:16:17 -05001797unlink_group:
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001798 unlink_group(group);
1799 mutex_unlock(&subsys->su_mutex);
1800}
1801EXPORT_SYMBOL(configfs_unregister_group);
1802
1803/**
1804 * configfs_register_default_group() - allocates and registers a child group
1805 * @parent_group: parent group
1806 * @name: child group name
1807 * @item_type: child item type description
1808 *
1809 * boilerplate to allocate and register a child group with its parent. We need
1810 * kzalloc'ed memory because child's default_group is initially empty.
1811 *
1812 * Return: allocated config group or ERR_PTR() on error
1813 */
1814struct config_group *
1815configfs_register_default_group(struct config_group *parent_group,
1816 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +02001817 const struct config_item_type *item_type)
Daniel Baluta5cf6a512015-11-20 15:56:53 -08001818{
1819 int ret;
1820 struct config_group *group;
1821
1822 group = kzalloc(sizeof(*group), GFP_KERNEL);
1823 if (!group)
1824 return ERR_PTR(-ENOMEM);
1825 config_group_init_type_name(group, name, item_type);
1826
1827 ret = configfs_register_group(parent_group, group);
1828 if (ret) {
1829 kfree(group);
1830 return ERR_PTR(ret);
1831 }
1832 return group;
1833}
1834EXPORT_SYMBOL(configfs_register_default_group);
1835
1836/**
1837 * configfs_unregister_default_group() - unregisters and frees a child group
1838 * @group: the group to act on
1839 */
1840void configfs_unregister_default_group(struct config_group *group)
1841{
1842 configfs_unregister_group(group);
1843 kfree(group);
1844}
1845EXPORT_SYMBOL(configfs_unregister_default_group);
1846
Joel Becker7063fbf2005-12-15 14:29:43 -08001847int configfs_register_subsystem(struct configfs_subsystem *subsys)
1848{
1849 int err;
1850 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001851 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001852 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001853 struct configfs_dirent *sd;
1854
Al Viro2a152ad2012-03-17 16:53:29 -04001855 root = configfs_pin_fs();
1856 if (IS_ERR(root))
1857 return PTR_ERR(root);
Joel Becker7063fbf2005-12-15 14:29:43 -08001858
1859 if (!group->cg_item.ci_name)
1860 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1861
Al Virob7c177f2012-03-17 16:24:54 -04001862 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001863 link_group(to_config_group(sd->s_element), group);
1864
Al Viro59551022016-01-22 15:40:57 -05001865 inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001866
Joel Becker7063fbf2005-12-15 14:29:43 -08001867 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001868 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001869 if (dentry) {
1870 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001871
Joel Beckerafdf04e2007-03-05 15:49:49 -08001872 err = configfs_attach_group(sd->s_element, &group->cg_item,
1873 dentry);
1874 if (err) {
David Howells2b0143b2015-03-17 22:25:59 +00001875 BUG_ON(d_inode(dentry));
Joel Beckerdf7f9962011-02-22 01:09:49 -08001876 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001877 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001878 } else {
1879 spin_lock(&configfs_dirent_lock);
1880 configfs_dir_set_ready(dentry->d_fsdata);
1881 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001882 }
1883 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001884
Al Viro59551022016-01-22 15:40:57 -05001885 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001886
Joel Beckerafdf04e2007-03-05 15:49:49 -08001887 if (err) {
1888 unlink_group(group);
1889 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001890 }
1891
1892 return err;
1893}
1894
1895void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1896{
1897 struct config_group *group = &subsys->su_group;
1898 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001899 struct dentry *root = dentry->d_sb->s_root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001900
Al Virob7c177f2012-03-17 16:24:54 -04001901 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001902 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001903 return;
1904 }
1905
Al Viro59551022016-01-22 15:40:57 -05001906 inode_lock_nested(d_inode(root),
Mark Fasheh55ed1602006-10-20 14:55:54 -07001907 I_MUTEX_PARENT);
Al Viro59551022016-01-22 15:40:57 -05001908 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001909 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001910 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001911 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001912 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001913 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001914 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001915 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001916 configfs_detach_group(&group->cg_item);
David Howells2b0143b2015-03-17 22:25:59 +00001917 d_inode(dentry)->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001918 dont_mount(dentry);
Al Viro59551022016-01-22 15:40:57 -05001919 inode_unlock(d_inode(dentry));
Joel Becker7063fbf2005-12-15 14:29:43 -08001920
1921 d_delete(dentry);
1922
Al Viro59551022016-01-22 15:40:57 -05001923 inode_unlock(d_inode(root));
Joel Becker7063fbf2005-12-15 14:29:43 -08001924
1925 dput(dentry);
1926
1927 unlink_group(group);
1928 configfs_release_fs();
1929}
1930
1931EXPORT_SYMBOL(configfs_register_subsystem);
1932EXPORT_SYMBOL(configfs_unregister_subsystem);