Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (c) 2014 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "xfs_bit.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "xfs_defer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "xfs_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "xfs_alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "xfs_rmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "xfs_rmap_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "xfs_errortag.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * Lookup the first record less than or equal to [bno, len, owner, offset]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * in the btree given by cur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) xfs_rmap_lookup_le(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	unsigned int		flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	int			*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	cur->bc_rec.r.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	cur->bc_rec.r.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	cur->bc_rec.r.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	cur->bc_rec.r.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	cur->bc_rec.r.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * Lookup the record exactly matching [bno, len, owner, offset]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * in the btree given by cur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) xfs_rmap_lookup_eq(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	unsigned int		flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	int			*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	cur->bc_rec.r.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	cur->bc_rec.r.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	cur->bc_rec.r.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	cur->bc_rec.r.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	cur->bc_rec.r.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * Update the record referred to by cur to the value given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * by [bno, len, owner, offset].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * This either works (return 0) or gets an EFSCORRUPTED error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) xfs_rmap_update(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	struct xfs_rmap_irec	*irec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	union xfs_btree_rec	rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	trace_xfs_rmap_update(cur->bc_mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			irec->rm_startblock, irec->rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 			irec->rm_owner, irec->rm_offset, irec->rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	rec.rmap.rm_offset = cpu_to_be64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 			xfs_rmap_irec_offset_pack(irec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	error = xfs_btree_update(cur, &rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		trace_xfs_rmap_update_error(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 				cur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) xfs_rmap_insert(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	struct xfs_btree_cur	*rcur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	xfs_agblock_t		agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	unsigned int		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	int			i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	trace_xfs_rmap_insert(rcur->bc_mp, rcur->bc_ag.agno, agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 			len, owner, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	if (XFS_IS_CORRUPT(rcur->bc_mp, i != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	rcur->bc_rec.r.rm_startblock = agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	rcur->bc_rec.r.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	rcur->bc_rec.r.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	rcur->bc_rec.r.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	rcur->bc_rec.r.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	error = xfs_btree_insert(rcur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	if (XFS_IS_CORRUPT(rcur->bc_mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		trace_xfs_rmap_insert_error(rcur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 				rcur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) xfs_rmap_delete(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	struct xfs_btree_cur	*rcur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	xfs_agblock_t		agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	unsigned int		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	int			i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	trace_xfs_rmap_delete(rcur->bc_mp, rcur->bc_ag.agno, agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			len, owner, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	if (XFS_IS_CORRUPT(rcur->bc_mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	error = xfs_btree_delete(rcur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	if (XFS_IS_CORRUPT(rcur->bc_mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		trace_xfs_rmap_delete_error(rcur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 				rcur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) /* Convert an internal btree record to an rmap record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) xfs_rmap_btrec_to_irec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	union xfs_btree_rec	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct xfs_rmap_irec	*irec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			irec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  * Get the data from the pointed-to record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) xfs_rmap_get_rec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	struct xfs_rmap_irec	*irec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	int			*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	struct xfs_mount	*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	xfs_agnumber_t		agno = cur->bc_ag.agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	union xfs_btree_rec	*rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	error = xfs_btree_get_rec(cur, &rec, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	if (error || !*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	if (xfs_rmap_btrec_to_irec(rec, irec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	if (irec->rm_blockcount == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	if (irec->rm_startblock <= XFS_AGFL_BLOCK(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		if (irec->rm_owner != XFS_RMAP_OWN_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		if (irec->rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		/* check for valid extent range, including overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		if (!xfs_verify_agbno(mp, agno, irec->rm_startblock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		if (irec->rm_startblock >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 				irec->rm_startblock + irec->rm_blockcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		if (!xfs_verify_agbno(mp, agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 				irec->rm_startblock + irec->rm_blockcount - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (!(xfs_verify_ino(mp, irec->rm_owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	      (irec->rm_owner <= XFS_RMAP_OWN_FS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	       irec->rm_owner >= XFS_RMAP_OWN_MIN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		goto out_bad_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) out_bad_rec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	xfs_warn(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		"Reverse Mapping BTree record corruption in AG %d detected!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	xfs_warn(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		"Owner 0x%llx, flags 0x%x, start block 0x%x block count 0x%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		irec->rm_owner, irec->rm_flags, irec->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		irec->rm_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) struct xfs_find_left_neighbor_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct xfs_rmap_irec	high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	struct xfs_rmap_irec	*irec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	int			*stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) /* For each rmap given, figure out if it matches the key we want. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) xfs_rmap_find_left_neighbor_helper(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	struct xfs_rmap_irec	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	void			*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct xfs_find_left_neighbor_info	*info = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	trace_xfs_rmap_find_left_neighbor_candidate(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			cur->bc_ag.agno, rec->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			rec->rm_blockcount, rec->rm_owner, rec->rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			rec->rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (rec->rm_owner != info->high.rm_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	    !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	    rec->rm_offset + rec->rm_blockcount - 1 != info->high.rm_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	*info->irec = *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	*info->stat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  * Find the record to the left of the given extent, being careful only to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  * return a match with the same owner and adjacent physical and logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  * block ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) xfs_rmap_find_left_neighbor(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	unsigned int		flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct xfs_rmap_irec	*irec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	int			*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct xfs_find_left_neighbor_info	info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	*stat = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (bno == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	info.high.rm_startblock = bno - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	info.high.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (!XFS_RMAP_NON_INODE_OWNER(owner) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	    !(flags & XFS_RMAP_BMBT_BLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		if (offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		info.high.rm_offset = offset - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		info.high.rm_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	info.high.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	info.high.rm_blockcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	info.irec = irec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	info.stat = stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	trace_xfs_rmap_find_left_neighbor_query(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 			cur->bc_ag.agno, bno, 0, owner, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	error = xfs_rmap_query_range(cur, &info.high, &info.high,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			xfs_rmap_find_left_neighbor_helper, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (error == -ECANCELED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	if (*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 				cur->bc_ag.agno, irec->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				irec->rm_blockcount, irec->rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 				irec->rm_offset, irec->rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) /* For each rmap given, figure out if it matches the key we want. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) xfs_rmap_lookup_le_range_helper(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	struct xfs_rmap_irec	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	void			*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	struct xfs_find_left_neighbor_info	*info = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	trace_xfs_rmap_lookup_le_range_candidate(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			cur->bc_ag.agno, rec->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			rec->rm_blockcount, rec->rm_owner, rec->rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			rec->rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (rec->rm_owner != info->high.rm_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	    !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	    (rec->rm_offset > info->high.rm_offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	     rec->rm_offset + rec->rm_blockcount <= info->high.rm_offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	*info->irec = *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	*info->stat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * Find the record to the left of the given extent, being careful only to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * return a match with the same owner and overlapping physical and logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  * block ranges.  This is the overlapping-interval version of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * xfs_rmap_lookup_le.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) xfs_rmap_lookup_le_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	unsigned int		flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	struct xfs_rmap_irec	*irec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	int			*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct xfs_find_left_neighbor_info	info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	info.high.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	info.high.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	if (!XFS_RMAP_NON_INODE_OWNER(owner) && !(flags & XFS_RMAP_BMBT_BLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		info.high.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		info.high.rm_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	info.high.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	info.high.rm_blockcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	*stat = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	info.irec = irec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	info.stat = stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	trace_xfs_rmap_lookup_le_range(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			cur->bc_ag.agno, bno, 0, owner, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	error = xfs_rmap_query_range(cur, &info.high, &info.high,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			xfs_rmap_lookup_le_range_helper, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (error == -ECANCELED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (*stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 				cur->bc_ag.agno, irec->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 				irec->rm_blockcount, irec->rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 				irec->rm_offset, irec->rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  * Perform all the relevant owner checks for a removal op.  If we're doing an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403)  * unknown-owner removal then we have no owner information to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) xfs_rmap_free_check_owner(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	uint64_t		ltoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	struct xfs_rmap_irec	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	xfs_filblks_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	uint64_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	unsigned int		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	if (owner == XFS_RMAP_OWN_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	/* Make sure the unwritten flag matches. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 			   (flags & XFS_RMAP_UNWRITTEN) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			   (rec->rm_flags & XFS_RMAP_UNWRITTEN))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	/* Make sure the owner matches what we expect to find in the tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	if (XFS_IS_CORRUPT(mp, owner != rec->rm_owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	/* Check the offset, if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (XFS_RMAP_NON_INODE_OWNER(owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	if (flags & XFS_RMAP_BMBT_BLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 				   !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		if (XFS_IS_CORRUPT(mp, rec->rm_offset > offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 				   offset + len > ltoff + rec->rm_blockcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461)  * Find the extent in the rmap btree and remove it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463)  * The record we find should always be an exact match for the extent that we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464)  * looking for, since we insert them into the btree without modification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466)  * Special Case #1: when growing the filesystem, we "free" an extent when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467)  * growing the last AG. This extent is new space and so it is not tracked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468)  * used space in the btree. The growfs code will pass in an owner of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469)  * XFS_RMAP_OWN_NULL to indicate that it expected that there is no owner of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470)  * extent. We verify that - the extent lookup result in a record that does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471)  * overlap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473)  * Special Case #2: EFIs do not record the owner of the extent, so when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474)  * recovering EFIs from the log we pass in XFS_RMAP_OWN_UNKNOWN to tell the rmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475)  * btree to ignore the owner (i.e. wildcard match) so we don't trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476)  * corruption checks during log recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) xfs_rmap_unmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	bool				unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	struct xfs_mount		*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	struct xfs_rmap_irec		ltrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	uint64_t			ltoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	int				i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	uint64_t			owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	uint64_t			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	unsigned int			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	bool				ignore_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 			(flags & XFS_RMAP_BMBT_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	if (unwritten)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		flags |= XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	trace_xfs_rmap_unmap(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	 * We should always have a left record because there's a static record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	 * for the AG headers at rm_startblock == 0 created by mkfs/growfs that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	 * will not ever be removed from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	error = xfs_rmap_get_rec(cur, &ltrec, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			cur->bc_ag.agno, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			ltrec.rm_offset, ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	ltoff = ltrec.rm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	 * For growfs, the incoming extent must be beyond the left record we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	 * just found as it is new space and won't be used by anyone. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	 * just a corruption check as we don't actually do anything with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	 * extent.  Note that we need to use >= instead of > because it might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	 * be the case that the "left" extent goes all the way to EOFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (owner == XFS_RMAP_OWN_NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 				   bno <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 				   ltrec.rm_startblock + ltrec.rm_blockcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	 * If we're doing an unknown-owner removal for EFI recovery, we expect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	 * to find the full range in the rmapbt or nothing at all.  If we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	 * don't find any rmaps overlapping either end of the range, we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	 * done.  Hopefully this means that the EFI creator already queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	 * (and finished) a RUI to remove the rmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (owner == XFS_RMAP_OWN_UNKNOWN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	    ltrec.rm_startblock + ltrec.rm_blockcount <= bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		struct xfs_rmap_irec    rtrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 			goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		error = xfs_rmap_get_rec(cur, &rtrec, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		if (rtrec.rm_startblock >= bno + len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	/* Make sure the extent we found covers the entire freeing range. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			   ltrec.rm_startblock > bno ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			   ltrec.rm_startblock + ltrec.rm_blockcount <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			   bno + len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	/* Check owner information. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	error = xfs_rmap_free_check_owner(mp, ltoff, &ltrec, len, owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		/* exact match, simply remove the record from rmap tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		trace_xfs_rmap_delete(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 				ltrec.rm_startblock, ltrec.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 				ltrec.rm_owner, ltrec.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 				ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		error = xfs_btree_delete(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	} else if (ltrec.rm_startblock == bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		 * overlap left hand side of extent: move the start, trim the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		 * length and update the current record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		 *       ltbno                ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		 * Orig:    |oooooooooooooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		 * Freeing: |fffffffff|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		 * Result:            |rrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		 *         bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		ltrec.rm_startblock += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		ltrec.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if (!ignore_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			ltrec.rm_offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	} else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		 * overlap right hand side of extent: trim the length and update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		 * the current record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		 *       ltbno                ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		 * Orig:    |oooooooooooooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		 * Freeing:            |fffffffff|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		 * Result:  |rrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		 *                    bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		ltrec.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		 * overlap middle of extent: trim the length of the existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		 * record to the length of the new left-extent size, increment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		 * the insertion position so we can insert a new record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		 * containing the remaining right-extent space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		 *       ltbno                ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		 * Orig:    |oooooooooooooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		 * Freeing:       |fffffffff|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		 * Result:  |rrrrr|         |rrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		 *               bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		xfs_extlen_t	orig_len = ltrec.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		ltrec.rm_blockcount = bno - ltrec.rm_startblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		cur->bc_rec.r.rm_startblock = bno + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		cur->bc_rec.r.rm_blockcount = orig_len - len -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 						     ltrec.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		cur->bc_rec.r.rm_owner = ltrec.rm_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		if (ignore_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			cur->bc_rec.r.rm_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			cur->bc_rec.r.rm_offset = offset + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		cur->bc_rec.r.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		trace_xfs_rmap_insert(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 				cur->bc_rec.r.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 				cur->bc_rec.r.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 				cur->bc_rec.r.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 				cur->bc_rec.r.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 				cur->bc_rec.r.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		error = xfs_btree_insert(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) out_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	trace_xfs_rmap_unmap_done(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		trace_xfs_rmap_unmap_error(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 				error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691)  * Remove a reference to an extent in the rmap btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) xfs_rmap_free(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	struct xfs_trans		*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	struct xfs_buf			*agbp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	xfs_agnumber_t			agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	struct xfs_mount		*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	struct xfs_btree_cur		*cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	error = xfs_rmap_unmap(cur, bno, len, false, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	xfs_btree_del_cursor(cur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718)  * A mergeable rmap must have the same owner and the same values for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719)  * the unwritten, attr_fork, and bmbt flags.  The startblock and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720)  * offset are checked separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) xfs_rmap_is_mergeable(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	struct xfs_rmap_irec	*irec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	uint64_t		owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	unsigned int		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (irec->rm_owner == XFS_RMAP_OWN_NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (irec->rm_owner != owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if ((flags & XFS_RMAP_UNWRITTEN) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	    (irec->rm_flags & XFS_RMAP_UNWRITTEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	if ((flags & XFS_RMAP_ATTR_FORK) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	    (irec->rm_flags & XFS_RMAP_ATTR_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	if ((flags & XFS_RMAP_BMBT_BLOCK) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	    (irec->rm_flags & XFS_RMAP_BMBT_BLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745)  * When we allocate a new block, the first thing we do is add a reference to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746)  * the extent in the rmap btree. This takes the form of a [agbno, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747)  * owner, offset] record.  Flags are encoded in the high bits of the offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748)  * field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) xfs_rmap_map(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	bool				unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	struct xfs_mount		*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	struct xfs_rmap_irec		ltrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	struct xfs_rmap_irec		gtrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	int				have_gt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	int				have_lt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	int				i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	uint64_t			owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	uint64_t			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	unsigned int			flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	bool				ignore_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	ASSERT(owner != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			(flags & XFS_RMAP_BMBT_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	if (unwritten)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		flags |= XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	trace_xfs_rmap_map(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	ASSERT(!xfs_rmap_should_skip_owner_update(oinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	 * For the initial lookup, look for an exact match or the left-adjacent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	 * record for our insertion point. This will also give us the record for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	 * start block contiguity tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			&have_lt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (have_lt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		error = xfs_rmap_get_rec(cur, &ltrec, &have_lt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		if (XFS_IS_CORRUPT(mp, have_lt != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 				cur->bc_ag.agno, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 				ltrec.rm_offset, ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		if (!xfs_rmap_is_mergeable(&ltrec, owner, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			have_lt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			   have_lt != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			   ltrec.rm_startblock + ltrec.rm_blockcount > bno)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	 * Increment the cursor to see if we have a right-adjacent record to our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	 * insertion point. This will give us the record for end block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	 * contiguity tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	error = xfs_btree_increment(cur, 0, &have_gt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	if (have_gt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		if (XFS_IS_CORRUPT(mp, have_gt != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		if (XFS_IS_CORRUPT(mp, bno + len > gtrec.rm_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			cur->bc_ag.agno, gtrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			gtrec.rm_blockcount, gtrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			gtrec.rm_offset, gtrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			have_gt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	 * Note: cursor currently points one record to the right of ltrec, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	 * if there is no record in the tree to the right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	if (have_lt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	    ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	    (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		 * left edge contiguous, merge into left record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		 *       ltbno     ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		 * orig:   |ooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		 * adding:           |aaaaaaaaa|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		 * result: |rrrrrrrrrrrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		 *                  bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		ltrec.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		if (have_gt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		    bno + len == gtrec.rm_startblock &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		    (ignore_off || offset + len == gtrec.rm_offset) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		    (unsigned long)ltrec.rm_blockcount + len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 				gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			 * right edge also contiguous, delete right record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			 * and merge into left record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			 *       ltbno     ltlen    gtbno     gtlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			 * orig:   |ooooooooo|         |ooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			 * adding:           |aaaaaaaaa|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			 * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			ltrec.rm_blockcount += gtrec.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			trace_xfs_rmap_delete(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 					gtrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 					gtrec.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 					gtrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 					gtrec.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 					gtrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			error = xfs_btree_delete(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 				goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 				error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 				goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		/* point the cursor back to the left record and update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		error = xfs_btree_decrement(cur, 0, &have_gt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	} else if (have_gt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		   bno + len == gtrec.rm_startblock &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		   (ignore_off || offset + len == gtrec.rm_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		 * right edge contiguous, merge into right record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		 *                 gtbno     gtlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		 * Orig:             |ooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		 * adding: |aaaaaaaaa|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		 * Result: |rrrrrrrrrrrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		 *        bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		gtrec.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		gtrec.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		if (!ignore_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			gtrec.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		error = xfs_rmap_update(cur, &gtrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		 * no contiguous edge with identical owner, insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		 * new record at current cursor position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		cur->bc_rec.r.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		cur->bc_rec.r.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		cur->bc_rec.r.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		cur->bc_rec.r.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		cur->bc_rec.r.rm_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		trace_xfs_rmap_insert(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			owner, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		error = xfs_btree_insert(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	trace_xfs_rmap_map_done(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		trace_xfs_rmap_map_error(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 				error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945)  * Add a reference to an extent in the rmap btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) xfs_rmap_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct xfs_trans		*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	struct xfs_buf			*agbp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	xfs_agnumber_t			agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	struct xfs_mount		*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	struct xfs_btree_cur		*cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	error = xfs_rmap_map(cur, bno, len, false, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	xfs_btree_del_cursor(cur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) #define RMAP_LEFT_CONTIG	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) #define RMAP_RIGHT_CONTIG	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) #define RMAP_LEFT_FILLING	(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) #define RMAP_RIGHT_FILLING	(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) #define RMAP_LEFT_VALID		(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) #define RMAP_RIGHT_VALID	(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) #define LEFT		r[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) #define RIGHT		r[1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) #define PREV		r[2]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) #define NEW		r[3]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983)  * Convert an unwritten extent to a real extent or vice versa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984)  * Does not handle overlapping extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) xfs_rmap_convert(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	bool				unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	struct xfs_mount		*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	struct xfs_rmap_irec		r[4];	/* neighbor extent entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 						/* left is 0, right is 1, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 						/* prev is 2, new is 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	uint64_t		owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	uint64_t		offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	uint64_t		new_endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	unsigned int		oldext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	unsigned int		newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	unsigned int		flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	int			i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	int			state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			(flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	new_endoff = offset + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	trace_xfs_rmap_convert(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	 * For the initial lookup, look for an exact match or the left-adjacent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	 * record for our insertion point. This will also give us the record for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	 * start block contiguity tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	error = xfs_rmap_get_rec(cur, &PREV, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			cur->bc_ag.agno, PREV.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 			PREV.rm_blockcount, PREV.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			PREV.rm_offset, PREV.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	ASSERT(PREV.rm_offset <= offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	newext = ~oldext & XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 * Set flags determining what part of the previous oldext allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 * extent is being replaced by a newext allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (PREV.rm_offset == offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		state |= RMAP_LEFT_FILLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		state |= RMAP_RIGHT_FILLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	 * Decrement the cursor to see if we have a left-adjacent record to our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	 * insertion point. This will give us the record for end block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	 * contiguity tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	error = xfs_btree_decrement(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	if (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		state |= RMAP_LEFT_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		error = xfs_rmap_get_rec(cur, &LEFT, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				   LEFT.rm_startblock + LEFT.rm_blockcount >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 				   bno)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 				cur->bc_ag.agno, LEFT.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 				LEFT.rm_blockcount, LEFT.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 				LEFT.rm_offset, LEFT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		if (LEFT.rm_startblock + LEFT.rm_blockcount == bno &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		    LEFT.rm_offset + LEFT.rm_blockcount == offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		    xfs_rmap_is_mergeable(&LEFT, owner, newext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			state |= RMAP_LEFT_CONTIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	 * Increment the cursor to see if we have a right-adjacent record to our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	 * insertion point. This will give us the record for end block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	 * contiguity tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	if (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		state |= RMAP_RIGHT_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		error = xfs_rmap_get_rec(cur, &RIGHT, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		if (XFS_IS_CORRUPT(mp, bno + len > RIGHT.rm_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 				cur->bc_ag.agno, RIGHT.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 				RIGHT.rm_blockcount, RIGHT.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 				RIGHT.rm_offset, RIGHT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		if (bno + len == RIGHT.rm_startblock &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		    offset + len == RIGHT.rm_offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		    xfs_rmap_is_mergeable(&RIGHT, owner, newext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 			state |= RMAP_RIGHT_CONTIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	/* check that left + prev + right is not too long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 			 RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	    (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	     RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	    (unsigned long)LEFT.rm_blockcount + len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	     RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		state &= ~RMAP_RIGHT_CONTIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	trace_xfs_rmap_convert_state(mp, cur->bc_ag.agno, state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			_RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	/* reset the cursor back to PREV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	 * Switch out based on the FILLING and CONTIG state bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			 RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	     RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		 * The left and right neighbors are both contiguous with new.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		trace_xfs_rmap_delete(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 				RIGHT.rm_startblock, RIGHT.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 				RIGHT.rm_owner, RIGHT.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 				RIGHT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		error = xfs_btree_delete(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		error = xfs_btree_decrement(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		trace_xfs_rmap_delete(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 				PREV.rm_startblock, PREV.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 				PREV.rm_owner, PREV.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 				PREV.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		error = xfs_btree_delete(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		error = xfs_btree_decrement(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		NEW = LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		 * The left neighbor is contiguous, the right is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		trace_xfs_rmap_delete(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 				PREV.rm_startblock, PREV.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 				PREV.rm_owner, PREV.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 				PREV.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		error = xfs_btree_delete(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		error = xfs_btree_decrement(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		NEW = LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		NEW.rm_blockcount += PREV.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		 * The right neighbor is contiguous, the left is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		trace_xfs_rmap_delete(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 				RIGHT.rm_startblock, RIGHT.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 				RIGHT.rm_owner, RIGHT.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 				RIGHT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		error = xfs_btree_delete(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		error = xfs_btree_decrement(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		NEW.rm_blockcount = len + RIGHT.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		NEW.rm_flags = newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		 * Neither the left nor right neighbors are contiguous with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		 * the new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		NEW.rm_flags = newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		 * Setting the first part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		 * The left neighbor is contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		NEW.rm_offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		NEW.rm_startblock += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		error = xfs_btree_decrement(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		NEW = LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		NEW.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	case RMAP_LEFT_FILLING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		 * Setting the first part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		 * The left neighbor is not contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		NEW.rm_startblock += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		NEW.rm_offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		NEW.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		NEW.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		NEW.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		NEW.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		NEW.rm_flags = newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		cur->bc_rec.r = NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		trace_xfs_rmap_insert(mp, cur->bc_ag.agno, bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 				len, owner, offset, newext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		error = xfs_btree_insert(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		 * Setting the last part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		 * The right neighbor is contiguous with the new allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		error = xfs_btree_increment(cur, 0, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		NEW = RIGHT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		NEW.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		NEW.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		NEW.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	case RMAP_RIGHT_FILLING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		 * Setting the last part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		 * The right neighbor is not contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				oldext, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		if (XFS_IS_CORRUPT(mp, i != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		NEW.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		NEW.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		NEW.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		NEW.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		NEW.rm_flags = newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		cur->bc_rec.r = NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		trace_xfs_rmap_insert(mp, cur->bc_ag.agno, bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 				len, owner, offset, newext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		error = xfs_btree_insert(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		 * Setting the middle part of a previous oldext extent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		 * newext.  Contiguity is impossible here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		 * One extent becomes three extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		/* new right extent - oldext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		NEW.rm_startblock = bno + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		NEW.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		NEW.rm_offset = new_endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 				new_endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		NEW.rm_flags = PREV.rm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		/* new left extent - oldext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		NEW.rm_blockcount = offset - PREV.rm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		cur->bc_rec.r = NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		trace_xfs_rmap_insert(mp, cur->bc_ag.agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 				NEW.rm_startblock, NEW.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 				NEW.rm_owner, NEW.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 				NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		error = xfs_btree_insert(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		 * Reset the cursor to the position of the new extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		 * we are about to insert as we can't trust it after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		 * the previous insert.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 				oldext, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		if (XFS_IS_CORRUPT(mp, i != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		/* new middle extent - newext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		cur->bc_rec.r.rm_flags &= ~XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		cur->bc_rec.r.rm_flags |= newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		trace_xfs_rmap_insert(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 				owner, offset, newext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		error = xfs_btree_insert(cur, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	case RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	case RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		 * These cases are all impossible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	trace_xfs_rmap_convert_done(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		trace_xfs_rmap_convert_error(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 				cur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  * Convert an unwritten extent to a real extent or vice versa.  If there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)  * possibility of overlapping extents, delegate to the simpler convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)  * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) xfs_rmap_convert_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	bool				unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	struct xfs_mount		*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	struct xfs_rmap_irec		r[4];	/* neighbor extent entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 						/* left is 0, right is 1, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 						/* prev is 2, new is 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	uint64_t		owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	uint64_t		offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	uint64_t		new_endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	unsigned int		oldext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	unsigned int		newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	unsigned int		flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	int			i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	int			state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 			(flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	new_endoff = offset + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	trace_xfs_rmap_convert(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	 * For the initial lookup, look for and exact match or the left-adjacent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	 * record for our insertion point. This will also give us the record for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	 * start block contiguity tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, oldext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			&PREV, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	ASSERT(PREV.rm_offset <= offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	newext = ~oldext & XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	 * Set flags determining what part of the previous oldext allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	 * extent is being replaced by a newext allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	if (PREV.rm_offset == offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		state |= RMAP_LEFT_FILLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		state |= RMAP_RIGHT_FILLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	/* Is there a left record that abuts our range? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, newext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			&LEFT, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		state |= RMAP_LEFT_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				   LEFT.rm_startblock + LEFT.rm_blockcount >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 				   bno)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		if (xfs_rmap_is_mergeable(&LEFT, owner, newext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			state |= RMAP_LEFT_CONTIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	/* Is there a right record that abuts our range? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 			newext, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	if (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		state |= RMAP_RIGHT_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		error = xfs_rmap_get_rec(cur, &RIGHT, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		if (XFS_IS_CORRUPT(mp, bno + len > RIGHT.rm_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 				cur->bc_ag.agno, RIGHT.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 				RIGHT.rm_blockcount, RIGHT.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 				RIGHT.rm_offset, RIGHT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		if (xfs_rmap_is_mergeable(&RIGHT, owner, newext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			state |= RMAP_RIGHT_CONTIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	/* check that left + prev + right is not too long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			 RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	    (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	     RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	    (unsigned long)LEFT.rm_blockcount + len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	     RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		state &= ~RMAP_RIGHT_CONTIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	trace_xfs_rmap_convert_state(mp, cur->bc_ag.agno, state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 			_RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	 * Switch out based on the FILLING and CONTIG state bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 			 RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	     RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		 * The left and right neighbors are both contiguous with new.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		error = xfs_rmap_delete(cur, RIGHT.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 				RIGHT.rm_blockcount, RIGHT.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 				RIGHT.rm_offset, RIGHT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		error = xfs_rmap_delete(cur, PREV.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 				PREV.rm_blockcount, PREV.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 				PREV.rm_offset, PREV.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		NEW = LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		 * The left neighbor is contiguous, the right is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		error = xfs_rmap_delete(cur, PREV.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 				PREV.rm_blockcount, PREV.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 				PREV.rm_offset, PREV.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		NEW = LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		NEW.rm_blockcount += PREV.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		 * The right neighbor is contiguous, the left is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		error = xfs_rmap_delete(cur, RIGHT.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 				RIGHT.rm_blockcount, RIGHT.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 				RIGHT.rm_offset, RIGHT.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		NEW.rm_blockcount += RIGHT.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		NEW.rm_flags = RIGHT.rm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		 * Setting all of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		 * Neither the left nor right neighbors are contiguous with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		 * the new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		NEW.rm_flags = newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		 * Setting the first part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		 * The left neighbor is contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		error = xfs_rmap_delete(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 				NEW.rm_offset, NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		NEW.rm_offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		NEW.rm_startblock += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		error = xfs_rmap_insert(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 				NEW.rm_offset, NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		NEW = LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		NEW.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	case RMAP_LEFT_FILLING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		 * Setting the first part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		 * The left neighbor is not contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		error = xfs_rmap_delete(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 				NEW.rm_offset, NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		NEW.rm_offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		NEW.rm_startblock += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		error = xfs_rmap_insert(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 				NEW.rm_offset, NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		error = xfs_rmap_insert(cur, bno, len, owner, offset, newext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		 * Setting the last part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		 * The right neighbor is contiguous with the new allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		NEW.rm_blockcount = offset - NEW.rm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		NEW = RIGHT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		error = xfs_rmap_delete(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 				NEW.rm_offset, NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		NEW.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		NEW.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		NEW.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		error = xfs_rmap_insert(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 				NEW.rm_offset, NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	case RMAP_RIGHT_FILLING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		 * Setting the last part of a previous oldext extent to newext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		 * The right neighbor is not contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		NEW.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		error = xfs_rmap_insert(cur, bno, len, owner, offset, newext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		 * Setting the middle part of a previous oldext extent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		 * newext.  Contiguity is impossible here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		 * One extent becomes three extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		/* new right extent - oldext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		NEW.rm_startblock = bno + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		NEW.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		NEW.rm_offset = new_endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 				new_endoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		NEW.rm_flags = PREV.rm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		error = xfs_rmap_insert(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 				NEW.rm_blockcount, NEW.rm_owner, NEW.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 				NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		/* new left extent - oldext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		NEW = PREV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 				NEW.rm_blockcount, NEW.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 				NEW.rm_offset, NEW.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		NEW.rm_blockcount = offset - NEW.rm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		error = xfs_rmap_update(cur, &NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		/* new middle extent - newext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		NEW.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		NEW.rm_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		NEW.rm_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		NEW.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		NEW.rm_flags = newext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		error = xfs_rmap_insert(cur, NEW.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 				NEW.rm_blockcount, NEW.rm_owner, NEW.rm_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 				NEW.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	case RMAP_LEFT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	case RMAP_RIGHT_CONTIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		 * These cases are all impossible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	trace_xfs_rmap_convert_done(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		trace_xfs_rmap_convert_error(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 				cur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) #undef	NEW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) #undef	LEFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) #undef	RIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) #undef	PREV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)  * Find an extent in the rmap btree and unmap it.  For rmap extent types that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)  * can overlap (data fork rmaps on reflink filesystems) we must be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)  * that the prev/next records in the btree might belong to another owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)  * Therefore we must use delete+insert to alter any of the key fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)  * For every other situation there can only be one owner for a given extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)  * so we can call the regular _free function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) xfs_rmap_unmap_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	bool				unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	struct xfs_mount		*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	struct xfs_rmap_irec		ltrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	uint64_t			ltoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	int				i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	uint64_t			owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	uint64_t			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	unsigned int			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	if (unwritten)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 		flags |= XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	trace_xfs_rmap_unmap(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	 * We should always have a left record because there's a static record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	 * for the AG headers at rm_startblock == 0 created by mkfs/growfs that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	 * will not ever be removed from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 			&ltrec, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	ltoff = ltrec.rm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	/* Make sure the extent we found covers the entire freeing range. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 			   ltrec.rm_startblock > bno ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 			   ltrec.rm_startblock + ltrec.rm_blockcount <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			   bno + len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	/* Make sure the owner matches what we expect to find in the tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	if (XFS_IS_CORRUPT(mp, owner != ltrec.rm_owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	/* Make sure the unwritten flag matches. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	if (XFS_IS_CORRUPT(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 			   (flags & XFS_RMAP_UNWRITTEN) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 			   (ltrec.rm_flags & XFS_RMAP_UNWRITTEN))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	/* Check the offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	if (XFS_IS_CORRUPT(mp, ltrec.rm_offset > offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	if (XFS_IS_CORRUPT(mp, offset > ltoff + ltrec.rm_blockcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		/* Exact match, simply remove the record from rmap tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		error = xfs_rmap_delete(cur, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 				ltrec.rm_offset, ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	} else if (ltrec.rm_startblock == bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		 * Overlap left hand side of extent: move the start, trim the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		 * length and update the current record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		 *       ltbno                ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		 * Orig:    |oooooooooooooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		 * Freeing: |fffffffff|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		 * Result:            |rrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		 *         bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		/* Delete prev rmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		error = xfs_rmap_delete(cur, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 				ltrec.rm_offset, ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		/* Add an rmap at the new offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		ltrec.rm_startblock += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		ltrec.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		ltrec.rm_offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		error = xfs_rmap_insert(cur, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 				ltrec.rm_offset, ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	} else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		 * Overlap right hand side of extent: trim the length and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		 * update the current record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		 *       ltbno                ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		 * Orig:    |oooooooooooooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		 * Freeing:            |fffffffff|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		 * Result:  |rrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		 *                    bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 				ltrec.rm_offset, ltrec.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		ltrec.rm_blockcount -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		 * Overlap middle of extent: trim the length of the existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		 * record to the length of the new left-extent size, increment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		 * the insertion position so we can insert a new record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		 * containing the remaining right-extent space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		 *       ltbno                ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		 * Orig:    |oooooooooooooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		 * Freeing:       |fffffffff|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		 * Result:  |rrrrr|         |rrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		 *               bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 		xfs_extlen_t	orig_len = ltrec.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		/* Shrink the left side of the rmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 				ltrec.rm_offset, ltrec.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		ltrec.rm_blockcount = bno - ltrec.rm_startblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		/* Add an rmap at the new offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		error = xfs_rmap_insert(cur, bno + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 				orig_len - len - ltrec.rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 				ltrec.rm_owner, offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 				ltrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	trace_xfs_rmap_unmap_done(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		trace_xfs_rmap_unmap_error(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 				cur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)  * Find an extent in the rmap btree and map it.  For rmap extent types that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)  * can overlap (data fork rmaps on reflink filesystems) we must be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  * that the prev/next records in the btree might belong to another owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  * Therefore we must use delete+insert to alter any of the key fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)  * For every other situation there can only be one owner for a given extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)  * so we can call the regular _alloc function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) xfs_rmap_map_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	bool				unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	const struct xfs_owner_info	*oinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	struct xfs_mount		*mp = cur->bc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	struct xfs_rmap_irec		ltrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	struct xfs_rmap_irec		gtrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	int				have_gt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	int				have_lt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	int				i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	uint64_t			owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	uint64_t			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	unsigned int			flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	if (unwritten)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		flags |= XFS_RMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	trace_xfs_rmap_map(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	/* Is there a left record that abuts our range? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 			&ltrec, &have_lt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	if (have_lt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	    !xfs_rmap_is_mergeable(&ltrec, owner, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		have_lt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	/* Is there a right record that abuts our range? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 			flags, &have_gt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	if (have_gt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		if (XFS_IS_CORRUPT(mp, have_gt != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 			cur->bc_ag.agno, gtrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 			gtrec.rm_blockcount, gtrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 			gtrec.rm_offset, gtrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 			have_gt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	if (have_lt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	    ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	    ltrec.rm_offset + ltrec.rm_blockcount == offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		 * Left edge contiguous, merge into left record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 		 *       ltbno     ltlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		 * orig:   |ooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		 * adding:           |aaaaaaaaa|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		 * result: |rrrrrrrrrrrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		 *                  bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		ltrec.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		if (have_gt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		    bno + len == gtrec.rm_startblock &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		    offset + len == gtrec.rm_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 			 * Right edge also contiguous, delete right record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 			 * and merge into left record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 			 *       ltbno     ltlen    gtbno     gtlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 			 * orig:   |ooooooooo|         |ooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 			 * adding:           |aaaaaaaaa|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 			 * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 			ltrec.rm_blockcount += gtrec.rm_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 			error = xfs_rmap_delete(cur, gtrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 					gtrec.rm_blockcount, gtrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 					gtrec.rm_offset, gtrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 				goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 		/* Point the cursor back to the left record and update. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 				ltrec.rm_blockcount, ltrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 				ltrec.rm_offset, ltrec.rm_flags, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 		if (XFS_IS_CORRUPT(mp, i != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		error = xfs_rmap_update(cur, &ltrec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	} else if (have_gt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		   bno + len == gtrec.rm_startblock &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		   offset + len == gtrec.rm_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 		 * Right edge contiguous, merge into right record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		 *                 gtbno     gtlen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		 * Orig:             |ooooooooo|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		 * adding: |aaaaaaaaa|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 		 * Result: |rrrrrrrrrrrrrrrrrrr|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		 *        bno       len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 		/* Delete the old record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		error = xfs_rmap_delete(cur, gtrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 				gtrec.rm_blockcount, gtrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 				gtrec.rm_offset, gtrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		/* Move the start and re-add it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		gtrec.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		gtrec.rm_blockcount += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		gtrec.rm_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		error = xfs_rmap_insert(cur, gtrec.rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 				gtrec.rm_blockcount, gtrec.rm_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 				gtrec.rm_offset, gtrec.rm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 		 * No contiguous edge with identical owner, insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 		 * new record at current cursor position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 		error = xfs_rmap_insert(cur, bno, len, owner, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	trace_xfs_rmap_map_done(mp, cur->bc_ag.agno, bno, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 			unwritten, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 		trace_xfs_rmap_map_error(cur->bc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 				cur->bc_ag.agno, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) /* Insert a raw rmap into the rmapbt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) xfs_rmap_map_raw(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	struct xfs_rmap_irec	*rmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	struct xfs_owner_info	oinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	oinfo.oi_owner = rmap->rm_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	oinfo.oi_offset = rmap->rm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	oinfo.oi_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	if (rmap->rm_flags & XFS_RMAP_ATTR_FORK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	if (rmap->rm_flags & XFS_RMAP_BMBT_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	if (rmap->rm_flags || XFS_RMAP_NON_INODE_OWNER(rmap->rm_owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		return xfs_rmap_map(cur, rmap->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 				rmap->rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 				rmap->rm_flags & XFS_RMAP_UNWRITTEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 				&oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	return xfs_rmap_map_shared(cur, rmap->rm_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 			rmap->rm_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 			rmap->rm_flags & XFS_RMAP_UNWRITTEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 			&oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) struct xfs_rmap_query_range_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	xfs_rmap_query_range_fn	fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	void				*priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) /* Format btree record and pass to our callback. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) xfs_rmap_query_range_helper(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	union xfs_btree_rec	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	void			*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	struct xfs_rmap_query_range_info	*query = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	struct xfs_rmap_irec			irec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	int					error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	error = xfs_rmap_btrec_to_irec(rec, &irec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	return query->fn(cur, &irec, query->priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) /* Find all rmaps between two keys. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) xfs_rmap_query_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	struct xfs_btree_cur			*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	struct xfs_rmap_irec			*low_rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	struct xfs_rmap_irec			*high_rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	xfs_rmap_query_range_fn			fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	void					*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	union xfs_btree_irec			low_brec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	union xfs_btree_irec			high_brec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	struct xfs_rmap_query_range_info	query;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	low_brec.r = *low_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	high_brec.r = *high_rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	query.priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	query.fn = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	return xfs_btree_query_range(cur, &low_brec, &high_brec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 			xfs_rmap_query_range_helper, &query);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) /* Find all rmaps. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) xfs_rmap_query_all(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	struct xfs_btree_cur			*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	xfs_rmap_query_range_fn			fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	void					*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	struct xfs_rmap_query_range_info	query;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	query.priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	query.fn = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	return xfs_btree_query_all(cur, xfs_rmap_query_range_helper, &query);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) /* Clean up after calling xfs_rmap_finish_one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) xfs_rmap_finish_one_cleanup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	struct xfs_btree_cur	*rcur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	int			error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	struct xfs_buf		*agbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	if (rcur == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	agbp = rcur->bc_ag.agbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	xfs_btree_del_cursor(rcur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 		xfs_trans_brelse(tp, agbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346)  * Process one of the deferred rmap operations.  We pass back the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347)  * btree cursor to maintain our lock on the rmapbt between calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348)  * This saves time and eliminates a buffer deadlock between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349)  * superblock and the AGF because we'll always grab them in the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350)  * order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) xfs_rmap_finish_one(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	struct xfs_trans		*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	enum xfs_rmap_intent_type	type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	uint64_t			owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	int				whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	xfs_fileoff_t			startoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	xfs_fsblock_t			startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	xfs_filblks_t			blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	xfs_exntst_t			state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	struct xfs_btree_cur		**pcur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	struct xfs_mount		*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	struct xfs_btree_cur		*rcur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	struct xfs_buf			*agbp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	xfs_agnumber_t			agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	struct xfs_owner_info		oinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	xfs_agblock_t			bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	bool				unwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	agno = XFS_FSB_TO_AGNO(mp, startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	ASSERT(agno != NULLAGNUMBER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	bno = XFS_FSB_TO_AGBNO(mp, startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	trace_xfs_rmap_deferred(mp, agno, type, bno, owner, whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 			startoff, blockcount, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	if (XFS_TEST_ERROR(false, mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 			XFS_ERRTAG_RMAP_FINISH_ONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	 * If we haven't gotten a cursor or the cursor AG doesn't match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	 * the startblock, get one now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	rcur = *pcur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	if (rcur != NULL && rcur->bc_ag.agno != agno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		xfs_rmap_finish_one_cleanup(tp, rcur, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		rcur = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		*pcur = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	if (rcur == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		 * Refresh the freelist before we start changing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		 * rmapbt, because a shape change could cause us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 		 * allocate blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 		error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		if (XFS_IS_CORRUPT(tp->t_mountp, !agbp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 		rcur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 		if (!rcur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 			goto out_cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	*pcur = rcur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	xfs_rmap_ino_owner(&oinfo, owner, whichfork, startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	unwritten = state == XFS_EXT_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	bno = XFS_FSB_TO_AGBNO(rcur->bc_mp, startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	case XFS_RMAP_ALLOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	case XFS_RMAP_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		error = xfs_rmap_map(rcur, bno, blockcount, unwritten, &oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	case XFS_RMAP_MAP_SHARED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 		error = xfs_rmap_map_shared(rcur, bno, blockcount, unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 				&oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	case XFS_RMAP_FREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	case XFS_RMAP_UNMAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		error = xfs_rmap_unmap(rcur, bno, blockcount, unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 				&oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	case XFS_RMAP_UNMAP_SHARED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		error = xfs_rmap_unmap_shared(rcur, bno, blockcount, unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 				&oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	case XFS_RMAP_CONVERT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		error = xfs_rmap_convert(rcur, bno, blockcount, !unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 				&oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	case XFS_RMAP_CONVERT_SHARED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		error = xfs_rmap_convert_shared(rcur, bno, blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 				!unwritten, &oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) out_cur:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	xfs_trans_brelse(tp, agbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)  * Don't defer an rmap if we aren't an rmap filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) xfs_rmap_update_is_needed(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	int			whichfork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	return xfs_sb_version_hasrmapbt(&mp->m_sb) && whichfork != XFS_COW_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468)  * Record a rmap intent; the list is kept sorted first by AG and then by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469)  * increasing age.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) __xfs_rmap_add(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	struct xfs_trans		*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	enum xfs_rmap_intent_type	type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	uint64_t			owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	int				whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	struct xfs_bmbt_irec		*bmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	struct xfs_rmap_intent		*ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	trace_xfs_rmap_defer(tp->t_mountp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 			XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 			type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 			XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 			owner, whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 			bmap->br_startoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 			bmap->br_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 			bmap->br_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	INIT_LIST_HEAD(&ri->ri_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	ri->ri_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	ri->ri_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	ri->ri_whichfork = whichfork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	ri->ri_bmap = *bmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_RMAP, &ri->ri_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) /* Map an extent into a file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) xfs_rmap_map_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	int			whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	struct xfs_bmbt_irec	*PREV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	enum xfs_rmap_intent_type type = XFS_RMAP_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	if (!xfs_rmap_update_is_needed(tp->t_mountp, whichfork))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	if (whichfork != XFS_ATTR_FORK && xfs_is_reflink_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		type = XFS_RMAP_MAP_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	__xfs_rmap_add(tp, type, ip->i_ino, whichfork, PREV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) /* Unmap an extent out of a file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) xfs_rmap_unmap_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	int			whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	struct xfs_bmbt_irec	*PREV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	enum xfs_rmap_intent_type type = XFS_RMAP_UNMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	if (!xfs_rmap_update_is_needed(tp->t_mountp, whichfork))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	if (whichfork != XFS_ATTR_FORK && xfs_is_reflink_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 		type = XFS_RMAP_UNMAP_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	__xfs_rmap_add(tp, type, ip->i_ino, whichfork, PREV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539)  * Convert a data fork extent from unwritten to real or vice versa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541)  * Note that tp can be NULL here as no transaction is used for COW fork
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542)  * unwritten conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) xfs_rmap_convert_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	int			whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	struct xfs_bmbt_irec	*PREV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	enum xfs_rmap_intent_type type = XFS_RMAP_CONVERT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	if (!xfs_rmap_update_is_needed(mp, whichfork))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	if (whichfork != XFS_ATTR_FORK && xfs_is_reflink_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 		type = XFS_RMAP_CONVERT_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	__xfs_rmap_add(tp, type, ip->i_ino, whichfork, PREV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) /* Schedule the creation of an rmap for non-file data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) xfs_rmap_alloc_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	uint64_t		owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	struct xfs_bmbt_irec	bmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	if (!xfs_rmap_update_is_needed(tp->t_mountp, XFS_DATA_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	bmap.br_startblock = XFS_AGB_TO_FSB(tp->t_mountp, agno, bno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	bmap.br_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	bmap.br_startoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	bmap.br_state = XFS_EXT_NORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 	__xfs_rmap_add(tp, XFS_RMAP_ALLOC, owner, XFS_DATA_FORK, &bmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) /* Schedule the deletion of an rmap for non-file data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) xfs_rmap_free_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	uint64_t		owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	struct xfs_bmbt_irec	bmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	if (!xfs_rmap_update_is_needed(tp->t_mountp, XFS_DATA_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	bmap.br_startblock = XFS_AGB_TO_FSB(tp->t_mountp, agno, bno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	bmap.br_blockcount = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	bmap.br_startoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	bmap.br_state = XFS_EXT_NORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	__xfs_rmap_add(tp, XFS_RMAP_FREE, owner, XFS_DATA_FORK, &bmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) /* Compare rmap records.  Returns -1 if a < b, 1 if a > b, and 0 if equal. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) xfs_rmap_compare(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	const struct xfs_rmap_irec	*a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	const struct xfs_rmap_irec	*b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	__u64				oa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	__u64				ob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	oa = xfs_rmap_irec_offset_pack(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	ob = xfs_rmap_irec_offset_pack(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	if (a->rm_startblock < b->rm_startblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	else if (a->rm_startblock > b->rm_startblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	else if (a->rm_owner < b->rm_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	else if (a->rm_owner > b->rm_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	else if (oa < ob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	else if (oa > ob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) /* Is there a record covering a given extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) xfs_rmap_has_record(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	struct xfs_btree_cur	*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	xfs_extlen_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	bool			*exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	union xfs_btree_irec	low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	union xfs_btree_irec	high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	memset(&low, 0, sizeof(low));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	low.r.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	memset(&high, 0xFF, sizeof(high));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	high.r.rm_startblock = bno + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	return xfs_btree_has_record(cur, &low, &high, exists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655)  * Is there a record for this owner completely covering a given physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656)  * extent?  If so, *has_rmap will be set to true.  If there is no record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657)  * or the record only covers part of the range, we set *has_rmap to false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658)  * This function doesn't perform range lookups or offset checks, so it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659)  * not suitable for checking data fork blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) xfs_rmap_record_exists(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	const struct xfs_owner_info	*oinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	bool				*has_rmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	uint64_t			owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 	uint64_t			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	unsigned int			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	int				has_record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	struct xfs_rmap_irec		irec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	ASSERT(XFS_RMAP_NON_INODE_OWNER(owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	       (flags & XFS_RMAP_BMBT_BLOCK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			&has_record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	if (!has_record) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 		*has_rmap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	error = xfs_rmap_get_rec(cur, &irec, &has_record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	if (!has_record) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 		*has_rmap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	*has_rmap = (irec.rm_owner == owner && irec.rm_startblock <= bno &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 		     irec.rm_startblock + irec.rm_blockcount >= bno + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) struct xfs_rmap_key_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	uint64_t			owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	uint64_t			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	unsigned int			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) /* For each rmap given, figure out if it doesn't match the key we want. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) xfs_rmap_has_other_keys_helper(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	struct xfs_rmap_irec		*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	void				*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	struct xfs_rmap_key_state	*rks = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	if (rks->owner == rec->rm_owner && rks->offset == rec->rm_offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	    ((rks->flags & rec->rm_flags) & XFS_RMAP_KEY_FLAGS) == rks->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724)  * Given an extent and some owner info, can we find records overlapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725)  * the extent whose owner info does not match the given owner?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) xfs_rmap_has_other_keys(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	xfs_agblock_t			bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	xfs_extlen_t			len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	const struct xfs_owner_info	*oinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	bool				*has_rmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	struct xfs_rmap_irec		low = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	struct xfs_rmap_irec		high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	struct xfs_rmap_key_state	rks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	xfs_owner_info_unpack(oinfo, &rks.owner, &rks.offset, &rks.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	*has_rmap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	low.rm_startblock = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	memset(&high, 0xFF, sizeof(high));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	high.rm_startblock = bno + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	error = xfs_rmap_query_range(cur, &low, &high,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 			xfs_rmap_has_other_keys_helper, &rks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	if (error == -ECANCELED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 		*has_rmap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	.oi_owner = XFS_RMAP_OWN_NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	.oi_owner = XFS_RMAP_OWN_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) const struct xfs_owner_info XFS_RMAP_OINFO_FS = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	.oi_owner = XFS_RMAP_OWN_FS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) const struct xfs_owner_info XFS_RMAP_OINFO_LOG = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	.oi_owner = XFS_RMAP_OWN_LOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) const struct xfs_owner_info XFS_RMAP_OINFO_AG = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	.oi_owner = XFS_RMAP_OWN_AG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) const struct xfs_owner_info XFS_RMAP_OINFO_INOBT = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	.oi_owner = XFS_RMAP_OWN_INOBT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) const struct xfs_owner_info XFS_RMAP_OINFO_INODES = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	.oi_owner = XFS_RMAP_OWN_INODES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) const struct xfs_owner_info XFS_RMAP_OINFO_REFC = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	.oi_owner = XFS_RMAP_OWN_REFC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) const struct xfs_owner_info XFS_RMAP_OINFO_COW = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	.oi_owner = XFS_RMAP_OWN_COW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) };