xref: /openbmc/linux/fs/xfs/xfs_filestream.c (revision 05cf492a8d01f48d4b8d8f0b93f2d75de7349f12)
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);
127*05cf492aSDave Chinner 		longest = 0;
128*05cf492aSDave Chinner 		err = xfs_bmap_longest_free_extent(pag, NULL, &longest);
129f48e2df8SDarrick J. Wong 		if (err) {
1304196ac08SDave Chinner 			xfs_perag_put(pag);
131*05cf492aSDave Chinner 			if (err != -EAGAIN)
1322a82b8beSDavid Chinner 				return err;
133f48e2df8SDarrick J. Wong 			/* Couldn't lock the AGF, skip this AG. */
134f650df71SBrian Foster 			goto next_ag;
1352a82b8beSDavid Chinner 		}
1362a82b8beSDavid Chinner 
1372a82b8beSDavid Chinner 		/* Keep track of the AG with the most free blocks. */
1382a82b8beSDavid Chinner 		if (pag->pagf_freeblks > maxfree) {
1392a82b8beSDavid Chinner 			maxfree = pag->pagf_freeblks;
1402a82b8beSDavid Chinner 			max_ag = ag;
1412a82b8beSDavid Chinner 		}
1422a82b8beSDavid Chinner 
1432a82b8beSDavid Chinner 		/*
1442a82b8beSDavid Chinner 		 * The AG reference count does two things: it enforces mutual
1452a82b8beSDavid Chinner 		 * exclusion when examining the suitability of an AG in this
1462a82b8beSDavid Chinner 		 * loop, and it guards against two filestreams being established
1472a82b8beSDavid Chinner 		 * in the same AG as each other.
1482a82b8beSDavid Chinner 		 */
1492a82b8beSDavid Chinner 		if (xfs_filestream_get_ag(mp, ag) > 1) {
1502a82b8beSDavid Chinner 			xfs_filestream_put_ag(mp, ag);
1512a82b8beSDavid Chinner 			goto next_ag;
1522a82b8beSDavid Chinner 		}
1532a82b8beSDavid Chinner 
1542a82b8beSDavid Chinner 		if (((minlen && longest >= minlen) ||
1552a82b8beSDavid Chinner 		     (!minlen && pag->pagf_freeblks >= minfree)) &&
1567ac2ff8bSDave Chinner 		    (!xfs_perag_prefers_metadata(pag) ||
1577ac2ff8bSDave Chinner 		     !(flags & XFS_PICK_USERDATA) ||
1582a82b8beSDavid Chinner 		     (flags & XFS_PICK_LOWSPACE))) {
1592a82b8beSDavid Chinner 
1602a82b8beSDavid Chinner 			/* Break out, retaining the reference on the AG. */
1612a82b8beSDavid Chinner 			free = pag->pagf_freeblks;
1624196ac08SDave Chinner 			xfs_perag_put(pag);
1632a82b8beSDavid Chinner 			*agp = ag;
1642a82b8beSDavid Chinner 			break;
1652a82b8beSDavid Chinner 		}
1662a82b8beSDavid Chinner 
1672a82b8beSDavid Chinner 		/* Drop the reference on this AG, it's not usable. */
1682a82b8beSDavid Chinner 		xfs_filestream_put_ag(mp, ag);
1692a82b8beSDavid Chinner next_ag:
1704196ac08SDave Chinner 		xfs_perag_put(pag);
1712a82b8beSDavid Chinner 		/* Move to the next AG, wrapping to AG 0 if necessary. */
1722a82b8beSDavid Chinner 		if (++ag >= mp->m_sb.sb_agcount)
1732a82b8beSDavid Chinner 			ag = 0;
1742a82b8beSDavid Chinner 
1752a82b8beSDavid Chinner 		/* If a full pass of the AGs hasn't been done yet, continue. */
1762a82b8beSDavid Chinner 		if (ag != startag)
1772a82b8beSDavid Chinner 			continue;
1782a82b8beSDavid Chinner 
17976b47e52SDave Chinner 		/* Allow sleeping in xfs_alloc_read_agf() on the 2nd pass. */
1802a82b8beSDavid Chinner 		if (trylock != 0) {
1812a82b8beSDavid Chinner 			trylock = 0;
1822a82b8beSDavid Chinner 			continue;
1832a82b8beSDavid Chinner 		}
1842a82b8beSDavid Chinner 
1852a82b8beSDavid Chinner 		/* Finally, if lowspace wasn't set, set it for the 3rd pass. */
1862a82b8beSDavid Chinner 		if (!(flags & XFS_PICK_LOWSPACE)) {
1872a82b8beSDavid Chinner 			flags |= XFS_PICK_LOWSPACE;
1882a82b8beSDavid Chinner 			continue;
1892a82b8beSDavid Chinner 		}
1902a82b8beSDavid Chinner 
1912a82b8beSDavid Chinner 		/*
1922a82b8beSDavid Chinner 		 * Take the AG with the most free space, regardless of whether
1932a82b8beSDavid Chinner 		 * it's already in use by another filestream.
1942a82b8beSDavid Chinner 		 */
1952a82b8beSDavid Chinner 		if (max_ag != NULLAGNUMBER) {
1962a82b8beSDavid Chinner 			xfs_filestream_get_ag(mp, max_ag);
1972a82b8beSDavid Chinner 			free = maxfree;
1982a82b8beSDavid Chinner 			*agp = max_ag;
1992a82b8beSDavid Chinner 			break;
2002a82b8beSDavid Chinner 		}
2012a82b8beSDavid Chinner 
2022a82b8beSDavid Chinner 		/* take AG 0 if none matched */
203b94acd47SChristoph Hellwig 		trace_xfs_filestream_pick(ip, *agp, free, nscan);
2042a82b8beSDavid Chinner 		*agp = 0;
2052a82b8beSDavid Chinner 		return 0;
2062a82b8beSDavid Chinner 	}
2072a82b8beSDavid Chinner 
208b94acd47SChristoph Hellwig 	trace_xfs_filestream_pick(ip, *agp, free, nscan);
2092a82b8beSDavid Chinner 
2102cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
2112a82b8beSDavid Chinner 		return 0;
2122a82b8beSDavid Chinner 
2132451337dSDave Chinner 	err = -ENOMEM;
2141919addaSChristoph Hellwig 	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
2152a82b8beSDavid Chinner 	if (!item)
2162cd2ef6aSChristoph Hellwig 		goto out_put_ag;
2172a82b8beSDavid Chinner 
2182cd2ef6aSChristoph Hellwig 	item->ag = *agp;
2192a82b8beSDavid Chinner 
22022328d71SChristoph Hellwig 	err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
2212a82b8beSDavid Chinner 	if (err) {
2222451337dSDave Chinner 		if (err == -EEXIST)
2232cd2ef6aSChristoph Hellwig 			err = 0;
2242cd2ef6aSChristoph Hellwig 		goto out_free_item;
2252cd2ef6aSChristoph Hellwig 	}
2262cd2ef6aSChristoph Hellwig 
2272cd2ef6aSChristoph Hellwig 	return 0;
2282cd2ef6aSChristoph Hellwig 
2292cd2ef6aSChristoph Hellwig out_free_item:
2301919addaSChristoph Hellwig 	kmem_free(item);
2312cd2ef6aSChristoph Hellwig out_put_ag:
2322cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(mp, *agp);
2332a82b8beSDavid Chinner 	return err;
2342a82b8beSDavid Chinner }
2352a82b8beSDavid Chinner 
2362cd2ef6aSChristoph Hellwig static struct xfs_inode *
2372cd2ef6aSChristoph Hellwig xfs_filestream_get_parent(
2382cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2392a82b8beSDavid Chinner {
2402cd2ef6aSChristoph Hellwig 	struct inode		*inode = VFS_I(ip), *dir = NULL;
2412cd2ef6aSChristoph Hellwig 	struct dentry		*dentry, *parent;
2422a82b8beSDavid Chinner 
2432cd2ef6aSChristoph Hellwig 	dentry = d_find_alias(inode);
2442cd2ef6aSChristoph Hellwig 	if (!dentry)
2452cd2ef6aSChristoph Hellwig 		goto out;
2462a82b8beSDavid Chinner 
2472cd2ef6aSChristoph Hellwig 	parent = dget_parent(dentry);
2482cd2ef6aSChristoph Hellwig 	if (!parent)
2492cd2ef6aSChristoph Hellwig 		goto out_dput;
2502a82b8beSDavid Chinner 
2512b0143b5SDavid Howells 	dir = igrab(d_inode(parent));
2522cd2ef6aSChristoph Hellwig 	dput(parent);
2532a82b8beSDavid Chinner 
2542cd2ef6aSChristoph Hellwig out_dput:
2552cd2ef6aSChristoph Hellwig 	dput(dentry);
2562cd2ef6aSChristoph Hellwig out:
2572cd2ef6aSChristoph Hellwig 	return dir ? XFS_I(dir) : NULL;
2582a82b8beSDavid Chinner }
2592a82b8beSDavid Chinner 
2602a82b8beSDavid Chinner /*
2613b8d9076SChristoph Hellwig  * Find the right allocation group for a file, either by finding an
2623b8d9076SChristoph Hellwig  * existing file stream or creating a new one.
2633b8d9076SChristoph Hellwig  *
2643b8d9076SChristoph Hellwig  * Returns NULLAGNUMBER in case of an error.
2652a82b8beSDavid Chinner  */
2662a82b8beSDavid Chinner xfs_agnumber_t
2672a82b8beSDavid Chinner xfs_filestream_lookup_ag(
2682cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2692a82b8beSDavid Chinner {
27022328d71SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
2712cd2ef6aSChristoph Hellwig 	struct xfs_inode	*pip = NULL;
2723b8d9076SChristoph Hellwig 	xfs_agnumber_t		startag, ag = NULLAGNUMBER;
27322328d71SChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
2742a82b8beSDavid Chinner 
275c19b3b05SDave Chinner 	ASSERT(S_ISREG(VFS_I(ip)->i_mode));
2762a82b8beSDavid Chinner 
2772cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
2782cd2ef6aSChristoph Hellwig 	if (!pip)
279b26384dcSEric Sandeen 		return NULLAGNUMBER;
2802a82b8beSDavid Chinner 
2812cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
2823b8d9076SChristoph Hellwig 	if (mru) {
2833b8d9076SChristoph Hellwig 		ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
28422328d71SChristoph Hellwig 		xfs_mru_cache_done(mp->m_filestream);
2852a82b8beSDavid Chinner 
2867fcd3efaSChristoph Hellwig 		trace_xfs_filestream_lookup(mp, ip->i_ino, ag);
2873b8d9076SChristoph Hellwig 		goto out;
2882a82b8beSDavid Chinner 	}
2892a82b8beSDavid Chinner 
2902a82b8beSDavid Chinner 	/*
2912a82b8beSDavid Chinner 	 * Set the starting AG using the rotor for inode32, otherwise
2922a82b8beSDavid Chinner 	 * use the directory inode's AG.
2932a82b8beSDavid Chinner 	 */
2942e973b2cSDave Chinner 	if (xfs_is_inode32(mp)) {
2952cd2ef6aSChristoph Hellwig 		xfs_agnumber_t	 rotorstep = xfs_rotorstep;
2962a82b8beSDavid Chinner 		startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
2972a82b8beSDavid Chinner 		mp->m_agfrotor = (mp->m_agfrotor + 1) %
2982a82b8beSDavid Chinner 		                 (mp->m_sb.sb_agcount * rotorstep);
2992a82b8beSDavid Chinner 	} else
3002a82b8beSDavid Chinner 		startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
3012a82b8beSDavid Chinner 
3023b8d9076SChristoph Hellwig 	if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
3033b8d9076SChristoph Hellwig 		ag = NULLAGNUMBER;
3043b8d9076SChristoph Hellwig out:
30544a8736bSDarrick J. Wong 	xfs_irele(pip);
3063b8d9076SChristoph Hellwig 	return ag;
3072a82b8beSDavid Chinner }
3082a82b8beSDavid Chinner 
3092a82b8beSDavid Chinner /*
3102cd2ef6aSChristoph Hellwig  * Pick a new allocation group for the current file and its file stream.
3112cd2ef6aSChristoph Hellwig  *
3122cd2ef6aSChristoph Hellwig  * This is called when the allocator can't find a suitable extent in the
3132cd2ef6aSChristoph Hellwig  * current AG, and we have to move the stream into a new AG with more space.
3142a82b8beSDavid Chinner  */
3152a82b8beSDavid Chinner int
3162a82b8beSDavid Chinner xfs_filestream_new_ag(
31768988114SDave Chinner 	struct xfs_bmalloca	*ap,
3182a82b8beSDavid Chinner 	xfs_agnumber_t		*agp)
3192a82b8beSDavid Chinner {
3202cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip = ap->ip, *pip;
3212cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3222cd2ef6aSChristoph Hellwig 	xfs_extlen_t		minlen = ap->length;
3232cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		startag = 0;
324292378edSDave Chinner 	int			flags = 0;
325292378edSDave Chinner 	int			err = 0;
3262cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
3272a82b8beSDavid Chinner 
3282a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
3292a82b8beSDavid Chinner 
3302cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
3312cd2ef6aSChristoph Hellwig 	if (!pip)
3322cd2ef6aSChristoph Hellwig 		goto exit;
3332cd2ef6aSChristoph Hellwig 
3342cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
33522328d71SChristoph Hellwig 	if (mru) {
3362cd2ef6aSChristoph Hellwig 		struct xfs_fstrm_item *item =
3372cd2ef6aSChristoph Hellwig 			container_of(mru, struct xfs_fstrm_item, mru);
3382cd2ef6aSChristoph Hellwig 		startag = (item->ag + 1) % mp->m_sb.sb_agcount;
3392a82b8beSDavid Chinner 	}
3402a82b8beSDavid Chinner 
341c34d570dSChristoph Hellwig 	if (ap->datatype & XFS_ALLOC_USERDATA)
342292378edSDave Chinner 		flags |= XFS_PICK_USERDATA;
3431214f1cfSBrian Foster 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE)
344292378edSDave Chinner 		flags |= XFS_PICK_LOWSPACE;
3452a82b8beSDavid Chinner 
3462cd2ef6aSChristoph Hellwig 	err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
3472a82b8beSDavid Chinner 
3482a82b8beSDavid Chinner 	/*
3492cd2ef6aSChristoph Hellwig 	 * Only free the item here so we skip over the old AG earlier.
3502a82b8beSDavid Chinner 	 */
3512cd2ef6aSChristoph Hellwig 	if (mru)
3527fcd3efaSChristoph Hellwig 		xfs_fstrm_free_func(mp, mru);
3532a82b8beSDavid Chinner 
35444a8736bSDarrick J. Wong 	xfs_irele(pip);
3552a82b8beSDavid Chinner exit:
3562cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
3572a82b8beSDavid Chinner 		*agp = 0;
3582a82b8beSDavid Chinner 	return err;
3592a82b8beSDavid Chinner }
3602a82b8beSDavid Chinner 
3612a82b8beSDavid Chinner void
3622a82b8beSDavid Chinner xfs_filestream_deassociate(
3632cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
3642a82b8beSDavid Chinner {
36522328d71SChristoph Hellwig 	xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
3662a82b8beSDavid Chinner }
3672cd2ef6aSChristoph Hellwig 
3682cd2ef6aSChristoph Hellwig int
3692cd2ef6aSChristoph Hellwig xfs_filestream_mount(
3702cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
3712cd2ef6aSChristoph Hellwig {
3722cd2ef6aSChristoph Hellwig 	/*
3732cd2ef6aSChristoph Hellwig 	 * The filestream timer tunable is currently fixed within the range of
3742cd2ef6aSChristoph Hellwig 	 * one second to four minutes, with five seconds being the default.  The
3752cd2ef6aSChristoph Hellwig 	 * group count is somewhat arbitrary, but it'd be nice to adhere to the
3762cd2ef6aSChristoph Hellwig 	 * timer tunable to within about 10 percent.  This requires at least 10
3772cd2ef6aSChristoph Hellwig 	 * groups.
3782cd2ef6aSChristoph Hellwig 	 */
3797fcd3efaSChristoph Hellwig 	return xfs_mru_cache_create(&mp->m_filestream, mp,
3807fcd3efaSChristoph Hellwig 			xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func);
3812cd2ef6aSChristoph Hellwig }
3822cd2ef6aSChristoph Hellwig 
3832cd2ef6aSChristoph Hellwig void
3842cd2ef6aSChristoph Hellwig xfs_filestream_unmount(
3852cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
3862cd2ef6aSChristoph Hellwig {
3872cd2ef6aSChristoph Hellwig 	xfs_mru_cache_destroy(mp->m_filestream);
3882cd2ef6aSChristoph Hellwig }
389