dir.c 44 KB

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