dir.c 44 KB

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