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 | |
| 42 | static kmem_zone_t *item_zone; |
| 43 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 44 | struct xfs_fstrm_item { |
| 45 | struct xfs_mru_cache_elem mru; |
| 46 | struct xfs_inode *ip; |
| 47 | xfs_agnumber_t ag; /* AG in use for this directory */ |
| 48 | }; |
| 49 | |
| 50 | enum xfs_fstrm_alloc { |
| 51 | XFS_PICK_USERDATA = 1, |
| 52 | XFS_PICK_LOWSPACE = 2, |
| 53 | }; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 54 | |
Christoph Hellwig | 0664ce8 | 2010-07-20 17:31:01 +1000 | [diff] [blame] | 55 | /* |
| 56 | * Allocation group filestream associations are tracked with per-ag atomic |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 57 | * counters. These counters allow xfs_filestream_pick_ag() to tell whether a |
Christoph Hellwig | 0664ce8 | 2010-07-20 17:31:01 +1000 | [diff] [blame] | 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. |
| 63 | * |
| 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: |
| 70 | * |
| 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. |
| 79 | * |
| 80 | * With a shrinkfs feature, the above scenario could panic the system. |
| 81 | * |
| 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. |
| 87 | * |
| 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 |
| 90 | * reallocated. |
| 91 | */ |
| 92 | static int |
| 93 | xfs_filestream_peek_ag( |
| 94 | xfs_mount_t *mp, |
| 95 | xfs_agnumber_t agno) |
| 96 | { |
| 97 | struct xfs_perag *pag; |
| 98 | int ret; |
| 99 | |
| 100 | pag = xfs_perag_get(mp, agno); |
| 101 | ret = atomic_read(&pag->pagf_fstrms); |
| 102 | xfs_perag_put(pag); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | static int |
| 107 | xfs_filestream_get_ag( |
| 108 | xfs_mount_t *mp, |
| 109 | xfs_agnumber_t agno) |
| 110 | { |
| 111 | struct xfs_perag *pag; |
| 112 | int ret; |
| 113 | |
| 114 | pag = xfs_perag_get(mp, agno); |
| 115 | ret = atomic_inc_return(&pag->pagf_fstrms); |
| 116 | xfs_perag_put(pag); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | xfs_filestream_put_ag( |
| 122 | xfs_mount_t *mp, |
| 123 | xfs_agnumber_t agno) |
| 124 | { |
| 125 | struct xfs_perag *pag; |
| 126 | |
| 127 | pag = xfs_perag_get(mp, agno); |
| 128 | atomic_dec(&pag->pagf_fstrms); |
| 129 | xfs_perag_put(pag); |
| 130 | } |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 131 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 132 | static void |
| 133 | xfs_fstrm_free_func( |
| 134 | struct xfs_mru_cache_elem *mru) |
| 135 | { |
| 136 | struct xfs_fstrm_item *item = |
| 137 | container_of(mru, struct xfs_fstrm_item, mru); |
| 138 | |
| 139 | xfs_filestream_put_ag(item->ip->i_mount, item->ag); |
| 140 | |
| 141 | TRACE_FREE(mp, ip, NULL, item->ag, |
| 142 | xfs_filestream_peek_ag(mp, item->ag)); |
| 143 | |
| 144 | kmem_zone_free(item_zone, item); |
| 145 | } |
| 146 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 147 | /* |
| 148 | * Scan the AGs starting at startag looking for an AG that isn't in use and has |
| 149 | * at least minlen blocks free. |
| 150 | */ |
| 151 | static int |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 152 | xfs_filestream_pick_ag( |
| 153 | struct xfs_inode *ip, |
| 154 | xfs_agnumber_t startag, |
| 155 | xfs_agnumber_t *agp, |
| 156 | int flags, |
| 157 | xfs_extlen_t minlen) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 158 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 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; |
| 166 | |
| 167 | ASSERT(S_ISDIR(ip->i_d.di_mode)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 168 | |
| 169 | /* 2% of an AG's blocks must be free for it to be chosen. */ |
| 170 | minfree = mp->m_sb.sb_agblocks / 50; |
| 171 | |
| 172 | ag = startag; |
| 173 | *agp = NULLAGNUMBER; |
| 174 | |
| 175 | /* For the first pass, don't sleep trying to init the per-AG. */ |
| 176 | trylock = XFS_ALLOC_FLAG_TRYLOCK; |
| 177 | |
| 178 | for (nscan = 0; 1; nscan++) { |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 179 | pag = xfs_perag_get(mp, ag); |
| 180 | TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 181 | |
| 182 | if (!pag->pagf_init) { |
| 183 | err = xfs_alloc_pagf_init(mp, NULL, ag, trylock); |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 184 | if (err && !trylock) { |
| 185 | xfs_perag_put(pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 186 | return err; |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 187 | } |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* Might fail sometimes during the 1st pass with trylock set. */ |
| 191 | if (!pag->pagf_init) |
| 192 | goto next_ag; |
| 193 | |
| 194 | /* Keep track of the AG with the most free blocks. */ |
| 195 | if (pag->pagf_freeblks > maxfree) { |
| 196 | maxfree = pag->pagf_freeblks; |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 197 | max_streams = atomic_read(&pag->pagf_fstrms); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 198 | max_ag = ag; |
| 199 | } |
| 200 | |
| 201 | /* |
| 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. |
| 206 | */ |
| 207 | if (xfs_filestream_get_ag(mp, ag) > 1) { |
| 208 | xfs_filestream_put_ag(mp, ag); |
| 209 | goto next_ag; |
| 210 | } |
| 211 | |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 212 | longest = xfs_alloc_longest_free_extent(mp, pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 213 | if (((minlen && longest >= minlen) || |
| 214 | (!minlen && pag->pagf_freeblks >= minfree)) && |
| 215 | (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) || |
| 216 | (flags & XFS_PICK_LOWSPACE))) { |
| 217 | |
| 218 | /* Break out, retaining the reference on the AG. */ |
| 219 | free = pag->pagf_freeblks; |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 220 | streams = atomic_read(&pag->pagf_fstrms); |
| 221 | xfs_perag_put(pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 222 | *agp = ag; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | /* Drop the reference on this AG, it's not usable. */ |
| 227 | xfs_filestream_put_ag(mp, ag); |
| 228 | next_ag: |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 229 | xfs_perag_put(pag); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 230 | /* Move to the next AG, wrapping to AG 0 if necessary. */ |
| 231 | if (++ag >= mp->m_sb.sb_agcount) |
| 232 | ag = 0; |
| 233 | |
| 234 | /* If a full pass of the AGs hasn't been done yet, continue. */ |
| 235 | if (ag != startag) |
| 236 | continue; |
| 237 | |
| 238 | /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */ |
| 239 | if (trylock != 0) { |
| 240 | trylock = 0; |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | /* Finally, if lowspace wasn't set, set it for the 3rd pass. */ |
| 245 | if (!(flags & XFS_PICK_LOWSPACE)) { |
| 246 | flags |= XFS_PICK_LOWSPACE; |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Take the AG with the most free space, regardless of whether |
| 252 | * it's already in use by another filestream. |
| 253 | */ |
| 254 | if (max_ag != NULLAGNUMBER) { |
| 255 | xfs_filestream_get_ag(mp, max_ag); |
| 256 | TRACE_AG_PICK1(mp, max_ag, maxfree); |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 257 | streams = max_streams; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 258 | free = maxfree; |
| 259 | *agp = max_ag; |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | /* take AG 0 if none matched */ |
| 264 | TRACE_AG_PICK1(mp, max_ag, maxfree); |
| 265 | *agp = 0; |
| 266 | return 0; |
| 267 | } |
| 268 | |
Dave Chinner | 4196ac0 | 2010-01-11 11:47:42 +0000 | [diff] [blame] | 269 | TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 270 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 271 | if (*agp == NULLAGNUMBER) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 272 | return 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 273 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 274 | err = ENOMEM; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 275 | item = kmem_zone_zalloc(item_zone, KM_MAYFAIL); |
| 276 | if (!item) |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 277 | goto out_put_ag; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 278 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 279 | item->ag = *agp; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 280 | item->ip = ip; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 281 | |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 282 | 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] | 283 | if (err) { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 284 | if (err == EEXIST) |
| 285 | err = 0; |
| 286 | goto out_free_item; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 287 | } |
| 288 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 289 | return 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 290 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 291 | out_free_item: |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 292 | kmem_zone_free(item_zone, item); |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 293 | out_put_ag: |
| 294 | xfs_filestream_put_ag(mp, *agp); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 295 | return err; |
| 296 | } |
| 297 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 298 | static struct xfs_inode * |
| 299 | xfs_filestream_get_parent( |
| 300 | struct xfs_inode *ip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 301 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 302 | struct inode *inode = VFS_I(ip), *dir = NULL; |
| 303 | struct dentry *dentry, *parent; |
| 304 | |
| 305 | dentry = d_find_alias(inode); |
| 306 | if (!dentry) |
| 307 | goto out; |
| 308 | |
| 309 | parent = dget_parent(dentry); |
| 310 | if (!parent) |
| 311 | goto out_dput; |
| 312 | |
| 313 | dir = igrab(parent->d_inode); |
| 314 | dput(parent); |
| 315 | |
| 316 | out_dput: |
| 317 | dput(dentry); |
| 318 | out: |
| 319 | return dir ? XFS_I(dir) : NULL; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 323 | * Return the AG of the filestream the file or directory belongs to, or |
| 324 | * NULLAGNUMBER otherwise. |
| 325 | */ |
| 326 | xfs_agnumber_t |
| 327 | xfs_filestream_lookup_ag( |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 328 | struct xfs_inode *ip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 329 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 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; |
| 334 | int ref = 0; |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 335 | struct xfs_mru_cache_elem *mru; |
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 | ASSERT(S_ISREG(ip->i_d.di_mode)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 338 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 339 | pip = xfs_filestream_get_parent(ip); |
| 340 | if (!pip) |
| 341 | goto out; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 342 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 343 | mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); |
| 344 | if (!mru) |
| 345 | goto out; |
| 346 | |
| 347 | item = container_of(mru, struct xfs_fstrm_item, mru); |
| 348 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 349 | ag = item->ag; |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 350 | xfs_mru_cache_done(mp->m_filestream); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 351 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 352 | ref = xfs_filestream_peek_ag(ip->i_mount, ag); |
| 353 | out: |
| 354 | TRACE_LOOKUP(mp, ip, pip, ag, ref); |
| 355 | IRELE(pip); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 356 | return ag; |
| 357 | } |
| 358 | |
| 359 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 360 | * Make sure a directory has a filestream associated with it. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 361 | * |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 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. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 365 | */ |
| 366 | int |
| 367 | xfs_filestream_associate( |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 368 | struct xfs_inode *pip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 369 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 370 | struct xfs_mount *mp = pip->i_mount; |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 371 | struct xfs_mru_cache_elem *mru; |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 372 | xfs_agnumber_t startag, ag; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 373 | |
Al Viro | 0320937 | 2011-07-25 20:54:24 -0400 | [diff] [blame] | 374 | ASSERT(S_ISDIR(pip->i_d.di_mode)); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 375 | |
| 376 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 377 | * If the directory already has a file stream associated we're done. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 378 | */ |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 379 | mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); |
| 380 | if (mru) { |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 381 | xfs_mru_cache_done(mp->m_filestream); |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 382 | return 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Set the starting AG using the rotor for inode32, otherwise |
| 387 | * use the directory inode's AG. |
| 388 | */ |
| 389 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 390 | xfs_agnumber_t rotorstep = xfs_rotorstep; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 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); |
| 394 | } else |
| 395 | startag = XFS_INO_TO_AGNO(mp, pip->i_ino); |
| 396 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 397 | return xfs_filestream_pick_ag(pip, startag, &ag, 0, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 401 | * Pick a new allocation group for the current file and its file stream. |
| 402 | * |
| 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. |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 405 | */ |
| 406 | int |
| 407 | xfs_filestream_new_ag( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 408 | struct xfs_bmalloca *ap, |
| 409 | xfs_agnumber_t *agp) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 410 | { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 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; |
| 415 | int flags, err = 0; |
| 416 | struct xfs_mru_cache_elem *mru; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 417 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 418 | *agp = NULLAGNUMBER; |
| 419 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 420 | pip = xfs_filestream_get_parent(ip); |
| 421 | if (!pip) |
| 422 | goto exit; |
| 423 | |
| 424 | mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino); |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 425 | if (mru) { |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 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; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 429 | } |
| 430 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 431 | flags = (ap->userdata ? XFS_PICK_USERDATA : 0) | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 432 | (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 433 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 434 | err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 435 | |
| 436 | /* |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 437 | * 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] | 438 | */ |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 439 | if (mru) |
| 440 | xfs_fstrm_free_func(mru); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 441 | |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 442 | IRELE(pip); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 443 | exit: |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 444 | if (*agp == NULLAGNUMBER) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 445 | *agp = 0; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 446 | return err; |
| 447 | } |
| 448 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 449 | void |
| 450 | xfs_filestream_deassociate( |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 451 | struct xfs_inode *ip) |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 452 | { |
Christoph Hellwig | 22328d7 | 2014-04-23 07:11:51 +1000 | [diff] [blame] | 453 | xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 454 | } |
Christoph Hellwig | 2cd2ef6 | 2014-04-23 07:11:51 +1000 | [diff] [blame^] | 455 | |
| 456 | int |
| 457 | xfs_filestream_mount( |
| 458 | xfs_mount_t *mp) |
| 459 | { |
| 460 | /* |
| 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 |
| 465 | * groups. |
| 466 | */ |
| 467 | return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10, |
| 468 | 10, xfs_fstrm_free_func); |
| 469 | } |
| 470 | |
| 471 | void |
| 472 | xfs_filestream_unmount( |
| 473 | xfs_mount_t *mp) |
| 474 | { |
| 475 | xfs_mru_cache_destroy(mp->m_filestream); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /* needs to return a positive errno for the init path */ |
| 480 | int |
| 481 | xfs_filestream_init(void) |
| 482 | { |
| 483 | item_zone = kmem_zone_init(sizeof(struct xfs_fstrm_item), "fstrm_item"); |
| 484 | if (!item_zone) |
| 485 | return -ENOMEM; |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | void |
| 490 | xfs_filestream_uninit(void) |
| 491 | { |
| 492 | kmem_zone_destroy(item_zone); |
| 493 | } |