dir.c 40 KB

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