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