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