dir.c 27 KB

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