dir.c 44 KB

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