xref: /openbmc/linux/fs/xfs/xfs_filestream.c (revision 2cd2ef6a300b1ac912bb515b75451585c3d33ea9)
12a82b8beSDavid Chinner /*
22a82b8beSDavid Chinner  * Copyright (c) 2006-2007 Silicon Graphics, Inc.
3*2cd2ef6aSChristoph 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 
422a82b8beSDavid Chinner static kmem_zone_t *item_zone;
432a82b8beSDavid Chinner 
44*2cd2ef6aSChristoph Hellwig struct xfs_fstrm_item {
4522328d71SChristoph Hellwig 	struct xfs_mru_cache_elem	mru;
46*2cd2ef6aSChristoph Hellwig 	struct xfs_inode		*ip;
47*2cd2ef6aSChristoph Hellwig 	xfs_agnumber_t			ag; /* AG in use for this directory */
48*2cd2ef6aSChristoph Hellwig };
49*2cd2ef6aSChristoph Hellwig 
50*2cd2ef6aSChristoph Hellwig enum xfs_fstrm_alloc {
51*2cd2ef6aSChristoph Hellwig 	XFS_PICK_USERDATA = 1,
52*2cd2ef6aSChristoph Hellwig 	XFS_PICK_LOWSPACE = 2,
53*2cd2ef6aSChristoph Hellwig };
542a82b8beSDavid Chinner 
550664ce8dSChristoph Hellwig /*
560664ce8dSChristoph Hellwig  * Allocation group filestream associations are tracked with per-ag atomic
57*2cd2ef6aSChristoph Hellwig  * counters.  These counters allow xfs_filestream_pick_ag() to tell whether a
580664ce8dSChristoph Hellwig  * particular AG already has active filestreams associated with it. The mount
590664ce8dSChristoph Hellwig  * point's m_peraglock is used to protect these counters from per-ag array
600664ce8dSChristoph Hellwig  * re-allocation during a growfs operation.  When xfs_growfs_data_private() is
610664ce8dSChristoph Hellwig  * about to reallocate the array, it calls xfs_filestream_flush() with the
620664ce8dSChristoph Hellwig  * m_peraglock held in write mode.
630664ce8dSChristoph Hellwig  *
640664ce8dSChristoph Hellwig  * Since xfs_mru_cache_flush() guarantees that all the free functions for all
650664ce8dSChristoph Hellwig  * the cache elements have finished executing before it returns, it's safe for
660664ce8dSChristoph Hellwig  * the free functions to use the atomic counters without m_peraglock protection.
670664ce8dSChristoph Hellwig  * This allows the implementation of xfs_fstrm_free_func() to be agnostic about
680664ce8dSChristoph Hellwig  * whether it was called with the m_peraglock held in read mode, write mode or
690664ce8dSChristoph Hellwig  * not held at all.  The race condition this addresses is the following:
700664ce8dSChristoph Hellwig  *
710664ce8dSChristoph Hellwig  *  - The work queue scheduler fires and pulls a filestream directory cache
720664ce8dSChristoph Hellwig  *    element off the LRU end of the cache for deletion, then gets pre-empted.
730664ce8dSChristoph Hellwig  *  - A growfs operation grabs the m_peraglock in write mode, flushes all the
740664ce8dSChristoph Hellwig  *    remaining items from the cache and reallocates the mount point's per-ag
750664ce8dSChristoph Hellwig  *    array, resetting all the counters to zero.
760664ce8dSChristoph Hellwig  *  - The work queue thread resumes and calls the free function for the element
770664ce8dSChristoph Hellwig  *    it started cleaning up earlier.  In the process it decrements the
780664ce8dSChristoph Hellwig  *    filestreams counter for an AG that now has no references.
790664ce8dSChristoph Hellwig  *
800664ce8dSChristoph Hellwig  * With a shrinkfs feature, the above scenario could panic the system.
810664ce8dSChristoph Hellwig  *
820664ce8dSChristoph Hellwig  * All other uses of the following macros should be protected by either the
830664ce8dSChristoph Hellwig  * m_peraglock held in read mode, or the cache's internal locking exposed by the
840664ce8dSChristoph Hellwig  * interval between a call to xfs_mru_cache_lookup() and a call to
850664ce8dSChristoph Hellwig  * xfs_mru_cache_done().  In addition, the m_peraglock must be held in read mode
860664ce8dSChristoph Hellwig  * when new elements are added to the cache.
870664ce8dSChristoph Hellwig  *
880664ce8dSChristoph Hellwig  * Combined, these locking rules ensure that no associations will ever exist in
890664ce8dSChristoph Hellwig  * the cache that reference per-ag array elements that have since been
900664ce8dSChristoph Hellwig  * reallocated.
910664ce8dSChristoph Hellwig  */
920664ce8dSChristoph Hellwig static int
930664ce8dSChristoph Hellwig xfs_filestream_peek_ag(
940664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
950664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
960664ce8dSChristoph Hellwig {
970664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
980664ce8dSChristoph Hellwig 	int		ret;
990664ce8dSChristoph Hellwig 
1000664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
1010664ce8dSChristoph Hellwig 	ret = atomic_read(&pag->pagf_fstrms);
1020664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
1030664ce8dSChristoph Hellwig 	return ret;
1040664ce8dSChristoph Hellwig }
1050664ce8dSChristoph Hellwig 
1060664ce8dSChristoph Hellwig static int
1070664ce8dSChristoph Hellwig xfs_filestream_get_ag(
1080664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
1090664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
1100664ce8dSChristoph Hellwig {
1110664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
1120664ce8dSChristoph Hellwig 	int		ret;
1130664ce8dSChristoph Hellwig 
1140664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
1150664ce8dSChristoph Hellwig 	ret = atomic_inc_return(&pag->pagf_fstrms);
1160664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
1170664ce8dSChristoph Hellwig 	return ret;
1180664ce8dSChristoph Hellwig }
1190664ce8dSChristoph Hellwig 
1200664ce8dSChristoph Hellwig static void
1210664ce8dSChristoph Hellwig xfs_filestream_put_ag(
1220664ce8dSChristoph Hellwig 	xfs_mount_t	*mp,
1230664ce8dSChristoph Hellwig 	xfs_agnumber_t	agno)
1240664ce8dSChristoph Hellwig {
1250664ce8dSChristoph Hellwig 	struct xfs_perag *pag;
1260664ce8dSChristoph Hellwig 
1270664ce8dSChristoph Hellwig 	pag = xfs_perag_get(mp, agno);
1280664ce8dSChristoph Hellwig 	atomic_dec(&pag->pagf_fstrms);
1290664ce8dSChristoph Hellwig 	xfs_perag_put(pag);
1300664ce8dSChristoph Hellwig }
1312a82b8beSDavid Chinner 
132*2cd2ef6aSChristoph Hellwig static void
133*2cd2ef6aSChristoph Hellwig xfs_fstrm_free_func(
134*2cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru)
135*2cd2ef6aSChristoph Hellwig {
136*2cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item =
137*2cd2ef6aSChristoph Hellwig 		container_of(mru, struct xfs_fstrm_item, mru);
138*2cd2ef6aSChristoph Hellwig 
139*2cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(item->ip->i_mount, item->ag);
140*2cd2ef6aSChristoph Hellwig 
141*2cd2ef6aSChristoph Hellwig 	TRACE_FREE(mp, ip, NULL, item->ag,
142*2cd2ef6aSChristoph Hellwig 		   xfs_filestream_peek_ag(mp, item->ag));
143*2cd2ef6aSChristoph Hellwig 
144*2cd2ef6aSChristoph Hellwig 	kmem_zone_free(item_zone, item);
145*2cd2ef6aSChristoph Hellwig }
146*2cd2ef6aSChristoph Hellwig 
1472a82b8beSDavid Chinner /*
1482a82b8beSDavid Chinner  * Scan the AGs starting at startag looking for an AG that isn't in use and has
1492a82b8beSDavid Chinner  * at least minlen blocks free.
1502a82b8beSDavid Chinner  */
1512a82b8beSDavid Chinner static int
152*2cd2ef6aSChristoph Hellwig xfs_filestream_pick_ag(
153*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip,
1542a82b8beSDavid Chinner 	xfs_agnumber_t		startag,
1552a82b8beSDavid Chinner 	xfs_agnumber_t		*agp,
1562a82b8beSDavid Chinner 	int			flags,
1572a82b8beSDavid Chinner 	xfs_extlen_t		minlen)
1582a82b8beSDavid Chinner {
159*2cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
160*2cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item;
161*2cd2ef6aSChristoph Hellwig 	struct xfs_perag	*pag;
1626cc87645SDave Chinner 	xfs_extlen_t		longest, free, minfree, maxfree = 0;
1632a82b8beSDavid Chinner 	xfs_agnumber_t		ag, max_ag = NULLAGNUMBER;
164*2cd2ef6aSChristoph Hellwig 	int			streams, max_streams;
165*2cd2ef6aSChristoph Hellwig 	int			err, trylock, nscan;
166*2cd2ef6aSChristoph Hellwig 
167*2cd2ef6aSChristoph Hellwig 	ASSERT(S_ISDIR(ip->i_d.di_mode));
1682a82b8beSDavid Chinner 
1692a82b8beSDavid Chinner 	/* 2% of an AG's blocks must be free for it to be chosen. */
1702a82b8beSDavid Chinner 	minfree = mp->m_sb.sb_agblocks / 50;
1712a82b8beSDavid Chinner 
1722a82b8beSDavid Chinner 	ag = startag;
1732a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
1742a82b8beSDavid Chinner 
1752a82b8beSDavid Chinner 	/* For the first pass, don't sleep trying to init the per-AG. */
1762a82b8beSDavid Chinner 	trylock = XFS_ALLOC_FLAG_TRYLOCK;
1772a82b8beSDavid Chinner 
1782a82b8beSDavid Chinner 	for (nscan = 0; 1; nscan++) {
1794196ac08SDave Chinner 		pag = xfs_perag_get(mp, ag);
1804196ac08SDave Chinner 		TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms));
1812a82b8beSDavid Chinner 
1822a82b8beSDavid Chinner 		if (!pag->pagf_init) {
1832a82b8beSDavid Chinner 			err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
1844196ac08SDave Chinner 			if (err && !trylock) {
1854196ac08SDave Chinner 				xfs_perag_put(pag);
1862a82b8beSDavid Chinner 				return err;
1872a82b8beSDavid Chinner 			}
1884196ac08SDave Chinner 		}
1892a82b8beSDavid Chinner 
1902a82b8beSDavid Chinner 		/* Might fail sometimes during the 1st pass with trylock set. */
1912a82b8beSDavid Chinner 		if (!pag->pagf_init)
1922a82b8beSDavid Chinner 			goto next_ag;
1932a82b8beSDavid Chinner 
1942a82b8beSDavid Chinner 		/* Keep track of the AG with the most free blocks. */
1952a82b8beSDavid Chinner 		if (pag->pagf_freeblks > maxfree) {
1962a82b8beSDavid Chinner 			maxfree = pag->pagf_freeblks;
1974196ac08SDave Chinner 			max_streams = atomic_read(&pag->pagf_fstrms);
1982a82b8beSDavid Chinner 			max_ag = ag;
1992a82b8beSDavid Chinner 		}
2002a82b8beSDavid Chinner 
2012a82b8beSDavid Chinner 		/*
2022a82b8beSDavid Chinner 		 * The AG reference count does two things: it enforces mutual
2032a82b8beSDavid Chinner 		 * exclusion when examining the suitability of an AG in this
2042a82b8beSDavid Chinner 		 * loop, and it guards against two filestreams being established
2052a82b8beSDavid Chinner 		 * in the same AG as each other.
2062a82b8beSDavid Chinner 		 */
2072a82b8beSDavid Chinner 		if (xfs_filestream_get_ag(mp, ag) > 1) {
2082a82b8beSDavid Chinner 			xfs_filestream_put_ag(mp, ag);
2092a82b8beSDavid Chinner 			goto next_ag;
2102a82b8beSDavid Chinner 		}
2112a82b8beSDavid Chinner 
2126cc87645SDave Chinner 		longest = xfs_alloc_longest_free_extent(mp, pag);
2132a82b8beSDavid Chinner 		if (((minlen && longest >= minlen) ||
2142a82b8beSDavid Chinner 		     (!minlen && pag->pagf_freeblks >= minfree)) &&
2152a82b8beSDavid Chinner 		    (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
2162a82b8beSDavid Chinner 		     (flags & XFS_PICK_LOWSPACE))) {
2172a82b8beSDavid Chinner 
2182a82b8beSDavid Chinner 			/* Break out, retaining the reference on the AG. */
2192a82b8beSDavid Chinner 			free = pag->pagf_freeblks;
2204196ac08SDave Chinner 			streams = atomic_read(&pag->pagf_fstrms);
2214196ac08SDave Chinner 			xfs_perag_put(pag);
2222a82b8beSDavid Chinner 			*agp = ag;
2232a82b8beSDavid Chinner 			break;
2242a82b8beSDavid Chinner 		}
2252a82b8beSDavid Chinner 
2262a82b8beSDavid Chinner 		/* Drop the reference on this AG, it's not usable. */
2272a82b8beSDavid Chinner 		xfs_filestream_put_ag(mp, ag);
2282a82b8beSDavid Chinner next_ag:
2294196ac08SDave Chinner 		xfs_perag_put(pag);
2302a82b8beSDavid Chinner 		/* Move to the next AG, wrapping to AG 0 if necessary. */
2312a82b8beSDavid Chinner 		if (++ag >= mp->m_sb.sb_agcount)
2322a82b8beSDavid Chinner 			ag = 0;
2332a82b8beSDavid Chinner 
2342a82b8beSDavid Chinner 		/* If a full pass of the AGs hasn't been done yet, continue. */
2352a82b8beSDavid Chinner 		if (ag != startag)
2362a82b8beSDavid Chinner 			continue;
2372a82b8beSDavid Chinner 
2382a82b8beSDavid Chinner 		/* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */
2392a82b8beSDavid Chinner 		if (trylock != 0) {
2402a82b8beSDavid Chinner 			trylock = 0;
2412a82b8beSDavid Chinner 			continue;
2422a82b8beSDavid Chinner 		}
2432a82b8beSDavid Chinner 
2442a82b8beSDavid Chinner 		/* Finally, if lowspace wasn't set, set it for the 3rd pass. */
2452a82b8beSDavid Chinner 		if (!(flags & XFS_PICK_LOWSPACE)) {
2462a82b8beSDavid Chinner 			flags |= XFS_PICK_LOWSPACE;
2472a82b8beSDavid Chinner 			continue;
2482a82b8beSDavid Chinner 		}
2492a82b8beSDavid Chinner 
2502a82b8beSDavid Chinner 		/*
2512a82b8beSDavid Chinner 		 * Take the AG with the most free space, regardless of whether
2522a82b8beSDavid Chinner 		 * it's already in use by another filestream.
2532a82b8beSDavid Chinner 		 */
2542a82b8beSDavid Chinner 		if (max_ag != NULLAGNUMBER) {
2552a82b8beSDavid Chinner 			xfs_filestream_get_ag(mp, max_ag);
2562a82b8beSDavid Chinner 			TRACE_AG_PICK1(mp, max_ag, maxfree);
2574196ac08SDave Chinner 			streams = max_streams;
2582a82b8beSDavid Chinner 			free = maxfree;
2592a82b8beSDavid Chinner 			*agp = max_ag;
2602a82b8beSDavid Chinner 			break;
2612a82b8beSDavid Chinner 		}
2622a82b8beSDavid Chinner 
2632a82b8beSDavid Chinner 		/* take AG 0 if none matched */
2642a82b8beSDavid Chinner 		TRACE_AG_PICK1(mp, max_ag, maxfree);
2652a82b8beSDavid Chinner 		*agp = 0;
2662a82b8beSDavid Chinner 		return 0;
2672a82b8beSDavid Chinner 	}
2682a82b8beSDavid Chinner 
2694196ac08SDave Chinner 	TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags);
2702a82b8beSDavid Chinner 
271*2cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
2722a82b8beSDavid Chinner 		return 0;
2732a82b8beSDavid Chinner 
274*2cd2ef6aSChristoph Hellwig 	err = ENOMEM;
2752a82b8beSDavid Chinner 	item = kmem_zone_zalloc(item_zone, KM_MAYFAIL);
2762a82b8beSDavid Chinner 	if (!item)
277*2cd2ef6aSChristoph Hellwig 		goto out_put_ag;
2782a82b8beSDavid Chinner 
279*2cd2ef6aSChristoph Hellwig 	item->ag = *agp;
2802a82b8beSDavid Chinner 	item->ip = ip;
2812a82b8beSDavid Chinner 
28222328d71SChristoph Hellwig 	err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
2832a82b8beSDavid Chinner 	if (err) {
284*2cd2ef6aSChristoph Hellwig 		if (err == EEXIST)
285*2cd2ef6aSChristoph Hellwig 			err = 0;
286*2cd2ef6aSChristoph Hellwig 		goto out_free_item;
287*2cd2ef6aSChristoph Hellwig 	}
288*2cd2ef6aSChristoph Hellwig 
289*2cd2ef6aSChristoph Hellwig 	return 0;
290*2cd2ef6aSChristoph Hellwig 
291*2cd2ef6aSChristoph Hellwig out_free_item:
2922a82b8beSDavid Chinner 	kmem_zone_free(item_zone, item);
293*2cd2ef6aSChristoph Hellwig out_put_ag:
294*2cd2ef6aSChristoph Hellwig 	xfs_filestream_put_ag(mp, *agp);
2952a82b8beSDavid Chinner 	return err;
2962a82b8beSDavid Chinner }
2972a82b8beSDavid Chinner 
298*2cd2ef6aSChristoph Hellwig static struct xfs_inode *
299*2cd2ef6aSChristoph Hellwig xfs_filestream_get_parent(
300*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
3012a82b8beSDavid Chinner {
302*2cd2ef6aSChristoph Hellwig 	struct inode		*inode = VFS_I(ip), *dir = NULL;
303*2cd2ef6aSChristoph Hellwig 	struct dentry		*dentry, *parent;
3042a82b8beSDavid Chinner 
305*2cd2ef6aSChristoph Hellwig 	dentry = d_find_alias(inode);
306*2cd2ef6aSChristoph Hellwig 	if (!dentry)
307*2cd2ef6aSChristoph Hellwig 		goto out;
3082a82b8beSDavid Chinner 
309*2cd2ef6aSChristoph Hellwig 	parent = dget_parent(dentry);
310*2cd2ef6aSChristoph Hellwig 	if (!parent)
311*2cd2ef6aSChristoph Hellwig 		goto out_dput;
3122a82b8beSDavid Chinner 
313*2cd2ef6aSChristoph Hellwig 	dir = igrab(parent->d_inode);
314*2cd2ef6aSChristoph Hellwig 	dput(parent);
3152a82b8beSDavid Chinner 
316*2cd2ef6aSChristoph Hellwig out_dput:
317*2cd2ef6aSChristoph Hellwig 	dput(dentry);
318*2cd2ef6aSChristoph Hellwig out:
319*2cd2ef6aSChristoph Hellwig 	return dir ? XFS_I(dir) : NULL;
3202a82b8beSDavid Chinner }
3212a82b8beSDavid Chinner 
3222a82b8beSDavid Chinner /*
3232a82b8beSDavid Chinner  * Return the AG of the filestream the file or directory belongs to, or
3242a82b8beSDavid Chinner  * NULLAGNUMBER otherwise.
3252a82b8beSDavid Chinner  */
3262a82b8beSDavid Chinner xfs_agnumber_t
3272a82b8beSDavid Chinner xfs_filestream_lookup_ag(
328*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
3292a82b8beSDavid Chinner {
33022328d71SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
331*2cd2ef6aSChristoph Hellwig 	struct xfs_fstrm_item	*item;
332*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*pip = NULL;
333*2cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		ag = NULLAGNUMBER;
334*2cd2ef6aSChristoph Hellwig 	int			ref = 0;
33522328d71SChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
3362a82b8beSDavid Chinner 
337*2cd2ef6aSChristoph Hellwig 	ASSERT(S_ISREG(ip->i_d.di_mode));
3382a82b8beSDavid Chinner 
339*2cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
340*2cd2ef6aSChristoph Hellwig 	if (!pip)
341*2cd2ef6aSChristoph Hellwig 		goto out;
3422a82b8beSDavid Chinner 
343*2cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
344*2cd2ef6aSChristoph Hellwig 	if (!mru)
345*2cd2ef6aSChristoph Hellwig 		goto out;
346*2cd2ef6aSChristoph Hellwig 
347*2cd2ef6aSChristoph Hellwig 	item = container_of(mru, struct xfs_fstrm_item, mru);
348*2cd2ef6aSChristoph Hellwig 
3492a82b8beSDavid Chinner 	ag = item->ag;
35022328d71SChristoph Hellwig 	xfs_mru_cache_done(mp->m_filestream);
3512a82b8beSDavid Chinner 
352*2cd2ef6aSChristoph Hellwig 	ref = xfs_filestream_peek_ag(ip->i_mount, ag);
353*2cd2ef6aSChristoph Hellwig out:
354*2cd2ef6aSChristoph Hellwig 	TRACE_LOOKUP(mp, ip, pip, ag, ref);
355*2cd2ef6aSChristoph Hellwig 	IRELE(pip);
3562a82b8beSDavid Chinner 	return ag;
3572a82b8beSDavid Chinner }
3582a82b8beSDavid Chinner 
3592a82b8beSDavid Chinner /*
360*2cd2ef6aSChristoph Hellwig  * Make sure a directory has a filestream associated with it.
3612a82b8beSDavid Chinner  *
362*2cd2ef6aSChristoph Hellwig  * This is called when creating regular files in an directory that has
363*2cd2ef6aSChristoph Hellwig  * filestreams enabled, so that a stream is ready by the time we need it
364*2cd2ef6aSChristoph Hellwig  * in the allocator for the files inside the directory.
3652a82b8beSDavid Chinner  */
3662a82b8beSDavid Chinner int
3672a82b8beSDavid Chinner xfs_filestream_associate(
368*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*pip)
3692a82b8beSDavid Chinner {
370*2cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = pip->i_mount;
37122328d71SChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
372*2cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		startag, ag;
3732a82b8beSDavid Chinner 
37403209378SAl Viro 	ASSERT(S_ISDIR(pip->i_d.di_mode));
3752a82b8beSDavid Chinner 
3762a82b8beSDavid Chinner 	/*
377*2cd2ef6aSChristoph Hellwig 	 * If the directory already has a file stream associated we're done.
3782a82b8beSDavid Chinner 	 */
37922328d71SChristoph Hellwig 	mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
38022328d71SChristoph Hellwig 	if (mru) {
38122328d71SChristoph Hellwig 		xfs_mru_cache_done(mp->m_filestream);
382*2cd2ef6aSChristoph Hellwig 		return 0;
3832a82b8beSDavid Chinner 	}
3842a82b8beSDavid Chinner 
3852a82b8beSDavid Chinner 	/*
3862a82b8beSDavid Chinner 	 * Set the starting AG using the rotor for inode32, otherwise
3872a82b8beSDavid Chinner 	 * use the directory inode's AG.
3882a82b8beSDavid Chinner 	 */
3892a82b8beSDavid Chinner 	if (mp->m_flags & XFS_MOUNT_32BITINODES) {
390*2cd2ef6aSChristoph Hellwig 		xfs_agnumber_t	 rotorstep = xfs_rotorstep;
3912a82b8beSDavid Chinner 		startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
3922a82b8beSDavid Chinner 		mp->m_agfrotor = (mp->m_agfrotor + 1) %
3932a82b8beSDavid Chinner 		                 (mp->m_sb.sb_agcount * rotorstep);
3942a82b8beSDavid Chinner 	} else
3952a82b8beSDavid Chinner 		startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
3962a82b8beSDavid Chinner 
397*2cd2ef6aSChristoph Hellwig 	return xfs_filestream_pick_ag(pip, startag, &ag, 0, 0);
3982a82b8beSDavid Chinner }
3992a82b8beSDavid Chinner 
4002a82b8beSDavid Chinner /*
401*2cd2ef6aSChristoph Hellwig  * Pick a new allocation group for the current file and its file stream.
402*2cd2ef6aSChristoph Hellwig  *
403*2cd2ef6aSChristoph Hellwig  * This is called when the allocator can't find a suitable extent in the
404*2cd2ef6aSChristoph Hellwig  * current AG, and we have to move the stream into a new AG with more space.
4052a82b8beSDavid Chinner  */
4062a82b8beSDavid Chinner int
4072a82b8beSDavid Chinner xfs_filestream_new_ag(
40868988114SDave Chinner 	struct xfs_bmalloca	*ap,
4092a82b8beSDavid Chinner 	xfs_agnumber_t		*agp)
4102a82b8beSDavid Chinner {
411*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip = ap->ip, *pip;
412*2cd2ef6aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
413*2cd2ef6aSChristoph Hellwig 	xfs_extlen_t		minlen = ap->length;
414*2cd2ef6aSChristoph Hellwig 	xfs_agnumber_t		startag = 0;
415*2cd2ef6aSChristoph Hellwig 	int			flags, err = 0;
416*2cd2ef6aSChristoph Hellwig 	struct xfs_mru_cache_elem *mru;
4172a82b8beSDavid Chinner 
4182a82b8beSDavid Chinner 	*agp = NULLAGNUMBER;
4192a82b8beSDavid Chinner 
420*2cd2ef6aSChristoph Hellwig 	pip = xfs_filestream_get_parent(ip);
421*2cd2ef6aSChristoph Hellwig 	if (!pip)
422*2cd2ef6aSChristoph Hellwig 		goto exit;
423*2cd2ef6aSChristoph Hellwig 
424*2cd2ef6aSChristoph Hellwig 	mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
42522328d71SChristoph Hellwig 	if (mru) {
426*2cd2ef6aSChristoph Hellwig 		struct xfs_fstrm_item *item =
427*2cd2ef6aSChristoph Hellwig 			container_of(mru, struct xfs_fstrm_item, mru);
428*2cd2ef6aSChristoph Hellwig 		startag = (item->ag + 1) % mp->m_sb.sb_agcount;
4292a82b8beSDavid Chinner 	}
4302a82b8beSDavid Chinner 
4312a82b8beSDavid Chinner 	flags = (ap->userdata ? XFS_PICK_USERDATA : 0) |
4320937e0fdSDave Chinner 	        (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0);
4332a82b8beSDavid Chinner 
434*2cd2ef6aSChristoph Hellwig 	err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
4352a82b8beSDavid Chinner 
4362a82b8beSDavid Chinner 	/*
437*2cd2ef6aSChristoph Hellwig 	 * Only free the item here so we skip over the old AG earlier.
4382a82b8beSDavid Chinner 	 */
439*2cd2ef6aSChristoph Hellwig 	if (mru)
440*2cd2ef6aSChristoph Hellwig 		xfs_fstrm_free_func(mru);
4412a82b8beSDavid Chinner 
442*2cd2ef6aSChristoph Hellwig 	IRELE(pip);
4432a82b8beSDavid Chinner exit:
444*2cd2ef6aSChristoph Hellwig 	if (*agp == NULLAGNUMBER)
4452a82b8beSDavid Chinner 		*agp = 0;
4462a82b8beSDavid Chinner 	return err;
4472a82b8beSDavid Chinner }
4482a82b8beSDavid Chinner 
4492a82b8beSDavid Chinner void
4502a82b8beSDavid Chinner xfs_filestream_deassociate(
451*2cd2ef6aSChristoph Hellwig 	struct xfs_inode	*ip)
4522a82b8beSDavid Chinner {
45322328d71SChristoph Hellwig 	xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
4542a82b8beSDavid Chinner }
455*2cd2ef6aSChristoph Hellwig 
456*2cd2ef6aSChristoph Hellwig int
457*2cd2ef6aSChristoph Hellwig xfs_filestream_mount(
458*2cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
459*2cd2ef6aSChristoph Hellwig {
460*2cd2ef6aSChristoph Hellwig 	/*
461*2cd2ef6aSChristoph Hellwig 	 * The filestream timer tunable is currently fixed within the range of
462*2cd2ef6aSChristoph Hellwig 	 * one second to four minutes, with five seconds being the default.  The
463*2cd2ef6aSChristoph Hellwig 	 * group count is somewhat arbitrary, but it'd be nice to adhere to the
464*2cd2ef6aSChristoph Hellwig 	 * timer tunable to within about 10 percent.  This requires at least 10
465*2cd2ef6aSChristoph Hellwig 	 * groups.
466*2cd2ef6aSChristoph Hellwig 	 */
467*2cd2ef6aSChristoph Hellwig 	return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10,
468*2cd2ef6aSChristoph Hellwig 				    10, xfs_fstrm_free_func);
469*2cd2ef6aSChristoph Hellwig }
470*2cd2ef6aSChristoph Hellwig 
471*2cd2ef6aSChristoph Hellwig void
472*2cd2ef6aSChristoph Hellwig xfs_filestream_unmount(
473*2cd2ef6aSChristoph Hellwig 	xfs_mount_t	*mp)
474*2cd2ef6aSChristoph Hellwig {
475*2cd2ef6aSChristoph Hellwig 	xfs_mru_cache_destroy(mp->m_filestream);
476*2cd2ef6aSChristoph Hellwig }
477*2cd2ef6aSChristoph Hellwig 
478*2cd2ef6aSChristoph Hellwig 
479*2cd2ef6aSChristoph Hellwig /* needs to return a positive errno for the init path */
480*2cd2ef6aSChristoph Hellwig int
481*2cd2ef6aSChristoph Hellwig xfs_filestream_init(void)
482*2cd2ef6aSChristoph Hellwig {
483*2cd2ef6aSChristoph Hellwig 	item_zone = kmem_zone_init(sizeof(struct xfs_fstrm_item), "fstrm_item");
484*2cd2ef6aSChristoph Hellwig 	if (!item_zone)
485*2cd2ef6aSChristoph Hellwig 		return -ENOMEM;
486*2cd2ef6aSChristoph Hellwig 	return 0;
487*2cd2ef6aSChristoph Hellwig }
488*2cd2ef6aSChristoph Hellwig 
489*2cd2ef6aSChristoph Hellwig void
490*2cd2ef6aSChristoph Hellwig xfs_filestream_uninit(void)
491*2cd2ef6aSChristoph Hellwig {
492*2cd2ef6aSChristoph Hellwig 	kmem_zone_destroy(item_zone);
493*2cd2ef6aSChristoph Hellwig }
494