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