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