Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * dir.c - Operations for configfs directories. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public |
| 17 | * License along with this program; if not, write to the |
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | * Boston, MA 021110-1307, USA. |
| 20 | * |
| 21 | * Based on sysfs: |
| 22 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel |
| 23 | * |
| 24 | * configfs Copyright (C) 2005 Oracle. All rights reserved. |
| 25 | */ |
| 26 | |
| 27 | #undef DEBUG |
| 28 | |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/mount.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/slab.h> |
Louis Rilling | 107ed40 | 2008-06-16 19:01:00 +0200 | [diff] [blame^] | 33 | #include <linux/err.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 34 | |
| 35 | #include <linux/configfs.h> |
| 36 | #include "configfs_internal.h" |
| 37 | |
| 38 | DECLARE_RWSEM(configfs_rename_sem); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 39 | /* |
| 40 | * Protects mutations of configfs_dirent linkage together with proper i_mutex |
Louis Rilling | 5301a77 | 2008-06-16 19:00:59 +0200 | [diff] [blame] | 41 | * Also protects mutations of symlinks linkage to target configfs_dirent |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 42 | * Mutators of configfs_dirent linkage must *both* have the proper inode locked |
| 43 | * and configfs_dirent_lock locked, in that order. |
Louis Rilling | 5301a77 | 2008-06-16 19:00:59 +0200 | [diff] [blame] | 44 | * This allows one to safely traverse configfs_dirent trees and symlinks without |
| 45 | * having to lock inodes. |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 46 | */ |
| 47 | DEFINE_SPINLOCK(configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 48 | |
| 49 | static void configfs_d_iput(struct dentry * dentry, |
| 50 | struct inode * inode) |
| 51 | { |
| 52 | struct configfs_dirent * sd = dentry->d_fsdata; |
| 53 | |
| 54 | if (sd) { |
| 55 | BUG_ON(sd->s_dentry != dentry); |
| 56 | sd->s_dentry = NULL; |
| 57 | configfs_put(sd); |
| 58 | } |
| 59 | iput(inode); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * We _must_ delete our dentries on last dput, as the chain-to-parent |
| 64 | * behavior is required to clear the parents of default_groups. |
| 65 | */ |
| 66 | static int configfs_d_delete(struct dentry *dentry) |
| 67 | { |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | static struct dentry_operations configfs_dentry_ops = { |
| 72 | .d_iput = configfs_d_iput, |
| 73 | /* simple_delete_dentry() isn't exported */ |
| 74 | .d_delete = configfs_d_delete, |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Allocates a new configfs_dirent and links it to the parent configfs_dirent |
| 79 | */ |
| 80 | static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd, |
| 81 | void * element) |
| 82 | { |
| 83 | struct configfs_dirent * sd; |
| 84 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 85 | sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 86 | if (!sd) |
Louis Rilling | 107ed40 | 2008-06-16 19:01:00 +0200 | [diff] [blame^] | 87 | return ERR_PTR(-ENOMEM); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 88 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 89 | atomic_set(&sd->s_count, 1); |
| 90 | INIT_LIST_HEAD(&sd->s_links); |
| 91 | INIT_LIST_HEAD(&sd->s_children); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 92 | sd->s_element = element; |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 93 | spin_lock(&configfs_dirent_lock); |
| 94 | list_add(&sd->s_sibling, &parent_sd->s_children); |
| 95 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 96 | |
| 97 | return sd; |
| 98 | } |
| 99 | |
Joel Becker | b4c98f6 | 2006-09-13 11:01:19 -0700 | [diff] [blame] | 100 | /* |
| 101 | * |
| 102 | * Return -EEXIST if there is already a configfs element with the same |
| 103 | * name for the same parent. |
| 104 | * |
| 105 | * called with parent inode's i_mutex held |
| 106 | */ |
Adrian Bunk | 58d206c | 2006-11-20 03:24:00 +0100 | [diff] [blame] | 107 | static int configfs_dirent_exists(struct configfs_dirent *parent_sd, |
| 108 | const unsigned char *new) |
Joel Becker | b4c98f6 | 2006-09-13 11:01:19 -0700 | [diff] [blame] | 109 | { |
| 110 | struct configfs_dirent * sd; |
| 111 | |
| 112 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 113 | if (sd->s_element) { |
| 114 | const unsigned char *existing = configfs_get_name(sd); |
| 115 | if (strcmp(existing, new)) |
| 116 | continue; |
| 117 | else |
| 118 | return -EEXIST; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 126 | int configfs_make_dirent(struct configfs_dirent * parent_sd, |
| 127 | struct dentry * dentry, void * element, |
| 128 | umode_t mode, int type) |
| 129 | { |
| 130 | struct configfs_dirent * sd; |
| 131 | |
| 132 | sd = configfs_new_dirent(parent_sd, element); |
Louis Rilling | 107ed40 | 2008-06-16 19:01:00 +0200 | [diff] [blame^] | 133 | if (IS_ERR(sd)) |
| 134 | return PTR_ERR(sd); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 135 | |
| 136 | sd->s_mode = mode; |
| 137 | sd->s_type = type; |
| 138 | sd->s_dentry = dentry; |
| 139 | if (dentry) { |
| 140 | dentry->d_fsdata = configfs_get(sd); |
| 141 | dentry->d_op = &configfs_dentry_ops; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int init_dir(struct inode * inode) |
| 148 | { |
| 149 | inode->i_op = &configfs_dir_inode_operations; |
| 150 | inode->i_fop = &configfs_dir_operations; |
| 151 | |
| 152 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 153 | inc_nlink(inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 157 | static int configfs_init_file(struct inode * inode) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 158 | { |
| 159 | inode->i_size = PAGE_SIZE; |
| 160 | inode->i_fop = &configfs_file_operations; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int init_symlink(struct inode * inode) |
| 165 | { |
| 166 | inode->i_op = &configfs_symlink_inode_operations; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int create_dir(struct config_item * k, struct dentry * p, |
| 171 | struct dentry * d) |
| 172 | { |
| 173 | int error; |
| 174 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
| 175 | |
Joel Becker | b4c98f6 | 2006-09-13 11:01:19 -0700 | [diff] [blame] | 176 | error = configfs_dirent_exists(p->d_fsdata, d->d_name.name); |
| 177 | if (!error) |
| 178 | error = configfs_make_dirent(p->d_fsdata, d, k, mode, |
| 179 | CONFIGFS_DIR); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 180 | if (!error) { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 181 | error = configfs_create(d, mode, init_dir); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 182 | if (!error) { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 183 | inc_nlink(p->d_inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 184 | (d)->d_op = &configfs_dentry_ops; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 185 | } else { |
| 186 | struct configfs_dirent *sd = d->d_fsdata; |
| 187 | if (sd) { |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 188 | spin_lock(&configfs_dirent_lock); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 189 | list_del_init(&sd->s_sibling); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 190 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 191 | configfs_put(sd); |
| 192 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | return error; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * configfs_create_dir - create a directory for an config_item. |
| 201 | * @item: config_itemwe're creating directory for. |
| 202 | * @dentry: config_item's dentry. |
| 203 | */ |
| 204 | |
| 205 | static int configfs_create_dir(struct config_item * item, struct dentry *dentry) |
| 206 | { |
| 207 | struct dentry * parent; |
| 208 | int error = 0; |
| 209 | |
| 210 | BUG_ON(!item); |
| 211 | |
| 212 | if (item->ci_parent) |
| 213 | parent = item->ci_parent->ci_dentry; |
| 214 | else if (configfs_mount && configfs_mount->mnt_sb) |
| 215 | parent = configfs_mount->mnt_sb->s_root; |
| 216 | else |
| 217 | return -EFAULT; |
| 218 | |
| 219 | error = create_dir(item,parent,dentry); |
| 220 | if (!error) |
| 221 | item->ci_dentry = dentry; |
| 222 | return error; |
| 223 | } |
| 224 | |
| 225 | int configfs_create_link(struct configfs_symlink *sl, |
| 226 | struct dentry *parent, |
| 227 | struct dentry *dentry) |
| 228 | { |
| 229 | int err = 0; |
| 230 | umode_t mode = S_IFLNK | S_IRWXUGO; |
| 231 | |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 232 | err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode, |
| 233 | CONFIGFS_ITEM_LINK); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 234 | if (!err) { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 235 | err = configfs_create(dentry, mode, init_symlink); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 236 | if (!err) |
| 237 | dentry->d_op = &configfs_dentry_ops; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 238 | else { |
| 239 | struct configfs_dirent *sd = dentry->d_fsdata; |
| 240 | if (sd) { |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 241 | spin_lock(&configfs_dirent_lock); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 242 | list_del_init(&sd->s_sibling); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 243 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 244 | configfs_put(sd); |
| 245 | } |
| 246 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 247 | } |
| 248 | return err; |
| 249 | } |
| 250 | |
| 251 | static void remove_dir(struct dentry * d) |
| 252 | { |
| 253 | struct dentry * parent = dget(d->d_parent); |
| 254 | struct configfs_dirent * sd; |
| 255 | |
| 256 | sd = d->d_fsdata; |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 257 | spin_lock(&configfs_dirent_lock); |
Joel Becker | e7515d0 | 2006-03-10 11:42:30 -0800 | [diff] [blame] | 258 | list_del_init(&sd->s_sibling); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 259 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 260 | configfs_put(sd); |
| 261 | if (d->d_inode) |
| 262 | simple_rmdir(parent->d_inode,d); |
| 263 | |
| 264 | pr_debug(" o %s removing done (%d)\n",d->d_name.name, |
| 265 | atomic_read(&d->d_count)); |
| 266 | |
| 267 | dput(parent); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * configfs_remove_dir - remove an config_item's directory. |
| 272 | * @item: config_item we're removing. |
| 273 | * |
| 274 | * The only thing special about this is that we remove any files in |
| 275 | * the directory before we remove the directory, and we've inlined |
| 276 | * what used to be configfs_rmdir() below, instead of calling separately. |
| 277 | */ |
| 278 | |
| 279 | static void configfs_remove_dir(struct config_item * item) |
| 280 | { |
| 281 | struct dentry * dentry = dget(item->ci_dentry); |
| 282 | |
| 283 | if (!dentry) |
| 284 | return; |
| 285 | |
| 286 | remove_dir(dentry); |
| 287 | /** |
| 288 | * Drop reference from dget() on entrance. |
| 289 | */ |
| 290 | dput(dentry); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /* attaches attribute's configfs_dirent to the dentry corresponding to the |
| 295 | * attribute file |
| 296 | */ |
| 297 | static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry) |
| 298 | { |
| 299 | struct configfs_attribute * attr = sd->s_element; |
| 300 | int error; |
| 301 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 302 | dentry->d_fsdata = configfs_get(sd); |
| 303 | sd->s_dentry = dentry; |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 304 | error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, |
| 305 | configfs_init_file); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 306 | if (error) { |
| 307 | configfs_put(sd); |
| 308 | return error; |
| 309 | } |
| 310 | |
| 311 | dentry->d_op = &configfs_dentry_ops; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 312 | d_rehash(dentry); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static struct dentry * configfs_lookup(struct inode *dir, |
| 318 | struct dentry *dentry, |
| 319 | struct nameidata *nd) |
| 320 | { |
| 321 | struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata; |
| 322 | struct configfs_dirent * sd; |
| 323 | int found = 0; |
| 324 | int err = 0; |
| 325 | |
| 326 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 327 | if (sd->s_type & CONFIGFS_NOT_PINNED) { |
| 328 | const unsigned char * name = configfs_get_name(sd); |
| 329 | |
| 330 | if (strcmp(name, dentry->d_name.name)) |
| 331 | continue; |
| 332 | |
| 333 | found = 1; |
| 334 | err = configfs_attach_attr(sd, dentry); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (!found) { |
| 340 | /* |
| 341 | * If it doesn't exist and it isn't a NOT_PINNED item, |
| 342 | * it must be negative. |
| 343 | */ |
| 344 | return simple_lookup(dir, dentry, nd); |
| 345 | } |
| 346 | |
| 347 | return ERR_PTR(err); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 352 | * attributes and are removed by rmdir(). We recurse, taking i_mutex |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 353 | * on all children that are candidates for default detach. If the |
| 354 | * result is clean, then configfs_detach_group() will handle dropping |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 355 | * 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] | 356 | * holders via configfs_detach_rollback(). |
| 357 | */ |
| 358 | static int configfs_detach_prep(struct dentry *dentry) |
| 359 | { |
| 360 | struct configfs_dirent *parent_sd = dentry->d_fsdata; |
| 361 | struct configfs_dirent *sd; |
| 362 | int ret; |
| 363 | |
| 364 | ret = -EBUSY; |
| 365 | if (!list_empty(&parent_sd->s_links)) |
| 366 | goto out; |
| 367 | |
| 368 | ret = 0; |
| 369 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 370 | if (sd->s_type & CONFIGFS_NOT_PINNED) |
| 371 | continue; |
| 372 | if (sd->s_type & CONFIGFS_USET_DEFAULT) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 373 | mutex_lock(&sd->s_dentry->d_inode->i_mutex); |
| 374 | /* Mark that we've taken i_mutex */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 375 | sd->s_type |= CONFIGFS_USET_DROPPING; |
| 376 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 377 | /* |
| 378 | * Yup, recursive. If there's a problem, blame |
| 379 | * deep nesting of default_groups |
| 380 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 381 | ret = configfs_detach_prep(sd->s_dentry); |
| 382 | if (!ret) |
Joel Becker | e7515d0 | 2006-03-10 11:42:30 -0800 | [diff] [blame] | 383 | continue; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 384 | } else |
| 385 | ret = -ENOTEMPTY; |
| 386 | |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | out: |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 395 | * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 396 | * set. |
| 397 | */ |
| 398 | static void configfs_detach_rollback(struct dentry *dentry) |
| 399 | { |
| 400 | struct configfs_dirent *parent_sd = dentry->d_fsdata; |
| 401 | struct configfs_dirent *sd; |
| 402 | |
| 403 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 404 | if (sd->s_type & CONFIGFS_USET_DEFAULT) { |
| 405 | configfs_detach_rollback(sd->s_dentry); |
| 406 | |
| 407 | if (sd->s_type & CONFIGFS_USET_DROPPING) { |
| 408 | sd->s_type &= ~CONFIGFS_USET_DROPPING; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 409 | mutex_unlock(&sd->s_dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static void detach_attrs(struct config_item * item) |
| 416 | { |
| 417 | struct dentry * dentry = dget(item->ci_dentry); |
| 418 | struct configfs_dirent * parent_sd; |
| 419 | struct configfs_dirent * sd, * tmp; |
| 420 | |
| 421 | if (!dentry) |
| 422 | return; |
| 423 | |
| 424 | pr_debug("configfs %s: dropping attrs for dir\n", |
| 425 | dentry->d_name.name); |
| 426 | |
| 427 | parent_sd = dentry->d_fsdata; |
| 428 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { |
| 429 | if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED)) |
| 430 | continue; |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 431 | spin_lock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 432 | list_del_init(&sd->s_sibling); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 433 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 434 | configfs_drop_dentry(sd, dentry); |
| 435 | configfs_put(sd); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Drop reference from dget() on entrance. |
| 440 | */ |
| 441 | dput(dentry); |
| 442 | } |
| 443 | |
| 444 | static int populate_attrs(struct config_item *item) |
| 445 | { |
| 446 | struct config_item_type *t = item->ci_type; |
| 447 | struct configfs_attribute *attr; |
| 448 | int error = 0; |
| 449 | int i; |
| 450 | |
| 451 | if (!t) |
| 452 | return -EINVAL; |
| 453 | if (t->ct_attrs) { |
| 454 | for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) { |
| 455 | if ((error = configfs_create_file(item, attr))) |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (error) |
| 461 | detach_attrs(item); |
| 462 | |
| 463 | return error; |
| 464 | } |
| 465 | |
| 466 | static int configfs_attach_group(struct config_item *parent_item, |
| 467 | struct config_item *item, |
| 468 | struct dentry *dentry); |
| 469 | static void configfs_detach_group(struct config_item *item); |
| 470 | |
| 471 | static void detach_groups(struct config_group *group) |
| 472 | { |
| 473 | struct dentry * dentry = dget(group->cg_item.ci_dentry); |
| 474 | struct dentry *child; |
| 475 | struct configfs_dirent *parent_sd; |
| 476 | struct configfs_dirent *sd, *tmp; |
| 477 | |
| 478 | if (!dentry) |
| 479 | return; |
| 480 | |
| 481 | parent_sd = dentry->d_fsdata; |
| 482 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { |
| 483 | if (!sd->s_element || |
| 484 | !(sd->s_type & CONFIGFS_USET_DEFAULT)) |
| 485 | continue; |
| 486 | |
| 487 | child = sd->s_dentry; |
| 488 | |
| 489 | configfs_detach_group(sd->s_element); |
| 490 | child->d_inode->i_flags |= S_DEAD; |
| 491 | |
| 492 | /* |
| 493 | * From rmdir/unregister, a configfs_detach_prep() pass |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 494 | * has taken our i_mutex for us. Drop it. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 495 | * From mkdir/register cleanup, there is no sem held. |
| 496 | */ |
| 497 | if (sd->s_type & CONFIGFS_USET_DROPPING) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 498 | mutex_unlock(&child->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 499 | |
| 500 | d_delete(child); |
| 501 | dput(child); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Drop reference from dget() on entrance. |
| 506 | */ |
| 507 | dput(dentry); |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * This fakes mkdir(2) on a default_groups[] entry. It |
| 512 | * creates a dentry, attachs it, and then does fixup |
| 513 | * on the sd->s_type. |
| 514 | * |
| 515 | * We could, perhaps, tweak our parent's ->mkdir for a minute and |
| 516 | * try using vfs_mkdir. Just a thought. |
| 517 | */ |
| 518 | static int create_default_group(struct config_group *parent_group, |
| 519 | struct config_group *group) |
| 520 | { |
| 521 | int ret; |
| 522 | struct qstr name; |
| 523 | struct configfs_dirent *sd; |
| 524 | /* We trust the caller holds a reference to parent */ |
| 525 | struct dentry *child, *parent = parent_group->cg_item.ci_dentry; |
| 526 | |
| 527 | if (!group->cg_item.ci_name) |
| 528 | group->cg_item.ci_name = group->cg_item.ci_namebuf; |
| 529 | name.name = group->cg_item.ci_name; |
| 530 | name.len = strlen(name.name); |
| 531 | name.hash = full_name_hash(name.name, name.len); |
| 532 | |
| 533 | ret = -ENOMEM; |
| 534 | child = d_alloc(parent, &name); |
| 535 | if (child) { |
| 536 | d_add(child, NULL); |
| 537 | |
| 538 | ret = configfs_attach_group(&parent_group->cg_item, |
| 539 | &group->cg_item, child); |
| 540 | if (!ret) { |
| 541 | sd = child->d_fsdata; |
| 542 | sd->s_type |= CONFIGFS_USET_DEFAULT; |
| 543 | } else { |
| 544 | d_delete(child); |
| 545 | dput(child); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | static int populate_groups(struct config_group *group) |
| 553 | { |
| 554 | struct config_group *new_group; |
| 555 | struct dentry *dentry = group->cg_item.ci_dentry; |
| 556 | int ret = 0; |
| 557 | int i; |
| 558 | |
Eric Sesterhenn | cbca692 | 2006-03-23 00:36:54 +0100 | [diff] [blame] | 559 | if (group->default_groups) { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 560 | /* |
| 561 | * FYI, we're faking mkdir here |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 562 | * I'm not sure we need this semaphore, as we're called |
| 563 | * from our parent's mkdir. That holds our parent's |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 564 | * i_mutex, so afaik lookup cannot continue through our |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 565 | * parent to find us, let alone mess with our tree. |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 566 | * That said, taking our i_mutex is closer to mkdir |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 567 | * emulation, and shouldn't hurt. |
| 568 | */ |
Joonwoo Park | ba611ed | 2007-12-26 12:09:57 +0900 | [diff] [blame] | 569 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 570 | |
| 571 | for (i = 0; group->default_groups[i]; i++) { |
| 572 | new_group = group->default_groups[i]; |
| 573 | |
| 574 | ret = create_default_group(group, new_group); |
| 575 | if (ret) |
| 576 | break; |
| 577 | } |
| 578 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 579 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | if (ret) |
| 583 | detach_groups(group); |
| 584 | |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * All of link_obj/unlink_obj/link_group/unlink_group require that |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 590 | * subsys->su_mutex is held. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 591 | */ |
| 592 | |
| 593 | static void unlink_obj(struct config_item *item) |
| 594 | { |
| 595 | struct config_group *group; |
| 596 | |
| 597 | group = item->ci_group; |
| 598 | if (group) { |
| 599 | list_del_init(&item->ci_entry); |
| 600 | |
| 601 | item->ci_group = NULL; |
| 602 | item->ci_parent = NULL; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 603 | |
| 604 | /* Drop the reference for ci_entry */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 605 | config_item_put(item); |
| 606 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 607 | /* Drop the reference for ci_parent */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 608 | config_group_put(group); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static void link_obj(struct config_item *parent_item, struct config_item *item) |
| 613 | { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 614 | /* |
| 615 | * Parent seems redundant with group, but it makes certain |
| 616 | * traversals much nicer. |
| 617 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 618 | item->ci_parent = parent_item; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 619 | |
| 620 | /* |
| 621 | * We hold a reference on the parent for the child's ci_parent |
| 622 | * link. |
| 623 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 624 | item->ci_group = config_group_get(to_config_group(parent_item)); |
| 625 | list_add_tail(&item->ci_entry, &item->ci_group->cg_children); |
| 626 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 627 | /* |
| 628 | * We hold a reference on the child for ci_entry on the parent's |
| 629 | * cg_children |
| 630 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 631 | config_item_get(item); |
| 632 | } |
| 633 | |
| 634 | static void unlink_group(struct config_group *group) |
| 635 | { |
| 636 | int i; |
| 637 | struct config_group *new_group; |
| 638 | |
| 639 | if (group->default_groups) { |
| 640 | for (i = 0; group->default_groups[i]; i++) { |
| 641 | new_group = group->default_groups[i]; |
| 642 | unlink_group(new_group); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | group->cg_subsys = NULL; |
| 647 | unlink_obj(&group->cg_item); |
| 648 | } |
| 649 | |
| 650 | static void link_group(struct config_group *parent_group, struct config_group *group) |
| 651 | { |
| 652 | int i; |
| 653 | struct config_group *new_group; |
| 654 | struct configfs_subsystem *subsys = NULL; /* gcc is a turd */ |
| 655 | |
| 656 | link_obj(&parent_group->cg_item, &group->cg_item); |
| 657 | |
| 658 | if (parent_group->cg_subsys) |
| 659 | subsys = parent_group->cg_subsys; |
| 660 | else if (configfs_is_root(&parent_group->cg_item)) |
| 661 | subsys = to_configfs_subsystem(group); |
| 662 | else |
| 663 | BUG(); |
| 664 | group->cg_subsys = subsys; |
| 665 | |
| 666 | if (group->default_groups) { |
| 667 | for (i = 0; group->default_groups[i]; i++) { |
| 668 | new_group = group->default_groups[i]; |
| 669 | link_group(group, new_group); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * The goal is that configfs_attach_item() (and |
| 676 | * configfs_attach_group()) can be called from either the VFS or this |
| 677 | * module. That is, they assume that the items have been created, |
| 678 | * the dentry allocated, and the dcache is all ready to go. |
| 679 | * |
| 680 | * If they fail, they must clean up after themselves as if they |
| 681 | * had never been called. The caller (VFS or local function) will |
| 682 | * handle cleaning up the dcache bits. |
| 683 | * |
| 684 | * configfs_detach_group() and configfs_detach_item() behave similarly on |
| 685 | * the way out. They assume that the proper semaphores are held, they |
| 686 | * clean up the configfs items, and they expect their callers will |
| 687 | * handle the dcache bits. |
| 688 | */ |
| 689 | static int configfs_attach_item(struct config_item *parent_item, |
| 690 | struct config_item *item, |
| 691 | struct dentry *dentry) |
| 692 | { |
| 693 | int ret; |
| 694 | |
| 695 | ret = configfs_create_dir(item, dentry); |
| 696 | if (!ret) { |
| 697 | ret = populate_attrs(item); |
| 698 | if (ret) { |
| 699 | configfs_remove_dir(item); |
| 700 | d_delete(dentry); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | static void configfs_detach_item(struct config_item *item) |
| 708 | { |
| 709 | detach_attrs(item); |
| 710 | configfs_remove_dir(item); |
| 711 | } |
| 712 | |
| 713 | static int configfs_attach_group(struct config_item *parent_item, |
| 714 | struct config_item *item, |
| 715 | struct dentry *dentry) |
| 716 | { |
| 717 | int ret; |
| 718 | struct configfs_dirent *sd; |
| 719 | |
| 720 | ret = configfs_attach_item(parent_item, item, dentry); |
| 721 | if (!ret) { |
| 722 | sd = dentry->d_fsdata; |
| 723 | sd->s_type |= CONFIGFS_USET_DIR; |
| 724 | |
| 725 | ret = populate_groups(to_config_group(item)); |
| 726 | if (ret) { |
| 727 | configfs_detach_item(item); |
| 728 | d_delete(dentry); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | static void configfs_detach_group(struct config_item *item) |
| 736 | { |
| 737 | detach_groups(to_config_group(item)); |
| 738 | configfs_detach_item(item); |
| 739 | } |
| 740 | |
| 741 | /* |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 742 | * After the item has been detached from the filesystem view, we are |
| 743 | * ready to tear it out of the hierarchy. Notify the client before |
| 744 | * we do that so they can perform any cleanup that requires |
| 745 | * navigating the hierarchy. A client does not need to provide this |
| 746 | * callback. The subsystem semaphore MUST be held by the caller, and |
| 747 | * references must be valid for both items. It also assumes the |
| 748 | * caller has validated ci_type. |
| 749 | */ |
| 750 | static void client_disconnect_notify(struct config_item *parent_item, |
| 751 | struct config_item *item) |
| 752 | { |
| 753 | struct config_item_type *type; |
| 754 | |
| 755 | type = parent_item->ci_type; |
| 756 | BUG_ON(!type); |
| 757 | |
| 758 | if (type->ct_group_ops && type->ct_group_ops->disconnect_notify) |
| 759 | type->ct_group_ops->disconnect_notify(to_config_group(parent_item), |
| 760 | item); |
| 761 | } |
| 762 | |
| 763 | /* |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 764 | * Drop the initial reference from make_item()/make_group() |
| 765 | * This function assumes that reference is held on item |
| 766 | * and that item holds a valid reference to the parent. Also, it |
| 767 | * assumes the caller has validated ci_type. |
| 768 | */ |
| 769 | static void client_drop_item(struct config_item *parent_item, |
| 770 | struct config_item *item) |
| 771 | { |
| 772 | struct config_item_type *type; |
| 773 | |
| 774 | type = parent_item->ci_type; |
| 775 | BUG_ON(!type); |
| 776 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 777 | /* |
| 778 | * If ->drop_item() exists, it is responsible for the |
| 779 | * config_item_put(). |
| 780 | */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 781 | if (type->ct_group_ops && type->ct_group_ops->drop_item) |
| 782 | type->ct_group_ops->drop_item(to_config_group(parent_item), |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 783 | item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 784 | else |
| 785 | config_item_put(item); |
| 786 | } |
| 787 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 788 | #ifdef DEBUG |
| 789 | static void configfs_dump_one(struct configfs_dirent *sd, int level) |
| 790 | { |
| 791 | printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd)); |
| 792 | |
| 793 | #define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type); |
| 794 | type_print(CONFIGFS_ROOT); |
| 795 | type_print(CONFIGFS_DIR); |
| 796 | type_print(CONFIGFS_ITEM_ATTR); |
| 797 | type_print(CONFIGFS_ITEM_LINK); |
| 798 | type_print(CONFIGFS_USET_DIR); |
| 799 | type_print(CONFIGFS_USET_DEFAULT); |
| 800 | type_print(CONFIGFS_USET_DROPPING); |
| 801 | #undef type_print |
| 802 | } |
| 803 | |
| 804 | static int configfs_dump(struct configfs_dirent *sd, int level) |
| 805 | { |
| 806 | struct configfs_dirent *child_sd; |
| 807 | int ret = 0; |
| 808 | |
| 809 | configfs_dump_one(sd, level); |
| 810 | |
| 811 | if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT))) |
| 812 | return 0; |
| 813 | |
| 814 | list_for_each_entry(child_sd, &sd->s_children, s_sibling) { |
| 815 | ret = configfs_dump(child_sd, level + 2); |
| 816 | if (ret) |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | return ret; |
| 821 | } |
| 822 | #endif |
| 823 | |
| 824 | |
| 825 | /* |
| 826 | * configfs_depend_item() and configfs_undepend_item() |
| 827 | * |
| 828 | * WARNING: Do not call these from a configfs callback! |
| 829 | * |
| 830 | * This describes these functions and their helpers. |
| 831 | * |
| 832 | * Allow another kernel system to depend on a config_item. If this |
| 833 | * happens, the item cannot go away until the dependant can live without |
| 834 | * it. The idea is to give client modules as simple an interface as |
| 835 | * possible. When a system asks them to depend on an item, they just |
| 836 | * call configfs_depend_item(). If the item is live and the client |
| 837 | * driver is in good shape, we'll happily do the work for them. |
| 838 | * |
| 839 | * Why is the locking complex? Because configfs uses the VFS to handle |
| 840 | * all locking, but this function is called outside the normal |
| 841 | * VFS->configfs path. So it must take VFS locks to prevent the |
| 842 | * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is |
| 843 | * why you can't call these functions underneath configfs callbacks. |
| 844 | * |
| 845 | * Note, btw, that this can be called at *any* time, even when a configfs |
| 846 | * subsystem isn't registered, or when configfs is loading or unloading. |
| 847 | * Just like configfs_register_subsystem(). So we take the same |
| 848 | * precautions. We pin the filesystem. We lock each i_mutex _in_order_ |
| 849 | * on our way down the tree. If we can find the target item in the |
| 850 | * configfs tree, it must be part of the subsystem tree as well, so we |
| 851 | * do not need the subsystem semaphore. Holding the i_mutex chain locks |
| 852 | * out mkdir() and rmdir(), who might be racing us. |
| 853 | */ |
| 854 | |
| 855 | /* |
| 856 | * configfs_depend_prep() |
| 857 | * |
| 858 | * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are |
| 859 | * attributes. This is similar but not the same to configfs_detach_prep(). |
| 860 | * Note that configfs_detach_prep() expects the parent to be locked when it |
| 861 | * is called, but we lock the parent *inside* configfs_depend_prep(). We |
| 862 | * do that so we can unlock it if we find nothing. |
| 863 | * |
| 864 | * Here we do a depth-first search of the dentry hierarchy looking for |
| 865 | * our object. We take i_mutex on each step of the way down. IT IS |
| 866 | * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch, |
| 867 | * we'll drop the i_mutex. |
| 868 | * |
| 869 | * If the target is not found, -ENOENT is bubbled up and we have released |
| 870 | * all locks. If the target was found, the locks will be cleared by |
| 871 | * configfs_depend_rollback(). |
| 872 | * |
| 873 | * This adds a requirement that all config_items be unique! |
| 874 | * |
| 875 | * This is recursive because the locking traversal is tricky. There isn't |
| 876 | * much on the stack, though, so folks that need this function - be careful |
| 877 | * about your stack! Patches will be accepted to make it iterative. |
| 878 | */ |
| 879 | static int configfs_depend_prep(struct dentry *origin, |
| 880 | struct config_item *target) |
| 881 | { |
| 882 | struct configfs_dirent *child_sd, *sd = origin->d_fsdata; |
| 883 | int ret = 0; |
| 884 | |
| 885 | BUG_ON(!origin || !sd); |
| 886 | |
| 887 | /* Lock this guy on the way down */ |
| 888 | mutex_lock(&sd->s_dentry->d_inode->i_mutex); |
| 889 | if (sd->s_element == target) /* Boo-yah */ |
| 890 | goto out; |
| 891 | |
| 892 | list_for_each_entry(child_sd, &sd->s_children, s_sibling) { |
| 893 | if (child_sd->s_type & CONFIGFS_DIR) { |
| 894 | ret = configfs_depend_prep(child_sd->s_dentry, |
| 895 | target); |
| 896 | if (!ret) |
| 897 | goto out; /* Child path boo-yah */ |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /* We looped all our children and didn't find target */ |
| 902 | mutex_unlock(&sd->s_dentry->d_inode->i_mutex); |
| 903 | ret = -ENOENT; |
| 904 | |
| 905 | out: |
| 906 | return ret; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * This is ONLY called if configfs_depend_prep() did its job. So we can |
| 911 | * trust the entire path from item back up to origin. |
| 912 | * |
| 913 | * We walk backwards from item, unlocking each i_mutex. We finish by |
| 914 | * unlocking origin. |
| 915 | */ |
| 916 | static void configfs_depend_rollback(struct dentry *origin, |
| 917 | struct config_item *item) |
| 918 | { |
| 919 | struct dentry *dentry = item->ci_dentry; |
| 920 | |
| 921 | while (dentry != origin) { |
| 922 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 923 | dentry = dentry->d_parent; |
| 924 | } |
| 925 | |
| 926 | mutex_unlock(&origin->d_inode->i_mutex); |
| 927 | } |
| 928 | |
| 929 | int configfs_depend_item(struct configfs_subsystem *subsys, |
| 930 | struct config_item *target) |
| 931 | { |
| 932 | int ret; |
| 933 | struct configfs_dirent *p, *root_sd, *subsys_sd = NULL; |
| 934 | struct config_item *s_item = &subsys->su_group.cg_item; |
| 935 | |
| 936 | /* |
| 937 | * Pin the configfs filesystem. This means we can safely access |
| 938 | * the root of the configfs filesystem. |
| 939 | */ |
| 940 | ret = configfs_pin_fs(); |
| 941 | if (ret) |
| 942 | return ret; |
| 943 | |
| 944 | /* |
| 945 | * Next, lock the root directory. We're going to check that the |
| 946 | * subsystem is really registered, and so we need to lock out |
| 947 | * configfs_[un]register_subsystem(). |
| 948 | */ |
| 949 | mutex_lock(&configfs_sb->s_root->d_inode->i_mutex); |
| 950 | |
| 951 | root_sd = configfs_sb->s_root->d_fsdata; |
| 952 | |
| 953 | list_for_each_entry(p, &root_sd->s_children, s_sibling) { |
| 954 | if (p->s_type & CONFIGFS_DIR) { |
| 955 | if (p->s_element == s_item) { |
| 956 | subsys_sd = p; |
| 957 | break; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | if (!subsys_sd) { |
| 963 | ret = -ENOENT; |
| 964 | goto out_unlock_fs; |
| 965 | } |
| 966 | |
| 967 | /* Ok, now we can trust subsys/s_item */ |
| 968 | |
| 969 | /* Scan the tree, locking i_mutex recursively, return 0 if found */ |
| 970 | ret = configfs_depend_prep(subsys_sd->s_dentry, target); |
| 971 | if (ret) |
| 972 | goto out_unlock_fs; |
| 973 | |
| 974 | /* We hold all i_mutexes from the subsystem down to the target */ |
| 975 | p = target->ci_dentry->d_fsdata; |
| 976 | p->s_dependent_count += 1; |
| 977 | |
| 978 | configfs_depend_rollback(subsys_sd->s_dentry, target); |
| 979 | |
| 980 | out_unlock_fs: |
| 981 | mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); |
| 982 | |
| 983 | /* |
| 984 | * If we succeeded, the fs is pinned via other methods. If not, |
| 985 | * we're done with it anyway. So release_fs() is always right. |
| 986 | */ |
| 987 | configfs_release_fs(); |
| 988 | |
| 989 | return ret; |
| 990 | } |
| 991 | EXPORT_SYMBOL(configfs_depend_item); |
| 992 | |
| 993 | /* |
| 994 | * Release the dependent linkage. This is much simpler than |
| 995 | * configfs_depend_item() because we know that that the client driver is |
| 996 | * pinned, thus the subsystem is pinned, and therefore configfs is pinned. |
| 997 | */ |
| 998 | void configfs_undepend_item(struct configfs_subsystem *subsys, |
| 999 | struct config_item *target) |
| 1000 | { |
| 1001 | struct configfs_dirent *sd; |
| 1002 | |
| 1003 | /* |
| 1004 | * Since we can trust everything is pinned, we just need i_mutex |
| 1005 | * on the item. |
| 1006 | */ |
| 1007 | mutex_lock(&target->ci_dentry->d_inode->i_mutex); |
| 1008 | |
| 1009 | sd = target->ci_dentry->d_fsdata; |
| 1010 | BUG_ON(sd->s_dependent_count < 1); |
| 1011 | |
| 1012 | sd->s_dependent_count -= 1; |
| 1013 | |
| 1014 | /* |
| 1015 | * After this unlock, we cannot trust the item to stay alive! |
| 1016 | * DO NOT REFERENCE item after this unlock. |
| 1017 | */ |
| 1018 | mutex_unlock(&target->ci_dentry->d_inode->i_mutex); |
| 1019 | } |
| 1020 | EXPORT_SYMBOL(configfs_undepend_item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1021 | |
| 1022 | static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1023 | { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1024 | int ret, module_got = 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1025 | struct config_group *group; |
| 1026 | struct config_item *item; |
| 1027 | struct config_item *parent_item; |
| 1028 | struct configfs_subsystem *subsys; |
| 1029 | struct configfs_dirent *sd; |
| 1030 | struct config_item_type *type; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1031 | struct module *owner = NULL; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1032 | char *name; |
| 1033 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1034 | if (dentry->d_parent == configfs_sb->s_root) { |
| 1035 | ret = -EPERM; |
| 1036 | goto out; |
| 1037 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1038 | |
| 1039 | sd = dentry->d_parent->d_fsdata; |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1040 | if (!(sd->s_type & CONFIGFS_USET_DIR)) { |
| 1041 | ret = -EPERM; |
| 1042 | goto out; |
| 1043 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1044 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1045 | /* Get a working ref for the duration of this function */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1046 | parent_item = configfs_get_config_item(dentry->d_parent); |
| 1047 | type = parent_item->ci_type; |
| 1048 | subsys = to_config_group(parent_item)->cg_subsys; |
| 1049 | BUG_ON(!subsys); |
| 1050 | |
| 1051 | if (!type || !type->ct_group_ops || |
| 1052 | (!type->ct_group_ops->make_group && |
| 1053 | !type->ct_group_ops->make_item)) { |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1054 | ret = -EPERM; /* Lack-of-mkdir returns -EPERM */ |
| 1055 | goto out_put; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL); |
| 1059 | if (!name) { |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1060 | ret = -ENOMEM; |
| 1061 | goto out_put; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1062 | } |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1063 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1064 | snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name); |
| 1065 | |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1066 | mutex_lock(&subsys->su_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1067 | group = NULL; |
| 1068 | item = NULL; |
| 1069 | if (type->ct_group_ops->make_group) { |
| 1070 | group = type->ct_group_ops->make_group(to_config_group(parent_item), name); |
| 1071 | if (group) { |
| 1072 | link_group(to_config_group(parent_item), group); |
| 1073 | item = &group->cg_item; |
| 1074 | } |
| 1075 | } else { |
| 1076 | item = type->ct_group_ops->make_item(to_config_group(parent_item), name); |
| 1077 | if (item) |
| 1078 | link_obj(parent_item, item); |
| 1079 | } |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1080 | mutex_unlock(&subsys->su_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1081 | |
| 1082 | kfree(name); |
| 1083 | if (!item) { |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1084 | /* |
| 1085 | * If item == NULL, then link_obj() was never called. |
| 1086 | * There are no extra references to clean up. |
| 1087 | */ |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1088 | ret = -ENOMEM; |
| 1089 | goto out_put; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1092 | /* |
| 1093 | * link_obj() has been called (via link_group() for groups). |
| 1094 | * From here on out, errors must clean that up. |
| 1095 | */ |
| 1096 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1097 | type = item->ci_type; |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1098 | if (!type) { |
| 1099 | ret = -EINVAL; |
| 1100 | goto out_unlink; |
| 1101 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1102 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1103 | owner = type->ct_owner; |
| 1104 | if (!try_module_get(owner)) { |
| 1105 | ret = -EINVAL; |
| 1106 | goto out_unlink; |
| 1107 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1108 | |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1109 | /* |
| 1110 | * I hate doing it this way, but if there is |
| 1111 | * an error, module_put() probably should |
| 1112 | * happen after any cleanup. |
| 1113 | */ |
| 1114 | module_got = 1; |
| 1115 | |
| 1116 | if (group) |
| 1117 | ret = configfs_attach_group(parent_item, item, dentry); |
| 1118 | else |
| 1119 | ret = configfs_attach_item(parent_item, item, dentry); |
| 1120 | |
| 1121 | out_unlink: |
| 1122 | if (ret) { |
| 1123 | /* Tear down everything we built up */ |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1124 | mutex_lock(&subsys->su_mutex); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 1125 | |
| 1126 | client_disconnect_notify(parent_item, item); |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1127 | if (group) |
| 1128 | unlink_group(group); |
| 1129 | else |
| 1130 | unlink_obj(item); |
| 1131 | client_drop_item(parent_item, item); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 1132 | |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1133 | mutex_unlock(&subsys->su_mutex); |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1134 | |
| 1135 | if (module_got) |
| 1136 | module_put(owner); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1139 | out_put: |
| 1140 | /* |
Joel Becker | eed7a0d | 2006-04-11 21:37:20 -0700 | [diff] [blame] | 1141 | * link_obj()/link_group() took a reference from child->parent, |
| 1142 | * so the parent is safely pinned. We can drop our working |
| 1143 | * reference. |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1144 | */ |
| 1145 | config_item_put(parent_item); |
| 1146 | |
| 1147 | out: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | static int configfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 1152 | { |
| 1153 | struct config_item *parent_item; |
| 1154 | struct config_item *item; |
| 1155 | struct configfs_subsystem *subsys; |
| 1156 | struct configfs_dirent *sd; |
| 1157 | struct module *owner = NULL; |
| 1158 | int ret; |
| 1159 | |
| 1160 | if (dentry->d_parent == configfs_sb->s_root) |
| 1161 | return -EPERM; |
| 1162 | |
| 1163 | sd = dentry->d_fsdata; |
| 1164 | if (sd->s_type & CONFIGFS_USET_DEFAULT) |
| 1165 | return -EPERM; |
| 1166 | |
Joel Becker | 631d1fe | 2007-06-18 18:06:09 -0700 | [diff] [blame] | 1167 | /* |
| 1168 | * Here's where we check for dependents. We're protected by |
| 1169 | * i_mutex. |
| 1170 | */ |
| 1171 | if (sd->s_dependent_count) |
| 1172 | return -EBUSY; |
| 1173 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1174 | /* Get a working ref until we have the child */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1175 | parent_item = configfs_get_config_item(dentry->d_parent); |
| 1176 | subsys = to_config_group(parent_item)->cg_subsys; |
| 1177 | BUG_ON(!subsys); |
| 1178 | |
| 1179 | if (!parent_item->ci_type) { |
| 1180 | config_item_put(parent_item); |
| 1181 | return -EINVAL; |
| 1182 | } |
| 1183 | |
| 1184 | ret = configfs_detach_prep(dentry); |
| 1185 | if (ret) { |
| 1186 | configfs_detach_rollback(dentry); |
| 1187 | config_item_put(parent_item); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
Joel Becker | 84efad1 | 2006-03-27 18:46:09 -0800 | [diff] [blame] | 1191 | /* Get a working ref for the duration of this function */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1192 | item = configfs_get_config_item(dentry); |
| 1193 | |
| 1194 | /* Drop reference from above, item already holds one. */ |
| 1195 | config_item_put(parent_item); |
| 1196 | |
| 1197 | if (item->ci_type) |
| 1198 | owner = item->ci_type->ct_owner; |
| 1199 | |
| 1200 | if (sd->s_type & CONFIGFS_USET_DIR) { |
| 1201 | configfs_detach_group(item); |
| 1202 | |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1203 | mutex_lock(&subsys->su_mutex); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 1204 | client_disconnect_notify(parent_item, item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1205 | unlink_group(to_config_group(item)); |
| 1206 | } else { |
| 1207 | configfs_detach_item(item); |
| 1208 | |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1209 | mutex_lock(&subsys->su_mutex); |
Joel Becker | 299894c | 2006-10-06 17:33:23 -0700 | [diff] [blame] | 1210 | client_disconnect_notify(parent_item, item); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1211 | unlink_obj(item); |
| 1212 | } |
| 1213 | |
| 1214 | client_drop_item(parent_item, item); |
Joel Becker | e6bd07a | 2007-07-06 23:33:17 -0700 | [diff] [blame] | 1215 | mutex_unlock(&subsys->su_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1216 | |
| 1217 | /* Drop our reference from above */ |
| 1218 | config_item_put(item); |
| 1219 | |
| 1220 | module_put(owner); |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1225 | const struct inode_operations configfs_dir_inode_operations = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1226 | .mkdir = configfs_mkdir, |
| 1227 | .rmdir = configfs_rmdir, |
| 1228 | .symlink = configfs_symlink, |
| 1229 | .unlink = configfs_unlink, |
| 1230 | .lookup = configfs_lookup, |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 1231 | .setattr = configfs_setattr, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1232 | }; |
| 1233 | |
| 1234 | #if 0 |
| 1235 | int configfs_rename_dir(struct config_item * item, const char *new_name) |
| 1236 | { |
| 1237 | int error = 0; |
| 1238 | struct dentry * new_dentry, * parent; |
| 1239 | |
| 1240 | if (!strcmp(config_item_name(item), new_name)) |
| 1241 | return -EINVAL; |
| 1242 | |
| 1243 | if (!item->parent) |
| 1244 | return -EINVAL; |
| 1245 | |
| 1246 | down_write(&configfs_rename_sem); |
| 1247 | parent = item->parent->dentry; |
| 1248 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1249 | mutex_lock(&parent->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1250 | |
| 1251 | new_dentry = lookup_one_len(new_name, parent, strlen(new_name)); |
| 1252 | if (!IS_ERR(new_dentry)) { |
Joel Becker | e7515d0 | 2006-03-10 11:42:30 -0800 | [diff] [blame] | 1253 | if (!new_dentry->d_inode) { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1254 | error = config_item_set_name(item, "%s", new_name); |
| 1255 | if (!error) { |
| 1256 | d_add(new_dentry, NULL); |
| 1257 | d_move(item->dentry, new_dentry); |
| 1258 | } |
| 1259 | else |
| 1260 | d_delete(new_dentry); |
| 1261 | } else |
| 1262 | error = -EEXIST; |
| 1263 | dput(new_dentry); |
| 1264 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1265 | mutex_unlock(&parent->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1266 | up_write(&configfs_rename_sem); |
| 1267 | |
| 1268 | return error; |
| 1269 | } |
| 1270 | #endif |
| 1271 | |
| 1272 | static int configfs_dir_open(struct inode *inode, struct file *file) |
| 1273 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1274 | struct dentry * dentry = file->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1275 | struct configfs_dirent * parent_sd = dentry->d_fsdata; |
| 1276 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1277 | mutex_lock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1278 | file->private_data = configfs_new_dirent(parent_sd, NULL); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1279 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1280 | |
Louis Rilling | 107ed40 | 2008-06-16 19:01:00 +0200 | [diff] [blame^] | 1281 | return IS_ERR(file->private_data) ? PTR_ERR(file->private_data) : 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1282 | |
| 1283 | } |
| 1284 | |
| 1285 | static int configfs_dir_close(struct inode *inode, struct file *file) |
| 1286 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1287 | struct dentry * dentry = file->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1288 | struct configfs_dirent * cursor = file->private_data; |
| 1289 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1290 | mutex_lock(&dentry->d_inode->i_mutex); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1291 | spin_lock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1292 | list_del_init(&cursor->s_sibling); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1293 | spin_unlock(&configfs_dirent_lock); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1294 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1295 | |
| 1296 | release_configfs_dirent(cursor); |
| 1297 | |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* Relationship between s_mode and the DT_xxx types */ |
| 1302 | static inline unsigned char dt_type(struct configfs_dirent *sd) |
| 1303 | { |
| 1304 | return (sd->s_mode >> 12) & 15; |
| 1305 | } |
| 1306 | |
| 1307 | static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 1308 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1309 | struct dentry *dentry = filp->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1310 | struct configfs_dirent * parent_sd = dentry->d_fsdata; |
| 1311 | struct configfs_dirent *cursor = filp->private_data; |
| 1312 | struct list_head *p, *q = &cursor->s_sibling; |
| 1313 | ino_t ino; |
| 1314 | int i = filp->f_pos; |
| 1315 | |
| 1316 | switch (i) { |
| 1317 | case 0: |
| 1318 | ino = dentry->d_inode->i_ino; |
| 1319 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 1320 | break; |
| 1321 | filp->f_pos++; |
| 1322 | i++; |
| 1323 | /* fallthrough */ |
| 1324 | case 1: |
| 1325 | ino = parent_ino(dentry); |
| 1326 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 1327 | break; |
| 1328 | filp->f_pos++; |
| 1329 | i++; |
| 1330 | /* fallthrough */ |
| 1331 | default: |
| 1332 | if (filp->f_pos == 2) { |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1333 | spin_lock(&configfs_dirent_lock); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 1334 | list_move(q, &parent_sd->s_children); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1335 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1336 | } |
| 1337 | for (p=q->next; p!= &parent_sd->s_children; p=p->next) { |
| 1338 | struct configfs_dirent *next; |
| 1339 | const char * name; |
| 1340 | int len; |
| 1341 | |
| 1342 | next = list_entry(p, struct configfs_dirent, |
| 1343 | s_sibling); |
| 1344 | if (!next->s_element) |
| 1345 | continue; |
| 1346 | |
| 1347 | name = configfs_get_name(next); |
| 1348 | len = strlen(name); |
| 1349 | if (next->s_dentry) |
| 1350 | ino = next->s_dentry->d_inode->i_ino; |
| 1351 | else |
| 1352 | ino = iunique(configfs_sb, 2); |
| 1353 | |
| 1354 | if (filldir(dirent, name, len, filp->f_pos, ino, |
| 1355 | dt_type(next)) < 0) |
| 1356 | return 0; |
| 1357 | |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1358 | spin_lock(&configfs_dirent_lock); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 1359 | list_move(q, p); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1360 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1361 | p = q; |
| 1362 | filp->f_pos++; |
| 1363 | } |
| 1364 | } |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin) |
| 1369 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1370 | struct dentry * dentry = file->f_path.dentry; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1371 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1372 | mutex_lock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1373 | switch (origin) { |
| 1374 | case 1: |
| 1375 | offset += file->f_pos; |
| 1376 | case 0: |
| 1377 | if (offset >= 0) |
| 1378 | break; |
| 1379 | default: |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 1380 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1381 | return -EINVAL; |
| 1382 | } |
| 1383 | if (offset != file->f_pos) { |
| 1384 | file->f_pos = offset; |
| 1385 | if (file->f_pos >= 2) { |
| 1386 | struct configfs_dirent *sd = dentry->d_fsdata; |
| 1387 | struct configfs_dirent *cursor = file->private_data; |
| 1388 | struct list_head *p; |
| 1389 | loff_t n = file->f_pos - 2; |
| 1390 | |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1391 | spin_lock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1392 | list_del(&cursor->s_sibling); |
| 1393 | p = sd->s_children.next; |
| 1394 | while (n && p != &sd->s_children) { |
| 1395 | struct configfs_dirent *next; |
| 1396 | next = list_entry(p, struct configfs_dirent, |
| 1397 | s_sibling); |
| 1398 | if (next->s_element) |
| 1399 | n--; |
| 1400 | p = p->next; |
| 1401 | } |
| 1402 | list_add_tail(&cursor->s_sibling, p); |
Louis Rilling | 6f61076 | 2008-06-16 19:00:58 +0200 | [diff] [blame] | 1403 | spin_unlock(&configfs_dirent_lock); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1404 | } |
| 1405 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1406 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1407 | return offset; |
| 1408 | } |
| 1409 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1410 | const struct file_operations configfs_dir_operations = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1411 | .open = configfs_dir_open, |
| 1412 | .release = configfs_dir_close, |
| 1413 | .llseek = configfs_dir_lseek, |
| 1414 | .read = generic_read_dir, |
| 1415 | .readdir = configfs_readdir, |
| 1416 | }; |
| 1417 | |
| 1418 | int configfs_register_subsystem(struct configfs_subsystem *subsys) |
| 1419 | { |
| 1420 | int err; |
| 1421 | struct config_group *group = &subsys->su_group; |
| 1422 | struct qstr name; |
| 1423 | struct dentry *dentry; |
| 1424 | struct configfs_dirent *sd; |
| 1425 | |
| 1426 | err = configfs_pin_fs(); |
| 1427 | if (err) |
| 1428 | return err; |
| 1429 | |
| 1430 | if (!group->cg_item.ci_name) |
| 1431 | group->cg_item.ci_name = group->cg_item.ci_namebuf; |
| 1432 | |
| 1433 | sd = configfs_sb->s_root->d_fsdata; |
| 1434 | link_group(to_config_group(sd->s_element), group); |
| 1435 | |
Joonwoo Park | ba611ed | 2007-12-26 12:09:57 +0900 | [diff] [blame] | 1436 | mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex, |
| 1437 | I_MUTEX_PARENT); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1438 | |
| 1439 | name.name = group->cg_item.ci_name; |
| 1440 | name.len = strlen(name.name); |
| 1441 | name.hash = full_name_hash(name.name, name.len); |
| 1442 | |
| 1443 | err = -ENOMEM; |
| 1444 | dentry = d_alloc(configfs_sb->s_root, &name); |
Joel Becker | afdf04e | 2007-03-05 15:49:49 -0800 | [diff] [blame] | 1445 | if (dentry) { |
| 1446 | d_add(dentry, NULL); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1447 | |
Joel Becker | afdf04e | 2007-03-05 15:49:49 -0800 | [diff] [blame] | 1448 | err = configfs_attach_group(sd->s_element, &group->cg_item, |
| 1449 | dentry); |
| 1450 | if (err) { |
| 1451 | d_delete(dentry); |
| 1452 | dput(dentry); |
| 1453 | } |
| 1454 | } |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1455 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1456 | mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1457 | |
Joel Becker | afdf04e | 2007-03-05 15:49:49 -0800 | [diff] [blame] | 1458 | if (err) { |
| 1459 | unlink_group(group); |
| 1460 | configfs_release_fs(); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | return err; |
| 1464 | } |
| 1465 | |
| 1466 | void configfs_unregister_subsystem(struct configfs_subsystem *subsys) |
| 1467 | { |
| 1468 | struct config_group *group = &subsys->su_group; |
| 1469 | struct dentry *dentry = group->cg_item.ci_dentry; |
| 1470 | |
| 1471 | if (dentry->d_parent != configfs_sb->s_root) { |
| 1472 | printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n"); |
| 1473 | return; |
| 1474 | } |
| 1475 | |
Mark Fasheh | 55ed160 | 2006-10-20 14:55:54 -0700 | [diff] [blame] | 1476 | mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex, |
| 1477 | I_MUTEX_PARENT); |
| 1478 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1479 | if (configfs_detach_prep(dentry)) { |
| 1480 | printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n"); |
| 1481 | } |
| 1482 | configfs_detach_group(&group->cg_item); |
| 1483 | dentry->d_inode->i_flags |= S_DEAD; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1484 | mutex_unlock(&dentry->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1485 | |
| 1486 | d_delete(dentry); |
| 1487 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1488 | mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1489 | |
| 1490 | dput(dentry); |
| 1491 | |
| 1492 | unlink_group(group); |
| 1493 | configfs_release_fs(); |
| 1494 | } |
| 1495 | |
| 1496 | EXPORT_SYMBOL(configfs_register_subsystem); |
| 1497 | EXPORT_SYMBOL(configfs_unregister_subsystem); |