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