1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #ifndef __XFS_BMAP_H__
7*4882a593Smuzhiyun #define __XFS_BMAP_H__
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun struct getbmap;
10*4882a593Smuzhiyun struct xfs_bmbt_irec;
11*4882a593Smuzhiyun struct xfs_ifork;
12*4882a593Smuzhiyun struct xfs_inode;
13*4882a593Smuzhiyun struct xfs_mount;
14*4882a593Smuzhiyun struct xfs_trans;
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun extern kmem_zone_t *xfs_bmap_free_item_zone;
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun * Argument structure for xfs_bmap_alloc.
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun struct xfs_bmalloca {
22*4882a593Smuzhiyun struct xfs_trans *tp; /* transaction pointer */
23*4882a593Smuzhiyun struct xfs_inode *ip; /* incore inode pointer */
24*4882a593Smuzhiyun struct xfs_bmbt_irec prev; /* extent before the new one */
25*4882a593Smuzhiyun struct xfs_bmbt_irec got; /* extent after, or delayed */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun xfs_fileoff_t offset; /* offset in file filling in */
28*4882a593Smuzhiyun xfs_extlen_t length; /* i/o length asked/allocated */
29*4882a593Smuzhiyun xfs_fsblock_t blkno; /* starting block of new extent */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct xfs_btree_cur *cur; /* btree cursor */
32*4882a593Smuzhiyun struct xfs_iext_cursor icur; /* incore extent cursor */
33*4882a593Smuzhiyun int nallocs;/* number of extents alloc'd */
34*4882a593Smuzhiyun int logflags;/* flags for transaction logging */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun xfs_extlen_t total; /* total blocks needed for xaction */
37*4882a593Smuzhiyun xfs_extlen_t minlen; /* minimum allocation size (blocks) */
38*4882a593Smuzhiyun xfs_extlen_t minleft; /* amount must be left after alloc */
39*4882a593Smuzhiyun bool eof; /* set if allocating past last extent */
40*4882a593Smuzhiyun bool wasdel; /* replacing a delayed allocation */
41*4882a593Smuzhiyun bool aeof; /* allocated space at eof */
42*4882a593Smuzhiyun bool conv; /* overwriting unwritten extents */
43*4882a593Smuzhiyun int datatype;/* data type being allocated */
44*4882a593Smuzhiyun int flags;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * List of extents to be free "later".
49*4882a593Smuzhiyun * The list is kept sorted on xbf_startblock.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun struct xfs_extent_free_item
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun xfs_fsblock_t xefi_startblock;/* starting fs block number */
54*4882a593Smuzhiyun xfs_extlen_t xefi_blockcount;/* number of blocks in extent */
55*4882a593Smuzhiyun bool xefi_skip_discard;
56*4882a593Smuzhiyun struct list_head xefi_list;
57*4882a593Smuzhiyun struct xfs_owner_info xefi_oinfo; /* extent owner */
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #define XFS_BMAP_MAX_NMAP 4
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun * Flags for xfs_bmapi_*
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun #define XFS_BMAPI_ENTIRE 0x001 /* return entire extent, not trimmed */
66*4882a593Smuzhiyun #define XFS_BMAPI_METADATA 0x002 /* mapping metadata not user data */
67*4882a593Smuzhiyun #define XFS_BMAPI_ATTRFORK 0x004 /* use attribute fork not data */
68*4882a593Smuzhiyun #define XFS_BMAPI_PREALLOC 0x008 /* preallocation op: unwritten space */
69*4882a593Smuzhiyun #define XFS_BMAPI_CONTIG 0x020 /* must allocate only one extent */
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * unwritten extent conversion - this needs write cache flushing and no additional
72*4882a593Smuzhiyun * allocation alignments. When specified with XFS_BMAPI_PREALLOC it converts
73*4882a593Smuzhiyun * from written to unwritten, otherwise convert from unwritten to written.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun #define XFS_BMAPI_CONVERT 0x040
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * allocate zeroed extents - this requires all newly allocated user data extents
79*4882a593Smuzhiyun * to be initialised to zero. It will be ignored if XFS_BMAPI_METADATA is set.
80*4882a593Smuzhiyun * Use in conjunction with XFS_BMAPI_CONVERT to convert unwritten extents found
81*4882a593Smuzhiyun * during the allocation range to zeroed written extents.
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun #define XFS_BMAPI_ZERO 0x080
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * Map the inode offset to the block given in ap->firstblock. Primarily
87*4882a593Smuzhiyun * used for reflink. The range must be in a hole, and this flag cannot be
88*4882a593Smuzhiyun * turned on with PREALLOC or CONVERT, and cannot be used on the attr fork.
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * For bunmapi, this flag unmaps the range without adjusting quota, reducing
91*4882a593Smuzhiyun * refcount, or freeing the blocks.
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun #define XFS_BMAPI_REMAP 0x100
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* Map something in the CoW fork. */
96*4882a593Smuzhiyun #define XFS_BMAPI_COWFORK 0x200
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* Skip online discard of freed extents */
99*4882a593Smuzhiyun #define XFS_BMAPI_NODISCARD 0x1000
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Do not update the rmap btree. Used for reconstructing bmbt from rmapbt. */
102*4882a593Smuzhiyun #define XFS_BMAPI_NORMAP 0x2000
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun #define XFS_BMAPI_FLAGS \
105*4882a593Smuzhiyun { XFS_BMAPI_ENTIRE, "ENTIRE" }, \
106*4882a593Smuzhiyun { XFS_BMAPI_METADATA, "METADATA" }, \
107*4882a593Smuzhiyun { XFS_BMAPI_ATTRFORK, "ATTRFORK" }, \
108*4882a593Smuzhiyun { XFS_BMAPI_PREALLOC, "PREALLOC" }, \
109*4882a593Smuzhiyun { XFS_BMAPI_CONTIG, "CONTIG" }, \
110*4882a593Smuzhiyun { XFS_BMAPI_CONVERT, "CONVERT" }, \
111*4882a593Smuzhiyun { XFS_BMAPI_ZERO, "ZERO" }, \
112*4882a593Smuzhiyun { XFS_BMAPI_REMAP, "REMAP" }, \
113*4882a593Smuzhiyun { XFS_BMAPI_COWFORK, "COWFORK" }, \
114*4882a593Smuzhiyun { XFS_BMAPI_NODISCARD, "NODISCARD" }, \
115*4882a593Smuzhiyun { XFS_BMAPI_NORMAP, "NORMAP" }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun
xfs_bmapi_aflag(int w)118*4882a593Smuzhiyun static inline int xfs_bmapi_aflag(int w)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK :
121*4882a593Smuzhiyun (w == XFS_COW_FORK ? XFS_BMAPI_COWFORK : 0));
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
xfs_bmapi_whichfork(int bmapi_flags)124*4882a593Smuzhiyun static inline int xfs_bmapi_whichfork(int bmapi_flags)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun if (bmapi_flags & XFS_BMAPI_COWFORK)
127*4882a593Smuzhiyun return XFS_COW_FORK;
128*4882a593Smuzhiyun else if (bmapi_flags & XFS_BMAPI_ATTRFORK)
129*4882a593Smuzhiyun return XFS_ATTR_FORK;
130*4882a593Smuzhiyun return XFS_DATA_FORK;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*
134*4882a593Smuzhiyun * Special values for xfs_bmbt_irec_t br_startblock field.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun #define DELAYSTARTBLOCK ((xfs_fsblock_t)-1LL)
137*4882a593Smuzhiyun #define HOLESTARTBLOCK ((xfs_fsblock_t)-2LL)
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * Flags for xfs_bmap_add_extent*.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun #define BMAP_LEFT_CONTIG (1 << 0)
143*4882a593Smuzhiyun #define BMAP_RIGHT_CONTIG (1 << 1)
144*4882a593Smuzhiyun #define BMAP_LEFT_FILLING (1 << 2)
145*4882a593Smuzhiyun #define BMAP_RIGHT_FILLING (1 << 3)
146*4882a593Smuzhiyun #define BMAP_LEFT_DELAY (1 << 4)
147*4882a593Smuzhiyun #define BMAP_RIGHT_DELAY (1 << 5)
148*4882a593Smuzhiyun #define BMAP_LEFT_VALID (1 << 6)
149*4882a593Smuzhiyun #define BMAP_RIGHT_VALID (1 << 7)
150*4882a593Smuzhiyun #define BMAP_ATTRFORK (1 << 8)
151*4882a593Smuzhiyun #define BMAP_COWFORK (1 << 9)
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun #define XFS_BMAP_EXT_FLAGS \
154*4882a593Smuzhiyun { BMAP_LEFT_CONTIG, "LC" }, \
155*4882a593Smuzhiyun { BMAP_RIGHT_CONTIG, "RC" }, \
156*4882a593Smuzhiyun { BMAP_LEFT_FILLING, "LF" }, \
157*4882a593Smuzhiyun { BMAP_RIGHT_FILLING, "RF" }, \
158*4882a593Smuzhiyun { BMAP_ATTRFORK, "ATTR" }, \
159*4882a593Smuzhiyun { BMAP_COWFORK, "COW" }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Return true if the extent is an allocated extent, written or not. */
xfs_bmap_is_real_extent(struct xfs_bmbt_irec * irec)162*4882a593Smuzhiyun static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun return irec->br_startblock != HOLESTARTBLOCK &&
165*4882a593Smuzhiyun irec->br_startblock != DELAYSTARTBLOCK &&
166*4882a593Smuzhiyun !isnullstartblock(irec->br_startblock);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * Return true if the extent is a real, allocated extent, or false if it is a
171*4882a593Smuzhiyun * delayed allocation, and unwritten extent or a hole.
172*4882a593Smuzhiyun */
xfs_bmap_is_written_extent(struct xfs_bmbt_irec * irec)173*4882a593Smuzhiyun static inline bool xfs_bmap_is_written_extent(struct xfs_bmbt_irec *irec)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun return xfs_bmap_is_real_extent(irec) &&
176*4882a593Smuzhiyun irec->br_state != XFS_EXT_UNWRITTEN;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun * Check the mapping for obviously garbage allocations that could trash the
181*4882a593Smuzhiyun * filesystem immediately.
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun #define xfs_valid_startblock(ip, startblock) \
184*4882a593Smuzhiyun ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun void xfs_trim_extent(struct xfs_bmbt_irec *irec, xfs_fileoff_t bno,
187*4882a593Smuzhiyun xfs_filblks_t len);
188*4882a593Smuzhiyun int xfs_bmap_add_attrfork(struct xfs_inode *ip, int size, int rsvd);
189*4882a593Smuzhiyun int xfs_bmap_set_attrforkoff(struct xfs_inode *ip, int size, int *version);
190*4882a593Smuzhiyun void xfs_bmap_local_to_extents_empty(struct xfs_trans *tp,
191*4882a593Smuzhiyun struct xfs_inode *ip, int whichfork);
192*4882a593Smuzhiyun void __xfs_bmap_add_free(struct xfs_trans *tp, xfs_fsblock_t bno,
193*4882a593Smuzhiyun xfs_filblks_t len, const struct xfs_owner_info *oinfo,
194*4882a593Smuzhiyun bool skip_discard);
195*4882a593Smuzhiyun void xfs_bmap_compute_maxlevels(struct xfs_mount *mp, int whichfork);
196*4882a593Smuzhiyun int xfs_bmap_first_unused(struct xfs_trans *tp, struct xfs_inode *ip,
197*4882a593Smuzhiyun xfs_extlen_t len, xfs_fileoff_t *unused, int whichfork);
198*4882a593Smuzhiyun int xfs_bmap_last_before(struct xfs_trans *tp, struct xfs_inode *ip,
199*4882a593Smuzhiyun xfs_fileoff_t *last_block, int whichfork);
200*4882a593Smuzhiyun int xfs_bmap_last_offset(struct xfs_inode *ip, xfs_fileoff_t *unused,
201*4882a593Smuzhiyun int whichfork);
202*4882a593Smuzhiyun int xfs_bmap_one_block(struct xfs_inode *ip, int whichfork);
203*4882a593Smuzhiyun int xfs_bmapi_read(struct xfs_inode *ip, xfs_fileoff_t bno,
204*4882a593Smuzhiyun xfs_filblks_t len, struct xfs_bmbt_irec *mval,
205*4882a593Smuzhiyun int *nmap, int flags);
206*4882a593Smuzhiyun int xfs_bmapi_write(struct xfs_trans *tp, struct xfs_inode *ip,
207*4882a593Smuzhiyun xfs_fileoff_t bno, xfs_filblks_t len, int flags,
208*4882a593Smuzhiyun xfs_extlen_t total, struct xfs_bmbt_irec *mval, int *nmap);
209*4882a593Smuzhiyun int __xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
210*4882a593Smuzhiyun xfs_fileoff_t bno, xfs_filblks_t *rlen, int flags,
211*4882a593Smuzhiyun xfs_extnum_t nexts);
212*4882a593Smuzhiyun int xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
213*4882a593Smuzhiyun xfs_fileoff_t bno, xfs_filblks_t len, int flags,
214*4882a593Smuzhiyun xfs_extnum_t nexts, int *done);
215*4882a593Smuzhiyun int xfs_bmap_del_extent_delay(struct xfs_inode *ip, int whichfork,
216*4882a593Smuzhiyun struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
217*4882a593Smuzhiyun struct xfs_bmbt_irec *del);
218*4882a593Smuzhiyun void xfs_bmap_del_extent_cow(struct xfs_inode *ip,
219*4882a593Smuzhiyun struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
220*4882a593Smuzhiyun struct xfs_bmbt_irec *del);
221*4882a593Smuzhiyun uint xfs_default_attroffset(struct xfs_inode *ip);
222*4882a593Smuzhiyun int xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
223*4882a593Smuzhiyun xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
224*4882a593Smuzhiyun bool *done);
225*4882a593Smuzhiyun int xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
226*4882a593Smuzhiyun xfs_fileoff_t shift);
227*4882a593Smuzhiyun int xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
228*4882a593Smuzhiyun xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
229*4882a593Smuzhiyun bool *done, xfs_fileoff_t stop_fsb);
230*4882a593Smuzhiyun int xfs_bmap_split_extent(struct xfs_trans *tp, struct xfs_inode *ip,
231*4882a593Smuzhiyun xfs_fileoff_t split_offset);
232*4882a593Smuzhiyun int xfs_bmapi_reserve_delalloc(struct xfs_inode *ip, int whichfork,
233*4882a593Smuzhiyun xfs_fileoff_t off, xfs_filblks_t len, xfs_filblks_t prealloc,
234*4882a593Smuzhiyun struct xfs_bmbt_irec *got, struct xfs_iext_cursor *cur,
235*4882a593Smuzhiyun int eof);
236*4882a593Smuzhiyun int xfs_bmapi_convert_delalloc(struct xfs_inode *ip, int whichfork,
237*4882a593Smuzhiyun xfs_off_t offset, struct iomap *iomap, unsigned int *seq);
238*4882a593Smuzhiyun int xfs_bmap_add_extent_unwritten_real(struct xfs_trans *tp,
239*4882a593Smuzhiyun struct xfs_inode *ip, int whichfork,
240*4882a593Smuzhiyun struct xfs_iext_cursor *icur, struct xfs_btree_cur **curp,
241*4882a593Smuzhiyun struct xfs_bmbt_irec *new, int *logflagsp);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun static inline void
xfs_bmap_add_free(struct xfs_trans * tp,xfs_fsblock_t bno,xfs_filblks_t len,const struct xfs_owner_info * oinfo)244*4882a593Smuzhiyun xfs_bmap_add_free(
245*4882a593Smuzhiyun struct xfs_trans *tp,
246*4882a593Smuzhiyun xfs_fsblock_t bno,
247*4882a593Smuzhiyun xfs_filblks_t len,
248*4882a593Smuzhiyun const struct xfs_owner_info *oinfo)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun __xfs_bmap_add_free(tp, bno, len, oinfo, false);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun enum xfs_bmap_intent_type {
254*4882a593Smuzhiyun XFS_BMAP_MAP = 1,
255*4882a593Smuzhiyun XFS_BMAP_UNMAP,
256*4882a593Smuzhiyun };
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun struct xfs_bmap_intent {
259*4882a593Smuzhiyun struct list_head bi_list;
260*4882a593Smuzhiyun enum xfs_bmap_intent_type bi_type;
261*4882a593Smuzhiyun struct xfs_inode *bi_owner;
262*4882a593Smuzhiyun int bi_whichfork;
263*4882a593Smuzhiyun struct xfs_bmbt_irec bi_bmap;
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun int xfs_bmap_finish_one(struct xfs_trans *tp, struct xfs_inode *ip,
267*4882a593Smuzhiyun enum xfs_bmap_intent_type type, int whichfork,
268*4882a593Smuzhiyun xfs_fileoff_t startoff, xfs_fsblock_t startblock,
269*4882a593Smuzhiyun xfs_filblks_t *blockcount, xfs_exntst_t state);
270*4882a593Smuzhiyun void xfs_bmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
271*4882a593Smuzhiyun struct xfs_bmbt_irec *imap);
272*4882a593Smuzhiyun void xfs_bmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
273*4882a593Smuzhiyun struct xfs_bmbt_irec *imap);
274*4882a593Smuzhiyun
xfs_bmap_fork_to_state(int whichfork)275*4882a593Smuzhiyun static inline int xfs_bmap_fork_to_state(int whichfork)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun switch (whichfork) {
278*4882a593Smuzhiyun case XFS_ATTR_FORK:
279*4882a593Smuzhiyun return BMAP_ATTRFORK;
280*4882a593Smuzhiyun case XFS_COW_FORK:
281*4882a593Smuzhiyun return BMAP_COWFORK;
282*4882a593Smuzhiyun default:
283*4882a593Smuzhiyun return 0;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun xfs_failaddr_t xfs_bmap_validate_extent(struct xfs_inode *ip, int whichfork,
288*4882a593Smuzhiyun struct xfs_bmbt_irec *irec);
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun int xfs_bmapi_remap(struct xfs_trans *tp, struct xfs_inode *ip,
291*4882a593Smuzhiyun xfs_fileoff_t bno, xfs_filblks_t len, xfs_fsblock_t startblock,
292*4882a593Smuzhiyun int flags);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun #endif /* __XFS_BMAP_H__ */
295