Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +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" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 22 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_btree.h" |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 30 | #include "xfs_rmap.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_alloc.h" |
Dave Chinner | efc27b5 | 2012-04-29 10:39:43 +0000 | [diff] [blame] | 33 | #include "xfs_extent_busy.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_error.h" |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 35 | #include "xfs_cksum.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 36 | #include "xfs_trace.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 37 | #include "xfs_trans.h" |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 38 | #include "xfs_buf_item.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 39 | #include "xfs_log.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 40 | #include "xfs_ag_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Dave Chinner | c999a22 | 2012-03-22 05:15:07 +0000 | [diff] [blame] | 42 | struct workqueue_struct *xfs_alloc_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b))) |
| 45 | |
| 46 | #define XFSA_FIXUP_BNO_OK 1 |
| 47 | #define XFSA_FIXUP_CNT_OK 2 |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *); |
| 50 | STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *); |
| 51 | STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *); |
| 52 | STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *, |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 53 | xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Darrick J. Wong | af30dfa | 2016-10-03 09:11:17 -0700 | [diff] [blame] | 55 | unsigned int |
| 56 | xfs_refc_block( |
| 57 | struct xfs_mount *mp) |
| 58 | { |
| 59 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 60 | return XFS_RMAP_BLOCK(mp) + 1; |
| 61 | if (xfs_sb_version_hasfinobt(&mp->m_sb)) |
| 62 | return XFS_FIBT_BLOCK(mp) + 1; |
| 63 | return XFS_IBT_BLOCK(mp) + 1; |
| 64 | } |
| 65 | |
Darrick J. Wong | 8018026 | 2016-08-03 11:31:47 +1000 | [diff] [blame] | 66 | xfs_extlen_t |
| 67 | xfs_prealloc_blocks( |
| 68 | struct xfs_mount *mp) |
| 69 | { |
Darrick J. Wong | af30dfa | 2016-10-03 09:11:17 -0700 | [diff] [blame] | 70 | if (xfs_sb_version_hasreflink(&mp->m_sb)) |
| 71 | return xfs_refc_block(mp) + 1; |
Darrick J. Wong | 8018026 | 2016-08-03 11:31:47 +1000 | [diff] [blame] | 72 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 73 | return XFS_RMAP_BLOCK(mp) + 1; |
| 74 | if (xfs_sb_version_hasfinobt(&mp->m_sb)) |
| 75 | return XFS_FIBT_BLOCK(mp) + 1; |
| 76 | return XFS_IBT_BLOCK(mp) + 1; |
| 77 | } |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | /* |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 80 | * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of |
| 81 | * AGF buffer (PV 947395), we place constraints on the relationship among |
| 82 | * actual allocations for data blocks, freelist blocks, and potential file data |
| 83 | * bmap btree blocks. However, these restrictions may result in no actual space |
| 84 | * allocated for a delayed extent, for example, a data block in a certain AG is |
| 85 | * allocated but there is no additional block for the additional bmap btree |
| 86 | * block due to a split of the bmap btree of the file. The result of this may |
| 87 | * lead to an infinite loop when the file gets flushed to disk and all delayed |
| 88 | * extents need to be actually allocated. To get around this, we explicitly set |
| 89 | * aside a few blocks which will not be reserved in delayed allocation. |
| 90 | * |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 91 | * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a |
| 92 | * potential split of the file's bmap btree. |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 93 | */ |
| 94 | unsigned int |
| 95 | xfs_alloc_set_aside( |
| 96 | struct xfs_mount *mp) |
| 97 | { |
| 98 | unsigned int blocks; |
| 99 | |
| 100 | blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE); |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 101 | return blocks; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * When deciding how much space to allocate out of an AG, we limit the |
| 106 | * allocation maximum size to the size the AG. However, we cannot use all the |
| 107 | * blocks in the AG - some are permanently used by metadata. These |
| 108 | * blocks are generally: |
| 109 | * - the AG superblock, AGF, AGI and AGFL |
| 110 | * - the AGF (bno and cnt) and AGI btree root blocks, and optionally |
| 111 | * the AGI free inode and rmap btree root blocks. |
| 112 | * - blocks on the AGFL according to xfs_alloc_set_aside() limits |
| 113 | * - the rmapbt root block |
| 114 | * |
| 115 | * The AG headers are sector sized, so the amount of space they take up is |
| 116 | * dependent on filesystem geometry. The others are all single blocks. |
| 117 | */ |
| 118 | unsigned int |
| 119 | xfs_alloc_ag_max_usable( |
| 120 | struct xfs_mount *mp) |
| 121 | { |
| 122 | unsigned int blocks; |
| 123 | |
| 124 | blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */ |
| 125 | blocks += XFS_ALLOC_AGFL_RESERVE; |
| 126 | blocks += 3; /* AGF, AGI btree root blocks */ |
| 127 | if (xfs_sb_version_hasfinobt(&mp->m_sb)) |
| 128 | blocks++; /* finobt root block */ |
| 129 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 130 | blocks++; /* rmap root block */ |
Darrick J. Wong | d0e853f | 2016-10-03 09:11:24 -0700 | [diff] [blame] | 131 | if (xfs_sb_version_hasreflink(&mp->m_sb)) |
| 132 | blocks++; /* refcount root block */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 133 | |
| 134 | return mp->m_sb.sb_agblocks - blocks; |
| 135 | } |
| 136 | |
| 137 | /* |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 138 | * Lookup the record equal to [bno, len] in the btree given by cur. |
| 139 | */ |
| 140 | STATIC int /* error */ |
| 141 | xfs_alloc_lookup_eq( |
| 142 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 143 | xfs_agblock_t bno, /* starting block of extent */ |
| 144 | xfs_extlen_t len, /* length of extent */ |
| 145 | int *stat) /* success/failure */ |
| 146 | { |
| 147 | cur->bc_rec.a.ar_startblock = bno; |
| 148 | cur->bc_rec.a.ar_blockcount = len; |
| 149 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Lookup the first record greater than or equal to [bno, len] |
| 154 | * in the btree given by cur. |
| 155 | */ |
Dave Chinner | a66d636 | 2012-03-22 05:15:12 +0000 | [diff] [blame] | 156 | int /* error */ |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 157 | xfs_alloc_lookup_ge( |
| 158 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 159 | xfs_agblock_t bno, /* starting block of extent */ |
| 160 | xfs_extlen_t len, /* length of extent */ |
| 161 | int *stat) /* success/failure */ |
| 162 | { |
| 163 | cur->bc_rec.a.ar_startblock = bno; |
| 164 | cur->bc_rec.a.ar_blockcount = len; |
| 165 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Lookup the first record less than or equal to [bno, len] |
| 170 | * in the btree given by cur. |
| 171 | */ |
Eric Sandeen | 0d5a75e | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 172 | static int /* error */ |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 173 | xfs_alloc_lookup_le( |
| 174 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 175 | xfs_agblock_t bno, /* starting block of extent */ |
| 176 | xfs_extlen_t len, /* length of extent */ |
| 177 | int *stat) /* success/failure */ |
| 178 | { |
| 179 | cur->bc_rec.a.ar_startblock = bno; |
| 180 | cur->bc_rec.a.ar_blockcount = len; |
| 181 | return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); |
| 182 | } |
| 183 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 184 | /* |
| 185 | * Update the record referred to by cur to the value given |
| 186 | * by [bno, len]. |
| 187 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 188 | */ |
| 189 | STATIC int /* error */ |
| 190 | xfs_alloc_update( |
| 191 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 192 | xfs_agblock_t bno, /* starting block of extent */ |
| 193 | xfs_extlen_t len) /* length of extent */ |
| 194 | { |
| 195 | union xfs_btree_rec rec; |
| 196 | |
| 197 | rec.alloc.ar_startblock = cpu_to_be32(bno); |
| 198 | rec.alloc.ar_blockcount = cpu_to_be32(len); |
| 199 | return xfs_btree_update(cur, &rec); |
| 200 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 201 | |
| 202 | /* |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 203 | * Get the data from the pointed-to record. |
| 204 | */ |
Christoph Hellwig | a46db60 | 2011-01-07 13:02:04 +0000 | [diff] [blame] | 205 | int /* error */ |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 206 | xfs_alloc_get_rec( |
| 207 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 208 | xfs_agblock_t *bno, /* output: starting block of extent */ |
| 209 | xfs_extlen_t *len, /* output: length of extent */ |
| 210 | int *stat) /* output: success/failure */ |
| 211 | { |
| 212 | union xfs_btree_rec *rec; |
| 213 | int error; |
| 214 | |
| 215 | error = xfs_btree_get_rec(cur, &rec, stat); |
| 216 | if (!error && *stat == 1) { |
| 217 | *bno = be32_to_cpu(rec->alloc.ar_startblock); |
| 218 | *len = be32_to_cpu(rec->alloc.ar_blockcount); |
| 219 | } |
| 220 | return error; |
| 221 | } |
| 222 | |
| 223 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | * Compute aligned version of the found extent. |
| 225 | * Takes alignment and min length into account. |
| 226 | */ |
David Chinner | 12375c8 | 2008-04-10 12:21:32 +1000 | [diff] [blame] | 227 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | xfs_alloc_compute_aligned( |
Christoph Hellwig | 86fa8af | 2011-03-04 12:59:54 +0000 | [diff] [blame] | 229 | xfs_alloc_arg_t *args, /* allocation argument structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | xfs_agblock_t foundbno, /* starting block in found extent */ |
| 231 | xfs_extlen_t foundlen, /* length in found extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | xfs_agblock_t *resbno, /* result block number */ |
| 233 | xfs_extlen_t *reslen) /* result length */ |
| 234 | { |
| 235 | xfs_agblock_t bno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | xfs_extlen_t len; |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 237 | xfs_extlen_t diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 239 | /* Trim busy sections out of found extent */ |
Dave Chinner | 4ecbfe6 | 2012-04-29 10:41:10 +0000 | [diff] [blame] | 240 | xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 241 | |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 242 | /* |
| 243 | * If we have a largish extent that happens to start before min_agbno, |
| 244 | * see if we can shift it into range... |
| 245 | */ |
| 246 | if (bno < args->min_agbno && bno + len > args->min_agbno) { |
| 247 | diff = args->min_agbno - bno; |
| 248 | if (len > diff) { |
| 249 | bno += diff; |
| 250 | len -= diff; |
| 251 | } |
| 252 | } |
| 253 | |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 254 | if (args->alignment > 1 && len >= args->minlen) { |
| 255 | xfs_agblock_t aligned_bno = roundup(bno, args->alignment); |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 256 | |
| 257 | diff = aligned_bno - bno; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 258 | |
| 259 | *resbno = aligned_bno; |
| 260 | *reslen = diff >= len ? 0 : len - diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } else { |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 262 | *resbno = bno; |
| 263 | *reslen = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Compute best start block and diff for "near" allocations. |
| 269 | * freelen >= wantlen already checked by caller. |
| 270 | */ |
| 271 | STATIC xfs_extlen_t /* difference value (absolute) */ |
| 272 | xfs_alloc_compute_diff( |
| 273 | xfs_agblock_t wantbno, /* target starting block */ |
| 274 | xfs_extlen_t wantlen, /* target length */ |
| 275 | xfs_extlen_t alignment, /* target alignment */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 276 | int datatype, /* are we allocating data? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | xfs_agblock_t freebno, /* freespace's starting block */ |
| 278 | xfs_extlen_t freelen, /* freespace's length */ |
| 279 | xfs_agblock_t *newbnop) /* result: best start block from free */ |
| 280 | { |
| 281 | xfs_agblock_t freeend; /* end of freespace extent */ |
| 282 | xfs_agblock_t newbno1; /* return block number */ |
| 283 | xfs_agblock_t newbno2; /* other new block number */ |
| 284 | xfs_extlen_t newlen1=0; /* length with newbno1 */ |
| 285 | xfs_extlen_t newlen2=0; /* length with newbno2 */ |
| 286 | xfs_agblock_t wantend; /* end of target extent */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 287 | bool userdata = xfs_alloc_is_userdata(datatype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | ASSERT(freelen >= wantlen); |
| 290 | freeend = freebno + freelen; |
| 291 | wantend = wantbno + wantlen; |
Jan Kara | 211d022 | 2013-04-11 22:09:56 +0200 | [diff] [blame] | 292 | /* |
| 293 | * We want to allocate from the start of a free extent if it is past |
| 294 | * the desired block or if we are allocating user data and the free |
| 295 | * extent is before desired block. The second case is there to allow |
| 296 | * for contiguous allocation from the remaining free space if the file |
| 297 | * grows in the short term. |
| 298 | */ |
| 299 | if (freebno >= wantbno || (userdata && freeend < wantend)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if ((newbno1 = roundup(freebno, alignment)) >= freeend) |
| 301 | newbno1 = NULLAGBLOCK; |
| 302 | } else if (freeend >= wantend && alignment > 1) { |
| 303 | newbno1 = roundup(wantbno, alignment); |
| 304 | newbno2 = newbno1 - alignment; |
| 305 | if (newbno1 >= freeend) |
| 306 | newbno1 = NULLAGBLOCK; |
| 307 | else |
| 308 | newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1); |
| 309 | if (newbno2 < freebno) |
| 310 | newbno2 = NULLAGBLOCK; |
| 311 | else |
| 312 | newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2); |
| 313 | if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) { |
| 314 | if (newlen1 < newlen2 || |
| 315 | (newlen1 == newlen2 && |
| 316 | XFS_ABSDIFF(newbno1, wantbno) > |
| 317 | XFS_ABSDIFF(newbno2, wantbno))) |
| 318 | newbno1 = newbno2; |
| 319 | } else if (newbno2 != NULLAGBLOCK) |
| 320 | newbno1 = newbno2; |
| 321 | } else if (freeend >= wantend) { |
| 322 | newbno1 = wantbno; |
| 323 | } else if (alignment > 1) { |
| 324 | newbno1 = roundup(freeend - wantlen, alignment); |
| 325 | if (newbno1 > freeend - wantlen && |
| 326 | newbno1 - alignment >= freebno) |
| 327 | newbno1 -= alignment; |
| 328 | else if (newbno1 >= freeend) |
| 329 | newbno1 = NULLAGBLOCK; |
| 330 | } else |
| 331 | newbno1 = freeend - wantlen; |
| 332 | *newbnop = newbno1; |
| 333 | return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Fix up the length, based on mod and prod. |
| 338 | * len should be k * prod + mod for some k. |
| 339 | * If len is too small it is returned unchanged. |
| 340 | * If len hits maxlen it is left alone. |
| 341 | */ |
| 342 | STATIC void |
| 343 | xfs_alloc_fix_len( |
| 344 | xfs_alloc_arg_t *args) /* allocation argument structure */ |
| 345 | { |
| 346 | xfs_extlen_t k; |
| 347 | xfs_extlen_t rlen; |
| 348 | |
| 349 | ASSERT(args->mod < args->prod); |
| 350 | rlen = args->len; |
| 351 | ASSERT(rlen >= args->minlen); |
| 352 | ASSERT(rlen <= args->maxlen); |
| 353 | if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen || |
| 354 | (args->mod == 0 && rlen < args->prod)) |
| 355 | return; |
| 356 | k = rlen % args->prod; |
| 357 | if (k == args->mod) |
| 358 | return; |
Jan Kara | 3026511 | 2014-06-06 16:06:37 +1000 | [diff] [blame] | 359 | if (k > args->mod) |
| 360 | rlen = rlen - (k - args->mod); |
| 361 | else |
| 362 | rlen = rlen - args->prod + (args->mod - k); |
Dave Chinner | 3790a8c | 2015-02-24 10:16:04 +1100 | [diff] [blame] | 363 | /* casts to (int) catch length underflows */ |
Jan Kara | 3026511 | 2014-06-06 16:06:37 +1000 | [diff] [blame] | 364 | if ((int)rlen < (int)args->minlen) |
| 365 | return; |
| 366 | ASSERT(rlen >= args->minlen && rlen <= args->maxlen); |
| 367 | ASSERT(rlen % args->prod == args->mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | args->len = rlen; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Fix up length if there is too little space left in the a.g. |
| 373 | * Return 1 if ok, 0 if too little, should give up. |
| 374 | */ |
| 375 | STATIC int |
| 376 | xfs_alloc_fix_minleft( |
| 377 | xfs_alloc_arg_t *args) /* allocation argument structure */ |
| 378 | { |
| 379 | xfs_agf_t *agf; /* a.g. freelist header */ |
| 380 | int diff; /* free space difference */ |
| 381 | |
| 382 | if (args->minleft == 0) |
| 383 | return 1; |
| 384 | agf = XFS_BUF_TO_AGF(args->agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 385 | diff = be32_to_cpu(agf->agf_freeblks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | - args->len - args->minleft; |
| 387 | if (diff >= 0) |
| 388 | return 1; |
| 389 | args->len += diff; /* shrink the allocated space */ |
Dave Chinner | 3790a8c | 2015-02-24 10:16:04 +1100 | [diff] [blame] | 390 | /* casts to (int) catch length underflows */ |
| 391 | if ((int)args->len >= (int)args->minlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return 1; |
| 393 | args->agbno = NULLAGBLOCK; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Update the two btrees, logically removing from freespace the extent |
| 399 | * starting at rbno, rlen blocks. The extent is contained within the |
| 400 | * actual (current) free extent fbno for flen blocks. |
| 401 | * Flags are passed in indicating whether the cursors are set to the |
| 402 | * relevant records. |
| 403 | */ |
| 404 | STATIC int /* error code */ |
| 405 | xfs_alloc_fixup_trees( |
| 406 | xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */ |
| 407 | xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */ |
| 408 | xfs_agblock_t fbno, /* starting block of free extent */ |
| 409 | xfs_extlen_t flen, /* length of free extent */ |
| 410 | xfs_agblock_t rbno, /* starting block of returned extent */ |
| 411 | xfs_extlen_t rlen, /* length of returned extent */ |
| 412 | int flags) /* flags, XFSA_FIXUP_... */ |
| 413 | { |
| 414 | int error; /* error code */ |
| 415 | int i; /* operation results */ |
| 416 | xfs_agblock_t nfbno1; /* first new free startblock */ |
| 417 | xfs_agblock_t nfbno2; /* second new free startblock */ |
| 418 | xfs_extlen_t nflen1=0; /* first new free length */ |
| 419 | xfs_extlen_t nflen2=0; /* second new free length */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 420 | struct xfs_mount *mp; |
| 421 | |
| 422 | mp = cnt_cur->bc_mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * Look up the record in the by-size tree if necessary. |
| 426 | */ |
| 427 | if (flags & XFSA_FIXUP_CNT_OK) { |
| 428 | #ifdef DEBUG |
| 429 | if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i))) |
| 430 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 431 | XFS_WANT_CORRUPTED_RETURN(mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | i == 1 && nfbno1 == fbno && nflen1 == flen); |
| 433 | #endif |
| 434 | } else { |
| 435 | if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i))) |
| 436 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 437 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | /* |
| 440 | * Look up the record in the by-block tree if necessary. |
| 441 | */ |
| 442 | if (flags & XFSA_FIXUP_BNO_OK) { |
| 443 | #ifdef DEBUG |
| 444 | if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i))) |
| 445 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 446 | XFS_WANT_CORRUPTED_RETURN(mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | i == 1 && nfbno1 == fbno && nflen1 == flen); |
| 448 | #endif |
| 449 | } else { |
| 450 | if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i))) |
| 451 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 452 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 455 | #ifdef DEBUG |
| 456 | if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) { |
| 457 | struct xfs_btree_block *bnoblock; |
| 458 | struct xfs_btree_block *cntblock; |
| 459 | |
| 460 | bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]); |
| 461 | cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]); |
| 462 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 463 | XFS_WANT_CORRUPTED_RETURN(mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 464 | bnoblock->bb_numrecs == cntblock->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | #endif |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* |
| 469 | * Deal with all four cases: the allocated record is contained |
| 470 | * within the freespace record, so we can have new freespace |
| 471 | * at either (or both) end, or no freespace remaining. |
| 472 | */ |
| 473 | if (rbno == fbno && rlen == flen) |
| 474 | nfbno1 = nfbno2 = NULLAGBLOCK; |
| 475 | else if (rbno == fbno) { |
| 476 | nfbno1 = rbno + rlen; |
| 477 | nflen1 = flen - rlen; |
| 478 | nfbno2 = NULLAGBLOCK; |
| 479 | } else if (rbno + rlen == fbno + flen) { |
| 480 | nfbno1 = fbno; |
| 481 | nflen1 = flen - rlen; |
| 482 | nfbno2 = NULLAGBLOCK; |
| 483 | } else { |
| 484 | nfbno1 = fbno; |
| 485 | nflen1 = rbno - fbno; |
| 486 | nfbno2 = rbno + rlen; |
| 487 | nflen2 = (fbno + flen) - nfbno2; |
| 488 | } |
| 489 | /* |
| 490 | * Delete the entry from the by-size btree. |
| 491 | */ |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 492 | if ((error = xfs_btree_delete(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 494 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | /* |
| 496 | * Add new by-size btree entry(s). |
| 497 | */ |
| 498 | if (nfbno1 != NULLAGBLOCK) { |
| 499 | if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i))) |
| 500 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 501 | XFS_WANT_CORRUPTED_RETURN(mp, i == 0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 502 | if ((error = xfs_btree_insert(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 504 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | if (nfbno2 != NULLAGBLOCK) { |
| 507 | if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i))) |
| 508 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 509 | XFS_WANT_CORRUPTED_RETURN(mp, i == 0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 510 | if ((error = xfs_btree_insert(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 512 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | /* |
| 515 | * Fix up the by-block btree entry(s). |
| 516 | */ |
| 517 | if (nfbno1 == NULLAGBLOCK) { |
| 518 | /* |
| 519 | * No remaining freespace, just delete the by-block tree entry. |
| 520 | */ |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 521 | if ((error = xfs_btree_delete(bno_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 523 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } else { |
| 525 | /* |
| 526 | * Update the by-block entry to start later|be shorter. |
| 527 | */ |
| 528 | if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1))) |
| 529 | return error; |
| 530 | } |
| 531 | if (nfbno2 != NULLAGBLOCK) { |
| 532 | /* |
| 533 | * 2 resulting free entries, need to add one. |
| 534 | */ |
| 535 | if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i))) |
| 536 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 537 | XFS_WANT_CORRUPTED_RETURN(mp, i == 0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 538 | if ((error = xfs_btree_insert(bno_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 540 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | return 0; |
| 543 | } |
| 544 | |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 545 | static bool |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 546 | xfs_agfl_verify( |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 547 | struct xfs_buf *bp) |
| 548 | { |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 549 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 550 | struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp); |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 551 | int i; |
| 552 | |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 553 | if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid)) |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 554 | return false; |
| 555 | if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC) |
| 556 | return false; |
| 557 | /* |
| 558 | * during growfs operations, the perag is not fully initialised, |
| 559 | * so we can't use it for any useful checking. growfs ensures we can't |
| 560 | * use it by using uncached buffers that don't have the perag attached |
| 561 | * so we can detect and avoid this problem. |
| 562 | */ |
| 563 | if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno) |
| 564 | return false; |
| 565 | |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 566 | for (i = 0; i < XFS_AGFL_SIZE(mp); i++) { |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 567 | if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK && |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 568 | be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks) |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 569 | return false; |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 570 | } |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 571 | |
| 572 | return xfs_log_check_lsn(mp, |
| 573 | be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)); |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 574 | } |
| 575 | |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 576 | static void |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 577 | xfs_agfl_read_verify( |
| 578 | struct xfs_buf *bp) |
| 579 | { |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 580 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 581 | |
| 582 | /* |
| 583 | * There is no verification of non-crc AGFLs because mkfs does not |
| 584 | * initialise the AGFL to zero or NULL. Hence the only valid part of the |
| 585 | * AGFL is what the AGF says is active. We can't get to the AGF, so we |
| 586 | * can't verify just those entries are valid. |
| 587 | */ |
| 588 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 589 | return; |
| 590 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 591 | if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 592 | xfs_buf_ioerror(bp, -EFSBADCRC); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 593 | else if (!xfs_agfl_verify(bp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 594 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 595 | |
| 596 | if (bp->b_error) |
| 597 | xfs_verifier_error(bp); |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void |
| 601 | xfs_agfl_write_verify( |
| 602 | struct xfs_buf *bp) |
| 603 | { |
| 604 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 605 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 606 | |
| 607 | /* no verification of non-crc AGFLs */ |
| 608 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 609 | return; |
| 610 | |
| 611 | if (!xfs_agfl_verify(bp)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 612 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 613 | xfs_verifier_error(bp); |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
| 617 | if (bip) |
| 618 | XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 619 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 620 | xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF); |
Dave Chinner | bb80c6d | 2012-11-12 22:54:06 +1100 | [diff] [blame] | 621 | } |
| 622 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 623 | const struct xfs_buf_ops xfs_agfl_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 624 | .name = "xfs_agfl", |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 625 | .verify_read = xfs_agfl_read_verify, |
| 626 | .verify_write = xfs_agfl_write_verify, |
| 627 | }; |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | /* |
| 630 | * Read in the allocation group free block array. |
| 631 | */ |
| 632 | STATIC int /* error */ |
| 633 | xfs_alloc_read_agfl( |
| 634 | xfs_mount_t *mp, /* mount point structure */ |
| 635 | xfs_trans_t *tp, /* transaction pointer */ |
| 636 | xfs_agnumber_t agno, /* allocation group number */ |
| 637 | xfs_buf_t **bpp) /* buffer for the ag free block array */ |
| 638 | { |
| 639 | xfs_buf_t *bp; /* return value */ |
| 640 | int error; |
| 641 | |
| 642 | ASSERT(agno != NULLAGNUMBER); |
| 643 | error = xfs_trans_read_buf( |
| 644 | mp, tp, mp->m_ddev_targp, |
| 645 | XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)), |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 646 | XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (error) |
| 648 | return error; |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 649 | xfs_buf_set_ref(bp, XFS_AGFL_REF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | *bpp = bp; |
| 651 | return 0; |
| 652 | } |
| 653 | |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 654 | STATIC int |
| 655 | xfs_alloc_update_counters( |
| 656 | struct xfs_trans *tp, |
| 657 | struct xfs_perag *pag, |
| 658 | struct xfs_buf *agbp, |
| 659 | long len) |
| 660 | { |
| 661 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); |
| 662 | |
| 663 | pag->pagf_freeblks += len; |
| 664 | be32_add_cpu(&agf->agf_freeblks, len); |
| 665 | |
| 666 | xfs_trans_agblocks_delta(tp, len); |
| 667 | if (unlikely(be32_to_cpu(agf->agf_freeblks) > |
| 668 | be32_to_cpu(agf->agf_length))) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 669 | return -EFSCORRUPTED; |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 670 | |
| 671 | xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS); |
| 672 | return 0; |
| 673 | } |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | /* |
| 676 | * Allocation group level functions. |
| 677 | */ |
| 678 | |
| 679 | /* |
| 680 | * Allocate a variable extent in the allocation group agno. |
| 681 | * Type and bno are used to determine where in the allocation group the |
| 682 | * extent will start. |
| 683 | * Extent's length (returned in *len) will be between minlen and maxlen, |
| 684 | * and of the form k * prod + mod unless there's nothing that large. |
| 685 | * Return the starting a.g. block, or NULLAGBLOCK if we can't do it. |
| 686 | */ |
| 687 | STATIC int /* error */ |
| 688 | xfs_alloc_ag_vextent( |
| 689 | xfs_alloc_arg_t *args) /* argument structure for allocation */ |
| 690 | { |
| 691 | int error=0; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 692 | xfs_extlen_t reservation; |
| 693 | xfs_extlen_t oldmax; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | ASSERT(args->minlen > 0); |
| 696 | ASSERT(args->maxlen > 0); |
| 697 | ASSERT(args->minlen <= args->maxlen); |
| 698 | ASSERT(args->mod < args->prod); |
| 699 | ASSERT(args->alignment > 0); |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * Clamp maxlen to the amount of free space minus any reservations |
| 703 | * that have been made. |
| 704 | */ |
| 705 | oldmax = args->maxlen; |
| 706 | reservation = xfs_ag_resv_needed(args->pag, args->resv); |
| 707 | if (args->maxlen > args->pag->pagf_freeblks - reservation) |
| 708 | args->maxlen = args->pag->pagf_freeblks - reservation; |
| 709 | if (args->maxlen == 0) { |
| 710 | args->agbno = NULLAGBLOCK; |
| 711 | args->maxlen = oldmax; |
| 712 | return 0; |
| 713 | } |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* |
| 716 | * Branch to correct routine based on the type. |
| 717 | */ |
| 718 | args->wasfromfl = 0; |
| 719 | switch (args->type) { |
| 720 | case XFS_ALLOCTYPE_THIS_AG: |
| 721 | error = xfs_alloc_ag_vextent_size(args); |
| 722 | break; |
| 723 | case XFS_ALLOCTYPE_NEAR_BNO: |
| 724 | error = xfs_alloc_ag_vextent_near(args); |
| 725 | break; |
| 726 | case XFS_ALLOCTYPE_THIS_BNO: |
| 727 | error = xfs_alloc_ag_vextent_exact(args); |
| 728 | break; |
| 729 | default: |
| 730 | ASSERT(0); |
| 731 | /* NOTREACHED */ |
| 732 | } |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 733 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 734 | args->maxlen = oldmax; |
| 735 | |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 736 | if (error || args->agbno == NULLAGBLOCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 739 | ASSERT(args->len >= args->minlen); |
| 740 | ASSERT(args->len <= args->maxlen); |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 741 | ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL); |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 742 | ASSERT(args->agbno % args->alignment == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 744 | /* if not file data, insert new block into the reverse map btree */ |
| 745 | if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) { |
| 746 | error = xfs_rmap_alloc(args->tp, args->agbp, args->agno, |
| 747 | args->agbno, args->len, &args->oinfo); |
| 748 | if (error) |
| 749 | return error; |
| 750 | } |
| 751 | |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 752 | if (!args->wasfromfl) { |
| 753 | error = xfs_alloc_update_counters(args->tp, args->pag, |
| 754 | args->agbp, |
| 755 | -((long)(args->len))); |
| 756 | if (error) |
| 757 | return error; |
| 758 | |
Dave Chinner | 4ecbfe6 | 2012-04-29 10:41:10 +0000 | [diff] [blame] | 759 | ASSERT(!xfs_extent_busy_search(args->mp, args->agno, |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 760 | args->agbno, args->len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 762 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 763 | xfs_ag_resv_alloc_extent(args->pag, args->resv, args); |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 764 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 765 | XFS_STATS_INC(args->mp, xs_allocx); |
| 766 | XFS_STATS_ADD(args->mp, xs_allocb, args->len); |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 767 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Allocate a variable extent at exactly agno/bno. |
| 772 | * Extent's length (returned in *len) will be between minlen and maxlen, |
| 773 | * and of the form k * prod + mod unless there's nothing that large. |
| 774 | * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it. |
| 775 | */ |
| 776 | STATIC int /* error */ |
| 777 | xfs_alloc_ag_vextent_exact( |
| 778 | xfs_alloc_arg_t *args) /* allocation argument structure */ |
| 779 | { |
| 780 | xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */ |
| 781 | xfs_btree_cur_t *cnt_cur;/* by count btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | int error; |
| 783 | xfs_agblock_t fbno; /* start block of found extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | xfs_extlen_t flen; /* length of found extent */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 785 | xfs_agblock_t tbno; /* start block of trimmed extent */ |
| 786 | xfs_extlen_t tlen; /* length of trimmed extent */ |
| 787 | xfs_agblock_t tend; /* end block of trimmed extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | int i; /* success/failure of operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
| 790 | ASSERT(args->alignment == 1); |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | /* |
| 793 | * Allocate/initialize a cursor for the by-number freespace btree. |
| 794 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 795 | bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp, |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 796 | args->agno, XFS_BTNUM_BNO); |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | /* |
| 799 | * Lookup bno and minlen in the btree (minlen is irrelevant, really). |
| 800 | * Look for the closest free block <= bno, it must contain bno |
| 801 | * if any free block does. |
| 802 | */ |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 803 | error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i); |
| 804 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | goto error0; |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 806 | if (!i) |
| 807 | goto not_found; |
| 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | /* |
| 810 | * Grab the freespace record. |
| 811 | */ |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 812 | error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i); |
| 813 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 815 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | ASSERT(fbno <= args->agbno); |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 819 | * Check for overlapping busy extents. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | */ |
Dave Chinner | 4ecbfe6 | 2012-04-29 10:41:10 +0000 | [diff] [blame] | 821 | xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 822 | |
| 823 | /* |
| 824 | * Give up if the start of the extent is busy, or the freespace isn't |
| 825 | * long enough for the minimum request. |
| 826 | */ |
| 827 | if (tbno > args->agbno) |
| 828 | goto not_found; |
| 829 | if (tlen < args->minlen) |
| 830 | goto not_found; |
| 831 | tend = tbno + tlen; |
| 832 | if (tend < args->agbno + args->minlen) |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 833 | goto not_found; |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* |
| 836 | * End of extent will be smaller of the freespace end and the |
| 837 | * maximal requested end. |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 838 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | * Fix the length according to mod and prod if given. |
| 840 | */ |
Chandra Seetharaman | 81463b1 | 2011-06-09 16:47:49 +0000 | [diff] [blame] | 841 | args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen) |
| 842 | - args->agbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | xfs_alloc_fix_len(args); |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 844 | if (!xfs_alloc_fix_minleft(args)) |
| 845 | goto not_found; |
| 846 | |
Chandra Seetharaman | 81463b1 | 2011-06-09 16:47:49 +0000 | [diff] [blame] | 847 | ASSERT(args->agbno + args->len <= tend); |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* |
Chandra Seetharaman | 81463b1 | 2011-06-09 16:47:49 +0000 | [diff] [blame] | 850 | * We are allocating agbno for args->len |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | * Allocate/initialize a cursor for the by-size btree. |
| 852 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 853 | cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp, |
| 854 | args->agno, XFS_BTNUM_CNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | ASSERT(args->agbno + args->len <= |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 856 | be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length)); |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 857 | error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno, |
| 858 | args->len, XFSA_FIXUP_BNO_OK); |
| 859 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); |
| 861 | goto error0; |
| 862 | } |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); |
| 865 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | args->wasfromfl = 0; |
Christoph Hellwig | 9f9baab | 2010-12-10 15:03:57 +0000 | [diff] [blame] | 868 | trace_xfs_alloc_exact_done(args); |
| 869 | return 0; |
| 870 | |
| 871 | not_found: |
| 872 | /* Didn't find it, return null. */ |
| 873 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); |
| 874 | args->agbno = NULLAGBLOCK; |
| 875 | trace_xfs_alloc_exact_notfound(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | return 0; |
| 877 | |
| 878 | error0: |
| 879 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 880 | trace_xfs_alloc_exact_error(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | return error; |
| 882 | } |
| 883 | |
| 884 | /* |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 885 | * Search the btree in a given direction via the search cursor and compare |
| 886 | * the records found against the good extent we've already found. |
| 887 | */ |
| 888 | STATIC int |
| 889 | xfs_alloc_find_best_extent( |
| 890 | struct xfs_alloc_arg *args, /* allocation argument structure */ |
| 891 | struct xfs_btree_cur **gcur, /* good cursor */ |
| 892 | struct xfs_btree_cur **scur, /* searching cursor */ |
| 893 | xfs_agblock_t gdiff, /* difference for search comparison */ |
| 894 | xfs_agblock_t *sbno, /* extent found by search */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 895 | xfs_extlen_t *slen, /* extent length */ |
| 896 | xfs_agblock_t *sbnoa, /* aligned extent found by search */ |
| 897 | xfs_extlen_t *slena, /* aligned extent length */ |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 898 | int dir) /* 0 = search right, 1 = search left */ |
| 899 | { |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 900 | xfs_agblock_t new; |
| 901 | xfs_agblock_t sdiff; |
| 902 | int error; |
| 903 | int i; |
| 904 | |
| 905 | /* The good extent is perfect, no need to search. */ |
| 906 | if (!gdiff) |
| 907 | goto out_use_good; |
| 908 | |
| 909 | /* |
| 910 | * Look until we find a better one, run out of space or run off the end. |
| 911 | */ |
| 912 | do { |
| 913 | error = xfs_alloc_get_rec(*scur, sbno, slen, &i); |
| 914 | if (error) |
| 915 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 916 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 917 | xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 918 | |
| 919 | /* |
| 920 | * The good extent is closer than this one. |
| 921 | */ |
| 922 | if (!dir) { |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 923 | if (*sbnoa > args->max_agbno) |
| 924 | goto out_use_good; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 925 | if (*sbnoa >= args->agbno + gdiff) |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 926 | goto out_use_good; |
| 927 | } else { |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 928 | if (*sbnoa < args->min_agbno) |
| 929 | goto out_use_good; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 930 | if (*sbnoa <= args->agbno - gdiff) |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 931 | goto out_use_good; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Same distance, compare length and pick the best. |
| 936 | */ |
| 937 | if (*slena >= args->minlen) { |
| 938 | args->len = XFS_EXTLEN_MIN(*slena, args->maxlen); |
| 939 | xfs_alloc_fix_len(args); |
| 940 | |
| 941 | sdiff = xfs_alloc_compute_diff(args->agbno, args->len, |
Jan Kara | 211d022 | 2013-04-11 22:09:56 +0200 | [diff] [blame] | 942 | args->alignment, |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 943 | args->datatype, *sbnoa, |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 944 | *slena, &new); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 945 | |
| 946 | /* |
| 947 | * Choose closer size and invalidate other cursor. |
| 948 | */ |
| 949 | if (sdiff < gdiff) |
| 950 | goto out_use_search; |
| 951 | goto out_use_good; |
| 952 | } |
| 953 | |
| 954 | if (!dir) |
| 955 | error = xfs_btree_increment(*scur, 0, &i); |
| 956 | else |
| 957 | error = xfs_btree_decrement(*scur, 0, &i); |
| 958 | if (error) |
| 959 | goto error0; |
| 960 | } while (i); |
| 961 | |
| 962 | out_use_good: |
| 963 | xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR); |
| 964 | *scur = NULL; |
| 965 | return 0; |
| 966 | |
| 967 | out_use_search: |
| 968 | xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR); |
| 969 | *gcur = NULL; |
| 970 | return 0; |
| 971 | |
| 972 | error0: |
| 973 | /* caller invalidates cursors */ |
| 974 | return error; |
| 975 | } |
| 976 | |
| 977 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | * Allocate a variable extent near bno in the allocation group agno. |
| 979 | * Extent's length (returned in len) will be between minlen and maxlen, |
| 980 | * and of the form k * prod + mod unless there's nothing that large. |
| 981 | * Return the starting a.g. block, or NULLAGBLOCK if we can't do it. |
| 982 | */ |
| 983 | STATIC int /* error */ |
| 984 | xfs_alloc_ag_vextent_near( |
| 985 | xfs_alloc_arg_t *args) /* allocation argument structure */ |
| 986 | { |
| 987 | xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */ |
| 988 | xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */ |
| 989 | xfs_btree_cur_t *cnt_cur; /* cursor for count btree */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | xfs_agblock_t gtbno; /* start bno of right side entry */ |
| 991 | xfs_agblock_t gtbnoa; /* aligned ... */ |
| 992 | xfs_extlen_t gtdiff; /* difference to right side entry */ |
| 993 | xfs_extlen_t gtlen; /* length of right side entry */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 994 | xfs_extlen_t gtlena; /* aligned ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | xfs_agblock_t gtnew; /* useful start bno of right side */ |
| 996 | int error; /* error code */ |
| 997 | int i; /* result code, temporary */ |
| 998 | int j; /* result code, temporary */ |
| 999 | xfs_agblock_t ltbno; /* start bno of left side entry */ |
| 1000 | xfs_agblock_t ltbnoa; /* aligned ... */ |
| 1001 | xfs_extlen_t ltdiff; /* difference to left side entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | xfs_extlen_t ltlen; /* length of left side entry */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1003 | xfs_extlen_t ltlena; /* aligned ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | xfs_agblock_t ltnew; /* useful start bno of left side */ |
| 1005 | xfs_extlen_t rlen; /* length of returned extent */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1006 | int forced = 0; |
Dave Chinner | 63d20d6 | 2013-08-12 20:49:50 +1000 | [diff] [blame] | 1007 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | /* |
| 1009 | * Randomly don't execute the first algorithm. |
| 1010 | */ |
| 1011 | int dofirst; /* set to do first algorithm */ |
| 1012 | |
Akinobu Mita | ecb3403 | 2013-03-04 21:58:20 +0900 | [diff] [blame] | 1013 | dofirst = prandom_u32() & 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | #endif |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1015 | |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1016 | /* handle unitialized agbno range so caller doesn't have to */ |
| 1017 | if (!args->min_agbno && !args->max_agbno) |
| 1018 | args->max_agbno = args->mp->m_sb.sb_agblocks - 1; |
| 1019 | ASSERT(args->min_agbno <= args->max_agbno); |
| 1020 | |
| 1021 | /* clamp agbno to the range if it's outside */ |
| 1022 | if (args->agbno < args->min_agbno) |
| 1023 | args->agbno = args->min_agbno; |
| 1024 | if (args->agbno > args->max_agbno) |
| 1025 | args->agbno = args->max_agbno; |
| 1026 | |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1027 | restart: |
| 1028 | bno_cur_lt = NULL; |
| 1029 | bno_cur_gt = NULL; |
| 1030 | ltlen = 0; |
| 1031 | gtlena = 0; |
| 1032 | ltlena = 0; |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * Get a cursor for the by-size btree. |
| 1036 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1037 | cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp, |
| 1038 | args->agno, XFS_BTNUM_CNT); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | /* |
| 1041 | * See if there are any free extents as big as maxlen. |
| 1042 | */ |
| 1043 | if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i))) |
| 1044 | goto error0; |
| 1045 | /* |
| 1046 | * If none, then pick up the last entry in the tree unless the |
| 1047 | * tree is empty. |
| 1048 | */ |
| 1049 | if (!i) { |
| 1050 | if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno, |
| 1051 | <len, &i))) |
| 1052 | goto error0; |
| 1053 | if (i == 0 || ltlen == 0) { |
| 1054 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1055 | trace_xfs_alloc_near_noentry(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return 0; |
| 1057 | } |
| 1058 | ASSERT(i == 1); |
| 1059 | } |
| 1060 | args->wasfromfl = 0; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | /* |
| 1063 | * First algorithm. |
| 1064 | * If the requested extent is large wrt the freespaces available |
| 1065 | * in this a.g., then the cursor will be pointing to a btree entry |
| 1066 | * near the right edge of the tree. If it's in the last btree leaf |
| 1067 | * block, then we just examine all the entries in that block |
| 1068 | * that are big enough, and pick the best one. |
| 1069 | * This is written as a while loop so we can break out of it, |
| 1070 | * but we never loop back to the top. |
| 1071 | */ |
| 1072 | while (xfs_btree_islastblock(cnt_cur, 0)) { |
| 1073 | xfs_extlen_t bdiff; |
| 1074 | int besti=0; |
| 1075 | xfs_extlen_t blen=0; |
| 1076 | xfs_agblock_t bnew=0; |
| 1077 | |
Dave Chinner | 63d20d6 | 2013-08-12 20:49:50 +1000 | [diff] [blame] | 1078 | #ifdef DEBUG |
| 1079 | if (dofirst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | break; |
| 1081 | #endif |
| 1082 | /* |
| 1083 | * Start from the entry that lookup found, sequence through |
| 1084 | * all larger free blocks. If we're actually pointing at a |
| 1085 | * record smaller than maxlen, go to the start of this block, |
| 1086 | * and skip all those smaller than minlen. |
| 1087 | */ |
| 1088 | if (ltlen || args->alignment > 1) { |
| 1089 | cnt_cur->bc_ptrs[0] = 1; |
| 1090 | do { |
| 1091 | if ((error = xfs_alloc_get_rec(cnt_cur, <bno, |
| 1092 | <len, &i))) |
| 1093 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1094 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if (ltlen >= args->minlen) |
| 1096 | break; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1097 | if ((error = xfs_btree_increment(cnt_cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | goto error0; |
| 1099 | } while (i); |
| 1100 | ASSERT(ltlen >= args->minlen); |
| 1101 | if (!i) |
| 1102 | break; |
| 1103 | } |
| 1104 | i = cnt_cur->bc_ptrs[0]; |
| 1105 | for (j = 1, blen = 0, bdiff = 0; |
| 1106 | !error && j && (blen < args->maxlen || bdiff > 0); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1107 | error = xfs_btree_increment(cnt_cur, 0, &j)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | /* |
| 1109 | * For each entry, decide if it's better than |
| 1110 | * the previous best entry. |
| 1111 | */ |
| 1112 | if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i))) |
| 1113 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1114 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Christoph Hellwig | 86fa8af | 2011-03-04 12:59:54 +0000 | [diff] [blame] | 1115 | xfs_alloc_compute_aligned(args, ltbno, ltlen, |
| 1116 | <bnoa, <lena); |
David Chinner | e643003 | 2008-04-17 16:49:49 +1000 | [diff] [blame] | 1117 | if (ltlena < args->minlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | continue; |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1119 | if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno) |
| 1120 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen); |
| 1122 | xfs_alloc_fix_len(args); |
| 1123 | ASSERT(args->len >= args->minlen); |
| 1124 | if (args->len < blen) |
| 1125 | continue; |
| 1126 | ltdiff = xfs_alloc_compute_diff(args->agbno, args->len, |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 1127 | args->alignment, args->datatype, ltbnoa, |
Jan Kara | 211d022 | 2013-04-11 22:09:56 +0200 | [diff] [blame] | 1128 | ltlena, <new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (ltnew != NULLAGBLOCK && |
| 1130 | (args->len > blen || ltdiff < bdiff)) { |
| 1131 | bdiff = ltdiff; |
| 1132 | bnew = ltnew; |
| 1133 | blen = args->len; |
| 1134 | besti = cnt_cur->bc_ptrs[0]; |
| 1135 | } |
| 1136 | } |
| 1137 | /* |
| 1138 | * It didn't work. We COULD be in a case where |
| 1139 | * there's a good record somewhere, so try again. |
| 1140 | */ |
| 1141 | if (blen == 0) |
| 1142 | break; |
| 1143 | /* |
| 1144 | * Point at the best entry, and retrieve it again. |
| 1145 | */ |
| 1146 | cnt_cur->bc_ptrs[0] = besti; |
| 1147 | if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i))) |
| 1148 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1149 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Christoph Hellwig | 73523a2 | 2010-07-20 17:54:45 +1000 | [diff] [blame] | 1150 | ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | args->len = blen; |
| 1152 | if (!xfs_alloc_fix_minleft(args)) { |
| 1153 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1154 | trace_xfs_alloc_near_nominleft(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | blen = args->len; |
| 1158 | /* |
| 1159 | * We are allocating starting at bnew for blen blocks. |
| 1160 | */ |
| 1161 | args->agbno = bnew; |
| 1162 | ASSERT(bnew >= ltbno); |
Christoph Hellwig | 73523a2 | 2010-07-20 17:54:45 +1000 | [diff] [blame] | 1163 | ASSERT(bnew + blen <= ltbno + ltlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * Set up a cursor for the by-bno tree. |
| 1166 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1167 | bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, |
| 1168 | args->agbp, args->agno, XFS_BTNUM_BNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | /* |
| 1170 | * Fix up the btree entries. |
| 1171 | */ |
| 1172 | if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, |
| 1173 | ltlen, bnew, blen, XFSA_FIXUP_CNT_OK))) |
| 1174 | goto error0; |
| 1175 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1176 | xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1177 | |
| 1178 | trace_xfs_alloc_near_first(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return 0; |
| 1180 | } |
| 1181 | /* |
| 1182 | * Second algorithm. |
| 1183 | * Search in the by-bno tree to the left and to the right |
| 1184 | * simultaneously, until in each case we find a space big enough, |
| 1185 | * or run into the edge of the tree. When we run into the edge, |
| 1186 | * we deallocate that cursor. |
| 1187 | * If both searches succeed, we compare the two spaces and pick |
| 1188 | * the better one. |
| 1189 | * With alignment, it's possible for both to fail; the upper |
| 1190 | * level algorithm that picks allocation groups for allocations |
| 1191 | * is not supposed to do this. |
| 1192 | */ |
| 1193 | /* |
| 1194 | * Allocate and initialize the cursor for the leftward search. |
| 1195 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1196 | bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp, |
| 1197 | args->agno, XFS_BTNUM_BNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | /* |
| 1199 | * Lookup <= bno to find the leftward search's starting point. |
| 1200 | */ |
| 1201 | if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i))) |
| 1202 | goto error0; |
| 1203 | if (!i) { |
| 1204 | /* |
| 1205 | * Didn't find anything; use this cursor for the rightward |
| 1206 | * search. |
| 1207 | */ |
| 1208 | bno_cur_gt = bno_cur_lt; |
| 1209 | bno_cur_lt = NULL; |
| 1210 | } |
| 1211 | /* |
| 1212 | * Found something. Duplicate the cursor for the rightward search. |
| 1213 | */ |
| 1214 | else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt))) |
| 1215 | goto error0; |
| 1216 | /* |
| 1217 | * Increment the cursor, so we will point at the entry just right |
| 1218 | * of the leftward entry if any, or to the leftmost entry. |
| 1219 | */ |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1220 | if ((error = xfs_btree_increment(bno_cur_gt, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | goto error0; |
| 1222 | if (!i) { |
| 1223 | /* |
| 1224 | * It failed, there are no rightward entries. |
| 1225 | */ |
| 1226 | xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR); |
| 1227 | bno_cur_gt = NULL; |
| 1228 | } |
| 1229 | /* |
| 1230 | * Loop going left with the leftward cursor, right with the |
| 1231 | * rightward cursor, until either both directions give up or |
| 1232 | * we find an entry at least as big as minlen. |
| 1233 | */ |
| 1234 | do { |
| 1235 | if (bno_cur_lt) { |
| 1236 | if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i))) |
| 1237 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1238 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Christoph Hellwig | 86fa8af | 2011-03-04 12:59:54 +0000 | [diff] [blame] | 1239 | xfs_alloc_compute_aligned(args, ltbno, ltlen, |
| 1240 | <bnoa, <lena); |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1241 | if (ltlena >= args->minlen && ltbnoa >= args->min_agbno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | break; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1243 | if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | goto error0; |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1245 | if (!i || ltbnoa < args->min_agbno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | xfs_btree_del_cursor(bno_cur_lt, |
| 1247 | XFS_BTREE_NOERROR); |
| 1248 | bno_cur_lt = NULL; |
| 1249 | } |
| 1250 | } |
| 1251 | if (bno_cur_gt) { |
| 1252 | if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i))) |
| 1253 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1254 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Christoph Hellwig | 86fa8af | 2011-03-04 12:59:54 +0000 | [diff] [blame] | 1255 | xfs_alloc_compute_aligned(args, gtbno, gtlen, |
| 1256 | >bnoa, >lena); |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1257 | if (gtlena >= args->minlen && gtbnoa <= args->max_agbno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | break; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1259 | if ((error = xfs_btree_increment(bno_cur_gt, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | goto error0; |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1261 | if (!i || gtbnoa > args->max_agbno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | xfs_btree_del_cursor(bno_cur_gt, |
| 1263 | XFS_BTREE_NOERROR); |
| 1264 | bno_cur_gt = NULL; |
| 1265 | } |
| 1266 | } |
| 1267 | } while (bno_cur_lt || bno_cur_gt); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | /* |
| 1270 | * Got both cursors still active, need to find better entry. |
| 1271 | */ |
| 1272 | if (bno_cur_lt && bno_cur_gt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | if (ltlena >= args->minlen) { |
| 1274 | /* |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1275 | * Left side is good, look for a right side entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | */ |
| 1277 | args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen); |
| 1278 | xfs_alloc_fix_len(args); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1279 | ltdiff = xfs_alloc_compute_diff(args->agbno, args->len, |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 1280 | args->alignment, args->datatype, ltbnoa, |
Jan Kara | 211d022 | 2013-04-11 22:09:56 +0200 | [diff] [blame] | 1281 | ltlena, <new); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1282 | |
| 1283 | error = xfs_alloc_find_best_extent(args, |
| 1284 | &bno_cur_lt, &bno_cur_gt, |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1285 | ltdiff, >bno, >len, |
| 1286 | >bnoa, >lena, |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1287 | 0 /* search right */); |
| 1288 | } else { |
| 1289 | ASSERT(gtlena >= args->minlen); |
| 1290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | /* |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1292 | * Right side is good, look for a left side entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | */ |
| 1294 | args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen); |
| 1295 | xfs_alloc_fix_len(args); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1296 | gtdiff = xfs_alloc_compute_diff(args->agbno, args->len, |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 1297 | args->alignment, args->datatype, gtbnoa, |
Jan Kara | 211d022 | 2013-04-11 22:09:56 +0200 | [diff] [blame] | 1298 | gtlena, >new); |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1299 | |
| 1300 | error = xfs_alloc_find_best_extent(args, |
| 1301 | &bno_cur_gt, &bno_cur_lt, |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1302 | gtdiff, <bno, <len, |
| 1303 | <bnoa, <lena, |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1304 | 1 /* search left */); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (error) |
| 1308 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | /* |
| 1312 | * If we couldn't get anything, give up. |
| 1313 | */ |
| 1314 | if (bno_cur_lt == NULL && bno_cur_gt == NULL) { |
Dave Chinner | e3a746f5 | 2012-07-12 07:40:42 +1000 | [diff] [blame] | 1315 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1316 | |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1317 | if (!forced++) { |
| 1318 | trace_xfs_alloc_near_busy(args); |
| 1319 | xfs_log_force(args->mp, XFS_LOG_SYNC); |
| 1320 | goto restart; |
| 1321 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1322 | trace_xfs_alloc_size_neither(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | args->agbno = NULLAGBLOCK; |
| 1324 | return 0; |
| 1325 | } |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * At this point we have selected a freespace entry, either to the |
| 1329 | * left or to the right. If it's on the right, copy all the |
| 1330 | * useful variables to the "left" set so we only have one |
| 1331 | * copy of this code. |
| 1332 | */ |
| 1333 | if (bno_cur_gt) { |
| 1334 | bno_cur_lt = bno_cur_gt; |
| 1335 | bno_cur_gt = NULL; |
| 1336 | ltbno = gtbno; |
| 1337 | ltbnoa = gtbnoa; |
| 1338 | ltlen = gtlen; |
| 1339 | ltlena = gtlena; |
| 1340 | j = 1; |
| 1341 | } else |
| 1342 | j = 0; |
Christoph Hellwig | 489a150 | 2010-12-10 15:04:11 +0000 | [diff] [blame] | 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | /* |
| 1345 | * Fix up the length and compute the useful address. |
| 1346 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen); |
| 1348 | xfs_alloc_fix_len(args); |
| 1349 | if (!xfs_alloc_fix_minleft(args)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1350 | trace_xfs_alloc_near_nominleft(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR); |
| 1352 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | rlen = args->len; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1356 | (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 1357 | args->datatype, ltbnoa, ltlena, <new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | ASSERT(ltnew >= ltbno); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1359 | ASSERT(ltnew + rlen <= ltbnoa + ltlena); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1360 | ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length)); |
Brian Foster | bfe46d4 | 2015-05-29 08:53:00 +1000 | [diff] [blame] | 1361 | ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | args->agbno = ltnew; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen, |
| 1365 | ltnew, rlen, XFSA_FIXUP_BNO_OK))) |
| 1366 | goto error0; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1367 | |
| 1368 | if (j) |
| 1369 | trace_xfs_alloc_near_greater(args); |
| 1370 | else |
| 1371 | trace_xfs_alloc_near_lesser(args); |
| 1372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1374 | xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR); |
| 1375 | return 0; |
| 1376 | |
| 1377 | error0: |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1378 | trace_xfs_alloc_near_error(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | if (cnt_cur != NULL) |
| 1380 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); |
| 1381 | if (bno_cur_lt != NULL) |
| 1382 | xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR); |
| 1383 | if (bno_cur_gt != NULL) |
| 1384 | xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR); |
| 1385 | return error; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Allocate a variable extent anywhere in the allocation group agno. |
| 1390 | * Extent's length (returned in len) will be between minlen and maxlen, |
| 1391 | * and of the form k * prod + mod unless there's nothing that large. |
| 1392 | * Return the starting a.g. block, or NULLAGBLOCK if we can't do it. |
| 1393 | */ |
| 1394 | STATIC int /* error */ |
| 1395 | xfs_alloc_ag_vextent_size( |
| 1396 | xfs_alloc_arg_t *args) /* allocation argument structure */ |
| 1397 | { |
| 1398 | xfs_btree_cur_t *bno_cur; /* cursor for bno btree */ |
| 1399 | xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */ |
| 1400 | int error; /* error result */ |
| 1401 | xfs_agblock_t fbno; /* start of found freespace */ |
| 1402 | xfs_extlen_t flen; /* length of found freespace */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | int i; /* temp status variable */ |
| 1404 | xfs_agblock_t rbno; /* returned block number */ |
| 1405 | xfs_extlen_t rlen; /* length of returned extent */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1406 | int forced = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1408 | restart: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | /* |
| 1410 | * Allocate and initialize a cursor for the by-size btree. |
| 1411 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1412 | cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp, |
| 1413 | args->agno, XFS_BTNUM_CNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | bno_cur = NULL; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | /* |
| 1417 | * Look for an entry >= maxlen+alignment-1 blocks. |
| 1418 | */ |
| 1419 | if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, |
| 1420 | args->maxlen + args->alignment - 1, &i))) |
| 1421 | goto error0; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | /* |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1424 | * If none or we have busy extents that we cannot allocate from, then |
| 1425 | * we have to settle for a smaller extent. In the case that there are |
| 1426 | * no large extents, this will return the last entry in the tree unless |
| 1427 | * the tree is empty. In the case that there are only busy large |
| 1428 | * extents, this will return the largest small extent unless there |
| 1429 | * are no smaller extents available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | */ |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1431 | if (!i || forced > 1) { |
| 1432 | error = xfs_alloc_ag_vextent_small(args, cnt_cur, |
| 1433 | &fbno, &flen, &i); |
| 1434 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | goto error0; |
| 1436 | if (i == 0 || flen == 0) { |
| 1437 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1438 | trace_xfs_alloc_size_noentry(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | return 0; |
| 1440 | } |
| 1441 | ASSERT(i == 1); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1442 | xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen); |
| 1443 | } else { |
| 1444 | /* |
| 1445 | * Search for a non-busy extent that is large enough. |
| 1446 | * If we are at low space, don't check, or if we fall of |
| 1447 | * the end of the btree, turn off the busy check and |
| 1448 | * restart. |
| 1449 | */ |
| 1450 | for (;;) { |
| 1451 | error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i); |
| 1452 | if (error) |
| 1453 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1454 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1455 | |
| 1456 | xfs_alloc_compute_aligned(args, fbno, flen, |
| 1457 | &rbno, &rlen); |
| 1458 | |
| 1459 | if (rlen >= args->maxlen) |
| 1460 | break; |
| 1461 | |
| 1462 | error = xfs_btree_increment(cnt_cur, 0, &i); |
| 1463 | if (error) |
| 1464 | goto error0; |
| 1465 | if (i == 0) { |
| 1466 | /* |
| 1467 | * Our only valid extents must have been busy. |
| 1468 | * Make it unbusy by forcing the log out and |
| 1469 | * retrying. If we've been here before, forcing |
| 1470 | * the log isn't making the extents available, |
| 1471 | * which means they have probably been freed in |
| 1472 | * this transaction. In that case, we have to |
| 1473 | * give up on them and we'll attempt a minlen |
| 1474 | * allocation the next time around. |
| 1475 | */ |
| 1476 | xfs_btree_del_cursor(cnt_cur, |
| 1477 | XFS_BTREE_NOERROR); |
| 1478 | trace_xfs_alloc_size_busy(args); |
| 1479 | if (!forced++) |
| 1480 | xfs_log_force(args->mp, XFS_LOG_SYNC); |
| 1481 | goto restart; |
| 1482 | } |
| 1483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | /* |
| 1487 | * In the first case above, we got the last entry in the |
| 1488 | * by-size btree. Now we check to see if the space hits maxlen |
| 1489 | * once aligned; if not, we search left for something better. |
| 1490 | * This can't happen in the second case above. |
| 1491 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | rlen = XFS_EXTLEN_MIN(args->maxlen, rlen); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1493 | XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | (rlen <= flen && rbno + rlen <= fbno + flen), error0); |
| 1495 | if (rlen < args->maxlen) { |
| 1496 | xfs_agblock_t bestfbno; |
| 1497 | xfs_extlen_t bestflen; |
| 1498 | xfs_agblock_t bestrbno; |
| 1499 | xfs_extlen_t bestrlen; |
| 1500 | |
| 1501 | bestrlen = rlen; |
| 1502 | bestrbno = rbno; |
| 1503 | bestflen = flen; |
| 1504 | bestfbno = fbno; |
| 1505 | for (;;) { |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1506 | if ((error = xfs_btree_decrement(cnt_cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | goto error0; |
| 1508 | if (i == 0) |
| 1509 | break; |
| 1510 | if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, |
| 1511 | &i))) |
| 1512 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1513 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | if (flen < bestrlen) |
| 1515 | break; |
Christoph Hellwig | 86fa8af | 2011-03-04 12:59:54 +0000 | [diff] [blame] | 1516 | xfs_alloc_compute_aligned(args, fbno, flen, |
| 1517 | &rbno, &rlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | rlen = XFS_EXTLEN_MIN(args->maxlen, rlen); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1519 | XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | (rlen <= flen && rbno + rlen <= fbno + flen), |
| 1521 | error0); |
| 1522 | if (rlen > bestrlen) { |
| 1523 | bestrlen = rlen; |
| 1524 | bestrbno = rbno; |
| 1525 | bestflen = flen; |
| 1526 | bestfbno = fbno; |
| 1527 | if (rlen == args->maxlen) |
| 1528 | break; |
| 1529 | } |
| 1530 | } |
| 1531 | if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen, |
| 1532 | &i))) |
| 1533 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1534 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | rlen = bestrlen; |
| 1536 | rbno = bestrbno; |
| 1537 | flen = bestflen; |
| 1538 | fbno = bestfbno; |
| 1539 | } |
| 1540 | args->wasfromfl = 0; |
| 1541 | /* |
| 1542 | * Fix up the length. |
| 1543 | */ |
| 1544 | args->len = rlen; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1545 | if (rlen < args->minlen) { |
| 1546 | if (!forced++) { |
| 1547 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1548 | trace_xfs_alloc_size_busy(args); |
| 1549 | xfs_log_force(args->mp, XFS_LOG_SYNC); |
| 1550 | goto restart; |
| 1551 | } |
| 1552 | goto out_nominleft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | } |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1554 | xfs_alloc_fix_len(args); |
| 1555 | |
| 1556 | if (!xfs_alloc_fix_minleft(args)) |
| 1557 | goto out_nominleft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | rlen = args->len; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1559 | XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | /* |
| 1561 | * Allocate and initialize a cursor for the by-block tree. |
| 1562 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1563 | bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp, |
| 1564 | args->agno, XFS_BTNUM_BNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, |
| 1566 | rbno, rlen, XFSA_FIXUP_CNT_OK))) |
| 1567 | goto error0; |
| 1568 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1569 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); |
| 1570 | cnt_cur = bno_cur = NULL; |
| 1571 | args->len = rlen; |
| 1572 | args->agbno = rbno; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1573 | XFS_WANT_CORRUPTED_GOTO(args->mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | args->agbno + args->len <= |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1575 | be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | error0); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1577 | trace_xfs_alloc_size_done(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | return 0; |
| 1579 | |
| 1580 | error0: |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1581 | trace_xfs_alloc_size_error(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | if (cnt_cur) |
| 1583 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); |
| 1584 | if (bno_cur) |
| 1585 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR); |
| 1586 | return error; |
Christoph Hellwig | e26f050 | 2011-04-24 19:06:15 +0000 | [diff] [blame] | 1587 | |
| 1588 | out_nominleft: |
| 1589 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1590 | trace_xfs_alloc_size_nominleft(args); |
| 1591 | args->agbno = NULLAGBLOCK; |
| 1592 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * Deal with the case where only small freespaces remain. |
| 1597 | * Either return the contents of the last freespace record, |
| 1598 | * or allocate space from the freelist if there is nothing in the tree. |
| 1599 | */ |
| 1600 | STATIC int /* error */ |
| 1601 | xfs_alloc_ag_vextent_small( |
| 1602 | xfs_alloc_arg_t *args, /* allocation argument structure */ |
| 1603 | xfs_btree_cur_t *ccur, /* by-size cursor */ |
| 1604 | xfs_agblock_t *fbnop, /* result block number */ |
| 1605 | xfs_extlen_t *flenp, /* result length */ |
| 1606 | int *stat) /* status: 0-freelist, 1-normal/none */ |
| 1607 | { |
Darrick J. Wong | a03f1a6 | 2016-08-17 11:12:57 +1000 | [diff] [blame] | 1608 | struct xfs_owner_info oinfo; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1609 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | int error; |
| 1611 | xfs_agblock_t fbno; |
| 1612 | xfs_extlen_t flen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | int i; |
| 1614 | |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1615 | if ((error = xfs_btree_decrement(ccur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | goto error0; |
| 1617 | if (i) { |
| 1618 | if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i))) |
| 1619 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1620 | XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
| 1622 | /* |
| 1623 | * Nothing in the btree, try the freelist. Make sure |
| 1624 | * to respect minleft even when pulling from the |
| 1625 | * freelist. |
| 1626 | */ |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1627 | else if (args->minlen == 1 && args->alignment == 1 && |
| 1628 | args->resv != XFS_AG_RESV_AGFL && |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1629 | (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount) |
| 1630 | > args->minleft)) { |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1631 | error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0); |
| 1632 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | goto error0; |
| 1634 | if (fbno != NULLAGBLOCK) { |
Dave Chinner | 4ecbfe6 | 2012-04-29 10:41:10 +0000 | [diff] [blame] | 1635 | xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1, |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 1636 | xfs_alloc_allow_busy_reuse(args->datatype)); |
Christoph Hellwig | 97d3ac7 | 2011-04-24 19:06:16 +0000 | [diff] [blame] | 1637 | |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 1638 | if (xfs_alloc_is_userdata(args->datatype)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | xfs_buf_t *bp; |
| 1640 | |
| 1641 | bp = xfs_btree_get_bufs(args->mp, args->tp, |
| 1642 | args->agno, fbno, 0); |
| 1643 | xfs_trans_binval(args->tp, bp); |
| 1644 | } |
| 1645 | args->len = 1; |
| 1646 | args->agbno = fbno; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1647 | XFS_WANT_CORRUPTED_GOTO(args->mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | args->agbno + args->len <= |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1649 | be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | error0); |
| 1651 | args->wasfromfl = 1; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1652 | trace_xfs_alloc_small_freelist(args); |
Darrick J. Wong | a03f1a6 | 2016-08-17 11:12:57 +1000 | [diff] [blame] | 1653 | |
| 1654 | /* |
| 1655 | * If we're feeding an AGFL block to something that |
| 1656 | * doesn't live in the free space, we need to clear |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1657 | * out the OWN_AG rmap and add the block back to |
| 1658 | * the AGFL per-AG reservation. |
Darrick J. Wong | a03f1a6 | 2016-08-17 11:12:57 +1000 | [diff] [blame] | 1659 | */ |
| 1660 | xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG); |
| 1661 | error = xfs_rmap_free(args->tp, args->agbp, args->agno, |
| 1662 | fbno, 1, &oinfo); |
| 1663 | if (error) |
| 1664 | goto error0; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1665 | pag = xfs_perag_get(args->mp, args->agno); |
| 1666 | xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL, |
| 1667 | args->tp, 1); |
| 1668 | xfs_perag_put(pag); |
Darrick J. Wong | a03f1a6 | 2016-08-17 11:12:57 +1000 | [diff] [blame] | 1669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | *stat = 0; |
| 1671 | return 0; |
| 1672 | } |
| 1673 | /* |
| 1674 | * Nothing in the freelist. |
| 1675 | */ |
| 1676 | else |
| 1677 | flen = 0; |
| 1678 | } |
| 1679 | /* |
| 1680 | * Can't allocate from the freelist for some reason. |
| 1681 | */ |
Nathan Scott | d432c80 | 2006-09-28 11:03:44 +1000 | [diff] [blame] | 1682 | else { |
| 1683 | fbno = NULLAGBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | flen = 0; |
Nathan Scott | d432c80 | 2006-09-28 11:03:44 +1000 | [diff] [blame] | 1685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | /* |
| 1687 | * Can't do the allocation, give up. |
| 1688 | */ |
| 1689 | if (flen < args->minlen) { |
| 1690 | args->agbno = NULLAGBLOCK; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1691 | trace_xfs_alloc_small_notenough(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | flen = 0; |
| 1693 | } |
| 1694 | *fbnop = fbno; |
| 1695 | *flenp = flen; |
| 1696 | *stat = 1; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1697 | trace_xfs_alloc_small_done(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | return 0; |
| 1699 | |
| 1700 | error0: |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1701 | trace_xfs_alloc_small_error(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | return error; |
| 1703 | } |
| 1704 | |
| 1705 | /* |
| 1706 | * Free the extent starting at agno/bno for length. |
| 1707 | */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 1708 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | xfs_free_ag_extent( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 1710 | xfs_trans_t *tp, |
| 1711 | xfs_buf_t *agbp, |
| 1712 | xfs_agnumber_t agno, |
| 1713 | xfs_agblock_t bno, |
| 1714 | xfs_extlen_t len, |
| 1715 | struct xfs_owner_info *oinfo, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1716 | enum xfs_ag_resv_type type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | { |
| 1718 | xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */ |
| 1719 | xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */ |
| 1720 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | xfs_agblock_t gtbno; /* start of right neighbor block */ |
| 1722 | xfs_extlen_t gtlen; /* length of right neighbor block */ |
| 1723 | int haveleft; /* have a left neighbor block */ |
| 1724 | int haveright; /* have a right neighbor block */ |
| 1725 | int i; /* temp, result code */ |
| 1726 | xfs_agblock_t ltbno; /* start of left neighbor block */ |
| 1727 | xfs_extlen_t ltlen; /* length of left neighbor block */ |
| 1728 | xfs_mount_t *mp; /* mount point struct for filesystem */ |
| 1729 | xfs_agblock_t nbno; /* new starting block of freespace */ |
| 1730 | xfs_extlen_t nlen; /* new length of freespace */ |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 1731 | xfs_perag_t *pag; /* per allocation group data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 1733 | bno_cur = cnt_cur = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | mp = tp->t_mountp; |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 1735 | |
| 1736 | if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) { |
| 1737 | error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo); |
| 1738 | if (error) |
| 1739 | goto error0; |
| 1740 | } |
| 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * Allocate and initialize a cursor for the by-block btree. |
| 1744 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1745 | bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | /* |
| 1747 | * Look for a neighboring block on the left (lower block numbers) |
| 1748 | * that is contiguous with this space. |
| 1749 | */ |
| 1750 | if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft))) |
| 1751 | goto error0; |
| 1752 | if (haveleft) { |
| 1753 | /* |
| 1754 | * There is a block to our left. |
| 1755 | */ |
| 1756 | if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i))) |
| 1757 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1758 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | /* |
| 1760 | * It's not contiguous, though. |
| 1761 | */ |
| 1762 | if (ltbno + ltlen < bno) |
| 1763 | haveleft = 0; |
| 1764 | else { |
| 1765 | /* |
| 1766 | * If this failure happens the request to free this |
| 1767 | * space was invalid, it's (partly) already free. |
| 1768 | * Very bad. |
| 1769 | */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1770 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1771 | ltbno + ltlen <= bno, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | /* |
| 1775 | * Look for a neighboring block on the right (higher block numbers) |
| 1776 | * that is contiguous with this space. |
| 1777 | */ |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1778 | if ((error = xfs_btree_increment(bno_cur, 0, &haveright))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | goto error0; |
| 1780 | if (haveright) { |
| 1781 | /* |
| 1782 | * There is a block to our right. |
| 1783 | */ |
| 1784 | if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i))) |
| 1785 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1786 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | /* |
| 1788 | * It's not contiguous, though. |
| 1789 | */ |
| 1790 | if (bno + len < gtbno) |
| 1791 | haveright = 0; |
| 1792 | else { |
| 1793 | /* |
| 1794 | * If this failure happens the request to free this |
| 1795 | * space was invalid, it's (partly) already free. |
| 1796 | * Very bad. |
| 1797 | */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1798 | XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | /* |
| 1802 | * Now allocate and initialize a cursor for the by-size tree. |
| 1803 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1804 | cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | /* |
| 1806 | * Have both left and right contiguous neighbors. |
| 1807 | * Merge all three into a single free block. |
| 1808 | */ |
| 1809 | if (haveleft && haveright) { |
| 1810 | /* |
| 1811 | * Delete the old by-size entry on the left. |
| 1812 | */ |
| 1813 | if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i))) |
| 1814 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1815 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1816 | if ((error = xfs_btree_delete(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1818 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | /* |
| 1820 | * Delete the old by-size entry on the right. |
| 1821 | */ |
| 1822 | if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i))) |
| 1823 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1824 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1825 | if ((error = xfs_btree_delete(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1827 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | /* |
| 1829 | * Delete the old by-block entry for the right block. |
| 1830 | */ |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1831 | if ((error = xfs_btree_delete(bno_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1833 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | /* |
| 1835 | * Move the by-block cursor back to the left neighbor. |
| 1836 | */ |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1837 | if ((error = xfs_btree_decrement(bno_cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1839 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | #ifdef DEBUG |
| 1841 | /* |
| 1842 | * Check that this is the right record: delete didn't |
| 1843 | * mangle the cursor. |
| 1844 | */ |
| 1845 | { |
| 1846 | xfs_agblock_t xxbno; |
| 1847 | xfs_extlen_t xxlen; |
| 1848 | |
| 1849 | if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen, |
| 1850 | &i))) |
| 1851 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1852 | XFS_WANT_CORRUPTED_GOTO(mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | i == 1 && xxbno == ltbno && xxlen == ltlen, |
| 1854 | error0); |
| 1855 | } |
| 1856 | #endif |
| 1857 | /* |
| 1858 | * Update remaining by-block entry to the new, joined block. |
| 1859 | */ |
| 1860 | nbno = ltbno; |
| 1861 | nlen = len + ltlen + gtlen; |
| 1862 | if ((error = xfs_alloc_update(bno_cur, nbno, nlen))) |
| 1863 | goto error0; |
| 1864 | } |
| 1865 | /* |
| 1866 | * Have only a left contiguous neighbor. |
| 1867 | * Merge it together with the new freespace. |
| 1868 | */ |
| 1869 | else if (haveleft) { |
| 1870 | /* |
| 1871 | * Delete the old by-size entry on the left. |
| 1872 | */ |
| 1873 | if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i))) |
| 1874 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1875 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1876 | if ((error = xfs_btree_delete(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1878 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | /* |
| 1880 | * Back up the by-block cursor to the left neighbor, and |
| 1881 | * update its length. |
| 1882 | */ |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1883 | if ((error = xfs_btree_decrement(bno_cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1885 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | nbno = ltbno; |
| 1887 | nlen = len + ltlen; |
| 1888 | if ((error = xfs_alloc_update(bno_cur, nbno, nlen))) |
| 1889 | goto error0; |
| 1890 | } |
| 1891 | /* |
| 1892 | * Have only a right contiguous neighbor. |
| 1893 | * Merge it together with the new freespace. |
| 1894 | */ |
| 1895 | else if (haveright) { |
| 1896 | /* |
| 1897 | * Delete the old by-size entry on the right. |
| 1898 | */ |
| 1899 | if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i))) |
| 1900 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1901 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1902 | if ((error = xfs_btree_delete(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1904 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | /* |
| 1906 | * Update the starting block and length of the right |
| 1907 | * neighbor in the by-block tree. |
| 1908 | */ |
| 1909 | nbno = bno; |
| 1910 | nlen = len + gtlen; |
| 1911 | if ((error = xfs_alloc_update(bno_cur, nbno, nlen))) |
| 1912 | goto error0; |
| 1913 | } |
| 1914 | /* |
| 1915 | * No contiguous neighbors. |
| 1916 | * Insert the new freespace into the by-block tree. |
| 1917 | */ |
| 1918 | else { |
| 1919 | nbno = bno; |
| 1920 | nlen = len; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1921 | if ((error = xfs_btree_insert(bno_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1923 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | } |
| 1925 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); |
| 1926 | bno_cur = NULL; |
| 1927 | /* |
| 1928 | * In all cases we need to insert the new freespace in the by-size tree. |
| 1929 | */ |
| 1930 | if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i))) |
| 1931 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1932 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1933 | if ((error = xfs_btree_insert(cnt_cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1935 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); |
| 1937 | cnt_cur = NULL; |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | /* |
| 1940 | * Update the freespace totals in the ag and superblock. |
| 1941 | */ |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 1942 | pag = xfs_perag_get(mp, agno); |
| 1943 | error = xfs_alloc_update_counters(tp, pag, agbp, len); |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1944 | xfs_ag_resv_free_extent(pag, type, tp, len); |
Christoph Hellwig | ecb6928 | 2011-03-04 12:59:55 +0000 | [diff] [blame] | 1945 | xfs_perag_put(pag); |
| 1946 | if (error) |
| 1947 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1949 | XFS_STATS_INC(mp, xs_freex); |
| 1950 | XFS_STATS_ADD(mp, xs_freeb, len); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1951 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1952 | trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL, |
| 1953 | haveleft, haveright); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | return 0; |
| 1956 | |
| 1957 | error0: |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1958 | trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL, |
| 1959 | -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | if (bno_cur) |
| 1961 | xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR); |
| 1962 | if (cnt_cur) |
| 1963 | xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); |
| 1964 | return error; |
| 1965 | } |
| 1966 | |
| 1967 | /* |
| 1968 | * Visible (exported) allocation/free functions. |
| 1969 | * Some of these are used just by xfs_alloc_btree.c and this file. |
| 1970 | */ |
| 1971 | |
| 1972 | /* |
| 1973 | * Compute and fill in value of m_ag_maxlevels. |
| 1974 | */ |
| 1975 | void |
| 1976 | xfs_alloc_compute_maxlevels( |
| 1977 | xfs_mount_t *mp) /* file system mount structure */ |
| 1978 | { |
Darrick J. Wong | 19b54ee | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 1979 | mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr, |
| 1980 | (mp->m_sb.sb_agblocks + 1) / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1984 | * Find the length of the longest extent in an AG. The 'need' parameter |
| 1985 | * specifies how much space we're going to need for the AGFL and the |
| 1986 | * 'reserved' parameter tells us how many blocks in this AG are reserved for |
| 1987 | * other callers. |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 1988 | */ |
| 1989 | xfs_extlen_t |
| 1990 | xfs_alloc_longest_free_extent( |
| 1991 | struct xfs_mount *mp, |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 1992 | struct xfs_perag *pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1993 | xfs_extlen_t need, |
| 1994 | xfs_extlen_t reserved) |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 1995 | { |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 1996 | xfs_extlen_t delta = 0; |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 1997 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 1998 | /* |
| 1999 | * If the AGFL needs a recharge, we'll have to subtract that from the |
| 2000 | * longest extent. |
| 2001 | */ |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 2002 | if (need > pag->pagf_flcount) |
| 2003 | delta = need - pag->pagf_flcount; |
| 2004 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2005 | /* |
| 2006 | * If we cannot maintain others' reservations with space from the |
| 2007 | * not-longest freesp extents, we'll have to subtract /that/ from |
| 2008 | * the longest extent too. |
| 2009 | */ |
| 2010 | if (pag->pagf_freeblks - pag->pagf_longest < reserved) |
| 2011 | delta += reserved - (pag->pagf_freeblks - pag->pagf_longest); |
| 2012 | |
| 2013 | /* |
| 2014 | * If the longest extent is long enough to satisfy all the |
| 2015 | * reservations and AGFL rules in place, we can return this extent. |
| 2016 | */ |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 2017 | if (pag->pagf_longest > delta) |
| 2018 | return pag->pagf_longest - delta; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2019 | |
| 2020 | /* Otherwise, let the caller try for 1 block if there's space. */ |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 2021 | return pag->pagf_flcount > 0 || pag->pagf_longest > 0; |
| 2022 | } |
| 2023 | |
Dave Chinner | 496817b | 2015-06-22 10:13:30 +1000 | [diff] [blame] | 2024 | unsigned int |
| 2025 | xfs_alloc_min_freelist( |
| 2026 | struct xfs_mount *mp, |
| 2027 | struct xfs_perag *pag) |
| 2028 | { |
| 2029 | unsigned int min_free; |
| 2030 | |
| 2031 | /* space needed by-bno freespace btree */ |
| 2032 | min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1, |
| 2033 | mp->m_ag_maxlevels); |
| 2034 | /* space needed by-size freespace btree */ |
| 2035 | min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1, |
| 2036 | mp->m_ag_maxlevels); |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 2037 | /* space needed reverse mapping used space btree */ |
| 2038 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 2039 | min_free += min_t(unsigned int, |
| 2040 | pag->pagf_levels[XFS_BTNUM_RMAPi] + 1, |
| 2041 | mp->m_rmap_maxlevels); |
Dave Chinner | 496817b | 2015-06-22 10:13:30 +1000 | [diff] [blame] | 2042 | |
| 2043 | return min_free; |
| 2044 | } |
| 2045 | |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 2046 | /* |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2047 | * Check if the operation we are fixing up the freelist for should go ahead or |
| 2048 | * not. If we are freeing blocks, we always allow it, otherwise the allocation |
| 2049 | * is dependent on whether the size and shape of free space available will |
| 2050 | * permit the requested allocation to take place. |
| 2051 | */ |
| 2052 | static bool |
| 2053 | xfs_alloc_space_available( |
| 2054 | struct xfs_alloc_arg *args, |
| 2055 | xfs_extlen_t min_free, |
| 2056 | int flags) |
| 2057 | { |
| 2058 | struct xfs_perag *pag = args->pag; |
| 2059 | xfs_extlen_t longest; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2060 | xfs_extlen_t reservation; /* blocks that are still reserved */ |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2061 | int available; |
| 2062 | |
| 2063 | if (flags & XFS_ALLOC_FLAG_FREEING) |
| 2064 | return true; |
| 2065 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2066 | reservation = xfs_ag_resv_needed(pag, args->resv); |
| 2067 | |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2068 | /* do we have enough contiguous free space for the allocation? */ |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2069 | longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free, |
| 2070 | reservation); |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2071 | if ((args->minlen + args->alignment + args->minalignslop - 1) > longest) |
| 2072 | return false; |
| 2073 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2074 | /* do we have enough free space remaining for the allocation? */ |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2075 | available = (int)(pag->pagf_freeblks + pag->pagf_flcount - |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2076 | reservation - min_free - args->total); |
| 2077 | if (available < (int)args->minleft || available <= 0) |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2078 | return false; |
| 2079 | |
| 2080 | return true; |
| 2081 | } |
| 2082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | /* |
| 2084 | * Decide whether to use this allocation group for this allocation. |
| 2085 | * If so, fix up the btree freelist's size. |
| 2086 | */ |
Darrick J. Wong | 2e9101d | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 2087 | int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | xfs_alloc_fix_freelist( |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2089 | struct xfs_alloc_arg *args, /* allocation argument structure */ |
| 2090 | int flags) /* XFS_ALLOC_FLAG_... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | { |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2092 | struct xfs_mount *mp = args->mp; |
| 2093 | struct xfs_perag *pag = args->pag; |
| 2094 | struct xfs_trans *tp = args->tp; |
| 2095 | struct xfs_buf *agbp = NULL; |
| 2096 | struct xfs_buf *agflbp = NULL; |
| 2097 | struct xfs_alloc_arg targs; /* local allocation arguments */ |
| 2098 | xfs_agblock_t bno; /* freelist block */ |
| 2099 | xfs_extlen_t need; /* total blocks needed in freelist */ |
Jan Kara | c184f85 | 2015-08-25 10:05:13 +1000 | [diff] [blame] | 2100 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | if (!pag->pagf_init) { |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2103 | error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp); |
| 2104 | if (error) |
| 2105 | goto out_no_agbp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | if (!pag->pagf_init) { |
Nathan Scott | 0e1edbd | 2006-08-10 14:40:41 +1000 | [diff] [blame] | 2107 | ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK); |
| 2108 | ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING)); |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2109 | goto out_agbp_relse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | |
Nathan Scott | 0e1edbd | 2006-08-10 14:40:41 +1000 | [diff] [blame] | 2113 | /* |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2114 | * If this is a metadata preferred pag and we are user data then try |
| 2115 | * somewhere else if we are not being asked to try harder at this |
| 2116 | * point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 2118 | if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) && |
Nathan Scott | 0e1edbd | 2006-08-10 14:40:41 +1000 | [diff] [blame] | 2119 | (flags & XFS_ALLOC_FLAG_TRYLOCK)) { |
| 2120 | ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING)); |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2121 | goto out_agbp_relse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
Dave Chinner | 496817b | 2015-06-22 10:13:30 +1000 | [diff] [blame] | 2124 | need = xfs_alloc_min_freelist(mp, pag); |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2125 | if (!xfs_alloc_space_available(args, need, flags)) |
| 2126 | goto out_agbp_relse; |
Nathan Scott | 0e1edbd | 2006-08-10 14:40:41 +1000 | [diff] [blame] | 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | /* |
| 2129 | * Get the a.g. freespace buffer. |
| 2130 | * Can fail if we're not blocking on locks, and it's held. |
| 2131 | */ |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2132 | if (!agbp) { |
| 2133 | error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp); |
| 2134 | if (error) |
| 2135 | goto out_no_agbp; |
| 2136 | if (!agbp) { |
Nathan Scott | 0e1edbd | 2006-08-10 14:40:41 +1000 | [diff] [blame] | 2137 | ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK); |
| 2138 | ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING)); |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2139 | goto out_no_agbp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
| 2141 | } |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2142 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2143 | /* If there isn't enough total space or single-extent, reject it. */ |
Dave Chinner | 496817b | 2015-06-22 10:13:30 +1000 | [diff] [blame] | 2144 | need = xfs_alloc_min_freelist(mp, pag); |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2145 | if (!xfs_alloc_space_available(args, need, flags)) |
| 2146 | goto out_agbp_relse; |
Dave Chinner | 72d5528 | 2015-06-22 10:04:42 +1000 | [diff] [blame] | 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | /* |
| 2149 | * Make the freelist shorter if it's too long. |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2150 | * |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2151 | * Note that from this point onwards, we will always release the agf and |
| 2152 | * agfl buffers on error. This handles the case where we error out and |
| 2153 | * the buffers are clean or may not have been joined to the transaction |
| 2154 | * and hence need to be released manually. If they have been joined to |
| 2155 | * the transaction, then xfs_trans_brelse() will handle them |
| 2156 | * appropriately based on the recursion count and dirty state of the |
| 2157 | * buffer. |
| 2158 | * |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2159 | * XXX (dgc): When we have lots of free space, does this buy us |
| 2160 | * anything other than extra overhead when we need to put more blocks |
| 2161 | * back on the free list? Maybe we should only do this when space is |
| 2162 | * getting low or the AGFL is more than half full? |
Darrick J. Wong | 04f1306 | 2016-08-03 12:19:53 +1000 | [diff] [blame] | 2163 | * |
| 2164 | * The NOSHRINK flag prevents the AGFL from being shrunk if it's too |
| 2165 | * big; the NORMAP flag prevents AGFL expand/shrink operations from |
| 2166 | * updating the rmapbt. Both flags are used in xfs_repair while we're |
| 2167 | * rebuilding the rmapbt, and neither are used by the kernel. They're |
| 2168 | * both required to ensure that rmaps are correctly recorded for the |
| 2169 | * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and |
| 2170 | * repair/rmap.c in xfsprogs for details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | */ |
Darrick J. Wong | 04f1306 | 2016-08-03 12:19:53 +1000 | [diff] [blame] | 2172 | memset(&targs, 0, sizeof(targs)); |
| 2173 | if (flags & XFS_ALLOC_FLAG_NORMAP) |
| 2174 | xfs_rmap_skip_owner_update(&targs.oinfo); |
| 2175 | else |
| 2176 | xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG); |
| 2177 | while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) { |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2178 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2180 | error = xfs_alloc_get_freelist(tp, agbp, &bno, 0); |
| 2181 | if (error) |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2182 | goto out_agbp_relse; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 2183 | error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2184 | &targs.oinfo, XFS_AG_RESV_AGFL); |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2185 | if (error) |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2186 | goto out_agbp_relse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0); |
| 2188 | xfs_trans_binval(tp, bp); |
| 2189 | } |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | targs.tp = tp; |
| 2192 | targs.mp = mp; |
| 2193 | targs.agbp = agbp; |
| 2194 | targs.agno = args->agno; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2195 | targs.alignment = targs.minlen = targs.prod = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | targs.type = XFS_ALLOCTYPE_THIS_AG; |
| 2197 | targs.pag = pag; |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2198 | error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp); |
| 2199 | if (error) |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2200 | goto out_agbp_relse; |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2201 | |
| 2202 | /* Make the freelist longer if it's too short. */ |
| 2203 | while (pag->pagf_flcount < need) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | targs.agbno = 0; |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2205 | targs.maxlen = need - pag->pagf_flcount; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2206 | targs.resv = XFS_AG_RESV_AGFL; |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 2207 | |
| 2208 | /* Allocate as many blocks as possible at once. */ |
| 2209 | error = xfs_alloc_ag_vextent(&targs); |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2210 | if (error) |
| 2211 | goto out_agflbp_relse; |
| 2212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | /* |
| 2214 | * Stop if we run out. Won't happen if callers are obeying |
| 2215 | * the restrictions correctly. Can happen for free calls |
| 2216 | * on a completely full ag. |
| 2217 | */ |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2218 | if (targs.agbno == NULLAGBLOCK) { |
Nathan Scott | 0e1edbd | 2006-08-10 14:40:41 +1000 | [diff] [blame] | 2219 | if (flags & XFS_ALLOC_FLAG_FREEING) |
| 2220 | break; |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2221 | goto out_agflbp_relse; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | /* |
| 2224 | * Put each allocated block on the list. |
| 2225 | */ |
| 2226 | for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) { |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2227 | error = xfs_alloc_put_freelist(tp, agbp, |
| 2228 | agflbp, bno, 0); |
| 2229 | if (error) |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2230 | goto out_agflbp_relse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
Nathan Scott | e63a369 | 2006-05-08 19:51:58 +1000 | [diff] [blame] | 2233 | xfs_trans_brelse(tp, agflbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | args->agbp = agbp; |
| 2235 | return 0; |
Dave Chinner | 396503f | 2015-06-22 10:13:19 +1000 | [diff] [blame] | 2236 | |
| 2237 | out_agflbp_relse: |
| 2238 | xfs_trans_brelse(tp, agflbp); |
| 2239 | out_agbp_relse: |
| 2240 | if (agbp) |
| 2241 | xfs_trans_brelse(tp, agbp); |
| 2242 | out_no_agbp: |
| 2243 | args->agbp = NULL; |
| 2244 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Get a block from the freelist. |
| 2249 | * Returns with the buffer for the block gotten. |
| 2250 | */ |
| 2251 | int /* error */ |
| 2252 | xfs_alloc_get_freelist( |
| 2253 | xfs_trans_t *tp, /* transaction pointer */ |
| 2254 | xfs_buf_t *agbp, /* buffer containing the agf structure */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2255 | xfs_agblock_t *bnop, /* block address retrieved from freelist */ |
| 2256 | int btreeblk) /* destination is a AGF btree */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | { |
| 2258 | xfs_agf_t *agf; /* a.g. freespace structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */ |
| 2260 | xfs_agblock_t bno; /* block number returned */ |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2261 | __be32 *agfl_bno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | int error; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2263 | int logflags; |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2264 | xfs_mount_t *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | xfs_perag_t *pag; /* per allocation group data */ |
| 2266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | /* |
| 2268 | * Freelist is empty, give up. |
| 2269 | */ |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2270 | agf = XFS_BUF_TO_AGF(agbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | if (!agf->agf_flcount) { |
| 2272 | *bnop = NULLAGBLOCK; |
| 2273 | return 0; |
| 2274 | } |
| 2275 | /* |
| 2276 | * Read the array of free blocks. |
| 2277 | */ |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2278 | error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno), |
| 2279 | &agflbp); |
| 2280 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | return error; |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2282 | |
| 2283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | /* |
| 2285 | * Get the block number and update the data structures. |
| 2286 | */ |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2287 | agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp); |
| 2288 | bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2289 | be32_add_cpu(&agf->agf_flfirst, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | xfs_trans_brelse(tp, agflbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2291 | if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | agf->agf_flfirst = 0; |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2293 | |
| 2294 | pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno)); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2295 | be32_add_cpu(&agf->agf_flcount, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | xfs_trans_agflist_delta(tp, -1); |
| 2297 | pag->pagf_flcount--; |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2298 | xfs_perag_put(pag); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2299 | |
| 2300 | logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT; |
| 2301 | if (btreeblk) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2302 | be32_add_cpu(&agf->agf_btreeblks, 1); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2303 | pag->pagf_btreeblks++; |
| 2304 | logflags |= XFS_AGF_BTREEBLKS; |
| 2305 | } |
| 2306 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2307 | xfs_alloc_log_agf(tp, agbp, logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | *bnop = bno; |
| 2309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | return 0; |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * Log the given fields from the agf structure. |
| 2315 | */ |
| 2316 | void |
| 2317 | xfs_alloc_log_agf( |
| 2318 | xfs_trans_t *tp, /* transaction pointer */ |
| 2319 | xfs_buf_t *bp, /* buffer for a.g. freelist header */ |
| 2320 | int fields) /* mask of fields to be logged (XFS_AGF_...) */ |
| 2321 | { |
| 2322 | int first; /* first byte offset */ |
| 2323 | int last; /* last byte offset */ |
| 2324 | static const short offsets[] = { |
| 2325 | offsetof(xfs_agf_t, agf_magicnum), |
| 2326 | offsetof(xfs_agf_t, agf_versionnum), |
| 2327 | offsetof(xfs_agf_t, agf_seqno), |
| 2328 | offsetof(xfs_agf_t, agf_length), |
| 2329 | offsetof(xfs_agf_t, agf_roots[0]), |
| 2330 | offsetof(xfs_agf_t, agf_levels[0]), |
| 2331 | offsetof(xfs_agf_t, agf_flfirst), |
| 2332 | offsetof(xfs_agf_t, agf_fllast), |
| 2333 | offsetof(xfs_agf_t, agf_flcount), |
| 2334 | offsetof(xfs_agf_t, agf_freeblks), |
| 2335 | offsetof(xfs_agf_t, agf_longest), |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2336 | offsetof(xfs_agf_t, agf_btreeblks), |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2337 | offsetof(xfs_agf_t, agf_uuid), |
Darrick J. Wong | f32866fd | 2016-08-17 08:31:49 +1000 | [diff] [blame] | 2338 | offsetof(xfs_agf_t, agf_rmap_blocks), |
Darrick J. Wong | bdf2863 | 2016-10-03 09:11:19 -0700 | [diff] [blame] | 2339 | offsetof(xfs_agf_t, agf_refcount_blocks), |
| 2340 | offsetof(xfs_agf_t, agf_refcount_root), |
| 2341 | offsetof(xfs_agf_t, agf_refcount_level), |
Darrick J. Wong | da1f039 | 2016-08-26 15:59:31 +1000 | [diff] [blame] | 2342 | /* needed so that we don't log the whole rest of the structure: */ |
| 2343 | offsetof(xfs_agf_t, agf_spare64), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | sizeof(xfs_agf_t) |
| 2345 | }; |
| 2346 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2347 | trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_); |
| 2348 | |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 2349 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF); |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last); |
| 2352 | xfs_trans_log_buf(tp, bp, (uint)first, (uint)last); |
| 2353 | } |
| 2354 | |
| 2355 | /* |
| 2356 | * Interface for inode allocation to force the pag data to be initialized. |
| 2357 | */ |
| 2358 | int /* error */ |
| 2359 | xfs_alloc_pagf_init( |
| 2360 | xfs_mount_t *mp, /* file system mount structure */ |
| 2361 | xfs_trans_t *tp, /* transaction pointer */ |
| 2362 | xfs_agnumber_t agno, /* allocation group number */ |
| 2363 | int flags) /* XFS_ALLOC_FLAGS_... */ |
| 2364 | { |
| 2365 | xfs_buf_t *bp; |
| 2366 | int error; |
| 2367 | |
| 2368 | if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp))) |
| 2369 | return error; |
| 2370 | if (bp) |
| 2371 | xfs_trans_brelse(tp, bp); |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * Put the block on the freelist for the allocation group. |
| 2377 | */ |
| 2378 | int /* error */ |
| 2379 | xfs_alloc_put_freelist( |
| 2380 | xfs_trans_t *tp, /* transaction pointer */ |
| 2381 | xfs_buf_t *agbp, /* buffer for a.g. freelist header */ |
| 2382 | xfs_buf_t *agflbp,/* buffer for a.g. free block array */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2383 | xfs_agblock_t bno, /* block being freed */ |
| 2384 | int btreeblk) /* block came from a AGF btree */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | { |
| 2386 | xfs_agf_t *agf; /* a.g. freespace structure */ |
Christoph Hellwig | e210100 | 2006-09-28 10:56:51 +1000 | [diff] [blame] | 2387 | __be32 *blockp;/* pointer to array entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | int error; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2389 | int logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | xfs_mount_t *mp; /* mount structure */ |
| 2391 | xfs_perag_t *pag; /* per allocation group data */ |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2392 | __be32 *agfl_bno; |
| 2393 | int startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
| 2395 | agf = XFS_BUF_TO_AGF(agbp); |
| 2396 | mp = tp->t_mountp; |
| 2397 | |
| 2398 | if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2399 | be32_to_cpu(agf->agf_seqno), &agflbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | return error; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2401 | be32_add_cpu(&agf->agf_fllast, 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2402 | if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | agf->agf_fllast = 0; |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2404 | |
| 2405 | pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno)); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2406 | be32_add_cpu(&agf->agf_flcount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | xfs_trans_agflist_delta(tp, 1); |
| 2408 | pag->pagf_flcount++; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2409 | |
| 2410 | logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT; |
| 2411 | if (btreeblk) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2412 | be32_add_cpu(&agf->agf_btreeblks, -1); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2413 | pag->pagf_btreeblks--; |
| 2414 | logflags |= XFS_AGF_BTREEBLKS; |
| 2415 | } |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2416 | xfs_perag_put(pag); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2417 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2418 | xfs_alloc_log_agf(tp, agbp, logflags); |
| 2419 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2420 | ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)); |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2421 | |
| 2422 | agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp); |
| 2423 | blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)]; |
Christoph Hellwig | e210100 | 2006-09-28 10:56:51 +1000 | [diff] [blame] | 2424 | *blockp = cpu_to_be32(bno); |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2425 | startoff = (char *)blockp - (char *)agflbp->b_addr; |
| 2426 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2427 | xfs_alloc_log_agf(tp, agbp, logflags); |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2428 | |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 2429 | xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF); |
Christoph Hellwig | 77c95bb | 2013-04-03 16:11:14 +1100 | [diff] [blame] | 2430 | xfs_trans_log_buf(tp, agflbp, startoff, |
| 2431 | startoff + sizeof(xfs_agblock_t) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | return 0; |
| 2433 | } |
| 2434 | |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2435 | static bool |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2436 | xfs_agf_verify( |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2437 | struct xfs_mount *mp, |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2438 | struct xfs_buf *bp) |
| 2439 | { |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2440 | struct xfs_agf *agf = XFS_BUF_TO_AGF(bp); |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2441 | |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 2442 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 2443 | if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid)) |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2444 | return false; |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 2445 | if (!xfs_log_check_lsn(mp, |
| 2446 | be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn))) |
| 2447 | return false; |
| 2448 | } |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2449 | |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2450 | if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) && |
| 2451 | XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) && |
| 2452 | be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) && |
| 2453 | be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) && |
| 2454 | be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) && |
| 2455 | be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp))) |
| 2456 | return false; |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2457 | |
Darrick J. Wong | d2a047f | 2016-12-05 12:32:50 +1100 | [diff] [blame] | 2458 | if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 || |
| 2459 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 || |
| 2460 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS || |
Eric Sandeen | e1b0572 | 2014-09-09 11:47:24 +1000 | [diff] [blame] | 2461 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS) |
| 2462 | return false; |
| 2463 | |
Darrick J. Wong | b870494 | 2016-08-03 11:30:32 +1000 | [diff] [blame] | 2464 | if (xfs_sb_version_hasrmapbt(&mp->m_sb) && |
Darrick J. Wong | d2a047f | 2016-12-05 12:32:50 +1100 | [diff] [blame] | 2465 | (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 || |
| 2466 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)) |
Darrick J. Wong | b870494 | 2016-08-03 11:30:32 +1000 | [diff] [blame] | 2467 | return false; |
| 2468 | |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2469 | /* |
| 2470 | * during growfs operations, the perag is not fully initialised, |
| 2471 | * so we can't use it for any useful checking. growfs ensures we can't |
| 2472 | * use it by using uncached buffers that don't have the perag attached |
| 2473 | * so we can detect and avoid this problem. |
| 2474 | */ |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2475 | if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno) |
| 2476 | return false; |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2477 | |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2478 | if (xfs_sb_version_haslazysbcount(&mp->m_sb) && |
| 2479 | be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length)) |
| 2480 | return false; |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2481 | |
Darrick J. Wong | 46eeb52 | 2016-10-03 09:11:16 -0700 | [diff] [blame] | 2482 | if (xfs_sb_version_hasreflink(&mp->m_sb) && |
Darrick J. Wong | d2a047f | 2016-12-05 12:32:50 +1100 | [diff] [blame] | 2483 | (be32_to_cpu(agf->agf_refcount_level) < 1 || |
| 2484 | be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS)) |
Darrick J. Wong | 46eeb52 | 2016-10-03 09:11:16 -0700 | [diff] [blame] | 2485 | return false; |
| 2486 | |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2487 | return true;; |
| 2488 | |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2489 | } |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2490 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2491 | static void |
| 2492 | xfs_agf_read_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2493 | struct xfs_buf *bp) |
| 2494 | { |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2495 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2496 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2497 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 2498 | !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2499 | xfs_buf_ioerror(bp, -EFSBADCRC); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2500 | else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp, |
| 2501 | XFS_ERRTAG_ALLOC_READ_AGF, |
| 2502 | XFS_RANDOM_ALLOC_READ_AGF)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2503 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2504 | |
| 2505 | if (bp->b_error) |
| 2506 | xfs_verifier_error(bp); |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2507 | } |
| 2508 | |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 2509 | static void |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2510 | xfs_agf_write_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2511 | struct xfs_buf *bp) |
| 2512 | { |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2513 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 2514 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 2515 | |
| 2516 | if (!xfs_agf_verify(mp, bp)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2517 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2518 | xfs_verifier_error(bp); |
Dave Chinner | 4e0e604 | 2013-04-03 16:11:13 +1100 | [diff] [blame] | 2519 | return; |
| 2520 | } |
| 2521 | |
| 2522 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 2523 | return; |
| 2524 | |
| 2525 | if (bip) |
| 2526 | XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 2527 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 2528 | xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF); |
Dave Chinner | 5d5f527 | 2012-11-14 17:44:56 +1100 | [diff] [blame] | 2529 | } |
| 2530 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2531 | const struct xfs_buf_ops xfs_agf_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 2532 | .name = "xfs_agf", |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2533 | .verify_read = xfs_agf_read_verify, |
| 2534 | .verify_write = xfs_agf_write_verify, |
| 2535 | }; |
| 2536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | /* |
| 2538 | * Read in the allocation group header (free/alloc section). |
| 2539 | */ |
| 2540 | int /* error */ |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2541 | xfs_read_agf( |
| 2542 | struct xfs_mount *mp, /* mount point structure */ |
| 2543 | struct xfs_trans *tp, /* transaction pointer */ |
| 2544 | xfs_agnumber_t agno, /* allocation group number */ |
| 2545 | int flags, /* XFS_BUF_ */ |
| 2546 | struct xfs_buf **bpp) /* buffer for the ag freelist header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | int error; |
| 2549 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2550 | trace_xfs_read_agf(mp, agno); |
| 2551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | ASSERT(agno != NULLAGNUMBER); |
| 2553 | error = xfs_trans_read_buf( |
| 2554 | mp, tp, mp->m_ddev_targp, |
| 2555 | XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)), |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2556 | XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | if (error) |
| 2558 | return error; |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2559 | if (!*bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | return 0; |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2561 | |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 2562 | ASSERT(!(*bpp)->b_error); |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 2563 | xfs_buf_set_ref(*bpp, XFS_AGF_REF); |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | /* |
| 2568 | * Read in the allocation group header (free/alloc section). |
| 2569 | */ |
| 2570 | int /* error */ |
| 2571 | xfs_alloc_read_agf( |
| 2572 | struct xfs_mount *mp, /* mount point structure */ |
| 2573 | struct xfs_trans *tp, /* transaction pointer */ |
| 2574 | xfs_agnumber_t agno, /* allocation group number */ |
| 2575 | int flags, /* XFS_ALLOC_FLAG_... */ |
| 2576 | struct xfs_buf **bpp) /* buffer for the ag freelist header */ |
| 2577 | { |
| 2578 | struct xfs_agf *agf; /* ag freelist header */ |
| 2579 | struct xfs_perag *pag; /* per allocation group data */ |
| 2580 | int error; |
| 2581 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2582 | trace_xfs_alloc_read_agf(mp, agno); |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2583 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2584 | ASSERT(agno != NULLAGNUMBER); |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2585 | error = xfs_read_agf(mp, tp, agno, |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 2586 | (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0, |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2587 | bpp); |
| 2588 | if (error) |
| 2589 | return error; |
| 2590 | if (!*bpp) |
| 2591 | return 0; |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 2592 | ASSERT(!(*bpp)->b_error); |
From: Christoph Hellwig | 4805621 | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 2593 | |
| 2594 | agf = XFS_BUF_TO_AGF(*bpp); |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2595 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | if (!pag->pagf_init) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2597 | pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2598 | pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2599 | pag->pagf_flcount = be32_to_cpu(agf->agf_flcount); |
| 2600 | pag->pagf_longest = be32_to_cpu(agf->agf_longest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | pag->pagf_levels[XFS_BTNUM_BNOi] = |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2602 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | pag->pagf_levels[XFS_BTNUM_CNTi] = |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2604 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]); |
Darrick J. Wong | b870494 | 2016-08-03 11:30:32 +1000 | [diff] [blame] | 2605 | pag->pagf_levels[XFS_BTNUM_RMAPi] = |
| 2606 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]); |
Darrick J. Wong | 46eeb52 | 2016-10-03 09:11:16 -0700 | [diff] [blame] | 2607 | pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level); |
Eric Sandeen | 007c61c | 2007-10-11 17:43:56 +1000 | [diff] [blame] | 2608 | spin_lock_init(&pag->pagb_lock); |
Dave Chinner | e57336f | 2010-01-11 11:47:49 +0000 | [diff] [blame] | 2609 | pag->pagb_count = 0; |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 2610 | pag->pagb_tree = RB_ROOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | pag->pagf_init = 1; |
| 2612 | } |
| 2613 | #ifdef DEBUG |
| 2614 | else if (!XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2615 | ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks)); |
Barry Naujok | 89b2839 | 2008-10-30 17:05:49 +1100 | [diff] [blame] | 2616 | ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks)); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2617 | ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount)); |
| 2618 | ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] == |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2620 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] == |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2622 | be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | } |
| 2624 | #endif |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2625 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | return 0; |
| 2627 | } |
| 2628 | |
| 2629 | /* |
| 2630 | * Allocate an extent (variable-size). |
| 2631 | * Depending on the allocation type, we either look in a single allocation |
| 2632 | * group or loop over the allocation groups to find the result. |
| 2633 | */ |
| 2634 | int /* error */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 2635 | xfs_alloc_vextent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | xfs_alloc_arg_t *args) /* allocation argument structure */ |
| 2637 | { |
| 2638 | xfs_agblock_t agsize; /* allocation group size */ |
| 2639 | int error; |
| 2640 | int flags; /* XFS_ALLOC_FLAG_... locking flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | xfs_extlen_t minleft;/* minimum left value, temp copy */ |
| 2642 | xfs_mount_t *mp; /* mount structure pointer */ |
| 2643 | xfs_agnumber_t sagno; /* starting allocation group number */ |
| 2644 | xfs_alloctype_t type; /* input allocation type */ |
| 2645 | int bump_rotor = 0; |
| 2646 | int no_min = 0; |
| 2647 | xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */ |
| 2648 | |
| 2649 | mp = args->mp; |
| 2650 | type = args->otype = args->type; |
| 2651 | args->agbno = NULLAGBLOCK; |
| 2652 | /* |
| 2653 | * Just fix this up, for the case where the last a.g. is shorter |
| 2654 | * (or there's only one a.g.) and the caller couldn't easily figure |
| 2655 | * that out (xfs_bmap_alloc). |
| 2656 | */ |
| 2657 | agsize = mp->m_sb.sb_agblocks; |
| 2658 | if (args->maxlen > agsize) |
| 2659 | args->maxlen = agsize; |
| 2660 | if (args->alignment == 0) |
| 2661 | args->alignment = 1; |
| 2662 | ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount); |
| 2663 | ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize); |
| 2664 | ASSERT(args->minlen <= args->maxlen); |
| 2665 | ASSERT(args->minlen <= agsize); |
| 2666 | ASSERT(args->mod < args->prod); |
| 2667 | if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount || |
| 2668 | XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize || |
| 2669 | args->minlen > args->maxlen || args->minlen > agsize || |
| 2670 | args->mod >= args->prod) { |
| 2671 | args->fsbno = NULLFSBLOCK; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2672 | trace_xfs_alloc_vextent_badargs(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | return 0; |
| 2674 | } |
| 2675 | minleft = args->minleft; |
| 2676 | |
| 2677 | switch (type) { |
| 2678 | case XFS_ALLOCTYPE_THIS_AG: |
| 2679 | case XFS_ALLOCTYPE_NEAR_BNO: |
| 2680 | case XFS_ALLOCTYPE_THIS_BNO: |
| 2681 | /* |
| 2682 | * These three force us into a single a.g. |
| 2683 | */ |
| 2684 | args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno); |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2685 | args->pag = xfs_perag_get(mp, args->agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | args->minleft = 0; |
| 2687 | error = xfs_alloc_fix_freelist(args, 0); |
| 2688 | args->minleft = minleft; |
| 2689 | if (error) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2690 | trace_xfs_alloc_vextent_nofix(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | goto error0; |
| 2692 | } |
| 2693 | if (!args->agbp) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2694 | trace_xfs_alloc_vextent_noagbp(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | break; |
| 2696 | } |
| 2697 | args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno); |
| 2698 | if ((error = xfs_alloc_ag_vextent(args))) |
| 2699 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | break; |
| 2701 | case XFS_ALLOCTYPE_START_BNO: |
| 2702 | /* |
| 2703 | * Try near allocation first, then anywhere-in-ag after |
| 2704 | * the first a.g. fails. |
| 2705 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 2706 | if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | (mp->m_flags & XFS_MOUNT_32BITINODES)) { |
| 2708 | args->fsbno = XFS_AGB_TO_FSB(mp, |
| 2709 | ((mp->m_agfrotor / rotorstep) % |
| 2710 | mp->m_sb.sb_agcount), 0); |
| 2711 | bump_rotor = 1; |
| 2712 | } |
| 2713 | args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno); |
| 2714 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2715 | /* FALLTHROUGH */ |
| 2716 | case XFS_ALLOCTYPE_ANY_AG: |
| 2717 | case XFS_ALLOCTYPE_START_AG: |
| 2718 | case XFS_ALLOCTYPE_FIRST_AG: |
| 2719 | /* |
| 2720 | * Rotate through the allocation groups looking for a winner. |
| 2721 | */ |
| 2722 | if (type == XFS_ALLOCTYPE_ANY_AG) { |
| 2723 | /* |
| 2724 | * Start with the last place we left off. |
| 2725 | */ |
| 2726 | args->agno = sagno = (mp->m_agfrotor / rotorstep) % |
| 2727 | mp->m_sb.sb_agcount; |
| 2728 | args->type = XFS_ALLOCTYPE_THIS_AG; |
| 2729 | flags = XFS_ALLOC_FLAG_TRYLOCK; |
| 2730 | } else if (type == XFS_ALLOCTYPE_FIRST_AG) { |
| 2731 | /* |
| 2732 | * Start with allocation group given by bno. |
| 2733 | */ |
| 2734 | args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2735 | args->type = XFS_ALLOCTYPE_THIS_AG; |
| 2736 | sagno = 0; |
| 2737 | flags = 0; |
| 2738 | } else { |
| 2739 | if (type == XFS_ALLOCTYPE_START_AG) |
| 2740 | args->type = XFS_ALLOCTYPE_THIS_AG; |
| 2741 | /* |
| 2742 | * Start with the given allocation group. |
| 2743 | */ |
| 2744 | args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2745 | flags = XFS_ALLOC_FLAG_TRYLOCK; |
| 2746 | } |
| 2747 | /* |
| 2748 | * Loop over allocation groups twice; first time with |
| 2749 | * trylock set, second time without. |
| 2750 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | for (;;) { |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2752 | args->pag = xfs_perag_get(mp, args->agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | if (no_min) args->minleft = 0; |
| 2754 | error = xfs_alloc_fix_freelist(args, flags); |
| 2755 | args->minleft = minleft; |
| 2756 | if (error) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2757 | trace_xfs_alloc_vextent_nofix(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | goto error0; |
| 2759 | } |
| 2760 | /* |
| 2761 | * If we get a buffer back then the allocation will fly. |
| 2762 | */ |
| 2763 | if (args->agbp) { |
| 2764 | if ((error = xfs_alloc_ag_vextent(args))) |
| 2765 | goto error0; |
| 2766 | break; |
| 2767 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2768 | |
| 2769 | trace_xfs_alloc_vextent_loopfailed(args); |
| 2770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | /* |
| 2772 | * Didn't work, figure out the next iteration. |
| 2773 | */ |
| 2774 | if (args->agno == sagno && |
| 2775 | type == XFS_ALLOCTYPE_START_BNO) |
| 2776 | args->type = XFS_ALLOCTYPE_THIS_AG; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2777 | /* |
| 2778 | * For the first allocation, we can try any AG to get |
| 2779 | * space. However, if we already have allocated a |
| 2780 | * block, we don't want to try AGs whose number is below |
| 2781 | * sagno. Otherwise, we may end up with out-of-order |
| 2782 | * locking of AGF, which might cause deadlock. |
| 2783 | */ |
| 2784 | if (++(args->agno) == mp->m_sb.sb_agcount) { |
| 2785 | if (args->firstblock != NULLFSBLOCK) |
| 2786 | args->agno = sagno; |
| 2787 | else |
| 2788 | args->agno = 0; |
| 2789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | /* |
| 2791 | * Reached the starting a.g., must either be done |
| 2792 | * or switch to non-trylock mode. |
| 2793 | */ |
| 2794 | if (args->agno == sagno) { |
| 2795 | if (no_min == 1) { |
| 2796 | args->agbno = NULLAGBLOCK; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2797 | trace_xfs_alloc_vextent_allfailed(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | break; |
| 2799 | } |
| 2800 | if (flags == 0) { |
| 2801 | no_min = 1; |
| 2802 | } else { |
| 2803 | flags = 0; |
| 2804 | if (type == XFS_ALLOCTYPE_START_BNO) { |
| 2805 | args->agbno = XFS_FSB_TO_AGBNO(mp, |
| 2806 | args->fsbno); |
| 2807 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2808 | } |
| 2809 | } |
| 2810 | } |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2811 | xfs_perag_put(args->pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) { |
| 2814 | if (args->agno == sagno) |
| 2815 | mp->m_agfrotor = (mp->m_agfrotor + 1) % |
| 2816 | (mp->m_sb.sb_agcount * rotorstep); |
| 2817 | else |
| 2818 | mp->m_agfrotor = (args->agno * rotorstep + 1) % |
| 2819 | (mp->m_sb.sb_agcount * rotorstep); |
| 2820 | } |
| 2821 | break; |
| 2822 | default: |
| 2823 | ASSERT(0); |
| 2824 | /* NOTREACHED */ |
| 2825 | } |
| 2826 | if (args->agbno == NULLAGBLOCK) |
| 2827 | args->fsbno = NULLFSBLOCK; |
| 2828 | else { |
| 2829 | args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno); |
| 2830 | #ifdef DEBUG |
| 2831 | ASSERT(args->len >= args->minlen); |
| 2832 | ASSERT(args->len <= args->maxlen); |
| 2833 | ASSERT(args->agbno % args->alignment == 0); |
| 2834 | XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno), |
| 2835 | args->len); |
| 2836 | #endif |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 2837 | |
| 2838 | /* Zero the extent if we were asked to do so */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 2839 | if (args->datatype & XFS_ALLOC_USERDATA_ZERO) { |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 2840 | error = xfs_zero_extent(args->ip, args->fsbno, args->len); |
| 2841 | if (error) |
| 2842 | goto error0; |
| 2843 | } |
| 2844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | } |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2846 | xfs_perag_put(args->pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | return 0; |
| 2848 | error0: |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2849 | xfs_perag_put(args->pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | return error; |
| 2851 | } |
| 2852 | |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2853 | /* Ensure that the freelist is at full capacity. */ |
| 2854 | int |
| 2855 | xfs_free_extent_fix_freelist( |
| 2856 | struct xfs_trans *tp, |
| 2857 | xfs_agnumber_t agno, |
| 2858 | struct xfs_buf **agbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | { |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2860 | struct xfs_alloc_arg args; |
| 2861 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2863 | memset(&args, 0, sizeof(struct xfs_alloc_arg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | args.tp = tp; |
| 2865 | args.mp = tp->t_mountp; |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2866 | args.agno = agno; |
Dave Chinner | be65b18 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 2867 | |
| 2868 | /* |
| 2869 | * validate that the block number is legal - the enables us to detect |
| 2870 | * and handle a silent filesystem corruption rather than crashing. |
| 2871 | */ |
Dave Chinner | be65b18 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 2872 | if (args.agno >= args.mp->m_sb.sb_agcount) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2873 | return -EFSCORRUPTED; |
Dave Chinner | be65b18 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 2874 | |
Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 2875 | args.pag = xfs_perag_get(args.mp, args.agno); |
Dave Chinner | be65b18 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 2876 | ASSERT(args.pag); |
| 2877 | |
| 2878 | error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING); |
| 2879 | if (error) |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2880 | goto out; |
| 2881 | |
| 2882 | *agbp = args.agbp; |
| 2883 | out: |
| 2884 | xfs_perag_put(args.pag); |
| 2885 | return error; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * Free an extent. |
| 2890 | * Just break up the extent address and hand off to xfs_free_ag_extent |
| 2891 | * after fixing up the freelist. |
| 2892 | */ |
| 2893 | int /* error */ |
| 2894 | xfs_free_extent( |
| 2895 | struct xfs_trans *tp, /* transaction pointer */ |
| 2896 | xfs_fsblock_t bno, /* starting block number of extent */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 2897 | xfs_extlen_t len, /* length of extent */ |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2898 | struct xfs_owner_info *oinfo, /* extent owner */ |
| 2899 | enum xfs_ag_resv_type type) /* block reservation type */ |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2900 | { |
| 2901 | struct xfs_mount *mp = tp->t_mountp; |
| 2902 | struct xfs_buf *agbp; |
| 2903 | xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno); |
| 2904 | xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 2905 | int error; |
| 2906 | |
| 2907 | ASSERT(len != 0); |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2908 | ASSERT(type != XFS_AG_RESV_AGFL); |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2909 | |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 2910 | if (XFS_TEST_ERROR(false, mp, |
| 2911 | XFS_ERRTAG_FREE_EXTENT, |
| 2912 | XFS_RANDOM_FREE_EXTENT)) |
| 2913 | return -EIO; |
| 2914 | |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2915 | error = xfs_free_extent_fix_freelist(tp, agno, &agbp); |
| 2916 | if (error) |
| 2917 | return error; |
| 2918 | |
| 2919 | XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err); |
Dave Chinner | be65b18 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 2920 | |
| 2921 | /* validate the extent size is legal now we have the agf locked */ |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2922 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 2923 | agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length), |
| 2924 | err); |
Dave Chinner | be65b18 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 2925 | |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 2926 | error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type); |
Dave Chinner | 4d89e20 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2927 | if (error) |
| 2928 | goto err; |
| 2929 | |
| 2930 | xfs_extent_busy_insert(tp, agno, agbno, len, 0); |
| 2931 | return 0; |
| 2932 | |
| 2933 | err: |
| 2934 | xfs_trans_brelse(tp, agbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | return error; |
| 2936 | } |