dir.c 36 KB

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