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