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> |
| 33 | |
| 34 | #include <linux/configfs.h> |
| 35 | #include "configfs_internal.h" |
| 36 | |
| 37 | DECLARE_RWSEM(configfs_rename_sem); |
| 38 | |
| 39 | static void configfs_d_iput(struct dentry * dentry, |
| 40 | struct inode * inode) |
| 41 | { |
| 42 | struct configfs_dirent * sd = dentry->d_fsdata; |
| 43 | |
| 44 | if (sd) { |
| 45 | BUG_ON(sd->s_dentry != dentry); |
| 46 | sd->s_dentry = NULL; |
| 47 | configfs_put(sd); |
| 48 | } |
| 49 | iput(inode); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * We _must_ delete our dentries on last dput, as the chain-to-parent |
| 54 | * behavior is required to clear the parents of default_groups. |
| 55 | */ |
| 56 | static int configfs_d_delete(struct dentry *dentry) |
| 57 | { |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static struct dentry_operations configfs_dentry_ops = { |
| 62 | .d_iput = configfs_d_iput, |
| 63 | /* simple_delete_dentry() isn't exported */ |
| 64 | .d_delete = configfs_d_delete, |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Allocates a new configfs_dirent and links it to the parent configfs_dirent |
| 69 | */ |
| 70 | static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd, |
| 71 | void * element) |
| 72 | { |
| 73 | struct configfs_dirent * sd; |
| 74 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 75 | sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 76 | if (!sd) |
| 77 | return NULL; |
| 78 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 79 | atomic_set(&sd->s_count, 1); |
| 80 | INIT_LIST_HEAD(&sd->s_links); |
| 81 | INIT_LIST_HEAD(&sd->s_children); |
| 82 | list_add(&sd->s_sibling, &parent_sd->s_children); |
| 83 | sd->s_element = element; |
| 84 | |
| 85 | return sd; |
| 86 | } |
| 87 | |
Joel Becker | b4c98f6 | 2006-09-13 11:01:19 -0700 | [diff] [blame] | 88 | /* |
| 89 | * |
| 90 | * Return -EEXIST if there is already a configfs element with the same |
| 91 | * name for the same parent. |
| 92 | * |
| 93 | * called with parent inode's i_mutex held |
| 94 | */ |
Adrian Bunk | 58d206c | 2006-11-20 03:24:00 +0100 | [diff] [blame] | 95 | static int configfs_dirent_exists(struct configfs_dirent *parent_sd, |
| 96 | const unsigned char *new) |
Joel Becker | b4c98f6 | 2006-09-13 11:01:19 -0700 | [diff] [blame] | 97 | { |
| 98 | struct configfs_dirent * sd; |
| 99 | |
| 100 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 101 | if (sd->s_element) { |
| 102 | const unsigned char *existing = configfs_get_name(sd); |
| 103 | if (strcmp(existing, new)) |
| 104 | continue; |
| 105 | else |
| 106 | return -EEXIST; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 114 | int configfs_make_dirent(struct configfs_dirent * parent_sd, |
| 115 | struct dentry * dentry, void * element, |
| 116 | umode_t mode, int type) |
| 117 | { |
| 118 | struct configfs_dirent * sd; |
| 119 | |
| 120 | sd = configfs_new_dirent(parent_sd, element); |
| 121 | if (!sd) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | sd->s_mode = mode; |
| 125 | sd->s_type = type; |
| 126 | sd->s_dentry = dentry; |
| 127 | if (dentry) { |
| 128 | dentry->d_fsdata = configfs_get(sd); |
| 129 | dentry->d_op = &configfs_dentry_ops; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int init_dir(struct inode * inode) |
| 136 | { |
| 137 | inode->i_op = &configfs_dir_inode_operations; |
| 138 | inode->i_fop = &configfs_dir_operations; |
| 139 | |
| 140 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 141 | inc_nlink(inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int init_file(struct inode * inode) |
| 146 | { |
| 147 | inode->i_size = PAGE_SIZE; |
| 148 | inode->i_fop = &configfs_file_operations; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int init_symlink(struct inode * inode) |
| 153 | { |
| 154 | inode->i_op = &configfs_symlink_inode_operations; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int create_dir(struct config_item * k, struct dentry * p, |
| 159 | struct dentry * d) |
| 160 | { |
| 161 | int error; |
| 162 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
| 163 | |
Joel Becker | b4c98f6 | 2006-09-13 11:01:19 -0700 | [diff] [blame] | 164 | error = configfs_dirent_exists(p->d_fsdata, d->d_name.name); |
| 165 | if (!error) |
| 166 | error = configfs_make_dirent(p->d_fsdata, d, k, mode, |
| 167 | CONFIGFS_DIR); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 168 | if (!error) { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 169 | error = configfs_create(d, mode, init_dir); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 170 | if (!error) { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 171 | inc_nlink(p->d_inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 172 | (d)->d_op = &configfs_dentry_ops; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 173 | } else { |
| 174 | struct configfs_dirent *sd = d->d_fsdata; |
| 175 | if (sd) { |
| 176 | list_del_init(&sd->s_sibling); |
| 177 | configfs_put(sd); |
| 178 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | return error; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * configfs_create_dir - create a directory for an config_item. |
| 187 | * @item: config_itemwe're creating directory for. |
| 188 | * @dentry: config_item's dentry. |
| 189 | */ |
| 190 | |
| 191 | static int configfs_create_dir(struct config_item * item, struct dentry *dentry) |
| 192 | { |
| 193 | struct dentry * parent; |
| 194 | int error = 0; |
| 195 | |
| 196 | BUG_ON(!item); |
| 197 | |
| 198 | if (item->ci_parent) |
| 199 | parent = item->ci_parent->ci_dentry; |
| 200 | else if (configfs_mount && configfs_mount->mnt_sb) |
| 201 | parent = configfs_mount->mnt_sb->s_root; |
| 202 | else |
| 203 | return -EFAULT; |
| 204 | |
| 205 | error = create_dir(item,parent,dentry); |
| 206 | if (!error) |
| 207 | item->ci_dentry = dentry; |
| 208 | return error; |
| 209 | } |
| 210 | |
| 211 | int configfs_create_link(struct configfs_symlink *sl, |
| 212 | struct dentry *parent, |
| 213 | struct dentry *dentry) |
| 214 | { |
| 215 | int err = 0; |
| 216 | umode_t mode = S_IFLNK | S_IRWXUGO; |
| 217 | |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 218 | err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode, |
| 219 | CONFIGFS_ITEM_LINK); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 220 | if (!err) { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 221 | err = configfs_create(dentry, mode, init_symlink); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 222 | if (!err) |
| 223 | dentry->d_op = &configfs_dentry_ops; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 224 | else { |
| 225 | struct configfs_dirent *sd = dentry->d_fsdata; |
| 226 | if (sd) { |
| 227 | list_del_init(&sd->s_sibling); |
| 228 | configfs_put(sd); |
| 229 | } |
| 230 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 231 | } |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | static void remove_dir(struct dentry * d) |
| 236 | { |
| 237 | struct dentry * parent = dget(d->d_parent); |
| 238 | struct configfs_dirent * sd; |
| 239 | |
| 240 | sd = d->d_fsdata; |
Joel Becker | e7515d0 | 2006-03-10 11:42:30 -0800 | [diff] [blame] | 241 | list_del_init(&sd->s_sibling); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 242 | configfs_put(sd); |
| 243 | if (d->d_inode) |
| 244 | simple_rmdir(parent->d_inode,d); |
| 245 | |
| 246 | pr_debug(" o %s removing done (%d)\n",d->d_name.name, |
| 247 | atomic_read(&d->d_count)); |
| 248 | |
| 249 | dput(parent); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * configfs_remove_dir - remove an config_item's directory. |
| 254 | * @item: config_item we're removing. |
| 255 | * |
| 256 | * The only thing special about this is that we remove any files in |
| 257 | * the directory before we remove the directory, and we've inlined |
| 258 | * what used to be configfs_rmdir() below, instead of calling separately. |
| 259 | */ |
| 260 | |
| 261 | static void configfs_remove_dir(struct config_item * item) |
| 262 | { |
| 263 | struct dentry * dentry = dget(item->ci_dentry); |
| 264 | |
| 265 | if (!dentry) |
| 266 | return; |
| 267 | |
| 268 | remove_dir(dentry); |
| 269 | /** |
| 270 | * Drop reference from dget() on entrance. |
| 271 | */ |
| 272 | dput(dentry); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* attaches attribute's configfs_dirent to the dentry corresponding to the |
| 277 | * attribute file |
| 278 | */ |
| 279 | static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry) |
| 280 | { |
| 281 | struct configfs_attribute * attr = sd->s_element; |
| 282 | int error; |
| 283 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 284 | dentry->d_fsdata = configfs_get(sd); |
| 285 | sd->s_dentry = dentry; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 286 | error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file); |
| 287 | if (error) { |
| 288 | configfs_put(sd); |
| 289 | return error; |
| 290 | } |
| 291 | |
| 292 | dentry->d_op = &configfs_dentry_ops; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 293 | d_rehash(dentry); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static struct dentry * configfs_lookup(struct inode *dir, |
| 299 | struct dentry *dentry, |
| 300 | struct nameidata *nd) |
| 301 | { |
| 302 | struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata; |
| 303 | struct configfs_dirent * sd; |
| 304 | int found = 0; |
| 305 | int err = 0; |
| 306 | |
| 307 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 308 | if (sd->s_type & CONFIGFS_NOT_PINNED) { |
| 309 | const unsigned char * name = configfs_get_name(sd); |
| 310 | |
| 311 | if (strcmp(name, dentry->d_name.name)) |
| 312 | continue; |
| 313 | |
| 314 | found = 1; |
| 315 | err = configfs_attach_attr(sd, dentry); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (!found) { |
| 321 | /* |
| 322 | * If it doesn't exist and it isn't a NOT_PINNED item, |
| 323 | * it must be negative. |
| 324 | */ |
| 325 | return simple_lookup(dir, dentry, nd); |
| 326 | } |
| 327 | |
| 328 | return ERR_PTR(err); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 333 | * attributes and are removed by rmdir(). We recurse, taking i_mutex |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 334 | * on all children that are candidates for default detach. If the |
| 335 | * result is clean, then configfs_detach_group() will handle dropping |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 336 | * i_mutex. If there is an error, the caller will clean up the i_mutex |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 337 | * holders via configfs_detach_rollback(). |
| 338 | */ |
| 339 | static int configfs_detach_prep(struct dentry *dentry) |
| 340 | { |
| 341 | struct configfs_dirent *parent_sd = dentry->d_fsdata; |
| 342 | struct configfs_dirent *sd; |
| 343 | int ret; |
| 344 | |
| 345 | ret = -EBUSY; |
| 346 | if (!list_empty(&parent_sd->s_links)) |
| 347 | goto out; |
| 348 | |
| 349 | ret = 0; |
| 350 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 351 | if (sd->s_type & CONFIGFS_NOT_PINNED) |
| 352 | continue; |
| 353 | if (sd->s_type & CONFIGFS_USET_DEFAULT) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 354 | mutex_lock(&sd->s_dentry->d_inode->i_mutex); |
| 355 | /* Mark that we've taken i_mutex */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 356 | sd->s_type |= CONFIGFS_USET_DROPPING; |
| 357 | |
| 358 | ret = configfs_detach_prep(sd->s_dentry); |
| 359 | if (!ret) |
Joel Becker | e7515d0 | 2006-03-10 11:42:30 -0800 | [diff] [blame] | 360 | continue; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 361 | } else |
| 362 | ret = -ENOTEMPTY; |
| 363 | |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | out: |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 372 | * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 373 | * set. |
| 374 | */ |
| 375 | static void configfs_detach_rollback(struct dentry *dentry) |
| 376 | { |
| 377 | struct configfs_dirent *parent_sd = dentry->d_fsdata; |
| 378 | struct configfs_dirent *sd; |
| 379 | |
| 380 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 381 | if (sd->s_type & CONFIGFS_USET_DEFAULT) { |
| 382 | configfs_detach_rollback(sd->s_dentry); |
| 383 | |
| 384 | if (sd->s_type & CONFIGFS_USET_DROPPING) { |
| 385 | sd->s_type &= ~CONFIGFS_USET_DROPPING; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 386 | mutex_unlock(&sd->s_dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void detach_attrs(struct config_item * item) |
| 393 | { |
| 394 | struct dentry * dentry = dget(item->ci_dentry); |
| 395 | struct configfs_dirent * parent_sd; |
| 396 | struct configfs_dirent * sd, * tmp; |
| 397 | |
| 398 | if (!dentry) |
| 399 | return; |
| 400 | |
| 401 | pr_debug("configfs %s: dropping attrs for dir\n", |
| 402 | dentry->d_name.name); |
| 403 | |
| 404 | parent_sd = dentry->d_fsdata; |
| 405 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { |
| 406 | if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED)) |
| 407 | continue; |
| 408 | list_del_init(&sd->s_sibling); |
| 409 | configfs_drop_dentry(sd, dentry); |
| 410 | configfs_put(sd); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Drop reference from dget() on entrance. |
| 415 | */ |
| 416 | dput(dentry); |
| 417 | } |
| 418 | |
| 419 | static int populate_attrs(struct config_item *item) |
| 420 | { |
| 421 | struct config_item_type *t = item->ci_type; |
| 422 | struct configfs_attribute *attr; |
| 423 | int error = 0; |
| 424 | int i; |
| 425 | |
| 426 | if (!t) |
| 427 | return -EINVAL; |
| 428 | if (t->ct_attrs) { |
| 429 | for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) { |
| 430 | if ((error = configfs_create_file(item, attr))) |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (error) |
| 436 | detach_attrs(item); |
| 437 | |
| 438 | return error; |
| 439 | } |
| 440 | |
| 441 | static int configfs_attach_group(struct config_item *parent_item, |
| 442 | struct config_item *item, |
| 443 | struct dentry *dentry); |
| 444 | static void configfs_detach_group(struct config_item *item); |
| 445 | |
| 446 | static void detach_groups(struct config_group *group) |
| 447 | { |
| 448 | struct dentry * dentry = dget(group->cg_item.ci_dentry); |
| 449 | struct dentry *child; |
| 450 | struct configfs_dirent *parent_sd; |
| 451 | struct configfs_dirent *sd, *tmp; |
| 452 | |
| 453 | if (!dentry) |
| 454 | return; |
| 455 | |
| 456 | parent_sd = dentry->d_fsdata; |
| 457 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { |
| 458 | if (!sd->s_element || |
| 459 | !(sd->s_type & CONFIGFS_USET_DEFAULT)) |
| 460 | continue; |
| 461 | |
| 462 | child = sd->s_dentry; |
| 463 | |
| 464 | configfs_detach_group(sd->s_element); |
| 465 | child->d_inode->i_flags |= S_DEAD; |
| 466 | |
| 467 | /* |
| 468 | * From rmdir/unregister, a configfs_detach_prep() pass |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 469 | * has taken our i_mutex for us. Drop it. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 470 | * From mkdir/register cleanup, there is no sem held. |
| 471 | */ |
| 472 | if (sd->s_type & CONFIGFS_USET_DROPPING) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 473 | mutex_unlock(&child->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 474 | |
| 475 | d_delete(child); |
| 476 | dput(child); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Drop reference from dget() on entrance. |
| 481 | */ |
| 482 | dput(dentry); |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * This fakes mkdir(2) on a default_groups[] entry. It |
| 487 | * creates a dentry, attachs it, and then does fixup |
| 488 | * on the sd->s_type. |
| 489 | * |
| 490 | * We could, perhaps, tweak our parent's ->mkdir for a minute and |
| 491 | * try using vfs_mkdir. Just a thought. |
| 492 | */ |
| 493 | static int create_default_group(struct config_group *parent_group, |
| 494 | struct config_group *group) |
| 495 | { |
| 496 | int ret; |
| 497 | struct qstr name; |
| 498 | struct configfs_dirent *sd; |
| 499 | /* We trust the caller holds a reference to parent */ |
| 500 | struct dentry *child, *parent = parent_group->cg_item.ci_dentry; |
| 501 | |
| 502 | if (!group->cg_item.ci_name) |
| 503 | group->cg_item.ci_name = group->cg_item.ci_namebuf; |
| 504 | name.name = group->cg_item.ci_name; |
| 505 | name.len = strlen(name.name); |
| 506 | name.hash = full_name_hash(name.name, name.len); |
| 507 | |
| 508 | ret = -ENOMEM; |
| 509 | child = d_alloc(parent, &name); |
| 510 | if (child) { |
| 511 | d_add(child, NULL); |
| 512 | |
| 513 | ret = configfs_attach_group(&parent_group->cg_item, |
| 514 | &group->cg_item, child); |
| 515 | if (!ret) { |
| 516 | sd = child->d_fsdata; |
| 517 | sd->s_type |= CONFIGFS_USET_DEFAULT; |
| 518 | } else { |
| 519 | d_delete(child); |
| 520 | dput(child); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | static int populate_groups(struct config_group *group) |
| 528 | { |
| 529 | struct config_group *new_group; |
| 530 | struct dentry *dentry = group->cg_item.ci_dentry; |
| 531 | int ret = 0; |
| 532 | int i; |
| 533 | |
Eric Sesterhenn | cbca692 | 2006-03-23 00:36:54 +0100 | [diff] [blame] | 534 | if (group->default_groups) { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 535 | /* |
| 536 | * FYI, we're faking mkdir here |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 537 | * I'm not sure we need this semaphore, as we're called |
| 538 | * from our parent's mkdir. That holds our parent's |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 539 | * i_mutex, so afaik lookup cannot continue through our |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 540 | * parent to find us, let alone mess with our tree. |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 541 | * That said, taking our i_mutex is closer to mkdir |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 542 | * emulation, and shouldn't hurt. |
| 543 | */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 544 | mutex_lock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 545 | |
| 546 | for (i = 0; group->default_groups[i]; i++) { |
| 547 | new_group = group->default_groups[i]; |
| 548 | |
| 549 | ret = create_default_group(group, new_group); |
| 550 | if (ret) |
| 551 | break; |
| 552 | } |
| 553 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 554 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (ret) |
| 558 | detach_groups(group); |
| 559 | |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * All of link_obj/unlink_obj/link_group/unlink_group require that |
| 565 | * subsys->su_sem is held. |
| 566 | */ |
| 567 | |
| 568 | static void unlink_obj(struct config_item *item) |
| 569 | { |
| 570 | struct config_group *group; |
| 571 | |
| 572 | group = item->ci_group; |
| 573 | if (group) { |
| 574 | list_del_init(&item->ci_entry); |
| 575 | |
| 576 | item->ci_group = NULL; |
| 577 | item->ci_parent = NULL; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 578 | |
| 579 | /* Drop the reference for ci_entry */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 580 | config_item_put(item); |
| 581 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 582 | /* Drop the reference for ci_parent */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 583 | config_group_put(group); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static void link_obj(struct config_item *parent_item, struct config_item *item) |
| 588 | { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 589 | /* |
| 590 | * Parent seems redundant with group, but it makes certain |
| 591 | * traversals much nicer. |
| 592 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 593 | item->ci_parent = parent_item; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 594 | |
| 595 | /* |
| 596 | * We hold a reference on the parent for the child's ci_parent |
| 597 | * link. |
| 598 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 599 | item->ci_group = config_group_get(to_config_group(parent_item)); |
| 600 | list_add_tail(&item->ci_entry, &item->ci_group->cg_children); |
| 601 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 602 | /* |
| 603 | * We hold a reference on the child for ci_entry on the parent's |
| 604 | * cg_children |
| 605 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 606 | config_item_get(item); |
| 607 | } |
| 608 | |
| 609 | static void unlink_group(struct config_group *group) |
| 610 | { |
| 611 | int i; |
| 612 | struct config_group *new_group; |
| 613 | |
| 614 | if (group->default_groups) { |
| 615 | for (i = 0; group->default_groups[i]; i++) { |
| 616 | new_group = group->default_groups[i]; |
| 617 | unlink_group(new_group); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | group->cg_subsys = NULL; |
| 622 | unlink_obj(&group->cg_item); |
| 623 | } |
| 624 | |
| 625 | static void link_group(struct config_group *parent_group, struct config_group *group) |
| 626 | { |
| 627 | int i; |
| 628 | struct config_group *new_group; |
| 629 | struct configfs_subsystem *subsys = NULL; /* gcc is a turd */ |
| 630 | |
| 631 | link_obj(&parent_group->cg_item, &group->cg_item); |
| 632 | |
| 633 | if (parent_group->cg_subsys) |
| 634 | subsys = parent_group->cg_subsys; |
| 635 | else if (configfs_is_root(&parent_group->cg_item)) |
| 636 | subsys = to_configfs_subsystem(group); |
| 637 | else |
| 638 | BUG(); |
| 639 | group->cg_subsys = subsys; |
| 640 | |
| 641 | if (group->default_groups) { |
| 642 | for (i = 0; group->default_groups[i]; i++) { |
| 643 | new_group = group->default_groups[i]; |
| 644 | link_group(group, new_group); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * The goal is that configfs_attach_item() (and |
| 651 | * configfs_attach_group()) can be called from either the VFS or this |
| 652 | * module. That is, they assume that the items have been created, |
| 653 | * the dentry allocated, and the dcache is all ready to go. |
| 654 | * |
| 655 | * If they fail, they must clean up after themselves as if they |
| 656 | * had never been called. The caller (VFS or local function) will |
| 657 | * handle cleaning up the dcache bits. |
| 658 | * |
| 659 | * configfs_detach_group() and configfs_detach_item() behave similarly on |
| 660 | * the way out. They assume that the proper semaphores are held, they |
| 661 | * clean up the configfs items, and they expect their callers will |
| 662 | * handle the dcache bits. |
| 663 | */ |
| 664 | static int configfs_attach_item(struct config_item *parent_item, |
| 665 | struct config_item *item, |
| 666 | struct dentry *dentry) |
| 667 | { |
| 668 | int ret; |
| 669 | |
| 670 | ret = configfs_create_dir(item, dentry); |
| 671 | if (!ret) { |
| 672 | ret = populate_attrs(item); |
| 673 | if (ret) { |
| 674 | configfs_remove_dir(item); |
| 675 | d_delete(dentry); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | static void configfs_detach_item(struct config_item *item) |
| 683 | { |
| 684 | detach_attrs(item); |
| 685 | configfs_remove_dir(item); |
| 686 | } |
| 687 | |
| 688 | static int configfs_attach_group(struct config_item *parent_item, |
| 689 | struct config_item *item, |
| 690 | struct dentry *dentry) |
| 691 | { |
| 692 | int ret; |
| 693 | struct configfs_dirent *sd; |
| 694 | |
| 695 | ret = configfs_attach_item(parent_item, item, dentry); |
| 696 | if (!ret) { |
| 697 | sd = dentry->d_fsdata; |
| 698 | sd->s_type |= CONFIGFS_USET_DIR; |
| 699 | |
| 700 | ret = populate_groups(to_config_group(item)); |
| 701 | if (ret) { |
| 702 | configfs_detach_item(item); |
| 703 | d_delete(dentry); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | static void configfs_detach_group(struct config_item *item) |
| 711 | { |
| 712 | detach_groups(to_config_group(item)); |
| 713 | configfs_detach_item(item); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Drop the initial reference from make_item()/make_group() |
| 718 | * This function assumes that reference is held on item |
| 719 | * and that item holds a valid reference to the parent. Also, it |
| 720 | * assumes the caller has validated ci_type. |
| 721 | */ |
| 722 | static void client_drop_item(struct config_item *parent_item, |
| 723 | struct config_item *item) |
| 724 | { |
| 725 | struct config_item_type *type; |
| 726 | |
| 727 | type = parent_item->ci_type; |
| 728 | BUG_ON(!type); |
| 729 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 730 | /* |
| 731 | * If ->drop_item() exists, it is responsible for the |
| 732 | * config_item_put(). |
| 733 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 734 | if (type->ct_group_ops && type->ct_group_ops->drop_item) |
| 735 | type->ct_group_ops->drop_item(to_config_group(parent_item), |
| 736 | item); |
| 737 | else |
| 738 | config_item_put(item); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 743 | { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 744 | int ret, module_got = 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 745 | struct config_group *group; |
| 746 | struct config_item *item; |
| 747 | struct config_item *parent_item; |
| 748 | struct configfs_subsystem *subsys; |
| 749 | struct configfs_dirent *sd; |
| 750 | struct config_item_type *type; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 751 | struct module *owner = NULL; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 752 | char *name; |
| 753 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 754 | if (dentry->d_parent == configfs_sb->s_root) { |
| 755 | ret = -EPERM; |
| 756 | goto out; |
| 757 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 758 | |
| 759 | sd = dentry->d_parent->d_fsdata; |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 760 | if (!(sd->s_type & CONFIGFS_USET_DIR)) { |
| 761 | ret = -EPERM; |
| 762 | goto out; |
| 763 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 764 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 765 | /* Get a working ref for the duration of this function */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 766 | parent_item = configfs_get_config_item(dentry->d_parent); |
| 767 | type = parent_item->ci_type; |
| 768 | subsys = to_config_group(parent_item)->cg_subsys; |
| 769 | BUG_ON(!subsys); |
| 770 | |
| 771 | if (!type || !type->ct_group_ops || |
| 772 | (!type->ct_group_ops->make_group && |
| 773 | !type->ct_group_ops->make_item)) { |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 774 | ret = -EPERM; /* Lack-of-mkdir returns -EPERM */ |
| 775 | goto out_put; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL); |
| 779 | if (!name) { |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 780 | ret = -ENOMEM; |
| 781 | goto out_put; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 782 | } |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 783 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 784 | snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name); |
| 785 | |
| 786 | down(&subsys->su_sem); |
| 787 | group = NULL; |
| 788 | item = NULL; |
| 789 | if (type->ct_group_ops->make_group) { |
| 790 | group = type->ct_group_ops->make_group(to_config_group(parent_item), name); |
| 791 | if (group) { |
| 792 | link_group(to_config_group(parent_item), group); |
| 793 | item = &group->cg_item; |
| 794 | } |
| 795 | } else { |
| 796 | item = type->ct_group_ops->make_item(to_config_group(parent_item), name); |
| 797 | if (item) |
| 798 | link_obj(parent_item, item); |
| 799 | } |
| 800 | up(&subsys->su_sem); |
| 801 | |
| 802 | kfree(name); |
| 803 | if (!item) { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 804 | /* |
| 805 | * If item == NULL, then link_obj() was never called. |
| 806 | * There are no extra references to clean up. |
| 807 | */ |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 808 | ret = -ENOMEM; |
| 809 | goto out_put; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 812 | /* |
| 813 | * link_obj() has been called (via link_group() for groups). |
| 814 | * From here on out, errors must clean that up. |
| 815 | */ |
| 816 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 817 | type = item->ci_type; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 818 | if (!type) { |
| 819 | ret = -EINVAL; |
| 820 | goto out_unlink; |
| 821 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 822 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 823 | owner = type->ct_owner; |
| 824 | if (!try_module_get(owner)) { |
| 825 | ret = -EINVAL; |
| 826 | goto out_unlink; |
| 827 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 828 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 829 | /* |
| 830 | * I hate doing it this way, but if there is |
| 831 | * an error, module_put() probably should |
| 832 | * happen after any cleanup. |
| 833 | */ |
| 834 | module_got = 1; |
| 835 | |
| 836 | if (group) |
| 837 | ret = configfs_attach_group(parent_item, item, dentry); |
| 838 | else |
| 839 | ret = configfs_attach_item(parent_item, item, dentry); |
| 840 | |
| 841 | out_unlink: |
| 842 | if (ret) { |
| 843 | /* Tear down everything we built up */ |
| 844 | down(&subsys->su_sem); |
| 845 | if (group) |
| 846 | unlink_group(group); |
| 847 | else |
| 848 | unlink_obj(item); |
| 849 | client_drop_item(parent_item, item); |
| 850 | up(&subsys->su_sem); |
| 851 | |
| 852 | if (module_got) |
| 853 | module_put(owner); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 856 | out_put: |
| 857 | /* |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 858 | * link_obj()/link_group() took a reference from child->parent, |
| 859 | * so the parent is safely pinned. We can drop our working |
| 860 | * reference. |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 861 | */ |
| 862 | config_item_put(parent_item); |
| 863 | |
| 864 | out: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 865 | return ret; |
| 866 | } |
| 867 | |
| 868 | static int configfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 869 | { |
| 870 | struct config_item *parent_item; |
| 871 | struct config_item *item; |
| 872 | struct configfs_subsystem *subsys; |
| 873 | struct configfs_dirent *sd; |
| 874 | struct module *owner = NULL; |
| 875 | int ret; |
| 876 | |
| 877 | if (dentry->d_parent == configfs_sb->s_root) |
| 878 | return -EPERM; |
| 879 | |
| 880 | sd = dentry->d_fsdata; |
| 881 | if (sd->s_type & CONFIGFS_USET_DEFAULT) |
| 882 | return -EPERM; |
| 883 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 884 | /* Get a working ref until we have the child */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 885 | parent_item = configfs_get_config_item(dentry->d_parent); |
| 886 | subsys = to_config_group(parent_item)->cg_subsys; |
| 887 | BUG_ON(!subsys); |
| 888 | |
| 889 | if (!parent_item->ci_type) { |
| 890 | config_item_put(parent_item); |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | ret = configfs_detach_prep(dentry); |
| 895 | if (ret) { |
| 896 | configfs_detach_rollback(dentry); |
| 897 | config_item_put(parent_item); |
| 898 | return ret; |
| 899 | } |
| 900 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 901 | /* Get a working ref for the duration of this function */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 902 | item = configfs_get_config_item(dentry); |
| 903 | |
| 904 | /* Drop reference from above, item already holds one. */ |
| 905 | config_item_put(parent_item); |
| 906 | |
| 907 | if (item->ci_type) |
| 908 | owner = item->ci_type->ct_owner; |
| 909 | |
| 910 | if (sd->s_type & CONFIGFS_USET_DIR) { |
| 911 | configfs_detach_group(item); |
| 912 | |
| 913 | down(&subsys->su_sem); |
| 914 | unlink_group(to_config_group(item)); |
| 915 | } else { |
| 916 | configfs_detach_item(item); |
| 917 | |
| 918 | down(&subsys->su_sem); |
| 919 | unlink_obj(item); |
| 920 | } |
| 921 | |
| 922 | client_drop_item(parent_item, item); |
| 923 | up(&subsys->su_sem); |
| 924 | |
| 925 | /* Drop our reference from above */ |
| 926 | config_item_put(item); |
| 927 | |
| 928 | module_put(owner); |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 933 | const struct inode_operations configfs_dir_inode_operations = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 934 | .mkdir = configfs_mkdir, |
| 935 | .rmdir = configfs_rmdir, |
| 936 | .symlink = configfs_symlink, |
| 937 | .unlink = configfs_unlink, |
| 938 | .lookup = configfs_lookup, |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 939 | .setattr = configfs_setattr, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 940 | }; |
| 941 | |
| 942 | #if 0 |
| 943 | int configfs_rename_dir(struct config_item * item, const char *new_name) |
| 944 | { |
| 945 | int error = 0; |
| 946 | struct dentry * new_dentry, * parent; |
| 947 | |
| 948 | if (!strcmp(config_item_name(item), new_name)) |
| 949 | return -EINVAL; |
| 950 | |
| 951 | if (!item->parent) |
| 952 | return -EINVAL; |
| 953 | |
| 954 | down_write(&configfs_rename_sem); |
| 955 | parent = item->parent->dentry; |
| 956 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 957 | mutex_lock(&parent->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 958 | |
| 959 | new_dentry = lookup_one_len(new_name, parent, strlen(new_name)); |
| 960 | if (!IS_ERR(new_dentry)) { |
Joel Becker | e7515d0 | 2006-03-10 11:42:30 -0800 | [diff] [blame] | 961 | if (!new_dentry->d_inode) { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 962 | error = config_item_set_name(item, "%s", new_name); |
| 963 | if (!error) { |
| 964 | d_add(new_dentry, NULL); |
| 965 | d_move(item->dentry, new_dentry); |
| 966 | } |
| 967 | else |
| 968 | d_delete(new_dentry); |
| 969 | } else |
| 970 | error = -EEXIST; |
| 971 | dput(new_dentry); |
| 972 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 973 | mutex_unlock(&parent->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 974 | up_write(&configfs_rename_sem); |
| 975 | |
| 976 | return error; |
| 977 | } |
| 978 | #endif |
| 979 | |
| 980 | static int configfs_dir_open(struct inode *inode, struct file *file) |
| 981 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 982 | struct dentry * dentry = file->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 983 | struct configfs_dirent * parent_sd = dentry->d_fsdata; |
| 984 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 985 | mutex_lock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 986 | file->private_data = configfs_new_dirent(parent_sd, NULL); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 987 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 988 | |
| 989 | return file->private_data ? 0 : -ENOMEM; |
| 990 | |
| 991 | } |
| 992 | |
| 993 | static int configfs_dir_close(struct inode *inode, struct file *file) |
| 994 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 995 | struct dentry * dentry = file->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 996 | struct configfs_dirent * cursor = file->private_data; |
| 997 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 998 | mutex_lock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 999 | list_del_init(&cursor->s_sibling); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1000 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1001 | |
| 1002 | release_configfs_dirent(cursor); |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | /* Relationship between s_mode and the DT_xxx types */ |
| 1008 | static inline unsigned char dt_type(struct configfs_dirent *sd) |
| 1009 | { |
| 1010 | return (sd->s_mode >> 12) & 15; |
| 1011 | } |
| 1012 | |
| 1013 | static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 1014 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1015 | struct dentry *dentry = filp->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1016 | struct configfs_dirent * parent_sd = dentry->d_fsdata; |
| 1017 | struct configfs_dirent *cursor = filp->private_data; |
| 1018 | struct list_head *p, *q = &cursor->s_sibling; |
| 1019 | ino_t ino; |
| 1020 | int i = filp->f_pos; |
| 1021 | |
| 1022 | switch (i) { |
| 1023 | case 0: |
| 1024 | ino = dentry->d_inode->i_ino; |
| 1025 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 1026 | break; |
| 1027 | filp->f_pos++; |
| 1028 | i++; |
| 1029 | /* fallthrough */ |
| 1030 | case 1: |
| 1031 | ino = parent_ino(dentry); |
| 1032 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 1033 | break; |
| 1034 | filp->f_pos++; |
| 1035 | i++; |
| 1036 | /* fallthrough */ |
| 1037 | default: |
| 1038 | if (filp->f_pos == 2) { |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 1039 | list_move(q, &parent_sd->s_children); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1040 | } |
| 1041 | for (p=q->next; p!= &parent_sd->s_children; p=p->next) { |
| 1042 | struct configfs_dirent *next; |
| 1043 | const char * name; |
| 1044 | int len; |
| 1045 | |
| 1046 | next = list_entry(p, struct configfs_dirent, |
| 1047 | s_sibling); |
| 1048 | if (!next->s_element) |
| 1049 | continue; |
| 1050 | |
| 1051 | name = configfs_get_name(next); |
| 1052 | len = strlen(name); |
| 1053 | if (next->s_dentry) |
| 1054 | ino = next->s_dentry->d_inode->i_ino; |
| 1055 | else |
| 1056 | ino = iunique(configfs_sb, 2); |
| 1057 | |
| 1058 | if (filldir(dirent, name, len, filp->f_pos, ino, |
| 1059 | dt_type(next)) < 0) |
| 1060 | return 0; |
| 1061 | |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 1062 | list_move(q, p); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1063 | p = q; |
| 1064 | filp->f_pos++; |
| 1065 | } |
| 1066 | } |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin) |
| 1071 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1072 | struct dentry * dentry = file->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1073 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1074 | mutex_lock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1075 | switch (origin) { |
| 1076 | case 1: |
| 1077 | offset += file->f_pos; |
| 1078 | case 0: |
| 1079 | if (offset >= 0) |
| 1080 | break; |
| 1081 | default: |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1082 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | if (offset != file->f_pos) { |
| 1086 | file->f_pos = offset; |
| 1087 | if (file->f_pos >= 2) { |
| 1088 | struct configfs_dirent *sd = dentry->d_fsdata; |
| 1089 | struct configfs_dirent *cursor = file->private_data; |
| 1090 | struct list_head *p; |
| 1091 | loff_t n = file->f_pos - 2; |
| 1092 | |
| 1093 | list_del(&cursor->s_sibling); |
| 1094 | p = sd->s_children.next; |
| 1095 | while (n && p != &sd->s_children) { |
| 1096 | struct configfs_dirent *next; |
| 1097 | next = list_entry(p, struct configfs_dirent, |
| 1098 | s_sibling); |
| 1099 | if (next->s_element) |
| 1100 | n--; |
| 1101 | p = p->next; |
| 1102 | } |
| 1103 | list_add_tail(&cursor->s_sibling, p); |
| 1104 | } |
| 1105 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1106 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1107 | return offset; |
| 1108 | } |
| 1109 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1110 | const struct file_operations configfs_dir_operations = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1111 | .open = configfs_dir_open, |
| 1112 | .release = configfs_dir_close, |
| 1113 | .llseek = configfs_dir_lseek, |
| 1114 | .read = generic_read_dir, |
| 1115 | .readdir = configfs_readdir, |
| 1116 | }; |
| 1117 | |
| 1118 | int configfs_register_subsystem(struct configfs_subsystem *subsys) |
| 1119 | { |
| 1120 | int err; |
| 1121 | struct config_group *group = &subsys->su_group; |
| 1122 | struct qstr name; |
| 1123 | struct dentry *dentry; |
| 1124 | struct configfs_dirent *sd; |
| 1125 | |
| 1126 | err = configfs_pin_fs(); |
| 1127 | if (err) |
| 1128 | return err; |
| 1129 | |
| 1130 | if (!group->cg_item.ci_name) |
| 1131 | group->cg_item.ci_name = group->cg_item.ci_namebuf; |
| 1132 | |
| 1133 | sd = configfs_sb->s_root->d_fsdata; |
| 1134 | link_group(to_config_group(sd->s_element), group); |
| 1135 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1136 | mutex_lock(&configfs_sb->s_root->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1137 | |
| 1138 | name.name = group->cg_item.ci_name; |
| 1139 | name.len = strlen(name.name); |
| 1140 | name.hash = full_name_hash(name.name, name.len); |
| 1141 | |
| 1142 | err = -ENOMEM; |
| 1143 | dentry = d_alloc(configfs_sb->s_root, &name); |
Joel Becker | afdf04e | 2007-03-05 15:49:49 -0800 | [diff] [blame] | 1144 | if (dentry) { |
| 1145 | d_add(dentry, NULL); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1146 | |
Joel Becker | afdf04e | 2007-03-05 15:49:49 -0800 | [diff] [blame] | 1147 | err = configfs_attach_group(sd->s_element, &group->cg_item, |
| 1148 | dentry); |
| 1149 | if (err) { |
| 1150 | d_delete(dentry); |
| 1151 | dput(dentry); |
| 1152 | } |
| 1153 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1154 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1155 | mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1156 | |
Joel Becker | afdf04e | 2007-03-05 15:49:49 -0800 | [diff] [blame] | 1157 | if (err) { |
| 1158 | unlink_group(group); |
| 1159 | configfs_release_fs(); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | return err; |
| 1163 | } |
| 1164 | |
| 1165 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys) |
| 1166 | { |
| 1167 | struct config_group *group = &subsys->su_group; |
| 1168 | struct dentry *dentry = group->cg_item.ci_dentry; |
| 1169 | |
| 1170 | if (dentry->d_parent != configfs_sb->s_root) { |
| 1171 | printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n"); |
| 1172 | return; |
| 1173 | } |
| 1174 | |
Mark Fasheh | 55ed160 | 2006-10-20 14:55:54 -0700 | [diff] [blame] | 1175 | mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex, |
| 1176 | I_MUTEX_PARENT); |
| 1177 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1178 | if (configfs_detach_prep(dentry)) { |
| 1179 | printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n"); |
| 1180 | } |
| 1181 | configfs_detach_group(&group->cg_item); |
| 1182 | dentry->d_inode->i_flags |= S_DEAD; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1183 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1184 | |
| 1185 | d_delete(dentry); |
| 1186 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1187 | mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1188 | |
| 1189 | dput(dentry); |
| 1190 | |
| 1191 | unlink_group(group); |
| 1192 | configfs_release_fs(); |
| 1193 | } |
| 1194 | |
| 1195 | EXPORT_SYMBOL(configfs_register_subsystem); |
| 1196 | EXPORT_SYMBOL(configfs_unregister_subsystem); |