blob: 0e64312a084ccfad43cce1ea9bbbbab8dde991b8 [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{
56 struct configfs_dirent * sd = dentry->d_fsdata;
57
58 if (sd) {
59 BUG_ON(sd->s_dentry != dentry);
60 sd->s_dentry = NULL;
61 configfs_put(sd);
62 }
63 iput(inode);
64}
65
66/*
67 * We _must_ delete our dentries on last dput, as the chain-to-parent
68 * behavior is required to clear the parents of default_groups.
69 */
70static int configfs_d_delete(struct dentry *dentry)
71{
72 return 1;
73}
74
75static struct dentry_operations configfs_dentry_ops = {
76 .d_iput = configfs_d_iput,
77 /* simple_delete_dentry() isn't exported */
78 .d_delete = configfs_d_delete,
79};
80
81/*
82 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
83 */
84static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
85 void * element)
86{
87 struct configfs_dirent * sd;
88
Robert P. J. Dayc3762222007-02-10 01:45:03 -080089 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -080090 if (!sd)
Louis Rilling107ed402008-06-16 19:01:00 +020091 return ERR_PTR(-ENOMEM);
Joel Becker7063fbf2005-12-15 14:29:43 -080092
Joel Becker7063fbf2005-12-15 14:29:43 -080093 atomic_set(&sd->s_count, 1);
94 INIT_LIST_HEAD(&sd->s_links);
95 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -080096 sd->s_element = element;
Louis Rilling6f610762008-06-16 19:00:58 +020097 spin_lock(&configfs_dirent_lock);
Louis Rillingb3e76af2008-06-16 19:01:01 +020098 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
99 spin_unlock(&configfs_dirent_lock);
100 kmem_cache_free(configfs_dir_cachep, sd);
101 return ERR_PTR(-ENOENT);
102 }
Louis Rilling6f610762008-06-16 19:00:58 +0200103 list_add(&sd->s_sibling, &parent_sd->s_children);
104 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800105
106 return sd;
107}
108
Joel Beckerb4c98f62006-09-13 11:01:19 -0700109/*
110 *
111 * Return -EEXIST if there is already a configfs element with the same
112 * name for the same parent.
113 *
114 * called with parent inode's i_mutex held
115 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100116static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
117 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700118{
119 struct configfs_dirent * sd;
120
121 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
122 if (sd->s_element) {
123 const unsigned char *existing = configfs_get_name(sd);
124 if (strcmp(existing, new))
125 continue;
126 else
127 return -EEXIST;
128 }
129 }
130
131 return 0;
132}
133
134
Joel Becker7063fbf2005-12-15 14:29:43 -0800135int configfs_make_dirent(struct configfs_dirent * parent_sd,
136 struct dentry * dentry, void * element,
137 umode_t mode, int type)
138{
139 struct configfs_dirent * sd;
140
141 sd = configfs_new_dirent(parent_sd, element);
Louis Rilling107ed402008-06-16 19:01:00 +0200142 if (IS_ERR(sd))
143 return PTR_ERR(sd);
Joel Becker7063fbf2005-12-15 14:29:43 -0800144
145 sd->s_mode = mode;
146 sd->s_type = type;
147 sd->s_dentry = dentry;
148 if (dentry) {
149 dentry->d_fsdata = configfs_get(sd);
150 dentry->d_op = &configfs_dentry_ops;
151 }
152
153 return 0;
154}
155
156static int init_dir(struct inode * inode)
157{
158 inode->i_op = &configfs_dir_inode_operations;
159 inode->i_fop = &configfs_dir_operations;
160
161 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700162 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800163 return 0;
164}
165
Dave Hansence8d2cd2007-10-16 23:31:13 -0700166static int configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800167{
168 inode->i_size = PAGE_SIZE;
169 inode->i_fop = &configfs_file_operations;
170 return 0;
171}
172
173static int init_symlink(struct inode * inode)
174{
175 inode->i_op = &configfs_symlink_inode_operations;
176 return 0;
177}
178
179static int create_dir(struct config_item * k, struct dentry * p,
180 struct dentry * d)
181{
182 int error;
183 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
184
Joel Beckerb4c98f62006-09-13 11:01:19 -0700185 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
186 if (!error)
187 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
188 CONFIGFS_DIR);
Joel Becker7063fbf2005-12-15 14:29:43 -0800189 if (!error) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800190 error = configfs_create(d, mode, init_dir);
Joel Becker7063fbf2005-12-15 14:29:43 -0800191 if (!error) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700192 inc_nlink(p->d_inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800193 (d)->d_op = &configfs_dentry_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800194 } else {
195 struct configfs_dirent *sd = d->d_fsdata;
196 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200197 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800198 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200199 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800200 configfs_put(sd);
201 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800202 }
203 }
204 return error;
205}
206
207
208/**
209 * configfs_create_dir - create a directory for an config_item.
210 * @item: config_itemwe're creating directory for.
211 * @dentry: config_item's dentry.
212 */
213
214static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
215{
216 struct dentry * parent;
217 int error = 0;
218
219 BUG_ON(!item);
220
221 if (item->ci_parent)
222 parent = item->ci_parent->ci_dentry;
223 else if (configfs_mount && configfs_mount->mnt_sb)
224 parent = configfs_mount->mnt_sb->s_root;
225 else
226 return -EFAULT;
227
228 error = create_dir(item,parent,dentry);
229 if (!error)
230 item->ci_dentry = dentry;
231 return error;
232}
233
234int configfs_create_link(struct configfs_symlink *sl,
235 struct dentry *parent,
236 struct dentry *dentry)
237{
238 int err = 0;
239 umode_t mode = S_IFLNK | S_IRWXUGO;
240
Joel Becker3d0f89b2006-01-25 13:31:07 -0800241 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
242 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800243 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800244 err = configfs_create(dentry, mode, init_symlink);
Joel Becker7063fbf2005-12-15 14:29:43 -0800245 if (!err)
246 dentry->d_op = &configfs_dentry_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800247 else {
248 struct configfs_dirent *sd = dentry->d_fsdata;
249 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200250 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800251 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200252 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800253 configfs_put(sd);
254 }
255 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800256 }
257 return err;
258}
259
260static void remove_dir(struct dentry * d)
261{
262 struct dentry * parent = dget(d->d_parent);
263 struct configfs_dirent * sd;
264
265 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200266 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800267 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200268 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800269 configfs_put(sd);
270 if (d->d_inode)
271 simple_rmdir(parent->d_inode,d);
272
273 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
274 atomic_read(&d->d_count));
275
276 dput(parent);
277}
278
279/**
280 * configfs_remove_dir - remove an config_item's directory.
281 * @item: config_item we're removing.
282 *
283 * The only thing special about this is that we remove any files in
284 * the directory before we remove the directory, and we've inlined
285 * what used to be configfs_rmdir() below, instead of calling separately.
286 */
287
288static void configfs_remove_dir(struct config_item * item)
289{
290 struct dentry * dentry = dget(item->ci_dentry);
291
292 if (!dentry)
293 return;
294
295 remove_dir(dentry);
296 /**
297 * Drop reference from dget() on entrance.
298 */
299 dput(dentry);
300}
301
302
303/* attaches attribute's configfs_dirent to the dentry corresponding to the
304 * attribute file
305 */
306static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
307{
308 struct configfs_attribute * attr = sd->s_element;
309 int error;
310
Joel Becker7063fbf2005-12-15 14:29:43 -0800311 dentry->d_fsdata = configfs_get(sd);
312 sd->s_dentry = dentry;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700313 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
314 configfs_init_file);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800315 if (error) {
316 configfs_put(sd);
317 return error;
318 }
319
320 dentry->d_op = &configfs_dentry_ops;
Joel Becker7063fbf2005-12-15 14:29:43 -0800321 d_rehash(dentry);
322
323 return 0;
324}
325
326static struct dentry * configfs_lookup(struct inode *dir,
327 struct dentry *dentry,
328 struct nameidata *nd)
329{
330 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
331 struct configfs_dirent * sd;
332 int found = 0;
333 int err = 0;
334
335 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
336 if (sd->s_type & CONFIGFS_NOT_PINNED) {
337 const unsigned char * name = configfs_get_name(sd);
338
339 if (strcmp(name, dentry->d_name.name))
340 continue;
341
342 found = 1;
343 err = configfs_attach_attr(sd, dentry);
344 break;
345 }
346 }
347
348 if (!found) {
349 /*
350 * If it doesn't exist and it isn't a NOT_PINNED item,
351 * it must be negative.
352 */
353 return simple_lookup(dir, dentry, nd);
354 }
355
356 return ERR_PTR(err);
357}
358
359/*
360 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Louis Rillingb3e76af2008-06-16 19:01:01 +0200361 * attributes and are removed by rmdir(). We recurse, setting
362 * CONFIGFS_USET_DROPPING on all children that are candidates for
363 * default detach.
364 * If there is an error, the caller will reset the flags via
365 * configfs_detach_rollback().
Joel Becker7063fbf2005-12-15 14:29:43 -0800366 */
Louis Rilling6d8344b2008-06-16 19:01:02 +0200367static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
Joel Becker7063fbf2005-12-15 14:29:43 -0800368{
369 struct configfs_dirent *parent_sd = dentry->d_fsdata;
370 struct configfs_dirent *sd;
371 int ret;
372
373 ret = -EBUSY;
374 if (!list_empty(&parent_sd->s_links))
375 goto out;
376
377 ret = 0;
378 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
379 if (sd->s_type & CONFIGFS_NOT_PINNED)
380 continue;
381 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Louis Rilling6d8344b2008-06-16 19:01:02 +0200382 /* Abort if racing with mkdir() */
383 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
384 if (wait_mutex)
385 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
386 return -EAGAIN;
387 }
Louis Rillingb3e76af2008-06-16 19:01:01 +0200388 /* Mark that we're trying to drop the group */
Joel Becker7063fbf2005-12-15 14:29:43 -0800389 sd->s_type |= CONFIGFS_USET_DROPPING;
390
Joel Becker631d1fe2007-06-18 18:06:09 -0700391 /*
392 * Yup, recursive. If there's a problem, blame
393 * deep nesting of default_groups
394 */
Louis Rilling6d8344b2008-06-16 19:01:02 +0200395 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800396 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800397 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800398 } else
399 ret = -ENOTEMPTY;
400
401 break;
402 }
403
404out:
405 return ret;
406}
407
408/*
Louis Rillingb3e76af2008-06-16 19:01:01 +0200409 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
Joel Becker7063fbf2005-12-15 14:29:43 -0800410 * set.
411 */
412static void configfs_detach_rollback(struct dentry *dentry)
413{
414 struct configfs_dirent *parent_sd = dentry->d_fsdata;
415 struct configfs_dirent *sd;
416
417 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
418 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
419 configfs_detach_rollback(sd->s_dentry);
Louis Rillingb3e76af2008-06-16 19:01:01 +0200420 sd->s_type &= ~CONFIGFS_USET_DROPPING;
Joel Becker7063fbf2005-12-15 14:29:43 -0800421 }
422 }
423}
424
425static void detach_attrs(struct config_item * item)
426{
427 struct dentry * dentry = dget(item->ci_dentry);
428 struct configfs_dirent * parent_sd;
429 struct configfs_dirent * sd, * tmp;
430
431 if (!dentry)
432 return;
433
434 pr_debug("configfs %s: dropping attrs for dir\n",
435 dentry->d_name.name);
436
437 parent_sd = dentry->d_fsdata;
438 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
439 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
440 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200441 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800442 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200443 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800444 configfs_drop_dentry(sd, dentry);
445 configfs_put(sd);
446 }
447
448 /**
449 * Drop reference from dget() on entrance.
450 */
451 dput(dentry);
452}
453
454static int populate_attrs(struct config_item *item)
455{
456 struct config_item_type *t = item->ci_type;
457 struct configfs_attribute *attr;
458 int error = 0;
459 int i;
460
461 if (!t)
462 return -EINVAL;
463 if (t->ct_attrs) {
464 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
465 if ((error = configfs_create_file(item, attr)))
466 break;
467 }
468 }
469
470 if (error)
471 detach_attrs(item);
472
473 return error;
474}
475
476static int configfs_attach_group(struct config_item *parent_item,
477 struct config_item *item,
478 struct dentry *dentry);
479static void configfs_detach_group(struct config_item *item);
480
481static void detach_groups(struct config_group *group)
482{
483 struct dentry * dentry = dget(group->cg_item.ci_dentry);
484 struct dentry *child;
485 struct configfs_dirent *parent_sd;
486 struct configfs_dirent *sd, *tmp;
487
488 if (!dentry)
489 return;
490
491 parent_sd = dentry->d_fsdata;
492 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
493 if (!sd->s_element ||
494 !(sd->s_type & CONFIGFS_USET_DEFAULT))
495 continue;
496
497 child = sd->s_dentry;
498
Louis Rillingb3e76af2008-06-16 19:01:01 +0200499 mutex_lock(&child->d_inode->i_mutex);
500
Joel Becker7063fbf2005-12-15 14:29:43 -0800501 configfs_detach_group(sd->s_element);
502 child->d_inode->i_flags |= S_DEAD;
503
Louis Rillingb3e76af2008-06-16 19:01:01 +0200504 mutex_unlock(&child->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800505
506 d_delete(child);
507 dput(child);
508 }
509
510 /**
511 * Drop reference from dget() on entrance.
512 */
513 dput(dentry);
514}
515
516/*
517 * This fakes mkdir(2) on a default_groups[] entry. It
518 * creates a dentry, attachs it, and then does fixup
519 * on the sd->s_type.
520 *
521 * We could, perhaps, tweak our parent's ->mkdir for a minute and
522 * try using vfs_mkdir. Just a thought.
523 */
524static int create_default_group(struct config_group *parent_group,
525 struct config_group *group)
526{
527 int ret;
528 struct qstr name;
529 struct configfs_dirent *sd;
530 /* We trust the caller holds a reference to parent */
531 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
532
533 if (!group->cg_item.ci_name)
534 group->cg_item.ci_name = group->cg_item.ci_namebuf;
535 name.name = group->cg_item.ci_name;
536 name.len = strlen(name.name);
537 name.hash = full_name_hash(name.name, name.len);
538
539 ret = -ENOMEM;
540 child = d_alloc(parent, &name);
541 if (child) {
542 d_add(child, NULL);
543
544 ret = configfs_attach_group(&parent_group->cg_item,
545 &group->cg_item, child);
546 if (!ret) {
547 sd = child->d_fsdata;
548 sd->s_type |= CONFIGFS_USET_DEFAULT;
549 } else {
550 d_delete(child);
551 dput(child);
552 }
553 }
554
555 return ret;
556}
557
558static int populate_groups(struct config_group *group)
559{
560 struct config_group *new_group;
561 struct dentry *dentry = group->cg_item.ci_dentry;
562 int ret = 0;
563 int i;
564
Eric Sesterhenncbca6922006-03-23 00:36:54 +0100565 if (group->default_groups) {
Joel Beckereed7a0d2006-04-11 21:37:20 -0700566 /*
567 * FYI, we're faking mkdir here
Joel Becker7063fbf2005-12-15 14:29:43 -0800568 * I'm not sure we need this semaphore, as we're called
569 * from our parent's mkdir. That holds our parent's
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800570 * i_mutex, so afaik lookup cannot continue through our
Joel Becker7063fbf2005-12-15 14:29:43 -0800571 * parent to find us, let alone mess with our tree.
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800572 * That said, taking our i_mutex is closer to mkdir
Joel Beckereed7a0d2006-04-11 21:37:20 -0700573 * emulation, and shouldn't hurt.
574 */
Joonwoo Parkba611ed2007-12-26 12:09:57 +0900575 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Joel Becker7063fbf2005-12-15 14:29:43 -0800576
577 for (i = 0; group->default_groups[i]; i++) {
578 new_group = group->default_groups[i];
579
580 ret = create_default_group(group, new_group);
581 if (ret)
582 break;
583 }
584
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800585 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800586 }
587
588 if (ret)
589 detach_groups(group);
590
591 return ret;
592}
593
594/*
595 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700596 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800597 */
598
599static void unlink_obj(struct config_item *item)
600{
601 struct config_group *group;
602
603 group = item->ci_group;
604 if (group) {
605 list_del_init(&item->ci_entry);
606
607 item->ci_group = NULL;
608 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700609
610 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800611 config_item_put(item);
612
Joel Beckereed7a0d2006-04-11 21:37:20 -0700613 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800614 config_group_put(group);
615 }
616}
617
618static void link_obj(struct config_item *parent_item, struct config_item *item)
619{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700620 /*
621 * Parent seems redundant with group, but it makes certain
622 * traversals much nicer.
623 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800624 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700625
626 /*
627 * We hold a reference on the parent for the child's ci_parent
628 * link.
629 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800630 item->ci_group = config_group_get(to_config_group(parent_item));
631 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
632
Joel Beckereed7a0d2006-04-11 21:37:20 -0700633 /*
634 * We hold a reference on the child for ci_entry on the parent's
635 * cg_children
636 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800637 config_item_get(item);
638}
639
640static void unlink_group(struct config_group *group)
641{
642 int i;
643 struct config_group *new_group;
644
645 if (group->default_groups) {
646 for (i = 0; group->default_groups[i]; i++) {
647 new_group = group->default_groups[i];
648 unlink_group(new_group);
649 }
650 }
651
652 group->cg_subsys = NULL;
653 unlink_obj(&group->cg_item);
654}
655
656static void link_group(struct config_group *parent_group, struct config_group *group)
657{
658 int i;
659 struct config_group *new_group;
660 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
661
662 link_obj(&parent_group->cg_item, &group->cg_item);
663
664 if (parent_group->cg_subsys)
665 subsys = parent_group->cg_subsys;
666 else if (configfs_is_root(&parent_group->cg_item))
667 subsys = to_configfs_subsystem(group);
668 else
669 BUG();
670 group->cg_subsys = subsys;
671
672 if (group->default_groups) {
673 for (i = 0; group->default_groups[i]; i++) {
674 new_group = group->default_groups[i];
675 link_group(group, new_group);
676 }
677 }
678}
679
680/*
681 * The goal is that configfs_attach_item() (and
682 * configfs_attach_group()) can be called from either the VFS or this
683 * module. That is, they assume that the items have been created,
684 * the dentry allocated, and the dcache is all ready to go.
685 *
686 * If they fail, they must clean up after themselves as if they
687 * had never been called. The caller (VFS or local function) will
688 * handle cleaning up the dcache bits.
689 *
690 * configfs_detach_group() and configfs_detach_item() behave similarly on
691 * the way out. They assume that the proper semaphores are held, they
692 * clean up the configfs items, and they expect their callers will
693 * handle the dcache bits.
694 */
695static int configfs_attach_item(struct config_item *parent_item,
696 struct config_item *item,
697 struct dentry *dentry)
698{
699 int ret;
700
701 ret = configfs_create_dir(item, dentry);
702 if (!ret) {
703 ret = populate_attrs(item);
704 if (ret) {
705 configfs_remove_dir(item);
706 d_delete(dentry);
707 }
708 }
709
710 return ret;
711}
712
713static void configfs_detach_item(struct config_item *item)
714{
715 detach_attrs(item);
716 configfs_remove_dir(item);
717}
718
719static int configfs_attach_group(struct config_item *parent_item,
720 struct config_item *item,
721 struct dentry *dentry)
722{
723 int ret;
724 struct configfs_dirent *sd;
725
726 ret = configfs_attach_item(parent_item, item, dentry);
727 if (!ret) {
728 sd = dentry->d_fsdata;
729 sd->s_type |= CONFIGFS_USET_DIR;
730
731 ret = populate_groups(to_config_group(item));
732 if (ret) {
733 configfs_detach_item(item);
734 d_delete(dentry);
735 }
736 }
737
738 return ret;
739}
740
741static void configfs_detach_group(struct config_item *item)
742{
743 detach_groups(to_config_group(item));
744 configfs_detach_item(item);
745}
746
747/*
Joel Becker299894c2006-10-06 17:33:23 -0700748 * After the item has been detached from the filesystem view, we are
749 * ready to tear it out of the hierarchy. Notify the client before
750 * we do that so they can perform any cleanup that requires
751 * navigating the hierarchy. A client does not need to provide this
752 * callback. The subsystem semaphore MUST be held by the caller, and
753 * references must be valid for both items. It also assumes the
754 * caller has validated ci_type.
755 */
756static void client_disconnect_notify(struct config_item *parent_item,
757 struct config_item *item)
758{
759 struct config_item_type *type;
760
761 type = parent_item->ci_type;
762 BUG_ON(!type);
763
764 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
765 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
766 item);
767}
768
769/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800770 * Drop the initial reference from make_item()/make_group()
771 * This function assumes that reference is held on item
772 * and that item holds a valid reference to the parent. Also, it
773 * assumes the caller has validated ci_type.
774 */
775static void client_drop_item(struct config_item *parent_item,
776 struct config_item *item)
777{
778 struct config_item_type *type;
779
780 type = parent_item->ci_type;
781 BUG_ON(!type);
782
Joel Beckereed7a0d2006-04-11 21:37:20 -0700783 /*
784 * If ->drop_item() exists, it is responsible for the
785 * config_item_put().
786 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800787 if (type->ct_group_ops && type->ct_group_ops->drop_item)
788 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700789 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800790 else
791 config_item_put(item);
792}
793
Joel Becker631d1fe2007-06-18 18:06:09 -0700794#ifdef DEBUG
795static void configfs_dump_one(struct configfs_dirent *sd, int level)
796{
797 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
798
799#define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
800 type_print(CONFIGFS_ROOT);
801 type_print(CONFIGFS_DIR);
802 type_print(CONFIGFS_ITEM_ATTR);
803 type_print(CONFIGFS_ITEM_LINK);
804 type_print(CONFIGFS_USET_DIR);
805 type_print(CONFIGFS_USET_DEFAULT);
806 type_print(CONFIGFS_USET_DROPPING);
807#undef type_print
808}
809
810static int configfs_dump(struct configfs_dirent *sd, int level)
811{
812 struct configfs_dirent *child_sd;
813 int ret = 0;
814
815 configfs_dump_one(sd, level);
816
817 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
818 return 0;
819
820 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
821 ret = configfs_dump(child_sd, level + 2);
822 if (ret)
823 break;
824 }
825
826 return ret;
827}
828#endif
829
830
831/*
832 * configfs_depend_item() and configfs_undepend_item()
833 *
834 * WARNING: Do not call these from a configfs callback!
835 *
836 * This describes these functions and their helpers.
837 *
838 * Allow another kernel system to depend on a config_item. If this
839 * happens, the item cannot go away until the dependant can live without
840 * it. The idea is to give client modules as simple an interface as
841 * possible. When a system asks them to depend on an item, they just
842 * call configfs_depend_item(). If the item is live and the client
843 * driver is in good shape, we'll happily do the work for them.
844 *
845 * Why is the locking complex? Because configfs uses the VFS to handle
846 * all locking, but this function is called outside the normal
847 * VFS->configfs path. So it must take VFS locks to prevent the
848 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
849 * why you can't call these functions underneath configfs callbacks.
850 *
851 * Note, btw, that this can be called at *any* time, even when a configfs
852 * subsystem isn't registered, or when configfs is loading or unloading.
853 * Just like configfs_register_subsystem(). So we take the same
854 * precautions. We pin the filesystem. We lock each i_mutex _in_order_
855 * on our way down the tree. If we can find the target item in the
856 * configfs tree, it must be part of the subsystem tree as well, so we
857 * do not need the subsystem semaphore. Holding the i_mutex chain locks
858 * out mkdir() and rmdir(), who might be racing us.
859 */
860
861/*
862 * configfs_depend_prep()
863 *
864 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
865 * attributes. This is similar but not the same to configfs_detach_prep().
866 * Note that configfs_detach_prep() expects the parent to be locked when it
867 * is called, but we lock the parent *inside* configfs_depend_prep(). We
868 * do that so we can unlock it if we find nothing.
869 *
870 * Here we do a depth-first search of the dentry hierarchy looking for
871 * our object. We take i_mutex on each step of the way down. IT IS
872 * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
873 * we'll drop the i_mutex.
874 *
875 * If the target is not found, -ENOENT is bubbled up and we have released
876 * all locks. If the target was found, the locks will be cleared by
877 * configfs_depend_rollback().
878 *
879 * This adds a requirement that all config_items be unique!
880 *
881 * This is recursive because the locking traversal is tricky. There isn't
882 * much on the stack, though, so folks that need this function - be careful
883 * about your stack! Patches will be accepted to make it iterative.
884 */
885static int configfs_depend_prep(struct dentry *origin,
886 struct config_item *target)
887{
888 struct configfs_dirent *child_sd, *sd = origin->d_fsdata;
889 int ret = 0;
890
891 BUG_ON(!origin || !sd);
892
893 /* Lock this guy on the way down */
894 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
895 if (sd->s_element == target) /* Boo-yah */
896 goto out;
897
898 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
899 if (child_sd->s_type & CONFIGFS_DIR) {
900 ret = configfs_depend_prep(child_sd->s_dentry,
901 target);
902 if (!ret)
903 goto out; /* Child path boo-yah */
904 }
905 }
906
907 /* We looped all our children and didn't find target */
908 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
909 ret = -ENOENT;
910
911out:
912 return ret;
913}
914
915/*
916 * This is ONLY called if configfs_depend_prep() did its job. So we can
917 * trust the entire path from item back up to origin.
918 *
919 * We walk backwards from item, unlocking each i_mutex. We finish by
920 * unlocking origin.
921 */
922static void configfs_depend_rollback(struct dentry *origin,
923 struct config_item *item)
924{
925 struct dentry *dentry = item->ci_dentry;
926
927 while (dentry != origin) {
928 mutex_unlock(&dentry->d_inode->i_mutex);
929 dentry = dentry->d_parent;
930 }
931
932 mutex_unlock(&origin->d_inode->i_mutex);
933}
934
935int configfs_depend_item(struct configfs_subsystem *subsys,
936 struct config_item *target)
937{
938 int ret;
939 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
940 struct config_item *s_item = &subsys->su_group.cg_item;
941
942 /*
943 * Pin the configfs filesystem. This means we can safely access
944 * the root of the configfs filesystem.
945 */
946 ret = configfs_pin_fs();
947 if (ret)
948 return ret;
949
950 /*
951 * Next, lock the root directory. We're going to check that the
952 * subsystem is really registered, and so we need to lock out
953 * configfs_[un]register_subsystem().
954 */
955 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
956
957 root_sd = configfs_sb->s_root->d_fsdata;
958
959 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
960 if (p->s_type & CONFIGFS_DIR) {
961 if (p->s_element == s_item) {
962 subsys_sd = p;
963 break;
964 }
965 }
966 }
967
968 if (!subsys_sd) {
969 ret = -ENOENT;
970 goto out_unlock_fs;
971 }
972
973 /* Ok, now we can trust subsys/s_item */
974
975 /* Scan the tree, locking i_mutex recursively, return 0 if found */
976 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
977 if (ret)
978 goto out_unlock_fs;
979
980 /* We hold all i_mutexes from the subsystem down to the target */
981 p = target->ci_dentry->d_fsdata;
982 p->s_dependent_count += 1;
983
984 configfs_depend_rollback(subsys_sd->s_dentry, target);
985
986out_unlock_fs:
987 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
988
989 /*
990 * If we succeeded, the fs is pinned via other methods. If not,
991 * we're done with it anyway. So release_fs() is always right.
992 */
993 configfs_release_fs();
994
995 return ret;
996}
997EXPORT_SYMBOL(configfs_depend_item);
998
999/*
1000 * Release the dependent linkage. This is much simpler than
1001 * configfs_depend_item() because we know that that the client driver is
1002 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1003 */
1004void configfs_undepend_item(struct configfs_subsystem *subsys,
1005 struct config_item *target)
1006{
1007 struct configfs_dirent *sd;
1008
1009 /*
1010 * Since we can trust everything is pinned, we just need i_mutex
1011 * on the item.
1012 */
1013 mutex_lock(&target->ci_dentry->d_inode->i_mutex);
1014
1015 sd = target->ci_dentry->d_fsdata;
1016 BUG_ON(sd->s_dependent_count < 1);
1017
1018 sd->s_dependent_count -= 1;
1019
1020 /*
1021 * After this unlock, we cannot trust the item to stay alive!
1022 * DO NOT REFERENCE item after this unlock.
1023 */
1024 mutex_unlock(&target->ci_dentry->d_inode->i_mutex);
1025}
1026EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001027
1028static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1029{
Joel Beckereed7a0d2006-04-11 21:37:20 -07001030 int ret, module_got = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001031 struct config_group *group;
1032 struct config_item *item;
1033 struct config_item *parent_item;
1034 struct configfs_subsystem *subsys;
1035 struct configfs_dirent *sd;
1036 struct config_item_type *type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001037 struct module *owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001038 char *name;
1039
Joel Becker84efad12006-03-27 18:46:09 -08001040 if (dentry->d_parent == configfs_sb->s_root) {
1041 ret = -EPERM;
1042 goto out;
1043 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001044
1045 sd = dentry->d_parent->d_fsdata;
Joel Becker84efad12006-03-27 18:46:09 -08001046 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1047 ret = -EPERM;
1048 goto out;
1049 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001050
Joel Becker84efad12006-03-27 18:46:09 -08001051 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001052 parent_item = configfs_get_config_item(dentry->d_parent);
1053 type = parent_item->ci_type;
1054 subsys = to_config_group(parent_item)->cg_subsys;
1055 BUG_ON(!subsys);
1056
1057 if (!type || !type->ct_group_ops ||
1058 (!type->ct_group_ops->make_group &&
1059 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001060 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1061 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001062 }
1063
1064 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1065 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001066 ret = -ENOMEM;
1067 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001068 }
Joel Becker84efad12006-03-27 18:46:09 -08001069
Joel Becker7063fbf2005-12-15 14:29:43 -08001070 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1071
Joel Beckere6bd07a2007-07-06 23:33:17 -07001072 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001073 group = NULL;
1074 item = NULL;
1075 if (type->ct_group_ops->make_group) {
Joel Becker11c3b792008-06-12 14:00:18 -07001076 ret = type->ct_group_ops->make_group(to_config_group(parent_item), name, &group);
1077 if (!ret) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001078 link_group(to_config_group(parent_item), group);
1079 item = &group->cg_item;
1080 }
1081 } else {
Joel Becker11c3b792008-06-12 14:00:18 -07001082 ret = type->ct_group_ops->make_item(to_config_group(parent_item), name, &item);
1083 if (!ret)
Joel Becker7063fbf2005-12-15 14:29:43 -08001084 link_obj(parent_item, item);
1085 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001086 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001087
1088 kfree(name);
Joel Becker11c3b792008-06-12 14:00:18 -07001089 if (ret) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001090 /*
Joel Becker11c3b792008-06-12 14:00:18 -07001091 * If ret != 0, then link_obj() was never called.
Joel Beckereed7a0d2006-04-11 21:37:20 -07001092 * There are no extra references to clean up.
1093 */
Joel Becker84efad12006-03-27 18:46:09 -08001094 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001095 }
1096
Joel Beckereed7a0d2006-04-11 21:37:20 -07001097 /*
1098 * link_obj() has been called (via link_group() for groups).
1099 * From here on out, errors must clean that up.
1100 */
1101
Joel Becker7063fbf2005-12-15 14:29:43 -08001102 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001103 if (!type) {
1104 ret = -EINVAL;
1105 goto out_unlink;
1106 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001107
Joel Beckereed7a0d2006-04-11 21:37:20 -07001108 owner = type->ct_owner;
1109 if (!try_module_get(owner)) {
1110 ret = -EINVAL;
1111 goto out_unlink;
1112 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001113
Joel Beckereed7a0d2006-04-11 21:37:20 -07001114 /*
1115 * I hate doing it this way, but if there is
1116 * an error, module_put() probably should
1117 * happen after any cleanup.
1118 */
1119 module_got = 1;
1120
Louis Rilling6d8344b2008-06-16 19:01:02 +02001121 /*
1122 * Make racing rmdir() fail if it did not tag parent with
1123 * CONFIGFS_USET_DROPPING
1124 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1125 * fail and let rmdir() terminate correctly
1126 */
1127 spin_lock(&configfs_dirent_lock);
1128 /* This will make configfs_detach_prep() fail */
1129 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1130 spin_unlock(&configfs_dirent_lock);
1131
Joel Beckereed7a0d2006-04-11 21:37:20 -07001132 if (group)
1133 ret = configfs_attach_group(parent_item, item, dentry);
1134 else
1135 ret = configfs_attach_item(parent_item, item, dentry);
1136
Louis Rilling6d8344b2008-06-16 19:01:02 +02001137 spin_lock(&configfs_dirent_lock);
1138 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1139 spin_unlock(&configfs_dirent_lock);
1140
Joel Beckereed7a0d2006-04-11 21:37:20 -07001141out_unlink:
1142 if (ret) {
1143 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001144 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001145
1146 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001147 if (group)
1148 unlink_group(group);
1149 else
1150 unlink_obj(item);
1151 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001152
Joel Beckere6bd07a2007-07-06 23:33:17 -07001153 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001154
1155 if (module_got)
1156 module_put(owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001157 }
1158
Joel Becker84efad12006-03-27 18:46:09 -08001159out_put:
1160 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001161 * link_obj()/link_group() took a reference from child->parent,
1162 * so the parent is safely pinned. We can drop our working
1163 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001164 */
1165 config_item_put(parent_item);
1166
1167out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001168 return ret;
1169}
1170
1171static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1172{
1173 struct config_item *parent_item;
1174 struct config_item *item;
1175 struct configfs_subsystem *subsys;
1176 struct configfs_dirent *sd;
1177 struct module *owner = NULL;
1178 int ret;
1179
1180 if (dentry->d_parent == configfs_sb->s_root)
1181 return -EPERM;
1182
1183 sd = dentry->d_fsdata;
1184 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1185 return -EPERM;
1186
Joel Becker631d1fe2007-06-18 18:06:09 -07001187 /*
1188 * Here's where we check for dependents. We're protected by
1189 * i_mutex.
1190 */
1191 if (sd->s_dependent_count)
1192 return -EBUSY;
1193
Joel Becker84efad12006-03-27 18:46:09 -08001194 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001195 parent_item = configfs_get_config_item(dentry->d_parent);
1196 subsys = to_config_group(parent_item)->cg_subsys;
1197 BUG_ON(!subsys);
1198
1199 if (!parent_item->ci_type) {
1200 config_item_put(parent_item);
1201 return -EINVAL;
1202 }
1203
Louis Rillingb3e76af2008-06-16 19:01:01 +02001204 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001205 do {
1206 struct mutex *wait_mutex;
1207
1208 ret = configfs_detach_prep(dentry, &wait_mutex);
1209 if (ret) {
1210 configfs_detach_rollback(dentry);
1211 spin_unlock(&configfs_dirent_lock);
1212 if (ret != -EAGAIN) {
1213 config_item_put(parent_item);
1214 return ret;
1215 }
1216
1217 /* Wait until the racing operation terminates */
1218 mutex_lock(wait_mutex);
1219 mutex_unlock(wait_mutex);
1220
1221 spin_lock(&configfs_dirent_lock);
1222 }
1223 } while (ret == -EAGAIN);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001224 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001225
Joel Becker84efad12006-03-27 18:46:09 -08001226 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001227 item = configfs_get_config_item(dentry);
1228
1229 /* Drop reference from above, item already holds one. */
1230 config_item_put(parent_item);
1231
1232 if (item->ci_type)
1233 owner = item->ci_type->ct_owner;
1234
1235 if (sd->s_type & CONFIGFS_USET_DIR) {
1236 configfs_detach_group(item);
1237
Joel Beckere6bd07a2007-07-06 23:33:17 -07001238 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001239 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001240 unlink_group(to_config_group(item));
1241 } else {
1242 configfs_detach_item(item);
1243
Joel Beckere6bd07a2007-07-06 23:33:17 -07001244 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001245 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001246 unlink_obj(item);
1247 }
1248
1249 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001250 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001251
1252 /* Drop our reference from above */
1253 config_item_put(item);
1254
1255 module_put(owner);
1256
1257 return 0;
1258}
1259
Arjan van de Ven754661f2007-02-12 00:55:38 -08001260const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001261 .mkdir = configfs_mkdir,
1262 .rmdir = configfs_rmdir,
1263 .symlink = configfs_symlink,
1264 .unlink = configfs_unlink,
1265 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001266 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001267};
1268
1269#if 0
1270int configfs_rename_dir(struct config_item * item, const char *new_name)
1271{
1272 int error = 0;
1273 struct dentry * new_dentry, * parent;
1274
1275 if (!strcmp(config_item_name(item), new_name))
1276 return -EINVAL;
1277
1278 if (!item->parent)
1279 return -EINVAL;
1280
1281 down_write(&configfs_rename_sem);
1282 parent = item->parent->dentry;
1283
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001284 mutex_lock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001285
1286 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1287 if (!IS_ERR(new_dentry)) {
Joel Beckere7515d02006-03-10 11:42:30 -08001288 if (!new_dentry->d_inode) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001289 error = config_item_set_name(item, "%s", new_name);
1290 if (!error) {
1291 d_add(new_dentry, NULL);
1292 d_move(item->dentry, new_dentry);
1293 }
1294 else
1295 d_delete(new_dentry);
1296 } else
1297 error = -EEXIST;
1298 dput(new_dentry);
1299 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001300 mutex_unlock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001301 up_write(&configfs_rename_sem);
1302
1303 return error;
1304}
1305#endif
1306
1307static int configfs_dir_open(struct inode *inode, struct file *file)
1308{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001309 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001310 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1311
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001312 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001313 file->private_data = configfs_new_dirent(parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001314 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001315
Louis Rilling107ed402008-06-16 19:01:00 +02001316 return IS_ERR(file->private_data) ? PTR_ERR(file->private_data) : 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001317
1318}
1319
1320static int configfs_dir_close(struct inode *inode, struct file *file)
1321{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001322 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001323 struct configfs_dirent * cursor = file->private_data;
1324
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001325 mutex_lock(&dentry->d_inode->i_mutex);
Louis Rilling6f610762008-06-16 19:00:58 +02001326 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001327 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001328 spin_unlock(&configfs_dirent_lock);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001329 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001330
1331 release_configfs_dirent(cursor);
1332
1333 return 0;
1334}
1335
1336/* Relationship between s_mode and the DT_xxx types */
1337static inline unsigned char dt_type(struct configfs_dirent *sd)
1338{
1339 return (sd->s_mode >> 12) & 15;
1340}
1341
1342static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1343{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001344 struct dentry *dentry = filp->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001345 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1346 struct configfs_dirent *cursor = filp->private_data;
1347 struct list_head *p, *q = &cursor->s_sibling;
1348 ino_t ino;
1349 int i = filp->f_pos;
1350
1351 switch (i) {
1352 case 0:
1353 ino = dentry->d_inode->i_ino;
1354 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1355 break;
1356 filp->f_pos++;
1357 i++;
1358 /* fallthrough */
1359 case 1:
1360 ino = parent_ino(dentry);
1361 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1362 break;
1363 filp->f_pos++;
1364 i++;
1365 /* fallthrough */
1366 default:
1367 if (filp->f_pos == 2) {
Louis Rilling6f610762008-06-16 19:00:58 +02001368 spin_lock(&configfs_dirent_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001369 list_move(q, &parent_sd->s_children);
Louis Rilling6f610762008-06-16 19:00:58 +02001370 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001371 }
1372 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1373 struct configfs_dirent *next;
1374 const char * name;
1375 int len;
1376
1377 next = list_entry(p, struct configfs_dirent,
1378 s_sibling);
1379 if (!next->s_element)
1380 continue;
1381
1382 name = configfs_get_name(next);
1383 len = strlen(name);
1384 if (next->s_dentry)
1385 ino = next->s_dentry->d_inode->i_ino;
1386 else
1387 ino = iunique(configfs_sb, 2);
1388
1389 if (filldir(dirent, name, len, filp->f_pos, ino,
1390 dt_type(next)) < 0)
1391 return 0;
1392
Louis Rilling6f610762008-06-16 19:00:58 +02001393 spin_lock(&configfs_dirent_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001394 list_move(q, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001395 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001396 p = q;
1397 filp->f_pos++;
1398 }
1399 }
1400 return 0;
1401}
1402
1403static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1404{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001405 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001406
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001407 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001408 switch (origin) {
1409 case 1:
1410 offset += file->f_pos;
1411 case 0:
1412 if (offset >= 0)
1413 break;
1414 default:
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001415 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001416 return -EINVAL;
1417 }
1418 if (offset != file->f_pos) {
1419 file->f_pos = offset;
1420 if (file->f_pos >= 2) {
1421 struct configfs_dirent *sd = dentry->d_fsdata;
1422 struct configfs_dirent *cursor = file->private_data;
1423 struct list_head *p;
1424 loff_t n = file->f_pos - 2;
1425
Louis Rilling6f610762008-06-16 19:00:58 +02001426 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001427 list_del(&cursor->s_sibling);
1428 p = sd->s_children.next;
1429 while (n && p != &sd->s_children) {
1430 struct configfs_dirent *next;
1431 next = list_entry(p, struct configfs_dirent,
1432 s_sibling);
1433 if (next->s_element)
1434 n--;
1435 p = p->next;
1436 }
1437 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001438 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001439 }
1440 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001441 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001442 return offset;
1443}
1444
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001445const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001446 .open = configfs_dir_open,
1447 .release = configfs_dir_close,
1448 .llseek = configfs_dir_lseek,
1449 .read = generic_read_dir,
1450 .readdir = configfs_readdir,
1451};
1452
1453int configfs_register_subsystem(struct configfs_subsystem *subsys)
1454{
1455 int err;
1456 struct config_group *group = &subsys->su_group;
1457 struct qstr name;
1458 struct dentry *dentry;
1459 struct configfs_dirent *sd;
1460
1461 err = configfs_pin_fs();
1462 if (err)
1463 return err;
1464
1465 if (!group->cg_item.ci_name)
1466 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1467
1468 sd = configfs_sb->s_root->d_fsdata;
1469 link_group(to_config_group(sd->s_element), group);
1470
Joonwoo Parkba611ed2007-12-26 12:09:57 +09001471 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1472 I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001473
1474 name.name = group->cg_item.ci_name;
1475 name.len = strlen(name.name);
1476 name.hash = full_name_hash(name.name, name.len);
1477
1478 err = -ENOMEM;
1479 dentry = d_alloc(configfs_sb->s_root, &name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001480 if (dentry) {
1481 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001482
Joel Beckerafdf04e2007-03-05 15:49:49 -08001483 err = configfs_attach_group(sd->s_element, &group->cg_item,
1484 dentry);
1485 if (err) {
1486 d_delete(dentry);
1487 dput(dentry);
1488 }
1489 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001490
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001491 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001492
Joel Beckerafdf04e2007-03-05 15:49:49 -08001493 if (err) {
1494 unlink_group(group);
1495 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001496 }
1497
1498 return err;
1499}
1500
1501void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1502{
1503 struct config_group *group = &subsys->su_group;
1504 struct dentry *dentry = group->cg_item.ci_dentry;
1505
1506 if (dentry->d_parent != configfs_sb->s_root) {
1507 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1508 return;
1509 }
1510
Mark Fasheh55ed1602006-10-20 14:55:54 -07001511 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1512 I_MUTEX_PARENT);
1513 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Louis Rillingb3e76af2008-06-16 19:01:01 +02001514 spin_lock(&configfs_dirent_lock);
Louis Rilling6d8344b2008-06-16 19:01:02 +02001515 if (configfs_detach_prep(dentry, NULL)) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001516 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1517 }
Louis Rillingb3e76af2008-06-16 19:01:01 +02001518 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001519 configfs_detach_group(&group->cg_item);
1520 dentry->d_inode->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001521 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001522
1523 d_delete(dentry);
1524
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001525 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001526
1527 dput(dentry);
1528
1529 unlink_group(group);
1530 configfs_release_fs();
1531}
1532
1533EXPORT_SYMBOL(configfs_register_subsystem);
1534EXPORT_SYMBOL(configfs_unregister_subsystem);