David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006-2007 Silicon Graphics, Inc. |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 3 | * Copyright (c) 2014 Christoph Hellwig. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 4 | * All Rights Reserved. |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | #include "xfs.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 20 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_log_format.h" |
| 22 | #include "xfs_trans_resv.h" |
| 23 | #include "xfs_ag.h" |
| 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_mount.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 26 | #include "xfs_inum.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 27 | #include "xfs_inode.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 28 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 29 | #include "xfs_bmap_util.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 30 | #include "xfs_alloc.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 31 | #include "xfs_mru_cache.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 32 | #include "xfs_dinode.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 33 | #include "xfs_filestream.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 34 | #include "xfs_trace.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 35 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 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) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 39 | #define TRACE_FREE(mp, ip, pip, ag, cnt) |
| 40 | #define TRACE_LOOKUP(mp, ip, pip, ag, cnt) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 41 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 42 | struct xfs_fstrm_item { |
| 43 | struct xfs_mru_cache_elem mru; |
| 44 | struct xfs_inode *ip; |
| 45 | xfs_agnumber_t ag; /* AG in use for this directory */ |
| 46 | }; |
| 47 | |
| 48 | enum xfs_fstrm_alloc { |
| 49 | XFS_PICK_USERDATA = 1, |
| 50 | XFS_PICK_LOWSPACE = 2, |
| 51 | }; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 52 | |
Christoph Hellwig | 0664ce8 | 2010-07-20 17:31:01 +1000 | [diff] [blame] | 53 | /* |
| 54 | * Allocation group filestream associations are tracked with per-ag atomic |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 55 | * counters. These counters allow xfs_filestream_pick_ag() to tell whether a |
Christoph Hellwig | 0664ce8 | 2010-07-20 17:31:01 +1000 | [diff] [blame] | 56 | * particular AG already has active filestreams associated with it. The mount |
| 57 | * point's m_peraglock is used to protect these counters from per-ag array |
| 58 | * re-allocation during a growfs operation. When xfs_growfs_data_private() is |
| 59 | * about to reallocate the array, it calls xfs_filestream_flush() with the |
| 60 | * m_peraglock held in write mode. |
| 61 | * |
| 62 | * Since xfs_mru_cache_flush() guarantees that all the free functions for all |
| 63 | * the cache elements have finished executing before it returns, it's safe for |
| 64 | * the free functions to use the atomic counters without m_peraglock protection. |
| 65 | * This allows the implementation of xfs_fstrm_free_func() to be agnostic about |
| 66 | * whether it was called with the m_peraglock held in read mode, write mode or |
| 67 | * not held at all. The race condition this addresses is the following: |
| 68 | * |
| 69 | * - The work queue scheduler fires and pulls a filestream directory cache |
| 70 | * element off the LRU end of the cache for deletion, then gets pre-empted. |
| 71 | * - A growfs operation grabs the m_peraglock in write mode, flushes all the |
| 72 | * remaining items from the cache and reallocates the mount point's per-ag |
| 73 | * array, resetting all the counters to zero. |
| 74 | * - The work queue thread resumes and calls the free function for the element |
| 75 | * it started cleaning up earlier. In the process it decrements the |
| 76 | * filestreams counter for an AG that now has no references. |
| 77 | * |
| 78 | * With a shrinkfs feature, the above scenario could panic the system. |
| 79 | * |
| 80 | * All other uses of the following macros should be protected by either the |
| 81 | * m_peraglock held in read mode, or the cache's internal locking exposed by the |
| 82 | * interval between a call to xfs_mru_cache_lookup() and a call to |
| 83 | * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode |
| 84 | * when new elements are added to the cache. |
| 85 | * |
| 86 | * Combined, these locking rules ensure that no associations will ever exist in |
| 87 | * the cache that reference per-ag array elements that have since been |
| 88 | * reallocated. |
| 89 | */ |
| 90 | static int |
| 91 | xfs_filestream_peek_ag( |
| 92 | xfs_mount_t *mp, |
| 93 | xfs_agnumber_t agno) |
| 94 | { |
| 95 | struct xfs_perag *pag; |
| 96 | int ret; |
| 97 | |
| 98 | pag = xfs_perag_get(mp, agno); |
| 99 | ret = atomic_read(&pag->pagf_fstrms); |
| 100 | xfs_perag_put(pag); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int |
| 105 | xfs_filestream_get_ag( |
| 106 | xfs_mount_t *mp, |
| 107 | xfs_agnumber_t agno) |
| 108 | { |
| 109 | struct xfs_perag *pag; |
| 110 | int ret; |
| 111 | |
| 112 | pag = xfs_perag_get(mp, agno); |
| 113 | ret = atomic_inc_return(&pag->pagf_fstrms); |
| 114 | xfs_perag_put(pag); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | xfs_filestream_put_ag( |
| 120 | xfs_mount_t *mp, |
| 121 | xfs_agnumber_t agno) |
| 122 | { |
| 123 | struct xfs_perag *pag; |
| 124 | |
| 125 | pag = xfs_perag_get(mp, agno); |
| 126 | atomic_dec(&pag->pagf_fstrms); |
| 127 | xfs_perag_put(pag); |
| 128 | } |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 129 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 130 | static void |
| 131 | xfs_fstrm_free_func( |
| 132 | struct xfs_mru_cache_elem *mru) |
| 133 | { |
| 134 | struct xfs_fstrm_item *item = |
| 135 | container_of(mru, struct xfs_fstrm_item, mru); |
| 136 | |
| 137 | xfs_filestream_put_ag(item->ip->i_mount, item->ag); |
| 138 | |
| 139 | TRACE_FREE(mp, ip, NULL, item->ag, |
| 140 | xfs_filestream_peek_ag(mp, item->ag)); |
| 141 | |
Christoph Hellwig | 1919add | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 142 | kmem_free(item); |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 143 | } |
| 144 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 145 | /* |
| 146 | * Scan the AGs starting at startag looking for an AG that isn't in use and has |
| 147 | * at least minlen blocks free. |
| 148 | */ |
| 149 | static int |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 150 | xfs_filestream_pick_ag( |
| 151 | struct xfs_inode *ip, |
| 152 | xfs_agnumber_t startag, |
| 153 | xfs_agnumber_t *agp, |
| 154 | int flags, |
| 155 | xfs_extlen_t minlen) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 156 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 157 | struct xfs_mount *mp = ip->i_mount; |
| 158 | struct xfs_fstrm_item *item; |
| 159 | struct xfs_perag *pag; |
| 160 | xfs_extlen_t longest, free, minfree, maxfree = 0; |
| 161 | xfs_agnumber_t ag, max_ag = NULLAGNUMBER; |
| 162 | int streams, max_streams; |
| 163 | int err, trylock, nscan; |
| 164 | |
| 165 | ASSERT(S_ISDIR(ip->i_d.di_mode)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 166 | |
| 167 | /* 2% of an AG's blocks must be free for it to be chosen. */ |
| 168 | minfree = mp->m_sb.sb_agblocks / 50; |
| 169 | |
| 170 | ag = startag; |
| 171 | *agp = NULLAGNUMBER; |
| 172 | |
| 173 | /* For the first pass, don't sleep trying to init the per-AG. */ |
| 174 | trylock = XFS_ALLOC_FLAG_TRYLOCK; |
| 175 | |
| 176 | for (nscan = 0; 1; nscan++) { |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 177 | pag = xfs_perag_get(mp, ag); |
| 178 | TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 179 | |
| 180 | if (!pag->pagf_init) { |
| 181 | err = xfs_alloc_pagf_init(mp, NULL, ag, trylock); |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 182 | if (err && !trylock) { |
| 183 | xfs_perag_put(pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 184 | return err; |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 185 | } |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Might fail sometimes during the 1st pass with trylock set. */ |
| 189 | if (!pag->pagf_init) |
| 190 | goto next_ag; |
| 191 | |
| 192 | /* Keep track of the AG with the most free blocks. */ |
| 193 | if (pag->pagf_freeblks > maxfree) { |
| 194 | maxfree = pag->pagf_freeblks; |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 195 | max_streams = atomic_read(&pag->pagf_fstrms); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 196 | max_ag = ag; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * The AG reference count does two things: it enforces mutual |
| 201 | * exclusion when examining the suitability of an AG in this |
| 202 | * loop, and it guards against two filestreams being established |
| 203 | * in the same AG as each other. |
| 204 | */ |
| 205 | if (xfs_filestream_get_ag(mp, ag) > 1) { |
| 206 | xfs_filestream_put_ag(mp, ag); |
| 207 | goto next_ag; |
| 208 | } |
| 209 | |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 210 | longest = xfs_alloc_longest_free_extent(mp, pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 211 | if (((minlen && longest >= minlen) || |
| 212 | (!minlen && pag->pagf_freeblks >= minfree)) && |
| 213 | (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) || |
| 214 | (flags & XFS_PICK_LOWSPACE))) { |
| 215 | |
| 216 | /* Break out, retaining the reference on the AG. */ |
| 217 | free = pag->pagf_freeblks; |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 218 | streams = atomic_read(&pag->pagf_fstrms); |
| 219 | xfs_perag_put(pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 220 | *agp = ag; |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | /* Drop the reference on this AG, it's not usable. */ |
| 225 | xfs_filestream_put_ag(mp, ag); |
| 226 | next_ag: |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 227 | xfs_perag_put(pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 228 | /* Move to the next AG, wrapping to AG 0 if necessary. */ |
| 229 | if (++ag >= mp->m_sb.sb_agcount) |
| 230 | ag = 0; |
| 231 | |
| 232 | /* If a full pass of the AGs hasn't been done yet, continue. */ |
| 233 | if (ag != startag) |
| 234 | continue; |
| 235 | |
| 236 | /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */ |
| 237 | if (trylock != 0) { |
| 238 | trylock = 0; |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | /* Finally, if lowspace wasn't set, set it for the 3rd pass. */ |
| 243 | if (!(flags & XFS_PICK_LOWSPACE)) { |
| 244 | flags |= XFS_PICK_LOWSPACE; |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Take the AG with the most free space, regardless of whether |
| 250 | * it's already in use by another filestream. |
| 251 | */ |
| 252 | if (max_ag != NULLAGNUMBER) { |
| 253 | xfs_filestream_get_ag(mp, max_ag); |
| 254 | TRACE_AG_PICK1(mp, max_ag, maxfree); |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 255 | streams = max_streams; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 256 | free = maxfree; |
| 257 | *agp = max_ag; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | /* take AG 0 if none matched */ |
| 262 | TRACE_AG_PICK1(mp, max_ag, maxfree); |
| 263 | *agp = 0; |
| 264 | return 0; |
| 265 | } |
| 266 | |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 267 | TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 268 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 269 | if (*agp == NULLAGNUMBER) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 270 | return 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 271 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 272 | err = ENOMEM; |
Christoph Hellwig | 1919add | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 273 | item = kmem_alloc(sizeof(*item), KM_MAYFAIL); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 274 | if (!item) |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 275 | goto out_put_ag; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 276 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 277 | item->ag = *agp; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 278 | item->ip = ip; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 279 | |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 280 | err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 281 | if (err) { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 282 | if (err == EEXIST) |
| 283 | err = 0; |
| 284 | goto out_free_item; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 285 | } |
| 286 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 287 | return 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 288 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 289 | out_free_item: |
Christoph Hellwig | 1919add | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 290 | kmem_free(item); |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 291 | out_put_ag: |
| 292 | xfs_filestream_put_ag(mp, *agp); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 293 | return err; |
| 294 | } |
| 295 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 296 | static struct xfs_inode * |
| 297 | xfs_filestream_get_parent( |
| 298 | struct xfs_inode *ip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 299 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 300 | struct inode *inode = VFS_I(ip), *dir = NULL; |
| 301 | struct dentry *dentry, *parent; |
| 302 | |
| 303 | dentry = d_find_alias(inode); |
| 304 | if (!dentry) |
| 305 | goto out; |
| 306 | |
| 307 | parent = dget_parent(dentry); |
| 308 | if (!parent) |
| 309 | goto out_dput; |
| 310 | |
| 311 | dir = igrab(parent->d_inode); |
| 312 | dput(parent); |
| 313 | |
| 314 | out_dput: |
| 315 | dput(dentry); |
| 316 | out: |
| 317 | return dir ? XFS_I(dir) : NULL; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 321 | * Return the AG of the filestream the file or directory belongs to, or |
| 322 | * NULLAGNUMBER otherwise. |
| 323 | */ |
| 324 | xfs_agnumber_t |
| 325 | xfs_filestream_lookup_ag( |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 326 | struct xfs_inode *ip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 327 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 328 | struct xfs_mount *mp = ip->i_mount; |
| 329 | struct xfs_fstrm_item *item; |
| 330 | struct xfs_inode *pip = NULL; |
| 331 | xfs_agnumber_t ag = NULLAGNUMBER; |
| 332 | int ref = 0; |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 333 | struct xfs_mru_cache_elem *mru; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 334 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 335 | ASSERT(S_ISREG(ip->i_d.di_mode)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 336 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 337 | pip = xfs_filestream_get_parent(ip); |
| 338 | if (!pip) |
| 339 | goto out; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 340 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 341 | mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); |
| 342 | if (!mru) |
| 343 | goto out; |
| 344 | |
| 345 | item = container_of(mru, struct xfs_fstrm_item, mru); |
| 346 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 347 | ag = item->ag; |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 348 | xfs_mru_cache_done(mp->m_filestream); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 349 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 350 | ref = xfs_filestream_peek_ag(ip->i_mount, ag); |
| 351 | out: |
| 352 | TRACE_LOOKUP(mp, ip, pip, ag, ref); |
| 353 | IRELE(pip); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 354 | return ag; |
| 355 | } |
| 356 | |
| 357 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 358 | * Make sure a directory has a filestream associated with it. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 359 | * |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 360 | * This is called when creating regular files in an directory that has |
| 361 | * filestreams enabled, so that a stream is ready by the time we need it |
| 362 | * in the allocator for the files inside the directory. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 363 | */ |
| 364 | int |
| 365 | xfs_filestream_associate( |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 366 | struct xfs_inode *pip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 367 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 368 | struct xfs_mount *mp = pip->i_mount; |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 369 | struct xfs_mru_cache_elem *mru; |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 370 | xfs_agnumber_t startag, ag; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 371 | |
Al Viro | 0320937 | 2011-07-25 20:54:24 -0400 | [diff] [blame] | 372 | ASSERT(S_ISDIR(pip->i_d.di_mode)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 373 | |
| 374 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 375 | * If the directory already has a file stream associated we're done. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 376 | */ |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 377 | mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); |
| 378 | if (mru) { |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 379 | xfs_mru_cache_done(mp->m_filestream); |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 380 | return 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Set the starting AG using the rotor for inode32, otherwise |
| 385 | * use the directory inode's AG. |
| 386 | */ |
| 387 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 388 | xfs_agnumber_t rotorstep = xfs_rotorstep; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 389 | startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount; |
| 390 | mp->m_agfrotor = (mp->m_agfrotor + 1) % |
| 391 | (mp->m_sb.sb_agcount * rotorstep); |
| 392 | } else |
| 393 | startag = XFS_INO_TO_AGNO(mp, pip->i_ino); |
| 394 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 395 | return xfs_filestream_pick_ag(pip, startag, &ag, 0, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 399 | * Pick a new allocation group for the current file and its file stream. |
| 400 | * |
| 401 | * This is called when the allocator can't find a suitable extent in the |
| 402 | * current AG, and we have to move the stream into a new AG with more space. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 403 | */ |
| 404 | int |
| 405 | xfs_filestream_new_ag( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 406 | struct xfs_bmalloca *ap, |
| 407 | xfs_agnumber_t *agp) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 408 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 409 | struct xfs_inode *ip = ap->ip, *pip; |
| 410 | struct xfs_mount *mp = ip->i_mount; |
| 411 | xfs_extlen_t minlen = ap->length; |
| 412 | xfs_agnumber_t startag = 0; |
| 413 | int flags, err = 0; |
| 414 | struct xfs_mru_cache_elem *mru; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 415 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 416 | *agp = NULLAGNUMBER; |
| 417 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 418 | pip = xfs_filestream_get_parent(ip); |
| 419 | if (!pip) |
| 420 | goto exit; |
| 421 | |
| 422 | mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino); |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 423 | if (mru) { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 424 | struct xfs_fstrm_item *item = |
| 425 | container_of(mru, struct xfs_fstrm_item, mru); |
| 426 | startag = (item->ag + 1) % mp->m_sb.sb_agcount; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 427 | } |
| 428 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 429 | flags = (ap->userdata ? XFS_PICK_USERDATA : 0) | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 430 | (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 431 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 432 | err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 433 | |
| 434 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 435 | * Only free the item here so we skip over the old AG earlier. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 436 | */ |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 437 | if (mru) |
| 438 | xfs_fstrm_free_func(mru); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 439 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 440 | IRELE(pip); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 441 | exit: |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 442 | if (*agp == NULLAGNUMBER) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 443 | *agp = 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 444 | return err; |
| 445 | } |
| 446 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 447 | void |
| 448 | xfs_filestream_deassociate( |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 449 | struct xfs_inode *ip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 450 | { |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 451 | xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 452 | } |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 453 | |
| 454 | int |
| 455 | xfs_filestream_mount( |
| 456 | xfs_mount_t *mp) |
| 457 | { |
| 458 | /* |
| 459 | * The filestream timer tunable is currently fixed within the range of |
| 460 | * one second to four minutes, with five seconds being the default. The |
| 461 | * group count is somewhat arbitrary, but it'd be nice to adhere to the |
| 462 | * timer tunable to within about 10 percent. This requires at least 10 |
| 463 | * groups. |
| 464 | */ |
| 465 | return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10, |
| 466 | 10, xfs_fstrm_free_func); |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | xfs_filestream_unmount( |
| 471 | xfs_mount_t *mp) |
| 472 | { |
| 473 | xfs_mru_cache_destroy(mp->m_filestream); |
| 474 | } |