dir.c 41 KB

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