blob: 668dcabc5695a3a153f5234cab0f759c7693af5c [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
243static int init_dir(struct inode * inode)
244{
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 return 0;
251}
252
Dave Hansence8d2cd2007-10-16 23:31:13 -0700253static int configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800254{
255 inode->i_size = PAGE_SIZE;
256 inode->i_fop = &configfs_file_operations;
257 return 0;
258}
259
260static int init_symlink(struct inode * inode)
261{
262 inode->i_op = &configfs_symlink_inode_operations;
263 return 0;
264}
265
Al Viro0dd6c082012-03-17 16:49:20 -0400266static int create_dir(struct config_item *k, struct dentry *d)
Joel Becker7063fbf2005-12-15 14:29:43 -0800267{
268 int error;
269 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Al Viro0dd6c082012-03-17 16:49:20 -0400270 struct dentry *p = d->d_parent;
271
272 BUG_ON(!k);
Joel Becker7063fbf2005-12-15 14:29:43 -0800273
Joel Beckerb4c98f62006-09-13 11:01:19 -0700274 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
275 if (!error)
276 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
Louis Rilling2a109f22008-07-04 16:56:05 +0200277 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
Joel Becker7063fbf2005-12-15 14:29:43 -0800278 if (!error) {
Louis Rillinge74cc062009-01-28 19:18:32 +0100279 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800280 error = configfs_create(d, mode, init_dir);
Joel Becker7063fbf2005-12-15 14:29:43 -0800281 if (!error) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700282 inc_nlink(p->d_inode);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800283 } else {
284 struct configfs_dirent *sd = d->d_fsdata;
285 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200286 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800287 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200288 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800289 configfs_put(sd);
290 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800291 }
292 }
293 return error;
294}
295
296
297/**
298 * configfs_create_dir - create a directory for an config_item.
299 * @item: config_itemwe're creating directory for.
300 * @dentry: config_item's dentry.
Louis Rilling2a109f22008-07-04 16:56:05 +0200301 *
302 * Note: user-created entries won't be allowed under this new directory
303 * until it is validated by configfs_dir_set_ready()
Joel Becker7063fbf2005-12-15 14:29:43 -0800304 */
305
306static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
307{
Al Viro0dd6c082012-03-17 16:49:20 -0400308 int error = create_dir(item, dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800309 if (!error)
310 item->ci_dentry = dentry;
311 return error;
312}
313
Louis Rilling2a109f22008-07-04 16:56:05 +0200314/*
315 * Allow userspace to create new entries under a new directory created with
316 * configfs_create_dir(), and under all of its chidlren directories recursively.
317 * @sd configfs_dirent of the new directory to validate
318 *
319 * Caller must hold configfs_dirent_lock.
320 */
321static void configfs_dir_set_ready(struct configfs_dirent *sd)
322{
323 struct configfs_dirent *child_sd;
324
325 sd->s_type &= ~CONFIGFS_USET_CREATING;
326 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
327 if (child_sd->s_type & CONFIGFS_USET_CREATING)
328 configfs_dir_set_ready(child_sd);
329}
330
331/*
332 * Check that a directory does not belong to a directory hierarchy being
333 * attached and not validated yet.
334 * @sd configfs_dirent of the directory to check
335 *
336 * @return non-zero iff the directory was validated
337 *
338 * Note: takes configfs_dirent_lock, so the result may change from false to true
339 * in two consecutive calls, but never from true to false.
340 */
341int configfs_dirent_is_ready(struct configfs_dirent *sd)
342{
343 int ret;
344
345 spin_lock(&configfs_dirent_lock);
346 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
347 spin_unlock(&configfs_dirent_lock);
348
349 return ret;
350}
351
Joel Becker7063fbf2005-12-15 14:29:43 -0800352int configfs_create_link(struct configfs_symlink *sl,
353 struct dentry *parent,
354 struct dentry *dentry)
355{
356 int err = 0;
357 umode_t mode = S_IFLNK | S_IRWXUGO;
358
Joel Becker3d0f89b2006-01-25 13:31:07 -0800359 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
360 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800361 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800362 err = configfs_create(dentry, mode, init_symlink);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100363 if (err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800364 struct configfs_dirent *sd = dentry->d_fsdata;
365 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200366 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800367 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200368 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800369 configfs_put(sd);
370 }
371 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800372 }
373 return err;
374}
375
376static void remove_dir(struct dentry * d)
377{
378 struct dentry * parent = dget(d->d_parent);
379 struct configfs_dirent * sd;
380
381 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200382 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800383 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200384 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800385 configfs_put(sd);
386 if (d->d_inode)
387 simple_rmdir(parent->d_inode,d);
388
Linus Torvaldsc75e2472013-07-09 11:26:44 -0700389 pr_debug(" o %s removing done (%d)\n",d->d_name.name, d_count(d));
Joel Becker7063fbf2005-12-15 14:29:43 -0800390
391 dput(parent);
392}
393
394/**
395 * configfs_remove_dir - remove an config_item's directory.
396 * @item: config_item we're removing.
397 *
398 * The only thing special about this is that we remove any files in
399 * the directory before we remove the directory, and we've inlined
400 * what used to be configfs_rmdir() below, instead of calling separately.
Louis Rilling2e2ce172008-07-04 16:56:06 +0200401 *
402 * Caller holds the mutex of the item's inode
Joel Becker7063fbf2005-12-15 14:29:43 -0800403 */
404
405static void configfs_remove_dir(struct config_item * item)
406{
407 struct dentry * dentry = dget(item->ci_dentry);
408
409 if (!dentry)
410 return;
411
412 remove_dir(dentry);
413 /**
414 * Drop reference from dget() on entrance.
415 */
416 dput(dentry);
417}
418
419
420/* attaches attribute's configfs_dirent to the dentry corresponding to the
421 * attribute file
422 */
423static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
424{
425 struct configfs_attribute * attr = sd->s_element;
426 int error;
427
Junxiao Bi76ae2812013-11-21 14:31:56 -0800428 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800429 dentry->d_fsdata = configfs_get(sd);
430 sd->s_dentry = dentry;
Junxiao Bi76ae2812013-11-21 14:31:56 -0800431 spin_unlock(&configfs_dirent_lock);
432
Dave Hansence8d2cd2007-10-16 23:31:13 -0700433 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
434 configfs_init_file);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800435 if (error) {
436 configfs_put(sd);
437 return error;
438 }
439
Joel Becker7063fbf2005-12-15 14:29:43 -0800440 d_rehash(dentry);
441
442 return 0;
443}
444
445static struct dentry * configfs_lookup(struct inode *dir,
446 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400447 unsigned int flags)
Joel Becker7063fbf2005-12-15 14:29:43 -0800448{
449 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
450 struct configfs_dirent * sd;
451 int found = 0;
Louis Rilling2a109f22008-07-04 16:56:05 +0200452 int err;
453
454 /*
455 * Fake invisibility if dir belongs to a group/default groups hierarchy
456 * being attached
457 *
458 * This forbids userspace to read/write attributes of items which may
459 * not complete their initialization, since the dentries of the
460 * attributes won't be instantiated.
461 */
462 err = -ENOENT;
463 if (!configfs_dirent_is_ready(parent_sd))
464 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800465
466 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
467 if (sd->s_type & CONFIGFS_NOT_PINNED) {
468 const unsigned char * name = configfs_get_name(sd);
469
470 if (strcmp(name, dentry->d_name.name))
471 continue;
472
473 found = 1;
474 err = configfs_attach_attr(sd, dentry);
475 break;
476 }
477 }
478
479 if (!found) {
480 /*
481 * If it doesn't exist and it isn't a NOT_PINNED item,
482 * it must be negative.
483 */
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100484 if (dentry->d_name.len > NAME_MAX)
485 return ERR_PTR(-ENAMETOOLONG);
Nick Pigginfbc8d4c02011-01-07 17:49:21 +1100486 d_add(dentry, NULL);
487 return NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800488 }
489
Louis Rilling2a109f22008-07-04 16:56:05 +0200490out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800491 return ERR_PTR(err);
492}
493
494/*
495 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200496 * attributes and are removed by rmdir(). We recurse, setting
497 * CONFIGFS_USET_DROPPING on all children that are candidates for
498 * default detach.
499 * If there is an error, the caller will reset the flags via
500 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800501 */
Louis Rilling6d8344b2008-06-16 19:01:02 +0200502static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
Joel Becker7063fbf2005-12-15 14:29:43 -0800503{
504 struct configfs_dirent *parent_sd = dentry->d_fsdata;
505 struct configfs_dirent *sd;
506 int ret;
507
Louis Rilling4768e9b2008-06-23 14:16:17 +0200508 /* Mark that we're trying to drop the group */
509 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
510
Joel Becker7063fbf2005-12-15 14:29:43 -0800511 ret = -EBUSY;
512 if (!list_empty(&parent_sd->s_links))
513 goto out;
514
515 ret = 0;
516 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Louis Rilling99cefda2008-06-27 13:10:25 +0200517 if (!sd->s_element ||
518 (sd->s_type & CONFIGFS_NOT_PINNED))
Joel Becker7063fbf2005-12-15 14:29:43 -0800519 continue;
520 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200521 /* Abort if racing with mkdir() */
522 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
523 if (wait_mutex)
524 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
525 return -EAGAIN;
526 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800527
Joel Becker631d1fe2007-06-18 18:06:09 -0700528 /*
529 * Yup, recursive. If there's a problem, blame
530 * deep nesting of default_groups
531 */
Louis Rilling6d8344b2008-06-16 19:01:02 +0200532 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800533 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800534 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800535 } else
536 ret = -ENOTEMPTY;
537
538 break;
539 }
540
541out:
542 return ret;
543}
544
545/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200546 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800547 * set.
548 */
549static void configfs_detach_rollback(struct dentry *dentry)
550{
551 struct configfs_dirent *parent_sd = dentry->d_fsdata;
552 struct configfs_dirent *sd;
553
Louis Rilling4768e9b2008-06-23 14:16:17 +0200554 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
555
556 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
557 if (sd->s_type & CONFIGFS_USET_DEFAULT)
Joel Becker7063fbf2005-12-15 14:29:43 -0800558 configfs_detach_rollback(sd->s_dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800559}
560
561static void detach_attrs(struct config_item * item)
562{
563 struct dentry * dentry = dget(item->ci_dentry);
564 struct configfs_dirent * parent_sd;
565 struct configfs_dirent * sd, * tmp;
566
567 if (!dentry)
568 return;
569
570 pr_debug("configfs %s: dropping attrs for dir\n",
571 dentry->d_name.name);
572
573 parent_sd = dentry->d_fsdata;
574 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
575 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
576 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200577 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800578 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200579 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800580 configfs_drop_dentry(sd, dentry);
581 configfs_put(sd);
582 }
583
584 /**
585 * Drop reference from dget() on entrance.
586 */
587 dput(dentry);
588}
589
590static int populate_attrs(struct config_item *item)
591{
592 struct config_item_type *t = item->ci_type;
593 struct configfs_attribute *attr;
594 int error = 0;
595 int i;
596
597 if (!t)
598 return -EINVAL;
599 if (t->ct_attrs) {
600 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
601 if ((error = configfs_create_file(item, attr)))
602 break;
603 }
604 }
605
606 if (error)
607 detach_attrs(item);
608
609 return error;
610}
611
612static int configfs_attach_group(struct config_item *parent_item,
613 struct config_item *item,
614 struct dentry *dentry);
615static void configfs_detach_group(struct config_item *item);
616
617static void detach_groups(struct config_group *group)
618{
619 struct dentry * dentry = dget(group->cg_item.ci_dentry);
620 struct dentry *child;
621 struct configfs_dirent *parent_sd;
622 struct configfs_dirent *sd, *tmp;
623
624 if (!dentry)
625 return;
626
627 parent_sd = dentry->d_fsdata;
628 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
629 if (!sd->s_element ||
630 !(sd->s_type & CONFIGFS_USET_DEFAULT))
631 continue;
632
633 child = sd->s_dentry;
634
Louis Rillingb3e76af2008-06-16 19:01:01 +0200635 mutex_lock(&child->d_inode->i_mutex);
636
Joel Becker7063fbf2005-12-15 14:29:43 -0800637 configfs_detach_group(sd->s_element);
638 child->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400639 dont_mount(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800640
Louis Rillingb3e76af2008-06-16 19:01:01 +0200641 mutex_unlock(&child->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800642
643 d_delete(child);
644 dput(child);
645 }
646
647 /**
648 * Drop reference from dget() on entrance.
649 */
650 dput(dentry);
651}
652
653/*
654 * This fakes mkdir(2) on a default_groups[] entry. It
655 * creates a dentry, attachs it, and then does fixup
656 * on the sd->s_type.
657 *
658 * We could, perhaps, tweak our parent's ->mkdir for a minute and
659 * try using vfs_mkdir. Just a thought.
660 */
661static int create_default_group(struct config_group *parent_group,
662 struct config_group *group)
663{
664 int ret;
Joel Becker7063fbf2005-12-15 14:29:43 -0800665 struct configfs_dirent *sd;
666 /* We trust the caller holds a reference to parent */
667 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
668
669 if (!group->cg_item.ci_name)
670 group->cg_item.ci_name = group->cg_item.ci_namebuf;
Joel Becker7063fbf2005-12-15 14:29:43 -0800671
672 ret = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +0400673 child = d_alloc_name(parent, group->cg_item.ci_name);
Joel Becker7063fbf2005-12-15 14:29:43 -0800674 if (child) {
675 d_add(child, NULL);
676
677 ret = configfs_attach_group(&parent_group->cg_item,
678 &group->cg_item, child);
679 if (!ret) {
680 sd = child->d_fsdata;
681 sd->s_type |= CONFIGFS_USET_DEFAULT;
682 } else {
Joel Beckerdf7f9962011-02-22 01:09:49 -0800683 BUG_ON(child->d_inode);
684 d_drop(child);
Joel Becker7063fbf2005-12-15 14:29:43 -0800685 dput(child);
686 }
687 }
688
689 return ret;
690}
691
692static int populate_groups(struct config_group *group)
693{
694 struct config_group *new_group;
Joel Becker7063fbf2005-12-15 14:29:43 -0800695 int ret = 0;
696 int i;
697
Eric Sesterhenncbca6922006-03-23 00:36:54 +0100698 if (group->default_groups) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800699 for (i = 0; group->default_groups[i]; i++) {
700 new_group = group->default_groups[i];
701
702 ret = create_default_group(group, new_group);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200703 if (ret) {
704 detach_groups(group);
Joel Becker7063fbf2005-12-15 14:29:43 -0800705 break;
Louis Rilling2e2ce172008-07-04 16:56:06 +0200706 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800707 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800708 }
709
Joel Becker7063fbf2005-12-15 14:29:43 -0800710 return ret;
711}
712
713/*
714 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700715 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800716 */
717
718static void unlink_obj(struct config_item *item)
719{
720 struct config_group *group;
721
722 group = item->ci_group;
723 if (group) {
724 list_del_init(&item->ci_entry);
725
726 item->ci_group = NULL;
727 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700728
729 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800730 config_item_put(item);
731
Joel Beckereed7a0d2006-04-11 21:37:20 -0700732 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800733 config_group_put(group);
734 }
735}
736
737static void link_obj(struct config_item *parent_item, struct config_item *item)
738{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700739 /*
740 * Parent seems redundant with group, but it makes certain
741 * traversals much nicer.
742 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800743 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700744
745 /*
746 * We hold a reference on the parent for the child's ci_parent
747 * link.
748 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800749 item->ci_group = config_group_get(to_config_group(parent_item));
750 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
751
Joel Beckereed7a0d2006-04-11 21:37:20 -0700752 /*
753 * We hold a reference on the child for ci_entry on the parent's
754 * cg_children
755 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800756 config_item_get(item);
757}
758
759static void unlink_group(struct config_group *group)
760{
761 int i;
762 struct config_group *new_group;
763
764 if (group->default_groups) {
765 for (i = 0; group->default_groups[i]; i++) {
766 new_group = group->default_groups[i];
767 unlink_group(new_group);
768 }
769 }
770
771 group->cg_subsys = NULL;
772 unlink_obj(&group->cg_item);
773}
774
775static void link_group(struct config_group *parent_group, struct config_group *group)
776{
777 int i;
778 struct config_group *new_group;
779 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
780
781 link_obj(&parent_group->cg_item, &group->cg_item);
782
783 if (parent_group->cg_subsys)
784 subsys = parent_group->cg_subsys;
785 else if (configfs_is_root(&parent_group->cg_item))
786 subsys = to_configfs_subsystem(group);
787 else
788 BUG();
789 group->cg_subsys = subsys;
790
791 if (group->default_groups) {
792 for (i = 0; group->default_groups[i]; i++) {
793 new_group = group->default_groups[i];
794 link_group(group, new_group);
795 }
796 }
797}
798
799/*
800 * The goal is that configfs_attach_item() (and
801 * configfs_attach_group()) can be called from either the VFS or this
802 * module. That is, they assume that the items have been created,
803 * the dentry allocated, and the dcache is all ready to go.
804 *
805 * If they fail, they must clean up after themselves as if they
806 * had never been called. The caller (VFS or local function) will
807 * handle cleaning up the dcache bits.
808 *
809 * configfs_detach_group() and configfs_detach_item() behave similarly on
810 * the way out. They assume that the proper semaphores are held, they
811 * clean up the configfs items, and they expect their callers will
812 * handle the dcache bits.
813 */
814static int configfs_attach_item(struct config_item *parent_item,
815 struct config_item *item,
816 struct dentry *dentry)
817{
818 int ret;
819
820 ret = configfs_create_dir(item, dentry);
821 if (!ret) {
822 ret = populate_attrs(item);
823 if (ret) {
Louis Rilling2e2ce172008-07-04 16:56:06 +0200824 /*
825 * We are going to remove an inode and its dentry but
826 * the VFS may already have hit and used them. Thus,
827 * we must lock them as rmdir() would.
828 */
829 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800830 configfs_remove_dir(item);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200831 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400832 dont_mount(dentry);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200833 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800834 d_delete(dentry);
835 }
836 }
837
838 return ret;
839}
840
Louis Rilling2e2ce172008-07-04 16:56:06 +0200841/* Caller holds the mutex of the item's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800842static void configfs_detach_item(struct config_item *item)
843{
844 detach_attrs(item);
845 configfs_remove_dir(item);
846}
847
848static int configfs_attach_group(struct config_item *parent_item,
849 struct config_item *item,
850 struct dentry *dentry)
851{
852 int ret;
853 struct configfs_dirent *sd;
854
855 ret = configfs_attach_item(parent_item, item, dentry);
856 if (!ret) {
857 sd = dentry->d_fsdata;
858 sd->s_type |= CONFIGFS_USET_DIR;
859
Louis Rilling2e2ce172008-07-04 16:56:06 +0200860 /*
861 * FYI, we're faking mkdir in populate_groups()
862 * We must lock the group's inode to avoid races with the VFS
863 * which can already hit the inode and try to add/remove entries
864 * under it.
865 *
866 * We must also lock the inode to remove it safely in case of
867 * error, as rmdir() would.
868 */
869 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Louis Rillinge74cc062009-01-28 19:18:32 +0100870 configfs_adjust_dir_dirent_depth_before_populate(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800871 ret = populate_groups(to_config_group(item));
872 if (ret) {
873 configfs_detach_item(item);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200874 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -0400875 dont_mount(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800876 }
Louis Rillinge74cc062009-01-28 19:18:32 +0100877 configfs_adjust_dir_dirent_depth_after_populate(sd);
Louis Rilling2e2ce172008-07-04 16:56:06 +0200878 mutex_unlock(&dentry->d_inode->i_mutex);
879 if (ret)
880 d_delete(dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800881 }
882
883 return ret;
884}
885
Louis Rilling2e2ce172008-07-04 16:56:06 +0200886/* Caller holds the mutex of the group's inode */
Joel Becker7063fbf2005-12-15 14:29:43 -0800887static void configfs_detach_group(struct config_item *item)
888{
889 detach_groups(to_config_group(item));
890 configfs_detach_item(item);
891}
892
893/*
Joel Becker299894c2006-10-06 17:33:23 -0700894 * After the item has been detached from the filesystem view, we are
895 * ready to tear it out of the hierarchy. Notify the client before
896 * we do that so they can perform any cleanup that requires
897 * navigating the hierarchy. A client does not need to provide this
898 * callback. The subsystem semaphore MUST be held by the caller, and
899 * references must be valid for both items. It also assumes the
900 * caller has validated ci_type.
901 */
902static void client_disconnect_notify(struct config_item *parent_item,
903 struct config_item *item)
904{
905 struct config_item_type *type;
906
907 type = parent_item->ci_type;
908 BUG_ON(!type);
909
910 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
911 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
912 item);
913}
914
915/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800916 * Drop the initial reference from make_item()/make_group()
917 * This function assumes that reference is held on item
918 * and that item holds a valid reference to the parent. Also, it
919 * assumes the caller has validated ci_type.
920 */
921static void client_drop_item(struct config_item *parent_item,
922 struct config_item *item)
923{
924 struct config_item_type *type;
925
926 type = parent_item->ci_type;
927 BUG_ON(!type);
928
Joel Beckereed7a0d2006-04-11 21:37:20 -0700929 /*
930 * If ->drop_item() exists, it is responsible for the
931 * config_item_put().
932 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800933 if (type->ct_group_ops && type->ct_group_ops->drop_item)
934 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700935 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800936 else
937 config_item_put(item);
938}
939
Joel Becker631d1fe2007-06-18 18:06:09 -0700940#ifdef DEBUG
941static void configfs_dump_one(struct configfs_dirent *sd, int level)
942{
Fabian Frederickc6686932014-06-04 16:05:58 -0700943 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
Joel Becker631d1fe2007-06-18 18:06:09 -0700944
Fabian Frederickc6686932014-06-04 16:05:58 -0700945#define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
Joel Becker631d1fe2007-06-18 18:06:09 -0700946 type_print(CONFIGFS_ROOT);
947 type_print(CONFIGFS_DIR);
948 type_print(CONFIGFS_ITEM_ATTR);
949 type_print(CONFIGFS_ITEM_LINK);
950 type_print(CONFIGFS_USET_DIR);
951 type_print(CONFIGFS_USET_DEFAULT);
952 type_print(CONFIGFS_USET_DROPPING);
953#undef type_print
954}
955
956static int configfs_dump(struct configfs_dirent *sd, int level)
957{
958 struct configfs_dirent *child_sd;
959 int ret = 0;
960
961 configfs_dump_one(sd, level);
962
963 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
964 return 0;
965
966 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
967 ret = configfs_dump(child_sd, level + 2);
968 if (ret)
969 break;
970 }
971
972 return ret;
973}
974#endif
975
976
977/*
978 * configfs_depend_item() and configfs_undepend_item()
979 *
980 * WARNING: Do not call these from a configfs callback!
981 *
982 * This describes these functions and their helpers.
983 *
984 * Allow another kernel system to depend on a config_item. If this
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300985 * happens, the item cannot go away until the dependent can live without
Joel Becker631d1fe2007-06-18 18:06:09 -0700986 * it. The idea is to give client modules as simple an interface as
987 * possible. When a system asks them to depend on an item, they just
988 * call configfs_depend_item(). If the item is live and the client
989 * driver is in good shape, we'll happily do the work for them.
990 *
991 * Why is the locking complex? Because configfs uses the VFS to handle
992 * all locking, but this function is called outside the normal
993 * VFS->configfs path. So it must take VFS locks to prevent the
994 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
995 * why you can't call these functions underneath configfs callbacks.
996 *
997 * Note, btw, that this can be called at *any* time, even when a configfs
998 * subsystem isn't registered, or when configfs is loading or unloading.
999 * Just like configfs_register_subsystem(). So we take the same
Louis Rilling420118c2009-01-28 19:18:33 +01001000 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1001 * If we can find the target item in the
Joel Becker631d1fe2007-06-18 18:06:09 -07001002 * configfs tree, it must be part of the subsystem tree as well, so we
Louis Rilling420118c2009-01-28 19:18:33 +01001003 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1004 * locking out mkdir() and rmdir(), who might be racing us.
Joel Becker631d1fe2007-06-18 18:06:09 -07001005 */
1006
1007/*
1008 * configfs_depend_prep()
1009 *
1010 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1011 * attributes. This is similar but not the same to configfs_detach_prep().
1012 * Note that configfs_detach_prep() expects the parent to be locked when it
1013 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1014 * do that so we can unlock it if we find nothing.
1015 *
1016 * Here we do a depth-first search of the dentry hierarchy looking for
Louis Rilling420118c2009-01-28 19:18:33 +01001017 * our object.
1018 * We deliberately ignore items tagged as dropping since they are virtually
1019 * dead, as well as items in the middle of attachment since they virtually
1020 * do not exist yet. This completes the locking out of racing mkdir() and
1021 * rmdir().
1022 * Note: subdirectories in the middle of attachment start with s_type =
1023 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1024 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1025 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001026 *
Louis Rilling420118c2009-01-28 19:18:33 +01001027 * If the target is not found, -ENOENT is bubbled up.
Joel Becker631d1fe2007-06-18 18:06:09 -07001028 *
1029 * This adds a requirement that all config_items be unique!
1030 *
Louis Rilling420118c2009-01-28 19:18:33 +01001031 * This is recursive. There isn't
Joel Becker631d1fe2007-06-18 18:06:09 -07001032 * much on the stack, though, so folks that need this function - be careful
1033 * about your stack! Patches will be accepted to make it iterative.
1034 */
1035static int configfs_depend_prep(struct dentry *origin,
1036 struct config_item *target)
1037{
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001038 struct configfs_dirent *child_sd, *sd;
Joel Becker631d1fe2007-06-18 18:06:09 -07001039 int ret = 0;
1040
Wei Yongjun49deb4b2013-02-21 16:42:43 -08001041 BUG_ON(!origin || !origin->d_fsdata);
1042 sd = origin->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001043
Joel Becker631d1fe2007-06-18 18:06:09 -07001044 if (sd->s_element == target) /* Boo-yah */
1045 goto out;
1046
1047 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
Louis Rilling420118c2009-01-28 19:18:33 +01001048 if ((child_sd->s_type & CONFIGFS_DIR) &&
1049 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1050 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
Joel Becker631d1fe2007-06-18 18:06:09 -07001051 ret = configfs_depend_prep(child_sd->s_dentry,
1052 target);
1053 if (!ret)
1054 goto out; /* Child path boo-yah */
1055 }
1056 }
1057
1058 /* We looped all our children and didn't find target */
Joel Becker631d1fe2007-06-18 18:06:09 -07001059 ret = -ENOENT;
1060
1061out:
1062 return ret;
1063}
1064
Joel Becker631d1fe2007-06-18 18:06:09 -07001065int configfs_depend_item(struct configfs_subsystem *subsys,
1066 struct config_item *target)
1067{
1068 int ret;
1069 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1070 struct config_item *s_item = &subsys->su_group.cg_item;
Al Virob7c177f2012-03-17 16:24:54 -04001071 struct dentry *root;
Joel Becker631d1fe2007-06-18 18:06:09 -07001072
1073 /*
1074 * Pin the configfs filesystem. This means we can safely access
1075 * the root of the configfs filesystem.
1076 */
Al Viro2a152ad2012-03-17 16:53:29 -04001077 root = configfs_pin_fs();
1078 if (IS_ERR(root))
1079 return PTR_ERR(root);
Joel Becker631d1fe2007-06-18 18:06:09 -07001080
1081 /*
1082 * Next, lock the root directory. We're going to check that the
1083 * subsystem is really registered, and so we need to lock out
1084 * configfs_[un]register_subsystem().
1085 */
Al Virob7c177f2012-03-17 16:24:54 -04001086 mutex_lock(&root->d_inode->i_mutex);
Joel Becker631d1fe2007-06-18 18:06:09 -07001087
Al Virob7c177f2012-03-17 16:24:54 -04001088 root_sd = root->d_fsdata;
Joel Becker631d1fe2007-06-18 18:06:09 -07001089
1090 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1091 if (p->s_type & CONFIGFS_DIR) {
1092 if (p->s_element == s_item) {
1093 subsys_sd = p;
1094 break;
1095 }
1096 }
1097 }
1098
1099 if (!subsys_sd) {
1100 ret = -ENOENT;
1101 goto out_unlock_fs;
1102 }
1103
1104 /* Ok, now we can trust subsys/s_item */
1105
Louis Rilling420118c2009-01-28 19:18:33 +01001106 spin_lock(&configfs_dirent_lock);
1107 /* Scan the tree, return 0 if found */
Joel Becker631d1fe2007-06-18 18:06:09 -07001108 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1109 if (ret)
Louis Rilling420118c2009-01-28 19:18:33 +01001110 goto out_unlock_dirent_lock;
Joel Becker631d1fe2007-06-18 18:06:09 -07001111
Louis Rilling420118c2009-01-28 19:18:33 +01001112 /*
1113 * We are sure that the item is not about to be removed by rmdir(), and
1114 * not in the middle of attachment by mkdir().
1115 */
Joel Becker631d1fe2007-06-18 18:06:09 -07001116 p = target->ci_dentry->d_fsdata;
1117 p->s_dependent_count += 1;
1118
Louis Rilling420118c2009-01-28 19:18:33 +01001119out_unlock_dirent_lock:
1120 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001121out_unlock_fs:
Al Virob7c177f2012-03-17 16:24:54 -04001122 mutex_unlock(&root->d_inode->i_mutex);
Joel Becker631d1fe2007-06-18 18:06:09 -07001123
1124 /*
1125 * If we succeeded, the fs is pinned via other methods. If not,
1126 * we're done with it anyway. So release_fs() is always right.
1127 */
1128 configfs_release_fs();
1129
1130 return ret;
1131}
1132EXPORT_SYMBOL(configfs_depend_item);
1133
1134/*
1135 * Release the dependent linkage. This is much simpler than
1136 * configfs_depend_item() because we know that that the client driver is
1137 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1138 */
1139void configfs_undepend_item(struct configfs_subsystem *subsys,
1140 struct config_item *target)
1141{
1142 struct configfs_dirent *sd;
1143
1144 /*
Louis Rilling420118c2009-01-28 19:18:33 +01001145 * Since we can trust everything is pinned, we just need
1146 * configfs_dirent_lock.
Joel Becker631d1fe2007-06-18 18:06:09 -07001147 */
Louis Rilling420118c2009-01-28 19:18:33 +01001148 spin_lock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001149
1150 sd = target->ci_dentry->d_fsdata;
1151 BUG_ON(sd->s_dependent_count < 1);
1152
1153 sd->s_dependent_count -= 1;
1154
1155 /*
1156 * After this unlock, we cannot trust the item to stay alive!
1157 * DO NOT REFERENCE item after this unlock.
1158 */
Louis Rilling420118c2009-01-28 19:18:33 +01001159 spin_unlock(&configfs_dirent_lock);
Joel Becker631d1fe2007-06-18 18:06:09 -07001160}
1161EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001162
Al Viro18bb1db2011-07-26 01:41:39 -04001163static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joel Becker7063fbf2005-12-15 14:29:43 -08001164{
Joel Beckera6795e92008-07-17 15:21:29 -07001165 int ret = 0;
1166 int module_got = 0;
1167 struct config_group *group = NULL;
1168 struct config_item *item = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001169 struct config_item *parent_item;
1170 struct configfs_subsystem *subsys;
1171 struct configfs_dirent *sd;
1172 struct config_item_type *type;
Joel Becker70526b62008-06-17 15:34:32 -07001173 struct module *subsys_owner = NULL, *new_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001174 char *name;
1175
Joel Becker7063fbf2005-12-15 14:29:43 -08001176 sd = dentry->d_parent->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001177
1178 /*
1179 * Fake invisibility if dir belongs to a group/default groups hierarchy
1180 * being attached
1181 */
1182 if (!configfs_dirent_is_ready(sd)) {
1183 ret = -ENOENT;
1184 goto out;
1185 }
1186
Joel Becker84efad12006-03-27 18:46:09 -08001187 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1188 ret = -EPERM;
1189 goto out;
1190 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001191
Joel Becker84efad12006-03-27 18:46:09 -08001192 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001193 parent_item = configfs_get_config_item(dentry->d_parent);
1194 type = parent_item->ci_type;
1195 subsys = to_config_group(parent_item)->cg_subsys;
1196 BUG_ON(!subsys);
1197
1198 if (!type || !type->ct_group_ops ||
1199 (!type->ct_group_ops->make_group &&
1200 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001201 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1202 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001203 }
1204
Joel Becker70526b62008-06-17 15:34:32 -07001205 /*
1206 * The subsystem may belong to a different module than the item
1207 * being created. We don't want to safely pin the new item but
1208 * fail to pin the subsystem it sits under.
1209 */
1210 if (!subsys->su_group.cg_item.ci_type) {
1211 ret = -EINVAL;
1212 goto out_put;
1213 }
1214 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1215 if (!try_module_get(subsys_owner)) {
1216 ret = -EINVAL;
1217 goto out_put;
1218 }
1219
Joel Becker7063fbf2005-12-15 14:29:43 -08001220 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1221 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001222 ret = -ENOMEM;
Joel Becker70526b62008-06-17 15:34:32 -07001223 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001224 }
Joel Becker84efad12006-03-27 18:46:09 -08001225
Joel Becker7063fbf2005-12-15 14:29:43 -08001226 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1227
Joel Beckere6bd07a2007-07-06 23:33:17 -07001228 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001229 if (type->ct_group_ops->make_group) {
Joel Beckerf89ab862008-07-17 14:53:48 -07001230 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001231 if (!group)
1232 group = ERR_PTR(-ENOMEM);
1233 if (!IS_ERR(group)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001234 link_group(to_config_group(parent_item), group);
1235 item = &group->cg_item;
Joel Beckera6795e92008-07-17 15:21:29 -07001236 } else
1237 ret = PTR_ERR(group);
Joel Becker7063fbf2005-12-15 14:29:43 -08001238 } else {
Joel Beckerf89ab862008-07-17 14:53:48 -07001239 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
Joel Beckera6795e92008-07-17 15:21:29 -07001240 if (!item)
1241 item = ERR_PTR(-ENOMEM);
1242 if (!IS_ERR(item))
Joel Becker7063fbf2005-12-15 14:29:43 -08001243 link_obj(parent_item, item);
Joel Beckera6795e92008-07-17 15:21:29 -07001244 else
1245 ret = PTR_ERR(item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001246 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001247 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001248
1249 kfree(name);
Joel Beckera6795e92008-07-17 15:21:29 -07001250 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001251 /*
Joel Beckerdacdd0e2008-07-17 16:54:19 -07001252 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001253 * There are no extra references to clean up.
1254 */
Joel Becker70526b62008-06-17 15:34:32 -07001255 goto out_subsys_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001256 }
1257
Joel Beckereed7a0d2006-04-11 21:37:20 -07001258 /*
1259 * link_obj() has been called (via link_group() for groups).
1260 * From here on out, errors must clean that up.
1261 */
1262
Joel Becker7063fbf2005-12-15 14:29:43 -08001263 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001264 if (!type) {
1265 ret = -EINVAL;
1266 goto out_unlink;
1267 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001268
Joel Becker70526b62008-06-17 15:34:32 -07001269 new_item_owner = type->ct_owner;
1270 if (!try_module_get(new_item_owner)) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001271 ret = -EINVAL;
1272 goto out_unlink;
1273 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001274
Joel Beckereed7a0d2006-04-11 21:37:20 -07001275 /*
1276 * I hate doing it this way, but if there is
1277 * an error, module_put() probably should
1278 * happen after any cleanup.
1279 */
1280 module_got = 1;
1281
Louis Rilling6d8344b2008-06-16 19:01:02 +02001282 /*
1283 * Make racing rmdir() fail if it did not tag parent with
1284 * CONFIGFS_USET_DROPPING
1285 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1286 * fail and let rmdir() terminate correctly
1287 */
1288 spin_lock(&configfs_dirent_lock);
1289 /* This will make configfs_detach_prep() fail */
1290 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1291 spin_unlock(&configfs_dirent_lock);
1292
Joel Beckereed7a0d2006-04-11 21:37:20 -07001293 if (group)
1294 ret = configfs_attach_group(parent_item, item, dentry);
1295 else
1296 ret = configfs_attach_item(parent_item, item, dentry);
1297
Louis Rilling6d8344b2008-06-16 19:01:02 +02001298 spin_lock(&configfs_dirent_lock);
1299 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
Louis Rilling2a109f22008-07-04 16:56:05 +02001300 if (!ret)
1301 configfs_dir_set_ready(dentry->d_fsdata);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001302 spin_unlock(&configfs_dirent_lock);
1303
Joel Beckereed7a0d2006-04-11 21:37:20 -07001304out_unlink:
1305 if (ret) {
1306 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001307 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001308
1309 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001310 if (group)
1311 unlink_group(group);
1312 else
1313 unlink_obj(item);
1314 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001315
Joel Beckere6bd07a2007-07-06 23:33:17 -07001316 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001317
1318 if (module_got)
Joel Becker70526b62008-06-17 15:34:32 -07001319 module_put(new_item_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001320 }
1321
Joel Becker70526b62008-06-17 15:34:32 -07001322out_subsys_put:
1323 if (ret)
1324 module_put(subsys_owner);
1325
Joel Becker84efad12006-03-27 18:46:09 -08001326out_put:
1327 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001328 * link_obj()/link_group() took a reference from child->parent,
1329 * so the parent is safely pinned. We can drop our working
1330 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001331 */
1332 config_item_put(parent_item);
1333
1334out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001335 return ret;
1336}
1337
1338static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1339{
1340 struct config_item *parent_item;
1341 struct config_item *item;
1342 struct configfs_subsystem *subsys;
1343 struct configfs_dirent *sd;
Joel Becker70526b62008-06-17 15:34:32 -07001344 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001345 int ret;
1346
Joel Becker7063fbf2005-12-15 14:29:43 -08001347 sd = dentry->d_fsdata;
1348 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1349 return -EPERM;
1350
Joel Becker84efad12006-03-27 18:46:09 -08001351 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001352 parent_item = configfs_get_config_item(dentry->d_parent);
1353 subsys = to_config_group(parent_item)->cg_subsys;
1354 BUG_ON(!subsys);
1355
1356 if (!parent_item->ci_type) {
1357 config_item_put(parent_item);
1358 return -EINVAL;
1359 }
1360
Joel Becker70526b62008-06-17 15:34:32 -07001361 /* configfs_mkdir() shouldn't have allowed this */
1362 BUG_ON(!subsys->su_group.cg_item.ci_type);
1363 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1364
Louis Rilling9a73d782008-06-20 14:09:22 +02001365 /*
1366 * Ensure that no racing symlink() will make detach_prep() fail while
1367 * the new link is temporarily attached
1368 */
Louis Rilling6d8344b2008-06-16 19:01:02 +02001369 do {
1370 struct mutex *wait_mutex;
1371
Louis Rillingde6bf182008-08-15 12:37:23 -07001372 mutex_lock(&configfs_symlink_mutex);
1373 spin_lock(&configfs_dirent_lock);
Louis Rilling420118c2009-01-28 19:18:33 +01001374 /*
1375 * Here's where we check for dependents. We're protected by
1376 * configfs_dirent_lock.
1377 * If no dependent, atomically tag the item as dropping.
1378 */
1379 ret = sd->s_dependent_count ? -EBUSY : 0;
1380 if (!ret) {
1381 ret = configfs_detach_prep(dentry, &wait_mutex);
1382 if (ret)
1383 configfs_detach_rollback(dentry);
1384 }
Louis Rillingde6bf182008-08-15 12:37:23 -07001385 spin_unlock(&configfs_dirent_lock);
1386 mutex_unlock(&configfs_symlink_mutex);
1387
1388 if (ret) {
Louis Rilling6d8344b2008-06-16 19:01:02 +02001389 if (ret != -EAGAIN) {
1390 config_item_put(parent_item);
1391 return ret;
1392 }
1393
1394 /* Wait until the racing operation terminates */
1395 mutex_lock(wait_mutex);
1396 mutex_unlock(wait_mutex);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001397 }
1398 } while (ret == -EAGAIN);
Joel Becker7063fbf2005-12-15 14:29:43 -08001399
Joel Becker84efad12006-03-27 18:46:09 -08001400 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001401 item = configfs_get_config_item(dentry);
1402
1403 /* Drop reference from above, item already holds one. */
1404 config_item_put(parent_item);
1405
1406 if (item->ci_type)
Joel Becker70526b62008-06-17 15:34:32 -07001407 dead_item_owner = item->ci_type->ct_owner;
Joel Becker7063fbf2005-12-15 14:29:43 -08001408
1409 if (sd->s_type & CONFIGFS_USET_DIR) {
1410 configfs_detach_group(item);
1411
Joel Beckere6bd07a2007-07-06 23:33:17 -07001412 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001413 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001414 unlink_group(to_config_group(item));
1415 } else {
1416 configfs_detach_item(item);
1417
Joel Beckere6bd07a2007-07-06 23:33:17 -07001418 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001419 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001420 unlink_obj(item);
1421 }
1422
1423 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001424 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001425
1426 /* Drop our reference from above */
1427 config_item_put(item);
1428
Joel Becker70526b62008-06-17 15:34:32 -07001429 module_put(dead_item_owner);
1430 module_put(subsys_owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001431
1432 return 0;
1433}
1434
Arjan van de Ven754661f2007-02-12 00:55:38 -08001435const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001436 .mkdir = configfs_mkdir,
1437 .rmdir = configfs_rmdir,
1438 .symlink = configfs_symlink,
1439 .unlink = configfs_unlink,
1440 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001441 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001442};
1443
Al Viro81d44ed12012-03-17 16:13:25 -04001444const struct inode_operations configfs_root_inode_operations = {
1445 .lookup = configfs_lookup,
1446 .setattr = configfs_setattr,
1447};
1448
Joel Becker7063fbf2005-12-15 14:29:43 -08001449#if 0
1450int configfs_rename_dir(struct config_item * item, const char *new_name)
1451{
1452 int error = 0;
1453 struct dentry * new_dentry, * parent;
1454
1455 if (!strcmp(config_item_name(item), new_name))
1456 return -EINVAL;
1457
1458 if (!item->parent)
1459 return -EINVAL;
1460
1461 down_write(&configfs_rename_sem);
1462 parent = item->parent->dentry;
1463
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001464 mutex_lock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001465
1466 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1467 if (!IS_ERR(new_dentry)) {
Joel Beckere7515d02006-03-10 11:42:30 -08001468 if (!new_dentry->d_inode) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001469 error = config_item_set_name(item, "%s", new_name);
1470 if (!error) {
1471 d_add(new_dentry, NULL);
1472 d_move(item->dentry, new_dentry);
1473 }
1474 else
1475 d_delete(new_dentry);
1476 } else
1477 error = -EEXIST;
1478 dput(new_dentry);
1479 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001480 mutex_unlock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001481 up_write(&configfs_rename_sem);
1482
1483 return error;
1484}
1485#endif
1486
1487static int configfs_dir_open(struct inode *inode, struct file *file)
1488{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001489 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001490 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Louis Rilling2a109f22008-07-04 16:56:05 +02001491 int err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001492
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001493 mutex_lock(&dentry->d_inode->i_mutex);
Louis Rilling2a109f22008-07-04 16:56:05 +02001494 /*
1495 * Fake invisibility if dir belongs to a group/default groups hierarchy
1496 * being attached
1497 */
1498 err = -ENOENT;
1499 if (configfs_dirent_is_ready(parent_sd)) {
Louis Rilling420118c2009-01-28 19:18:33 +01001500 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
Louis Rilling2a109f22008-07-04 16:56:05 +02001501 if (IS_ERR(file->private_data))
1502 err = PTR_ERR(file->private_data);
1503 else
1504 err = 0;
1505 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001506 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001507
Louis Rilling2a109f22008-07-04 16:56:05 +02001508 return err;
Joel Becker7063fbf2005-12-15 14:29:43 -08001509}
1510
1511static int configfs_dir_close(struct inode *inode, struct file *file)
1512{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001513 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001514 struct configfs_dirent * cursor = file->private_data;
1515
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001516 mutex_lock(&dentry->d_inode->i_mutex);
Louis Rilling6f610762008-06-16 19:00:58 +02001517 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001518 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001519 spin_unlock(&configfs_dirent_lock);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001520 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001521
1522 release_configfs_dirent(cursor);
1523
1524 return 0;
1525}
1526
1527/* Relationship between s_mode and the DT_xxx types */
1528static inline unsigned char dt_type(struct configfs_dirent *sd)
1529{
1530 return (sd->s_mode >> 12) & 15;
1531}
1532
Al Viro52018852013-05-16 01:28:34 -04001533static int configfs_readdir(struct file *file, struct dir_context *ctx)
Joel Becker7063fbf2005-12-15 14:29:43 -08001534{
Al Viro52018852013-05-16 01:28:34 -04001535 struct dentry *dentry = file->f_path.dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001536 struct super_block *sb = dentry->d_sb;
Joel Becker7063fbf2005-12-15 14:29:43 -08001537 struct configfs_dirent * parent_sd = dentry->d_fsdata;
Al Viro52018852013-05-16 01:28:34 -04001538 struct configfs_dirent *cursor = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -08001539 struct list_head *p, *q = &cursor->s_sibling;
Joel Becker24307aa2011-05-18 04:08:16 -07001540 ino_t ino = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001541
Al Viro52018852013-05-16 01:28:34 -04001542 if (!dir_emit_dots(file, ctx))
1543 return 0;
1544 if (ctx->pos == 2) {
1545 spin_lock(&configfs_dirent_lock);
1546 list_move(q, &parent_sd->s_children);
1547 spin_unlock(&configfs_dirent_lock);
1548 }
1549 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1550 struct configfs_dirent *next;
1551 const char *name;
1552 int len;
1553 struct inode *inode = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001554
Al Viro52018852013-05-16 01:28:34 -04001555 next = list_entry(p, struct configfs_dirent, s_sibling);
1556 if (!next->s_element)
1557 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -08001558
Al Viro52018852013-05-16 01:28:34 -04001559 name = configfs_get_name(next);
1560 len = strlen(name);
Joel Becker24307aa2011-05-18 04:08:16 -07001561
Al Viro52018852013-05-16 01:28:34 -04001562 /*
1563 * We'll have a dentry and an inode for
1564 * PINNED items and for open attribute
1565 * files. We lock here to prevent a race
1566 * with configfs_d_iput() clearing
1567 * s_dentry before calling iput().
1568 *
1569 * Why do we go to the trouble? If
1570 * someone has an attribute file open,
1571 * the inode number should match until
1572 * they close it. Beyond that, we don't
1573 * care.
1574 */
1575 spin_lock(&configfs_dirent_lock);
1576 dentry = next->s_dentry;
1577 if (dentry)
1578 inode = dentry->d_inode;
1579 if (inode)
1580 ino = inode->i_ino;
1581 spin_unlock(&configfs_dirent_lock);
1582 if (!inode)
1583 ino = iunique(sb, 2);
Joel Becker7063fbf2005-12-15 14:29:43 -08001584
Al Viro52018852013-05-16 01:28:34 -04001585 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1586 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001587
Al Viro52018852013-05-16 01:28:34 -04001588 spin_lock(&configfs_dirent_lock);
1589 list_move(q, p);
1590 spin_unlock(&configfs_dirent_lock);
1591 p = q;
1592 ctx->pos++;
Joel Becker7063fbf2005-12-15 14:29:43 -08001593 }
1594 return 0;
1595}
1596
Andrew Morton965c8e52012-12-17 15:59:39 -08001597static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
Joel Becker7063fbf2005-12-15 14:29:43 -08001598{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001599 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001600
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001601 mutex_lock(&dentry->d_inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -08001602 switch (whence) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001603 case 1:
1604 offset += file->f_pos;
1605 case 0:
1606 if (offset >= 0)
1607 break;
1608 default:
Al Viro496ad9a2013-01-23 17:07:38 -05001609 mutex_unlock(&file_inode(file)->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001610 return -EINVAL;
1611 }
1612 if (offset != file->f_pos) {
1613 file->f_pos = offset;
1614 if (file->f_pos >= 2) {
1615 struct configfs_dirent *sd = dentry->d_fsdata;
1616 struct configfs_dirent *cursor = file->private_data;
1617 struct list_head *p;
1618 loff_t n = file->f_pos - 2;
1619
Louis Rilling6f610762008-06-16 19:00:58 +02001620 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001621 list_del(&cursor->s_sibling);
1622 p = sd->s_children.next;
1623 while (n && p != &sd->s_children) {
1624 struct configfs_dirent *next;
1625 next = list_entry(p, struct configfs_dirent,
1626 s_sibling);
1627 if (next->s_element)
1628 n--;
1629 p = p->next;
1630 }
1631 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001632 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001633 }
1634 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001635 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001636 return offset;
1637}
1638
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001639const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001640 .open = configfs_dir_open,
1641 .release = configfs_dir_close,
1642 .llseek = configfs_dir_lseek,
1643 .read = generic_read_dir,
Al Viro52018852013-05-16 01:28:34 -04001644 .iterate = configfs_readdir,
Joel Becker7063fbf2005-12-15 14:29:43 -08001645};
1646
1647int configfs_register_subsystem(struct configfs_subsystem *subsys)
1648{
1649 int err;
1650 struct config_group *group = &subsys->su_group;
Joel Becker7063fbf2005-12-15 14:29:43 -08001651 struct dentry *dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001652 struct dentry *root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001653 struct configfs_dirent *sd;
1654
Al Viro2a152ad2012-03-17 16:53:29 -04001655 root = configfs_pin_fs();
1656 if (IS_ERR(root))
1657 return PTR_ERR(root);
Joel Becker7063fbf2005-12-15 14:29:43 -08001658
1659 if (!group->cg_item.ci_name)
1660 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1661
Al Virob7c177f2012-03-17 16:24:54 -04001662 sd = root->d_fsdata;
Joel Becker7063fbf2005-12-15 14:29:43 -08001663 link_group(to_config_group(sd->s_element), group);
1664
Al Virob7c177f2012-03-17 16:24:54 -04001665 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001666
Joel Becker7063fbf2005-12-15 14:29:43 -08001667 err = -ENOMEM;
Al Viroec193cf2013-07-14 17:16:52 +04001668 dentry = d_alloc_name(root, group->cg_item.ci_name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001669 if (dentry) {
1670 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001671
Joel Beckerafdf04e2007-03-05 15:49:49 -08001672 err = configfs_attach_group(sd->s_element, &group->cg_item,
1673 dentry);
1674 if (err) {
Joel Beckerdf7f9962011-02-22 01:09:49 -08001675 BUG_ON(dentry->d_inode);
1676 d_drop(dentry);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001677 dput(dentry);
Louis Rilling2a109f22008-07-04 16:56:05 +02001678 } else {
1679 spin_lock(&configfs_dirent_lock);
1680 configfs_dir_set_ready(dentry->d_fsdata);
1681 spin_unlock(&configfs_dirent_lock);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001682 }
1683 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001684
Al Virob7c177f2012-03-17 16:24:54 -04001685 mutex_unlock(&root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001686
Joel Beckerafdf04e2007-03-05 15:49:49 -08001687 if (err) {
1688 unlink_group(group);
1689 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001690 }
1691
1692 return err;
1693}
1694
1695void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1696{
1697 struct config_group *group = &subsys->su_group;
1698 struct dentry *dentry = group->cg_item.ci_dentry;
Al Virob7c177f2012-03-17 16:24:54 -04001699 struct dentry *root = dentry->d_sb->s_root;
Joel Becker7063fbf2005-12-15 14:29:43 -08001700
Al Virob7c177f2012-03-17 16:24:54 -04001701 if (dentry->d_parent != root) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001702 pr_err("Tried to unregister non-subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001703 return;
1704 }
1705
Al Virob7c177f2012-03-17 16:24:54 -04001706 mutex_lock_nested(&root->d_inode->i_mutex,
Mark Fasheh55ed1602006-10-20 14:55:54 -07001707 I_MUTEX_PARENT);
1708 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Louis Rilling9a73d782008-06-20 14:09:22 +02001709 mutex_lock(&configfs_symlink_mutex);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001710 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001711 if (configfs_detach_prep(dentry, NULL)) {
Fabian Frederick1d88aa42014-06-04 16:05:59 -07001712 pr_err("Tried to unregister non-empty subsystem!\n");
Joel Becker7063fbf2005-12-15 14:29:43 -08001713 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001714 spin_unlock(&configfs_dirent_lock);
Louis Rilling9a73d782008-06-20 14:09:22 +02001715 mutex_unlock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001716 configfs_detach_group(&group->cg_item);
1717 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04001718 dont_mount(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001719 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001720
1721 d_delete(dentry);
1722
Al Virob7c177f2012-03-17 16:24:54 -04001723 mutex_unlock(&root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001724
1725 dput(dentry);
1726
1727 unlink_group(group);
1728 configfs_release_fs();
1729}
1730
1731EXPORT_SYMBOL(configfs_register_subsystem);
1732EXPORT_SYMBOL(configfs_unregister_subsystem);