Annotation of sys/ufs/ffs/fs.h, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: fs.h,v 1.32 2007/06/01 07:03:27 otto 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: * addressible; 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: /*
92: * MINBSIZE is the smallest allowable block size.
93: * In order to insure that it is possible to create files of size
94: * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
95: * MINBSIZE must be big enough to hold a cylinder group block,
96: * thus changes to (struct cg) must keep its size within MINBSIZE.
97: * Note that super blocks are always of size SBSIZE,
98: * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
99: */
100: #define MINBSIZE 4096
101:
102: /*
103: * The path name on which the file system is mounted is maintained
104: * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
105: * the super block for this name.
106: */
107: #define MAXMNTLEN 468
108:
109: /*
110: * The volume name for this file system is kept in fs_volname.
111: * MAXVOLLEN defines the length of the buffer allocated.
112: */
113: #define MAXVOLLEN 32
114:
115: /*
116: * There is a 128-byte region in the superblock reserved for in-core
117: * pointers to summary information. Originally this included an array
118: * of pointers to blocks of struct csum; now there are just three
119: * pointers and the remaining space is padded with fs_ocsp[].
120: *
121: * NOCSPTRS determines the size of this padding. One pointer (fs_csp)
122: * is taken away to point to a contiguous array of struct csum for
123: * all cylinder groups; a second (fs_maxcluster) points to an array
124: * of cluster sizes that is computed as cylinder groups are inspected,
125: * and the third points to an array that tracks the creation of new
126: * directories.
127: */
128: #define NOCSPTRS ((128 / sizeof(void *)) - 4)
129:
130: /*
131: * A summary of contiguous blocks of various sizes is maintained
132: * in each cylinder group. Normally this is set by the initial
133: * value of fs_maxcontig. To conserve space, a maximum summary size
134: * is set by FS_MAXCONTIG.
135: */
136: #define FS_MAXCONTIG 16
137:
138: /*
139: * MINFREE gives the minimum acceptable percentage of file system
140: * blocks which may be free. If the freelist drops below this level
141: * only the superuser may continue to allocate blocks. This may
142: * be set to 0 if no reserve of free blocks is deemed necessary,
143: * however throughput drops by fifty percent if the file system
144: * is run at between 95% and 100% full; thus the minimum default
145: * value of fs_minfree is 5%. However, to get good clustering
146: * performance, 10% is a better choice. With 5% free space,
147: * fragmentation is not a problem, so we choose to optimize for time.
148: */
149: #define MINFREE 5
150: #define DEFAULTOPT FS_OPTTIME
151:
152: /*
153: * The directory preference algorithm(dirpref) can be tuned by adjusting
154: * the following parameters which tell the system the average file size
155: * and the average number of files per directory. These defaults are well
156: * selected for typical filesystems, but may need to be tuned for odd
157: * cases like filesystems being used for sqiud caches or news spools.
158: */
159: #define AVFILESIZ 16384 /* expected average file size */
160: #define AFPDIR 64 /* expected number of files per directory */
161:
162: /*
163: * Size of superblock space reserved for snapshots.
164: */
165: #define FSMAXSNAP 20
166:
167: /*
168: * Per cylinder group information; summarized in blocks allocated
169: * from first cylinder group data blocks. These blocks have to be
170: * read in from fs_csaddr (size fs_cssize) in addition to the
171: * super block.
172: */
173: struct csum {
174: int32_t cs_ndir; /* number of directories */
175: int32_t cs_nbfree; /* number of free blocks */
176: int32_t cs_nifree; /* number of free inodes */
177: int32_t cs_nffree; /* number of free frags */
178: };
179:
180: struct csum_total {
181: int64_t cs_ndir; /* number of directories */
182: int64_t cs_nbfree; /* number of free blocks */
183: int64_t cs_nifree; /* number of free inodes */
184: int64_t cs_nffree; /* number of free frags */
185: int64_t cs_spare[4]; /* future expansion */
186: };
187:
188: /*
189: * Super block for an FFS file system.
190: */
191: struct fs {
192: int32_t fs_firstfield; /* historic file system linked list, */
193: int32_t fs_unused_1; /* used for incore super blocks */
194: int32_t fs_sblkno; /* addr of super-block / frags */
195: int32_t fs_cblkno; /* offset of cyl-block / frags */
196: int32_t fs_iblkno; /* offset of inode-blocks / frags */
197: int32_t fs_dblkno; /* offset of first data / frags */
198: int32_t fs_cgoffset; /* cylinder group offset in cylinder */
199: int32_t fs_cgmask; /* used to calc mod fs_ntrak */
200: int32_t fs_ffs1_time; /* last time written */
201: int32_t fs_ffs1_size; /* # of blocks in fs / frags */
202: int32_t fs_ffs1_dsize; /* # of data blocks in fs */
203: int32_t fs_ncg; /* # of cylinder groups */
204: int32_t fs_bsize; /* size of basic blocks / bytes */
205: int32_t fs_fsize; /* size of frag blocks / bytes */
206: int32_t fs_frag; /* # of frags in a block in fs */
207: /* these are configuration parameters */
208: int32_t fs_minfree; /* minimum percentage of free blocks */
209: int32_t fs_rotdelay; /* # of ms for optimal next block */
210: int32_t fs_rps; /* disk revolutions per second */
211: /* these fields can be computed from the others */
212: int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */
213: int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */
214: int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */
215: int32_t fs_fshift; /* ``numfrags'' calc # of frags */
216: /* these are configuration parameters */
217: int32_t fs_maxcontig; /* max # of contiguous blks */
218: int32_t fs_maxbpg; /* max # of blks per cyl group */
219: /* these fields can be computed from the others */
220: int32_t fs_fragshift; /* block to frag shift */
221: int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
222: int32_t fs_sbsize; /* actual size of super block */
223: int32_t fs_csmask; /* csum block offset (now unused) */
224: int32_t fs_csshift; /* csum block number (now unused) */
225: int32_t fs_nindir; /* value of NINDIR */
226: int32_t fs_inopb; /* inodes per file system block */
227: int32_t fs_nspf; /* value of NSPF */
228: /* yet another configuration parameter */
229: int32_t fs_optim; /* optimization preference, see below */
230: /* these fields are derived from the hardware */
231: int32_t fs_npsect; /* # sectors/track including spares */
232: int32_t fs_interleave; /* hardware sector interleave */
233: int32_t fs_trackskew; /* sector 0 skew, per track */
234: /* fs_id takes the space of the unused fs_headswitch and fs_trkseek fields */
235: int32_t fs_id[2]; /* unique filesystem id */
236: /* sizes determined by number of cylinder groups and their sizes */
237: int32_t fs_ffs1_csaddr; /* blk addr of cyl grp summary area */
238: int32_t fs_cssize; /* cyl grp summary area size / bytes */
239: int32_t fs_cgsize; /* cyl grp block size / bytes */
240: /* these fields are derived from the hardware */
241: int32_t fs_ntrak; /* tracks per cylinder */
242: int32_t fs_nsect; /* sectors per track */
243: int32_t fs_spc; /* sectors per cylinder */
244: /* this comes from the disk driver partitioning */
245: int32_t fs_ncyl; /* cylinders in file system */
246: /* these fields can be computed from the others */
247: int32_t fs_cpg; /* cylinders per group */
248: int32_t fs_ipg; /* inodes per group */
249: int32_t fs_fpg; /* blocks per group * fs_frag */
250: /* this data must be re-computed after crashes */
251: struct csum fs_ffs1_cstotal; /* cylinder summary information */
252: /* these fields are cleared at mount time */
253: int8_t fs_fmod; /* super block modified flag */
254: int8_t fs_clean; /* file system is clean flag */
255: int8_t fs_ronly; /* mounted read-only flag */
256: int8_t fs_ffs1_flags; /* see FS_ below */
257: u_char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
258: u_char fs_volname[MAXVOLLEN]; /* volume name */
259: u_int64_t fs_swuid; /* system-wide uid */
260: int32_t fs_pad; /* due to alignment of fs_swuid */
261: /* these fields retain the current block allocation info */
262: int32_t fs_cgrotor; /* last cg searched */
263: void *fs_ocsp[NOCSPTRS]; /* padding; was list of fs_cs buffers */
264: u_int8_t *fs_contigdirs; /* # of contiguously allocated dirs */
265: struct csum *fs_csp; /* cg summary info buffer for fs_cs */
266: int32_t *fs_maxcluster; /* max cluster in each cyl group */
267: u_char *fs_active; /* reserved for snapshots */
268: int32_t fs_cpc; /* cyl per cycle in postbl */
269: /* this area is only allocated if fs_ffs1_flags & FS_FLAGS_UPDATED */
270: int32_t fs_maxbsize; /* maximum blocking factor permitted */
271: int64_t fs_spareconf64[17]; /* old rotation block list head */
272: int64_t fs_sblockloc; /* offset of standard super block */
273: struct csum_total fs_cstotal; /* cylinder summary information */
274: int64_t fs_time; /* time last written */
275: int64_t fs_size; /* number of blocks in fs */
276: int64_t fs_dsize; /* number of data blocks in fs */
277: int64_t fs_csaddr; /* blk addr of cyl grp summary area */
278: int64_t fs_pendingblocks; /* blocks in process of being freed */
279: int32_t fs_pendinginodes; /* inodes in process of being freed */
280: int32_t fs_snapinum[FSMAXSNAP];/* space reserved for snapshots */
281: /* back to stuff that has been around a while */
282: int32_t fs_avgfilesize; /* expected average file size */
283: int32_t fs_avgfpdir; /* expected # of files per directory */
284: int32_t fs_sparecon[26]; /* reserved for future constants */
285: u_int32_t fs_flags; /* see FS_ flags below */
286: int32_t fs_fscktime; /* last time fsck(8)ed */
287: int32_t fs_contigsumsize; /* size of cluster summary array */
288: int32_t fs_maxsymlinklen; /* max length of an internal symlink */
289: int32_t fs_inodefmt; /* format of on-disk inodes */
290: u_int64_t fs_maxfilesize; /* maximum representable file size */
291: int64_t fs_qbmask; /* ~fs_bmask - for use with quad size */
292: int64_t fs_qfmask; /* ~fs_fmask - for use with quad size */
293: int32_t fs_state; /* validate fs_clean field */
294: int32_t fs_postblformat; /* format of positional layout tables */
295: int32_t fs_nrpos; /* number of rotational positions */
296: int32_t fs_postbloff; /* (u_int16) rotation block list head */
297: int32_t fs_rotbloff; /* (u_int8) blocks for each rotation */
298: int32_t fs_magic; /* magic number */
299: u_int8_t fs_space[1]; /* list of blocks for each rotation */
300: /* actually longer */
301: };
302:
303: /*
304: * Filesystem identification
305: */
306: #define FS_MAGIC 0x011954 /* the fast filesystem magic number */
307: #define FS_UFS1_MAGIC 0x011954 /* the fast filesystem magic number */
308: #define FS_UFS2_MAGIC 0x19540119 /* UFS fast filesystem magic number */
309: #define FS_OKAY 0x7c269d38 /* superblock checksum */
310: #define FS_42INODEFMT -1 /* 4.2BSD inode format */
311: #define FS_44INODEFMT 2 /* 4.4BSD inode format */
312:
313: /*
314: * Filesystem clean flags
315: */
316: #define FS_ISCLEAN 0x01
317: #define FS_WASCLEAN 0x02
318:
319: /*
320: * Preference for optimization.
321: */
322: #define FS_OPTTIME 0 /* minimize allocation time */
323: #define FS_OPTSPACE 1 /* minimize disk fragmentation */
324:
325: /*
326: * Filesystem flags.
327: */
328: #define FS_UNCLEAN 0x01 /* filesystem not clean at mount */
329: #define FS_DOSOFTDEP 0x02 /* filesystem using soft dependencies */
330: /*
331: * The following flag is used to detect a FFS1 file system that had its flags
332: * moved to the new (FFS2) location for compatibility.
333: */
334: #define FS_FLAGS_UPDATED 0x80 /* file system has FFS2-like flags */
335:
336: /*
337: * Rotational layout table format types
338: */
339: #define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */
340: #define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */
341: /*
342: * Macros for access to superblock array structures
343: */
344: #define fs_rotbl(fs) \
345: (((fs)->fs_postblformat == FS_42POSTBLFMT) \
346: ? ((fs)->fs_space) \
347: : ((u_int8_t *)((u_int8_t *)(fs) + (fs)->fs_rotbloff)))
348:
349: /*
350: * The size of a cylinder group is calculated by CGSIZE. The maximum size
351: * is limited by the fact that cylinder groups are at most one block.
352: * Its size is derived from the size of the maps maintained in the
353: * cylinder group and the (struct cg) size.
354: */
355: #define CGSIZE(fs) \
356: /* base cg */ (sizeof(struct cg) + sizeof(int32_t) + \
357: /* blktot size */ (fs)->fs_cpg * sizeof(int32_t) + \
358: /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(int16_t) + \
359: /* inode map */ howmany((fs)->fs_ipg, NBBY) + \
360: /* block map */ howmany((fs)->fs_fpg, NBBY) + \
361: /* if present */ ((fs)->fs_contigsumsize <= 0 ? 0 : \
362: /* cluster sum */ (fs)->fs_contigsumsize * sizeof(int32_t) + \
363: /* cluster map */ howmany(fragstoblks(fs, (fs)->fs_fpg), NBBY)))
364:
365: /*
366: * Convert cylinder group to base address of its global summary info.
367: */
368: #define fs_cs(fs, indx) fs_csp[indx]
369:
370: /*
371: * Cylinder group block for a file system.
372: */
373: #define CG_MAGIC 0x090255
374: struct cg {
375: int32_t cg_firstfield; /* historic cyl groups linked list */
376: int32_t cg_magic; /* magic number */
377: int32_t cg_time; /* time last written */
378: int32_t cg_cgx; /* we are the cgx'th cylinder group */
379: int16_t cg_ncyl; /* number of cyl's this cg */
380: int16_t cg_niblk; /* number of inode blocks this cg */
381: int32_t cg_ndblk; /* number of data blocks this cg */
382: struct csum cg_cs; /* cylinder summary information */
383: int32_t cg_rotor; /* position of last used block */
384: int32_t cg_frotor; /* position of last used frag */
385: int32_t cg_irotor; /* position of last used inode */
386: int32_t cg_frsum[MAXFRAG]; /* counts of available frags */
387: int32_t cg_btotoff; /* (int32) block totals per cylinder */
388: int32_t cg_boff; /* (u_int16) free block positions */
389: int32_t cg_iusedoff; /* (u_int8) used inode map */
390: int32_t cg_freeoff; /* (u_int8) free block map */
391: int32_t cg_nextfreeoff; /* (u_int8) next available space */
392: int32_t cg_clustersumoff; /* (u_int32) counts of avail clusters */
393: int32_t cg_clusteroff; /* (u_int8) free cluster map */
394: int32_t cg_nclusterblks; /* number of clusters this cg */
395: int32_t cg_ffs2_niblk; /* number of inode blocks this cg */
396: int32_t cg_initediblk; /* last initialized inode */
397: int32_t cg_sparecon32[3]; /* reserved for future use */
398: int64_t cg_ffs2_time; /* time last written */
399: int64_t cg_sparecon64[3]; /* reserved for future use */
400: /* actually longer */
401: };
402:
403: /*
404: * Macros for access to cylinder group array structures
405: */
406: #define cg_blktot(cgp) \
407: (((cgp)->cg_magic != CG_MAGIC) \
408: ? (((struct ocg *)(cgp))->cg_btot) \
409: : ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_btotoff)))
410: #define cg_blks(fs, cgp, cylno) \
411: (((cgp)->cg_magic != CG_MAGIC) \
412: ? (((struct ocg *)(cgp))->cg_b[cylno]) \
413: : ((int16_t *)((u_int8_t *)(cgp) + \
414: (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
415: #define cg_inosused(cgp) \
416: (((cgp)->cg_magic != CG_MAGIC) \
417: ? (((struct ocg *)(cgp))->cg_iused) \
418: : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_iusedoff)))
419: #define cg_blksfree(cgp) \
420: (((cgp)->cg_magic != CG_MAGIC) \
421: ? (((struct ocg *)(cgp))->cg_free) \
422: : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_freeoff)))
423: #define cg_chkmagic(cgp) \
424: ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
425: #define cg_clustersfree(cgp) \
426: ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_clusteroff))
427: #define cg_clustersum(cgp) \
428: ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_clustersumoff))
429:
430: /*
431: * The following structure is defined
432: * for compatibility with old file systems.
433: */
434: struct ocg {
435: int32_t cg_firstfield; /* historic linked list of cyl groups */
436: int32_t cg_unused_1; /* used for incore cyl groups */
437: int32_t cg_time; /* time last written */
438: int32_t cg_cgx; /* we are the cgx'th cylinder group */
439: int16_t cg_ncyl; /* number of cyl's this cg */
440: int16_t cg_niblk; /* number of inode blocks this cg */
441: int32_t cg_ndblk; /* number of data blocks this cg */
442: struct csum cg_cs; /* cylinder summary information */
443: int32_t cg_rotor; /* position of last used block */
444: int32_t cg_frotor; /* position of last used frag */
445: int32_t cg_irotor; /* position of last used inode */
446: int32_t cg_frsum[8]; /* counts of available frags */
447: int32_t cg_btot[32]; /* block totals per cylinder */
448: int16_t cg_b[32][8]; /* positions of free blocks */
449: u_int8_t cg_iused[256]; /* used inode map */
450: int32_t cg_magic; /* magic number */
451: u_int8_t cg_free[1]; /* free block map */
452: /* actually longer */
453: };
454:
455: /*
456: * Turn file system block numbers into disk block addresses.
457: * This maps file system blocks to device size blocks.
458: */
459: #define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb)
460: #define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
461:
462: /*
463: * Cylinder group macros to locate things in cylinder groups.
464: * They calc file system addresses of cylinder group data structures.
465: */
466: #define cgbase(fs, c) ((daddr_t)((fs)->fs_fpg * (c)))
467: #define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
468: #define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
469: #define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
470: #define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
471: #define cgstart(fs, c) \
472: (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
473:
474: /*
475: * Macros for handling inode numbers:
476: * inode number to file system block offset.
477: * inode number to cylinder group number.
478: * inode number to file system block address.
479: */
480: #define ino_to_cg(fs, x) ((x) / (fs)->fs_ipg)
481: #define ino_to_fsba(fs, x) \
482: ((daddr_t)(cgimin(fs, ino_to_cg(fs, x)) + \
483: (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
484: #define ino_to_fsbo(fs, x) ((x) % INOPB(fs))
485:
486: /*
487: * Give cylinder group number for a file system block.
488: * Give frag block number in cylinder group for a file system block.
489: */
490: #define dtog(fs, d) ((d) / (fs)->fs_fpg)
491: #define dtogd(fs, d) ((d) % (fs)->fs_fpg)
492:
493: /*
494: * Extract the bits for a block from a map.
495: * Compute the cylinder and rotational position of a cyl block addr.
496: */
497: #define blkmap(fs, map, loc) \
498: (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
499: #define cbtocylno(fs, bno) \
500: (fsbtodb(fs, bno) / (fs)->fs_spc)
501: #define cbtorpos(fs, bno) \
502: ((fs)->fs_nrpos <= 1 ? 0 : \
503: (fsbtodb(fs, bno) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
504: fsbtodb(fs, bno) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
505: (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
506:
507: /*
508: * The following macros optimize certain frequently calculated
509: * quantities by using shifts and masks in place of divisions
510: * modulos and multiplications.
511: */
512: #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
513: ((loc) & (fs)->fs_qbmask)
514: #define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
515: ((loc) & (fs)->fs_qfmask)
516: #define lblktosize(fs, blk) /* calculates ((off_t)blk * fs->fs_bsize) */ \
517: ((off_t)(blk) << (fs)->fs_bshift)
518: #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
519: ((loc) >> (fs)->fs_bshift)
520: #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
521: ((loc) >> (fs)->fs_fshift)
522: #define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
523: (((size) + (fs)->fs_qbmask) & (fs)->fs_bmask)
524: #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
525: (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask)
526: #define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
527: ((frags) >> (fs)->fs_fragshift)
528: #define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
529: ((blks) << (fs)->fs_fragshift)
530: #define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
531: ((fsb) & ((fs)->fs_frag - 1))
532: #define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
533: ((fsb) &~ ((fs)->fs_frag - 1))
534:
535: /*
536: * Determine the number of available frags given a
537: * percentage to hold in reserve.
538: */
539: #define freespace(fs, percentreserved) \
540: (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
541: (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
542:
543: /*
544: * Determining the size of a file block in the file system.
545: */
546: #define blksize(fs, ip, lbn) \
547: (((lbn) >= NDADDR || DIP((ip), size) >= ((lbn) + 1) << (fs)->fs_bshift) \
548: ? (fs)->fs_bsize \
549: : (fragroundup(fs, blkoff(fs, DIP((ip), size)))))
550: #define dblksize(fs, dip, lbn) \
551: (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
552: ? (fs)->fs_bsize \
553: : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
554:
555: #define sblksize(fs, size, lbn) \
556: (((lbn) >= NDADDR || (size) >= ((lbn) + 1) << (fs)->fs_bshift) \
557: ? (fs)->fs_bsize \
558: : (fragroundup(fs, blkoff(fs, (size)))))
559:
560:
561: /*
562: * Number of disk sectors per block/fragment; assumes DEV_BSIZE byte
563: * sector size.
564: */
565: #define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
566: #define NSPF(fs) ((fs)->fs_nspf)
567:
568: /* Number of inodes per file system block (fs->fs_bsize) */
569: #define INOPB(fs) ((fs)->fs_inopb)
570: /* Number of inodes per file system fragment (fs->fs_fsize) */
571: #define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
572:
573: /*
574: * Number of indirects in a file system block.
575: */
576: #define NINDIR(fs) ((fs)->fs_nindir)
577:
578: extern const int inside[], around[];
579: extern const u_char *fragtbl[];
CVSweb