dir.c 49 KB

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