xfs_filestream.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_bmap.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_mru_cache.h"
  18. #include "xfs_trace.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_pagf_init(mp, NULL, ag, trylock);
  111. if (err) {
  112. xfs_perag_put(pag);
  113. if (err != -EAGAIN)
  114. return err;
  115. /* Couldn't lock the AGF, skip this AG. */
  116. continue;
  117. }
  118. }
  119. /* Keep track of the AG with the most free blocks. */
  120. if (pag->pagf_freeblks > maxfree) {
  121. maxfree = pag->pagf_freeblks;
  122. max_ag = ag;
  123. }
  124. /*
  125. * The AG reference count does two things: it enforces mutual
  126. * exclusion when examining the suitability of an AG in this
  127. * loop, and it guards against two filestreams being established
  128. * in the same AG as each other.
  129. */
  130. if (xfs_filestream_get_ag(mp, ag) > 1) {
  131. xfs_filestream_put_ag(mp, ag);
  132. goto next_ag;
  133. }
  134. longest = xfs_alloc_longest_free_extent(pag,
  135. xfs_alloc_min_freelist(mp, pag),
  136. xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
  137. if (((minlen && longest >= minlen) ||
  138. (!minlen && pag->pagf_freeblks >= minfree)) &&
  139. (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
  140. (flags & XFS_PICK_LOWSPACE))) {
  141. /* Break out, retaining the reference on the AG. */
  142. free = pag->pagf_freeblks;
  143. xfs_perag_put(pag);
  144. *agp = ag;
  145. break;
  146. }
  147. /* Drop the reference on this AG, it's not usable. */
  148. xfs_filestream_put_ag(mp, ag);
  149. next_ag:
  150. xfs_perag_put(pag);
  151. /* Move to the next AG, wrapping to AG 0 if necessary. */
  152. if (++ag >= mp->m_sb.sb_agcount)
  153. ag = 0;
  154. /* If a full pass of the AGs hasn't been done yet, continue. */
  155. if (ag != startag)
  156. continue;
  157. /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */
  158. if (trylock != 0) {
  159. trylock = 0;
  160. continue;
  161. }
  162. /* Finally, if lowspace wasn't set, set it for the 3rd pass. */
  163. if (!(flags & XFS_PICK_LOWSPACE)) {
  164. flags |= XFS_PICK_LOWSPACE;
  165. continue;
  166. }
  167. /*
  168. * Take the AG with the most free space, regardless of whether
  169. * it's already in use by another filestream.
  170. */
  171. if (max_ag != NULLAGNUMBER) {
  172. xfs_filestream_get_ag(mp, max_ag);
  173. free = maxfree;
  174. *agp = max_ag;
  175. break;
  176. }
  177. /* take AG 0 if none matched */
  178. trace_xfs_filestream_pick(ip, *agp, free, nscan);
  179. *agp = 0;
  180. return 0;
  181. }
  182. trace_xfs_filestream_pick(ip, *agp, free, nscan);
  183. if (*agp == NULLAGNUMBER)
  184. return 0;
  185. err = -ENOMEM;
  186. item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
  187. if (!item)
  188. goto out_put_ag;
  189. item->ag = *agp;
  190. err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
  191. if (err) {
  192. if (err == -EEXIST)
  193. err = 0;
  194. goto out_free_item;
  195. }
  196. return 0;
  197. out_free_item:
  198. kmem_free(item);
  199. out_put_ag:
  200. xfs_filestream_put_ag(mp, *agp);
  201. return err;
  202. }
  203. static struct xfs_inode *
  204. xfs_filestream_get_parent(
  205. struct xfs_inode *ip)
  206. {
  207. struct inode *inode = VFS_I(ip), *dir = NULL;
  208. struct dentry *dentry, *parent;
  209. dentry = d_find_alias(inode);
  210. if (!dentry)
  211. goto out;
  212. parent = dget_parent(dentry);
  213. if (!parent)
  214. goto out_dput;
  215. dir = igrab(d_inode(parent));
  216. dput(parent);
  217. out_dput:
  218. dput(dentry);
  219. out:
  220. return dir ? XFS_I(dir) : NULL;
  221. }
  222. /*
  223. * Find the right allocation group for a file, either by finding an
  224. * existing file stream or creating a new one.
  225. *
  226. * Returns NULLAGNUMBER in case of an error.
  227. */
  228. xfs_agnumber_t
  229. xfs_filestream_lookup_ag(
  230. struct xfs_inode *ip)
  231. {
  232. struct xfs_mount *mp = ip->i_mount;
  233. struct xfs_inode *pip = NULL;
  234. xfs_agnumber_t startag, ag = NULLAGNUMBER;
  235. struct xfs_mru_cache_elem *mru;
  236. ASSERT(S_ISREG(VFS_I(ip)->i_mode));
  237. pip = xfs_filestream_get_parent(ip);
  238. if (!pip)
  239. return NULLAGNUMBER;
  240. mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
  241. if (mru) {
  242. ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
  243. xfs_mru_cache_done(mp->m_filestream);
  244. trace_xfs_filestream_lookup(mp, ip->i_ino, ag);
  245. goto out;
  246. }
  247. /*
  248. * Set the starting AG using the rotor for inode32, otherwise
  249. * use the directory inode's AG.
  250. */
  251. if (mp->m_flags & XFS_MOUNT_32BITINODES) {
  252. xfs_agnumber_t rotorstep = xfs_rotorstep;
  253. startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
  254. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  255. (mp->m_sb.sb_agcount * rotorstep);
  256. } else
  257. startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
  258. if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
  259. ag = NULLAGNUMBER;
  260. out:
  261. xfs_irele(pip);
  262. return ag;
  263. }
  264. /*
  265. * Pick a new allocation group for the current file and its file stream.
  266. *
  267. * This is called when the allocator can't find a suitable extent in the
  268. * current AG, and we have to move the stream into a new AG with more space.
  269. */
  270. int
  271. xfs_filestream_new_ag(
  272. struct xfs_bmalloca *ap,
  273. xfs_agnumber_t *agp)
  274. {
  275. struct xfs_inode *ip = ap->ip, *pip;
  276. struct xfs_mount *mp = ip->i_mount;
  277. xfs_extlen_t minlen = ap->length;
  278. xfs_agnumber_t startag = 0;
  279. int flags = 0;
  280. int err = 0;
  281. struct xfs_mru_cache_elem *mru;
  282. *agp = NULLAGNUMBER;
  283. pip = xfs_filestream_get_parent(ip);
  284. if (!pip)
  285. goto exit;
  286. mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
  287. if (mru) {
  288. struct xfs_fstrm_item *item =
  289. container_of(mru, struct xfs_fstrm_item, mru);
  290. startag = (item->ag + 1) % mp->m_sb.sb_agcount;
  291. }
  292. if (ap->datatype & XFS_ALLOC_USERDATA)
  293. flags |= XFS_PICK_USERDATA;
  294. if (ap->tp->t_flags & XFS_TRANS_LOWMODE)
  295. flags |= XFS_PICK_LOWSPACE;
  296. err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
  297. /*
  298. * Only free the item here so we skip over the old AG earlier.
  299. */
  300. if (mru)
  301. xfs_fstrm_free_func(mp, mru);
  302. xfs_irele(pip);
  303. exit:
  304. if (*agp == NULLAGNUMBER)
  305. *agp = 0;
  306. return err;
  307. }
  308. void
  309. xfs_filestream_deassociate(
  310. struct xfs_inode *ip)
  311. {
  312. xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
  313. }
  314. int
  315. xfs_filestream_mount(
  316. xfs_mount_t *mp)
  317. {
  318. /*
  319. * The filestream timer tunable is currently fixed within the range of
  320. * one second to four minutes, with five seconds being the default. The
  321. * group count is somewhat arbitrary, but it'd be nice to adhere to the
  322. * timer tunable to within about 10 percent. This requires at least 10
  323. * groups.
  324. */
  325. return xfs_mru_cache_create(&mp->m_filestream, mp,
  326. xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func);
  327. }
  328. void
  329. xfs_filestream_unmount(
  330. xfs_mount_t *mp)
  331. {
  332. xfs_mru_cache_destroy(mp->m_filestream);
  333. }