dir.c 41 KB

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