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