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