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