dir.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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_attribute *attr;
  495. struct configfs_bin_attribute *bin_attr;
  496. int error = 0;
  497. int i;
  498. if (!t)
  499. return -EINVAL;
  500. if (t->ct_attrs) {
  501. for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
  502. if ((error = configfs_create_file(item, attr)))
  503. break;
  504. }
  505. }
  506. if (t->ct_bin_attrs) {
  507. for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
  508. error = configfs_create_bin_file(item, bin_attr);
  509. if (error)
  510. break;
  511. }
  512. }
  513. if (error)
  514. detach_attrs(item);
  515. return error;
  516. }
  517. static int configfs_attach_group(struct config_item *parent_item,
  518. struct config_item *item,
  519. struct dentry *dentry,
  520. struct configfs_fragment *frag);
  521. static void configfs_detach_group(struct config_item *item);
  522. static void detach_groups(struct config_group *group)
  523. {
  524. struct dentry * dentry = dget(group->cg_item.ci_dentry);
  525. struct dentry *child;
  526. struct configfs_dirent *parent_sd;
  527. struct configfs_dirent *sd, *tmp;
  528. if (!dentry)
  529. return;
  530. parent_sd = dentry->d_fsdata;
  531. list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  532. if (!sd->s_element ||
  533. !(sd->s_type & CONFIGFS_USET_DEFAULT))
  534. continue;
  535. child = sd->s_dentry;
  536. inode_lock(d_inode(child));
  537. configfs_detach_group(sd->s_element);
  538. d_inode(child)->i_flags |= S_DEAD;
  539. dont_mount(child);
  540. inode_unlock(d_inode(child));
  541. d_delete(child);
  542. dput(child);
  543. }
  544. /**
  545. * Drop reference from dget() on entrance.
  546. */
  547. dput(dentry);
  548. }
  549. /*
  550. * This fakes mkdir(2) on a default_groups[] entry. It
  551. * creates a dentry, attachs it, and then does fixup
  552. * on the sd->s_type.
  553. *
  554. * We could, perhaps, tweak our parent's ->mkdir for a minute and
  555. * try using vfs_mkdir. Just a thought.
  556. */
  557. static int create_default_group(struct config_group *parent_group,
  558. struct config_group *group,
  559. struct configfs_fragment *frag)
  560. {
  561. int ret;
  562. struct configfs_dirent *sd;
  563. /* We trust the caller holds a reference to parent */
  564. struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
  565. if (!group->cg_item.ci_name)
  566. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  567. ret = -ENOMEM;
  568. child = d_alloc_name(parent, group->cg_item.ci_name);
  569. if (child) {
  570. d_add(child, NULL);
  571. ret = configfs_attach_group(&parent_group->cg_item,
  572. &group->cg_item, child, frag);
  573. if (!ret) {
  574. sd = child->d_fsdata;
  575. sd->s_type |= CONFIGFS_USET_DEFAULT;
  576. } else {
  577. BUG_ON(d_inode(child));
  578. d_drop(child);
  579. dput(child);
  580. }
  581. }
  582. return ret;
  583. }
  584. static int populate_groups(struct config_group *group,
  585. struct configfs_fragment *frag)
  586. {
  587. struct config_group *new_group;
  588. int ret = 0;
  589. list_for_each_entry(new_group, &group->default_groups, group_entry) {
  590. ret = create_default_group(group, new_group, frag);
  591. if (ret) {
  592. detach_groups(group);
  593. break;
  594. }
  595. }
  596. return ret;
  597. }
  598. void configfs_remove_default_groups(struct config_group *group)
  599. {
  600. struct config_group *g, *n;
  601. list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
  602. list_del(&g->group_entry);
  603. config_item_put(&g->cg_item);
  604. }
  605. }
  606. EXPORT_SYMBOL(configfs_remove_default_groups);
  607. /*
  608. * All of link_obj/unlink_obj/link_group/unlink_group require that
  609. * subsys->su_mutex is held.
  610. */
  611. static void unlink_obj(struct config_item *item)
  612. {
  613. struct config_group *group;
  614. group = item->ci_group;
  615. if (group) {
  616. list_del_init(&item->ci_entry);
  617. item->ci_group = NULL;
  618. item->ci_parent = NULL;
  619. /* Drop the reference for ci_entry */
  620. config_item_put(item);
  621. /* Drop the reference for ci_parent */
  622. config_group_put(group);
  623. }
  624. }
  625. static void link_obj(struct config_item *parent_item, struct config_item *item)
  626. {
  627. /*
  628. * Parent seems redundant with group, but it makes certain
  629. * traversals much nicer.
  630. */
  631. item->ci_parent = parent_item;
  632. /*
  633. * We hold a reference on the parent for the child's ci_parent
  634. * link.
  635. */
  636. item->ci_group = config_group_get(to_config_group(parent_item));
  637. list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
  638. /*
  639. * We hold a reference on the child for ci_entry on the parent's
  640. * cg_children
  641. */
  642. config_item_get(item);
  643. }
  644. static void unlink_group(struct config_group *group)
  645. {
  646. struct config_group *new_group;
  647. list_for_each_entry(new_group, &group->default_groups, group_entry)
  648. unlink_group(new_group);
  649. group->cg_subsys = NULL;
  650. unlink_obj(&group->cg_item);
  651. }
  652. static void link_group(struct config_group *parent_group, struct config_group *group)
  653. {
  654. struct config_group *new_group;
  655. struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
  656. link_obj(&parent_group->cg_item, &group->cg_item);
  657. if (parent_group->cg_subsys)
  658. subsys = parent_group->cg_subsys;
  659. else if (configfs_is_root(&parent_group->cg_item))
  660. subsys = to_configfs_subsystem(group);
  661. else
  662. BUG();
  663. group->cg_subsys = subsys;
  664. list_for_each_entry(new_group, &group->default_groups, group_entry)
  665. link_group(group, new_group);
  666. }
  667. /*
  668. * The goal is that configfs_attach_item() (and
  669. * configfs_attach_group()) can be called from either the VFS or this
  670. * module. That is, they assume that the items have been created,
  671. * the dentry allocated, and the dcache is all ready to go.
  672. *
  673. * If they fail, they must clean up after themselves as if they
  674. * had never been called. The caller (VFS or local function) will
  675. * handle cleaning up the dcache bits.
  676. *
  677. * configfs_detach_group() and configfs_detach_item() behave similarly on
  678. * the way out. They assume that the proper semaphores are held, they
  679. * clean up the configfs items, and they expect their callers will
  680. * handle the dcache bits.
  681. */
  682. static int configfs_attach_item(struct config_item *parent_item,
  683. struct config_item *item,
  684. struct dentry *dentry,
  685. struct configfs_fragment *frag)
  686. {
  687. int ret;
  688. ret = configfs_create_dir(item, dentry, frag);
  689. if (!ret) {
  690. ret = populate_attrs(item);
  691. if (ret) {
  692. /*
  693. * We are going to remove an inode and its dentry but
  694. * the VFS may already have hit and used them. Thus,
  695. * we must lock them as rmdir() would.
  696. */
  697. inode_lock(d_inode(dentry));
  698. configfs_remove_dir(item);
  699. d_inode(dentry)->i_flags |= S_DEAD;
  700. dont_mount(dentry);
  701. inode_unlock(d_inode(dentry));
  702. d_delete(dentry);
  703. }
  704. }
  705. return ret;
  706. }
  707. /* Caller holds the mutex of the item's inode */
  708. static void configfs_detach_item(struct config_item *item)
  709. {
  710. detach_attrs(item);
  711. configfs_remove_dir(item);
  712. }
  713. static int configfs_attach_group(struct config_item *parent_item,
  714. struct config_item *item,
  715. struct dentry *dentry,
  716. struct configfs_fragment *frag)
  717. {
  718. int ret;
  719. struct configfs_dirent *sd;
  720. ret = configfs_attach_item(parent_item, item, dentry, frag);
  721. if (!ret) {
  722. sd = dentry->d_fsdata;
  723. sd->s_type |= CONFIGFS_USET_DIR;
  724. /*
  725. * FYI, we're faking mkdir in populate_groups()
  726. * We must lock the group's inode to avoid races with the VFS
  727. * which can already hit the inode and try to add/remove entries
  728. * under it.
  729. *
  730. * We must also lock the inode to remove it safely in case of
  731. * error, as rmdir() would.
  732. */
  733. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  734. configfs_adjust_dir_dirent_depth_before_populate(sd);
  735. ret = populate_groups(to_config_group(item), frag);
  736. if (ret) {
  737. configfs_detach_item(item);
  738. d_inode(dentry)->i_flags |= S_DEAD;
  739. dont_mount(dentry);
  740. }
  741. configfs_adjust_dir_dirent_depth_after_populate(sd);
  742. inode_unlock(d_inode(dentry));
  743. if (ret)
  744. d_delete(dentry);
  745. }
  746. return ret;
  747. }
  748. /* Caller holds the mutex of the group's inode */
  749. static void configfs_detach_group(struct config_item *item)
  750. {
  751. detach_groups(to_config_group(item));
  752. configfs_detach_item(item);
  753. }
  754. /*
  755. * After the item has been detached from the filesystem view, we are
  756. * ready to tear it out of the hierarchy. Notify the client before
  757. * we do that so they can perform any cleanup that requires
  758. * navigating the hierarchy. A client does not need to provide this
  759. * callback. The subsystem semaphore MUST be held by the caller, and
  760. * references must be valid for both items. It also assumes the
  761. * caller has validated ci_type.
  762. */
  763. static void client_disconnect_notify(struct config_item *parent_item,
  764. struct config_item *item)
  765. {
  766. const struct config_item_type *type;
  767. type = parent_item->ci_type;
  768. BUG_ON(!type);
  769. if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
  770. type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
  771. item);
  772. }
  773. /*
  774. * Drop the initial reference from make_item()/make_group()
  775. * This function assumes that reference is held on item
  776. * and that item holds a valid reference to the parent. Also, it
  777. * assumes the caller has validated ci_type.
  778. */
  779. static void client_drop_item(struct config_item *parent_item,
  780. struct config_item *item)
  781. {
  782. const struct config_item_type *type;
  783. type = parent_item->ci_type;
  784. BUG_ON(!type);
  785. /*
  786. * If ->drop_item() exists, it is responsible for the
  787. * config_item_put().
  788. */
  789. if (type->ct_group_ops && type->ct_group_ops->drop_item)
  790. type->ct_group_ops->drop_item(to_config_group(parent_item),
  791. item);
  792. else
  793. config_item_put(item);
  794. }
  795. #ifdef DEBUG
  796. static void configfs_dump_one(struct configfs_dirent *sd, int level)
  797. {
  798. pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
  799. #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
  800. type_print(CONFIGFS_ROOT);
  801. type_print(CONFIGFS_DIR);
  802. type_print(CONFIGFS_ITEM_ATTR);
  803. type_print(CONFIGFS_ITEM_LINK);
  804. type_print(CONFIGFS_USET_DIR);
  805. type_print(CONFIGFS_USET_DEFAULT);
  806. type_print(CONFIGFS_USET_DROPPING);
  807. #undef type_print
  808. }
  809. static int configfs_dump(struct configfs_dirent *sd, int level)
  810. {
  811. struct configfs_dirent *child_sd;
  812. int ret = 0;
  813. configfs_dump_one(sd, level);
  814. if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
  815. return 0;
  816. list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
  817. ret = configfs_dump(child_sd, level + 2);
  818. if (ret)
  819. break;
  820. }
  821. return ret;
  822. }
  823. #endif
  824. /*
  825. * configfs_depend_item() and configfs_undepend_item()
  826. *
  827. * WARNING: Do not call these from a configfs callback!
  828. *
  829. * This describes these functions and their helpers.
  830. *
  831. * Allow another kernel system to depend on a config_item. If this
  832. * happens, the item cannot go away until the dependent can live without
  833. * it. The idea is to give client modules as simple an interface as
  834. * possible. When a system asks them to depend on an item, they just
  835. * call configfs_depend_item(). If the item is live and the client
  836. * driver is in good shape, we'll happily do the work for them.
  837. *
  838. * Why is the locking complex? Because configfs uses the VFS to handle
  839. * all locking, but this function is called outside the normal
  840. * VFS->configfs path. So it must take VFS locks to prevent the
  841. * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
  842. * why you can't call these functions underneath configfs callbacks.
  843. *
  844. * Note, btw, that this can be called at *any* time, even when a configfs
  845. * subsystem isn't registered, or when configfs is loading or unloading.
  846. * Just like configfs_register_subsystem(). So we take the same
  847. * precautions. We pin the filesystem. We lock configfs_dirent_lock.
  848. * If we can find the target item in the
  849. * configfs tree, it must be part of the subsystem tree as well, so we
  850. * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
  851. * locking out mkdir() and rmdir(), who might be racing us.
  852. */
  853. /*
  854. * configfs_depend_prep()
  855. *
  856. * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
  857. * attributes. This is similar but not the same to configfs_detach_prep().
  858. * Note that configfs_detach_prep() expects the parent to be locked when it
  859. * is called, but we lock the parent *inside* configfs_depend_prep(). We
  860. * do that so we can unlock it if we find nothing.
  861. *
  862. * Here we do a depth-first search of the dentry hierarchy looking for
  863. * our object.
  864. * We deliberately ignore items tagged as dropping since they are virtually
  865. * dead, as well as items in the middle of attachment since they virtually
  866. * do not exist yet. This completes the locking out of racing mkdir() and
  867. * rmdir().
  868. * Note: subdirectories in the middle of attachment start with s_type =
  869. * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
  870. * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
  871. * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
  872. *
  873. * If the target is not found, -ENOENT is bubbled up.
  874. *
  875. * This adds a requirement that all config_items be unique!
  876. *
  877. * This is recursive. There isn't
  878. * much on the stack, though, so folks that need this function - be careful
  879. * about your stack! Patches will be accepted to make it iterative.
  880. */
  881. static int configfs_depend_prep(struct dentry *origin,
  882. struct config_item *target)
  883. {
  884. struct configfs_dirent *child_sd, *sd;
  885. int ret = 0;
  886. BUG_ON(!origin || !origin->d_fsdata);
  887. sd = origin->d_fsdata;
  888. if (sd->s_element == target) /* Boo-yah */
  889. goto out;
  890. list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
  891. if ((child_sd->s_type & CONFIGFS_DIR) &&
  892. !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
  893. !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
  894. ret = configfs_depend_prep(child_sd->s_dentry,
  895. target);
  896. if (!ret)
  897. goto out; /* Child path boo-yah */
  898. }
  899. }
  900. /* We looped all our children and didn't find target */
  901. ret = -ENOENT;
  902. out:
  903. return ret;
  904. }
  905. static int configfs_do_depend_item(struct dentry *subsys_dentry,
  906. struct config_item *target)
  907. {
  908. struct configfs_dirent *p;
  909. int ret;
  910. spin_lock(&configfs_dirent_lock);
  911. /* Scan the tree, return 0 if found */
  912. ret = configfs_depend_prep(subsys_dentry, target);
  913. if (ret)
  914. goto out_unlock_dirent_lock;
  915. /*
  916. * We are sure that the item is not about to be removed by rmdir(), and
  917. * not in the middle of attachment by mkdir().
  918. */
  919. p = target->ci_dentry->d_fsdata;
  920. p->s_dependent_count += 1;
  921. out_unlock_dirent_lock:
  922. spin_unlock(&configfs_dirent_lock);
  923. return ret;
  924. }
  925. static inline struct configfs_dirent *
  926. configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
  927. struct config_item *subsys_item)
  928. {
  929. struct configfs_dirent *p;
  930. struct configfs_dirent *ret = NULL;
  931. list_for_each_entry(p, &root_sd->s_children, s_sibling) {
  932. if (p->s_type & CONFIGFS_DIR &&
  933. p->s_element == subsys_item) {
  934. ret = p;
  935. break;
  936. }
  937. }
  938. return ret;
  939. }
  940. int configfs_depend_item(struct configfs_subsystem *subsys,
  941. struct config_item *target)
  942. {
  943. int ret;
  944. struct configfs_dirent *subsys_sd;
  945. struct config_item *s_item = &subsys->su_group.cg_item;
  946. struct dentry *root;
  947. /*
  948. * Pin the configfs filesystem. This means we can safely access
  949. * the root of the configfs filesystem.
  950. */
  951. root = configfs_pin_fs();
  952. if (IS_ERR(root))
  953. return PTR_ERR(root);
  954. /*
  955. * Next, lock the root directory. We're going to check that the
  956. * subsystem is really registered, and so we need to lock out
  957. * configfs_[un]register_subsystem().
  958. */
  959. inode_lock(d_inode(root));
  960. subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
  961. if (!subsys_sd) {
  962. ret = -ENOENT;
  963. goto out_unlock_fs;
  964. }
  965. /* Ok, now we can trust subsys/s_item */
  966. ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
  967. out_unlock_fs:
  968. inode_unlock(d_inode(root));
  969. /*
  970. * If we succeeded, the fs is pinned via other methods. If not,
  971. * we're done with it anyway. So release_fs() is always right.
  972. */
  973. configfs_release_fs();
  974. return ret;
  975. }
  976. EXPORT_SYMBOL(configfs_depend_item);
  977. /*
  978. * Release the dependent linkage. This is much simpler than
  979. * configfs_depend_item() because we know that the client driver is
  980. * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
  981. */
  982. void configfs_undepend_item(struct config_item *target)
  983. {
  984. struct configfs_dirent *sd;
  985. /*
  986. * Since we can trust everything is pinned, we just need
  987. * configfs_dirent_lock.
  988. */
  989. spin_lock(&configfs_dirent_lock);
  990. sd = target->ci_dentry->d_fsdata;
  991. BUG_ON(sd->s_dependent_count < 1);
  992. sd->s_dependent_count -= 1;
  993. /*
  994. * After this unlock, we cannot trust the item to stay alive!
  995. * DO NOT REFERENCE item after this unlock.
  996. */
  997. spin_unlock(&configfs_dirent_lock);
  998. }
  999. EXPORT_SYMBOL(configfs_undepend_item);
  1000. /*
  1001. * caller_subsys is a caller's subsystem not target's. This is used to
  1002. * determine if we should lock root and check subsys or not. When we are
  1003. * in the same subsystem as our target there is no need to do locking as
  1004. * we know that subsys is valid and is not unregistered during this function
  1005. * as we are called from callback of one of his children and VFS holds a lock
  1006. * on some inode. Otherwise we have to lock our root to ensure that target's
  1007. * subsystem it is not unregistered during this function.
  1008. */
  1009. int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
  1010. struct config_item *target)
  1011. {
  1012. struct configfs_subsystem *target_subsys;
  1013. struct config_group *root, *parent;
  1014. struct configfs_dirent *subsys_sd;
  1015. int ret = -ENOENT;
  1016. /* Disallow this function for configfs root */
  1017. if (configfs_is_root(target))
  1018. return -EINVAL;
  1019. parent = target->ci_group;
  1020. /*
  1021. * This may happen when someone is trying to depend root
  1022. * directory of some subsystem
  1023. */
  1024. if (configfs_is_root(&parent->cg_item)) {
  1025. target_subsys = to_configfs_subsystem(to_config_group(target));
  1026. root = parent;
  1027. } else {
  1028. target_subsys = parent->cg_subsys;
  1029. /* Find a cofnigfs root as we may need it for locking */
  1030. for (root = parent; !configfs_is_root(&root->cg_item);
  1031. root = root->cg_item.ci_group)
  1032. ;
  1033. }
  1034. if (target_subsys != caller_subsys) {
  1035. /*
  1036. * We are in other configfs subsystem, so we have to do
  1037. * additional locking to prevent other subsystem from being
  1038. * unregistered
  1039. */
  1040. inode_lock(d_inode(root->cg_item.ci_dentry));
  1041. /*
  1042. * As we are trying to depend item from other subsystem
  1043. * we have to check if this subsystem is still registered
  1044. */
  1045. subsys_sd = configfs_find_subsys_dentry(
  1046. root->cg_item.ci_dentry->d_fsdata,
  1047. &target_subsys->su_group.cg_item);
  1048. if (!subsys_sd)
  1049. goto out_root_unlock;
  1050. } else {
  1051. subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
  1052. }
  1053. /* Now we can execute core of depend item */
  1054. ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
  1055. if (target_subsys != caller_subsys)
  1056. out_root_unlock:
  1057. /*
  1058. * We were called from subsystem other than our target so we
  1059. * took some locks so now it's time to release them
  1060. */
  1061. inode_unlock(d_inode(root->cg_item.ci_dentry));
  1062. return ret;
  1063. }
  1064. EXPORT_SYMBOL(configfs_depend_item_unlocked);
  1065. static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
  1066. struct dentry *dentry, umode_t mode)
  1067. {
  1068. int ret = 0;
  1069. int module_got = 0;
  1070. struct config_group *group = NULL;
  1071. struct config_item *item = NULL;
  1072. struct config_item *parent_item;
  1073. struct configfs_subsystem *subsys;
  1074. struct configfs_dirent *sd;
  1075. const struct config_item_type *type;
  1076. struct module *subsys_owner = NULL, *new_item_owner = NULL;
  1077. struct configfs_fragment *frag;
  1078. char *name;
  1079. sd = dentry->d_parent->d_fsdata;
  1080. /*
  1081. * Fake invisibility if dir belongs to a group/default groups hierarchy
  1082. * being attached
  1083. */
  1084. if (!configfs_dirent_is_ready(sd)) {
  1085. ret = -ENOENT;
  1086. goto out;
  1087. }
  1088. if (!(sd->s_type & CONFIGFS_USET_DIR)) {
  1089. ret = -EPERM;
  1090. goto out;
  1091. }
  1092. frag = new_fragment();
  1093. if (!frag) {
  1094. ret = -ENOMEM;
  1095. goto out;
  1096. }
  1097. /* Get a working ref for the duration of this function */
  1098. parent_item = configfs_get_config_item(dentry->d_parent);
  1099. type = parent_item->ci_type;
  1100. subsys = to_config_group(parent_item)->cg_subsys;
  1101. BUG_ON(!subsys);
  1102. if (!type || !type->ct_group_ops ||
  1103. (!type->ct_group_ops->make_group &&
  1104. !type->ct_group_ops->make_item)) {
  1105. ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
  1106. goto out_put;
  1107. }
  1108. /*
  1109. * The subsystem may belong to a different module than the item
  1110. * being created. We don't want to safely pin the new item but
  1111. * fail to pin the subsystem it sits under.
  1112. */
  1113. if (!subsys->su_group.cg_item.ci_type) {
  1114. ret = -EINVAL;
  1115. goto out_put;
  1116. }
  1117. subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
  1118. if (!try_module_get(subsys_owner)) {
  1119. ret = -EINVAL;
  1120. goto out_put;
  1121. }
  1122. name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
  1123. if (!name) {
  1124. ret = -ENOMEM;
  1125. goto out_subsys_put;
  1126. }
  1127. snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
  1128. mutex_lock(&subsys->su_mutex);
  1129. if (type->ct_group_ops->make_group) {
  1130. group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
  1131. if (!group)
  1132. group = ERR_PTR(-ENOMEM);
  1133. if (!IS_ERR(group)) {
  1134. link_group(to_config_group(parent_item), group);
  1135. item = &group->cg_item;
  1136. } else
  1137. ret = PTR_ERR(group);
  1138. } else {
  1139. item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
  1140. if (!item)
  1141. item = ERR_PTR(-ENOMEM);
  1142. if (!IS_ERR(item))
  1143. link_obj(parent_item, item);
  1144. else
  1145. ret = PTR_ERR(item);
  1146. }
  1147. mutex_unlock(&subsys->su_mutex);
  1148. kfree(name);
  1149. if (ret) {
  1150. /*
  1151. * If ret != 0, then link_obj() was never called.
  1152. * There are no extra references to clean up.
  1153. */
  1154. goto out_subsys_put;
  1155. }
  1156. /*
  1157. * link_obj() has been called (via link_group() for groups).
  1158. * From here on out, errors must clean that up.
  1159. */
  1160. type = item->ci_type;
  1161. if (!type) {
  1162. ret = -EINVAL;
  1163. goto out_unlink;
  1164. }
  1165. new_item_owner = type->ct_owner;
  1166. if (!try_module_get(new_item_owner)) {
  1167. ret = -EINVAL;
  1168. goto out_unlink;
  1169. }
  1170. /*
  1171. * I hate doing it this way, but if there is
  1172. * an error, module_put() probably should
  1173. * happen after any cleanup.
  1174. */
  1175. module_got = 1;
  1176. /*
  1177. * Make racing rmdir() fail if it did not tag parent with
  1178. * CONFIGFS_USET_DROPPING
  1179. * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
  1180. * fail and let rmdir() terminate correctly
  1181. */
  1182. spin_lock(&configfs_dirent_lock);
  1183. /* This will make configfs_detach_prep() fail */
  1184. sd->s_type |= CONFIGFS_USET_IN_MKDIR;
  1185. spin_unlock(&configfs_dirent_lock);
  1186. if (group)
  1187. ret = configfs_attach_group(parent_item, item, dentry, frag);
  1188. else
  1189. ret = configfs_attach_item(parent_item, item, dentry, frag);
  1190. spin_lock(&configfs_dirent_lock);
  1191. sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
  1192. if (!ret)
  1193. configfs_dir_set_ready(dentry->d_fsdata);
  1194. spin_unlock(&configfs_dirent_lock);
  1195. out_unlink:
  1196. if (ret) {
  1197. /* Tear down everything we built up */
  1198. mutex_lock(&subsys->su_mutex);
  1199. client_disconnect_notify(parent_item, item);
  1200. if (group)
  1201. unlink_group(group);
  1202. else
  1203. unlink_obj(item);
  1204. client_drop_item(parent_item, item);
  1205. mutex_unlock(&subsys->su_mutex);
  1206. if (module_got)
  1207. module_put(new_item_owner);
  1208. }
  1209. out_subsys_put:
  1210. if (ret)
  1211. module_put(subsys_owner);
  1212. out_put:
  1213. /*
  1214. * link_obj()/link_group() took a reference from child->parent,
  1215. * so the parent is safely pinned. We can drop our working
  1216. * reference.
  1217. */
  1218. config_item_put(parent_item);
  1219. put_fragment(frag);
  1220. out:
  1221. return ret;
  1222. }
  1223. static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
  1224. {
  1225. struct config_item *parent_item;
  1226. struct config_item *item;
  1227. struct configfs_subsystem *subsys;
  1228. struct configfs_dirent *sd;
  1229. struct configfs_fragment *frag;
  1230. struct module *subsys_owner = NULL, *dead_item_owner = NULL;
  1231. int ret;
  1232. sd = dentry->d_fsdata;
  1233. if (sd->s_type & CONFIGFS_USET_DEFAULT)
  1234. return -EPERM;
  1235. /* Get a working ref until we have the child */
  1236. parent_item = configfs_get_config_item(dentry->d_parent);
  1237. subsys = to_config_group(parent_item)->cg_subsys;
  1238. BUG_ON(!subsys);
  1239. if (!parent_item->ci_type) {
  1240. config_item_put(parent_item);
  1241. return -EINVAL;
  1242. }
  1243. /* configfs_mkdir() shouldn't have allowed this */
  1244. BUG_ON(!subsys->su_group.cg_item.ci_type);
  1245. subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
  1246. /*
  1247. * Ensure that no racing symlink() will make detach_prep() fail while
  1248. * the new link is temporarily attached
  1249. */
  1250. do {
  1251. struct dentry *wait;
  1252. mutex_lock(&configfs_symlink_mutex);
  1253. spin_lock(&configfs_dirent_lock);
  1254. /*
  1255. * Here's where we check for dependents. We're protected by
  1256. * configfs_dirent_lock.
  1257. * If no dependent, atomically tag the item as dropping.
  1258. */
  1259. ret = sd->s_dependent_count ? -EBUSY : 0;
  1260. if (!ret) {
  1261. ret = configfs_detach_prep(dentry, &wait);
  1262. if (ret)
  1263. configfs_detach_rollback(dentry);
  1264. }
  1265. spin_unlock(&configfs_dirent_lock);
  1266. mutex_unlock(&configfs_symlink_mutex);
  1267. if (ret) {
  1268. if (ret != -EAGAIN) {
  1269. config_item_put(parent_item);
  1270. return ret;
  1271. }
  1272. /* Wait until the racing operation terminates */
  1273. inode_lock(d_inode(wait));
  1274. inode_unlock(d_inode(wait));
  1275. dput(wait);
  1276. }
  1277. } while (ret == -EAGAIN);
  1278. frag = sd->s_frag;
  1279. if (down_write_killable(&frag->frag_sem)) {
  1280. spin_lock(&configfs_dirent_lock);
  1281. configfs_detach_rollback(dentry);
  1282. spin_unlock(&configfs_dirent_lock);
  1283. config_item_put(parent_item);
  1284. return -EINTR;
  1285. }
  1286. frag->frag_dead = true;
  1287. up_write(&frag->frag_sem);
  1288. /* Get a working ref for the duration of this function */
  1289. item = configfs_get_config_item(dentry);
  1290. /* Drop reference from above, item already holds one. */
  1291. config_item_put(parent_item);
  1292. if (item->ci_type)
  1293. dead_item_owner = item->ci_type->ct_owner;
  1294. if (sd->s_type & CONFIGFS_USET_DIR) {
  1295. configfs_detach_group(item);
  1296. mutex_lock(&subsys->su_mutex);
  1297. client_disconnect_notify(parent_item, item);
  1298. unlink_group(to_config_group(item));
  1299. } else {
  1300. configfs_detach_item(item);
  1301. mutex_lock(&subsys->su_mutex);
  1302. client_disconnect_notify(parent_item, item);
  1303. unlink_obj(item);
  1304. }
  1305. client_drop_item(parent_item, item);
  1306. mutex_unlock(&subsys->su_mutex);
  1307. /* Drop our reference from above */
  1308. config_item_put(item);
  1309. module_put(dead_item_owner);
  1310. module_put(subsys_owner);
  1311. return 0;
  1312. }
  1313. const struct inode_operations configfs_dir_inode_operations = {
  1314. .mkdir = configfs_mkdir,
  1315. .rmdir = configfs_rmdir,
  1316. .symlink = configfs_symlink,
  1317. .unlink = configfs_unlink,
  1318. .lookup = configfs_lookup,
  1319. .setattr = configfs_setattr,
  1320. };
  1321. const struct inode_operations configfs_root_inode_operations = {
  1322. .lookup = configfs_lookup,
  1323. .setattr = configfs_setattr,
  1324. };
  1325. static int configfs_dir_open(struct inode *inode, struct file *file)
  1326. {
  1327. struct dentry * dentry = file->f_path.dentry;
  1328. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  1329. int err;
  1330. inode_lock(d_inode(dentry));
  1331. /*
  1332. * Fake invisibility if dir belongs to a group/default groups hierarchy
  1333. * being attached
  1334. */
  1335. err = -ENOENT;
  1336. if (configfs_dirent_is_ready(parent_sd)) {
  1337. file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
  1338. if (IS_ERR(file->private_data))
  1339. err = PTR_ERR(file->private_data);
  1340. else
  1341. err = 0;
  1342. }
  1343. inode_unlock(d_inode(dentry));
  1344. return err;
  1345. }
  1346. static int configfs_dir_close(struct inode *inode, struct file *file)
  1347. {
  1348. struct dentry * dentry = file->f_path.dentry;
  1349. struct configfs_dirent * cursor = file->private_data;
  1350. inode_lock(d_inode(dentry));
  1351. spin_lock(&configfs_dirent_lock);
  1352. list_del_init(&cursor->s_sibling);
  1353. spin_unlock(&configfs_dirent_lock);
  1354. inode_unlock(d_inode(dentry));
  1355. release_configfs_dirent(cursor);
  1356. return 0;
  1357. }
  1358. /* Relationship between s_mode and the DT_xxx types */
  1359. static inline unsigned char dt_type(struct configfs_dirent *sd)
  1360. {
  1361. return (sd->s_mode >> 12) & 15;
  1362. }
  1363. static int configfs_readdir(struct file *file, struct dir_context *ctx)
  1364. {
  1365. struct dentry *dentry = file->f_path.dentry;
  1366. struct super_block *sb = dentry->d_sb;
  1367. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  1368. struct configfs_dirent *cursor = file->private_data;
  1369. struct list_head *p, *q = &cursor->s_sibling;
  1370. ino_t ino = 0;
  1371. if (!dir_emit_dots(file, ctx))
  1372. return 0;
  1373. spin_lock(&configfs_dirent_lock);
  1374. if (ctx->pos == 2)
  1375. list_move(q, &parent_sd->s_children);
  1376. for (p = q->next; p != &parent_sd->s_children; p = p->next) {
  1377. struct configfs_dirent *next;
  1378. const char *name;
  1379. int len;
  1380. struct inode *inode = NULL;
  1381. next = list_entry(p, struct configfs_dirent, s_sibling);
  1382. if (!next->s_element)
  1383. continue;
  1384. /*
  1385. * We'll have a dentry and an inode for
  1386. * PINNED items and for open attribute
  1387. * files. We lock here to prevent a race
  1388. * with configfs_d_iput() clearing
  1389. * s_dentry before calling iput().
  1390. *
  1391. * Why do we go to the trouble? If
  1392. * someone has an attribute file open,
  1393. * the inode number should match until
  1394. * they close it. Beyond that, we don't
  1395. * care.
  1396. */
  1397. dentry = next->s_dentry;
  1398. if (dentry)
  1399. inode = d_inode(dentry);
  1400. if (inode)
  1401. ino = inode->i_ino;
  1402. spin_unlock(&configfs_dirent_lock);
  1403. if (!inode)
  1404. ino = iunique(sb, 2);
  1405. name = configfs_get_name(next);
  1406. len = strlen(name);
  1407. if (!dir_emit(ctx, name, len, ino, dt_type(next)))
  1408. return 0;
  1409. spin_lock(&configfs_dirent_lock);
  1410. list_move(q, p);
  1411. p = q;
  1412. ctx->pos++;
  1413. }
  1414. spin_unlock(&configfs_dirent_lock);
  1415. return 0;
  1416. }
  1417. static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
  1418. {
  1419. struct dentry * dentry = file->f_path.dentry;
  1420. switch (whence) {
  1421. case 1:
  1422. offset += file->f_pos;
  1423. fallthrough;
  1424. case 0:
  1425. if (offset >= 0)
  1426. break;
  1427. fallthrough;
  1428. default:
  1429. return -EINVAL;
  1430. }
  1431. if (offset != file->f_pos) {
  1432. file->f_pos = offset;
  1433. if (file->f_pos >= 2) {
  1434. struct configfs_dirent *sd = dentry->d_fsdata;
  1435. struct configfs_dirent *cursor = file->private_data;
  1436. struct list_head *p;
  1437. loff_t n = file->f_pos - 2;
  1438. spin_lock(&configfs_dirent_lock);
  1439. list_del(&cursor->s_sibling);
  1440. p = sd->s_children.next;
  1441. while (n && p != &sd->s_children) {
  1442. struct configfs_dirent *next;
  1443. next = list_entry(p, struct configfs_dirent,
  1444. s_sibling);
  1445. if (next->s_element)
  1446. n--;
  1447. p = p->next;
  1448. }
  1449. list_add_tail(&cursor->s_sibling, p);
  1450. spin_unlock(&configfs_dirent_lock);
  1451. }
  1452. }
  1453. return offset;
  1454. }
  1455. const struct file_operations configfs_dir_operations = {
  1456. .open = configfs_dir_open,
  1457. .release = configfs_dir_close,
  1458. .llseek = configfs_dir_lseek,
  1459. .read = generic_read_dir,
  1460. .iterate_shared = configfs_readdir,
  1461. };
  1462. /**
  1463. * configfs_register_group - creates a parent-child relation between two groups
  1464. * @parent_group: parent group
  1465. * @group: child group
  1466. *
  1467. * link groups, creates dentry for the child and attaches it to the
  1468. * parent dentry.
  1469. *
  1470. * Return: 0 on success, negative errno code on error
  1471. */
  1472. int configfs_register_group(struct config_group *parent_group,
  1473. struct config_group *group)
  1474. {
  1475. struct configfs_subsystem *subsys = parent_group->cg_subsys;
  1476. struct dentry *parent;
  1477. struct configfs_fragment *frag;
  1478. int ret;
  1479. frag = new_fragment();
  1480. if (!frag)
  1481. return -ENOMEM;
  1482. mutex_lock(&subsys->su_mutex);
  1483. link_group(parent_group, group);
  1484. mutex_unlock(&subsys->su_mutex);
  1485. parent = parent_group->cg_item.ci_dentry;
  1486. inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
  1487. ret = create_default_group(parent_group, group, frag);
  1488. if (ret)
  1489. goto err_out;
  1490. spin_lock(&configfs_dirent_lock);
  1491. configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
  1492. spin_unlock(&configfs_dirent_lock);
  1493. inode_unlock(d_inode(parent));
  1494. put_fragment(frag);
  1495. return 0;
  1496. err_out:
  1497. inode_unlock(d_inode(parent));
  1498. mutex_lock(&subsys->su_mutex);
  1499. unlink_group(group);
  1500. mutex_unlock(&subsys->su_mutex);
  1501. put_fragment(frag);
  1502. return ret;
  1503. }
  1504. EXPORT_SYMBOL(configfs_register_group);
  1505. /**
  1506. * configfs_unregister_group() - unregisters a child group from its parent
  1507. * @group: parent group to be unregistered
  1508. *
  1509. * Undoes configfs_register_group()
  1510. */
  1511. void configfs_unregister_group(struct config_group *group)
  1512. {
  1513. struct configfs_subsystem *subsys = group->cg_subsys;
  1514. struct dentry *dentry = group->cg_item.ci_dentry;
  1515. struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
  1516. struct configfs_dirent *sd = dentry->d_fsdata;
  1517. struct configfs_fragment *frag = sd->s_frag;
  1518. down_write(&frag->frag_sem);
  1519. frag->frag_dead = true;
  1520. up_write(&frag->frag_sem);
  1521. inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
  1522. spin_lock(&configfs_dirent_lock);
  1523. configfs_detach_prep(dentry, NULL);
  1524. spin_unlock(&configfs_dirent_lock);
  1525. configfs_detach_group(&group->cg_item);
  1526. d_inode(dentry)->i_flags |= S_DEAD;
  1527. dont_mount(dentry);
  1528. d_drop(dentry);
  1529. fsnotify_rmdir(d_inode(parent), dentry);
  1530. inode_unlock(d_inode(parent));
  1531. dput(dentry);
  1532. mutex_lock(&subsys->su_mutex);
  1533. unlink_group(group);
  1534. mutex_unlock(&subsys->su_mutex);
  1535. }
  1536. EXPORT_SYMBOL(configfs_unregister_group);
  1537. /**
  1538. * configfs_register_default_group() - allocates and registers a child group
  1539. * @parent_group: parent group
  1540. * @name: child group name
  1541. * @item_type: child item type description
  1542. *
  1543. * boilerplate to allocate and register a child group with its parent. We need
  1544. * kzalloc'ed memory because child's default_group is initially empty.
  1545. *
  1546. * Return: allocated config group or ERR_PTR() on error
  1547. */
  1548. struct config_group *
  1549. configfs_register_default_group(struct config_group *parent_group,
  1550. const char *name,
  1551. const struct config_item_type *item_type)
  1552. {
  1553. int ret;
  1554. struct config_group *group;
  1555. group = kzalloc(sizeof(*group), GFP_KERNEL);
  1556. if (!group)
  1557. return ERR_PTR(-ENOMEM);
  1558. config_group_init_type_name(group, name, item_type);
  1559. ret = configfs_register_group(parent_group, group);
  1560. if (ret) {
  1561. kfree(group);
  1562. return ERR_PTR(ret);
  1563. }
  1564. return group;
  1565. }
  1566. EXPORT_SYMBOL(configfs_register_default_group);
  1567. /**
  1568. * configfs_unregister_default_group() - unregisters and frees a child group
  1569. * @group: the group to act on
  1570. */
  1571. void configfs_unregister_default_group(struct config_group *group)
  1572. {
  1573. configfs_unregister_group(group);
  1574. kfree(group);
  1575. }
  1576. EXPORT_SYMBOL(configfs_unregister_default_group);
  1577. int configfs_register_subsystem(struct configfs_subsystem *subsys)
  1578. {
  1579. int err;
  1580. struct config_group *group = &subsys->su_group;
  1581. struct dentry *dentry;
  1582. struct dentry *root;
  1583. struct configfs_dirent *sd;
  1584. struct configfs_fragment *frag;
  1585. frag = new_fragment();
  1586. if (!frag)
  1587. return -ENOMEM;
  1588. root = configfs_pin_fs();
  1589. if (IS_ERR(root)) {
  1590. put_fragment(frag);
  1591. return PTR_ERR(root);
  1592. }
  1593. if (!group->cg_item.ci_name)
  1594. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  1595. sd = root->d_fsdata;
  1596. mutex_lock(&configfs_subsystem_mutex);
  1597. link_group(to_config_group(sd->s_element), group);
  1598. mutex_unlock(&configfs_subsystem_mutex);
  1599. inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
  1600. err = -ENOMEM;
  1601. dentry = d_alloc_name(root, group->cg_item.ci_name);
  1602. if (dentry) {
  1603. d_add(dentry, NULL);
  1604. err = configfs_attach_group(sd->s_element, &group->cg_item,
  1605. dentry, frag);
  1606. if (err) {
  1607. BUG_ON(d_inode(dentry));
  1608. d_drop(dentry);
  1609. dput(dentry);
  1610. } else {
  1611. spin_lock(&configfs_dirent_lock);
  1612. configfs_dir_set_ready(dentry->d_fsdata);
  1613. spin_unlock(&configfs_dirent_lock);
  1614. }
  1615. }
  1616. inode_unlock(d_inode(root));
  1617. if (err) {
  1618. mutex_lock(&configfs_subsystem_mutex);
  1619. unlink_group(group);
  1620. mutex_unlock(&configfs_subsystem_mutex);
  1621. configfs_release_fs();
  1622. }
  1623. put_fragment(frag);
  1624. return err;
  1625. }
  1626. void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
  1627. {
  1628. struct config_group *group = &subsys->su_group;
  1629. struct dentry *dentry = group->cg_item.ci_dentry;
  1630. struct dentry *root = dentry->d_sb->s_root;
  1631. struct configfs_dirent *sd = dentry->d_fsdata;
  1632. struct configfs_fragment *frag = sd->s_frag;
  1633. if (dentry->d_parent != root) {
  1634. pr_err("Tried to unregister non-subsystem!\n");
  1635. return;
  1636. }
  1637. down_write(&frag->frag_sem);
  1638. frag->frag_dead = true;
  1639. up_write(&frag->frag_sem);
  1640. inode_lock_nested(d_inode(root),
  1641. I_MUTEX_PARENT);
  1642. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  1643. mutex_lock(&configfs_symlink_mutex);
  1644. spin_lock(&configfs_dirent_lock);
  1645. if (configfs_detach_prep(dentry, NULL)) {
  1646. pr_err("Tried to unregister non-empty subsystem!\n");
  1647. }
  1648. spin_unlock(&configfs_dirent_lock);
  1649. mutex_unlock(&configfs_symlink_mutex);
  1650. configfs_detach_group(&group->cg_item);
  1651. d_inode(dentry)->i_flags |= S_DEAD;
  1652. dont_mount(dentry);
  1653. inode_unlock(d_inode(dentry));
  1654. d_drop(dentry);
  1655. fsnotify_rmdir(d_inode(root), dentry);
  1656. inode_unlock(d_inode(root));
  1657. dput(dentry);
  1658. mutex_lock(&configfs_subsystem_mutex);
  1659. unlink_group(group);
  1660. mutex_unlock(&configfs_subsystem_mutex);
  1661. configfs_release_fs();
  1662. }
  1663. EXPORT_SYMBOL(configfs_register_subsystem);
  1664. EXPORT_SYMBOL(configfs_unregister_subsystem);