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