2 * Copyright (c) 2006-2007 Silicon Graphics, Inc.
3 * Copyright (c) 2014 Christoph Hellwig.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
27 #include "xfs_inode.h"
29 #include "xfs_bmap_util.h"
30 #include "xfs_alloc.h"
31 #include "xfs_mru_cache.h"
32 #include "xfs_dinode.h"
33 #include "xfs_filestream.h"
34 #include "xfs_trace.h"
36 #define TRACE_AG_SCAN(mp, ag, ag2)
37 #define TRACE_AG_PICK1(mp, max_ag, maxfree)
38 #define TRACE_AG_PICK2(mp, ag, ag2, cnt, free, scan, flag)
39 #define TRACE_FREE(mp, ip, pip, ag, cnt)
40 #define TRACE_LOOKUP(mp, ip, pip, ag, cnt)
42 static kmem_zone_t *item_zone;
44 struct xfs_fstrm_item {
45 struct xfs_mru_cache_elem mru;
47 xfs_agnumber_t ag; /* AG in use for this directory */
50 enum xfs_fstrm_alloc {
51 XFS_PICK_USERDATA = 1,
52 XFS_PICK_LOWSPACE = 2,
56 * Allocation group filestream associations are tracked with per-ag atomic
57 * counters. These counters allow xfs_filestream_pick_ag() to tell whether a
58 * particular AG already has active filestreams associated with it. The mount
59 * point's m_peraglock is used to protect these counters from per-ag array
60 * re-allocation during a growfs operation. When xfs_growfs_data_private() is
61 * about to reallocate the array, it calls xfs_filestream_flush() with the
62 * m_peraglock held in write mode.
64 * Since xfs_mru_cache_flush() guarantees that all the free functions for all
65 * the cache elements have finished executing before it returns, it's safe for
66 * the free functions to use the atomic counters without m_peraglock protection.
67 * This allows the implementation of xfs_fstrm_free_func() to be agnostic about
68 * whether it was called with the m_peraglock held in read mode, write mode or
69 * not held at all. The race condition this addresses is the following:
71 * - The work queue scheduler fires and pulls a filestream directory cache
72 * element off the LRU end of the cache for deletion, then gets pre-empted.
73 * - A growfs operation grabs the m_peraglock in write mode, flushes all the
74 * remaining items from the cache and reallocates the mount point's per-ag
75 * array, resetting all the counters to zero.
76 * - The work queue thread resumes and calls the free function for the element
77 * it started cleaning up earlier. In the process it decrements the
78 * filestreams counter for an AG that now has no references.
80 * With a shrinkfs feature, the above scenario could panic the system.
82 * All other uses of the following macros should be protected by either the
83 * m_peraglock held in read mode, or the cache's internal locking exposed by the
84 * interval between a call to xfs_mru_cache_lookup() and a call to
85 * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode
86 * when new elements are added to the cache.
88 * Combined, these locking rules ensure that no associations will ever exist in
89 * the cache that reference per-ag array elements that have since been
93 xfs_filestream_peek_ag(
97 struct xfs_perag *pag;
100 pag = xfs_perag_get(mp, agno);
101 ret = atomic_read(&pag->pagf_fstrms);
107 xfs_filestream_get_ag(
111 struct xfs_perag *pag;
114 pag = xfs_perag_get(mp, agno);
115 ret = atomic_inc_return(&pag->pagf_fstrms);
121 xfs_filestream_put_ag(
125 struct xfs_perag *pag;
127 pag = xfs_perag_get(mp, agno);
128 atomic_dec(&pag->pagf_fstrms);
134 struct xfs_mru_cache_elem *mru)
136 struct xfs_fstrm_item *item =
137 container_of(mru, struct xfs_fstrm_item, mru);
139 xfs_filestream_put_ag(item->ip->i_mount, item->ag);
141 TRACE_FREE(mp, ip, NULL, item->ag,
142 xfs_filestream_peek_ag(mp, item->ag));
144 kmem_zone_free(item_zone, item);
148 * Scan the AGs starting at startag looking for an AG that isn't in use and has
149 * at least minlen blocks free.
152 xfs_filestream_pick_ag(
153 struct xfs_inode *ip,
154 xfs_agnumber_t startag,
159 struct xfs_mount *mp = ip->i_mount;
160 struct xfs_fstrm_item *item;
161 struct xfs_perag *pag;
162 xfs_extlen_t longest, free, minfree, maxfree = 0;
163 xfs_agnumber_t ag, max_ag = NULLAGNUMBER;
164 int streams, max_streams;
165 int err, trylock, nscan;
167 ASSERT(S_ISDIR(ip->i_d.di_mode));
169 /* 2% of an AG's blocks must be free for it to be chosen. */
170 minfree = mp->m_sb.sb_agblocks / 50;
175 /* For the first pass, don't sleep trying to init the per-AG. */
176 trylock = XFS_ALLOC_FLAG_TRYLOCK;
178 for (nscan = 0; 1; nscan++) {
179 pag = xfs_perag_get(mp, ag);
180 TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms));
182 if (!pag->pagf_init) {
183 err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
184 if (err && !trylock) {
190 /* Might fail sometimes during the 1st pass with trylock set. */
194 /* Keep track of the AG with the most free blocks. */
195 if (pag->pagf_freeblks > maxfree) {
196 maxfree = pag->pagf_freeblks;
197 max_streams = atomic_read(&pag->pagf_fstrms);
202 * The AG reference count does two things: it enforces mutual
203 * exclusion when examining the suitability of an AG in this
204 * loop, and it guards against two filestreams being established
205 * in the same AG as each other.
207 if (xfs_filestream_get_ag(mp, ag) > 1) {
208 xfs_filestream_put_ag(mp, ag);
212 longest = xfs_alloc_longest_free_extent(mp, pag);
213 if (((minlen && longest >= minlen) ||
214 (!minlen && pag->pagf_freeblks >= minfree)) &&
215 (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
216 (flags & XFS_PICK_LOWSPACE))) {
218 /* Break out, retaining the reference on the AG. */
219 free = pag->pagf_freeblks;
220 streams = atomic_read(&pag->pagf_fstrms);
226 /* Drop the reference on this AG, it's not usable. */
227 xfs_filestream_put_ag(mp, ag);
230 /* Move to the next AG, wrapping to AG 0 if necessary. */
231 if (++ag >= mp->m_sb.sb_agcount)
234 /* If a full pass of the AGs hasn't been done yet, continue. */
238 /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */
244 /* Finally, if lowspace wasn't set, set it for the 3rd pass. */
245 if (!(flags & XFS_PICK_LOWSPACE)) {
246 flags |= XFS_PICK_LOWSPACE;
251 * Take the AG with the most free space, regardless of whether
252 * it's already in use by another filestream.
254 if (max_ag != NULLAGNUMBER) {
255 xfs_filestream_get_ag(mp, max_ag);
256 TRACE_AG_PICK1(mp, max_ag, maxfree);
257 streams = max_streams;
263 /* take AG 0 if none matched */
264 TRACE_AG_PICK1(mp, max_ag, maxfree);
269 TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags);
271 if (*agp == NULLAGNUMBER)
275 item = kmem_zone_zalloc(item_zone, KM_MAYFAIL);
282 err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
292 kmem_zone_free(item_zone, item);
294 xfs_filestream_put_ag(mp, *agp);
298 static struct xfs_inode *
299 xfs_filestream_get_parent(
300 struct xfs_inode *ip)
302 struct inode *inode = VFS_I(ip), *dir = NULL;
303 struct dentry *dentry, *parent;
305 dentry = d_find_alias(inode);
309 parent = dget_parent(dentry);
313 dir = igrab(parent->d_inode);
319 return dir ? XFS_I(dir) : NULL;
323 * Return the AG of the filestream the file or directory belongs to, or
324 * NULLAGNUMBER otherwise.
327 xfs_filestream_lookup_ag(
328 struct xfs_inode *ip)
330 struct xfs_mount *mp = ip->i_mount;
331 struct xfs_fstrm_item *item;
332 struct xfs_inode *pip = NULL;
333 xfs_agnumber_t ag = NULLAGNUMBER;
335 struct xfs_mru_cache_elem *mru;
337 ASSERT(S_ISREG(ip->i_d.di_mode));
339 pip = xfs_filestream_get_parent(ip);
343 mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
347 item = container_of(mru, struct xfs_fstrm_item, mru);
350 xfs_mru_cache_done(mp->m_filestream);
352 ref = xfs_filestream_peek_ag(ip->i_mount, ag);
354 TRACE_LOOKUP(mp, ip, pip, ag, ref);
360 * Make sure a directory has a filestream associated with it.
362 * This is called when creating regular files in an directory that has
363 * filestreams enabled, so that a stream is ready by the time we need it
364 * in the allocator for the files inside the directory.
367 xfs_filestream_associate(
368 struct xfs_inode *pip)
370 struct xfs_mount *mp = pip->i_mount;
371 struct xfs_mru_cache_elem *mru;
372 xfs_agnumber_t startag, ag;
374 ASSERT(S_ISDIR(pip->i_d.di_mode));
377 * If the directory already has a file stream associated we're done.
379 mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
381 xfs_mru_cache_done(mp->m_filestream);
386 * Set the starting AG using the rotor for inode32, otherwise
387 * use the directory inode's AG.
389 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
390 xfs_agnumber_t rotorstep = xfs_rotorstep;
391 startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
392 mp->m_agfrotor = (mp->m_agfrotor + 1) %
393 (mp->m_sb.sb_agcount * rotorstep);
395 startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
397 return xfs_filestream_pick_ag(pip, startag, &ag, 0, 0);
401 * Pick a new allocation group for the current file and its file stream.
403 * This is called when the allocator can't find a suitable extent in the
404 * current AG, and we have to move the stream into a new AG with more space.
407 xfs_filestream_new_ag(
408 struct xfs_bmalloca *ap,
411 struct xfs_inode *ip = ap->ip, *pip;
412 struct xfs_mount *mp = ip->i_mount;
413 xfs_extlen_t minlen = ap->length;
414 xfs_agnumber_t startag = 0;
416 struct xfs_mru_cache_elem *mru;
420 pip = xfs_filestream_get_parent(ip);
424 mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino);
426 struct xfs_fstrm_item *item =
427 container_of(mru, struct xfs_fstrm_item, mru);
428 startag = (item->ag + 1) % mp->m_sb.sb_agcount;
431 flags = (ap->userdata ? XFS_PICK_USERDATA : 0) |
432 (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0);
434 err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
437 * Only free the item here so we skip over the old AG earlier.
440 xfs_fstrm_free_func(mru);
444 if (*agp == NULLAGNUMBER)
450 xfs_filestream_deassociate(
451 struct xfs_inode *ip)
453 xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino);
457 xfs_filestream_mount(
461 * The filestream timer tunable is currently fixed within the range of
462 * one second to four minutes, with five seconds being the default. The
463 * group count is somewhat arbitrary, but it'd be nice to adhere to the
464 * timer tunable to within about 10 percent. This requires at least 10
467 return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10,
468 10, xfs_fstrm_free_func);
472 xfs_filestream_unmount(
475 xfs_mru_cache_destroy(mp->m_filestream);
479 /* needs to return a positive errno for the init path */
481 xfs_filestream_init(void)
483 item_zone = kmem_zone_init(sizeof(struct xfs_fstrm_item), "fstrm_item");
490 xfs_filestream_uninit(void)
492 kmem_zone_destroy(item_zone);