Mention branches and keyring.
[releases.git] / libxfs / xfs_bmap_btree.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_alloc.h"
17 #include "xfs_btree.h"
18 #include "xfs_btree_staging.h"
19 #include "xfs_bmap_btree.h"
20 #include "xfs_bmap.h"
21 #include "xfs_error.h"
22 #include "xfs_quota.h"
23 #include "xfs_trace.h"
24 #include "xfs_rmap.h"
25 #include "xfs_ag.h"
26
27 static struct kmem_cache        *xfs_bmbt_cur_cache;
28
29 /*
30  * Convert on-disk form of btree root to in-memory form.
31  */
32 void
33 xfs_bmdr_to_bmbt(
34         struct xfs_inode        *ip,
35         xfs_bmdr_block_t        *dblock,
36         int                     dblocklen,
37         struct xfs_btree_block  *rblock,
38         int                     rblocklen)
39 {
40         struct xfs_mount        *mp = ip->i_mount;
41         int                     dmxr;
42         xfs_bmbt_key_t          *fkp;
43         __be64                  *fpp;
44         xfs_bmbt_key_t          *tkp;
45         __be64                  *tpp;
46
47         xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL,
48                                  XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
49                                  XFS_BTREE_LONG_PTRS);
50         rblock->bb_level = dblock->bb_level;
51         ASSERT(be16_to_cpu(rblock->bb_level) > 0);
52         rblock->bb_numrecs = dblock->bb_numrecs;
53         dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
54         fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
55         tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
56         fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
57         tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
58         dmxr = be16_to_cpu(dblock->bb_numrecs);
59         memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
60         memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
61 }
62
63 void
64 xfs_bmbt_disk_get_all(
65         const struct xfs_bmbt_rec *rec,
66         struct xfs_bmbt_irec    *irec)
67 {
68         uint64_t                l0 = get_unaligned_be64(&rec->l0);
69         uint64_t                l1 = get_unaligned_be64(&rec->l1);
70
71         irec->br_startoff = (l0 & xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
72         irec->br_startblock = ((l0 & xfs_mask64lo(9)) << 43) | (l1 >> 21);
73         irec->br_blockcount = l1 & xfs_mask64lo(21);
74         if (l0 >> (64 - BMBT_EXNTFLAG_BITLEN))
75                 irec->br_state = XFS_EXT_UNWRITTEN;
76         else
77                 irec->br_state = XFS_EXT_NORM;
78 }
79
80 /*
81  * Extract the blockcount field from an on disk bmap extent record.
82  */
83 xfs_filblks_t
84 xfs_bmbt_disk_get_blockcount(
85         const struct xfs_bmbt_rec       *r)
86 {
87         return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
88 }
89
90 /*
91  * Extract the startoff field from a disk format bmap extent record.
92  */
93 xfs_fileoff_t
94 xfs_bmbt_disk_get_startoff(
95         const struct xfs_bmbt_rec       *r)
96 {
97         return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
98                  xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
99 }
100
101 /*
102  * Set all the fields in a bmap extent record from the uncompressed form.
103  */
104 void
105 xfs_bmbt_disk_set_all(
106         struct xfs_bmbt_rec     *r,
107         struct xfs_bmbt_irec    *s)
108 {
109         int                     extent_flag = (s->br_state != XFS_EXT_NORM);
110
111         ASSERT(s->br_state == XFS_EXT_NORM || s->br_state == XFS_EXT_UNWRITTEN);
112         ASSERT(!(s->br_startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)));
113         ASSERT(!(s->br_blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)));
114         ASSERT(!(s->br_startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)));
115
116         put_unaligned_be64(
117                 ((xfs_bmbt_rec_base_t)extent_flag << 63) |
118                  ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
119                  ((xfs_bmbt_rec_base_t)s->br_startblock >> 43), &r->l0);
120         put_unaligned_be64(
121                 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
122                  ((xfs_bmbt_rec_base_t)s->br_blockcount &
123                   (xfs_bmbt_rec_base_t)xfs_mask64lo(21)), &r->l1);
124 }
125
126 /*
127  * Convert in-memory form of btree root to on-disk form.
128  */
129 void
130 xfs_bmbt_to_bmdr(
131         struct xfs_mount        *mp,
132         struct xfs_btree_block  *rblock,
133         int                     rblocklen,
134         xfs_bmdr_block_t        *dblock,
135         int                     dblocklen)
136 {
137         int                     dmxr;
138         xfs_bmbt_key_t          *fkp;
139         __be64                  *fpp;
140         xfs_bmbt_key_t          *tkp;
141         __be64                  *tpp;
142
143         if (xfs_has_crc(mp)) {
144                 ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC));
145                 ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid,
146                        &mp->m_sb.sb_meta_uuid));
147                 ASSERT(rblock->bb_u.l.bb_blkno ==
148                        cpu_to_be64(XFS_BUF_DADDR_NULL));
149         } else
150                 ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC));
151         ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK));
152         ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK));
153         ASSERT(rblock->bb_level != 0);
154         dblock->bb_level = rblock->bb_level;
155         dblock->bb_numrecs = rblock->bb_numrecs;
156         dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
157         fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
158         tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
159         fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
160         tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
161         dmxr = be16_to_cpu(dblock->bb_numrecs);
162         memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
163         memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
164 }
165
166 STATIC struct xfs_btree_cur *
167 xfs_bmbt_dup_cursor(
168         struct xfs_btree_cur    *cur)
169 {
170         struct xfs_btree_cur    *new;
171
172         new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
173                         cur->bc_ino.ip, cur->bc_ino.whichfork);
174
175         /*
176          * Copy the firstblock, dfops, and flags values,
177          * since init cursor doesn't get them.
178          */
179         new->bc_ino.flags = cur->bc_ino.flags;
180
181         return new;
182 }
183
184 STATIC void
185 xfs_bmbt_update_cursor(
186         struct xfs_btree_cur    *src,
187         struct xfs_btree_cur    *dst)
188 {
189         ASSERT((dst->bc_tp->t_highest_agno != NULLAGNUMBER) ||
190                (dst->bc_ino.ip->i_diflags & XFS_DIFLAG_REALTIME));
191
192         dst->bc_ino.allocated += src->bc_ino.allocated;
193         dst->bc_tp->t_highest_agno = src->bc_tp->t_highest_agno;
194
195         src->bc_ino.allocated = 0;
196 }
197
198 STATIC int
199 xfs_bmbt_alloc_block(
200         struct xfs_btree_cur            *cur,
201         const union xfs_btree_ptr       *start,
202         union xfs_btree_ptr             *new,
203         int                             *stat)
204 {
205         struct xfs_alloc_arg    args;
206         int                     error;
207
208         memset(&args, 0, sizeof(args));
209         args.tp = cur->bc_tp;
210         args.mp = cur->bc_mp;
211         xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_ino.ip->i_ino,
212                         cur->bc_ino.whichfork);
213         args.minlen = args.maxlen = args.prod = 1;
214         args.wasdel = cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL;
215         if (!args.wasdel && args.tp->t_blk_res == 0)
216                 return -ENOSPC;
217
218         /*
219          * If we are coming here from something like unwritten extent
220          * conversion, there has been no data extent allocation already done, so
221          * we have to ensure that we attempt to locate the entire set of bmbt
222          * allocations in the same AG, as xfs_bmapi_write() would have reserved.
223          */
224         if (cur->bc_tp->t_highest_agno == NULLAGNUMBER)
225                 args.minleft = xfs_bmapi_minleft(cur->bc_tp, cur->bc_ino.ip,
226                                         cur->bc_ino.whichfork);
227
228         error = xfs_alloc_vextent_start_ag(&args, be64_to_cpu(start->l));
229         if (error)
230                 return error;
231
232         if (args.fsbno == NULLFSBLOCK && args.minleft) {
233                 /*
234                  * Could not find an AG with enough free space to satisfy
235                  * a full btree split.  Try again and if
236                  * successful activate the lowspace algorithm.
237                  */
238                 args.minleft = 0;
239                 error = xfs_alloc_vextent_start_ag(&args, 0);
240                 if (error)
241                         return error;
242                 cur->bc_tp->t_flags |= XFS_TRANS_LOWMODE;
243         }
244         if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
245                 *stat = 0;
246                 return 0;
247         }
248
249         ASSERT(args.len == 1);
250         cur->bc_ino.allocated++;
251         cur->bc_ino.ip->i_nblocks++;
252         xfs_trans_log_inode(args.tp, cur->bc_ino.ip, XFS_ILOG_CORE);
253         xfs_trans_mod_dquot_byino(args.tp, cur->bc_ino.ip,
254                         XFS_TRANS_DQ_BCOUNT, 1L);
255
256         new->l = cpu_to_be64(args.fsbno);
257
258         *stat = 1;
259         return 0;
260 }
261
262 STATIC int
263 xfs_bmbt_free_block(
264         struct xfs_btree_cur    *cur,
265         struct xfs_buf          *bp)
266 {
267         struct xfs_mount        *mp = cur->bc_mp;
268         struct xfs_inode        *ip = cur->bc_ino.ip;
269         struct xfs_trans        *tp = cur->bc_tp;
270         xfs_fsblock_t           fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
271         struct xfs_owner_info   oinfo;
272         int                     error;
273
274         xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
275         error = xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo,
276                         XFS_AG_RESV_NONE, false);
277         if (error)
278                 return error;
279
280         ip->i_nblocks--;
281         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
282         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
283         return 0;
284 }
285
286 STATIC int
287 xfs_bmbt_get_minrecs(
288         struct xfs_btree_cur    *cur,
289         int                     level)
290 {
291         if (level == cur->bc_nlevels - 1) {
292                 struct xfs_ifork        *ifp = xfs_btree_ifork_ptr(cur);
293
294                 return xfs_bmbt_maxrecs(cur->bc_mp,
295                                         ifp->if_broot_bytes, level == 0) / 2;
296         }
297
298         return cur->bc_mp->m_bmap_dmnr[level != 0];
299 }
300
301 int
302 xfs_bmbt_get_maxrecs(
303         struct xfs_btree_cur    *cur,
304         int                     level)
305 {
306         if (level == cur->bc_nlevels - 1) {
307                 struct xfs_ifork        *ifp = xfs_btree_ifork_ptr(cur);
308
309                 return xfs_bmbt_maxrecs(cur->bc_mp,
310                                         ifp->if_broot_bytes, level == 0);
311         }
312
313         return cur->bc_mp->m_bmap_dmxr[level != 0];
314
315 }
316
317 /*
318  * Get the maximum records we could store in the on-disk format.
319  *
320  * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
321  * for the root node this checks the available space in the dinode fork
322  * so that we can resize the in-memory buffer to match it.  After a
323  * resize to the maximum size this function returns the same value
324  * as xfs_bmbt_get_maxrecs for the root node, too.
325  */
326 STATIC int
327 xfs_bmbt_get_dmaxrecs(
328         struct xfs_btree_cur    *cur,
329         int                     level)
330 {
331         if (level != cur->bc_nlevels - 1)
332                 return cur->bc_mp->m_bmap_dmxr[level != 0];
333         return xfs_bmdr_maxrecs(cur->bc_ino.forksize, level == 0);
334 }
335
336 STATIC void
337 xfs_bmbt_init_key_from_rec(
338         union xfs_btree_key             *key,
339         const union xfs_btree_rec       *rec)
340 {
341         key->bmbt.br_startoff =
342                 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
343 }
344
345 STATIC void
346 xfs_bmbt_init_high_key_from_rec(
347         union xfs_btree_key             *key,
348         const union xfs_btree_rec       *rec)
349 {
350         key->bmbt.br_startoff = cpu_to_be64(
351                         xfs_bmbt_disk_get_startoff(&rec->bmbt) +
352                         xfs_bmbt_disk_get_blockcount(&rec->bmbt) - 1);
353 }
354
355 STATIC void
356 xfs_bmbt_init_rec_from_cur(
357         struct xfs_btree_cur    *cur,
358         union xfs_btree_rec     *rec)
359 {
360         xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
361 }
362
363 STATIC void
364 xfs_bmbt_init_ptr_from_cur(
365         struct xfs_btree_cur    *cur,
366         union xfs_btree_ptr     *ptr)
367 {
368         ptr->l = 0;
369 }
370
371 STATIC int64_t
372 xfs_bmbt_key_diff(
373         struct xfs_btree_cur            *cur,
374         const union xfs_btree_key       *key)
375 {
376         return (int64_t)be64_to_cpu(key->bmbt.br_startoff) -
377                                       cur->bc_rec.b.br_startoff;
378 }
379
380 STATIC int64_t
381 xfs_bmbt_diff_two_keys(
382         struct xfs_btree_cur            *cur,
383         const union xfs_btree_key       *k1,
384         const union xfs_btree_key       *k2,
385         const union xfs_btree_key       *mask)
386 {
387         uint64_t                        a = be64_to_cpu(k1->bmbt.br_startoff);
388         uint64_t                        b = be64_to_cpu(k2->bmbt.br_startoff);
389
390         ASSERT(!mask || mask->bmbt.br_startoff);
391
392         /*
393          * Note: This routine previously casted a and b to int64 and subtracted
394          * them to generate a result.  This lead to problems if b was the
395          * "maximum" key value (all ones) being signed incorrectly, hence this
396          * somewhat less efficient version.
397          */
398         if (a > b)
399                 return 1;
400         if (b > a)
401                 return -1;
402         return 0;
403 }
404
405 static xfs_failaddr_t
406 xfs_bmbt_verify(
407         struct xfs_buf          *bp)
408 {
409         struct xfs_mount        *mp = bp->b_mount;
410         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
411         xfs_failaddr_t          fa;
412         unsigned int            level;
413
414         if (!xfs_verify_magic(bp, block->bb_magic))
415                 return __this_address;
416
417         if (xfs_has_crc(mp)) {
418                 /*
419                  * XXX: need a better way of verifying the owner here. Right now
420                  * just make sure there has been one set.
421                  */
422                 fa = xfs_btree_lblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
423                 if (fa)
424                         return fa;
425         }
426
427         /*
428          * numrecs and level verification.
429          *
430          * We don't know what fork we belong to, so just verify that the level
431          * is less than the maximum of the two. Later checks will be more
432          * precise.
433          */
434         level = be16_to_cpu(block->bb_level);
435         if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
436                 return __this_address;
437
438         return xfs_btree_lblock_verify(bp, mp->m_bmap_dmxr[level != 0]);
439 }
440
441 static void
442 xfs_bmbt_read_verify(
443         struct xfs_buf  *bp)
444 {
445         xfs_failaddr_t  fa;
446
447         if (!xfs_btree_lblock_verify_crc(bp))
448                 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
449         else {
450                 fa = xfs_bmbt_verify(bp);
451                 if (fa)
452                         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
453         }
454
455         if (bp->b_error)
456                 trace_xfs_btree_corrupt(bp, _RET_IP_);
457 }
458
459 static void
460 xfs_bmbt_write_verify(
461         struct xfs_buf  *bp)
462 {
463         xfs_failaddr_t  fa;
464
465         fa = xfs_bmbt_verify(bp);
466         if (fa) {
467                 trace_xfs_btree_corrupt(bp, _RET_IP_);
468                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
469                 return;
470         }
471         xfs_btree_lblock_calc_crc(bp);
472 }
473
474 const struct xfs_buf_ops xfs_bmbt_buf_ops = {
475         .name = "xfs_bmbt",
476         .magic = { cpu_to_be32(XFS_BMAP_MAGIC),
477                    cpu_to_be32(XFS_BMAP_CRC_MAGIC) },
478         .verify_read = xfs_bmbt_read_verify,
479         .verify_write = xfs_bmbt_write_verify,
480         .verify_struct = xfs_bmbt_verify,
481 };
482
483
484 STATIC int
485 xfs_bmbt_keys_inorder(
486         struct xfs_btree_cur            *cur,
487         const union xfs_btree_key       *k1,
488         const union xfs_btree_key       *k2)
489 {
490         return be64_to_cpu(k1->bmbt.br_startoff) <
491                 be64_to_cpu(k2->bmbt.br_startoff);
492 }
493
494 STATIC int
495 xfs_bmbt_recs_inorder(
496         struct xfs_btree_cur            *cur,
497         const union xfs_btree_rec       *r1,
498         const union xfs_btree_rec       *r2)
499 {
500         return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
501                 xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
502                 xfs_bmbt_disk_get_startoff(&r2->bmbt);
503 }
504
505 STATIC enum xbtree_key_contig
506 xfs_bmbt_keys_contiguous(
507         struct xfs_btree_cur            *cur,
508         const union xfs_btree_key       *key1,
509         const union xfs_btree_key       *key2,
510         const union xfs_btree_key       *mask)
511 {
512         ASSERT(!mask || mask->bmbt.br_startoff);
513
514         return xbtree_key_contig(be64_to_cpu(key1->bmbt.br_startoff),
515                                  be64_to_cpu(key2->bmbt.br_startoff));
516 }
517
518 static const struct xfs_btree_ops xfs_bmbt_ops = {
519         .rec_len                = sizeof(xfs_bmbt_rec_t),
520         .key_len                = sizeof(xfs_bmbt_key_t),
521
522         .dup_cursor             = xfs_bmbt_dup_cursor,
523         .update_cursor          = xfs_bmbt_update_cursor,
524         .alloc_block            = xfs_bmbt_alloc_block,
525         .free_block             = xfs_bmbt_free_block,
526         .get_maxrecs            = xfs_bmbt_get_maxrecs,
527         .get_minrecs            = xfs_bmbt_get_minrecs,
528         .get_dmaxrecs           = xfs_bmbt_get_dmaxrecs,
529         .init_key_from_rec      = xfs_bmbt_init_key_from_rec,
530         .init_high_key_from_rec = xfs_bmbt_init_high_key_from_rec,
531         .init_rec_from_cur      = xfs_bmbt_init_rec_from_cur,
532         .init_ptr_from_cur      = xfs_bmbt_init_ptr_from_cur,
533         .key_diff               = xfs_bmbt_key_diff,
534         .diff_two_keys          = xfs_bmbt_diff_two_keys,
535         .buf_ops                = &xfs_bmbt_buf_ops,
536         .keys_inorder           = xfs_bmbt_keys_inorder,
537         .recs_inorder           = xfs_bmbt_recs_inorder,
538         .keys_contiguous        = xfs_bmbt_keys_contiguous,
539 };
540
541 static struct xfs_btree_cur *
542 xfs_bmbt_init_common(
543         struct xfs_mount        *mp,
544         struct xfs_trans        *tp,
545         struct xfs_inode        *ip,
546         int                     whichfork)
547 {
548         struct xfs_btree_cur    *cur;
549
550         ASSERT(whichfork != XFS_COW_FORK);
551
552         cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_BMAP,
553                         mp->m_bm_maxlevels[whichfork], xfs_bmbt_cur_cache);
554         cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_bmbt_2);
555
556         cur->bc_ops = &xfs_bmbt_ops;
557         cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
558         if (xfs_has_crc(mp))
559                 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
560
561         cur->bc_ino.ip = ip;
562         cur->bc_ino.allocated = 0;
563         cur->bc_ino.flags = 0;
564
565         return cur;
566 }
567
568 /*
569  * Allocate a new bmap btree cursor.
570  */
571 struct xfs_btree_cur *
572 xfs_bmbt_init_cursor(
573         struct xfs_mount        *mp,
574         struct xfs_trans        *tp,
575         struct xfs_inode        *ip,
576         int                     whichfork)
577 {
578         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
579         struct xfs_btree_cur    *cur;
580
581         cur = xfs_bmbt_init_common(mp, tp, ip, whichfork);
582
583         cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
584         cur->bc_ino.forksize = xfs_inode_fork_size(ip, whichfork);
585         cur->bc_ino.whichfork = whichfork;
586
587         return cur;
588 }
589
590 /* Calculate number of records in a block mapping btree block. */
591 static inline unsigned int
592 xfs_bmbt_block_maxrecs(
593         unsigned int            blocklen,
594         bool                    leaf)
595 {
596         if (leaf)
597                 return blocklen / sizeof(xfs_bmbt_rec_t);
598         return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
599 }
600
601 /*
602  * Allocate a new bmap btree cursor for reloading an inode block mapping data
603  * structure.  Note that callers can use the staged cursor to reload extents
604  * format inode forks if they rebuild the iext tree and commit the staged
605  * cursor immediately.
606  */
607 struct xfs_btree_cur *
608 xfs_bmbt_stage_cursor(
609         struct xfs_mount        *mp,
610         struct xfs_inode        *ip,
611         struct xbtree_ifakeroot *ifake)
612 {
613         struct xfs_btree_cur    *cur;
614         struct xfs_btree_ops    *ops;
615
616         /* data fork always has larger maxheight */
617         cur = xfs_bmbt_init_common(mp, NULL, ip, XFS_DATA_FORK);
618         cur->bc_nlevels = ifake->if_levels;
619         cur->bc_ino.forksize = ifake->if_fork_size;
620
621         /* Don't let anyone think we're attached to the real fork yet. */
622         cur->bc_ino.whichfork = -1;
623         xfs_btree_stage_ifakeroot(cur, ifake, &ops);
624         ops->update_cursor = NULL;
625         return cur;
626 }
627
628 /*
629  * Swap in the new inode fork root.  Once we pass this point the newly rebuilt
630  * mappings are in place and we have to kill off any old btree blocks.
631  */
632 void
633 xfs_bmbt_commit_staged_btree(
634         struct xfs_btree_cur    *cur,
635         struct xfs_trans        *tp,
636         int                     whichfork)
637 {
638         struct xbtree_ifakeroot *ifake = cur->bc_ino.ifake;
639         struct xfs_ifork        *ifp;
640         static const short      brootflag[2] = {XFS_ILOG_DBROOT, XFS_ILOG_ABROOT};
641         static const short      extflag[2] = {XFS_ILOG_DEXT, XFS_ILOG_AEXT};
642         int                     flags = XFS_ILOG_CORE;
643
644         ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
645         ASSERT(whichfork != XFS_COW_FORK);
646
647         /*
648          * Free any resources hanging off the real fork, then shallow-copy the
649          * staging fork's contents into the real fork to transfer everything
650          * we just built.
651          */
652         ifp = xfs_ifork_ptr(cur->bc_ino.ip, whichfork);
653         xfs_idestroy_fork(ifp);
654         memcpy(ifp, ifake->if_fork, sizeof(struct xfs_ifork));
655
656         switch (ifp->if_format) {
657         case XFS_DINODE_FMT_EXTENTS:
658                 flags |= extflag[whichfork];
659                 break;
660         case XFS_DINODE_FMT_BTREE:
661                 flags |= brootflag[whichfork];
662                 break;
663         default:
664                 ASSERT(0);
665                 break;
666         }
667         xfs_trans_log_inode(tp, cur->bc_ino.ip, flags);
668         xfs_btree_commit_ifakeroot(cur, tp, whichfork, &xfs_bmbt_ops);
669 }
670
671 /*
672  * Calculate number of records in a bmap btree block.
673  */
674 int
675 xfs_bmbt_maxrecs(
676         struct xfs_mount        *mp,
677         int                     blocklen,
678         int                     leaf)
679 {
680         blocklen -= XFS_BMBT_BLOCK_LEN(mp);
681         return xfs_bmbt_block_maxrecs(blocklen, leaf);
682 }
683
684 /*
685  * Calculate the maximum possible height of the btree that the on-disk format
686  * supports. This is used for sizing structures large enough to support every
687  * possible configuration of a filesystem that might get mounted.
688  */
689 unsigned int
690 xfs_bmbt_maxlevels_ondisk(void)
691 {
692         unsigned int            minrecs[2];
693         unsigned int            blocklen;
694
695         blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
696                        XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
697
698         minrecs[0] = xfs_bmbt_block_maxrecs(blocklen, true) / 2;
699         minrecs[1] = xfs_bmbt_block_maxrecs(blocklen, false) / 2;
700
701         /* One extra level for the inode root. */
702         return xfs_btree_compute_maxlevels(minrecs,
703                         XFS_MAX_EXTCNT_DATA_FORK_LARGE) + 1;
704 }
705
706 /*
707  * Calculate number of records in a bmap btree inode root.
708  */
709 int
710 xfs_bmdr_maxrecs(
711         int                     blocklen,
712         int                     leaf)
713 {
714         blocklen -= sizeof(xfs_bmdr_block_t);
715
716         if (leaf)
717                 return blocklen / sizeof(xfs_bmdr_rec_t);
718         return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
719 }
720
721 /*
722  * Change the owner of a btree format fork fo the inode passed in. Change it to
723  * the owner of that is passed in so that we can change owners before or after
724  * we switch forks between inodes. The operation that the caller is doing will
725  * determine whether is needs to change owner before or after the switch.
726  *
727  * For demand paged transactional modification, the fork switch should be done
728  * after reading in all the blocks, modifying them and pinning them in the
729  * transaction. For modification when the buffers are already pinned in memory,
730  * the fork switch can be done before changing the owner as we won't need to
731  * validate the owner until the btree buffers are unpinned and writes can occur
732  * again.
733  *
734  * For recovery based ownership change, there is no transactional context and
735  * so a buffer list must be supplied so that we can record the buffers that we
736  * modified for the caller to issue IO on.
737  */
738 int
739 xfs_bmbt_change_owner(
740         struct xfs_trans        *tp,
741         struct xfs_inode        *ip,
742         int                     whichfork,
743         xfs_ino_t               new_owner,
744         struct list_head        *buffer_list)
745 {
746         struct xfs_btree_cur    *cur;
747         int                     error;
748
749         ASSERT(tp || buffer_list);
750         ASSERT(!(tp && buffer_list));
751         ASSERT(xfs_ifork_ptr(ip, whichfork)->if_format == XFS_DINODE_FMT_BTREE);
752
753         cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
754         cur->bc_ino.flags |= XFS_BTCUR_BMBT_INVALID_OWNER;
755
756         error = xfs_btree_change_owner(cur, new_owner, buffer_list);
757         xfs_btree_del_cursor(cur, error);
758         return error;
759 }
760
761 /* Calculate the bmap btree size for some records. */
762 unsigned long long
763 xfs_bmbt_calc_size(
764         struct xfs_mount        *mp,
765         unsigned long long      len)
766 {
767         return xfs_btree_calc_size(mp->m_bmap_dmnr, len);
768 }
769
770 int __init
771 xfs_bmbt_init_cur_cache(void)
772 {
773         xfs_bmbt_cur_cache = kmem_cache_create("xfs_bmbt_cur",
774                         xfs_btree_cur_sizeof(xfs_bmbt_maxlevels_ondisk()),
775                         0, 0, NULL);
776
777         if (!xfs_bmbt_cur_cache)
778                 return -ENOMEM;
779         return 0;
780 }
781
782 void
783 xfs_bmbt_destroy_cur_cache(void)
784 {
785         kmem_cache_destroy(xfs_bmbt_cur_cache);
786         xfs_bmbt_cur_cache = NULL;
787 }