| 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-2001,2005 Silicon Graphics, Inc. | 
|  | 3 | * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or | 
|  | 6 | * modify it under the terms of the GNU General Public License as | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. | 
|  | 8 | * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, | 
|  | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 12 | * GNU General Public License for more details. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License | 
|  | 15 | * along with this program; if not, write the Free Software Foundation, | 
|  | 16 | * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" | 
|  | 25 | #include "xfs_sb.h" | 
|  | 26 | #include "xfs_ag.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_dir2.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dmapi.h" | 
|  | 29 | #include "xfs_mount.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" | 
|  | 32 | #include "xfs_ialloc_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" | 
|  | 34 | #include "xfs_attr_sf.h" | 
|  | 35 | #include "xfs_dinode.h" | 
|  | 36 | #include "xfs_inode.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_btree.h" | 
| Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 38 | #include "xfs_btree_trace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_ialloc.h" | 
|  | 40 | #include "xfs_alloc.h" | 
|  | 41 | #include "xfs_error.h" | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 42 | #include "xfs_trace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 |  | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 45 | STATIC struct xfs_btree_cur * | 
|  | 46 | xfs_allocbt_dup_cursor( | 
|  | 47 | struct xfs_btree_cur	*cur) | 
|  | 48 | { | 
|  | 49 | return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp, | 
|  | 50 | cur->bc_private.a.agbp, cur->bc_private.a.agno, | 
|  | 51 | cur->bc_btnum); | 
|  | 52 | } | 
|  | 53 |  | 
| Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 54 | STATIC void | 
|  | 55 | xfs_allocbt_set_root( | 
|  | 56 | struct xfs_btree_cur	*cur, | 
|  | 57 | union xfs_btree_ptr	*ptr, | 
|  | 58 | int			inc) | 
|  | 59 | { | 
|  | 60 | struct xfs_buf		*agbp = cur->bc_private.a.agbp; | 
|  | 61 | struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp); | 
|  | 62 | xfs_agnumber_t		seqno = be32_to_cpu(agf->agf_seqno); | 
|  | 63 | int			btnum = cur->bc_btnum; | 
| Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 64 | struct xfs_perag	*pag = xfs_perag_get(cur->bc_mp, seqno); | 
| Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 65 |  | 
|  | 66 | ASSERT(ptr->s != 0); | 
|  | 67 |  | 
|  | 68 | agf->agf_roots[btnum] = ptr->s; | 
|  | 69 | be32_add_cpu(&agf->agf_levels[btnum], inc); | 
| Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 70 | pag->pagf_levels[btnum] += inc; | 
|  | 71 | xfs_perag_put(pag); | 
| Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 72 |  | 
|  | 73 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS); | 
|  | 74 | } | 
|  | 75 |  | 
| Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 76 | STATIC int | 
|  | 77 | xfs_allocbt_alloc_block( | 
|  | 78 | struct xfs_btree_cur	*cur, | 
|  | 79 | union xfs_btree_ptr	*start, | 
|  | 80 | union xfs_btree_ptr	*new, | 
|  | 81 | int			length, | 
|  | 82 | int			*stat) | 
|  | 83 | { | 
|  | 84 | int			error; | 
|  | 85 | xfs_agblock_t		bno; | 
|  | 86 |  | 
|  | 87 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | 
|  | 88 |  | 
|  | 89 | /* Allocate the new block from the freelist. If we can't, give up.  */ | 
|  | 90 | error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp, | 
|  | 91 | &bno, 1); | 
|  | 92 | if (error) { | 
|  | 93 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | 
|  | 94 | return error; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | if (bno == NULLAGBLOCK) { | 
|  | 98 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | 
|  | 99 | *stat = 0; | 
|  | 100 | return 0; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | xfs_trans_agbtree_delta(cur->bc_tp, 1); | 
|  | 104 | new->s = cpu_to_be32(bno); | 
|  | 105 |  | 
|  | 106 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | 
|  | 107 | *stat = 1; | 
|  | 108 | return 0; | 
|  | 109 | } | 
|  | 110 |  | 
| Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 111 | STATIC int | 
|  | 112 | xfs_allocbt_free_block( | 
|  | 113 | struct xfs_btree_cur	*cur, | 
|  | 114 | struct xfs_buf		*bp) | 
|  | 115 | { | 
|  | 116 | struct xfs_buf		*agbp = cur->bc_private.a.agbp; | 
|  | 117 | struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp); | 
|  | 118 | xfs_agblock_t		bno; | 
|  | 119 | int			error; | 
|  | 120 |  | 
| Eric Sandeen | b6e3222 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 121 | bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp)); | 
| Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 122 | error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1); | 
|  | 123 | if (error) | 
|  | 124 | return error; | 
|  | 125 |  | 
|  | 126 | /* | 
|  | 127 | * Since blocks move to the free list without the coordination used in | 
|  | 128 | * xfs_bmap_finish, we can't allow block to be available for | 
|  | 129 | * reallocation and non-transaction writing (user data) until we know | 
|  | 130 | * that the transaction that moved it to the free list is permanently | 
|  | 131 | * on disk. We track the blocks by declaring these blocks as "busy"; | 
|  | 132 | * the busy list is maintained on a per-ag basis and each transaction | 
|  | 133 | * records which entries should be removed when the iclog commits to | 
|  | 134 | * disk. If a busy block is allocated, the iclog is pushed up to the | 
|  | 135 | * LSN that freed the block. | 
|  | 136 | */ | 
| Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 137 | xfs_alloc_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1); | 
| Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 138 | xfs_trans_agbtree_delta(cur->bc_tp, -1); | 
|  | 139 | return 0; | 
|  | 140 | } | 
|  | 141 |  | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 142 | /* | 
|  | 143 | * Update the longest extent in the AGF | 
|  | 144 | */ | 
|  | 145 | STATIC void | 
|  | 146 | xfs_allocbt_update_lastrec( | 
|  | 147 | struct xfs_btree_cur	*cur, | 
|  | 148 | struct xfs_btree_block	*block, | 
|  | 149 | union xfs_btree_rec	*rec, | 
|  | 150 | int			ptr, | 
|  | 151 | int			reason) | 
|  | 152 | { | 
|  | 153 | struct xfs_agf		*agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); | 
|  | 154 | xfs_agnumber_t		seqno = be32_to_cpu(agf->agf_seqno); | 
| Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 155 | struct xfs_perag	*pag; | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 156 | __be32			len; | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 157 | int			numrecs; | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 158 |  | 
|  | 159 | ASSERT(cur->bc_btnum == XFS_BTNUM_CNT); | 
|  | 160 |  | 
|  | 161 | switch (reason) { | 
|  | 162 | case LASTREC_UPDATE: | 
|  | 163 | /* | 
|  | 164 | * If this is the last leaf block and it's the last record, | 
|  | 165 | * then update the size of the longest extent in the AG. | 
|  | 166 | */ | 
|  | 167 | if (ptr != xfs_btree_get_numrecs(block)) | 
|  | 168 | return; | 
|  | 169 | len = rec->alloc.ar_blockcount; | 
|  | 170 | break; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 171 | case LASTREC_INSREC: | 
|  | 172 | if (be32_to_cpu(rec->alloc.ar_blockcount) <= | 
|  | 173 | be32_to_cpu(agf->agf_longest)) | 
|  | 174 | return; | 
|  | 175 | len = rec->alloc.ar_blockcount; | 
|  | 176 | break; | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 177 | case LASTREC_DELREC: | 
|  | 178 | numrecs = xfs_btree_get_numrecs(block); | 
|  | 179 | if (ptr <= numrecs) | 
|  | 180 | return; | 
|  | 181 | ASSERT(ptr == numrecs + 1); | 
|  | 182 |  | 
|  | 183 | if (numrecs) { | 
|  | 184 | xfs_alloc_rec_t *rrp; | 
|  | 185 |  | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 186 | rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 187 | len = rrp->ar_blockcount; | 
|  | 188 | } else { | 
|  | 189 | len = 0; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | break; | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 193 | default: | 
|  | 194 | ASSERT(0); | 
|  | 195 | return; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | agf->agf_longest = len; | 
| Dave Chinner | a862e0f | 2010-01-11 11:47:41 +0000 | [diff] [blame] | 199 | pag = xfs_perag_get(cur->bc_mp, seqno); | 
|  | 200 | pag->pagf_longest = be32_to_cpu(len); | 
|  | 201 | xfs_perag_put(pag); | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 202 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST); | 
|  | 203 | } | 
|  | 204 |  | 
| Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 205 | STATIC int | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 206 | xfs_allocbt_get_minrecs( | 
|  | 207 | struct xfs_btree_cur	*cur, | 
|  | 208 | int			level) | 
|  | 209 | { | 
|  | 210 | return cur->bc_mp->m_alloc_mnr[level != 0]; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | STATIC int | 
| Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 214 | xfs_allocbt_get_maxrecs( | 
|  | 215 | struct xfs_btree_cur	*cur, | 
|  | 216 | int			level) | 
|  | 217 | { | 
|  | 218 | return cur->bc_mp->m_alloc_mxr[level != 0]; | 
|  | 219 | } | 
|  | 220 |  | 
| Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 221 | STATIC void | 
|  | 222 | xfs_allocbt_init_key_from_rec( | 
|  | 223 | union xfs_btree_key	*key, | 
|  | 224 | union xfs_btree_rec	*rec) | 
|  | 225 | { | 
|  | 226 | ASSERT(rec->alloc.ar_startblock != 0); | 
|  | 227 |  | 
|  | 228 | key->alloc.ar_startblock = rec->alloc.ar_startblock; | 
|  | 229 | key->alloc.ar_blockcount = rec->alloc.ar_blockcount; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | STATIC void | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 233 | xfs_allocbt_init_rec_from_key( | 
|  | 234 | union xfs_btree_key	*key, | 
|  | 235 | union xfs_btree_rec	*rec) | 
|  | 236 | { | 
|  | 237 | ASSERT(key->alloc.ar_startblock != 0); | 
|  | 238 |  | 
|  | 239 | rec->alloc.ar_startblock = key->alloc.ar_startblock; | 
|  | 240 | rec->alloc.ar_blockcount = key->alloc.ar_blockcount; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | STATIC void | 
|  | 244 | xfs_allocbt_init_rec_from_cur( | 
|  | 245 | struct xfs_btree_cur	*cur, | 
|  | 246 | union xfs_btree_rec	*rec) | 
|  | 247 | { | 
|  | 248 | ASSERT(cur->bc_rec.a.ar_startblock != 0); | 
|  | 249 |  | 
|  | 250 | rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock); | 
|  | 251 | rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | STATIC void | 
| Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 255 | xfs_allocbt_init_ptr_from_cur( | 
|  | 256 | struct xfs_btree_cur	*cur, | 
|  | 257 | union xfs_btree_ptr	*ptr) | 
|  | 258 | { | 
|  | 259 | struct xfs_agf		*agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); | 
|  | 260 |  | 
|  | 261 | ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno)); | 
|  | 262 | ASSERT(agf->agf_roots[cur->bc_btnum] != 0); | 
|  | 263 |  | 
|  | 264 | ptr->s = agf->agf_roots[cur->bc_btnum]; | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | STATIC __int64_t | 
|  | 268 | xfs_allocbt_key_diff( | 
|  | 269 | struct xfs_btree_cur	*cur, | 
|  | 270 | union xfs_btree_key	*key) | 
|  | 271 | { | 
|  | 272 | xfs_alloc_rec_incore_t	*rec = &cur->bc_rec.a; | 
|  | 273 | xfs_alloc_key_t		*kp = &key->alloc; | 
|  | 274 | __int64_t		diff; | 
|  | 275 |  | 
|  | 276 | if (cur->bc_btnum == XFS_BTNUM_BNO) { | 
|  | 277 | return (__int64_t)be32_to_cpu(kp->ar_startblock) - | 
|  | 278 | rec->ar_startblock; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount; | 
|  | 282 | if (diff) | 
|  | 283 | return diff; | 
|  | 284 |  | 
|  | 285 | return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock; | 
|  | 286 | } | 
|  | 287 |  | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 288 | STATIC int | 
|  | 289 | xfs_allocbt_kill_root( | 
|  | 290 | struct xfs_btree_cur	*cur, | 
|  | 291 | struct xfs_buf		*bp, | 
|  | 292 | int			level, | 
|  | 293 | union xfs_btree_ptr	*newroot) | 
|  | 294 | { | 
|  | 295 | int			error; | 
|  | 296 |  | 
|  | 297 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); | 
|  | 298 | XFS_BTREE_STATS_INC(cur, killroot); | 
|  | 299 |  | 
|  | 300 | /* | 
|  | 301 | * Update the root pointer, decreasing the level by 1 and then | 
|  | 302 | * free the old root. | 
|  | 303 | */ | 
|  | 304 | xfs_allocbt_set_root(cur, newroot, -1); | 
|  | 305 | error = xfs_allocbt_free_block(cur, bp); | 
|  | 306 | if (error) { | 
|  | 307 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | 
|  | 308 | return error; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | XFS_BTREE_STATS_INC(cur, free); | 
|  | 312 |  | 
|  | 313 | xfs_btree_setbuf(cur, level, NULL); | 
|  | 314 | cur->bc_nlevels--; | 
|  | 315 |  | 
|  | 316 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | 
|  | 317 | return 0; | 
|  | 318 | } | 
|  | 319 |  | 
| Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 320 | #ifdef DEBUG | 
|  | 321 | STATIC int | 
|  | 322 | xfs_allocbt_keys_inorder( | 
|  | 323 | struct xfs_btree_cur	*cur, | 
|  | 324 | union xfs_btree_key	*k1, | 
|  | 325 | union xfs_btree_key	*k2) | 
|  | 326 | { | 
|  | 327 | if (cur->bc_btnum == XFS_BTNUM_BNO) { | 
|  | 328 | return be32_to_cpu(k1->alloc.ar_startblock) < | 
|  | 329 | be32_to_cpu(k2->alloc.ar_startblock); | 
|  | 330 | } else { | 
|  | 331 | return be32_to_cpu(k1->alloc.ar_blockcount) < | 
|  | 332 | be32_to_cpu(k2->alloc.ar_blockcount) || | 
|  | 333 | (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount && | 
|  | 334 | be32_to_cpu(k1->alloc.ar_startblock) < | 
|  | 335 | be32_to_cpu(k2->alloc.ar_startblock)); | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | STATIC int | 
|  | 340 | xfs_allocbt_recs_inorder( | 
|  | 341 | struct xfs_btree_cur	*cur, | 
|  | 342 | union xfs_btree_rec	*r1, | 
|  | 343 | union xfs_btree_rec	*r2) | 
|  | 344 | { | 
|  | 345 | if (cur->bc_btnum == XFS_BTNUM_BNO) { | 
|  | 346 | return be32_to_cpu(r1->alloc.ar_startblock) + | 
|  | 347 | be32_to_cpu(r1->alloc.ar_blockcount) <= | 
|  | 348 | be32_to_cpu(r2->alloc.ar_startblock); | 
|  | 349 | } else { | 
|  | 350 | return be32_to_cpu(r1->alloc.ar_blockcount) < | 
|  | 351 | be32_to_cpu(r2->alloc.ar_blockcount) || | 
|  | 352 | (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount && | 
|  | 353 | be32_to_cpu(r1->alloc.ar_startblock) < | 
|  | 354 | be32_to_cpu(r2->alloc.ar_startblock)); | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 | #endif	/* DEBUG */ | 
|  | 358 |  | 
| Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 359 | #ifdef XFS_BTREE_TRACE | 
|  | 360 | ktrace_t	*xfs_allocbt_trace_buf; | 
|  | 361 |  | 
|  | 362 | STATIC void | 
|  | 363 | xfs_allocbt_trace_enter( | 
|  | 364 | struct xfs_btree_cur	*cur, | 
|  | 365 | const char		*func, | 
|  | 366 | char			*s, | 
|  | 367 | int			type, | 
|  | 368 | int			line, | 
|  | 369 | __psunsigned_t		a0, | 
|  | 370 | __psunsigned_t		a1, | 
|  | 371 | __psunsigned_t		a2, | 
|  | 372 | __psunsigned_t		a3, | 
|  | 373 | __psunsigned_t		a4, | 
|  | 374 | __psunsigned_t		a5, | 
|  | 375 | __psunsigned_t		a6, | 
|  | 376 | __psunsigned_t		a7, | 
|  | 377 | __psunsigned_t		a8, | 
|  | 378 | __psunsigned_t		a9, | 
|  | 379 | __psunsigned_t		a10) | 
|  | 380 | { | 
|  | 381 | ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type, | 
|  | 382 | (void *)func, (void *)s, NULL, (void *)cur, | 
|  | 383 | (void *)a0, (void *)a1, (void *)a2, (void *)a3, | 
|  | 384 | (void *)a4, (void *)a5, (void *)a6, (void *)a7, | 
|  | 385 | (void *)a8, (void *)a9, (void *)a10); | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | STATIC void | 
|  | 389 | xfs_allocbt_trace_cursor( | 
|  | 390 | struct xfs_btree_cur	*cur, | 
|  | 391 | __uint32_t		*s0, | 
|  | 392 | __uint64_t		*l0, | 
|  | 393 | __uint64_t		*l1) | 
|  | 394 | { | 
|  | 395 | *s0 = cur->bc_private.a.agno; | 
|  | 396 | *l0 = cur->bc_rec.a.ar_startblock; | 
|  | 397 | *l1 = cur->bc_rec.a.ar_blockcount; | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | STATIC void | 
|  | 401 | xfs_allocbt_trace_key( | 
|  | 402 | struct xfs_btree_cur	*cur, | 
|  | 403 | union xfs_btree_key	*key, | 
|  | 404 | __uint64_t		*l0, | 
|  | 405 | __uint64_t		*l1) | 
|  | 406 | { | 
|  | 407 | *l0 = be32_to_cpu(key->alloc.ar_startblock); | 
|  | 408 | *l1 = be32_to_cpu(key->alloc.ar_blockcount); | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | STATIC void | 
|  | 412 | xfs_allocbt_trace_record( | 
|  | 413 | struct xfs_btree_cur	*cur, | 
|  | 414 | union xfs_btree_rec	*rec, | 
|  | 415 | __uint64_t		*l0, | 
|  | 416 | __uint64_t		*l1, | 
|  | 417 | __uint64_t		*l2) | 
|  | 418 | { | 
|  | 419 | *l0 = be32_to_cpu(rec->alloc.ar_startblock); | 
|  | 420 | *l1 = be32_to_cpu(rec->alloc.ar_blockcount); | 
|  | 421 | *l2 = 0; | 
|  | 422 | } | 
|  | 423 | #endif /* XFS_BTREE_TRACE */ | 
|  | 424 |  | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 425 | static const struct xfs_btree_ops xfs_allocbt_ops = { | 
| Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 426 | .rec_len		= sizeof(xfs_alloc_rec_t), | 
|  | 427 | .key_len		= sizeof(xfs_alloc_key_t), | 
|  | 428 |  | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 429 | .dup_cursor		= xfs_allocbt_dup_cursor, | 
| Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 430 | .set_root		= xfs_allocbt_set_root, | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 431 | .kill_root		= xfs_allocbt_kill_root, | 
| Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 432 | .alloc_block		= xfs_allocbt_alloc_block, | 
| Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 433 | .free_block		= xfs_allocbt_free_block, | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 434 | .update_lastrec		= xfs_allocbt_update_lastrec, | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 435 | .get_minrecs		= xfs_allocbt_get_minrecs, | 
| Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 436 | .get_maxrecs		= xfs_allocbt_get_maxrecs, | 
| Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 437 | .init_key_from_rec	= xfs_allocbt_init_key_from_rec, | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 438 | .init_rec_from_key	= xfs_allocbt_init_rec_from_key, | 
|  | 439 | .init_rec_from_cur	= xfs_allocbt_init_rec_from_cur, | 
| Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 440 | .init_ptr_from_cur	= xfs_allocbt_init_ptr_from_cur, | 
|  | 441 | .key_diff		= xfs_allocbt_key_diff, | 
| Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 442 |  | 
| Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 443 | #ifdef DEBUG | 
|  | 444 | .keys_inorder		= xfs_allocbt_keys_inorder, | 
|  | 445 | .recs_inorder		= xfs_allocbt_recs_inorder, | 
|  | 446 | #endif | 
|  | 447 |  | 
| Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 448 | #ifdef XFS_BTREE_TRACE | 
|  | 449 | .trace_enter		= xfs_allocbt_trace_enter, | 
|  | 450 | .trace_cursor		= xfs_allocbt_trace_cursor, | 
|  | 451 | .trace_key		= xfs_allocbt_trace_key, | 
|  | 452 | .trace_record		= xfs_allocbt_trace_record, | 
|  | 453 | #endif | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 454 | }; | 
|  | 455 |  | 
|  | 456 | /* | 
|  | 457 | * Allocate a new allocation btree cursor. | 
|  | 458 | */ | 
|  | 459 | struct xfs_btree_cur *			/* new alloc btree cursor */ | 
|  | 460 | xfs_allocbt_init_cursor( | 
|  | 461 | struct xfs_mount	*mp,		/* file system mount point */ | 
|  | 462 | struct xfs_trans	*tp,		/* transaction pointer */ | 
|  | 463 | struct xfs_buf		*agbp,		/* buffer for agf structure */ | 
|  | 464 | xfs_agnumber_t		agno,		/* allocation group number */ | 
|  | 465 | xfs_btnum_t		btnum)		/* btree identifier */ | 
|  | 466 | { | 
|  | 467 | struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp); | 
|  | 468 | struct xfs_btree_cur	*cur; | 
|  | 469 |  | 
|  | 470 | ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT); | 
|  | 471 |  | 
|  | 472 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP); | 
|  | 473 |  | 
|  | 474 | cur->bc_tp = tp; | 
|  | 475 | cur->bc_mp = mp; | 
|  | 476 | cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]); | 
|  | 477 | cur->bc_btnum = btnum; | 
|  | 478 | cur->bc_blocklog = mp->m_sb.sb_blocklog; | 
|  | 479 |  | 
|  | 480 | cur->bc_ops = &xfs_allocbt_ops; | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 481 | if (btnum == XFS_BTNUM_CNT) | 
|  | 482 | cur->bc_flags = XFS_BTREE_LASTREC_UPDATE; | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 483 |  | 
|  | 484 | cur->bc_private.a.agbp = agbp; | 
|  | 485 | cur->bc_private.a.agno = agno; | 
|  | 486 |  | 
|  | 487 | return cur; | 
|  | 488 | } | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 489 |  | 
|  | 490 | /* | 
|  | 491 | * Calculate number of records in an alloc btree block. | 
|  | 492 | */ | 
|  | 493 | int | 
|  | 494 | xfs_allocbt_maxrecs( | 
|  | 495 | struct xfs_mount	*mp, | 
|  | 496 | int			blocklen, | 
|  | 497 | int			leaf) | 
|  | 498 | { | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 499 | blocklen -= XFS_ALLOC_BLOCK_LEN(mp); | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 500 |  | 
|  | 501 | if (leaf) | 
|  | 502 | return blocklen / sizeof(xfs_alloc_rec_t); | 
|  | 503 | return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t)); | 
|  | 504 | } |