18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Oracle. All Rights Reserved. 48c2ecf20Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#include "xfs.h" 78c2ecf20Sopenharmony_ci#include "xfs_fs.h" 88c2ecf20Sopenharmony_ci#include "xfs_shared.h" 98c2ecf20Sopenharmony_ci#include "xfs_format.h" 108c2ecf20Sopenharmony_ci#include "xfs_log_format.h" 118c2ecf20Sopenharmony_ci#include "xfs_trans_resv.h" 128c2ecf20Sopenharmony_ci#include "xfs_mount.h" 138c2ecf20Sopenharmony_ci#include "xfs_defer.h" 148c2ecf20Sopenharmony_ci#include "xfs_btree.h" 158c2ecf20Sopenharmony_ci#include "xfs_bmap.h" 168c2ecf20Sopenharmony_ci#include "xfs_refcount_btree.h" 178c2ecf20Sopenharmony_ci#include "xfs_alloc.h" 188c2ecf20Sopenharmony_ci#include "xfs_errortag.h" 198c2ecf20Sopenharmony_ci#include "xfs_error.h" 208c2ecf20Sopenharmony_ci#include "xfs_trace.h" 218c2ecf20Sopenharmony_ci#include "xfs_trans.h" 228c2ecf20Sopenharmony_ci#include "xfs_bit.h" 238c2ecf20Sopenharmony_ci#include "xfs_refcount.h" 248c2ecf20Sopenharmony_ci#include "xfs_rmap.h" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* Allowable refcount adjustment amounts. */ 278c2ecf20Sopenharmony_cienum xfs_refc_adjust_op { 288c2ecf20Sopenharmony_ci XFS_REFCOUNT_ADJUST_INCREASE = 1, 298c2ecf20Sopenharmony_ci XFS_REFCOUNT_ADJUST_DECREASE = -1, 308c2ecf20Sopenharmony_ci XFS_REFCOUNT_ADJUST_COW_ALLOC = 0, 318c2ecf20Sopenharmony_ci XFS_REFCOUNT_ADJUST_COW_FREE = -1, 328c2ecf20Sopenharmony_ci}; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciSTATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur, 358c2ecf20Sopenharmony_ci xfs_agblock_t agbno, xfs_extlen_t aglen); 368c2ecf20Sopenharmony_ciSTATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur, 378c2ecf20Sopenharmony_ci xfs_agblock_t agbno, xfs_extlen_t aglen); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* 408c2ecf20Sopenharmony_ci * Look up the first record less than or equal to [bno, len] in the btree 418c2ecf20Sopenharmony_ci * given by cur. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ciint 448c2ecf20Sopenharmony_cixfs_refcount_lookup_le( 458c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 468c2ecf20Sopenharmony_ci xfs_agblock_t bno, 478c2ecf20Sopenharmony_ci int *stat) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.agno, bno, 508c2ecf20Sopenharmony_ci XFS_LOOKUP_LE); 518c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_startblock = bno; 528c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_blockcount = 0; 538c2ecf20Sopenharmony_ci return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * Look up the first record greater than or equal to [bno, len] in the btree 588c2ecf20Sopenharmony_ci * given by cur. 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ciint 618c2ecf20Sopenharmony_cixfs_refcount_lookup_ge( 628c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 638c2ecf20Sopenharmony_ci xfs_agblock_t bno, 648c2ecf20Sopenharmony_ci int *stat) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.agno, bno, 678c2ecf20Sopenharmony_ci XFS_LOOKUP_GE); 688c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_startblock = bno; 698c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_blockcount = 0; 708c2ecf20Sopenharmony_ci return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* 748c2ecf20Sopenharmony_ci * Look up the first record equal to [bno, len] in the btree 758c2ecf20Sopenharmony_ci * given by cur. 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ciint 788c2ecf20Sopenharmony_cixfs_refcount_lookup_eq( 798c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 808c2ecf20Sopenharmony_ci xfs_agblock_t bno, 818c2ecf20Sopenharmony_ci int *stat) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.agno, bno, 848c2ecf20Sopenharmony_ci XFS_LOOKUP_LE); 858c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_startblock = bno; 868c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_blockcount = 0; 878c2ecf20Sopenharmony_ci return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/* Convert on-disk record to in-core format. */ 918c2ecf20Sopenharmony_civoid 928c2ecf20Sopenharmony_cixfs_refcount_btrec_to_irec( 938c2ecf20Sopenharmony_ci union xfs_btree_rec *rec, 948c2ecf20Sopenharmony_ci struct xfs_refcount_irec *irec) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock); 978c2ecf20Sopenharmony_ci irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount); 988c2ecf20Sopenharmony_ci irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/* 1028c2ecf20Sopenharmony_ci * Get the data from the pointed-to record. 1038c2ecf20Sopenharmony_ci */ 1048c2ecf20Sopenharmony_ciint 1058c2ecf20Sopenharmony_cixfs_refcount_get_rec( 1068c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 1078c2ecf20Sopenharmony_ci struct xfs_refcount_irec *irec, 1088c2ecf20Sopenharmony_ci int *stat) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci struct xfs_mount *mp = cur->bc_mp; 1118c2ecf20Sopenharmony_ci xfs_agnumber_t agno = cur->bc_ag.agno; 1128c2ecf20Sopenharmony_ci union xfs_btree_rec *rec; 1138c2ecf20Sopenharmony_ci int error; 1148c2ecf20Sopenharmony_ci xfs_agblock_t realstart; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci error = xfs_btree_get_rec(cur, &rec, stat); 1178c2ecf20Sopenharmony_ci if (error || !*stat) 1188c2ecf20Sopenharmony_ci return error; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci xfs_refcount_btrec_to_irec(rec, irec); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci agno = cur->bc_ag.agno; 1238c2ecf20Sopenharmony_ci if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN) 1248c2ecf20Sopenharmony_ci goto out_bad_rec; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* handle special COW-staging state */ 1278c2ecf20Sopenharmony_ci realstart = irec->rc_startblock; 1288c2ecf20Sopenharmony_ci if (realstart & XFS_REFC_COW_START) { 1298c2ecf20Sopenharmony_ci if (irec->rc_refcount != 1) 1308c2ecf20Sopenharmony_ci goto out_bad_rec; 1318c2ecf20Sopenharmony_ci realstart &= ~XFS_REFC_COW_START; 1328c2ecf20Sopenharmony_ci } else if (irec->rc_refcount < 2) { 1338c2ecf20Sopenharmony_ci goto out_bad_rec; 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci /* check for valid extent range, including overflow */ 1378c2ecf20Sopenharmony_ci if (!xfs_verify_agbno(mp, agno, realstart)) 1388c2ecf20Sopenharmony_ci goto out_bad_rec; 1398c2ecf20Sopenharmony_ci if (realstart > realstart + irec->rc_blockcount) 1408c2ecf20Sopenharmony_ci goto out_bad_rec; 1418c2ecf20Sopenharmony_ci if (!xfs_verify_agbno(mp, agno, realstart + irec->rc_blockcount - 1)) 1428c2ecf20Sopenharmony_ci goto out_bad_rec; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT) 1458c2ecf20Sopenharmony_ci goto out_bad_rec; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci trace_xfs_refcount_get(cur->bc_mp, cur->bc_ag.agno, irec); 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ciout_bad_rec: 1518c2ecf20Sopenharmony_ci xfs_warn(mp, 1528c2ecf20Sopenharmony_ci "Refcount BTree record corruption in AG %d detected!", agno); 1538c2ecf20Sopenharmony_ci xfs_warn(mp, 1548c2ecf20Sopenharmony_ci "Start block 0x%x, block count 0x%x, references 0x%x", 1558c2ecf20Sopenharmony_ci irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount); 1568c2ecf20Sopenharmony_ci return -EFSCORRUPTED; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci/* 1608c2ecf20Sopenharmony_ci * Update the record referred to by cur to the value given 1618c2ecf20Sopenharmony_ci * by [bno, len, refcount]. 1628c2ecf20Sopenharmony_ci * This either works (return 0) or gets an EFSCORRUPTED error. 1638c2ecf20Sopenharmony_ci */ 1648c2ecf20Sopenharmony_ciSTATIC int 1658c2ecf20Sopenharmony_cixfs_refcount_update( 1668c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 1678c2ecf20Sopenharmony_ci struct xfs_refcount_irec *irec) 1688c2ecf20Sopenharmony_ci{ 1698c2ecf20Sopenharmony_ci union xfs_btree_rec rec; 1708c2ecf20Sopenharmony_ci int error; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci trace_xfs_refcount_update(cur->bc_mp, cur->bc_ag.agno, irec); 1738c2ecf20Sopenharmony_ci rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock); 1748c2ecf20Sopenharmony_ci rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount); 1758c2ecf20Sopenharmony_ci rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount); 1768c2ecf20Sopenharmony_ci error = xfs_btree_update(cur, &rec); 1778c2ecf20Sopenharmony_ci if (error) 1788c2ecf20Sopenharmony_ci trace_xfs_refcount_update_error(cur->bc_mp, 1798c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 1808c2ecf20Sopenharmony_ci return error; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* 1848c2ecf20Sopenharmony_ci * Insert the record referred to by cur to the value given 1858c2ecf20Sopenharmony_ci * by [bno, len, refcount]. 1868c2ecf20Sopenharmony_ci * This either works (return 0) or gets an EFSCORRUPTED error. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ciint 1898c2ecf20Sopenharmony_cixfs_refcount_insert( 1908c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 1918c2ecf20Sopenharmony_ci struct xfs_refcount_irec *irec, 1928c2ecf20Sopenharmony_ci int *i) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci int error; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci trace_xfs_refcount_insert(cur->bc_mp, cur->bc_ag.agno, irec); 1978c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_startblock = irec->rc_startblock; 1988c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount; 1998c2ecf20Sopenharmony_ci cur->bc_rec.rc.rc_refcount = irec->rc_refcount; 2008c2ecf20Sopenharmony_ci error = xfs_btree_insert(cur, i); 2018c2ecf20Sopenharmony_ci if (error) 2028c2ecf20Sopenharmony_ci goto out_error; 2038c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) { 2048c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 2058c2ecf20Sopenharmony_ci goto out_error; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ciout_error: 2098c2ecf20Sopenharmony_ci if (error) 2108c2ecf20Sopenharmony_ci trace_xfs_refcount_insert_error(cur->bc_mp, 2118c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 2128c2ecf20Sopenharmony_ci return error; 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci/* 2168c2ecf20Sopenharmony_ci * Remove the record referred to by cur, then set the pointer to the spot 2178c2ecf20Sopenharmony_ci * where the record could be re-inserted, in case we want to increment or 2188c2ecf20Sopenharmony_ci * decrement the cursor. 2198c2ecf20Sopenharmony_ci * This either works (return 0) or gets an EFSCORRUPTED error. 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_ciSTATIC int 2228c2ecf20Sopenharmony_cixfs_refcount_delete( 2238c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 2248c2ecf20Sopenharmony_ci int *i) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci struct xfs_refcount_irec irec; 2278c2ecf20Sopenharmony_ci int found_rec; 2288c2ecf20Sopenharmony_ci int error; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &irec, &found_rec); 2318c2ecf20Sopenharmony_ci if (error) 2328c2ecf20Sopenharmony_ci goto out_error; 2338c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 2348c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 2358c2ecf20Sopenharmony_ci goto out_error; 2368c2ecf20Sopenharmony_ci } 2378c2ecf20Sopenharmony_ci trace_xfs_refcount_delete(cur->bc_mp, cur->bc_ag.agno, &irec); 2388c2ecf20Sopenharmony_ci error = xfs_btree_delete(cur, i); 2398c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) { 2408c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 2418c2ecf20Sopenharmony_ci goto out_error; 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci if (error) 2448c2ecf20Sopenharmony_ci goto out_error; 2458c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec); 2468c2ecf20Sopenharmony_ciout_error: 2478c2ecf20Sopenharmony_ci if (error) 2488c2ecf20Sopenharmony_ci trace_xfs_refcount_delete_error(cur->bc_mp, 2498c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 2508c2ecf20Sopenharmony_ci return error; 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/* 2548c2ecf20Sopenharmony_ci * Adjusting the Reference Count 2558c2ecf20Sopenharmony_ci * 2568c2ecf20Sopenharmony_ci * As stated elsewhere, the reference count btree (refcbt) stores 2578c2ecf20Sopenharmony_ci * >1 reference counts for extents of physical blocks. In this 2588c2ecf20Sopenharmony_ci * operation, we're either raising or lowering the reference count of 2598c2ecf20Sopenharmony_ci * some subrange stored in the tree: 2608c2ecf20Sopenharmony_ci * 2618c2ecf20Sopenharmony_ci * <------ adjustment range ------> 2628c2ecf20Sopenharmony_ci * ----+ +---+-----+ +--+--------+--------- 2638c2ecf20Sopenharmony_ci * 2 | | 3 | 4 | |17| 55 | 10 2648c2ecf20Sopenharmony_ci * ----+ +---+-----+ +--+--------+--------- 2658c2ecf20Sopenharmony_ci * X axis is physical blocks number; 2668c2ecf20Sopenharmony_ci * reference counts are the numbers inside the rectangles 2678c2ecf20Sopenharmony_ci * 2688c2ecf20Sopenharmony_ci * The first thing we need to do is to ensure that there are no 2698c2ecf20Sopenharmony_ci * refcount extents crossing either boundary of the range to be 2708c2ecf20Sopenharmony_ci * adjusted. For any extent that does cross a boundary, split it into 2718c2ecf20Sopenharmony_ci * two extents so that we can increment the refcount of one of the 2728c2ecf20Sopenharmony_ci * pieces later: 2738c2ecf20Sopenharmony_ci * 2748c2ecf20Sopenharmony_ci * <------ adjustment range ------> 2758c2ecf20Sopenharmony_ci * ----+ +---+-----+ +--+--------+----+---- 2768c2ecf20Sopenharmony_ci * 2 | | 3 | 2 | |17| 55 | 10 | 10 2778c2ecf20Sopenharmony_ci * ----+ +---+-----+ +--+--------+----+---- 2788c2ecf20Sopenharmony_ci * 2798c2ecf20Sopenharmony_ci * For this next step, let's assume that all the physical blocks in 2808c2ecf20Sopenharmony_ci * the adjustment range are mapped to a file and are therefore in use 2818c2ecf20Sopenharmony_ci * at least once. Therefore, we can infer that any gap in the 2828c2ecf20Sopenharmony_ci * refcount tree within the adjustment range represents a physical 2838c2ecf20Sopenharmony_ci * extent with refcount == 1: 2848c2ecf20Sopenharmony_ci * 2858c2ecf20Sopenharmony_ci * <------ adjustment range ------> 2868c2ecf20Sopenharmony_ci * ----+---+---+-----+-+--+--------+----+---- 2878c2ecf20Sopenharmony_ci * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10 2888c2ecf20Sopenharmony_ci * ----+---+---+-----+-+--+--------+----+---- 2898c2ecf20Sopenharmony_ci * ^ 2908c2ecf20Sopenharmony_ci * 2918c2ecf20Sopenharmony_ci * For each extent that falls within the interval range, figure out 2928c2ecf20Sopenharmony_ci * which extent is to the left or the right of that extent. Now we 2938c2ecf20Sopenharmony_ci * have a left, current, and right extent. If the new reference count 2948c2ecf20Sopenharmony_ci * of the center extent enables us to merge left, center, and right 2958c2ecf20Sopenharmony_ci * into one record covering all three, do so. If the center extent is 2968c2ecf20Sopenharmony_ci * at the left end of the range, abuts the left extent, and its new 2978c2ecf20Sopenharmony_ci * reference count matches the left extent's record, then merge them. 2988c2ecf20Sopenharmony_ci * If the center extent is at the right end of the range, abuts the 2998c2ecf20Sopenharmony_ci * right extent, and the reference counts match, merge those. In the 3008c2ecf20Sopenharmony_ci * example, we can left merge (assuming an increment operation): 3018c2ecf20Sopenharmony_ci * 3028c2ecf20Sopenharmony_ci * <------ adjustment range ------> 3038c2ecf20Sopenharmony_ci * --------+---+-----+-+--+--------+----+---- 3048c2ecf20Sopenharmony_ci * 2 | 3 | 2 |1|17| 55 | 10 | 10 3058c2ecf20Sopenharmony_ci * --------+---+-----+-+--+--------+----+---- 3068c2ecf20Sopenharmony_ci * ^ 3078c2ecf20Sopenharmony_ci * 3088c2ecf20Sopenharmony_ci * For all other extents within the range, adjust the reference count 3098c2ecf20Sopenharmony_ci * or delete it if the refcount falls below 2. If we were 3108c2ecf20Sopenharmony_ci * incrementing, the end result looks like this: 3118c2ecf20Sopenharmony_ci * 3128c2ecf20Sopenharmony_ci * <------ adjustment range ------> 3138c2ecf20Sopenharmony_ci * --------+---+-----+-+--+--------+----+---- 3148c2ecf20Sopenharmony_ci * 2 | 4 | 3 |2|18| 56 | 11 | 10 3158c2ecf20Sopenharmony_ci * --------+---+-----+-+--+--------+----+---- 3168c2ecf20Sopenharmony_ci * 3178c2ecf20Sopenharmony_ci * The result of a decrement operation looks as such: 3188c2ecf20Sopenharmony_ci * 3198c2ecf20Sopenharmony_ci * <------ adjustment range ------> 3208c2ecf20Sopenharmony_ci * ----+ +---+ +--+--------+----+---- 3218c2ecf20Sopenharmony_ci * 2 | | 2 | |16| 54 | 9 | 10 3228c2ecf20Sopenharmony_ci * ----+ +---+ +--+--------+----+---- 3238c2ecf20Sopenharmony_ci * DDDD 111111DD 3248c2ecf20Sopenharmony_ci * 3258c2ecf20Sopenharmony_ci * The blocks marked "D" are freed; the blocks marked "1" are only 3268c2ecf20Sopenharmony_ci * referenced once and therefore the record is removed from the 3278c2ecf20Sopenharmony_ci * refcount btree. 3288c2ecf20Sopenharmony_ci */ 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci/* Next block after this extent. */ 3318c2ecf20Sopenharmony_cistatic inline xfs_agblock_t 3328c2ecf20Sopenharmony_cixfs_refc_next( 3338c2ecf20Sopenharmony_ci struct xfs_refcount_irec *rc) 3348c2ecf20Sopenharmony_ci{ 3358c2ecf20Sopenharmony_ci return rc->rc_startblock + rc->rc_blockcount; 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci/* 3398c2ecf20Sopenharmony_ci * Split a refcount extent that crosses agbno. 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ciSTATIC int 3428c2ecf20Sopenharmony_cixfs_refcount_split_extent( 3438c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 3448c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 3458c2ecf20Sopenharmony_ci bool *shape_changed) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci struct xfs_refcount_irec rcext, tmp; 3488c2ecf20Sopenharmony_ci int found_rec; 3498c2ecf20Sopenharmony_ci int error; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci *shape_changed = false; 3528c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, agbno, &found_rec); 3538c2ecf20Sopenharmony_ci if (error) 3548c2ecf20Sopenharmony_ci goto out_error; 3558c2ecf20Sopenharmony_ci if (!found_rec) 3568c2ecf20Sopenharmony_ci return 0; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &rcext, &found_rec); 3598c2ecf20Sopenharmony_ci if (error) 3608c2ecf20Sopenharmony_ci goto out_error; 3618c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 3628c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 3638c2ecf20Sopenharmony_ci goto out_error; 3648c2ecf20Sopenharmony_ci } 3658c2ecf20Sopenharmony_ci if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno) 3668c2ecf20Sopenharmony_ci return 0; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci *shape_changed = true; 3698c2ecf20Sopenharmony_ci trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_ag.agno, 3708c2ecf20Sopenharmony_ci &rcext, agbno); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci /* Establish the right extent. */ 3738c2ecf20Sopenharmony_ci tmp = rcext; 3748c2ecf20Sopenharmony_ci tmp.rc_startblock = agbno; 3758c2ecf20Sopenharmony_ci tmp.rc_blockcount -= (agbno - rcext.rc_startblock); 3768c2ecf20Sopenharmony_ci error = xfs_refcount_update(cur, &tmp); 3778c2ecf20Sopenharmony_ci if (error) 3788c2ecf20Sopenharmony_ci goto out_error; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci /* Insert the left extent. */ 3818c2ecf20Sopenharmony_ci tmp = rcext; 3828c2ecf20Sopenharmony_ci tmp.rc_blockcount = agbno - rcext.rc_startblock; 3838c2ecf20Sopenharmony_ci error = xfs_refcount_insert(cur, &tmp, &found_rec); 3848c2ecf20Sopenharmony_ci if (error) 3858c2ecf20Sopenharmony_ci goto out_error; 3868c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 3878c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 3888c2ecf20Sopenharmony_ci goto out_error; 3898c2ecf20Sopenharmony_ci } 3908c2ecf20Sopenharmony_ci return error; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ciout_error: 3938c2ecf20Sopenharmony_ci trace_xfs_refcount_split_extent_error(cur->bc_mp, 3948c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 3958c2ecf20Sopenharmony_ci return error; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci/* 3998c2ecf20Sopenharmony_ci * Merge the left, center, and right extents. 4008c2ecf20Sopenharmony_ci */ 4018c2ecf20Sopenharmony_ciSTATIC int 4028c2ecf20Sopenharmony_cixfs_refcount_merge_center_extents( 4038c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 4048c2ecf20Sopenharmony_ci struct xfs_refcount_irec *left, 4058c2ecf20Sopenharmony_ci struct xfs_refcount_irec *center, 4068c2ecf20Sopenharmony_ci struct xfs_refcount_irec *right, 4078c2ecf20Sopenharmony_ci unsigned long long extlen, 4088c2ecf20Sopenharmony_ci xfs_extlen_t *aglen) 4098c2ecf20Sopenharmony_ci{ 4108c2ecf20Sopenharmony_ci int error; 4118c2ecf20Sopenharmony_ci int found_rec; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci trace_xfs_refcount_merge_center_extents(cur->bc_mp, 4148c2ecf20Sopenharmony_ci cur->bc_ag.agno, left, center, right); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci /* 4178c2ecf20Sopenharmony_ci * Make sure the center and right extents are not in the btree. 4188c2ecf20Sopenharmony_ci * If the center extent was synthesized, the first delete call 4198c2ecf20Sopenharmony_ci * removes the right extent and we skip the second deletion. 4208c2ecf20Sopenharmony_ci * If center and right were in the btree, then the first delete 4218c2ecf20Sopenharmony_ci * call removes the center and the second one removes the right 4228c2ecf20Sopenharmony_ci * extent. 4238c2ecf20Sopenharmony_ci */ 4248c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_ge(cur, center->rc_startblock, 4258c2ecf20Sopenharmony_ci &found_rec); 4268c2ecf20Sopenharmony_ci if (error) 4278c2ecf20Sopenharmony_ci goto out_error; 4288c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 4298c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 4308c2ecf20Sopenharmony_ci goto out_error; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci error = xfs_refcount_delete(cur, &found_rec); 4348c2ecf20Sopenharmony_ci if (error) 4358c2ecf20Sopenharmony_ci goto out_error; 4368c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 4378c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 4388c2ecf20Sopenharmony_ci goto out_error; 4398c2ecf20Sopenharmony_ci } 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci if (center->rc_refcount > 1) { 4428c2ecf20Sopenharmony_ci error = xfs_refcount_delete(cur, &found_rec); 4438c2ecf20Sopenharmony_ci if (error) 4448c2ecf20Sopenharmony_ci goto out_error; 4458c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 4468c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 4478c2ecf20Sopenharmony_ci goto out_error; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci } 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci /* Enlarge the left extent. */ 4528c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, left->rc_startblock, 4538c2ecf20Sopenharmony_ci &found_rec); 4548c2ecf20Sopenharmony_ci if (error) 4558c2ecf20Sopenharmony_ci goto out_error; 4568c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 4578c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 4588c2ecf20Sopenharmony_ci goto out_error; 4598c2ecf20Sopenharmony_ci } 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci left->rc_blockcount = extlen; 4628c2ecf20Sopenharmony_ci error = xfs_refcount_update(cur, left); 4638c2ecf20Sopenharmony_ci if (error) 4648c2ecf20Sopenharmony_ci goto out_error; 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci *aglen = 0; 4678c2ecf20Sopenharmony_ci return error; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ciout_error: 4708c2ecf20Sopenharmony_ci trace_xfs_refcount_merge_center_extents_error(cur->bc_mp, 4718c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 4728c2ecf20Sopenharmony_ci return error; 4738c2ecf20Sopenharmony_ci} 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci/* 4768c2ecf20Sopenharmony_ci * Merge with the left extent. 4778c2ecf20Sopenharmony_ci */ 4788c2ecf20Sopenharmony_ciSTATIC int 4798c2ecf20Sopenharmony_cixfs_refcount_merge_left_extent( 4808c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 4818c2ecf20Sopenharmony_ci struct xfs_refcount_irec *left, 4828c2ecf20Sopenharmony_ci struct xfs_refcount_irec *cleft, 4838c2ecf20Sopenharmony_ci xfs_agblock_t *agbno, 4848c2ecf20Sopenharmony_ci xfs_extlen_t *aglen) 4858c2ecf20Sopenharmony_ci{ 4868c2ecf20Sopenharmony_ci int error; 4878c2ecf20Sopenharmony_ci int found_rec; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci trace_xfs_refcount_merge_left_extent(cur->bc_mp, 4908c2ecf20Sopenharmony_ci cur->bc_ag.agno, left, cleft); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci /* If the extent at agbno (cleft) wasn't synthesized, remove it. */ 4938c2ecf20Sopenharmony_ci if (cleft->rc_refcount > 1) { 4948c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, cleft->rc_startblock, 4958c2ecf20Sopenharmony_ci &found_rec); 4968c2ecf20Sopenharmony_ci if (error) 4978c2ecf20Sopenharmony_ci goto out_error; 4988c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 4998c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 5008c2ecf20Sopenharmony_ci goto out_error; 5018c2ecf20Sopenharmony_ci } 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci error = xfs_refcount_delete(cur, &found_rec); 5048c2ecf20Sopenharmony_ci if (error) 5058c2ecf20Sopenharmony_ci goto out_error; 5068c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 5078c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 5088c2ecf20Sopenharmony_ci goto out_error; 5098c2ecf20Sopenharmony_ci } 5108c2ecf20Sopenharmony_ci } 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci /* Enlarge the left extent. */ 5138c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, left->rc_startblock, 5148c2ecf20Sopenharmony_ci &found_rec); 5158c2ecf20Sopenharmony_ci if (error) 5168c2ecf20Sopenharmony_ci goto out_error; 5178c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 5188c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 5198c2ecf20Sopenharmony_ci goto out_error; 5208c2ecf20Sopenharmony_ci } 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci left->rc_blockcount += cleft->rc_blockcount; 5238c2ecf20Sopenharmony_ci error = xfs_refcount_update(cur, left); 5248c2ecf20Sopenharmony_ci if (error) 5258c2ecf20Sopenharmony_ci goto out_error; 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci *agbno += cleft->rc_blockcount; 5288c2ecf20Sopenharmony_ci *aglen -= cleft->rc_blockcount; 5298c2ecf20Sopenharmony_ci return error; 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ciout_error: 5328c2ecf20Sopenharmony_ci trace_xfs_refcount_merge_left_extent_error(cur->bc_mp, 5338c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 5348c2ecf20Sopenharmony_ci return error; 5358c2ecf20Sopenharmony_ci} 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci/* 5388c2ecf20Sopenharmony_ci * Merge with the right extent. 5398c2ecf20Sopenharmony_ci */ 5408c2ecf20Sopenharmony_ciSTATIC int 5418c2ecf20Sopenharmony_cixfs_refcount_merge_right_extent( 5428c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 5438c2ecf20Sopenharmony_ci struct xfs_refcount_irec *right, 5448c2ecf20Sopenharmony_ci struct xfs_refcount_irec *cright, 5458c2ecf20Sopenharmony_ci xfs_extlen_t *aglen) 5468c2ecf20Sopenharmony_ci{ 5478c2ecf20Sopenharmony_ci int error; 5488c2ecf20Sopenharmony_ci int found_rec; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci trace_xfs_refcount_merge_right_extent(cur->bc_mp, 5518c2ecf20Sopenharmony_ci cur->bc_ag.agno, cright, right); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci /* 5548c2ecf20Sopenharmony_ci * If the extent ending at agbno+aglen (cright) wasn't synthesized, 5558c2ecf20Sopenharmony_ci * remove it. 5568c2ecf20Sopenharmony_ci */ 5578c2ecf20Sopenharmony_ci if (cright->rc_refcount > 1) { 5588c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, cright->rc_startblock, 5598c2ecf20Sopenharmony_ci &found_rec); 5608c2ecf20Sopenharmony_ci if (error) 5618c2ecf20Sopenharmony_ci goto out_error; 5628c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 5638c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 5648c2ecf20Sopenharmony_ci goto out_error; 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci error = xfs_refcount_delete(cur, &found_rec); 5688c2ecf20Sopenharmony_ci if (error) 5698c2ecf20Sopenharmony_ci goto out_error; 5708c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 5718c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 5728c2ecf20Sopenharmony_ci goto out_error; 5738c2ecf20Sopenharmony_ci } 5748c2ecf20Sopenharmony_ci } 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci /* Enlarge the right extent. */ 5778c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, right->rc_startblock, 5788c2ecf20Sopenharmony_ci &found_rec); 5798c2ecf20Sopenharmony_ci if (error) 5808c2ecf20Sopenharmony_ci goto out_error; 5818c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 5828c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 5838c2ecf20Sopenharmony_ci goto out_error; 5848c2ecf20Sopenharmony_ci } 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci right->rc_startblock -= cright->rc_blockcount; 5878c2ecf20Sopenharmony_ci right->rc_blockcount += cright->rc_blockcount; 5888c2ecf20Sopenharmony_ci error = xfs_refcount_update(cur, right); 5898c2ecf20Sopenharmony_ci if (error) 5908c2ecf20Sopenharmony_ci goto out_error; 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci *aglen -= cright->rc_blockcount; 5938c2ecf20Sopenharmony_ci return error; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ciout_error: 5968c2ecf20Sopenharmony_ci trace_xfs_refcount_merge_right_extent_error(cur->bc_mp, 5978c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 5988c2ecf20Sopenharmony_ci return error; 5998c2ecf20Sopenharmony_ci} 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci#define XFS_FIND_RCEXT_SHARED 1 6028c2ecf20Sopenharmony_ci#define XFS_FIND_RCEXT_COW 2 6038c2ecf20Sopenharmony_ci/* 6048c2ecf20Sopenharmony_ci * Find the left extent and the one after it (cleft). This function assumes 6058c2ecf20Sopenharmony_ci * that we've already split any extent crossing agbno. 6068c2ecf20Sopenharmony_ci */ 6078c2ecf20Sopenharmony_ciSTATIC int 6088c2ecf20Sopenharmony_cixfs_refcount_find_left_extents( 6098c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 6108c2ecf20Sopenharmony_ci struct xfs_refcount_irec *left, 6118c2ecf20Sopenharmony_ci struct xfs_refcount_irec *cleft, 6128c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 6138c2ecf20Sopenharmony_ci xfs_extlen_t aglen, 6148c2ecf20Sopenharmony_ci int flags) 6158c2ecf20Sopenharmony_ci{ 6168c2ecf20Sopenharmony_ci struct xfs_refcount_irec tmp; 6178c2ecf20Sopenharmony_ci int error; 6188c2ecf20Sopenharmony_ci int found_rec; 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK; 6218c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec); 6228c2ecf20Sopenharmony_ci if (error) 6238c2ecf20Sopenharmony_ci goto out_error; 6248c2ecf20Sopenharmony_ci if (!found_rec) 6258c2ecf20Sopenharmony_ci return 0; 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &found_rec); 6288c2ecf20Sopenharmony_ci if (error) 6298c2ecf20Sopenharmony_ci goto out_error; 6308c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 6318c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 6328c2ecf20Sopenharmony_ci goto out_error; 6338c2ecf20Sopenharmony_ci } 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci if (xfs_refc_next(&tmp) != agbno) 6368c2ecf20Sopenharmony_ci return 0; 6378c2ecf20Sopenharmony_ci if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2) 6388c2ecf20Sopenharmony_ci return 0; 6398c2ecf20Sopenharmony_ci if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1) 6408c2ecf20Sopenharmony_ci return 0; 6418c2ecf20Sopenharmony_ci /* We have a left extent; retrieve (or invent) the next right one */ 6428c2ecf20Sopenharmony_ci *left = tmp; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci error = xfs_btree_increment(cur, 0, &found_rec); 6458c2ecf20Sopenharmony_ci if (error) 6468c2ecf20Sopenharmony_ci goto out_error; 6478c2ecf20Sopenharmony_ci if (found_rec) { 6488c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &found_rec); 6498c2ecf20Sopenharmony_ci if (error) 6508c2ecf20Sopenharmony_ci goto out_error; 6518c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 6528c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 6538c2ecf20Sopenharmony_ci goto out_error; 6548c2ecf20Sopenharmony_ci } 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci /* if tmp starts at the end of our range, just use that */ 6578c2ecf20Sopenharmony_ci if (tmp.rc_startblock == agbno) 6588c2ecf20Sopenharmony_ci *cleft = tmp; 6598c2ecf20Sopenharmony_ci else { 6608c2ecf20Sopenharmony_ci /* 6618c2ecf20Sopenharmony_ci * There's a gap in the refcntbt at the start of the 6628c2ecf20Sopenharmony_ci * range we're interested in (refcount == 1) so 6638c2ecf20Sopenharmony_ci * synthesize the implied extent and pass it back. 6648c2ecf20Sopenharmony_ci * We assume here that the agbno/aglen range was 6658c2ecf20Sopenharmony_ci * passed in from a data fork extent mapping and 6668c2ecf20Sopenharmony_ci * therefore is allocated to exactly one owner. 6678c2ecf20Sopenharmony_ci */ 6688c2ecf20Sopenharmony_ci cleft->rc_startblock = agbno; 6698c2ecf20Sopenharmony_ci cleft->rc_blockcount = min(aglen, 6708c2ecf20Sopenharmony_ci tmp.rc_startblock - agbno); 6718c2ecf20Sopenharmony_ci cleft->rc_refcount = 1; 6728c2ecf20Sopenharmony_ci } 6738c2ecf20Sopenharmony_ci } else { 6748c2ecf20Sopenharmony_ci /* 6758c2ecf20Sopenharmony_ci * No extents, so pretend that there's one covering the whole 6768c2ecf20Sopenharmony_ci * range. 6778c2ecf20Sopenharmony_ci */ 6788c2ecf20Sopenharmony_ci cleft->rc_startblock = agbno; 6798c2ecf20Sopenharmony_ci cleft->rc_blockcount = aglen; 6808c2ecf20Sopenharmony_ci cleft->rc_refcount = 1; 6818c2ecf20Sopenharmony_ci } 6828c2ecf20Sopenharmony_ci trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_ag.agno, 6838c2ecf20Sopenharmony_ci left, cleft, agbno); 6848c2ecf20Sopenharmony_ci return error; 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ciout_error: 6878c2ecf20Sopenharmony_ci trace_xfs_refcount_find_left_extent_error(cur->bc_mp, 6888c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 6898c2ecf20Sopenharmony_ci return error; 6908c2ecf20Sopenharmony_ci} 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci/* 6938c2ecf20Sopenharmony_ci * Find the right extent and the one before it (cright). This function 6948c2ecf20Sopenharmony_ci * assumes that we've already split any extents crossing agbno + aglen. 6958c2ecf20Sopenharmony_ci */ 6968c2ecf20Sopenharmony_ciSTATIC int 6978c2ecf20Sopenharmony_cixfs_refcount_find_right_extents( 6988c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 6998c2ecf20Sopenharmony_ci struct xfs_refcount_irec *right, 7008c2ecf20Sopenharmony_ci struct xfs_refcount_irec *cright, 7018c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 7028c2ecf20Sopenharmony_ci xfs_extlen_t aglen, 7038c2ecf20Sopenharmony_ci int flags) 7048c2ecf20Sopenharmony_ci{ 7058c2ecf20Sopenharmony_ci struct xfs_refcount_irec tmp; 7068c2ecf20Sopenharmony_ci int error; 7078c2ecf20Sopenharmony_ci int found_rec; 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci right->rc_startblock = cright->rc_startblock = NULLAGBLOCK; 7108c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec); 7118c2ecf20Sopenharmony_ci if (error) 7128c2ecf20Sopenharmony_ci goto out_error; 7138c2ecf20Sopenharmony_ci if (!found_rec) 7148c2ecf20Sopenharmony_ci return 0; 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &found_rec); 7178c2ecf20Sopenharmony_ci if (error) 7188c2ecf20Sopenharmony_ci goto out_error; 7198c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 7208c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 7218c2ecf20Sopenharmony_ci goto out_error; 7228c2ecf20Sopenharmony_ci } 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci if (tmp.rc_startblock != agbno + aglen) 7258c2ecf20Sopenharmony_ci return 0; 7268c2ecf20Sopenharmony_ci if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2) 7278c2ecf20Sopenharmony_ci return 0; 7288c2ecf20Sopenharmony_ci if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1) 7298c2ecf20Sopenharmony_ci return 0; 7308c2ecf20Sopenharmony_ci /* We have a right extent; retrieve (or invent) the next left one */ 7318c2ecf20Sopenharmony_ci *right = tmp; 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci error = xfs_btree_decrement(cur, 0, &found_rec); 7348c2ecf20Sopenharmony_ci if (error) 7358c2ecf20Sopenharmony_ci goto out_error; 7368c2ecf20Sopenharmony_ci if (found_rec) { 7378c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &found_rec); 7388c2ecf20Sopenharmony_ci if (error) 7398c2ecf20Sopenharmony_ci goto out_error; 7408c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 7418c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 7428c2ecf20Sopenharmony_ci goto out_error; 7438c2ecf20Sopenharmony_ci } 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci /* if tmp ends at the end of our range, just use that */ 7468c2ecf20Sopenharmony_ci if (xfs_refc_next(&tmp) == agbno + aglen) 7478c2ecf20Sopenharmony_ci *cright = tmp; 7488c2ecf20Sopenharmony_ci else { 7498c2ecf20Sopenharmony_ci /* 7508c2ecf20Sopenharmony_ci * There's a gap in the refcntbt at the end of the 7518c2ecf20Sopenharmony_ci * range we're interested in (refcount == 1) so 7528c2ecf20Sopenharmony_ci * create the implied extent and pass it back. 7538c2ecf20Sopenharmony_ci * We assume here that the agbno/aglen range was 7548c2ecf20Sopenharmony_ci * passed in from a data fork extent mapping and 7558c2ecf20Sopenharmony_ci * therefore is allocated to exactly one owner. 7568c2ecf20Sopenharmony_ci */ 7578c2ecf20Sopenharmony_ci cright->rc_startblock = max(agbno, xfs_refc_next(&tmp)); 7588c2ecf20Sopenharmony_ci cright->rc_blockcount = right->rc_startblock - 7598c2ecf20Sopenharmony_ci cright->rc_startblock; 7608c2ecf20Sopenharmony_ci cright->rc_refcount = 1; 7618c2ecf20Sopenharmony_ci } 7628c2ecf20Sopenharmony_ci } else { 7638c2ecf20Sopenharmony_ci /* 7648c2ecf20Sopenharmony_ci * No extents, so pretend that there's one covering the whole 7658c2ecf20Sopenharmony_ci * range. 7668c2ecf20Sopenharmony_ci */ 7678c2ecf20Sopenharmony_ci cright->rc_startblock = agbno; 7688c2ecf20Sopenharmony_ci cright->rc_blockcount = aglen; 7698c2ecf20Sopenharmony_ci cright->rc_refcount = 1; 7708c2ecf20Sopenharmony_ci } 7718c2ecf20Sopenharmony_ci trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_ag.agno, 7728c2ecf20Sopenharmony_ci cright, right, agbno + aglen); 7738c2ecf20Sopenharmony_ci return error; 7748c2ecf20Sopenharmony_ci 7758c2ecf20Sopenharmony_ciout_error: 7768c2ecf20Sopenharmony_ci trace_xfs_refcount_find_right_extent_error(cur->bc_mp, 7778c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 7788c2ecf20Sopenharmony_ci return error; 7798c2ecf20Sopenharmony_ci} 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ci/* Is this extent valid? */ 7828c2ecf20Sopenharmony_cistatic inline bool 7838c2ecf20Sopenharmony_cixfs_refc_valid( 7848c2ecf20Sopenharmony_ci struct xfs_refcount_irec *rc) 7858c2ecf20Sopenharmony_ci{ 7868c2ecf20Sopenharmony_ci return rc->rc_startblock != NULLAGBLOCK; 7878c2ecf20Sopenharmony_ci} 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci/* 7908c2ecf20Sopenharmony_ci * Try to merge with any extents on the boundaries of the adjustment range. 7918c2ecf20Sopenharmony_ci */ 7928c2ecf20Sopenharmony_ciSTATIC int 7938c2ecf20Sopenharmony_cixfs_refcount_merge_extents( 7948c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 7958c2ecf20Sopenharmony_ci xfs_agblock_t *agbno, 7968c2ecf20Sopenharmony_ci xfs_extlen_t *aglen, 7978c2ecf20Sopenharmony_ci enum xfs_refc_adjust_op adjust, 7988c2ecf20Sopenharmony_ci int flags, 7998c2ecf20Sopenharmony_ci bool *shape_changed) 8008c2ecf20Sopenharmony_ci{ 8018c2ecf20Sopenharmony_ci struct xfs_refcount_irec left = {0}, cleft = {0}; 8028c2ecf20Sopenharmony_ci struct xfs_refcount_irec cright = {0}, right = {0}; 8038c2ecf20Sopenharmony_ci int error; 8048c2ecf20Sopenharmony_ci unsigned long long ulen; 8058c2ecf20Sopenharmony_ci bool cequal; 8068c2ecf20Sopenharmony_ci 8078c2ecf20Sopenharmony_ci *shape_changed = false; 8088c2ecf20Sopenharmony_ci /* 8098c2ecf20Sopenharmony_ci * Find the extent just below agbno [left], just above agbno [cleft], 8108c2ecf20Sopenharmony_ci * just below (agbno + aglen) [cright], and just above (agbno + aglen) 8118c2ecf20Sopenharmony_ci * [right]. 8128c2ecf20Sopenharmony_ci */ 8138c2ecf20Sopenharmony_ci error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno, 8148c2ecf20Sopenharmony_ci *aglen, flags); 8158c2ecf20Sopenharmony_ci if (error) 8168c2ecf20Sopenharmony_ci return error; 8178c2ecf20Sopenharmony_ci error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno, 8188c2ecf20Sopenharmony_ci *aglen, flags); 8198c2ecf20Sopenharmony_ci if (error) 8208c2ecf20Sopenharmony_ci return error; 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_ci /* No left or right extent to merge; exit. */ 8238c2ecf20Sopenharmony_ci if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right)) 8248c2ecf20Sopenharmony_ci return 0; 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_ci cequal = (cleft.rc_startblock == cright.rc_startblock) && 8278c2ecf20Sopenharmony_ci (cleft.rc_blockcount == cright.rc_blockcount); 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci /* Try to merge left, cleft, and right. cleft must == cright. */ 8308c2ecf20Sopenharmony_ci ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount + 8318c2ecf20Sopenharmony_ci right.rc_blockcount; 8328c2ecf20Sopenharmony_ci if (xfs_refc_valid(&left) && xfs_refc_valid(&right) && 8338c2ecf20Sopenharmony_ci xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal && 8348c2ecf20Sopenharmony_ci left.rc_refcount == cleft.rc_refcount + adjust && 8358c2ecf20Sopenharmony_ci right.rc_refcount == cleft.rc_refcount + adjust && 8368c2ecf20Sopenharmony_ci ulen < MAXREFCEXTLEN) { 8378c2ecf20Sopenharmony_ci *shape_changed = true; 8388c2ecf20Sopenharmony_ci return xfs_refcount_merge_center_extents(cur, &left, &cleft, 8398c2ecf20Sopenharmony_ci &right, ulen, aglen); 8408c2ecf20Sopenharmony_ci } 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci /* Try to merge left and cleft. */ 8438c2ecf20Sopenharmony_ci ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount; 8448c2ecf20Sopenharmony_ci if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) && 8458c2ecf20Sopenharmony_ci left.rc_refcount == cleft.rc_refcount + adjust && 8468c2ecf20Sopenharmony_ci ulen < MAXREFCEXTLEN) { 8478c2ecf20Sopenharmony_ci *shape_changed = true; 8488c2ecf20Sopenharmony_ci error = xfs_refcount_merge_left_extent(cur, &left, &cleft, 8498c2ecf20Sopenharmony_ci agbno, aglen); 8508c2ecf20Sopenharmony_ci if (error) 8518c2ecf20Sopenharmony_ci return error; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci /* 8548c2ecf20Sopenharmony_ci * If we just merged left + cleft and cleft == cright, 8558c2ecf20Sopenharmony_ci * we no longer have a cright to merge with right. We're done. 8568c2ecf20Sopenharmony_ci */ 8578c2ecf20Sopenharmony_ci if (cequal) 8588c2ecf20Sopenharmony_ci return 0; 8598c2ecf20Sopenharmony_ci } 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci /* Try to merge cright and right. */ 8628c2ecf20Sopenharmony_ci ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount; 8638c2ecf20Sopenharmony_ci if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) && 8648c2ecf20Sopenharmony_ci right.rc_refcount == cright.rc_refcount + adjust && 8658c2ecf20Sopenharmony_ci ulen < MAXREFCEXTLEN) { 8668c2ecf20Sopenharmony_ci *shape_changed = true; 8678c2ecf20Sopenharmony_ci return xfs_refcount_merge_right_extent(cur, &right, &cright, 8688c2ecf20Sopenharmony_ci aglen); 8698c2ecf20Sopenharmony_ci } 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci return error; 8728c2ecf20Sopenharmony_ci} 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci/* 8758c2ecf20Sopenharmony_ci * XXX: This is a pretty hand-wavy estimate. The penalty for guessing 8768c2ecf20Sopenharmony_ci * true incorrectly is a shutdown FS; the penalty for guessing false 8778c2ecf20Sopenharmony_ci * incorrectly is more transaction rolls than might be necessary. 8788c2ecf20Sopenharmony_ci * Be conservative here. 8798c2ecf20Sopenharmony_ci */ 8808c2ecf20Sopenharmony_cistatic bool 8818c2ecf20Sopenharmony_cixfs_refcount_still_have_space( 8828c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur) 8838c2ecf20Sopenharmony_ci{ 8848c2ecf20Sopenharmony_ci unsigned long overhead; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci overhead = cur->bc_ag.refc.shape_changes * 8878c2ecf20Sopenharmony_ci xfs_allocfree_log_count(cur->bc_mp, 1); 8888c2ecf20Sopenharmony_ci overhead *= cur->bc_mp->m_sb.sb_blocksize; 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci /* 8918c2ecf20Sopenharmony_ci * Only allow 2 refcount extent updates per transaction if the 8928c2ecf20Sopenharmony_ci * refcount continue update "error" has been injected. 8938c2ecf20Sopenharmony_ci */ 8948c2ecf20Sopenharmony_ci if (cur->bc_ag.refc.nr_ops > 2 && 8958c2ecf20Sopenharmony_ci XFS_TEST_ERROR(false, cur->bc_mp, 8968c2ecf20Sopenharmony_ci XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE)) 8978c2ecf20Sopenharmony_ci return false; 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci if (cur->bc_ag.refc.nr_ops == 0) 9008c2ecf20Sopenharmony_ci return true; 9018c2ecf20Sopenharmony_ci else if (overhead > cur->bc_tp->t_log_res) 9028c2ecf20Sopenharmony_ci return false; 9038c2ecf20Sopenharmony_ci return cur->bc_tp->t_log_res - overhead > 9048c2ecf20Sopenharmony_ci cur->bc_ag.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD; 9058c2ecf20Sopenharmony_ci} 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_ci/* 9088c2ecf20Sopenharmony_ci * Adjust the refcounts of middle extents. At this point we should have 9098c2ecf20Sopenharmony_ci * split extents that crossed the adjustment range; merged with adjacent 9108c2ecf20Sopenharmony_ci * extents; and updated agbno/aglen to reflect the merges. Therefore, 9118c2ecf20Sopenharmony_ci * all we have to do is update the extents inside [agbno, agbno + aglen]. 9128c2ecf20Sopenharmony_ci */ 9138c2ecf20Sopenharmony_ciSTATIC int 9148c2ecf20Sopenharmony_cixfs_refcount_adjust_extents( 9158c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 9168c2ecf20Sopenharmony_ci xfs_agblock_t *agbno, 9178c2ecf20Sopenharmony_ci xfs_extlen_t *aglen, 9188c2ecf20Sopenharmony_ci enum xfs_refc_adjust_op adj, 9198c2ecf20Sopenharmony_ci struct xfs_owner_info *oinfo) 9208c2ecf20Sopenharmony_ci{ 9218c2ecf20Sopenharmony_ci struct xfs_refcount_irec ext, tmp; 9228c2ecf20Sopenharmony_ci int error; 9238c2ecf20Sopenharmony_ci int found_rec, found_tmp; 9248c2ecf20Sopenharmony_ci xfs_fsblock_t fsbno; 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci /* Merging did all the work already. */ 9278c2ecf20Sopenharmony_ci if (*aglen == 0) 9288c2ecf20Sopenharmony_ci return 0; 9298c2ecf20Sopenharmony_ci 9308c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec); 9318c2ecf20Sopenharmony_ci if (error) 9328c2ecf20Sopenharmony_ci goto out_error; 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci while (*aglen > 0 && xfs_refcount_still_have_space(cur)) { 9358c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &ext, &found_rec); 9368c2ecf20Sopenharmony_ci if (error) 9378c2ecf20Sopenharmony_ci goto out_error; 9388c2ecf20Sopenharmony_ci if (!found_rec) { 9398c2ecf20Sopenharmony_ci ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks; 9408c2ecf20Sopenharmony_ci ext.rc_blockcount = 0; 9418c2ecf20Sopenharmony_ci ext.rc_refcount = 0; 9428c2ecf20Sopenharmony_ci } 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci /* 9458c2ecf20Sopenharmony_ci * Deal with a hole in the refcount tree; if a file maps to 9468c2ecf20Sopenharmony_ci * these blocks and there's no refcountbt record, pretend that 9478c2ecf20Sopenharmony_ci * there is one with refcount == 1. 9488c2ecf20Sopenharmony_ci */ 9498c2ecf20Sopenharmony_ci if (ext.rc_startblock != *agbno) { 9508c2ecf20Sopenharmony_ci tmp.rc_startblock = *agbno; 9518c2ecf20Sopenharmony_ci tmp.rc_blockcount = min(*aglen, 9528c2ecf20Sopenharmony_ci ext.rc_startblock - *agbno); 9538c2ecf20Sopenharmony_ci tmp.rc_refcount = 1 + adj; 9548c2ecf20Sopenharmony_ci trace_xfs_refcount_modify_extent(cur->bc_mp, 9558c2ecf20Sopenharmony_ci cur->bc_ag.agno, &tmp); 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_ci /* 9588c2ecf20Sopenharmony_ci * Either cover the hole (increment) or 9598c2ecf20Sopenharmony_ci * delete the range (decrement). 9608c2ecf20Sopenharmony_ci */ 9618c2ecf20Sopenharmony_ci if (tmp.rc_refcount) { 9628c2ecf20Sopenharmony_ci error = xfs_refcount_insert(cur, &tmp, 9638c2ecf20Sopenharmony_ci &found_tmp); 9648c2ecf20Sopenharmony_ci if (error) 9658c2ecf20Sopenharmony_ci goto out_error; 9668c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, 9678c2ecf20Sopenharmony_ci found_tmp != 1)) { 9688c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 9698c2ecf20Sopenharmony_ci goto out_error; 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci cur->bc_ag.refc.nr_ops++; 9728c2ecf20Sopenharmony_ci } else { 9738c2ecf20Sopenharmony_ci fsbno = XFS_AGB_TO_FSB(cur->bc_mp, 9748c2ecf20Sopenharmony_ci cur->bc_ag.agno, 9758c2ecf20Sopenharmony_ci tmp.rc_startblock); 9768c2ecf20Sopenharmony_ci xfs_bmap_add_free(cur->bc_tp, fsbno, 9778c2ecf20Sopenharmony_ci tmp.rc_blockcount, oinfo); 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci (*agbno) += tmp.rc_blockcount; 9818c2ecf20Sopenharmony_ci (*aglen) -= tmp.rc_blockcount; 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_ge(cur, *agbno, 9848c2ecf20Sopenharmony_ci &found_rec); 9858c2ecf20Sopenharmony_ci if (error) 9868c2ecf20Sopenharmony_ci goto out_error; 9878c2ecf20Sopenharmony_ci } 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci /* Stop if there's nothing left to modify */ 9908c2ecf20Sopenharmony_ci if (*aglen == 0 || !xfs_refcount_still_have_space(cur)) 9918c2ecf20Sopenharmony_ci break; 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci /* 9948c2ecf20Sopenharmony_ci * Adjust the reference count and either update the tree 9958c2ecf20Sopenharmony_ci * (incr) or free the blocks (decr). 9968c2ecf20Sopenharmony_ci */ 9978c2ecf20Sopenharmony_ci if (ext.rc_refcount == MAXREFCOUNT) 9988c2ecf20Sopenharmony_ci goto skip; 9998c2ecf20Sopenharmony_ci ext.rc_refcount += adj; 10008c2ecf20Sopenharmony_ci trace_xfs_refcount_modify_extent(cur->bc_mp, 10018c2ecf20Sopenharmony_ci cur->bc_ag.agno, &ext); 10028c2ecf20Sopenharmony_ci if (ext.rc_refcount > 1) { 10038c2ecf20Sopenharmony_ci error = xfs_refcount_update(cur, &ext); 10048c2ecf20Sopenharmony_ci if (error) 10058c2ecf20Sopenharmony_ci goto out_error; 10068c2ecf20Sopenharmony_ci cur->bc_ag.refc.nr_ops++; 10078c2ecf20Sopenharmony_ci } else if (ext.rc_refcount == 1) { 10088c2ecf20Sopenharmony_ci error = xfs_refcount_delete(cur, &found_rec); 10098c2ecf20Sopenharmony_ci if (error) 10108c2ecf20Sopenharmony_ci goto out_error; 10118c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 10128c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 10138c2ecf20Sopenharmony_ci goto out_error; 10148c2ecf20Sopenharmony_ci } 10158c2ecf20Sopenharmony_ci cur->bc_ag.refc.nr_ops++; 10168c2ecf20Sopenharmony_ci goto advloop; 10178c2ecf20Sopenharmony_ci } else { 10188c2ecf20Sopenharmony_ci fsbno = XFS_AGB_TO_FSB(cur->bc_mp, 10198c2ecf20Sopenharmony_ci cur->bc_ag.agno, 10208c2ecf20Sopenharmony_ci ext.rc_startblock); 10218c2ecf20Sopenharmony_ci xfs_bmap_add_free(cur->bc_tp, fsbno, ext.rc_blockcount, 10228c2ecf20Sopenharmony_ci oinfo); 10238c2ecf20Sopenharmony_ci } 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ciskip: 10268c2ecf20Sopenharmony_ci error = xfs_btree_increment(cur, 0, &found_rec); 10278c2ecf20Sopenharmony_ci if (error) 10288c2ecf20Sopenharmony_ci goto out_error; 10298c2ecf20Sopenharmony_ci 10308c2ecf20Sopenharmony_ciadvloop: 10318c2ecf20Sopenharmony_ci (*agbno) += ext.rc_blockcount; 10328c2ecf20Sopenharmony_ci (*aglen) -= ext.rc_blockcount; 10338c2ecf20Sopenharmony_ci } 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci return error; 10368c2ecf20Sopenharmony_ciout_error: 10378c2ecf20Sopenharmony_ci trace_xfs_refcount_modify_extent_error(cur->bc_mp, 10388c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 10398c2ecf20Sopenharmony_ci return error; 10408c2ecf20Sopenharmony_ci} 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci/* Adjust the reference count of a range of AG blocks. */ 10438c2ecf20Sopenharmony_ciSTATIC int 10448c2ecf20Sopenharmony_cixfs_refcount_adjust( 10458c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 10468c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 10478c2ecf20Sopenharmony_ci xfs_extlen_t aglen, 10488c2ecf20Sopenharmony_ci xfs_agblock_t *new_agbno, 10498c2ecf20Sopenharmony_ci xfs_extlen_t *new_aglen, 10508c2ecf20Sopenharmony_ci enum xfs_refc_adjust_op adj, 10518c2ecf20Sopenharmony_ci struct xfs_owner_info *oinfo) 10528c2ecf20Sopenharmony_ci{ 10538c2ecf20Sopenharmony_ci bool shape_changed; 10548c2ecf20Sopenharmony_ci int shape_changes = 0; 10558c2ecf20Sopenharmony_ci int error; 10568c2ecf20Sopenharmony_ci 10578c2ecf20Sopenharmony_ci *new_agbno = agbno; 10588c2ecf20Sopenharmony_ci *new_aglen = aglen; 10598c2ecf20Sopenharmony_ci if (adj == XFS_REFCOUNT_ADJUST_INCREASE) 10608c2ecf20Sopenharmony_ci trace_xfs_refcount_increase(cur->bc_mp, cur->bc_ag.agno, 10618c2ecf20Sopenharmony_ci agbno, aglen); 10628c2ecf20Sopenharmony_ci else 10638c2ecf20Sopenharmony_ci trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_ag.agno, 10648c2ecf20Sopenharmony_ci agbno, aglen); 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci /* 10678c2ecf20Sopenharmony_ci * Ensure that no rcextents cross the boundary of the adjustment range. 10688c2ecf20Sopenharmony_ci */ 10698c2ecf20Sopenharmony_ci error = xfs_refcount_split_extent(cur, agbno, &shape_changed); 10708c2ecf20Sopenharmony_ci if (error) 10718c2ecf20Sopenharmony_ci goto out_error; 10728c2ecf20Sopenharmony_ci if (shape_changed) 10738c2ecf20Sopenharmony_ci shape_changes++; 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_ci error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed); 10768c2ecf20Sopenharmony_ci if (error) 10778c2ecf20Sopenharmony_ci goto out_error; 10788c2ecf20Sopenharmony_ci if (shape_changed) 10798c2ecf20Sopenharmony_ci shape_changes++; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci /* 10828c2ecf20Sopenharmony_ci * Try to merge with the left or right extents of the range. 10838c2ecf20Sopenharmony_ci */ 10848c2ecf20Sopenharmony_ci error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj, 10858c2ecf20Sopenharmony_ci XFS_FIND_RCEXT_SHARED, &shape_changed); 10868c2ecf20Sopenharmony_ci if (error) 10878c2ecf20Sopenharmony_ci goto out_error; 10888c2ecf20Sopenharmony_ci if (shape_changed) 10898c2ecf20Sopenharmony_ci shape_changes++; 10908c2ecf20Sopenharmony_ci if (shape_changes) 10918c2ecf20Sopenharmony_ci cur->bc_ag.refc.shape_changes++; 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci /* Now that we've taken care of the ends, adjust the middle extents */ 10948c2ecf20Sopenharmony_ci error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen, 10958c2ecf20Sopenharmony_ci adj, oinfo); 10968c2ecf20Sopenharmony_ci if (error) 10978c2ecf20Sopenharmony_ci goto out_error; 10988c2ecf20Sopenharmony_ci 10998c2ecf20Sopenharmony_ci return 0; 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ciout_error: 11028c2ecf20Sopenharmony_ci trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_ag.agno, 11038c2ecf20Sopenharmony_ci error, _RET_IP_); 11048c2ecf20Sopenharmony_ci return error; 11058c2ecf20Sopenharmony_ci} 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_ci/* Clean up after calling xfs_refcount_finish_one. */ 11088c2ecf20Sopenharmony_civoid 11098c2ecf20Sopenharmony_cixfs_refcount_finish_one_cleanup( 11108c2ecf20Sopenharmony_ci struct xfs_trans *tp, 11118c2ecf20Sopenharmony_ci struct xfs_btree_cur *rcur, 11128c2ecf20Sopenharmony_ci int error) 11138c2ecf20Sopenharmony_ci{ 11148c2ecf20Sopenharmony_ci struct xfs_buf *agbp; 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ci if (rcur == NULL) 11178c2ecf20Sopenharmony_ci return; 11188c2ecf20Sopenharmony_ci agbp = rcur->bc_ag.agbp; 11198c2ecf20Sopenharmony_ci xfs_btree_del_cursor(rcur, error); 11208c2ecf20Sopenharmony_ci if (error) 11218c2ecf20Sopenharmony_ci xfs_trans_brelse(tp, agbp); 11228c2ecf20Sopenharmony_ci} 11238c2ecf20Sopenharmony_ci 11248c2ecf20Sopenharmony_ci/* 11258c2ecf20Sopenharmony_ci * Process one of the deferred refcount operations. We pass back the 11268c2ecf20Sopenharmony_ci * btree cursor to maintain our lock on the btree between calls. 11278c2ecf20Sopenharmony_ci * This saves time and eliminates a buffer deadlock between the 11288c2ecf20Sopenharmony_ci * superblock and the AGF because we'll always grab them in the same 11298c2ecf20Sopenharmony_ci * order. 11308c2ecf20Sopenharmony_ci */ 11318c2ecf20Sopenharmony_ciint 11328c2ecf20Sopenharmony_cixfs_refcount_finish_one( 11338c2ecf20Sopenharmony_ci struct xfs_trans *tp, 11348c2ecf20Sopenharmony_ci enum xfs_refcount_intent_type type, 11358c2ecf20Sopenharmony_ci xfs_fsblock_t startblock, 11368c2ecf20Sopenharmony_ci xfs_extlen_t blockcount, 11378c2ecf20Sopenharmony_ci xfs_fsblock_t *new_fsb, 11388c2ecf20Sopenharmony_ci xfs_extlen_t *new_len, 11398c2ecf20Sopenharmony_ci struct xfs_btree_cur **pcur) 11408c2ecf20Sopenharmony_ci{ 11418c2ecf20Sopenharmony_ci struct xfs_mount *mp = tp->t_mountp; 11428c2ecf20Sopenharmony_ci struct xfs_btree_cur *rcur; 11438c2ecf20Sopenharmony_ci struct xfs_buf *agbp = NULL; 11448c2ecf20Sopenharmony_ci int error = 0; 11458c2ecf20Sopenharmony_ci xfs_agnumber_t agno; 11468c2ecf20Sopenharmony_ci xfs_agblock_t bno; 11478c2ecf20Sopenharmony_ci xfs_agblock_t new_agbno; 11488c2ecf20Sopenharmony_ci unsigned long nr_ops = 0; 11498c2ecf20Sopenharmony_ci int shape_changes = 0; 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ci agno = XFS_FSB_TO_AGNO(mp, startblock); 11528c2ecf20Sopenharmony_ci ASSERT(agno != NULLAGNUMBER); 11538c2ecf20Sopenharmony_ci bno = XFS_FSB_TO_AGBNO(mp, startblock); 11548c2ecf20Sopenharmony_ci 11558c2ecf20Sopenharmony_ci trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock), 11568c2ecf20Sopenharmony_ci type, XFS_FSB_TO_AGBNO(mp, startblock), 11578c2ecf20Sopenharmony_ci blockcount); 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci if (XFS_TEST_ERROR(false, mp, 11608c2ecf20Sopenharmony_ci XFS_ERRTAG_REFCOUNT_FINISH_ONE)) 11618c2ecf20Sopenharmony_ci return -EIO; 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci /* 11648c2ecf20Sopenharmony_ci * If we haven't gotten a cursor or the cursor AG doesn't match 11658c2ecf20Sopenharmony_ci * the startblock, get one now. 11668c2ecf20Sopenharmony_ci */ 11678c2ecf20Sopenharmony_ci rcur = *pcur; 11688c2ecf20Sopenharmony_ci if (rcur != NULL && rcur->bc_ag.agno != agno) { 11698c2ecf20Sopenharmony_ci nr_ops = rcur->bc_ag.refc.nr_ops; 11708c2ecf20Sopenharmony_ci shape_changes = rcur->bc_ag.refc.shape_changes; 11718c2ecf20Sopenharmony_ci xfs_refcount_finish_one_cleanup(tp, rcur, 0); 11728c2ecf20Sopenharmony_ci rcur = NULL; 11738c2ecf20Sopenharmony_ci *pcur = NULL; 11748c2ecf20Sopenharmony_ci } 11758c2ecf20Sopenharmony_ci if (rcur == NULL) { 11768c2ecf20Sopenharmony_ci error = xfs_alloc_read_agf(tp->t_mountp, tp, agno, 11778c2ecf20Sopenharmony_ci XFS_ALLOC_FLAG_FREEING, &agbp); 11788c2ecf20Sopenharmony_ci if (error) 11798c2ecf20Sopenharmony_ci return error; 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_ci rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno); 11828c2ecf20Sopenharmony_ci if (!rcur) { 11838c2ecf20Sopenharmony_ci error = -ENOMEM; 11848c2ecf20Sopenharmony_ci goto out_cur; 11858c2ecf20Sopenharmony_ci } 11868c2ecf20Sopenharmony_ci rcur->bc_ag.refc.nr_ops = nr_ops; 11878c2ecf20Sopenharmony_ci rcur->bc_ag.refc.shape_changes = shape_changes; 11888c2ecf20Sopenharmony_ci } 11898c2ecf20Sopenharmony_ci *pcur = rcur; 11908c2ecf20Sopenharmony_ci 11918c2ecf20Sopenharmony_ci switch (type) { 11928c2ecf20Sopenharmony_ci case XFS_REFCOUNT_INCREASE: 11938c2ecf20Sopenharmony_ci error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno, 11948c2ecf20Sopenharmony_ci new_len, XFS_REFCOUNT_ADJUST_INCREASE, NULL); 11958c2ecf20Sopenharmony_ci *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno); 11968c2ecf20Sopenharmony_ci break; 11978c2ecf20Sopenharmony_ci case XFS_REFCOUNT_DECREASE: 11988c2ecf20Sopenharmony_ci error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno, 11998c2ecf20Sopenharmony_ci new_len, XFS_REFCOUNT_ADJUST_DECREASE, NULL); 12008c2ecf20Sopenharmony_ci *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno); 12018c2ecf20Sopenharmony_ci break; 12028c2ecf20Sopenharmony_ci case XFS_REFCOUNT_ALLOC_COW: 12038c2ecf20Sopenharmony_ci *new_fsb = startblock + blockcount; 12048c2ecf20Sopenharmony_ci *new_len = 0; 12058c2ecf20Sopenharmony_ci error = __xfs_refcount_cow_alloc(rcur, bno, blockcount); 12068c2ecf20Sopenharmony_ci break; 12078c2ecf20Sopenharmony_ci case XFS_REFCOUNT_FREE_COW: 12088c2ecf20Sopenharmony_ci *new_fsb = startblock + blockcount; 12098c2ecf20Sopenharmony_ci *new_len = 0; 12108c2ecf20Sopenharmony_ci error = __xfs_refcount_cow_free(rcur, bno, blockcount); 12118c2ecf20Sopenharmony_ci break; 12128c2ecf20Sopenharmony_ci default: 12138c2ecf20Sopenharmony_ci ASSERT(0); 12148c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 12158c2ecf20Sopenharmony_ci } 12168c2ecf20Sopenharmony_ci if (!error && *new_len > 0) 12178c2ecf20Sopenharmony_ci trace_xfs_refcount_finish_one_leftover(mp, agno, type, 12188c2ecf20Sopenharmony_ci bno, blockcount, new_agbno, *new_len); 12198c2ecf20Sopenharmony_ci return error; 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ciout_cur: 12228c2ecf20Sopenharmony_ci xfs_trans_brelse(tp, agbp); 12238c2ecf20Sopenharmony_ci 12248c2ecf20Sopenharmony_ci return error; 12258c2ecf20Sopenharmony_ci} 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_ci/* 12288c2ecf20Sopenharmony_ci * Record a refcount intent for later processing. 12298c2ecf20Sopenharmony_ci */ 12308c2ecf20Sopenharmony_cistatic void 12318c2ecf20Sopenharmony_ci__xfs_refcount_add( 12328c2ecf20Sopenharmony_ci struct xfs_trans *tp, 12338c2ecf20Sopenharmony_ci enum xfs_refcount_intent_type type, 12348c2ecf20Sopenharmony_ci xfs_fsblock_t startblock, 12358c2ecf20Sopenharmony_ci xfs_extlen_t blockcount) 12368c2ecf20Sopenharmony_ci{ 12378c2ecf20Sopenharmony_ci struct xfs_refcount_intent *ri; 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_ci trace_xfs_refcount_defer(tp->t_mountp, 12408c2ecf20Sopenharmony_ci XFS_FSB_TO_AGNO(tp->t_mountp, startblock), 12418c2ecf20Sopenharmony_ci type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), 12428c2ecf20Sopenharmony_ci blockcount); 12438c2ecf20Sopenharmony_ci 12448c2ecf20Sopenharmony_ci ri = kmem_alloc(sizeof(struct xfs_refcount_intent), 12458c2ecf20Sopenharmony_ci KM_NOFS); 12468c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ri->ri_list); 12478c2ecf20Sopenharmony_ci ri->ri_type = type; 12488c2ecf20Sopenharmony_ci ri->ri_startblock = startblock; 12498c2ecf20Sopenharmony_ci ri->ri_blockcount = blockcount; 12508c2ecf20Sopenharmony_ci 12518c2ecf20Sopenharmony_ci xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list); 12528c2ecf20Sopenharmony_ci} 12538c2ecf20Sopenharmony_ci 12548c2ecf20Sopenharmony_ci/* 12558c2ecf20Sopenharmony_ci * Increase the reference count of the blocks backing a file's extent. 12568c2ecf20Sopenharmony_ci */ 12578c2ecf20Sopenharmony_civoid 12588c2ecf20Sopenharmony_cixfs_refcount_increase_extent( 12598c2ecf20Sopenharmony_ci struct xfs_trans *tp, 12608c2ecf20Sopenharmony_ci struct xfs_bmbt_irec *PREV) 12618c2ecf20Sopenharmony_ci{ 12628c2ecf20Sopenharmony_ci if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb)) 12638c2ecf20Sopenharmony_ci return; 12648c2ecf20Sopenharmony_ci 12658c2ecf20Sopenharmony_ci __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE, PREV->br_startblock, 12668c2ecf20Sopenharmony_ci PREV->br_blockcount); 12678c2ecf20Sopenharmony_ci} 12688c2ecf20Sopenharmony_ci 12698c2ecf20Sopenharmony_ci/* 12708c2ecf20Sopenharmony_ci * Decrease the reference count of the blocks backing a file's extent. 12718c2ecf20Sopenharmony_ci */ 12728c2ecf20Sopenharmony_civoid 12738c2ecf20Sopenharmony_cixfs_refcount_decrease_extent( 12748c2ecf20Sopenharmony_ci struct xfs_trans *tp, 12758c2ecf20Sopenharmony_ci struct xfs_bmbt_irec *PREV) 12768c2ecf20Sopenharmony_ci{ 12778c2ecf20Sopenharmony_ci if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb)) 12788c2ecf20Sopenharmony_ci return; 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE, PREV->br_startblock, 12818c2ecf20Sopenharmony_ci PREV->br_blockcount); 12828c2ecf20Sopenharmony_ci} 12838c2ecf20Sopenharmony_ci 12848c2ecf20Sopenharmony_ci/* 12858c2ecf20Sopenharmony_ci * Given an AG extent, find the lowest-numbered run of shared blocks 12868c2ecf20Sopenharmony_ci * within that range and return the range in fbno/flen. If 12878c2ecf20Sopenharmony_ci * find_end_of_shared is set, return the longest contiguous extent of 12888c2ecf20Sopenharmony_ci * shared blocks; if not, just return the first extent we find. If no 12898c2ecf20Sopenharmony_ci * shared blocks are found, fbno and flen will be set to NULLAGBLOCK 12908c2ecf20Sopenharmony_ci * and 0, respectively. 12918c2ecf20Sopenharmony_ci */ 12928c2ecf20Sopenharmony_ciint 12938c2ecf20Sopenharmony_cixfs_refcount_find_shared( 12948c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 12958c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 12968c2ecf20Sopenharmony_ci xfs_extlen_t aglen, 12978c2ecf20Sopenharmony_ci xfs_agblock_t *fbno, 12988c2ecf20Sopenharmony_ci xfs_extlen_t *flen, 12998c2ecf20Sopenharmony_ci bool find_end_of_shared) 13008c2ecf20Sopenharmony_ci{ 13018c2ecf20Sopenharmony_ci struct xfs_refcount_irec tmp; 13028c2ecf20Sopenharmony_ci int i; 13038c2ecf20Sopenharmony_ci int have; 13048c2ecf20Sopenharmony_ci int error; 13058c2ecf20Sopenharmony_ci 13068c2ecf20Sopenharmony_ci trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_ag.agno, 13078c2ecf20Sopenharmony_ci agbno, aglen); 13088c2ecf20Sopenharmony_ci 13098c2ecf20Sopenharmony_ci /* By default, skip the whole range */ 13108c2ecf20Sopenharmony_ci *fbno = NULLAGBLOCK; 13118c2ecf20Sopenharmony_ci *flen = 0; 13128c2ecf20Sopenharmony_ci 13138c2ecf20Sopenharmony_ci /* Try to find a refcount extent that crosses the start */ 13148c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_le(cur, agbno, &have); 13158c2ecf20Sopenharmony_ci if (error) 13168c2ecf20Sopenharmony_ci goto out_error; 13178c2ecf20Sopenharmony_ci if (!have) { 13188c2ecf20Sopenharmony_ci /* No left extent, look at the next one */ 13198c2ecf20Sopenharmony_ci error = xfs_btree_increment(cur, 0, &have); 13208c2ecf20Sopenharmony_ci if (error) 13218c2ecf20Sopenharmony_ci goto out_error; 13228c2ecf20Sopenharmony_ci if (!have) 13238c2ecf20Sopenharmony_ci goto done; 13248c2ecf20Sopenharmony_ci } 13258c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &i); 13268c2ecf20Sopenharmony_ci if (error) 13278c2ecf20Sopenharmony_ci goto out_error; 13288c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 13298c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 13308c2ecf20Sopenharmony_ci goto out_error; 13318c2ecf20Sopenharmony_ci } 13328c2ecf20Sopenharmony_ci 13338c2ecf20Sopenharmony_ci /* If the extent ends before the start, look at the next one */ 13348c2ecf20Sopenharmony_ci if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) { 13358c2ecf20Sopenharmony_ci error = xfs_btree_increment(cur, 0, &have); 13368c2ecf20Sopenharmony_ci if (error) 13378c2ecf20Sopenharmony_ci goto out_error; 13388c2ecf20Sopenharmony_ci if (!have) 13398c2ecf20Sopenharmony_ci goto done; 13408c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &i); 13418c2ecf20Sopenharmony_ci if (error) 13428c2ecf20Sopenharmony_ci goto out_error; 13438c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 13448c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 13458c2ecf20Sopenharmony_ci goto out_error; 13468c2ecf20Sopenharmony_ci } 13478c2ecf20Sopenharmony_ci } 13488c2ecf20Sopenharmony_ci 13498c2ecf20Sopenharmony_ci /* If the extent starts after the range we want, bail out */ 13508c2ecf20Sopenharmony_ci if (tmp.rc_startblock >= agbno + aglen) 13518c2ecf20Sopenharmony_ci goto done; 13528c2ecf20Sopenharmony_ci 13538c2ecf20Sopenharmony_ci /* We found the start of a shared extent! */ 13548c2ecf20Sopenharmony_ci if (tmp.rc_startblock < agbno) { 13558c2ecf20Sopenharmony_ci tmp.rc_blockcount -= (agbno - tmp.rc_startblock); 13568c2ecf20Sopenharmony_ci tmp.rc_startblock = agbno; 13578c2ecf20Sopenharmony_ci } 13588c2ecf20Sopenharmony_ci 13598c2ecf20Sopenharmony_ci *fbno = tmp.rc_startblock; 13608c2ecf20Sopenharmony_ci *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno); 13618c2ecf20Sopenharmony_ci if (!find_end_of_shared) 13628c2ecf20Sopenharmony_ci goto done; 13638c2ecf20Sopenharmony_ci 13648c2ecf20Sopenharmony_ci /* Otherwise, find the end of this shared extent */ 13658c2ecf20Sopenharmony_ci while (*fbno + *flen < agbno + aglen) { 13668c2ecf20Sopenharmony_ci error = xfs_btree_increment(cur, 0, &have); 13678c2ecf20Sopenharmony_ci if (error) 13688c2ecf20Sopenharmony_ci goto out_error; 13698c2ecf20Sopenharmony_ci if (!have) 13708c2ecf20Sopenharmony_ci break; 13718c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &tmp, &i); 13728c2ecf20Sopenharmony_ci if (error) 13738c2ecf20Sopenharmony_ci goto out_error; 13748c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 13758c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 13768c2ecf20Sopenharmony_ci goto out_error; 13778c2ecf20Sopenharmony_ci } 13788c2ecf20Sopenharmony_ci if (tmp.rc_startblock >= agbno + aglen || 13798c2ecf20Sopenharmony_ci tmp.rc_startblock != *fbno + *flen) 13808c2ecf20Sopenharmony_ci break; 13818c2ecf20Sopenharmony_ci *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno); 13828c2ecf20Sopenharmony_ci } 13838c2ecf20Sopenharmony_ci 13848c2ecf20Sopenharmony_cidone: 13858c2ecf20Sopenharmony_ci trace_xfs_refcount_find_shared_result(cur->bc_mp, 13868c2ecf20Sopenharmony_ci cur->bc_ag.agno, *fbno, *flen); 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_ciout_error: 13898c2ecf20Sopenharmony_ci if (error) 13908c2ecf20Sopenharmony_ci trace_xfs_refcount_find_shared_error(cur->bc_mp, 13918c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 13928c2ecf20Sopenharmony_ci return error; 13938c2ecf20Sopenharmony_ci} 13948c2ecf20Sopenharmony_ci 13958c2ecf20Sopenharmony_ci/* 13968c2ecf20Sopenharmony_ci * Recovering CoW Blocks After a Crash 13978c2ecf20Sopenharmony_ci * 13988c2ecf20Sopenharmony_ci * Due to the way that the copy on write mechanism works, there's a window of 13998c2ecf20Sopenharmony_ci * opportunity in which we can lose track of allocated blocks during a crash. 14008c2ecf20Sopenharmony_ci * Because CoW uses delayed allocation in the in-core CoW fork, writeback 14018c2ecf20Sopenharmony_ci * causes blocks to be allocated and stored in the CoW fork. The blocks are 14028c2ecf20Sopenharmony_ci * no longer in the free space btree but are not otherwise recorded anywhere 14038c2ecf20Sopenharmony_ci * until the write completes and the blocks are mapped into the file. A crash 14048c2ecf20Sopenharmony_ci * in between allocation and remapping results in the replacement blocks being 14058c2ecf20Sopenharmony_ci * lost. This situation is exacerbated by the CoW extent size hint because 14068c2ecf20Sopenharmony_ci * allocations can hang around for long time. 14078c2ecf20Sopenharmony_ci * 14088c2ecf20Sopenharmony_ci * However, there is a place where we can record these allocations before they 14098c2ecf20Sopenharmony_ci * become mappings -- the reference count btree. The btree does not record 14108c2ecf20Sopenharmony_ci * extents with refcount == 1, so we can record allocations with a refcount of 14118c2ecf20Sopenharmony_ci * 1. Blocks being used for CoW writeout cannot be shared, so there should be 14128c2ecf20Sopenharmony_ci * no conflict with shared block records. These mappings should be created 14138c2ecf20Sopenharmony_ci * when we allocate blocks to the CoW fork and deleted when they're removed 14148c2ecf20Sopenharmony_ci * from the CoW fork. 14158c2ecf20Sopenharmony_ci * 14168c2ecf20Sopenharmony_ci * Minor nit: records for in-progress CoW allocations and records for shared 14178c2ecf20Sopenharmony_ci * extents must never be merged, to preserve the property that (except for CoW 14188c2ecf20Sopenharmony_ci * allocations) there are no refcount btree entries with refcount == 1. The 14198c2ecf20Sopenharmony_ci * only time this could potentially happen is when unsharing a block that's 14208c2ecf20Sopenharmony_ci * adjacent to CoW allocations, so we must be careful to avoid this. 14218c2ecf20Sopenharmony_ci * 14228c2ecf20Sopenharmony_ci * At mount time we recover lost CoW allocations by searching the refcount 14238c2ecf20Sopenharmony_ci * btree for these refcount == 1 mappings. These represent CoW allocations 14248c2ecf20Sopenharmony_ci * that were in progress at the time the filesystem went down, so we can free 14258c2ecf20Sopenharmony_ci * them to get the space back. 14268c2ecf20Sopenharmony_ci * 14278c2ecf20Sopenharmony_ci * This mechanism is superior to creating EFIs for unmapped CoW extents for 14288c2ecf20Sopenharmony_ci * several reasons -- first, EFIs pin the tail of the log and would have to be 14298c2ecf20Sopenharmony_ci * periodically relogged to avoid filling up the log. Second, CoW completions 14308c2ecf20Sopenharmony_ci * will have to file an EFD and create new EFIs for whatever remains in the 14318c2ecf20Sopenharmony_ci * CoW fork; this partially takes care of (1) but extent-size reservations 14328c2ecf20Sopenharmony_ci * will have to periodically relog even if there's no writeout in progress. 14338c2ecf20Sopenharmony_ci * This can happen if the CoW extent size hint is set, which you really want. 14348c2ecf20Sopenharmony_ci * Third, EFIs cannot currently be automatically relogged into newer 14358c2ecf20Sopenharmony_ci * transactions to advance the log tail. Fourth, stuffing the log full of 14368c2ecf20Sopenharmony_ci * EFIs places an upper bound on the number of CoW allocations that can be 14378c2ecf20Sopenharmony_ci * held filesystem-wide at any given time. Recording them in the refcount 14388c2ecf20Sopenharmony_ci * btree doesn't require us to maintain any state in memory and doesn't pin 14398c2ecf20Sopenharmony_ci * the log. 14408c2ecf20Sopenharmony_ci */ 14418c2ecf20Sopenharmony_ci/* 14428c2ecf20Sopenharmony_ci * Adjust the refcounts of CoW allocations. These allocations are "magic" 14438c2ecf20Sopenharmony_ci * in that they're not referenced anywhere else in the filesystem, so we 14448c2ecf20Sopenharmony_ci * stash them in the refcount btree with a refcount of 1 until either file 14458c2ecf20Sopenharmony_ci * remapping (or CoW cancellation) happens. 14468c2ecf20Sopenharmony_ci */ 14478c2ecf20Sopenharmony_ciSTATIC int 14488c2ecf20Sopenharmony_cixfs_refcount_adjust_cow_extents( 14498c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 14508c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 14518c2ecf20Sopenharmony_ci xfs_extlen_t aglen, 14528c2ecf20Sopenharmony_ci enum xfs_refc_adjust_op adj) 14538c2ecf20Sopenharmony_ci{ 14548c2ecf20Sopenharmony_ci struct xfs_refcount_irec ext, tmp; 14558c2ecf20Sopenharmony_ci int error; 14568c2ecf20Sopenharmony_ci int found_rec, found_tmp; 14578c2ecf20Sopenharmony_ci 14588c2ecf20Sopenharmony_ci if (aglen == 0) 14598c2ecf20Sopenharmony_ci return 0; 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_ci /* Find any overlapping refcount records */ 14628c2ecf20Sopenharmony_ci error = xfs_refcount_lookup_ge(cur, agbno, &found_rec); 14638c2ecf20Sopenharmony_ci if (error) 14648c2ecf20Sopenharmony_ci goto out_error; 14658c2ecf20Sopenharmony_ci error = xfs_refcount_get_rec(cur, &ext, &found_rec); 14668c2ecf20Sopenharmony_ci if (error) 14678c2ecf20Sopenharmony_ci goto out_error; 14688c2ecf20Sopenharmony_ci if (!found_rec) { 14698c2ecf20Sopenharmony_ci ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks + 14708c2ecf20Sopenharmony_ci XFS_REFC_COW_START; 14718c2ecf20Sopenharmony_ci ext.rc_blockcount = 0; 14728c2ecf20Sopenharmony_ci ext.rc_refcount = 0; 14738c2ecf20Sopenharmony_ci } 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ci switch (adj) { 14768c2ecf20Sopenharmony_ci case XFS_REFCOUNT_ADJUST_COW_ALLOC: 14778c2ecf20Sopenharmony_ci /* Adding a CoW reservation, there should be nothing here. */ 14788c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, 14798c2ecf20Sopenharmony_ci agbno + aglen > ext.rc_startblock)) { 14808c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 14818c2ecf20Sopenharmony_ci goto out_error; 14828c2ecf20Sopenharmony_ci } 14838c2ecf20Sopenharmony_ci 14848c2ecf20Sopenharmony_ci tmp.rc_startblock = agbno; 14858c2ecf20Sopenharmony_ci tmp.rc_blockcount = aglen; 14868c2ecf20Sopenharmony_ci tmp.rc_refcount = 1; 14878c2ecf20Sopenharmony_ci trace_xfs_refcount_modify_extent(cur->bc_mp, 14888c2ecf20Sopenharmony_ci cur->bc_ag.agno, &tmp); 14898c2ecf20Sopenharmony_ci 14908c2ecf20Sopenharmony_ci error = xfs_refcount_insert(cur, &tmp, 14918c2ecf20Sopenharmony_ci &found_tmp); 14928c2ecf20Sopenharmony_ci if (error) 14938c2ecf20Sopenharmony_ci goto out_error; 14948c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_tmp != 1)) { 14958c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 14968c2ecf20Sopenharmony_ci goto out_error; 14978c2ecf20Sopenharmony_ci } 14988c2ecf20Sopenharmony_ci break; 14998c2ecf20Sopenharmony_ci case XFS_REFCOUNT_ADJUST_COW_FREE: 15008c2ecf20Sopenharmony_ci /* Removing a CoW reservation, there should be one extent. */ 15018c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_startblock != agbno)) { 15028c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 15038c2ecf20Sopenharmony_ci goto out_error; 15048c2ecf20Sopenharmony_ci } 15058c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount != aglen)) { 15068c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 15078c2ecf20Sopenharmony_ci goto out_error; 15088c2ecf20Sopenharmony_ci } 15098c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_refcount != 1)) { 15108c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 15118c2ecf20Sopenharmony_ci goto out_error; 15128c2ecf20Sopenharmony_ci } 15138c2ecf20Sopenharmony_ci 15148c2ecf20Sopenharmony_ci ext.rc_refcount = 0; 15158c2ecf20Sopenharmony_ci trace_xfs_refcount_modify_extent(cur->bc_mp, 15168c2ecf20Sopenharmony_ci cur->bc_ag.agno, &ext); 15178c2ecf20Sopenharmony_ci error = xfs_refcount_delete(cur, &found_rec); 15188c2ecf20Sopenharmony_ci if (error) 15198c2ecf20Sopenharmony_ci goto out_error; 15208c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) { 15218c2ecf20Sopenharmony_ci error = -EFSCORRUPTED; 15228c2ecf20Sopenharmony_ci goto out_error; 15238c2ecf20Sopenharmony_ci } 15248c2ecf20Sopenharmony_ci break; 15258c2ecf20Sopenharmony_ci default: 15268c2ecf20Sopenharmony_ci ASSERT(0); 15278c2ecf20Sopenharmony_ci } 15288c2ecf20Sopenharmony_ci 15298c2ecf20Sopenharmony_ci return error; 15308c2ecf20Sopenharmony_ciout_error: 15318c2ecf20Sopenharmony_ci trace_xfs_refcount_modify_extent_error(cur->bc_mp, 15328c2ecf20Sopenharmony_ci cur->bc_ag.agno, error, _RET_IP_); 15338c2ecf20Sopenharmony_ci return error; 15348c2ecf20Sopenharmony_ci} 15358c2ecf20Sopenharmony_ci 15368c2ecf20Sopenharmony_ci/* 15378c2ecf20Sopenharmony_ci * Add or remove refcount btree entries for CoW reservations. 15388c2ecf20Sopenharmony_ci */ 15398c2ecf20Sopenharmony_ciSTATIC int 15408c2ecf20Sopenharmony_cixfs_refcount_adjust_cow( 15418c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 15428c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 15438c2ecf20Sopenharmony_ci xfs_extlen_t aglen, 15448c2ecf20Sopenharmony_ci enum xfs_refc_adjust_op adj) 15458c2ecf20Sopenharmony_ci{ 15468c2ecf20Sopenharmony_ci bool shape_changed; 15478c2ecf20Sopenharmony_ci int error; 15488c2ecf20Sopenharmony_ci 15498c2ecf20Sopenharmony_ci agbno += XFS_REFC_COW_START; 15508c2ecf20Sopenharmony_ci 15518c2ecf20Sopenharmony_ci /* 15528c2ecf20Sopenharmony_ci * Ensure that no rcextents cross the boundary of the adjustment range. 15538c2ecf20Sopenharmony_ci */ 15548c2ecf20Sopenharmony_ci error = xfs_refcount_split_extent(cur, agbno, &shape_changed); 15558c2ecf20Sopenharmony_ci if (error) 15568c2ecf20Sopenharmony_ci goto out_error; 15578c2ecf20Sopenharmony_ci 15588c2ecf20Sopenharmony_ci error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed); 15598c2ecf20Sopenharmony_ci if (error) 15608c2ecf20Sopenharmony_ci goto out_error; 15618c2ecf20Sopenharmony_ci 15628c2ecf20Sopenharmony_ci /* 15638c2ecf20Sopenharmony_ci * Try to merge with the left or right extents of the range. 15648c2ecf20Sopenharmony_ci */ 15658c2ecf20Sopenharmony_ci error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj, 15668c2ecf20Sopenharmony_ci XFS_FIND_RCEXT_COW, &shape_changed); 15678c2ecf20Sopenharmony_ci if (error) 15688c2ecf20Sopenharmony_ci goto out_error; 15698c2ecf20Sopenharmony_ci 15708c2ecf20Sopenharmony_ci /* Now that we've taken care of the ends, adjust the middle extents */ 15718c2ecf20Sopenharmony_ci error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj); 15728c2ecf20Sopenharmony_ci if (error) 15738c2ecf20Sopenharmony_ci goto out_error; 15748c2ecf20Sopenharmony_ci 15758c2ecf20Sopenharmony_ci return 0; 15768c2ecf20Sopenharmony_ci 15778c2ecf20Sopenharmony_ciout_error: 15788c2ecf20Sopenharmony_ci trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_ag.agno, 15798c2ecf20Sopenharmony_ci error, _RET_IP_); 15808c2ecf20Sopenharmony_ci return error; 15818c2ecf20Sopenharmony_ci} 15828c2ecf20Sopenharmony_ci 15838c2ecf20Sopenharmony_ci/* 15848c2ecf20Sopenharmony_ci * Record a CoW allocation in the refcount btree. 15858c2ecf20Sopenharmony_ci */ 15868c2ecf20Sopenharmony_ciSTATIC int 15878c2ecf20Sopenharmony_ci__xfs_refcount_cow_alloc( 15888c2ecf20Sopenharmony_ci struct xfs_btree_cur *rcur, 15898c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 15908c2ecf20Sopenharmony_ci xfs_extlen_t aglen) 15918c2ecf20Sopenharmony_ci{ 15928c2ecf20Sopenharmony_ci trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_ag.agno, 15938c2ecf20Sopenharmony_ci agbno, aglen); 15948c2ecf20Sopenharmony_ci 15958c2ecf20Sopenharmony_ci /* Add refcount btree reservation */ 15968c2ecf20Sopenharmony_ci return xfs_refcount_adjust_cow(rcur, agbno, aglen, 15978c2ecf20Sopenharmony_ci XFS_REFCOUNT_ADJUST_COW_ALLOC); 15988c2ecf20Sopenharmony_ci} 15998c2ecf20Sopenharmony_ci 16008c2ecf20Sopenharmony_ci/* 16018c2ecf20Sopenharmony_ci * Remove a CoW allocation from the refcount btree. 16028c2ecf20Sopenharmony_ci */ 16038c2ecf20Sopenharmony_ciSTATIC int 16048c2ecf20Sopenharmony_ci__xfs_refcount_cow_free( 16058c2ecf20Sopenharmony_ci struct xfs_btree_cur *rcur, 16068c2ecf20Sopenharmony_ci xfs_agblock_t agbno, 16078c2ecf20Sopenharmony_ci xfs_extlen_t aglen) 16088c2ecf20Sopenharmony_ci{ 16098c2ecf20Sopenharmony_ci trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_ag.agno, 16108c2ecf20Sopenharmony_ci agbno, aglen); 16118c2ecf20Sopenharmony_ci 16128c2ecf20Sopenharmony_ci /* Remove refcount btree reservation */ 16138c2ecf20Sopenharmony_ci return xfs_refcount_adjust_cow(rcur, agbno, aglen, 16148c2ecf20Sopenharmony_ci XFS_REFCOUNT_ADJUST_COW_FREE); 16158c2ecf20Sopenharmony_ci} 16168c2ecf20Sopenharmony_ci 16178c2ecf20Sopenharmony_ci/* Record a CoW staging extent in the refcount btree. */ 16188c2ecf20Sopenharmony_civoid 16198c2ecf20Sopenharmony_cixfs_refcount_alloc_cow_extent( 16208c2ecf20Sopenharmony_ci struct xfs_trans *tp, 16218c2ecf20Sopenharmony_ci xfs_fsblock_t fsb, 16228c2ecf20Sopenharmony_ci xfs_extlen_t len) 16238c2ecf20Sopenharmony_ci{ 16248c2ecf20Sopenharmony_ci struct xfs_mount *mp = tp->t_mountp; 16258c2ecf20Sopenharmony_ci 16268c2ecf20Sopenharmony_ci if (!xfs_sb_version_hasreflink(&mp->m_sb)) 16278c2ecf20Sopenharmony_ci return; 16288c2ecf20Sopenharmony_ci 16298c2ecf20Sopenharmony_ci __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len); 16308c2ecf20Sopenharmony_ci 16318c2ecf20Sopenharmony_ci /* Add rmap entry */ 16328c2ecf20Sopenharmony_ci xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb), 16338c2ecf20Sopenharmony_ci XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW); 16348c2ecf20Sopenharmony_ci} 16358c2ecf20Sopenharmony_ci 16368c2ecf20Sopenharmony_ci/* Forget a CoW staging event in the refcount btree. */ 16378c2ecf20Sopenharmony_civoid 16388c2ecf20Sopenharmony_cixfs_refcount_free_cow_extent( 16398c2ecf20Sopenharmony_ci struct xfs_trans *tp, 16408c2ecf20Sopenharmony_ci xfs_fsblock_t fsb, 16418c2ecf20Sopenharmony_ci xfs_extlen_t len) 16428c2ecf20Sopenharmony_ci{ 16438c2ecf20Sopenharmony_ci struct xfs_mount *mp = tp->t_mountp; 16448c2ecf20Sopenharmony_ci 16458c2ecf20Sopenharmony_ci if (!xfs_sb_version_hasreflink(&mp->m_sb)) 16468c2ecf20Sopenharmony_ci return; 16478c2ecf20Sopenharmony_ci 16488c2ecf20Sopenharmony_ci /* Remove rmap entry */ 16498c2ecf20Sopenharmony_ci xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb), 16508c2ecf20Sopenharmony_ci XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW); 16518c2ecf20Sopenharmony_ci __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len); 16528c2ecf20Sopenharmony_ci} 16538c2ecf20Sopenharmony_ci 16548c2ecf20Sopenharmony_cistruct xfs_refcount_recovery { 16558c2ecf20Sopenharmony_ci struct list_head rr_list; 16568c2ecf20Sopenharmony_ci struct xfs_refcount_irec rr_rrec; 16578c2ecf20Sopenharmony_ci}; 16588c2ecf20Sopenharmony_ci 16598c2ecf20Sopenharmony_ci/* Stuff an extent on the recovery list. */ 16608c2ecf20Sopenharmony_ciSTATIC int 16618c2ecf20Sopenharmony_cixfs_refcount_recover_extent( 16628c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 16638c2ecf20Sopenharmony_ci union xfs_btree_rec *rec, 16648c2ecf20Sopenharmony_ci void *priv) 16658c2ecf20Sopenharmony_ci{ 16668c2ecf20Sopenharmony_ci struct list_head *debris = priv; 16678c2ecf20Sopenharmony_ci struct xfs_refcount_recovery *rr; 16688c2ecf20Sopenharmony_ci 16698c2ecf20Sopenharmony_ci if (XFS_IS_CORRUPT(cur->bc_mp, 16708c2ecf20Sopenharmony_ci be32_to_cpu(rec->refc.rc_refcount) != 1)) 16718c2ecf20Sopenharmony_ci return -EFSCORRUPTED; 16728c2ecf20Sopenharmony_ci 16738c2ecf20Sopenharmony_ci rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), 0); 16748c2ecf20Sopenharmony_ci xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec); 16758c2ecf20Sopenharmony_ci list_add_tail(&rr->rr_list, debris); 16768c2ecf20Sopenharmony_ci 16778c2ecf20Sopenharmony_ci return 0; 16788c2ecf20Sopenharmony_ci} 16798c2ecf20Sopenharmony_ci 16808c2ecf20Sopenharmony_ci/* Find and remove leftover CoW reservations. */ 16818c2ecf20Sopenharmony_ciint 16828c2ecf20Sopenharmony_cixfs_refcount_recover_cow_leftovers( 16838c2ecf20Sopenharmony_ci struct xfs_mount *mp, 16848c2ecf20Sopenharmony_ci xfs_agnumber_t agno) 16858c2ecf20Sopenharmony_ci{ 16868c2ecf20Sopenharmony_ci struct xfs_trans *tp; 16878c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur; 16888c2ecf20Sopenharmony_ci struct xfs_buf *agbp; 16898c2ecf20Sopenharmony_ci struct xfs_refcount_recovery *rr, *n; 16908c2ecf20Sopenharmony_ci struct list_head debris; 16918c2ecf20Sopenharmony_ci union xfs_btree_irec low; 16928c2ecf20Sopenharmony_ci union xfs_btree_irec high; 16938c2ecf20Sopenharmony_ci xfs_fsblock_t fsb; 16948c2ecf20Sopenharmony_ci xfs_agblock_t agbno; 16958c2ecf20Sopenharmony_ci int error; 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_ci if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START) 16988c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 16998c2ecf20Sopenharmony_ci 17008c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&debris); 17018c2ecf20Sopenharmony_ci 17028c2ecf20Sopenharmony_ci /* 17038c2ecf20Sopenharmony_ci * In this first part, we use an empty transaction to gather up 17048c2ecf20Sopenharmony_ci * all the leftover CoW extents so that we can subsequently 17058c2ecf20Sopenharmony_ci * delete them. The empty transaction is used to avoid 17068c2ecf20Sopenharmony_ci * a buffer lock deadlock if there happens to be a loop in the 17078c2ecf20Sopenharmony_ci * refcountbt because we're allowed to re-grab a buffer that is 17088c2ecf20Sopenharmony_ci * already attached to our transaction. When we're done 17098c2ecf20Sopenharmony_ci * recording the CoW debris we cancel the (empty) transaction 17108c2ecf20Sopenharmony_ci * and everything goes away cleanly. 17118c2ecf20Sopenharmony_ci */ 17128c2ecf20Sopenharmony_ci error = xfs_trans_alloc_empty(mp, &tp); 17138c2ecf20Sopenharmony_ci if (error) 17148c2ecf20Sopenharmony_ci return error; 17158c2ecf20Sopenharmony_ci 17168c2ecf20Sopenharmony_ci error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); 17178c2ecf20Sopenharmony_ci if (error) 17188c2ecf20Sopenharmony_ci goto out_trans; 17198c2ecf20Sopenharmony_ci cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno); 17208c2ecf20Sopenharmony_ci 17218c2ecf20Sopenharmony_ci /* Find all the leftover CoW staging extents. */ 17228c2ecf20Sopenharmony_ci memset(&low, 0, sizeof(low)); 17238c2ecf20Sopenharmony_ci memset(&high, 0, sizeof(high)); 17248c2ecf20Sopenharmony_ci low.rc.rc_startblock = XFS_REFC_COW_START; 17258c2ecf20Sopenharmony_ci high.rc.rc_startblock = -1U; 17268c2ecf20Sopenharmony_ci error = xfs_btree_query_range(cur, &low, &high, 17278c2ecf20Sopenharmony_ci xfs_refcount_recover_extent, &debris); 17288c2ecf20Sopenharmony_ci xfs_btree_del_cursor(cur, error); 17298c2ecf20Sopenharmony_ci xfs_trans_brelse(tp, agbp); 17308c2ecf20Sopenharmony_ci xfs_trans_cancel(tp); 17318c2ecf20Sopenharmony_ci if (error) 17328c2ecf20Sopenharmony_ci goto out_free; 17338c2ecf20Sopenharmony_ci 17348c2ecf20Sopenharmony_ci /* Now iterate the list to free the leftovers */ 17358c2ecf20Sopenharmony_ci list_for_each_entry_safe(rr, n, &debris, rr_list) { 17368c2ecf20Sopenharmony_ci /* Set up transaction. */ 17378c2ecf20Sopenharmony_ci error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp); 17388c2ecf20Sopenharmony_ci if (error) 17398c2ecf20Sopenharmony_ci goto out_free; 17408c2ecf20Sopenharmony_ci 17418c2ecf20Sopenharmony_ci trace_xfs_refcount_recover_extent(mp, agno, &rr->rr_rrec); 17428c2ecf20Sopenharmony_ci 17438c2ecf20Sopenharmony_ci /* Free the orphan record */ 17448c2ecf20Sopenharmony_ci agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START; 17458c2ecf20Sopenharmony_ci fsb = XFS_AGB_TO_FSB(mp, agno, agbno); 17468c2ecf20Sopenharmony_ci xfs_refcount_free_cow_extent(tp, fsb, 17478c2ecf20Sopenharmony_ci rr->rr_rrec.rc_blockcount); 17488c2ecf20Sopenharmony_ci 17498c2ecf20Sopenharmony_ci /* Free the block. */ 17508c2ecf20Sopenharmony_ci xfs_bmap_add_free(tp, fsb, rr->rr_rrec.rc_blockcount, NULL); 17518c2ecf20Sopenharmony_ci 17528c2ecf20Sopenharmony_ci error = xfs_trans_commit(tp); 17538c2ecf20Sopenharmony_ci if (error) 17548c2ecf20Sopenharmony_ci goto out_free; 17558c2ecf20Sopenharmony_ci 17568c2ecf20Sopenharmony_ci list_del(&rr->rr_list); 17578c2ecf20Sopenharmony_ci kmem_free(rr); 17588c2ecf20Sopenharmony_ci } 17598c2ecf20Sopenharmony_ci 17608c2ecf20Sopenharmony_ci return error; 17618c2ecf20Sopenharmony_ciout_trans: 17628c2ecf20Sopenharmony_ci xfs_trans_cancel(tp); 17638c2ecf20Sopenharmony_ciout_free: 17648c2ecf20Sopenharmony_ci /* Free the leftover list */ 17658c2ecf20Sopenharmony_ci list_for_each_entry_safe(rr, n, &debris, rr_list) { 17668c2ecf20Sopenharmony_ci list_del(&rr->rr_list); 17678c2ecf20Sopenharmony_ci kmem_free(rr); 17688c2ecf20Sopenharmony_ci } 17698c2ecf20Sopenharmony_ci return error; 17708c2ecf20Sopenharmony_ci} 17718c2ecf20Sopenharmony_ci 17728c2ecf20Sopenharmony_ci/* Is there a record covering a given extent? */ 17738c2ecf20Sopenharmony_ciint 17748c2ecf20Sopenharmony_cixfs_refcount_has_record( 17758c2ecf20Sopenharmony_ci struct xfs_btree_cur *cur, 17768c2ecf20Sopenharmony_ci xfs_agblock_t bno, 17778c2ecf20Sopenharmony_ci xfs_extlen_t len, 17788c2ecf20Sopenharmony_ci bool *exists) 17798c2ecf20Sopenharmony_ci{ 17808c2ecf20Sopenharmony_ci union xfs_btree_irec low; 17818c2ecf20Sopenharmony_ci union xfs_btree_irec high; 17828c2ecf20Sopenharmony_ci 17838c2ecf20Sopenharmony_ci memset(&low, 0, sizeof(low)); 17848c2ecf20Sopenharmony_ci low.rc.rc_startblock = bno; 17858c2ecf20Sopenharmony_ci memset(&high, 0xFF, sizeof(high)); 17868c2ecf20Sopenharmony_ci high.rc.rc_startblock = bno + len - 1; 17878c2ecf20Sopenharmony_ci 17888c2ecf20Sopenharmony_ci return xfs_btree_has_record(cur, &low, &high, exists); 17898c2ecf20Sopenharmony_ci} 1790