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