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