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