dir.c 43 KB

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