dir.c 26 KB

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