drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / configfs / dir.c
CommitLineData
7063fbf2
JB
1/* -*- 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>
107ed40b 33#include <linux/err.h>
7063fbf2
JB
34
35#include <linux/configfs.h>
36#include "configfs_internal.h"
37
38DECLARE_RWSEM(configfs_rename_sem);
6f610764
LR
39/*
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
5301a77d 41 * Also protects mutations of symlinks linkage to target configfs_dirent
6f610764
LR
42 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
5301a77d
LR
44 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
b3e76af8
LR
46 *
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()
6f610764
LR
50 */
51DEFINE_SPINLOCK(configfs_dirent_lock);
7063fbf2
JB
52
53static void configfs_d_iput(struct dentry * dentry,
54 struct inode * inode)
55{
24307aa1 56 struct configfs_dirent *sd = dentry->d_fsdata;
7063fbf2
JB
57
58 if (sd) {
24307aa1
JB
59 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock);
7a4ca203
JB
61 /* Coordinate with configfs_attach_attr where will increase
62 * sd->s_count and update sd->s_dentry to new allocated one.
63 * Only set sd->dentry to null when this dentry is the only
64 * sd owner.
65 * If not do so, configfs_d_iput may run just after
66 * configfs_attach_attr and set sd->s_dentry to null
67 * even it's still in use.
68 */
69 if (atomic_read(&sd->s_count) <= 2)
70 sd->s_dentry = NULL;
71
24307aa1 72 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
73 configfs_put(sd);
74 }
75 iput(inode);
76}
77
78/*
79 * We _must_ delete our dentries on last dput, as the chain-to-parent
80 * behavior is required to clear the parents of default_groups.
81 */
fe15ce44 82static int configfs_d_delete(const struct dentry *dentry)
7063fbf2
JB
83{
84 return 1;
85}
86
d463a0c4 87const struct dentry_operations configfs_dentry_ops = {
7063fbf2
JB
88 .d_iput = configfs_d_iput,
89 /* simple_delete_dentry() isn't exported */
90 .d_delete = configfs_d_delete,
91};
92
e74cc06d
LR
93#ifdef CONFIG_LOCKDEP
94
95/*
96 * Helpers to make lockdep happy with our recursive locking of default groups'
97 * inodes (see configfs_attach_group() and configfs_detach_group()).
98 * We put default groups i_mutexes in separate classes according to their depth
99 * from the youngest non-default group ancestor.
100 *
101 * For a non-default group A having default groups A/B, A/C, and A/C/D, default
102 * groups A/B and A/C will have their inode's mutex in class
103 * default_group_class[0], and default group A/C/D will be in
104 * default_group_class[1].
105 *
106 * The lock classes are declared and assigned in inode.c, according to the
107 * s_depth value.
108 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
109 * default groups, and reset to -1 when all default groups are attached. During
110 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
111 * inode's mutex is set to default_group_class[s_depth - 1].
112 */
113
114static void configfs_init_dirent_depth(struct configfs_dirent *sd)
115{
116 sd->s_depth = -1;
117}
118
119static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
120 struct configfs_dirent *sd)
121{
122 int parent_depth = parent_sd->s_depth;
123
124 if (parent_depth >= 0)
125 sd->s_depth = parent_depth + 1;
126}
127
128static void
129configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
130{
131 /*
132 * item's i_mutex class is already setup, so s_depth is now only
133 * used to set new sub-directories s_depth, which is always done
134 * with item's i_mutex locked.
135 */
136 /*
137 * sd->s_depth == -1 iff we are a non default group.
138 * else (we are a default group) sd->s_depth > 0 (see
139 * create_dir()).
140 */
141 if (sd->s_depth == -1)
142 /*
143 * We are a non default group and we are going to create
144 * default groups.
145 */
146 sd->s_depth = 0;
147}
148
149static void
150configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
151{
152 /* We will not create default groups anymore. */
153 sd->s_depth = -1;
154}
155
156#else /* CONFIG_LOCKDEP */
157
158static void configfs_init_dirent_depth(struct configfs_dirent *sd)
159{
160}
161
162static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
163 struct configfs_dirent *sd)
164{
165}
166
167static void
168configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
169{
170}
171
172static void
173configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
174{
175}
176
177#endif /* CONFIG_LOCKDEP */
178
7063fbf2
JB
179/*
180 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
181 */
420118ca
LR
182static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
183 void *element, int type)
7063fbf2
JB
184{
185 struct configfs_dirent * sd;
186
c3762229 187 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
7063fbf2 188 if (!sd)
107ed40b 189 return ERR_PTR(-ENOMEM);
7063fbf2 190
7063fbf2
JB
191 atomic_set(&sd->s_count, 1);
192 INIT_LIST_HEAD(&sd->s_links);
193 INIT_LIST_HEAD(&sd->s_children);
7063fbf2 194 sd->s_element = element;
420118ca 195 sd->s_type = type;
e74cc06d 196 configfs_init_dirent_depth(sd);
6f610764 197 spin_lock(&configfs_dirent_lock);
b3e76af8
LR
198 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
199 spin_unlock(&configfs_dirent_lock);
200 kmem_cache_free(configfs_dir_cachep, sd);
201 return ERR_PTR(-ENOENT);
202 }
6f610764
LR
203 list_add(&sd->s_sibling, &parent_sd->s_children);
204 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
205
206 return sd;
207}
208
b4c98f62
JB
209/*
210 *
211 * Return -EEXIST if there is already a configfs element with the same
212 * name for the same parent.
213 *
214 * called with parent inode's i_mutex held
215 */
58d206c2
AB
216static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
217 const unsigned char *new)
b4c98f62
JB
218{
219 struct configfs_dirent * sd;
220
221 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
222 if (sd->s_element) {
223 const unsigned char *existing = configfs_get_name(sd);
224 if (strcmp(existing, new))
225 continue;
226 else
227 return -EEXIST;
228 }
229 }
230
231 return 0;
232}
233
234
7063fbf2
JB
235int configfs_make_dirent(struct configfs_dirent * parent_sd,
236 struct dentry * dentry, void * element,
237 umode_t mode, int type)
238{
239 struct configfs_dirent * sd;
240
420118ca 241 sd = configfs_new_dirent(parent_sd, element, type);
107ed40b
LR
242 if (IS_ERR(sd))
243 return PTR_ERR(sd);
7063fbf2
JB
244
245 sd->s_mode = mode;
7063fbf2 246 sd->s_dentry = dentry;
fbc8d4c0 247 if (dentry)
7063fbf2 248 dentry->d_fsdata = configfs_get(sd);
7063fbf2
JB
249
250 return 0;
251}
252
253static int init_dir(struct inode * inode)
254{
255 inode->i_op = &configfs_dir_inode_operations;
256 inode->i_fop = &configfs_dir_operations;
257
258 /* directory inodes start off with i_nlink == 2 (for "." entry) */
d8c76e6f 259 inc_nlink(inode);
7063fbf2
JB
260 return 0;
261}
262
ce8d2cdf 263static int configfs_init_file(struct inode * inode)
7063fbf2
JB
264{
265 inode->i_size = PAGE_SIZE;
266 inode->i_fop = &configfs_file_operations;
267 return 0;
268}
269
270static int init_symlink(struct inode * inode)
271{
272 inode->i_op = &configfs_symlink_inode_operations;
273 return 0;
274}
275
0dd6c08a 276static int create_dir(struct config_item *k, struct dentry *d)
7063fbf2
JB
277{
278 int error;
279 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
0dd6c08a
AV
280 struct dentry *p = d->d_parent;
281
282 BUG_ON(!k);
7063fbf2 283
b4c98f62
JB
284 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
285 if (!error)
286 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
2a109f2a 287 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
7063fbf2 288 if (!error) {
e74cc06d 289 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
3d0f89bb 290 error = configfs_create(d, mode, init_dir);
7063fbf2 291 if (!error) {
d8c76e6f 292 inc_nlink(p->d_inode);
3d0f89bb
JB
293 } else {
294 struct configfs_dirent *sd = d->d_fsdata;
295 if (sd) {
6f610764 296 spin_lock(&configfs_dirent_lock);
3d0f89bb 297 list_del_init(&sd->s_sibling);
6f610764 298 spin_unlock(&configfs_dirent_lock);
3d0f89bb
JB
299 configfs_put(sd);
300 }
7063fbf2
JB
301 }
302 }
303 return error;
304}
305
306
307/**
308 * configfs_create_dir - create a directory for an config_item.
309 * @item: config_itemwe're creating directory for.
310 * @dentry: config_item's dentry.
2a109f2a
LR
311 *
312 * Note: user-created entries won't be allowed under this new directory
313 * until it is validated by configfs_dir_set_ready()
7063fbf2
JB
314 */
315
316static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
317{
0dd6c08a 318 int error = create_dir(item, dentry);
7063fbf2
JB
319 if (!error)
320 item->ci_dentry = dentry;
321 return error;
322}
323
2a109f2a
LR
324/*
325 * Allow userspace to create new entries under a new directory created with
326 * configfs_create_dir(), and under all of its chidlren directories recursively.
327 * @sd configfs_dirent of the new directory to validate
328 *
329 * Caller must hold configfs_dirent_lock.
330 */
331static void configfs_dir_set_ready(struct configfs_dirent *sd)
332{
333 struct configfs_dirent *child_sd;
334
335 sd->s_type &= ~CONFIGFS_USET_CREATING;
336 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
337 if (child_sd->s_type & CONFIGFS_USET_CREATING)
338 configfs_dir_set_ready(child_sd);
339}
340
341/*
342 * Check that a directory does not belong to a directory hierarchy being
343 * attached and not validated yet.
344 * @sd configfs_dirent of the directory to check
345 *
346 * @return non-zero iff the directory was validated
347 *
348 * Note: takes configfs_dirent_lock, so the result may change from false to true
349 * in two consecutive calls, but never from true to false.
350 */
351int configfs_dirent_is_ready(struct configfs_dirent *sd)
352{
353 int ret;
354
355 spin_lock(&configfs_dirent_lock);
356 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
357 spin_unlock(&configfs_dirent_lock);
358
359 return ret;
360}
361
7063fbf2
JB
362int configfs_create_link(struct configfs_symlink *sl,
363 struct dentry *parent,
364 struct dentry *dentry)
365{
366 int err = 0;
367 umode_t mode = S_IFLNK | S_IRWXUGO;
368
3d0f89bb
JB
369 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
370 CONFIGFS_ITEM_LINK);
7063fbf2 371 if (!err) {
3d0f89bb 372 err = configfs_create(dentry, mode, init_symlink);
fbc8d4c0 373 if (err) {
3d0f89bb
JB
374 struct configfs_dirent *sd = dentry->d_fsdata;
375 if (sd) {
6f610764 376 spin_lock(&configfs_dirent_lock);
3d0f89bb 377 list_del_init(&sd->s_sibling);
6f610764 378 spin_unlock(&configfs_dirent_lock);
3d0f89bb
JB
379 configfs_put(sd);
380 }
381 }
7063fbf2
JB
382 }
383 return err;
384}
385
386static void remove_dir(struct dentry * d)
387{
388 struct dentry * parent = dget(d->d_parent);
389 struct configfs_dirent * sd;
390
391 sd = d->d_fsdata;
6f610764 392 spin_lock(&configfs_dirent_lock);
e7515d06 393 list_del_init(&sd->s_sibling);
6f610764 394 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
395 configfs_put(sd);
396 if (d->d_inode)
397 simple_rmdir(parent->d_inode,d);
398
b7ab39f6 399 pr_debug(" o %s removing done (%d)\n",d->d_name.name, d->d_count);
7063fbf2
JB
400
401 dput(parent);
402}
403
404/**
405 * configfs_remove_dir - remove an config_item's directory.
406 * @item: config_item we're removing.
407 *
408 * The only thing special about this is that we remove any files in
409 * the directory before we remove the directory, and we've inlined
410 * what used to be configfs_rmdir() below, instead of calling separately.
2e2ce171
LR
411 *
412 * Caller holds the mutex of the item's inode
7063fbf2
JB
413 */
414
415static void configfs_remove_dir(struct config_item * item)
416{
417 struct dentry * dentry = dget(item->ci_dentry);
418
419 if (!dentry)
420 return;
421
422 remove_dir(dentry);
423 /**
424 * Drop reference from dget() on entrance.
425 */
426 dput(dentry);
427}
428
429
430/* attaches attribute's configfs_dirent to the dentry corresponding to the
431 * attribute file
432 */
433static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
434{
435 struct configfs_attribute * attr = sd->s_element;
436 int error;
437
7a4ca203 438 spin_lock(&configfs_dirent_lock);
3d0f89bb
JB
439 dentry->d_fsdata = configfs_get(sd);
440 sd->s_dentry = dentry;
7a4ca203
JB
441 spin_unlock(&configfs_dirent_lock);
442
ce8d2cdf
DH
443 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
444 configfs_init_file);
3d0f89bb
JB
445 if (error) {
446 configfs_put(sd);
7063fbf2 447 return error;
3d0f89bb 448 }
7063fbf2 449
7063fbf2
JB
450 d_rehash(dentry);
451
452 return 0;
453}
454
455static struct dentry * configfs_lookup(struct inode *dir,
456 struct dentry *dentry,
00cd8dd3 457 unsigned int flags)
7063fbf2
JB
458{
459 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
460 struct configfs_dirent * sd;
461 int found = 0;
2a109f2a
LR
462 int err;
463
464 /*
465 * Fake invisibility if dir belongs to a group/default groups hierarchy
466 * being attached
467 *
468 * This forbids userspace to read/write attributes of items which may
469 * not complete their initialization, since the dentries of the
470 * attributes won't be instantiated.
471 */
472 err = -ENOENT;
473 if (!configfs_dirent_is_ready(parent_sd))
474 goto out;
7063fbf2
JB
475
476 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
477 if (sd->s_type & CONFIGFS_NOT_PINNED) {
478 const unsigned char * name = configfs_get_name(sd);
479
480 if (strcmp(name, dentry->d_name.name))
481 continue;
482
483 found = 1;
484 err = configfs_attach_attr(sd, dentry);
485 break;
486 }
487 }
488
489 if (!found) {
490 /*
491 * If it doesn't exist and it isn't a NOT_PINNED item,
492 * it must be negative.
493 */
fbc8d4c0
NP
494 if (dentry->d_name.len > NAME_MAX)
495 return ERR_PTR(-ENAMETOOLONG);
fbc8d4c0
NP
496 d_add(dentry, NULL);
497 return NULL;
7063fbf2
JB
498 }
499
2a109f2a 500out:
7063fbf2
JB
501 return ERR_PTR(err);
502}
503
504/*
505 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
b3e76af8
LR
506 * attributes and are removed by rmdir(). We recurse, setting
507 * CONFIGFS_USET_DROPPING on all children that are candidates for
508 * default detach.
509 * If there is an error, the caller will reset the flags via
510 * configfs_detach_rollback().
7063fbf2 511 */
6d8344ba 512static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
7063fbf2
JB
513{
514 struct configfs_dirent *parent_sd = dentry->d_fsdata;
515 struct configfs_dirent *sd;
516 int ret;
517
4768e9b1
LR
518 /* Mark that we're trying to drop the group */
519 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
520
7063fbf2
JB
521 ret = -EBUSY;
522 if (!list_empty(&parent_sd->s_links))
523 goto out;
524
525 ret = 0;
526 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
99cefda4
LR
527 if (!sd->s_element ||
528 (sd->s_type & CONFIGFS_NOT_PINNED))
7063fbf2
JB
529 continue;
530 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
6d8344ba
LR
531 /* Abort if racing with mkdir() */
532 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
533 if (wait_mutex)
534 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
535 return -EAGAIN;
536 }
7063fbf2 537
631d1feb
JB
538 /*
539 * Yup, recursive. If there's a problem, blame
540 * deep nesting of default_groups
541 */
6d8344ba 542 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
7063fbf2 543 if (!ret)
e7515d06 544 continue;
7063fbf2
JB
545 } else
546 ret = -ENOTEMPTY;
547
548 break;
549 }
550
551out:
552 return ret;
553}
554
555/*
b3e76af8 556 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
7063fbf2
JB
557 * set.
558 */
559static void configfs_detach_rollback(struct dentry *dentry)
560{
561 struct configfs_dirent *parent_sd = dentry->d_fsdata;
562 struct configfs_dirent *sd;
563
4768e9b1
LR
564 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
565
566 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
567 if (sd->s_type & CONFIGFS_USET_DEFAULT)
7063fbf2 568 configfs_detach_rollback(sd->s_dentry);
7063fbf2
JB
569}
570
571static void detach_attrs(struct config_item * item)
572{
573 struct dentry * dentry = dget(item->ci_dentry);
574 struct configfs_dirent * parent_sd;
575 struct configfs_dirent * sd, * tmp;
576
577 if (!dentry)
578 return;
579
580 pr_debug("configfs %s: dropping attrs for dir\n",
581 dentry->d_name.name);
582
583 parent_sd = dentry->d_fsdata;
584 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
585 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
586 continue;
6f610764 587 spin_lock(&configfs_dirent_lock);
7063fbf2 588 list_del_init(&sd->s_sibling);
6f610764 589 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
590 configfs_drop_dentry(sd, dentry);
591 configfs_put(sd);
592 }
593
594 /**
595 * Drop reference from dget() on entrance.
596 */
597 dput(dentry);
598}
599
600static int populate_attrs(struct config_item *item)
601{
602 struct config_item_type *t = item->ci_type;
603 struct configfs_attribute *attr;
604 int error = 0;
605 int i;
606
607 if (!t)
608 return -EINVAL;
609 if (t->ct_attrs) {
610 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
611 if ((error = configfs_create_file(item, attr)))
612 break;
613 }
614 }
615
616 if (error)
617 detach_attrs(item);
618
619 return error;
620}
621
622static int configfs_attach_group(struct config_item *parent_item,
623 struct config_item *item,
624 struct dentry *dentry);
625static void configfs_detach_group(struct config_item *item);
626
627static void detach_groups(struct config_group *group)
628{
629 struct dentry * dentry = dget(group->cg_item.ci_dentry);
630 struct dentry *child;
631 struct configfs_dirent *parent_sd;
632 struct configfs_dirent *sd, *tmp;
633
634 if (!dentry)
635 return;
636
637 parent_sd = dentry->d_fsdata;
638 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
639 if (!sd->s_element ||
640 !(sd->s_type & CONFIGFS_USET_DEFAULT))
641 continue;
642
643 child = sd->s_dentry;
644
b3e76af8
LR
645 mutex_lock(&child->d_inode->i_mutex);
646
7063fbf2
JB
647 configfs_detach_group(sd->s_element);
648 child->d_inode->i_flags |= S_DEAD;
d83c49f3 649 dont_mount(child);
7063fbf2 650
b3e76af8 651 mutex_unlock(&child->d_inode->i_mutex);
7063fbf2
JB
652
653 d_delete(child);
654 dput(child);
655 }
656
657 /**
658 * Drop reference from dget() on entrance.
659 */
660 dput(dentry);
661}
662
663/*
664 * This fakes mkdir(2) on a default_groups[] entry. It
665 * creates a dentry, attachs it, and then does fixup
666 * on the sd->s_type.
667 *
668 * We could, perhaps, tweak our parent's ->mkdir for a minute and
669 * try using vfs_mkdir. Just a thought.
670 */
671static int create_default_group(struct config_group *parent_group,
672 struct config_group *group)
673{
674 int ret;
675 struct qstr name;
676 struct configfs_dirent *sd;
677 /* We trust the caller holds a reference to parent */
678 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
679
680 if (!group->cg_item.ci_name)
681 group->cg_item.ci_name = group->cg_item.ci_namebuf;
682 name.name = group->cg_item.ci_name;
683 name.len = strlen(name.name);
684 name.hash = full_name_hash(name.name, name.len);
685
686 ret = -ENOMEM;
687 child = d_alloc(parent, &name);
688 if (child) {
689 d_add(child, NULL);
690
691 ret = configfs_attach_group(&parent_group->cg_item,
692 &group->cg_item, child);
693 if (!ret) {
694 sd = child->d_fsdata;
695 sd->s_type |= CONFIGFS_USET_DEFAULT;
696 } else {
df7f9967
JB
697 BUG_ON(child->d_inode);
698 d_drop(child);
7063fbf2
JB
699 dput(child);
700 }
701 }
702
703 return ret;
704}
705
706static int populate_groups(struct config_group *group)
707{
708 struct config_group *new_group;
7063fbf2
JB
709 int ret = 0;
710 int i;
711
cbca692c 712 if (group->default_groups) {
7063fbf2
JB
713 for (i = 0; group->default_groups[i]; i++) {
714 new_group = group->default_groups[i];
715
716 ret = create_default_group(group, new_group);
2e2ce171
LR
717 if (ret) {
718 detach_groups(group);
7063fbf2 719 break;
2e2ce171 720 }
7063fbf2 721 }
7063fbf2
JB
722 }
723
7063fbf2
JB
724 return ret;
725}
726
727/*
728 * All of link_obj/unlink_obj/link_group/unlink_group require that
e6bd07ae 729 * subsys->su_mutex is held.
7063fbf2
JB
730 */
731
732static void unlink_obj(struct config_item *item)
733{
734 struct config_group *group;
735
736 group = item->ci_group;
737 if (group) {
738 list_del_init(&item->ci_entry);
739
740 item->ci_group = NULL;
741 item->ci_parent = NULL;
eed7a0db
JB
742
743 /* Drop the reference for ci_entry */
7063fbf2
JB
744 config_item_put(item);
745
eed7a0db 746 /* Drop the reference for ci_parent */
7063fbf2
JB
747 config_group_put(group);
748 }
749}
750
751static void link_obj(struct config_item *parent_item, struct config_item *item)
752{
eed7a0db
JB
753 /*
754 * Parent seems redundant with group, but it makes certain
755 * traversals much nicer.
756 */
7063fbf2 757 item->ci_parent = parent_item;
eed7a0db
JB
758
759 /*
760 * We hold a reference on the parent for the child's ci_parent
761 * link.
762 */
7063fbf2
JB
763 item->ci_group = config_group_get(to_config_group(parent_item));
764 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
765
eed7a0db
JB
766 /*
767 * We hold a reference on the child for ci_entry on the parent's
768 * cg_children
769 */
7063fbf2
JB
770 config_item_get(item);
771}
772
773static void unlink_group(struct config_group *group)
774{
775 int i;
776 struct config_group *new_group;
777
778 if (group->default_groups) {
779 for (i = 0; group->default_groups[i]; i++) {
780 new_group = group->default_groups[i];
781 unlink_group(new_group);
782 }
783 }
784
785 group->cg_subsys = NULL;
786 unlink_obj(&group->cg_item);
787}
788
789static void link_group(struct config_group *parent_group, struct config_group *group)
790{
791 int i;
792 struct config_group *new_group;
793 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
794
795 link_obj(&parent_group->cg_item, &group->cg_item);
796
797 if (parent_group->cg_subsys)
798 subsys = parent_group->cg_subsys;
799 else if (configfs_is_root(&parent_group->cg_item))
800 subsys = to_configfs_subsystem(group);
801 else
802 BUG();
803 group->cg_subsys = subsys;
804
805 if (group->default_groups) {
806 for (i = 0; group->default_groups[i]; i++) {
807 new_group = group->default_groups[i];
808 link_group(group, new_group);
809 }
810 }
811}
812
813/*
814 * The goal is that configfs_attach_item() (and
815 * configfs_attach_group()) can be called from either the VFS or this
816 * module. That is, they assume that the items have been created,
817 * the dentry allocated, and the dcache is all ready to go.
818 *
819 * If they fail, they must clean up after themselves as if they
820 * had never been called. The caller (VFS or local function) will
821 * handle cleaning up the dcache bits.
822 *
823 * configfs_detach_group() and configfs_detach_item() behave similarly on
824 * the way out. They assume that the proper semaphores are held, they
825 * clean up the configfs items, and they expect their callers will
826 * handle the dcache bits.
827 */
828static int configfs_attach_item(struct config_item *parent_item,
829 struct config_item *item,
830 struct dentry *dentry)
831{
832 int ret;
833
834 ret = configfs_create_dir(item, dentry);
835 if (!ret) {
836 ret = populate_attrs(item);
837 if (ret) {
2e2ce171
LR
838 /*
839 * We are going to remove an inode and its dentry but
840 * the VFS may already have hit and used them. Thus,
841 * we must lock them as rmdir() would.
842 */
843 mutex_lock(&dentry->d_inode->i_mutex);
7063fbf2 844 configfs_remove_dir(item);
2e2ce171 845 dentry->d_inode->i_flags |= S_DEAD;
d83c49f3 846 dont_mount(dentry);
2e2ce171 847 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
848 d_delete(dentry);
849 }
850 }
851
852 return ret;
853}
854
2e2ce171 855/* Caller holds the mutex of the item's inode */
7063fbf2
JB
856static void configfs_detach_item(struct config_item *item)
857{
858 detach_attrs(item);
859 configfs_remove_dir(item);
860}
861
862static int configfs_attach_group(struct config_item *parent_item,
863 struct config_item *item,
864 struct dentry *dentry)
865{
866 int ret;
867 struct configfs_dirent *sd;
868
869 ret = configfs_attach_item(parent_item, item, dentry);
870 if (!ret) {
871 sd = dentry->d_fsdata;
872 sd->s_type |= CONFIGFS_USET_DIR;
873
2e2ce171
LR
874 /*
875 * FYI, we're faking mkdir in populate_groups()
876 * We must lock the group's inode to avoid races with the VFS
877 * which can already hit the inode and try to add/remove entries
878 * under it.
879 *
880 * We must also lock the inode to remove it safely in case of
881 * error, as rmdir() would.
882 */
883 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
e74cc06d 884 configfs_adjust_dir_dirent_depth_before_populate(sd);
7063fbf2
JB
885 ret = populate_groups(to_config_group(item));
886 if (ret) {
887 configfs_detach_item(item);
2e2ce171 888 dentry->d_inode->i_flags |= S_DEAD;
d83c49f3 889 dont_mount(dentry);
7063fbf2 890 }
e74cc06d 891 configfs_adjust_dir_dirent_depth_after_populate(sd);
2e2ce171
LR
892 mutex_unlock(&dentry->d_inode->i_mutex);
893 if (ret)
894 d_delete(dentry);
7063fbf2
JB
895 }
896
897 return ret;
898}
899
2e2ce171 900/* Caller holds the mutex of the group's inode */
7063fbf2
JB
901static void configfs_detach_group(struct config_item *item)
902{
903 detach_groups(to_config_group(item));
904 configfs_detach_item(item);
905}
906
299894cc
JB
907/*
908 * After the item has been detached from the filesystem view, we are
909 * ready to tear it out of the hierarchy. Notify the client before
910 * we do that so they can perform any cleanup that requires
911 * navigating the hierarchy. A client does not need to provide this
912 * callback. The subsystem semaphore MUST be held by the caller, and
913 * references must be valid for both items. It also assumes the
914 * caller has validated ci_type.
915 */
916static void client_disconnect_notify(struct config_item *parent_item,
917 struct config_item *item)
918{
919 struct config_item_type *type;
920
921 type = parent_item->ci_type;
922 BUG_ON(!type);
923
924 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
925 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
926 item);
927}
928
7063fbf2
JB
929/*
930 * Drop the initial reference from make_item()/make_group()
931 * This function assumes that reference is held on item
932 * and that item holds a valid reference to the parent. Also, it
933 * assumes the caller has validated ci_type.
934 */
935static void client_drop_item(struct config_item *parent_item,
936 struct config_item *item)
937{
938 struct config_item_type *type;
939
940 type = parent_item->ci_type;
941 BUG_ON(!type);
942
eed7a0db
JB
943 /*
944 * If ->drop_item() exists, it is responsible for the
945 * config_item_put().
946 */
7063fbf2
JB
947 if (type->ct_group_ops && type->ct_group_ops->drop_item)
948 type->ct_group_ops->drop_item(to_config_group(parent_item),
299894cc 949 item);
7063fbf2
JB
950 else
951 config_item_put(item);
952}
953
631d1feb
JB
954#ifdef DEBUG
955static void configfs_dump_one(struct configfs_dirent *sd, int level)
956{
957 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
958
959#define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
960 type_print(CONFIGFS_ROOT);
961 type_print(CONFIGFS_DIR);
962 type_print(CONFIGFS_ITEM_ATTR);
963 type_print(CONFIGFS_ITEM_LINK);
964 type_print(CONFIGFS_USET_DIR);
965 type_print(CONFIGFS_USET_DEFAULT);
966 type_print(CONFIGFS_USET_DROPPING);
967#undef type_print
968}
969
970static int configfs_dump(struct configfs_dirent *sd, int level)
971{
972 struct configfs_dirent *child_sd;
973 int ret = 0;
974
975 configfs_dump_one(sd, level);
976
977 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
978 return 0;
979
980 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
981 ret = configfs_dump(child_sd, level + 2);
982 if (ret)
983 break;
984 }
985
986 return ret;
987}
988#endif
989
990
991/*
992 * configfs_depend_item() and configfs_undepend_item()
993 *
994 * WARNING: Do not call these from a configfs callback!
995 *
996 * This describes these functions and their helpers.
997 *
998 * Allow another kernel system to depend on a config_item. If this
25985edc 999 * happens, the item cannot go away until the dependent can live without
631d1feb
JB
1000 * it. The idea is to give client modules as simple an interface as
1001 * possible. When a system asks them to depend on an item, they just
1002 * call configfs_depend_item(). If the item is live and the client
1003 * driver is in good shape, we'll happily do the work for them.
1004 *
1005 * Why is the locking complex? Because configfs uses the VFS to handle
1006 * all locking, but this function is called outside the normal
1007 * VFS->configfs path. So it must take VFS locks to prevent the
1008 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
1009 * why you can't call these functions underneath configfs callbacks.
1010 *
1011 * Note, btw, that this can be called at *any* time, even when a configfs
1012 * subsystem isn't registered, or when configfs is loading or unloading.
1013 * Just like configfs_register_subsystem(). So we take the same
420118ca
LR
1014 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1015 * If we can find the target item in the
631d1feb 1016 * configfs tree, it must be part of the subsystem tree as well, so we
420118ca
LR
1017 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1018 * locking out mkdir() and rmdir(), who might be racing us.
631d1feb
JB
1019 */
1020
1021/*
1022 * configfs_depend_prep()
1023 *
1024 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
1025 * attributes. This is similar but not the same to configfs_detach_prep().
1026 * Note that configfs_detach_prep() expects the parent to be locked when it
1027 * is called, but we lock the parent *inside* configfs_depend_prep(). We
1028 * do that so we can unlock it if we find nothing.
1029 *
1030 * Here we do a depth-first search of the dentry hierarchy looking for
420118ca
LR
1031 * our object.
1032 * We deliberately ignore items tagged as dropping since they are virtually
1033 * dead, as well as items in the middle of attachment since they virtually
1034 * do not exist yet. This completes the locking out of racing mkdir() and
1035 * rmdir().
1036 * Note: subdirectories in the middle of attachment start with s_type =
1037 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1038 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1039 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
631d1feb 1040 *
420118ca 1041 * If the target is not found, -ENOENT is bubbled up.
631d1feb
JB
1042 *
1043 * This adds a requirement that all config_items be unique!
1044 *
420118ca 1045 * This is recursive. There isn't
631d1feb
JB
1046 * much on the stack, though, so folks that need this function - be careful
1047 * about your stack! Patches will be accepted to make it iterative.
1048 */
1049static int configfs_depend_prep(struct dentry *origin,
1050 struct config_item *target)
1051{
49deb4bc 1052 struct configfs_dirent *child_sd, *sd;
631d1feb
JB
1053 int ret = 0;
1054
49deb4bc
WY
1055 BUG_ON(!origin || !origin->d_fsdata);
1056 sd = origin->d_fsdata;
631d1feb 1057
631d1feb
JB
1058 if (sd->s_element == target) /* Boo-yah */
1059 goto out;
1060
1061 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
420118ca
LR
1062 if ((child_sd->s_type & CONFIGFS_DIR) &&
1063 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1064 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
631d1feb
JB
1065 ret = configfs_depend_prep(child_sd->s_dentry,
1066 target);
1067 if (!ret)
1068 goto out; /* Child path boo-yah */
1069 }
1070 }
1071
1072 /* We looped all our children and didn't find target */
631d1feb
JB
1073 ret = -ENOENT;
1074
1075out:
1076 return ret;
1077}
1078
631d1feb
JB
1079int configfs_depend_item(struct configfs_subsystem *subsys,
1080 struct config_item *target)
1081{
1082 int ret;
1083 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1084 struct config_item *s_item = &subsys->su_group.cg_item;
b7c177fc 1085 struct dentry *root;
631d1feb
JB
1086
1087 /*
1088 * Pin the configfs filesystem. This means we can safely access
1089 * the root of the configfs filesystem.
1090 */
2a152ad3
AV
1091 root = configfs_pin_fs();
1092 if (IS_ERR(root))
1093 return PTR_ERR(root);
631d1feb
JB
1094
1095 /*
1096 * Next, lock the root directory. We're going to check that the
1097 * subsystem is really registered, and so we need to lock out
1098 * configfs_[un]register_subsystem().
1099 */
b7c177fc 1100 mutex_lock(&root->d_inode->i_mutex);
631d1feb 1101
b7c177fc 1102 root_sd = root->d_fsdata;
631d1feb
JB
1103
1104 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1105 if (p->s_type & CONFIGFS_DIR) {
1106 if (p->s_element == s_item) {
1107 subsys_sd = p;
1108 break;
1109 }
1110 }
1111 }
1112
1113 if (!subsys_sd) {
1114 ret = -ENOENT;
1115 goto out_unlock_fs;
1116 }
1117
1118 /* Ok, now we can trust subsys/s_item */
1119
420118ca
LR
1120 spin_lock(&configfs_dirent_lock);
1121 /* Scan the tree, return 0 if found */
631d1feb
JB
1122 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1123 if (ret)
420118ca 1124 goto out_unlock_dirent_lock;
631d1feb 1125
420118ca
LR
1126 /*
1127 * We are sure that the item is not about to be removed by rmdir(), and
1128 * not in the middle of attachment by mkdir().
1129 */
631d1feb
JB
1130 p = target->ci_dentry->d_fsdata;
1131 p->s_dependent_count += 1;
1132
420118ca
LR
1133out_unlock_dirent_lock:
1134 spin_unlock(&configfs_dirent_lock);
631d1feb 1135out_unlock_fs:
b7c177fc 1136 mutex_unlock(&root->d_inode->i_mutex);
631d1feb
JB
1137
1138 /*
1139 * If we succeeded, the fs is pinned via other methods. If not,
1140 * we're done with it anyway. So release_fs() is always right.
1141 */
1142 configfs_release_fs();
1143
1144 return ret;
1145}
1146EXPORT_SYMBOL(configfs_depend_item);
1147
1148/*
1149 * Release the dependent linkage. This is much simpler than
1150 * configfs_depend_item() because we know that that the client driver is
1151 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1152 */
1153void configfs_undepend_item(struct configfs_subsystem *subsys,
1154 struct config_item *target)
1155{
1156 struct configfs_dirent *sd;
1157
1158 /*
420118ca
LR
1159 * Since we can trust everything is pinned, we just need
1160 * configfs_dirent_lock.
631d1feb 1161 */
420118ca 1162 spin_lock(&configfs_dirent_lock);
631d1feb
JB
1163
1164 sd = target->ci_dentry->d_fsdata;
1165 BUG_ON(sd->s_dependent_count < 1);
1166
1167 sd->s_dependent_count -= 1;
1168
1169 /*
1170 * After this unlock, we cannot trust the item to stay alive!
1171 * DO NOT REFERENCE item after this unlock.
1172 */
420118ca 1173 spin_unlock(&configfs_dirent_lock);
631d1feb
JB
1174}
1175EXPORT_SYMBOL(configfs_undepend_item);
7063fbf2 1176
18bb1db3 1177static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
7063fbf2 1178{
a6795e9e
JB
1179 int ret = 0;
1180 int module_got = 0;
1181 struct config_group *group = NULL;
1182 struct config_item *item = NULL;
7063fbf2
JB
1183 struct config_item *parent_item;
1184 struct configfs_subsystem *subsys;
1185 struct configfs_dirent *sd;
1186 struct config_item_type *type;
70526b67 1187 struct module *subsys_owner = NULL, *new_item_owner = NULL;
7063fbf2
JB
1188 char *name;
1189
7063fbf2 1190 sd = dentry->d_parent->d_fsdata;
2a109f2a
LR
1191
1192 /*
1193 * Fake invisibility if dir belongs to a group/default groups hierarchy
1194 * being attached
1195 */
1196 if (!configfs_dirent_is_ready(sd)) {
1197 ret = -ENOENT;
1198 goto out;
1199 }
1200
84efad1a
JB
1201 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1202 ret = -EPERM;
1203 goto out;
1204 }
7063fbf2 1205
84efad1a 1206 /* Get a working ref for the duration of this function */
7063fbf2
JB
1207 parent_item = configfs_get_config_item(dentry->d_parent);
1208 type = parent_item->ci_type;
1209 subsys = to_config_group(parent_item)->cg_subsys;
1210 BUG_ON(!subsys);
1211
1212 if (!type || !type->ct_group_ops ||
1213 (!type->ct_group_ops->make_group &&
1214 !type->ct_group_ops->make_item)) {
84efad1a
JB
1215 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1216 goto out_put;
7063fbf2
JB
1217 }
1218
70526b67
JB
1219 /*
1220 * The subsystem may belong to a different module than the item
1221 * being created. We don't want to safely pin the new item but
1222 * fail to pin the subsystem it sits under.
1223 */
1224 if (!subsys->su_group.cg_item.ci_type) {
1225 ret = -EINVAL;
1226 goto out_put;
1227 }
1228 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1229 if (!try_module_get(subsys_owner)) {
1230 ret = -EINVAL;
1231 goto out_put;
1232 }
1233
7063fbf2
JB
1234 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1235 if (!name) {
84efad1a 1236 ret = -ENOMEM;
70526b67 1237 goto out_subsys_put;
7063fbf2 1238 }
84efad1a 1239
7063fbf2
JB
1240 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1241
e6bd07ae 1242 mutex_lock(&subsys->su_mutex);
7063fbf2 1243 if (type->ct_group_ops->make_group) {
f89ab861 1244 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
a6795e9e
JB
1245 if (!group)
1246 group = ERR_PTR(-ENOMEM);
1247 if (!IS_ERR(group)) {
7063fbf2
JB
1248 link_group(to_config_group(parent_item), group);
1249 item = &group->cg_item;
a6795e9e
JB
1250 } else
1251 ret = PTR_ERR(group);
7063fbf2 1252 } else {
f89ab861 1253 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
a6795e9e
JB
1254 if (!item)
1255 item = ERR_PTR(-ENOMEM);
1256 if (!IS_ERR(item))
7063fbf2 1257 link_obj(parent_item, item);
a6795e9e
JB
1258 else
1259 ret = PTR_ERR(item);
7063fbf2 1260 }
e6bd07ae 1261 mutex_unlock(&subsys->su_mutex);
7063fbf2
JB
1262
1263 kfree(name);
a6795e9e 1264 if (ret) {
eed7a0db 1265 /*
dacdd0e0 1266 * If ret != 0, then link_obj() was never called.
eed7a0db
JB
1267 * There are no extra references to clean up.
1268 */
70526b67 1269 goto out_subsys_put;
7063fbf2
JB
1270 }
1271
eed7a0db
JB
1272 /*
1273 * link_obj() has been called (via link_group() for groups).
1274 * From here on out, errors must clean that up.
1275 */
1276
7063fbf2 1277 type = item->ci_type;
eed7a0db
JB
1278 if (!type) {
1279 ret = -EINVAL;
1280 goto out_unlink;
1281 }
7063fbf2 1282
70526b67
JB
1283 new_item_owner = type->ct_owner;
1284 if (!try_module_get(new_item_owner)) {
eed7a0db
JB
1285 ret = -EINVAL;
1286 goto out_unlink;
1287 }
7063fbf2 1288
eed7a0db
JB
1289 /*
1290 * I hate doing it this way, but if there is
1291 * an error, module_put() probably should
1292 * happen after any cleanup.
1293 */
1294 module_got = 1;
1295
6d8344ba
LR
1296 /*
1297 * Make racing rmdir() fail if it did not tag parent with
1298 * CONFIGFS_USET_DROPPING
1299 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1300 * fail and let rmdir() terminate correctly
1301 */
1302 spin_lock(&configfs_dirent_lock);
1303 /* This will make configfs_detach_prep() fail */
1304 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1305 spin_unlock(&configfs_dirent_lock);
1306
eed7a0db
JB
1307 if (group)
1308 ret = configfs_attach_group(parent_item, item, dentry);
1309 else
1310 ret = configfs_attach_item(parent_item, item, dentry);
1311
6d8344ba
LR
1312 spin_lock(&configfs_dirent_lock);
1313 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
2a109f2a
LR
1314 if (!ret)
1315 configfs_dir_set_ready(dentry->d_fsdata);
6d8344ba
LR
1316 spin_unlock(&configfs_dirent_lock);
1317
eed7a0db
JB
1318out_unlink:
1319 if (ret) {
1320 /* Tear down everything we built up */
e6bd07ae 1321 mutex_lock(&subsys->su_mutex);
299894cc
JB
1322
1323 client_disconnect_notify(parent_item, item);
eed7a0db
JB
1324 if (group)
1325 unlink_group(group);
1326 else
1327 unlink_obj(item);
1328 client_drop_item(parent_item, item);
299894cc 1329
e6bd07ae 1330 mutex_unlock(&subsys->su_mutex);
eed7a0db
JB
1331
1332 if (module_got)
70526b67 1333 module_put(new_item_owner);
7063fbf2
JB
1334 }
1335
70526b67
JB
1336out_subsys_put:
1337 if (ret)
1338 module_put(subsys_owner);
1339
84efad1a
JB
1340out_put:
1341 /*
eed7a0db
JB
1342 * link_obj()/link_group() took a reference from child->parent,
1343 * so the parent is safely pinned. We can drop our working
1344 * reference.
84efad1a
JB
1345 */
1346 config_item_put(parent_item);
1347
1348out:
7063fbf2
JB
1349 return ret;
1350}
1351
1352static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1353{
1354 struct config_item *parent_item;
1355 struct config_item *item;
1356 struct configfs_subsystem *subsys;
1357 struct configfs_dirent *sd;
70526b67 1358 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
7063fbf2
JB
1359 int ret;
1360
7063fbf2
JB
1361 sd = dentry->d_fsdata;
1362 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1363 return -EPERM;
1364
84efad1a 1365 /* Get a working ref until we have the child */
7063fbf2
JB
1366 parent_item = configfs_get_config_item(dentry->d_parent);
1367 subsys = to_config_group(parent_item)->cg_subsys;
1368 BUG_ON(!subsys);
1369
1370 if (!parent_item->ci_type) {
1371 config_item_put(parent_item);
1372 return -EINVAL;
1373 }
1374
70526b67
JB
1375 /* configfs_mkdir() shouldn't have allowed this */
1376 BUG_ON(!subsys->su_group.cg_item.ci_type);
1377 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1378
9a73d78c
LR
1379 /*
1380 * Ensure that no racing symlink() will make detach_prep() fail while
1381 * the new link is temporarily attached
1382 */
6d8344ba
LR
1383 do {
1384 struct mutex *wait_mutex;
1385
de6bf18e
LR
1386 mutex_lock(&configfs_symlink_mutex);
1387 spin_lock(&configfs_dirent_lock);
420118ca
LR
1388 /*
1389 * Here's where we check for dependents. We're protected by
1390 * configfs_dirent_lock.
1391 * If no dependent, atomically tag the item as dropping.
1392 */
1393 ret = sd->s_dependent_count ? -EBUSY : 0;
1394 if (!ret) {
1395 ret = configfs_detach_prep(dentry, &wait_mutex);
1396 if (ret)
1397 configfs_detach_rollback(dentry);
1398 }
de6bf18e
LR
1399 spin_unlock(&configfs_dirent_lock);
1400 mutex_unlock(&configfs_symlink_mutex);
1401
1402 if (ret) {
6d8344ba
LR
1403 if (ret != -EAGAIN) {
1404 config_item_put(parent_item);
1405 return ret;
1406 }
1407
1408 /* Wait until the racing operation terminates */
1409 mutex_lock(wait_mutex);
1410 mutex_unlock(wait_mutex);
6d8344ba
LR
1411 }
1412 } while (ret == -EAGAIN);
7063fbf2 1413
84efad1a 1414 /* Get a working ref for the duration of this function */
7063fbf2
JB
1415 item = configfs_get_config_item(dentry);
1416
1417 /* Drop reference from above, item already holds one. */
1418 config_item_put(parent_item);
1419
1420 if (item->ci_type)
70526b67 1421 dead_item_owner = item->ci_type->ct_owner;
7063fbf2
JB
1422
1423 if (sd->s_type & CONFIGFS_USET_DIR) {
1424 configfs_detach_group(item);
1425
e6bd07ae 1426 mutex_lock(&subsys->su_mutex);
299894cc 1427 client_disconnect_notify(parent_item, item);
7063fbf2
JB
1428 unlink_group(to_config_group(item));
1429 } else {
1430 configfs_detach_item(item);
1431
e6bd07ae 1432 mutex_lock(&subsys->su_mutex);
299894cc 1433 client_disconnect_notify(parent_item, item);
7063fbf2
JB
1434 unlink_obj(item);
1435 }
1436
1437 client_drop_item(parent_item, item);
e6bd07ae 1438 mutex_unlock(&subsys->su_mutex);
7063fbf2
JB
1439
1440 /* Drop our reference from above */
1441 config_item_put(item);
1442
70526b67
JB
1443 module_put(dead_item_owner);
1444 module_put(subsys_owner);
7063fbf2
JB
1445
1446 return 0;
1447}
1448
754661f1 1449const struct inode_operations configfs_dir_inode_operations = {
7063fbf2
JB
1450 .mkdir = configfs_mkdir,
1451 .rmdir = configfs_rmdir,
1452 .symlink = configfs_symlink,
1453 .unlink = configfs_unlink,
1454 .lookup = configfs_lookup,
3d0f89bb 1455 .setattr = configfs_setattr,
7063fbf2
JB
1456};
1457
81d44ed1
AV
1458const struct inode_operations configfs_root_inode_operations = {
1459 .lookup = configfs_lookup,
1460 .setattr = configfs_setattr,
1461};
1462
7063fbf2
JB
1463#if 0
1464int configfs_rename_dir(struct config_item * item, const char *new_name)
1465{
1466 int error = 0;
1467 struct dentry * new_dentry, * parent;
1468
1469 if (!strcmp(config_item_name(item), new_name))
1470 return -EINVAL;
1471
1472 if (!item->parent)
1473 return -EINVAL;
1474
1475 down_write(&configfs_rename_sem);
1476 parent = item->parent->dentry;
1477
1b1dcc1b 1478 mutex_lock(&parent->d_inode->i_mutex);
7063fbf2
JB
1479
1480 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1481 if (!IS_ERR(new_dentry)) {
e7515d06 1482 if (!new_dentry->d_inode) {
7063fbf2
JB
1483 error = config_item_set_name(item, "%s", new_name);
1484 if (!error) {
1485 d_add(new_dentry, NULL);
1486 d_move(item->dentry, new_dentry);
1487 }
1488 else
1489 d_delete(new_dentry);
1490 } else
1491 error = -EEXIST;
1492 dput(new_dentry);
1493 }
1b1dcc1b 1494 mutex_unlock(&parent->d_inode->i_mutex);
7063fbf2
JB
1495 up_write(&configfs_rename_sem);
1496
1497 return error;
1498}
1499#endif
1500
1501static int configfs_dir_open(struct inode *inode, struct file *file)
1502{
867fa491 1503 struct dentry * dentry = file->f_path.dentry;
7063fbf2 1504 struct configfs_dirent * parent_sd = dentry->d_fsdata;
2a109f2a 1505 int err;
7063fbf2 1506
1b1dcc1b 1507 mutex_lock(&dentry->d_inode->i_mutex);
2a109f2a
LR
1508 /*
1509 * Fake invisibility if dir belongs to a group/default groups hierarchy
1510 * being attached
1511 */
1512 err = -ENOENT;
1513 if (configfs_dirent_is_ready(parent_sd)) {
420118ca 1514 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
2a109f2a
LR
1515 if (IS_ERR(file->private_data))
1516 err = PTR_ERR(file->private_data);
1517 else
1518 err = 0;
1519 }
1b1dcc1b 1520 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2 1521
2a109f2a 1522 return err;
7063fbf2
JB
1523}
1524
1525static int configfs_dir_close(struct inode *inode, struct file *file)
1526{
867fa491 1527 struct dentry * dentry = file->f_path.dentry;
7063fbf2
JB
1528 struct configfs_dirent * cursor = file->private_data;
1529
1b1dcc1b 1530 mutex_lock(&dentry->d_inode->i_mutex);
6f610764 1531 spin_lock(&configfs_dirent_lock);
7063fbf2 1532 list_del_init(&cursor->s_sibling);
6f610764 1533 spin_unlock(&configfs_dirent_lock);
1b1dcc1b 1534 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
1535
1536 release_configfs_dirent(cursor);
1537
1538 return 0;
1539}
1540
1541/* Relationship between s_mode and the DT_xxx types */
1542static inline unsigned char dt_type(struct configfs_dirent *sd)
1543{
1544 return (sd->s_mode >> 12) & 15;
1545}
1546
1547static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1548{
867fa491 1549 struct dentry *dentry = filp->f_path.dentry;
b7c177fc 1550 struct super_block *sb = dentry->d_sb;
7063fbf2
JB
1551 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1552 struct configfs_dirent *cursor = filp->private_data;
1553 struct list_head *p, *q = &cursor->s_sibling;
24307aa1 1554 ino_t ino = 0;
7063fbf2
JB
1555 int i = filp->f_pos;
1556
1557 switch (i) {
1558 case 0:
1559 ino = dentry->d_inode->i_ino;
1560 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1561 break;
1562 filp->f_pos++;
1563 i++;
1564 /* fallthrough */
1565 case 1:
1566 ino = parent_ino(dentry);
1567 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1568 break;
1569 filp->f_pos++;
1570 i++;
1571 /* fallthrough */
1572 default:
1573 if (filp->f_pos == 2) {
6f610764 1574 spin_lock(&configfs_dirent_lock);
f116629d 1575 list_move(q, &parent_sd->s_children);
6f610764 1576 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
1577 }
1578 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1579 struct configfs_dirent *next;
1580 const char * name;
1581 int len;
24307aa1 1582 struct inode *inode = NULL;
7063fbf2
JB
1583
1584 next = list_entry(p, struct configfs_dirent,
1585 s_sibling);
1586 if (!next->s_element)
1587 continue;
1588
1589 name = configfs_get_name(next);
1590 len = strlen(name);
24307aa1
JB
1591
1592 /*
1593 * We'll have a dentry and an inode for
1594 * PINNED items and for open attribute
1595 * files. We lock here to prevent a race
1596 * with configfs_d_iput() clearing
1597 * s_dentry before calling iput().
1598 *
1599 * Why do we go to the trouble? If
1600 * someone has an attribute file open,
1601 * the inode number should match until
1602 * they close it. Beyond that, we don't
1603 * care.
1604 */
1605 spin_lock(&configfs_dirent_lock);
1606 dentry = next->s_dentry;
1607 if (dentry)
1608 inode = dentry->d_inode;
1609 if (inode)
1610 ino = inode->i_ino;
1611 spin_unlock(&configfs_dirent_lock);
1612 if (!inode)
b7c177fc 1613 ino = iunique(sb, 2);
7063fbf2
JB
1614
1615 if (filldir(dirent, name, len, filp->f_pos, ino,
1616 dt_type(next)) < 0)
1617 return 0;
1618
6f610764 1619 spin_lock(&configfs_dirent_lock);
f116629d 1620 list_move(q, p);
6f610764 1621 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
1622 p = q;
1623 filp->f_pos++;
1624 }
1625 }
1626 return 0;
1627}
1628
965c8e59 1629static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
7063fbf2 1630{
867fa491 1631 struct dentry * dentry = file->f_path.dentry;
7063fbf2 1632
1b1dcc1b 1633 mutex_lock(&dentry->d_inode->i_mutex);
965c8e59 1634 switch (whence) {
7063fbf2
JB
1635 case 1:
1636 offset += file->f_pos;
1637 case 0:
1638 if (offset >= 0)
1639 break;
1640 default:
496ad9aa 1641 mutex_unlock(&file_inode(file)->i_mutex);
7063fbf2
JB
1642 return -EINVAL;
1643 }
1644 if (offset != file->f_pos) {
1645 file->f_pos = offset;
1646 if (file->f_pos >= 2) {
1647 struct configfs_dirent *sd = dentry->d_fsdata;
1648 struct configfs_dirent *cursor = file->private_data;
1649 struct list_head *p;
1650 loff_t n = file->f_pos - 2;
1651
6f610764 1652 spin_lock(&configfs_dirent_lock);
7063fbf2
JB
1653 list_del(&cursor->s_sibling);
1654 p = sd->s_children.next;
1655 while (n && p != &sd->s_children) {
1656 struct configfs_dirent *next;
1657 next = list_entry(p, struct configfs_dirent,
1658 s_sibling);
1659 if (next->s_element)
1660 n--;
1661 p = p->next;
1662 }
1663 list_add_tail(&cursor->s_sibling, p);
6f610764 1664 spin_unlock(&configfs_dirent_lock);
7063fbf2
JB
1665 }
1666 }
1b1dcc1b 1667 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
1668 return offset;
1669}
1670
4b6f5d20 1671const struct file_operations configfs_dir_operations = {
7063fbf2
JB
1672 .open = configfs_dir_open,
1673 .release = configfs_dir_close,
1674 .llseek = configfs_dir_lseek,
1675 .read = generic_read_dir,
1676 .readdir = configfs_readdir,
1677};
1678
1679int configfs_register_subsystem(struct configfs_subsystem *subsys)
1680{
1681 int err;
1682 struct config_group *group = &subsys->su_group;
1683 struct qstr name;
1684 struct dentry *dentry;
b7c177fc 1685 struct dentry *root;
7063fbf2
JB
1686 struct configfs_dirent *sd;
1687
2a152ad3
AV
1688 root = configfs_pin_fs();
1689 if (IS_ERR(root))
1690 return PTR_ERR(root);
7063fbf2
JB
1691
1692 if (!group->cg_item.ci_name)
1693 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1694
b7c177fc 1695 sd = root->d_fsdata;
7063fbf2
JB
1696 link_group(to_config_group(sd->s_element), group);
1697
b7c177fc 1698 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
7063fbf2
JB
1699
1700 name.name = group->cg_item.ci_name;
1701 name.len = strlen(name.name);
1702 name.hash = full_name_hash(name.name, name.len);
1703
1704 err = -ENOMEM;
b7c177fc 1705 dentry = d_alloc(root, &name);
afdf04ea
JB
1706 if (dentry) {
1707 d_add(dentry, NULL);
7063fbf2 1708
afdf04ea
JB
1709 err = configfs_attach_group(sd->s_element, &group->cg_item,
1710 dentry);
1711 if (err) {
df7f9967
JB
1712 BUG_ON(dentry->d_inode);
1713 d_drop(dentry);
afdf04ea 1714 dput(dentry);
2a109f2a
LR
1715 } else {
1716 spin_lock(&configfs_dirent_lock);
1717 configfs_dir_set_ready(dentry->d_fsdata);
1718 spin_unlock(&configfs_dirent_lock);
afdf04ea
JB
1719 }
1720 }
7063fbf2 1721
b7c177fc 1722 mutex_unlock(&root->d_inode->i_mutex);
7063fbf2 1723
afdf04ea
JB
1724 if (err) {
1725 unlink_group(group);
1726 configfs_release_fs();
7063fbf2
JB
1727 }
1728
1729 return err;
1730}
1731
1732void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1733{
1734 struct config_group *group = &subsys->su_group;
1735 struct dentry *dentry = group->cg_item.ci_dentry;
b7c177fc 1736 struct dentry *root = dentry->d_sb->s_root;
7063fbf2 1737
b7c177fc 1738 if (dentry->d_parent != root) {
7063fbf2
JB
1739 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1740 return;
1741 }
1742
b7c177fc 1743 mutex_lock_nested(&root->d_inode->i_mutex,
55ed1602
MF
1744 I_MUTEX_PARENT);
1745 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
9a73d78c 1746 mutex_lock(&configfs_symlink_mutex);
b3e76af8 1747 spin_lock(&configfs_dirent_lock);
6d8344ba 1748 if (configfs_detach_prep(dentry, NULL)) {
7063fbf2
JB
1749 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1750 }
b3e76af8 1751 spin_unlock(&configfs_dirent_lock);
9a73d78c 1752 mutex_unlock(&configfs_symlink_mutex);
7063fbf2
JB
1753 configfs_detach_group(&group->cg_item);
1754 dentry->d_inode->i_flags |= S_DEAD;
d83c49f3 1755 dont_mount(dentry);
1b1dcc1b 1756 mutex_unlock(&dentry->d_inode->i_mutex);
7063fbf2
JB
1757
1758 d_delete(dentry);
1759
b7c177fc 1760 mutex_unlock(&root->d_inode->i_mutex);
7063fbf2
JB
1761
1762 dput(dentry);
1763
1764 unlink_group(group);
1765 configfs_release_fs();
1766}
1767
1768EXPORT_SYMBOL(configfs_register_subsystem);
1769EXPORT_SYMBOL(configfs_unregister_subsystem);