Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
6ca1c906 | 20 | #include "xfs_format.h" |
239880ef DC |
21 | #include "xfs_log_format.h" |
22 | #include "xfs_trans_resv.h" | |
a844f451 | 23 | #include "xfs_bit.h" |
1da177e4 LT |
24 | #include "xfs_sb.h" |
25 | #include "xfs_ag.h" | |
1da177e4 | 26 | #include "xfs_mount.h" |
1da177e4 | 27 | #include "xfs_bmap_btree.h" |
a844f451 | 28 | #include "xfs_alloc_btree.h" |
1da177e4 | 29 | #include "xfs_ialloc_btree.h" |
1da177e4 | 30 | #include "xfs_dinode.h" |
1da177e4 | 31 | #include "xfs_inode.h" |
239880ef | 32 | #include "xfs_trans.h" |
a844f451 | 33 | #include "xfs_inode_item.h" |
1da177e4 | 34 | #include "xfs_alloc.h" |
a844f451 | 35 | #include "xfs_btree.h" |
a844f451 | 36 | #include "xfs_itable.h" |
1da177e4 LT |
37 | #include "xfs_bmap.h" |
38 | #include "xfs_error.h" | |
39 | #include "xfs_quota.h" | |
3d3e6f64 | 40 | #include "xfs_trace.h" |
ee1a47ab | 41 | #include "xfs_cksum.h" |
1da177e4 | 42 | |
1da177e4 LT |
43 | /* |
44 | * Determine the extent state. | |
45 | */ | |
46 | /* ARGSUSED */ | |
47 | STATIC xfs_exntst_t | |
48 | xfs_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 | ||
1da177e4 LT |
59 | /* |
60 | * Convert on-disk form of btree root to in-memory form. | |
61 | */ | |
62 | void | |
63 | xfs_bmdr_to_bmbt( | |
ee1a47ab | 64 | struct xfs_inode *ip, |
1da177e4 LT |
65 | xfs_bmdr_block_t *dblock, |
66 | int dblocklen, | |
7cc95a82 | 67 | struct xfs_btree_block *rblock, |
1da177e4 LT |
68 | int rblocklen) |
69 | { | |
ee1a47ab | 70 | struct xfs_mount *mp = ip->i_mount; |
1da177e4 LT |
71 | int dmxr; |
72 | xfs_bmbt_key_t *fkp; | |
576039cf | 73 | __be64 *fpp; |
1da177e4 | 74 | xfs_bmbt_key_t *tkp; |
576039cf | 75 | __be64 *tpp; |
1da177e4 | 76 | |
ee1a47ab CH |
77 | 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 | ||
16259e7d CH |
86 | rblock->bb_level = dblock->bb_level; |
87 | ASSERT(be16_to_cpu(rblock->bb_level) > 0); | |
88 | rblock->bb_numrecs = dblock->bb_numrecs; | |
60197e8d | 89 | dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0); |
136341b4 CH |
90 | 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); | |
60197e8d | 93 | tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
16259e7d | 94 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
1da177e4 | 95 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
576039cf | 96 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
1da177e4 LT |
97 | } |
98 | ||
1da177e4 LT |
99 | /* |
100 | * 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 | */ | |
b8f82a4a | 104 | STATIC void |
1da177e4 LT |
105 | __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 & | |
fb82557f | 115 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
1da177e4 | 116 | #if XFS_BIG_BLKNOS |
fb82557f | 117 | s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) | |
1da177e4 LT |
118 | (((xfs_fsblock_t)l1) >> 21); |
119 | #else | |
120 | #ifdef DEBUG | |
121 | { | |
122 | xfs_dfsbno_t b; | |
123 | ||
fb82557f | 124 | b = (((xfs_dfsbno_t)l0 & xfs_mask64lo(9)) << 43) | |
1da177e4 | 125 | (((xfs_dfsbno_t)l1) >> 21); |
9d87c319 | 126 | ASSERT((b >> 32) == 0 || isnulldstartblock(b)); |
1da177e4 LT |
127 | 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 */ | |
fb82557f | 133 | s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21)); |
1da177e4 LT |
134 | /* 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 | ||
143 | void | |
144 | xfs_bmbt_get_all( | |
a6f64d4a | 145 | xfs_bmbt_rec_host_t *r, |
1da177e4 LT |
146 | xfs_bmbt_irec_t *s) |
147 | { | |
148 | __xfs_bmbt_get_all(r->l0, r->l1, s); | |
149 | } | |
150 | ||
1da177e4 LT |
151 | /* |
152 | * Extract the blockcount field from an in memory bmap extent record. | |
153 | */ | |
154 | xfs_filblks_t | |
155 | xfs_bmbt_get_blockcount( | |
a6f64d4a | 156 | xfs_bmbt_rec_host_t *r) |
1da177e4 | 157 | { |
fb82557f | 158 | return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21)); |
1da177e4 LT |
159 | } |
160 | ||
161 | /* | |
162 | * Extract the startblock field from an in memory bmap extent record. | |
163 | */ | |
164 | xfs_fsblock_t | |
165 | xfs_bmbt_get_startblock( | |
a6f64d4a | 166 | xfs_bmbt_rec_host_t *r) |
1da177e4 LT |
167 | { |
168 | #if XFS_BIG_BLKNOS | |
fb82557f | 169 | return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) | |
1da177e4 LT |
170 | (((xfs_fsblock_t)r->l1) >> 21); |
171 | #else | |
172 | #ifdef DEBUG | |
173 | xfs_dfsbno_t b; | |
174 | ||
fb82557f | 175 | b = (((xfs_dfsbno_t)r->l0 & xfs_mask64lo(9)) << 43) | |
1da177e4 | 176 | (((xfs_dfsbno_t)r->l1) >> 21); |
9d87c319 | 177 | ASSERT((b >> 32) == 0 || isnulldstartblock(b)); |
1da177e4 LT |
178 | 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 | */ | |
188 | xfs_fileoff_t | |
189 | xfs_bmbt_get_startoff( | |
a6f64d4a | 190 | xfs_bmbt_rec_host_t *r) |
1da177e4 LT |
191 | { |
192 | return ((xfs_fileoff_t)r->l0 & | |
fb82557f | 193 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
1da177e4 LT |
194 | } |
195 | ||
196 | xfs_exntst_t | |
197 | xfs_bmbt_get_state( | |
a6f64d4a | 198 | xfs_bmbt_rec_host_t *r) |
1da177e4 LT |
199 | { |
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 | ||
1da177e4 LT |
207 | /* |
208 | * Extract the blockcount field from an on disk bmap extent record. | |
209 | */ | |
210 | xfs_filblks_t | |
211 | xfs_bmbt_disk_get_blockcount( | |
212 | xfs_bmbt_rec_t *r) | |
213 | { | |
fb82557f | 214 | return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21)); |
1da177e4 LT |
215 | } |
216 | ||
1da177e4 LT |
217 | /* |
218 | * Extract the startoff field from a disk format bmap extent record. | |
219 | */ | |
220 | xfs_fileoff_t | |
221 | xfs_bmbt_disk_get_startoff( | |
222 | xfs_bmbt_rec_t *r) | |
223 | { | |
cd8b0a97 | 224 | return ((xfs_fileoff_t)be64_to_cpu(r->l0) & |
fb82557f | 225 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
1da177e4 | 226 | } |
1da177e4 | 227 | |
1da177e4 | 228 | |
1da177e4 LT |
229 | /* |
230 | * Set all the fields in a bmap extent record from the arguments. | |
231 | */ | |
232 | void | |
233 | xfs_bmbt_set_allf( | |
8cba4344 CH |
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) | |
1da177e4 | 239 | { |
8cba4344 CH |
240 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
241 | ||
242 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); | |
fb82557f ES |
243 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
244 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); | |
1da177e4 | 245 | |
1da177e4 | 246 | #if XFS_BIG_BLKNOS |
fb82557f | 247 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
8cba4344 | 248 | |
1da177e4 | 249 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
8cba4344 CH |
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 & | |
fb82557f | 254 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
1da177e4 | 255 | #else /* !XFS_BIG_BLKNOS */ |
9d87c319 | 256 | if (isnullstartblock(startblock)) { |
1da177e4 | 257 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
8cba4344 | 258 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
fb82557f ES |
259 | (xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
260 | r->l1 = xfs_mask64hi(11) | | |
8cba4344 CH |
261 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
262 | ((xfs_bmbt_rec_base_t)blockcount & | |
fb82557f | 263 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
1da177e4 LT |
264 | } else { |
265 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | | |
8cba4344 CH |
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 & | |
fb82557f | 269 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
1da177e4 LT |
270 | } |
271 | #endif /* XFS_BIG_BLKNOS */ | |
272 | } | |
273 | ||
1da177e4 LT |
274 | /* |
275 | * Set all the fields in a bmap extent record from the uncompressed form. | |
276 | */ | |
277 | void | |
8cba4344 CH |
278 | xfs_bmbt_set_all( |
279 | xfs_bmbt_rec_host_t *r, | |
280 | xfs_bmbt_irec_t *s) | |
1da177e4 | 281 | { |
8cba4344 CH |
282 | xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock, |
283 | s->br_blockcount, s->br_state); | |
1da177e4 LT |
284 | } |
285 | ||
8cba4344 | 286 | |
1da177e4 LT |
287 | /* |
288 | * Set all the fields in a disk format bmap extent record from the arguments. | |
289 | */ | |
290 | void | |
291 | xfs_bmbt_disk_set_allf( | |
8cba4344 CH |
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) | |
1da177e4 | 297 | { |
8cba4344 CH |
298 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
299 | ||
300 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); | |
fb82557f ES |
301 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
302 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); | |
1da177e4 | 303 | |
1da177e4 | 304 | #if XFS_BIG_BLKNOS |
fb82557f | 305 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
8cba4344 | 306 | |
cd8b0a97 | 307 | r->l0 = cpu_to_be64( |
8cba4344 CH |
308 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
309 | ((xfs_bmbt_rec_base_t)startoff << 9) | | |
310 | ((xfs_bmbt_rec_base_t)startblock >> 43)); | |
cd8b0a97 | 311 | r->l1 = cpu_to_be64( |
8cba4344 CH |
312 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
313 | ((xfs_bmbt_rec_base_t)blockcount & | |
fb82557f | 314 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
1da177e4 | 315 | #else /* !XFS_BIG_BLKNOS */ |
9d87c319 | 316 | if (isnullstartblock(startblock)) { |
cd8b0a97 | 317 | r->l0 = cpu_to_be64( |
8cba4344 CH |
318 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
319 | ((xfs_bmbt_rec_base_t)startoff << 9) | | |
fb82557f ES |
320 | (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
321 | r->l1 = cpu_to_be64(xfs_mask64hi(11) | | |
8cba4344 CH |
322 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
323 | ((xfs_bmbt_rec_base_t)blockcount & | |
fb82557f | 324 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
1da177e4 | 325 | } else { |
cd8b0a97 | 326 | r->l0 = cpu_to_be64( |
8cba4344 CH |
327 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
328 | ((xfs_bmbt_rec_base_t)startoff << 9)); | |
cd8b0a97 | 329 | r->l1 = cpu_to_be64( |
8cba4344 CH |
330 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
331 | ((xfs_bmbt_rec_base_t)blockcount & | |
fb82557f | 332 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
1da177e4 LT |
333 | } |
334 | #endif /* XFS_BIG_BLKNOS */ | |
335 | } | |
8cba4344 CH |
336 | |
337 | /* | |
338 | * Set all the fields in a bmap extent record from the uncompressed form. | |
339 | */ | |
5d77c0dc | 340 | STATIC void |
8cba4344 CH |
341 | xfs_bmbt_disk_set_all( |
342 | xfs_bmbt_rec_t *r, | |
343 | xfs_bmbt_irec_t *s) | |
344 | { | |
345 | xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock, | |
346 | s->br_blockcount, s->br_state); | |
347 | } | |
1da177e4 LT |
348 | |
349 | /* | |
350 | * Set the blockcount field in a bmap extent record. | |
351 | */ | |
352 | void | |
353 | xfs_bmbt_set_blockcount( | |
a6f64d4a | 354 | xfs_bmbt_rec_host_t *r, |
1da177e4 LT |
355 | xfs_filblks_t v) |
356 | { | |
fb82557f ES |
357 | 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)); | |
1da177e4 LT |
360 | } |
361 | ||
362 | /* | |
363 | * Set the startblock field in a bmap extent record. | |
364 | */ | |
365 | void | |
366 | xfs_bmbt_set_startblock( | |
a6f64d4a | 367 | xfs_bmbt_rec_host_t *r, |
1da177e4 LT |
368 | xfs_fsblock_t v) |
369 | { | |
370 | #if XFS_BIG_BLKNOS | |
fb82557f ES |
371 | ASSERT((v & xfs_mask64hi(12)) == 0); |
372 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) | | |
1da177e4 | 373 | (xfs_bmbt_rec_base_t)(v >> 43); |
fb82557f | 374 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) | |
1da177e4 LT |
375 | (xfs_bmbt_rec_base_t)(v << 21); |
376 | #else /* !XFS_BIG_BLKNOS */ | |
9d87c319 | 377 | if (isnullstartblock(v)) { |
fb82557f ES |
378 | r->l0 |= (xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
379 | r->l1 = (xfs_bmbt_rec_base_t)xfs_mask64hi(11) | | |
1da177e4 | 380 | ((xfs_bmbt_rec_base_t)v << 21) | |
fb82557f | 381 | (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
1da177e4 | 382 | } else { |
fb82557f | 383 | r->l0 &= ~(xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
1da177e4 | 384 | r->l1 = ((xfs_bmbt_rec_base_t)v << 21) | |
fb82557f | 385 | (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
1da177e4 LT |
386 | } |
387 | #endif /* XFS_BIG_BLKNOS */ | |
388 | } | |
389 | ||
390 | /* | |
391 | * Set the startoff field in a bmap extent record. | |
392 | */ | |
393 | void | |
394 | xfs_bmbt_set_startoff( | |
a6f64d4a | 395 | xfs_bmbt_rec_host_t *r, |
1da177e4 LT |
396 | xfs_fileoff_t v) |
397 | { | |
fb82557f ES |
398 | ASSERT((v & xfs_mask64hi(9)) == 0); |
399 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) | | |
1da177e4 | 400 | ((xfs_bmbt_rec_base_t)v << 9) | |
fb82557f | 401 | (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
1da177e4 LT |
402 | } |
403 | ||
404 | /* | |
405 | * Set the extent state field in a bmap extent record. | |
406 | */ | |
407 | void | |
408 | xfs_bmbt_set_state( | |
a6f64d4a | 409 | xfs_bmbt_rec_host_t *r, |
1da177e4 LT |
410 | xfs_exntst_t v) |
411 | { | |
412 | ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN); | |
413 | if (v == XFS_EXT_NORM) | |
fb82557f | 414 | r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN); |
1da177e4 | 415 | else |
fb82557f | 416 | r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN); |
1da177e4 LT |
417 | } |
418 | ||
419 | /* | |
420 | * Convert in-memory form of btree root to on-disk form. | |
421 | */ | |
422 | void | |
423 | xfs_bmbt_to_bmdr( | |
60197e8d | 424 | struct xfs_mount *mp, |
7cc95a82 | 425 | struct xfs_btree_block *rblock, |
1da177e4 LT |
426 | int rblocklen, |
427 | xfs_bmdr_block_t *dblock, | |
428 | int dblocklen) | |
429 | { | |
430 | int dmxr; | |
431 | xfs_bmbt_key_t *fkp; | |
576039cf | 432 | __be64 *fpp; |
1da177e4 | 433 | xfs_bmbt_key_t *tkp; |
576039cf | 434 | __be64 *tpp; |
1da177e4 | 435 | |
ee1a47ab CH |
436 | 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)); | |
69ef921b CH |
443 | 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); | |
16259e7d CH |
446 | dblock->bb_level = rblock->bb_level; |
447 | dblock->bb_numrecs = rblock->bb_numrecs; | |
60197e8d | 448 | dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0); |
136341b4 CH |
449 | fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
450 | tkp = XFS_BMDR_KEY_ADDR(dblock, 1); | |
60197e8d | 451 | fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
136341b4 | 452 | tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); |
16259e7d | 453 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
1da177e4 | 454 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
576039cf | 455 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
1da177e4 LT |
456 | } |
457 | ||
1da177e4 | 458 | /* |
4eea22f0 | 459 | * Check extent records, which have just been read, for |
1da177e4 LT |
460 | * 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 | ||
466 | int | |
467 | xfs_check_nostate_extents( | |
4eea22f0 MK |
468 | xfs_ifork_t *ifp, |
469 | xfs_extnum_t idx, | |
1da177e4 LT |
470 | xfs_extnum_t num) |
471 | { | |
4eea22f0 | 472 | for (; num > 0; num--, idx++) { |
a6f64d4a | 473 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
1da177e4 LT |
474 | if ((ep->l0 >> |
475 | (64 - BMBT_EXNTFLAG_BITLEN)) != 0) { | |
476 | ASSERT(0); | |
477 | return 1; | |
478 | } | |
479 | } | |
480 | return 0; | |
481 | } | |
561f7d17 CH |
482 | |
483 | ||
484 | STATIC struct xfs_btree_cur * | |
485 | xfs_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 | ||
4b22a571 CH |
504 | STATIC void |
505 | xfs_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 | ||
f5eb8e7c CH |
519 | STATIC int |
520 | xfs_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); | |
7d095257 | 591 | xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip, |
f5eb8e7c CH |
592 | 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 | ||
d4b3a4b7 CH |
605 | STATIC int |
606 | xfs_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); | |
7d095257 | 619 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
d4b3a4b7 CH |
620 | xfs_trans_binval(tp, bp); |
621 | return 0; | |
622 | } | |
623 | ||
91cca5df CH |
624 | STATIC int |
625 | xfs_bmbt_get_minrecs( | |
626 | struct xfs_btree_cur *cur, | |
627 | int level) | |
628 | { | |
60197e8d CH |
629 | 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]; | |
91cca5df CH |
640 | } |
641 | ||
60197e8d | 642 | int |
ce5e42db CH |
643 | xfs_bmbt_get_maxrecs( |
644 | struct xfs_btree_cur *cur, | |
645 | int level) | |
646 | { | |
60197e8d CH |
647 | 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 | ||
ce5e42db CH |
659 | } |
660 | ||
4b22a571 CH |
661 | /* |
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 | */ | |
670 | STATIC int | |
671 | xfs_bmbt_get_dmaxrecs( | |
672 | struct xfs_btree_cur *cur, | |
673 | int level) | |
674 | { | |
60197e8d CH |
675 | 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); | |
4b22a571 CH |
679 | } |
680 | ||
fe033cc8 CH |
681 | STATIC void |
682 | xfs_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 | ||
4b22a571 CH |
690 | STATIC void |
691 | xfs_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 | ||
701 | STATIC void | |
702 | xfs_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 | ||
fe033cc8 CH |
709 | STATIC void |
710 | xfs_bmbt_init_ptr_from_cur( | |
711 | struct xfs_btree_cur *cur, | |
712 | union xfs_btree_ptr *ptr) | |
713 | { | |
714 | ptr->l = 0; | |
715 | } | |
716 | ||
717 | STATIC __int64_t | |
718 | xfs_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 | ||
c5c249b4 | 726 | static bool |
612cfbfe | 727 | xfs_bmbt_verify( |
3d3e6f64 DC |
728 | 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; | |
3d3e6f64 | 733 | |
ee1a47ab CH |
734 | 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. | |
3d3e6f64 | 757 | * |
ee1a47ab | 758 | * We don't know what fork we belong to, so just verify that the level |
3d3e6f64 DC |
759 | * is less than the maximum of the two. Later checks will be more |
760 | * precise. | |
761 | */ | |
762 | level = be16_to_cpu(block->bb_level); | |
ee1a47ab CH |
763 | 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; | |
3d3e6f64 DC |
767 | |
768 | /* sibling pointer verification */ | |
ee1a47ab CH |
769 | 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; | |
777 | ||
778 | return true; | |
612cfbfe | 779 | } |
3d3e6f64 | 780 | |
1813dd64 DC |
781 | static void |
782 | xfs_bmbt_read_verify( | |
612cfbfe DC |
783 | struct xfs_buf *bp) |
784 | { | |
ee1a47ab CH |
785 | 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 | } | |
612cfbfe DC |
792 | } |
793 | ||
1813dd64 DC |
794 | static void |
795 | xfs_bmbt_write_verify( | |
612cfbfe DC |
796 | struct xfs_buf *bp) |
797 | { | |
ee1a47ab CH |
798 | 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); | |
3d3e6f64 DC |
807 | } |
808 | ||
1813dd64 DC |
809 | const struct xfs_buf_ops xfs_bmbt_buf_ops = { |
810 | .verify_read = xfs_bmbt_read_verify, | |
811 | .verify_write = xfs_bmbt_write_verify, | |
812 | }; | |
813 | ||
814 | ||
742ae1e3 | 815 | #if defined(DEBUG) || defined(XFS_WARN) |
4a26e66e CH |
816 | STATIC int |
817 | xfs_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 | ||
826 | STATIC int | |
827 | xfs_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 | ||
561f7d17 | 838 | static const struct xfs_btree_ops xfs_bmbt_ops = { |
65f1eaea CH |
839 | .rec_len = sizeof(xfs_bmbt_rec_t), |
840 | .key_len = sizeof(xfs_bmbt_key_t), | |
841 | ||
561f7d17 | 842 | .dup_cursor = xfs_bmbt_dup_cursor, |
4b22a571 | 843 | .update_cursor = xfs_bmbt_update_cursor, |
f5eb8e7c | 844 | .alloc_block = xfs_bmbt_alloc_block, |
d4b3a4b7 | 845 | .free_block = xfs_bmbt_free_block, |
ce5e42db | 846 | .get_maxrecs = xfs_bmbt_get_maxrecs, |
91cca5df | 847 | .get_minrecs = xfs_bmbt_get_minrecs, |
4b22a571 | 848 | .get_dmaxrecs = xfs_bmbt_get_dmaxrecs, |
fe033cc8 | 849 | .init_key_from_rec = xfs_bmbt_init_key_from_rec, |
4b22a571 CH |
850 | .init_rec_from_key = xfs_bmbt_init_rec_from_key, |
851 | .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, | |
fe033cc8 CH |
852 | .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur, |
853 | .key_diff = xfs_bmbt_key_diff, | |
1813dd64 | 854 | .buf_ops = &xfs_bmbt_buf_ops, |
742ae1e3 | 855 | #if defined(DEBUG) || defined(XFS_WARN) |
4a26e66e CH |
856 | .keys_inorder = xfs_bmbt_keys_inorder, |
857 | .recs_inorder = xfs_bmbt_recs_inorder, | |
858 | #endif | |
561f7d17 CH |
859 | }; |
860 | ||
861 | /* | |
862 | * Allocate a new bmap btree cursor. | |
863 | */ | |
864 | struct xfs_btree_cur * /* new bmap btree cursor */ | |
865 | xfs_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; | |
e99ab90d | 883 | cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE; |
ee1a47ab CH |
884 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
885 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; | |
561f7d17 CH |
886 | |
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 | } | |
60197e8d CH |
897 | |
898 | /* | |
899 | * Calculate number of records in a bmap btree block. | |
900 | */ | |
901 | int | |
902 | xfs_bmbt_maxrecs( | |
903 | struct xfs_mount *mp, | |
904 | int blocklen, | |
905 | int leaf) | |
906 | { | |
7cc95a82 | 907 | blocklen -= XFS_BMBT_BLOCK_LEN(mp); |
60197e8d CH |
908 | |
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 | */ | |
917 | int | |
918 | xfs_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 | } | |
21b5c978 DC |
929 | |
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 | * | |
638f4416 DC |
936 | * 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. | |
21b5c978 DC |
946 | */ |
947 | int | |
948 | xfs_bmbt_change_owner( | |
949 | struct xfs_trans *tp, | |
950 | struct xfs_inode *ip, | |
951 | int whichfork, | |
638f4416 DC |
952 | xfs_ino_t new_owner, |
953 | struct list_head *buffer_list) | |
21b5c978 DC |
954 | { |
955 | struct xfs_btree_cur *cur; | |
956 | int error; | |
957 | ||
638f4416 DC |
958 | ASSERT(tp || buffer_list); |
959 | ASSERT(!(tp && buffer_list)); | |
21b5c978 | 960 | if (whichfork == XFS_DATA_FORK) |
aa9e1040 | 961 | ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE); |
21b5c978 | 962 | else |
aa9e1040 | 963 | ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE); |
21b5c978 DC |
964 | |
965 | cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork); | |
638f4416 DC |
966 | if (!cur) |
967 | return ENOMEM; | |
968 | ||
969 | error = xfs_btree_change_owner(cur, new_owner, buffer_list); | |
21b5c978 DC |
970 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
971 | return error; | |
972 | } |