dir.c 35 KB

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