Commit b4c98f625fffee3a6f633082e9e4be3e952ca2ab

Authored by Joel Becker
Committed by Mark Fasheh
1 parent e478bec0ba

configfs: Prevent duplicate subsystem names.

For all child objects, creation comes through mkdir(2), so duplicate names
are prevented.

Subsystems, though, are registered by client drivers at init_module()/__init
time.  This patch prevents duplicate subsystem names.

Signed-off-by: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Mark Fasheh <mark.fasheh@oracle.com>

Showing 1 changed file with 30 additions and 2 deletions Inline Diff

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