xref: /openbmc/linux/fs/xfs/libxfs/xfs_format.h (revision 83a21c18)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #ifndef __XFS_FORMAT_H__
7 #define __XFS_FORMAT_H__
8 
9 /*
10  * XFS On Disk Format Definitions
11  *
12  * This header file defines all the on-disk format definitions for
13  * general XFS objects. Directory and attribute related objects are defined in
14  * xfs_da_format.h, which log and log item formats are defined in
15  * xfs_log_format.h. Everything else goes here.
16  */
17 
18 struct xfs_mount;
19 struct xfs_trans;
20 struct xfs_inode;
21 struct xfs_buf;
22 struct xfs_ifork;
23 
24 /*
25  * Super block
26  * Fits into a sector-sized buffer at address 0 of each allocation group.
27  * Only the first of these is ever updated except during growfs.
28  */
29 #define	XFS_SB_MAGIC		0x58465342	/* 'XFSB' */
30 #define	XFS_SB_VERSION_1	1		/* 5.3, 6.0.1, 6.1 */
31 #define	XFS_SB_VERSION_2	2		/* 6.2 - attributes */
32 #define	XFS_SB_VERSION_3	3		/* 6.2 - new inode version */
33 #define	XFS_SB_VERSION_4	4		/* 6.2+ - bitmask version */
34 #define	XFS_SB_VERSION_5	5		/* CRC enabled filesystem */
35 #define	XFS_SB_VERSION_NUMBITS		0x000f
36 #define	XFS_SB_VERSION_ALLFBITS		0xfff0
37 #define	XFS_SB_VERSION_ATTRBIT		0x0010
38 #define	XFS_SB_VERSION_NLINKBIT		0x0020
39 #define	XFS_SB_VERSION_QUOTABIT		0x0040
40 #define	XFS_SB_VERSION_ALIGNBIT		0x0080
41 #define	XFS_SB_VERSION_DALIGNBIT	0x0100
42 #define	XFS_SB_VERSION_SHAREDBIT	0x0200
43 #define XFS_SB_VERSION_LOGV2BIT		0x0400
44 #define XFS_SB_VERSION_SECTORBIT	0x0800
45 #define	XFS_SB_VERSION_EXTFLGBIT	0x1000
46 #define	XFS_SB_VERSION_DIRV2BIT		0x2000
47 #define	XFS_SB_VERSION_BORGBIT		0x4000	/* ASCII only case-insens. */
48 #define	XFS_SB_VERSION_MOREBITSBIT	0x8000
49 
50 /*
51  * The size of a single extended attribute on disk is limited by
52  * the size of index values within the attribute entries themselves.
53  * These are be16 fields, so we can only support attribute data
54  * sizes up to 2^16 bytes in length.
55  */
56 #define XFS_XATTR_SIZE_MAX (1 << 16)
57 
58 /*
59  * Supported feature bit list is just all bits in the versionnum field because
60  * we've used them all up and understand them all. Except, of course, for the
61  * shared superblock bit, which nobody knows what it does and so is unsupported.
62  */
63 #define	XFS_SB_VERSION_OKBITS		\
64 	((XFS_SB_VERSION_NUMBITS | XFS_SB_VERSION_ALLFBITS) & \
65 		~XFS_SB_VERSION_SHAREDBIT)
66 
67 /*
68  * There are two words to hold XFS "feature" bits: the original
69  * word, sb_versionnum, and sb_features2.  Whenever a bit is set in
70  * sb_features2, the feature bit XFS_SB_VERSION_MOREBITSBIT must be set.
71  *
72  * These defines represent bits in sb_features2.
73  */
74 #define XFS_SB_VERSION2_RESERVED1BIT	0x00000001
75 #define XFS_SB_VERSION2_LAZYSBCOUNTBIT	0x00000002	/* Superblk counters */
76 #define XFS_SB_VERSION2_RESERVED4BIT	0x00000004
77 #define XFS_SB_VERSION2_ATTR2BIT	0x00000008	/* Inline attr rework */
78 #define XFS_SB_VERSION2_PARENTBIT	0x00000010	/* parent pointers */
79 #define XFS_SB_VERSION2_PROJID32BIT	0x00000080	/* 32 bit project id */
80 #define XFS_SB_VERSION2_CRCBIT		0x00000100	/* metadata CRCs */
81 #define XFS_SB_VERSION2_FTYPE		0x00000200	/* inode type in dir */
82 
83 #define	XFS_SB_VERSION2_OKBITS		\
84 	(XFS_SB_VERSION2_LAZYSBCOUNTBIT	| \
85 	 XFS_SB_VERSION2_ATTR2BIT	| \
86 	 XFS_SB_VERSION2_PROJID32BIT	| \
87 	 XFS_SB_VERSION2_FTYPE)
88 
89 /* Maximum size of the xfs filesystem label, no terminating NULL */
90 #define XFSLABEL_MAX			12
91 
92 /*
93  * Superblock - in core version.  Must match the ondisk version below.
94  * Must be padded to 64 bit alignment.
95  */
96 typedef struct xfs_sb {
97 	uint32_t	sb_magicnum;	/* magic number == XFS_SB_MAGIC */
98 	uint32_t	sb_blocksize;	/* logical block size, bytes */
99 	xfs_rfsblock_t	sb_dblocks;	/* number of data blocks */
100 	xfs_rfsblock_t	sb_rblocks;	/* number of realtime blocks */
101 	xfs_rtblock_t	sb_rextents;	/* number of realtime extents */
102 	uuid_t		sb_uuid;	/* user-visible file system unique id */
103 	xfs_fsblock_t	sb_logstart;	/* starting block of log if internal */
104 	xfs_ino_t	sb_rootino;	/* root inode number */
105 	xfs_ino_t	sb_rbmino;	/* bitmap inode for realtime extents */
106 	xfs_ino_t	sb_rsumino;	/* summary inode for rt bitmap */
107 	xfs_agblock_t	sb_rextsize;	/* realtime extent size, blocks */
108 	xfs_agblock_t	sb_agblocks;	/* size of an allocation group */
109 	xfs_agnumber_t	sb_agcount;	/* number of allocation groups */
110 	xfs_extlen_t	sb_rbmblocks;	/* number of rt bitmap blocks */
111 	xfs_extlen_t	sb_logblocks;	/* number of log blocks */
112 	uint16_t	sb_versionnum;	/* header version == XFS_SB_VERSION */
113 	uint16_t	sb_sectsize;	/* volume sector size, bytes */
114 	uint16_t	sb_inodesize;	/* inode size, bytes */
115 	uint16_t	sb_inopblock;	/* inodes per block */
116 	char		sb_fname[XFSLABEL_MAX]; /* file system name */
117 	uint8_t		sb_blocklog;	/* log2 of sb_blocksize */
118 	uint8_t		sb_sectlog;	/* log2 of sb_sectsize */
119 	uint8_t		sb_inodelog;	/* log2 of sb_inodesize */
120 	uint8_t		sb_inopblog;	/* log2 of sb_inopblock */
121 	uint8_t		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
122 	uint8_t		sb_rextslog;	/* log2 of sb_rextents */
123 	uint8_t		sb_inprogress;	/* mkfs is in progress, don't mount */
124 	uint8_t		sb_imax_pct;	/* max % of fs for inode space */
125 					/* statistics */
126 	/*
127 	 * These fields must remain contiguous.  If you really
128 	 * want to change their layout, make sure you fix the
129 	 * code in xfs_trans_apply_sb_deltas().
130 	 */
131 	uint64_t	sb_icount;	/* allocated inodes */
132 	uint64_t	sb_ifree;	/* free inodes */
133 	uint64_t	sb_fdblocks;	/* free data blocks */
134 	uint64_t	sb_frextents;	/* free realtime extents */
135 	/*
136 	 * End contiguous fields.
137 	 */
138 	xfs_ino_t	sb_uquotino;	/* user quota inode */
139 	xfs_ino_t	sb_gquotino;	/* group quota inode */
140 	uint16_t	sb_qflags;	/* quota flags */
141 	uint8_t		sb_flags;	/* misc. flags */
142 	uint8_t		sb_shared_vn;	/* shared version number */
143 	xfs_extlen_t	sb_inoalignmt;	/* inode chunk alignment, fsblocks */
144 	uint32_t	sb_unit;	/* stripe or raid unit */
145 	uint32_t	sb_width;	/* stripe or raid width */
146 	uint8_t		sb_dirblklog;	/* log2 of dir block size (fsbs) */
147 	uint8_t		sb_logsectlog;	/* log2 of the log sector size */
148 	uint16_t	sb_logsectsize;	/* sector size for the log, bytes */
149 	uint32_t	sb_logsunit;	/* stripe unit size for the log */
150 	uint32_t	sb_features2;	/* additional feature bits */
151 
152 	/*
153 	 * bad features2 field as a result of failing to pad the sb structure to
154 	 * 64 bits. Some machines will be using this field for features2 bits.
155 	 * Easiest just to mark it bad and not use it for anything else.
156 	 *
157 	 * This is not kept up to date in memory; it is always overwritten by
158 	 * the value in sb_features2 when formatting the incore superblock to
159 	 * the disk buffer.
160 	 */
161 	uint32_t	sb_bad_features2;
162 
163 	/* version 5 superblock fields start here */
164 
165 	/* feature masks */
166 	uint32_t	sb_features_compat;
167 	uint32_t	sb_features_ro_compat;
168 	uint32_t	sb_features_incompat;
169 	uint32_t	sb_features_log_incompat;
170 
171 	uint32_t	sb_crc;		/* superblock crc */
172 	xfs_extlen_t	sb_spino_align;	/* sparse inode chunk alignment */
173 
174 	xfs_ino_t	sb_pquotino;	/* project quota inode */
175 	xfs_lsn_t	sb_lsn;		/* last write sequence */
176 	uuid_t		sb_meta_uuid;	/* metadata file system unique id */
177 
178 	/* must be padded to 64 bit alignment */
179 } xfs_sb_t;
180 
181 #define XFS_SB_CRC_OFF		offsetof(struct xfs_sb, sb_crc)
182 
183 /*
184  * Superblock - on disk version.  Must match the in core version above.
185  * Must be padded to 64 bit alignment.
186  */
187 struct xfs_dsb {
188 	__be32		sb_magicnum;	/* magic number == XFS_SB_MAGIC */
189 	__be32		sb_blocksize;	/* logical block size, bytes */
190 	__be64		sb_dblocks;	/* number of data blocks */
191 	__be64		sb_rblocks;	/* number of realtime blocks */
192 	__be64		sb_rextents;	/* number of realtime extents */
193 	uuid_t		sb_uuid;	/* user-visible file system unique id */
194 	__be64		sb_logstart;	/* starting block of log if internal */
195 	__be64		sb_rootino;	/* root inode number */
196 	__be64		sb_rbmino;	/* bitmap inode for realtime extents */
197 	__be64		sb_rsumino;	/* summary inode for rt bitmap */
198 	__be32		sb_rextsize;	/* realtime extent size, blocks */
199 	__be32		sb_agblocks;	/* size of an allocation group */
200 	__be32		sb_agcount;	/* number of allocation groups */
201 	__be32		sb_rbmblocks;	/* number of rt bitmap blocks */
202 	__be32		sb_logblocks;	/* number of log blocks */
203 	__be16		sb_versionnum;	/* header version == XFS_SB_VERSION */
204 	__be16		sb_sectsize;	/* volume sector size, bytes */
205 	__be16		sb_inodesize;	/* inode size, bytes */
206 	__be16		sb_inopblock;	/* inodes per block */
207 	char		sb_fname[XFSLABEL_MAX]; /* file system name */
208 	__u8		sb_blocklog;	/* log2 of sb_blocksize */
209 	__u8		sb_sectlog;	/* log2 of sb_sectsize */
210 	__u8		sb_inodelog;	/* log2 of sb_inodesize */
211 	__u8		sb_inopblog;	/* log2 of sb_inopblock */
212 	__u8		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
213 	__u8		sb_rextslog;	/* log2 of sb_rextents */
214 	__u8		sb_inprogress;	/* mkfs is in progress, don't mount */
215 	__u8		sb_imax_pct;	/* max % of fs for inode space */
216 					/* statistics */
217 	/*
218 	 * These fields must remain contiguous.  If you really
219 	 * want to change their layout, make sure you fix the
220 	 * code in xfs_trans_apply_sb_deltas().
221 	 */
222 	__be64		sb_icount;	/* allocated inodes */
223 	__be64		sb_ifree;	/* free inodes */
224 	__be64		sb_fdblocks;	/* free data blocks */
225 	__be64		sb_frextents;	/* free realtime extents */
226 	/*
227 	 * End contiguous fields.
228 	 */
229 	__be64		sb_uquotino;	/* user quota inode */
230 	__be64		sb_gquotino;	/* group quota inode */
231 	__be16		sb_qflags;	/* quota flags */
232 	__u8		sb_flags;	/* misc. flags */
233 	__u8		sb_shared_vn;	/* shared version number */
234 	__be32		sb_inoalignmt;	/* inode chunk alignment, fsblocks */
235 	__be32		sb_unit;	/* stripe or raid unit */
236 	__be32		sb_width;	/* stripe or raid width */
237 	__u8		sb_dirblklog;	/* log2 of dir block size (fsbs) */
238 	__u8		sb_logsectlog;	/* log2 of the log sector size */
239 	__be16		sb_logsectsize;	/* sector size for the log, bytes */
240 	__be32		sb_logsunit;	/* stripe unit size for the log */
241 	__be32		sb_features2;	/* additional feature bits */
242 	/*
243 	 * bad features2 field as a result of failing to pad the sb
244 	 * structure to 64 bits. Some machines will be using this field
245 	 * for features2 bits. Easiest just to mark it bad and not use
246 	 * it for anything else.
247 	 */
248 	__be32		sb_bad_features2;
249 
250 	/* version 5 superblock fields start here */
251 
252 	/* feature masks */
253 	__be32		sb_features_compat;
254 	__be32		sb_features_ro_compat;
255 	__be32		sb_features_incompat;
256 	__be32		sb_features_log_incompat;
257 
258 	__le32		sb_crc;		/* superblock crc */
259 	__be32		sb_spino_align;	/* sparse inode chunk alignment */
260 
261 	__be64		sb_pquotino;	/* project quota inode */
262 	__be64		sb_lsn;		/* last write sequence */
263 	uuid_t		sb_meta_uuid;	/* metadata file system unique id */
264 
265 	/* must be padded to 64 bit alignment */
266 };
267 
268 /*
269  * Misc. Flags - warning - these will be cleared by xfs_repair unless
270  * a feature bit is set when the flag is used.
271  */
272 #define XFS_SBF_NOFLAGS		0x00	/* no flags set */
273 #define XFS_SBF_READONLY	0x01	/* only read-only mounts allowed */
274 
275 /*
276  * define max. shared version we can interoperate with
277  */
278 #define XFS_SB_MAX_SHARED_VN	0
279 
280 #define	XFS_SB_VERSION_NUM(sbp)	((sbp)->sb_versionnum & XFS_SB_VERSION_NUMBITS)
281 
282 static inline bool xfs_sb_is_v5(struct xfs_sb *sbp)
283 {
284 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5;
285 }
286 
287 /*
288  * Detect a mismatched features2 field.  Older kernels read/wrote
289  * this into the wrong slot, so to be safe we keep them in sync.
290  */
291 static inline bool xfs_sb_has_mismatched_features2(struct xfs_sb *sbp)
292 {
293 	return sbp->sb_bad_features2 != sbp->sb_features2;
294 }
295 
296 static inline bool xfs_sb_version_hasmorebits(struct xfs_sb *sbp)
297 {
298 	return xfs_sb_is_v5(sbp) ||
299 	       (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT);
300 }
301 
302 static inline void xfs_sb_version_addattr(struct xfs_sb *sbp)
303 {
304 	sbp->sb_versionnum |= XFS_SB_VERSION_ATTRBIT;
305 }
306 
307 static inline void xfs_sb_version_addquota(struct xfs_sb *sbp)
308 {
309 	sbp->sb_versionnum |= XFS_SB_VERSION_QUOTABIT;
310 }
311 
312 static inline void xfs_sb_version_addattr2(struct xfs_sb *sbp)
313 {
314 	sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
315 	sbp->sb_features2 |= XFS_SB_VERSION2_ATTR2BIT;
316 }
317 
318 static inline void xfs_sb_version_addprojid32(struct xfs_sb *sbp)
319 {
320 	sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
321 	sbp->sb_features2 |= XFS_SB_VERSION2_PROJID32BIT;
322 }
323 
324 /*
325  * Extended v5 superblock feature masks. These are to be used for new v5
326  * superblock features only.
327  *
328  * Compat features are new features that old kernels will not notice or affect
329  * and so can mount read-write without issues.
330  *
331  * RO-Compat (read only) are features that old kernels can read but will break
332  * if they write. Hence only read-only mounts of such filesystems are allowed on
333  * kernels that don't support the feature bit.
334  *
335  * InCompat features are features which old kernels will not understand and so
336  * must not mount.
337  *
338  * Log-InCompat features are for changes to log formats or new transactions that
339  * can't be replayed on older kernels. The fields are set when the filesystem is
340  * mounted, and a clean unmount clears the fields.
341  */
342 #define XFS_SB_FEAT_COMPAT_ALL 0
343 #define XFS_SB_FEAT_COMPAT_UNKNOWN	~XFS_SB_FEAT_COMPAT_ALL
344 static inline bool
345 xfs_sb_has_compat_feature(
346 	struct xfs_sb	*sbp,
347 	uint32_t	feature)
348 {
349 	return (sbp->sb_features_compat & feature) != 0;
350 }
351 
352 #define XFS_SB_FEAT_RO_COMPAT_FINOBT   (1 << 0)		/* free inode btree */
353 #define XFS_SB_FEAT_RO_COMPAT_RMAPBT   (1 << 1)		/* reverse map btree */
354 #define XFS_SB_FEAT_RO_COMPAT_REFLINK  (1 << 2)		/* reflinked files */
355 #define XFS_SB_FEAT_RO_COMPAT_INOBTCNT (1 << 3)		/* inobt block counts */
356 #define XFS_SB_FEAT_RO_COMPAT_ALL \
357 		(XFS_SB_FEAT_RO_COMPAT_FINOBT | \
358 		 XFS_SB_FEAT_RO_COMPAT_RMAPBT | \
359 		 XFS_SB_FEAT_RO_COMPAT_REFLINK| \
360 		 XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
361 #define XFS_SB_FEAT_RO_COMPAT_UNKNOWN	~XFS_SB_FEAT_RO_COMPAT_ALL
362 static inline bool
363 xfs_sb_has_ro_compat_feature(
364 	struct xfs_sb	*sbp,
365 	uint32_t	feature)
366 {
367 	return (sbp->sb_features_ro_compat & feature) != 0;
368 }
369 
370 #define XFS_SB_FEAT_INCOMPAT_FTYPE	(1 << 0)	/* filetype in dirent */
371 #define XFS_SB_FEAT_INCOMPAT_SPINODES	(1 << 1)	/* sparse inode chunks */
372 #define XFS_SB_FEAT_INCOMPAT_META_UUID	(1 << 2)	/* metadata UUID */
373 #define XFS_SB_FEAT_INCOMPAT_BIGTIME	(1 << 3)	/* large timestamps */
374 #define XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR (1 << 4)	/* needs xfs_repair */
375 #define XFS_SB_FEAT_INCOMPAT_NREXT64	(1 << 5)	/* large extent counters */
376 #define XFS_SB_FEAT_INCOMPAT_ALL \
377 		(XFS_SB_FEAT_INCOMPAT_FTYPE|	\
378 		 XFS_SB_FEAT_INCOMPAT_SPINODES|	\
379 		 XFS_SB_FEAT_INCOMPAT_META_UUID| \
380 		 XFS_SB_FEAT_INCOMPAT_BIGTIME| \
381 		 XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
382 
383 #define XFS_SB_FEAT_INCOMPAT_UNKNOWN	~XFS_SB_FEAT_INCOMPAT_ALL
384 static inline bool
385 xfs_sb_has_incompat_feature(
386 	struct xfs_sb	*sbp,
387 	uint32_t	feature)
388 {
389 	return (sbp->sb_features_incompat & feature) != 0;
390 }
391 
392 #define XFS_SB_FEAT_INCOMPAT_LOG_ALL 0
393 #define XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN	~XFS_SB_FEAT_INCOMPAT_LOG_ALL
394 static inline bool
395 xfs_sb_has_incompat_log_feature(
396 	struct xfs_sb	*sbp,
397 	uint32_t	feature)
398 {
399 	return (sbp->sb_features_log_incompat & feature) != 0;
400 }
401 
402 static inline void
403 xfs_sb_remove_incompat_log_features(
404 	struct xfs_sb	*sbp)
405 {
406 	sbp->sb_features_log_incompat &= ~XFS_SB_FEAT_INCOMPAT_LOG_ALL;
407 }
408 
409 static inline void
410 xfs_sb_add_incompat_log_features(
411 	struct xfs_sb	*sbp,
412 	unsigned int	features)
413 {
414 	sbp->sb_features_log_incompat |= features;
415 }
416 
417 
418 static inline bool
419 xfs_is_quota_inode(struct xfs_sb *sbp, xfs_ino_t ino)
420 {
421 	return (ino == sbp->sb_uquotino ||
422 		ino == sbp->sb_gquotino ||
423 		ino == sbp->sb_pquotino);
424 }
425 
426 #define XFS_SB_DADDR		((xfs_daddr_t)0) /* daddr in filesystem/ag */
427 #define	XFS_SB_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_SB_DADDR)
428 
429 #define	XFS_HDR_BLOCK(mp,d)	((xfs_agblock_t)XFS_BB_TO_FSBT(mp,d))
430 #define	XFS_DADDR_TO_FSB(mp,d)	XFS_AGB_TO_FSB(mp, \
431 			xfs_daddr_to_agno(mp,d), xfs_daddr_to_agbno(mp,d))
432 #define	XFS_FSB_TO_DADDR(mp,fsbno)	XFS_AGB_TO_DADDR(mp, \
433 			XFS_FSB_TO_AGNO(mp,fsbno), XFS_FSB_TO_AGBNO(mp,fsbno))
434 
435 /*
436  * File system sector to basic block conversions.
437  */
438 #define XFS_FSS_TO_BB(mp,sec)	((sec) << (mp)->m_sectbb_log)
439 
440 /*
441  * File system block to basic block conversions.
442  */
443 #define	XFS_FSB_TO_BB(mp,fsbno)	((fsbno) << (mp)->m_blkbb_log)
444 #define	XFS_BB_TO_FSB(mp,bb)	\
445 	(((bb) + (XFS_FSB_TO_BB(mp,1) - 1)) >> (mp)->m_blkbb_log)
446 #define	XFS_BB_TO_FSBT(mp,bb)	((bb) >> (mp)->m_blkbb_log)
447 
448 /*
449  * File system block to byte conversions.
450  */
451 #define XFS_FSB_TO_B(mp,fsbno)	((xfs_fsize_t)(fsbno) << (mp)->m_sb.sb_blocklog)
452 #define XFS_B_TO_FSB(mp,b)	\
453 	((((uint64_t)(b)) + (mp)->m_blockmask) >> (mp)->m_sb.sb_blocklog)
454 #define XFS_B_TO_FSBT(mp,b)	(((uint64_t)(b)) >> (mp)->m_sb.sb_blocklog)
455 
456 /*
457  * Allocation group header
458  *
459  * This is divided into three structures, placed in sequential 512-byte
460  * buffers after a copy of the superblock (also in a 512-byte buffer).
461  */
462 #define	XFS_AGF_MAGIC	0x58414746	/* 'XAGF' */
463 #define	XFS_AGI_MAGIC	0x58414749	/* 'XAGI' */
464 #define	XFS_AGFL_MAGIC	0x5841464c	/* 'XAFL' */
465 #define	XFS_AGF_VERSION	1
466 #define	XFS_AGI_VERSION	1
467 
468 #define	XFS_AGF_GOOD_VERSION(v)	((v) == XFS_AGF_VERSION)
469 #define	XFS_AGI_GOOD_VERSION(v)	((v) == XFS_AGI_VERSION)
470 
471 /*
472  * Btree number 0 is bno, 1 is cnt, 2 is rmap. This value gives the size of the
473  * arrays below.
474  */
475 #define	XFS_BTNUM_AGF	((int)XFS_BTNUM_RMAPi + 1)
476 
477 /*
478  * The second word of agf_levels in the first a.g. overlaps the EFS
479  * superblock's magic number.  Since the magic numbers valid for EFS
480  * are > 64k, our value cannot be confused for an EFS superblock's.
481  */
482 
483 typedef struct xfs_agf {
484 	/*
485 	 * Common allocation group header information
486 	 */
487 	__be32		agf_magicnum;	/* magic number == XFS_AGF_MAGIC */
488 	__be32		agf_versionnum;	/* header version == XFS_AGF_VERSION */
489 	__be32		agf_seqno;	/* sequence # starting from 0 */
490 	__be32		agf_length;	/* size in blocks of a.g. */
491 	/*
492 	 * Freespace and rmap information
493 	 */
494 	__be32		agf_roots[XFS_BTNUM_AGF];	/* root blocks */
495 	__be32		agf_levels[XFS_BTNUM_AGF];	/* btree levels */
496 
497 	__be32		agf_flfirst;	/* first freelist block's index */
498 	__be32		agf_fllast;	/* last freelist block's index */
499 	__be32		agf_flcount;	/* count of blocks in freelist */
500 	__be32		agf_freeblks;	/* total free blocks */
501 
502 	__be32		agf_longest;	/* longest free space */
503 	__be32		agf_btreeblks;	/* # of blocks held in AGF btrees */
504 	uuid_t		agf_uuid;	/* uuid of filesystem */
505 
506 	__be32		agf_rmap_blocks;	/* rmapbt blocks used */
507 	__be32		agf_refcount_blocks;	/* refcountbt blocks used */
508 
509 	__be32		agf_refcount_root;	/* refcount tree root block */
510 	__be32		agf_refcount_level;	/* refcount btree levels */
511 
512 	/*
513 	 * reserve some contiguous space for future logged fields before we add
514 	 * the unlogged fields. This makes the range logging via flags and
515 	 * structure offsets much simpler.
516 	 */
517 	__be64		agf_spare64[14];
518 
519 	/* unlogged fields, written during buffer writeback. */
520 	__be64		agf_lsn;	/* last write sequence */
521 	__be32		agf_crc;	/* crc of agf sector */
522 	__be32		agf_spare2;
523 
524 	/* structure must be padded to 64 bit alignment */
525 } xfs_agf_t;
526 
527 #define XFS_AGF_CRC_OFF		offsetof(struct xfs_agf, agf_crc)
528 
529 #define	XFS_AGF_MAGICNUM	0x00000001
530 #define	XFS_AGF_VERSIONNUM	0x00000002
531 #define	XFS_AGF_SEQNO		0x00000004
532 #define	XFS_AGF_LENGTH		0x00000008
533 #define	XFS_AGF_ROOTS		0x00000010
534 #define	XFS_AGF_LEVELS		0x00000020
535 #define	XFS_AGF_FLFIRST		0x00000040
536 #define	XFS_AGF_FLLAST		0x00000080
537 #define	XFS_AGF_FLCOUNT		0x00000100
538 #define	XFS_AGF_FREEBLKS	0x00000200
539 #define	XFS_AGF_LONGEST		0x00000400
540 #define	XFS_AGF_BTREEBLKS	0x00000800
541 #define	XFS_AGF_UUID		0x00001000
542 #define	XFS_AGF_RMAP_BLOCKS	0x00002000
543 #define	XFS_AGF_REFCOUNT_BLOCKS	0x00004000
544 #define	XFS_AGF_REFCOUNT_ROOT	0x00008000
545 #define	XFS_AGF_REFCOUNT_LEVEL	0x00010000
546 #define	XFS_AGF_SPARE64		0x00020000
547 #define	XFS_AGF_NUM_BITS	18
548 #define	XFS_AGF_ALL_BITS	((1 << XFS_AGF_NUM_BITS) - 1)
549 
550 #define XFS_AGF_FLAGS \
551 	{ XFS_AGF_MAGICNUM,	"MAGICNUM" }, \
552 	{ XFS_AGF_VERSIONNUM,	"VERSIONNUM" }, \
553 	{ XFS_AGF_SEQNO,	"SEQNO" }, \
554 	{ XFS_AGF_LENGTH,	"LENGTH" }, \
555 	{ XFS_AGF_ROOTS,	"ROOTS" }, \
556 	{ XFS_AGF_LEVELS,	"LEVELS" }, \
557 	{ XFS_AGF_FLFIRST,	"FLFIRST" }, \
558 	{ XFS_AGF_FLLAST,	"FLLAST" }, \
559 	{ XFS_AGF_FLCOUNT,	"FLCOUNT" }, \
560 	{ XFS_AGF_FREEBLKS,	"FREEBLKS" }, \
561 	{ XFS_AGF_LONGEST,	"LONGEST" }, \
562 	{ XFS_AGF_BTREEBLKS,	"BTREEBLKS" }, \
563 	{ XFS_AGF_UUID,		"UUID" }, \
564 	{ XFS_AGF_RMAP_BLOCKS,	"RMAP_BLOCKS" }, \
565 	{ XFS_AGF_REFCOUNT_BLOCKS,	"REFCOUNT_BLOCKS" }, \
566 	{ XFS_AGF_REFCOUNT_ROOT,	"REFCOUNT_ROOT" }, \
567 	{ XFS_AGF_REFCOUNT_LEVEL,	"REFCOUNT_LEVEL" }, \
568 	{ XFS_AGF_SPARE64,	"SPARE64" }
569 
570 /* disk block (xfs_daddr_t) in the AG */
571 #define XFS_AGF_DADDR(mp)	((xfs_daddr_t)(1 << (mp)->m_sectbb_log))
572 #define	XFS_AGF_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGF_DADDR(mp))
573 
574 /*
575  * Size of the unlinked inode hash table in the agi.
576  */
577 #define	XFS_AGI_UNLINKED_BUCKETS	64
578 
579 typedef struct xfs_agi {
580 	/*
581 	 * Common allocation group header information
582 	 */
583 	__be32		agi_magicnum;	/* magic number == XFS_AGI_MAGIC */
584 	__be32		agi_versionnum;	/* header version == XFS_AGI_VERSION */
585 	__be32		agi_seqno;	/* sequence # starting from 0 */
586 	__be32		agi_length;	/* size in blocks of a.g. */
587 	/*
588 	 * Inode information
589 	 * Inodes are mapped by interpreting the inode number, so no
590 	 * mapping data is needed here.
591 	 */
592 	__be32		agi_count;	/* count of allocated inodes */
593 	__be32		agi_root;	/* root of inode btree */
594 	__be32		agi_level;	/* levels in inode btree */
595 	__be32		agi_freecount;	/* number of free inodes */
596 
597 	__be32		agi_newino;	/* new inode just allocated */
598 	__be32		agi_dirino;	/* last directory inode chunk */
599 	/*
600 	 * Hash table of inodes which have been unlinked but are
601 	 * still being referenced.
602 	 */
603 	__be32		agi_unlinked[XFS_AGI_UNLINKED_BUCKETS];
604 	/*
605 	 * This marks the end of logging region 1 and start of logging region 2.
606 	 */
607 	uuid_t		agi_uuid;	/* uuid of filesystem */
608 	__be32		agi_crc;	/* crc of agi sector */
609 	__be32		agi_pad32;
610 	__be64		agi_lsn;	/* last write sequence */
611 
612 	__be32		agi_free_root; /* root of the free inode btree */
613 	__be32		agi_free_level;/* levels in free inode btree */
614 
615 	__be32		agi_iblocks;	/* inobt blocks used */
616 	__be32		agi_fblocks;	/* finobt blocks used */
617 
618 	/* structure must be padded to 64 bit alignment */
619 } xfs_agi_t;
620 
621 #define XFS_AGI_CRC_OFF		offsetof(struct xfs_agi, agi_crc)
622 
623 #define	XFS_AGI_MAGICNUM	(1 << 0)
624 #define	XFS_AGI_VERSIONNUM	(1 << 1)
625 #define	XFS_AGI_SEQNO		(1 << 2)
626 #define	XFS_AGI_LENGTH		(1 << 3)
627 #define	XFS_AGI_COUNT		(1 << 4)
628 #define	XFS_AGI_ROOT		(1 << 5)
629 #define	XFS_AGI_LEVEL		(1 << 6)
630 #define	XFS_AGI_FREECOUNT	(1 << 7)
631 #define	XFS_AGI_NEWINO		(1 << 8)
632 #define	XFS_AGI_DIRINO		(1 << 9)
633 #define	XFS_AGI_UNLINKED	(1 << 10)
634 #define	XFS_AGI_NUM_BITS_R1	11	/* end of the 1st agi logging region */
635 #define	XFS_AGI_ALL_BITS_R1	((1 << XFS_AGI_NUM_BITS_R1) - 1)
636 #define	XFS_AGI_FREE_ROOT	(1 << 11)
637 #define	XFS_AGI_FREE_LEVEL	(1 << 12)
638 #define	XFS_AGI_IBLOCKS		(1 << 13) /* both inobt/finobt block counters */
639 #define	XFS_AGI_NUM_BITS_R2	14
640 
641 /* disk block (xfs_daddr_t) in the AG */
642 #define XFS_AGI_DADDR(mp)	((xfs_daddr_t)(2 << (mp)->m_sectbb_log))
643 #define	XFS_AGI_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGI_DADDR(mp))
644 
645 /*
646  * The third a.g. block contains the a.g. freelist, an array
647  * of block pointers to blocks owned by the allocation btree code.
648  */
649 #define XFS_AGFL_DADDR(mp)	((xfs_daddr_t)(3 << (mp)->m_sectbb_log))
650 #define	XFS_AGFL_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGFL_DADDR(mp))
651 #define	XFS_BUF_TO_AGFL(bp)	((struct xfs_agfl *)((bp)->b_addr))
652 
653 struct xfs_agfl {
654 	__be32		agfl_magicnum;
655 	__be32		agfl_seqno;
656 	uuid_t		agfl_uuid;
657 	__be64		agfl_lsn;
658 	__be32		agfl_crc;
659 } __attribute__((packed));
660 
661 #define XFS_AGFL_CRC_OFF	offsetof(struct xfs_agfl, agfl_crc)
662 
663 #define XFS_AGB_TO_FSB(mp,agno,agbno)	\
664 	(((xfs_fsblock_t)(agno) << (mp)->m_sb.sb_agblklog) | (agbno))
665 #define	XFS_FSB_TO_AGNO(mp,fsbno)	\
666 	((xfs_agnumber_t)((fsbno) >> (mp)->m_sb.sb_agblklog))
667 #define	XFS_FSB_TO_AGBNO(mp,fsbno)	\
668 	((xfs_agblock_t)((fsbno) & xfs_mask32lo((mp)->m_sb.sb_agblklog)))
669 #define	XFS_AGB_TO_DADDR(mp,agno,agbno)	\
670 	((xfs_daddr_t)XFS_FSB_TO_BB(mp, \
671 		(xfs_fsblock_t)(agno) * (mp)->m_sb.sb_agblocks + (agbno)))
672 #define	XFS_AG_DADDR(mp,agno,d)		(XFS_AGB_TO_DADDR(mp, agno, 0) + (d))
673 
674 /*
675  * For checking for bad ranges of xfs_daddr_t's, covering multiple
676  * allocation groups or a single xfs_daddr_t that's a superblock copy.
677  */
678 #define	XFS_AG_CHECK_DADDR(mp,d,len)	\
679 	((len) == 1 ? \
680 	    ASSERT((d) == XFS_SB_DADDR || \
681 		   xfs_daddr_to_agbno(mp, d) != XFS_SB_DADDR) : \
682 	    ASSERT(xfs_daddr_to_agno(mp, d) == \
683 		   xfs_daddr_to_agno(mp, (d) + (len) - 1)))
684 
685 /*
686  * XFS Timestamps
687  * ==============
688  *
689  * Traditional ondisk inode timestamps consist of signed 32-bit counters for
690  * seconds and nanoseconds; time zero is the Unix epoch, Jan  1 00:00:00 UTC
691  * 1970, which means that the timestamp epoch is the same as the Unix epoch.
692  * Therefore, the ondisk min and max defined here can be used directly to
693  * constrain the incore timestamps on a Unix system.  Note that we actually
694  * encode a __be64 value on disk.
695  *
696  * When the bigtime feature is enabled, ondisk inode timestamps become an
697  * unsigned 64-bit nanoseconds counter.  This means that the bigtime inode
698  * timestamp epoch is the start of the classic timestamp range, which is
699  * Dec 31 20:45:52 UTC 1901.  Because the epochs are not the same, callers
700  * /must/ use the bigtime conversion functions when encoding and decoding raw
701  * timestamps.
702  */
703 typedef __be64 xfs_timestamp_t;
704 
705 /* Legacy timestamp encoding format. */
706 struct xfs_legacy_timestamp {
707 	__be32		t_sec;		/* timestamp seconds */
708 	__be32		t_nsec;		/* timestamp nanoseconds */
709 };
710 
711 /*
712  * Smallest possible ondisk seconds value with traditional timestamps.  This
713  * corresponds exactly with the incore timestamp Dec 13 20:45:52 UTC 1901.
714  */
715 #define XFS_LEGACY_TIME_MIN	((int64_t)S32_MIN)
716 
717 /*
718  * Largest possible ondisk seconds value with traditional timestamps.  This
719  * corresponds exactly with the incore timestamp Jan 19 03:14:07 UTC 2038.
720  */
721 #define XFS_LEGACY_TIME_MAX	((int64_t)S32_MAX)
722 
723 /*
724  * Smallest possible ondisk seconds value with bigtime timestamps.  This
725  * corresponds (after conversion to a Unix timestamp) with the traditional
726  * minimum timestamp of Dec 13 20:45:52 UTC 1901.
727  */
728 #define XFS_BIGTIME_TIME_MIN	((int64_t)0)
729 
730 /*
731  * Largest supported ondisk seconds value with bigtime timestamps.  This
732  * corresponds (after conversion to a Unix timestamp) with an incore timestamp
733  * of Jul  2 20:20:24 UTC 2486.
734  *
735  * We round down the ondisk limit so that the bigtime quota and inode max
736  * timestamps will be the same.
737  */
738 #define XFS_BIGTIME_TIME_MAX	((int64_t)((-1ULL / NSEC_PER_SEC) & ~0x3ULL))
739 
740 /*
741  * Bigtime epoch is set exactly to the minimum time value that a traditional
742  * 32-bit timestamp can represent when using the Unix epoch as a reference.
743  * Hence the Unix epoch is at a fixed offset into the supported bigtime
744  * timestamp range.
745  *
746  * The bigtime epoch also matches the minimum value an on-disk 32-bit XFS
747  * timestamp can represent so we will not lose any fidelity in converting
748  * to/from unix and bigtime timestamps.
749  *
750  * The following conversion factor converts a seconds counter from the Unix
751  * epoch to the bigtime epoch.
752  */
753 #define XFS_BIGTIME_EPOCH_OFFSET	(-(int64_t)S32_MIN)
754 
755 /* Convert a timestamp from the Unix epoch to the bigtime epoch. */
756 static inline uint64_t xfs_unix_to_bigtime(time64_t unix_seconds)
757 {
758 	return (uint64_t)unix_seconds + XFS_BIGTIME_EPOCH_OFFSET;
759 }
760 
761 /* Convert a timestamp from the bigtime epoch to the Unix epoch. */
762 static inline time64_t xfs_bigtime_to_unix(uint64_t ondisk_seconds)
763 {
764 	return (time64_t)ondisk_seconds - XFS_BIGTIME_EPOCH_OFFSET;
765 }
766 
767 /*
768  * On-disk inode structure.
769  *
770  * This is just the header or "dinode core", the inode is expanded to fill a
771  * variable size the leftover area split into a data and an attribute fork.
772  * The format of the data and attribute fork depends on the format of the
773  * inode as indicated by di_format and di_aformat.  To access the data and
774  * attribute use the XFS_DFORK_DPTR, XFS_DFORK_APTR, and XFS_DFORK_PTR macros
775  * below.
776  *
777  * There is a very similar struct xfs_log_dinode which matches the layout of
778  * this structure, but is kept in native format instead of big endian.
779  *
780  * Note: di_flushiter is only used by v1/2 inodes - it's effectively a zeroed
781  * padding field for v3 inodes.
782  */
783 #define	XFS_DINODE_MAGIC		0x494e	/* 'IN' */
784 struct xfs_dinode {
785 	__be16		di_magic;	/* inode magic # = XFS_DINODE_MAGIC */
786 	__be16		di_mode;	/* mode and type of file */
787 	__u8		di_version;	/* inode version */
788 	__u8		di_format;	/* format of di_c data */
789 	__be16		di_onlink;	/* old number of links to file */
790 	__be32		di_uid;		/* owner's user id */
791 	__be32		di_gid;		/* owner's group id */
792 	__be32		di_nlink;	/* number of links to file */
793 	__be16		di_projid_lo;	/* lower part of owner's project id */
794 	__be16		di_projid_hi;	/* higher part owner's project id */
795 	union {
796 		/* Number of data fork extents if NREXT64 is set */
797 		__be64	di_big_nextents;
798 
799 		/* Padding for V3 inodes without NREXT64 set. */
800 		__be64	di_v3_pad;
801 
802 		/* Padding and inode flush counter for V2 inodes. */
803 		struct {
804 			__u8	di_v2_pad[6];
805 			__be16	di_flushiter;
806 		};
807 	};
808 	xfs_timestamp_t	di_atime;	/* time last accessed */
809 	xfs_timestamp_t	di_mtime;	/* time last modified */
810 	xfs_timestamp_t	di_ctime;	/* time created/inode modified */
811 	__be64		di_size;	/* number of bytes in file */
812 	__be64		di_nblocks;	/* # of direct & btree blocks used */
813 	__be32		di_extsize;	/* basic/minimum extent size for file */
814 	union {
815 		/*
816 		 * For V2 inodes and V3 inodes without NREXT64 set, this
817 		 * is the number of data and attr fork extents.
818 		 */
819 		struct {
820 			__be32	di_nextents;
821 			__be16	di_anextents;
822 		} __packed;
823 
824 		/* Number of attr fork extents if NREXT64 is set. */
825 		struct {
826 			__be32	di_big_anextents;
827 			__be16	di_nrext64_pad;
828 		} __packed;
829 	} __packed;
830 	__u8		di_forkoff;	/* attr fork offs, <<3 for 64b align */
831 	__s8		di_aformat;	/* format of attr fork's data */
832 	__be32		di_dmevmask;	/* DMIG event mask */
833 	__be16		di_dmstate;	/* DMIG state info */
834 	__be16		di_flags;	/* random flags, XFS_DIFLAG_... */
835 	__be32		di_gen;		/* generation number */
836 
837 	/* di_next_unlinked is the only non-core field in the old dinode */
838 	__be32		di_next_unlinked;/* agi unlinked list ptr */
839 
840 	/* start of the extended dinode, writable fields */
841 	__le32		di_crc;		/* CRC of the inode */
842 	__be64		di_changecount;	/* number of attribute changes */
843 	__be64		di_lsn;		/* flush sequence */
844 	__be64		di_flags2;	/* more random flags */
845 	__be32		di_cowextsize;	/* basic cow extent size for file */
846 	__u8		di_pad2[12];	/* more padding for future expansion */
847 
848 	/* fields only written to during inode creation */
849 	xfs_timestamp_t	di_crtime;	/* time created */
850 	__be64		di_ino;		/* inode number */
851 	uuid_t		di_uuid;	/* UUID of the filesystem */
852 
853 	/* structure must be padded to 64 bit alignment */
854 };
855 
856 #define XFS_DINODE_CRC_OFF	offsetof(struct xfs_dinode, di_crc)
857 
858 #define DI_MAX_FLUSH 0xffff
859 
860 /*
861  * Size of the core inode on disk.  Version 1 and 2 inodes have
862  * the same size, but version 3 has grown a few additional fields.
863  */
864 static inline uint xfs_dinode_size(int version)
865 {
866 	if (version == 3)
867 		return sizeof(struct xfs_dinode);
868 	return offsetof(struct xfs_dinode, di_crc);
869 }
870 
871 /*
872  * The 32 bit link count in the inode theoretically maxes out at UINT_MAX.
873  * Since the pathconf interface is signed, we use 2^31 - 1 instead.
874  */
875 #define	XFS_MAXLINK		((1U << 31) - 1U)
876 
877 /*
878  * Values for di_format
879  *
880  * This enum is used in string mapping in xfs_trace.h; please keep the
881  * TRACE_DEFINE_ENUMs for it up to date.
882  */
883 enum xfs_dinode_fmt {
884 	XFS_DINODE_FMT_DEV,		/* xfs_dev_t */
885 	XFS_DINODE_FMT_LOCAL,		/* bulk data */
886 	XFS_DINODE_FMT_EXTENTS,		/* struct xfs_bmbt_rec */
887 	XFS_DINODE_FMT_BTREE,		/* struct xfs_bmdr_block */
888 	XFS_DINODE_FMT_UUID		/* added long ago, but never used */
889 };
890 
891 #define XFS_INODE_FORMAT_STR \
892 	{ XFS_DINODE_FMT_DEV,		"dev" }, \
893 	{ XFS_DINODE_FMT_LOCAL,		"local" }, \
894 	{ XFS_DINODE_FMT_EXTENTS,	"extent" }, \
895 	{ XFS_DINODE_FMT_BTREE,		"btree" }, \
896 	{ XFS_DINODE_FMT_UUID,		"uuid" }
897 
898 /*
899  * Max values for extnum and aextnum.
900  *
901  * The original on-disk extent counts were held in signed fields, resulting in
902  * maximum extent counts of 2^31 and 2^15 for the data and attr forks
903  * respectively. Similarly the maximum extent length is limited to 2^21 blocks
904  * by the 21-bit wide blockcount field of a BMBT extent record.
905  *
906  * The newly introduced data fork extent counter can hold a 64-bit value,
907  * however the maximum number of extents in a file is also limited to 2^54
908  * extents by the 54-bit wide startoff field of a BMBT extent record.
909  *
910  * It is further limited by the maximum supported file size of 2^63
911  * *bytes*. This leads to a maximum extent count for maximally sized filesystem
912  * blocks (64kB) of:
913  *
914  * 2^63 bytes / 2^16 bytes per block = 2^47 blocks
915  *
916  * Rounding up 47 to the nearest multiple of bits-per-byte results in 48. Hence
917  * 2^48 was chosen as the maximum data fork extent count.
918  *
919  * The maximum file size that can be represented by the data fork extent counter
920  * in the worst case occurs when all extents are 1 block in length and each
921  * block is 1KB in size.
922  *
923  * With XFS_MAX_EXTCNT_DATA_FORK_SMALL representing maximum extent count and
924  * with 1KB sized blocks, a file can reach upto,
925  * 1KB * (2^31) = 2TB
926  *
927  * This is much larger than the theoretical maximum size of a directory
928  * i.e. XFS_DIR2_SPACE_SIZE * XFS_DIR2_MAX_SPACES = ~96GB.
929  *
930  * Hence, a directory inode can never overflow its data fork extent counter.
931  */
932 #define XFS_MAX_EXTCNT_DATA_FORK_LARGE	((xfs_extnum_t)((1ULL << 48) - 1))
933 #define XFS_MAX_EXTCNT_ATTR_FORK_LARGE	((xfs_extnum_t)((1ULL << 32) - 1))
934 #define XFS_MAX_EXTCNT_DATA_FORK_SMALL	((xfs_extnum_t)((1ULL << 31) - 1))
935 #define XFS_MAX_EXTCNT_ATTR_FORK_SMALL	((xfs_extnum_t)((1ULL << 15) - 1))
936 
937 /*
938  * Inode minimum and maximum sizes.
939  */
940 #define	XFS_DINODE_MIN_LOG	8
941 #define	XFS_DINODE_MAX_LOG	11
942 #define	XFS_DINODE_MIN_SIZE	(1 << XFS_DINODE_MIN_LOG)
943 #define	XFS_DINODE_MAX_SIZE	(1 << XFS_DINODE_MAX_LOG)
944 
945 /*
946  * Inode size for given fs.
947  */
948 #define XFS_DINODE_SIZE(mp) \
949 	(xfs_has_v3inodes(mp) ? \
950 		sizeof(struct xfs_dinode) : \
951 		offsetof(struct xfs_dinode, di_crc))
952 #define XFS_LITINO(mp) \
953 	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(mp))
954 
955 /*
956  * Inode data & attribute fork sizes, per inode.
957  */
958 #define XFS_DFORK_BOFF(dip)		((int)((dip)->di_forkoff << 3))
959 
960 #define XFS_DFORK_DSIZE(dip,mp) \
961 	((dip)->di_forkoff ? XFS_DFORK_BOFF(dip) : XFS_LITINO(mp))
962 #define XFS_DFORK_ASIZE(dip,mp) \
963 	((dip)->di_forkoff ? XFS_LITINO(mp) - XFS_DFORK_BOFF(dip) : 0)
964 #define XFS_DFORK_SIZE(dip,mp,w) \
965 	((w) == XFS_DATA_FORK ? \
966 		XFS_DFORK_DSIZE(dip, mp) : \
967 		XFS_DFORK_ASIZE(dip, mp))
968 
969 #define XFS_DFORK_MAXEXT(dip, mp, w) \
970 	(XFS_DFORK_SIZE(dip, mp, w) / sizeof(struct xfs_bmbt_rec))
971 
972 /*
973  * Return pointers to the data or attribute forks.
974  */
975 #define XFS_DFORK_DPTR(dip) \
976 	((char *)dip + xfs_dinode_size(dip->di_version))
977 #define XFS_DFORK_APTR(dip)	\
978 	(XFS_DFORK_DPTR(dip) + XFS_DFORK_BOFF(dip))
979 #define XFS_DFORK_PTR(dip,w)	\
980 	((w) == XFS_DATA_FORK ? XFS_DFORK_DPTR(dip) : XFS_DFORK_APTR(dip))
981 
982 #define XFS_DFORK_FORMAT(dip,w) \
983 	((w) == XFS_DATA_FORK ? \
984 		(dip)->di_format : \
985 		(dip)->di_aformat)
986 
987 /*
988  * For block and character special files the 32bit dev_t is stored at the
989  * beginning of the data fork.
990  */
991 static inline xfs_dev_t xfs_dinode_get_rdev(struct xfs_dinode *dip)
992 {
993 	return be32_to_cpu(*(__be32 *)XFS_DFORK_DPTR(dip));
994 }
995 
996 static inline void xfs_dinode_put_rdev(struct xfs_dinode *dip, xfs_dev_t rdev)
997 {
998 	*(__be32 *)XFS_DFORK_DPTR(dip) = cpu_to_be32(rdev);
999 }
1000 
1001 /*
1002  * Values for di_flags
1003  */
1004 #define XFS_DIFLAG_REALTIME_BIT  0	/* file's blocks come from rt area */
1005 #define XFS_DIFLAG_PREALLOC_BIT  1	/* file space has been preallocated */
1006 #define XFS_DIFLAG_NEWRTBM_BIT   2	/* for rtbitmap inode, new format */
1007 #define XFS_DIFLAG_IMMUTABLE_BIT 3	/* inode is immutable */
1008 #define XFS_DIFLAG_APPEND_BIT    4	/* inode is append-only */
1009 #define XFS_DIFLAG_SYNC_BIT      5	/* inode is written synchronously */
1010 #define XFS_DIFLAG_NOATIME_BIT   6	/* do not update atime */
1011 #define XFS_DIFLAG_NODUMP_BIT    7	/* do not dump */
1012 #define XFS_DIFLAG_RTINHERIT_BIT 8	/* create with realtime bit set */
1013 #define XFS_DIFLAG_PROJINHERIT_BIT   9	/* create with parents projid */
1014 #define XFS_DIFLAG_NOSYMLINKS_BIT   10	/* disallow symlink creation */
1015 #define XFS_DIFLAG_EXTSIZE_BIT      11	/* inode extent size allocator hint */
1016 #define XFS_DIFLAG_EXTSZINHERIT_BIT 12	/* inherit inode extent size */
1017 #define XFS_DIFLAG_NODEFRAG_BIT     13	/* do not reorganize/defragment */
1018 #define XFS_DIFLAG_FILESTREAM_BIT   14  /* use filestream allocator */
1019 /* Do not use bit 15, di_flags is legacy and unchanging now */
1020 
1021 #define XFS_DIFLAG_REALTIME      (1 << XFS_DIFLAG_REALTIME_BIT)
1022 #define XFS_DIFLAG_PREALLOC      (1 << XFS_DIFLAG_PREALLOC_BIT)
1023 #define XFS_DIFLAG_NEWRTBM       (1 << XFS_DIFLAG_NEWRTBM_BIT)
1024 #define XFS_DIFLAG_IMMUTABLE     (1 << XFS_DIFLAG_IMMUTABLE_BIT)
1025 #define XFS_DIFLAG_APPEND        (1 << XFS_DIFLAG_APPEND_BIT)
1026 #define XFS_DIFLAG_SYNC          (1 << XFS_DIFLAG_SYNC_BIT)
1027 #define XFS_DIFLAG_NOATIME       (1 << XFS_DIFLAG_NOATIME_BIT)
1028 #define XFS_DIFLAG_NODUMP        (1 << XFS_DIFLAG_NODUMP_BIT)
1029 #define XFS_DIFLAG_RTINHERIT     (1 << XFS_DIFLAG_RTINHERIT_BIT)
1030 #define XFS_DIFLAG_PROJINHERIT   (1 << XFS_DIFLAG_PROJINHERIT_BIT)
1031 #define XFS_DIFLAG_NOSYMLINKS    (1 << XFS_DIFLAG_NOSYMLINKS_BIT)
1032 #define XFS_DIFLAG_EXTSIZE       (1 << XFS_DIFLAG_EXTSIZE_BIT)
1033 #define XFS_DIFLAG_EXTSZINHERIT  (1 << XFS_DIFLAG_EXTSZINHERIT_BIT)
1034 #define XFS_DIFLAG_NODEFRAG      (1 << XFS_DIFLAG_NODEFRAG_BIT)
1035 #define XFS_DIFLAG_FILESTREAM    (1 << XFS_DIFLAG_FILESTREAM_BIT)
1036 
1037 #define XFS_DIFLAG_ANY \
1038 	(XFS_DIFLAG_REALTIME | XFS_DIFLAG_PREALLOC | XFS_DIFLAG_NEWRTBM | \
1039 	 XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND | XFS_DIFLAG_SYNC | \
1040 	 XFS_DIFLAG_NOATIME | XFS_DIFLAG_NODUMP | XFS_DIFLAG_RTINHERIT | \
1041 	 XFS_DIFLAG_PROJINHERIT | XFS_DIFLAG_NOSYMLINKS | XFS_DIFLAG_EXTSIZE | \
1042 	 XFS_DIFLAG_EXTSZINHERIT | XFS_DIFLAG_NODEFRAG | XFS_DIFLAG_FILESTREAM)
1043 
1044 /*
1045  * Values for di_flags2 These start by being exposed to userspace in the upper
1046  * 16 bits of the XFS_XFLAG_s range.
1047  */
1048 #define XFS_DIFLAG2_DAX_BIT	0	/* use DAX for this inode */
1049 #define XFS_DIFLAG2_REFLINK_BIT	1	/* file's blocks may be shared */
1050 #define XFS_DIFLAG2_COWEXTSIZE_BIT   2  /* copy on write extent size hint */
1051 #define XFS_DIFLAG2_BIGTIME_BIT	3	/* big timestamps */
1052 #define XFS_DIFLAG2_NREXT64_BIT 4	/* large extent counters */
1053 
1054 #define XFS_DIFLAG2_DAX		(1 << XFS_DIFLAG2_DAX_BIT)
1055 #define XFS_DIFLAG2_REFLINK     (1 << XFS_DIFLAG2_REFLINK_BIT)
1056 #define XFS_DIFLAG2_COWEXTSIZE  (1 << XFS_DIFLAG2_COWEXTSIZE_BIT)
1057 #define XFS_DIFLAG2_BIGTIME	(1 << XFS_DIFLAG2_BIGTIME_BIT)
1058 #define XFS_DIFLAG2_NREXT64	(1 << XFS_DIFLAG2_NREXT64_BIT)
1059 
1060 #define XFS_DIFLAG2_ANY \
1061 	(XFS_DIFLAG2_DAX | XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE | \
1062 	 XFS_DIFLAG2_BIGTIME | XFS_DIFLAG2_NREXT64)
1063 
1064 static inline bool xfs_dinode_has_bigtime(const struct xfs_dinode *dip)
1065 {
1066 	return dip->di_version >= 3 &&
1067 	       (dip->di_flags2 & cpu_to_be64(XFS_DIFLAG2_BIGTIME));
1068 }
1069 
1070 static inline bool xfs_dinode_has_large_extent_counts(
1071 	const struct xfs_dinode *dip)
1072 {
1073 	return dip->di_version >= 3 &&
1074 	       (dip->di_flags2 & cpu_to_be64(XFS_DIFLAG2_NREXT64));
1075 }
1076 
1077 /*
1078  * Inode number format:
1079  * low inopblog bits - offset in block
1080  * next agblklog bits - block number in ag
1081  * next agno_log bits - ag number
1082  * high agno_log-agblklog-inopblog bits - 0
1083  */
1084 #define	XFS_INO_MASK(k)			(uint32_t)((1ULL << (k)) - 1)
1085 #define	XFS_INO_OFFSET_BITS(mp)		(mp)->m_sb.sb_inopblog
1086 #define	XFS_INO_AGBNO_BITS(mp)		(mp)->m_sb.sb_agblklog
1087 #define	XFS_INO_AGINO_BITS(mp)		((mp)->m_ino_geo.agino_log)
1088 #define	XFS_INO_AGNO_BITS(mp)		(mp)->m_agno_log
1089 #define	XFS_INO_BITS(mp)		\
1090 	XFS_INO_AGNO_BITS(mp) + XFS_INO_AGINO_BITS(mp)
1091 #define	XFS_INO_TO_AGNO(mp,i)		\
1092 	((xfs_agnumber_t)((i) >> XFS_INO_AGINO_BITS(mp)))
1093 #define	XFS_INO_TO_AGINO(mp,i)		\
1094 	((xfs_agino_t)(i) & XFS_INO_MASK(XFS_INO_AGINO_BITS(mp)))
1095 #define	XFS_INO_TO_AGBNO(mp,i)		\
1096 	(((xfs_agblock_t)(i) >> XFS_INO_OFFSET_BITS(mp)) & \
1097 		XFS_INO_MASK(XFS_INO_AGBNO_BITS(mp)))
1098 #define	XFS_INO_TO_OFFSET(mp,i)		\
1099 	((int)(i) & XFS_INO_MASK(XFS_INO_OFFSET_BITS(mp)))
1100 #define	XFS_INO_TO_FSB(mp,i)		\
1101 	XFS_AGB_TO_FSB(mp, XFS_INO_TO_AGNO(mp,i), XFS_INO_TO_AGBNO(mp,i))
1102 #define	XFS_AGINO_TO_INO(mp,a,i)	\
1103 	(((xfs_ino_t)(a) << XFS_INO_AGINO_BITS(mp)) | (i))
1104 #define	XFS_AGINO_TO_AGBNO(mp,i)	((i) >> XFS_INO_OFFSET_BITS(mp))
1105 #define	XFS_AGINO_TO_OFFSET(mp,i)	\
1106 	((i) & XFS_INO_MASK(XFS_INO_OFFSET_BITS(mp)))
1107 #define	XFS_OFFBNO_TO_AGINO(mp,b,o)	\
1108 	((xfs_agino_t)(((b) << XFS_INO_OFFSET_BITS(mp)) | (o)))
1109 #define	XFS_FSB_TO_INO(mp, b)	((xfs_ino_t)((b) << XFS_INO_OFFSET_BITS(mp)))
1110 #define	XFS_AGB_TO_AGINO(mp, b)	((xfs_agino_t)((b) << XFS_INO_OFFSET_BITS(mp)))
1111 
1112 #define	XFS_MAXINUMBER		((xfs_ino_t)((1ULL << 56) - 1ULL))
1113 #define	XFS_MAXINUMBER_32	((xfs_ino_t)((1ULL << 32) - 1ULL))
1114 
1115 /*
1116  * RealTime Device format definitions
1117  */
1118 
1119 /* Min and max rt extent sizes, specified in bytes */
1120 #define	XFS_MAX_RTEXTSIZE	(1024 * 1024 * 1024)	/* 1GB */
1121 #define	XFS_DFL_RTEXTSIZE	(64 * 1024)	        /* 64kB */
1122 #define	XFS_MIN_RTEXTSIZE	(4 * 1024)		/* 4kB */
1123 
1124 #define	XFS_BLOCKSIZE(mp)	((mp)->m_sb.sb_blocksize)
1125 #define	XFS_BLOCKMASK(mp)	((mp)->m_blockmask)
1126 #define	XFS_BLOCKWSIZE(mp)	((mp)->m_blockwsize)
1127 #define	XFS_BLOCKWMASK(mp)	((mp)->m_blockwmask)
1128 
1129 /*
1130  * RT Summary and bit manipulation macros.
1131  */
1132 #define	XFS_SUMOFFS(mp,ls,bb)	((int)((ls) * (mp)->m_sb.sb_rbmblocks + (bb)))
1133 #define	XFS_SUMOFFSTOBLOCK(mp,s)	\
1134 	(((s) * (uint)sizeof(xfs_suminfo_t)) >> (mp)->m_sb.sb_blocklog)
1135 #define	XFS_SUMPTR(mp,bp,so)	\
1136 	((xfs_suminfo_t *)((bp)->b_addr + \
1137 		(((so) * (uint)sizeof(xfs_suminfo_t)) & XFS_BLOCKMASK(mp))))
1138 
1139 #define	XFS_BITTOBLOCK(mp,bi)	((bi) >> (mp)->m_blkbit_log)
1140 #define	XFS_BLOCKTOBIT(mp,bb)	((bb) << (mp)->m_blkbit_log)
1141 #define	XFS_BITTOWORD(mp,bi)	\
1142 	((int)(((bi) >> XFS_NBWORDLOG) & XFS_BLOCKWMASK(mp)))
1143 
1144 #define	XFS_RTMIN(a,b)	((a) < (b) ? (a) : (b))
1145 #define	XFS_RTMAX(a,b)	((a) > (b) ? (a) : (b))
1146 
1147 #define	XFS_RTLOBIT(w)	xfs_lowbit32(w)
1148 #define	XFS_RTHIBIT(w)	xfs_highbit32(w)
1149 
1150 #define	XFS_RTBLOCKLOG(b)	xfs_highbit64(b)
1151 
1152 /*
1153  * Dquot and dquot block format definitions
1154  */
1155 #define XFS_DQUOT_MAGIC		0x4451		/* 'DQ' */
1156 #define XFS_DQUOT_VERSION	(uint8_t)0x01	/* latest version number */
1157 
1158 #define XFS_DQTYPE_USER		0x01		/* user dquot record */
1159 #define XFS_DQTYPE_PROJ		0x02		/* project dquot record */
1160 #define XFS_DQTYPE_GROUP	0x04		/* group dquot record */
1161 #define XFS_DQTYPE_BIGTIME	0x80		/* large expiry timestamps */
1162 
1163 /* bitmask to determine if this is a user/group/project dquot */
1164 #define XFS_DQTYPE_REC_MASK	(XFS_DQTYPE_USER | \
1165 				 XFS_DQTYPE_PROJ | \
1166 				 XFS_DQTYPE_GROUP)
1167 
1168 #define XFS_DQTYPE_ANY		(XFS_DQTYPE_REC_MASK | \
1169 				 XFS_DQTYPE_BIGTIME)
1170 
1171 /*
1172  * XFS Quota Timers
1173  * ================
1174  *
1175  * Traditional quota grace period expiration timers are an unsigned 32-bit
1176  * seconds counter; time zero is the Unix epoch, Jan  1 00:00:01 UTC 1970.
1177  * Note that an expiration value of zero means that the quota limit has not
1178  * been reached, and therefore no expiration has been set.  Therefore, the
1179  * ondisk min and max defined here can be used directly to constrain the incore
1180  * quota expiration timestamps on a Unix system.
1181  *
1182  * When bigtime is enabled, we trade two bits of precision to expand the
1183  * expiration timeout range to match that of big inode timestamps.  The min and
1184  * max recorded here are the on-disk limits, not a Unix timestamp.
1185  *
1186  * The grace period for each quota type is stored in the root dquot (id = 0)
1187  * and is applied to a non-root dquot when it exceeds the soft or hard limits.
1188  * The length of quota grace periods are unsigned 32-bit quantities measured in
1189  * units of seconds.  A value of zero means to use the default period.
1190  */
1191 
1192 /*
1193  * Smallest possible ondisk quota expiration value with traditional timestamps.
1194  * This corresponds exactly with the incore expiration Jan  1 00:00:01 UTC 1970.
1195  */
1196 #define XFS_DQ_LEGACY_EXPIRY_MIN	((int64_t)1)
1197 
1198 /*
1199  * Largest possible ondisk quota expiration value with traditional timestamps.
1200  * This corresponds exactly with the incore expiration Feb  7 06:28:15 UTC 2106.
1201  */
1202 #define XFS_DQ_LEGACY_EXPIRY_MAX	((int64_t)U32_MAX)
1203 
1204 /*
1205  * Smallest possible ondisk quota expiration value with bigtime timestamps.
1206  * This corresponds (after conversion to a Unix timestamp) with the incore
1207  * expiration of Jan  1 00:00:04 UTC 1970.
1208  */
1209 #define XFS_DQ_BIGTIME_EXPIRY_MIN	(XFS_DQ_LEGACY_EXPIRY_MIN)
1210 
1211 /*
1212  * Largest supported ondisk quota expiration value with bigtime timestamps.
1213  * This corresponds (after conversion to a Unix timestamp) with an incore
1214  * expiration of Jul  2 20:20:24 UTC 2486.
1215  *
1216  * The ondisk field supports values up to -1U, which corresponds to an incore
1217  * expiration in 2514.  This is beyond the maximum the bigtime inode timestamp,
1218  * so we cap the maximum bigtime quota expiration to the max inode timestamp.
1219  */
1220 #define XFS_DQ_BIGTIME_EXPIRY_MAX	((int64_t)4074815106U)
1221 
1222 /*
1223  * The following conversion factors assist in converting a quota expiration
1224  * timestamp between the incore and ondisk formats.
1225  */
1226 #define XFS_DQ_BIGTIME_SHIFT	(2)
1227 #define XFS_DQ_BIGTIME_SLACK	((int64_t)(1ULL << XFS_DQ_BIGTIME_SHIFT) - 1)
1228 
1229 /* Convert an incore quota expiration timestamp to an ondisk bigtime value. */
1230 static inline uint32_t xfs_dq_unix_to_bigtime(time64_t unix_seconds)
1231 {
1232 	/*
1233 	 * Round the expiration timestamp up to the nearest bigtime timestamp
1234 	 * that we can store, to give users the most time to fix problems.
1235 	 */
1236 	return ((uint64_t)unix_seconds + XFS_DQ_BIGTIME_SLACK) >>
1237 			XFS_DQ_BIGTIME_SHIFT;
1238 }
1239 
1240 /* Convert an ondisk bigtime quota expiration value to an incore timestamp. */
1241 static inline time64_t xfs_dq_bigtime_to_unix(uint32_t ondisk_seconds)
1242 {
1243 	return (time64_t)ondisk_seconds << XFS_DQ_BIGTIME_SHIFT;
1244 }
1245 
1246 /*
1247  * Default quota grace periods, ranging from zero (use the compiled defaults)
1248  * to ~136 years.  These are applied to a non-root dquot that has exceeded
1249  * either limit.
1250  */
1251 #define XFS_DQ_GRACE_MIN		((int64_t)0)
1252 #define XFS_DQ_GRACE_MAX		((int64_t)U32_MAX)
1253 
1254 /*
1255  * This is the main portion of the on-disk representation of quota information
1256  * for a user.  We pad this with some more expansion room to construct the on
1257  * disk structure.
1258  */
1259 struct xfs_disk_dquot {
1260 	__be16		d_magic;	/* dquot magic = XFS_DQUOT_MAGIC */
1261 	__u8		d_version;	/* dquot version */
1262 	__u8		d_type;		/* XFS_DQTYPE_USER/PROJ/GROUP */
1263 	__be32		d_id;		/* user,project,group id */
1264 	__be64		d_blk_hardlimit;/* absolute limit on disk blks */
1265 	__be64		d_blk_softlimit;/* preferred limit on disk blks */
1266 	__be64		d_ino_hardlimit;/* maximum # allocated inodes */
1267 	__be64		d_ino_softlimit;/* preferred inode limit */
1268 	__be64		d_bcount;	/* disk blocks owned by the user */
1269 	__be64		d_icount;	/* inodes owned by the user */
1270 	__be32		d_itimer;	/* zero if within inode limits if not,
1271 					   this is when we refuse service */
1272 	__be32		d_btimer;	/* similar to above; for disk blocks */
1273 	__be16		d_iwarns;	/* warnings issued wrt num inodes */
1274 	__be16		d_bwarns;	/* warnings issued wrt disk blocks */
1275 	__be32		d_pad0;		/* 64 bit align */
1276 	__be64		d_rtb_hardlimit;/* absolute limit on realtime blks */
1277 	__be64		d_rtb_softlimit;/* preferred limit on RT disk blks */
1278 	__be64		d_rtbcount;	/* realtime blocks owned */
1279 	__be32		d_rtbtimer;	/* similar to above; for RT disk blocks */
1280 	__be16		d_rtbwarns;	/* warnings issued wrt RT disk blocks */
1281 	__be16		d_pad;
1282 };
1283 
1284 /*
1285  * This is what goes on disk. This is separated from the xfs_disk_dquot because
1286  * carrying the unnecessary padding would be a waste of memory.
1287  */
1288 struct xfs_dqblk {
1289 	struct xfs_disk_dquot	dd_diskdq; /* portion living incore as well */
1290 	char			dd_fill[4];/* filling for posterity */
1291 
1292 	/*
1293 	 * These two are only present on filesystems with the CRC bits set.
1294 	 */
1295 	__be32		  dd_crc;	/* checksum */
1296 	__be64		  dd_lsn;	/* last modification in log */
1297 	uuid_t		  dd_uuid;	/* location information */
1298 };
1299 
1300 #define XFS_DQUOT_CRC_OFF	offsetof(struct xfs_dqblk, dd_crc)
1301 
1302 /*
1303  * This defines the unit of allocation of dquots.
1304  *
1305  * Currently, it is just one file system block, and a 4K blk contains 30
1306  * (136 * 30 = 4080) dquots. It's probably not worth trying to make
1307  * this more dynamic.
1308  *
1309  * However, if this number is changed, we have to make sure that we don't
1310  * implicitly assume that we do allocations in chunks of a single filesystem
1311  * block in the dquot/xqm code.
1312  *
1313  * This is part of the ondisk format because the structure size is not a power
1314  * of two, which leaves slack at the end of the disk block.
1315  */
1316 #define XFS_DQUOT_CLUSTER_SIZE_FSB	(xfs_filblks_t)1
1317 
1318 /*
1319  * Remote symlink format and access functions.
1320  */
1321 #define XFS_SYMLINK_MAGIC	0x58534c4d	/* XSLM */
1322 
1323 struct xfs_dsymlink_hdr {
1324 	__be32	sl_magic;
1325 	__be32	sl_offset;
1326 	__be32	sl_bytes;
1327 	__be32	sl_crc;
1328 	uuid_t	sl_uuid;
1329 	__be64	sl_owner;
1330 	__be64	sl_blkno;
1331 	__be64	sl_lsn;
1332 };
1333 
1334 #define XFS_SYMLINK_CRC_OFF	offsetof(struct xfs_dsymlink_hdr, sl_crc)
1335 
1336 #define XFS_SYMLINK_MAXLEN	1024
1337 /*
1338  * The maximum pathlen is 1024 bytes. Since the minimum file system
1339  * blocksize is 512 bytes, we can get a max of 3 extents back from
1340  * bmapi when crc headers are taken into account.
1341  */
1342 #define XFS_SYMLINK_MAPS 3
1343 
1344 #define XFS_SYMLINK_BUF_SPACE(mp, bufsize)	\
1345 	((bufsize) - (xfs_has_crc((mp)) ? \
1346 			sizeof(struct xfs_dsymlink_hdr) : 0))
1347 
1348 
1349 /*
1350  * Allocation Btree format definitions
1351  *
1352  * There are two on-disk btrees, one sorted by blockno and one sorted
1353  * by blockcount and blockno.  All blocks look the same to make the code
1354  * simpler; if we have time later, we'll make the optimizations.
1355  */
1356 #define	XFS_ABTB_MAGIC		0x41425442	/* 'ABTB' for bno tree */
1357 #define	XFS_ABTB_CRC_MAGIC	0x41423342	/* 'AB3B' */
1358 #define	XFS_ABTC_MAGIC		0x41425443	/* 'ABTC' for cnt tree */
1359 #define	XFS_ABTC_CRC_MAGIC	0x41423343	/* 'AB3C' */
1360 
1361 /*
1362  * Data record/key structure
1363  */
1364 typedef struct xfs_alloc_rec {
1365 	__be32		ar_startblock;	/* starting block number */
1366 	__be32		ar_blockcount;	/* count of free blocks */
1367 } xfs_alloc_rec_t, xfs_alloc_key_t;
1368 
1369 typedef struct xfs_alloc_rec_incore {
1370 	xfs_agblock_t	ar_startblock;	/* starting block number */
1371 	xfs_extlen_t	ar_blockcount;	/* count of free blocks */
1372 } xfs_alloc_rec_incore_t;
1373 
1374 /* btree pointer type */
1375 typedef __be32 xfs_alloc_ptr_t;
1376 
1377 /*
1378  * Block numbers in the AG:
1379  * SB is sector 0, AGF is sector 1, AGI is sector 2, AGFL is sector 3.
1380  */
1381 #define	XFS_BNO_BLOCK(mp)	((xfs_agblock_t)(XFS_AGFL_BLOCK(mp) + 1))
1382 #define	XFS_CNT_BLOCK(mp)	((xfs_agblock_t)(XFS_BNO_BLOCK(mp) + 1))
1383 
1384 
1385 /*
1386  * Inode Allocation Btree format definitions
1387  *
1388  * There is a btree for the inode map per allocation group.
1389  */
1390 #define	XFS_IBT_MAGIC		0x49414254	/* 'IABT' */
1391 #define	XFS_IBT_CRC_MAGIC	0x49414233	/* 'IAB3' */
1392 #define	XFS_FIBT_MAGIC		0x46494254	/* 'FIBT' */
1393 #define	XFS_FIBT_CRC_MAGIC	0x46494233	/* 'FIB3' */
1394 
1395 typedef uint64_t	xfs_inofree_t;
1396 #define	XFS_INODES_PER_CHUNK		(NBBY * sizeof(xfs_inofree_t))
1397 #define	XFS_INODES_PER_CHUNK_LOG	(XFS_NBBYLOG + 3)
1398 #define	XFS_INOBT_ALL_FREE		((xfs_inofree_t)-1)
1399 #define	XFS_INOBT_MASK(i)		((xfs_inofree_t)1 << (i))
1400 
1401 #define XFS_INOBT_HOLEMASK_FULL		0	/* holemask for full chunk */
1402 #define XFS_INOBT_HOLEMASK_BITS		(NBBY * sizeof(uint16_t))
1403 #define XFS_INODES_PER_HOLEMASK_BIT	\
1404 	(XFS_INODES_PER_CHUNK / (NBBY * sizeof(uint16_t)))
1405 
1406 static inline xfs_inofree_t xfs_inobt_maskn(int i, int n)
1407 {
1408 	return ((n >= XFS_INODES_PER_CHUNK ? 0 : XFS_INOBT_MASK(n)) - 1) << i;
1409 }
1410 
1411 /*
1412  * The on-disk inode record structure has two formats. The original "full"
1413  * format uses a 4-byte freecount. The "sparse" format uses a 1-byte freecount
1414  * and replaces the 3 high-order freecount bytes wth the holemask and inode
1415  * count.
1416  *
1417  * The holemask of the sparse record format allows an inode chunk to have holes
1418  * that refer to blocks not owned by the inode record. This facilitates inode
1419  * allocation in the event of severe free space fragmentation.
1420  */
1421 typedef struct xfs_inobt_rec {
1422 	__be32		ir_startino;	/* starting inode number */
1423 	union {
1424 		struct {
1425 			__be32	ir_freecount;	/* count of free inodes */
1426 		} f;
1427 		struct {
1428 			__be16	ir_holemask;/* hole mask for sparse chunks */
1429 			__u8	ir_count;	/* total inode count */
1430 			__u8	ir_freecount;	/* count of free inodes */
1431 		} sp;
1432 	} ir_u;
1433 	__be64		ir_free;	/* free inode mask */
1434 } xfs_inobt_rec_t;
1435 
1436 typedef struct xfs_inobt_rec_incore {
1437 	xfs_agino_t	ir_startino;	/* starting inode number */
1438 	uint16_t	ir_holemask;	/* hole mask for sparse chunks */
1439 	uint8_t		ir_count;	/* total inode count */
1440 	uint8_t		ir_freecount;	/* count of free inodes (set bits) */
1441 	xfs_inofree_t	ir_free;	/* free inode mask */
1442 } xfs_inobt_rec_incore_t;
1443 
1444 static inline bool xfs_inobt_issparse(uint16_t holemask)
1445 {
1446 	/* non-zero holemask represents a sparse rec. */
1447 	return holemask;
1448 }
1449 
1450 /*
1451  * Key structure
1452  */
1453 typedef struct xfs_inobt_key {
1454 	__be32		ir_startino;	/* starting inode number */
1455 } xfs_inobt_key_t;
1456 
1457 /* btree pointer type */
1458 typedef __be32 xfs_inobt_ptr_t;
1459 
1460 /*
1461  * block numbers in the AG.
1462  */
1463 #define	XFS_IBT_BLOCK(mp)		((xfs_agblock_t)(XFS_CNT_BLOCK(mp) + 1))
1464 #define	XFS_FIBT_BLOCK(mp)		((xfs_agblock_t)(XFS_IBT_BLOCK(mp) + 1))
1465 
1466 /*
1467  * Reverse mapping btree format definitions
1468  *
1469  * There is a btree for the reverse map per allocation group
1470  */
1471 #define	XFS_RMAP_CRC_MAGIC	0x524d4233	/* 'RMB3' */
1472 
1473 /*
1474  * Ownership info for an extent.  This is used to create reverse-mapping
1475  * entries.
1476  */
1477 #define XFS_OWNER_INFO_ATTR_FORK	(1 << 0)
1478 #define XFS_OWNER_INFO_BMBT_BLOCK	(1 << 1)
1479 struct xfs_owner_info {
1480 	uint64_t		oi_owner;
1481 	xfs_fileoff_t		oi_offset;
1482 	unsigned int		oi_flags;
1483 };
1484 
1485 /*
1486  * Special owner types.
1487  *
1488  * Seeing as we only support up to 8EB, we have the upper bit of the owner field
1489  * to tell us we have a special owner value. We use these for static metadata
1490  * allocated at mkfs/growfs time, as well as for freespace management metadata.
1491  */
1492 #define XFS_RMAP_OWN_NULL	(-1ULL)	/* No owner, for growfs */
1493 #define XFS_RMAP_OWN_UNKNOWN	(-2ULL)	/* Unknown owner, for EFI recovery */
1494 #define XFS_RMAP_OWN_FS		(-3ULL)	/* static fs metadata */
1495 #define XFS_RMAP_OWN_LOG	(-4ULL)	/* static fs metadata */
1496 #define XFS_RMAP_OWN_AG		(-5ULL)	/* AG freespace btree blocks */
1497 #define XFS_RMAP_OWN_INOBT	(-6ULL)	/* Inode btree blocks */
1498 #define XFS_RMAP_OWN_INODES	(-7ULL)	/* Inode chunk */
1499 #define XFS_RMAP_OWN_REFC	(-8ULL) /* refcount tree */
1500 #define XFS_RMAP_OWN_COW	(-9ULL) /* cow allocations */
1501 #define XFS_RMAP_OWN_MIN	(-10ULL) /* guard */
1502 
1503 #define XFS_RMAP_NON_INODE_OWNER(owner)	(!!((owner) & (1ULL << 63)))
1504 
1505 /*
1506  * Data record structure
1507  */
1508 struct xfs_rmap_rec {
1509 	__be32		rm_startblock;	/* extent start block */
1510 	__be32		rm_blockcount;	/* extent length */
1511 	__be64		rm_owner;	/* extent owner */
1512 	__be64		rm_offset;	/* offset within the owner */
1513 };
1514 
1515 /*
1516  * rmap btree record
1517  *  rm_offset:63 is the attribute fork flag
1518  *  rm_offset:62 is the bmbt block flag
1519  *  rm_offset:61 is the unwritten extent flag (same as l0:63 in bmbt)
1520  *  rm_offset:54-60 aren't used and should be zero
1521  *  rm_offset:0-53 is the block offset within the inode
1522  */
1523 #define XFS_RMAP_OFF_ATTR_FORK	((uint64_t)1ULL << 63)
1524 #define XFS_RMAP_OFF_BMBT_BLOCK	((uint64_t)1ULL << 62)
1525 #define XFS_RMAP_OFF_UNWRITTEN	((uint64_t)1ULL << 61)
1526 
1527 #define XFS_RMAP_LEN_MAX	((uint32_t)~0U)
1528 #define XFS_RMAP_OFF_FLAGS	(XFS_RMAP_OFF_ATTR_FORK | \
1529 				 XFS_RMAP_OFF_BMBT_BLOCK | \
1530 				 XFS_RMAP_OFF_UNWRITTEN)
1531 #define XFS_RMAP_OFF_MASK	((uint64_t)0x3FFFFFFFFFFFFFULL)
1532 
1533 #define XFS_RMAP_OFF(off)		((off) & XFS_RMAP_OFF_MASK)
1534 
1535 #define XFS_RMAP_IS_BMBT_BLOCK(off)	(!!((off) & XFS_RMAP_OFF_BMBT_BLOCK))
1536 #define XFS_RMAP_IS_ATTR_FORK(off)	(!!((off) & XFS_RMAP_OFF_ATTR_FORK))
1537 #define XFS_RMAP_IS_UNWRITTEN(len)	(!!((off) & XFS_RMAP_OFF_UNWRITTEN))
1538 
1539 #define RMAPBT_STARTBLOCK_BITLEN	32
1540 #define RMAPBT_BLOCKCOUNT_BITLEN	32
1541 #define RMAPBT_OWNER_BITLEN		64
1542 #define RMAPBT_ATTRFLAG_BITLEN		1
1543 #define RMAPBT_BMBTFLAG_BITLEN		1
1544 #define RMAPBT_EXNTFLAG_BITLEN		1
1545 #define RMAPBT_UNUSED_OFFSET_BITLEN	7
1546 #define RMAPBT_OFFSET_BITLEN		54
1547 
1548 #define XFS_RMAP_ATTR_FORK		(1 << 0)
1549 #define XFS_RMAP_BMBT_BLOCK		(1 << 1)
1550 #define XFS_RMAP_UNWRITTEN		(1 << 2)
1551 #define XFS_RMAP_KEY_FLAGS		(XFS_RMAP_ATTR_FORK | \
1552 					 XFS_RMAP_BMBT_BLOCK)
1553 #define XFS_RMAP_REC_FLAGS		(XFS_RMAP_UNWRITTEN)
1554 struct xfs_rmap_irec {
1555 	xfs_agblock_t	rm_startblock;	/* extent start block */
1556 	xfs_extlen_t	rm_blockcount;	/* extent length */
1557 	uint64_t	rm_owner;	/* extent owner */
1558 	uint64_t	rm_offset;	/* offset within the owner */
1559 	unsigned int	rm_flags;	/* state flags */
1560 };
1561 
1562 /*
1563  * Key structure
1564  *
1565  * We don't use the length for lookups
1566  */
1567 struct xfs_rmap_key {
1568 	__be32		rm_startblock;	/* extent start block */
1569 	__be64		rm_owner;	/* extent owner */
1570 	__be64		rm_offset;	/* offset within the owner */
1571 } __attribute__((packed));
1572 
1573 /* btree pointer type */
1574 typedef __be32 xfs_rmap_ptr_t;
1575 
1576 #define	XFS_RMAP_BLOCK(mp) \
1577 	(xfs_has_finobt(((mp))) ? \
1578 	 XFS_FIBT_BLOCK(mp) + 1 : \
1579 	 XFS_IBT_BLOCK(mp) + 1)
1580 
1581 /*
1582  * Reference Count Btree format definitions
1583  *
1584  */
1585 #define	XFS_REFC_CRC_MAGIC	0x52334643	/* 'R3FC' */
1586 
1587 unsigned int xfs_refc_block(struct xfs_mount *mp);
1588 
1589 /*
1590  * Data record/key structure
1591  *
1592  * Each record associates a range of physical blocks (starting at
1593  * rc_startblock and ending rc_blockcount blocks later) with a reference
1594  * count (rc_refcount).  Extents that are being used to stage a copy on
1595  * write (CoW) operation are recorded in the refcount btree with a
1596  * refcount of 1.  All other records must have a refcount > 1 and must
1597  * track an extent mapped only by file data forks.
1598  *
1599  * Extents with a single owner (attributes, metadata, non-shared file
1600  * data) are not tracked here.  Free space is also not tracked here.
1601  * This is consistent with pre-reflink XFS.
1602  */
1603 
1604 /*
1605  * Extents that are being used to stage a copy on write are stored
1606  * in the refcount btree with a refcount of 1 and the upper bit set
1607  * on the startblock.  This speeds up mount time deletion of stale
1608  * staging extents because they're all at the right side of the tree.
1609  */
1610 #define XFS_REFC_COW_START		((xfs_agblock_t)(1U << 31))
1611 #define REFCNTBT_COWFLAG_BITLEN		1
1612 #define REFCNTBT_AGBLOCK_BITLEN		31
1613 
1614 struct xfs_refcount_rec {
1615 	__be32		rc_startblock;	/* starting block number */
1616 	__be32		rc_blockcount;	/* count of blocks */
1617 	__be32		rc_refcount;	/* number of inodes linked here */
1618 };
1619 
1620 struct xfs_refcount_key {
1621 	__be32		rc_startblock;	/* starting block number */
1622 };
1623 
1624 struct xfs_refcount_irec {
1625 	xfs_agblock_t	rc_startblock;	/* starting block number */
1626 	xfs_extlen_t	rc_blockcount;	/* count of free blocks */
1627 	xfs_nlink_t	rc_refcount;	/* number of inodes linked here */
1628 };
1629 
1630 #define MAXREFCOUNT	((xfs_nlink_t)~0U)
1631 #define MAXREFCEXTLEN	((xfs_extlen_t)~0U)
1632 
1633 /* btree pointer type */
1634 typedef __be32 xfs_refcount_ptr_t;
1635 
1636 
1637 /*
1638  * BMAP Btree format definitions
1639  *
1640  * This includes both the root block definition that sits inside an inode fork
1641  * and the record/pointer formats for the leaf/node in the blocks.
1642  */
1643 #define XFS_BMAP_MAGIC		0x424d4150	/* 'BMAP' */
1644 #define XFS_BMAP_CRC_MAGIC	0x424d4133	/* 'BMA3' */
1645 
1646 /*
1647  * Bmap root header, on-disk form only.
1648  */
1649 typedef struct xfs_bmdr_block {
1650 	__be16		bb_level;	/* 0 is a leaf */
1651 	__be16		bb_numrecs;	/* current # of data records */
1652 } xfs_bmdr_block_t;
1653 
1654 /*
1655  * Bmap btree record and extent descriptor.
1656  *  l0:63 is an extent flag (value 1 indicates non-normal).
1657  *  l0:9-62 are startoff.
1658  *  l0:0-8 and l1:21-63 are startblock.
1659  *  l1:0-20 are blockcount.
1660  */
1661 #define BMBT_EXNTFLAG_BITLEN	1
1662 #define BMBT_STARTOFF_BITLEN	54
1663 #define BMBT_STARTBLOCK_BITLEN	52
1664 #define BMBT_BLOCKCOUNT_BITLEN	21
1665 
1666 #define BMBT_STARTOFF_MASK	((1ULL << BMBT_STARTOFF_BITLEN) - 1)
1667 #define BMBT_BLOCKCOUNT_MASK	((1ULL << BMBT_BLOCKCOUNT_BITLEN) - 1)
1668 
1669 #define XFS_MAX_BMBT_EXTLEN	((xfs_extlen_t)(BMBT_BLOCKCOUNT_MASK))
1670 
1671 /*
1672  * bmbt records have a file offset (block) field that is 54 bits wide, so this
1673  * is the largest xfs_fileoff_t that we ever expect to see.
1674  */
1675 #define XFS_MAX_FILEOFF		(BMBT_STARTOFF_MASK + BMBT_BLOCKCOUNT_MASK)
1676 
1677 typedef struct xfs_bmbt_rec {
1678 	__be64			l0, l1;
1679 } xfs_bmbt_rec_t;
1680 
1681 typedef uint64_t	xfs_bmbt_rec_base_t;	/* use this for casts */
1682 typedef xfs_bmbt_rec_t xfs_bmdr_rec_t;
1683 
1684 /*
1685  * Values and macros for delayed-allocation startblock fields.
1686  */
1687 #define STARTBLOCKVALBITS	17
1688 #define STARTBLOCKMASKBITS	(15 + 20)
1689 #define STARTBLOCKMASK		\
1690 	(((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
1691 
1692 static inline int isnullstartblock(xfs_fsblock_t x)
1693 {
1694 	return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
1695 }
1696 
1697 static inline xfs_fsblock_t nullstartblock(int k)
1698 {
1699 	ASSERT(k < (1 << STARTBLOCKVALBITS));
1700 	return STARTBLOCKMASK | (k);
1701 }
1702 
1703 static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
1704 {
1705 	return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
1706 }
1707 
1708 /*
1709  * Key structure for non-leaf levels of the tree.
1710  */
1711 typedef struct xfs_bmbt_key {
1712 	__be64		br_startoff;	/* starting file offset */
1713 } xfs_bmbt_key_t, xfs_bmdr_key_t;
1714 
1715 /* btree pointer type */
1716 typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
1717 
1718 
1719 /*
1720  * Generic Btree block format definitions
1721  *
1722  * This is a combination of the actual format used on disk for short and long
1723  * format btrees.  The first three fields are shared by both format, but the
1724  * pointers are different and should be used with care.
1725  *
1726  * To get the size of the actual short or long form headers please use the size
1727  * macros below.  Never use sizeof(xfs_btree_block).
1728  *
1729  * The blkno, crc, lsn, owner and uuid fields are only available in filesystems
1730  * with the crc feature bit, and all accesses to them must be conditional on
1731  * that flag.
1732  */
1733 /* short form block header */
1734 struct xfs_btree_block_shdr {
1735 	__be32		bb_leftsib;
1736 	__be32		bb_rightsib;
1737 
1738 	__be64		bb_blkno;
1739 	__be64		bb_lsn;
1740 	uuid_t		bb_uuid;
1741 	__be32		bb_owner;
1742 	__le32		bb_crc;
1743 };
1744 
1745 /* long form block header */
1746 struct xfs_btree_block_lhdr {
1747 	__be64		bb_leftsib;
1748 	__be64		bb_rightsib;
1749 
1750 	__be64		bb_blkno;
1751 	__be64		bb_lsn;
1752 	uuid_t		bb_uuid;
1753 	__be64		bb_owner;
1754 	__le32		bb_crc;
1755 	__be32		bb_pad; /* padding for alignment */
1756 };
1757 
1758 struct xfs_btree_block {
1759 	__be32		bb_magic;	/* magic number for block type */
1760 	__be16		bb_level;	/* 0 is a leaf */
1761 	__be16		bb_numrecs;	/* current # of data records */
1762 	union {
1763 		struct xfs_btree_block_shdr s;
1764 		struct xfs_btree_block_lhdr l;
1765 	} bb_u;				/* rest */
1766 };
1767 
1768 /* size of a short form block */
1769 #define XFS_BTREE_SBLOCK_LEN \
1770 	(offsetof(struct xfs_btree_block, bb_u) + \
1771 	 offsetof(struct xfs_btree_block_shdr, bb_blkno))
1772 /* size of a long form block */
1773 #define XFS_BTREE_LBLOCK_LEN \
1774 	(offsetof(struct xfs_btree_block, bb_u) + \
1775 	 offsetof(struct xfs_btree_block_lhdr, bb_blkno))
1776 
1777 /* sizes of CRC enabled btree blocks */
1778 #define XFS_BTREE_SBLOCK_CRC_LEN \
1779 	(offsetof(struct xfs_btree_block, bb_u) + \
1780 	 sizeof(struct xfs_btree_block_shdr))
1781 #define XFS_BTREE_LBLOCK_CRC_LEN \
1782 	(offsetof(struct xfs_btree_block, bb_u) + \
1783 	 sizeof(struct xfs_btree_block_lhdr))
1784 
1785 #define XFS_BTREE_SBLOCK_CRC_OFF \
1786 	offsetof(struct xfs_btree_block, bb_u.s.bb_crc)
1787 #define XFS_BTREE_LBLOCK_CRC_OFF \
1788 	offsetof(struct xfs_btree_block, bb_u.l.bb_crc)
1789 
1790 /*
1791  * On-disk XFS access control list structure.
1792  */
1793 struct xfs_acl_entry {
1794 	__be32	ae_tag;
1795 	__be32	ae_id;
1796 	__be16	ae_perm;
1797 	__be16	ae_pad;		/* fill the implicit hole in the structure */
1798 };
1799 
1800 struct xfs_acl {
1801 	__be32			acl_cnt;
1802 	struct xfs_acl_entry	acl_entry[];
1803 };
1804 
1805 /*
1806  * The number of ACL entries allowed is defined by the on-disk format.
1807  * For v4 superblocks, that is limited to 25 entries. For v5 superblocks, it is
1808  * limited only by the maximum size of the xattr that stores the information.
1809  */
1810 #define XFS_ACL_MAX_ENTRIES(mp)	\
1811 	(xfs_has_crc(mp) \
1812 		?  (XFS_XATTR_SIZE_MAX - sizeof(struct xfs_acl)) / \
1813 						sizeof(struct xfs_acl_entry) \
1814 		: 25)
1815 
1816 #define XFS_ACL_SIZE(cnt) \
1817 	(sizeof(struct xfs_acl) + \
1818 		sizeof(struct xfs_acl_entry) * cnt)
1819 
1820 #define XFS_ACL_MAX_SIZE(mp) \
1821 	XFS_ACL_SIZE(XFS_ACL_MAX_ENTRIES((mp)))
1822 
1823 
1824 /* On-disk XFS extended attribute names */
1825 #define SGI_ACL_FILE		"SGI_ACL_FILE"
1826 #define SGI_ACL_DEFAULT		"SGI_ACL_DEFAULT"
1827 #define SGI_ACL_FILE_SIZE	(sizeof(SGI_ACL_FILE)-1)
1828 #define SGI_ACL_DEFAULT_SIZE	(sizeof(SGI_ACL_DEFAULT)-1)
1829 
1830 #endif /* __XFS_FORMAT_H__ */
1831