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