dir.c 44 KB

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