xfs_filestream.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2006-2007 Silicon Graphics, Inc.
  4. * Copyright (c) 2014 Christoph Hellwig.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_bmap.h"
  15. #include "xfs_alloc.h"
  16. #include "xfs_mru_cache.h"
  17. #include "xfs_trace.h"
  18. #include "xfs_ag.h"
  19. #include "xfs_ag_resv.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_filestream.h"
  22. struct xfs_fstrm_item {
  23. struct xfs_mru_cache_elem mru;
  24. xfs_agnumber_t ag; /* AG in use for this directory */
  25. };
  26. enum xfs_fstrm_alloc {
  27. XFS_PICK_USERDATA = 1,
  28. XFS_PICK_LOWSPACE = 2,
  29. };
  30. /*
  31. * Allocation group filestream associations are tracked with per-ag atomic
  32. * counters. These counters allow xfs_filestream_pick_ag() to tell whether a
  33. * particular AG already has active filestreams associated with it.
  34. */
  35. int
  36. xfs_filestream_peek_ag(
  37. xfs_mount_t *mp,
  38. xfs_agnumber_t agno)
  39. {
  40. struct xfs_perag *pag;
  41. int ret;
  42. pag = xfs_perag_get(mp, agno);
  43. ret = atomic_read(&pag->pagf_fstrms);
  44. xfs_perag_put(pag);
  45. return ret;
  46. }
  47. static int
  48. xfs_filestream_get_ag(
  49. xfs_mount_t *mp,
  50. xfs_agnumber_t agno)
  51. {
  52. struct xfs_perag *pag;
  53. int ret;
  54. pag = xfs_perag_get(mp, agno);
  55. ret = atomic_inc_return(&pag->pagf_fstrms);
  56. xfs_perag_put(pag);
  57. return ret;
  58. }
  59. static void
  60. xfs_filestream_put_ag(
  61. xfs_mount_t *mp,
  62. xfs_agnumber_t agno)
  63. {
  64. struct xfs_perag *pag;
  65. pag = xfs_perag_get(mp, agno);
  66. atomic_dec(&pag->pagf_fstrms);
  67. xfs_perag_put(pag);
  68. }
  69. static void
  70. xfs_fstrm_free_func(
  71. void *data,
  72. struct xfs_mru_cache_elem *mru)
  73. {
  74. struct xfs_mount *mp = data;
  75. struct xfs_fstrm_item *item =
  76. container_of(mru, struct xfs_fstrm_item, mru);
  77. xfs_filestream_put_ag(mp, item->ag);
  78. trace_xfs_filestream_free(mp, mru->key, item->ag);
  79. kmem_free(item);
  80. }
  81. /*
  82. * Scan the AGs starting at startag looking for an AG that isn't in use and has
  83. * at least minlen blocks free.
  84. */
  85. static int
  86. xfs_filestream_pick_ag(
  87. struct xfs_inode *ip,
  88. xfs_agnumber_t startag,
  89. xfs_agnumber_t *agp,
  90. int flags,
  91. xfs_extlen_t minlen)
  92. {
  93. struct xfs_mount *mp = ip->i_mount;
  94. struct xfs_fstrm_item *item;
  95. struct xfs_perag *pag;
  96. xfs_extlen_t longest, free = 0, minfree, maxfree = 0;
  97. xfs_agnumber_t ag, max_ag = NULLAGNUMBER;
  98. int err, trylock, nscan;
  99. ASSERT(S_ISDIR(VFS_I(ip)->i_mode));
  100. /* 2% of an AG's blocks must be free for it to be chosen. */
  101. minfree = mp->m_sb.sb_agblocks / 50;
  102. ag = startag;
  103. *agp = NULLAGNUMBER;
  104. /* For the first pass, don't sleep trying to init the per-AG. */
  105. trylock = XFS_ALLOC_FLAG_TRYLOCK;
  106. for (nscan = 0; 1; nscan++) {
  107. trace_xfs_filestream_scan(mp, ip->i_ino, ag);
  108. pag = xfs_perag_get(mp, ag);
  109. if (!pag->pagf_init) {
  110. err = xfs_alloc_read_agf(pag, NULL, trylock, NULL);
  111. if (err) {
  112. if (err != -EAGAIN) {
  113. xfs_perag_put(pag);
  114. return err;
  115. }
  116. /* Couldn't lock the AGF, skip this AG. */
  117. goto next_ag;
  118. }
  119. }
  120. /* Keep track of the AG with the most free blocks. */
  121. if (pag->pagf_freeblks > maxfree) {
  122. maxfree = pag->pagf_freeblks;
  123. max_ag = ag;
  124. }
  125. /*
  126. * The AG reference count does two things: it enforces mutual
  127. * exclusion when examining the suitability of an AG in this
  128. * loop, and it guards against two filestreams being established
  129. * in the same AG as each other.
  130. */
  131. if (xfs_filestream_get_ag(mp, ag) > 1) {
  132. xfs_filestream_put_ag(mp, ag);
  133. goto next_ag;
  134. }
  135. longest = xfs_alloc_longest_free_extent(pag,
  136. xfs_alloc_min_freelist(mp, pag),
  137. xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
  138. if (((minlen && longest >= minlen) ||
  139. (!minlen && pag->pagf_freeblks >= minfree)) &&
  140. (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
  141. (flags & XFS_PICK_LOWSPACE))) {
  142. /* Break out, retaining the reference on the AG. */
  143. free = pag->pagf_freeblks;
  144. xfs_perag_put(pag);
  145. *agp = ag;
  146. break;
  147. }
  148. /* Drop the reference on this AG, it's not usable. */
  149. xfs_filestream_put_ag(mp, ag);
  150. next_ag:
  151. xfs_perag_put(pag);
  152. /* Move to the next AG, wrapping to AG 0 if necessary. */
  153. if (++ag >= mp->m_sb.sb_agcount)
  154. ag = 0;
  155. /* If a full pass of the AGs hasn't been done yet, continue. */
  156. if (ag != startag)
  157. continue;
  158. /* Allow sleeping in xfs_alloc_read_agf() on the 2nd pass. */
  159. if (trylock != 0) {
  160. trylock = 0;
  161. continue;
  162. }
  163. /* Finally, if lowspace wasn't set, set it for the 3rd pass. */
  164. if (!(flags & XFS_PICK_LOWSPACE)) {
  165. flags |= XFS_PICK_LOWSPACE;
  166. continue;
  167. }
  168. /*
  169. * Take the AG with the most free space, regardless of whether
  170. * it's already in use by another filestream.
  171. */
  172. if (max_ag != NULLAGNUMBER) {
  173. xfs_filestream_get_ag(mp, max_ag);
  174. free = maxfree;
  175. *agp = max_ag;
  176. break;
  177. }
  178. /* take AG 0 if none matched */
  179. trace_xfs_filestream_pick(ip, *agp, free, nscan);
  180. *agp = 0;
  181. return 0;
  182. }
  183. trace_xfs_filestream_pick(ip, *agp, free, nscan);
  184. if (*agp == NULLAGNUMBER)
  185. return 0;
  186. err = -ENOMEM;
  187. item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
  188. if (!item)
  189. goto out_put_ag;
  190. item->ag = *agp;
  191. err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
  192. if (err) {
  193. if (err == -EEXIST)
  194. err = 0;
  195. goto out_free_item;
  196. }
  197. return 0;
  198. out_free_item:
  199. kmem_free(item);
  200. out_put_ag:
  201. xfs_filestream_put_ag(mp, *agp);
  202. return err;
  203. }
  204. static struct xfs_inode *
  205. xfs_filestream_get_parent(
  206. struct xfs_inode *ip)
  207. {
  208. struct inode *inode = VFS_I(ip), *dir = NULL;
  209. struct dentry *dentry, *parent;
  210. dentry = d_find_alias(inode);
  211. if (!dentry)
  212. goto out;
  213. parent = dget_parent(dentry);
  214. if (!parent)
  215. goto out_dput;
  216. dir = igrab(d_inode(parent));
  217. dput(parent);
  218. out_dput:
  219. dput(dentry);
  220. out:
  221. return dir ? XFS_I(dir) : NULL;
  222. }
  223. /*
  224. * Find the right allocation group for a file, either by finding an
  225. * existing file stream or creating a new one.
  226. *
  227. * Returns NULLAGNUMBER in case of an error.
  228. */
  229. xfs_agnumber_t
  230. xfs_filestream_lookup_ag(
  231. struct xfs_inode *ip)
  232. {
  233. struct xfs_mount *mp = ip->i_mount;
  234. struct xfs_inode *pip = NULL;
  235. xfs_agnumber_t startag, ag = NULLAGNUMBER;
  236. struct xfs_mru_cache_elem *mru;
  237. ASSERT(S_ISREG(VFS_I(ip)->i_mode));
  238. pip = xfs_filestream_get_parent(ip);
  239. if (!pip)
  240. return NULLAGNUMBER;
  241. mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
  242. if (mru) {
  243. ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
  244. xfs_mru_cache_done(mp->m_filestream);
  245. trace_xfs_filestream_lookup(mp, ip->i_ino, ag);
  246. goto out;
  247. }
  248. /*
  249. * Set the starting AG using the rotor for inode32, otherwise
  250. * use the directory inode's AG.
  251. */
  252. if (xfs_is_inode32(mp)) {
  253. xfs_agnumber_t rotorstep = xfs_rotorstep;
  254. startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
  255. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  256. (mp->m_sb.sb_agcount * rotorstep);
  257. } else
  258. startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
  259. if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
  260. ag = NULLAGNUMBER;
  261. out:
  262. xfs_irele(pip);
  263. return ag;
  264. }
  265. /*
  266. * Pick a new allocation group for the current file and its file stream.
  267. *
  268. * This is called when the allocator can't find a suitable extent in the
  269. * current AG, and we have to move the stream into a new AG with more space.
  270. */
  271. int
  272. xfs_filestream_new_ag(
  273. struct xfs_bmalloca *ap,
  274. xfs_agnumber_t *agp)
  275. {
  276. struct xfs_inode *ip = ap->ip, *pip;
  277. struct xfs_mount *mp = ip->i_mount;
  278. xfs_extlen_t minlen = ap->length;
  279. xfs_agnumber_t startag = 0;
  280. int flags = 0;
  281. int err = 0;
  282. struct xfs_mru_cache_elem *mru;
  283. *agp = NULLAGNUMBER;
  284. pip = xfs_filestream_get_parent(ip);
  285. if (!pip)
  286. goto exit;
  287. mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
  288. if (mru) {
  289. struct xfs_fstrm_item *item =
  290. container_of(mru, struct xfs_fstrm_item, mru);
  291. startag = (item->ag + 1) % mp->m_sb.sb_agcount;
  292. }
  293. if (ap->datatype & XFS_ALLOC_USERDATA)
  294. flags |= XFS_PICK_USERDATA;
  295. if (ap->tp->t_flags & XFS_TRANS_LOWMODE)
  296. flags |= XFS_PICK_LOWSPACE;
  297. err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
  298. /*
  299. * Only free the item here so we skip over the old AG earlier.
  300. */
  301. if (mru)
  302. xfs_fstrm_free_func(mp, mru);
  303. xfs_irele(pip);
  304. exit:
  305. if (*agp == NULLAGNUMBER)
  306. *agp = 0;
  307. return err;
  308. }
  309. void
  310. xfs_filestream_deassociate(
  311. struct xfs_inode *ip)
  312. {
  313. xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
  314. }
  315. int
  316. xfs_filestream_mount(
  317. xfs_mount_t *mp)
  318. {
  319. /*
  320. * The filestream timer tunable is currently fixed within the range of
  321. * one second to four minutes, with five seconds being the default. The
  322. * group count is somewhat arbitrary, but it'd be nice to adhere to the
  323. * timer tunable to within about 10 percent. This requires at least 10
  324. * groups.
  325. */
  326. return xfs_mru_cache_create(&mp->m_filestream, mp,
  327. xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func);
  328. }
  329. void
  330. xfs_filestream_unmount(
  331. xfs_mount_t *mp)
  332. {
  333. xfs_mru_cache_destroy(mp->m_filestream);
  334. }