162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2000-2006 Silicon Graphics, Inc.
462306a36Sopenharmony_ci * All Rights Reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#ifndef __XFS_BMAP_H__
762306a36Sopenharmony_ci#define	__XFS_BMAP_H__
862306a36Sopenharmony_ci
962306a36Sopenharmony_cistruct getbmap;
1062306a36Sopenharmony_cistruct xfs_bmbt_irec;
1162306a36Sopenharmony_cistruct xfs_ifork;
1262306a36Sopenharmony_cistruct xfs_inode;
1362306a36Sopenharmony_cistruct xfs_mount;
1462306a36Sopenharmony_cistruct xfs_trans;
1562306a36Sopenharmony_cistruct xfs_alloc_arg;
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*
1862306a36Sopenharmony_ci * Argument structure for xfs_bmap_alloc.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_cistruct xfs_bmalloca {
2162306a36Sopenharmony_ci	struct xfs_trans	*tp;	/* transaction pointer */
2262306a36Sopenharmony_ci	struct xfs_inode	*ip;	/* incore inode pointer */
2362306a36Sopenharmony_ci	struct xfs_bmbt_irec	prev;	/* extent before the new one */
2462306a36Sopenharmony_ci	struct xfs_bmbt_irec	got;	/* extent after, or delayed */
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	xfs_fileoff_t		offset;	/* offset in file filling in */
2762306a36Sopenharmony_ci	xfs_extlen_t		length;	/* i/o length asked/allocated */
2862306a36Sopenharmony_ci	xfs_fsblock_t		blkno;	/* starting block of new extent */
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	struct xfs_btree_cur	*cur;	/* btree cursor */
3162306a36Sopenharmony_ci	struct xfs_iext_cursor	icur;	/* incore extent cursor */
3262306a36Sopenharmony_ci	int			nallocs;/* number of extents alloc'd */
3362306a36Sopenharmony_ci	int			logflags;/* flags for transaction logging */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	xfs_extlen_t		total;	/* total blocks needed for xaction */
3662306a36Sopenharmony_ci	xfs_extlen_t		minlen;	/* minimum allocation size (blocks) */
3762306a36Sopenharmony_ci	xfs_extlen_t		minleft; /* amount must be left after alloc */
3862306a36Sopenharmony_ci	bool			eof;	/* set if allocating past last extent */
3962306a36Sopenharmony_ci	bool			wasdel;	/* replacing a delayed allocation */
4062306a36Sopenharmony_ci	bool			aeof;	/* allocated space at eof */
4162306a36Sopenharmony_ci	bool			conv;	/* overwriting unwritten extents */
4262306a36Sopenharmony_ci	int			datatype;/* data type being allocated */
4362306a36Sopenharmony_ci	uint32_t		flags;
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define	XFS_BMAP_MAX_NMAP	4
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/*
4962306a36Sopenharmony_ci * Flags for xfs_bmapi_*
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_ci#define XFS_BMAPI_ENTIRE	(1u << 0) /* return entire extent untrimmed */
5262306a36Sopenharmony_ci#define XFS_BMAPI_METADATA	(1u << 1) /* mapping metadata not user data */
5362306a36Sopenharmony_ci#define XFS_BMAPI_ATTRFORK	(1u << 2) /* use attribute fork not data */
5462306a36Sopenharmony_ci#define XFS_BMAPI_PREALLOC	(1u << 3) /* preallocating unwritten space */
5562306a36Sopenharmony_ci#define XFS_BMAPI_CONTIG	(1u << 4) /* must allocate only one extent */
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * unwritten extent conversion - this needs write cache flushing and no additional
5862306a36Sopenharmony_ci * allocation alignments. When specified with XFS_BMAPI_PREALLOC it converts
5962306a36Sopenharmony_ci * from written to unwritten, otherwise convert from unwritten to written.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci#define XFS_BMAPI_CONVERT	(1u << 5)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/*
6462306a36Sopenharmony_ci * allocate zeroed extents - this requires all newly allocated user data extents
6562306a36Sopenharmony_ci * to be initialised to zero. It will be ignored if XFS_BMAPI_METADATA is set.
6662306a36Sopenharmony_ci * Use in conjunction with XFS_BMAPI_CONVERT to convert unwritten extents found
6762306a36Sopenharmony_ci * during the allocation range to zeroed written extents.
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_ci#define XFS_BMAPI_ZERO		(1u << 6)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/*
7262306a36Sopenharmony_ci * Map the inode offset to the block given in ap->firstblock.  Primarily
7362306a36Sopenharmony_ci * used for reflink.  The range must be in a hole, and this flag cannot be
7462306a36Sopenharmony_ci * turned on with PREALLOC or CONVERT, and cannot be used on the attr fork.
7562306a36Sopenharmony_ci *
7662306a36Sopenharmony_ci * For bunmapi, this flag unmaps the range without adjusting quota, reducing
7762306a36Sopenharmony_ci * refcount, or freeing the blocks.
7862306a36Sopenharmony_ci */
7962306a36Sopenharmony_ci#define XFS_BMAPI_REMAP		(1u << 7)
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/* Map something in the CoW fork. */
8262306a36Sopenharmony_ci#define XFS_BMAPI_COWFORK	(1u << 8)
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/* Skip online discard of freed extents */
8562306a36Sopenharmony_ci#define XFS_BMAPI_NODISCARD	(1u << 9)
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/* Do not update the rmap btree.  Used for reconstructing bmbt from rmapbt. */
8862306a36Sopenharmony_ci#define XFS_BMAPI_NORMAP	(1u << 10)
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci#define XFS_BMAPI_FLAGS \
9162306a36Sopenharmony_ci	{ XFS_BMAPI_ENTIRE,	"ENTIRE" }, \
9262306a36Sopenharmony_ci	{ XFS_BMAPI_METADATA,	"METADATA" }, \
9362306a36Sopenharmony_ci	{ XFS_BMAPI_ATTRFORK,	"ATTRFORK" }, \
9462306a36Sopenharmony_ci	{ XFS_BMAPI_PREALLOC,	"PREALLOC" }, \
9562306a36Sopenharmony_ci	{ XFS_BMAPI_CONTIG,	"CONTIG" }, \
9662306a36Sopenharmony_ci	{ XFS_BMAPI_CONVERT,	"CONVERT" }, \
9762306a36Sopenharmony_ci	{ XFS_BMAPI_ZERO,	"ZERO" }, \
9862306a36Sopenharmony_ci	{ XFS_BMAPI_REMAP,	"REMAP" }, \
9962306a36Sopenharmony_ci	{ XFS_BMAPI_COWFORK,	"COWFORK" }, \
10062306a36Sopenharmony_ci	{ XFS_BMAPI_NODISCARD,	"NODISCARD" }, \
10162306a36Sopenharmony_ci	{ XFS_BMAPI_NORMAP,	"NORMAP" }
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistatic inline int xfs_bmapi_aflag(int w)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK :
10762306a36Sopenharmony_ci	       (w == XFS_COW_FORK ? XFS_BMAPI_COWFORK : 0));
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic inline int xfs_bmapi_whichfork(uint32_t bmapi_flags)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	if (bmapi_flags & XFS_BMAPI_COWFORK)
11362306a36Sopenharmony_ci		return XFS_COW_FORK;
11462306a36Sopenharmony_ci	else if (bmapi_flags & XFS_BMAPI_ATTRFORK)
11562306a36Sopenharmony_ci		return XFS_ATTR_FORK;
11662306a36Sopenharmony_ci	return XFS_DATA_FORK;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci/*
12062306a36Sopenharmony_ci * Special values for xfs_bmbt_irec_t br_startblock field.
12162306a36Sopenharmony_ci */
12262306a36Sopenharmony_ci#define	DELAYSTARTBLOCK		((xfs_fsblock_t)-1LL)
12362306a36Sopenharmony_ci#define	HOLESTARTBLOCK		((xfs_fsblock_t)-2LL)
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * Flags for xfs_bmap_add_extent*.
12762306a36Sopenharmony_ci */
12862306a36Sopenharmony_ci#define BMAP_LEFT_CONTIG	(1u << 0)
12962306a36Sopenharmony_ci#define BMAP_RIGHT_CONTIG	(1u << 1)
13062306a36Sopenharmony_ci#define BMAP_LEFT_FILLING	(1u << 2)
13162306a36Sopenharmony_ci#define BMAP_RIGHT_FILLING	(1u << 3)
13262306a36Sopenharmony_ci#define BMAP_LEFT_DELAY		(1u << 4)
13362306a36Sopenharmony_ci#define BMAP_RIGHT_DELAY	(1u << 5)
13462306a36Sopenharmony_ci#define BMAP_LEFT_VALID		(1u << 6)
13562306a36Sopenharmony_ci#define BMAP_RIGHT_VALID	(1u << 7)
13662306a36Sopenharmony_ci#define BMAP_ATTRFORK		(1u << 8)
13762306a36Sopenharmony_ci#define BMAP_COWFORK		(1u << 9)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define XFS_BMAP_EXT_FLAGS \
14062306a36Sopenharmony_ci	{ BMAP_LEFT_CONTIG,	"LC" }, \
14162306a36Sopenharmony_ci	{ BMAP_RIGHT_CONTIG,	"RC" }, \
14262306a36Sopenharmony_ci	{ BMAP_LEFT_FILLING,	"LF" }, \
14362306a36Sopenharmony_ci	{ BMAP_RIGHT_FILLING,	"RF" }, \
14462306a36Sopenharmony_ci	{ BMAP_ATTRFORK,	"ATTR" }, \
14562306a36Sopenharmony_ci	{ BMAP_COWFORK,		"COW" }
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci/* Return true if the extent is an allocated extent, written or not. */
14862306a36Sopenharmony_cistatic inline bool xfs_bmap_is_real_extent(const struct xfs_bmbt_irec *irec)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	return irec->br_startblock != HOLESTARTBLOCK &&
15162306a36Sopenharmony_ci		irec->br_startblock != DELAYSTARTBLOCK &&
15262306a36Sopenharmony_ci		!isnullstartblock(irec->br_startblock);
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/*
15662306a36Sopenharmony_ci * Return true if the extent is a real, allocated extent, or false if it is  a
15762306a36Sopenharmony_ci * delayed allocation, and unwritten extent or a hole.
15862306a36Sopenharmony_ci */
15962306a36Sopenharmony_cistatic inline bool xfs_bmap_is_written_extent(struct xfs_bmbt_irec *irec)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	return xfs_bmap_is_real_extent(irec) &&
16262306a36Sopenharmony_ci	       irec->br_state != XFS_EXT_UNWRITTEN;
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/*
16662306a36Sopenharmony_ci * Check the mapping for obviously garbage allocations that could trash the
16762306a36Sopenharmony_ci * filesystem immediately.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_ci#define xfs_valid_startblock(ip, startblock) \
17062306a36Sopenharmony_ci	((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciint	xfs_bmap_longest_free_extent(struct xfs_perag *pag,
17362306a36Sopenharmony_ci		struct xfs_trans *tp, xfs_extlen_t *blen);
17462306a36Sopenharmony_civoid	xfs_trim_extent(struct xfs_bmbt_irec *irec, xfs_fileoff_t bno,
17562306a36Sopenharmony_ci		xfs_filblks_t len);
17662306a36Sopenharmony_ciunsigned int xfs_bmap_compute_attr_offset(struct xfs_mount *mp);
17762306a36Sopenharmony_ciint	xfs_bmap_add_attrfork(struct xfs_inode *ip, int size, int rsvd);
17862306a36Sopenharmony_civoid	xfs_bmap_local_to_extents_empty(struct xfs_trans *tp,
17962306a36Sopenharmony_ci		struct xfs_inode *ip, int whichfork);
18062306a36Sopenharmony_civoid	xfs_bmap_compute_maxlevels(struct xfs_mount *mp, int whichfork);
18162306a36Sopenharmony_ciint	xfs_bmap_first_unused(struct xfs_trans *tp, struct xfs_inode *ip,
18262306a36Sopenharmony_ci		xfs_extlen_t len, xfs_fileoff_t *unused, int whichfork);
18362306a36Sopenharmony_ciint	xfs_bmap_last_before(struct xfs_trans *tp, struct xfs_inode *ip,
18462306a36Sopenharmony_ci		xfs_fileoff_t *last_block, int whichfork);
18562306a36Sopenharmony_ciint	xfs_bmap_last_offset(struct xfs_inode *ip, xfs_fileoff_t *unused,
18662306a36Sopenharmony_ci		int whichfork);
18762306a36Sopenharmony_ciint	xfs_bmapi_read(struct xfs_inode *ip, xfs_fileoff_t bno,
18862306a36Sopenharmony_ci		xfs_filblks_t len, struct xfs_bmbt_irec *mval,
18962306a36Sopenharmony_ci		int *nmap, uint32_t flags);
19062306a36Sopenharmony_ciint	xfs_bmapi_write(struct xfs_trans *tp, struct xfs_inode *ip,
19162306a36Sopenharmony_ci		xfs_fileoff_t bno, xfs_filblks_t len, uint32_t flags,
19262306a36Sopenharmony_ci		xfs_extlen_t total, struct xfs_bmbt_irec *mval, int *nmap);
19362306a36Sopenharmony_ciint	__xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
19462306a36Sopenharmony_ci		xfs_fileoff_t bno, xfs_filblks_t *rlen, uint32_t flags,
19562306a36Sopenharmony_ci		xfs_extnum_t nexts);
19662306a36Sopenharmony_ciint	xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
19762306a36Sopenharmony_ci		xfs_fileoff_t bno, xfs_filblks_t len, uint32_t flags,
19862306a36Sopenharmony_ci		xfs_extnum_t nexts, int *done);
19962306a36Sopenharmony_ciint	xfs_bmap_del_extent_delay(struct xfs_inode *ip, int whichfork,
20062306a36Sopenharmony_ci		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
20162306a36Sopenharmony_ci		struct xfs_bmbt_irec *del);
20262306a36Sopenharmony_civoid	xfs_bmap_del_extent_cow(struct xfs_inode *ip,
20362306a36Sopenharmony_ci		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
20462306a36Sopenharmony_ci		struct xfs_bmbt_irec *del);
20562306a36Sopenharmony_ciuint	xfs_default_attroffset(struct xfs_inode *ip);
20662306a36Sopenharmony_ciint	xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
20762306a36Sopenharmony_ci		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
20862306a36Sopenharmony_ci		bool *done);
20962306a36Sopenharmony_ciint	xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
21062306a36Sopenharmony_ci		xfs_fileoff_t shift);
21162306a36Sopenharmony_ciint	xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
21262306a36Sopenharmony_ci		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
21362306a36Sopenharmony_ci		bool *done, xfs_fileoff_t stop_fsb);
21462306a36Sopenharmony_ciint	xfs_bmap_split_extent(struct xfs_trans *tp, struct xfs_inode *ip,
21562306a36Sopenharmony_ci		xfs_fileoff_t split_offset);
21662306a36Sopenharmony_ciint	xfs_bmapi_reserve_delalloc(struct xfs_inode *ip, int whichfork,
21762306a36Sopenharmony_ci		xfs_fileoff_t off, xfs_filblks_t len, xfs_filblks_t prealloc,
21862306a36Sopenharmony_ci		struct xfs_bmbt_irec *got, struct xfs_iext_cursor *cur,
21962306a36Sopenharmony_ci		int eof);
22062306a36Sopenharmony_ciint	xfs_bmapi_convert_delalloc(struct xfs_inode *ip, int whichfork,
22162306a36Sopenharmony_ci		xfs_off_t offset, struct iomap *iomap, unsigned int *seq);
22262306a36Sopenharmony_ciint	xfs_bmap_add_extent_unwritten_real(struct xfs_trans *tp,
22362306a36Sopenharmony_ci		struct xfs_inode *ip, int whichfork,
22462306a36Sopenharmony_ci		struct xfs_iext_cursor *icur, struct xfs_btree_cur **curp,
22562306a36Sopenharmony_ci		struct xfs_bmbt_irec *new, int *logflagsp);
22662306a36Sopenharmony_cixfs_extlen_t xfs_bmapi_minleft(struct xfs_trans *tp, struct xfs_inode *ip,
22762306a36Sopenharmony_ci		int fork);
22862306a36Sopenharmony_ciint	xfs_bmap_btalloc_low_space(struct xfs_bmalloca *ap,
22962306a36Sopenharmony_ci		struct xfs_alloc_arg *args);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_cienum xfs_bmap_intent_type {
23262306a36Sopenharmony_ci	XFS_BMAP_MAP = 1,
23362306a36Sopenharmony_ci	XFS_BMAP_UNMAP,
23462306a36Sopenharmony_ci};
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistruct xfs_bmap_intent {
23762306a36Sopenharmony_ci	struct list_head			bi_list;
23862306a36Sopenharmony_ci	enum xfs_bmap_intent_type		bi_type;
23962306a36Sopenharmony_ci	int					bi_whichfork;
24062306a36Sopenharmony_ci	struct xfs_inode			*bi_owner;
24162306a36Sopenharmony_ci	struct xfs_perag			*bi_pag;
24262306a36Sopenharmony_ci	struct xfs_bmbt_irec			bi_bmap;
24362306a36Sopenharmony_ci};
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_civoid xfs_bmap_update_get_group(struct xfs_mount *mp,
24662306a36Sopenharmony_ci		struct xfs_bmap_intent *bi);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ciint	xfs_bmap_finish_one(struct xfs_trans *tp, struct xfs_bmap_intent *bi);
24962306a36Sopenharmony_civoid	xfs_bmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
25062306a36Sopenharmony_ci		struct xfs_bmbt_irec *imap);
25162306a36Sopenharmony_civoid	xfs_bmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
25262306a36Sopenharmony_ci		struct xfs_bmbt_irec *imap);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_cistatic inline uint32_t xfs_bmap_fork_to_state(int whichfork)
25562306a36Sopenharmony_ci{
25662306a36Sopenharmony_ci	switch (whichfork) {
25762306a36Sopenharmony_ci	case XFS_ATTR_FORK:
25862306a36Sopenharmony_ci		return BMAP_ATTRFORK;
25962306a36Sopenharmony_ci	case XFS_COW_FORK:
26062306a36Sopenharmony_ci		return BMAP_COWFORK;
26162306a36Sopenharmony_ci	default:
26262306a36Sopenharmony_ci		return 0;
26362306a36Sopenharmony_ci	}
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cixfs_failaddr_t xfs_bmap_validate_extent(struct xfs_inode *ip, int whichfork,
26762306a36Sopenharmony_ci		struct xfs_bmbt_irec *irec);
26862306a36Sopenharmony_ciint xfs_bmap_complain_bad_rec(struct xfs_inode *ip, int whichfork,
26962306a36Sopenharmony_ci		xfs_failaddr_t fa, const struct xfs_bmbt_irec *irec);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ciint	xfs_bmapi_remap(struct xfs_trans *tp, struct xfs_inode *ip,
27262306a36Sopenharmony_ci		xfs_fileoff_t bno, xfs_filblks_t len, xfs_fsblock_t startblock,
27362306a36Sopenharmony_ci		uint32_t flags);
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ciextern struct kmem_cache	*xfs_bmap_intent_cache;
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ciint __init xfs_bmap_intent_init_cache(void);
27862306a36Sopenharmony_civoid xfs_bmap_intent_destroy_cache(void);
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci#endif	/* __XFS_BMAP_H__ */
281