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