dir.c 25 KB

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