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