dir.c 36 KB

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