dir.c 35 KB

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