]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/xfs/xfs_bmap_btree.c
xfs: decouple log and transaction headers
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / xfs_bmap_btree.c
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_bit.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_alloc.h"
35 #include "xfs_btree.h"
36 #include "xfs_itable.h"
37 #include "xfs_bmap.h"
38 #include "xfs_error.h"
39 #include "xfs_quota.h"
40 #include "xfs_trace.h"
41 #include "xfs_cksum.h"
42
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
59 /*
60 * Convert on-disk form of btree root to in-memory form.
61 */
62 void
63 xfs_bmdr_to_bmbt(
64 struct xfs_inode *ip,
65 xfs_bmdr_block_t *dblock,
66 int dblocklen,
67 struct xfs_btree_block *rblock,
68 int rblocklen)
69 {
70 struct xfs_mount *mp = ip->i_mount;
71 int dmxr;
72 xfs_bmbt_key_t *fkp;
73 __be64 *fpp;
74 xfs_bmbt_key_t *tkp;
75 __be64 *tpp;
76
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
86 rblock->bb_level = dblock->bb_level;
87 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
88 rblock->bb_numrecs = dblock->bb_numrecs;
89 dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
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);
93 tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
94 dmxr = be16_to_cpu(dblock->bb_numrecs);
95 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
96 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
97 }
98
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 */
104 STATIC void
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 &
115 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
116 #if XFS_BIG_BLKNOS
117 s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) |
118 (((xfs_fsblock_t)l1) >> 21);
119 #else
120 #ifdef DEBUG
121 {
122 xfs_dfsbno_t b;
123
124 b = (((xfs_dfsbno_t)l0 & xfs_mask64lo(9)) << 43) |
125 (((xfs_dfsbno_t)l1) >> 21);
126 ASSERT((b >> 32) == 0 || isnulldstartblock(b));
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 */
133 s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21));
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(
145 xfs_bmbt_rec_host_t *r,
146 xfs_bmbt_irec_t *s)
147 {
148 __xfs_bmbt_get_all(r->l0, r->l1, s);
149 }
150
151 /*
152 * Extract the blockcount field from an in memory bmap extent record.
153 */
154 xfs_filblks_t
155 xfs_bmbt_get_blockcount(
156 xfs_bmbt_rec_host_t *r)
157 {
158 return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21));
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(
166 xfs_bmbt_rec_host_t *r)
167 {
168 #if XFS_BIG_BLKNOS
169 return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) |
170 (((xfs_fsblock_t)r->l1) >> 21);
171 #else
172 #ifdef DEBUG
173 xfs_dfsbno_t b;
174
175 b = (((xfs_dfsbno_t)r->l0 & xfs_mask64lo(9)) << 43) |
176 (((xfs_dfsbno_t)r->l1) >> 21);
177 ASSERT((b >> 32) == 0 || isnulldstartblock(b));
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(
190 xfs_bmbt_rec_host_t *r)
191 {
192 return ((xfs_fileoff_t)r->l0 &
193 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
194 }
195
196 xfs_exntst_t
197 xfs_bmbt_get_state(
198 xfs_bmbt_rec_host_t *r)
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
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 {
214 return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
215 }
216
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 {
224 return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
225 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
226 }
227
228
229 /*
230 * Set all the fields in a bmap extent record from the arguments.
231 */
232 void
233 xfs_bmbt_set_allf(
234 xfs_bmbt_rec_host_t *r,
235 xfs_fileoff_t startoff,
236 xfs_fsblock_t startblock,
237 xfs_filblks_t blockcount,
238 xfs_exntst_t state)
239 {
240 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
241
242 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
243 ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0);
244 ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
245
246 #if XFS_BIG_BLKNOS
247 ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0);
248
249 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
250 ((xfs_bmbt_rec_base_t)startoff << 9) |
251 ((xfs_bmbt_rec_base_t)startblock >> 43);
252 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
253 ((xfs_bmbt_rec_base_t)blockcount &
254 (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
255 #else /* !XFS_BIG_BLKNOS */
256 if (isnullstartblock(startblock)) {
257 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
258 ((xfs_bmbt_rec_base_t)startoff << 9) |
259 (xfs_bmbt_rec_base_t)xfs_mask64lo(9);
260 r->l1 = xfs_mask64hi(11) |
261 ((xfs_bmbt_rec_base_t)startblock << 21) |
262 ((xfs_bmbt_rec_base_t)blockcount &
263 (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
264 } else {
265 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
266 ((xfs_bmbt_rec_base_t)startoff << 9);
267 r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
268 ((xfs_bmbt_rec_base_t)blockcount &
269 (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
270 }
271 #endif /* XFS_BIG_BLKNOS */
272 }
273
274 /*
275 * Set all the fields in a bmap extent record from the uncompressed form.
276 */
277 void
278 xfs_bmbt_set_all(
279 xfs_bmbt_rec_host_t *r,
280 xfs_bmbt_irec_t *s)
281 {
282 xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
283 s->br_blockcount, s->br_state);
284 }
285
286
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(
292 xfs_bmbt_rec_t *r,
293 xfs_fileoff_t startoff,
294 xfs_fsblock_t startblock,
295 xfs_filblks_t blockcount,
296 xfs_exntst_t state)
297 {
298 int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
299
300 ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
301 ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0);
302 ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
303
304 #if XFS_BIG_BLKNOS
305 ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0);
306
307 r->l0 = cpu_to_be64(
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));
311 r->l1 = cpu_to_be64(
312 ((xfs_bmbt_rec_base_t)startblock << 21) |
313 ((xfs_bmbt_rec_base_t)blockcount &
314 (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
315 #else /* !XFS_BIG_BLKNOS */
316 if (isnullstartblock(startblock)) {
317 r->l0 = cpu_to_be64(
318 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
319 ((xfs_bmbt_rec_base_t)startoff << 9) |
320 (xfs_bmbt_rec_base_t)xfs_mask64lo(9));
321 r->l1 = cpu_to_be64(xfs_mask64hi(11) |
322 ((xfs_bmbt_rec_base_t)startblock << 21) |
323 ((xfs_bmbt_rec_base_t)blockcount &
324 (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
325 } else {
326 r->l0 = cpu_to_be64(
327 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
328 ((xfs_bmbt_rec_base_t)startoff << 9));
329 r->l1 = cpu_to_be64(
330 ((xfs_bmbt_rec_base_t)startblock << 21) |
331 ((xfs_bmbt_rec_base_t)blockcount &
332 (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
333 }
334 #endif /* XFS_BIG_BLKNOS */
335 }
336
337 /*
338 * Set all the fields in a bmap extent record from the uncompressed form.
339 */
340 STATIC void
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 }
348
349 /*
350 * Set the blockcount field in a bmap extent record.
351 */
352 void
353 xfs_bmbt_set_blockcount(
354 xfs_bmbt_rec_host_t *r,
355 xfs_filblks_t v)
356 {
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));
360 }
361
362 /*
363 * Set the startblock field in a bmap extent record.
364 */
365 void
366 xfs_bmbt_set_startblock(
367 xfs_bmbt_rec_host_t *r,
368 xfs_fsblock_t v)
369 {
370 #if XFS_BIG_BLKNOS
371 ASSERT((v & xfs_mask64hi(12)) == 0);
372 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) |
373 (xfs_bmbt_rec_base_t)(v >> 43);
374 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) |
375 (xfs_bmbt_rec_base_t)(v << 21);
376 #else /* !XFS_BIG_BLKNOS */
377 if (isnullstartblock(v)) {
378 r->l0 |= (xfs_bmbt_rec_base_t)xfs_mask64lo(9);
379 r->l1 = (xfs_bmbt_rec_base_t)xfs_mask64hi(11) |
380 ((xfs_bmbt_rec_base_t)v << 21) |
381 (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
382 } else {
383 r->l0 &= ~(xfs_bmbt_rec_base_t)xfs_mask64lo(9);
384 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
385 (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
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(
395 xfs_bmbt_rec_host_t *r,
396 xfs_fileoff_t v)
397 {
398 ASSERT((v & xfs_mask64hi(9)) == 0);
399 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) |
400 ((xfs_bmbt_rec_base_t)v << 9) |
401 (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9));
402 }
403
404 /*
405 * Set the extent state field in a bmap extent record.
406 */
407 void
408 xfs_bmbt_set_state(
409 xfs_bmbt_rec_host_t *r,
410 xfs_exntst_t v)
411 {
412 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
413 if (v == XFS_EXT_NORM)
414 r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN);
415 else
416 r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN);
417 }
418
419 /*
420 * Convert in-memory form of btree root to on-disk form.
421 */
422 void
423 xfs_bmbt_to_bmdr(
424 struct xfs_mount *mp,
425 struct xfs_btree_block *rblock,
426 int rblocklen,
427 xfs_bmdr_block_t *dblock,
428 int dblocklen)
429 {
430 int dmxr;
431 xfs_bmbt_key_t *fkp;
432 __be64 *fpp;
433 xfs_bmbt_key_t *tkp;
434 __be64 *tpp;
435
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));
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);
446 dblock->bb_level = rblock->bb_level;
447 dblock->bb_numrecs = rblock->bb_numrecs;
448 dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
449 fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
450 tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
451 fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
452 tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
453 dmxr = be16_to_cpu(dblock->bb_numrecs);
454 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
455 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
456 }
457
458 /*
459 * Check extent records, which have just been read, for
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(
468 xfs_ifork_t *ifp,
469 xfs_extnum_t idx,
470 xfs_extnum_t num)
471 {
472 for (; num > 0; num--, idx++) {
473 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
474 if ((ep->l0 >>
475 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
476 ASSERT(0);
477 return 1;
478 }
479 }
480 return 0;
481 }
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
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
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);
591 xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip,
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
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);
619 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
620 xfs_trans_binval(tp, bp);
621 return 0;
622 }
623
624 STATIC int
625 xfs_bmbt_get_minrecs(
626 struct xfs_btree_cur *cur,
627 int level)
628 {
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];
640 }
641
642 int
643 xfs_bmbt_get_maxrecs(
644 struct xfs_btree_cur *cur,
645 int level)
646 {
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
659 }
660
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 {
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);
679 }
680
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
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
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
726 static bool
727 xfs_bmbt_verify(
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;
733
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.
757 *
758 * We don't know what fork we belong to, so just verify that the level
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);
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;
767
768 /* sibling pointer verification */
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;
779 }
780
781 static void
782 xfs_bmbt_read_verify(
783 struct xfs_buf *bp)
784 {
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 }
792 }
793
794 static void
795 xfs_bmbt_write_verify(
796 struct xfs_buf *bp)
797 {
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);
807 }
808
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
815 #if defined(DEBUG) || defined(XFS_WARN)
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
838 static const struct xfs_btree_ops xfs_bmbt_ops = {
839 .rec_len = sizeof(xfs_bmbt_rec_t),
840 .key_len = sizeof(xfs_bmbt_key_t),
841
842 .dup_cursor = xfs_bmbt_dup_cursor,
843 .update_cursor = xfs_bmbt_update_cursor,
844 .alloc_block = xfs_bmbt_alloc_block,
845 .free_block = xfs_bmbt_free_block,
846 .get_maxrecs = xfs_bmbt_get_maxrecs,
847 .get_minrecs = xfs_bmbt_get_minrecs,
848 .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
849 .init_key_from_rec = xfs_bmbt_init_key_from_rec,
850 .init_rec_from_key = xfs_bmbt_init_rec_from_key,
851 .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
852 .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
853 .key_diff = xfs_bmbt_key_diff,
854 .buf_ops = &xfs_bmbt_buf_ops,
855 #if defined(DEBUG) || defined(XFS_WARN)
856 .keys_inorder = xfs_bmbt_keys_inorder,
857 .recs_inorder = xfs_bmbt_recs_inorder,
858 #endif
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;
883 cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
884 if (xfs_sb_version_hascrc(&mp->m_sb))
885 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
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 }
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 {
907 blocklen -= XFS_BMBT_BLOCK_LEN(mp);
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 }
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 *
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.
946 */
947 int
948 xfs_bmbt_change_owner(
949 struct xfs_trans *tp,
950 struct xfs_inode *ip,
951 int whichfork,
952 xfs_ino_t new_owner,
953 struct list_head *buffer_list)
954 {
955 struct xfs_btree_cur *cur;
956 int error;
957
958 ASSERT(tp || buffer_list);
959 ASSERT(!(tp && buffer_list));
960 if (whichfork == XFS_DATA_FORK)
961 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE);
962 else
963 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE);
964
965 cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
966 if (!cur)
967 return ENOMEM;
968
969 error = xfs_btree_change_owner(cur, new_owner, buffer_list);
970 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
971 return error;
972 }