dir.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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_del(q);
  843. list_add(q, &parent_sd->s_children);
  844. }
  845. for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
  846. struct configfs_dirent *next;
  847. const char * name;
  848. int len;
  849. next = list_entry(p, struct configfs_dirent,
  850. s_sibling);
  851. if (!next->s_element)
  852. continue;
  853. name = configfs_get_name(next);
  854. len = strlen(name);
  855. if (next->s_dentry)
  856. ino = next->s_dentry->d_inode->i_ino;
  857. else
  858. ino = iunique(configfs_sb, 2);
  859. if (filldir(dirent, name, len, filp->f_pos, ino,
  860. dt_type(next)) < 0)
  861. return 0;
  862. list_del(q);
  863. list_add(q, p);
  864. p = q;
  865. filp->f_pos++;
  866. }
  867. }
  868. return 0;
  869. }
  870. static loff_t configfs_dir_lseek(struct file * file, loff_t offset, int origin)
  871. {
  872. struct dentry * dentry = file->f_dentry;
  873. mutex_lock(&dentry->d_inode->i_mutex);
  874. switch (origin) {
  875. case 1:
  876. offset += file->f_pos;
  877. case 0:
  878. if (offset >= 0)
  879. break;
  880. default:
  881. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  882. return -EINVAL;
  883. }
  884. if (offset != file->f_pos) {
  885. file->f_pos = offset;
  886. if (file->f_pos >= 2) {
  887. struct configfs_dirent *sd = dentry->d_fsdata;
  888. struct configfs_dirent *cursor = file->private_data;
  889. struct list_head *p;
  890. loff_t n = file->f_pos - 2;
  891. list_del(&cursor->s_sibling);
  892. p = sd->s_children.next;
  893. while (n && p != &sd->s_children) {
  894. struct configfs_dirent *next;
  895. next = list_entry(p, struct configfs_dirent,
  896. s_sibling);
  897. if (next->s_element)
  898. n--;
  899. p = p->next;
  900. }
  901. list_add_tail(&cursor->s_sibling, p);
  902. }
  903. }
  904. mutex_unlock(&dentry->d_inode->i_mutex);
  905. return offset;
  906. }
  907. const struct file_operations configfs_dir_operations = {
  908. .open = configfs_dir_open,
  909. .release = configfs_dir_close,
  910. .llseek = configfs_dir_lseek,
  911. .read = generic_read_dir,
  912. .readdir = configfs_readdir,
  913. };
  914. int configfs_register_subsystem(struct configfs_subsystem *subsys)
  915. {
  916. int err;
  917. struct config_group *group = &subsys->su_group;
  918. struct qstr name;
  919. struct dentry *dentry;
  920. struct configfs_dirent *sd;
  921. err = configfs_pin_fs();
  922. if (err)
  923. return err;
  924. if (!group->cg_item.ci_name)
  925. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  926. sd = configfs_sb->s_root->d_fsdata;
  927. link_group(to_config_group(sd->s_element), group);
  928. mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
  929. name.name = group->cg_item.ci_name;
  930. name.len = strlen(name.name);
  931. name.hash = full_name_hash(name.name, name.len);
  932. err = -ENOMEM;
  933. dentry = d_alloc(configfs_sb->s_root, &name);
  934. if (!dentry)
  935. goto out_release;
  936. d_add(dentry, NULL);
  937. err = configfs_attach_group(sd->s_element, &group->cg_item,
  938. dentry);
  939. if (!err)
  940. dentry = NULL;
  941. else
  942. d_delete(dentry);
  943. mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
  944. if (dentry) {
  945. dput(dentry);
  946. out_release:
  947. unlink_group(group);
  948. configfs_release_fs();
  949. }
  950. return err;
  951. }
  952. void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
  953. {
  954. struct config_group *group = &subsys->su_group;
  955. struct dentry *dentry = group->cg_item.ci_dentry;
  956. if (dentry->d_parent != configfs_sb->s_root) {
  957. printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
  958. return;
  959. }
  960. mutex_lock(&configfs_sb->s_root->d_inode->i_mutex);
  961. mutex_lock(&dentry->d_inode->i_mutex);
  962. if (configfs_detach_prep(dentry)) {
  963. printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
  964. }
  965. configfs_detach_group(&group->cg_item);
  966. dentry->d_inode->i_flags |= S_DEAD;
  967. mutex_unlock(&dentry->d_inode->i_mutex);
  968. d_delete(dentry);
  969. mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
  970. dput(dentry);
  971. unlink_group(group);
  972. configfs_release_fs();
  973. }
  974. EXPORT_SYMBOL(configfs_register_subsystem);
  975. EXPORT_SYMBOL(configfs_unregister_subsystem);