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" 8*5467b34bSDarrick 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_sb.h" 13239880efSDave Chinner #include "xfs_mount.h" 143ab78df2SDarrick J. Wong #include "xfs_defer.h" 152a82b8beSDavid Chinner #include "xfs_inode.h" 162a82b8beSDavid Chinner #include "xfs_bmap.h" 1768988114SDave Chinner #include "xfs_bmap_util.h" 182a82b8beSDavid Chinner #include "xfs_alloc.h" 192a82b8beSDavid Chinner #include "xfs_mru_cache.h" 202a82b8beSDavid Chinner #include "xfs_filestream.h" 210b1b213fSChristoph Hellwig #include "xfs_trace.h" 223fd129b6SDarrick J. Wong #include "xfs_ag_resv.h" 233e3673e3SBrian Foster #include "xfs_trans.h" 241214f1cfSBrian Foster #include "xfs_shared.h" 252a82b8beSDavid Chinner 262cd2ef6aSChristoph Hellwig struct xfs_fstrm_item { 2722328d71SChristoph Hellwig struct xfs_mru_cache_elem mru; 282cd2ef6aSChristoph Hellwig xfs_agnumber_t ag; /* AG in use for this directory */ 292cd2ef6aSChristoph Hellwig }; 302cd2ef6aSChristoph Hellwig 312cd2ef6aSChristoph Hellwig enum xfs_fstrm_alloc { 322cd2ef6aSChristoph Hellwig XFS_PICK_USERDATA = 1, 332cd2ef6aSChristoph Hellwig XFS_PICK_LOWSPACE = 2, 342cd2ef6aSChristoph Hellwig }; 352a82b8beSDavid Chinner 360664ce8dSChristoph Hellwig /* 370664ce8dSChristoph Hellwig * Allocation group filestream associations are tracked with per-ag atomic 382cd2ef6aSChristoph Hellwig * counters. These counters allow xfs_filestream_pick_ag() to tell whether a 390664ce8dSChristoph Hellwig * particular AG already has active filestreams associated with it. The mount 400664ce8dSChristoph Hellwig * point's m_peraglock is used to protect these counters from per-ag array 410664ce8dSChristoph Hellwig * re-allocation during a growfs operation. When xfs_growfs_data_private() is 420664ce8dSChristoph Hellwig * about to reallocate the array, it calls xfs_filestream_flush() with the 430664ce8dSChristoph Hellwig * m_peraglock held in write mode. 440664ce8dSChristoph Hellwig * 450664ce8dSChristoph Hellwig * Since xfs_mru_cache_flush() guarantees that all the free functions for all 460664ce8dSChristoph Hellwig * the cache elements have finished executing before it returns, it's safe for 470664ce8dSChristoph Hellwig * the free functions to use the atomic counters without m_peraglock protection. 480664ce8dSChristoph Hellwig * This allows the implementation of xfs_fstrm_free_func() to be agnostic about 490664ce8dSChristoph Hellwig * whether it was called with the m_peraglock held in read mode, write mode or 500664ce8dSChristoph Hellwig * not held at all. The race condition this addresses is the following: 510664ce8dSChristoph Hellwig * 520664ce8dSChristoph Hellwig * - The work queue scheduler fires and pulls a filestream directory cache 530664ce8dSChristoph Hellwig * element off the LRU end of the cache for deletion, then gets pre-empted. 540664ce8dSChristoph Hellwig * - A growfs operation grabs the m_peraglock in write mode, flushes all the 550664ce8dSChristoph Hellwig * remaining items from the cache and reallocates the mount point's per-ag 560664ce8dSChristoph Hellwig * array, resetting all the counters to zero. 570664ce8dSChristoph Hellwig * - The work queue thread resumes and calls the free function for the element 580664ce8dSChristoph Hellwig * it started cleaning up earlier. In the process it decrements the 590664ce8dSChristoph Hellwig * filestreams counter for an AG that now has no references. 600664ce8dSChristoph Hellwig * 610664ce8dSChristoph Hellwig * With a shrinkfs feature, the above scenario could panic the system. 620664ce8dSChristoph Hellwig * 630664ce8dSChristoph Hellwig * All other uses of the following macros should be protected by either the 640664ce8dSChristoph Hellwig * m_peraglock held in read mode, or the cache's internal locking exposed by the 650664ce8dSChristoph Hellwig * interval between a call to xfs_mru_cache_lookup() and a call to 660664ce8dSChristoph Hellwig * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode 670664ce8dSChristoph Hellwig * when new elements are added to the cache. 680664ce8dSChristoph Hellwig * 690664ce8dSChristoph Hellwig * Combined, these locking rules ensure that no associations will ever exist in 700664ce8dSChristoph Hellwig * the cache that reference per-ag array elements that have since been 710664ce8dSChristoph Hellwig * reallocated. 720664ce8dSChristoph Hellwig */ 73b94acd47SChristoph Hellwig int 740664ce8dSChristoph Hellwig xfs_filestream_peek_ag( 750664ce8dSChristoph Hellwig xfs_mount_t *mp, 760664ce8dSChristoph Hellwig xfs_agnumber_t agno) 770664ce8dSChristoph Hellwig { 780664ce8dSChristoph Hellwig struct xfs_perag *pag; 790664ce8dSChristoph Hellwig int ret; 800664ce8dSChristoph Hellwig 810664ce8dSChristoph Hellwig pag = xfs_perag_get(mp, agno); 820664ce8dSChristoph Hellwig ret = atomic_read(&pag->pagf_fstrms); 830664ce8dSChristoph Hellwig xfs_perag_put(pag); 840664ce8dSChristoph Hellwig return ret; 850664ce8dSChristoph Hellwig } 860664ce8dSChristoph Hellwig 870664ce8dSChristoph Hellwig static int 880664ce8dSChristoph Hellwig xfs_filestream_get_ag( 890664ce8dSChristoph Hellwig xfs_mount_t *mp, 900664ce8dSChristoph Hellwig xfs_agnumber_t agno) 910664ce8dSChristoph Hellwig { 920664ce8dSChristoph Hellwig struct xfs_perag *pag; 930664ce8dSChristoph Hellwig int ret; 940664ce8dSChristoph Hellwig 950664ce8dSChristoph Hellwig pag = xfs_perag_get(mp, agno); 960664ce8dSChristoph Hellwig ret = atomic_inc_return(&pag->pagf_fstrms); 970664ce8dSChristoph Hellwig xfs_perag_put(pag); 980664ce8dSChristoph Hellwig return ret; 990664ce8dSChristoph Hellwig } 1000664ce8dSChristoph Hellwig 1010664ce8dSChristoph Hellwig static void 1020664ce8dSChristoph Hellwig xfs_filestream_put_ag( 1030664ce8dSChristoph Hellwig xfs_mount_t *mp, 1040664ce8dSChristoph Hellwig xfs_agnumber_t agno) 1050664ce8dSChristoph Hellwig { 1060664ce8dSChristoph Hellwig struct xfs_perag *pag; 1070664ce8dSChristoph Hellwig 1080664ce8dSChristoph Hellwig pag = xfs_perag_get(mp, agno); 1090664ce8dSChristoph Hellwig atomic_dec(&pag->pagf_fstrms); 1100664ce8dSChristoph Hellwig xfs_perag_put(pag); 1110664ce8dSChristoph Hellwig } 1122a82b8beSDavid Chinner 1132cd2ef6aSChristoph Hellwig static void 1142cd2ef6aSChristoph Hellwig xfs_fstrm_free_func( 1157fcd3efaSChristoph Hellwig void *data, 1162cd2ef6aSChristoph Hellwig struct xfs_mru_cache_elem *mru) 1172cd2ef6aSChristoph Hellwig { 1187fcd3efaSChristoph Hellwig struct xfs_mount *mp = data; 1192cd2ef6aSChristoph Hellwig struct xfs_fstrm_item *item = 1202cd2ef6aSChristoph Hellwig container_of(mru, struct xfs_fstrm_item, mru); 1212cd2ef6aSChristoph Hellwig 1227fcd3efaSChristoph Hellwig xfs_filestream_put_ag(mp, item->ag); 1237fcd3efaSChristoph Hellwig trace_xfs_filestream_free(mp, mru->key, item->ag); 1242cd2ef6aSChristoph Hellwig 1251919addaSChristoph Hellwig kmem_free(item); 1262cd2ef6aSChristoph Hellwig } 1272cd2ef6aSChristoph Hellwig 1282a82b8beSDavid Chinner /* 1292a82b8beSDavid Chinner * Scan the AGs starting at startag looking for an AG that isn't in use and has 1302a82b8beSDavid Chinner * at least minlen blocks free. 1312a82b8beSDavid Chinner */ 1322a82b8beSDavid Chinner static int 1332cd2ef6aSChristoph Hellwig xfs_filestream_pick_ag( 1342cd2ef6aSChristoph Hellwig struct xfs_inode *ip, 1352a82b8beSDavid Chinner xfs_agnumber_t startag, 1362a82b8beSDavid Chinner xfs_agnumber_t *agp, 1372a82b8beSDavid Chinner int flags, 1382a82b8beSDavid Chinner xfs_extlen_t minlen) 1392a82b8beSDavid Chinner { 1402cd2ef6aSChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 1412cd2ef6aSChristoph Hellwig struct xfs_fstrm_item *item; 1422cd2ef6aSChristoph Hellwig struct xfs_perag *pag; 143b94acd47SChristoph Hellwig xfs_extlen_t longest, free = 0, minfree, maxfree = 0; 1442a82b8beSDavid Chinner xfs_agnumber_t ag, max_ag = NULLAGNUMBER; 1452cd2ef6aSChristoph Hellwig int err, trylock, nscan; 1462cd2ef6aSChristoph Hellwig 147c19b3b05SDave Chinner ASSERT(S_ISDIR(VFS_I(ip)->i_mode)); 1482a82b8beSDavid Chinner 1492a82b8beSDavid Chinner /* 2% of an AG's blocks must be free for it to be chosen. */ 1502a82b8beSDavid Chinner minfree = mp->m_sb.sb_agblocks / 50; 1512a82b8beSDavid Chinner 1522a82b8beSDavid Chinner ag = startag; 1532a82b8beSDavid Chinner *agp = NULLAGNUMBER; 1542a82b8beSDavid Chinner 1552a82b8beSDavid Chinner /* For the first pass, don't sleep trying to init the per-AG. */ 1562a82b8beSDavid Chinner trylock = XFS_ALLOC_FLAG_TRYLOCK; 1572a82b8beSDavid Chinner 1582a82b8beSDavid Chinner for (nscan = 0; 1; nscan++) { 1597fcd3efaSChristoph Hellwig trace_xfs_filestream_scan(mp, ip->i_ino, ag); 160b94acd47SChristoph Hellwig 1614196ac08SDave Chinner pag = xfs_perag_get(mp, ag); 1622a82b8beSDavid Chinner 1632a82b8beSDavid Chinner if (!pag->pagf_init) { 1642a82b8beSDavid Chinner err = xfs_alloc_pagf_init(mp, NULL, ag, trylock); 1654196ac08SDave Chinner if (err && !trylock) { 1664196ac08SDave Chinner xfs_perag_put(pag); 1672a82b8beSDavid Chinner return err; 1682a82b8beSDavid Chinner } 1694196ac08SDave Chinner } 1702a82b8beSDavid Chinner 1712a82b8beSDavid Chinner /* Might fail sometimes during the 1st pass with trylock set. */ 1722a82b8beSDavid Chinner if (!pag->pagf_init) 1732a82b8beSDavid Chinner goto next_ag; 1742a82b8beSDavid Chinner 1752a82b8beSDavid Chinner /* Keep track of the AG with the most free blocks. */ 1762a82b8beSDavid Chinner if (pag->pagf_freeblks > maxfree) { 1772a82b8beSDavid Chinner maxfree = pag->pagf_freeblks; 1782a82b8beSDavid Chinner max_ag = ag; 1792a82b8beSDavid Chinner } 1802a82b8beSDavid Chinner 1812a82b8beSDavid Chinner /* 1822a82b8beSDavid Chinner * The AG reference count does two things: it enforces mutual 1832a82b8beSDavid Chinner * exclusion when examining the suitability of an AG in this 1842a82b8beSDavid Chinner * loop, and it guards against two filestreams being established 1852a82b8beSDavid Chinner * in the same AG as each other. 1862a82b8beSDavid Chinner */ 1872a82b8beSDavid Chinner if (xfs_filestream_get_ag(mp, ag) > 1) { 1882a82b8beSDavid Chinner xfs_filestream_put_ag(mp, ag); 1892a82b8beSDavid Chinner goto next_ag; 1902a82b8beSDavid Chinner } 1912a82b8beSDavid Chinner 192a1f69417SEric Sandeen longest = xfs_alloc_longest_free_extent(pag, 1933fd129b6SDarrick J. Wong xfs_alloc_min_freelist(mp, pag), 1943fd129b6SDarrick J. Wong xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); 1952a82b8beSDavid Chinner if (((minlen && longest >= minlen) || 1962a82b8beSDavid Chinner (!minlen && pag->pagf_freeblks >= minfree)) && 1972a82b8beSDavid Chinner (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) || 1982a82b8beSDavid Chinner (flags & XFS_PICK_LOWSPACE))) { 1992a82b8beSDavid Chinner 2002a82b8beSDavid Chinner /* Break out, retaining the reference on the AG. */ 2012a82b8beSDavid Chinner free = pag->pagf_freeblks; 2024196ac08SDave Chinner xfs_perag_put(pag); 2032a82b8beSDavid Chinner *agp = ag; 2042a82b8beSDavid Chinner break; 2052a82b8beSDavid Chinner } 2062a82b8beSDavid Chinner 2072a82b8beSDavid Chinner /* Drop the reference on this AG, it's not usable. */ 2082a82b8beSDavid Chinner xfs_filestream_put_ag(mp, ag); 2092a82b8beSDavid Chinner next_ag: 2104196ac08SDave Chinner xfs_perag_put(pag); 2112a82b8beSDavid Chinner /* Move to the next AG, wrapping to AG 0 if necessary. */ 2122a82b8beSDavid Chinner if (++ag >= mp->m_sb.sb_agcount) 2132a82b8beSDavid Chinner ag = 0; 2142a82b8beSDavid Chinner 2152a82b8beSDavid Chinner /* If a full pass of the AGs hasn't been done yet, continue. */ 2162a82b8beSDavid Chinner if (ag != startag) 2172a82b8beSDavid Chinner continue; 2182a82b8beSDavid Chinner 2192a82b8beSDavid Chinner /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */ 2202a82b8beSDavid Chinner if (trylock != 0) { 2212a82b8beSDavid Chinner trylock = 0; 2222a82b8beSDavid Chinner continue; 2232a82b8beSDavid Chinner } 2242a82b8beSDavid Chinner 2252a82b8beSDavid Chinner /* Finally, if lowspace wasn't set, set it for the 3rd pass. */ 2262a82b8beSDavid Chinner if (!(flags & XFS_PICK_LOWSPACE)) { 2272a82b8beSDavid Chinner flags |= XFS_PICK_LOWSPACE; 2282a82b8beSDavid Chinner continue; 2292a82b8beSDavid Chinner } 2302a82b8beSDavid Chinner 2312a82b8beSDavid Chinner /* 2322a82b8beSDavid Chinner * Take the AG with the most free space, regardless of whether 2332a82b8beSDavid Chinner * it's already in use by another filestream. 2342a82b8beSDavid Chinner */ 2352a82b8beSDavid Chinner if (max_ag != NULLAGNUMBER) { 2362a82b8beSDavid Chinner xfs_filestream_get_ag(mp, max_ag); 2372a82b8beSDavid Chinner free = maxfree; 2382a82b8beSDavid Chinner *agp = max_ag; 2392a82b8beSDavid Chinner break; 2402a82b8beSDavid Chinner } 2412a82b8beSDavid Chinner 2422a82b8beSDavid Chinner /* take AG 0 if none matched */ 243b94acd47SChristoph Hellwig trace_xfs_filestream_pick(ip, *agp, free, nscan); 2442a82b8beSDavid Chinner *agp = 0; 2452a82b8beSDavid Chinner return 0; 2462a82b8beSDavid Chinner } 2472a82b8beSDavid Chinner 248b94acd47SChristoph Hellwig trace_xfs_filestream_pick(ip, *agp, free, nscan); 2492a82b8beSDavid Chinner 2502cd2ef6aSChristoph Hellwig if (*agp == NULLAGNUMBER) 2512a82b8beSDavid Chinner return 0; 2522a82b8beSDavid Chinner 2532451337dSDave Chinner err = -ENOMEM; 2541919addaSChristoph Hellwig item = kmem_alloc(sizeof(*item), KM_MAYFAIL); 2552a82b8beSDavid Chinner if (!item) 2562cd2ef6aSChristoph Hellwig goto out_put_ag; 2572a82b8beSDavid Chinner 2582cd2ef6aSChristoph Hellwig item->ag = *agp; 2592a82b8beSDavid Chinner 26022328d71SChristoph Hellwig err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru); 2612a82b8beSDavid Chinner if (err) { 2622451337dSDave Chinner if (err == -EEXIST) 2632cd2ef6aSChristoph Hellwig err = 0; 2642cd2ef6aSChristoph Hellwig goto out_free_item; 2652cd2ef6aSChristoph Hellwig } 2662cd2ef6aSChristoph Hellwig 2672cd2ef6aSChristoph Hellwig return 0; 2682cd2ef6aSChristoph Hellwig 2692cd2ef6aSChristoph Hellwig out_free_item: 2701919addaSChristoph Hellwig kmem_free(item); 2712cd2ef6aSChristoph Hellwig out_put_ag: 2722cd2ef6aSChristoph Hellwig xfs_filestream_put_ag(mp, *agp); 2732a82b8beSDavid Chinner return err; 2742a82b8beSDavid Chinner } 2752a82b8beSDavid Chinner 2762cd2ef6aSChristoph Hellwig static struct xfs_inode * 2772cd2ef6aSChristoph Hellwig xfs_filestream_get_parent( 2782cd2ef6aSChristoph Hellwig struct xfs_inode *ip) 2792a82b8beSDavid Chinner { 2802cd2ef6aSChristoph Hellwig struct inode *inode = VFS_I(ip), *dir = NULL; 2812cd2ef6aSChristoph Hellwig struct dentry *dentry, *parent; 2822a82b8beSDavid Chinner 2832cd2ef6aSChristoph Hellwig dentry = d_find_alias(inode); 2842cd2ef6aSChristoph Hellwig if (!dentry) 2852cd2ef6aSChristoph Hellwig goto out; 2862a82b8beSDavid Chinner 2872cd2ef6aSChristoph Hellwig parent = dget_parent(dentry); 2882cd2ef6aSChristoph Hellwig if (!parent) 2892cd2ef6aSChristoph Hellwig goto out_dput; 2902a82b8beSDavid Chinner 2912b0143b5SDavid Howells dir = igrab(d_inode(parent)); 2922cd2ef6aSChristoph Hellwig dput(parent); 2932a82b8beSDavid Chinner 2942cd2ef6aSChristoph Hellwig out_dput: 2952cd2ef6aSChristoph Hellwig dput(dentry); 2962cd2ef6aSChristoph Hellwig out: 2972cd2ef6aSChristoph Hellwig return dir ? XFS_I(dir) : NULL; 2982a82b8beSDavid Chinner } 2992a82b8beSDavid Chinner 3002a82b8beSDavid Chinner /* 3013b8d9076SChristoph Hellwig * Find the right allocation group for a file, either by finding an 3023b8d9076SChristoph Hellwig * existing file stream or creating a new one. 3033b8d9076SChristoph Hellwig * 3043b8d9076SChristoph Hellwig * Returns NULLAGNUMBER in case of an error. 3052a82b8beSDavid Chinner */ 3062a82b8beSDavid Chinner xfs_agnumber_t 3072a82b8beSDavid Chinner xfs_filestream_lookup_ag( 3082cd2ef6aSChristoph Hellwig struct xfs_inode *ip) 3092a82b8beSDavid Chinner { 31022328d71SChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 3112cd2ef6aSChristoph Hellwig struct xfs_inode *pip = NULL; 3123b8d9076SChristoph Hellwig xfs_agnumber_t startag, ag = NULLAGNUMBER; 31322328d71SChristoph Hellwig struct xfs_mru_cache_elem *mru; 3142a82b8beSDavid Chinner 315c19b3b05SDave Chinner ASSERT(S_ISREG(VFS_I(ip)->i_mode)); 3162a82b8beSDavid Chinner 3172cd2ef6aSChristoph Hellwig pip = xfs_filestream_get_parent(ip); 3182cd2ef6aSChristoph Hellwig if (!pip) 319b26384dcSEric Sandeen return NULLAGNUMBER; 3202a82b8beSDavid Chinner 3212cd2ef6aSChristoph Hellwig mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); 3223b8d9076SChristoph Hellwig if (mru) { 3233b8d9076SChristoph Hellwig ag = container_of(mru, struct xfs_fstrm_item, mru)->ag; 32422328d71SChristoph Hellwig xfs_mru_cache_done(mp->m_filestream); 3252a82b8beSDavid Chinner 3267fcd3efaSChristoph Hellwig trace_xfs_filestream_lookup(mp, ip->i_ino, ag); 3273b8d9076SChristoph Hellwig goto out; 3282a82b8beSDavid Chinner } 3292a82b8beSDavid Chinner 3302a82b8beSDavid Chinner /* 3312a82b8beSDavid Chinner * Set the starting AG using the rotor for inode32, otherwise 3322a82b8beSDavid Chinner * use the directory inode's AG. 3332a82b8beSDavid Chinner */ 3342a82b8beSDavid Chinner if (mp->m_flags & XFS_MOUNT_32BITINODES) { 3352cd2ef6aSChristoph Hellwig xfs_agnumber_t rotorstep = xfs_rotorstep; 3362a82b8beSDavid Chinner startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount; 3372a82b8beSDavid Chinner mp->m_agfrotor = (mp->m_agfrotor + 1) % 3382a82b8beSDavid Chinner (mp->m_sb.sb_agcount * rotorstep); 3392a82b8beSDavid Chinner } else 3402a82b8beSDavid Chinner startag = XFS_INO_TO_AGNO(mp, pip->i_ino); 3412a82b8beSDavid Chinner 3423b8d9076SChristoph Hellwig if (xfs_filestream_pick_ag(pip, startag, &ag, 0, 0)) 3433b8d9076SChristoph Hellwig ag = NULLAGNUMBER; 3443b8d9076SChristoph Hellwig out: 34544a8736bSDarrick J. Wong xfs_irele(pip); 3463b8d9076SChristoph Hellwig return ag; 3472a82b8beSDavid Chinner } 3482a82b8beSDavid Chinner 3492a82b8beSDavid Chinner /* 3502cd2ef6aSChristoph Hellwig * Pick a new allocation group for the current file and its file stream. 3512cd2ef6aSChristoph Hellwig * 3522cd2ef6aSChristoph Hellwig * This is called when the allocator can't find a suitable extent in the 3532cd2ef6aSChristoph Hellwig * current AG, and we have to move the stream into a new AG with more space. 3542a82b8beSDavid Chinner */ 3552a82b8beSDavid Chinner int 3562a82b8beSDavid Chinner xfs_filestream_new_ag( 35768988114SDave Chinner struct xfs_bmalloca *ap, 3582a82b8beSDavid Chinner xfs_agnumber_t *agp) 3592a82b8beSDavid Chinner { 3602cd2ef6aSChristoph Hellwig struct xfs_inode *ip = ap->ip, *pip; 3612cd2ef6aSChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 3622cd2ef6aSChristoph Hellwig xfs_extlen_t minlen = ap->length; 3632cd2ef6aSChristoph Hellwig xfs_agnumber_t startag = 0; 364292378edSDave Chinner int flags = 0; 365292378edSDave Chinner int err = 0; 3662cd2ef6aSChristoph Hellwig struct xfs_mru_cache_elem *mru; 3672a82b8beSDavid Chinner 3682a82b8beSDavid Chinner *agp = NULLAGNUMBER; 3692a82b8beSDavid Chinner 3702cd2ef6aSChristoph Hellwig pip = xfs_filestream_get_parent(ip); 3712cd2ef6aSChristoph Hellwig if (!pip) 3722cd2ef6aSChristoph Hellwig goto exit; 3732cd2ef6aSChristoph Hellwig 3742cd2ef6aSChristoph Hellwig mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino); 37522328d71SChristoph Hellwig if (mru) { 3762cd2ef6aSChristoph Hellwig struct xfs_fstrm_item *item = 3772cd2ef6aSChristoph Hellwig container_of(mru, struct xfs_fstrm_item, mru); 3782cd2ef6aSChristoph Hellwig startag = (item->ag + 1) % mp->m_sb.sb_agcount; 3792a82b8beSDavid Chinner } 3802a82b8beSDavid Chinner 381292378edSDave Chinner if (xfs_alloc_is_userdata(ap->datatype)) 382292378edSDave Chinner flags |= XFS_PICK_USERDATA; 3831214f1cfSBrian Foster if (ap->tp->t_flags & XFS_TRANS_LOWMODE) 384292378edSDave Chinner flags |= XFS_PICK_LOWSPACE; 3852a82b8beSDavid Chinner 3862cd2ef6aSChristoph Hellwig err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen); 3872a82b8beSDavid Chinner 3882a82b8beSDavid Chinner /* 3892cd2ef6aSChristoph Hellwig * Only free the item here so we skip over the old AG earlier. 3902a82b8beSDavid Chinner */ 3912cd2ef6aSChristoph Hellwig if (mru) 3927fcd3efaSChristoph Hellwig xfs_fstrm_free_func(mp, mru); 3932a82b8beSDavid Chinner 39444a8736bSDarrick J. Wong xfs_irele(pip); 3952a82b8beSDavid Chinner exit: 3962cd2ef6aSChristoph Hellwig if (*agp == NULLAGNUMBER) 3972a82b8beSDavid Chinner *agp = 0; 3982a82b8beSDavid Chinner return err; 3992a82b8beSDavid Chinner } 4002a82b8beSDavid Chinner 4012a82b8beSDavid Chinner void 4022a82b8beSDavid Chinner xfs_filestream_deassociate( 4032cd2ef6aSChristoph Hellwig struct xfs_inode *ip) 4042a82b8beSDavid Chinner { 40522328d71SChristoph Hellwig xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino); 4062a82b8beSDavid Chinner } 4072cd2ef6aSChristoph Hellwig 4082cd2ef6aSChristoph Hellwig int 4092cd2ef6aSChristoph Hellwig xfs_filestream_mount( 4102cd2ef6aSChristoph Hellwig xfs_mount_t *mp) 4112cd2ef6aSChristoph Hellwig { 4122cd2ef6aSChristoph Hellwig /* 4132cd2ef6aSChristoph Hellwig * The filestream timer tunable is currently fixed within the range of 4142cd2ef6aSChristoph Hellwig * one second to four minutes, with five seconds being the default. The 4152cd2ef6aSChristoph Hellwig * group count is somewhat arbitrary, but it'd be nice to adhere to the 4162cd2ef6aSChristoph Hellwig * timer tunable to within about 10 percent. This requires at least 10 4172cd2ef6aSChristoph Hellwig * groups. 4182cd2ef6aSChristoph Hellwig */ 4197fcd3efaSChristoph Hellwig return xfs_mru_cache_create(&mp->m_filestream, mp, 4207fcd3efaSChristoph Hellwig xfs_fstrm_centisecs * 10, 10, xfs_fstrm_free_func); 4212cd2ef6aSChristoph Hellwig } 4222cd2ef6aSChristoph Hellwig 4232cd2ef6aSChristoph Hellwig void 4242cd2ef6aSChristoph Hellwig xfs_filestream_unmount( 4252cd2ef6aSChristoph Hellwig xfs_mount_t *mp) 4262cd2ef6aSChristoph Hellwig { 4272cd2ef6aSChristoph Hellwig xfs_mru_cache_destroy(mp->m_filestream); 4282cd2ef6aSChristoph Hellwig } 429