dir.c 49 KB

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