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