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