FreeBSD/Linux Kernel Cross Reference
sys/ufs/ffs/fs.h
1 /* $OpenBSD: fs.h,v 1.44 2022/01/11 03:13:59 jsg Exp $ */
2 /* $NetBSD: fs.h,v 1.6 1995/04/12 21:21:02 mycroft Exp $ */
3
4 /*
5 * Copyright (c) 1982, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)fs.h 8.10 (Berkeley) 10/27/94
33 */
34
35 /*
36 * Each disk drive contains some number of file systems.
37 * A file system consists of a number of cylinder groups.
38 * Each cylinder group has inodes and data.
39 *
40 * A file system is described by its super-block, which in turn
41 * describes the cylinder groups. The super-block is critical
42 * data and is replicated in each cylinder group to protect against
43 * catastrophic loss. This is done at `newfs' time and the critical
44 * super-block data does not change, so the copies need not be
45 * referenced further unless disaster strikes.
46 *
47 * For file system fs, the offsets of the various blocks of interest
48 * are given in the super block as:
49 * [fs->fs_sblkno] Super-block
50 * [fs->fs_cblkno] Cylinder group block
51 * [fs->fs_iblkno] Inode blocks
52 * [fs->fs_dblkno] Data blocks
53 * The beginning of cylinder group cg in fs, is given by
54 * the ``cgbase(fs, cg)'' macro.
55 *
56 * The first boot and super blocks are given in absolute disk addresses.
57 * The byte-offset forms are preferred, as they don't imply a sector size.
58 */
59 #define BBSIZE 8192
60 #define SBSIZE 8192
61 #define BBOFF ((off_t)(0))
62 #define SBOFF ((off_t)(BBOFF + BBSIZE))
63 #define BBLOCK ((daddr_t)(0))
64 #define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
65 #define SBLOCK_UFS1 8192
66 #define SBLOCK_UFS2 65536
67 #define SBLOCK_PIGGY 262144
68 #define SBLOCKSIZE 8192
69 #define SBLOCKSEARCH \
70 { SBLOCK_UFS2, SBLOCK_UFS1, SBLOCK_PIGGY, -1 }
71
72 /*
73 * Addresses stored in inodes are capable of addressing fragments
74 * of `blocks'. File system blocks of at most size MAXBSIZE can
75 * be optionally broken into 2, 4, or 8 pieces, each of which is
76 * addressable; these pieces may be DEV_BSIZE, or some multiple of
77 * a DEV_BSIZE unit.
78 *
79 * Large files consist of exclusively large data blocks. To avoid
80 * undue wasted disk space, the last data block of a small file may be
81 * allocated as only as many fragments of a large block as are
82 * necessary. The file system format retains only a single pointer
83 * to such a fragment, which is a piece of a single large block that
84 * has been divided. The size of such a fragment is determinable from
85 * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
86 *
87 * The file system records space availability at the fragment level;
88 * to determine block availability, aligned fragments are examined.
89 */
90
91 #define MAXFRAG 8
92
93 /*
94 * MINBSIZE is the smallest allowable block size.
95 * In order to insure that it is possible to create files of size
96 * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
97 * MINBSIZE must be big enough to hold a cylinder group block,
98 * thus changes to (struct cg) must keep its size within MINBSIZE.
99 * Note that super blocks are always of size SBSIZE,
100 * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
101 */
102 #define MINBSIZE 4096
103
104 /*
105 * The path name on which the file system is mounted is maintained
106 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
107 * the super block for this name.
108 */
109 #define MAXMNTLEN 468
110
111 /*
112 * The volume name for this file system is kept in fs_volname.
113 * MAXVOLLEN defines the length of the buffer allocated.
114 */
115 #define MAXVOLLEN 32
116
117 /*
118 * There is a 128-byte region in the superblock reserved for in-core
119 * pointers to summary information. Originally this included an array
120 * of pointers to blocks of struct csum; now there are just three
121 * pointers and the remaining space is padded with fs_ocsp[].
122 *
123 * NOCSPTRS determines the size of this padding. One pointer (fs_csp)
124 * is taken away to point to a contiguous array of struct csum for
125 * all cylinder groups; a second (fs_maxcluster) points to an array
126 * of cluster sizes that is computed as cylinder groups are inspected,
127 * and the third points to an array that tracks the creation of new
128 * directories.
129 */
130 #define NOCSPTRS ((128 / sizeof(void *)) - 4)
131
132 /*
133 * A summary of contiguous blocks of various sizes is maintained
134 * in each cylinder group. Normally this is set by the initial
135 * value of fs_maxcontig. To conserve space, a maximum summary size
136 * is set by FS_MAXCONTIG.
137 */
138 #define FS_MAXCONTIG 16
139
140 /*
141 * MINFREE gives the minimum acceptable percentage of file system
142 * blocks which may be free. If the freelist drops below this level
143 * only the superuser may continue to allocate blocks. This may
144 * be set to 0 if no reserve of free blocks is deemed necessary,
145 * however throughput drops by fifty percent if the file system
146 * is run at between 95% and 100% full; thus the minimum default
147 * value of fs_minfree is 5%. However, to get good clustering
148 * performance, 10% is a better choice. With 5% free space,
149 * fragmentation is not a problem, so we choose to optimize for time.
150 */
151 #define MINFREE 5
152 #define DEFAULTOPT FS_OPTTIME
153
154 /*
155 * The directory preference algorithm(dirpref) can be tuned by adjusting
156 * the following parameters which tell the system the average file size
157 * and the average number of files per directory. These defaults are well
158 * selected for typical filesystems, but may need to be tuned for odd
159 * cases like filesystems being used for squid caches or news spools.
160 */
161 #define AVFILESIZ 16384 /* expected average file size */
162 #define AFPDIR 64 /* expected number of files per directory */
163
164 /*
165 * Size of superblock space reserved for snapshots.
166 */
167 #define FSMAXSNAP 20
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 struct csum {
176 int32_t cs_ndir; /* number of directories */
177 int32_t cs_nbfree; /* number of free blocks */
178 int32_t cs_nifree; /* number of free inodes */
179 int32_t cs_nffree; /* number of free frags */
180 };
181
182 struct csum_total {
183 int64_t cs_ndir; /* number of directories */
184 int64_t cs_nbfree; /* number of free blocks */
185 int64_t cs_nifree; /* number of free inodes */
186 int64_t cs_nffree; /* number of free frags */
187 int64_t cs_spare[4]; /* future expansion */
188 };
189
190 /*
191 * Super block for an FFS file system.
192 */
193 struct fs {
194 int32_t fs_firstfield; /* historic file system linked list, */
195 int32_t fs_unused_1; /* used for incore super blocks */
196 int32_t fs_sblkno; /* addr of super-block / frags */
197 int32_t fs_cblkno; /* offset of cyl-block / frags */
198 int32_t fs_iblkno; /* offset of inode-blocks / frags */
199 int32_t fs_dblkno; /* offset of first data / frags */
200 int32_t fs_cgoffset; /* cylinder group offset in cylinder */
201 int32_t fs_cgmask; /* used to calc mod fs_ntrak */
202 int32_t fs_ffs1_time; /* last time written */
203 int32_t fs_ffs1_size; /* # of blocks in fs / frags */
204 int32_t fs_ffs1_dsize; /* # of data blocks in fs */
205 u_int32_t fs_ncg; /* # of cylinder groups */
206 int32_t fs_bsize; /* size of basic blocks / bytes */
207 int32_t fs_fsize; /* size of frag blocks / bytes */
208 int32_t fs_frag; /* # of frags in a block in fs */
209 /* these are configuration parameters */
210 int32_t fs_minfree; /* minimum percentage of free blocks */
211 int32_t fs_rotdelay; /* # of ms for optimal next block */
212 int32_t fs_rps; /* disk revolutions per second */
213 /* these fields can be computed from the others */
214 int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */
215 int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */
216 int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */
217 int32_t fs_fshift; /* ``numfrags'' calc # of frags */
218 /* these are configuration parameters */
219 int32_t fs_maxcontig; /* max # of contiguous blks */
220 int32_t fs_maxbpg; /* max # of blks per cyl group */
221 /* these fields can be computed from the others */
222 int32_t fs_fragshift; /* block to frag shift */
223 int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
224 int32_t fs_sbsize; /* actual size of super block */
225 int32_t fs_csmask; /* csum block offset (now unused) */
226 int32_t fs_csshift; /* csum block number (now unused) */
227 int32_t fs_nindir; /* value of NINDIR */
228 u_int32_t fs_inopb; /* inodes per file system block */
229 int32_t fs_nspf; /* DEV_BSIZE sectors per frag */
230 /* yet another configuration parameter */
231 int32_t fs_optim; /* optimization preference, see below */
232 /* these fields are derived from the hardware */
233 int32_t fs_npsect; /* DEV_BSIZE sectors/track + spares */
234 int32_t fs_interleave; /* DEV_BSIZE sector interleave */
235 int32_t fs_trackskew; /* sector 0 skew, per track */
236 /* fs_id takes the space of the unused fs_headswitch and fs_trkseek fields */
237 int32_t fs_id[2]; /* unique filesystem id */
238 /* sizes determined by number of cylinder groups and their sizes */
239 int32_t fs_ffs1_csaddr; /* blk addr of cyl grp summary area */
240 int32_t fs_cssize; /* cyl grp summary area size / bytes */
241 int32_t fs_cgsize; /* cyl grp block size / bytes */
242 /* these fields are derived from the hardware */
243 int32_t fs_ntrak; /* tracks per cylinder */
244 int32_t fs_nsect; /* DEV_BSIZE sectors per track */
245 int32_t fs_spc; /* DEV_BSIZE sectors per cylinder */
246 /* this comes from the disk driver partitioning */
247 int32_t fs_ncyl; /* cylinders in file system */
248 /* these fields can be computed from the others */
249 int32_t fs_cpg; /* cylinders per group */
250 u_int32_t fs_ipg; /* inodes per group */
251 int32_t fs_fpg; /* blocks per group * fs_frag */
252 /* this data must be re-computed after crashes */
253 struct csum fs_ffs1_cstotal; /* cylinder summary information */
254 /* these fields are cleared at mount time */
255 int8_t fs_fmod; /* super block modified flag */
256 int8_t fs_clean; /* file system is clean flag */
257 int8_t fs_ronly; /* mounted read-only flag */
258 int8_t fs_ffs1_flags; /* see FS_ below */
259 u_char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
260 u_char fs_volname[MAXVOLLEN]; /* volume name */
261 u_int64_t fs_swuid; /* system-wide uid */
262 int32_t fs_pad; /* due to alignment of fs_swuid */
263 /* these fields retain the current block allocation info */
264 int32_t fs_cgrotor; /* last cg searched */
265 void *fs_ocsp[NOCSPTRS]; /* padding; was list of fs_cs buffers */
266 u_int8_t *fs_contigdirs; /* # of contiguously allocated dirs */
267 struct csum *fs_csp; /* cg summary info buffer for fs_cs */
268 int32_t *fs_maxcluster; /* max cluster in each cyl group */
269 u_char *fs_active; /* reserved for snapshots */
270 int32_t fs_cpc; /* cyl per cycle in postbl */
271 /* this area is only allocated if fs_ffs1_flags & FS_FLAGS_UPDATED */
272 int32_t fs_maxbsize; /* maximum blocking factor permitted */
273 int64_t fs_spareconf64[17]; /* old rotation block list head */
274 int64_t fs_sblockloc; /* offset of standard super block */
275 struct csum_total fs_cstotal; /* cylinder summary information */
276 int64_t fs_time; /* time last written */
277 int64_t fs_size; /* number of blocks in fs */
278 int64_t fs_dsize; /* number of data blocks in fs */
279 int64_t fs_csaddr; /* blk addr of cyl grp summary area */
280 int64_t fs_pendingblocks; /* blocks in process of being freed */
281 u_int32_t fs_pendinginodes; /* inodes in process of being freed */
282 u_int32_t fs_snapinum[FSMAXSNAP];/* space reserved for snapshots */
283 /* back to stuff that has been around a while */
284 u_int32_t fs_avgfilesize; /* expected average file size */
285 u_int32_t fs_avgfpdir; /* expected # of files per directory */
286 int32_t fs_sparecon[26]; /* reserved for future constants */
287 u_int32_t fs_flags; /* see FS_ flags below */
288 int32_t fs_fscktime; /* last time fsck(8)ed */
289 int32_t fs_contigsumsize; /* size of cluster summary array */
290 int32_t fs_maxsymlinklen; /* max length of an internal symlink */
291 int32_t fs_inodefmt; /* format of on-disk inodes */
292 u_int64_t fs_maxfilesize; /* maximum representable file size */
293 int64_t fs_qbmask; /* ~fs_bmask - for use with quad size */
294 int64_t fs_qfmask; /* ~fs_fmask - for use with quad size */
295 int32_t fs_state; /* validate fs_clean field */
296 int32_t fs_postblformat; /* format of positional layout tables */
297 int32_t fs_nrpos; /* number of rotational positions */
298 int32_t fs_postbloff; /* (u_int16) rotation block list head */
299 int32_t fs_rotbloff; /* (u_int8) blocks for each rotation */
300 int32_t fs_magic; /* magic number */
301 u_int8_t fs_space[1]; /* list of blocks for each rotation */
302 /* actually longer */
303 };
304
305 /*
306 * Filesystem identification
307 */
308 #define FS_MAGIC 0x011954 /* the fast filesystem magic number */
309 #define FS_UFS1_MAGIC 0x011954 /* the fast filesystem magic number */
310 #define FS_UFS2_MAGIC 0x19540119 /* UFS fast filesystem magic number */
311 #define FS_OKAY 0x7c269d38 /* superblock checksum */
312 #define FS_42INODEFMT -1 /* 4.2BSD inode format */
313 #define FS_44INODEFMT 2 /* 4.4BSD inode format */
314
315 /*
316 * Filesystem clean flags
317 */
318 #define FS_ISCLEAN 0x01
319 #define FS_WASCLEAN 0x02
320
321 /*
322 * Preference for optimization.
323 */
324 #define FS_OPTTIME 0 /* minimize allocation time */
325 #define FS_OPTSPACE 1 /* minimize disk fragmentation */
326
327 /*
328 * Filesystem flags.
329 */
330 #define FS_UNCLEAN 0x01 /* filesystem not clean at mount */
331 #define FS_DOSOFTDEP 0x02 /* filesystem using soft dependencies */
332 /*
333 * The following flag is used to detect a FFS1 file system that had its flags
334 * moved to the new (FFS2) location for compatibility.
335 */
336 #define FS_FLAGS_UPDATED 0x80 /* file system has FFS2-like flags */
337
338 /*
339 * Rotational layout table format types
340 */
341 #define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */
342 #define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */
343 /*
344 * Macros for access to superblock array structures
345 */
346 #define fs_rotbl(fs) \
347 (((fs)->fs_postblformat == FS_42POSTBLFMT) \
348 ? ((fs)->fs_space) \
349 : ((u_int8_t *)((u_int8_t *)(fs) + (fs)->fs_rotbloff)))
350
351 /*
352 * The size of a cylinder group is calculated by CGSIZE. The maximum size
353 * is limited by the fact that cylinder groups are at most one block.
354 * Its size is derived from the size of the maps maintained in the
355 * cylinder group and the (struct cg) size.
356 */
357 #define CGSIZE(fs) \
358 /* base cg */ (sizeof(struct cg) + sizeof(int32_t) + \
359 /* blktot size */ (fs)->fs_cpg * sizeof(int32_t) + \
360 /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(int16_t) + \
361 /* inode map */ howmany((fs)->fs_ipg, NBBY) + \
362 /* block map */ howmany((fs)->fs_fpg, NBBY) + \
363 /* if present */ ((fs)->fs_contigsumsize <= 0 ? 0 : \
364 /* cluster sum */ (fs)->fs_contigsumsize * sizeof(int32_t) + \
365 /* cluster map */ howmany(fragstoblks(fs, (fs)->fs_fpg), NBBY)))
366
367 /*
368 * Convert cylinder group to base address of its global summary info.
369 */
370 #define fs_cs(fs, indx) fs_csp[indx]
371
372 /*
373 * Cylinder group block for a file system.
374 */
375 #define CG_MAGIC 0x090255
376 struct cg {
377 int32_t cg_firstfield; /* historic cyl groups linked list */
378 int32_t cg_magic; /* magic number */
379 int32_t cg_time; /* time last written */
380 u_int32_t cg_cgx; /* we are the cgx'th cylinder group */
381 int16_t cg_ncyl; /* number of cyl's this cg */
382 int16_t cg_niblk; /* number of inode blocks this cg */
383 u_int32_t cg_ndblk; /* number of data blocks this cg */
384 struct csum cg_cs; /* cylinder summary information */
385 u_int32_t cg_rotor; /* position of last used block */
386 u_int32_t cg_frotor; /* position of last used frag */
387 u_int32_t cg_irotor; /* position of last used inode */
388 u_int32_t cg_frsum[MAXFRAG]; /* counts of available frags */
389 int32_t cg_btotoff; /* (int32) block totals per cylinder */
390 int32_t cg_boff; /* (u_int16) free block positions */
391 u_int32_t cg_iusedoff; /* (u_int8) used inode map */
392 u_int32_t cg_freeoff; /* (u_int8) free block map */
393 u_int32_t cg_nextfreeoff; /* (u_int8) next available space */
394 u_int32_t cg_clustersumoff; /* (u_int32) counts of avail clusters */
395 u_int32_t cg_clusteroff; /* (u_int8) free cluster map */
396 u_int32_t cg_nclusterblks; /* number of clusters this cg */
397 u_int32_t cg_ffs2_niblk; /* number of inode blocks this cg */
398 u_int32_t cg_initediblk; /* last initialized inode */
399 int32_t cg_sparecon32[3]; /* reserved for future use */
400 int64_t cg_ffs2_time; /* time last written */
401 int64_t cg_sparecon64[3]; /* reserved for future use */
402 /* actually longer */
403 };
404
405 /*
406 * Macros for access to cylinder group array structures
407 */
408 #define cg_blktot(cgp) \
409 (((cgp)->cg_magic != CG_MAGIC) \
410 ? (((struct ocg *)(cgp))->cg_btot) \
411 : ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_btotoff)))
412 #define cg_blks(fs, cgp, cylno) \
413 (((cgp)->cg_magic != CG_MAGIC) \
414 ? (((struct ocg *)(cgp))->cg_b[cylno]) \
415 : ((int16_t *)((u_int8_t *)(cgp) + \
416 (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
417 #define cg_inosused(cgp) \
418 (((cgp)->cg_magic != CG_MAGIC) \
419 ? (((struct ocg *)(cgp))->cg_iused) \
420 : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_iusedoff)))
421 #define cg_blksfree(cgp) \
422 (((cgp)->cg_magic != CG_MAGIC) \
423 ? (((struct ocg *)(cgp))->cg_free) \
424 : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_freeoff)))
425 #define cg_chkmagic(cgp) \
426 ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
427 #define cg_clustersfree(cgp) \
428 ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_clusteroff))
429 #define cg_clustersum(cgp) \
430 ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_clustersumoff))
431
432 /*
433 * The following structure is defined
434 * for compatibility with old file systems.
435 */
436 struct ocg {
437 int32_t cg_firstfield; /* historic linked list of cyl groups */
438 int32_t cg_unused_1; /* used for incore cyl groups */
439 int32_t cg_time; /* time last written */
440 int32_t cg_cgx; /* we are the cgx'th cylinder group */
441 int16_t cg_ncyl; /* number of cyl's this cg */
442 int16_t cg_niblk; /* number of inode blocks this cg */
443 int32_t cg_ndblk; /* number of data blocks this cg */
444 struct csum cg_cs; /* cylinder summary information */
445 int32_t cg_rotor; /* position of last used block */
446 int32_t cg_frotor; /* position of last used frag */
447 int32_t cg_irotor; /* position of last used inode */
448 int32_t cg_frsum[8]; /* counts of available frags */
449 int32_t cg_btot[32]; /* block totals per cylinder */
450 int16_t cg_b[32][8]; /* positions of free blocks */
451 u_int8_t cg_iused[256]; /* used inode map */
452 int32_t cg_magic; /* magic number */
453 u_int8_t cg_free[1]; /* free block map */
454 /* actually longer */
455 };
456
457 /*
458 * Turn file system block numbers into disk block addresses.
459 * This maps file system blocks to DEV_BSIZE (a.k.a. 512-byte) size disk
460 * blocks.
461 */
462 #define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb)
463 #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
464
465 /*
466 * Cylinder group macros to locate things in cylinder groups.
467 * They calc file system addresses of cylinder group data structures.
468 */
469 #define cgbase(fs, c) ((daddr_t)(fs)->fs_fpg * (c))
470 #define cgdata(fs, c) (cgdmin(fs, c) + (fs)->fs_minfree) /* data zone */
471 #define cgmeta(fs, c) (cgdmin(fs, c)) /* meta data */
472 #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
473 #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
474 #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
475 #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
476 #define cgstart(fs, c) \
477 (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
478
479 /*
480 * Macros for handling inode numbers:
481 * inode number to file system block offset.
482 * inode number to cylinder group number.
483 * inode number to file system block address.
484 */
485 #define ino_to_cg(fs, x) ((x) / (fs)->fs_ipg)
486 #define ino_to_fsba(fs, x) \
487 ((daddr_t)(cgimin(fs, ino_to_cg(fs, x)) + \
488 (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
489 #define ino_to_fsbo(fs, x) ((x) % INOPB(fs))
490
491 /*
492 * Give cylinder group number for a file system block.
493 * Give frag block number in cylinder group for a file system block.
494 */
495 #define dtog(fs, d) ((d) / (fs)->fs_fpg)
496 #define dtogd(fs, d) ((d) % (fs)->fs_fpg)
497
498 /*
499 * Extract the bits for a block from a map.
500 * Compute the cylinder and rotational position of a cyl block addr.
501 */
502 #define blkmap(fs, map, loc) \
503 (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
504 #define cbtocylno(fs, bno) \
505 (fsbtodb(fs, bno) / (fs)->fs_spc)
506 #define cbtorpos(fs, bno) \
507 ((fs)->fs_nrpos <= 1 ? 0 : \
508 (fsbtodb(fs, bno) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
509 fsbtodb(fs, bno) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
510 (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
511
512 /*
513 * The following macros optimize certain frequently calculated
514 * quantities by using shifts and masks in place of divisions
515 * modulos and multiplications.
516 */
517 #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
518 ((loc) & (fs)->fs_qbmask)
519 #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
520 ((loc) & (fs)->fs_qfmask)
521 #define lblktosize(fs, blk) /* calculates ((off_t)blk * fs->fs_bsize) */ \
522 ((off_t)(blk) << (fs)->fs_bshift)
523 #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
524 ((loc) >> (fs)->fs_bshift)
525 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
526 ((loc) >> (fs)->fs_fshift)
527 #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
528 (((size) + (fs)->fs_qbmask) & (fs)->fs_bmask)
529 #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
530 (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask)
531 #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
532 ((frags) >> (fs)->fs_fragshift)
533 #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
534 ((blks) << (fs)->fs_fragshift)
535 #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
536 ((fsb) & ((fs)->fs_frag - 1))
537 #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
538 ((fsb) &~ ((fs)->fs_frag - 1))
539
540 /*
541 * Determine the number of available frags given a
542 * percentage to hold in reserve.
543 */
544 #define freespace(fs, percentreserved) \
545 (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
546 (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
547
548 /*
549 * Determining the size of a file block in the file system.
550 */
551 #define blksize(fs, ip, lbn) \
552 (((lbn) >= NDADDR || DIP((ip), size) >= ((lbn) + 1) << (fs)->fs_bshift) \
553 ? (u_int64_t)(fs)->fs_bsize \
554 : (fragroundup(fs, blkoff(fs, DIP((ip), size)))))
555 #define dblksize(fs, dip, lbn) \
556 (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
557 ? (u_int64_t)(fs)->fs_bsize \
558 : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
559
560 #define sblksize(fs, size, lbn) \
561 (((lbn) >= NDADDR || (size) >= ((lbn) + 1) << (fs)->fs_bshift) \
562 ? (u_int64_t)(fs)->fs_bsize \
563 : (fragroundup(fs, blkoff(fs, (size)))))
564
565
566 /*
567 * Number of disk sectors per block/fragment; assumes DEV_BSIZE byte
568 * sector size.
569 */
570 #define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
571 #define NSPF(fs) ((fs)->fs_nspf)
572
573 /* Number of inodes per file system block (fs->fs_bsize) */
574 #define INOPB(fs) ((fs)->fs_inopb)
575 /* Number of inodes per file system fragment (fs->fs_fsize) */
576 #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
577
578 /*
579 * Number of indirects in a file system block.
580 */
581 #define NINDIR(fs) ((fs)->fs_nindir)
582
583 /* Maximum file size the kernel allows.
584 * Even though ffs can handle files up to 16TB, we do limit the max file
585 * to 2^31 pages to prevent overflow of a 32-bit unsigned int. The buffer
586 * cache has its own checks but a little added paranoia never hurts.
587 */
588 #define FS_KERNMAXFILESIZE(pgsiz, fs) ((u_int64_t)0x80000000 * \
589 MIN((pgsiz), (fs)->fs_bsize) - 1)
590
591 extern const int inside[], around[];
592 extern const u_char *fragtbl[];
Cache object: 56b4d775a45430b6454d1a3e4388f6b6
|