dir.c 37 KB

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