dir.c 45 KB

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