xfs_filestream.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_sb.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_defer.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_bmap.h"
  16. #include "xfs_bmap_util.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_mru_cache.h"
  19. #include "xfs_filestream.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_ag_resv.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. The mount
  34. * point's m_peraglock is used to protect these counters from per-ag array
  35. * re-allocation during a growfs operation. When xfs_growfs_data_private() is
  36. * about to reallocate the array, it calls xfs_filestream_flush() with the
  37. * m_peraglock held in write mode.
  38. *
  39. * Since xfs_mru_cache_flush() guarantees that all the free functions for all
  40. * the cache elements have finished executing before it returns, it's safe for
  41. * the free functions to use the atomic counters without m_peraglock protection.
  42. * This allows the implementation of xfs_fstrm_free_func() to be agnostic about
  43. * whether it was called with the m_peraglock held in read mode, write mode or
  44. * not held at all. The race condition this addresses is the following:
  45. *
  46. * - The work queue scheduler fires and pulls a filestream directory cache
  47. * element off the LRU end of the cache for deletion, then gets pre-empted.
  48. * - A growfs operation grabs the m_peraglock in write mode, flushes all the
  49. * remaining items from the cache and reallocates the mount point's per-ag
  50. * array, resetting all the counters to zero.
  51. * - The work queue thread resumes and calls the free function for the element
  52. * it started cleaning up earlier. In the process it decrements the
  53. * filestreams counter for an AG that now has no references.
  54. *
  55. * With a shrinkfs feature, the above scenario could panic the system.
  56. *
  57. * All other uses of the following macros should be protected by either the
  58. * m_peraglock held in read mode, or the cache's internal locking exposed by the
  59. * interval between a call to xfs_mru_cache_lookup() and a call to
  60. * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode
  61. * when new elements are added to the cache.
  62. *
  63. * Combined, these locking rules ensure that no associations will ever exist in
  64. * the cache that reference per-ag array elements that have since been
  65. * reallocated.
  66. */
  67. int
  68. xfs_filestream_peek_ag(
  69. xfs_mount_t *mp,
  70. xfs_agnumber_t agno)
  71. {
  72. struct xfs_perag *pag;
  73. int ret;
  74. pag = xfs_perag_get(mp, agno);
  75. ret = atomic_read(&pag->pagf_fstrms);
  76. xfs_perag_put(pag);
  77. return ret;
  78. }
  79. static int
  80. xfs_filestream_get_ag(
  81. xfs_mount_t *mp,
  82. xfs_agnumber_t agno)
  83. {
  84. struct xfs_perag *pag;
  85. int ret;
  86. pag = xfs_perag_get(mp, agno);
  87. ret = atomic_inc_return(&pag->pagf_fstrms);
  88. xfs_perag_put(pag);
  89. return ret;
  90. }
  91. static void
  92. xfs_filestream_put_ag(
  93. xfs_mount_t *mp,
  94. xfs_agnumber_t agno)
  95. {
  96. struct xfs_perag *pag;
  97. pag = xfs_perag_get(mp, agno);
  98. atomic_dec(&pag->pagf_fstrms);
  99. xfs_perag_put(pag);
  100. }
  101. static void
  102. xfs_fstrm_free_func(
  103. void *data,
  104. struct xfs_mru_cache_elem *mru)
  105. {
  106. struct xfs_mount *mp = data;
  107. struct xfs_fstrm_item *item =
  108. container_of(mru, struct xfs_fstrm_item, mru);
  109. xfs_filestream_put_ag(mp, item->ag);
  110. trace_xfs_filestream_free(mp, mru->key, item->ag);
  111. kmem_free(item);
  112. }
  113. /*
  114. * Scan the AGs starting at startag looking for an AG that isn't in use and has
  115. * at least minlen blocks free.
  116. */
  117. static int
  118. xfs_filestream_pick_ag(
  119. struct xfs_inode *ip,
  120. xfs_agnumber_t startag,
  121. xfs_agnumber_t *agp,
  122. int flags,
  123. xfs_extlen_t minlen)
  124. {
  125. struct xfs_mount *mp = ip->i_mount;
  126. struct xfs_fstrm_item *item;
  127. struct xfs_perag *pag;
  128. xfs_extlen_t longest, free = 0, minfree, maxfree = 0;
  129. xfs_agnumber_t ag, max_ag = NULLAGNUMBER;
  130. int err, trylock, nscan;
  131. ASSERT(S_ISDIR(VFS_I(ip)->i_mode));
  132. /* 2% of an AG's blocks must be free for it to be chosen. */
  133. minfree = mp->m_sb.sb_agblocks / 50;
  134. ag = startag;
  135. *agp = NULLAGNUMBER;
  136. /* For the first pass, don't sleep trying to init the per-AG. */
  137. trylock = XFS_ALLOC_FLAG_TRYLOCK;
  138. for (nscan = 0; 1; nscan++) {
  139. trace_xfs_filestream_scan(mp, ip->i_ino, ag);
  140. pag = xfs_perag_get(mp, ag);
  141. if (!pag->pagf_init) {
  142. err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
  143. if (err && !trylock) {
  144. xfs_perag_put(pag);
  145. return err;
  146. }
  147. }
  148. /* Might fail sometimes during the 1st pass with trylock set. */
  149. if (!pag->pagf_init)
  150. goto next_ag;
  151. /* Keep track of the AG with the most free blocks. */
  152. if (pag->pagf_freeblks > maxfree) {
  153. maxfree = pag->pagf_freeblks;
  154. max_ag = ag;
  155. }
  156. /*
  157. * The AG reference count does two things: it enforces mutual
  158. * exclusion when examining the suitability of an AG in this
  159. * loop, and it guards against two filestreams being established
  160. * in the same AG as each other.
  161. */
  162. if (xfs_filestream_get_ag(mp, ag) > 1) {
  163. xfs_filestream_put_ag(mp, ag);
  164. goto next_ag;
  165. }
  166. longest = xfs_alloc_longest_free_extent(pag,
  167. xfs_alloc_min_freelist(mp, pag),
  168. xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
  169. if (((minlen && longest >= minlen) ||
  170. (!minlen && pag->pagf_freeblks >= minfree)) &&
  171. (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
  172. (flags & XFS_PICK_LOWSPACE))) {
  173. /* Break out, retaining the reference on the AG. */
  174. free = pag->pagf_freeblks;
  175. xfs_perag_put(pag);
  176. *agp = ag;
  177. break;
  178. }
  179. /* Drop the reference on this AG, it's not usable. */
  180. xfs_filestream_put_ag(mp, ag);
  181. next_ag:
  182. xfs_perag_put(pag);
  183. /* Move to the next AG, wrapping to AG 0 if necessary. */
  184. if (++ag >= mp->m_sb.sb_agcount)
  185. ag = 0;
  186. /* If a full pass of the AGs hasn't been done yet, continue. */
  187. if (ag != startag)
  188. continue;
  189. /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */
  190. if (trylock != 0) {
  191. trylock = 0;
  192. continue;
  193. }
  194. /* Finally, if lowspace wasn't set, set it for the 3rd pass. */
  195. if (!(flags & XFS_PICK_LOWSPACE)) {
  196. flags |= XFS_PICK_LOWSPACE;
  197. continue;
  198. }
  199. /*
  200. * Take the AG with the most free space, regardless of whether
  201. * it's already in use by another filestream.
  202. */
  203. if (max_ag != NULLAGNUMBER) {
  204. xfs_filestream_get_ag(mp, max_ag);
  205. free = maxfree;
  206. *agp = max_ag;
  207. break;
  208. }
  209. /* take AG 0 if none matched */
  210. trace_xfs_filestream_pick(ip, *agp, free, nscan);
  211. *agp = 0;
  212. return 0;
  213. }
  214. trace_xfs_filestream_pick(ip, *agp, free, nscan);
  215. if (*agp == NULLAGNUMBER)
  216. return 0;
  217. err = -ENOMEM;
  218. item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
  219. if (!item)
  220. goto out_put_ag;
  221. item->ag = *agp;
  222. err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
  223. if (err) {
  224. if (err == -EEXIST)
  225. err = 0;
  226. goto out_free_item;
  227. }
  228. return 0;
  229. out_free_item:
  230. kmem_free(item);
  231. out_put_ag:
  232. xfs_filestream_put_ag(mp, *agp);
  233. return err;
  234. }
  235. static struct xfs_inode *
  236. xfs_filestream_get_parent(
  237. struct xfs_inode *ip)
  238. {
  239. struct inode *inode = VFS_I(ip), *dir = NULL;
  240. struct dentry *dentry, *parent;
  241. dentry = d_find_alias(inode);
  242. if (!dentry)
  243. goto out;
  244. parent = dget_parent(dentry);
  245. if (!parent)
  246. goto out_dput;
  247. dir = igrab(d_inode(parent));
  248. dput(parent);
  249. out_dput:
  250. dput(dentry);
  251. out:
  252. return dir ? XFS_I(dir) : NULL;
  253. }
  254. /*
  255. * Find the right allocation group for a file, either by finding an
  256. * existing file stream or creating a new one.
  257. *
  258. * Returns NULLAGNUMBER in case of an error.
  259. */
  260. xfs_agnumber_t
  261. xfs_filestream_lookup_ag(
  262. struct xfs_inode *ip)
  263. {
  264. struct xfs_mount *mp = ip->i_mount;
  265. struct xfs_inode *pip = NULL;
  266. xfs_agnumber_t startag, ag = NULLAGNUMBER;
  267. struct xfs_mru_cache_elem *mru;
  268. ASSERT(S_ISREG(VFS_I(ip)->i_mode));
  269. pip = xfs_filestream_get_parent(ip);
  270. if (!pip)
  271. return NULLAGNUMBER;
  272. mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
  273. if (mru) {
  274. ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
  275. xfs_mru_cache_done(mp->m_filestream);
  276. trace_xfs_filestream_lookup(mp, ip->i_ino, ag);
  277. goto out;
  278. }
  279. /*
  280. * Set the starting AG using the rotor for inode32, otherwise
  281. * use the directory inode's AG.
  282. */
  283. if (mp->m_flags & XFS_MOUNT_32BITINODES) {
  284. xfs_agnumber_t rotorstep = xfs_rotorstep;
  285. startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
  286. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  287. (mp->m_sb.sb_agcount * rotorstep);
  288. } else
  289. startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
  290. if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
  291. ag = NULLAGNUMBER;
  292. out:
  293. IRELE(pip);
  294. return ag;
  295. }
  296. /*
  297. * Pick a new allocation group for the current file and its file stream.
  298. *
  299. * This is called when the allocator can't find a suitable extent in the
  300. * current AG, and we have to move the stream into a new AG with more space.
  301. */
  302. int
  303. xfs_filestream_new_ag(
  304. struct xfs_bmalloca *ap,
  305. xfs_agnumber_t *agp)
  306. {
  307. struct xfs_inode *ip = ap->ip, *pip;
  308. struct xfs_mount *mp = ip->i_mount;
  309. xfs_extlen_t minlen = ap->length;
  310. xfs_agnumber_t startag = 0;
  311. int flags = 0;
  312. int err = 0;
  313. struct xfs_mru_cache_elem *mru;
  314. *agp = NULLAGNUMBER;
  315. pip = xfs_filestream_get_parent(ip);
  316. if (!pip)
  317. goto exit;
  318. mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
  319. if (mru) {
  320. struct xfs_fstrm_item *item =
  321. container_of(mru, struct xfs_fstrm_item, mru);
  322. startag = (item->ag + 1) % mp->m_sb.sb_agcount;
  323. }
  324. if (xfs_alloc_is_userdata(ap->datatype))
  325. flags |= XFS_PICK_USERDATA;
  326. if (ap->dfops->dop_low)
  327. flags |= XFS_PICK_LOWSPACE;
  328. err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
  329. /*
  330. * Only free the item here so we skip over the old AG earlier.
  331. */
  332. if (mru)
  333. xfs_fstrm_free_func(mp, mru);
  334. IRELE(pip);
  335. exit:
  336. if (*agp == NULLAGNUMBER)
  337. *agp = 0;
  338. return err;
  339. }
  340. void
  341. xfs_filestream_deassociate(
  342. struct xfs_inode *ip)
  343. {
  344. xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
  345. }
  346. int
  347. xfs_filestream_mount(
  348. xfs_mount_t *mp)
  349. {
  350. /*
  351. * The filestream timer tunable is currently fixed within the range of
  352. * one second to four minutes, with five seconds being the default. The
  353. * group count is somewhat arbitrary, but it'd be nice to adhere to the
  354. * timer tunable to within about 10 percent. This requires at least 10
  355. * groups.
  356. */
  357. return xfs_mru_cache_create(&mp->m_filestream, mp,
  358. xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func);
  359. }
  360. void
  361. xfs_filestream_unmount(
  362. xfs_mount_t *mp)
  363. {
  364. xfs_mru_cache_destroy(mp->m_filestream);
  365. }