xref: /openbmc/linux/fs/xfs/xfs_filestream.c (revision 76b47e528e3a27a3bf3b3f9153aad9435e03be8c)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
22a82b8beSDavid Chinner /*
32a82b8beSDavid Chinner  * Copyright (c) 2006-2007 Silicon Graphics, Inc.
42cd2ef6aSChristoph Hellwig  * Copyright (c) 2014 Christoph Hellwig.
52a82b8beSDavid Chinner  * All Rights Reserved.
62a82b8beSDavid Chinner  */
72a82b8beSDavid Chinner #include "xfs.h"
85467b34bSDarrick J. Wong #include "xfs_shared.h"
9a4fbe6abSDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
12239880efSDave Chinner #include "xfs_mount.h"
132a82b8beSDavid Chinner #include "xfs_inode.h"
142a82b8beSDavid Chinner #include "xfs_bmap.h"
152a82b8beSDavid Chinner #include "xfs_alloc.h"
162a82b8beSDavid Chinner #include "xfs_mru_cache.h"
170b1b213fSChristoph Hellwig #include "xfs_trace.h"
189bbafc71SDave Chinner #include "xfs_ag.h"
193fd129b6SDarrick J. Wong #include "xfs_ag_resv.h"
203e3673e3SBrian Foster #include "xfs_trans.h"
21f368b29bSDarrick J. Wong #include "xfs_filestream.h"
222a82b8beSDavid Chinner 
232cd2ef6aSChristoph Hellwig struct xfs_fstrm_item {
2422328d71SChristoph Hellwig 	struct xfs_mru_cache_elem	mru;
252cd2ef6aSChristoph Hellwig 	xfs_agnumber_t			ag; /* AG in use for this directory */
262cd2ef6aSChristoph Hellwig };
272cd2ef6aSChristoph Hellwig 
282cd2ef6aSChristoph Hellwig enum xfs_fstrm_alloc {
292cd2ef6aSChristoph Hellwig 	XFS_PICK_USERDATA = 1,
302cd2ef6aSChristoph Hellwig 	XFS_PICK_LOWSPACE = 2,
312cd2ef6aSChristoph Hellwig };
322a82b8beSDavid Chinner 
330664ce8dSChristoph Hellwig /*
340664ce8dSChristoph Hellwig  * Allocation group filestream associations are tracked with per-ag atomic
352cd2ef6aSChristoph Hellwig  * counters.  These counters allow xfs_filestream_pick_ag() to tell whether a
36b38e0740SGao Xiang  * particular AG already has active filestreams associated with it.
370664ce8dSChristoph Hellwig  */
38b94acd47SChristoph Hellwig int
390664ce8dSChristoph Hellwig xfs_filestream_peek_ag(
400664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
410664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
420664ce8dSChristoph Hellwig {
430664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
440664ce8dSChristoph Hellwig 	int		ret;
450664ce8dSChristoph Hellwig 
460664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
470664ce8dSChristoph Hellwig 	ret = atomic_read(&pag->pagf_fstrms);
480664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
490664ce8dSChristoph Hellwig 	return ret;
500664ce8dSChristoph Hellwig }
510664ce8dSChristoph Hellwig 
520664ce8dSChristoph Hellwig static int
530664ce8dSChristoph Hellwig xfs_filestream_get_ag(
540664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
550664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
560664ce8dSChristoph Hellwig {
570664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
580664ce8dSChristoph Hellwig 	int		ret;
590664ce8dSChristoph Hellwig 
600664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
610664ce8dSChristoph Hellwig 	ret = atomic_inc_return(&pag->pagf_fstrms);
620664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
630664ce8dSChristoph Hellwig 	return ret;
640664ce8dSChristoph Hellwig }
650664ce8dSChristoph Hellwig 
660664ce8dSChristoph Hellwig static void
670664ce8dSChristoph Hellwig xfs_filestream_put_ag(
680664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
690664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
700664ce8dSChristoph Hellwig {
710664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
720664ce8dSChristoph Hellwig 
730664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
740664ce8dSChristoph Hellwig 	atomic_dec(&pag->pagf_fstrms);
750664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
760664ce8dSChristoph Hellwig }
772a82b8beSDavid Chinner 
782cd2ef6aSChristoph Hellwig static void
792cd2ef6aSChristoph Hellwig xfs_fstrm_free_func(
807fcd3efaSChristoph Hellwig 	void			*data,
812cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru)
822cd2ef6aSChristoph Hellwig {
837fcd3efaSChristoph Hellwig 	struct xfs_mount	*mp = data;
842cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item =
852cd2ef6aSChristoph Hellwig 		container_of(mru, struct xfs_fstrm_item, mru);
862cd2ef6aSChristoph Hellwig 
877fcd3efaSChristoph Hellwig 	xfs_filestream_put_ag(mp, item->ag);
887fcd3efaSChristoph Hellwig 	trace_xfs_filestream_free(mp, mru->key, item->ag);
892cd2ef6aSChristoph Hellwig 
901919addaSChristoph Hellwig 	kmem_free(item);
912cd2ef6aSChristoph Hellwig }
922cd2ef6aSChristoph Hellwig 
932a82b8beSDavid Chinner /*
942a82b8beSDavid Chinner  * Scan the AGs starting at startag looking for an AG that isn't in use and has
952a82b8beSDavid Chinner  * at least minlen blocks free.
962a82b8beSDavid Chinner  */
972a82b8beSDavid Chinner static int
982cd2ef6aSChristoph Hellwig xfs_filestream_pick_ag(
992cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip,
1002a82b8beSDavid Chinner 	xfs_agnumber_t		startag,
1012a82b8beSDavid Chinner 	xfs_agnumber_t		*agp,
1022a82b8beSDavid Chinner 	int			flags,
1032a82b8beSDavid Chinner 	xfs_extlen_t		minlen)
1042a82b8beSDavid Chinner {
1052cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
1062cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item;
1072cd2ef6aSChristoph Hellwig 	struct xfs_perag	*pag;
108b94acd47SChristoph Hellwig 	xfs_extlen_t		longest, free = 0, minfree, maxfree = 0;
1092a82b8beSDavid Chinner 	xfs_agnumber_t		ag, max_ag = NULLAGNUMBER;
1102cd2ef6aSChristoph Hellwig 	int			err, trylock, nscan;
1112cd2ef6aSChristoph Hellwig 
112c19b3b05SDave Chinner 	ASSERT(S_ISDIR(VFS_I(ip)->i_mode));
1132a82b8beSDavid Chinner 
1142a82b8beSDavid Chinner 	/* 2% of an AG's blocks must be free for it to be chosen. */
1152a82b8beSDavid Chinner 	minfree = mp->m_sb.sb_agblocks / 50;
1162a82b8beSDavid Chinner 
1172a82b8beSDavid Chinner 	ag = startag;
1182a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
1192a82b8beSDavid Chinner 
1202a82b8beSDavid Chinner 	/* For the first pass, don't sleep trying to init the per-AG. */
1212a82b8beSDavid Chinner 	trylock = XFS_ALLOC_FLAG_TRYLOCK;
1222a82b8beSDavid Chinner 
1232a82b8beSDavid Chinner 	for (nscan = 0; 1; nscan++) {
1247fcd3efaSChristoph Hellwig 		trace_xfs_filestream_scan(mp, ip->i_ino, ag);
125b94acd47SChristoph Hellwig 
1264196ac08SDave Chinner 		pag = xfs_perag_get(mp, ag);
1272a82b8beSDavid Chinner 
1282a82b8beSDavid Chinner 		if (!pag->pagf_init) {
129*76b47e52SDave Chinner 			err = xfs_alloc_read_agf(mp, NULL, ag, trylock, NULL);
130f48e2df8SDarrick J. Wong 			if (err) {
131f650df71SBrian Foster 				if (err != -EAGAIN) {
1324196ac08SDave Chinner 					xfs_perag_put(pag);
1332a82b8beSDavid Chinner 					return err;
134f650df71SBrian Foster 				}
135f48e2df8SDarrick J. Wong 				/* Couldn't lock the AGF, skip this AG. */
136f650df71SBrian Foster 				goto next_ag;
1372a82b8beSDavid Chinner 			}
1384196ac08SDave Chinner 		}
1392a82b8beSDavid Chinner 
1402a82b8beSDavid Chinner 		/* Keep track of the AG with the most free blocks. */
1412a82b8beSDavid Chinner 		if (pag->pagf_freeblks > maxfree) {
1422a82b8beSDavid Chinner 			maxfree = pag->pagf_freeblks;
1432a82b8beSDavid Chinner 			max_ag = ag;
1442a82b8beSDavid Chinner 		}
1452a82b8beSDavid Chinner 
1462a82b8beSDavid Chinner 		/*
1472a82b8beSDavid Chinner 		 * The AG reference count does two things: it enforces mutual
1482a82b8beSDavid Chinner 		 * exclusion when examining the suitability of an AG in this
1492a82b8beSDavid Chinner 		 * loop, and it guards against two filestreams being established
1502a82b8beSDavid Chinner 		 * in the same AG as each other.
1512a82b8beSDavid Chinner 		 */
1522a82b8beSDavid Chinner 		if (xfs_filestream_get_ag(mp, ag) > 1) {
1532a82b8beSDavid Chinner 			xfs_filestream_put_ag(mp, ag);
1542a82b8beSDavid Chinner 			goto next_ag;
1552a82b8beSDavid Chinner 		}
1562a82b8beSDavid Chinner 
157a1f69417SEric Sandeen 		longest = xfs_alloc_longest_free_extent(pag,
1583fd129b6SDarrick J. Wong 				xfs_alloc_min_freelist(mp, pag),
1593fd129b6SDarrick J. Wong 				xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
1602a82b8beSDavid Chinner 		if (((minlen && longest >= minlen) ||
1612a82b8beSDavid Chinner 		     (!minlen && pag->pagf_freeblks >= minfree)) &&
1622a82b8beSDavid Chinner 		    (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
1632a82b8beSDavid Chinner 		     (flags & XFS_PICK_LOWSPACE))) {
1642a82b8beSDavid Chinner 
1652a82b8beSDavid Chinner 			/* Break out, retaining the reference on the AG. */
1662a82b8beSDavid Chinner 			free = pag->pagf_freeblks;
1674196ac08SDave Chinner 			xfs_perag_put(pag);
1682a82b8beSDavid Chinner 			*agp = ag;
1692a82b8beSDavid Chinner 			break;
1702a82b8beSDavid Chinner 		}
1712a82b8beSDavid Chinner 
1722a82b8beSDavid Chinner 		/* Drop the reference on this AG, it's not usable. */
1732a82b8beSDavid Chinner 		xfs_filestream_put_ag(mp, ag);
1742a82b8beSDavid Chinner next_ag:
1754196ac08SDave Chinner 		xfs_perag_put(pag);
1762a82b8beSDavid Chinner 		/* Move to the next AG, wrapping to AG 0 if necessary. */
1772a82b8beSDavid Chinner 		if (++ag >= mp->m_sb.sb_agcount)
1782a82b8beSDavid Chinner 			ag = 0;
1792a82b8beSDavid Chinner 
1802a82b8beSDavid Chinner 		/* If a full pass of the AGs hasn't been done yet, continue. */
1812a82b8beSDavid Chinner 		if (ag != startag)
1822a82b8beSDavid Chinner 			continue;
1832a82b8beSDavid Chinner 
184*76b47e52SDave Chinner 		/* Allow sleeping in xfs_alloc_read_agf() on the 2nd pass. */
1852a82b8beSDavid Chinner 		if (trylock != 0) {
1862a82b8beSDavid Chinner 			trylock = 0;
1872a82b8beSDavid Chinner 			continue;
1882a82b8beSDavid Chinner 		}
1892a82b8beSDavid Chinner 
1902a82b8beSDavid Chinner 		/* Finally, if lowspace wasn't set, set it for the 3rd pass. */
1912a82b8beSDavid Chinner 		if (!(flags & XFS_PICK_LOWSPACE)) {
1922a82b8beSDavid Chinner 			flags |= XFS_PICK_LOWSPACE;
1932a82b8beSDavid Chinner 			continue;
1942a82b8beSDavid Chinner 		}
1952a82b8beSDavid Chinner 
1962a82b8beSDavid Chinner 		/*
1972a82b8beSDavid Chinner 		 * Take the AG with the most free space, regardless of whether
1982a82b8beSDavid Chinner 		 * it's already in use by another filestream.
1992a82b8beSDavid Chinner 		 */
2002a82b8beSDavid Chinner 		if (max_ag != NULLAGNUMBER) {
2012a82b8beSDavid Chinner 			xfs_filestream_get_ag(mp, max_ag);
2022a82b8beSDavid Chinner 			free = maxfree;
2032a82b8beSDavid Chinner 			*agp = max_ag;
2042a82b8beSDavid Chinner 			break;
2052a82b8beSDavid Chinner 		}
2062a82b8beSDavid Chinner 
2072a82b8beSDavid Chinner 		/* take AG 0 if none matched */
208b94acd47SChristoph Hellwig 		trace_xfs_filestream_pick(ip, *agp, free, nscan);
2092a82b8beSDavid Chinner 		*agp = 0;
2102a82b8beSDavid Chinner 		return 0;
2112a82b8beSDavid Chinner 	}
2122a82b8beSDavid Chinner 
213b94acd47SChristoph Hellwig 	trace_xfs_filestream_pick(ip, *agp, free, nscan);
2142a82b8beSDavid Chinner 
2152cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
2162a82b8beSDavid Chinner 		return 0;
2172a82b8beSDavid Chinner 
2182451337dSDave Chinner 	err = -ENOMEM;
2191919addaSChristoph Hellwig 	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
2202a82b8beSDavid Chinner 	if (!item)
2212cd2ef6aSChristoph Hellwig 		goto out_put_ag;
2222a82b8beSDavid Chinner 
2232cd2ef6aSChristoph Hellwig 	item->ag = *agp;
2242a82b8beSDavid Chinner 
22522328d71SChristoph Hellwig 	err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
2262a82b8beSDavid Chinner 	if (err) {
2272451337dSDave Chinner 		if (err == -EEXIST)
2282cd2ef6aSChristoph Hellwig 			err = 0;
2292cd2ef6aSChristoph Hellwig 		goto out_free_item;
2302cd2ef6aSChristoph Hellwig 	}
2312cd2ef6aSChristoph Hellwig 
2322cd2ef6aSChristoph Hellwig 	return 0;
2332cd2ef6aSChristoph Hellwig 
2342cd2ef6aSChristoph Hellwig out_free_item:
2351919addaSChristoph Hellwig 	kmem_free(item);
2362cd2ef6aSChristoph Hellwig out_put_ag:
2372cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(mp, *agp);
2382a82b8beSDavid Chinner 	return err;
2392a82b8beSDavid Chinner }
2402a82b8beSDavid Chinner 
2412cd2ef6aSChristoph Hellwig static struct xfs_inode *
2422cd2ef6aSChristoph Hellwig xfs_filestream_get_parent(
2432cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2442a82b8beSDavid Chinner {
2452cd2ef6aSChristoph Hellwig 	struct inode		*inode = VFS_I(ip), *dir = NULL;
2462cd2ef6aSChristoph Hellwig 	struct dentry		*dentry, *parent;
2472a82b8beSDavid Chinner 
2482cd2ef6aSChristoph Hellwig 	dentry = d_find_alias(inode);
2492cd2ef6aSChristoph Hellwig 	if (!dentry)
2502cd2ef6aSChristoph Hellwig 		goto out;
2512a82b8beSDavid Chinner 
2522cd2ef6aSChristoph Hellwig 	parent = dget_parent(dentry);
2532cd2ef6aSChristoph Hellwig 	if (!parent)
2542cd2ef6aSChristoph Hellwig 		goto out_dput;
2552a82b8beSDavid Chinner 
2562b0143b5SDavid Howells 	dir = igrab(d_inode(parent));
2572cd2ef6aSChristoph Hellwig 	dput(parent);
2582a82b8beSDavid Chinner 
2592cd2ef6aSChristoph Hellwig out_dput:
2602cd2ef6aSChristoph Hellwig 	dput(dentry);
2612cd2ef6aSChristoph Hellwig out:
2622cd2ef6aSChristoph Hellwig 	return dir ? XFS_I(dir) : NULL;
2632a82b8beSDavid Chinner }
2642a82b8beSDavid Chinner 
2652a82b8beSDavid Chinner /*
2663b8d9076SChristoph Hellwig  * Find the right allocation group for a file, either by finding an
2673b8d9076SChristoph Hellwig  * existing file stream or creating a new one.
2683b8d9076SChristoph Hellwig  *
2693b8d9076SChristoph Hellwig  * Returns NULLAGNUMBER in case of an error.
2702a82b8beSDavid Chinner  */
2712a82b8beSDavid Chinner xfs_agnumber_t
2722a82b8beSDavid Chinner xfs_filestream_lookup_ag(
2732cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2742a82b8beSDavid Chinner {
27522328d71SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
2762cd2ef6aSChristoph Hellwig 	struct xfs_inode	*pip = NULL;
2773b8d9076SChristoph Hellwig 	xfs_agnumber_t		startag, ag = NULLAGNUMBER;
27822328d71SChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
2792a82b8beSDavid Chinner 
280c19b3b05SDave Chinner 	ASSERT(S_ISREG(VFS_I(ip)->i_mode));
2812a82b8beSDavid Chinner 
2822cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
2832cd2ef6aSChristoph Hellwig 	if (!pip)
284b26384dcSEric Sandeen 		return NULLAGNUMBER;
2852a82b8beSDavid Chinner 
2862cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
2873b8d9076SChristoph Hellwig 	if (mru) {
2883b8d9076SChristoph Hellwig 		ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
28922328d71SChristoph Hellwig 		xfs_mru_cache_done(mp->m_filestream);
2902a82b8beSDavid Chinner 
2917fcd3efaSChristoph Hellwig 		trace_xfs_filestream_lookup(mp, ip->i_ino, ag);
2923b8d9076SChristoph Hellwig 		goto out;
2932a82b8beSDavid Chinner 	}
2942a82b8beSDavid Chinner 
2952a82b8beSDavid Chinner 	/*
2962a82b8beSDavid Chinner 	 * Set the starting AG using the rotor for inode32, otherwise
2972a82b8beSDavid Chinner 	 * use the directory inode's AG.
2982a82b8beSDavid Chinner 	 */
2992e973b2cSDave Chinner 	if (xfs_is_inode32(mp)) {
3002cd2ef6aSChristoph Hellwig 		xfs_agnumber_t	 rotorstep = xfs_rotorstep;
3012a82b8beSDavid Chinner 		startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
3022a82b8beSDavid Chinner 		mp->m_agfrotor = (mp->m_agfrotor + 1) %
3032a82b8beSDavid Chinner 		                 (mp->m_sb.sb_agcount * rotorstep);
3042a82b8beSDavid Chinner 	} else
3052a82b8beSDavid Chinner 		startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
3062a82b8beSDavid Chinner 
3073b8d9076SChristoph Hellwig 	if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
3083b8d9076SChristoph Hellwig 		ag = NULLAGNUMBER;
3093b8d9076SChristoph Hellwig out:
31044a8736bSDarrick J. Wong 	xfs_irele(pip);
3113b8d9076SChristoph Hellwig 	return ag;
3122a82b8beSDavid Chinner }
3132a82b8beSDavid Chinner 
3142a82b8beSDavid Chinner /*
3152cd2ef6aSChristoph Hellwig  * Pick a new allocation group for the current file and its file stream.
3162cd2ef6aSChristoph Hellwig  *
3172cd2ef6aSChristoph Hellwig  * This is called when the allocator can't find a suitable extent in the
3182cd2ef6aSChristoph Hellwig  * current AG, and we have to move the stream into a new AG with more space.
3192a82b8beSDavid Chinner  */
3202a82b8beSDavid Chinner int
3212a82b8beSDavid Chinner xfs_filestream_new_ag(
32268988114SDave Chinner 	struct xfs_bmalloca	*ap,
3232a82b8beSDavid Chinner 	xfs_agnumber_t		*agp)
3242a82b8beSDavid Chinner {
3252cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip = ap->ip, *pip;
3262cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3272cd2ef6aSChristoph Hellwig 	xfs_extlen_t		minlen = ap->length;
3282cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		startag = 0;
329292378edSDave Chinner 	int			flags = 0;
330292378edSDave Chinner 	int			err = 0;
3312cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
3322a82b8beSDavid Chinner 
3332a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
3342a82b8beSDavid Chinner 
3352cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
3362cd2ef6aSChristoph Hellwig 	if (!pip)
3372cd2ef6aSChristoph Hellwig 		goto exit;
3382cd2ef6aSChristoph Hellwig 
3392cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
34022328d71SChristoph Hellwig 	if (mru) {
3412cd2ef6aSChristoph Hellwig 		struct xfs_fstrm_item *item =
3422cd2ef6aSChristoph Hellwig 			container_of(mru, struct xfs_fstrm_item, mru);
3432cd2ef6aSChristoph Hellwig 		startag = (item->ag + 1) % mp->m_sb.sb_agcount;
3442a82b8beSDavid Chinner 	}
3452a82b8beSDavid Chinner 
346c34d570dSChristoph Hellwig 	if (ap->datatype & XFS_ALLOC_USERDATA)
347292378edSDave Chinner 		flags |= XFS_PICK_USERDATA;
3481214f1cfSBrian Foster 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE)
349292378edSDave Chinner 		flags |= XFS_PICK_LOWSPACE;
3502a82b8beSDavid Chinner 
3512cd2ef6aSChristoph Hellwig 	err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
3522a82b8beSDavid Chinner 
3532a82b8beSDavid Chinner 	/*
3542cd2ef6aSChristoph Hellwig 	 * Only free the item here so we skip over the old AG earlier.
3552a82b8beSDavid Chinner 	 */
3562cd2ef6aSChristoph Hellwig 	if (mru)
3577fcd3efaSChristoph Hellwig 		xfs_fstrm_free_func(mp, mru);
3582a82b8beSDavid Chinner 
35944a8736bSDarrick J. Wong 	xfs_irele(pip);
3602a82b8beSDavid Chinner exit:
3612cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
3622a82b8beSDavid Chinner 		*agp = 0;
3632a82b8beSDavid Chinner 	return err;
3642a82b8beSDavid Chinner }
3652a82b8beSDavid Chinner 
3662a82b8beSDavid Chinner void
3672a82b8beSDavid Chinner xfs_filestream_deassociate(
3682cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
3692a82b8beSDavid Chinner {
37022328d71SChristoph Hellwig 	xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
3712a82b8beSDavid Chinner }
3722cd2ef6aSChristoph Hellwig 
3732cd2ef6aSChristoph Hellwig int
3742cd2ef6aSChristoph Hellwig xfs_filestream_mount(
3752cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
3762cd2ef6aSChristoph Hellwig {
3772cd2ef6aSChristoph Hellwig 	/*
3782cd2ef6aSChristoph Hellwig 	 * The filestream timer tunable is currently fixed within the range of
3792cd2ef6aSChristoph Hellwig 	 * one second to four minutes, with five seconds being the default.  The
3802cd2ef6aSChristoph Hellwig 	 * group count is somewhat arbitrary, but it'd be nice to adhere to the
3812cd2ef6aSChristoph Hellwig 	 * timer tunable to within about 10 percent.  This requires at least 10
3822cd2ef6aSChristoph Hellwig 	 * groups.
3832cd2ef6aSChristoph Hellwig 	 */
3847fcd3efaSChristoph Hellwig 	return xfs_mru_cache_create(&mp->m_filestream, mp,
3857fcd3efaSChristoph Hellwig 			xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func);
3862cd2ef6aSChristoph Hellwig }
3872cd2ef6aSChristoph Hellwig 
3882cd2ef6aSChristoph Hellwig void
3892cd2ef6aSChristoph Hellwig xfs_filestream_unmount(
3902cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
3912cd2ef6aSChristoph Hellwig {
3922cd2ef6aSChristoph Hellwig 	xfs_mru_cache_destroy(mp->m_filestream);
3932cd2ef6aSChristoph Hellwig }
394