blob: 6371ba19ee43f5e737d06d38f563c08440b3e0ea [file] [log] [blame]
Joel Becker7063fbf2005-12-15 14:29:43 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dir.c - Operations for configfs directories.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
20 *
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
23 *
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
25 */
26
27#undef DEBUG
28
29#include <linux/fs.h>
30#include <linux/mount.h>
31#include <linux/module.h>
32#include <linux/slab.h>
Louis Rilling107ed402008-06-16 19:01:00 +020033#include <linux/err.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080034
35#include <linux/configfs.h>
36#include "configfs_internal.h"
37
38DECLARE_RWSEM(configfs_rename_sem);
Louis Rilling6f610762008-06-16 19:00:58 +020039/*
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
Louis Rilling5301a772008-06-16 19:00:59 +020041 * Also protects mutations of symlinks linkage to target configfs_dirent
Louis Rilling6f610762008-06-16 19:00:58 +020042 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
Louis Rilling5301a772008-06-16 19:00:59 +020044 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
Louis Rillingb3e76af2008-06-16 19:01:01 +020046 *
47 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
48 * unlocked is not reliable unless in detach_groups() called from
49 * rmdir()/unregister() and from configfs_attach_group()
Louis Rilling6f610762008-06-16 19:00:58 +020050 */
51DEFINE_SPINLOCK(configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080052
53static void configfs_d_iput(struct dentry * dentry,
54 struct inode * inode)
55{
Joel Becker24307aa2011-05-18 04:08:16 -070056 struct configfs_dirent *sd = dentry->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -080057
58 if (sd) {
Joel Becker24307aa2011-05-18 04:08:16 -070059 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock);
Junxiao Bi76ae2812013-11-21 14:31:56 -080061 /* Coordinate with configfs_attach_attr where will increase
62 * sd->s_count and update sd->s_dentry to new allocated one.
63 * Only set sd->dentry to null when this dentry is the only
64 * sd owner.
65 * If not do so, configfs_d_iput may run just after
66 * configfs_attach_attr and set sd->s_dentry to null
67 * even it's still in use.
68 */
69 if (atomic_read(&sd->s_count) <= 2)
70 sd->s_dentry = NULL;
71
Joel Becker24307aa2011-05-18 04:08:16 -070072 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080073 configfs_put(sd);
74 }
75 iput(inode);
76}
77
Al Virod463a0c2011-01-12 16:41:05 -050078const struct dentry_operations configfs_dentry_ops = {
Joel Becker7063fbf2005-12-15 14:29:43 -080079 .d_iput = configfs_d_iput,
Al Virob26d4cd2013-10-25 18:47:37 -040080 .d_delete = always_delete_dentry,
Joel Becker7063fbf2005-12-15 14:29:43 -080081};
82
Louis Rillinge74cc062009-01-28 19:18:32 +010083#ifdef CONFIG_LOCKDEP
84
85/*
86 * Helpers to make lockdep happy with our recursive locking of default groups'
87 * inodes (see configfs_attach_group() and configfs_detach_group()).
88 * We put default groups i_mutexes in separate classes according to their depth
89 * from the youngest non-default group ancestor.
90 *
91 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
92 * groups A/B and A/C will have their inode's mutex in class
93 * default_group_class[0], and default group A/C/D will be in
94 * default_group_class[1].
95 *
96 * The lock classes are declared and assigned in inode.c, according to the
97 * s_depth value.
98 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
99 * default groups, and reset to -1 when all default groups are attached. During
100 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
101 * inode's mutex is set to default_group_class[s_depth - 1].
102 */
103
104static void configfs_init_dirent_depth(struct configfs_dirent *sd)
105{
106 sd->s_depth = -1;
107}
108
109static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
110 struct configfs_dirent *sd)
111{
112 int parent_depth = parent_sd->s_depth;
113
114 if (parent_depth >= 0)
115 sd->s_depth = parent_depth + 1;
116}
117
118static void
119configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
120{
121 /*
122 * item's i_mutex class is already setup, so s_depth is now only
123 * used to set new sub-directories s_depth, which is always done
124 * with item's i_mutex locked.
125 */
126 /*
127 * sd->s_depth == -1 iff we are a non default group.
128 * else (we are a default group) sd->s_depth > 0 (see
129 * create_dir()).
130 */
131 if (sd->s_depth == -1)
132 /*
133 * We are a non default group and we are going to create
134 * default groups.
135 */
136 sd->s_depth = 0;
137}
138
139static void
140configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
141{
142 /* We will not create default groups anymore. */
143 sd->s_depth = -1;
144}
145
146#else /* CONFIG_LOCKDEP */
147
148static void configfs_init_dirent_depth(struct configfs_dirent *sd)
149{
150}
151
152static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
153 struct configfs_dirent *sd)
154{
155}
156
157static void
158configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
159{
160}
161
162static void
163configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
164{
165}
166
167#endif /* CONFIG_LOCKDEP */
168
Joel Becker7063fbf2005-12-15 14:29:43 -0800169/*
170 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
171 */
Louis Rilling420118c2009-01-28 19:18:33 +0100172static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
173 void *element, int type)
Joel Becker7063fbf2005-12-15 14:29:43 -0800174{
175 struct configfs_dirent * sd;
176
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800177 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -0800178 if (!sd)
Louis Rilling107ed402008-06-16 19:01:00 +0200179 return ERR_PTR(-ENOMEM);
Joel Becker7063fbf2005-12-15 14:29:43 -0800180
Joel Becker7063fbf2005-12-15 14:29:43 -0800181 atomic_set(&sd->s_count, 1);
182 INIT_LIST_HEAD(&sd->s_links);
183 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -0800184 sd->s_element = element;
Louis Rilling420118c2009-01-28 19:18:33 +0100185 sd->s_type = type;
Louis Rillinge74cc062009-01-28 19:18:32 +0100186 configfs_init_dirent_depth(sd);
Louis Rilling6f610762008-06-16 19:00:58 +0200187 spin_lock(&configfs_dirent_lock);
Louis Rillingb3e76af2008-06-16 19:01:01 +0200188 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
189 spin_unlock(&configfs_dirent_lock);
190 kmem_cache_free(configfs_dir_cachep, sd);
191 return ERR_PTR(-ENOENT);
192 }
Louis Rilling6f610762008-06-16 19:00:58 +0200193 list_add(&sd->s_sibling, &parent_sd->s_children);
194 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800195
196 return sd;
197}
198
Joel Beckerb4c98f62006-09-13 11:01:19 -0700199/*
200 *
201 * Return -EEXIST if there is already a configfs element with the same
202 * name for the same parent.
203 *
204 * called with parent inode's i_mutex held
205 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100206static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
207 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700208{
209 struct configfs_dirent * sd;
210
211 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
212 if (sd->s_element) {
213 const unsigned char *existing = configfs_get_name(sd);
214 if (strcmp(existing, new))
215 continue;
216 else
217 return -EEXIST;
218 }
219 }
220
221 return 0;
222}
223
224
Joel Becker7063fbf2005-12-15 14:29:43 -0800225int configfs_make_dirent(struct configfs_dirent * parent_sd,
226 struct dentry * dentry, void * element,
227 umode_t mode, int type)
228{
229 struct configfs_dirent * sd;
230
Louis Rilling420118c2009-01-28 19:18:33 +0100231 sd = configfs_new_dirent(parent_sd, element, type);
Louis Rilling107ed402008-06-16 19:01:00 +0200232 if (IS_ERR(sd))
233 return PTR_ERR(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800234
235 sd->s_mode = mode;
Joel Becker7063fbf2005-12-15 14:29:43 -0800236 sd->s_dentry = dentry;
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100237 if (dentry)
Joel Becker7063fbf2005-12-15 14:29:43 -0800238 dentry->d_fsdata = configfs_get(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800239
240 return 0;
241}
242
Al Viroc88b1e72015-01-29 00:17:57 -0500243static void init_dir(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800244{
245 inode->i_op = &configfs_dir_inode_operations;
246 inode->i_fop = &configfs_dir_operations;
247
248 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700249 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800250}
251
Al Viroc88b1e72015-01-29 00:17:57 -0500252static void configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800253{
254 inode->i_size = PAGE_SIZE;
255 inode->i_fop = &configfs_file_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800256}
257
Al Viroc88b1e72015-01-29 00:17:57 -0500258static void init_symlink(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800259{
260 inode->i_op = &configfs_symlink_inode_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -0800261}
262
Al Viro0dd6c082012-03-17 16:49:20 -0400263static int create_dir(struct config_item *k, struct dentry *d)
Joel Becker7063fbf2005-12-15 14:29:43 -0800264{
265 int error;
266 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Al Viro0dd6c082012-03-17 16:49:20 -0400267 struct dentry *p = d->d_parent;
268
269 BUG_ON(!k);
Joel Becker7063fbf2005-12-15 14:29:43 -0800270
Joel Beckerb4c98f62006-09-13 11:01:19 -0700271 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
272 if (!error)
273 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
Louis Rilling2a109f22008-07-04 16:56:05 +0200274 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
Joel Becker7063fbf2005-12-15 14:29:43 -0800275 if (!error) {
Louis Rillinge74cc062009-01-28 19:18:32 +0100276 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800277 error = configfs_create(d, mode, init_dir);
Joel Becker7063fbf2005-12-15 14:29:43 -0800278 if (!error) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700279 inc_nlink(p->d_inode);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800280 } else {
281 struct configfs_dirent *sd = d->d_fsdata;
282 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200283 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800284 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200285 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800286 configfs_put(sd);
287 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800288 }
289 }
290 return error;
291}
292
293
294/**
295 * configfs_create_dir - create a directory for an config_item.
296 * @item: config_itemwe're creating directory for.
297 * @dentry: config_item's dentry.
Louis Rilling2a109f22008-07-04 16:56:05 +0200298 *
299 * Note: user-created entries won't be allowed under this new directory
300 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800301 */
302
303static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
304{
Al Viro0dd6c082012-03-17 16:49:20 -0400305 int error = create_dir(item, dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800306 if (!error)
307 item->ci_dentry = dentry;
308 return error;
309}
310
Louis Rilling2a109f22008-07-04 16:56:05 +0200311/*
312 * Allow userspace to create new entries under a new directory created with
313 * configfs_create_dir(), and under all of its chidlren directories recursively.
314 * @sd configfs_dirent of the new directory to validate
315 *
316 * Caller must hold configfs_dirent_lock.
317 */
318static void configfs_dir_set_ready(struct configfs_dirent *sd)
319{
320 struct configfs_dirent *child_sd;
321
322 sd->s_type &= ~CONFIGFS_USET_CREATING;
323 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
324 if (child_sd->s_type & CONFIGFS_USET_CREATING)
325 configfs_dir_set_ready(child_sd);
326}
327
328/*
329 * Check that a directory does not belong to a directory hierarchy being
330 * attached and not validated yet.
331 * @sd configfs_dirent of the directory to check
332 *
333 * @return non-zero iff the directory was validated
334 *
335 * Note: takes configfs_dirent_lock, so the result may change from false to true
336 * in two consecutive calls, but never from true to false.
337 */
338int configfs_dirent_is_ready(struct configfs_dirent *sd)
339{
340 int ret;
341
342 spin_lock(&configfs_dirent_lock);
343 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
344 spin_unlock(&configfs_dirent_lock);
345
346 return ret;
347}
348
Joel Becker7063fbf2005-12-15 14:29:43 -0800349int configfs_create_link(struct configfs_symlink *sl,
350 struct dentry *parent,
351 struct dentry *dentry)
352{
353 int err = 0;
354 umode_t mode = S_IFLNK | S_IRWXUGO;
355
Joel Becker3d0f89b2006-01-25 13:31:07 -0800356 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
357 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800358 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800359 err = configfs_create(dentry, mode, init_symlink);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100360 if (err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800361 struct configfs_dirent *sd = dentry->d_fsdata;
362 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200363 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800364 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200365 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800366 configfs_put(sd);
367 }
368 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800369 }
370 return err;
371}
372
373static void remove_dir(struct dentry * d)
374{
375 struct dentry * parent = dget(d->d_parent);
376 struct configfs_dirent * sd;
377
378 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200379 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800380 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200381 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800382 configfs_put(sd);
383 if (d->d_inode)
384 simple_rmdir(parent->d_inode,d);
385
Al Viroa4555892014-10-21 20:11:25 -0400386 pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800387
388 dput(parent);
389}
390
391/**
392 * configfs_remove_dir - remove an config_item's directory.
393 * @item: config_item we're removing.
394 *
395 * The only thing special about this is that we remove any files in
396 * the directory before we remove the directory, and we've inlined
397 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200398 *
399 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800400 */
401
402static void configfs_remove_dir(struct config_item * item)
403{
404 struct dentry * dentry = dget(item->ci_dentry);
405
406 if (!dentry)
407 return;
408
409 remove_dir(dentry);
410 /**
411 * Drop reference from dget() on entrance.
412 */
413 dput(dentry);
414}
415
416
417/* attaches attribute's configfs_dirent to the dentry corresponding to the
418 * attribute file
419 */
420static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
421{
422 struct configfs_attribute * attr = sd->s_element;
423 int error;
424
Junxiao Bi76ae2812013-11-21 14:31:56 -0800425 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800426 dentry->d_fsdata = configfs_get(sd);
427 sd->s_dentry = dentry;
Junxiao Bi76ae2812013-11-21 14:31:56 -0800428 spin_unlock(&configfs_dirent_lock);
429
Dave Hansence8d2cd2007-10-16 23:31:13 -0700430 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
431 configfs_init_file);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800432 if (error) {
433 configfs_put(sd);
434 return error;
435 }
436
Joel Becker7063fbf2005-12-15 14:29:43 -0800437 d_rehash(dentry);
438
439 return 0;
440}
441
442static struct dentry * configfs_lookup(struct inode *dir,
443 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400444 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800445{
446 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
447 struct configfs_dirent * sd;
448 int found = 0;
Louis Rilling2a109f22008-07-04 16:56:05 +0200449 int err;
450
451 /*
452 * Fake invisibility if dir belongs to a group/default groups hierarchy
453 * being attached
454 *
455 * This forbids userspace to read/write attributes of items which may
456 * not complete their initialization, since the dentries of the
457 * attributes won't be instantiated.
458 */
459 err = -ENOENT;
460 if (!configfs_dirent_is_ready(parent_sd))
461 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800462
463 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
464 if (sd->s_type & CONFIGFS_NOT_PINNED) {
465 const unsigned char * name = configfs_get_name(sd);
466
467 if (strcmp(name, dentry->d_name.name))
468 continue;
469
470 found = 1;
471 err = configfs_attach_attr(sd, dentry);
472 break;
473 }
474 }
475
476 if (!found) {
477 /*
478 * If it doesn't exist and it isn't a NOT_PINNED item,
479 * it must be negative.
480 */
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100481 if (dentry->d_name.len > NAME_MAX)
482 return ERR_PTR(-ENAMETOOLONG);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100483 d_add(dentry, NULL);
484 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800485 }
486
Louis Rilling2a109f22008-07-04 16:56:05 +0200487out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800488 return ERR_PTR(err);
489}
490
491/*
492 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200493 * attributes and are removed by rmdir(). We recurse, setting
494 * CONFIGFS_USET_DROPPING on all children that are candidates for
495 * default detach.
496 * If there is an error, the caller will reset the flags via
497 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800498 */
Louis Rilling6d8344b2008-06-16 19:01:02 +0200499static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
Joel Becker7063fbf2005-12-15 14:29:43 -0800500{
501 struct configfs_dirent *parent_sd = dentry->d_fsdata;
502 struct configfs_dirent *sd;
503 int ret;
504
Louis Rilling4768e9b2008-06-23 14:16:17 +0200505 /* Mark that we're trying to drop the group */
506 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
507
Joel Becker7063fbf2005-12-15 14:29:43 -0800508 ret = -EBUSY;
509 if (!list_empty(&parent_sd->s_links))
510 goto out;
511
512 ret = 0;
513 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200514 if (!sd->s_element ||
515 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800516 continue;
517 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200518 /* Abort if racing with mkdir() */
519 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
520 if (wait_mutex)
521 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
522 return -EAGAIN;
523 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800524
Joel Becker631d1fe2007-06-18 18:06:09 -0700525 /*
526 * Yup, recursive. If there's a problem, blame
527 * deep nesting of default_groups
528 */
Louis Rilling6d8344b2008-06-16 19:01:02 +0200529 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800530 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800531 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800532 } else
533 ret = -ENOTEMPTY;
534
535 break;
536 }
537
538out:
539 return ret;
540}
541
542/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200543 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800544 * set.
545 */
546static void configfs_detach_rollback(struct dentry *dentry)
547{
548 struct configfs_dirent *parent_sd = dentry->d_fsdata;
549 struct configfs_dirent *sd;
550
Louis Rilling4768e9b2008-06-23 14:16:17 +0200551 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
552
553 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
554 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800555 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800556}
557
558static void detach_attrs(struct config_item * item)
559{
560 struct dentry * dentry = dget(item->ci_dentry);
561 struct configfs_dirent * parent_sd;
562 struct configfs_dirent * sd, * tmp;
563
564 if (!dentry)
565 return;
566
567 pr_debug("configfs %s: dropping attrs for dir\n",
568 dentry->d_name.name);
569
570 parent_sd = dentry->d_fsdata;
571 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
572 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
573 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200574 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800575 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200576 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800577 configfs_drop_dentry(sd, dentry);
578 configfs_put(sd);
579 }
580
581 /**
582 * Drop reference from dget() on entrance.
583 */
584 dput(dentry);
585}
586
587static int populate_attrs(struct config_item *item)
588{
589 struct config_item_type *t = item->ci_type;
590 struct configfs_attribute *attr;
591 int error = 0;
592 int i;
593
594 if (!t)
595 return -EINVAL;
596 if (t->ct_attrs) {
597 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
598 if ((error = configfs_create_file(item, attr)))
599 break;
600 }
601 }
602
603 if (error)
604 detach_attrs(item);
605
606 return error;
607}
608
609static int configfs_attach_group(struct config_item *parent_item,
610 struct config_item *item,
611 struct dentry *dentry);
612static void configfs_detach_group(struct config_item *item);
613
614static void detach_groups(struct config_group *group)
615{
616 struct dentry * dentry = dget(group->cg_item.ci_dentry);
617 struct dentry *child;
618 struct configfs_dirent *parent_sd;
619 struct configfs_dirent *sd, *tmp;
620
621 if (!dentry)
622 return;
623
624 parent_sd = dentry->d_fsdata;
625 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
626 if (!sd->s_element ||
627 !(sd->s_type & CONFIGFS_USET_DEFAULT))
628 continue;
629
630 child = sd->s_dentry;
631
Louis Rillingb3e76af2008-06-16 19:01:01 +0200632 mutex_lock(&child->d_inode->i_mutex);
633
Joel Becker7063fbf2005-12-15 14:29:43 -0800634 configfs_detach_group(sd->s_element);
635 child->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400636 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800637
Louis Rillingb3e76af2008-06-16 19:01:01 +0200638 mutex_unlock(&child->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800639
640 d_delete(child);
641 dput(child);
642 }
643
644 /**
645 * Drop reference from dget() on entrance.
646 */
647 dput(dentry);
648}
649
650/*
651 * This fakes mkdir(2) on a default_groups[] entry. It
652 * creates a dentry, attachs it, and then does fixup
653 * on the sd->s_type.
654 *
655 * We could, perhaps, tweak our parent's ->mkdir for a minute and
656 * try using vfs_mkdir. Just a thought.
657 */
658static int create_default_group(struct config_group *parent_group,
659 struct config_group *group)
660{
661 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800662 struct configfs_dirent *sd;
663 /* We trust the caller holds a reference to parent */
664 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
665
666 if (!group->cg_item.ci_name)
667 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800668
669 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400670 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800671 if (child) {
672 d_add(child, NULL);
673
674 ret = configfs_attach_group(&parent_group->cg_item,
675 &group->cg_item, child);
676 if (!ret) {
677 sd = child->d_fsdata;
678 sd->s_type |= CONFIGFS_USET_DEFAULT;
679 } else {
Joel Beckerdf7f9962011-02-22 01:09:49 -0800680 BUG_ON(child->d_inode);
681 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800682 dput(child);
683 }
684 }
685
686 return ret;
687}
688
689static int populate_groups(struct config_group *group)
690{
691 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800692 int ret = 0;
693 int i;
694
Eric Sesterhenncbca6922006-03-23 00:36:54 +0100695 if (group->default_groups) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800696 for (i = 0; group->default_groups[i]; i++) {
697 new_group = group->default_groups[i];
698
699 ret = create_default_group(group, new_group);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200700 if (ret) {
701 detach_groups(group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800702 break;
Louis Rilling2e2ce172008-07-04 16:56:06 +0200703 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800704 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800705 }
706
Joel Becker7063fbf2005-12-15 14:29:43 -0800707 return ret;
708}
709
710/*
711 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700712 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800713 */
714
715static void unlink_obj(struct config_item *item)
716{
717 struct config_group *group;
718
719 group = item->ci_group;
720 if (group) {
721 list_del_init(&item->ci_entry);
722
723 item->ci_group = NULL;
724 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700725
726 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800727 config_item_put(item);
728
Joel Beckereed7a0d2006-04-11 21:37:20 -0700729 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800730 config_group_put(group);
731 }
732}
733
734static void link_obj(struct config_item *parent_item, struct config_item *item)
735{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700736 /*
737 * Parent seems redundant with group, but it makes certain
738 * traversals much nicer.
739 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800740 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700741
742 /*
743 * We hold a reference on the parent for the child's ci_parent
744 * link.
745 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800746 item->ci_group = config_group_get(to_config_group(parent_item));
747 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
748
Joel Beckereed7a0d2006-04-11 21:37:20 -0700749 /*
750 * We hold a reference on the child for ci_entry on the parent's
751 * cg_children
752 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800753 config_item_get(item);
754}
755
756static void unlink_group(struct config_group *group)
757{
758 int i;
759 struct config_group *new_group;
760
761 if (group->default_groups) {
762 for (i = 0; group->default_groups[i]; i++) {
763 new_group = group->default_groups[i];
764 unlink_group(new_group);
765 }
766 }
767
768 group->cg_subsys = NULL;
769 unlink_obj(&group->cg_item);
770}
771
772static void link_group(struct config_group *parent_group, struct config_group *group)
773{
774 int i;
775 struct config_group *new_group;
776 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
777
778 link_obj(&parent_group->cg_item, &group->cg_item);
779
780 if (parent_group->cg_subsys)
781 subsys = parent_group->cg_subsys;
782 else if (configfs_is_root(&parent_group->cg_item))
783 subsys = to_configfs_subsystem(group);
784 else
785 BUG();
786 group->cg_subsys = subsys;
787
788 if (group->default_groups) {
789 for (i = 0; group->default_groups[i]; i++) {
790 new_group = group->default_groups[i];
791 link_group(group, new_group);
792 }
793 }
794}
795
796/*
797 * The goal is that configfs_attach_item() (and
798 * configfs_attach_group()) can be called from either the VFS or this
799 * module. That is, they assume that the items have been created,
800 * the dentry allocated, and the dcache is all ready to go.
801 *
802 * If they fail, they must clean up after themselves as if they
803 * had never been called. The caller (VFS or local function) will
804 * handle cleaning up the dcache bits.
805 *
806 * configfs_detach_group() and configfs_detach_item() behave similarly on
807 * the way out. They assume that the proper semaphores are held, they
808 * clean up the configfs items, and they expect their callers will
809 * handle the dcache bits.
810 */
811static int configfs_attach_item(struct config_item *parent_item,
812 struct config_item *item,
813 struct dentry *dentry)
814{
815 int ret;
816
817 ret = configfs_create_dir(item, dentry);
818 if (!ret) {
819 ret = populate_attrs(item);
820 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200821 /*
822 * We are going to remove an inode and its dentry but
823 * the VFS may already have hit and used them. Thus,
824 * we must lock them as rmdir() would.
825 */
826 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800827 configfs_remove_dir(item);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200828 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400829 dont_mount(dentry);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200830 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800831 d_delete(dentry);
832 }
833 }
834
835 return ret;
836}
837
Louis Rilling2e2ce172008-07-04 16:56:06 +0200838/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800839static void configfs_detach_item(struct config_item *item)
840{
841 detach_attrs(item);
842 configfs_remove_dir(item);
843}
844
845static int configfs_attach_group(struct config_item *parent_item,
846 struct config_item *item,
847 struct dentry *dentry)
848{
849 int ret;
850 struct configfs_dirent *sd;
851
852 ret = configfs_attach_item(parent_item, item, dentry);
853 if (!ret) {
854 sd = dentry->d_fsdata;
855 sd->s_type |= CONFIGFS_USET_DIR;
856
Louis Rilling2e2ce172008-07-04 16:56:06 +0200857 /*
858 * FYI, we're faking mkdir in populate_groups()
859 * We must lock the group's inode to avoid races with the VFS
860 * which can already hit the inode and try to add/remove entries
861 * under it.
862 *
863 * We must also lock the inode to remove it safely in case of
864 * error, as rmdir() would.
865 */
866 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100867 configfs_adjust_dir_dirent_depth_before_populate(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800868 ret = populate_groups(to_config_group(item));
869 if (ret) {
870 configfs_detach_item(item);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200871 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400872 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800873 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100874 configfs_adjust_dir_dirent_depth_after_populate(sd);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200875 mutex_unlock(&dentry->d_inode->i_mutex);
876 if (ret)
877 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800878 }
879
880 return ret;
881}
882
Louis Rilling2e2ce172008-07-04 16:56:06 +0200883/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800884static void configfs_detach_group(struct config_item *item)
885{
886 detach_groups(to_config_group(item));
887 configfs_detach_item(item);
888}
889
890/*
Joel Becker299894c2006-10-06 17:33:23 -0700891 * After the item has been detached from the filesystem view, we are
892 * ready to tear it out of the hierarchy. Notify the client before
893 * we do that so they can perform any cleanup that requires
894 * navigating the hierarchy. A client does not need to provide this
895 * callback. The subsystem semaphore MUST be held by the caller, and
896 * references must be valid for both items. It also assumes the
897 * caller has validated ci_type.
898 */
899static void client_disconnect_notify(struct config_item *parent_item,
900 struct config_item *item)
901{
902 struct config_item_type *type;
903
904 type = parent_item->ci_type;
905 BUG_ON(!type);
906
907 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
908 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
909 item);
910}
911
912/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800913 * Drop the initial reference from make_item()/make_group()
914 * This function assumes that reference is held on item
915 * and that item holds a valid reference to the parent. Also, it
916 * assumes the caller has validated ci_type.
917 */
918static void client_drop_item(struct config_item *parent_item,
919 struct config_item *item)
920{
921 struct config_item_type *type;
922
923 type = parent_item->ci_type;
924 BUG_ON(!type);
925
Joel Beckereed7a0d2006-04-11 21:37:20 -0700926 /*
927 * If ->drop_item() exists, it is responsible for the
928 * config_item_put().
929 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800930 if (type->ct_group_ops && type->ct_group_ops->drop_item)
931 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700932 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800933 else
934 config_item_put(item);
935}
936
Joel Becker631d1fe2007-06-18 18:06:09 -0700937#ifdef DEBUG
938static void configfs_dump_one(struct configfs_dirent *sd, int level)
939{
Fabian Frederickc6686932014-06-04 16:05:58 -0700940 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700941
Fabian Frederickc6686932014-06-04 16:05:58 -0700942#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700943 type_print(CONFIGFS_ROOT);
944 type_print(CONFIGFS_DIR);
945 type_print(CONFIGFS_ITEM_ATTR);
946 type_print(CONFIGFS_ITEM_LINK);
947 type_print(CONFIGFS_USET_DIR);
948 type_print(CONFIGFS_USET_DEFAULT);
949 type_print(CONFIGFS_USET_DROPPING);
950#undef type_print
951}
952
953static int configfs_dump(struct configfs_dirent *sd, int level)
954{
955 struct configfs_dirent *child_sd;
956 int ret = 0;
957
958 configfs_dump_one(sd, level);
959
960 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
961 return 0;
962
963 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
964 ret = configfs_dump(child_sd, level + 2);
965 if (ret)
966 break;
967 }
968
969 return ret;
970}
971#endif
972
973
974/*
975 * configfs_depend_item() and configfs_undepend_item()
976 *
977 * WARNING: Do not call these from a configfs callback!
978 *
979 * This describes these functions and their helpers.
980 *
981 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300982 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -0700983 * it. The idea is to give client modules as simple an interface as
984 * possible. When a system asks them to depend on an item, they just
985 * call configfs_depend_item(). If the item is live and the client
986 * driver is in good shape, we'll happily do the work for them.
987 *
988 * Why is the locking complex? Because configfs uses the VFS to handle
989 * all locking, but this function is called outside the normal
990 * VFS->configfs path. So it must take VFS locks to prevent the
991 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
992 * why you can't call these functions underneath configfs callbacks.
993 *
994 * Note, btw, that this can be called at *any* time, even when a configfs
995 * subsystem isn't registered, or when configfs is loading or unloading.
996 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +0100997 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
998 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -0700999 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +01001000 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1001 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -07001002 */
1003
1004/*
1005 * configfs_depend_prep()
1006 *
1007 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1008 * attributes. This is similar but not the same to configfs_detach_prep().
1009 * Note that configfs_detach_prep() expects the parent to be locked when it
1010 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1011 * do that so we can unlock it if we find nothing.
1012 *
1013 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001014 * our object.
1015 * We deliberately ignore items tagged as dropping since they are virtually
1016 * dead, as well as items in the middle of attachment since they virtually
1017 * do not exist yet. This completes the locking out of racing mkdir() and
1018 * rmdir().
1019 * Note: subdirectories in the middle of attachment start with s_type =
1020 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1021 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1022 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001023 *
Louis Rilling420118c2009-01-28 19:18:33 +01001024 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001025 *
1026 * This adds a requirement that all config_items be unique!
1027 *
Louis Rilling420118c2009-01-28 19:18:33 +01001028 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001029 * much on the stack, though, so folks that need this function - be careful
1030 * about your stack! Patches will be accepted to make it iterative.
1031 */
1032static int configfs_depend_prep(struct dentry *origin,
1033 struct config_item *target)
1034{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001035 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001036 int ret = 0;
1037
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001038 BUG_ON(!origin || !origin->d_fsdata);
1039 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001040
Joel Becker631d1fe2007-06-18 18:06:09 -07001041 if (sd->s_element == target) /* Boo-yah */
1042 goto out;
1043
1044 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001045 if ((child_sd->s_type & CONFIGFS_DIR) &&
1046 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1047 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001048 ret = configfs_depend_prep(child_sd->s_dentry,
1049 target);
1050 if (!ret)
1051 goto out; /* Child path boo-yah */
1052 }
1053 }
1054
1055 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001056 ret = -ENOENT;
1057
1058out:
1059 return ret;
1060}
1061
Joel Becker631d1fe2007-06-18 18:06:09 -07001062int configfs_depend_item(struct configfs_subsystem *subsys,
1063 struct config_item *target)
1064{
1065 int ret;
1066 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1067 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001068 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001069
1070 /*
1071 * Pin the configfs filesystem. This means we can safely access
1072 * the root of the configfs filesystem.
1073 */
Al Viro2a152ad2012-03-17 16:53:29 -04001074 root = configfs_pin_fs();
1075 if (IS_ERR(root))
1076 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001077
1078 /*
1079 * Next, lock the root directory. We're going to check that the
1080 * subsystem is really registered, and so we need to lock out
1081 * configfs_[un]register_subsystem().
1082 */
Al Virob7c177f2012-03-17 16:24:54 -04001083 mutex_lock(&root->d_inode->i_mutex);
Joel Becker631d1fe2007-06-18 18:06:09 -07001084
Al Virob7c177f2012-03-17 16:24:54 -04001085 root_sd = root->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001086
1087 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1088 if (p->s_type & CONFIGFS_DIR) {
1089 if (p->s_element == s_item) {
1090 subsys_sd = p;
1091 break;
1092 }
1093 }
1094 }
1095
1096 if (!subsys_sd) {
1097 ret = -ENOENT;
1098 goto out_unlock_fs;
1099 }
1100
1101 /* Ok, now we can trust subsys/s_item */
1102
Louis Rilling420118c2009-01-28 19:18:33 +01001103 spin_lock(&configfs_dirent_lock);
1104 /* Scan the tree, return 0 if found */
Joel Becker631d1fe2007-06-18 18:06:09 -07001105 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1106 if (ret)
Louis Rilling420118c2009-01-28 19:18:33 +01001107 goto out_unlock_dirent_lock;
Joel Becker631d1fe2007-06-18 18:06:09 -07001108
Louis Rilling420118c2009-01-28 19:18:33 +01001109 /*
1110 * We are sure that the item is not about to be removed by rmdir(), and
1111 * not in the middle of attachment by mkdir().
1112 */
Joel Becker631d1fe2007-06-18 18:06:09 -07001113 p = target->ci_dentry->d_fsdata;
1114 p->s_dependent_count += 1;
1115
Louis Rilling420118c2009-01-28 19:18:33 +01001116out_unlock_dirent_lock:
1117 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001118out_unlock_fs:
Al Virob7c177f2012-03-17 16:24:54 -04001119 mutex_unlock(&root->d_inode->i_mutex);
Joel Becker631d1fe2007-06-18 18:06:09 -07001120
1121 /*
1122 * If we succeeded, the fs is pinned via other methods. If not,
1123 * we're done with it anyway. So release_fs() is always right.
1124 */
1125 configfs_release_fs();
1126
1127 return ret;
1128}
1129EXPORT_SYMBOL(configfs_depend_item);
1130
1131/*
1132 * Release the dependent linkage. This is much simpler than
1133 * configfs_depend_item() because we know that that the client driver is
1134 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1135 */
1136void configfs_undepend_item(struct configfs_subsystem *subsys,
1137 struct config_item *target)
1138{
1139 struct configfs_dirent *sd;
1140
1141 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001142 * Since we can trust everything is pinned, we just need
1143 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001144 */
Louis Rilling420118c2009-01-28 19:18:33 +01001145 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001146
1147 sd = target->ci_dentry->d_fsdata;
1148 BUG_ON(sd->s_dependent_count < 1);
1149
1150 sd->s_dependent_count -= 1;
1151
1152 /*
1153 * After this unlock, we cannot trust the item to stay alive!
1154 * DO NOT REFERENCE item after this unlock.
1155 */
Louis Rilling420118c2009-01-28 19:18:33 +01001156 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001157}
1158EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001159
Al Viro18bb1db2011-07-26 01:41:39 -04001160static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001161{
Joel Beckera6795e92008-07-17 15:21:29 -07001162 int ret = 0;
1163 int module_got = 0;
1164 struct config_group *group = NULL;
1165 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001166 struct config_item *parent_item;
1167 struct configfs_subsystem *subsys;
1168 struct configfs_dirent *sd;
1169 struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001170 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001171 char *name;
1172
Joel Becker7063fbf2005-12-15 14:29:43 -08001173 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001174
1175 /*
1176 * Fake invisibility if dir belongs to a group/default groups hierarchy
1177 * being attached
1178 */
1179 if (!configfs_dirent_is_ready(sd)) {
1180 ret = -ENOENT;
1181 goto out;
1182 }
1183
Joel Becker84efad12006-03-27 18:46:09 -08001184 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1185 ret = -EPERM;
1186 goto out;
1187 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001188
Joel Becker84efad12006-03-27 18:46:09 -08001189 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001190 parent_item = configfs_get_config_item(dentry->d_parent);
1191 type = parent_item->ci_type;
1192 subsys = to_config_group(parent_item)->cg_subsys;
1193 BUG_ON(!subsys);
1194
1195 if (!type || !type->ct_group_ops ||
1196 (!type->ct_group_ops->make_group &&
1197 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001198 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1199 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001200 }
1201
Joel Becker70526b62008-06-17 15:34:32 -07001202 /*
1203 * The subsystem may belong to a different module than the item
1204 * being created. We don't want to safely pin the new item but
1205 * fail to pin the subsystem it sits under.
1206 */
1207 if (!subsys->su_group.cg_item.ci_type) {
1208 ret = -EINVAL;
1209 goto out_put;
1210 }
1211 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1212 if (!try_module_get(subsys_owner)) {
1213 ret = -EINVAL;
1214 goto out_put;
1215 }
1216
Joel Becker7063fbf2005-12-15 14:29:43 -08001217 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1218 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001219 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001220 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001221 }
Joel Becker84efad12006-03-27 18:46:09 -08001222
Joel Becker7063fbf2005-12-15 14:29:43 -08001223 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1224
Joel Beckere6bd07a2007-07-06 23:33:17 -07001225 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001226 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001227 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001228 if (!group)
1229 group = ERR_PTR(-ENOMEM);
1230 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001231 link_group(to_config_group(parent_item), group);
1232 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001233 } else
1234 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001235 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001236 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001237 if (!item)
1238 item = ERR_PTR(-ENOMEM);
1239 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001240 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001241 else
1242 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001243 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001244 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001245
1246 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001247 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001248 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001249 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001250 * There are no extra references to clean up.
1251 */
Joel Becker70526b62008-06-17 15:34:32 -07001252 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001253 }
1254
Joel Beckereed7a0d2006-04-11 21:37:20 -07001255 /*
1256 * link_obj() has been called (via link_group() for groups).
1257 * From here on out, errors must clean that up.
1258 */
1259
Joel Becker7063fbf2005-12-15 14:29:43 -08001260 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001261 if (!type) {
1262 ret = -EINVAL;
1263 goto out_unlink;
1264 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001265
Joel Becker70526b62008-06-17 15:34:32 -07001266 new_item_owner = type->ct_owner;
1267 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001268 ret = -EINVAL;
1269 goto out_unlink;
1270 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001271
Joel Beckereed7a0d2006-04-11 21:37:20 -07001272 /*
1273 * I hate doing it this way, but if there is
1274 * an error, module_put() probably should
1275 * happen after any cleanup.
1276 */
1277 module_got = 1;
1278
Louis Rilling6d8344b2008-06-16 19:01:02 +02001279 /*
1280 * Make racing rmdir() fail if it did not tag parent with
1281 * CONFIGFS_USET_DROPPING
1282 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1283 * fail and let rmdir() terminate correctly
1284 */
1285 spin_lock(&configfs_dirent_lock);
1286 /* This will make configfs_detach_prep() fail */
1287 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1288 spin_unlock(&configfs_dirent_lock);
1289
Joel Beckereed7a0d2006-04-11 21:37:20 -07001290 if (group)
1291 ret = configfs_attach_group(parent_item, item, dentry);
1292 else
1293 ret = configfs_attach_item(parent_item, item, dentry);
1294
Louis Rilling6d8344b2008-06-16 19:01:02 +02001295 spin_lock(&configfs_dirent_lock);
1296 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001297 if (!ret)
1298 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001299 spin_unlock(&configfs_dirent_lock);
1300
Joel Beckereed7a0d2006-04-11 21:37:20 -07001301out_unlink:
1302 if (ret) {
1303 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001304 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001305
1306 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001307 if (group)
1308 unlink_group(group);
1309 else
1310 unlink_obj(item);
1311 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001312
Joel Beckere6bd07a2007-07-06 23:33:17 -07001313 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001314
1315 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001316 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001317 }
1318
Joel Becker70526b62008-06-17 15:34:32 -07001319out_subsys_put:
1320 if (ret)
1321 module_put(subsys_owner);
1322
Joel Becker84efad12006-03-27 18:46:09 -08001323out_put:
1324 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001325 * link_obj()/link_group() took a reference from child->parent,
1326 * so the parent is safely pinned. We can drop our working
1327 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001328 */
1329 config_item_put(parent_item);
1330
1331out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001332 return ret;
1333}
1334
1335static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1336{
1337 struct config_item *parent_item;
1338 struct config_item *item;
1339 struct configfs_subsystem *subsys;
1340 struct configfs_dirent *sd;
Joel Becker70526b62008-06-17 15:34:32 -07001341 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001342 int ret;
1343
Joel Becker7063fbf2005-12-15 14:29:43 -08001344 sd = dentry->d_fsdata;
1345 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1346 return -EPERM;
1347
Joel Becker84efad12006-03-27 18:46:09 -08001348 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001349 parent_item = configfs_get_config_item(dentry->d_parent);
1350 subsys = to_config_group(parent_item)->cg_subsys;
1351 BUG_ON(!subsys);
1352
1353 if (!parent_item->ci_type) {
1354 config_item_put(parent_item);
1355 return -EINVAL;
1356 }
1357
Joel Becker70526b62008-06-17 15:34:32 -07001358 /* configfs_mkdir() shouldn't have allowed this */
1359 BUG_ON(!subsys->su_group.cg_item.ci_type);
1360 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1361
Louis Rilling9a73d782008-06-20 14:09:22 +02001362 /*
1363 * Ensure that no racing symlink() will make detach_prep() fail while
1364 * the new link is temporarily attached
1365 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001366 do {
1367 struct mutex *wait_mutex;
1368
Louis Rillingde6bf182008-08-15 12:37:23 -07001369 mutex_lock(&configfs_symlink_mutex);
1370 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001371 /*
1372 * Here's where we check for dependents. We're protected by
1373 * configfs_dirent_lock.
1374 * If no dependent, atomically tag the item as dropping.
1375 */
1376 ret = sd->s_dependent_count ? -EBUSY : 0;
1377 if (!ret) {
1378 ret = configfs_detach_prep(dentry, &wait_mutex);
1379 if (ret)
1380 configfs_detach_rollback(dentry);
1381 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001382 spin_unlock(&configfs_dirent_lock);
1383 mutex_unlock(&configfs_symlink_mutex);
1384
1385 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001386 if (ret != -EAGAIN) {
1387 config_item_put(parent_item);
1388 return ret;
1389 }
1390
1391 /* Wait until the racing operation terminates */
1392 mutex_lock(wait_mutex);
1393 mutex_unlock(wait_mutex);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001394 }
1395 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001396
Joel Becker84efad12006-03-27 18:46:09 -08001397 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001398 item = configfs_get_config_item(dentry);
1399
1400 /* Drop reference from above, item already holds one. */
1401 config_item_put(parent_item);
1402
1403 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001404 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001405
1406 if (sd->s_type & CONFIGFS_USET_DIR) {
1407 configfs_detach_group(item);
1408
Joel Beckere6bd07a2007-07-06 23:33:17 -07001409 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001410 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001411 unlink_group(to_config_group(item));
1412 } else {
1413 configfs_detach_item(item);
1414
Joel Beckere6bd07a2007-07-06 23:33:17 -07001415 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001416 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001417 unlink_obj(item);
1418 }
1419
1420 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001421 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001422
1423 /* Drop our reference from above */
1424 config_item_put(item);
1425
Joel Becker70526b62008-06-17 15:34:32 -07001426 module_put(dead_item_owner);
1427 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001428
1429 return 0;
1430}
1431
Arjan van de Ven754661f2007-02-12 00:55:38 -08001432const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001433 .mkdir = configfs_mkdir,
1434 .rmdir = configfs_rmdir,
1435 .symlink = configfs_symlink,
1436 .unlink = configfs_unlink,
1437 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001438 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001439};
1440
Al Viro81d44ed12012-03-17 16:13:25 -04001441const struct inode_operations configfs_root_inode_operations = {
1442 .lookup = configfs_lookup,
1443 .setattr = configfs_setattr,
1444};
1445
Joel Becker7063fbf2005-12-15 14:29:43 -08001446#if 0
1447int configfs_rename_dir(struct config_item * item, const char *new_name)
1448{
1449 int error = 0;
1450 struct dentry * new_dentry, * parent;
1451
1452 if (!strcmp(config_item_name(item), new_name))
1453 return -EINVAL;
1454
1455 if (!item->parent)
1456 return -EINVAL;
1457
1458 down_write(&configfs_rename_sem);
1459 parent = item->parent->dentry;
1460
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001461 mutex_lock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001462
1463 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1464 if (!IS_ERR(new_dentry)) {
Joel Beckere7515d02006-03-10 11:42:30 -08001465 if (!new_dentry->d_inode) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001466 error = config_item_set_name(item, "%s", new_name);
1467 if (!error) {
1468 d_add(new_dentry, NULL);
1469 d_move(item->dentry, new_dentry);
1470 }
1471 else
1472 d_delete(new_dentry);
1473 } else
1474 error = -EEXIST;
1475 dput(new_dentry);
1476 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001477 mutex_unlock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001478 up_write(&configfs_rename_sem);
1479
1480 return error;
1481}
1482#endif
1483
1484static int configfs_dir_open(struct inode *inode, struct file *file)
1485{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001486 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001487 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001488 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001489
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001490 mutex_lock(&dentry->d_inode->i_mutex);
Louis Rilling2a109f22008-07-04 16:56:05 +02001491 /*
1492 * Fake invisibility if dir belongs to a group/default groups hierarchy
1493 * being attached
1494 */
1495 err = -ENOENT;
1496 if (configfs_dirent_is_ready(parent_sd)) {
Louis Rilling420118c2009-01-28 19:18:33 +01001497 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
Louis Rilling2a109f22008-07-04 16:56:05 +02001498 if (IS_ERR(file->private_data))
1499 err = PTR_ERR(file->private_data);
1500 else
1501 err = 0;
1502 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001503 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001504
Louis Rilling2a109f22008-07-04 16:56:05 +02001505 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001506}
1507
1508static int configfs_dir_close(struct inode *inode, struct file *file)
1509{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001510 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001511 struct configfs_dirent * cursor = file->private_data;
1512
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001513 mutex_lock(&dentry->d_inode->i_mutex);
Louis Rilling6f610762008-06-16 19:00:58 +02001514 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001515 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001516 spin_unlock(&configfs_dirent_lock);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001517 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001518
1519 release_configfs_dirent(cursor);
1520
1521 return 0;
1522}
1523
1524/* Relationship between s_mode and the DT_xxx types */
1525static inline unsigned char dt_type(struct configfs_dirent *sd)
1526{
1527 return (sd->s_mode >> 12) & 15;
1528}
1529
Al Viro52018852013-05-16 01:28:34 -04001530static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001531{
Al Viro52018852013-05-16 01:28:34 -04001532 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001533 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001534 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001535 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001536 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001537 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001538
Al Viro52018852013-05-16 01:28:34 -04001539 if (!dir_emit_dots(file, ctx))
1540 return 0;
1541 if (ctx->pos == 2) {
1542 spin_lock(&configfs_dirent_lock);
1543 list_move(q, &parent_sd->s_children);
1544 spin_unlock(&configfs_dirent_lock);
1545 }
1546 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1547 struct configfs_dirent *next;
1548 const char *name;
1549 int len;
1550 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001551
Al Viro52018852013-05-16 01:28:34 -04001552 next = list_entry(p, struct configfs_dirent, s_sibling);
1553 if (!next->s_element)
1554 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001555
Al Viro52018852013-05-16 01:28:34 -04001556 name = configfs_get_name(next);
1557 len = strlen(name);
Joel Becker24307aa2011-05-18 04:08:16 -07001558
Al Viro52018852013-05-16 01:28:34 -04001559 /*
1560 * We'll have a dentry and an inode for
1561 * PINNED items and for open attribute
1562 * files. We lock here to prevent a race
1563 * with configfs_d_iput() clearing
1564 * s_dentry before calling iput().
1565 *
1566 * Why do we go to the trouble? If
1567 * someone has an attribute file open,
1568 * the inode number should match until
1569 * they close it. Beyond that, we don't
1570 * care.
1571 */
1572 spin_lock(&configfs_dirent_lock);
1573 dentry = next->s_dentry;
1574 if (dentry)
1575 inode = dentry->d_inode;
1576 if (inode)
1577 ino = inode->i_ino;
1578 spin_unlock(&configfs_dirent_lock);
1579 if (!inode)
1580 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001581
Al Viro52018852013-05-16 01:28:34 -04001582 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1583 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001584
Al Viro52018852013-05-16 01:28:34 -04001585 spin_lock(&configfs_dirent_lock);
1586 list_move(q, p);
1587 spin_unlock(&configfs_dirent_lock);
1588 p = q;
1589 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001590 }
1591 return 0;
1592}
1593
Andrew Morton965c8e52012-12-17 15:59:39 -08001594static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001595{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001596 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001597
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001598 mutex_lock(&dentry->d_inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -08001599 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001600 case 1:
1601 offset += file->f_pos;
1602 case 0:
1603 if (offset >= 0)
1604 break;
1605 default:
Al Viro496ad9a2013-01-23 17:07:38 -05001606 mutex_unlock(&file_inode(file)->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001607 return -EINVAL;
1608 }
1609 if (offset != file->f_pos) {
1610 file->f_pos = offset;
1611 if (file->f_pos >= 2) {
1612 struct configfs_dirent *sd = dentry->d_fsdata;
1613 struct configfs_dirent *cursor = file->private_data;
1614 struct list_head *p;
1615 loff_t n = file->f_pos - 2;
1616
Louis Rilling6f610762008-06-16 19:00:58 +02001617 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001618 list_del(&cursor->s_sibling);
1619 p = sd->s_children.next;
1620 while (n && p != &sd->s_children) {
1621 struct configfs_dirent *next;
1622 next = list_entry(p, struct configfs_dirent,
1623 s_sibling);
1624 if (next->s_element)
1625 n--;
1626 p = p->next;
1627 }
1628 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001629 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001630 }
1631 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001632 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001633 return offset;
1634}
1635
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001636const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001637 .open = configfs_dir_open,
1638 .release = configfs_dir_close,
1639 .llseek = configfs_dir_lseek,
1640 .read = generic_read_dir,
Al Viro52018852013-05-16 01:28:34 -04001641 .iterate = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001642};
1643
1644int configfs_register_subsystem(struct configfs_subsystem *subsys)
1645{
1646 int err;
1647 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001648 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001649 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001650 struct configfs_dirent *sd;
1651
Al Viro2a152ad2012-03-17 16:53:29 -04001652 root = configfs_pin_fs();
1653 if (IS_ERR(root))
1654 return PTR_ERR(root);
Joel Becker7063fbf2005-12-15 14:29:43 -08001655
1656 if (!group->cg_item.ci_name)
1657 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1658
Al Virob7c177f2012-03-17 16:24:54 -04001659 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001660 link_group(to_config_group(sd->s_element), group);
1661
Al Virob7c177f2012-03-17 16:24:54 -04001662 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001663
Joel Becker7063fbf2005-12-15 14:29:43 -08001664 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001665 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001666 if (dentry) {
1667 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001668
Joel Beckerafdf04e2007-03-05 15:49:49 -08001669 err = configfs_attach_group(sd->s_element, &group->cg_item,
1670 dentry);
1671 if (err) {
Joel Beckerdf7f9962011-02-22 01:09:49 -08001672 BUG_ON(dentry->d_inode);
1673 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001674 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001675 } else {
1676 spin_lock(&configfs_dirent_lock);
1677 configfs_dir_set_ready(dentry->d_fsdata);
1678 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001679 }
1680 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001681
Al Virob7c177f2012-03-17 16:24:54 -04001682 mutex_unlock(&root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001683
Joel Beckerafdf04e2007-03-05 15:49:49 -08001684 if (err) {
1685 unlink_group(group);
1686 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001687 }
1688
1689 return err;
1690}
1691
1692void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1693{
1694 struct config_group *group = &subsys->su_group;
1695 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001696 struct dentry *root = dentry->d_sb->s_root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001697
Al Virob7c177f2012-03-17 16:24:54 -04001698 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001699 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001700 return;
1701 }
1702
Al Virob7c177f2012-03-17 16:24:54 -04001703 mutex_lock_nested(&root->d_inode->i_mutex,
Mark Fasheh55ed1602006-10-20 14:55:54 -07001704 I_MUTEX_PARENT);
1705 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001706 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001707 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001708 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001709 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001710 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001711 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001712 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001713 configfs_detach_group(&group->cg_item);
1714 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001715 dont_mount(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001716 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001717
1718 d_delete(dentry);
1719
Al Virob7c177f2012-03-17 16:24:54 -04001720 mutex_unlock(&root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001721
1722 dput(dentry);
1723
1724 unlink_group(group);
1725 configfs_release_fs();
1726}
1727
1728EXPORT_SYMBOL(configfs_register_subsystem);
1729EXPORT_SYMBOL(configfs_unregister_subsystem);