dir.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. /*
  426. * FYI, we're faking mkdir here
  427. * I'm not sure we need this semaphore, as we're called
  428. * from our parent's mkdir. That holds our parent's
  429. * i_mutex, so afaik lookup cannot continue through our
  430. * parent to find us, let alone mess with our tree.
  431. * That said, taking our i_mutex is closer to mkdir
  432. * emulation, and shouldn't hurt.
  433. */
  434. mutex_lock(&dentry->d_inode->i_mutex);
  435. for (i = 0; group->default_groups[i]; i++) {
  436. new_group = group->default_groups[i];
  437. ret = create_default_group(group, new_group);
  438. if (ret)
  439. break;
  440. }
  441. mutex_unlock(&dentry->d_inode->i_mutex);
  442. }
  443. if (ret)
  444. detach_groups(group);
  445. return ret;
  446. }
  447. /*
  448. * All of link_obj/unlink_obj/link_group/unlink_group require that
  449. * subsys->su_sem is held.
  450. */
  451. static void unlink_obj(struct config_item *item)
  452. {
  453. struct config_group *group;
  454. group = item->ci_group;
  455. if (group) {
  456. list_del_init(&item->ci_entry);
  457. item->ci_group = NULL;
  458. item->ci_parent = NULL;
  459. /* Drop the reference for ci_entry */
  460. config_item_put(item);
  461. /* Drop the reference for ci_parent */
  462. config_group_put(group);
  463. }
  464. }
  465. static void link_obj(struct config_item *parent_item, struct config_item *item)
  466. {
  467. /*
  468. * Parent seems redundant with group, but it makes certain
  469. * traversals much nicer.
  470. */
  471. item->ci_parent = parent_item;
  472. /*
  473. * We hold a reference on the parent for the child's ci_parent
  474. * link.
  475. */
  476. item->ci_group = config_group_get(to_config_group(parent_item));
  477. list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
  478. /*
  479. * We hold a reference on the child for ci_entry on the parent's
  480. * cg_children
  481. */
  482. config_item_get(item);
  483. }
  484. static void unlink_group(struct config_group *group)
  485. {
  486. int i;
  487. struct config_group *new_group;
  488. if (group->default_groups) {
  489. for (i = 0; group->default_groups[i]; i++) {
  490. new_group = group->default_groups[i];
  491. unlink_group(new_group);
  492. }
  493. }
  494. group->cg_subsys = NULL;
  495. unlink_obj(&group->cg_item);
  496. }
  497. static void link_group(struct config_group *parent_group, struct config_group *group)
  498. {
  499. int i;
  500. struct config_group *new_group;
  501. struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
  502. link_obj(&parent_group->cg_item, &group->cg_item);
  503. if (parent_group->cg_subsys)
  504. subsys = parent_group->cg_subsys;
  505. else if (configfs_is_root(&parent_group->cg_item))
  506. subsys = to_configfs_subsystem(group);
  507. else
  508. BUG();
  509. group->cg_subsys = subsys;
  510. if (group->default_groups) {
  511. for (i = 0; group->default_groups[i]; i++) {
  512. new_group = group->default_groups[i];
  513. link_group(group, new_group);
  514. }
  515. }
  516. }
  517. /*
  518. * The goal is that configfs_attach_item() (and
  519. * configfs_attach_group()) can be called from either the VFS or this
  520. * module. That is, they assume that the items have been created,
  521. * the dentry allocated, and the dcache is all ready to go.
  522. *
  523. * If they fail, they must clean up after themselves as if they
  524. * had never been called. The caller (VFS or local function) will
  525. * handle cleaning up the dcache bits.
  526. *
  527. * configfs_detach_group() and configfs_detach_item() behave similarly on
  528. * the way out. They assume that the proper semaphores are held, they
  529. * clean up the configfs items, and they expect their callers will
  530. * handle the dcache bits.
  531. */
  532. static int configfs_attach_item(struct config_item *parent_item,
  533. struct config_item *item,
  534. struct dentry *dentry)
  535. {
  536. int ret;
  537. ret = configfs_create_dir(item, dentry);
  538. if (!ret) {
  539. ret = populate_attrs(item);
  540. if (ret) {
  541. configfs_remove_dir(item);
  542. d_delete(dentry);
  543. }
  544. }
  545. return ret;
  546. }
  547. static void configfs_detach_item(struct config_item *item)
  548. {
  549. detach_attrs(item);
  550. configfs_remove_dir(item);
  551. }
  552. static int configfs_attach_group(struct config_item *parent_item,
  553. struct config_item *item,
  554. struct dentry *dentry)
  555. {
  556. int ret;
  557. struct configfs_dirent *sd;
  558. ret = configfs_attach_item(parent_item, item, dentry);
  559. if (!ret) {
  560. sd = dentry->d_fsdata;
  561. sd->s_type |= CONFIGFS_USET_DIR;
  562. ret = populate_groups(to_config_group(item));
  563. if (ret) {
  564. configfs_detach_item(item);
  565. d_delete(dentry);
  566. }
  567. }
  568. return ret;
  569. }
  570. static void configfs_detach_group(struct config_item *item)
  571. {
  572. detach_groups(to_config_group(item));
  573. configfs_detach_item(item);
  574. }
  575. /*
  576. * Drop the initial reference from make_item()/make_group()
  577. * This function assumes that reference is held on item
  578. * and that item holds a valid reference to the parent. Also, it
  579. * assumes the caller has validated ci_type.
  580. */
  581. static void client_drop_item(struct config_item *parent_item,
  582. struct config_item *item)
  583. {
  584. struct config_item_type *type;
  585. type = parent_item->ci_type;
  586. BUG_ON(!type);
  587. /*
  588. * If ->drop_item() exists, it is responsible for the
  589. * config_item_put().
  590. */
  591. if (type->ct_group_ops && type->ct_group_ops->drop_item)
  592. type->ct_group_ops->drop_item(to_config_group(parent_item),
  593. item);
  594. else
  595. config_item_put(item);
  596. }
  597. static int configfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  598. {
  599. int ret, module_got = 0;
  600. struct config_group *group;
  601. struct config_item *item;
  602. struct config_item *parent_item;
  603. struct configfs_subsystem *subsys;
  604. struct configfs_dirent *sd;
  605. struct config_item_type *type;
  606. struct module *owner = NULL;
  607. char *name;
  608. if (dentry->d_parent == configfs_sb->s_root) {
  609. ret = -EPERM;
  610. goto out;
  611. }
  612. sd = dentry->d_parent->d_fsdata;
  613. if (!(sd->s_type & CONFIGFS_USET_DIR)) {
  614. ret = -EPERM;
  615. goto out;
  616. }
  617. /* Get a working ref for the duration of this function */
  618. parent_item = configfs_get_config_item(dentry->d_parent);
  619. type = parent_item->ci_type;
  620. subsys = to_config_group(parent_item)->cg_subsys;
  621. BUG_ON(!subsys);
  622. if (!type || !type->ct_group_ops ||
  623. (!type->ct_group_ops->make_group &&
  624. !type->ct_group_ops->make_item)) {
  625. ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
  626. goto out_put;
  627. }
  628. name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
  629. if (!name) {
  630. ret = -ENOMEM;
  631. goto out_put;
  632. }
  633. snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
  634. down(&subsys->su_sem);
  635. group = NULL;
  636. item = NULL;
  637. if (type->ct_group_ops->make_group) {
  638. group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
  639. if (group) {
  640. link_group(to_config_group(parent_item), group);
  641. item = &group->cg_item;
  642. }
  643. } else {
  644. item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
  645. if (item)
  646. link_obj(parent_item, item);
  647. }
  648. up(&subsys->su_sem);
  649. kfree(name);
  650. if (!item) {
  651. /*
  652. * If item == NULL, then link_obj() was never called.
  653. * There are no extra references to clean up.
  654. */
  655. ret = -ENOMEM;
  656. goto out_put;
  657. }
  658. /*
  659. * link_obj() has been called (via link_group() for groups).
  660. * From here on out, errors must clean that up.
  661. */
  662. type = item->ci_type;
  663. if (!type) {
  664. ret = -EINVAL;
  665. goto out_unlink;
  666. }
  667. owner = type->ct_owner;
  668. if (!try_module_get(owner)) {
  669. ret = -EINVAL;
  670. goto out_unlink;
  671. }
  672. /*
  673. * I hate doing it this way, but if there is
  674. * an error, module_put() probably should
  675. * happen after any cleanup.
  676. */
  677. module_got = 1;
  678. if (group)
  679. ret = configfs_attach_group(parent_item, item, dentry);
  680. else
  681. ret = configfs_attach_item(parent_item, item, dentry);
  682. out_unlink:
  683. if (ret) {
  684. /* Tear down everything we built up */
  685. down(&subsys->su_sem);
  686. if (group)
  687. unlink_group(group);
  688. else
  689. unlink_obj(item);
  690. client_drop_item(parent_item, item);
  691. up(&subsys->su_sem);
  692. if (module_got)
  693. module_put(owner);
  694. }
  695. out_put:
  696. /*
  697. * link_obj()/link_group() took a reference from child->parent,
  698. * so the parent is safely pinned. We can drop our working
  699. * reference.
  700. */
  701. config_item_put(parent_item);
  702. out:
  703. return ret;
  704. }
  705. static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
  706. {
  707. struct config_item *parent_item;
  708. struct config_item *item;
  709. struct configfs_subsystem *subsys;
  710. struct configfs_dirent *sd;
  711. struct module *owner = NULL;
  712. int ret;
  713. if (dentry->d_parent == configfs_sb->s_root)
  714. return -EPERM;
  715. sd = dentry->d_fsdata;
  716. if (sd->s_type & CONFIGFS_USET_DEFAULT)
  717. return -EPERM;
  718. /* Get a working ref until we have the child */
  719. parent_item = configfs_get_config_item(dentry->d_parent);
  720. subsys = to_config_group(parent_item)->cg_subsys;
  721. BUG_ON(!subsys);
  722. if (!parent_item->ci_type) {
  723. config_item_put(parent_item);
  724. return -EINVAL;
  725. }
  726. ret = configfs_detach_prep(dentry);
  727. if (ret) {
  728. configfs_detach_rollback(dentry);
  729. config_item_put(parent_item);
  730. return ret;
  731. }
  732. /* Get a working ref for the duration of this function */
  733. item = configfs_get_config_item(dentry);
  734. /* Drop reference from above, item already holds one. */
  735. config_item_put(parent_item);
  736. if (item->ci_type)
  737. owner = item->ci_type->ct_owner;
  738. if (sd->s_type & CONFIGFS_USET_DIR) {
  739. configfs_detach_group(item);
  740. down(&subsys->su_sem);
  741. unlink_group(to_config_group(item));
  742. } else {
  743. configfs_detach_item(item);
  744. down(&subsys->su_sem);
  745. unlink_obj(item);
  746. }
  747. client_drop_item(parent_item, item);
  748. up(&subsys->su_sem);
  749. /* Drop our reference from above */
  750. config_item_put(item);
  751. module_put(owner);
  752. return 0;
  753. }
  754. struct inode_operations configfs_dir_inode_operations = {
  755. .mkdir = configfs_mkdir,
  756. .rmdir = configfs_rmdir,
  757. .symlink = configfs_symlink,
  758. .unlink = configfs_unlink,
  759. .lookup = configfs_lookup,
  760. .setattr = configfs_setattr,
  761. };
  762. #if 0
  763. int configfs_rename_dir(struct config_item * item, const char *new_name)
  764. {
  765. int error = 0;
  766. struct dentry * new_dentry, * parent;
  767. if (!strcmp(config_item_name(item), new_name))
  768. return -EINVAL;
  769. if (!item->parent)
  770. return -EINVAL;
  771. down_write(&configfs_rename_sem);
  772. parent = item->parent->dentry;
  773. mutex_lock(&parent->d_inode->i_mutex);
  774. new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
  775. if (!IS_ERR(new_dentry)) {
  776. if (!new_dentry->d_inode) {
  777. error = config_item_set_name(item, "%s", new_name);
  778. if (!error) {
  779. d_add(new_dentry, NULL);
  780. d_move(item->dentry, new_dentry);
  781. }
  782. else
  783. d_delete(new_dentry);
  784. } else
  785. error = -EEXIST;
  786. dput(new_dentry);
  787. }
  788. mutex_unlock(&parent->d_inode->i_mutex);
  789. up_write(&configfs_rename_sem);
  790. return error;
  791. }
  792. #endif
  793. static int configfs_dir_open(struct inode *inode, struct file *file)
  794. {
  795. struct dentry * dentry = file->f_dentry;
  796. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  797. mutex_lock(&dentry->d_inode->i_mutex);
  798. file->private_data = configfs_new_dirent(parent_sd, NULL);
  799. mutex_unlock(&dentry->d_inode->i_mutex);
  800. return file->private_data ? 0 : -ENOMEM;
  801. }
  802. static int configfs_dir_close(struct inode *inode, struct file *file)
  803. {
  804. struct dentry * dentry = file->f_dentry;
  805. struct configfs_dirent * cursor = file->private_data;
  806. mutex_lock(&dentry->d_inode->i_mutex);
  807. list_del_init(&cursor->s_sibling);
  808. mutex_unlock(&dentry->d_inode->i_mutex);
  809. release_configfs_dirent(cursor);
  810. return 0;
  811. }
  812. /* Relationship between s_mode and the DT_xxx types */
  813. static inline unsigned char dt_type(struct configfs_dirent *sd)
  814. {
  815. return (sd->s_mode >> 12) & 15;
  816. }
  817. static int configfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  818. {
  819. struct dentry *dentry = filp->f_dentry;
  820. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  821. struct configfs_dirent *cursor = filp->private_data;
  822. struct list_head *p, *q = &cursor->s_sibling;
  823. ino_t ino;
  824. int i = filp->f_pos;
  825. switch (i) {
  826. case 0:
  827. ino = dentry->d_inode->i_ino;
  828. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  829. break;
  830. filp->f_pos++;
  831. i++;
  832. /* fallthrough */
  833. case 1:
  834. ino = parent_ino(dentry);
  835. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  836. break;
  837. filp->f_pos++;
  838. i++;
  839. /* fallthrough */
  840. default:
  841. if (filp->f_pos == 2) {
  842. list_move(q, &parent_sd->s_children);
  843. }
  844. for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
  845. struct configfs_dirent *next;
  846. const char * name;
  847. int len;
  848. next = list_entry(p, struct configfs_dirent,
  849. s_sibling);
  850. if (!next->s_element)
  851. continue;
  852. name = configfs_get_name(next);
  853. len = strlen(name);
  854. if (next->s_dentry)
  855. ino = next->s_dentry->d_inode->i_ino;
  856. else
  857. ino = iunique(configfs_sb, 2);
  858. if (filldir(dirent, name, len, filp->f_pos, ino,
  859. dt_type(next)) < 0)
  860. return 0;
  861. list_move(q, p);
  862. p = q;
  863. filp->f_pos++;
  864. }
  865. }
  866. return 0;
  867. }
  868. static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
  869. {
  870. struct dentry * dentry = file->f_dentry;
  871. mutex_lock(&dentry->d_inode->i_mutex);
  872. switch (origin) {
  873. case 1:
  874. offset += file->f_pos;
  875. case 0:
  876. if (offset >= 0)
  877. break;
  878. default:
  879. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  880. return -EINVAL;
  881. }
  882. if (offset != file->f_pos) {
  883. file->f_pos = offset;
  884. if (file->f_pos >= 2) {
  885. struct configfs_dirent *sd = dentry->d_fsdata;
  886. struct configfs_dirent *cursor = file->private_data;
  887. struct list_head *p;
  888. loff_t n = file->f_pos - 2;
  889. list_del(&cursor->s_sibling);
  890. p = sd->s_children.next;
  891. while (n && p != &sd->s_children) {
  892. struct configfs_dirent *next;
  893. next = list_entry(p, struct configfs_dirent,
  894. s_sibling);
  895. if (next->s_element)
  896. n--;
  897. p = p->next;
  898. }
  899. list_add_tail(&cursor->s_sibling, p);
  900. }
  901. }
  902. mutex_unlock(&dentry->d_inode->i_mutex);
  903. return offset;
  904. }
  905. const struct file_operations configfs_dir_operations = {
  906. .open = configfs_dir_open,
  907. .release = configfs_dir_close,
  908. .llseek = configfs_dir_lseek,
  909. .read = generic_read_dir,
  910. .readdir = configfs_readdir,
  911. };
  912. int configfs_register_subsystem(struct configfs_subsystem *subsys)
  913. {
  914. int err;
  915. struct config_group *group = &subsys->su_group;
  916. struct qstr name;
  917. struct dentry *dentry;
  918. struct configfs_dirent *sd;
  919. err = configfs_pin_fs();
  920. if (err)
  921. return err;
  922. if (!group->cg_item.ci_name)
  923. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  924. sd = configfs_sb->s_root->d_fsdata;
  925. link_group(to_config_group(sd->s_element), group);
  926. mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
  927. name.name = group->cg_item.ci_name;
  928. name.len = strlen(name.name);
  929. name.hash = full_name_hash(name.name, name.len);
  930. err = -ENOMEM;
  931. dentry = d_alloc(configfs_sb->s_root, &name);
  932. if (!dentry)
  933. goto out_release;
  934. d_add(dentry, NULL);
  935. err = configfs_attach_group(sd->s_element, &group->cg_item,
  936. dentry);
  937. if (!err)
  938. dentry = NULL;
  939. else
  940. d_delete(dentry);
  941. mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
  942. if (dentry) {
  943. dput(dentry);
  944. out_release:
  945. unlink_group(group);
  946. configfs_release_fs();
  947. }
  948. return err;
  949. }
  950. void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
  951. {
  952. struct config_group *group = &subsys->su_group;
  953. struct dentry *dentry = group->cg_item.ci_dentry;
  954. if (dentry->d_parent != configfs_sb->s_root) {
  955. printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
  956. return;
  957. }
  958. mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
  959. mutex_lock(&dentry->d_inode->i_mutex);
  960. if (configfs_detach_prep(dentry)) {
  961. printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
  962. }
  963. configfs_detach_group(&group->cg_item);
  964. dentry->d_inode->i_flags |= S_DEAD;
  965. mutex_unlock(&dentry->d_inode->i_mutex);
  966. d_delete(dentry);
  967. mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
  968. dput(dentry);
  969. unlink_group(group);
  970. configfs_release_fs();
  971. }
  972. EXPORT_SYMBOL(configfs_register_subsystem);
  973. EXPORT_SYMBOL(configfs_unregister_subsystem);