dir.c 43 KB

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