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>
33 #include <linux/err.h>
34
35 #include <linux/configfs.h>
36 #include "configfs_internal.h"
37
38 DECLARE_RWSEM(configfs_rename_sem);
39 /*
40 * Protects mutations of configfs_dirent linkage together with proper i_mutex
41 * Also protects mutations of symlinks linkage to target configfs_dirent
42 * Mutators of configfs_dirent linkage must *both* have the proper inode locked
43 * and configfs_dirent_lock locked, in that order.
44 * This allows one to safely traverse configfs_dirent trees and symlinks without
45 * having to lock inodes.
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()
50 */
51 DEFINE_SPINLOCK(configfs_dirent_lock);
52
configfs_d_iput(struct dentry * dentry,struct inode * inode)53 static void configfs_d_iput(struct dentry * dentry,
54 struct inode * inode)
55 {
56 struct configfs_dirent *sd = dentry->d_fsdata;
57
58 if (sd) {
59 /* Coordinate with configfs_readdir */
60 spin_lock(&configfs_dirent_lock);
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
72 spin_unlock(&configfs_dirent_lock);
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 */
configfs_d_delete(const struct dentry * dentry)82 static int configfs_d_delete(const struct dentry *dentry)
83 {
84 return 1;
85 }
86
87 const struct dentry_operations configfs_dentry_ops = {
88 .d_iput = configfs_d_iput,
89 /* simple_delete_dentry() isn't exported */
90 .d_delete = configfs_d_delete,
91 };
92
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
configfs_init_dirent_depth(struct configfs_dirent * sd)114 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
115 {
116 sd->s_depth = -1;
117 }
118
configfs_set_dir_dirent_depth(struct configfs_dirent * parent_sd,struct configfs_dirent * sd)119 static 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
128 static void
configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent * sd)129 configfs_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
149 static void
configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent * sd)150 configfs_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
configfs_init_dirent_depth(struct configfs_dirent * sd)158 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
159 {
160 }
161
configfs_set_dir_dirent_depth(struct configfs_dirent * parent_sd,struct configfs_dirent * sd)162 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
163 struct configfs_dirent *sd)
164 {
165 }
166
167 static void
configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent * sd)168 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
169 {
170 }
171
172 static void
configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent * sd)173 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
174 {
175 }
176
177 #endif /* CONFIG_LOCKDEP */
178
179 /*
180 * Allocates a new configfs_dirent and links it to the parent configfs_dirent
181 */
configfs_new_dirent(struct configfs_dirent * parent_sd,void * element,int type)182 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
183 void *element, int type)
184 {
185 struct configfs_dirent * sd;
186
187 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
188 if (!sd)
189 return ERR_PTR(-ENOMEM);
190
191 atomic_set(&sd->s_count, 1);
192 INIT_LIST_HEAD(&sd->s_links);
193 INIT_LIST_HEAD(&sd->s_children);
194 sd->s_element = element;
195 sd->s_type = type;
196 configfs_init_dirent_depth(sd);
197 spin_lock(&configfs_dirent_lock);
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 }
203 list_add(&sd->s_sibling, &parent_sd->s_children);
204 spin_unlock(&configfs_dirent_lock);
205
206 return sd;
207 }
208
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 */
configfs_dirent_exists(struct configfs_dirent * parent_sd,const unsigned char * new)216 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
217 const unsigned char *new)
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
configfs_make_dirent(struct configfs_dirent * parent_sd,struct dentry * dentry,void * element,umode_t mode,int type)235 int 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
241 sd = configfs_new_dirent(parent_sd, element, type);
242 if (IS_ERR(sd))
243 return PTR_ERR(sd);
244
245 sd->s_mode = mode;
246 sd->s_dentry = dentry;
247 if (dentry)
248 dentry->d_fsdata = configfs_get(sd);
249
250 return 0;
251 }
252
init_dir(struct inode * inode)253 static 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) */
259 inc_nlink(inode);
260 return 0;
261 }
262
configfs_init_file(struct inode * inode)263 static int configfs_init_file(struct inode * inode)
264 {
265 inode->i_size = PAGE_SIZE;
266 inode->i_fop = &configfs_file_operations;
267 return 0;
268 }
269
init_symlink(struct inode * inode)270 static int init_symlink(struct inode * inode)
271 {
272 inode->i_op = &configfs_symlink_inode_operations;
273 return 0;
274 }
275
create_dir(struct config_item * k,struct dentry * d)276 static int create_dir(struct config_item *k, struct dentry *d)
277 {
278 int error;
279 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
280 struct dentry *p = d->d_parent;
281
282 BUG_ON(!k);
283
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,
287 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
288 if (!error) {
289 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
290 error = configfs_create(d, mode, init_dir);
291 if (!error) {
292 inc_nlink(p->d_inode);
293 } else {
294 struct configfs_dirent *sd = d->d_fsdata;
295 if (sd) {
296 spin_lock(&configfs_dirent_lock);
297 list_del_init(&sd->s_sibling);
298 spin_unlock(&configfs_dirent_lock);
299 configfs_put(sd);
300 }
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.
311 *
312 * Note: user-created entries won't be allowed under this new directory
313 * until it is validated by configfs_dir_set_ready()
314 */
315
configfs_create_dir(struct config_item * item,struct dentry * dentry)316 static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
317 {
318 int error = create_dir(item, dentry);
319 if (!error)
320 item->ci_dentry = dentry;
321 return error;
322 }
323
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 */
configfs_dir_set_ready(struct configfs_dirent * sd)331 static 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 */
configfs_dirent_is_ready(struct configfs_dirent * sd)351 int 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
configfs_create_link(struct configfs_symlink * sl,struct dentry * parent,struct dentry * dentry)362 int 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
369 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
370 CONFIGFS_ITEM_LINK);
371 if (!err) {
372 err = configfs_create(dentry, mode, init_symlink);
373 if (err) {
374 struct configfs_dirent *sd = dentry->d_fsdata;
375 if (sd) {
376 spin_lock(&configfs_dirent_lock);
377 list_del_init(&sd->s_sibling);
378 spin_unlock(&configfs_dirent_lock);
379 configfs_put(sd);
380 }
381 }
382 }
383 return err;
384 }
385
remove_dir(struct dentry * d)386 static 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;
392 spin_lock(&configfs_dirent_lock);
393 list_del_init(&sd->s_sibling);
394 spin_unlock(&configfs_dirent_lock);
395 configfs_put(sd);
396 if (d->d_inode)
397 simple_rmdir(parent->d_inode,d);
398
399 pr_debug(" o %s removing done (%d)\n",d->d_name.name, d->d_count);
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.
411 *
412 * Caller holds the mutex of the item's inode
413 */
414
configfs_remove_dir(struct config_item * item)415 static 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 */
configfs_attach_attr(struct configfs_dirent * sd,struct dentry * dentry)433 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
434 {
435 struct configfs_attribute * attr = sd->s_element;
436 int error;
437
438 spin_lock(&configfs_dirent_lock);
439 dentry->d_fsdata = configfs_get(sd);
440 sd->s_dentry = dentry;
441 spin_unlock(&configfs_dirent_lock);
442
443 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
444 configfs_init_file);
445 if (error) {
446 configfs_put(sd);
447 return error;
448 }
449
450 d_rehash(dentry);
451
452 return 0;
453 }
454
configfs_lookup(struct inode * dir,struct dentry * dentry,struct nameidata * nd)455 static struct dentry * configfs_lookup(struct inode *dir,
456 struct dentry *dentry,
457 struct nameidata *nd)
458 {
459 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
460 struct configfs_dirent * sd;
461 int found = 0;
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;
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 */
494 if (dentry->d_name.len > NAME_MAX)
495 return ERR_PTR(-ENAMETOOLONG);
496 d_add(dentry, NULL);
497 return NULL;
498 }
499
500 out:
501 return ERR_PTR(err);
502 }
503
504 /*
505 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
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().
511 */
configfs_detach_prep(struct dentry * dentry,struct mutex ** wait_mutex)512 static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
513 {
514 struct configfs_dirent *parent_sd = dentry->d_fsdata;
515 struct configfs_dirent *sd;
516 int ret;
517
518 /* Mark that we're trying to drop the group */
519 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
520
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) {
527 if (!sd->s_element ||
528 (sd->s_type & CONFIGFS_NOT_PINNED))
529 continue;
530 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
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 }
537
538 /*
539 * Yup, recursive. If there's a problem, blame
540 * deep nesting of default_groups
541 */
542 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
543 if (!ret)
544 continue;
545 } else
546 ret = -ENOTEMPTY;
547
548 break;
549 }
550
551 out:
552 return ret;
553 }
554
555 /*
556 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
557 * set.
558 */
configfs_detach_rollback(struct dentry * dentry)559 static void configfs_detach_rollback(struct dentry *dentry)
560 {
561 struct configfs_dirent *parent_sd = dentry->d_fsdata;
562 struct configfs_dirent *sd;
563
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)
568 configfs_detach_rollback(sd->s_dentry);
569 }
570
detach_attrs(struct config_item * item)571 static 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;
587 spin_lock(&configfs_dirent_lock);
588 list_del_init(&sd->s_sibling);
589 spin_unlock(&configfs_dirent_lock);
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
populate_attrs(struct config_item * item)600 static 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
622 static int configfs_attach_group(struct config_item *parent_item,
623 struct config_item *item,
624 struct dentry *dentry);
625 static void configfs_detach_group(struct config_item *item);
626
detach_groups(struct config_group * group)627 static 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
645 mutex_lock(&child->d_inode->i_mutex);
646
647 configfs_detach_group(sd->s_element);
648 child->d_inode->i_flags |= S_DEAD;
649 dont_mount(child);
650
651 mutex_unlock(&child->d_inode->i_mutex);
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 */
create_default_group(struct config_group * parent_group,struct config_group * group)671 static 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 {
697 BUG_ON(child->d_inode);
698 d_drop(child);
699 dput(child);
700 }
701 }
702
703 return ret;
704 }
705
populate_groups(struct config_group * group)706 static int populate_groups(struct config_group *group)
707 {
708 struct config_group *new_group;
709 int ret = 0;
710 int i;
711
712 if (group->default_groups) {
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);
717 if (ret) {
718 detach_groups(group);
719 break;
720 }
721 }
722 }
723
724 return ret;
725 }
726
727 /*
728 * All of link_obj/unlink_obj/link_group/unlink_group require that
729 * subsys->su_mutex is held.
730 */
731
unlink_obj(struct config_item * item)732 static 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;
742
743 /* Drop the reference for ci_entry */
744 config_item_put(item);
745
746 /* Drop the reference for ci_parent */
747 config_group_put(group);
748 }
749 }
750
link_obj(struct config_item * parent_item,struct config_item * item)751 static void link_obj(struct config_item *parent_item, struct config_item *item)
752 {
753 /*
754 * Parent seems redundant with group, but it makes certain
755 * traversals much nicer.
756 */
757 item->ci_parent = parent_item;
758
759 /*
760 * We hold a reference on the parent for the child's ci_parent
761 * link.
762 */
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
766 /*
767 * We hold a reference on the child for ci_entry on the parent's
768 * cg_children
769 */
770 config_item_get(item);
771 }
772
unlink_group(struct config_group * group)773 static 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
link_group(struct config_group * parent_group,struct config_group * group)789 static 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 */
configfs_attach_item(struct config_item * parent_item,struct config_item * item,struct dentry * dentry)828 static 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) {
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);
844 configfs_remove_dir(item);
845 dentry->d_inode->i_flags |= S_DEAD;
846 dont_mount(dentry);
847 mutex_unlock(&dentry->d_inode->i_mutex);
848 d_delete(dentry);
849 }
850 }
851
852 return ret;
853 }
854
855 /* Caller holds the mutex of the item's inode */
configfs_detach_item(struct config_item * item)856 static void configfs_detach_item(struct config_item *item)
857 {
858 detach_attrs(item);
859 configfs_remove_dir(item);
860 }
861
configfs_attach_group(struct config_item * parent_item,struct config_item * item,struct dentry * dentry)862 static 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
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);
884 configfs_adjust_dir_dirent_depth_before_populate(sd);
885 ret = populate_groups(to_config_group(item));
886 if (ret) {
887 configfs_detach_item(item);
888 dentry->d_inode->i_flags |= S_DEAD;
889 dont_mount(dentry);
890 }
891 configfs_adjust_dir_dirent_depth_after_populate(sd);
892 mutex_unlock(&dentry->d_inode->i_mutex);
893 if (ret)
894 d_delete(dentry);
895 }
896
897 return ret;
898 }
899
900 /* Caller holds the mutex of the group's inode */
configfs_detach_group(struct config_item * item)901 static void configfs_detach_group(struct config_item *item)
902 {
903 detach_groups(to_config_group(item));
904 configfs_detach_item(item);
905 }
906
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 */
client_disconnect_notify(struct config_item * parent_item,struct config_item * item)916 static 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
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 */
client_drop_item(struct config_item * parent_item,struct config_item * item)935 static 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
943 /*
944 * If ->drop_item() exists, it is responsible for the
945 * config_item_put().
946 */
947 if (type->ct_group_ops && type->ct_group_ops->drop_item)
948 type->ct_group_ops->drop_item(to_config_group(parent_item),
949 item);
950 else
951 config_item_put(item);
952 }
953
954 #ifdef DEBUG
configfs_dump_one(struct configfs_dirent * sd,int level)955 static 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
configfs_dump(struct configfs_dirent * sd,int level)970 static 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
999 * happens, the item cannot go away until the dependent can live without
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
1014 * precautions. We pin the filesystem. We lock configfs_dirent_lock.
1015 * If we can find the target item in the
1016 * configfs tree, it must be part of the subsystem tree as well, so we
1017 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1018 * locking out mkdir() and rmdir(), who might be racing us.
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
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.
1040 *
1041 * If the target is not found, -ENOENT is bubbled up.
1042 *
1043 * This adds a requirement that all config_items be unique!
1044 *
1045 * This is recursive. There isn't
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 */
configfs_depend_prep(struct dentry * origin,struct config_item * target)1049 static int configfs_depend_prep(struct dentry *origin,
1050 struct config_item *target)
1051 {
1052 struct configfs_dirent *child_sd, *sd = origin->d_fsdata;
1053 int ret = 0;
1054
1055 BUG_ON(!origin || !sd);
1056
1057 if (sd->s_element == target) /* Boo-yah */
1058 goto out;
1059
1060 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1061 if ((child_sd->s_type & CONFIGFS_DIR) &&
1062 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1063 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1064 ret = configfs_depend_prep(child_sd->s_dentry,
1065 target);
1066 if (!ret)
1067 goto out; /* Child path boo-yah */
1068 }
1069 }
1070
1071 /* We looped all our children and didn't find target */
1072 ret = -ENOENT;
1073
1074 out:
1075 return ret;
1076 }
1077
configfs_depend_item(struct configfs_subsystem * subsys,struct config_item * target)1078 int configfs_depend_item(struct configfs_subsystem *subsys,
1079 struct config_item *target)
1080 {
1081 int ret;
1082 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1083 struct config_item *s_item = &subsys->su_group.cg_item;
1084 struct dentry *root;
1085
1086 /*
1087 * Pin the configfs filesystem. This means we can safely access
1088 * the root of the configfs filesystem.
1089 */
1090 root = configfs_pin_fs();
1091 if (IS_ERR(root))
1092 return PTR_ERR(root);
1093
1094 /*
1095 * Next, lock the root directory. We're going to check that the
1096 * subsystem is really registered, and so we need to lock out
1097 * configfs_[un]register_subsystem().
1098 */
1099 mutex_lock(&root->d_inode->i_mutex);
1100
1101 root_sd = root->d_fsdata;
1102
1103 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1104 if (p->s_type & CONFIGFS_DIR) {
1105 if (p->s_element == s_item) {
1106 subsys_sd = p;
1107 break;
1108 }
1109 }
1110 }
1111
1112 if (!subsys_sd) {
1113 ret = -ENOENT;
1114 goto out_unlock_fs;
1115 }
1116
1117 /* Ok, now we can trust subsys/s_item */
1118
1119 spin_lock(&configfs_dirent_lock);
1120 /* Scan the tree, return 0 if found */
1121 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1122 if (ret)
1123 goto out_unlock_dirent_lock;
1124
1125 /*
1126 * We are sure that the item is not about to be removed by rmdir(), and
1127 * not in the middle of attachment by mkdir().
1128 */
1129 p = target->ci_dentry->d_fsdata;
1130 p->s_dependent_count += 1;
1131
1132 out_unlock_dirent_lock:
1133 spin_unlock(&configfs_dirent_lock);
1134 out_unlock_fs:
1135 mutex_unlock(&root->d_inode->i_mutex);
1136
1137 /*
1138 * If we succeeded, the fs is pinned via other methods. If not,
1139 * we're done with it anyway. So release_fs() is always right.
1140 */
1141 configfs_release_fs();
1142
1143 return ret;
1144 }
1145 EXPORT_SYMBOL(configfs_depend_item);
1146
1147 /*
1148 * Release the dependent linkage. This is much simpler than
1149 * configfs_depend_item() because we know that that the client driver is
1150 * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1151 */
configfs_undepend_item(struct configfs_subsystem * subsys,struct config_item * target)1152 void configfs_undepend_item(struct configfs_subsystem *subsys,
1153 struct config_item *target)
1154 {
1155 struct configfs_dirent *sd;
1156
1157 /*
1158 * Since we can trust everything is pinned, we just need
1159 * configfs_dirent_lock.
1160 */
1161 spin_lock(&configfs_dirent_lock);
1162
1163 sd = target->ci_dentry->d_fsdata;
1164 BUG_ON(sd->s_dependent_count < 1);
1165
1166 sd->s_dependent_count -= 1;
1167
1168 /*
1169 * After this unlock, we cannot trust the item to stay alive!
1170 * DO NOT REFERENCE item after this unlock.
1171 */
1172 spin_unlock(&configfs_dirent_lock);
1173 }
1174 EXPORT_SYMBOL(configfs_undepend_item);
1175
configfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)1176 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1177 {
1178 int ret = 0;
1179 int module_got = 0;
1180 struct config_group *group = NULL;
1181 struct config_item *item = NULL;
1182 struct config_item *parent_item;
1183 struct configfs_subsystem *subsys;
1184 struct configfs_dirent *sd;
1185 struct config_item_type *type;
1186 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1187 char *name;
1188
1189 sd = dentry->d_parent->d_fsdata;
1190
1191 /*
1192 * Fake invisibility if dir belongs to a group/default groups hierarchy
1193 * being attached
1194 */
1195 if (!configfs_dirent_is_ready(sd)) {
1196 ret = -ENOENT;
1197 goto out;
1198 }
1199
1200 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1201 ret = -EPERM;
1202 goto out;
1203 }
1204
1205 /* Get a working ref for the duration of this function */
1206 parent_item = configfs_get_config_item(dentry->d_parent);
1207 type = parent_item->ci_type;
1208 subsys = to_config_group(parent_item)->cg_subsys;
1209 BUG_ON(!subsys);
1210
1211 if (!type || !type->ct_group_ops ||
1212 (!type->ct_group_ops->make_group &&
1213 !type->ct_group_ops->make_item)) {
1214 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
1215 goto out_put;
1216 }
1217
1218 /*
1219 * The subsystem may belong to a different module than the item
1220 * being created. We don't want to safely pin the new item but
1221 * fail to pin the subsystem it sits under.
1222 */
1223 if (!subsys->su_group.cg_item.ci_type) {
1224 ret = -EINVAL;
1225 goto out_put;
1226 }
1227 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1228 if (!try_module_get(subsys_owner)) {
1229 ret = -EINVAL;
1230 goto out_put;
1231 }
1232
1233 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1234 if (!name) {
1235 ret = -ENOMEM;
1236 goto out_subsys_put;
1237 }
1238
1239 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1240
1241 mutex_lock(&subsys->su_mutex);
1242 if (type->ct_group_ops->make_group) {
1243 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1244 if (!group)
1245 group = ERR_PTR(-ENOMEM);
1246 if (!IS_ERR(group)) {
1247 link_group(to_config_group(parent_item), group);
1248 item = &group->cg_item;
1249 } else
1250 ret = PTR_ERR(group);
1251 } else {
1252 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1253 if (!item)
1254 item = ERR_PTR(-ENOMEM);
1255 if (!IS_ERR(item))
1256 link_obj(parent_item, item);
1257 else
1258 ret = PTR_ERR(item);
1259 }
1260 mutex_unlock(&subsys->su_mutex);
1261
1262 kfree(name);
1263 if (ret) {
1264 /*
1265 * If ret != 0, then link_obj() was never called.
1266 * There are no extra references to clean up.
1267 */
1268 goto out_subsys_put;
1269 }
1270
1271 /*
1272 * link_obj() has been called (via link_group() for groups).
1273 * From here on out, errors must clean that up.
1274 */
1275
1276 type = item->ci_type;
1277 if (!type) {
1278 ret = -EINVAL;
1279 goto out_unlink;
1280 }
1281
1282 new_item_owner = type->ct_owner;
1283 if (!try_module_get(new_item_owner)) {
1284 ret = -EINVAL;
1285 goto out_unlink;
1286 }
1287
1288 /*
1289 * I hate doing it this way, but if there is
1290 * an error, module_put() probably should
1291 * happen after any cleanup.
1292 */
1293 module_got = 1;
1294
1295 /*
1296 * Make racing rmdir() fail if it did not tag parent with
1297 * CONFIGFS_USET_DROPPING
1298 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1299 * fail and let rmdir() terminate correctly
1300 */
1301 spin_lock(&configfs_dirent_lock);
1302 /* This will make configfs_detach_prep() fail */
1303 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1304 spin_unlock(&configfs_dirent_lock);
1305
1306 if (group)
1307 ret = configfs_attach_group(parent_item, item, dentry);
1308 else
1309 ret = configfs_attach_item(parent_item, item, dentry);
1310
1311 spin_lock(&configfs_dirent_lock);
1312 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1313 if (!ret)
1314 configfs_dir_set_ready(dentry->d_fsdata);
1315 spin_unlock(&configfs_dirent_lock);
1316
1317 out_unlink:
1318 if (ret) {
1319 /* Tear down everything we built up */
1320 mutex_lock(&subsys->su_mutex);
1321
1322 client_disconnect_notify(parent_item, item);
1323 if (group)
1324 unlink_group(group);
1325 else
1326 unlink_obj(item);
1327 client_drop_item(parent_item, item);
1328
1329 mutex_unlock(&subsys->su_mutex);
1330
1331 if (module_got)
1332 module_put(new_item_owner);
1333 }
1334
1335 out_subsys_put:
1336 if (ret)
1337 module_put(subsys_owner);
1338
1339 out_put:
1340 /*
1341 * link_obj()/link_group() took a reference from child->parent,
1342 * so the parent is safely pinned. We can drop our working
1343 * reference.
1344 */
1345 config_item_put(parent_item);
1346
1347 out:
1348 return ret;
1349 }
1350
configfs_rmdir(struct inode * dir,struct dentry * dentry)1351 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1352 {
1353 struct config_item *parent_item;
1354 struct config_item *item;
1355 struct configfs_subsystem *subsys;
1356 struct configfs_dirent *sd;
1357 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1358 int ret;
1359
1360 sd = dentry->d_fsdata;
1361 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1362 return -EPERM;
1363
1364 /* Get a working ref until we have the child */
1365 parent_item = configfs_get_config_item(dentry->d_parent);
1366 subsys = to_config_group(parent_item)->cg_subsys;
1367 BUG_ON(!subsys);
1368
1369 if (!parent_item->ci_type) {
1370 config_item_put(parent_item);
1371 return -EINVAL;
1372 }
1373
1374 /* configfs_mkdir() shouldn't have allowed this */
1375 BUG_ON(!subsys->su_group.cg_item.ci_type);
1376 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1377
1378 /*
1379 * Ensure that no racing symlink() will make detach_prep() fail while
1380 * the new link is temporarily attached
1381 */
1382 do {
1383 struct mutex *wait_mutex;
1384
1385 mutex_lock(&configfs_symlink_mutex);
1386 spin_lock(&configfs_dirent_lock);
1387 /*
1388 * Here's where we check for dependents. We're protected by
1389 * configfs_dirent_lock.
1390 * If no dependent, atomically tag the item as dropping.
1391 */
1392 ret = sd->s_dependent_count ? -EBUSY : 0;
1393 if (!ret) {
1394 ret = configfs_detach_prep(dentry, &wait_mutex);
1395 if (ret)
1396 configfs_detach_rollback(dentry);
1397 }
1398 spin_unlock(&configfs_dirent_lock);
1399 mutex_unlock(&configfs_symlink_mutex);
1400
1401 if (ret) {
1402 if (ret != -EAGAIN) {
1403 config_item_put(parent_item);
1404 return ret;
1405 }
1406
1407 /* Wait until the racing operation terminates */
1408 mutex_lock(wait_mutex);
1409 mutex_unlock(wait_mutex);
1410 }
1411 } while (ret == -EAGAIN);
1412
1413 /* Get a working ref for the duration of this function */
1414 item = configfs_get_config_item(dentry);
1415
1416 /* Drop reference from above, item already holds one. */
1417 config_item_put(parent_item);
1418
1419 if (item->ci_type)
1420 dead_item_owner = item->ci_type->ct_owner;
1421
1422 if (sd->s_type & CONFIGFS_USET_DIR) {
1423 configfs_detach_group(item);
1424
1425 mutex_lock(&subsys->su_mutex);
1426 client_disconnect_notify(parent_item, item);
1427 unlink_group(to_config_group(item));
1428 } else {
1429 configfs_detach_item(item);
1430
1431 mutex_lock(&subsys->su_mutex);
1432 client_disconnect_notify(parent_item, item);
1433 unlink_obj(item);
1434 }
1435
1436 client_drop_item(parent_item, item);
1437 mutex_unlock(&subsys->su_mutex);
1438
1439 /* Drop our reference from above */
1440 config_item_put(item);
1441
1442 module_put(dead_item_owner);
1443 module_put(subsys_owner);
1444
1445 return 0;
1446 }
1447
1448 const struct inode_operations configfs_dir_inode_operations = {
1449 .mkdir = configfs_mkdir,
1450 .rmdir = configfs_rmdir,
1451 .symlink = configfs_symlink,
1452 .unlink = configfs_unlink,
1453 .lookup = configfs_lookup,
1454 .setattr = configfs_setattr,
1455 };
1456
1457 const struct inode_operations configfs_root_inode_operations = {
1458 .lookup = configfs_lookup,
1459 .setattr = configfs_setattr,
1460 };
1461
1462 #if 0
1463 int configfs_rename_dir(struct config_item * item, const char *new_name)
1464 {
1465 int error = 0;
1466 struct dentry * new_dentry, * parent;
1467
1468 if (!strcmp(config_item_name(item), new_name))
1469 return -EINVAL;
1470
1471 if (!item->parent)
1472 return -EINVAL;
1473
1474 down_write(&configfs_rename_sem);
1475 parent = item->parent->dentry;
1476
1477 mutex_lock(&parent->d_inode->i_mutex);
1478
1479 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1480 if (!IS_ERR(new_dentry)) {
1481 if (!new_dentry->d_inode) {
1482 error = config_item_set_name(item, "%s", new_name);
1483 if (!error) {
1484 d_add(new_dentry, NULL);
1485 d_move(item->dentry, new_dentry);
1486 }
1487 else
1488 d_delete(new_dentry);
1489 } else
1490 error = -EEXIST;
1491 dput(new_dentry);
1492 }
1493 mutex_unlock(&parent->d_inode->i_mutex);
1494 up_write(&configfs_rename_sem);
1495
1496 return error;
1497 }
1498 #endif
1499
configfs_dir_open(struct inode * inode,struct file * file)1500 static int configfs_dir_open(struct inode *inode, struct file *file)
1501 {
1502 struct dentry * dentry = file->f_path.dentry;
1503 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1504 int err;
1505
1506 mutex_lock(&dentry->d_inode->i_mutex);
1507 /*
1508 * Fake invisibility if dir belongs to a group/default groups hierarchy
1509 * being attached
1510 */
1511 err = -ENOENT;
1512 if (configfs_dirent_is_ready(parent_sd)) {
1513 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1514 if (IS_ERR(file->private_data))
1515 err = PTR_ERR(file->private_data);
1516 else
1517 err = 0;
1518 }
1519 mutex_unlock(&dentry->d_inode->i_mutex);
1520
1521 return err;
1522 }
1523
configfs_dir_close(struct inode * inode,struct file * file)1524 static int configfs_dir_close(struct inode *inode, struct file *file)
1525 {
1526 struct dentry * dentry = file->f_path.dentry;
1527 struct configfs_dirent * cursor = file->private_data;
1528
1529 mutex_lock(&dentry->d_inode->i_mutex);
1530 spin_lock(&configfs_dirent_lock);
1531 list_del_init(&cursor->s_sibling);
1532 spin_unlock(&configfs_dirent_lock);
1533 mutex_unlock(&dentry->d_inode->i_mutex);
1534
1535 release_configfs_dirent(cursor);
1536
1537 return 0;
1538 }
1539
1540 /* Relationship between s_mode and the DT_xxx types */
dt_type(struct configfs_dirent * sd)1541 static inline unsigned char dt_type(struct configfs_dirent *sd)
1542 {
1543 return (sd->s_mode >> 12) & 15;
1544 }
1545
configfs_readdir(struct file * filp,void * dirent,filldir_t filldir)1546 static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1547 {
1548 struct dentry *dentry = filp->f_path.dentry;
1549 struct super_block *sb = dentry->d_sb;
1550 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1551 struct configfs_dirent *cursor = filp->private_data;
1552 struct list_head *p, *q = &cursor->s_sibling;
1553 ino_t ino = 0;
1554 int i = filp->f_pos;
1555
1556 switch (i) {
1557 case 0:
1558 ino = dentry->d_inode->i_ino;
1559 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1560 break;
1561 filp->f_pos++;
1562 i++;
1563 /* fallthrough */
1564 case 1:
1565 ino = parent_ino(dentry);
1566 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1567 break;
1568 filp->f_pos++;
1569 i++;
1570 /* fallthrough */
1571 default:
1572 if (filp->f_pos == 2) {
1573 spin_lock(&configfs_dirent_lock);
1574 list_move(q, &parent_sd->s_children);
1575 spin_unlock(&configfs_dirent_lock);
1576 }
1577 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
1578 struct configfs_dirent *next;
1579 const char * name;
1580 int len;
1581 struct inode *inode = NULL;
1582
1583 next = list_entry(p, struct configfs_dirent,
1584 s_sibling);
1585 if (!next->s_element)
1586 continue;
1587
1588 name = configfs_get_name(next);
1589 len = strlen(name);
1590
1591 /*
1592 * We'll have a dentry and an inode for
1593 * PINNED items and for open attribute
1594 * files. We lock here to prevent a race
1595 * with configfs_d_iput() clearing
1596 * s_dentry before calling iput().
1597 *
1598 * Why do we go to the trouble? If
1599 * someone has an attribute file open,
1600 * the inode number should match until
1601 * they close it. Beyond that, we don't
1602 * care.
1603 */
1604 spin_lock(&configfs_dirent_lock);
1605 dentry = next->s_dentry;
1606 if (dentry)
1607 inode = dentry->d_inode;
1608 if (inode)
1609 ino = inode->i_ino;
1610 spin_unlock(&configfs_dirent_lock);
1611 if (!inode)
1612 ino = iunique(sb, 2);
1613
1614 if (filldir(dirent, name, len, filp->f_pos, ino,
1615 dt_type(next)) < 0)
1616 return 0;
1617
1618 spin_lock(&configfs_dirent_lock);
1619 list_move(q, p);
1620 spin_unlock(&configfs_dirent_lock);
1621 p = q;
1622 filp->f_pos++;
1623 }
1624 }
1625 return 0;
1626 }
1627
configfs_dir_lseek(struct file * file,loff_t offset,int origin)1628 static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
1629 {
1630 struct dentry * dentry = file->f_path.dentry;
1631
1632 mutex_lock(&dentry->d_inode->i_mutex);
1633 switch (origin) {
1634 case 1:
1635 offset += file->f_pos;
1636 case 0:
1637 if (offset >= 0)
1638 break;
1639 default:
1640 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
1641 return -EINVAL;
1642 }
1643 if (offset != file->f_pos) {
1644 file->f_pos = offset;
1645 if (file->f_pos >= 2) {
1646 struct configfs_dirent *sd = dentry->d_fsdata;
1647 struct configfs_dirent *cursor = file->private_data;
1648 struct list_head *p;
1649 loff_t n = file->f_pos - 2;
1650
1651 spin_lock(&configfs_dirent_lock);
1652 list_del(&cursor->s_sibling);
1653 p = sd->s_children.next;
1654 while (n && p != &sd->s_children) {
1655 struct configfs_dirent *next;
1656 next = list_entry(p, struct configfs_dirent,
1657 s_sibling);
1658 if (next->s_element)
1659 n--;
1660 p = p->next;
1661 }
1662 list_add_tail(&cursor->s_sibling, p);
1663 spin_unlock(&configfs_dirent_lock);
1664 }
1665 }
1666 mutex_unlock(&dentry->d_inode->i_mutex);
1667 return offset;
1668 }
1669
1670 const struct file_operations configfs_dir_operations = {
1671 .open = configfs_dir_open,
1672 .release = configfs_dir_close,
1673 .llseek = configfs_dir_lseek,
1674 .read = generic_read_dir,
1675 .readdir = configfs_readdir,
1676 };
1677
configfs_register_subsystem(struct configfs_subsystem * subsys)1678 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1679 {
1680 int err;
1681 struct config_group *group = &subsys->su_group;
1682 struct qstr name;
1683 struct dentry *dentry;
1684 struct dentry *root;
1685 struct configfs_dirent *sd;
1686
1687 root = configfs_pin_fs();
1688 if (IS_ERR(root))
1689 return PTR_ERR(root);
1690
1691 if (!group->cg_item.ci_name)
1692 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1693
1694 sd = root->d_fsdata;
1695 link_group(to_config_group(sd->s_element), group);
1696
1697 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
1698
1699 name.name = group->cg_item.ci_name;
1700 name.len = strlen(name.name);
1701 name.hash = full_name_hash(name.name, name.len);
1702
1703 err = -ENOMEM;
1704 dentry = d_alloc(root, &name);
1705 if (dentry) {
1706 d_add(dentry, NULL);
1707
1708 err = configfs_attach_group(sd->s_element, &group->cg_item,
1709 dentry);
1710 if (err) {
1711 BUG_ON(dentry->d_inode);
1712 d_drop(dentry);
1713 dput(dentry);
1714 } else {
1715 spin_lock(&configfs_dirent_lock);
1716 configfs_dir_set_ready(dentry->d_fsdata);
1717 spin_unlock(&configfs_dirent_lock);
1718 }
1719 }
1720
1721 mutex_unlock(&root->d_inode->i_mutex);
1722
1723 if (err) {
1724 unlink_group(group);
1725 configfs_release_fs();
1726 }
1727
1728 return err;
1729 }
1730
configfs_unregister_subsystem(struct configfs_subsystem * subsys)1731 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1732 {
1733 struct config_group *group = &subsys->su_group;
1734 struct dentry *dentry = group->cg_item.ci_dentry;
1735 struct dentry *root = dentry->d_sb->s_root;
1736
1737 if (dentry->d_parent != root) {
1738 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1739 return;
1740 }
1741
1742 mutex_lock_nested(&root->d_inode->i_mutex,
1743 I_MUTEX_PARENT);
1744 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1745 mutex_lock(&configfs_symlink_mutex);
1746 spin_lock(&configfs_dirent_lock);
1747 if (configfs_detach_prep(dentry, NULL)) {
1748 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1749 }
1750 spin_unlock(&configfs_dirent_lock);
1751 mutex_unlock(&configfs_symlink_mutex);
1752 configfs_detach_group(&group->cg_item);
1753 dentry->d_inode->i_flags |= S_DEAD;
1754 dont_mount(dentry);
1755 mutex_unlock(&dentry->d_inode->i_mutex);
1756
1757 d_delete(dentry);
1758
1759 mutex_unlock(&root->d_inode->i_mutex);
1760
1761 dput(dentry);
1762
1763 unlink_group(group);
1764 configfs_release_fs();
1765 }
1766
1767 EXPORT_SYMBOL(configfs_register_subsystem);
1768 EXPORT_SYMBOL(configfs_unregister_subsystem);
1769