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