xref: /openbmc/linux/fs/xfs/xfs_filestream.c (revision 3b8d90766a85e079fefaee74ca9dde43ce75edea)
12a82b8beSDavid Chinner /*
22a82b8beSDavid Chinner  * Copyright (c) 2006-2007 Silicon Graphics, Inc.
32cd2ef6aSChristoph Hellwig  * Copyright (c) 2014 Christoph Hellwig.
42a82b8beSDavid Chinner  * All Rights Reserved.
52a82b8beSDavid Chinner  *
62a82b8beSDavid Chinner  * This program is free software; you can redistribute it and/or
72a82b8beSDavid Chinner  * modify it under the terms of the GNU General Public License as
82a82b8beSDavid Chinner  * published by the Free Software Foundation.
92a82b8beSDavid Chinner  *
102a82b8beSDavid Chinner  * This program is distributed in the hope that it would be useful,
112a82b8beSDavid Chinner  * but WITHOUT ANY WARRANTY; without even the implied warranty of
122a82b8beSDavid Chinner  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132a82b8beSDavid Chinner  * GNU General Public License for more details.
142a82b8beSDavid Chinner  *
152a82b8beSDavid Chinner  * You should have received a copy of the GNU General Public License
162a82b8beSDavid Chinner  * along with this program; if not, write the Free Software Foundation,
172a82b8beSDavid Chinner  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
182a82b8beSDavid Chinner  */
192a82b8beSDavid Chinner #include "xfs.h"
20a4fbe6abSDave Chinner #include "xfs_format.h"
21239880efSDave Chinner #include "xfs_log_format.h"
22239880efSDave Chinner #include "xfs_trans_resv.h"
23239880efSDave Chinner #include "xfs_ag.h"
24239880efSDave Chinner #include "xfs_sb.h"
25239880efSDave Chinner #include "xfs_mount.h"
262a82b8beSDavid Chinner #include "xfs_inum.h"
272a82b8beSDavid Chinner #include "xfs_inode.h"
282a82b8beSDavid Chinner #include "xfs_bmap.h"
2968988114SDave Chinner #include "xfs_bmap_util.h"
302a82b8beSDavid Chinner #include "xfs_alloc.h"
312a82b8beSDavid Chinner #include "xfs_mru_cache.h"
32a4fbe6abSDave Chinner #include "xfs_dinode.h"
332a82b8beSDavid Chinner #include "xfs_filestream.h"
340b1b213fSChristoph Hellwig #include "xfs_trace.h"
352a82b8beSDavid Chinner 
362a82b8beSDavid Chinner #define TRACE_AG_SCAN(mp, ag, ag2)
372a82b8beSDavid Chinner #define TRACE_AG_PICK1(mp, max_ag, maxfree)
382a82b8beSDavid Chinner #define TRACE_AG_PICK2(mp, ag, ag2, cnt, free, scan, flag)
392a82b8beSDavid Chinner #define TRACE_FREE(mp, ip, pip, ag, cnt)
402a82b8beSDavid Chinner #define TRACE_LOOKUP(mp, ip, pip, ag, cnt)
412a82b8beSDavid Chinner 
422cd2ef6aSChristoph Hellwig struct xfs_fstrm_item {
4322328d71SChristoph Hellwig 	struct xfs_mru_cache_elem	mru;
442cd2ef6aSChristoph Hellwig 	struct xfs_inode		*ip;
452cd2ef6aSChristoph Hellwig 	xfs_agnumber_t			ag; /* AG in use for this directory */
462cd2ef6aSChristoph Hellwig };
472cd2ef6aSChristoph Hellwig 
482cd2ef6aSChristoph Hellwig enum xfs_fstrm_alloc {
492cd2ef6aSChristoph Hellwig 	XFS_PICK_USERDATA = 1,
502cd2ef6aSChristoph Hellwig 	XFS_PICK_LOWSPACE = 2,
512cd2ef6aSChristoph Hellwig };
522a82b8beSDavid Chinner 
530664ce8dSChristoph Hellwig /*
540664ce8dSChristoph Hellwig  * Allocation group filestream associations are tracked with per-ag atomic
552cd2ef6aSChristoph Hellwig  * counters.  These counters allow xfs_filestream_pick_ag() to tell whether a
560664ce8dSChristoph Hellwig  * particular AG already has active filestreams associated with it. The mount
570664ce8dSChristoph Hellwig  * point's m_peraglock is used to protect these counters from per-ag array
580664ce8dSChristoph Hellwig  * re-allocation during a growfs operation.  When xfs_growfs_data_private() is
590664ce8dSChristoph Hellwig  * about to reallocate the array, it calls xfs_filestream_flush() with the
600664ce8dSChristoph Hellwig  * m_peraglock held in write mode.
610664ce8dSChristoph Hellwig  *
620664ce8dSChristoph Hellwig  * Since xfs_mru_cache_flush() guarantees that all the free functions for all
630664ce8dSChristoph Hellwig  * the cache elements have finished executing before it returns, it's safe for
640664ce8dSChristoph Hellwig  * the free functions to use the atomic counters without m_peraglock protection.
650664ce8dSChristoph Hellwig  * This allows the implementation of xfs_fstrm_free_func() to be agnostic about
660664ce8dSChristoph Hellwig  * whether it was called with the m_peraglock held in read mode, write mode or
670664ce8dSChristoph Hellwig  * not held at all.  The race condition this addresses is the following:
680664ce8dSChristoph Hellwig  *
690664ce8dSChristoph Hellwig  *  - The work queue scheduler fires and pulls a filestream directory cache
700664ce8dSChristoph Hellwig  *    element off the LRU end of the cache for deletion, then gets pre-empted.
710664ce8dSChristoph Hellwig  *  - A growfs operation grabs the m_peraglock in write mode, flushes all the
720664ce8dSChristoph Hellwig  *    remaining items from the cache and reallocates the mount point's per-ag
730664ce8dSChristoph Hellwig  *    array, resetting all the counters to zero.
740664ce8dSChristoph Hellwig  *  - The work queue thread resumes and calls the free function for the element
750664ce8dSChristoph Hellwig  *    it started cleaning up earlier.  In the process it decrements the
760664ce8dSChristoph Hellwig  *    filestreams counter for an AG that now has no references.
770664ce8dSChristoph Hellwig  *
780664ce8dSChristoph Hellwig  * With a shrinkfs feature, the above scenario could panic the system.
790664ce8dSChristoph Hellwig  *
800664ce8dSChristoph Hellwig  * All other uses of the following macros should be protected by either the
810664ce8dSChristoph Hellwig  * m_peraglock held in read mode, or the cache's internal locking exposed by the
820664ce8dSChristoph Hellwig  * interval between a call to xfs_mru_cache_lookup() and a call to
830664ce8dSChristoph Hellwig  * xfs_mru_cache_done().  In addition, the m_peraglock must be held in read mode
840664ce8dSChristoph Hellwig  * when new elements are added to the cache.
850664ce8dSChristoph Hellwig  *
860664ce8dSChristoph Hellwig  * Combined, these locking rules ensure that no associations will ever exist in
870664ce8dSChristoph Hellwig  * the cache that reference per-ag array elements that have since been
880664ce8dSChristoph Hellwig  * reallocated.
890664ce8dSChristoph Hellwig  */
900664ce8dSChristoph Hellwig static int
910664ce8dSChristoph Hellwig xfs_filestream_peek_ag(
920664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
930664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
940664ce8dSChristoph Hellwig {
950664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
960664ce8dSChristoph Hellwig 	int		ret;
970664ce8dSChristoph Hellwig 
980664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
990664ce8dSChristoph Hellwig 	ret = atomic_read(&pag->pagf_fstrms);
1000664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
1010664ce8dSChristoph Hellwig 	return ret;
1020664ce8dSChristoph Hellwig }
1030664ce8dSChristoph Hellwig 
1040664ce8dSChristoph Hellwig static int
1050664ce8dSChristoph Hellwig xfs_filestream_get_ag(
1060664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
1070664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
1080664ce8dSChristoph Hellwig {
1090664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
1100664ce8dSChristoph Hellwig 	int		ret;
1110664ce8dSChristoph Hellwig 
1120664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
1130664ce8dSChristoph Hellwig 	ret = atomic_inc_return(&pag->pagf_fstrms);
1140664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
1150664ce8dSChristoph Hellwig 	return ret;
1160664ce8dSChristoph Hellwig }
1170664ce8dSChristoph Hellwig 
1180664ce8dSChristoph Hellwig static void
1190664ce8dSChristoph Hellwig xfs_filestream_put_ag(
1200664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
1210664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
1220664ce8dSChristoph Hellwig {
1230664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
1240664ce8dSChristoph Hellwig 
1250664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
1260664ce8dSChristoph Hellwig 	atomic_dec(&pag->pagf_fstrms);
1270664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
1280664ce8dSChristoph Hellwig }
1292a82b8beSDavid Chinner 
1302cd2ef6aSChristoph Hellwig static void
1312cd2ef6aSChristoph Hellwig xfs_fstrm_free_func(
1322cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru)
1332cd2ef6aSChristoph Hellwig {
1342cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item =
1352cd2ef6aSChristoph Hellwig 		container_of(mru, struct xfs_fstrm_item, mru);
1362cd2ef6aSChristoph Hellwig 
1372cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(item->ip->i_mount, item->ag);
1382cd2ef6aSChristoph Hellwig 
1392cd2ef6aSChristoph Hellwig 	TRACE_FREE(mp, ip, NULL, item->ag,
1402cd2ef6aSChristoph Hellwig 		   xfs_filestream_peek_ag(mp, item->ag));
1412cd2ef6aSChristoph Hellwig 
1421919addaSChristoph Hellwig 	kmem_free(item);
1432cd2ef6aSChristoph Hellwig }
1442cd2ef6aSChristoph Hellwig 
1452a82b8beSDavid Chinner /*
1462a82b8beSDavid Chinner  * Scan the AGs starting at startag looking for an AG that isn't in use and has
1472a82b8beSDavid Chinner  * at least minlen blocks free.
1482a82b8beSDavid Chinner  */
1492a82b8beSDavid Chinner static int
1502cd2ef6aSChristoph Hellwig xfs_filestream_pick_ag(
1512cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip,
1522a82b8beSDavid Chinner 	xfs_agnumber_t		startag,
1532a82b8beSDavid Chinner 	xfs_agnumber_t		*agp,
1542a82b8beSDavid Chinner 	int			flags,
1552a82b8beSDavid Chinner 	xfs_extlen_t		minlen)
1562a82b8beSDavid Chinner {
1572cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
1582cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item;
1592cd2ef6aSChristoph Hellwig 	struct xfs_perag	*pag;
1606cc87645SDave Chinner 	xfs_extlen_t		longest, free, minfree, maxfree = 0;
1612a82b8beSDavid Chinner 	xfs_agnumber_t		ag, max_ag = NULLAGNUMBER;
1622cd2ef6aSChristoph Hellwig 	int			streams, max_streams;
1632cd2ef6aSChristoph Hellwig 	int			err, trylock, nscan;
1642cd2ef6aSChristoph Hellwig 
1652cd2ef6aSChristoph Hellwig 	ASSERT(S_ISDIR(ip->i_d.di_mode));
1662a82b8beSDavid Chinner 
1672a82b8beSDavid Chinner 	/* 2% of an AG's blocks must be free for it to be chosen. */
1682a82b8beSDavid Chinner 	minfree = mp->m_sb.sb_agblocks / 50;
1692a82b8beSDavid Chinner 
1702a82b8beSDavid Chinner 	ag = startag;
1712a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
1722a82b8beSDavid Chinner 
1732a82b8beSDavid Chinner 	/* For the first pass, don't sleep trying to init the per-AG. */
1742a82b8beSDavid Chinner 	trylock = XFS_ALLOC_FLAG_TRYLOCK;
1752a82b8beSDavid Chinner 
1762a82b8beSDavid Chinner 	for (nscan = 0; 1; nscan++) {
1774196ac08SDave Chinner 		pag = xfs_perag_get(mp, ag);
1784196ac08SDave Chinner 		TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms));
1792a82b8beSDavid Chinner 
1802a82b8beSDavid Chinner 		if (!pag->pagf_init) {
1812a82b8beSDavid Chinner 			err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
1824196ac08SDave Chinner 			if (err && !trylock) {
1834196ac08SDave Chinner 				xfs_perag_put(pag);
1842a82b8beSDavid Chinner 				return err;
1852a82b8beSDavid Chinner 			}
1864196ac08SDave Chinner 		}
1872a82b8beSDavid Chinner 
1882a82b8beSDavid Chinner 		/* Might fail sometimes during the 1st pass with trylock set. */
1892a82b8beSDavid Chinner 		if (!pag->pagf_init)
1902a82b8beSDavid Chinner 			goto next_ag;
1912a82b8beSDavid Chinner 
1922a82b8beSDavid Chinner 		/* Keep track of the AG with the most free blocks. */
1932a82b8beSDavid Chinner 		if (pag->pagf_freeblks > maxfree) {
1942a82b8beSDavid Chinner 			maxfree = pag->pagf_freeblks;
1954196ac08SDave Chinner 			max_streams = atomic_read(&pag->pagf_fstrms);
1962a82b8beSDavid Chinner 			max_ag = ag;
1972a82b8beSDavid Chinner 		}
1982a82b8beSDavid Chinner 
1992a82b8beSDavid Chinner 		/*
2002a82b8beSDavid Chinner 		 * The AG reference count does two things: it enforces mutual
2012a82b8beSDavid Chinner 		 * exclusion when examining the suitability of an AG in this
2022a82b8beSDavid Chinner 		 * loop, and it guards against two filestreams being established
2032a82b8beSDavid Chinner 		 * in the same AG as each other.
2042a82b8beSDavid Chinner 		 */
2052a82b8beSDavid Chinner 		if (xfs_filestream_get_ag(mp, ag) > 1) {
2062a82b8beSDavid Chinner 			xfs_filestream_put_ag(mp, ag);
2072a82b8beSDavid Chinner 			goto next_ag;
2082a82b8beSDavid Chinner 		}
2092a82b8beSDavid Chinner 
2106cc87645SDave Chinner 		longest = xfs_alloc_longest_free_extent(mp, pag);
2112a82b8beSDavid Chinner 		if (((minlen && longest >= minlen) ||
2122a82b8beSDavid Chinner 		     (!minlen && pag->pagf_freeblks >= minfree)) &&
2132a82b8beSDavid Chinner 		    (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
2142a82b8beSDavid Chinner 		     (flags & XFS_PICK_LOWSPACE))) {
2152a82b8beSDavid Chinner 
2162a82b8beSDavid Chinner 			/* Break out, retaining the reference on the AG. */
2172a82b8beSDavid Chinner 			free = pag->pagf_freeblks;
2184196ac08SDave Chinner 			streams = atomic_read(&pag->pagf_fstrms);
2194196ac08SDave Chinner 			xfs_perag_put(pag);
2202a82b8beSDavid Chinner 			*agp = ag;
2212a82b8beSDavid Chinner 			break;
2222a82b8beSDavid Chinner 		}
2232a82b8beSDavid Chinner 
2242a82b8beSDavid Chinner 		/* Drop the reference on this AG, it's not usable. */
2252a82b8beSDavid Chinner 		xfs_filestream_put_ag(mp, ag);
2262a82b8beSDavid Chinner next_ag:
2274196ac08SDave Chinner 		xfs_perag_put(pag);
2282a82b8beSDavid Chinner 		/* Move to the next AG, wrapping to AG 0 if necessary. */
2292a82b8beSDavid Chinner 		if (++ag >= mp->m_sb.sb_agcount)
2302a82b8beSDavid Chinner 			ag = 0;
2312a82b8beSDavid Chinner 
2322a82b8beSDavid Chinner 		/* If a full pass of the AGs hasn't been done yet, continue. */
2332a82b8beSDavid Chinner 		if (ag != startag)
2342a82b8beSDavid Chinner 			continue;
2352a82b8beSDavid Chinner 
2362a82b8beSDavid Chinner 		/* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */
2372a82b8beSDavid Chinner 		if (trylock != 0) {
2382a82b8beSDavid Chinner 			trylock = 0;
2392a82b8beSDavid Chinner 			continue;
2402a82b8beSDavid Chinner 		}
2412a82b8beSDavid Chinner 
2422a82b8beSDavid Chinner 		/* Finally, if lowspace wasn't set, set it for the 3rd pass. */
2432a82b8beSDavid Chinner 		if (!(flags & XFS_PICK_LOWSPACE)) {
2442a82b8beSDavid Chinner 			flags |= XFS_PICK_LOWSPACE;
2452a82b8beSDavid Chinner 			continue;
2462a82b8beSDavid Chinner 		}
2472a82b8beSDavid Chinner 
2482a82b8beSDavid Chinner 		/*
2492a82b8beSDavid Chinner 		 * Take the AG with the most free space, regardless of whether
2502a82b8beSDavid Chinner 		 * it's already in use by another filestream.
2512a82b8beSDavid Chinner 		 */
2522a82b8beSDavid Chinner 		if (max_ag != NULLAGNUMBER) {
2532a82b8beSDavid Chinner 			xfs_filestream_get_ag(mp, max_ag);
2542a82b8beSDavid Chinner 			TRACE_AG_PICK1(mp, max_ag, maxfree);
2554196ac08SDave Chinner 			streams = max_streams;
2562a82b8beSDavid Chinner 			free = maxfree;
2572a82b8beSDavid Chinner 			*agp = max_ag;
2582a82b8beSDavid Chinner 			break;
2592a82b8beSDavid Chinner 		}
2602a82b8beSDavid Chinner 
2612a82b8beSDavid Chinner 		/* take AG 0 if none matched */
2622a82b8beSDavid Chinner 		TRACE_AG_PICK1(mp, max_ag, maxfree);
2632a82b8beSDavid Chinner 		*agp = 0;
2642a82b8beSDavid Chinner 		return 0;
2652a82b8beSDavid Chinner 	}
2662a82b8beSDavid Chinner 
2674196ac08SDave Chinner 	TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags);
2682a82b8beSDavid Chinner 
2692cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
2702a82b8beSDavid Chinner 		return 0;
2712a82b8beSDavid Chinner 
2722cd2ef6aSChristoph Hellwig 	err = ENOMEM;
2731919addaSChristoph Hellwig 	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
2742a82b8beSDavid Chinner 	if (!item)
2752cd2ef6aSChristoph Hellwig 		goto out_put_ag;
2762a82b8beSDavid Chinner 
2772cd2ef6aSChristoph Hellwig 	item->ag = *agp;
2782a82b8beSDavid Chinner 	item->ip = ip;
2792a82b8beSDavid Chinner 
28022328d71SChristoph Hellwig 	err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
2812a82b8beSDavid Chinner 	if (err) {
2822cd2ef6aSChristoph Hellwig 		if (err == EEXIST)
2832cd2ef6aSChristoph Hellwig 			err = 0;
2842cd2ef6aSChristoph Hellwig 		goto out_free_item;
2852cd2ef6aSChristoph Hellwig 	}
2862cd2ef6aSChristoph Hellwig 
2872cd2ef6aSChristoph Hellwig 	return 0;
2882cd2ef6aSChristoph Hellwig 
2892cd2ef6aSChristoph Hellwig out_free_item:
2901919addaSChristoph Hellwig 	kmem_free(item);
2912cd2ef6aSChristoph Hellwig out_put_ag:
2922cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(mp, *agp);
2932a82b8beSDavid Chinner 	return err;
2942a82b8beSDavid Chinner }
2952a82b8beSDavid Chinner 
2962cd2ef6aSChristoph Hellwig static struct xfs_inode *
2972cd2ef6aSChristoph Hellwig xfs_filestream_get_parent(
2982cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
2992a82b8beSDavid Chinner {
3002cd2ef6aSChristoph Hellwig 	struct inode		*inode = VFS_I(ip), *dir = NULL;
3012cd2ef6aSChristoph Hellwig 	struct dentry		*dentry, *parent;
3022a82b8beSDavid Chinner 
3032cd2ef6aSChristoph Hellwig 	dentry = d_find_alias(inode);
3042cd2ef6aSChristoph Hellwig 	if (!dentry)
3052cd2ef6aSChristoph Hellwig 		goto out;
3062a82b8beSDavid Chinner 
3072cd2ef6aSChristoph Hellwig 	parent = dget_parent(dentry);
3082cd2ef6aSChristoph Hellwig 	if (!parent)
3092cd2ef6aSChristoph Hellwig 		goto out_dput;
3102a82b8beSDavid Chinner 
3112cd2ef6aSChristoph Hellwig 	dir = igrab(parent->d_inode);
3122cd2ef6aSChristoph Hellwig 	dput(parent);
3132a82b8beSDavid Chinner 
3142cd2ef6aSChristoph Hellwig out_dput:
3152cd2ef6aSChristoph Hellwig 	dput(dentry);
3162cd2ef6aSChristoph Hellwig out:
3172cd2ef6aSChristoph Hellwig 	return dir ? XFS_I(dir) : NULL;
3182a82b8beSDavid Chinner }
3192a82b8beSDavid Chinner 
3202a82b8beSDavid Chinner /*
321*3b8d9076SChristoph Hellwig  * Find the right allocation group for a file, either by finding an
322*3b8d9076SChristoph Hellwig  * existing file stream or creating a new one.
323*3b8d9076SChristoph Hellwig  *
324*3b8d9076SChristoph Hellwig  * Returns NULLAGNUMBER in case of an error.
3252a82b8beSDavid Chinner  */
3262a82b8beSDavid Chinner xfs_agnumber_t
3272a82b8beSDavid Chinner xfs_filestream_lookup_ag(
3282cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
3292a82b8beSDavid Chinner {
33022328d71SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3312cd2ef6aSChristoph Hellwig 	struct xfs_inode	*pip = NULL;
332*3b8d9076SChristoph Hellwig 	xfs_agnumber_t		startag, ag = NULLAGNUMBER;
3332cd2ef6aSChristoph Hellwig 	int			ref = 0;
33422328d71SChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
3352a82b8beSDavid Chinner 
3362cd2ef6aSChristoph Hellwig 	ASSERT(S_ISREG(ip->i_d.di_mode));
3372a82b8beSDavid Chinner 
3382cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
3392cd2ef6aSChristoph Hellwig 	if (!pip)
3402cd2ef6aSChristoph Hellwig 		goto out;
3412a82b8beSDavid Chinner 
3422cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
343*3b8d9076SChristoph Hellwig 	if (mru) {
344*3b8d9076SChristoph Hellwig 		ag = container_of(mru, struct xfs_fstrm_item, mru)->ag;
34522328d71SChristoph Hellwig 		xfs_mru_cache_done(mp->m_filestream);
3462a82b8beSDavid Chinner 
3472cd2ef6aSChristoph Hellwig 		ref = xfs_filestream_peek_ag(ip->i_mount, ag);
3482cd2ef6aSChristoph Hellwig 		TRACE_LOOKUP(mp, ip, pip, ag, ref);
349*3b8d9076SChristoph Hellwig 		goto out;
3502a82b8beSDavid Chinner 	}
3512a82b8beSDavid Chinner 
3522a82b8beSDavid Chinner 	/*
3532a82b8beSDavid Chinner 	 * Set the starting AG using the rotor for inode32, otherwise
3542a82b8beSDavid Chinner 	 * use the directory inode's AG.
3552a82b8beSDavid Chinner 	 */
3562a82b8beSDavid Chinner 	if (mp->m_flags & XFS_MOUNT_32BITINODES) {
3572cd2ef6aSChristoph Hellwig 		xfs_agnumber_t	 rotorstep = xfs_rotorstep;
3582a82b8beSDavid Chinner 		startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
3592a82b8beSDavid Chinner 		mp->m_agfrotor = (mp->m_agfrotor + 1) %
3602a82b8beSDavid Chinner 		                 (mp->m_sb.sb_agcount * rotorstep);
3612a82b8beSDavid Chinner 	} else
3622a82b8beSDavid Chinner 		startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
3632a82b8beSDavid Chinner 
364*3b8d9076SChristoph Hellwig 	if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0))
365*3b8d9076SChristoph Hellwig 		ag = NULLAGNUMBER;
366*3b8d9076SChristoph Hellwig out:
367*3b8d9076SChristoph Hellwig 	IRELE(pip);
368*3b8d9076SChristoph Hellwig 	return ag;
3692a82b8beSDavid Chinner }
3702a82b8beSDavid Chinner 
3712a82b8beSDavid Chinner /*
3722cd2ef6aSChristoph Hellwig  * Pick a new allocation group for the current file and its file stream.
3732cd2ef6aSChristoph Hellwig  *
3742cd2ef6aSChristoph Hellwig  * This is called when the allocator can't find a suitable extent in the
3752cd2ef6aSChristoph Hellwig  * current AG, and we have to move the stream into a new AG with more space.
3762a82b8beSDavid Chinner  */
3772a82b8beSDavid Chinner int
3782a82b8beSDavid Chinner xfs_filestream_new_ag(
37968988114SDave Chinner 	struct xfs_bmalloca	*ap,
3802a82b8beSDavid Chinner 	xfs_agnumber_t		*agp)
3812a82b8beSDavid Chinner {
3822cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip = ap->ip, *pip;
3832cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3842cd2ef6aSChristoph Hellwig 	xfs_extlen_t		minlen = ap->length;
3852cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		startag = 0;
3862cd2ef6aSChristoph Hellwig 	int			flags, err = 0;
3872cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
3882a82b8beSDavid Chinner 
3892a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
3902a82b8beSDavid Chinner 
3912cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
3922cd2ef6aSChristoph Hellwig 	if (!pip)
3932cd2ef6aSChristoph Hellwig 		goto exit;
3942cd2ef6aSChristoph Hellwig 
3952cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
39622328d71SChristoph Hellwig 	if (mru) {
3972cd2ef6aSChristoph Hellwig 		struct xfs_fstrm_item *item =
3982cd2ef6aSChristoph Hellwig 			container_of(mru, struct xfs_fstrm_item, mru);
3992cd2ef6aSChristoph Hellwig 		startag = (item->ag + 1) % mp->m_sb.sb_agcount;
4002a82b8beSDavid Chinner 	}
4012a82b8beSDavid Chinner 
4022a82b8beSDavid Chinner 	flags = (ap->userdata ? XFS_PICK_USERDATA : 0) |
4030937e0fdSDave Chinner 	        (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0);
4042a82b8beSDavid Chinner 
4052cd2ef6aSChristoph Hellwig 	err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
4062a82b8beSDavid Chinner 
4072a82b8beSDavid Chinner 	/*
4082cd2ef6aSChristoph Hellwig 	 * Only free the item here so we skip over the old AG earlier.
4092a82b8beSDavid Chinner 	 */
4102cd2ef6aSChristoph Hellwig 	if (mru)
4112cd2ef6aSChristoph Hellwig 		xfs_fstrm_free_func(mru);
4122a82b8beSDavid Chinner 
4132cd2ef6aSChristoph Hellwig 	IRELE(pip);
4142a82b8beSDavid Chinner exit:
4152cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
4162a82b8beSDavid Chinner 		*agp = 0;
4172a82b8beSDavid Chinner 	return err;
4182a82b8beSDavid Chinner }
4192a82b8beSDavid Chinner 
4202a82b8beSDavid Chinner void
4212a82b8beSDavid Chinner xfs_filestream_deassociate(
4222cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
4232a82b8beSDavid Chinner {
42422328d71SChristoph Hellwig 	xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
4252a82b8beSDavid Chinner }
4262cd2ef6aSChristoph Hellwig 
4272cd2ef6aSChristoph Hellwig int
4282cd2ef6aSChristoph Hellwig xfs_filestream_mount(
4292cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
4302cd2ef6aSChristoph Hellwig {
4312cd2ef6aSChristoph Hellwig 	/*
4322cd2ef6aSChristoph Hellwig 	 * The filestream timer tunable is currently fixed within the range of
4332cd2ef6aSChristoph Hellwig 	 * one second to four minutes, with five seconds being the default.  The
4342cd2ef6aSChristoph Hellwig 	 * group count is somewhat arbitrary, but it'd be nice to adhere to the
4352cd2ef6aSChristoph Hellwig 	 * timer tunable to within about 10 percent.  This requires at least 10
4362cd2ef6aSChristoph Hellwig 	 * groups.
4372cd2ef6aSChristoph Hellwig 	 */
4382cd2ef6aSChristoph Hellwig 	return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10,
4392cd2ef6aSChristoph Hellwig 				    10, xfs_fstrm_free_func);
4402cd2ef6aSChristoph Hellwig }
4412cd2ef6aSChristoph Hellwig 
4422cd2ef6aSChristoph Hellwig void
4432cd2ef6aSChristoph Hellwig xfs_filestream_unmount(
4442cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
4452cd2ef6aSChristoph Hellwig {
4462cd2ef6aSChristoph Hellwig 	xfs_mru_cache_destroy(mp->m_filestream);
4472cd2ef6aSChristoph Hellwig }
448