dir.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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/err.h>
  32. #include <linux/configfs.h>
  33. #include "configfs_internal.h"
  34. DECLARE_RWSEM(configfs_rename_sem);
  35. /*
  36. * Protects mutations of configfs_dirent linkage together with proper i_mutex
  37. * Also protects mutations of symlinks linkage to target configfs_dirent
  38. * Mutators of configfs_dirent linkage must *both* have the proper inode locked
  39. * and configfs_dirent_lock locked, in that order.
  40. * This allows one to safely traverse configfs_dirent trees and symlinks without
  41. * having to lock inodes.
  42. *
  43. * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
  44. * unlocked is not reliable unless in detach_groups() called from
  45. * rmdir()/unregister() and from configfs_attach_group()
  46. */
  47. DEFINE_SPINLOCK(configfs_dirent_lock);
  48. static void configfs_d_iput(struct dentry * dentry,
  49. struct inode * inode)
  50. {
  51. struct configfs_dirent *sd = dentry->d_fsdata;
  52. if (sd) {
  53. /* Coordinate with configfs_readdir */
  54. spin_lock(&configfs_dirent_lock);
  55. /* Coordinate with configfs_attach_attr where will increase
  56. * sd->s_count and update sd->s_dentry to new allocated one.
  57. * Only set sd->dentry to null when this dentry is the only
  58. * sd owner.
  59. * If not do so, configfs_d_iput may run just after
  60. * configfs_attach_attr and set sd->s_dentry to null
  61. * even it's still in use.
  62. */
  63. if (atomic_read(&sd->s_count) <= 2)
  64. sd->s_dentry = NULL;
  65. spin_unlock(&configfs_dirent_lock);
  66. configfs_put(sd);
  67. }
  68. iput(inode);
  69. }
  70. const struct dentry_operations configfs_dentry_ops = {
  71. .d_iput = configfs_d_iput,
  72. .d_delete = always_delete_dentry,
  73. };
  74. #ifdef CONFIG_LOCKDEP
  75. /*
  76. * Helpers to make lockdep happy with our recursive locking of default groups'
  77. * inodes (see configfs_attach_group() and configfs_detach_group()).
  78. * We put default groups i_mutexes in separate classes according to their depth
  79. * from the youngest non-default group ancestor.
  80. *
  81. * For a non-default group A having default groups A/B, A/C, and A/C/D, default
  82. * groups A/B and A/C will have their inode's mutex in class
  83. * default_group_class[0], and default group A/C/D will be in
  84. * default_group_class[1].
  85. *
  86. * The lock classes are declared and assigned in inode.c, according to the
  87. * s_depth value.
  88. * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
  89. * default groups, and reset to -1 when all default groups are attached. During
  90. * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
  91. * inode's mutex is set to default_group_class[s_depth - 1].
  92. */
  93. static void configfs_init_dirent_depth(struct configfs_dirent *sd)
  94. {
  95. sd->s_depth = -1;
  96. }
  97. static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
  98. struct configfs_dirent *sd)
  99. {
  100. int parent_depth = parent_sd->s_depth;
  101. if (parent_depth >= 0)
  102. sd->s_depth = parent_depth + 1;
  103. }
  104. static void
  105. configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
  106. {
  107. /*
  108. * item's i_mutex class is already setup, so s_depth is now only
  109. * used to set new sub-directories s_depth, which is always done
  110. * with item's i_mutex locked.
  111. */
  112. /*
  113. * sd->s_depth == -1 iff we are a non default group.
  114. * else (we are a default group) sd->s_depth > 0 (see
  115. * create_dir()).
  116. */
  117. if (sd->s_depth == -1)
  118. /*
  119. * We are a non default group and we are going to create
  120. * default groups.
  121. */
  122. sd->s_depth = 0;
  123. }
  124. static void
  125. configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
  126. {
  127. /* We will not create default groups anymore. */
  128. sd->s_depth = -1;
  129. }
  130. #else /* CONFIG_LOCKDEP */
  131. static void configfs_init_dirent_depth(struct configfs_dirent *sd)
  132. {
  133. }
  134. static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
  135. struct configfs_dirent *sd)
  136. {
  137. }
  138. static void
  139. configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
  140. {
  141. }
  142. static void
  143. configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
  144. {
  145. }
  146. #endif /* CONFIG_LOCKDEP */
  147. /*
  148. * Allocates a new configfs_dirent and links it to the parent configfs_dirent
  149. */
  150. static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
  151. void *element, int type)
  152. {
  153. struct configfs_dirent * sd;
  154. sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
  155. if (!sd)
  156. return ERR_PTR(-ENOMEM);
  157. atomic_set(&sd->s_count, 1);
  158. INIT_LIST_HEAD(&sd->s_links);
  159. INIT_LIST_HEAD(&sd->s_children);
  160. sd->s_element = element;
  161. sd->s_type = type;
  162. configfs_init_dirent_depth(sd);
  163. spin_lock(&configfs_dirent_lock);
  164. if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
  165. spin_unlock(&configfs_dirent_lock);
  166. kmem_cache_free(configfs_dir_cachep, sd);
  167. return ERR_PTR(-ENOENT);
  168. }
  169. list_add(&sd->s_sibling, &parent_sd->s_children);
  170. spin_unlock(&configfs_dirent_lock);
  171. return sd;
  172. }
  173. /*
  174. *
  175. * Return -EEXIST if there is already a configfs element with the same
  176. * name for the same parent.
  177. *
  178. * called with parent inode's i_mutex held
  179. */
  180. static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
  181. const unsigned char *new)
  182. {
  183. struct configfs_dirent * sd;
  184. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  185. if (sd->s_element) {
  186. const unsigned char *existing = configfs_get_name(sd);
  187. if (strcmp(existing, new))
  188. continue;
  189. else
  190. return -EEXIST;
  191. }
  192. }
  193. return 0;
  194. }
  195. int configfs_make_dirent(struct configfs_dirent * parent_sd,
  196. struct dentry * dentry, void * element,
  197. umode_t mode, int type)
  198. {
  199. struct configfs_dirent * sd;
  200. sd = configfs_new_dirent(parent_sd, element, type);
  201. if (IS_ERR(sd))
  202. return PTR_ERR(sd);
  203. sd->s_mode = mode;
  204. sd->s_dentry = dentry;
  205. if (dentry)
  206. dentry->d_fsdata = configfs_get(sd);
  207. return 0;
  208. }
  209. static void init_dir(struct inode * inode)
  210. {
  211. inode->i_op = &configfs_dir_inode_operations;
  212. inode->i_fop = &configfs_dir_operations;
  213. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  214. inc_nlink(inode);
  215. }
  216. static void configfs_init_file(struct inode * inode)
  217. {
  218. inode->i_size = PAGE_SIZE;
  219. inode->i_fop = &configfs_file_operations;
  220. }
  221. static void configfs_init_bin_file(struct inode *inode)
  222. {
  223. inode->i_size = 0;
  224. inode->i_fop = &configfs_bin_file_operations;
  225. }
  226. static void init_symlink(struct inode * inode)
  227. {
  228. inode->i_op = &configfs_symlink_inode_operations;
  229. }
  230. /**
  231. * configfs_create_dir - create a directory for an config_item.
  232. * @item: config_itemwe're creating directory for.
  233. * @dentry: config_item's dentry.
  234. *
  235. * Note: user-created entries won't be allowed under this new directory
  236. * until it is validated by configfs_dir_set_ready()
  237. */
  238. static int configfs_create_dir(struct config_item *item, struct dentry *dentry)
  239. {
  240. int error;
  241. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  242. struct dentry *p = dentry->d_parent;
  243. BUG_ON(!item);
  244. error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
  245. if (unlikely(error))
  246. return error;
  247. error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
  248. CONFIGFS_DIR | CONFIGFS_USET_CREATING);
  249. if (unlikely(error))
  250. return error;
  251. configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
  252. error = configfs_create(dentry, mode, init_dir);
  253. if (!error) {
  254. inc_nlink(d_inode(p));
  255. item->ci_dentry = dentry;
  256. } else {
  257. struct configfs_dirent *sd = dentry->d_fsdata;
  258. if (sd) {
  259. spin_lock(&configfs_dirent_lock);
  260. list_del_init(&sd->s_sibling);
  261. spin_unlock(&configfs_dirent_lock);
  262. configfs_put(sd);
  263. }
  264. }
  265. return error;
  266. }
  267. /*
  268. * Allow userspace to create new entries under a new directory created with
  269. * configfs_create_dir(), and under all of its chidlren directories recursively.
  270. * @sd configfs_dirent of the new directory to validate
  271. *
  272. * Caller must hold configfs_dirent_lock.
  273. */
  274. static void configfs_dir_set_ready(struct configfs_dirent *sd)
  275. {
  276. struct configfs_dirent *child_sd;
  277. sd->s_type &= ~CONFIGFS_USET_CREATING;
  278. list_for_each_entry(child_sd, &sd->s_children, s_sibling)
  279. if (child_sd->s_type & CONFIGFS_USET_CREATING)
  280. configfs_dir_set_ready(child_sd);
  281. }
  282. /*
  283. * Check that a directory does not belong to a directory hierarchy being
  284. * attached and not validated yet.
  285. * @sd configfs_dirent of the directory to check
  286. *
  287. * @return non-zero iff the directory was validated
  288. *
  289. * Note: takes configfs_dirent_lock, so the result may change from false to true
  290. * in two consecutive calls, but never from true to false.
  291. */
  292. int configfs_dirent_is_ready(struct configfs_dirent *sd)
  293. {
  294. int ret;
  295. spin_lock(&configfs_dirent_lock);
  296. ret = !(sd->s_type & CONFIGFS_USET_CREATING);
  297. spin_unlock(&configfs_dirent_lock);
  298. return ret;
  299. }
  300. int configfs_create_link(struct configfs_symlink *sl,
  301. struct dentry *parent,
  302. struct dentry *dentry)
  303. {
  304. int err = 0;
  305. umode_t mode = S_IFLNK | S_IRWXUGO;
  306. err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
  307. CONFIGFS_ITEM_LINK);
  308. if (!err) {
  309. err = configfs_create(dentry, mode, init_symlink);
  310. if (err) {
  311. struct configfs_dirent *sd = dentry->d_fsdata;
  312. if (sd) {
  313. spin_lock(&configfs_dirent_lock);
  314. list_del_init(&sd->s_sibling);
  315. spin_unlock(&configfs_dirent_lock);
  316. configfs_put(sd);
  317. }
  318. }
  319. }
  320. return err;
  321. }
  322. static void remove_dir(struct dentry * d)
  323. {
  324. struct dentry * parent = dget(d->d_parent);
  325. struct configfs_dirent * sd;
  326. sd = d->d_fsdata;
  327. spin_lock(&configfs_dirent_lock);
  328. list_del_init(&sd->s_sibling);
  329. spin_unlock(&configfs_dirent_lock);
  330. configfs_put(sd);
  331. if (d_really_is_positive(d))
  332. simple_rmdir(d_inode(parent),d);
  333. pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
  334. dput(parent);
  335. }
  336. /**
  337. * configfs_remove_dir - remove an config_item's directory.
  338. * @item: config_item we're removing.
  339. *
  340. * The only thing special about this is that we remove any files in
  341. * the directory before we remove the directory, and we've inlined
  342. * what used to be configfs_rmdir() below, instead of calling separately.
  343. *
  344. * Caller holds the mutex of the item's inode
  345. */
  346. static void configfs_remove_dir(struct config_item * item)
  347. {
  348. struct dentry * dentry = dget(item->ci_dentry);
  349. if (!dentry)
  350. return;
  351. remove_dir(dentry);
  352. /**
  353. * Drop reference from dget() on entrance.
  354. */
  355. dput(dentry);
  356. }
  357. /* attaches attribute's configfs_dirent to the dentry corresponding to the
  358. * attribute file
  359. */
  360. static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
  361. {
  362. struct configfs_attribute * attr = sd->s_element;
  363. int error;
  364. spin_lock(&configfs_dirent_lock);
  365. dentry->d_fsdata = configfs_get(sd);
  366. sd->s_dentry = dentry;
  367. spin_unlock(&configfs_dirent_lock);
  368. error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
  369. (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
  370. configfs_init_bin_file :
  371. configfs_init_file);
  372. if (error)
  373. configfs_put(sd);
  374. return error;
  375. }
  376. static struct dentry * configfs_lookup(struct inode *dir,
  377. struct dentry *dentry,
  378. unsigned int flags)
  379. {
  380. struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
  381. struct configfs_dirent * sd;
  382. int found = 0;
  383. int err;
  384. /*
  385. * Fake invisibility if dir belongs to a group/default groups hierarchy
  386. * being attached
  387. *
  388. * This forbids userspace to read/write attributes of items which may
  389. * not complete their initialization, since the dentries of the
  390. * attributes won't be instantiated.
  391. */
  392. err = -ENOENT;
  393. if (!configfs_dirent_is_ready(parent_sd))
  394. goto out;
  395. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  396. if (sd->s_type & CONFIGFS_NOT_PINNED) {
  397. const unsigned char * name = configfs_get_name(sd);
  398. if (strcmp(name, dentry->d_name.name))
  399. continue;
  400. found = 1;
  401. err = configfs_attach_attr(sd, dentry);
  402. break;
  403. }
  404. }
  405. if (!found) {
  406. /*
  407. * If it doesn't exist and it isn't a NOT_PINNED item,
  408. * it must be negative.
  409. */
  410. if (dentry->d_name.len > NAME_MAX)
  411. return ERR_PTR(-ENAMETOOLONG);
  412. d_add(dentry, NULL);
  413. return NULL;
  414. }
  415. out:
  416. return ERR_PTR(err);
  417. }
  418. /*
  419. * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
  420. * attributes and are removed by rmdir(). We recurse, setting
  421. * CONFIGFS_USET_DROPPING on all children that are candidates for
  422. * default detach.
  423. * If there is an error, the caller will reset the flags via
  424. * configfs_detach_rollback().
  425. */
  426. static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
  427. {
  428. struct configfs_dirent *parent_sd = dentry->d_fsdata;
  429. struct configfs_dirent *sd;
  430. int ret;
  431. /* Mark that we're trying to drop the group */
  432. parent_sd->s_type |= CONFIGFS_USET_DROPPING;
  433. ret = -EBUSY;
  434. if (!list_empty(&parent_sd->s_links))
  435. goto out;
  436. ret = 0;
  437. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  438. if (!sd->s_element ||
  439. (sd->s_type & CONFIGFS_NOT_PINNED))
  440. continue;
  441. if (sd->s_type & CONFIGFS_USET_DEFAULT) {
  442. /* Abort if racing with mkdir() */
  443. if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
  444. if (wait)
  445. *wait= dget(sd->s_dentry);
  446. return -EAGAIN;
  447. }
  448. /*
  449. * Yup, recursive. If there's a problem, blame
  450. * deep nesting of default_groups
  451. */
  452. ret = configfs_detach_prep(sd->s_dentry, wait);
  453. if (!ret)
  454. continue;
  455. } else
  456. ret = -ENOTEMPTY;
  457. break;
  458. }
  459. out:
  460. return ret;
  461. }
  462. /*
  463. * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
  464. * set.
  465. */
  466. static void configfs_detach_rollback(struct dentry *dentry)
  467. {
  468. struct configfs_dirent *parent_sd = dentry->d_fsdata;
  469. struct configfs_dirent *sd;
  470. parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
  471. list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
  472. if (sd->s_type & CONFIGFS_USET_DEFAULT)
  473. configfs_detach_rollback(sd->s_dentry);
  474. }
  475. static void detach_attrs(struct config_item * item)
  476. {
  477. struct dentry * dentry = dget(item->ci_dentry);
  478. struct configfs_dirent * parent_sd;
  479. struct configfs_dirent * sd, * tmp;
  480. if (!dentry)
  481. return;
  482. pr_debug("configfs %s: dropping attrs for dir\n",
  483. dentry->d_name.name);
  484. parent_sd = dentry->d_fsdata;
  485. list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  486. if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
  487. continue;
  488. spin_lock(&configfs_dirent_lock);
  489. list_del_init(&sd->s_sibling);
  490. spin_unlock(&configfs_dirent_lock);
  491. configfs_drop_dentry(sd, dentry);
  492. configfs_put(sd);
  493. }
  494. /**
  495. * Drop reference from dget() on entrance.
  496. */
  497. dput(dentry);
  498. }
  499. static int populate_attrs(struct config_item *item)
  500. {
  501. struct config_item_type *t = item->ci_type;
  502. struct configfs_attribute *attr;
  503. struct configfs_bin_attribute *bin_attr;
  504. int error = 0;
  505. int i;
  506. if (!t)
  507. return -EINVAL;
  508. if (t->ct_attrs) {
  509. for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
  510. if ((error = configfs_create_file(item, attr)))
  511. break;
  512. }
  513. }
  514. if (t->ct_bin_attrs) {
  515. for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
  516. error = configfs_create_bin_file(item, bin_attr);
  517. if (error)
  518. break;
  519. }
  520. }
  521. if (error)
  522. detach_attrs(item);
  523. return error;
  524. }
  525. static int configfs_attach_group(struct config_item *parent_item,
  526. struct config_item *item,
  527. struct dentry *dentry);
  528. static void configfs_detach_group(struct config_item *item);
  529. static void detach_groups(struct config_group *group)
  530. {
  531. struct dentry * dentry = dget(group->cg_item.ci_dentry);
  532. struct dentry *child;
  533. struct configfs_dirent *parent_sd;
  534. struct configfs_dirent *sd, *tmp;
  535. if (!dentry)
  536. return;
  537. parent_sd = dentry->d_fsdata;
  538. list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
  539. if (!sd->s_element ||
  540. !(sd->s_type & CONFIGFS_USET_DEFAULT))
  541. continue;
  542. child = sd->s_dentry;
  543. inode_lock(d_inode(child));
  544. configfs_detach_group(sd->s_element);
  545. d_inode(child)->i_flags |= S_DEAD;
  546. dont_mount(child);
  547. inode_unlock(d_inode(child));
  548. d_delete(child);
  549. dput(child);
  550. }
  551. /**
  552. * Drop reference from dget() on entrance.
  553. */
  554. dput(dentry);
  555. }
  556. /*
  557. * This fakes mkdir(2) on a default_groups[] entry. It
  558. * creates a dentry, attachs it, and then does fixup
  559. * on the sd->s_type.
  560. *
  561. * We could, perhaps, tweak our parent's ->mkdir for a minute and
  562. * try using vfs_mkdir. Just a thought.
  563. */
  564. static int create_default_group(struct config_group *parent_group,
  565. struct config_group *group)
  566. {
  567. int ret;
  568. struct configfs_dirent *sd;
  569. /* We trust the caller holds a reference to parent */
  570. struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
  571. if (!group->cg_item.ci_name)
  572. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  573. ret = -ENOMEM;
  574. child = d_alloc_name(parent, group->cg_item.ci_name);
  575. if (child) {
  576. d_add(child, NULL);
  577. ret = configfs_attach_group(&parent_group->cg_item,
  578. &group->cg_item, child);
  579. if (!ret) {
  580. sd = child->d_fsdata;
  581. sd->s_type |= CONFIGFS_USET_DEFAULT;
  582. } else {
  583. BUG_ON(d_inode(child));
  584. d_drop(child);
  585. dput(child);
  586. }
  587. }
  588. return ret;
  589. }
  590. static int populate_groups(struct config_group *group)
  591. {
  592. struct config_group *new_group;
  593. int ret = 0;
  594. list_for_each_entry(new_group, &group->default_groups, group_entry) {
  595. ret = create_default_group(group, new_group);
  596. if (ret) {
  597. detach_groups(group);
  598. break;
  599. }
  600. }
  601. return ret;
  602. }
  603. void configfs_remove_default_groups(struct config_group *group)
  604. {
  605. struct config_group *g, *n;
  606. list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
  607. list_del(&g->group_entry);
  608. config_item_put(&g->cg_item);
  609. }
  610. }
  611. EXPORT_SYMBOL(configfs_remove_default_groups);
  612. /*
  613. * All of link_obj/unlink_obj/link_group/unlink_group require that
  614. * subsys->su_mutex is held.
  615. */
  616. static void unlink_obj(struct config_item *item)
  617. {
  618. struct config_group *group;
  619. group = item->ci_group;
  620. if (group) {
  621. list_del_init(&item->ci_entry);
  622. item->ci_group = NULL;
  623. item->ci_parent = NULL;
  624. /* Drop the reference for ci_entry */
  625. config_item_put(item);
  626. /* Drop the reference for ci_parent */
  627. config_group_put(group);
  628. }
  629. }
  630. static void link_obj(struct config_item *parent_item, struct config_item *item)
  631. {
  632. /*
  633. * Parent seems redundant with group, but it makes certain
  634. * traversals much nicer.
  635. */
  636. item->ci_parent = parent_item;
  637. /*
  638. * We hold a reference on the parent for the child's ci_parent
  639. * link.
  640. */
  641. item->ci_group = config_group_get(to_config_group(parent_item));
  642. list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
  643. /*
  644. * We hold a reference on the child for ci_entry on the parent's
  645. * cg_children
  646. */
  647. config_item_get(item);
  648. }
  649. static void unlink_group(struct config_group *group)
  650. {
  651. struct config_group *new_group;
  652. list_for_each_entry(new_group, &group->default_groups, group_entry)
  653. unlink_group(new_group);
  654. group->cg_subsys = NULL;
  655. unlink_obj(&group->cg_item);
  656. }
  657. static void link_group(struct config_group *parent_group, struct config_group *group)
  658. {
  659. struct config_group *new_group;
  660. struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
  661. link_obj(&parent_group->cg_item, &group->cg_item);
  662. if (parent_group->cg_subsys)
  663. subsys = parent_group->cg_subsys;
  664. else if (configfs_is_root(&parent_group->cg_item))
  665. subsys = to_configfs_subsystem(group);
  666. else
  667. BUG();
  668. group->cg_subsys = subsys;
  669. list_for_each_entry(new_group, &group->default_groups, group_entry)
  670. link_group(group, new_group);
  671. }
  672. /*
  673. * The goal is that configfs_attach_item() (and
  674. * configfs_attach_group()) can be called from either the VFS or this
  675. * module. That is, they assume that the items have been created,
  676. * the dentry allocated, and the dcache is all ready to go.
  677. *
  678. * If they fail, they must clean up after themselves as if they
  679. * had never been called. The caller (VFS or local function) will
  680. * handle cleaning up the dcache bits.
  681. *
  682. * configfs_detach_group() and configfs_detach_item() behave similarly on
  683. * the way out. They assume that the proper semaphores are held, they
  684. * clean up the configfs items, and they expect their callers will
  685. * handle the dcache bits.
  686. */
  687. static int configfs_attach_item(struct config_item *parent_item,
  688. struct config_item *item,
  689. struct dentry *dentry)
  690. {
  691. int ret;
  692. ret = configfs_create_dir(item, dentry);
  693. if (!ret) {
  694. ret = populate_attrs(item);
  695. if (ret) {
  696. /*
  697. * We are going to remove an inode and its dentry but
  698. * the VFS may already have hit and used them. Thus,
  699. * we must lock them as rmdir() would.
  700. */
  701. inode_lock(d_inode(dentry));
  702. configfs_remove_dir(item);
  703. d_inode(dentry)->i_flags |= S_DEAD;
  704. dont_mount(dentry);
  705. inode_unlock(d_inode(dentry));
  706. d_delete(dentry);
  707. }
  708. }
  709. return ret;
  710. }
  711. /* Caller holds the mutex of the item's inode */
  712. static void configfs_detach_item(struct config_item *item)
  713. {
  714. detach_attrs(item);
  715. configfs_remove_dir(item);
  716. }
  717. static int configfs_attach_group(struct config_item *parent_item,
  718. struct config_item *item,
  719. struct dentry *dentry)
  720. {
  721. int ret;
  722. struct configfs_dirent *sd;
  723. ret = configfs_attach_item(parent_item, item, dentry);
  724. if (!ret) {
  725. sd = dentry->d_fsdata;
  726. sd->s_type |= CONFIGFS_USET_DIR;
  727. /*
  728. * FYI, we're faking mkdir in populate_groups()
  729. * We must lock the group's inode to avoid races with the VFS
  730. * which can already hit the inode and try to add/remove entries
  731. * under it.
  732. *
  733. * We must also lock the inode to remove it safely in case of
  734. * error, as rmdir() would.
  735. */
  736. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  737. configfs_adjust_dir_dirent_depth_before_populate(sd);
  738. ret = populate_groups(to_config_group(item));
  739. if (ret) {
  740. configfs_detach_item(item);
  741. d_inode(dentry)->i_flags |= S_DEAD;
  742. dont_mount(dentry);
  743. }
  744. configfs_adjust_dir_dirent_depth_after_populate(sd);
  745. inode_unlock(d_inode(dentry));
  746. if (ret)
  747. d_delete(dentry);
  748. }
  749. return ret;
  750. }
  751. /* Caller holds the mutex of the group's inode */
  752. static void configfs_detach_group(struct config_item *item)
  753. {
  754. detach_groups(to_config_group(item));
  755. configfs_detach_item(item);
  756. }
  757. /*
  758. * After the item has been detached from the filesystem view, we are
  759. * ready to tear it out of the hierarchy. Notify the client before
  760. * we do that so they can perform any cleanup that requires
  761. * navigating the hierarchy. A client does not need to provide this
  762. * callback. The subsystem semaphore MUST be held by the caller, and
  763. * references must be valid for both items. It also assumes the
  764. * caller has validated ci_type.
  765. */
  766. static void client_disconnect_notify(struct config_item *parent_item,
  767. struct config_item *item)
  768. {
  769. struct config_item_type *type;
  770. type = parent_item->ci_type;
  771. BUG_ON(!type);
  772. if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
  773. type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
  774. item);
  775. }
  776. /*
  777. * Drop the initial reference from make_item()/make_group()
  778. * This function assumes that reference is held on item
  779. * and that item holds a valid reference to the parent. Also, it
  780. * assumes the caller has validated ci_type.
  781. */
  782. static void client_drop_item(struct config_item *parent_item,
  783. struct config_item *item)
  784. {
  785. struct config_item_type *type;
  786. type = parent_item->ci_type;
  787. BUG_ON(!type);
  788. /*
  789. * If ->drop_item() exists, it is responsible for the
  790. * config_item_put().
  791. */
  792. if (type->ct_group_ops && type->ct_group_ops->drop_item)
  793. type->ct_group_ops->drop_item(to_config_group(parent_item),
  794. item);
  795. else
  796. config_item_put(item);
  797. }
  798. #ifdef DEBUG
  799. static void configfs_dump_one(struct configfs_dirent *sd, int level)
  800. {
  801. pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
  802. #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
  803. type_print(CONFIGFS_ROOT);
  804. type_print(CONFIGFS_DIR);
  805. type_print(CONFIGFS_ITEM_ATTR);
  806. type_print(CONFIGFS_ITEM_LINK);
  807. type_print(CONFIGFS_USET_DIR);
  808. type_print(CONFIGFS_USET_DEFAULT);
  809. type_print(CONFIGFS_USET_DROPPING);
  810. #undef type_print
  811. }
  812. static int configfs_dump(struct configfs_dirent *sd, int level)
  813. {
  814. struct configfs_dirent *child_sd;
  815. int ret = 0;
  816. configfs_dump_one(sd, level);
  817. if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
  818. return 0;
  819. list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
  820. ret = configfs_dump(child_sd, level + 2);
  821. if (ret)
  822. break;
  823. }
  824. return ret;
  825. }
  826. #endif
  827. /*
  828. * configfs_depend_item() and configfs_undepend_item()
  829. *
  830. * WARNING: Do not call these from a configfs callback!
  831. *
  832. * This describes these functions and their helpers.
  833. *
  834. * Allow another kernel system to depend on a config_item. If this
  835. * happens, the item cannot go away until the dependent can live without
  836. * it. The idea is to give client modules as simple an interface as
  837. * possible. When a system asks them to depend on an item, they just
  838. * call configfs_depend_item(). If the item is live and the client
  839. * driver is in good shape, we'll happily do the work for them.
  840. *
  841. * Why is the locking complex? Because configfs uses the VFS to handle
  842. * all locking, but this function is called outside the normal
  843. * VFS->configfs path. So it must take VFS locks to prevent the
  844. * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is
  845. * why you can't call these functions underneath configfs callbacks.
  846. *
  847. * Note, btw, that this can be called at *any* time, even when a configfs
  848. * subsystem isn't registered, or when configfs is loading or unloading.
  849. * Just like configfs_register_subsystem(). So we take the same
  850. * precautions. We pin the filesystem. We lock configfs_dirent_lock.
  851. * If we can find the target item in the
  852. * configfs tree, it must be part of the subsystem tree as well, so we
  853. * do not need the subsystem semaphore. Holding configfs_dirent_lock helps
  854. * locking out mkdir() and rmdir(), who might be racing us.
  855. */
  856. /*
  857. * configfs_depend_prep()
  858. *
  859. * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are
  860. * attributes. This is similar but not the same to configfs_detach_prep().
  861. * Note that configfs_detach_prep() expects the parent to be locked when it
  862. * is called, but we lock the parent *inside* configfs_depend_prep(). We
  863. * do that so we can unlock it if we find nothing.
  864. *
  865. * Here we do a depth-first search of the dentry hierarchy looking for
  866. * our object.
  867. * We deliberately ignore items tagged as dropping since they are virtually
  868. * dead, as well as items in the middle of attachment since they virtually
  869. * do not exist yet. This completes the locking out of racing mkdir() and
  870. * rmdir().
  871. * Note: subdirectories in the middle of attachment start with s_type =
  872. * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
  873. * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
  874. * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
  875. *
  876. * If the target is not found, -ENOENT is bubbled up.
  877. *
  878. * This adds a requirement that all config_items be unique!
  879. *
  880. * This is recursive. There isn't
  881. * much on the stack, though, so folks that need this function - be careful
  882. * about your stack! Patches will be accepted to make it iterative.
  883. */
  884. static int configfs_depend_prep(struct dentry *origin,
  885. struct config_item *target)
  886. {
  887. struct configfs_dirent *child_sd, *sd;
  888. int ret = 0;
  889. BUG_ON(!origin || !origin->d_fsdata);
  890. sd = origin->d_fsdata;
  891. if (sd->s_element == target) /* Boo-yah */
  892. goto out;
  893. list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
  894. if ((child_sd->s_type & CONFIGFS_DIR) &&
  895. !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
  896. !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
  897. ret = configfs_depend_prep(child_sd->s_dentry,
  898. target);
  899. if (!ret)
  900. goto out; /* Child path boo-yah */
  901. }
  902. }
  903. /* We looped all our children and didn't find target */
  904. ret = -ENOENT;
  905. out:
  906. return ret;
  907. }
  908. static int configfs_do_depend_item(struct dentry *subsys_dentry,
  909. struct config_item *target)
  910. {
  911. struct configfs_dirent *p;
  912. int ret;
  913. spin_lock(&configfs_dirent_lock);
  914. /* Scan the tree, return 0 if found */
  915. ret = configfs_depend_prep(subsys_dentry, target);
  916. if (ret)
  917. goto out_unlock_dirent_lock;
  918. /*
  919. * We are sure that the item is not about to be removed by rmdir(), and
  920. * not in the middle of attachment by mkdir().
  921. */
  922. p = target->ci_dentry->d_fsdata;
  923. p->s_dependent_count += 1;
  924. out_unlock_dirent_lock:
  925. spin_unlock(&configfs_dirent_lock);
  926. return ret;
  927. }
  928. static inline struct configfs_dirent *
  929. configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
  930. struct config_item *subsys_item)
  931. {
  932. struct configfs_dirent *p;
  933. struct configfs_dirent *ret = NULL;
  934. list_for_each_entry(p, &root_sd->s_children, s_sibling) {
  935. if (p->s_type & CONFIGFS_DIR &&
  936. p->s_element == subsys_item) {
  937. ret = p;
  938. break;
  939. }
  940. }
  941. return ret;
  942. }
  943. int configfs_depend_item(struct configfs_subsystem *subsys,
  944. struct config_item *target)
  945. {
  946. int ret;
  947. struct configfs_dirent *subsys_sd;
  948. struct config_item *s_item = &subsys->su_group.cg_item;
  949. struct dentry *root;
  950. /*
  951. * Pin the configfs filesystem. This means we can safely access
  952. * the root of the configfs filesystem.
  953. */
  954. root = configfs_pin_fs();
  955. if (IS_ERR(root))
  956. return PTR_ERR(root);
  957. /*
  958. * Next, lock the root directory. We're going to check that the
  959. * subsystem is really registered, and so we need to lock out
  960. * configfs_[un]register_subsystem().
  961. */
  962. inode_lock(d_inode(root));
  963. subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
  964. if (!subsys_sd) {
  965. ret = -ENOENT;
  966. goto out_unlock_fs;
  967. }
  968. /* Ok, now we can trust subsys/s_item */
  969. ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
  970. out_unlock_fs:
  971. inode_unlock(d_inode(root));
  972. /*
  973. * If we succeeded, the fs is pinned via other methods. If not,
  974. * we're done with it anyway. So release_fs() is always right.
  975. */
  976. configfs_release_fs();
  977. return ret;
  978. }
  979. EXPORT_SYMBOL(configfs_depend_item);
  980. /*
  981. * Release the dependent linkage. This is much simpler than
  982. * configfs_depend_item() because we know that that the client driver is
  983. * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
  984. */
  985. void configfs_undepend_item(struct config_item *target)
  986. {
  987. struct configfs_dirent *sd;
  988. /*
  989. * Since we can trust everything is pinned, we just need
  990. * configfs_dirent_lock.
  991. */
  992. spin_lock(&configfs_dirent_lock);
  993. sd = target->ci_dentry->d_fsdata;
  994. BUG_ON(sd->s_dependent_count < 1);
  995. sd->s_dependent_count -= 1;
  996. /*
  997. * After this unlock, we cannot trust the item to stay alive!
  998. * DO NOT REFERENCE item after this unlock.
  999. */
  1000. spin_unlock(&configfs_dirent_lock);
  1001. }
  1002. EXPORT_SYMBOL(configfs_undepend_item);
  1003. /*
  1004. * caller_subsys is a caller's subsystem not target's. This is used to
  1005. * determine if we should lock root and check subsys or not. When we are
  1006. * in the same subsystem as our target there is no need to do locking as
  1007. * we know that subsys is valid and is not unregistered during this function
  1008. * as we are called from callback of one of his children and VFS holds a lock
  1009. * on some inode. Otherwise we have to lock our root to ensure that target's
  1010. * subsystem it is not unregistered during this function.
  1011. */
  1012. int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
  1013. struct config_item *target)
  1014. {
  1015. struct configfs_subsystem *target_subsys;
  1016. struct config_group *root, *parent;
  1017. struct configfs_dirent *subsys_sd;
  1018. int ret = -ENOENT;
  1019. /* Disallow this function for configfs root */
  1020. if (configfs_is_root(target))
  1021. return -EINVAL;
  1022. parent = target->ci_group;
  1023. /*
  1024. * This may happen when someone is trying to depend root
  1025. * directory of some subsystem
  1026. */
  1027. if (configfs_is_root(&parent->cg_item)) {
  1028. target_subsys = to_configfs_subsystem(to_config_group(target));
  1029. root = parent;
  1030. } else {
  1031. target_subsys = parent->cg_subsys;
  1032. /* Find a cofnigfs root as we may need it for locking */
  1033. for (root = parent; !configfs_is_root(&root->cg_item);
  1034. root = root->cg_item.ci_group)
  1035. ;
  1036. }
  1037. if (target_subsys != caller_subsys) {
  1038. /*
  1039. * We are in other configfs subsystem, so we have to do
  1040. * additional locking to prevent other subsystem from being
  1041. * unregistered
  1042. */
  1043. inode_lock(d_inode(root->cg_item.ci_dentry));
  1044. /*
  1045. * As we are trying to depend item from other subsystem
  1046. * we have to check if this subsystem is still registered
  1047. */
  1048. subsys_sd = configfs_find_subsys_dentry(
  1049. root->cg_item.ci_dentry->d_fsdata,
  1050. &target_subsys->su_group.cg_item);
  1051. if (!subsys_sd)
  1052. goto out_root_unlock;
  1053. } else {
  1054. subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
  1055. }
  1056. /* Now we can execute core of depend item */
  1057. ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
  1058. if (target_subsys != caller_subsys)
  1059. out_root_unlock:
  1060. /*
  1061. * We were called from subsystem other than our target so we
  1062. * took some locks so now it's time to release them
  1063. */
  1064. inode_unlock(d_inode(root->cg_item.ci_dentry));
  1065. return ret;
  1066. }
  1067. EXPORT_SYMBOL(configfs_depend_item_unlocked);
  1068. static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1069. {
  1070. int ret = 0;
  1071. int module_got = 0;
  1072. struct config_group *group = NULL;
  1073. struct config_item *item = NULL;
  1074. struct config_item *parent_item;
  1075. struct configfs_subsystem *subsys;
  1076. struct configfs_dirent *sd;
  1077. struct config_item_type *type;
  1078. struct module *subsys_owner = NULL, *new_item_owner = NULL;
  1079. char *name;
  1080. sd = dentry->d_parent->d_fsdata;
  1081. /*
  1082. * Fake invisibility if dir belongs to a group/default groups hierarchy
  1083. * being attached
  1084. */
  1085. if (!configfs_dirent_is_ready(sd)) {
  1086. ret = -ENOENT;
  1087. goto out;
  1088. }
  1089. if (!(sd->s_type & CONFIGFS_USET_DIR)) {
  1090. ret = -EPERM;
  1091. goto out;
  1092. }
  1093. /* Get a working ref for the duration of this function */
  1094. parent_item = configfs_get_config_item(dentry->d_parent);
  1095. type = parent_item->ci_type;
  1096. subsys = to_config_group(parent_item)->cg_subsys;
  1097. BUG_ON(!subsys);
  1098. if (!type || !type->ct_group_ops ||
  1099. (!type->ct_group_ops->make_group &&
  1100. !type->ct_group_ops->make_item)) {
  1101. ret = -EPERM; /* Lack-of-mkdir returns -EPERM */
  1102. goto out_put;
  1103. }
  1104. /*
  1105. * The subsystem may belong to a different module than the item
  1106. * being created. We don't want to safely pin the new item but
  1107. * fail to pin the subsystem it sits under.
  1108. */
  1109. if (!subsys->su_group.cg_item.ci_type) {
  1110. ret = -EINVAL;
  1111. goto out_put;
  1112. }
  1113. subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
  1114. if (!try_module_get(subsys_owner)) {
  1115. ret = -EINVAL;
  1116. goto out_put;
  1117. }
  1118. name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
  1119. if (!name) {
  1120. ret = -ENOMEM;
  1121. goto out_subsys_put;
  1122. }
  1123. snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
  1124. mutex_lock(&subsys->su_mutex);
  1125. if (type->ct_group_ops->make_group) {
  1126. group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
  1127. if (!group)
  1128. group = ERR_PTR(-ENOMEM);
  1129. if (!IS_ERR(group)) {
  1130. link_group(to_config_group(parent_item), group);
  1131. item = &group->cg_item;
  1132. } else
  1133. ret = PTR_ERR(group);
  1134. } else {
  1135. item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
  1136. if (!item)
  1137. item = ERR_PTR(-ENOMEM);
  1138. if (!IS_ERR(item))
  1139. link_obj(parent_item, item);
  1140. else
  1141. ret = PTR_ERR(item);
  1142. }
  1143. mutex_unlock(&subsys->su_mutex);
  1144. kfree(name);
  1145. if (ret) {
  1146. /*
  1147. * If ret != 0, then link_obj() was never called.
  1148. * There are no extra references to clean up.
  1149. */
  1150. goto out_subsys_put;
  1151. }
  1152. /*
  1153. * link_obj() has been called (via link_group() for groups).
  1154. * From here on out, errors must clean that up.
  1155. */
  1156. type = item->ci_type;
  1157. if (!type) {
  1158. ret = -EINVAL;
  1159. goto out_unlink;
  1160. }
  1161. new_item_owner = type->ct_owner;
  1162. if (!try_module_get(new_item_owner)) {
  1163. ret = -EINVAL;
  1164. goto out_unlink;
  1165. }
  1166. /*
  1167. * I hate doing it this way, but if there is
  1168. * an error, module_put() probably should
  1169. * happen after any cleanup.
  1170. */
  1171. module_got = 1;
  1172. /*
  1173. * Make racing rmdir() fail if it did not tag parent with
  1174. * CONFIGFS_USET_DROPPING
  1175. * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
  1176. * fail and let rmdir() terminate correctly
  1177. */
  1178. spin_lock(&configfs_dirent_lock);
  1179. /* This will make configfs_detach_prep() fail */
  1180. sd->s_type |= CONFIGFS_USET_IN_MKDIR;
  1181. spin_unlock(&configfs_dirent_lock);
  1182. if (group)
  1183. ret = configfs_attach_group(parent_item, item, dentry);
  1184. else
  1185. ret = configfs_attach_item(parent_item, item, dentry);
  1186. spin_lock(&configfs_dirent_lock);
  1187. sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
  1188. if (!ret)
  1189. configfs_dir_set_ready(dentry->d_fsdata);
  1190. spin_unlock(&configfs_dirent_lock);
  1191. out_unlink:
  1192. if (ret) {
  1193. /* Tear down everything we built up */
  1194. mutex_lock(&subsys->su_mutex);
  1195. client_disconnect_notify(parent_item, item);
  1196. if (group)
  1197. unlink_group(group);
  1198. else
  1199. unlink_obj(item);
  1200. client_drop_item(parent_item, item);
  1201. mutex_unlock(&subsys->su_mutex);
  1202. if (module_got)
  1203. module_put(new_item_owner);
  1204. }
  1205. out_subsys_put:
  1206. if (ret)
  1207. module_put(subsys_owner);
  1208. out_put:
  1209. /*
  1210. * link_obj()/link_group() took a reference from child->parent,
  1211. * so the parent is safely pinned. We can drop our working
  1212. * reference.
  1213. */
  1214. config_item_put(parent_item);
  1215. out:
  1216. return ret;
  1217. }
  1218. static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
  1219. {
  1220. struct config_item *parent_item;
  1221. struct config_item *item;
  1222. struct configfs_subsystem *subsys;
  1223. struct configfs_dirent *sd;
  1224. struct module *subsys_owner = NULL, *dead_item_owner = NULL;
  1225. int ret;
  1226. sd = dentry->d_fsdata;
  1227. if (sd->s_type & CONFIGFS_USET_DEFAULT)
  1228. return -EPERM;
  1229. /* Get a working ref until we have the child */
  1230. parent_item = configfs_get_config_item(dentry->d_parent);
  1231. subsys = to_config_group(parent_item)->cg_subsys;
  1232. BUG_ON(!subsys);
  1233. if (!parent_item->ci_type) {
  1234. config_item_put(parent_item);
  1235. return -EINVAL;
  1236. }
  1237. /* configfs_mkdir() shouldn't have allowed this */
  1238. BUG_ON(!subsys->su_group.cg_item.ci_type);
  1239. subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
  1240. /*
  1241. * Ensure that no racing symlink() will make detach_prep() fail while
  1242. * the new link is temporarily attached
  1243. */
  1244. do {
  1245. struct dentry *wait;
  1246. mutex_lock(&configfs_symlink_mutex);
  1247. spin_lock(&configfs_dirent_lock);
  1248. /*
  1249. * Here's where we check for dependents. We're protected by
  1250. * configfs_dirent_lock.
  1251. * If no dependent, atomically tag the item as dropping.
  1252. */
  1253. ret = sd->s_dependent_count ? -EBUSY : 0;
  1254. if (!ret) {
  1255. ret = configfs_detach_prep(dentry, &wait);
  1256. if (ret)
  1257. configfs_detach_rollback(dentry);
  1258. }
  1259. spin_unlock(&configfs_dirent_lock);
  1260. mutex_unlock(&configfs_symlink_mutex);
  1261. if (ret) {
  1262. if (ret != -EAGAIN) {
  1263. config_item_put(parent_item);
  1264. return ret;
  1265. }
  1266. /* Wait until the racing operation terminates */
  1267. inode_lock(d_inode(wait));
  1268. inode_unlock(d_inode(wait));
  1269. dput(wait);
  1270. }
  1271. } while (ret == -EAGAIN);
  1272. /* Get a working ref for the duration of this function */
  1273. item = configfs_get_config_item(dentry);
  1274. /* Drop reference from above, item already holds one. */
  1275. config_item_put(parent_item);
  1276. if (item->ci_type)
  1277. dead_item_owner = item->ci_type->ct_owner;
  1278. if (sd->s_type & CONFIGFS_USET_DIR) {
  1279. configfs_detach_group(item);
  1280. mutex_lock(&subsys->su_mutex);
  1281. client_disconnect_notify(parent_item, item);
  1282. unlink_group(to_config_group(item));
  1283. } else {
  1284. configfs_detach_item(item);
  1285. mutex_lock(&subsys->su_mutex);
  1286. client_disconnect_notify(parent_item, item);
  1287. unlink_obj(item);
  1288. }
  1289. client_drop_item(parent_item, item);
  1290. mutex_unlock(&subsys->su_mutex);
  1291. /* Drop our reference from above */
  1292. config_item_put(item);
  1293. module_put(dead_item_owner);
  1294. module_put(subsys_owner);
  1295. return 0;
  1296. }
  1297. const struct inode_operations configfs_dir_inode_operations = {
  1298. .mkdir = configfs_mkdir,
  1299. .rmdir = configfs_rmdir,
  1300. .symlink = configfs_symlink,
  1301. .unlink = configfs_unlink,
  1302. .lookup = configfs_lookup,
  1303. .setattr = configfs_setattr,
  1304. };
  1305. const struct inode_operations configfs_root_inode_operations = {
  1306. .lookup = configfs_lookup,
  1307. .setattr = configfs_setattr,
  1308. };
  1309. #if 0
  1310. int configfs_rename_dir(struct config_item * item, const char *new_name)
  1311. {
  1312. int error = 0;
  1313. struct dentry * new_dentry, * parent;
  1314. if (!strcmp(config_item_name(item), new_name))
  1315. return -EINVAL;
  1316. if (!item->parent)
  1317. return -EINVAL;
  1318. down_write(&configfs_rename_sem);
  1319. parent = item->parent->dentry;
  1320. inode_lock(d_inode(parent));
  1321. new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
  1322. if (!IS_ERR(new_dentry)) {
  1323. if (d_really_is_negative(new_dentry)) {
  1324. error = config_item_set_name(item, "%s", new_name);
  1325. if (!error) {
  1326. d_add(new_dentry, NULL);
  1327. d_move(item->dentry, new_dentry);
  1328. }
  1329. else
  1330. d_delete(new_dentry);
  1331. } else
  1332. error = -EEXIST;
  1333. dput(new_dentry);
  1334. }
  1335. inode_unlock(d_inode(parent));
  1336. up_write(&configfs_rename_sem);
  1337. return error;
  1338. }
  1339. #endif
  1340. static int configfs_dir_open(struct inode *inode, struct file *file)
  1341. {
  1342. struct dentry * dentry = file->f_path.dentry;
  1343. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  1344. int err;
  1345. inode_lock(d_inode(dentry));
  1346. /*
  1347. * Fake invisibility if dir belongs to a group/default groups hierarchy
  1348. * being attached
  1349. */
  1350. err = -ENOENT;
  1351. if (configfs_dirent_is_ready(parent_sd)) {
  1352. file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
  1353. if (IS_ERR(file->private_data))
  1354. err = PTR_ERR(file->private_data);
  1355. else
  1356. err = 0;
  1357. }
  1358. inode_unlock(d_inode(dentry));
  1359. return err;
  1360. }
  1361. static int configfs_dir_close(struct inode *inode, struct file *file)
  1362. {
  1363. struct dentry * dentry = file->f_path.dentry;
  1364. struct configfs_dirent * cursor = file->private_data;
  1365. inode_lock(d_inode(dentry));
  1366. spin_lock(&configfs_dirent_lock);
  1367. list_del_init(&cursor->s_sibling);
  1368. spin_unlock(&configfs_dirent_lock);
  1369. inode_unlock(d_inode(dentry));
  1370. release_configfs_dirent(cursor);
  1371. return 0;
  1372. }
  1373. /* Relationship between s_mode and the DT_xxx types */
  1374. static inline unsigned char dt_type(struct configfs_dirent *sd)
  1375. {
  1376. return (sd->s_mode >> 12) & 15;
  1377. }
  1378. static int configfs_readdir(struct file *file, struct dir_context *ctx)
  1379. {
  1380. struct dentry *dentry = file->f_path.dentry;
  1381. struct super_block *sb = dentry->d_sb;
  1382. struct configfs_dirent * parent_sd = dentry->d_fsdata;
  1383. struct configfs_dirent *cursor = file->private_data;
  1384. struct list_head *p, *q = &cursor->s_sibling;
  1385. ino_t ino = 0;
  1386. if (!dir_emit_dots(file, ctx))
  1387. return 0;
  1388. spin_lock(&configfs_dirent_lock);
  1389. if (ctx->pos == 2)
  1390. list_move(q, &parent_sd->s_children);
  1391. for (p = q->next; p != &parent_sd->s_children; p = p->next) {
  1392. struct configfs_dirent *next;
  1393. const char *name;
  1394. int len;
  1395. struct inode *inode = NULL;
  1396. next = list_entry(p, struct configfs_dirent, s_sibling);
  1397. if (!next->s_element)
  1398. continue;
  1399. /*
  1400. * We'll have a dentry and an inode for
  1401. * PINNED items and for open attribute
  1402. * files. We lock here to prevent a race
  1403. * with configfs_d_iput() clearing
  1404. * s_dentry before calling iput().
  1405. *
  1406. * Why do we go to the trouble? If
  1407. * someone has an attribute file open,
  1408. * the inode number should match until
  1409. * they close it. Beyond that, we don't
  1410. * care.
  1411. */
  1412. dentry = next->s_dentry;
  1413. if (dentry)
  1414. inode = d_inode(dentry);
  1415. if (inode)
  1416. ino = inode->i_ino;
  1417. spin_unlock(&configfs_dirent_lock);
  1418. if (!inode)
  1419. ino = iunique(sb, 2);
  1420. name = configfs_get_name(next);
  1421. len = strlen(name);
  1422. if (!dir_emit(ctx, name, len, ino, dt_type(next)))
  1423. return 0;
  1424. spin_lock(&configfs_dirent_lock);
  1425. list_move(q, p);
  1426. p = q;
  1427. ctx->pos++;
  1428. }
  1429. spin_unlock(&configfs_dirent_lock);
  1430. return 0;
  1431. }
  1432. static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
  1433. {
  1434. struct dentry * dentry = file->f_path.dentry;
  1435. switch (whence) {
  1436. case 1:
  1437. offset += file->f_pos;
  1438. case 0:
  1439. if (offset >= 0)
  1440. break;
  1441. default:
  1442. return -EINVAL;
  1443. }
  1444. if (offset != file->f_pos) {
  1445. file->f_pos = offset;
  1446. if (file->f_pos >= 2) {
  1447. struct configfs_dirent *sd = dentry->d_fsdata;
  1448. struct configfs_dirent *cursor = file->private_data;
  1449. struct list_head *p;
  1450. loff_t n = file->f_pos - 2;
  1451. spin_lock(&configfs_dirent_lock);
  1452. list_del(&cursor->s_sibling);
  1453. p = sd->s_children.next;
  1454. while (n && p != &sd->s_children) {
  1455. struct configfs_dirent *next;
  1456. next = list_entry(p, struct configfs_dirent,
  1457. s_sibling);
  1458. if (next->s_element)
  1459. n--;
  1460. p = p->next;
  1461. }
  1462. list_add_tail(&cursor->s_sibling, p);
  1463. spin_unlock(&configfs_dirent_lock);
  1464. }
  1465. }
  1466. return offset;
  1467. }
  1468. const struct file_operations configfs_dir_operations = {
  1469. .open = configfs_dir_open,
  1470. .release = configfs_dir_close,
  1471. .llseek = configfs_dir_lseek,
  1472. .read = generic_read_dir,
  1473. .iterate_shared = configfs_readdir,
  1474. };
  1475. /**
  1476. * configfs_register_group - creates a parent-child relation between two groups
  1477. * @parent_group: parent group
  1478. * @group: child group
  1479. *
  1480. * link groups, creates dentry for the child and attaches it to the
  1481. * parent dentry.
  1482. *
  1483. * Return: 0 on success, negative errno code on error
  1484. */
  1485. int configfs_register_group(struct config_group *parent_group,
  1486. struct config_group *group)
  1487. {
  1488. struct configfs_subsystem *subsys = parent_group->cg_subsys;
  1489. struct dentry *parent;
  1490. int ret;
  1491. mutex_lock(&subsys->su_mutex);
  1492. link_group(parent_group, group);
  1493. mutex_unlock(&subsys->su_mutex);
  1494. parent = parent_group->cg_item.ci_dentry;
  1495. inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
  1496. ret = create_default_group(parent_group, group);
  1497. if (!ret) {
  1498. spin_lock(&configfs_dirent_lock);
  1499. configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
  1500. spin_unlock(&configfs_dirent_lock);
  1501. }
  1502. inode_unlock(d_inode(parent));
  1503. return ret;
  1504. }
  1505. EXPORT_SYMBOL(configfs_register_group);
  1506. /**
  1507. * configfs_unregister_group() - unregisters a child group from its parent
  1508. * @group: parent group to be unregistered
  1509. *
  1510. * Undoes configfs_register_group()
  1511. */
  1512. void configfs_unregister_group(struct config_group *group)
  1513. {
  1514. struct configfs_subsystem *subsys = group->cg_subsys;
  1515. struct dentry *dentry = group->cg_item.ci_dentry;
  1516. struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
  1517. inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
  1518. spin_lock(&configfs_dirent_lock);
  1519. configfs_detach_prep(dentry, NULL);
  1520. spin_unlock(&configfs_dirent_lock);
  1521. configfs_detach_group(&group->cg_item);
  1522. d_inode(dentry)->i_flags |= S_DEAD;
  1523. dont_mount(dentry);
  1524. d_delete(dentry);
  1525. inode_unlock(d_inode(parent));
  1526. dput(dentry);
  1527. mutex_lock(&subsys->su_mutex);
  1528. unlink_group(group);
  1529. mutex_unlock(&subsys->su_mutex);
  1530. }
  1531. EXPORT_SYMBOL(configfs_unregister_group);
  1532. /**
  1533. * configfs_register_default_group() - allocates and registers a child group
  1534. * @parent_group: parent group
  1535. * @name: child group name
  1536. * @item_type: child item type description
  1537. *
  1538. * boilerplate to allocate and register a child group with its parent. We need
  1539. * kzalloc'ed memory because child's default_group is initially empty.
  1540. *
  1541. * Return: allocated config group or ERR_PTR() on error
  1542. */
  1543. struct config_group *
  1544. configfs_register_default_group(struct config_group *parent_group,
  1545. const char *name,
  1546. struct config_item_type *item_type)
  1547. {
  1548. int ret;
  1549. struct config_group *group;
  1550. group = kzalloc(sizeof(*group), GFP_KERNEL);
  1551. if (!group)
  1552. return ERR_PTR(-ENOMEM);
  1553. config_group_init_type_name(group, name, item_type);
  1554. ret = configfs_register_group(parent_group, group);
  1555. if (ret) {
  1556. kfree(group);
  1557. return ERR_PTR(ret);
  1558. }
  1559. return group;
  1560. }
  1561. EXPORT_SYMBOL(configfs_register_default_group);
  1562. /**
  1563. * configfs_unregister_default_group() - unregisters and frees a child group
  1564. * @group: the group to act on
  1565. */
  1566. void configfs_unregister_default_group(struct config_group *group)
  1567. {
  1568. configfs_unregister_group(group);
  1569. kfree(group);
  1570. }
  1571. EXPORT_SYMBOL(configfs_unregister_default_group);
  1572. int configfs_register_subsystem(struct configfs_subsystem *subsys)
  1573. {
  1574. int err;
  1575. struct config_group *group = &subsys->su_group;
  1576. struct dentry *dentry;
  1577. struct dentry *root;
  1578. struct configfs_dirent *sd;
  1579. root = configfs_pin_fs();
  1580. if (IS_ERR(root))
  1581. return PTR_ERR(root);
  1582. if (!group->cg_item.ci_name)
  1583. group->cg_item.ci_name = group->cg_item.ci_namebuf;
  1584. sd = root->d_fsdata;
  1585. link_group(to_config_group(sd->s_element), group);
  1586. inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
  1587. err = -ENOMEM;
  1588. dentry = d_alloc_name(root, group->cg_item.ci_name);
  1589. if (dentry) {
  1590. d_add(dentry, NULL);
  1591. err = configfs_attach_group(sd->s_element, &group->cg_item,
  1592. dentry);
  1593. if (err) {
  1594. BUG_ON(d_inode(dentry));
  1595. d_drop(dentry);
  1596. dput(dentry);
  1597. } else {
  1598. spin_lock(&configfs_dirent_lock);
  1599. configfs_dir_set_ready(dentry->d_fsdata);
  1600. spin_unlock(&configfs_dirent_lock);
  1601. }
  1602. }
  1603. inode_unlock(d_inode(root));
  1604. if (err) {
  1605. unlink_group(group);
  1606. configfs_release_fs();
  1607. }
  1608. return err;
  1609. }
  1610. void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
  1611. {
  1612. struct config_group *group = &subsys->su_group;
  1613. struct dentry *dentry = group->cg_item.ci_dentry;
  1614. struct dentry *root = dentry->d_sb->s_root;
  1615. if (dentry->d_parent != root) {
  1616. pr_err("Tried to unregister non-subsystem!\n");
  1617. return;
  1618. }
  1619. inode_lock_nested(d_inode(root),
  1620. I_MUTEX_PARENT);
  1621. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  1622. mutex_lock(&configfs_symlink_mutex);
  1623. spin_lock(&configfs_dirent_lock);
  1624. if (configfs_detach_prep(dentry, NULL)) {
  1625. pr_err("Tried to unregister non-empty subsystem!\n");
  1626. }
  1627. spin_unlock(&configfs_dirent_lock);
  1628. mutex_unlock(&configfs_symlink_mutex);
  1629. configfs_detach_group(&group->cg_item);
  1630. d_inode(dentry)->i_flags |= S_DEAD;
  1631. dont_mount(dentry);
  1632. inode_unlock(d_inode(dentry));
  1633. d_delete(dentry);
  1634. inode_unlock(d_inode(root));
  1635. dput(dentry);
  1636. unlink_group(group);
  1637. configfs_release_fs();
  1638. }
  1639. EXPORT_SYMBOL(configfs_register_subsystem);
  1640. EXPORT_SYMBOL(configfs_unregister_subsystem);