xref: /titanic_50/usr/src/uts/common/sys/fs/ufs_fs.h (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #ifndef	_SYS_FS_UFS_FS_H
41 #define	_SYS_FS_UFS_FS_H
42 
43 #pragma ident	"%Z%%M%	%I%	%E% SMI"
44 
45 #include <sys/isa_defs.h>
46 #include <sys/types32.h>
47 #include <sys/t_lock.h>		/* for kmutex_t */
48 
49 #ifdef	__cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  * The following values are minor release values for UFS.
55  * The fs_version field in the superblock will equal one of them
56  * if the file system's magic number is MTB_UFS_MAGIC.
57  */
58 
59 #define		MTB_UFS_VERSION_MIN	1
60 #define		MTB_UFS_VERSION_1	1
61 
62 /*
63  * Each disk drive contains some number of file systems.
64  * A file system consists of a number of cylinder groups.
65  * Each cylinder group has inodes and data.
66  *
67  * A file system is described by its super-block, which in turn
68  * describes the cylinder groups.  The super-block is critical
69  * data and is replicated in the first 10 cylinder groups and the
70  * the last 10 cylinder groups to protect against
71  * catastrophic loss.  This is done at mkfs time and the critical
72  * super-block data does not change, so the copies need not be
73  * referenced further unless disaster strikes.
74  *
75  * For file system fs, the offsets of the various blocks of interest
76  * are given in the super block as:
77  *	[fs->fs_sblkno]		Super-block
78  *	[fs->fs_cblkno]		Cylinder group block
79  *	[fs->fs_iblkno]		Inode blocks
80  *	[fs->fs_dblkno]		Data blocks
81  * The beginning of cylinder group cg in fs, is given by
82  * the ``cgbase(fs, cg)'' macro.
83  *
84  * The first boot and super blocks are given in absolute disk addresses.
85  * The byte-offset forms are preferred, as they don't imply a sector size.
86  */
87 #define	BBSIZE		8192
88 #define	SBSIZE		8192
89 #define	BBOFF		((off_t)(0))
90 #define	SBOFF		((off_t)(BBOFF + BBSIZE))
91 #define	BBLOCK		((daddr32_t)(0))
92 #define	SBLOCK		((daddr32_t)(BBLOCK + BBSIZE / DEV_BSIZE))
93 
94 /*
95  * Addresses stored in inodes are capable of addressing fragments
96  * of `blocks'. File system blocks of at most size MAXBSIZE can
97  * be optionally broken into 2, 4, or 8 pieces, each of which is
98  * addressible; these pieces may be DEV_BSIZE, or some multiple of
99  * a DEV_BSIZE unit.
100  *
101  * Large files consist of exclusively large data blocks.  To avoid
102  * undue wasted disk space, the last data block of a small file may be
103  * allocated as only as many fragments of a large block as are
104  * necessary.  The file system format retains only a single pointer
105  * to such a fragment, which is a piece of a single large block that
106  * has been divided.  The size of such a fragment is determinable from
107  * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
108  *
109  * The file system records space availability at the fragment level;
110  * to determine block availability, aligned fragments are examined.
111  *
112  * The root inode is the root of the file system.
113  * Inode 0 can't be used for normal purposes and
114  * historically bad blocks were linked to inode 1,
115  * thus the root inode is 2. (inode 1 is no longer used for
116  * this purpose, however numerous dump tapes make this
117  * assumption, so we are stuck with it)
118  * The lost+found directory is given the next available
119  * inode when it is created by ``mkfs''.
120  */
121 #define	UFSROOTINO	((ino_t)2)	/* i number of all roots */
122 #define	LOSTFOUNDINO    (UFSROOTINO + 1)
123 #ifndef _LONGLONG_TYPE
124 #define	UFS_MAXOFFSET_T	MAXOFF_T
125 #define	UFS_FILESIZE_BITS	32
126 #else
127 #define	UFS_MAXOFFSET_T	((1LL << NBBY * sizeof (daddr32_t) + DEV_BSHIFT - 1) \
128 							- 1)
129 #define	UFS_FILESIZE_BITS	41
130 #endif /* _LONGLONG_TYPE */
131 
132 /*
133  * MINBSIZE is the smallest allowable block size.
134  * In order to insure that it is possible to create files of size
135  * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
136  * MINBSIZE must be big enough to hold a cylinder group block,
137  * thus changes to (struct cg) must keep its size within MINBSIZE.
138  * Note that super blocks are always of size SBSIZE,
139  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
140  */
141 #define	MINBSIZE	4096
142 
143 /*
144  * The path name on which the file system is mounted is maintained
145  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
146  * the super block for this name.
147  * The limit on the amount of summary information per file system
148  * is defined by MAXCSBUFS. It is currently parameterized for a
149  * maximum of two million cylinders.
150  */
151 #define	MAXMNTLEN 512
152 #define	MAXCSBUFS 32
153 
154 /*
155  * Per cylinder group information; summarized in blocks allocated
156  * from first cylinder group data blocks.  These blocks have to be
157  * read in from fs_csaddr (size fs_cssize) in addition to the
158  * super block.
159  *
160  * N.B. sizeof (struct csum) must be a power of two in order for
161  * the ``fs_cs'' macro to work (see below).
162  */
163 struct csum {
164 	int32_t	cs_ndir;	/* number of directories */
165 	int32_t	cs_nbfree;	/* number of free blocks */
166 	int32_t	cs_nifree;	/* number of free inodes */
167 	int32_t	cs_nffree;	/* number of free frags */
168 };
169 
170 /*
171  * In the 5.0 release, the file system state flag in the superblock (fs_clean)
172  * is now used. The value of fs_clean can be:
173  *	FSACTIVE	file system may have fsck inconsistencies
174  *	FSCLEAN		file system has successfully unmounted (implies
175  *			everything is ok)
176  *	FSSTABLE	No fsck inconsistencies, no guarantee on user data
177  *	FSBAD		file system is mounted from a partition that is
178  *			neither FSCLEAN or FSSTABLE
179  *	FSSUSPEND	Clean flag processing is temporarily disabled
180  *	FSLOG		Logging file system
181  * Under this scheme, fsck can safely skip file systems that
182  * are FSCLEAN or FSSTABLE.  To provide additional safeguard,
183  * fs_clean information could be trusted only if
184  * fs_state == FSOKAY - fs_time, where FSOKAY is a constant
185  *
186  * Note: mount(2) will now return ENOSPC if fs_clean is neither FSCLEAN nor
187  * FSSTABLE, or fs_state is not valid.  The exceptions are the root or
188  * the read-only partitions
189  */
190 
191 /*
192  * Super block for a file system.
193  *
194  * Most of the data in the super block is read-only data and needs
195  * no explicit locking to protect it. Exceptions are:
196  *	fs_time
197  *	fs_optim
198  *	fs_cstotal
199  *	fs_fmod
200  *	fs_cgrotor
201  *	fs_flags   (largefiles flag - set when a file grows large)
202  * These fields require the use of fs->fs_lock.
203  */
204 #define	FS_MAGIC	0x011954
205 #define	MTB_UFS_MAGIC	0xdecade
206 #define	FSOKAY		(0x7c269d38)
207 /*  #define	FSOKAY		(0x7c269d38 + 3) */
208 /*
209  * fs_clean values
210  */
211 #define	FSACTIVE	((char)0)
212 #define	FSCLEAN		((char)0x1)
213 #define	FSSTABLE	((char)0x2)
214 #define	FSBAD		((char)0xff)	/* mounted !FSCLEAN and !FSSTABLE */
215 #define	FSSUSPEND	((char)0xfe)	/* temporarily suspended */
216 #define	FSLOG		((char)0xfd)	/* logging fs */
217 #define	FSFIX		((char)0xfc)	/* being repaired while mounted */
218 
219 /*
220  * fs_flags values
221  */
222 #define	FSLARGEFILES	((char)0x1)	/* largefiles exist on filesystem */
223 
224 struct  fs {
225 	uint32_t fs_link;		/* linked list of file systems */
226 	uint32_t fs_rolled;		/* logging only: fs fully rolled */
227 	daddr32_t fs_sblkno;		/* addr of super-block in filesys */
228 	daddr32_t fs_cblkno;		/* offset of cyl-block in filesys */
229 	daddr32_t fs_iblkno;		/* offset of inode-blocks in filesys */
230 	daddr32_t fs_dblkno;		/* offset of first data after cg */
231 	int32_t	fs_cgoffset;		/* cylinder group offset in cylinder */
232 	int32_t	fs_cgmask;		/* used to calc mod fs_ntrak */
233 	time32_t fs_time;		/* last time written */
234 	int32_t	fs_size;		/* number of blocks in fs */
235 	int32_t	fs_dsize;		/* number of data blocks in fs */
236 	int32_t	fs_ncg;			/* number of cylinder groups */
237 	int32_t	fs_bsize;		/* size of basic blocks in fs */
238 	int32_t	fs_fsize;		/* size of frag blocks in fs */
239 	int32_t	fs_frag;		/* number of frags in a block in fs */
240 /* these are configuration parameters */
241 	int32_t	fs_minfree;		/* minimum percentage of free blocks */
242 	int32_t	fs_rotdelay;		/* num of ms for optimal next block */
243 	int32_t	fs_rps;			/* disk revolutions per second */
244 /* these fields can be computed from the others */
245 	int32_t	fs_bmask;		/* ``blkoff'' calc of blk offsets */
246 	int32_t	fs_fmask;		/* ``fragoff'' calc of frag offsets */
247 	int32_t	fs_bshift;		/* ``lblkno'' calc of logical blkno */
248 	int32_t	fs_fshift;		/* ``numfrags'' calc number of frags */
249 /* these are configuration parameters */
250 	int32_t	fs_maxcontig;		/* max number of contiguous blks */
251 	int32_t	fs_maxbpg;		/* max number of blks per cyl group */
252 /* these fields can be computed from the others */
253 	int32_t	fs_fragshift;		/* block to frag shift */
254 	int32_t	fs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
255 	int32_t	fs_sbsize;		/* actual size of super block */
256 	int32_t	fs_csmask;		/* csum block offset */
257 	int32_t	fs_csshift;		/* csum block number */
258 	int32_t	fs_nindir;		/* value of NINDIR */
259 	int32_t	fs_inopb;		/* value of INOPB */
260 	int32_t	fs_nspf;		/* value of NSPF */
261 /* yet another configuration parameter */
262 	int32_t	fs_optim;		/* optimization preference, see below */
263 /* these fields are derived from the hardware */
264 	/* USL SVR4 compatibility */
265 #ifdef _LITTLE_ENDIAN
266 	/*
267 	 * USL SVR4 compatibility
268 	 *
269 	 * There was a significant divergence here between Solaris and
270 	 * SVR4 for x86.  By swapping these two members in the superblock,
271 	 * we get read-only compatibility of SVR4 filesystems.  Otherwise
272 	 * there would be no compatibility.  This change was introduced
273 	 * during bootstrapping of Solaris on x86.  By making this ifdef'ed
274 	 * on byte order, we provide ongoing compatibility across all
275 	 * platforms with the same byte order, the highest compatibility
276 	 * that can be achieved.
277 	 */
278 	int32_t	fs_state;		/* file system state time stamp */
279 #else
280 	int32_t	fs_npsect;		/* # sectors/track including spares */
281 #endif
282 	int32_t fs_si;			/* summary info state - lufs only */
283 	int32_t	fs_trackskew;		/* sector 0 skew, per track */
284 /* a unique id for this filesystem (currently unused and unmaintained) */
285 /* In 4.3 Tahoe this space is used by fs_headswitch and fs_trkseek */
286 /* Neither of those fields is used in the Tahoe code right now but */
287 /* there could be problems if they are.				*/
288 	int32_t	fs_id[2];		/* file system id */
289 /* sizes determined by number of cylinder groups and their sizes */
290 	daddr32_t fs_csaddr;		/* blk addr of cyl grp summary area */
291 	int32_t	fs_cssize;		/* size of cyl grp summary area */
292 	int32_t	fs_cgsize;		/* cylinder group size */
293 /* these fields are derived from the hardware */
294 	int32_t	fs_ntrak;		/* tracks per cylinder */
295 	int32_t	fs_nsect;		/* sectors per track */
296 	int32_t	fs_spc;			/* sectors per cylinder */
297 /* this comes from the disk driver partitioning */
298 	int32_t	fs_ncyl;		/* cylinders in file system */
299 /* these fields can be computed from the others */
300 	int32_t	fs_cpg;			/* cylinders per group */
301 	int32_t	fs_ipg;			/* inodes per group */
302 	int32_t	fs_fpg;			/* blocks per group * fs_frag */
303 /* this data must be re-computed after crashes */
304 	struct	csum fs_cstotal;	/* cylinder summary information */
305 /* these fields are cleared at mount time */
306 	char	fs_fmod;		/* super block modified flag */
307 	char	fs_clean;		/* file system state flag */
308 	char	fs_ronly;		/* mounted read-only flag */
309 	char	fs_flags;		/* largefiles flag, etc. */
310 	char	fs_fsmnt[MAXMNTLEN];	/* name mounted on */
311 /* these fields retain the current block allocation info */
312 	int32_t	fs_cgrotor;		/* last cg searched */
313 	/*
314 	 * The following used to be fs_csp[MAXCSBUFS]. It was not
315 	 * used anywhere except in old utilities.  We removed this
316 	 * in 5.6 and expect fs_u.fs_csp to be used instead.
317 	 * We no longer limit fs_cssize based on MAXCSBUFS.
318 	 */
319 	union { 			/* fs_cs (csum) info */
320 		uint32_t fs_csp_pad[MAXCSBUFS];
321 		struct csum *fs_csp;
322 	} fs_u;
323 	int32_t	fs_cpc;			/* cyl per cycle in postbl */
324 	short	fs_opostbl[16][8];	/* old rotation block list head */
325 	int32_t	fs_sparecon[51];	/* reserved for future constants */
326 	int32_t fs_version;		/* minor version of MTB ufs */
327 	int32_t	fs_logbno;		/* block # of embedded log */
328 	int32_t fs_reclaim;		/* reclaim open, deleted files */
329 	int32_t	fs_sparecon2;		/* reserved for future constant */
330 #ifdef _LITTLE_ENDIAN
331 	/* USL SVR4 compatibility */
332 	int32_t	fs_npsect;		/* # sectors/track including spares */
333 #else
334 	int32_t	fs_state;		/* file system state time stamp */
335 #endif
336 	quad_t	fs_qbmask;		/* ~fs_bmask - for use with quad size */
337 	quad_t	fs_qfmask;		/* ~fs_fmask - for use with quad size */
338 	int32_t	fs_postblformat;	/* format of positional layout tables */
339 	int32_t	fs_nrpos;		/* number of rotaional positions */
340 	int32_t	fs_postbloff;		/* (short) rotation block list head */
341 	int32_t	fs_rotbloff;		/* (uchar_t) blocks for each rotation */
342 	int32_t	fs_magic;		/* magic number */
343 	uchar_t	fs_space[1];		/* list of blocks for each rotation */
344 /* actually longer */
345 };
346 
347 /*
348  * values for fs_reclaim
349  */
350 #define	FS_RECLAIM	(0x00000001)	/* run the reclaim-files thread */
351 #define	FS_RECLAIMING	(0x00000002)	/* running the reclaim-files thread */
352 #define	FS_CHECKCLEAN	(0x00000004)	/* checking for a clean file system */
353 #define	FS_CHECKRECLAIM	(0x00000008)	/* checking for a reclaimable file */
354 
355 /*
356  * values for fs_rolled
357  */
358 #define	FS_PRE_FLAG	0	/* old system, prior to fs_rolled flag */
359 #define	FS_ALL_ROLLED	1
360 #define	FS_NEED_ROLL	2
361 
362 /*
363  * values for fs_si, logging only
364  * si is the summary of the summary - a copy of the cylinder group summary
365  * info held in an array for perf. On a mount if this is out of date
366  * (FS_SI_BAD) it can be re-constructed by re-reading the cgs.
367  */
368 #define	FS_SI_OK	0	/* on-disk summary info ok */
369 #define	FS_SI_BAD	1	/* out of date on-disk si */
370 
371 /*
372  * Preference for optimization.
373  */
374 #define	FS_OPTTIME	0	/* minimize allocation time */
375 #define	FS_OPTSPACE	1	/* minimize disk fragmentation */
376 
377 /*
378  * Rotational layout table format types
379  */
380 #define	FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
381 #define	FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
382 
383 /*
384  * Macros for access to superblock array structures
385  */
386 #ifdef _KERNEL
387 #define	fs_postbl(ufsvfsp, cylno) \
388 	(((ufsvfsp)->vfs_fs->fs_postblformat != FS_DYNAMICPOSTBLFMT) \
389 	? ((ufsvfsp)->vfs_fs->fs_opostbl[cylno]) \
390 	: ((short *)((char *)(ufsvfsp)->vfs_fs + \
391 	(ufsvfsp)->vfs_fs->fs_postbloff) \
392 	+ (cylno) * (ufsvfsp)->vfs_nrpos))
393 #else
394 #define	fs_postbl(fs, cylno) \
395 	(((fs)->fs_postblformat != FS_DYNAMICPOSTBLFMT) \
396 	? ((fs)->fs_opostbl[cylno]) \
397 	: ((short *)((char *)(fs) + \
398 	(fs)->fs_postbloff) \
399 	+ (cylno) * (fs)->fs_nrpos))
400 #endif
401 
402 #define	fs_rotbl(fs) \
403 	(((fs)->fs_postblformat != FS_DYNAMICPOSTBLFMT) \
404 	? ((fs)->fs_space) \
405 	: ((uchar_t *)((char *)(fs) + (fs)->fs_rotbloff)))
406 
407 /*
408  * Convert cylinder group to base address of its global summary info.
409  *
410  * N.B. This macro assumes that sizeof (struct csum) is a power of two.
411  * We just index off the first entry into one big array
412  */
413 
414 #define	fs_cs(fs, indx) fs_u.fs_csp[(indx)]
415 
416 /*
417  * Cylinder group block for a file system.
418  *
419  * Writable fields in the cylinder group are protected by the associated
420  * super block lock fs->fs_lock.
421  */
422 #define	CG_MAGIC	0x090255
423 struct	cg {
424 	uint32_t cg_link;		/* NOT USED linked list of cyl groups */
425 	int32_t	cg_magic;		/* magic number */
426 	time32_t cg_time;		/* time last written */
427 	int32_t	cg_cgx;			/* we are the cgx'th cylinder group */
428 	short	cg_ncyl;		/* number of cyl's this cg */
429 	short	cg_niblk;		/* number of inode blocks this cg */
430 	int32_t	cg_ndblk;		/* number of data blocks this cg */
431 	struct	csum cg_cs;		/* cylinder summary information */
432 	int32_t	cg_rotor;		/* position of last used block */
433 	int32_t	cg_frotor;		/* position of last used frag */
434 	int32_t	cg_irotor;		/* position of last used inode */
435 	int32_t	cg_frsum[MAXFRAG];	/* counts of available frags */
436 	int32_t	cg_btotoff;		/* (int32_t)block totals per cylinder */
437 	int32_t	cg_boff;		/* (short) free block positions */
438 	int32_t	cg_iusedoff;		/* (char) used inode map */
439 	int32_t	cg_freeoff;		/* (uchar_t) free block map */
440 	int32_t	cg_nextfreeoff;		/* (uchar_t) next available space */
441 	int32_t	cg_sparecon[16];	/* reserved for future use */
442 	uchar_t	cg_space[1];		/* space for cylinder group maps */
443 /* actually longer */
444 };
445 
446 /*
447  * Macros for access to cylinder group array structures
448  */
449 
450 #define	cg_blktot(cgp) \
451 	(((cgp)->cg_magic != CG_MAGIC) \
452 	? (((struct ocg *)(cgp))->cg_btot) \
453 	: ((int32_t *)((char *)(cgp) + (cgp)->cg_btotoff)))
454 
455 #ifdef _KERNEL
456 #define	cg_blks(ufsvfsp, cgp, cylno) \
457 	(((cgp)->cg_magic != CG_MAGIC) \
458 	? (((struct ocg *)(cgp))->cg_b[cylno]) \
459 	: ((short *)((char *)(cgp) + (cgp)->cg_boff) + \
460 	(cylno) * (ufsvfsp)->vfs_nrpos))
461 #else
462 #define	cg_blks(fs, cgp, cylno) \
463 	(((cgp)->cg_magic != CG_MAGIC) \
464 	? (((struct ocg *)(cgp))->cg_b[cylno]) \
465 	: ((short *)((char *)(cgp) + (cgp)->cg_boff) + \
466 	(cylno) * (fs)->fs_nrpos))
467 #endif
468 
469 #define	cg_inosused(cgp) \
470 	(((cgp)->cg_magic != CG_MAGIC) \
471 	? (((struct ocg *)(cgp))->cg_iused) \
472 	: ((char *)((char *)(cgp) + (cgp)->cg_iusedoff)))
473 
474 #define	cg_blksfree(cgp) \
475 	(((cgp)->cg_magic != CG_MAGIC) \
476 	? (((struct ocg *)(cgp))->cg_free) \
477 	: ((uchar_t *)((char *)(cgp) + (cgp)->cg_freeoff)))
478 
479 #define	cg_chkmagic(cgp) \
480 	((cgp)->cg_magic == CG_MAGIC || \
481 	((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
482 
483 /*
484  * The following structure is defined
485  * for compatibility with old file systems.
486  */
487 struct	ocg {
488 	uint32_t cg_link;		/* NOT USED linked list of cyl groups */
489 	uint32_t cg_rlink;		/* NOT USED incore cyl groups */
490 	time32_t cg_time;		/* time last written */
491 	int32_t	cg_cgx;			/* we are the cgx'th cylinder group */
492 	short	cg_ncyl;		/* number of cyl's this cg */
493 	short	cg_niblk;		/* number of inode blocks this cg */
494 	int32_t	cg_ndblk;		/* number of data blocks this cg */
495 	struct	csum cg_cs;		/* cylinder summary information */
496 	int32_t	cg_rotor;		/* position of last used block */
497 	int32_t	cg_frotor;		/* position of last used frag */
498 	int32_t	cg_irotor;		/* position of last used inode */
499 	int32_t	cg_frsum[8];		/* counts of available frags */
500 	int32_t	cg_btot[32];		/* block totals per cylinder */
501 	short	cg_b[32][8];		/* positions of free blocks */
502 	char	cg_iused[256];		/* used inode map */
503 	int32_t	cg_magic;		/* magic number */
504 	uchar_t	cg_free[1];		/* free block map */
505 /* actually longer */
506 };
507 
508 /*
509  * Turn frag offsets into disk block addresses.
510  * This maps frags to device size blocks.
511  * (In the names of these macros, "fsb" refers to "frags", not
512  * file system blocks.)
513  */
514 #ifdef KERNEL
515 #define	fsbtodb(fs, b)	(((daddr_t)(b)) << (fs)->fs_fsbtodb)
516 #else /* KERNEL */
517 #define	fsbtodb(fs, b)	(((diskaddr_t)(b)) << (fs)->fs_fsbtodb)
518 #endif /* KERNEL */
519 
520 #define	dbtofsb(fs, b)	((b) >> (fs)->fs_fsbtodb)
521 
522 /*
523  * Get the offset of the log, in either sectors, frags, or file system
524  * blocks.  The interpretation of the fs_logbno field depends on whether
525  * this is UFS or MTB UFS.  (UFS stores the value as sectors.  MTBUFS
526  * stores the value as frags.)
527  */
528 
529 #ifdef KERNEL
530 #define	logbtodb(fs, b)	((fs)->fs_magic == FS_MAGIC ? \
531 		(daddr_t)(b) : ((daddr_t)(b) << (fs)->fs_fsbtodb))
532 #else /* KERNEL */
533 #define	logbtodb(fs, b)	((fs)->fs_magic == FS_MAGIC ? \
534 		(diskaddr_t)(b) : ((diskaddr_t)(b) << (fs)->fs_fsbtodb))
535 #endif /* KERNEL */
536 #define	logbtofrag(fs, b)	((fs)->fs_magic == FS_MAGIC ? \
537 		(b) >> (fs)->fs_fsbtodb : (b))
538 #define	logbtofsblk(fs, b) ((fs)->fs_magic == FS_MAGIC ? \
539 		(b) >> ((fs)->fs_fsbtodb + (fs)->fs_fragshift) : \
540 		(b) >> (fs)->fs_fragshift)
541 
542 /*
543  * Cylinder group macros to locate things in cylinder groups.
544  * They calc file system addresses of cylinder group data structures.
545  */
546 #define	cgbase(fs, c)	((daddr32_t)((fs)->fs_fpg * (c)))
547 
548 #define	cgstart(fs, c) \
549 	(cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
550 
551 #define	cgsblock(fs, c)	(cgstart(fs, c) + (fs)->fs_sblkno)	/* super blk */
552 
553 #define	cgtod(fs, c)	(cgstart(fs, c) + (fs)->fs_cblkno)	/* cg block */
554 
555 #define	cgimin(fs, c)	(cgstart(fs, c) + (fs)->fs_iblkno)	/* inode blk */
556 
557 #define	cgdmin(fs, c)	(cgstart(fs, c) + (fs)->fs_dblkno)	/* 1st data */
558 
559 /*
560  * Macros for handling inode numbers:
561  *	inode number to file system block offset.
562  *	inode number to cylinder group number.
563  *	inode number to file system block address.
564  */
565 #define	itoo(fs, x)	((x) % (uint32_t)INOPB(fs))
566 
567 #define	itog(fs, x)	((x) / (uint32_t)(fs)->fs_ipg)
568 
569 #define	itod(fs, x) \
570 	((daddr32_t)(cgimin(fs, itog(fs, x)) + \
571 	(blkstofrags((fs), (((x)%(ulong_t)(fs)->fs_ipg)/(ulong_t)INOPB(fs))))))
572 
573 /*
574  * Give cylinder group number for a file system block.
575  * Give cylinder group block number for a file system block.
576  */
577 #define	dtog(fs, d)	((d) / (fs)->fs_fpg)
578 #define	dtogd(fs, d)	((d) % (fs)->fs_fpg)
579 
580 /*
581  * Extract the bits for a block from a map.
582  * Compute the cylinder and rotational position of a cyl block addr.
583  */
584 #define	blkmap(fs, map, loc) \
585 	(((map)[(loc) / NBBY] >> ((loc) % NBBY)) & \
586 	(0xff >> (NBBY - (fs)->fs_frag)))
587 
588 #define	cbtocylno(fs, bno) \
589 	((bno) * NSPF(fs) / (fs)->fs_spc)
590 
591 #ifdef _KERNEL
592 #define	cbtorpos(ufsvfsp, bno) \
593 	((((bno) * NSPF((ufsvfsp)->vfs_fs) % (ufsvfsp)->vfs_fs->fs_spc) % \
594 	(ufsvfsp)->vfs_fs->fs_nsect) * \
595 	(ufsvfsp)->vfs_nrpos) / (ufsvfsp)->vfs_fs->fs_nsect
596 #else
597 #define	cbtorpos(fs, bno) \
598 	((((bno) * NSPF(fs) % (fs)->fs_spc) % \
599 	(fs)->fs_nsect) * \
600 	(fs)->fs_nrpos) / (fs)->fs_nsect
601 #endif
602 
603 /*
604  * The following macros optimize certain frequently calculated
605  * quantities by using shifts and masks in place of divisions
606  * modulos and multiplications.
607  */
608 
609 /*
610  * This macro works for 40 bit offset support in ufs because
611  * this calculates offset in the block and therefore no loss of
612  * information while casting to int.
613  */
614 
615 #define	blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
616 	((int)((loc) & ~(fs)->fs_bmask))
617 
618 /*
619  * This macro works for 40 bit offset support similar to blkoff
620  */
621 
622 #define	fragoff(fs, loc)	/* calculates (loc % fs->fs_fsize) */ \
623 	((int)((loc) & ~(fs)->fs_fmask))
624 
625 /*
626  * The cast to int32_t does not result in any loss of information because
627  * the number of logical blocks in the file system is limited to
628  * what fits in an int32_t anyway.
629  */
630 
631 #define	lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
632 	((int32_t)((loc) >> (fs)->fs_bshift))
633 
634 /*
635  * The same argument as above applies here.
636  */
637 
638 #define	numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
639 	((int32_t)((loc) >> (fs)->fs_fshift))
640 
641 /*
642  * Size can be a 64-bit value and therefore we sign extend fs_bmask
643  * to a 64-bit value too so that the higher 32 bits are masked
644  * properly. Note that the type of fs_bmask has to be signed. Otherwise
645  * compiler will set the higher 32 bits as zero and we don't want
646  * this to happen.
647  */
648 
649 #define	blkroundup(fs, size)	/* calculates roundup(size, fs->fs_bsize) */ \
650 	(((size) + (fs)->fs_bsize - 1) & (offset_t)(fs)->fs_bmask)
651 
652 /*
653  * Same argument as above.
654  */
655 
656 #define	fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
657 	(((size) + (fs)->fs_fsize - 1) & (offset_t)(fs)->fs_fmask)
658 
659 /*
660  * frags cannot exceed 32-bit value since we only support 40bit sizes.
661  */
662 
663 #define	fragstoblks(fs, frags)	/* calculates (frags / fs->fs_frag) */ \
664 	((frags) >> (fs)->fs_fragshift)
665 
666 #define	blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
667 	((blks) << (fs)->fs_fragshift)
668 
669 #define	fragnum(fs, fsb)	/* calculates (fsb % fs->fs_frag) */ \
670 	((fsb) & ((fs)->fs_frag - 1))
671 
672 #define	blknum(fs, fsb)		/* calculates rounddown(fsb, fs->fs_frag) */ \
673 	((fsb) &~ ((fs)->fs_frag - 1))
674 
675 /*
676  * Determine the number of available frags given a
677  * percentage to hold in reserve
678  */
679 #define	freespace(fs, ufsvfsp) \
680 	((blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
681 	(fs)->fs_cstotal.cs_nffree) - (ufsvfsp)->vfs_minfrags)
682 
683 /*
684  * Determining the size of a file block in the file system.
685  */
686 
687 #define	blksize(fs, ip, lbn) \
688 	(((lbn) >= NDADDR || \
689 	(ip)->i_size >= (offset_t)((lbn) + 1) << (fs)->fs_bshift) \
690 	    ? (fs)->fs_bsize \
691 	    : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
692 
693 #define	dblksize(fs, dip, lbn) \
694 	(((lbn) >= NDADDR || \
695 	(dip)->di_size >= (offset_t)((lbn) + 1) << (fs)->fs_bshift) \
696 	    ? (fs)->fs_bsize \
697 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
698 
699 /*
700  * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
701  */
702 #define	NSPB(fs)	((fs)->fs_nspf << (fs)->fs_fragshift)
703 #define	NSPF(fs)	((fs)->fs_nspf)
704 
705 /*
706  * INOPB is the number of inodes in a secondary storage block.
707  */
708 #define	INOPB(fs)	((fs)->fs_inopb)
709 #define	INOPF(fs)	((fs)->fs_inopb >> (fs)->fs_fragshift)
710 
711 /*
712  * NINDIR is the number of indirects in a file system block.
713  */
714 #define	NINDIR(fs)	((fs)->fs_nindir)
715 
716 /*
717  * bit map related macros
718  */
719 #define	bitloc(a, i)	((a)[(i)/NBBY])
720 #define	setbit(a, i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
721 #define	clrbit(a, i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
722 #define	isset(a, i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
723 #define	isclr(a, i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
724 
725 #define	getfs(vfsp) \
726 	((struct fs *)((struct ufsvfs *)vfsp->vfs_data)->vfs_bufp->b_un.b_addr)
727 
728 #ifdef	__cplusplus
729 }
730 #endif
731 
732 #endif	/* _SYS_FS_UFS_FS_H */
733