blob: 89ef4e67ff546b395a88f62c2894ff6522f67843 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_dinode.h"
31#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
34#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_bmap.h"
38#include "xfs_error.h"
39#include "xfs_quota.h"
Dave Chinner3d3e6f62012-11-12 22:54:08 +110040#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050041#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * Determine the extent state.
45 */
46/* ARGSUSED */
47STATIC xfs_exntst_t
48xfs_extent_state(
49 xfs_filblks_t blks,
50 int extent_flag)
51{
52 if (extent_flag) {
53 ASSERT(blks != 0); /* saved for DMIG */
54 return XFS_EXT_UNWRITTEN;
55 }
56 return XFS_EXT_NORM;
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Convert on-disk form of btree root to in-memory form.
61 */
62void
63xfs_bmdr_to_bmbt(
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050064 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 xfs_bmdr_block_t *dblock,
66 int dblocklen,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110067 struct xfs_btree_block *rblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int rblocklen)
69{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050070 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int dmxr;
72 xfs_bmbt_key_t *fkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +100073 __be64 *fpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 xfs_bmbt_key_t *tkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +100075 __be64 *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050077 if (xfs_sb_version_hascrc(&mp->m_sb))
78 xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL,
79 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
80 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
81 else
82 xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL,
83 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
84 XFS_BTREE_LONG_PTRS);
85
Christoph Hellwig16259e72005-11-02 15:11:25 +110086 rblock->bb_level = dblock->bb_level;
87 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
88 rblock->bb_numrecs = dblock->bb_numrecs;
Christoph Hellwig60197e82008-10-30 17:11:19 +110089 dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +110090 fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
91 tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
92 fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
Christoph Hellwig60197e82008-10-30 17:11:19 +110093 tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
Christoph Hellwig16259e72005-11-02 15:11:25 +110094 dmxr = be16_to_cpu(dblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +100096 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * Convert a compressed bmap extent record to an uncompressed form.
101 * This code must be in sync with the routines xfs_bmbt_get_startoff,
102 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
103 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000104STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105__xfs_bmbt_get_all(
106 __uint64_t l0,
107 __uint64_t l1,
108 xfs_bmbt_irec_t *s)
109{
110 int ext_flag;
111 xfs_exntst_t st;
112
113 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
114 s->br_startoff = ((xfs_fileoff_t)l0 &
Eric Sandeenfb825572009-01-09 15:53:54 +1100115 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#if XFS_BIG_BLKNOS
Eric Sandeenfb825572009-01-09 15:53:54 +1100117 s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 (((xfs_fsblock_t)l1) >> 21);
119#else
120#ifdef DEBUG
121 {
122 xfs_dfsbno_t b;
123
Eric Sandeenfb825572009-01-09 15:53:54 +1100124 b = (((xfs_dfsbno_t)l0 & xfs_mask64lo(9)) << 43) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 (((xfs_dfsbno_t)l1) >> 21);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600126 ASSERT((b >> 32) == 0 || isnulldstartblock(b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 s->br_startblock = (xfs_fsblock_t)b;
128 }
129#else /* !DEBUG */
130 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
131#endif /* DEBUG */
132#endif /* XFS_BIG_BLKNOS */
Eric Sandeenfb825572009-01-09 15:53:54 +1100133 s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* This is xfs_extent_state() in-line */
135 if (ext_flag) {
136 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
137 st = XFS_EXT_UNWRITTEN;
138 } else
139 st = XFS_EXT_NORM;
140 s->br_state = st;
141}
142
143void
144xfs_bmbt_get_all(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000145 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 xfs_bmbt_irec_t *s)
147{
148 __xfs_bmbt_get_all(r->l0, r->l1, s);
149}
150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Extract the blockcount field from an in memory bmap extent record.
153 */
154xfs_filblks_t
155xfs_bmbt_get_blockcount(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000156 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Eric Sandeenfb825572009-01-09 15:53:54 +1100158 return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
162 * Extract the startblock field from an in memory bmap extent record.
163 */
164xfs_fsblock_t
165xfs_bmbt_get_startblock(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000166 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168#if XFS_BIG_BLKNOS
Eric Sandeenfb825572009-01-09 15:53:54 +1100169 return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 (((xfs_fsblock_t)r->l1) >> 21);
171#else
172#ifdef DEBUG
173 xfs_dfsbno_t b;
174
Eric Sandeenfb825572009-01-09 15:53:54 +1100175 b = (((xfs_dfsbno_t)r->l0 & xfs_mask64lo(9)) << 43) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 (((xfs_dfsbno_t)r->l1) >> 21);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600177 ASSERT((b >> 32) == 0 || isnulldstartblock(b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return (xfs_fsblock_t)b;
179#else /* !DEBUG */
180 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
181#endif /* DEBUG */
182#endif /* XFS_BIG_BLKNOS */
183}
184
185/*
186 * Extract the startoff field from an in memory bmap extent record.
187 */
188xfs_fileoff_t
189xfs_bmbt_get_startoff(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000190 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 return ((xfs_fileoff_t)r->l0 &
Eric Sandeenfb825572009-01-09 15:53:54 +1100193 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196xfs_exntst_t
197xfs_bmbt_get_state(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000198 xfs_bmbt_rec_host_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int ext_flag;
201
202 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
203 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
204 ext_flag);
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Extract the blockcount field from an on disk bmap extent record.
209 */
210xfs_filblks_t
211xfs_bmbt_disk_get_blockcount(
212 xfs_bmbt_rec_t *r)
213{
Eric Sandeenfb825572009-01-09 15:53:54 +1100214 return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Extract the startoff field from a disk format bmap extent record.
219 */
220xfs_fileoff_t
221xfs_bmbt_disk_get_startoff(
222 xfs_bmbt_rec_t *r)
223{
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000224 return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
Eric Sandeenfb825572009-01-09 15:53:54 +1100225 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000230 * Set all the fields in a bmap extent record from the arguments.
231 */
232void
233xfs_bmbt_set_allf(
234 xfs_bmbt_rec_host_t *r,
235 xfs_fileoff_t startoff,
236 xfs_fsblock_t startblock,
237 xfs_filblks_t blockcount,
238 xfs_exntst_t state)
239{
240 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
241
242 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
Eric Sandeenfb825572009-01-09 15:53:54 +1100243 ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0);
244 ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000245
246#if XFS_BIG_BLKNOS
Eric Sandeenfb825572009-01-09 15:53:54 +1100247 ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0);
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000248
249 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
250 ((xfs_bmbt_rec_base_t)startoff << 9) |
251 ((xfs_bmbt_rec_base_t)startblock >> 43);
252 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
253 ((xfs_bmbt_rec_base_t)blockcount &
Eric Sandeenfb825572009-01-09 15:53:54 +1100254 (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000255#else /* !XFS_BIG_BLKNOS */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600256 if (isnullstartblock(startblock)) {
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000257 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
258 ((xfs_bmbt_rec_base_t)startoff << 9) |
Eric Sandeenfb825572009-01-09 15:53:54 +1100259 (xfs_bmbt_rec_base_t)xfs_mask64lo(9);
260 r->l1 = xfs_mask64hi(11) |
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000261 ((xfs_bmbt_rec_base_t)startblock << 21) |
262 ((xfs_bmbt_rec_base_t)blockcount &
Eric Sandeenfb825572009-01-09 15:53:54 +1100263 (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000264 } else {
265 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
266 ((xfs_bmbt_rec_base_t)startoff << 9);
267 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
268 ((xfs_bmbt_rec_base_t)blockcount &
Eric Sandeenfb825572009-01-09 15:53:54 +1100269 (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000270 }
271#endif /* XFS_BIG_BLKNOS */
272}
273
274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Set all the fields in a bmap extent record from the uncompressed form.
276 */
277void
278xfs_bmbt_set_all(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000279 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 xfs_bmbt_irec_t *s)
281{
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000282 xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
283 s->br_blockcount, s->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000288 * Set all the fields in a disk format bmap extent record from the arguments.
289 */
290void
291xfs_bmbt_disk_set_allf(
292 xfs_bmbt_rec_t *r,
293 xfs_fileoff_t startoff,
294 xfs_fsblock_t startblock,
295 xfs_filblks_t blockcount,
296 xfs_exntst_t state)
297{
298 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
299
300 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
Eric Sandeenfb825572009-01-09 15:53:54 +1100301 ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0);
302 ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000303
304#if XFS_BIG_BLKNOS
Eric Sandeenfb825572009-01-09 15:53:54 +1100305 ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0);
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000306
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000307 r->l0 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000308 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
309 ((xfs_bmbt_rec_base_t)startoff << 9) |
310 ((xfs_bmbt_rec_base_t)startblock >> 43));
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000311 r->l1 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000312 ((xfs_bmbt_rec_base_t)startblock << 21) |
313 ((xfs_bmbt_rec_base_t)blockcount &
Eric Sandeenfb825572009-01-09 15:53:54 +1100314 (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000315#else /* !XFS_BIG_BLKNOS */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600316 if (isnullstartblock(startblock)) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000317 r->l0 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000318 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
319 ((xfs_bmbt_rec_base_t)startoff << 9) |
Eric Sandeenfb825572009-01-09 15:53:54 +1100320 (xfs_bmbt_rec_base_t)xfs_mask64lo(9));
321 r->l1 = cpu_to_be64(xfs_mask64hi(11) |
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000322 ((xfs_bmbt_rec_base_t)startblock << 21) |
323 ((xfs_bmbt_rec_base_t)blockcount &
Eric Sandeenfb825572009-01-09 15:53:54 +1100324 (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000325 } else {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000326 r->l0 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000327 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
328 ((xfs_bmbt_rec_base_t)startoff << 9));
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000329 r->l1 = cpu_to_be64(
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000330 ((xfs_bmbt_rec_base_t)startblock << 21) |
331 ((xfs_bmbt_rec_base_t)blockcount &
Eric Sandeenfb825572009-01-09 15:53:54 +1100332 (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000333 }
334#endif /* XFS_BIG_BLKNOS */
335}
336
337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * Set all the fields in a bmap extent record from the uncompressed form.
339 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000340STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341xfs_bmbt_disk_set_all(
342 xfs_bmbt_rec_t *r,
343 xfs_bmbt_irec_t *s)
344{
Christoph Hellwig8cba4342007-08-16 16:23:53 +1000345 xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
346 s->br_blockcount, s->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/*
350 * Set the blockcount field in a bmap extent record.
351 */
352void
353xfs_bmbt_set_blockcount(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000354 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 xfs_filblks_t v)
356{
Eric Sandeenfb825572009-01-09 15:53:54 +1100357 ASSERT((v & xfs_mask64hi(43)) == 0);
358 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64hi(43)) |
359 (xfs_bmbt_rec_base_t)(v & xfs_mask64lo(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/*
363 * Set the startblock field in a bmap extent record.
364 */
365void
366xfs_bmbt_set_startblock(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000367 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 xfs_fsblock_t v)
369{
370#if XFS_BIG_BLKNOS
Eric Sandeenfb825572009-01-09 15:53:54 +1100371 ASSERT((v & xfs_mask64hi(12)) == 0);
372 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 (xfs_bmbt_rec_base_t)(v >> 43);
Eric Sandeenfb825572009-01-09 15:53:54 +1100374 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 (xfs_bmbt_rec_base_t)(v << 21);
376#else /* !XFS_BIG_BLKNOS */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600377 if (isnullstartblock(v)) {
Eric Sandeenfb825572009-01-09 15:53:54 +1100378 r->l0 |= (xfs_bmbt_rec_base_t)xfs_mask64lo(9);
379 r->l1 = (xfs_bmbt_rec_base_t)xfs_mask64hi(11) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 ((xfs_bmbt_rec_base_t)v << 21) |
Eric Sandeenfb825572009-01-09 15:53:54 +1100381 (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 } else {
Eric Sandeenfb825572009-01-09 15:53:54 +1100383 r->l0 &= ~(xfs_bmbt_rec_base_t)xfs_mask64lo(9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
Eric Sandeenfb825572009-01-09 15:53:54 +1100385 (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387#endif /* XFS_BIG_BLKNOS */
388}
389
390/*
391 * Set the startoff field in a bmap extent record.
392 */
393void
394xfs_bmbt_set_startoff(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000395 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 xfs_fileoff_t v)
397{
Eric Sandeenfb825572009-01-09 15:53:54 +1100398 ASSERT((v & xfs_mask64hi(9)) == 0);
399 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ((xfs_bmbt_rec_base_t)v << 9) |
Eric Sandeenfb825572009-01-09 15:53:54 +1100401 (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * Set the extent state field in a bmap extent record.
406 */
407void
408xfs_bmbt_set_state(
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000409 xfs_bmbt_rec_host_t *r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 xfs_exntst_t v)
411{
412 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
413 if (v == XFS_EXT_NORM)
Eric Sandeenfb825572009-01-09 15:53:54 +1100414 r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 else
Eric Sandeenfb825572009-01-09 15:53:54 +1100416 r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*
420 * Convert in-memory form of btree root to on-disk form.
421 */
422void
423xfs_bmbt_to_bmdr(
Christoph Hellwig60197e82008-10-30 17:11:19 +1100424 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100425 struct xfs_btree_block *rblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int rblocklen,
427 xfs_bmdr_block_t *dblock,
428 int dblocklen)
429{
430 int dmxr;
431 xfs_bmbt_key_t *fkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +1000432 __be64 *fpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 xfs_bmbt_key_t *tkp;
Christoph Hellwig576039c2006-09-28 10:58:06 +1000434 __be64 *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500436 if (xfs_sb_version_hascrc(&mp->m_sb)) {
437 ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC));
438 ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid));
439 ASSERT(rblock->bb_u.l.bb_blkno ==
440 cpu_to_be64(XFS_BUF_DADDR_NULL));
441 } else
442 ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200443 ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO));
444 ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO));
445 ASSERT(rblock->bb_level != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100446 dblock->bb_level = rblock->bb_level;
447 dblock->bb_numrecs = rblock->bb_numrecs;
Christoph Hellwig60197e82008-10-30 17:11:19 +1100448 dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +1100449 fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
450 tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100451 fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
Christoph Hellwig136341b2008-10-30 17:11:40 +1100452 tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100453 dmxr = be16_to_cpu(dblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +1000455 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100459 * Check extent records, which have just been read, for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * any bit in the extent flag field. ASSERT on debug
461 * kernels, as this condition should not occur.
462 * Return an error condition (1) if any flags found,
463 * otherwise return 0.
464 */
465
466int
467xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100468 xfs_ifork_t *ifp,
469 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 xfs_extnum_t num)
471{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100472 for (; num > 0; num--, idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000473 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if ((ep->l0 >>
475 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
476 ASSERT(0);
477 return 1;
478 }
479 }
480 return 0;
481}
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100482
483
484STATIC struct xfs_btree_cur *
485xfs_bmbt_dup_cursor(
486 struct xfs_btree_cur *cur)
487{
488 struct xfs_btree_cur *new;
489
490 new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
491 cur->bc_private.b.ip, cur->bc_private.b.whichfork);
492
493 /*
494 * Copy the firstblock, flist, and flags values,
495 * since init cursor doesn't get them.
496 */
497 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
498 new->bc_private.b.flist = cur->bc_private.b.flist;
499 new->bc_private.b.flags = cur->bc_private.b.flags;
500
501 return new;
502}
503
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100504STATIC void
505xfs_bmbt_update_cursor(
506 struct xfs_btree_cur *src,
507 struct xfs_btree_cur *dst)
508{
509 ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
510 (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
511 ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist);
512
513 dst->bc_private.b.allocated += src->bc_private.b.allocated;
514 dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
515
516 src->bc_private.b.allocated = 0;
517}
518
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100519STATIC int
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100520xfs_bmbt_alloc_block(
521 struct xfs_btree_cur *cur,
522 union xfs_btree_ptr *start,
523 union xfs_btree_ptr *new,
524 int length,
525 int *stat)
526{
527 xfs_alloc_arg_t args; /* block allocation args */
528 int error; /* error return value */
529
530 memset(&args, 0, sizeof(args));
531 args.tp = cur->bc_tp;
532 args.mp = cur->bc_mp;
533 args.fsbno = cur->bc_private.b.firstblock;
534 args.firstblock = args.fsbno;
535
536 if (args.fsbno == NULLFSBLOCK) {
537 args.fsbno = be64_to_cpu(start->l);
538 args.type = XFS_ALLOCTYPE_START_BNO;
539 /*
540 * Make sure there is sufficient room left in the AG to
541 * complete a full tree split for an extent insert. If
542 * we are converting the middle part of an extent then
543 * we may need space for two tree splits.
544 *
545 * We are relying on the caller to make the correct block
546 * reservation for this operation to succeed. If the
547 * reservation amount is insufficient then we may fail a
548 * block allocation here and corrupt the filesystem.
549 */
550 args.minleft = xfs_trans_get_block_res(args.tp);
551 } else if (cur->bc_private.b.flist->xbf_low) {
552 args.type = XFS_ALLOCTYPE_START_BNO;
553 } else {
554 args.type = XFS_ALLOCTYPE_NEAR_BNO;
555 }
556
557 args.minlen = args.maxlen = args.prod = 1;
558 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
559 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
560 error = XFS_ERROR(ENOSPC);
561 goto error0;
562 }
563 error = xfs_alloc_vextent(&args);
564 if (error)
565 goto error0;
566
567 if (args.fsbno == NULLFSBLOCK && args.minleft) {
568 /*
569 * Could not find an AG with enough free space to satisfy
570 * a full btree split. Try again without minleft and if
571 * successful activate the lowspace algorithm.
572 */
573 args.fsbno = 0;
574 args.type = XFS_ALLOCTYPE_FIRST_AG;
575 args.minleft = 0;
576 error = xfs_alloc_vextent(&args);
577 if (error)
578 goto error0;
579 cur->bc_private.b.flist->xbf_low = 1;
580 }
581 if (args.fsbno == NULLFSBLOCK) {
582 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
583 *stat = 0;
584 return 0;
585 }
586 ASSERT(args.len == 1);
587 cur->bc_private.b.firstblock = args.fsbno;
588 cur->bc_private.b.allocated++;
589 cur->bc_private.b.ip->i_d.di_nblocks++;
590 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200591 xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100592 XFS_TRANS_DQ_BCOUNT, 1L);
593
594 new->l = cpu_to_be64(args.fsbno);
595
596 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
597 *stat = 1;
598 return 0;
599
600 error0:
601 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
602 return error;
603}
604
605STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100606xfs_bmbt_free_block(
607 struct xfs_btree_cur *cur,
608 struct xfs_buf *bp)
609{
610 struct xfs_mount *mp = cur->bc_mp;
611 struct xfs_inode *ip = cur->bc_private.b.ip;
612 struct xfs_trans *tp = cur->bc_tp;
613 xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
614
615 xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
616 ip->i_d.di_nblocks--;
617
618 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200619 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100620 xfs_trans_binval(tp, bp);
621 return 0;
622}
623
624STATIC int
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100625xfs_bmbt_get_minrecs(
626 struct xfs_btree_cur *cur,
627 int level)
628{
Christoph Hellwig60197e82008-10-30 17:11:19 +1100629 if (level == cur->bc_nlevels - 1) {
630 struct xfs_ifork *ifp;
631
632 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
633 cur->bc_private.b.whichfork);
634
635 return xfs_bmbt_maxrecs(cur->bc_mp,
636 ifp->if_broot_bytes, level == 0) / 2;
637 }
638
639 return cur->bc_mp->m_bmap_dmnr[level != 0];
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100640}
641
Christoph Hellwig60197e82008-10-30 17:11:19 +1100642int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100643xfs_bmbt_get_maxrecs(
644 struct xfs_btree_cur *cur,
645 int level)
646{
Christoph Hellwig60197e82008-10-30 17:11:19 +1100647 if (level == cur->bc_nlevels - 1) {
648 struct xfs_ifork *ifp;
649
650 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
651 cur->bc_private.b.whichfork);
652
653 return xfs_bmbt_maxrecs(cur->bc_mp,
654 ifp->if_broot_bytes, level == 0);
655 }
656
657 return cur->bc_mp->m_bmap_dmxr[level != 0];
658
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100659}
660
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100661/*
662 * Get the maximum records we could store in the on-disk format.
663 *
664 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
665 * for the root node this checks the available space in the dinode fork
666 * so that we can resize the in-memory buffer to match it. After a
667 * resize to the maximum size this function returns the same value
668 * as xfs_bmbt_get_maxrecs for the root node, too.
669 */
670STATIC int
671xfs_bmbt_get_dmaxrecs(
672 struct xfs_btree_cur *cur,
673 int level)
674{
Christoph Hellwig60197e82008-10-30 17:11:19 +1100675 if (level != cur->bc_nlevels - 1)
676 return cur->bc_mp->m_bmap_dmxr[level != 0];
677 return xfs_bmdr_maxrecs(cur->bc_mp, cur->bc_private.b.forksize,
678 level == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100679}
680
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100681STATIC void
682xfs_bmbt_init_key_from_rec(
683 union xfs_btree_key *key,
684 union xfs_btree_rec *rec)
685{
686 key->bmbt.br_startoff =
687 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
688}
689
690STATIC void
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100691xfs_bmbt_init_rec_from_key(
692 union xfs_btree_key *key,
693 union xfs_btree_rec *rec)
694{
695 ASSERT(key->bmbt.br_startoff != 0);
696
697 xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
698 0, 0, XFS_EXT_NORM);
699}
700
701STATIC void
702xfs_bmbt_init_rec_from_cur(
703 struct xfs_btree_cur *cur,
704 union xfs_btree_rec *rec)
705{
706 xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
707}
708
709STATIC void
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100710xfs_bmbt_init_ptr_from_cur(
711 struct xfs_btree_cur *cur,
712 union xfs_btree_ptr *ptr)
713{
714 ptr->l = 0;
715}
716
717STATIC __int64_t
718xfs_bmbt_key_diff(
719 struct xfs_btree_cur *cur,
720 union xfs_btree_key *key)
721{
722 return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
723 cur->bc_rec.b.br_startoff;
724}
725
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000726static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100727xfs_bmbt_verify(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100728 struct xfs_buf *bp)
729{
730 struct xfs_mount *mp = bp->b_target->bt_mount;
731 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
732 unsigned int level;
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100733
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500734 switch (block->bb_magic) {
735 case cpu_to_be32(XFS_BMAP_CRC_MAGIC):
736 if (!xfs_sb_version_hascrc(&mp->m_sb))
737 return false;
738 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid))
739 return false;
740 if (be64_to_cpu(block->bb_u.l.bb_blkno) != bp->b_bn)
741 return false;
742 /*
743 * XXX: need a better way of verifying the owner here. Right now
744 * just make sure there has been one set.
745 */
746 if (be64_to_cpu(block->bb_u.l.bb_owner) == 0)
747 return false;
748 /* fall through */
749 case cpu_to_be32(XFS_BMAP_MAGIC):
750 break;
751 default:
752 return false;
753 }
754
755 /*
756 * numrecs and level verification.
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100757 *
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500758 * We don't know what fork we belong to, so just verify that the level
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100759 * is less than the maximum of the two. Later checks will be more
760 * precise.
761 */
762 level = be16_to_cpu(block->bb_level);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500763 if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
764 return false;
765 if (be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
766 return false;
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100767
768 /* sibling pointer verification */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500769 if (!block->bb_u.l.bb_leftsib ||
770 (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLDFSBNO) &&
771 !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_leftsib))))
772 return false;
773 if (!block->bb_u.l.bb_rightsib ||
774 (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLDFSBNO) &&
775 !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_rightsib))))
776 return false;
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100777
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500778 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100779}
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100780
Dave Chinner1813dd62012-11-14 17:54:40 +1100781static void
782xfs_bmbt_read_verify(
783 struct xfs_buf *bp)
784{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500785 if (!(xfs_btree_lblock_verify_crc(bp) &&
786 xfs_bmbt_verify(bp))) {
787 trace_xfs_btree_corrupt(bp, _RET_IP_);
788 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
789 bp->b_target->bt_mount, bp->b_addr);
790 xfs_buf_ioerror(bp, EFSCORRUPTED);
791 }
Dave Chinner1813dd62012-11-14 17:54:40 +1100792}
793
794static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100795xfs_bmbt_write_verify(
796 struct xfs_buf *bp)
797{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500798 if (!xfs_bmbt_verify(bp)) {
799 xfs_warn(bp->b_target->bt_mount, "bmbt daddr 0x%llx failed", bp->b_bn);
800 trace_xfs_btree_corrupt(bp, _RET_IP_);
801 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
802 bp->b_target->bt_mount, bp->b_addr);
803 xfs_buf_ioerror(bp, EFSCORRUPTED);
804 return;
805 }
806 xfs_btree_lblock_calc_crc(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100807}
808
Dave Chinner1813dd62012-11-14 17:54:40 +1100809const struct xfs_buf_ops xfs_bmbt_buf_ops = {
810 .verify_read = xfs_bmbt_read_verify,
811 .verify_write = xfs_bmbt_write_verify,
812};
813
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100814
Dave Chinner742ae1e2013-04-30 21:39:34 +1000815#if defined(DEBUG) || defined(XFS_WARN)
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100816STATIC int
817xfs_bmbt_keys_inorder(
818 struct xfs_btree_cur *cur,
819 union xfs_btree_key *k1,
820 union xfs_btree_key *k2)
821{
822 return be64_to_cpu(k1->bmbt.br_startoff) <
823 be64_to_cpu(k2->bmbt.br_startoff);
824}
825
826STATIC int
827xfs_bmbt_recs_inorder(
828 struct xfs_btree_cur *cur,
829 union xfs_btree_rec *r1,
830 union xfs_btree_rec *r2)
831{
832 return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
833 xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
834 xfs_bmbt_disk_get_startoff(&r2->bmbt);
835}
836#endif /* DEBUG */
837
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100838static const struct xfs_btree_ops xfs_bmbt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100839 .rec_len = sizeof(xfs_bmbt_rec_t),
840 .key_len = sizeof(xfs_bmbt_key_t),
841
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100842 .dup_cursor = xfs_bmbt_dup_cursor,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100843 .update_cursor = xfs_bmbt_update_cursor,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100844 .alloc_block = xfs_bmbt_alloc_block,
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100845 .free_block = xfs_bmbt_free_block,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100846 .get_maxrecs = xfs_bmbt_get_maxrecs,
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100847 .get_minrecs = xfs_bmbt_get_minrecs,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100848 .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100849 .init_key_from_rec = xfs_bmbt_init_key_from_rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100850 .init_rec_from_key = xfs_bmbt_init_rec_from_key,
851 .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100852 .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
853 .key_diff = xfs_bmbt_key_diff,
Dave Chinner1813dd62012-11-14 17:54:40 +1100854 .buf_ops = &xfs_bmbt_buf_ops,
Dave Chinner742ae1e2013-04-30 21:39:34 +1000855#if defined(DEBUG) || defined(XFS_WARN)
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100856 .keys_inorder = xfs_bmbt_keys_inorder,
857 .recs_inorder = xfs_bmbt_recs_inorder,
858#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100859};
860
861/*
862 * Allocate a new bmap btree cursor.
863 */
864struct xfs_btree_cur * /* new bmap btree cursor */
865xfs_bmbt_init_cursor(
866 struct xfs_mount *mp, /* file system mount point */
867 struct xfs_trans *tp, /* transaction pointer */
868 struct xfs_inode *ip, /* inode owning the btree */
869 int whichfork) /* data or attr fork */
870{
871 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
872 struct xfs_btree_cur *cur;
873
874 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
875
876 cur->bc_tp = tp;
877 cur->bc_mp = mp;
878 cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
879 cur->bc_btnum = XFS_BTNUM_BMAP;
880 cur->bc_blocklog = mp->m_sb.sb_blocklog;
881
882 cur->bc_ops = &xfs_bmbt_ops;
Christoph Hellwige99ab902008-10-30 16:54:33 +1100883 cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500884 if (xfs_sb_version_hascrc(&mp->m_sb))
885 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100886
887 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
888 cur->bc_private.b.ip = ip;
889 cur->bc_private.b.firstblock = NULLFSBLOCK;
890 cur->bc_private.b.flist = NULL;
891 cur->bc_private.b.allocated = 0;
892 cur->bc_private.b.flags = 0;
893 cur->bc_private.b.whichfork = whichfork;
894
895 return cur;
896}
Christoph Hellwig60197e82008-10-30 17:11:19 +1100897
898/*
899 * Calculate number of records in a bmap btree block.
900 */
901int
902xfs_bmbt_maxrecs(
903 struct xfs_mount *mp,
904 int blocklen,
905 int leaf)
906{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100907 blocklen -= XFS_BMBT_BLOCK_LEN(mp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100908
909 if (leaf)
910 return blocklen / sizeof(xfs_bmbt_rec_t);
911 return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
912}
913
914/*
915 * Calculate number of records in a bmap btree inode root.
916 */
917int
918xfs_bmdr_maxrecs(
919 struct xfs_mount *mp,
920 int blocklen,
921 int leaf)
922{
923 blocklen -= sizeof(xfs_bmdr_block_t);
924
925 if (leaf)
926 return blocklen / sizeof(xfs_bmdr_rec_t);
927 return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
928}
Dave Chinner21b5c972013-08-30 10:23:44 +1000929
930/*
931 * Change the owner of a btree format fork fo the inode passed in. Change it to
932 * the owner of that is passed in so that we can change owners before or after
933 * we switch forks between inodes. The operation that the caller is doing will
934 * determine whether is needs to change owner before or after the switch.
935 *
Dave Chinner638f44162013-08-30 10:23:45 +1000936 * For demand paged transactional modification, the fork switch should be done
937 * after reading in all the blocks, modifying them and pinning them in the
938 * transaction. For modification when the buffers are already pinned in memory,
939 * the fork switch can be done before changing the owner as we won't need to
940 * validate the owner until the btree buffers are unpinned and writes can occur
941 * again.
942 *
943 * For recovery based ownership change, there is no transactional context and
944 * so a buffer list must be supplied so that we can record the buffers that we
945 * modified for the caller to issue IO on.
Dave Chinner21b5c972013-08-30 10:23:44 +1000946 */
947int
948xfs_bmbt_change_owner(
949 struct xfs_trans *tp,
950 struct xfs_inode *ip,
951 int whichfork,
Dave Chinner638f44162013-08-30 10:23:45 +1000952 xfs_ino_t new_owner,
953 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +1000954{
955 struct xfs_btree_cur *cur;
956 int error;
957
Dave Chinner638f44162013-08-30 10:23:45 +1000958 ASSERT(tp || buffer_list);
959 ASSERT(!(tp && buffer_list));
Dave Chinner21b5c972013-08-30 10:23:44 +1000960 if (whichfork == XFS_DATA_FORK)
Dan Carpenteraa9e1042013-09-12 00:17:31 +0300961 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE);
Dave Chinner21b5c972013-08-30 10:23:44 +1000962 else
Dan Carpenteraa9e1042013-09-12 00:17:31 +0300963 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE);
Dave Chinner21b5c972013-08-30 10:23:44 +1000964
965 cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
Dave Chinner638f44162013-08-30 10:23:45 +1000966 if (!cur)
967 return ENOMEM;
968
969 error = xfs_btree_change_owner(cur, new_owner, buffer_list);
Dave Chinner21b5c972013-08-30 10:23:44 +1000970 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
971 return error;
972}