xref: /openbmc/linux/fs/xfs/xfs_filestream.c (revision 7ac2ff8bb3713c7cb43564c04384af2ee7cc1f8d)
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 
128*7ac2ff8bSDave Chinner 		if (!xfs_perag_initialised_agf(pag)) {
12908d3e84fSDave Chinner 			err = xfs_alloc_read_agf(pag, NULL, 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)) &&
162*7ac2ff8bSDave Chinner 		    (!xfs_perag_prefers_metadata(pag) ||
163*7ac2ff8bSDave Chinner 		     !(flags & XFS_PICK_USERDATA) ||
1642a82b8beSDavid Chinner 		     (flags & XFS_PICK_LOWSPACE))) {
1652a82b8beSDavid Chinner 
1662a82b8beSDavid Chinner 			/* Break out, retaining the reference on the AG. */
1672a82b8beSDavid Chinner 			free = pag->pagf_freeblks;
1684196ac08SDave Chinner 			xfs_perag_put(pag);
1692a82b8beSDavid Chinner 			*agp = ag;
1702a82b8beSDavid Chinner 			break;
1712a82b8beSDavid Chinner 		}
1722a82b8beSDavid Chinner 
1732a82b8beSDavid Chinner 		/* Drop the reference on this AG, it's not usable. */
1742a82b8beSDavid Chinner 		xfs_filestream_put_ag(mp, ag);
1752a82b8beSDavid Chinner next_ag:
1764196ac08SDave Chinner 		xfs_perag_put(pag);
1772a82b8beSDavid Chinner 		/* Move to the next AG, wrapping to AG 0 if necessary. */
1782a82b8beSDavid Chinner 		if (++ag >= mp->m_sb.sb_agcount)
1792a82b8beSDavid Chinner 			ag = 0;
1802a82b8beSDavid Chinner 
1812a82b8beSDavid Chinner 		/* If a full pass of the AGs hasn't been done yet, continue. */
1822a82b8beSDavid Chinner 		if (ag != startag)
1832a82b8beSDavid Chinner 			continue;
1842a82b8beSDavid Chinner 
18576b47e52SDave Chinner 		/* Allow sleeping in xfs_alloc_read_agf() on the 2nd pass. */
1862a82b8beSDavid Chinner 		if (trylock != 0) {
1872a82b8beSDavid Chinner 			trylock = 0;
1882a82b8beSDavid Chinner 			continue;
1892a82b8beSDavid Chinner 		}
1902a82b8beSDavid Chinner 
1912a82b8beSDavid Chinner 		/* Finally, if lowspace wasn't set, set it for the 3rd pass. */
1922a82b8beSDavid Chinner 		if (!(flags & XFS_PICK_LOWSPACE)) {
1932a82b8beSDavid Chinner 			flags |= XFS_PICK_LOWSPACE;
1942a82b8beSDavid Chinner 			continue;
1952a82b8beSDavid Chinner 		}
1962a82b8beSDavid Chinner 
1972a82b8beSDavid Chinner 		/*
1982a82b8beSDavid Chinner 		 * Take the AG with the most free space, regardless of whether
1992a82b8beSDavid Chinner 		 * it's already in use by another filestream.
2002a82b8beSDavid Chinner 		 */
2012a82b8beSDavid Chinner 		if (max_ag != NULLAGNUMBER) {
2022a82b8beSDavid Chinner 			xfs_filestream_get_ag(mp, max_ag);
2032a82b8beSDavid Chinner 			free = maxfree;
2042a82b8beSDavid Chinner 			*agp = max_ag;
2052a82b8beSDavid Chinner 			break;
2062a82b8beSDavid Chinner 		}
2072a82b8beSDavid Chinner 
2082a82b8beSDavid Chinner 		/* take AG 0 if none matched */
209b94acd47SChristoph Hellwig 		trace_xfs_filestream_pick(ip, *agp, free, nscan);
2102a82b8beSDavid Chinner 		*agp = 0;
2112a82b8beSDavid Chinner 		return 0;
2122a82b8beSDavid Chinner 	}
2132a82b8beSDavid Chinner 
214b94acd47SChristoph Hellwig 	trace_xfs_filestream_pick(ip, *agp, free, nscan);
2152a82b8beSDavid Chinner 
2162cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
2172a82b8beSDavid Chinner 		return 0;
2182a82b8beSDavid Chinner 
2192451337dSDave Chinner 	err = -ENOMEM;
2201919addaSChristoph Hellwig 	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
2212a82b8beSDavid Chinner 	if (!item)
2222cd2ef6aSChristoph Hellwig 		goto out_put_ag;
2232a82b8beSDavid Chinner 
2242cd2ef6aSChristoph Hellwig 	item->ag = *agp;
2252a82b8beSDavid Chinner 
22622328d71SChristoph Hellwig 	err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
2272a82b8beSDavid Chinner 	if (err) {
2282451337dSDave Chinner 		if (err == -EEXIST)
2292cd2ef6aSChristoph Hellwig 			err = 0;
2302cd2ef6aSChristoph Hellwig 		goto out_free_item;
2312cd2ef6aSChristoph Hellwig 	}
2322cd2ef6aSChristoph Hellwig 
2332cd2ef6aSChristoph Hellwig 	return 0;
2342cd2ef6aSChristoph Hellwig 
2352cd2ef6aSChristoph Hellwig out_free_item:
2361919addaSChristoph Hellwig 	kmem_free(item);
2372cd2ef6aSChristoph Hellwig out_put_ag:
2382cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(mp, *agp);
2392a82b8beSDavid Chinner 	return err;
2402a82b8beSDavid Chinner }
2412a82b8beSDavid Chinner 
2422cd2ef6aSChristoph Hellwig static struct xfs_inode *
2432cd2ef6aSChristoph Hellwig xfs_filestream_get_parent(
2442cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2452a82b8beSDavid Chinner {
2462cd2ef6aSChristoph Hellwig 	struct inode		*inode = VFS_I(ip), *dir = NULL;
2472cd2ef6aSChristoph Hellwig 	struct dentry		*dentry, *parent;
2482a82b8beSDavid Chinner 
2492cd2ef6aSChristoph Hellwig 	dentry = d_find_alias(inode);
2502cd2ef6aSChristoph Hellwig 	if (!dentry)
2512cd2ef6aSChristoph Hellwig 		goto out;
2522a82b8beSDavid Chinner 
2532cd2ef6aSChristoph Hellwig 	parent = dget_parent(dentry);
2542cd2ef6aSChristoph Hellwig 	if (!parent)
2552cd2ef6aSChristoph Hellwig 		goto out_dput;
2562a82b8beSDavid Chinner 
2572b0143b5SDavid Howells 	dir = igrab(d_inode(parent));
2582cd2ef6aSChristoph Hellwig 	dput(parent);
2592a82b8beSDavid Chinner 
2602cd2ef6aSChristoph Hellwig out_dput:
2612cd2ef6aSChristoph Hellwig 	dput(dentry);
2622cd2ef6aSChristoph Hellwig out:
2632cd2ef6aSChristoph Hellwig 	return dir ? XFS_I(dir) : NULL;
2642a82b8beSDavid Chinner }
2652a82b8beSDavid Chinner 
2662a82b8beSDavid Chinner /*
2673b8d9076SChristoph Hellwig  * Find the right allocation group for a file, either by finding an
2683b8d9076SChristoph Hellwig  * existing file stream or creating a new one.
2693b8d9076SChristoph Hellwig  *
2703b8d9076SChristoph Hellwig  * Returns NULLAGNUMBER in case of an error.
2712a82b8beSDavid Chinner  */
2722a82b8beSDavid Chinner xfs_agnumber_t
2732a82b8beSDavid Chinner xfs_filestream_lookup_ag(
2742cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2752a82b8beSDavid Chinner {
27622328d71SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
2772cd2ef6aSChristoph Hellwig 	struct xfs_inode	*pip = NULL;
2783b8d9076SChristoph Hellwig 	xfs_agnumber_t		startag, ag = NULLAGNUMBER;
27922328d71SChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
2802a82b8beSDavid Chinner 
281c19b3b05SDave Chinner 	ASSERT(S_ISREG(VFS_I(ip)->i_mode));
2822a82b8beSDavid Chinner 
2832cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
2842cd2ef6aSChristoph Hellwig 	if (!pip)
285b26384dcSEric Sandeen 		return NULLAGNUMBER;
2862a82b8beSDavid Chinner 
2872cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
2883b8d9076SChristoph Hellwig 	if (mru) {
2893b8d9076SChristoph Hellwig 		ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
29022328d71SChristoph Hellwig 		xfs_mru_cache_done(mp->m_filestream);
2912a82b8beSDavid Chinner 
2927fcd3efaSChristoph Hellwig 		trace_xfs_filestream_lookup(mp, ip->i_ino, ag);
2933b8d9076SChristoph Hellwig 		goto out;
2942a82b8beSDavid Chinner 	}
2952a82b8beSDavid Chinner 
2962a82b8beSDavid Chinner 	/*
2972a82b8beSDavid Chinner 	 * Set the starting AG using the rotor for inode32, otherwise
2982a82b8beSDavid Chinner 	 * use the directory inode's AG.
2992a82b8beSDavid Chinner 	 */
3002e973b2cSDave Chinner 	if (xfs_is_inode32(mp)) {
3012cd2ef6aSChristoph Hellwig 		xfs_agnumber_t	 rotorstep = xfs_rotorstep;
3022a82b8beSDavid Chinner 		startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
3032a82b8beSDavid Chinner 		mp->m_agfrotor = (mp->m_agfrotor + 1) %
3042a82b8beSDavid Chinner 		                 (mp->m_sb.sb_agcount * rotorstep);
3052a82b8beSDavid Chinner 	} else
3062a82b8beSDavid Chinner 		startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
3072a82b8beSDavid Chinner 
3083b8d9076SChristoph Hellwig 	if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
3093b8d9076SChristoph Hellwig 		ag = NULLAGNUMBER;
3103b8d9076SChristoph Hellwig out:
31144a8736bSDarrick J. Wong 	xfs_irele(pip);
3123b8d9076SChristoph Hellwig 	return ag;
3132a82b8beSDavid Chinner }
3142a82b8beSDavid Chinner 
3152a82b8beSDavid Chinner /*
3162cd2ef6aSChristoph Hellwig  * Pick a new allocation group for the current file and its file stream.
3172cd2ef6aSChristoph Hellwig  *
3182cd2ef6aSChristoph Hellwig  * This is called when the allocator can't find a suitable extent in the
3192cd2ef6aSChristoph Hellwig  * current AG, and we have to move the stream into a new AG with more space.
3202a82b8beSDavid Chinner  */
3212a82b8beSDavid Chinner int
3222a82b8beSDavid Chinner xfs_filestream_new_ag(
32368988114SDave Chinner 	struct xfs_bmalloca	*ap,
3242a82b8beSDavid Chinner 	xfs_agnumber_t		*agp)
3252a82b8beSDavid Chinner {
3262cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip = ap->ip, *pip;
3272cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3282cd2ef6aSChristoph Hellwig 	xfs_extlen_t		minlen = ap->length;
3292cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		startag = 0;
330292378edSDave Chinner 	int			flags = 0;
331292378edSDave Chinner 	int			err = 0;
3322cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
3332a82b8beSDavid Chinner 
3342a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
3352a82b8beSDavid Chinner 
3362cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
3372cd2ef6aSChristoph Hellwig 	if (!pip)
3382cd2ef6aSChristoph Hellwig 		goto exit;
3392cd2ef6aSChristoph Hellwig 
3402cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
34122328d71SChristoph Hellwig 	if (mru) {
3422cd2ef6aSChristoph Hellwig 		struct xfs_fstrm_item *item =
3432cd2ef6aSChristoph Hellwig 			container_of(mru, struct xfs_fstrm_item, mru);
3442cd2ef6aSChristoph Hellwig 		startag = (item->ag + 1) % mp->m_sb.sb_agcount;
3452a82b8beSDavid Chinner 	}
3462a82b8beSDavid Chinner 
347c34d570dSChristoph Hellwig 	if (ap->datatype & XFS_ALLOC_USERDATA)
348292378edSDave Chinner 		flags |= XFS_PICK_USERDATA;
3491214f1cfSBrian Foster 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE)
350292378edSDave Chinner 		flags |= XFS_PICK_LOWSPACE;
3512a82b8beSDavid Chinner 
3522cd2ef6aSChristoph Hellwig 	err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
3532a82b8beSDavid Chinner 
3542a82b8beSDavid Chinner 	/*
3552cd2ef6aSChristoph Hellwig 	 * Only free the item here so we skip over the old AG earlier.
3562a82b8beSDavid Chinner 	 */
3572cd2ef6aSChristoph Hellwig 	if (mru)
3587fcd3efaSChristoph Hellwig 		xfs_fstrm_free_func(mp, mru);
3592a82b8beSDavid Chinner 
36044a8736bSDarrick J. Wong 	xfs_irele(pip);
3612a82b8beSDavid Chinner exit:
3622cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
3632a82b8beSDavid Chinner 		*agp = 0;
3642a82b8beSDavid Chinner 	return err;
3652a82b8beSDavid Chinner }
3662a82b8beSDavid Chinner 
3672a82b8beSDavid Chinner void
3682a82b8beSDavid Chinner xfs_filestream_deassociate(
3692cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
3702a82b8beSDavid Chinner {
37122328d71SChristoph Hellwig 	xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
3722a82b8beSDavid Chinner }
3732cd2ef6aSChristoph Hellwig 
3742cd2ef6aSChristoph Hellwig int
3752cd2ef6aSChristoph Hellwig xfs_filestream_mount(
3762cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
3772cd2ef6aSChristoph Hellwig {
3782cd2ef6aSChristoph Hellwig 	/*
3792cd2ef6aSChristoph Hellwig 	 * The filestream timer tunable is currently fixed within the range of
3802cd2ef6aSChristoph Hellwig 	 * one second to four minutes, with five seconds being the default.  The
3812cd2ef6aSChristoph Hellwig 	 * group count is somewhat arbitrary, but it'd be nice to adhere to the
3822cd2ef6aSChristoph Hellwig 	 * timer tunable to within about 10 percent.  This requires at least 10
3832cd2ef6aSChristoph Hellwig 	 * groups.
3842cd2ef6aSChristoph Hellwig 	 */
3857fcd3efaSChristoph Hellwig 	return xfs_mru_cache_create(&mp->m_filestream, mp,
3867fcd3efaSChristoph Hellwig 			xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func);
3872cd2ef6aSChristoph Hellwig }
3882cd2ef6aSChristoph Hellwig 
3892cd2ef6aSChristoph Hellwig void
3902cd2ef6aSChristoph Hellwig xfs_filestream_unmount(
3912cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
3922cd2ef6aSChristoph Hellwig {
3932cd2ef6aSChristoph Hellwig 	xfs_mru_cache_destroy(mp->m_filestream);
3942cd2ef6aSChristoph Hellwig }
395