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