dir.c 51 KB

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