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