xfs_filestream.c 11 KB

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