blob: a08e5c2f25e88b1da0abdf646a31486d9a833248 [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>
33
34#include <linux/configfs.h>
35#include "configfs_internal.h"
36
37DECLARE_RWSEM(configfs_rename_sem);
Louis Rilling6f610762008-06-16 19:00:58 +020038/*
39 * Protects mutations of configfs_dirent linkage together with proper i_mutex
Louis Rilling5301a772008-06-16 19:00:59 +020040 * Also protects mutations of symlinks linkage to target configfs_dirent
Louis Rilling6f610762008-06-16 19:00:58 +020041 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
42 * and configfs_dirent_lock locked, in that order.
Louis Rilling5301a772008-06-16 19:00:59 +020043 * This allows one to safely traverse configfs_dirent trees and symlinks without
44 * having to lock inodes.
Louis Rilling6f610762008-06-16 19:00:58 +020045 */
46DEFINE_SPINLOCK(configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080047
48static void configfs_d_iput(struct dentry * dentry,
49 struct inode * inode)
50{
51 struct configfs_dirent * sd = dentry->d_fsdata;
52
53 if (sd) {
54 BUG_ON(sd->s_dentry != dentry);
55 sd->s_dentry = NULL;
56 configfs_put(sd);
57 }
58 iput(inode);
59}
60
61/*
62 * We _must_ delete our dentries on last dput, as the chain-to-parent
63 * behavior is required to clear the parents of default_groups.
64 */
65static int configfs_d_delete(struct dentry *dentry)
66{
67 return 1;
68}
69
70static struct dentry_operations configfs_dentry_ops = {
71 .d_iput = configfs_d_iput,
72 /* simple_delete_dentry() isn't exported */
73 .d_delete = configfs_d_delete,
74};
75
76/*
77 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
78 */
79static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
80 void * element)
81{
82 struct configfs_dirent * sd;
83
Robert P. J. Dayc3762222007-02-10 01:45:03 -080084 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
Joel Becker7063fbf2005-12-15 14:29:43 -080085 if (!sd)
86 return NULL;
87
Joel Becker7063fbf2005-12-15 14:29:43 -080088 atomic_set(&sd->s_count, 1);
89 INIT_LIST_HEAD(&sd->s_links);
90 INIT_LIST_HEAD(&sd->s_children);
Joel Becker7063fbf2005-12-15 14:29:43 -080091 sd->s_element = element;
Louis Rilling6f610762008-06-16 19:00:58 +020092 spin_lock(&configfs_dirent_lock);
93 list_add(&sd->s_sibling, &parent_sd->s_children);
94 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080095
96 return sd;
97}
98
Joel Beckerb4c98f62006-09-13 11:01:19 -070099/*
100 *
101 * Return -EEXIST if there is already a configfs element with the same
102 * name for the same parent.
103 *
104 * called with parent inode's i_mutex held
105 */
Adrian Bunk58d206c2006-11-20 03:24:00 +0100106static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
107 const unsigned char *new)
Joel Beckerb4c98f62006-09-13 11:01:19 -0700108{
109 struct configfs_dirent * sd;
110
111 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
112 if (sd->s_element) {
113 const unsigned char *existing = configfs_get_name(sd);
114 if (strcmp(existing, new))
115 continue;
116 else
117 return -EEXIST;
118 }
119 }
120
121 return 0;
122}
123
124
Joel Becker7063fbf2005-12-15 14:29:43 -0800125int configfs_make_dirent(struct configfs_dirent * parent_sd,
126 struct dentry * dentry, void * element,
127 umode_t mode, int type)
128{
129 struct configfs_dirent * sd;
130
131 sd = configfs_new_dirent(parent_sd, element);
132 if (!sd)
133 return -ENOMEM;
134
135 sd->s_mode = mode;
136 sd->s_type = type;
137 sd->s_dentry = dentry;
138 if (dentry) {
139 dentry->d_fsdata = configfs_get(sd);
140 dentry->d_op = &configfs_dentry_ops;
141 }
142
143 return 0;
144}
145
146static int init_dir(struct inode * inode)
147{
148 inode->i_op = &configfs_dir_inode_operations;
149 inode->i_fop = &configfs_dir_operations;
150
151 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700152 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800153 return 0;
154}
155
Dave Hansence8d2cd2007-10-16 23:31:13 -0700156static int configfs_init_file(struct inode * inode)
Joel Becker7063fbf2005-12-15 14:29:43 -0800157{
158 inode->i_size = PAGE_SIZE;
159 inode->i_fop = &configfs_file_operations;
160 return 0;
161}
162
163static int init_symlink(struct inode * inode)
164{
165 inode->i_op = &configfs_symlink_inode_operations;
166 return 0;
167}
168
169static int create_dir(struct config_item * k, struct dentry * p,
170 struct dentry * d)
171{
172 int error;
173 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
174
Joel Beckerb4c98f62006-09-13 11:01:19 -0700175 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
176 if (!error)
177 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
178 CONFIGFS_DIR);
Joel Becker7063fbf2005-12-15 14:29:43 -0800179 if (!error) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800180 error = configfs_create(d, mode, init_dir);
Joel Becker7063fbf2005-12-15 14:29:43 -0800181 if (!error) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700182 inc_nlink(p->d_inode);
Joel Becker7063fbf2005-12-15 14:29:43 -0800183 (d)->d_op = &configfs_dentry_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800184 } else {
185 struct configfs_dirent *sd = d->d_fsdata;
186 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200187 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800188 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200189 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800190 configfs_put(sd);
191 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800192 }
193 }
194 return error;
195}
196
197
198/**
199 * configfs_create_dir - create a directory for an config_item.
200 * @item: config_itemwe're creating directory for.
201 * @dentry: config_item's dentry.
202 */
203
204static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
205{
206 struct dentry * parent;
207 int error = 0;
208
209 BUG_ON(!item);
210
211 if (item->ci_parent)
212 parent = item->ci_parent->ci_dentry;
213 else if (configfs_mount && configfs_mount->mnt_sb)
214 parent = configfs_mount->mnt_sb->s_root;
215 else
216 return -EFAULT;
217
218 error = create_dir(item,parent,dentry);
219 if (!error)
220 item->ci_dentry = dentry;
221 return error;
222}
223
224int configfs_create_link(struct configfs_symlink *sl,
225 struct dentry *parent,
226 struct dentry *dentry)
227{
228 int err = 0;
229 umode_t mode = S_IFLNK | S_IRWXUGO;
230
Joel Becker3d0f89b2006-01-25 13:31:07 -0800231 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
232 CONFIGFS_ITEM_LINK);
Joel Becker7063fbf2005-12-15 14:29:43 -0800233 if (!err) {
Joel Becker3d0f89b2006-01-25 13:31:07 -0800234 err = configfs_create(dentry, mode, init_symlink);
Joel Becker7063fbf2005-12-15 14:29:43 -0800235 if (!err)
236 dentry->d_op = &configfs_dentry_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800237 else {
238 struct configfs_dirent *sd = dentry->d_fsdata;
239 if (sd) {
Louis Rilling6f610762008-06-16 19:00:58 +0200240 spin_lock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800241 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200242 spin_unlock(&configfs_dirent_lock);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800243 configfs_put(sd);
244 }
245 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800246 }
247 return err;
248}
249
250static void remove_dir(struct dentry * d)
251{
252 struct dentry * parent = dget(d->d_parent);
253 struct configfs_dirent * sd;
254
255 sd = d->d_fsdata;
Louis Rilling6f610762008-06-16 19:00:58 +0200256 spin_lock(&configfs_dirent_lock);
Joel Beckere7515d02006-03-10 11:42:30 -0800257 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200258 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800259 configfs_put(sd);
260 if (d->d_inode)
261 simple_rmdir(parent->d_inode,d);
262
263 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
264 atomic_read(&d->d_count));
265
266 dput(parent);
267}
268
269/**
270 * configfs_remove_dir - remove an config_item's directory.
271 * @item: config_item we're removing.
272 *
273 * The only thing special about this is that we remove any files in
274 * the directory before we remove the directory, and we've inlined
275 * what used to be configfs_rmdir() below, instead of calling separately.
276 */
277
278static void configfs_remove_dir(struct config_item * item)
279{
280 struct dentry * dentry = dget(item->ci_dentry);
281
282 if (!dentry)
283 return;
284
285 remove_dir(dentry);
286 /**
287 * Drop reference from dget() on entrance.
288 */
289 dput(dentry);
290}
291
292
293/* attaches attribute's configfs_dirent to the dentry corresponding to the
294 * attribute file
295 */
296static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
297{
298 struct configfs_attribute * attr = sd->s_element;
299 int error;
300
Joel Becker7063fbf2005-12-15 14:29:43 -0800301 dentry->d_fsdata = configfs_get(sd);
302 sd->s_dentry = dentry;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700303 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
304 configfs_init_file);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800305 if (error) {
306 configfs_put(sd);
307 return error;
308 }
309
310 dentry->d_op = &configfs_dentry_ops;
Joel Becker7063fbf2005-12-15 14:29:43 -0800311 d_rehash(dentry);
312
313 return 0;
314}
315
316static struct dentry * configfs_lookup(struct inode *dir,
317 struct dentry *dentry,
318 struct nameidata *nd)
319{
320 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
321 struct configfs_dirent * sd;
322 int found = 0;
323 int err = 0;
324
325 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
326 if (sd->s_type & CONFIGFS_NOT_PINNED) {
327 const unsigned char * name = configfs_get_name(sd);
328
329 if (strcmp(name, dentry->d_name.name))
330 continue;
331
332 found = 1;
333 err = configfs_attach_attr(sd, dentry);
334 break;
335 }
336 }
337
338 if (!found) {
339 /*
340 * If it doesn't exist and it isn't a NOT_PINNED item,
341 * it must be negative.
342 */
343 return simple_lookup(dir, dentry, nd);
344 }
345
346 return ERR_PTR(err);
347}
348
349/*
350 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800351 * attributes and are removed by rmdir(). We recurse, taking i_mutex
Joel Becker7063fbf2005-12-15 14:29:43 -0800352 * on all children that are candidates for default detach. If the
353 * result is clean, then configfs_detach_group() will handle dropping
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800354 * i_mutex. If there is an error, the caller will clean up the i_mutex
Joel Becker7063fbf2005-12-15 14:29:43 -0800355 * holders via configfs_detach_rollback().
356 */
357static int configfs_detach_prep(struct dentry *dentry)
358{
359 struct configfs_dirent *parent_sd = dentry->d_fsdata;
360 struct configfs_dirent *sd;
361 int ret;
362
363 ret = -EBUSY;
364 if (!list_empty(&parent_sd->s_links))
365 goto out;
366
367 ret = 0;
368 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
369 if (sd->s_type & CONFIGFS_NOT_PINNED)
370 continue;
371 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800372 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
373 /* Mark that we've taken i_mutex */
Joel Becker7063fbf2005-12-15 14:29:43 -0800374 sd->s_type |= CONFIGFS_USET_DROPPING;
375
Joel Becker631d1fe2007-06-18 18:06:09 -0700376 /*
377 * Yup, recursive. If there's a problem, blame
378 * deep nesting of default_groups
379 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800380 ret = configfs_detach_prep(sd->s_dentry);
381 if (!ret)
Joel Beckere7515d02006-03-10 11:42:30 -0800382 continue;
Joel Becker7063fbf2005-12-15 14:29:43 -0800383 } else
384 ret = -ENOTEMPTY;
385
386 break;
387 }
388
389out:
390 return ret;
391}
392
393/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800394 * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
Joel Becker7063fbf2005-12-15 14:29:43 -0800395 * set.
396 */
397static void configfs_detach_rollback(struct dentry *dentry)
398{
399 struct configfs_dirent *parent_sd = dentry->d_fsdata;
400 struct configfs_dirent *sd;
401
402 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
403 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
404 configfs_detach_rollback(sd->s_dentry);
405
406 if (sd->s_type & CONFIGFS_USET_DROPPING) {
407 sd->s_type &= ~CONFIGFS_USET_DROPPING;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800408 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800409 }
410 }
411 }
412}
413
414static void detach_attrs(struct config_item * item)
415{
416 struct dentry * dentry = dget(item->ci_dentry);
417 struct configfs_dirent * parent_sd;
418 struct configfs_dirent * sd, * tmp;
419
420 if (!dentry)
421 return;
422
423 pr_debug("configfs %s: dropping attrs for dir\n",
424 dentry->d_name.name);
425
426 parent_sd = dentry->d_fsdata;
427 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
428 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
429 continue;
Louis Rilling6f610762008-06-16 19:00:58 +0200430 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800431 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200432 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800433 configfs_drop_dentry(sd, dentry);
434 configfs_put(sd);
435 }
436
437 /**
438 * Drop reference from dget() on entrance.
439 */
440 dput(dentry);
441}
442
443static int populate_attrs(struct config_item *item)
444{
445 struct config_item_type *t = item->ci_type;
446 struct configfs_attribute *attr;
447 int error = 0;
448 int i;
449
450 if (!t)
451 return -EINVAL;
452 if (t->ct_attrs) {
453 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
454 if ((error = configfs_create_file(item, attr)))
455 break;
456 }
457 }
458
459 if (error)
460 detach_attrs(item);
461
462 return error;
463}
464
465static int configfs_attach_group(struct config_item *parent_item,
466 struct config_item *item,
467 struct dentry *dentry);
468static void configfs_detach_group(struct config_item *item);
469
470static void detach_groups(struct config_group *group)
471{
472 struct dentry * dentry = dget(group->cg_item.ci_dentry);
473 struct dentry *child;
474 struct configfs_dirent *parent_sd;
475 struct configfs_dirent *sd, *tmp;
476
477 if (!dentry)
478 return;
479
480 parent_sd = dentry->d_fsdata;
481 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
482 if (!sd->s_element ||
483 !(sd->s_type & CONFIGFS_USET_DEFAULT))
484 continue;
485
486 child = sd->s_dentry;
487
488 configfs_detach_group(sd->s_element);
489 child->d_inode->i_flags |= S_DEAD;
490
491 /*
492 * From rmdir/unregister, a configfs_detach_prep() pass
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800493 * has taken our i_mutex for us. Drop it.
Joel Becker7063fbf2005-12-15 14:29:43 -0800494 * From mkdir/register cleanup, there is no sem held.
495 */
496 if (sd->s_type & CONFIGFS_USET_DROPPING)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800497 mutex_unlock(&child->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800498
499 d_delete(child);
500 dput(child);
501 }
502
503 /**
504 * Drop reference from dget() on entrance.
505 */
506 dput(dentry);
507}
508
509/*
510 * This fakes mkdir(2) on a default_groups[] entry. It
511 * creates a dentry, attachs it, and then does fixup
512 * on the sd->s_type.
513 *
514 * We could, perhaps, tweak our parent's ->mkdir for a minute and
515 * try using vfs_mkdir. Just a thought.
516 */
517static int create_default_group(struct config_group *parent_group,
518 struct config_group *group)
519{
520 int ret;
521 struct qstr name;
522 struct configfs_dirent *sd;
523 /* We trust the caller holds a reference to parent */
524 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
525
526 if (!group->cg_item.ci_name)
527 group->cg_item.ci_name = group->cg_item.ci_namebuf;
528 name.name = group->cg_item.ci_name;
529 name.len = strlen(name.name);
530 name.hash = full_name_hash(name.name, name.len);
531
532 ret = -ENOMEM;
533 child = d_alloc(parent, &name);
534 if (child) {
535 d_add(child, NULL);
536
537 ret = configfs_attach_group(&parent_group->cg_item,
538 &group->cg_item, child);
539 if (!ret) {
540 sd = child->d_fsdata;
541 sd->s_type |= CONFIGFS_USET_DEFAULT;
542 } else {
543 d_delete(child);
544 dput(child);
545 }
546 }
547
548 return ret;
549}
550
551static int populate_groups(struct config_group *group)
552{
553 struct config_group *new_group;
554 struct dentry *dentry = group->cg_item.ci_dentry;
555 int ret = 0;
556 int i;
557
Eric Sesterhenncbca6922006-03-23 00:36:54 +0100558 if (group->default_groups) {
Joel Beckereed7a0d2006-04-11 21:37:20 -0700559 /*
560 * FYI, we're faking mkdir here
Joel Becker7063fbf2005-12-15 14:29:43 -0800561 * I'm not sure we need this semaphore, as we're called
562 * from our parent's mkdir. That holds our parent's
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800563 * i_mutex, so afaik lookup cannot continue through our
Joel Becker7063fbf2005-12-15 14:29:43 -0800564 * parent to find us, let alone mess with our tree.
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800565 * That said, taking our i_mutex is closer to mkdir
Joel Beckereed7a0d2006-04-11 21:37:20 -0700566 * emulation, and shouldn't hurt.
567 */
Joonwoo Parkba611ed2007-12-26 12:09:57 +0900568 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Joel Becker7063fbf2005-12-15 14:29:43 -0800569
570 for (i = 0; group->default_groups[i]; i++) {
571 new_group = group->default_groups[i];
572
573 ret = create_default_group(group, new_group);
574 if (ret)
575 break;
576 }
577
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800578 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800579 }
580
581 if (ret)
582 detach_groups(group);
583
584 return ret;
585}
586
587/*
588 * All of link_obj/unlink_obj/link_group/unlink_group require that
Joel Beckere6bd07a2007-07-06 23:33:17 -0700589 * subsys->su_mutex is held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800590 */
591
592static void unlink_obj(struct config_item *item)
593{
594 struct config_group *group;
595
596 group = item->ci_group;
597 if (group) {
598 list_del_init(&item->ci_entry);
599
600 item->ci_group = NULL;
601 item->ci_parent = NULL;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700602
603 /* Drop the reference for ci_entry */
Joel Becker7063fbf2005-12-15 14:29:43 -0800604 config_item_put(item);
605
Joel Beckereed7a0d2006-04-11 21:37:20 -0700606 /* Drop the reference for ci_parent */
Joel Becker7063fbf2005-12-15 14:29:43 -0800607 config_group_put(group);
608 }
609}
610
611static void link_obj(struct config_item *parent_item, struct config_item *item)
612{
Joel Beckereed7a0d2006-04-11 21:37:20 -0700613 /*
614 * Parent seems redundant with group, but it makes certain
615 * traversals much nicer.
616 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800617 item->ci_parent = parent_item;
Joel Beckereed7a0d2006-04-11 21:37:20 -0700618
619 /*
620 * We hold a reference on the parent for the child's ci_parent
621 * link.
622 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800623 item->ci_group = config_group_get(to_config_group(parent_item));
624 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
625
Joel Beckereed7a0d2006-04-11 21:37:20 -0700626 /*
627 * We hold a reference on the child for ci_entry on the parent's
628 * cg_children
629 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800630 config_item_get(item);
631}
632
633static void unlink_group(struct config_group *group)
634{
635 int i;
636 struct config_group *new_group;
637
638 if (group->default_groups) {
639 for (i = 0; group->default_groups[i]; i++) {
640 new_group = group->default_groups[i];
641 unlink_group(new_group);
642 }
643 }
644
645 group->cg_subsys = NULL;
646 unlink_obj(&group->cg_item);
647}
648
649static void link_group(struct config_group *parent_group, struct config_group *group)
650{
651 int i;
652 struct config_group *new_group;
653 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
654
655 link_obj(&parent_group->cg_item, &group->cg_item);
656
657 if (parent_group->cg_subsys)
658 subsys = parent_group->cg_subsys;
659 else if (configfs_is_root(&parent_group->cg_item))
660 subsys = to_configfs_subsystem(group);
661 else
662 BUG();
663 group->cg_subsys = subsys;
664
665 if (group->default_groups) {
666 for (i = 0; group->default_groups[i]; i++) {
667 new_group = group->default_groups[i];
668 link_group(group, new_group);
669 }
670 }
671}
672
673/*
674 * The goal is that configfs_attach_item() (and
675 * configfs_attach_group()) can be called from either the VFS or this
676 * module. That is, they assume that the items have been created,
677 * the dentry allocated, and the dcache is all ready to go.
678 *
679 * If they fail, they must clean up after themselves as if they
680 * had never been called. The caller (VFS or local function) will
681 * handle cleaning up the dcache bits.
682 *
683 * configfs_detach_group() and configfs_detach_item() behave similarly on
684 * the way out. They assume that the proper semaphores are held, they
685 * clean up the configfs items, and they expect their callers will
686 * handle the dcache bits.
687 */
688static int configfs_attach_item(struct config_item *parent_item,
689 struct config_item *item,
690 struct dentry *dentry)
691{
692 int ret;
693
694 ret = configfs_create_dir(item, dentry);
695 if (!ret) {
696 ret = populate_attrs(item);
697 if (ret) {
698 configfs_remove_dir(item);
699 d_delete(dentry);
700 }
701 }
702
703 return ret;
704}
705
706static void configfs_detach_item(struct config_item *item)
707{
708 detach_attrs(item);
709 configfs_remove_dir(item);
710}
711
712static int configfs_attach_group(struct config_item *parent_item,
713 struct config_item *item,
714 struct dentry *dentry)
715{
716 int ret;
717 struct configfs_dirent *sd;
718
719 ret = configfs_attach_item(parent_item, item, dentry);
720 if (!ret) {
721 sd = dentry->d_fsdata;
722 sd->s_type |= CONFIGFS_USET_DIR;
723
724 ret = populate_groups(to_config_group(item));
725 if (ret) {
726 configfs_detach_item(item);
727 d_delete(dentry);
728 }
729 }
730
731 return ret;
732}
733
734static void configfs_detach_group(struct config_item *item)
735{
736 detach_groups(to_config_group(item));
737 configfs_detach_item(item);
738}
739
740/*
Joel Becker299894c2006-10-06 17:33:23 -0700741 * After the item has been detached from the filesystem view, we are
742 * ready to tear it out of the hierarchy. Notify the client before
743 * we do that so they can perform any cleanup that requires
744 * navigating the hierarchy. A client does not need to provide this
745 * callback. The subsystem semaphore MUST be held by the caller, and
746 * references must be valid for both items. It also assumes the
747 * caller has validated ci_type.
748 */
749static void client_disconnect_notify(struct config_item *parent_item,
750 struct config_item *item)
751{
752 struct config_item_type *type;
753
754 type = parent_item->ci_type;
755 BUG_ON(!type);
756
757 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
758 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
759 item);
760}
761
762/*
Joel Becker7063fbf2005-12-15 14:29:43 -0800763 * Drop the initial reference from make_item()/make_group()
764 * This function assumes that reference is held on item
765 * and that item holds a valid reference to the parent. Also, it
766 * assumes the caller has validated ci_type.
767 */
768static void client_drop_item(struct config_item *parent_item,
769 struct config_item *item)
770{
771 struct config_item_type *type;
772
773 type = parent_item->ci_type;
774 BUG_ON(!type);
775
Joel Beckereed7a0d2006-04-11 21:37:20 -0700776 /*
777 * If ->drop_item() exists, it is responsible for the
778 * config_item_put().
779 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800780 if (type->ct_group_ops && type->ct_group_ops->drop_item)
781 type->ct_group_ops->drop_item(to_config_group(parent_item),
Joel Becker299894c2006-10-06 17:33:23 -0700782 item);
Joel Becker7063fbf2005-12-15 14:29:43 -0800783 else
784 config_item_put(item);
785}
786
Joel Becker631d1fe2007-06-18 18:06:09 -0700787#ifdef DEBUG
788static void configfs_dump_one(struct configfs_dirent *sd, int level)
789{
790 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
791
792#define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
793 type_print(CONFIGFS_ROOT);
794 type_print(CONFIGFS_DIR);
795 type_print(CONFIGFS_ITEM_ATTR);
796 type_print(CONFIGFS_ITEM_LINK);
797 type_print(CONFIGFS_USET_DIR);
798 type_print(CONFIGFS_USET_DEFAULT);
799 type_print(CONFIGFS_USET_DROPPING);
800#undef type_print
801}
802
803static int configfs_dump(struct configfs_dirent *sd, int level)
804{
805 struct configfs_dirent *child_sd;
806 int ret = 0;
807
808 configfs_dump_one(sd, level);
809
810 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
811 return 0;
812
813 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
814 ret = configfs_dump(child_sd, level + 2);
815 if (ret)
816 break;
817 }
818
819 return ret;
820}
821#endif
822
823
824/*
825 * configfs_depend_item() and configfs_undepend_item()
826 *
827 * WARNING: Do not call these from a configfs callback!
828 *
829 * This describes these functions and their helpers.
830 *
831 * Allow another kernel system to depend on a config_item. If this
832 * happens, the item cannot go away until the dependant can live without
833 * it. The idea is to give client modules as simple an interface as
834 * possible. When a system asks them to depend on an item, they just
835 * call configfs_depend_item(). If the item is live and the client
836 * driver is in good shape, we'll happily do the work for them.
837 *
838 * Why is the locking complex? Because configfs uses the VFS to handle
839 * all locking, but this function is called outside the normal
840 * VFS->configfs path. So it must take VFS locks to prevent the
841 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
842 * why you can't call these functions underneath configfs callbacks.
843 *
844 * Note, btw, that this can be called at *any* time, even when a configfs
845 * subsystem isn't registered, or when configfs is loading or unloading.
846 * Just like configfs_register_subsystem(). So we take the same
847 * precautions. We pin the filesystem. We lock each i_mutex _in_order_
848 * on our way down the tree. If we can find the target item in the
849 * configfs tree, it must be part of the subsystem tree as well, so we
850 * do not need the subsystem semaphore. Holding the i_mutex chain locks
851 * out mkdir() and rmdir(), who might be racing us.
852 */
853
854/*
855 * configfs_depend_prep()
856 *
857 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
858 * attributes. This is similar but not the same to configfs_detach_prep().
859 * Note that configfs_detach_prep() expects the parent to be locked when it
860 * is called, but we lock the parent *inside* configfs_depend_prep(). We
861 * do that so we can unlock it if we find nothing.
862 *
863 * Here we do a depth-first search of the dentry hierarchy looking for
864 * our object. We take i_mutex on each step of the way down. IT IS
865 * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
866 * we'll drop the i_mutex.
867 *
868 * If the target is not found, -ENOENT is bubbled up and we have released
869 * all locks. If the target was found, the locks will be cleared by
870 * configfs_depend_rollback().
871 *
872 * This adds a requirement that all config_items be unique!
873 *
874 * This is recursive because the locking traversal is tricky. There isn't
875 * much on the stack, though, so folks that need this function - be careful
876 * about your stack! Patches will be accepted to make it iterative.
877 */
878static int configfs_depend_prep(struct dentry *origin,
879 struct config_item *target)
880{
881 struct configfs_dirent *child_sd, *sd = origin->d_fsdata;
882 int ret = 0;
883
884 BUG_ON(!origin || !sd);
885
886 /* Lock this guy on the way down */
887 mutex_lock(&sd->s_dentry->d_inode->i_mutex);
888 if (sd->s_element == target) /* Boo-yah */
889 goto out;
890
891 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
892 if (child_sd->s_type & CONFIGFS_DIR) {
893 ret = configfs_depend_prep(child_sd->s_dentry,
894 target);
895 if (!ret)
896 goto out; /* Child path boo-yah */
897 }
898 }
899
900 /* We looped all our children and didn't find target */
901 mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
902 ret = -ENOENT;
903
904out:
905 return ret;
906}
907
908/*
909 * This is ONLY called if configfs_depend_prep() did its job. So we can
910 * trust the entire path from item back up to origin.
911 *
912 * We walk backwards from item, unlocking each i_mutex. We finish by
913 * unlocking origin.
914 */
915static void configfs_depend_rollback(struct dentry *origin,
916 struct config_item *item)
917{
918 struct dentry *dentry = item->ci_dentry;
919
920 while (dentry != origin) {
921 mutex_unlock(&dentry->d_inode->i_mutex);
922 dentry = dentry->d_parent;
923 }
924
925 mutex_unlock(&origin->d_inode->i_mutex);
926}
927
928int configfs_depend_item(struct configfs_subsystem *subsys,
929 struct config_item *target)
930{
931 int ret;
932 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
933 struct config_item *s_item = &subsys->su_group.cg_item;
934
935 /*
936 * Pin the configfs filesystem. This means we can safely access
937 * the root of the configfs filesystem.
938 */
939 ret = configfs_pin_fs();
940 if (ret)
941 return ret;
942
943 /*
944 * Next, lock the root directory. We're going to check that the
945 * subsystem is really registered, and so we need to lock out
946 * configfs_[un]register_subsystem().
947 */
948 mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
949
950 root_sd = configfs_sb->s_root->d_fsdata;
951
952 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
953 if (p->s_type & CONFIGFS_DIR) {
954 if (p->s_element == s_item) {
955 subsys_sd = p;
956 break;
957 }
958 }
959 }
960
961 if (!subsys_sd) {
962 ret = -ENOENT;
963 goto out_unlock_fs;
964 }
965
966 /* Ok, now we can trust subsys/s_item */
967
968 /* Scan the tree, locking i_mutex recursively, return 0 if found */
969 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
970 if (ret)
971 goto out_unlock_fs;
972
973 /* We hold all i_mutexes from the subsystem down to the target */
974 p = target->ci_dentry->d_fsdata;
975 p->s_dependent_count += 1;
976
977 configfs_depend_rollback(subsys_sd->s_dentry, target);
978
979out_unlock_fs:
980 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
981
982 /*
983 * If we succeeded, the fs is pinned via other methods. If not,
984 * we're done with it anyway. So release_fs() is always right.
985 */
986 configfs_release_fs();
987
988 return ret;
989}
990EXPORT_SYMBOL(configfs_depend_item);
991
992/*
993 * Release the dependent linkage. This is much simpler than
994 * configfs_depend_item() because we know that that the client driver is
995 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
996 */
997void configfs_undepend_item(struct configfs_subsystem *subsys,
998 struct config_item *target)
999{
1000 struct configfs_dirent *sd;
1001
1002 /*
1003 * Since we can trust everything is pinned, we just need i_mutex
1004 * on the item.
1005 */
1006 mutex_lock(&target->ci_dentry->d_inode->i_mutex);
1007
1008 sd = target->ci_dentry->d_fsdata;
1009 BUG_ON(sd->s_dependent_count < 1);
1010
1011 sd->s_dependent_count -= 1;
1012
1013 /*
1014 * After this unlock, we cannot trust the item to stay alive!
1015 * DO NOT REFERENCE item after this unlock.
1016 */
1017 mutex_unlock(&target->ci_dentry->d_inode->i_mutex);
1018}
1019EXPORT_SYMBOL(configfs_undepend_item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001020
1021static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1022{
Joel Beckereed7a0d2006-04-11 21:37:20 -07001023 int ret, module_got = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -08001024 struct config_group *group;
1025 struct config_item *item;
1026 struct config_item *parent_item;
1027 struct configfs_subsystem *subsys;
1028 struct configfs_dirent *sd;
1029 struct config_item_type *type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001030 struct module *owner = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -08001031 char *name;
1032
Joel Becker84efad12006-03-27 18:46:09 -08001033 if (dentry->d_parent == configfs_sb->s_root) {
1034 ret = -EPERM;
1035 goto out;
1036 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001037
1038 sd = dentry->d_parent->d_fsdata;
Joel Becker84efad12006-03-27 18:46:09 -08001039 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1040 ret = -EPERM;
1041 goto out;
1042 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001043
Joel Becker84efad12006-03-27 18:46:09 -08001044 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001045 parent_item = configfs_get_config_item(dentry->d_parent);
1046 type = parent_item->ci_type;
1047 subsys = to_config_group(parent_item)->cg_subsys;
1048 BUG_ON(!subsys);
1049
1050 if (!type || !type->ct_group_ops ||
1051 (!type->ct_group_ops->make_group &&
1052 !type->ct_group_ops->make_item)) {
Joel Becker84efad12006-03-27 18:46:09 -08001053 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1054 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001055 }
1056
1057 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1058 if (!name) {
Joel Becker84efad12006-03-27 18:46:09 -08001059 ret = -ENOMEM;
1060 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001061 }
Joel Becker84efad12006-03-27 18:46:09 -08001062
Joel Becker7063fbf2005-12-15 14:29:43 -08001063 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1064
Joel Beckere6bd07a2007-07-06 23:33:17 -07001065 mutex_lock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001066 group = NULL;
1067 item = NULL;
1068 if (type->ct_group_ops->make_group) {
1069 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1070 if (group) {
1071 link_group(to_config_group(parent_item), group);
1072 item = &group->cg_item;
1073 }
1074 } else {
1075 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1076 if (item)
1077 link_obj(parent_item, item);
1078 }
Joel Beckere6bd07a2007-07-06 23:33:17 -07001079 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001080
1081 kfree(name);
1082 if (!item) {
Joel Beckereed7a0d2006-04-11 21:37:20 -07001083 /*
1084 * If item == NULL, then link_obj() was never called.
1085 * There are no extra references to clean up.
1086 */
Joel Becker84efad12006-03-27 18:46:09 -08001087 ret = -ENOMEM;
1088 goto out_put;
Joel Becker7063fbf2005-12-15 14:29:43 -08001089 }
1090
Joel Beckereed7a0d2006-04-11 21:37:20 -07001091 /*
1092 * link_obj() has been called (via link_group() for groups).
1093 * From here on out, errors must clean that up.
1094 */
1095
Joel Becker7063fbf2005-12-15 14:29:43 -08001096 type = item->ci_type;
Joel Beckereed7a0d2006-04-11 21:37:20 -07001097 if (!type) {
1098 ret = -EINVAL;
1099 goto out_unlink;
1100 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001101
Joel Beckereed7a0d2006-04-11 21:37:20 -07001102 owner = type->ct_owner;
1103 if (!try_module_get(owner)) {
1104 ret = -EINVAL;
1105 goto out_unlink;
1106 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001107
Joel Beckereed7a0d2006-04-11 21:37:20 -07001108 /*
1109 * I hate doing it this way, but if there is
1110 * an error, module_put() probably should
1111 * happen after any cleanup.
1112 */
1113 module_got = 1;
1114
1115 if (group)
1116 ret = configfs_attach_group(parent_item, item, dentry);
1117 else
1118 ret = configfs_attach_item(parent_item, item, dentry);
1119
1120out_unlink:
1121 if (ret) {
1122 /* Tear down everything we built up */
Joel Beckere6bd07a2007-07-06 23:33:17 -07001123 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001124
1125 client_disconnect_notify(parent_item, item);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001126 if (group)
1127 unlink_group(group);
1128 else
1129 unlink_obj(item);
1130 client_drop_item(parent_item, item);
Joel Becker299894c2006-10-06 17:33:23 -07001131
Joel Beckere6bd07a2007-07-06 23:33:17 -07001132 mutex_unlock(&subsys->su_mutex);
Joel Beckereed7a0d2006-04-11 21:37:20 -07001133
1134 if (module_got)
1135 module_put(owner);
Joel Becker7063fbf2005-12-15 14:29:43 -08001136 }
1137
Joel Becker84efad12006-03-27 18:46:09 -08001138out_put:
1139 /*
Joel Beckereed7a0d2006-04-11 21:37:20 -07001140 * link_obj()/link_group() took a reference from child->parent,
1141 * so the parent is safely pinned. We can drop our working
1142 * reference.
Joel Becker84efad12006-03-27 18:46:09 -08001143 */
1144 config_item_put(parent_item);
1145
1146out:
Joel Becker7063fbf2005-12-15 14:29:43 -08001147 return ret;
1148}
1149
1150static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1151{
1152 struct config_item *parent_item;
1153 struct config_item *item;
1154 struct configfs_subsystem *subsys;
1155 struct configfs_dirent *sd;
1156 struct module *owner = NULL;
1157 int ret;
1158
1159 if (dentry->d_parent == configfs_sb->s_root)
1160 return -EPERM;
1161
1162 sd = dentry->d_fsdata;
1163 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1164 return -EPERM;
1165
Joel Becker631d1fe2007-06-18 18:06:09 -07001166 /*
1167 * Here's where we check for dependents. We're protected by
1168 * i_mutex.
1169 */
1170 if (sd->s_dependent_count)
1171 return -EBUSY;
1172
Joel Becker84efad12006-03-27 18:46:09 -08001173 /* Get a working ref until we have the child */
Joel Becker7063fbf2005-12-15 14:29:43 -08001174 parent_item = configfs_get_config_item(dentry->d_parent);
1175 subsys = to_config_group(parent_item)->cg_subsys;
1176 BUG_ON(!subsys);
1177
1178 if (!parent_item->ci_type) {
1179 config_item_put(parent_item);
1180 return -EINVAL;
1181 }
1182
1183 ret = configfs_detach_prep(dentry);
1184 if (ret) {
1185 configfs_detach_rollback(dentry);
1186 config_item_put(parent_item);
1187 return ret;
1188 }
1189
Joel Becker84efad12006-03-27 18:46:09 -08001190 /* Get a working ref for the duration of this function */
Joel Becker7063fbf2005-12-15 14:29:43 -08001191 item = configfs_get_config_item(dentry);
1192
1193 /* Drop reference from above, item already holds one. */
1194 config_item_put(parent_item);
1195
1196 if (item->ci_type)
1197 owner = item->ci_type->ct_owner;
1198
1199 if (sd->s_type & CONFIGFS_USET_DIR) {
1200 configfs_detach_group(item);
1201
Joel Beckere6bd07a2007-07-06 23:33:17 -07001202 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001203 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001204 unlink_group(to_config_group(item));
1205 } else {
1206 configfs_detach_item(item);
1207
Joel Beckere6bd07a2007-07-06 23:33:17 -07001208 mutex_lock(&subsys->su_mutex);
Joel Becker299894c2006-10-06 17:33:23 -07001209 client_disconnect_notify(parent_item, item);
Joel Becker7063fbf2005-12-15 14:29:43 -08001210 unlink_obj(item);
1211 }
1212
1213 client_drop_item(parent_item, item);
Joel Beckere6bd07a2007-07-06 23:33:17 -07001214 mutex_unlock(&subsys->su_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001215
1216 /* Drop our reference from above */
1217 config_item_put(item);
1218
1219 module_put(owner);
1220
1221 return 0;
1222}
1223
Arjan van de Ven754661f2007-02-12 00:55:38 -08001224const struct inode_operations configfs_dir_inode_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001225 .mkdir = configfs_mkdir,
1226 .rmdir = configfs_rmdir,
1227 .symlink = configfs_symlink,
1228 .unlink = configfs_unlink,
1229 .lookup = configfs_lookup,
Joel Becker3d0f89b2006-01-25 13:31:07 -08001230 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -08001231};
1232
1233#if 0
1234int configfs_rename_dir(struct config_item * item, const char *new_name)
1235{
1236 int error = 0;
1237 struct dentry * new_dentry, * parent;
1238
1239 if (!strcmp(config_item_name(item), new_name))
1240 return -EINVAL;
1241
1242 if (!item->parent)
1243 return -EINVAL;
1244
1245 down_write(&configfs_rename_sem);
1246 parent = item->parent->dentry;
1247
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001248 mutex_lock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001249
1250 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1251 if (!IS_ERR(new_dentry)) {
Joel Beckere7515d02006-03-10 11:42:30 -08001252 if (!new_dentry->d_inode) {
Joel Becker7063fbf2005-12-15 14:29:43 -08001253 error = config_item_set_name(item, "%s", new_name);
1254 if (!error) {
1255 d_add(new_dentry, NULL);
1256 d_move(item->dentry, new_dentry);
1257 }
1258 else
1259 d_delete(new_dentry);
1260 } else
1261 error = -EEXIST;
1262 dput(new_dentry);
1263 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001264 mutex_unlock(&parent->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001265 up_write(&configfs_rename_sem);
1266
1267 return error;
1268}
1269#endif
1270
1271static int configfs_dir_open(struct inode *inode, struct file *file)
1272{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001273 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001274 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1275
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001276 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001277 file->private_data = configfs_new_dirent(parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001278 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001279
1280 return file->private_data ? 0 : -ENOMEM;
1281
1282}
1283
1284static int configfs_dir_close(struct inode *inode, struct file *file)
1285{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001286 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001287 struct configfs_dirent * cursor = file->private_data;
1288
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001289 mutex_lock(&dentry->d_inode->i_mutex);
Louis Rilling6f610762008-06-16 19:00:58 +02001290 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001291 list_del_init(&cursor->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +02001292 spin_unlock(&configfs_dirent_lock);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001293 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001294
1295 release_configfs_dirent(cursor);
1296
1297 return 0;
1298}
1299
1300/* Relationship between s_mode and the DT_xxx types */
1301static inline unsigned char dt_type(struct configfs_dirent *sd)
1302{
1303 return (sd->s_mode >> 12) & 15;
1304}
1305
1306static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1307{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001308 struct dentry *dentry = filp->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001309 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1310 struct configfs_dirent *cursor = filp->private_data;
1311 struct list_head *p, *q = &cursor->s_sibling;
1312 ino_t ino;
1313 int i = filp->f_pos;
1314
1315 switch (i) {
1316 case 0:
1317 ino = dentry->d_inode->i_ino;
1318 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1319 break;
1320 filp->f_pos++;
1321 i++;
1322 /* fallthrough */
1323 case 1:
1324 ino = parent_ino(dentry);
1325 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1326 break;
1327 filp->f_pos++;
1328 i++;
1329 /* fallthrough */
1330 default:
1331 if (filp->f_pos == 2) {
Louis Rilling6f610762008-06-16 19:00:58 +02001332 spin_lock(&configfs_dirent_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001333 list_move(q, &parent_sd->s_children);
Louis Rilling6f610762008-06-16 19:00:58 +02001334 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001335 }
1336 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1337 struct configfs_dirent *next;
1338 const char * name;
1339 int len;
1340
1341 next = list_entry(p, struct configfs_dirent,
1342 s_sibling);
1343 if (!next->s_element)
1344 continue;
1345
1346 name = configfs_get_name(next);
1347 len = strlen(name);
1348 if (next->s_dentry)
1349 ino = next->s_dentry->d_inode->i_ino;
1350 else
1351 ino = iunique(configfs_sb, 2);
1352
1353 if (filldir(dirent, name, len, filp->f_pos, ino,
1354 dt_type(next)) < 0)
1355 return 0;
1356
Louis Rilling6f610762008-06-16 19:00:58 +02001357 spin_lock(&configfs_dirent_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001358 list_move(q, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001359 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001360 p = q;
1361 filp->f_pos++;
1362 }
1363 }
1364 return 0;
1365}
1366
1367static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1368{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001369 struct dentry * dentry = file->f_path.dentry;
Joel Becker7063fbf2005-12-15 14:29:43 -08001370
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001371 mutex_lock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001372 switch (origin) {
1373 case 1:
1374 offset += file->f_pos;
1375 case 0:
1376 if (offset >= 0)
1377 break;
1378 default:
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -08001379 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001380 return -EINVAL;
1381 }
1382 if (offset != file->f_pos) {
1383 file->f_pos = offset;
1384 if (file->f_pos >= 2) {
1385 struct configfs_dirent *sd = dentry->d_fsdata;
1386 struct configfs_dirent *cursor = file->private_data;
1387 struct list_head *p;
1388 loff_t n = file->f_pos - 2;
1389
Louis Rilling6f610762008-06-16 19:00:58 +02001390 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001391 list_del(&cursor->s_sibling);
1392 p = sd->s_children.next;
1393 while (n && p != &sd->s_children) {
1394 struct configfs_dirent *next;
1395 next = list_entry(p, struct configfs_dirent,
1396 s_sibling);
1397 if (next->s_element)
1398 n--;
1399 p = p->next;
1400 }
1401 list_add_tail(&cursor->s_sibling, p);
Louis Rilling6f610762008-06-16 19:00:58 +02001402 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -08001403 }
1404 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001405 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001406 return offset;
1407}
1408
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001409const struct file_operations configfs_dir_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -08001410 .open = configfs_dir_open,
1411 .release = configfs_dir_close,
1412 .llseek = configfs_dir_lseek,
1413 .read = generic_read_dir,
1414 .readdir = configfs_readdir,
1415};
1416
1417int configfs_register_subsystem(struct configfs_subsystem *subsys)
1418{
1419 int err;
1420 struct config_group *group = &subsys->su_group;
1421 struct qstr name;
1422 struct dentry *dentry;
1423 struct configfs_dirent *sd;
1424
1425 err = configfs_pin_fs();
1426 if (err)
1427 return err;
1428
1429 if (!group->cg_item.ci_name)
1430 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1431
1432 sd = configfs_sb->s_root->d_fsdata;
1433 link_group(to_config_group(sd->s_element), group);
1434
Joonwoo Parkba611ed2007-12-26 12:09:57 +09001435 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1436 I_MUTEX_PARENT);
Joel Becker7063fbf2005-12-15 14:29:43 -08001437
1438 name.name = group->cg_item.ci_name;
1439 name.len = strlen(name.name);
1440 name.hash = full_name_hash(name.name, name.len);
1441
1442 err = -ENOMEM;
1443 dentry = d_alloc(configfs_sb->s_root, &name);
Joel Beckerafdf04e2007-03-05 15:49:49 -08001444 if (dentry) {
1445 d_add(dentry, NULL);
Joel Becker7063fbf2005-12-15 14:29:43 -08001446
Joel Beckerafdf04e2007-03-05 15:49:49 -08001447 err = configfs_attach_group(sd->s_element, &group->cg_item,
1448 dentry);
1449 if (err) {
1450 d_delete(dentry);
1451 dput(dentry);
1452 }
1453 }
Joel Becker7063fbf2005-12-15 14:29:43 -08001454
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001455 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001456
Joel Beckerafdf04e2007-03-05 15:49:49 -08001457 if (err) {
1458 unlink_group(group);
1459 configfs_release_fs();
Joel Becker7063fbf2005-12-15 14:29:43 -08001460 }
1461
1462 return err;
1463}
1464
1465void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1466{
1467 struct config_group *group = &subsys->su_group;
1468 struct dentry *dentry = group->cg_item.ci_dentry;
1469
1470 if (dentry->d_parent != configfs_sb->s_root) {
1471 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1472 return;
1473 }
1474
Mark Fasheh55ed1602006-10-20 14:55:54 -07001475 mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
1476 I_MUTEX_PARENT);
1477 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
Joel Becker7063fbf2005-12-15 14:29:43 -08001478 if (configfs_detach_prep(dentry)) {
1479 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1480 }
1481 configfs_detach_group(&group->cg_item);
1482 dentry->d_inode->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001483 mutex_unlock(&dentry->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001484
1485 d_delete(dentry);
1486
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001487 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -08001488
1489 dput(dentry);
1490
1491 unlink_group(group);
1492 configfs_release_fs();
1493}
1494
1495EXPORT_SYMBOL(configfs_register_subsystem);
1496EXPORT_SYMBOL(configfs_unregister_subsystem);