dir.c 38 KB

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