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