dir.c 38 KB

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