]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 | 6 | #include "xfs.h" |
a844f451 | 7 | #include "xfs_fs.h" |
632b89e8 | 8 | #include "xfs_shared.h" |
6ca1c906 | 9 | #include "xfs_format.h" |
239880ef DC |
10 | #include "xfs_log_format.h" |
11 | #include "xfs_trans_resv.h" | |
a844f451 | 12 | #include "xfs_bit.h" |
1da177e4 | 13 | #include "xfs_mount.h" |
1da177e4 | 14 | #include "xfs_inode.h" |
239880ef | 15 | #include "xfs_trans.h" |
1da177e4 | 16 | #include "xfs_alloc.h" |
a844f451 | 17 | #include "xfs_btree.h" |
a4fbe6ab | 18 | #include "xfs_bmap_btree.h" |
1da177e4 LT |
19 | #include "xfs_bmap.h" |
20 | #include "xfs_error.h" | |
21 | #include "xfs_quota.h" | |
3d3e6f64 | 22 | #include "xfs_trace.h" |
340785cc | 23 | #include "xfs_rmap.h" |
1da177e4 | 24 | |
1da177e4 LT |
25 | /* |
26 | * Convert on-disk form of btree root to in-memory form. | |
27 | */ | |
28 | void | |
29 | xfs_bmdr_to_bmbt( | |
ee1a47ab | 30 | struct xfs_inode *ip, |
1da177e4 LT |
31 | xfs_bmdr_block_t *dblock, |
32 | int dblocklen, | |
7cc95a82 | 33 | struct xfs_btree_block *rblock, |
1da177e4 LT |
34 | int rblocklen) |
35 | { | |
ee1a47ab | 36 | struct xfs_mount *mp = ip->i_mount; |
1da177e4 LT |
37 | int dmxr; |
38 | xfs_bmbt_key_t *fkp; | |
576039cf | 39 | __be64 *fpp; |
1da177e4 | 40 | xfs_bmbt_key_t *tkp; |
576039cf | 41 | __be64 *tpp; |
1da177e4 | 42 | |
b6f41e44 ES |
43 | xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL, |
44 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, | |
f88ae46b | 45 | XFS_BTREE_LONG_PTRS); |
16259e7d CH |
46 | rblock->bb_level = dblock->bb_level; |
47 | ASSERT(be16_to_cpu(rblock->bb_level) > 0); | |
48 | rblock->bb_numrecs = dblock->bb_numrecs; | |
152d93b7 | 49 | dmxr = xfs_bmdr_maxrecs(dblocklen, 0); |
136341b4 CH |
50 | fkp = XFS_BMDR_KEY_ADDR(dblock, 1); |
51 | tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); | |
52 | fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); | |
60197e8d | 53 | tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
16259e7d | 54 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
1da177e4 | 55 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
576039cf | 56 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
1da177e4 LT |
57 | } |
58 | ||
1da177e4 | 59 | void |
6bdcf26a | 60 | xfs_bmbt_disk_get_all( |
22ece4e8 | 61 | const struct xfs_bmbt_rec *rec, |
6bdcf26a | 62 | struct xfs_bmbt_irec *irec) |
1da177e4 | 63 | { |
6bdcf26a CH |
64 | uint64_t l0 = get_unaligned_be64(&rec->l0); |
65 | uint64_t l1 = get_unaligned_be64(&rec->l1); | |
1da177e4 | 66 | |
6bdcf26a CH |
67 | irec->br_startoff = (l0 & xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
68 | irec->br_startblock = ((l0 & xfs_mask64lo(9)) << 43) | (l1 >> 21); | |
69 | irec->br_blockcount = l1 & xfs_mask64lo(21); | |
70 | if (l0 >> (64 - BMBT_EXNTFLAG_BITLEN)) | |
71 | irec->br_state = XFS_EXT_UNWRITTEN; | |
72 | else | |
73 | irec->br_state = XFS_EXT_NORM; | |
1da177e4 LT |
74 | } |
75 | ||
1da177e4 LT |
76 | /* |
77 | * Extract the blockcount field from an on disk bmap extent record. | |
78 | */ | |
79 | xfs_filblks_t | |
80 | xfs_bmbt_disk_get_blockcount( | |
23825cd1 | 81 | const struct xfs_bmbt_rec *r) |
1da177e4 | 82 | { |
fb82557f | 83 | return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21)); |
1da177e4 LT |
84 | } |
85 | ||
1da177e4 LT |
86 | /* |
87 | * Extract the startoff field from a disk format bmap extent record. | |
88 | */ | |
89 | xfs_fileoff_t | |
90 | xfs_bmbt_disk_get_startoff( | |
23825cd1 | 91 | const struct xfs_bmbt_rec *r) |
1da177e4 | 92 | { |
cd8b0a97 | 93 | return ((xfs_fileoff_t)be64_to_cpu(r->l0) & |
fb82557f | 94 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
1da177e4 | 95 | } |
1da177e4 | 96 | |
1da177e4 | 97 | /* |
a67d00a5 | 98 | * Set all the fields in a bmap extent record from the uncompressed form. |
1da177e4 LT |
99 | */ |
100 | void | |
a67d00a5 CH |
101 | xfs_bmbt_disk_set_all( |
102 | struct xfs_bmbt_rec *r, | |
103 | struct xfs_bmbt_irec *s) | |
1da177e4 | 104 | { |
a67d00a5 | 105 | int extent_flag = (s->br_state != XFS_EXT_NORM); |
8cba4344 | 106 | |
a67d00a5 CH |
107 | ASSERT(s->br_state == XFS_EXT_NORM || s->br_state == XFS_EXT_UNWRITTEN); |
108 | ASSERT(!(s->br_startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN))); | |
109 | ASSERT(!(s->br_blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN))); | |
110 | ASSERT(!(s->br_startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN))); | |
8cba4344 | 111 | |
135dcc10 | 112 | put_unaligned_be64( |
8cba4344 | 113 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
a67d00a5 | 114 | ((xfs_bmbt_rec_base_t)s->br_startoff << 9) | |
135dcc10 CH |
115 | ((xfs_bmbt_rec_base_t)s->br_startblock >> 43), &r->l0); |
116 | put_unaligned_be64( | |
a67d00a5 CH |
117 | ((xfs_bmbt_rec_base_t)s->br_startblock << 21) | |
118 | ((xfs_bmbt_rec_base_t)s->br_blockcount & | |
135dcc10 | 119 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)), &r->l1); |
1da177e4 | 120 | } |
8cba4344 | 121 | |
1da177e4 LT |
122 | /* |
123 | * Convert in-memory form of btree root to on-disk form. | |
124 | */ | |
125 | void | |
126 | xfs_bmbt_to_bmdr( | |
60197e8d | 127 | struct xfs_mount *mp, |
7cc95a82 | 128 | struct xfs_btree_block *rblock, |
1da177e4 LT |
129 | int rblocklen, |
130 | xfs_bmdr_block_t *dblock, | |
131 | int dblocklen) | |
132 | { | |
133 | int dmxr; | |
134 | xfs_bmbt_key_t *fkp; | |
576039cf | 135 | __be64 *fpp; |
1da177e4 | 136 | xfs_bmbt_key_t *tkp; |
576039cf | 137 | __be64 *tpp; |
1da177e4 | 138 | |
38c26bfd | 139 | if (xfs_has_crc(mp)) { |
ee1a47ab | 140 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC)); |
ce748eaa ES |
141 | ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid, |
142 | &mp->m_sb.sb_meta_uuid)); | |
ee1a47ab CH |
143 | ASSERT(rblock->bb_u.l.bb_blkno == |
144 | cpu_to_be64(XFS_BUF_DADDR_NULL)); | |
145 | } else | |
146 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC)); | |
d5cf09ba CH |
147 | ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK)); |
148 | ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK)); | |
69ef921b | 149 | ASSERT(rblock->bb_level != 0); |
16259e7d CH |
150 | dblock->bb_level = rblock->bb_level; |
151 | dblock->bb_numrecs = rblock->bb_numrecs; | |
152d93b7 | 152 | dmxr = xfs_bmdr_maxrecs(dblocklen, 0); |
136341b4 CH |
153 | fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
154 | tkp = XFS_BMDR_KEY_ADDR(dblock, 1); | |
60197e8d | 155 | fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
136341b4 | 156 | tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); |
16259e7d | 157 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
1da177e4 | 158 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
576039cf | 159 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
1da177e4 LT |
160 | } |
161 | ||
561f7d17 CH |
162 | STATIC struct xfs_btree_cur * |
163 | xfs_bmbt_dup_cursor( | |
164 | struct xfs_btree_cur *cur) | |
165 | { | |
166 | struct xfs_btree_cur *new; | |
167 | ||
168 | new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp, | |
92219c29 | 169 | cur->bc_ino.ip, cur->bc_ino.whichfork); |
561f7d17 CH |
170 | |
171 | /* | |
2c3234d1 | 172 | * Copy the firstblock, dfops, and flags values, |
561f7d17 CH |
173 | * since init cursor doesn't get them. |
174 | */ | |
92219c29 | 175 | new->bc_ino.flags = cur->bc_ino.flags; |
561f7d17 CH |
176 | |
177 | return new; | |
178 | } | |
179 | ||
4b22a571 CH |
180 | STATIC void |
181 | xfs_bmbt_update_cursor( | |
182 | struct xfs_btree_cur *src, | |
183 | struct xfs_btree_cur *dst) | |
184 | { | |
cf612de7 | 185 | ASSERT((dst->bc_tp->t_firstblock != NULLFSBLOCK) || |
db07349d | 186 | (dst->bc_ino.ip->i_diflags & XFS_DIFLAG_REALTIME)); |
4b22a571 | 187 | |
92219c29 | 188 | dst->bc_ino.allocated += src->bc_ino.allocated; |
cf612de7 | 189 | dst->bc_tp->t_firstblock = src->bc_tp->t_firstblock; |
4b22a571 | 190 | |
92219c29 | 191 | src->bc_ino.allocated = 0; |
4b22a571 CH |
192 | } |
193 | ||
f5eb8e7c CH |
194 | STATIC int |
195 | xfs_bmbt_alloc_block( | |
deb06b9a DW |
196 | struct xfs_btree_cur *cur, |
197 | const union xfs_btree_ptr *start, | |
198 | union xfs_btree_ptr *new, | |
199 | int *stat) | |
f5eb8e7c CH |
200 | { |
201 | xfs_alloc_arg_t args; /* block allocation args */ | |
202 | int error; /* error return value */ | |
203 | ||
204 | memset(&args, 0, sizeof(args)); | |
205 | args.tp = cur->bc_tp; | |
206 | args.mp = cur->bc_mp; | |
cf612de7 | 207 | args.fsbno = cur->bc_tp->t_firstblock; |
92219c29 DC |
208 | xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_ino.ip->i_ino, |
209 | cur->bc_ino.whichfork); | |
f5eb8e7c CH |
210 | |
211 | if (args.fsbno == NULLFSBLOCK) { | |
212 | args.fsbno = be64_to_cpu(start->l); | |
213 | args.type = XFS_ALLOCTYPE_START_BNO; | |
214 | /* | |
215 | * Make sure there is sufficient room left in the AG to | |
216 | * complete a full tree split for an extent insert. If | |
217 | * we are converting the middle part of an extent then | |
218 | * we may need space for two tree splits. | |
219 | * | |
220 | * We are relying on the caller to make the correct block | |
221 | * reservation for this operation to succeed. If the | |
222 | * reservation amount is insufficient then we may fail a | |
223 | * block allocation here and corrupt the filesystem. | |
224 | */ | |
a7e5d03b | 225 | args.minleft = args.tp->t_blk_res; |
1214f1cf | 226 | } else if (cur->bc_tp->t_flags & XFS_TRANS_LOWMODE) { |
f5eb8e7c CH |
227 | args.type = XFS_ALLOCTYPE_START_BNO; |
228 | } else { | |
229 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
230 | } | |
231 | ||
232 | args.minlen = args.maxlen = args.prod = 1; | |
8ef54797 | 233 | args.wasdel = cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL; |
a7e5d03b | 234 | if (!args.wasdel && args.tp->t_blk_res == 0) { |
2451337d | 235 | error = -ENOSPC; |
f5eb8e7c CH |
236 | goto error0; |
237 | } | |
238 | error = xfs_alloc_vextent(&args); | |
239 | if (error) | |
240 | goto error0; | |
241 | ||
242 | if (args.fsbno == NULLFSBLOCK && args.minleft) { | |
243 | /* | |
244 | * Could not find an AG with enough free space to satisfy | |
255c5162 | 245 | * a full btree split. Try again and if |
f5eb8e7c CH |
246 | * successful activate the lowspace algorithm. |
247 | */ | |
248 | args.fsbno = 0; | |
249 | args.type = XFS_ALLOCTYPE_FIRST_AG; | |
f5eb8e7c CH |
250 | error = xfs_alloc_vextent(&args); |
251 | if (error) | |
252 | goto error0; | |
1214f1cf | 253 | cur->bc_tp->t_flags |= XFS_TRANS_LOWMODE; |
f5eb8e7c | 254 | } |
2fcc319d | 255 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
f5eb8e7c CH |
256 | *stat = 0; |
257 | return 0; | |
258 | } | |
e157ebdc | 259 | |
f5eb8e7c | 260 | ASSERT(args.len == 1); |
cf612de7 | 261 | cur->bc_tp->t_firstblock = args.fsbno; |
92219c29 | 262 | cur->bc_ino.allocated++; |
6e73a545 | 263 | cur->bc_ino.ip->i_nblocks++; |
92219c29 DC |
264 | xfs_trans_log_inode(args.tp, cur->bc_ino.ip, XFS_ILOG_CORE); |
265 | xfs_trans_mod_dquot_byino(args.tp, cur->bc_ino.ip, | |
f5eb8e7c CH |
266 | XFS_TRANS_DQ_BCOUNT, 1L); |
267 | ||
268 | new->l = cpu_to_be64(args.fsbno); | |
269 | ||
f5eb8e7c CH |
270 | *stat = 1; |
271 | return 0; | |
272 | ||
273 | error0: | |
f5eb8e7c CH |
274 | return error; |
275 | } | |
276 | ||
d4b3a4b7 CH |
277 | STATIC int |
278 | xfs_bmbt_free_block( | |
279 | struct xfs_btree_cur *cur, | |
280 | struct xfs_buf *bp) | |
281 | { | |
282 | struct xfs_mount *mp = cur->bc_mp; | |
92219c29 | 283 | struct xfs_inode *ip = cur->bc_ino.ip; |
d4b3a4b7 CH |
284 | struct xfs_trans *tp = cur->bc_tp; |
285 | xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp)); | |
340785cc | 286 | struct xfs_owner_info oinfo; |
d4b3a4b7 | 287 | |
92219c29 | 288 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork); |
0f37d178 | 289 | xfs_bmap_add_free(cur->bc_tp, fsbno, 1, &oinfo); |
6e73a545 | 290 | ip->i_nblocks--; |
d4b3a4b7 CH |
291 | |
292 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
7d095257 | 293 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
d4b3a4b7 CH |
294 | return 0; |
295 | } | |
296 | ||
91cca5df CH |
297 | STATIC int |
298 | xfs_bmbt_get_minrecs( | |
299 | struct xfs_btree_cur *cur, | |
300 | int level) | |
301 | { | |
60197e8d CH |
302 | if (level == cur->bc_nlevels - 1) { |
303 | struct xfs_ifork *ifp; | |
304 | ||
92219c29 DC |
305 | ifp = XFS_IFORK_PTR(cur->bc_ino.ip, |
306 | cur->bc_ino.whichfork); | |
60197e8d CH |
307 | |
308 | return xfs_bmbt_maxrecs(cur->bc_mp, | |
309 | ifp->if_broot_bytes, level == 0) / 2; | |
310 | } | |
311 | ||
312 | return cur->bc_mp->m_bmap_dmnr[level != 0]; | |
91cca5df CH |
313 | } |
314 | ||
60197e8d | 315 | int |
ce5e42db CH |
316 | xfs_bmbt_get_maxrecs( |
317 | struct xfs_btree_cur *cur, | |
318 | int level) | |
319 | { | |
60197e8d CH |
320 | if (level == cur->bc_nlevels - 1) { |
321 | struct xfs_ifork *ifp; | |
322 | ||
92219c29 DC |
323 | ifp = XFS_IFORK_PTR(cur->bc_ino.ip, |
324 | cur->bc_ino.whichfork); | |
60197e8d CH |
325 | |
326 | return xfs_bmbt_maxrecs(cur->bc_mp, | |
327 | ifp->if_broot_bytes, level == 0); | |
328 | } | |
329 | ||
330 | return cur->bc_mp->m_bmap_dmxr[level != 0]; | |
331 | ||
ce5e42db CH |
332 | } |
333 | ||
4b22a571 CH |
334 | /* |
335 | * Get the maximum records we could store in the on-disk format. | |
336 | * | |
337 | * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but | |
338 | * for the root node this checks the available space in the dinode fork | |
339 | * so that we can resize the in-memory buffer to match it. After a | |
340 | * resize to the maximum size this function returns the same value | |
341 | * as xfs_bmbt_get_maxrecs for the root node, too. | |
342 | */ | |
343 | STATIC int | |
344 | xfs_bmbt_get_dmaxrecs( | |
345 | struct xfs_btree_cur *cur, | |
346 | int level) | |
347 | { | |
60197e8d CH |
348 | if (level != cur->bc_nlevels - 1) |
349 | return cur->bc_mp->m_bmap_dmxr[level != 0]; | |
92219c29 | 350 | return xfs_bmdr_maxrecs(cur->bc_ino.forksize, level == 0); |
4b22a571 CH |
351 | } |
352 | ||
fe033cc8 CH |
353 | STATIC void |
354 | xfs_bmbt_init_key_from_rec( | |
23825cd1 DW |
355 | union xfs_btree_key *key, |
356 | const union xfs_btree_rec *rec) | |
fe033cc8 CH |
357 | { |
358 | key->bmbt.br_startoff = | |
359 | cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt)); | |
360 | } | |
361 | ||
118bb47e DW |
362 | STATIC void |
363 | xfs_bmbt_init_high_key_from_rec( | |
23825cd1 DW |
364 | union xfs_btree_key *key, |
365 | const union xfs_btree_rec *rec) | |
118bb47e DW |
366 | { |
367 | key->bmbt.br_startoff = cpu_to_be64( | |
368 | xfs_bmbt_disk_get_startoff(&rec->bmbt) + | |
369 | xfs_bmbt_disk_get_blockcount(&rec->bmbt) - 1); | |
370 | } | |
371 | ||
4b22a571 CH |
372 | STATIC void |
373 | xfs_bmbt_init_rec_from_cur( | |
374 | struct xfs_btree_cur *cur, | |
375 | union xfs_btree_rec *rec) | |
376 | { | |
377 | xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b); | |
378 | } | |
379 | ||
fe033cc8 CH |
380 | STATIC void |
381 | xfs_bmbt_init_ptr_from_cur( | |
382 | struct xfs_btree_cur *cur, | |
383 | union xfs_btree_ptr *ptr) | |
384 | { | |
385 | ptr->l = 0; | |
386 | } | |
387 | ||
c8ce540d | 388 | STATIC int64_t |
fe033cc8 | 389 | xfs_bmbt_key_diff( |
d29d5577 DW |
390 | struct xfs_btree_cur *cur, |
391 | const union xfs_btree_key *key) | |
fe033cc8 | 392 | { |
c8ce540d | 393 | return (int64_t)be64_to_cpu(key->bmbt.br_startoff) - |
fe033cc8 CH |
394 | cur->bc_rec.b.br_startoff; |
395 | } | |
396 | ||
118bb47e DW |
397 | STATIC int64_t |
398 | xfs_bmbt_diff_two_keys( | |
d29d5577 DW |
399 | struct xfs_btree_cur *cur, |
400 | const union xfs_btree_key *k1, | |
401 | const union xfs_btree_key *k2) | |
118bb47e | 402 | { |
d29d5577 DW |
403 | uint64_t a = be64_to_cpu(k1->bmbt.br_startoff); |
404 | uint64_t b = be64_to_cpu(k2->bmbt.br_startoff); | |
b521c890 DW |
405 | |
406 | /* | |
407 | * Note: This routine previously casted a and b to int64 and subtracted | |
408 | * them to generate a result. This lead to problems if b was the | |
409 | * "maximum" key value (all ones) being signed incorrectly, hence this | |
410 | * somewhat less efficient version. | |
411 | */ | |
412 | if (a > b) | |
413 | return 1; | |
414 | if (b > a) | |
415 | return -1; | |
416 | return 0; | |
118bb47e DW |
417 | } |
418 | ||
a6a781a5 | 419 | static xfs_failaddr_t |
612cfbfe | 420 | xfs_bmbt_verify( |
3d3e6f64 DC |
421 | struct xfs_buf *bp) |
422 | { | |
dbd329f1 | 423 | struct xfs_mount *mp = bp->b_mount; |
3d3e6f64 | 424 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
a6a781a5 | 425 | xfs_failaddr_t fa; |
3d3e6f64 | 426 | unsigned int level; |
3d3e6f64 | 427 | |
39708c20 BF |
428 | if (!xfs_verify_magic(bp, block->bb_magic)) |
429 | return __this_address; | |
430 | ||
ebd9027d | 431 | if (xfs_has_crc(mp)) { |
ee1a47ab CH |
432 | /* |
433 | * XXX: need a better way of verifying the owner here. Right now | |
434 | * just make sure there has been one set. | |
435 | */ | |
a6a781a5 DW |
436 | fa = xfs_btree_lblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN); |
437 | if (fa) | |
438 | return fa; | |
ee1a47ab CH |
439 | } |
440 | ||
441 | /* | |
442 | * numrecs and level verification. | |
3d3e6f64 | 443 | * |
ee1a47ab | 444 | * We don't know what fork we belong to, so just verify that the level |
3d3e6f64 DC |
445 | * is less than the maximum of the two. Later checks will be more |
446 | * precise. | |
447 | */ | |
448 | level = be16_to_cpu(block->bb_level); | |
ee1a47ab | 449 | if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1])) |
a6a781a5 | 450 | return __this_address; |
ee1a47ab | 451 | |
8368a601 | 452 | return xfs_btree_lblock_verify(bp, mp->m_bmap_dmxr[level != 0]); |
612cfbfe | 453 | } |
3d3e6f64 | 454 | |
1813dd64 DC |
455 | static void |
456 | xfs_bmbt_read_verify( | |
612cfbfe DC |
457 | struct xfs_buf *bp) |
458 | { | |
bc1a09b8 DW |
459 | xfs_failaddr_t fa; |
460 | ||
ce5028cf | 461 | if (!xfs_btree_lblock_verify_crc(bp)) |
bc1a09b8 DW |
462 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
463 | else { | |
464 | fa = xfs_bmbt_verify(bp); | |
465 | if (fa) | |
466 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
467 | } | |
ce5028cf | 468 | |
31ca03c9 | 469 | if (bp->b_error) |
ce5028cf | 470 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
612cfbfe DC |
471 | } |
472 | ||
1813dd64 DC |
473 | static void |
474 | xfs_bmbt_write_verify( | |
612cfbfe DC |
475 | struct xfs_buf *bp) |
476 | { | |
bc1a09b8 DW |
477 | xfs_failaddr_t fa; |
478 | ||
479 | fa = xfs_bmbt_verify(bp); | |
480 | if (fa) { | |
ee1a47ab | 481 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
bc1a09b8 | 482 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
ee1a47ab CH |
483 | return; |
484 | } | |
485 | xfs_btree_lblock_calc_crc(bp); | |
3d3e6f64 DC |
486 | } |
487 | ||
1813dd64 | 488 | const struct xfs_buf_ops xfs_bmbt_buf_ops = { |
233135b7 | 489 | .name = "xfs_bmbt", |
39708c20 BF |
490 | .magic = { cpu_to_be32(XFS_BMAP_MAGIC), |
491 | cpu_to_be32(XFS_BMAP_CRC_MAGIC) }, | |
1813dd64 DC |
492 | .verify_read = xfs_bmbt_read_verify, |
493 | .verify_write = xfs_bmbt_write_verify, | |
b5572597 | 494 | .verify_struct = xfs_bmbt_verify, |
1813dd64 DC |
495 | }; |
496 | ||
497 | ||
4a26e66e CH |
498 | STATIC int |
499 | xfs_bmbt_keys_inorder( | |
8e38dc88 DW |
500 | struct xfs_btree_cur *cur, |
501 | const union xfs_btree_key *k1, | |
502 | const union xfs_btree_key *k2) | |
4a26e66e CH |
503 | { |
504 | return be64_to_cpu(k1->bmbt.br_startoff) < | |
505 | be64_to_cpu(k2->bmbt.br_startoff); | |
506 | } | |
507 | ||
508 | STATIC int | |
509 | xfs_bmbt_recs_inorder( | |
8e38dc88 DW |
510 | struct xfs_btree_cur *cur, |
511 | const union xfs_btree_rec *r1, | |
512 | const union xfs_btree_rec *r2) | |
4a26e66e CH |
513 | { |
514 | return xfs_bmbt_disk_get_startoff(&r1->bmbt) + | |
515 | xfs_bmbt_disk_get_blockcount(&r1->bmbt) <= | |
516 | xfs_bmbt_disk_get_startoff(&r2->bmbt); | |
517 | } | |
4a26e66e | 518 | |
561f7d17 | 519 | static const struct xfs_btree_ops xfs_bmbt_ops = { |
65f1eaea CH |
520 | .rec_len = sizeof(xfs_bmbt_rec_t), |
521 | .key_len = sizeof(xfs_bmbt_key_t), | |
522 | ||
561f7d17 | 523 | .dup_cursor = xfs_bmbt_dup_cursor, |
4b22a571 | 524 | .update_cursor = xfs_bmbt_update_cursor, |
f5eb8e7c | 525 | .alloc_block = xfs_bmbt_alloc_block, |
d4b3a4b7 | 526 | .free_block = xfs_bmbt_free_block, |
ce5e42db | 527 | .get_maxrecs = xfs_bmbt_get_maxrecs, |
91cca5df | 528 | .get_minrecs = xfs_bmbt_get_minrecs, |
4b22a571 | 529 | .get_dmaxrecs = xfs_bmbt_get_dmaxrecs, |
fe033cc8 | 530 | .init_key_from_rec = xfs_bmbt_init_key_from_rec, |
118bb47e | 531 | .init_high_key_from_rec = xfs_bmbt_init_high_key_from_rec, |
4b22a571 | 532 | .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, |
fe033cc8 CH |
533 | .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur, |
534 | .key_diff = xfs_bmbt_key_diff, | |
118bb47e | 535 | .diff_two_keys = xfs_bmbt_diff_two_keys, |
1813dd64 | 536 | .buf_ops = &xfs_bmbt_buf_ops, |
4a26e66e CH |
537 | .keys_inorder = xfs_bmbt_keys_inorder, |
538 | .recs_inorder = xfs_bmbt_recs_inorder, | |
561f7d17 CH |
539 | }; |
540 | ||
541 | /* | |
542 | * Allocate a new bmap btree cursor. | |
543 | */ | |
544 | struct xfs_btree_cur * /* new bmap btree cursor */ | |
545 | xfs_bmbt_init_cursor( | |
546 | struct xfs_mount *mp, /* file system mount point */ | |
547 | struct xfs_trans *tp, /* transaction pointer */ | |
548 | struct xfs_inode *ip, /* inode owning the btree */ | |
549 | int whichfork) /* data or attr fork */ | |
550 | { | |
551 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
552 | struct xfs_btree_cur *cur; | |
3993baeb | 553 | ASSERT(whichfork != XFS_COW_FORK); |
561f7d17 | 554 | |
32a2b11f | 555 | cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL); |
561f7d17 CH |
556 | |
557 | cur->bc_tp = tp; | |
558 | cur->bc_mp = mp; | |
559 | cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1; | |
560 | cur->bc_btnum = XFS_BTNUM_BMAP; | |
561 | cur->bc_blocklog = mp->m_sb.sb_blocklog; | |
11ef38af | 562 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_bmbt_2); |
561f7d17 CH |
563 | |
564 | cur->bc_ops = &xfs_bmbt_ops; | |
e99ab90d | 565 | cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE; |
38c26bfd | 566 | if (xfs_has_crc(mp)) |
ee1a47ab | 567 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; |
561f7d17 | 568 | |
92219c29 DC |
569 | cur->bc_ino.forksize = XFS_IFORK_SIZE(ip, whichfork); |
570 | cur->bc_ino.ip = ip; | |
571 | cur->bc_ino.allocated = 0; | |
572 | cur->bc_ino.flags = 0; | |
573 | cur->bc_ino.whichfork = whichfork; | |
561f7d17 CH |
574 | |
575 | return cur; | |
576 | } | |
60197e8d CH |
577 | |
578 | /* | |
579 | * Calculate number of records in a bmap btree block. | |
580 | */ | |
581 | int | |
582 | xfs_bmbt_maxrecs( | |
583 | struct xfs_mount *mp, | |
584 | int blocklen, | |
585 | int leaf) | |
586 | { | |
7cc95a82 | 587 | blocklen -= XFS_BMBT_BLOCK_LEN(mp); |
60197e8d CH |
588 | |
589 | if (leaf) | |
590 | return blocklen / sizeof(xfs_bmbt_rec_t); | |
591 | return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t)); | |
592 | } | |
593 | ||
594 | /* | |
595 | * Calculate number of records in a bmap btree inode root. | |
596 | */ | |
597 | int | |
598 | xfs_bmdr_maxrecs( | |
60197e8d CH |
599 | int blocklen, |
600 | int leaf) | |
601 | { | |
602 | blocklen -= sizeof(xfs_bmdr_block_t); | |
603 | ||
604 | if (leaf) | |
605 | return blocklen / sizeof(xfs_bmdr_rec_t); | |
606 | return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t)); | |
607 | } | |
21b5c978 DC |
608 | |
609 | /* | |
610 | * Change the owner of a btree format fork fo the inode passed in. Change it to | |
611 | * the owner of that is passed in so that we can change owners before or after | |
612 | * we switch forks between inodes. The operation that the caller is doing will | |
613 | * determine whether is needs to change owner before or after the switch. | |
614 | * | |
638f4416 DC |
615 | * For demand paged transactional modification, the fork switch should be done |
616 | * after reading in all the blocks, modifying them and pinning them in the | |
617 | * transaction. For modification when the buffers are already pinned in memory, | |
618 | * the fork switch can be done before changing the owner as we won't need to | |
619 | * validate the owner until the btree buffers are unpinned and writes can occur | |
620 | * again. | |
621 | * | |
622 | * For recovery based ownership change, there is no transactional context and | |
623 | * so a buffer list must be supplied so that we can record the buffers that we | |
624 | * modified for the caller to issue IO on. | |
21b5c978 DC |
625 | */ |
626 | int | |
627 | xfs_bmbt_change_owner( | |
628 | struct xfs_trans *tp, | |
629 | struct xfs_inode *ip, | |
630 | int whichfork, | |
638f4416 DC |
631 | xfs_ino_t new_owner, |
632 | struct list_head *buffer_list) | |
21b5c978 DC |
633 | { |
634 | struct xfs_btree_cur *cur; | |
635 | int error; | |
636 | ||
638f4416 DC |
637 | ASSERT(tp || buffer_list); |
638 | ASSERT(!(tp && buffer_list)); | |
f7e67b20 | 639 | ASSERT(XFS_IFORK_PTR(ip, whichfork)->if_format == XFS_DINODE_FMT_BTREE); |
21b5c978 DC |
640 | |
641 | cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork); | |
8ef54797 | 642 | cur->bc_ino.flags |= XFS_BTCUR_BMBT_INVALID_OWNER; |
638f4416 DC |
643 | |
644 | error = xfs_btree_change_owner(cur, new_owner, buffer_list); | |
0b04b6b8 | 645 | xfs_btree_del_cursor(cur, error); |
21b5c978 DC |
646 | return error; |
647 | } | |
14861c47 DW |
648 | |
649 | /* Calculate the bmap btree size for some records. */ | |
650 | unsigned long long | |
651 | xfs_bmbt_calc_size( | |
652 | struct xfs_mount *mp, | |
653 | unsigned long long len) | |
654 | { | |
655 | return xfs_btree_calc_size(mp->m_bmap_dmnr, len); | |
656 | } |