dir.c 50 KB

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