xref: /openbmc/linux/fs/xfs/libxfs/xfs_bmap.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1508578f2SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
284be0ffcSDave Chinner /*
384be0ffcSDave Chinner  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
484be0ffcSDave Chinner  * All Rights Reserved.
584be0ffcSDave Chinner  */
684be0ffcSDave Chinner #ifndef __XFS_BMAP_H__
784be0ffcSDave Chinner #define	__XFS_BMAP_H__
884be0ffcSDave Chinner 
984be0ffcSDave Chinner struct getbmap;
1084be0ffcSDave Chinner struct xfs_bmbt_irec;
1184be0ffcSDave Chinner struct xfs_ifork;
1284be0ffcSDave Chinner struct xfs_inode;
1384be0ffcSDave Chinner struct xfs_mount;
1484be0ffcSDave Chinner struct xfs_trans;
158f7747adSDave Chinner struct xfs_alloc_arg;
1684be0ffcSDave Chinner 
1784be0ffcSDave Chinner /*
189799b438SDave Chinner  * Argument structure for xfs_bmap_alloc.
199799b438SDave Chinner  */
209799b438SDave Chinner struct xfs_bmalloca {
219799b438SDave Chinner 	struct xfs_trans	*tp;	/* transaction pointer */
229799b438SDave Chinner 	struct xfs_inode	*ip;	/* incore inode pointer */
239799b438SDave Chinner 	struct xfs_bmbt_irec	prev;	/* extent before the new one */
249799b438SDave Chinner 	struct xfs_bmbt_irec	got;	/* extent after, or delayed */
259799b438SDave Chinner 
269799b438SDave Chinner 	xfs_fileoff_t		offset;	/* offset in file filling in */
279799b438SDave Chinner 	xfs_extlen_t		length;	/* i/o length asked/allocated */
289799b438SDave Chinner 	xfs_fsblock_t		blkno;	/* starting block of new extent */
299799b438SDave Chinner 
309799b438SDave Chinner 	struct xfs_btree_cur	*cur;	/* btree cursor */
31b2b1712aSChristoph Hellwig 	struct xfs_iext_cursor	icur;	/* incore extent cursor */
329799b438SDave Chinner 	int			nallocs;/* number of extents alloc'd */
339799b438SDave Chinner 	int			logflags;/* flags for transaction logging */
349799b438SDave Chinner 
359799b438SDave Chinner 	xfs_extlen_t		total;	/* total blocks needed for xaction */
369799b438SDave Chinner 	xfs_extlen_t		minlen;	/* minimum allocation size (blocks) */
379799b438SDave Chinner 	xfs_extlen_t		minleft; /* amount must be left after alloc */
389799b438SDave Chinner 	bool			eof;	/* set if allocating past last extent */
399799b438SDave Chinner 	bool			wasdel;	/* replacing a delayed allocation */
409799b438SDave Chinner 	bool			aeof;	/* allocated space at eof */
419799b438SDave Chinner 	bool			conv;	/* overwriting unwritten extents */
42292378edSDave Chinner 	int			datatype;/* data type being allocated */
43e7d410acSDave Chinner 	uint32_t		flags;
449799b438SDave Chinner };
459799b438SDave Chinner 
4684be0ffcSDave Chinner #define	XFS_BMAP_MAX_NMAP	4
4784be0ffcSDave Chinner 
4884be0ffcSDave Chinner /*
4984be0ffcSDave Chinner  * Flags for xfs_bmapi_*
5084be0ffcSDave Chinner  */
51e7d410acSDave Chinner #define XFS_BMAPI_ENTIRE	(1u << 0) /* return entire extent untrimmed */
52e7d410acSDave Chinner #define XFS_BMAPI_METADATA	(1u << 1) /* mapping metadata not user data */
53e7d410acSDave Chinner #define XFS_BMAPI_ATTRFORK	(1u << 2) /* use attribute fork not data */
54e7d410acSDave Chinner #define XFS_BMAPI_PREALLOC	(1u << 3) /* preallocating unwritten space */
55e7d410acSDave Chinner #define XFS_BMAPI_CONTIG	(1u << 4) /* must allocate only one extent */
5684be0ffcSDave Chinner /*
5784be0ffcSDave Chinner  * unwritten extent conversion - this needs write cache flushing and no additional
5884be0ffcSDave Chinner  * allocation alignments. When specified with XFS_BMAPI_PREALLOC it converts
5984be0ffcSDave Chinner  * from written to unwritten, otherwise convert from unwritten to written.
6084be0ffcSDave Chinner  */
61e7d410acSDave Chinner #define XFS_BMAPI_CONVERT	(1u << 5)
6284be0ffcSDave Chinner 
633fbbbea3SDave Chinner /*
643fbbbea3SDave Chinner  * allocate zeroed extents - this requires all newly allocated user data extents
653fbbbea3SDave Chinner  * to be initialised to zero. It will be ignored if XFS_BMAPI_METADATA is set.
663fbbbea3SDave Chinner  * Use in conjunction with XFS_BMAPI_CONVERT to convert unwritten extents found
673fbbbea3SDave Chinner  * during the allocation range to zeroed written extents.
683fbbbea3SDave Chinner  */
69e7d410acSDave Chinner #define XFS_BMAPI_ZERO		(1u << 6)
703fbbbea3SDave Chinner 
71f65306eaSDarrick J. Wong /*
72f65306eaSDarrick J. Wong  * Map the inode offset to the block given in ap->firstblock.  Primarily
73f65306eaSDarrick J. Wong  * used for reflink.  The range must be in a hole, and this flag cannot be
74f65306eaSDarrick J. Wong  * turned on with PREALLOC or CONVERT, and cannot be used on the attr fork.
754847acf8SDarrick J. Wong  *
764847acf8SDarrick J. Wong  * For bunmapi, this flag unmaps the range without adjusting quota, reducing
774847acf8SDarrick J. Wong  * refcount, or freeing the blocks.
78f65306eaSDarrick J. Wong  */
79e7d410acSDave Chinner #define XFS_BMAPI_REMAP		(1u << 7)
80f65306eaSDarrick J. Wong 
813993baebSDarrick J. Wong /* Map something in the CoW fork. */
82e7d410acSDave Chinner #define XFS_BMAPI_COWFORK	(1u << 8)
833993baebSDarrick J. Wong 
84fcb762f5SBrian Foster /* Skip online discard of freed extents */
85e7d410acSDave Chinner #define XFS_BMAPI_NODISCARD	(1u << 9)
86fcb762f5SBrian Foster 
8795eb308cSDarrick J. Wong /* Do not update the rmap btree.  Used for reconstructing bmbt from rmapbt. */
88e7d410acSDave Chinner #define XFS_BMAPI_NORMAP	(1u << 10)
8995eb308cSDarrick J. Wong 
9084be0ffcSDave Chinner #define XFS_BMAPI_FLAGS \
9184be0ffcSDave Chinner 	{ XFS_BMAPI_ENTIRE,	"ENTIRE" }, \
9284be0ffcSDave Chinner 	{ XFS_BMAPI_METADATA,	"METADATA" }, \
9384be0ffcSDave Chinner 	{ XFS_BMAPI_ATTRFORK,	"ATTRFORK" }, \
9484be0ffcSDave Chinner 	{ XFS_BMAPI_PREALLOC,	"PREALLOC" }, \
9584be0ffcSDave Chinner 	{ XFS_BMAPI_CONTIG,	"CONTIG" }, \
963fbbbea3SDave Chinner 	{ XFS_BMAPI_CONVERT,	"CONVERT" }, \
97f65306eaSDarrick J. Wong 	{ XFS_BMAPI_ZERO,	"ZERO" }, \
983993baebSDarrick J. Wong 	{ XFS_BMAPI_REMAP,	"REMAP" }, \
99d2b3964aSChristoph Hellwig 	{ XFS_BMAPI_COWFORK,	"COWFORK" }, \
10095eb308cSDarrick J. Wong 	{ XFS_BMAPI_NODISCARD,	"NODISCARD" }, \
10195eb308cSDarrick J. Wong 	{ XFS_BMAPI_NORMAP,	"NORMAP" }
10284be0ffcSDave Chinner 
10384be0ffcSDave Chinner 
xfs_bmapi_aflag(int w)10484be0ffcSDave Chinner static inline int xfs_bmapi_aflag(int w)
10584be0ffcSDave Chinner {
1063993baebSDarrick J. Wong 	return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK :
1073993baebSDarrick J. Wong 	       (w == XFS_COW_FORK ? XFS_BMAPI_COWFORK : 0));
1083993baebSDarrick J. Wong }
1093993baebSDarrick J. Wong 
xfs_bmapi_whichfork(uint32_t bmapi_flags)110e7d410acSDave Chinner static inline int xfs_bmapi_whichfork(uint32_t bmapi_flags)
1113993baebSDarrick J. Wong {
1123993baebSDarrick J. Wong 	if (bmapi_flags & XFS_BMAPI_COWFORK)
1133993baebSDarrick J. Wong 		return XFS_COW_FORK;
1143993baebSDarrick J. Wong 	else if (bmapi_flags & XFS_BMAPI_ATTRFORK)
1153993baebSDarrick J. Wong 		return XFS_ATTR_FORK;
1163993baebSDarrick J. Wong 	return XFS_DATA_FORK;
11784be0ffcSDave Chinner }
11884be0ffcSDave Chinner 
11984be0ffcSDave Chinner /*
12084be0ffcSDave Chinner  * Special values for xfs_bmbt_irec_t br_startblock field.
12184be0ffcSDave Chinner  */
12284be0ffcSDave Chinner #define	DELAYSTARTBLOCK		((xfs_fsblock_t)-1LL)
12384be0ffcSDave Chinner #define	HOLESTARTBLOCK		((xfs_fsblock_t)-2LL)
12484be0ffcSDave Chinner 
12584be0ffcSDave Chinner /*
12684be0ffcSDave Chinner  * Flags for xfs_bmap_add_extent*.
12784be0ffcSDave Chinner  */
1280e5b8e45SDave Chinner #define BMAP_LEFT_CONTIG	(1u << 0)
1290e5b8e45SDave Chinner #define BMAP_RIGHT_CONTIG	(1u << 1)
1300e5b8e45SDave Chinner #define BMAP_LEFT_FILLING	(1u << 2)
1310e5b8e45SDave Chinner #define BMAP_RIGHT_FILLING	(1u << 3)
1320e5b8e45SDave Chinner #define BMAP_LEFT_DELAY		(1u << 4)
1330e5b8e45SDave Chinner #define BMAP_RIGHT_DELAY	(1u << 5)
1340e5b8e45SDave Chinner #define BMAP_LEFT_VALID		(1u << 6)
1350e5b8e45SDave Chinner #define BMAP_RIGHT_VALID	(1u << 7)
1360e5b8e45SDave Chinner #define BMAP_ATTRFORK		(1u << 8)
1370e5b8e45SDave Chinner #define BMAP_COWFORK		(1u << 9)
13884be0ffcSDave Chinner 
13984be0ffcSDave Chinner #define XFS_BMAP_EXT_FLAGS \
14084be0ffcSDave Chinner 	{ BMAP_LEFT_CONTIG,	"LC" }, \
14184be0ffcSDave Chinner 	{ BMAP_RIGHT_CONTIG,	"RC" }, \
14284be0ffcSDave Chinner 	{ BMAP_LEFT_FILLING,	"LF" }, \
14384be0ffcSDave Chinner 	{ BMAP_RIGHT_FILLING,	"RF" }, \
1443993baebSDarrick J. Wong 	{ BMAP_ATTRFORK,	"ATTR" }, \
1453993baebSDarrick J. Wong 	{ BMAP_COWFORK,		"COW" }
14684be0ffcSDave Chinner 
14700fd1d56SDarrick J. Wong /* Return true if the extent is an allocated extent, written or not. */
xfs_bmap_is_real_extent(const struct xfs_bmbt_irec * irec)148*634d4a79SDarrick J. Wong static inline bool xfs_bmap_is_real_extent(const struct xfs_bmbt_irec *irec)
14900fd1d56SDarrick J. Wong {
15000fd1d56SDarrick J. Wong 	return irec->br_startblock != HOLESTARTBLOCK &&
15100fd1d56SDarrick J. Wong 		irec->br_startblock != DELAYSTARTBLOCK &&
15200fd1d56SDarrick J. Wong 		!isnullstartblock(irec->br_startblock);
15300fd1d56SDarrick J. Wong }
15484be0ffcSDave Chinner 
15584be0ffcSDave Chinner /*
1569c4f29d3SChristoph Hellwig  * Return true if the extent is a real, allocated extent, or false if it is  a
1579c4f29d3SChristoph Hellwig  * delayed allocation, and unwritten extent or a hole.
1589c4f29d3SChristoph Hellwig  */
xfs_bmap_is_written_extent(struct xfs_bmbt_irec * irec)159877f58f5SDarrick J. Wong static inline bool xfs_bmap_is_written_extent(struct xfs_bmbt_irec *irec)
1609c4f29d3SChristoph Hellwig {
16100fd1d56SDarrick J. Wong 	return xfs_bmap_is_real_extent(irec) &&
16200fd1d56SDarrick J. Wong 	       irec->br_state != XFS_EXT_UNWRITTEN;
1639c4f29d3SChristoph Hellwig }
1649c4f29d3SChristoph Hellwig 
165eb77b23bSChristoph Hellwig /*
166eb77b23bSChristoph Hellwig  * Check the mapping for obviously garbage allocations that could trash the
167eb77b23bSChristoph Hellwig  * filesystem immediately.
168eb77b23bSChristoph Hellwig  */
169eb77b23bSChristoph Hellwig #define xfs_valid_startblock(ip, startblock) \
170eb77b23bSChristoph Hellwig 	((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))
171eb77b23bSChristoph Hellwig 
17205cf492aSDave Chinner int	xfs_bmap_longest_free_extent(struct xfs_perag *pag,
17305cf492aSDave Chinner 		struct xfs_trans *tp, xfs_extlen_t *blen);
1740a0af28cSDarrick J. Wong void	xfs_trim_extent(struct xfs_bmbt_irec *irec, xfs_fileoff_t bno,
1750a0af28cSDarrick J. Wong 		xfs_filblks_t len);
176b2941046SDave Chinner unsigned int xfs_bmap_compute_attr_offset(struct xfs_mount *mp);
17784be0ffcSDave Chinner int	xfs_bmap_add_attrfork(struct xfs_inode *ip, int size, int rsvd);
178aeea4b75SBrian Foster void	xfs_bmap_local_to_extents_empty(struct xfs_trans *tp,
179aeea4b75SBrian Foster 		struct xfs_inode *ip, int whichfork);
18084be0ffcSDave Chinner void	xfs_bmap_compute_maxlevels(struct xfs_mount *mp, int whichfork);
18184be0ffcSDave Chinner int	xfs_bmap_first_unused(struct xfs_trans *tp, struct xfs_inode *ip,
18284be0ffcSDave Chinner 		xfs_extlen_t len, xfs_fileoff_t *unused, int whichfork);
18384be0ffcSDave Chinner int	xfs_bmap_last_before(struct xfs_trans *tp, struct xfs_inode *ip,
18484be0ffcSDave Chinner 		xfs_fileoff_t *last_block, int whichfork);
18584be0ffcSDave Chinner int	xfs_bmap_last_offset(struct xfs_inode *ip, xfs_fileoff_t *unused,
18684be0ffcSDave Chinner 		int whichfork);
18784be0ffcSDave Chinner int	xfs_bmapi_read(struct xfs_inode *ip, xfs_fileoff_t bno,
18884be0ffcSDave Chinner 		xfs_filblks_t len, struct xfs_bmbt_irec *mval,
189e7d410acSDave Chinner 		int *nmap, uint32_t flags);
19084be0ffcSDave Chinner int	xfs_bmapi_write(struct xfs_trans *tp, struct xfs_inode *ip,
191e7d410acSDave Chinner 		xfs_fileoff_t bno, xfs_filblks_t len, uint32_t flags,
192a7beabeaSBrian Foster 		xfs_extlen_t total, struct xfs_bmbt_irec *mval, int *nmap);
1934453593bSDarrick J. Wong int	__xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
194e7d410acSDave Chinner 		xfs_fileoff_t bno, xfs_filblks_t *rlen, uint32_t flags,
1952af52842SBrian Foster 		xfs_extnum_t nexts);
19684be0ffcSDave Chinner int	xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
197e7d410acSDave Chinner 		xfs_fileoff_t bno, xfs_filblks_t len, uint32_t flags,
1982af52842SBrian Foster 		xfs_extnum_t nexts, int *done);
199fa5c836cSChristoph Hellwig int	xfs_bmap_del_extent_delay(struct xfs_inode *ip, int whichfork,
200b2b1712aSChristoph Hellwig 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
201fa5c836cSChristoph Hellwig 		struct xfs_bmbt_irec *del);
202b2b1712aSChristoph Hellwig void	xfs_bmap_del_extent_cow(struct xfs_inode *ip,
203b2b1712aSChristoph Hellwig 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
204b2b1712aSChristoph Hellwig 		struct xfs_bmbt_irec *del);
20584be0ffcSDave Chinner uint	xfs_default_attroffset(struct xfs_inode *ip);
206ecfea3f0SChristoph Hellwig int	xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
207a904b1caSNamjae Jeon 		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
208333f950cSBrian Foster 		bool *done);
209f62cb48eSDarrick J. Wong int	xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
210f62cb48eSDarrick J. Wong 		xfs_fileoff_t shift);
211ecfea3f0SChristoph Hellwig int	xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
212ecfea3f0SChristoph Hellwig 		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
213333f950cSBrian Foster 		bool *done, xfs_fileoff_t stop_fsb);
214b73df17eSBrian Foster int	xfs_bmap_split_extent(struct xfs_trans *tp, struct xfs_inode *ip,
215b73df17eSBrian Foster 		xfs_fileoff_t split_offset);
216be51f811SDarrick J. Wong int	xfs_bmapi_reserve_delalloc(struct xfs_inode *ip, int whichfork,
217974ae922SBrian Foster 		xfs_fileoff_t off, xfs_filblks_t len, xfs_filblks_t prealloc,
218b2b1712aSChristoph Hellwig 		struct xfs_bmbt_irec *got, struct xfs_iext_cursor *cur,
219b2b1712aSChristoph Hellwig 		int eof);
220491ce61eSChristoph Hellwig int	xfs_bmapi_convert_delalloc(struct xfs_inode *ip, int whichfork,
2214e087a3bSChristoph Hellwig 		xfs_off_t offset, struct iomap *iomap, unsigned int *seq);
22226b91c72SChristoph Hellwig int	xfs_bmap_add_extent_unwritten_real(struct xfs_trans *tp,
22326b91c72SChristoph Hellwig 		struct xfs_inode *ip, int whichfork,
22426b91c72SChristoph Hellwig 		struct xfs_iext_cursor *icur, struct xfs_btree_cur **curp,
22526b91c72SChristoph Hellwig 		struct xfs_bmbt_irec *new, int *logflagsp);
226d5753847SDave Chinner xfs_extlen_t xfs_bmapi_minleft(struct xfs_trans *tp, struct xfs_inode *ip,
227d5753847SDave Chinner 		int fork);
2288f7747adSDave Chinner int	xfs_bmap_btalloc_low_space(struct xfs_bmalloca *ap,
2298f7747adSDave Chinner 		struct xfs_alloc_arg *args);
23084be0ffcSDave Chinner 
23177d61fe4SDarrick J. Wong enum xfs_bmap_intent_type {
23277d61fe4SDarrick J. Wong 	XFS_BMAP_MAP = 1,
23377d61fe4SDarrick J. Wong 	XFS_BMAP_UNMAP,
23477d61fe4SDarrick J. Wong };
23577d61fe4SDarrick J. Wong 
23677d61fe4SDarrick J. Wong struct xfs_bmap_intent {
23777d61fe4SDarrick J. Wong 	struct list_head			bi_list;
23877d61fe4SDarrick J. Wong 	enum xfs_bmap_intent_type		bi_type;
23977d61fe4SDarrick J. Wong 	int					bi_whichfork;
2409e253954SDarrick J. Wong 	struct xfs_inode			*bi_owner;
241774a99b4SDarrick J. Wong 	struct xfs_perag			*bi_pag;
24277d61fe4SDarrick J. Wong 	struct xfs_bmbt_irec			bi_bmap;
24377d61fe4SDarrick J. Wong };
24477d61fe4SDarrick J. Wong 
245774a99b4SDarrick J. Wong void xfs_bmap_update_get_group(struct xfs_mount *mp,
246774a99b4SDarrick J. Wong 		struct xfs_bmap_intent *bi);
247774a99b4SDarrick J. Wong 
248ddccb81bSDarrick J. Wong int	xfs_bmap_finish_one(struct xfs_trans *tp, struct xfs_bmap_intent *bi);
2493e08f42aSDarrick J. Wong void	xfs_bmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
2500f37d178SBrian Foster 		struct xfs_bmbt_irec *imap);
2513e08f42aSDarrick J. Wong void	xfs_bmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
2520f37d178SBrian Foster 		struct xfs_bmbt_irec *imap);
2539f3afb57SDarrick J. Wong 
xfs_bmap_fork_to_state(int whichfork)2540e5b8e45SDave Chinner static inline uint32_t xfs_bmap_fork_to_state(int whichfork)
255060ea65bSChristoph Hellwig {
256060ea65bSChristoph Hellwig 	switch (whichfork) {
257060ea65bSChristoph Hellwig 	case XFS_ATTR_FORK:
258060ea65bSChristoph Hellwig 		return BMAP_ATTRFORK;
259060ea65bSChristoph Hellwig 	case XFS_COW_FORK:
260060ea65bSChristoph Hellwig 		return BMAP_COWFORK;
261060ea65bSChristoph Hellwig 	default:
262060ea65bSChristoph Hellwig 		return 0;
263060ea65bSChristoph Hellwig 	}
264060ea65bSChristoph Hellwig }
265060ea65bSChristoph Hellwig 
26630b0984dSDarrick J. Wong xfs_failaddr_t xfs_bmap_validate_extent(struct xfs_inode *ip, int whichfork,
26730b0984dSDarrick J. Wong 		struct xfs_bmbt_irec *irec);
2686a3bd8fcSDarrick J. Wong int xfs_bmap_complain_bad_rec(struct xfs_inode *ip, int whichfork,
2696a3bd8fcSDarrick J. Wong 		xfs_failaddr_t fa, const struct xfs_bmbt_irec *irec);
27030b0984dSDarrick J. Wong 
2717cf199baSDarrick J. Wong int	xfs_bmapi_remap(struct xfs_trans *tp, struct xfs_inode *ip,
2727cf199baSDarrick J. Wong 		xfs_fileoff_t bno, xfs_filblks_t len, xfs_fsblock_t startblock,
273e7d410acSDave Chinner 		uint32_t flags);
2747cf199baSDarrick J. Wong 
275f3c799c2SDarrick J. Wong extern struct kmem_cache	*xfs_bmap_intent_cache;
276f3c799c2SDarrick J. Wong 
277f3c799c2SDarrick J. Wong int __init xfs_bmap_intent_init_cache(void);
278f3c799c2SDarrick J. Wong void xfs_bmap_intent_destroy_cache(void);
279f3c799c2SDarrick J. Wong 
28084be0ffcSDave Chinner #endif	/* __XFS_BMAP_H__ */
281