dir.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. static void configfs_d_iput(struct dentry * dentry,
  35. struct inode * inode)
  36. {
  37. struct configfs_dirent * sd = dentry->d_fsdata;
  38. if (sd) {
  39. BUG_ON(sd->s_dentry != dentry);
  40. sd->s_dentry = NULL;
  41. configfs_put(sd);
  42. }
  43. iput(inode);
  44. }
  45. /*
  46. * We _must_ delete our dentries on last dput, as the chain-to-parent
  47. * behavior is required to clear the parents of default_groups.
  48. */
  49. static int configfs_d_delete(struct dentry *dentry)
  50. {
  51. return 1;
  52. }
  53. static struct dentry_operations configfs_dentry_ops = {
  54. .d_iput = configfs_d_iput,
  55. /* simple_delete_dentry() isn't exported */
  56. .d_delete = configfs_d_delete,
  57. };
  58. /*
  59. * Allocates a new configfs_dirent and links it to the parent configfs_dirent
  60. */
  61. static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
  62. void * element)
  63. {
  64. struct configfs_dirent * sd;
  65. sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
  66. if (!sd)
  67. return NULL;
  68. atomic_set(&sd->s_count, 1);
  69. INIT_LIST_HEAD(&sd->s_links);
  70. INIT_LIST_HEAD(&sd->s_children);
  71. list_add(&sd->s_sibling, &parent_sd->s_children);
  72. sd->s_element = element;
  73. return sd;
  74. }
  75. /*
  76. *
  77. * Return -EEXIST if there is already a configfs element with the same
  78. * name for the same parent.
  79. *
  80. * called with parent inode's i_mutex held
  81. */
  82. static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
  83. const unsigned char *new)
  84. {
  85. struct configfs_dirent * sd;
  86. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  87. if (sd->s_element) {
  88. const unsigned char *existing = configfs_get_name(sd);
  89. if (strcmp(existing, new))
  90. continue;
  91. else
  92. return -EEXIST;
  93. }
  94. }
  95. return 0;
  96. }
  97. int configfs_make_dirent(struct configfs_dirent * parent_sd,
  98. struct dentry * dentry, void * element,
  99. umode_t mode, int type)
  100. {
  101. struct configfs_dirent * sd;
  102. sd = configfs_new_dirent(parent_sd, element);
  103. if (!sd)
  104. return -ENOMEM;
  105. sd->s_mode = mode;
  106. sd->s_type = type;
  107. sd->s_dentry = dentry;
  108. if (dentry) {
  109. dentry->d_fsdata = configfs_get(sd);
  110. dentry->d_op = &configfs_dentry_ops;
  111. }
  112. return 0;
  113. }
  114. static int init_dir(struct inode * inode)
  115. {
  116. inode->i_op = &configfs_dir_inode_operations;
  117. inode->i_fop = &configfs_dir_operations;
  118. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  119. inc_nlink(inode);
  120. return 0;
  121. }
  122. static int init_file(struct inode * inode)
  123. {
  124. inode->i_size = PAGE_SIZE;
  125. inode->i_fop = &configfs_file_operations;
  126. return 0;
  127. }
  128. static int init_symlink(struct inode * inode)
  129. {
  130. inode->i_op = &configfs_symlink_inode_operations;
  131. return 0;
  132. }
  133. static int create_dir(struct config_item * k, struct dentry * p,
  134. struct dentry * d)
  135. {
  136. int error;
  137. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  138. error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
  139. if (!error)
  140. error = configfs_make_dirent(p->d_fsdata, d, k, mode,
  141. CONFIGFS_DIR);
  142. if (!error) {
  143. error = configfs_create(d, mode, init_dir);
  144. if (!error) {
  145. inc_nlink(p->d_inode);
  146. (d)->d_op = &configfs_dentry_ops;
  147. } else {
  148. struct configfs_dirent *sd = d->d_fsdata;
  149. if (sd) {
  150. list_del_init(&sd->s_sibling);
  151. configfs_put(sd);
  152. }
  153. }
  154. }
  155. return error;
  156. }
  157. /**
  158. * configfs_create_dir - create a directory for an config_item.
  159. * @item: config_itemwe're creating directory for.
  160. * @dentry: config_item's dentry.
  161. */
  162. static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
  163. {
  164. struct dentry * parent;
  165. int error = 0;
  166. BUG_ON(!item);
  167. if (item->ci_parent)
  168. parent = item->ci_parent->ci_dentry;
  169. else if (configfs_mount && configfs_mount->mnt_sb)
  170. parent = configfs_mount->mnt_sb->s_root;
  171. else
  172. return -EFAULT;
  173. error = create_dir(item,parent,dentry);
  174. if (!error)
  175. item->ci_dentry = dentry;
  176. return error;
  177. }
  178. int configfs_create_link(struct configfs_symlink *sl,
  179. struct dentry *parent,
  180. struct dentry *dentry)
  181. {
  182. int err = 0;
  183. umode_t mode = S_IFLNK | S_IRWXUGO;
  184. err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
  185. CONFIGFS_ITEM_LINK);
  186. if (!err) {
  187. err = configfs_create(dentry, mode, init_symlink);
  188. if (!err)
  189. dentry->d_op = &configfs_dentry_ops;
  190. else {
  191. struct configfs_dirent *sd = dentry->d_fsdata;
  192. if (sd) {
  193. list_del_init(&sd->s_sibling);
  194. configfs_put(sd);
  195. }
  196. }
  197. }
  198. return err;
  199. }
  200. static void remove_dir(struct dentry * d)
  201. {
  202. struct dentry * parent = dget(d->d_parent);
  203. struct configfs_dirent * sd;
  204. sd = d->d_fsdata;
  205. list_del_init(&sd->s_sibling);
  206. configfs_put(sd);
  207. if (d->d_inode)
  208. simple_rmdir(parent->d_inode,d);
  209. pr_debug(" o %s removing done (%d)\n",d->d_name.name,
  210. atomic_read(&d->d_count));
  211. dput(parent);
  212. }
  213. /**
  214. * configfs_remove_dir - remove an config_item's directory.
  215. * @item: config_item we're removing.
  216. *
  217. * The only thing special about this is that we remove any files in
  218. * the directory before we remove the directory, and we've inlined
  219. * what used to be configfs_rmdir() below, instead of calling separately.
  220. */
  221. static void configfs_remove_dir(struct config_item * item)
  222. {
  223. struct dentry * dentry = dget(item->ci_dentry);
  224. if (!dentry)
  225. return;
  226. remove_dir(dentry);
  227. /**
  228. * Drop reference from dget() on entrance.
  229. */
  230. dput(dentry);
  231. }
  232. /* attaches attribute's configfs_dirent to the dentry corresponding to the
  233. * attribute file
  234. */
  235. static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
  236. {
  237. struct configfs_attribute * attr = sd->s_element;
  238. int error;
  239. dentry->d_fsdata = configfs_get(sd);
  240. sd->s_dentry = dentry;
  241. error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
  242. if (error) {
  243. configfs_put(sd);
  244. return error;
  245. }
  246. dentry->d_op = &configfs_dentry_ops;
  247. d_rehash(dentry);
  248. return 0;
  249. }
  250. static struct dentry * configfs_lookup(struct inode *dir,
  251. struct dentry *dentry,
  252. struct nameidata *nd)
  253. {
  254. struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
  255. struct configfs_dirent * sd;
  256. int found = 0;
  257. int err = 0;
  258. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  259. if (sd->s_type & CONFIGFS_NOT_PINNED) {
  260. const unsigned char * name = configfs_get_name(sd);
  261. if (strcmp(name, dentry->d_name.name))
  262. continue;
  263. found = 1;
  264. err = configfs_attach_attr(sd, dentry);
  265. break;
  266. }
  267. }
  268. if (!found) {
  269. /*
  270. * If it doesn't exist and it isn't a NOT_PINNED item,
  271. * it must be negative.
  272. */
  273. return simple_lookup(dir, dentry, nd);
  274. }
  275. return ERR_PTR(err);
  276. }
  277. /*
  278. * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
  279. * attributes and are removed by rmdir(). We recurse, taking i_mutex
  280. * on all children that are candidates for default detach. If the
  281. * result is clean, then configfs_detach_group() will handle dropping
  282. * i_mutex. If there is an error, the caller will clean up the i_mutex
  283. * holders via configfs_detach_rollback().
  284. */
  285. static int configfs_detach_prep(struct dentry *dentry)
  286. {
  287. struct configfs_dirent *parent_sd = dentry->d_fsdata;
  288. struct configfs_dirent *sd;
  289. int ret;
  290. ret = -EBUSY;
  291. if (!list_empty(&parent_sd->s_links))
  292. goto out;
  293. ret = 0;
  294. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  295. if (sd->s_type & CONFIGFS_NOT_PINNED)
  296. continue;
  297. if (sd->s_type & CONFIGFS_USET_DEFAULT) {
  298. mutex_lock(&sd->s_dentry->d_inode->i_mutex);
  299. /* Mark that we've taken i_mutex */
  300. sd->s_type |= CONFIGFS_USET_DROPPING;
  301. ret = configfs_detach_prep(sd->s_dentry);
  302. if (!ret)
  303. continue;
  304. } else
  305. ret = -ENOTEMPTY;
  306. break;
  307. }
  308. out:
  309. return ret;
  310. }
  311. /*
  312. * Walk the tree, dropping i_mutex wherever CONFIGFS_USET_DROPPING is
  313. * set.
  314. */
  315. static void configfs_detach_rollback(struct dentry *dentry)
  316. {
  317. struct configfs_dirent *parent_sd = dentry->d_fsdata;
  318. struct configfs_dirent *sd;
  319. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  320. if (sd->s_type & CONFIGFS_USET_DEFAULT) {
  321. configfs_detach_rollback(sd->s_dentry);
  322. if (sd->s_type & CONFIGFS_USET_DROPPING) {
  323. sd->s_type &= ~CONFIGFS_USET_DROPPING;
  324. mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
  325. }
  326. }
  327. }
  328. }
  329. static void detach_attrs(struct config_item * item)
  330. {
  331. struct dentry * dentry = dget(item->ci_dentry);
  332. struct configfs_dirent * parent_sd;
  333. struct configfs_dirent * sd, * tmp;
  334. if (!dentry)
  335. return;
  336. pr_debug("configfs %s: dropping attrs for dir\n",
  337. dentry->d_name.name);
  338. parent_sd = dentry->d_fsdata;
  339. list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  340. if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
  341. continue;
  342. list_del_init(&sd->s_sibling);
  343. configfs_drop_dentry(sd, dentry);
  344. configfs_put(sd);
  345. }
  346. /**
  347. * Drop reference from dget() on entrance.
  348. */
  349. dput(dentry);
  350. }
  351. static int populate_attrs(struct config_item *item)
  352. {
  353. struct config_item_type *t = item->ci_type;
  354. struct configfs_attribute *attr;
  355. int error = 0;
  356. int i;
  357. if (!t)
  358. return -EINVAL;
  359. if (t->ct_attrs) {
  360. for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
  361. if ((error = configfs_create_file(item, attr)))
  362. break;
  363. }
  364. }
  365. if (error)
  366. detach_attrs(item);
  367. return error;
  368. }
  369. static int configfs_attach_group(struct config_item *parent_item,
  370. struct config_item *item,
  371. struct dentry *dentry);
  372. static void configfs_detach_group(struct config_item *item);
  373. static void detach_groups(struct config_group *group)
  374. {
  375. struct dentry * dentry = dget(group->cg_item.ci_dentry);
  376. struct dentry *child;
  377. struct configfs_dirent *parent_sd;
  378. struct configfs_dirent *sd, *tmp;
  379. if (!dentry)
  380. return;
  381. parent_sd = dentry->d_fsdata;
  382. list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  383. if (!sd->s_element ||
  384. !(sd->s_type & CONFIGFS_USET_DEFAULT))
  385. continue;
  386. child = sd->s_dentry;
  387. configfs_detach_group(sd->s_element);
  388. child->d_inode->i_flags |= S_DEAD;
  389. /*
  390. * From rmdir/unregister, a configfs_detach_prep() pass
  391. * has taken our i_mutex for us. Drop it.
  392. * From mkdir/register cleanup, there is no sem held.
  393. */
  394. if (sd->s_type & CONFIGFS_USET_DROPPING)
  395. mutex_unlock(&child->d_inode->i_mutex);
  396. d_delete(child);
  397. dput(child);
  398. }
  399. /**
  400. * Drop reference from dget() on entrance.
  401. */
  402. dput(dentry);
  403. }
  404. /*
  405. * This fakes mkdir(2) on a default_groups[] entry. It
  406. * creates a dentry, attachs it, and then does fixup
  407. * on the sd->s_type.
  408. *
  409. * We could, perhaps, tweak our parent's ->mkdir for a minute and
  410. * try using vfs_mkdir. Just a thought.
  411. */
  412. static int create_default_group(struct config_group *parent_group,
  413. struct config_group *group)
  414. {
  415. int ret;
  416. struct qstr name;
  417. struct configfs_dirent *sd;
  418. /* We trust the caller holds a reference to parent */
  419. struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
  420. if (!group->cg_item.ci_name)
  421. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  422. name.name = group->cg_item.ci_name;
  423. name.len = strlen(name.name);
  424. name.hash = full_name_hash(name.name, name.len);
  425. ret = -ENOMEM;
  426. child = d_alloc(parent, &name);
  427. if (child) {
  428. d_add(child, NULL);
  429. ret = configfs_attach_group(&parent_group->cg_item,
  430. &group->cg_item, child);
  431. if (!ret) {
  432. sd = child->d_fsdata;
  433. sd->s_type |= CONFIGFS_USET_DEFAULT;
  434. } else {
  435. d_delete(child);
  436. dput(child);
  437. }
  438. }
  439. return ret;
  440. }
  441. static int populate_groups(struct config_group *group)
  442. {
  443. struct config_group *new_group;
  444. struct dentry *dentry = group->cg_item.ci_dentry;
  445. int ret = 0;
  446. int i;
  447. if (group->default_groups) {
  448. /*
  449. * FYI, we're faking mkdir here
  450. * I'm not sure we need this semaphore, as we're called
  451. * from our parent's mkdir. That holds our parent's
  452. * i_mutex, so afaik lookup cannot continue through our
  453. * parent to find us, let alone mess with our tree.
  454. * That said, taking our i_mutex is closer to mkdir
  455. * emulation, and shouldn't hurt.
  456. */
  457. mutex_lock(&dentry->d_inode->i_mutex);
  458. for (i = 0; group->default_groups[i]; i++) {
  459. new_group = group->default_groups[i];
  460. ret = create_default_group(group, new_group);
  461. if (ret)
  462. break;
  463. }
  464. mutex_unlock(&dentry->d_inode->i_mutex);
  465. }
  466. if (ret)
  467. detach_groups(group);
  468. return ret;
  469. }
  470. /*
  471. * All of link_obj/unlink_obj/link_group/unlink_group require that
  472. * subsys->su_sem is held.
  473. */
  474. static void unlink_obj(struct config_item *item)
  475. {
  476. struct config_group *group;
  477. group = item->ci_group;
  478. if (group) {
  479. list_del_init(&item->ci_entry);
  480. item->ci_group = NULL;
  481. item->ci_parent = NULL;
  482. /* Drop the reference for ci_entry */
  483. config_item_put(item);
  484. /* Drop the reference for ci_parent */
  485. config_group_put(group);
  486. }
  487. }
  488. static void link_obj(struct config_item *parent_item, struct config_item *item)
  489. {
  490. /*
  491. * Parent seems redundant with group, but it makes certain
  492. * traversals much nicer.
  493. */
  494. item->ci_parent = parent_item;
  495. /*
  496. * We hold a reference on the parent for the child's ci_parent
  497. * link.
  498. */
  499. item->ci_group = config_group_get(to_config_group(parent_item));
  500. list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
  501. /*
  502. * We hold a reference on the child for ci_entry on the parent's
  503. * cg_children
  504. */
  505. config_item_get(item);
  506. }
  507. static void unlink_group(struct config_group *group)
  508. {
  509. int i;
  510. struct config_group *new_group;
  511. if (group->default_groups) {
  512. for (i = 0; group->default_groups[i]; i++) {
  513. new_group = group->default_groups[i];
  514. unlink_group(new_group);
  515. }
  516. }
  517. group->cg_subsys = NULL;
  518. unlink_obj(&group->cg_item);
  519. }
  520. static void link_group(struct config_group *parent_group, struct config_group *group)
  521. {
  522. int i;
  523. struct config_group *new_group;
  524. struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
  525. link_obj(&parent_group->cg_item, &group->cg_item);
  526. if (parent_group->cg_subsys)
  527. subsys = parent_group->cg_subsys;
  528. else if (configfs_is_root(&parent_group->cg_item))
  529. subsys = to_configfs_subsystem(group);
  530. else
  531. BUG();
  532. group->cg_subsys = subsys;
  533. if (group->default_groups) {
  534. for (i = 0; group->default_groups[i]; i++) {
  535. new_group = group->default_groups[i];
  536. link_group(group, new_group);
  537. }
  538. }
  539. }
  540. /*
  541. * The goal is that configfs_attach_item() (and
  542. * configfs_attach_group()) can be called from either the VFS or this
  543. * module. That is, they assume that the items have been created,
  544. * the dentry allocated, and the dcache is all ready to go.
  545. *
  546. * If they fail, they must clean up after themselves as if they
  547. * had never been called. The caller (VFS or local function) will
  548. * handle cleaning up the dcache bits.
  549. *
  550. * configfs_detach_group() and configfs_detach_item() behave similarly on
  551. * the way out. They assume that the proper semaphores are held, they
  552. * clean up the configfs items, and they expect their callers will
  553. * handle the dcache bits.
  554. */
  555. static int configfs_attach_item(struct config_item *parent_item,
  556. struct config_item *item,
  557. struct dentry *dentry)
  558. {
  559. int ret;
  560. ret = configfs_create_dir(item, dentry);
  561. if (!ret) {
  562. ret = populate_attrs(item);
  563. if (ret) {
  564. configfs_remove_dir(item);
  565. d_delete(dentry);
  566. }
  567. }
  568. return ret;
  569. }
  570. static void configfs_detach_item(struct config_item *item)
  571. {
  572. detach_attrs(item);
  573. configfs_remove_dir(item);
  574. }
  575. static int configfs_attach_group(struct config_item *parent_item,
  576. struct config_item *item,
  577. struct dentry *dentry)
  578. {
  579. int ret;
  580. struct configfs_dirent *sd;
  581. ret = configfs_attach_item(parent_item, item, dentry);
  582. if (!ret) {
  583. sd = dentry->d_fsdata;
  584. sd->s_type |= CONFIGFS_USET_DIR;
  585. ret = populate_groups(to_config_group(item));
  586. if (ret) {
  587. configfs_detach_item(item);
  588. d_delete(dentry);
  589. }
  590. }
  591. return ret;
  592. }
  593. static void configfs_detach_group(struct config_item *item)
  594. {
  595. detach_groups(to_config_group(item));
  596. configfs_detach_item(item);
  597. }
  598. /*
  599. * Drop the initial reference from make_item()/make_group()
  600. * This function assumes that reference is held on item
  601. * and that item holds a valid reference to the parent. Also, it
  602. * assumes the caller has validated ci_type.
  603. */
  604. static void client_drop_item(struct config_item *parent_item,
  605. struct config_item *item)
  606. {
  607. struct config_item_type *type;
  608. type = parent_item->ci_type;
  609. BUG_ON(!type);
  610. /*
  611. * If ->drop_item() exists, it is responsible for the
  612. * config_item_put().
  613. */
  614. if (type->ct_group_ops && type->ct_group_ops->drop_item)
  615. type->ct_group_ops->drop_item(to_config_group(parent_item),
  616. item);
  617. else
  618. config_item_put(item);
  619. }
  620. static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  621. {
  622. int ret, module_got = 0;
  623. struct config_group *group;
  624. struct config_item *item;
  625. struct config_item *parent_item;
  626. struct configfs_subsystem *subsys;
  627. struct configfs_dirent *sd;
  628. struct config_item_type *type;
  629. struct module *owner = NULL;
  630. char *name;
  631. if (dentry->d_parent == configfs_sb->s_root) {
  632. ret = -EPERM;
  633. goto out;
  634. }
  635. sd = dentry->d_parent->d_fsdata;
  636. if (!(sd->s_type & CONFIGFS_USET_DIR)) {
  637. ret = -EPERM;
  638. goto out;
  639. }
  640. /* Get a working ref for the duration of this function */
  641. parent_item = configfs_get_config_item(dentry->d_parent);
  642. type = parent_item->ci_type;
  643. subsys = to_config_group(parent_item)->cg_subsys;
  644. BUG_ON(!subsys);
  645. if (!type || !type->ct_group_ops ||
  646. (!type->ct_group_ops->make_group &&
  647. !type->ct_group_ops->make_item)) {
  648. ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
  649. goto out_put;
  650. }
  651. name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
  652. if (!name) {
  653. ret = -ENOMEM;
  654. goto out_put;
  655. }
  656. snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
  657. down(&subsys->su_sem);
  658. group = NULL;
  659. item = NULL;
  660. if (type->ct_group_ops->make_group) {
  661. group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
  662. if (group) {
  663. link_group(to_config_group(parent_item), group);
  664. item = &group->cg_item;
  665. }
  666. } else {
  667. item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
  668. if (item)
  669. link_obj(parent_item, item);
  670. }
  671. up(&subsys->su_sem);
  672. kfree(name);
  673. if (!item) {
  674. /*
  675. * If item == NULL, then link_obj() was never called.
  676. * There are no extra references to clean up.
  677. */
  678. ret = -ENOMEM;
  679. goto out_put;
  680. }
  681. /*
  682. * link_obj() has been called (via link_group() for groups).
  683. * From here on out, errors must clean that up.
  684. */
  685. type = item->ci_type;
  686. if (!type) {
  687. ret = -EINVAL;
  688. goto out_unlink;
  689. }
  690. owner = type->ct_owner;
  691. if (!try_module_get(owner)) {
  692. ret = -EINVAL;
  693. goto out_unlink;
  694. }
  695. /*
  696. * I hate doing it this way, but if there is
  697. * an error, module_put() probably should
  698. * happen after any cleanup.
  699. */
  700. module_got = 1;
  701. if (group)
  702. ret = configfs_attach_group(parent_item, item, dentry);
  703. else
  704. ret = configfs_attach_item(parent_item, item, dentry);
  705. out_unlink:
  706. if (ret) {
  707. /* Tear down everything we built up */
  708. down(&subsys->su_sem);
  709. if (group)
  710. unlink_group(group);
  711. else
  712. unlink_obj(item);
  713. client_drop_item(parent_item, item);
  714. up(&subsys->su_sem);
  715. if (module_got)
  716. module_put(owner);
  717. }
  718. out_put:
  719. /*
  720. * link_obj()/link_group() took a reference from child->parent,
  721. * so the parent is safely pinned. We can drop our working
  722. * reference.
  723. */
  724. config_item_put(parent_item);
  725. out:
  726. return ret;
  727. }
  728. static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
  729. {
  730. struct config_item *parent_item;
  731. struct config_item *item;
  732. struct configfs_subsystem *subsys;
  733. struct configfs_dirent *sd;
  734. struct module *owner = NULL;
  735. int ret;
  736. if (dentry->d_parent == configfs_sb->s_root)
  737. return -EPERM;
  738. sd = dentry->d_fsdata;
  739. if (sd->s_type & CONFIGFS_USET_DEFAULT)
  740. return -EPERM;
  741. /* Get a working ref until we have the child */
  742. parent_item = configfs_get_config_item(dentry->d_parent);
  743. subsys = to_config_group(parent_item)->cg_subsys;
  744. BUG_ON(!subsys);
  745. if (!parent_item->ci_type) {
  746. config_item_put(parent_item);
  747. return -EINVAL;
  748. }
  749. ret = configfs_detach_prep(dentry);
  750. if (ret) {
  751. configfs_detach_rollback(dentry);
  752. config_item_put(parent_item);
  753. return ret;
  754. }
  755. /* Get a working ref for the duration of this function */
  756. item = configfs_get_config_item(dentry);
  757. /* Drop reference from above, item already holds one. */
  758. config_item_put(parent_item);
  759. if (item->ci_type)
  760. owner = item->ci_type->ct_owner;
  761. if (sd->s_type & CONFIGFS_USET_DIR) {
  762. configfs_detach_group(item);
  763. down(&subsys->su_sem);
  764. unlink_group(to_config_group(item));
  765. } else {
  766. configfs_detach_item(item);
  767. down(&subsys->su_sem);
  768. unlink_obj(item);
  769. }
  770. client_drop_item(parent_item, item);
  771. up(&subsys->su_sem);
  772. /* Drop our reference from above */
  773. config_item_put(item);
  774. module_put(owner);
  775. return 0;
  776. }
  777. const struct inode_operations configfs_dir_inode_operations = {
  778. .mkdir = configfs_mkdir,
  779. .rmdir = configfs_rmdir,
  780. .symlink = configfs_symlink,
  781. .unlink = configfs_unlink,
  782. .lookup = configfs_lookup,
  783. .setattr = configfs_setattr,
  784. };
  785. #if 0
  786. int configfs_rename_dir(struct config_item * item, const char *new_name)
  787. {
  788. int error = 0;
  789. struct dentry * new_dentry, * parent;
  790. if (!strcmp(config_item_name(item), new_name))
  791. return -EINVAL;
  792. if (!item->parent)
  793. return -EINVAL;
  794. down_write(&configfs_rename_sem);
  795. parent = item->parent->dentry;
  796. mutex_lock(&parent->d_inode->i_mutex);
  797. new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
  798. if (!IS_ERR(new_dentry)) {
  799. if (!new_dentry->d_inode) {
  800. error = config_item_set_name(item, "%s", new_name);
  801. if (!error) {
  802. d_add(new_dentry, NULL);
  803. d_move(item->dentry, new_dentry);
  804. }
  805. else
  806. d_delete(new_dentry);
  807. } else
  808. error = -EEXIST;
  809. dput(new_dentry);
  810. }
  811. mutex_unlock(&parent->d_inode->i_mutex);
  812. up_write(&configfs_rename_sem);
  813. return error;
  814. }
  815. #endif
  816. static int configfs_dir_open(struct inode *inode, struct file *file)
  817. {
  818. struct dentry * dentry = file->f_path.dentry;
  819. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  820. mutex_lock(&dentry->d_inode->i_mutex);
  821. file->private_data = configfs_new_dirent(parent_sd, NULL);
  822. mutex_unlock(&dentry->d_inode->i_mutex);
  823. return file->private_data ? 0 : -ENOMEM;
  824. }
  825. static int configfs_dir_close(struct inode *inode, struct file *file)
  826. {
  827. struct dentry * dentry = file->f_path.dentry;
  828. struct configfs_dirent * cursor = file->private_data;
  829. mutex_lock(&dentry->d_inode->i_mutex);
  830. list_del_init(&cursor->s_sibling);
  831. mutex_unlock(&dentry->d_inode->i_mutex);
  832. release_configfs_dirent(cursor);
  833. return 0;
  834. }
  835. /* Relationship between s_mode and the DT_xxx types */
  836. static inline unsigned char dt_type(struct configfs_dirent *sd)
  837. {
  838. return (sd->s_mode >> 12) & 15;
  839. }
  840. static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  841. {
  842. struct dentry *dentry = filp->f_path.dentry;
  843. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  844. struct configfs_dirent *cursor = filp->private_data;
  845. struct list_head *p, *q = &cursor->s_sibling;
  846. ino_t ino;
  847. int i = filp->f_pos;
  848. switch (i) {
  849. case 0:
  850. ino = dentry->d_inode->i_ino;
  851. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  852. break;
  853. filp->f_pos++;
  854. i++;
  855. /* fallthrough */
  856. case 1:
  857. ino = parent_ino(dentry);
  858. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  859. break;
  860. filp->f_pos++;
  861. i++;
  862. /* fallthrough */
  863. default:
  864. if (filp->f_pos == 2) {
  865. list_move(q, &parent_sd->s_children);
  866. }
  867. for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
  868. struct configfs_dirent *next;
  869. const char * name;
  870. int len;
  871. next = list_entry(p, struct configfs_dirent,
  872. s_sibling);
  873. if (!next->s_element)
  874. continue;
  875. name = configfs_get_name(next);
  876. len = strlen(name);
  877. if (next->s_dentry)
  878. ino = next->s_dentry->d_inode->i_ino;
  879. else
  880. ino = iunique(configfs_sb, 2);
  881. if (filldir(dirent, name, len, filp->f_pos, ino,
  882. dt_type(next)) < 0)
  883. return 0;
  884. list_move(q, p);
  885. p = q;
  886. filp->f_pos++;
  887. }
  888. }
  889. return 0;
  890. }
  891. static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
  892. {
  893. struct dentry * dentry = file->f_path.dentry;
  894. mutex_lock(&dentry->d_inode->i_mutex);
  895. switch (origin) {
  896. case 1:
  897. offset += file->f_pos;
  898. case 0:
  899. if (offset >= 0)
  900. break;
  901. default:
  902. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  903. return -EINVAL;
  904. }
  905. if (offset != file->f_pos) {
  906. file->f_pos = offset;
  907. if (file->f_pos >= 2) {
  908. struct configfs_dirent *sd = dentry->d_fsdata;
  909. struct configfs_dirent *cursor = file->private_data;
  910. struct list_head *p;
  911. loff_t n = file->f_pos - 2;
  912. list_del(&cursor->s_sibling);
  913. p = sd->s_children.next;
  914. while (n && p != &sd->s_children) {
  915. struct configfs_dirent *next;
  916. next = list_entry(p, struct configfs_dirent,
  917. s_sibling);
  918. if (next->s_element)
  919. n--;
  920. p = p->next;
  921. }
  922. list_add_tail(&cursor->s_sibling, p);
  923. }
  924. }
  925. mutex_unlock(&dentry->d_inode->i_mutex);
  926. return offset;
  927. }
  928. const struct file_operations configfs_dir_operations = {
  929. .open = configfs_dir_open,
  930. .release = configfs_dir_close,
  931. .llseek = configfs_dir_lseek,
  932. .read = generic_read_dir,
  933. .readdir = configfs_readdir,
  934. };
  935. int configfs_register_subsystem(struct configfs_subsystem *subsys)
  936. {
  937. int err;
  938. struct config_group *group = &subsys->su_group;
  939. struct qstr name;
  940. struct dentry *dentry;
  941. struct configfs_dirent *sd;
  942. err = configfs_pin_fs();
  943. if (err)
  944. return err;
  945. if (!group->cg_item.ci_name)
  946. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  947. sd = configfs_sb->s_root->d_fsdata;
  948. link_group(to_config_group(sd->s_element), group);
  949. mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
  950. name.name = group->cg_item.ci_name;
  951. name.len = strlen(name.name);
  952. name.hash = full_name_hash(name.name, name.len);
  953. err = -ENOMEM;
  954. dentry = d_alloc(configfs_sb->s_root, &name);
  955. if (dentry) {
  956. d_add(dentry, NULL);
  957. err = configfs_attach_group(sd->s_element, &group->cg_item,
  958. dentry);
  959. if (err) {
  960. d_delete(dentry);
  961. dput(dentry);
  962. }
  963. }
  964. mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
  965. if (err) {
  966. unlink_group(group);
  967. configfs_release_fs();
  968. }
  969. return err;
  970. }
  971. void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
  972. {
  973. struct config_group *group = &subsys->su_group;
  974. struct dentry *dentry = group->cg_item.ci_dentry;
  975. if (dentry->d_parent != configfs_sb->s_root) {
  976. printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
  977. return;
  978. }
  979. mutex_lock_nested(&configfs_sb->s_root->d_inode->i_mutex,
  980. I_MUTEX_PARENT);
  981. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
  982. if (configfs_detach_prep(dentry)) {
  983. printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
  984. }
  985. configfs_detach_group(&group->cg_item);
  986. dentry->d_inode->i_flags |= S_DEAD;
  987. mutex_unlock(&dentry->d_inode->i_mutex);
  988. d_delete(dentry);
  989. mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
  990. dput(dentry);
  991. unlink_group(group);
  992. configfs_release_fs();
  993. }
  994. EXPORT_SYMBOL(configfs_register_subsystem);
  995. EXPORT_SYMBOL(configfs_unregister_subsystem);