]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
5000d01d | 4 | * |
da23017d 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 | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
5000d01d | 8 | * |
da23017d 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. | |
5000d01d | 13 | * |
da23017d 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 | |
2bd0ea18 | 17 | */ |
9c799827 | 18 | #include "libxfs_priv.h" |
b626fb59 DC |
19 | #include "xfs_fs.h" |
20 | #include "xfs_shared.h" | |
21 | #include "xfs_format.h" | |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
24 | #include "xfs_bit.h" | |
25 | #include "xfs_mount.h" | |
26 | #include "xfs_inode.h" | |
27 | #include "xfs_trans.h" | |
28 | #include "xfs_alloc.h" | |
29 | #include "xfs_btree.h" | |
30 | #include "xfs_bmap_btree.h" | |
31 | #include "xfs_bmap.h" | |
32 | #include "xfs_trace.h" | |
33 | #include "xfs_cksum.h" | |
2bd0ea18 | 34 | |
2bd0ea18 NS |
35 | /* |
36 | * Determine the extent state. | |
37 | */ | |
38 | /* ARGSUSED */ | |
39 | STATIC xfs_exntst_t | |
40 | xfs_extent_state( | |
41 | xfs_filblks_t blks, | |
42 | int extent_flag) | |
43 | { | |
44 | if (extent_flag) { | |
45 | ASSERT(blks != 0); /* saved for DMIG */ | |
46 | return XFS_EXT_UNWRITTEN; | |
47 | } | |
48 | return XFS_EXT_NORM; | |
49 | } | |
50 | ||
2bd0ea18 NS |
51 | /* |
52 | * Convert on-disk form of btree root to in-memory form. | |
53 | */ | |
54 | void | |
55 | xfs_bmdr_to_bmbt( | |
5dfa5cd2 | 56 | struct xfs_inode *ip, |
2bd0ea18 NS |
57 | xfs_bmdr_block_t *dblock, |
58 | int dblocklen, | |
b3563c19 | 59 | struct xfs_btree_block *rblock, |
2bd0ea18 NS |
60 | int rblocklen) |
61 | { | |
5dfa5cd2 | 62 | struct xfs_mount *mp = ip->i_mount; |
2bd0ea18 NS |
63 | int dmxr; |
64 | xfs_bmbt_key_t *fkp; | |
5e656dbb | 65 | __be64 *fpp; |
2bd0ea18 | 66 | xfs_bmbt_key_t *tkp; |
5e656dbb | 67 | __be64 *tpp; |
2bd0ea18 | 68 | |
5dfa5cd2 DC |
69 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
70 | xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL, | |
71 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, | |
72 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); | |
73 | else | |
74 | xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL, | |
75 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, | |
76 | XFS_BTREE_LONG_PTRS); | |
77 | ||
6e3140c7 NS |
78 | rblock->bb_level = dblock->bb_level; |
79 | ASSERT(be16_to_cpu(rblock->bb_level) > 0); | |
80 | rblock->bb_numrecs = dblock->bb_numrecs; | |
ff105f75 | 81 | dmxr = xfs_bmdr_maxrecs(dblocklen, 0); |
b3563c19 BN |
82 | fkp = XFS_BMDR_KEY_ADDR(dblock, 1); |
83 | tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); | |
84 | fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); | |
85 | tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); | |
6e3140c7 | 86 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
32181a02 | 87 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
5e656dbb | 88 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
2bd0ea18 NS |
89 | } |
90 | ||
2bd0ea18 NS |
91 | /* |
92 | * Convert a compressed bmap extent record to an uncompressed form. | |
93 | * This code must be in sync with the routines xfs_bmbt_get_startoff, | |
94 | * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state. | |
95 | */ | |
56b2de80 | 96 | STATIC void |
f9e56f43 NS |
97 | __xfs_bmbt_get_all( |
98 | __uint64_t l0, | |
99 | __uint64_t l1, | |
100 | xfs_bmbt_irec_t *s) | |
2bd0ea18 NS |
101 | { |
102 | int ext_flag; | |
103 | xfs_exntst_t st; | |
104 | ||
4542f191 NS |
105 | ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN)); |
106 | s->br_startoff = ((xfs_fileoff_t)l0 & | |
56b2de80 | 107 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
56b2de80 | 108 | s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) | |
4542f191 | 109 | (((xfs_fsblock_t)l1) >> 21); |
56b2de80 | 110 | s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21)); |
2bd0ea18 NS |
111 | /* This is xfs_extent_state() in-line */ |
112 | if (ext_flag) { | |
113 | ASSERT(s->br_blockcount != 0); /* saved for DMIG */ | |
114 | st = XFS_EXT_UNWRITTEN; | |
115 | } else | |
116 | st = XFS_EXT_NORM; | |
117 | s->br_state = st; | |
118 | } | |
119 | ||
f9e56f43 NS |
120 | void |
121 | xfs_bmbt_get_all( | |
5e656dbb | 122 | xfs_bmbt_rec_host_t *r, |
f9e56f43 NS |
123 | xfs_bmbt_irec_t *s) |
124 | { | |
125 | __xfs_bmbt_get_all(r->l0, r->l1, s); | |
126 | } | |
127 | ||
2bd0ea18 | 128 | /* |
b34acbba | 129 | * Extract the blockcount field from an in memory bmap extent record. |
2bd0ea18 NS |
130 | */ |
131 | xfs_filblks_t | |
132 | xfs_bmbt_get_blockcount( | |
5e656dbb | 133 | xfs_bmbt_rec_host_t *r) |
2bd0ea18 | 134 | { |
56b2de80 | 135 | return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21)); |
2bd0ea18 NS |
136 | } |
137 | ||
138 | /* | |
b34acbba | 139 | * Extract the startblock field from an in memory bmap extent record. |
2bd0ea18 NS |
140 | */ |
141 | xfs_fsblock_t | |
142 | xfs_bmbt_get_startblock( | |
5e656dbb | 143 | xfs_bmbt_rec_host_t *r) |
2bd0ea18 | 144 | { |
56b2de80 | 145 | return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) | |
f9e56f43 | 146 | (((xfs_fsblock_t)r->l1) >> 21); |
f9e56f43 NS |
147 | } |
148 | ||
149 | /* | |
b34acbba | 150 | * Extract the startoff field from an in memory bmap extent record. |
f9e56f43 NS |
151 | */ |
152 | xfs_fileoff_t | |
153 | xfs_bmbt_get_startoff( | |
5e656dbb | 154 | xfs_bmbt_rec_host_t *r) |
f9e56f43 NS |
155 | { |
156 | return ((xfs_fileoff_t)r->l0 & | |
56b2de80 | 157 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
f9e56f43 NS |
158 | } |
159 | ||
160 | xfs_exntst_t | |
161 | xfs_bmbt_get_state( | |
5e656dbb | 162 | xfs_bmbt_rec_host_t *r) |
f9e56f43 NS |
163 | { |
164 | int ext_flag; | |
165 | ||
166 | ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN)); | |
167 | return xfs_extent_state(xfs_bmbt_get_blockcount(r), | |
b194c7d8 | 168 | ext_flag); |
2bd0ea18 NS |
169 | } |
170 | ||
2bd0ea18 | 171 | /* |
b194c7d8 | 172 | * Extract the blockcount field from an on disk bmap extent record. |
2bd0ea18 | 173 | */ |
b194c7d8 BN |
174 | xfs_filblks_t |
175 | xfs_bmbt_disk_get_blockcount( | |
176 | xfs_bmbt_rec_t *r) | |
2bd0ea18 | 177 | { |
56b2de80 | 178 | return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21)); |
2bd0ea18 NS |
179 | } |
180 | ||
2bd0ea18 | 181 | /* |
b194c7d8 | 182 | * Extract the startoff field from a disk format bmap extent record. |
2bd0ea18 | 183 | */ |
b194c7d8 BN |
184 | xfs_fileoff_t |
185 | xfs_bmbt_disk_get_startoff( | |
186 | xfs_bmbt_rec_t *r) | |
2bd0ea18 | 187 | { |
b194c7d8 | 188 | return ((xfs_fileoff_t)be64_to_cpu(r->l0) & |
56b2de80 | 189 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
2bd0ea18 NS |
190 | } |
191 | ||
b194c7d8 | 192 | |
f9e56f43 NS |
193 | /* |
194 | * Set all the fields in a bmap extent record from the arguments. | |
195 | */ | |
196 | void | |
197 | xfs_bmbt_set_allf( | |
5e656dbb BN |
198 | xfs_bmbt_rec_host_t *r, |
199 | xfs_fileoff_t startoff, | |
200 | xfs_fsblock_t startblock, | |
201 | xfs_filblks_t blockcount, | |
202 | xfs_exntst_t state) | |
f9e56f43 | 203 | { |
5e656dbb BN |
204 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
205 | ||
206 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); | |
56b2de80 DC |
207 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
208 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); | |
f9e56f43 | 209 | |
56b2de80 | 210 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
5e656dbb | 211 | |
f9e56f43 | 212 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
5e656dbb BN |
213 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
214 | ((xfs_bmbt_rec_base_t)startblock >> 43); | |
215 | r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) | | |
216 | ((xfs_bmbt_rec_base_t)blockcount & | |
56b2de80 | 217 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
f9e56f43 NS |
218 | } |
219 | ||
f9e56f43 NS |
220 | /* |
221 | * Set all the fields in a bmap extent record from the uncompressed form. | |
222 | */ | |
223 | void | |
5e656dbb BN |
224 | xfs_bmbt_set_all( |
225 | xfs_bmbt_rec_host_t *r, | |
226 | xfs_bmbt_irec_t *s) | |
f9e56f43 | 227 | { |
5e656dbb BN |
228 | xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock, |
229 | s->br_blockcount, s->br_state); | |
2bd0ea18 NS |
230 | } |
231 | ||
5e656dbb | 232 | |
2bd0ea18 | 233 | /* |
f9e56f43 | 234 | * Set all the fields in a disk format bmap extent record from the arguments. |
2bd0ea18 NS |
235 | */ |
236 | void | |
f9e56f43 | 237 | xfs_bmbt_disk_set_allf( |
5e656dbb BN |
238 | xfs_bmbt_rec_t *r, |
239 | xfs_fileoff_t startoff, | |
240 | xfs_fsblock_t startblock, | |
241 | xfs_filblks_t blockcount, | |
242 | xfs_exntst_t state) | |
2bd0ea18 | 243 | { |
5e656dbb BN |
244 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
245 | ||
246 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); | |
56b2de80 DC |
247 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
248 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); | |
56b2de80 | 249 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
5e656dbb BN |
250 | |
251 | r->l0 = cpu_to_be64( | |
252 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | | |
253 | ((xfs_bmbt_rec_base_t)startoff << 9) | | |
254 | ((xfs_bmbt_rec_base_t)startblock >> 43)); | |
255 | r->l1 = cpu_to_be64( | |
256 | ((xfs_bmbt_rec_base_t)startblock << 21) | | |
257 | ((xfs_bmbt_rec_base_t)blockcount & | |
56b2de80 | 258 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
2bd0ea18 | 259 | } |
5e656dbb BN |
260 | |
261 | /* | |
262 | * Set all the fields in a bmap extent record from the uncompressed form. | |
263 | */ | |
56b2de80 | 264 | STATIC void |
5e656dbb BN |
265 | xfs_bmbt_disk_set_all( |
266 | xfs_bmbt_rec_t *r, | |
267 | xfs_bmbt_irec_t *s) | |
268 | { | |
269 | xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock, | |
270 | s->br_blockcount, s->br_state); | |
271 | } | |
2bd0ea18 NS |
272 | |
273 | /* | |
274 | * Set the blockcount field in a bmap extent record. | |
275 | */ | |
276 | void | |
277 | xfs_bmbt_set_blockcount( | |
5e656dbb | 278 | xfs_bmbt_rec_host_t *r, |
2bd0ea18 NS |
279 | xfs_filblks_t v) |
280 | { | |
56b2de80 DC |
281 | ASSERT((v & xfs_mask64hi(43)) == 0); |
282 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64hi(43)) | | |
283 | (xfs_bmbt_rec_base_t)(v & xfs_mask64lo(21)); | |
2bd0ea18 NS |
284 | } |
285 | ||
286 | /* | |
287 | * Set the startblock field in a bmap extent record. | |
288 | */ | |
289 | void | |
290 | xfs_bmbt_set_startblock( | |
5e656dbb | 291 | xfs_bmbt_rec_host_t *r, |
2bd0ea18 NS |
292 | xfs_fsblock_t v) |
293 | { | |
56b2de80 DC |
294 | ASSERT((v & xfs_mask64hi(12)) == 0); |
295 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) | | |
f9e56f43 | 296 | (xfs_bmbt_rec_base_t)(v >> 43); |
56b2de80 | 297 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) | |
f9e56f43 | 298 | (xfs_bmbt_rec_base_t)(v << 21); |
2bd0ea18 NS |
299 | } |
300 | ||
301 | /* | |
302 | * Set the startoff field in a bmap extent record. | |
303 | */ | |
304 | void | |
305 | xfs_bmbt_set_startoff( | |
5e656dbb | 306 | xfs_bmbt_rec_host_t *r, |
2bd0ea18 NS |
307 | xfs_fileoff_t v) |
308 | { | |
56b2de80 DC |
309 | ASSERT((v & xfs_mask64hi(9)) == 0); |
310 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) | | |
2bd0ea18 | 311 | ((xfs_bmbt_rec_base_t)v << 9) | |
56b2de80 | 312 | (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
2bd0ea18 NS |
313 | } |
314 | ||
315 | /* | |
316 | * Set the extent state field in a bmap extent record. | |
317 | */ | |
318 | void | |
319 | xfs_bmbt_set_state( | |
5e656dbb | 320 | xfs_bmbt_rec_host_t *r, |
2bd0ea18 NS |
321 | xfs_exntst_t v) |
322 | { | |
323 | ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN); | |
324 | if (v == XFS_EXT_NORM) | |
56b2de80 | 325 | r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN); |
2bd0ea18 | 326 | else |
56b2de80 | 327 | r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN); |
2bd0ea18 NS |
328 | } |
329 | ||
330 | /* | |
331 | * Convert in-memory form of btree root to on-disk form. | |
332 | */ | |
333 | void | |
334 | xfs_bmbt_to_bmdr( | |
b3563c19 BN |
335 | struct xfs_mount *mp, |
336 | struct xfs_btree_block *rblock, | |
2bd0ea18 NS |
337 | int rblocklen, |
338 | xfs_bmdr_block_t *dblock, | |
339 | int dblocklen) | |
340 | { | |
341 | int dmxr; | |
342 | xfs_bmbt_key_t *fkp; | |
5e656dbb | 343 | __be64 *fpp; |
2bd0ea18 | 344 | xfs_bmbt_key_t *tkp; |
5e656dbb | 345 | __be64 *tpp; |
2bd0ea18 | 346 | |
5dfa5cd2 DC |
347 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
348 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC)); | |
9c4e12fb ES |
349 | ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid, |
350 | &mp->m_sb.sb_meta_uuid)); | |
5dfa5cd2 DC |
351 | ASSERT(rblock->bb_u.l.bb_blkno == |
352 | cpu_to_be64(XFS_BUF_DADDR_NULL)); | |
353 | } else | |
354 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC)); | |
5a35bf2c DC |
355 | ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK)); |
356 | ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK)); | |
a2ceac1f | 357 | ASSERT(rblock->bb_level != 0); |
6e3140c7 NS |
358 | dblock->bb_level = rblock->bb_level; |
359 | dblock->bb_numrecs = rblock->bb_numrecs; | |
ff105f75 | 360 | dmxr = xfs_bmdr_maxrecs(dblocklen, 0); |
b3563c19 BN |
361 | fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
362 | tkp = XFS_BMDR_KEY_ADDR(dblock, 1); | |
363 | fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); | |
364 | tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); | |
6e3140c7 | 365 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
32181a02 | 366 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
5e656dbb | 367 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
2bd0ea18 NS |
368 | } |
369 | ||
2bd0ea18 | 370 | /* |
5e656dbb | 371 | * Check extent records, which have just been read, for |
2bd0ea18 NS |
372 | * any bit in the extent flag field. ASSERT on debug |
373 | * kernels, as this condition should not occur. | |
374 | * Return an error condition (1) if any flags found, | |
375 | * otherwise return 0. | |
376 | */ | |
dfc130f3 | 377 | |
2bd0ea18 NS |
378 | int |
379 | xfs_check_nostate_extents( | |
5e656dbb BN |
380 | xfs_ifork_t *ifp, |
381 | xfs_extnum_t idx, | |
2bd0ea18 NS |
382 | xfs_extnum_t num) |
383 | { | |
5e656dbb BN |
384 | for (; num > 0; num--, idx++) { |
385 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); | |
f9e56f43 | 386 | if ((ep->l0 >> |
4542f191 | 387 | (64 - BMBT_EXNTFLAG_BITLEN)) != 0) { |
2bd0ea18 NS |
388 | ASSERT(0); |
389 | return 1; | |
390 | } | |
391 | } | |
392 | return 0; | |
393 | } | |
b194c7d8 BN |
394 | |
395 | ||
396 | STATIC struct xfs_btree_cur * | |
397 | xfs_bmbt_dup_cursor( | |
398 | struct xfs_btree_cur *cur) | |
399 | { | |
400 | struct xfs_btree_cur *new; | |
401 | ||
402 | new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp, | |
403 | cur->bc_private.b.ip, cur->bc_private.b.whichfork); | |
404 | ||
405 | /* | |
406 | * Copy the firstblock, flist, and flags values, | |
407 | * since init cursor doesn't get them. | |
408 | */ | |
409 | new->bc_private.b.firstblock = cur->bc_private.b.firstblock; | |
410 | new->bc_private.b.flist = cur->bc_private.b.flist; | |
411 | new->bc_private.b.flags = cur->bc_private.b.flags; | |
412 | ||
413 | return new; | |
414 | } | |
415 | ||
416 | STATIC void | |
417 | xfs_bmbt_update_cursor( | |
418 | struct xfs_btree_cur *src, | |
419 | struct xfs_btree_cur *dst) | |
420 | { | |
421 | ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) || | |
422 | (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME)); | |
423 | ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist); | |
424 | ||
425 | dst->bc_private.b.allocated += src->bc_private.b.allocated; | |
426 | dst->bc_private.b.firstblock = src->bc_private.b.firstblock; | |
427 | ||
428 | src->bc_private.b.allocated = 0; | |
429 | } | |
430 | ||
431 | STATIC int | |
432 | xfs_bmbt_alloc_block( | |
433 | struct xfs_btree_cur *cur, | |
434 | union xfs_btree_ptr *start, | |
435 | union xfs_btree_ptr *new, | |
b194c7d8 BN |
436 | int *stat) |
437 | { | |
438 | xfs_alloc_arg_t args; /* block allocation args */ | |
439 | int error; /* error return value */ | |
440 | ||
441 | memset(&args, 0, sizeof(args)); | |
442 | args.tp = cur->bc_tp; | |
443 | args.mp = cur->bc_mp; | |
444 | args.fsbno = cur->bc_private.b.firstblock; | |
445 | args.firstblock = args.fsbno; | |
446 | ||
447 | if (args.fsbno == NULLFSBLOCK) { | |
448 | args.fsbno = be64_to_cpu(start->l); | |
449 | args.type = XFS_ALLOCTYPE_START_BNO; | |
450 | /* | |
451 | * Make sure there is sufficient room left in the AG to | |
452 | * complete a full tree split for an extent insert. If | |
453 | * we are converting the middle part of an extent then | |
454 | * we may need space for two tree splits. | |
455 | * | |
456 | * We are relying on the caller to make the correct block | |
457 | * reservation for this operation to succeed. If the | |
458 | * reservation amount is insufficient then we may fail a | |
459 | * block allocation here and corrupt the filesystem. | |
460 | */ | |
461 | args.minleft = xfs_trans_get_block_res(args.tp); | |
462 | } else if (cur->bc_private.b.flist->xbf_low) { | |
463 | args.type = XFS_ALLOCTYPE_START_BNO; | |
464 | } else { | |
465 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
466 | } | |
467 | ||
468 | args.minlen = args.maxlen = args.prod = 1; | |
469 | args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL; | |
470 | if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) { | |
12b53197 | 471 | error = -ENOSPC; |
b194c7d8 BN |
472 | goto error0; |
473 | } | |
474 | error = xfs_alloc_vextent(&args); | |
475 | if (error) | |
476 | goto error0; | |
477 | ||
478 | if (args.fsbno == NULLFSBLOCK && args.minleft) { | |
479 | /* | |
480 | * Could not find an AG with enough free space to satisfy | |
481 | * a full btree split. Try again without minleft and if | |
482 | * successful activate the lowspace algorithm. | |
483 | */ | |
484 | args.fsbno = 0; | |
485 | args.type = XFS_ALLOCTYPE_FIRST_AG; | |
486 | args.minleft = 0; | |
487 | error = xfs_alloc_vextent(&args); | |
488 | if (error) | |
489 | goto error0; | |
490 | cur->bc_private.b.flist->xbf_low = 1; | |
491 | } | |
492 | if (args.fsbno == NULLFSBLOCK) { | |
493 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
494 | *stat = 0; | |
495 | return 0; | |
496 | } | |
497 | ASSERT(args.len == 1); | |
498 | cur->bc_private.b.firstblock = args.fsbno; | |
499 | cur->bc_private.b.allocated++; | |
500 | cur->bc_private.b.ip->i_d.di_nblocks++; | |
501 | xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE); | |
56b2de80 | 502 | xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip, |
b194c7d8 BN |
503 | XFS_TRANS_DQ_BCOUNT, 1L); |
504 | ||
505 | new->l = cpu_to_be64(args.fsbno); | |
506 | ||
507 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); | |
508 | *stat = 1; | |
509 | return 0; | |
510 | ||
511 | error0: | |
512 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); | |
513 | return error; | |
514 | } | |
515 | ||
516 | STATIC int | |
517 | xfs_bmbt_free_block( | |
518 | struct xfs_btree_cur *cur, | |
519 | struct xfs_buf *bp) | |
520 | { | |
521 | struct xfs_mount *mp = cur->bc_mp; | |
522 | struct xfs_inode *ip = cur->bc_private.b.ip; | |
523 | struct xfs_trans *tp = cur->bc_tp; | |
524 | xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp)); | |
525 | ||
af2e7c6e | 526 | xfs_bmap_add_free(mp, cur->bc_private.b.flist, fsbno, 1); |
b194c7d8 BN |
527 | ip->i_d.di_nblocks--; |
528 | ||
529 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
56b2de80 | 530 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
b194c7d8 BN |
531 | return 0; |
532 | } | |
533 | ||
534 | STATIC int | |
535 | xfs_bmbt_get_minrecs( | |
536 | struct xfs_btree_cur *cur, | |
537 | int level) | |
538 | { | |
b3563c19 BN |
539 | if (level == cur->bc_nlevels - 1) { |
540 | struct xfs_ifork *ifp; | |
541 | ||
542 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, | |
543 | cur->bc_private.b.whichfork); | |
544 | ||
545 | return xfs_bmbt_maxrecs(cur->bc_mp, | |
546 | ifp->if_broot_bytes, level == 0) / 2; | |
547 | } | |
548 | ||
549 | return cur->bc_mp->m_bmap_dmnr[level != 0]; | |
b194c7d8 BN |
550 | } |
551 | ||
b3563c19 | 552 | int |
b194c7d8 BN |
553 | xfs_bmbt_get_maxrecs( |
554 | struct xfs_btree_cur *cur, | |
555 | int level) | |
556 | { | |
b3563c19 BN |
557 | if (level == cur->bc_nlevels - 1) { |
558 | struct xfs_ifork *ifp; | |
559 | ||
560 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, | |
561 | cur->bc_private.b.whichfork); | |
562 | ||
563 | return xfs_bmbt_maxrecs(cur->bc_mp, | |
564 | ifp->if_broot_bytes, level == 0); | |
565 | } | |
566 | ||
567 | return cur->bc_mp->m_bmap_dmxr[level != 0]; | |
568 | ||
b194c7d8 BN |
569 | } |
570 | ||
571 | /* | |
572 | * Get the maximum records we could store in the on-disk format. | |
573 | * | |
574 | * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but | |
575 | * for the root node this checks the available space in the dinode fork | |
576 | * so that we can resize the in-memory buffer to match it. After a | |
577 | * resize to the maximum size this function returns the same value | |
578 | * as xfs_bmbt_get_maxrecs for the root node, too. | |
579 | */ | |
580 | STATIC int | |
581 | xfs_bmbt_get_dmaxrecs( | |
582 | struct xfs_btree_cur *cur, | |
583 | int level) | |
584 | { | |
b3563c19 BN |
585 | if (level != cur->bc_nlevels - 1) |
586 | return cur->bc_mp->m_bmap_dmxr[level != 0]; | |
ff105f75 | 587 | return xfs_bmdr_maxrecs(cur->bc_private.b.forksize, level == 0); |
b194c7d8 BN |
588 | } |
589 | ||
590 | STATIC void | |
591 | xfs_bmbt_init_key_from_rec( | |
592 | union xfs_btree_key *key, | |
593 | union xfs_btree_rec *rec) | |
594 | { | |
595 | key->bmbt.br_startoff = | |
596 | cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt)); | |
597 | } | |
598 | ||
599 | STATIC void | |
600 | xfs_bmbt_init_rec_from_key( | |
601 | union xfs_btree_key *key, | |
602 | union xfs_btree_rec *rec) | |
603 | { | |
604 | ASSERT(key->bmbt.br_startoff != 0); | |
605 | ||
606 | xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff), | |
607 | 0, 0, XFS_EXT_NORM); | |
608 | } | |
609 | ||
610 | STATIC void | |
611 | xfs_bmbt_init_rec_from_cur( | |
612 | struct xfs_btree_cur *cur, | |
613 | union xfs_btree_rec *rec) | |
614 | { | |
615 | xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b); | |
616 | } | |
617 | ||
618 | STATIC void | |
619 | xfs_bmbt_init_ptr_from_cur( | |
620 | struct xfs_btree_cur *cur, | |
621 | union xfs_btree_ptr *ptr) | |
622 | { | |
623 | ptr->l = 0; | |
624 | } | |
625 | ||
626 | STATIC __int64_t | |
627 | xfs_bmbt_key_diff( | |
628 | struct xfs_btree_cur *cur, | |
629 | union xfs_btree_key *key) | |
630 | { | |
631 | return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) - | |
632 | cur->bc_rec.b.br_startoff; | |
633 | } | |
634 | ||
5dfa5cd2 | 635 | static bool |
a2ceac1f DC |
636 | xfs_bmbt_verify( |
637 | struct xfs_buf *bp) | |
638 | { | |
639 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
640 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); | |
641 | unsigned int level; | |
a2ceac1f | 642 | |
c0adfb03 DC |
643 | switch (block->bb_magic) { |
644 | case cpu_to_be32(XFS_BMAP_CRC_MAGIC): | |
5dfa5cd2 DC |
645 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
646 | return false; | |
9c4e12fb | 647 | if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid)) |
5dfa5cd2 | 648 | return false; |
c0adfb03 | 649 | if (be64_to_cpu(block->bb_u.l.bb_blkno) != bp->b_bn) |
5dfa5cd2 DC |
650 | return false; |
651 | /* | |
652 | * XXX: need a better way of verifying the owner here. Right now | |
653 | * just make sure there has been one set. | |
654 | */ | |
655 | if (be64_to_cpu(block->bb_u.l.bb_owner) == 0) | |
656 | return false; | |
657 | /* fall through */ | |
c0adfb03 | 658 | case cpu_to_be32(XFS_BMAP_MAGIC): |
5dfa5cd2 DC |
659 | break; |
660 | default: | |
661 | return false; | |
662 | } | |
663 | ||
664 | /* | |
665 | * numrecs and level verification. | |
a2ceac1f | 666 | * |
5dfa5cd2 | 667 | * We don't know what fork we belong to, so just verify that the level |
a2ceac1f DC |
668 | * is less than the maximum of the two. Later checks will be more |
669 | * precise. | |
670 | */ | |
671 | level = be16_to_cpu(block->bb_level); | |
e6d77a21 | 672 | if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1])) |
5dfa5cd2 DC |
673 | return false; |
674 | if (be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) | |
675 | return false; | |
a2ceac1f DC |
676 | |
677 | /* sibling pointer verification */ | |
5dfa5cd2 | 678 | if (!block->bb_u.l.bb_leftsib || |
5a35bf2c | 679 | (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) && |
5dfa5cd2 DC |
680 | !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))) |
681 | return false; | |
682 | if (!block->bb_u.l.bb_rightsib || | |
5a35bf2c | 683 | (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) && |
5dfa5cd2 DC |
684 | !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))) |
685 | return false; | |
686 | ||
687 | return true; | |
a2ceac1f DC |
688 | } |
689 | ||
690 | static void | |
691 | xfs_bmbt_read_verify( | |
692 | struct xfs_buf *bp) | |
693 | { | |
45922933 | 694 | if (!xfs_btree_lblock_verify_crc(bp)) |
12b53197 | 695 | xfs_buf_ioerror(bp, -EFSBADCRC); |
45922933 | 696 | else if (!xfs_bmbt_verify(bp)) |
12b53197 | 697 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
45922933 DC |
698 | |
699 | if (bp->b_error) { | |
700 | trace_xfs_btree_corrupt(bp, _RET_IP_); | |
701 | xfs_verifier_error(bp); | |
5dfa5cd2 | 702 | } |
a2ceac1f DC |
703 | } |
704 | ||
705 | static void | |
706 | xfs_bmbt_write_verify( | |
707 | struct xfs_buf *bp) | |
708 | { | |
5dfa5cd2 | 709 | if (!xfs_bmbt_verify(bp)) { |
5dfa5cd2 | 710 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
12b53197 | 711 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
45922933 | 712 | xfs_verifier_error(bp); |
5dfa5cd2 DC |
713 | return; |
714 | } | |
715 | xfs_btree_lblock_calc_crc(bp); | |
a2ceac1f DC |
716 | } |
717 | ||
718 | const struct xfs_buf_ops xfs_bmbt_buf_ops = { | |
a3fac935 | 719 | .name = "xfs_bmbt", |
a2ceac1f DC |
720 | .verify_read = xfs_bmbt_read_verify, |
721 | .verify_write = xfs_bmbt_write_verify, | |
722 | }; | |
723 | ||
724 | ||
c0adfb03 | 725 | #if defined(DEBUG) || defined(XFS_WARN) |
b194c7d8 BN |
726 | STATIC int |
727 | xfs_bmbt_keys_inorder( | |
728 | struct xfs_btree_cur *cur, | |
729 | union xfs_btree_key *k1, | |
730 | union xfs_btree_key *k2) | |
731 | { | |
732 | return be64_to_cpu(k1->bmbt.br_startoff) < | |
733 | be64_to_cpu(k2->bmbt.br_startoff); | |
734 | } | |
735 | ||
736 | STATIC int | |
737 | xfs_bmbt_recs_inorder( | |
738 | struct xfs_btree_cur *cur, | |
739 | union xfs_btree_rec *r1, | |
740 | union xfs_btree_rec *r2) | |
741 | { | |
742 | return xfs_bmbt_disk_get_startoff(&r1->bmbt) + | |
743 | xfs_bmbt_disk_get_blockcount(&r1->bmbt) <= | |
744 | xfs_bmbt_disk_get_startoff(&r2->bmbt); | |
745 | } | |
746 | #endif /* DEBUG */ | |
747 | ||
b194c7d8 BN |
748 | static const struct xfs_btree_ops xfs_bmbt_ops = { |
749 | .rec_len = sizeof(xfs_bmbt_rec_t), | |
750 | .key_len = sizeof(xfs_bmbt_key_t), | |
751 | ||
752 | .dup_cursor = xfs_bmbt_dup_cursor, | |
753 | .update_cursor = xfs_bmbt_update_cursor, | |
754 | .alloc_block = xfs_bmbt_alloc_block, | |
755 | .free_block = xfs_bmbt_free_block, | |
756 | .get_maxrecs = xfs_bmbt_get_maxrecs, | |
757 | .get_minrecs = xfs_bmbt_get_minrecs, | |
758 | .get_dmaxrecs = xfs_bmbt_get_dmaxrecs, | |
759 | .init_key_from_rec = xfs_bmbt_init_key_from_rec, | |
760 | .init_rec_from_key = xfs_bmbt_init_rec_from_key, | |
761 | .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, | |
762 | .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur, | |
763 | .key_diff = xfs_bmbt_key_diff, | |
a2ceac1f | 764 | .buf_ops = &xfs_bmbt_buf_ops, |
c0adfb03 | 765 | #if defined(DEBUG) || defined(XFS_WARN) |
b194c7d8 BN |
766 | .keys_inorder = xfs_bmbt_keys_inorder, |
767 | .recs_inorder = xfs_bmbt_recs_inorder, | |
768 | #endif | |
b194c7d8 BN |
769 | }; |
770 | ||
771 | /* | |
772 | * Allocate a new bmap btree cursor. | |
773 | */ | |
774 | struct xfs_btree_cur * /* new bmap btree cursor */ | |
775 | xfs_bmbt_init_cursor( | |
776 | struct xfs_mount *mp, /* file system mount point */ | |
777 | struct xfs_trans *tp, /* transaction pointer */ | |
778 | struct xfs_inode *ip, /* inode owning the btree */ | |
779 | int whichfork) /* data or attr fork */ | |
780 | { | |
781 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
782 | struct xfs_btree_cur *cur; | |
783 | ||
784 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP); | |
785 | ||
786 | cur->bc_tp = tp; | |
787 | cur->bc_mp = mp; | |
788 | cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1; | |
789 | cur->bc_btnum = XFS_BTNUM_BMAP; | |
790 | cur->bc_blocklog = mp->m_sb.sb_blocklog; | |
791 | ||
792 | cur->bc_ops = &xfs_bmbt_ops; | |
793 | cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE; | |
5dfa5cd2 DC |
794 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
795 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; | |
b194c7d8 BN |
796 | |
797 | cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork); | |
798 | cur->bc_private.b.ip = ip; | |
799 | cur->bc_private.b.firstblock = NULLFSBLOCK; | |
800 | cur->bc_private.b.flist = NULL; | |
801 | cur->bc_private.b.allocated = 0; | |
802 | cur->bc_private.b.flags = 0; | |
803 | cur->bc_private.b.whichfork = whichfork; | |
804 | ||
805 | return cur; | |
806 | } | |
b3563c19 BN |
807 | |
808 | /* | |
809 | * Calculate number of records in a bmap btree block. | |
810 | */ | |
811 | int | |
812 | xfs_bmbt_maxrecs( | |
813 | struct xfs_mount *mp, | |
814 | int blocklen, | |
815 | int leaf) | |
816 | { | |
817 | blocklen -= XFS_BMBT_BLOCK_LEN(mp); | |
818 | ||
819 | if (leaf) | |
820 | return blocklen / sizeof(xfs_bmbt_rec_t); | |
821 | return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t)); | |
822 | } | |
823 | ||
824 | /* | |
825 | * Calculate number of records in a bmap btree inode root. | |
826 | */ | |
827 | int | |
828 | xfs_bmdr_maxrecs( | |
b3563c19 BN |
829 | int blocklen, |
830 | int leaf) | |
831 | { | |
832 | blocklen -= sizeof(xfs_bmdr_block_t); | |
833 | ||
834 | if (leaf) | |
835 | return blocklen / sizeof(xfs_bmdr_rec_t); | |
836 | return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t)); | |
837 | } | |
9c6ebc42 DC |
838 | |
839 | /* | |
ff105f75 | 840 | * Change the owner of a btree format fork fo the inode passed in. Change it to |
9c6ebc42 DC |
841 | * the owner of that is passed in so that we can change owners before or after |
842 | * we switch forks between inodes. The operation that the caller is doing will | |
843 | * determine whether is needs to change owner before or after the switch. | |
844 | * | |
845 | * For demand paged transactional modification, the fork switch should be done | |
846 | * after reading in all the blocks, modifying them and pinning them in the | |
847 | * transaction. For modification when the buffers are already pinned in memory, | |
848 | * the fork switch can be done before changing the owner as we won't need to | |
849 | * validate the owner until the btree buffers are unpinned and writes can occur | |
850 | * again. | |
851 | * | |
852 | * For recovery based ownership change, there is no transactional context and | |
853 | * so a buffer list must be supplied so that we can record the buffers that we | |
854 | * modified for the caller to issue IO on. | |
855 | */ | |
856 | int | |
857 | xfs_bmbt_change_owner( | |
858 | struct xfs_trans *tp, | |
859 | struct xfs_inode *ip, | |
860 | int whichfork, | |
861 | xfs_ino_t new_owner, | |
862 | struct list_head *buffer_list) | |
863 | { | |
864 | struct xfs_btree_cur *cur; | |
865 | int error; | |
866 | ||
867 | ASSERT(tp || buffer_list); | |
868 | ASSERT(!(tp && buffer_list)); | |
869 | if (whichfork == XFS_DATA_FORK) | |
870 | ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE); | |
871 | else | |
872 | ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE); | |
873 | ||
874 | cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork); | |
875 | if (!cur) | |
12b53197 | 876 | return -ENOMEM; |
9c6ebc42 DC |
877 | |
878 | error = xfs_btree_change_owner(cur, new_owner, buffer_list); | |
879 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
880 | return error; | |
881 | } |