dir.c 46 KB

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