include: demolish _LONGLONG_TYPE; consider "long long" always valid
[unleashed.git] / include / sys / fs / ufs_fs.h
blobbb5c966b0c3d0b3077196ebbc8d088dead4d5d7b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
39 #ifndef _SYS_FS_UFS_FS_H
40 #define _SYS_FS_UFS_FS_H
42 #pragma ident "%Z%%M% %I% %E% SMI"
44 #include <sys/isa_defs.h>
45 #include <sys/types32.h>
46 #include <sys/t_lock.h> /* for kmutex_t */
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
53 * The following values are minor release values for UFS.
54 * The fs_version field in the superblock will equal one of them.
57 #define MTB_UFS_VERSION_MIN 1
58 #define MTB_UFS_VERSION_1 1
59 #define UFS_VERSION_MIN 0
60 #define UFS_EFISTYLE4NONEFI_VERSION_2 2
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.
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.
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.
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.
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))
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.
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.
109 * The file system records space availability at the fragment level;
110 * to determine block availability, aligned fragments are examined.
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''.
121 #define UFSROOTINO ((ino_t)2) /* i number of all roots */
122 #define LOSTFOUNDINO (UFSROOTINO + 1)
123 #define UFS_MAXOFFSET_T ((1LL << NBBY * sizeof (daddr32_t) + DEV_BSHIFT - 1) \
124 - 1)
125 #define UFS_FILESIZE_BITS 41
128 * MINBSIZE is the smallest allowable block size.
129 * In order to insure that it is possible to create files of size
130 * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
131 * MINBSIZE must be big enough to hold a cylinder group block,
132 * thus changes to (struct cg) must keep its size within MINBSIZE.
133 * Note that super blocks are always of size SBSIZE,
134 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
136 #define MINBSIZE 4096
139 * The path name on which the file system is mounted is maintained
140 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
141 * the super block for this name.
142 * The limit on the amount of summary information per file system
143 * is defined by MAXCSBUFS. It is currently parameterized for a
144 * maximum of two million cylinders.
146 #define MAXMNTLEN 512
147 #define MAXCSBUFS 32
149 #define LABEL_TYPE_VTOC 1
150 #define LABEL_TYPE_EFI 2
151 #define LABEL_TYPE_OTHER 3
154 * The following constant is taken from the ANSI T13 ATA Specification
155 * and defines the maximum size (in sectors) that an ATA disk can be
156 * and still has to provide CHS translation. For a disk above this
157 * size all sectors are to be accessed via their LBA address. This
158 * makes a good cut off value to move from disk provided geometry
159 * to the predefined defaults used in efi label disks.
161 #define CHSLIMIT (63 * 256 * 1024)
164 * Per cylinder group information; summarized in blocks allocated
165 * from first cylinder group data blocks. These blocks have to be
166 * read in from fs_csaddr (size fs_cssize) in addition to the
167 * super block.
169 * N.B. sizeof (struct csum) must be a power of two in order for
170 * the ``fs_cs'' macro to work (see below).
172 struct csum {
173 int32_t cs_ndir; /* number of directories */
174 int32_t cs_nbfree; /* number of free blocks */
175 int32_t cs_nifree; /* number of free inodes */
176 int32_t cs_nffree; /* number of free frags */
180 * In the 5.0 release, the file system state flag in the superblock (fs_clean)
181 * is now used. The value of fs_clean can be:
182 * FSACTIVE file system may have fsck inconsistencies
183 * FSCLEAN file system has successfully unmounted (implies
184 * everything is ok)
185 * FSSTABLE No fsck inconsistencies, no guarantee on user data
186 * FSBAD file system is mounted from a partition that is
187 * neither FSCLEAN or FSSTABLE
188 * FSSUSPEND Clean flag processing is temporarily disabled
189 * FSLOG Logging file system
190 * Under this scheme, fsck can safely skip file systems that
191 * are FSCLEAN or FSSTABLE. To provide additional safeguard,
192 * fs_clean information could be trusted only if
193 * fs_state == FSOKAY - fs_time, where FSOKAY is a constant
195 * Note: mount(2) will now return ENOSPC if fs_clean is neither FSCLEAN nor
196 * FSSTABLE, or fs_state is not valid. The exceptions are the root or
197 * the read-only partitions
201 * Super block for a file system.
203 * Most of the data in the super block is read-only data and needs
204 * no explicit locking to protect it. Exceptions are:
205 * fs_time
206 * fs_optim
207 * fs_cstotal
208 * fs_fmod
209 * fs_cgrotor
210 * fs_flags (largefiles flag - set when a file grows large)
211 * These fields require the use of fs->fs_lock.
213 #define FS_MAGIC 0x011954
214 #define MTB_UFS_MAGIC 0xdecade
215 #define FSOKAY (0x7c269d38)
216 /* #define FSOKAY (0x7c269d38 + 3) */
218 * fs_clean values
220 #define FSACTIVE ((char)0)
221 #define FSCLEAN ((char)0x1)
222 #define FSSTABLE ((char)0x2)
223 #define FSBAD ((char)0xff) /* mounted !FSCLEAN and !FSSTABLE */
224 #define FSSUSPEND ((char)0xfe) /* temporarily suspended */
225 #define FSLOG ((char)0xfd) /* logging fs */
226 #define FSFIX ((char)0xfc) /* being repaired while mounted */
229 * fs_flags values
231 #define FSLARGEFILES ((char)0x1) /* largefiles exist on filesystem */
233 struct fs {
234 uint32_t fs_link; /* linked list of file systems */
235 uint32_t fs_rolled; /* logging only: fs fully rolled */
236 daddr32_t fs_sblkno; /* addr of super-block in filesys */
237 daddr32_t fs_cblkno; /* offset of cyl-block in filesys */
238 daddr32_t fs_iblkno; /* offset of inode-blocks in filesys */
239 daddr32_t fs_dblkno; /* offset of first data after cg */
240 int32_t fs_cgoffset; /* cylinder group offset in cylinder */
241 int32_t fs_cgmask; /* used to calc mod fs_ntrak */
242 time32_t fs_time; /* last time written */
243 int32_t fs_size; /* number of blocks in fs */
244 int32_t fs_dsize; /* number of data blocks in fs */
245 int32_t fs_ncg; /* number of cylinder groups */
246 int32_t fs_bsize; /* size of basic blocks in fs */
247 int32_t fs_fsize; /* size of frag blocks in fs */
248 int32_t fs_frag; /* number of frags in a block in fs */
249 /* these are configuration parameters */
250 int32_t fs_minfree; /* minimum percentage of free blocks */
251 int32_t fs_rotdelay; /* num of ms for optimal next block */
252 int32_t fs_rps; /* disk revolutions per second */
253 /* these fields can be computed from the others */
254 int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */
255 int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */
256 int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */
257 int32_t fs_fshift; /* ``numfrags'' calc number of frags */
258 /* these are configuration parameters */
259 int32_t fs_maxcontig; /* max number of contiguous blks */
260 int32_t fs_maxbpg; /* max number of blks per cyl group */
261 /* these fields can be computed from the others */
262 int32_t fs_fragshift; /* block to frag shift */
263 int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
264 int32_t fs_sbsize; /* actual size of super block */
265 int32_t fs_csmask; /* csum block offset */
266 int32_t fs_csshift; /* csum block number */
267 int32_t fs_nindir; /* value of NINDIR */
268 int32_t fs_inopb; /* value of INOPB */
269 int32_t fs_nspf; /* value of NSPF */
270 /* yet another configuration parameter */
271 int32_t fs_optim; /* optimization preference, see below */
272 /* these fields are derived from the hardware */
273 /* USL SVR4 compatibility */
274 #ifdef _LITTLE_ENDIAN
276 * USL SVR4 compatibility
278 * There was a significant divergence here between Solaris and
279 * SVR4 for x86. By swapping these two members in the superblock,
280 * we get read-only compatibility of SVR4 filesystems. Otherwise
281 * there would be no compatibility. This change was introduced
282 * during bootstrapping of Solaris on x86. By making this ifdef'ed
283 * on byte order, we provide ongoing compatibility across all
284 * platforms with the same byte order, the highest compatibility
285 * that can be achieved.
287 int32_t fs_state; /* file system state time stamp */
288 #else
289 int32_t fs_npsect; /* # sectors/track including spares */
290 #endif
291 int32_t fs_si; /* summary info state - lufs only */
292 int32_t fs_trackskew; /* sector 0 skew, per track */
293 /* a unique id for this filesystem (currently unused and unmaintained) */
294 /* In 4.3 Tahoe this space is used by fs_headswitch and fs_trkseek */
295 /* Neither of those fields is used in the Tahoe code right now but */
296 /* there could be problems if they are. */
297 int32_t fs_id[2]; /* file system id */
298 /* sizes determined by number of cylinder groups and their sizes */
299 daddr32_t fs_csaddr; /* blk addr of cyl grp summary area */
300 int32_t fs_cssize; /* size of cyl grp summary area */
301 int32_t fs_cgsize; /* cylinder group size */
302 /* these fields are derived from the hardware */
303 int32_t fs_ntrak; /* tracks per cylinder */
304 int32_t fs_nsect; /* sectors per track */
305 int32_t fs_spc; /* sectors per cylinder */
306 /* this comes from the disk driver partitioning */
307 int32_t fs_ncyl; /* cylinders in file system */
308 /* these fields can be computed from the others */
309 int32_t fs_cpg; /* cylinders per group */
310 int32_t fs_ipg; /* inodes per group */
311 int32_t fs_fpg; /* blocks per group * fs_frag */
312 /* this data must be re-computed after crashes */
313 struct csum fs_cstotal; /* cylinder summary information */
314 /* these fields are cleared at mount time */
315 char fs_fmod; /* super block modified flag */
316 char fs_clean; /* file system state flag */
317 char fs_ronly; /* mounted read-only flag */
318 char fs_flags; /* largefiles flag, etc. */
319 char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
320 /* these fields retain the current block allocation info */
321 int32_t fs_cgrotor; /* last cg searched */
323 * The following used to be fs_csp[MAXCSBUFS]. It was not
324 * used anywhere except in old utilities. We removed this
325 * in 5.6 and expect fs_u.fs_csp to be used instead.
326 * We no longer limit fs_cssize based on MAXCSBUFS.
328 union { /* fs_cs (csum) info */
329 uint32_t fs_csp_pad[MAXCSBUFS];
330 struct csum *fs_csp;
331 } fs_u;
332 int32_t fs_cpc; /* cyl per cycle in postbl */
333 short fs_opostbl[16][8]; /* old rotation block list head */
334 int32_t fs_sparecon[51]; /* reserved for future constants */
335 int32_t fs_version; /* minor version of ufs */
336 int32_t fs_logbno; /* block # of embedded log */
337 int32_t fs_reclaim; /* reclaim open, deleted files */
338 int32_t fs_sparecon2; /* reserved for future constant */
339 #ifdef _LITTLE_ENDIAN
340 /* USL SVR4 compatibility */
341 int32_t fs_npsect; /* # sectors/track including spares */
342 #else
343 int32_t fs_state; /* file system state time stamp */
344 #endif
345 quad_t fs_qbmask; /* ~fs_bmask - for use with quad size */
346 quad_t fs_qfmask; /* ~fs_fmask - for use with quad size */
347 int32_t fs_postblformat; /* format of positional layout tables */
348 int32_t fs_nrpos; /* number of rotaional positions */
349 int32_t fs_postbloff; /* (short) rotation block list head */
350 int32_t fs_rotbloff; /* (uchar_t) blocks for each rotation */
351 int32_t fs_magic; /* magic number */
352 uchar_t fs_space[1]; /* list of blocks for each rotation */
353 /* actually longer */
357 * values for fs_reclaim
359 #define FS_RECLAIM (0x00000001) /* run the reclaim-files thread */
360 #define FS_RECLAIMING (0x00000002) /* running the reclaim-files thread */
361 #define FS_CHECKCLEAN (0x00000004) /* checking for a clean file system */
362 #define FS_CHECKRECLAIM (0x00000008) /* checking for a reclaimable file */
365 * values for fs_rolled
367 #define FS_PRE_FLAG 0 /* old system, prior to fs_rolled flag */
368 #define FS_ALL_ROLLED 1
369 #define FS_NEED_ROLL 2
372 * values for fs_si, logging only
373 * si is the summary of the summary - a copy of the cylinder group summary
374 * info held in an array for perf. On a mount if this is out of date
375 * (FS_SI_BAD) it can be re-constructed by re-reading the cgs.
377 #define FS_SI_OK 0 /* on-disk summary info ok */
378 #define FS_SI_BAD 1 /* out of date on-disk si */
381 * Preference for optimization.
383 #define FS_OPTTIME 0 /* minimize allocation time */
384 #define FS_OPTSPACE 1 /* minimize disk fragmentation */
387 * Rotational layout table format types
389 #define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */
390 #define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */
393 * Macros for access to superblock array structures
395 #ifdef _KERNEL
396 #define fs_postbl(ufsvfsp, cylno) \
397 (((ufsvfsp)->vfs_fs->fs_postblformat != FS_DYNAMICPOSTBLFMT) \
398 ? ((ufsvfsp)->vfs_fs->fs_opostbl[cylno]) \
399 : ((short *)((char *)(ufsvfsp)->vfs_fs + \
400 (ufsvfsp)->vfs_fs->fs_postbloff) \
401 + (cylno) * (ufsvfsp)->vfs_nrpos))
402 #else
403 #define fs_postbl(fs, cylno) \
404 (((fs)->fs_postblformat != FS_DYNAMICPOSTBLFMT) \
405 ? ((fs)->fs_opostbl[cylno]) \
406 : ((short *)((char *)(fs) + \
407 (fs)->fs_postbloff) \
408 + (cylno) * (fs)->fs_nrpos))
409 #endif
411 #define fs_rotbl(fs) \
412 (((fs)->fs_postblformat != FS_DYNAMICPOSTBLFMT) \
413 ? ((fs)->fs_space) \
414 : ((uchar_t *)((char *)(fs) + (fs)->fs_rotbloff)))
417 * Convert cylinder group to base address of its global summary info.
419 * N.B. This macro assumes that sizeof (struct csum) is a power of two.
420 * We just index off the first entry into one big array
423 #define fs_cs(fs, indx) fs_u.fs_csp[(indx)]
426 * Cylinder group block for a file system.
428 * Writable fields in the cylinder group are protected by the associated
429 * super block lock fs->fs_lock.
431 #define CG_MAGIC 0x090255
432 struct cg {
433 uint32_t cg_link; /* NOT USED linked list of cyl groups */
434 int32_t cg_magic; /* magic number */
435 time32_t cg_time; /* time last written */
436 int32_t cg_cgx; /* we are the cgx'th cylinder group */
437 short cg_ncyl; /* number of cyl's this cg */
438 short cg_niblk; /* number of inode blocks this cg */
439 int32_t cg_ndblk; /* number of data blocks this cg */
440 struct csum cg_cs; /* cylinder summary information */
441 int32_t cg_rotor; /* position of last used block */
442 int32_t cg_frotor; /* position of last used frag */
443 int32_t cg_irotor; /* position of last used inode */
444 int32_t cg_frsum[MAXFRAG]; /* counts of available frags */
445 int32_t cg_btotoff; /* (int32_t)block totals per cylinder */
446 int32_t cg_boff; /* (short) free block positions */
447 int32_t cg_iusedoff; /* (char) used inode map */
448 int32_t cg_freeoff; /* (uchar_t) free block map */
449 int32_t cg_nextfreeoff; /* (uchar_t) next available space */
450 int32_t cg_sparecon[16]; /* reserved for future use */
451 uchar_t cg_space[1]; /* space for cylinder group maps */
452 /* actually longer */
456 * Macros for access to cylinder group array structures
459 #define cg_blktot(cgp) \
460 (((cgp)->cg_magic != CG_MAGIC) \
461 ? (((struct ocg *)(cgp))->cg_btot) \
462 : ((int32_t *)((char *)(cgp) + (cgp)->cg_btotoff)))
464 #ifdef _KERNEL
465 #define cg_blks(ufsvfsp, cgp, cylno) \
466 (((cgp)->cg_magic != CG_MAGIC) \
467 ? (((struct ocg *)(cgp))->cg_b[cylno]) \
468 : ((short *)((char *)(cgp) + (cgp)->cg_boff) + \
469 (cylno) * (ufsvfsp)->vfs_nrpos))
470 #else
471 #define cg_blks(fs, cgp, cylno) \
472 (((cgp)->cg_magic != CG_MAGIC) \
473 ? (((struct ocg *)(cgp))->cg_b[cylno]) \
474 : ((short *)((char *)(cgp) + (cgp)->cg_boff) + \
475 (cylno) * (fs)->fs_nrpos))
476 #endif
478 #define cg_inosused(cgp) \
479 (((cgp)->cg_magic != CG_MAGIC) \
480 ? (((struct ocg *)(cgp))->cg_iused) \
481 : ((char *)((char *)(cgp) + (cgp)->cg_iusedoff)))
483 #define cg_blksfree(cgp) \
484 (((cgp)->cg_magic != CG_MAGIC) \
485 ? (((struct ocg *)(cgp))->cg_free) \
486 : ((uchar_t *)((char *)(cgp) + (cgp)->cg_freeoff)))
488 #define cg_chkmagic(cgp) \
489 ((cgp)->cg_magic == CG_MAGIC || \
490 ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
493 * The following structure is defined
494 * for compatibility with old file systems.
496 struct ocg {
497 uint32_t cg_link; /* NOT USED linked list of cyl groups */
498 uint32_t cg_rlink; /* NOT USED incore cyl groups */
499 time32_t cg_time; /* time last written */
500 int32_t cg_cgx; /* we are the cgx'th cylinder group */
501 short cg_ncyl; /* number of cyl's this cg */
502 short cg_niblk; /* number of inode blocks this cg */
503 int32_t cg_ndblk; /* number of data blocks this cg */
504 struct csum cg_cs; /* cylinder summary information */
505 int32_t cg_rotor; /* position of last used block */
506 int32_t cg_frotor; /* position of last used frag */
507 int32_t cg_irotor; /* position of last used inode */
508 int32_t cg_frsum[8]; /* counts of available frags */
509 int32_t cg_btot[32]; /* block totals per cylinder */
510 short cg_b[32][8]; /* positions of free blocks */
511 char cg_iused[256]; /* used inode map */
512 int32_t cg_magic; /* magic number */
513 uchar_t cg_free[1]; /* free block map */
514 /* actually longer */
518 * Turn frag offsets into disk block addresses.
519 * This maps frags to device size blocks.
520 * (In the names of these macros, "fsb" refers to "frags", not
521 * file system blocks.)
523 #ifdef KERNEL
524 #define fsbtodb(fs, b) (((daddr_t)(b)) << (fs)->fs_fsbtodb)
525 #else /* KERNEL */
526 #define fsbtodb(fs, b) (((diskaddr_t)(b)) << (fs)->fs_fsbtodb)
527 #endif /* KERNEL */
529 #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
532 * Get the offset of the log, in either sectors, frags, or file system
533 * blocks. The interpretation of the fs_logbno field depends on whether
534 * this is UFS or MTB UFS. (UFS stores the value as sectors. MTBUFS
535 * stores the value as frags.)
538 #ifdef KERNEL
539 #define logbtodb(fs, b) ((fs)->fs_magic == FS_MAGIC ? \
540 (daddr_t)(b) : ((daddr_t)(b) << (fs)->fs_fsbtodb))
541 #else /* KERNEL */
542 #define logbtodb(fs, b) ((fs)->fs_magic == FS_MAGIC ? \
543 (diskaddr_t)(b) : ((diskaddr_t)(b) << (fs)->fs_fsbtodb))
544 #endif /* KERNEL */
545 #define logbtofrag(fs, b) ((fs)->fs_magic == FS_MAGIC ? \
546 (b) >> (fs)->fs_fsbtodb : (b))
547 #define logbtofsblk(fs, b) ((fs)->fs_magic == FS_MAGIC ? \
548 (b) >> ((fs)->fs_fsbtodb + (fs)->fs_fragshift) : \
549 (b) >> (fs)->fs_fragshift)
552 * Cylinder group macros to locate things in cylinder groups.
553 * They calc file system addresses of cylinder group data structures.
555 #define cgbase(fs, c) ((daddr32_t)((fs)->fs_fpg * (c)))
557 #define cgstart(fs, c) \
558 (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
560 #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
562 #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
564 #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
566 #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
569 * Macros for handling inode numbers:
570 * inode number to file system block offset.
571 * inode number to cylinder group number.
572 * inode number to file system block address.
574 #define itoo(fs, x) ((x) % (uint32_t)INOPB(fs))
576 #define itog(fs, x) ((x) / (uint32_t)(fs)->fs_ipg)
578 #define itod(fs, x) \
579 ((daddr32_t)(cgimin(fs, itog(fs, x)) + \
580 (blkstofrags((fs), (((x)%(ulong_t)(fs)->fs_ipg)/(ulong_t)INOPB(fs))))))
583 * Give cylinder group number for a file system block.
584 * Give cylinder group block number for a file system block.
586 #define dtog(fs, d) ((d) / (fs)->fs_fpg)
587 #define dtogd(fs, d) ((d) % (fs)->fs_fpg)
590 * Extract the bits for a block from a map.
591 * Compute the cylinder and rotational position of a cyl block addr.
593 #define blkmap(fs, map, loc) \
594 (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & \
595 (0xff >> (NBBY - (fs)->fs_frag)))
597 #define cbtocylno(fs, bno) \
598 ((bno) * NSPF(fs) / (fs)->fs_spc)
600 #ifdef _KERNEL
601 #define cbtorpos(ufsvfsp, bno) \
602 ((((bno) * NSPF((ufsvfsp)->vfs_fs) % (ufsvfsp)->vfs_fs->fs_spc) % \
603 (ufsvfsp)->vfs_fs->fs_nsect) * \
604 (ufsvfsp)->vfs_nrpos) / (ufsvfsp)->vfs_fs->fs_nsect
605 #else
606 #define cbtorpos(fs, bno) \
607 ((((bno) * NSPF(fs) % (fs)->fs_spc) % \
608 (fs)->fs_nsect) * \
609 (fs)->fs_nrpos) / (fs)->fs_nsect
610 #endif
613 * The following macros optimize certain frequently calculated
614 * quantities by using shifts and masks in place of divisions
615 * modulos and multiplications.
619 * This macro works for 40 bit offset support in ufs because
620 * this calculates offset in the block and therefore no loss of
621 * information while casting to int.
624 #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
625 ((int)((loc) & ~(fs)->fs_bmask))
628 * This macro works for 40 bit offset support similar to blkoff
631 #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
632 ((int)((loc) & ~(fs)->fs_fmask))
635 * The cast to int32_t does not result in any loss of information because
636 * the number of logical blocks in the file system is limited to
637 * what fits in an int32_t anyway.
640 #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
641 ((int32_t)((loc) >> (fs)->fs_bshift))
644 * The same argument as above applies here.
647 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
648 ((int32_t)((loc) >> (fs)->fs_fshift))
651 * Size can be a 64-bit value and therefore we sign extend fs_bmask
652 * to a 64-bit value too so that the higher 32 bits are masked
653 * properly. Note that the type of fs_bmask has to be signed. Otherwise
654 * compiler will set the higher 32 bits as zero and we don't want
655 * this to happen.
658 #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
659 (((size) + (fs)->fs_bsize - 1) & (offset_t)(fs)->fs_bmask)
662 * Same argument as above.
665 #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
666 (((size) + (fs)->fs_fsize - 1) & (offset_t)(fs)->fs_fmask)
669 * frags cannot exceed 32-bit value since we only support 40bit sizes.
672 #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
673 ((frags) >> (fs)->fs_fragshift)
675 #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
676 ((blks) << (fs)->fs_fragshift)
678 #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
679 ((fsb) & ((fs)->fs_frag - 1))
681 #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
682 ((fsb) &~ ((fs)->fs_frag - 1))
685 * Determine the number of available frags given a
686 * percentage to hold in reserve
688 #define freespace(fs, ufsvfsp) \
689 ((blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
690 (fs)->fs_cstotal.cs_nffree) - (ufsvfsp)->vfs_minfrags)
693 * Determining the size of a file block in the file system.
696 #define blksize(fs, ip, lbn) \
697 (((lbn) >= NDADDR || \
698 (ip)->i_size >= (offset_t)((lbn) + 1) << (fs)->fs_bshift) \
699 ? (fs)->fs_bsize \
700 : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
702 #define dblksize(fs, dip, lbn) \
703 (((lbn) >= NDADDR || \
704 (dip)->di_size >= (offset_t)((lbn) + 1) << (fs)->fs_bshift) \
705 ? (fs)->fs_bsize \
706 : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
709 * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
711 #define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
712 #define NSPF(fs) ((fs)->fs_nspf)
715 * INOPB is the number of inodes in a secondary storage block.
717 #define INOPB(fs) ((fs)->fs_inopb)
718 #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
721 * NINDIR is the number of indirects in a file system block.
723 #define NINDIR(fs) ((fs)->fs_nindir)
726 * bit map related macros
728 #define bitloc(a, i) ((a)[(i)/NBBY])
729 #define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
730 #define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
731 #define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
732 #define isclr(a, i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
734 #define getfs(vfsp) \
735 ((struct fs *)((struct ufsvfs *)vfsp->vfs_data)->vfs_bufp->b_un.b_addr)
737 #define RETRY_LOCK_DELAY 1
740 * Macros to test and acquire i_rwlock:
741 * some vnops hold the target directory's i_rwlock after calling
742 * ufs_lockfs_begin but in many other operations (like ufs_readdir)
743 * fop_rwlock is explicitly called by the filesystem independent code before
744 * calling the file system operation. In these cases the order is reversed
745 * (i.e i_rwlock is taken first and then ufs_lockfs_begin is called). This
746 * is fine as long as ufs_lockfs_begin acts as a VOP counter but with
747 * ufs_quiesce setting the SLOCK bit this becomes a synchronizing
748 * object which might lead to a deadlock. So we use rw_tryenter instead of
749 * rw_enter. If we fail to get this lock and find that SLOCK bit is set, we
750 * call ufs_lockfs_end and restart the operation.
753 #define ufs_tryirwlock(ulp, lock, mode) \
754 ({ \
755 int ret = 0; \
756 while (!rw_tryenter(lock, mode)) { \
757 if (ulp && ULOCKFS_IS_SLOCK(ulp)) { \
758 ret = 1; \
759 break; \
761 delay(RETRY_LOCK_DELAY); \
763 ret; \
767 * The macro ufs_tryirwlock_trans is used in functions which call
768 * TRANS_BEGIN_CSYNC and ufs_lockfs_begin, hence the need to call
769 * TRANS_END_CSYNC and ufs_lockfs_end.
772 #define ufs_tryirwlock_trans(ulp, lock, mode, transmode, ufsvfsp, error, \
773 issync, trans_size) \
774 ({ \
775 int ret = 0; \
776 while (!rw_tryenter(lock, mode)) { \
777 if (ulp && ULOCKFS_IS_SLOCK(ulp)) { \
778 TRANS_END_CSYNC(ufsvfsp, error, issync, \
779 transmode, trans_size); \
780 ufs_lockfs_end(ulp); \
781 ret = 1; \
782 break; \
784 delay(RETRY_LOCK_DELAY); \
786 ret; \
789 #ifdef __cplusplus
791 #endif
793 #endif /* _SYS_FS_UFS_FS_H */