dir.c 44 KB

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