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