• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  */
26 /*
27  * Copyright (c) 1982, 1986 Regents of the University of California.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms are permitted
31  * provided that the above copyright notice and this paragraph are
32  * duplicated in all such forms and that any documentation,
33  * advertising materials, and other materials related to such
34  * distribution and use acknowledge that the software was developed
35  * by the University of California, Berkeley.  The name of the
36  * University may not be used to endorse or promote products derived
37  * from this software without specific prior written permission.
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
39  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
40  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41  *
42  *	@(#)fs.h	7.7 (Berkeley) 5/9/89
43  */
44 
45 /*
46  * Each disk drive contains some number of file systems.
47  * A file system consists of a number of cylinder groups.
48  * Each cylinder group has inodes and data.
49  *
50  * A file system is described by its super-block, which in turn
51  * describes the cylinder groups.  The super-block is critical
52  * data and is replicated in each cylinder group to protect against
53  * catastrophic loss.  This is done at `newfs' time and the critical
54  * super-block data does not change, so the copies need not be
55  * referenced further unless disaster strikes.
56  *
57  * For file system fs, the offsets of the various blocks of interest
58  * are given in the super block as:
59  *	[fs->fs_sblkno]		Super-block
60  *	[fs->fs_cblkno]		Cylinder group block
61  *	[fs->fs_iblkno]		Inode blocks
62  *	[fs->fs_dblkno]		Data blocks
63  * The beginning of cylinder group cg in fs, is given by
64  * the ``cgbase(fs, cg)'' macro.
65  *
66  * The first boot and super blocks are given in absolute disk addresses.
67  * The byte-offset forms are preferred, as they don't imply a sector size.
68  */
69 #define BBSIZE		8192
70 #define SBSIZE		8192
71 #define	BBOFF		((mach_off_t)(0))
72 #define	SBOFF		((mach_off_t)(BBOFF + BBSIZE))
73 #define	BBLOCK		((mach_daddr_t)(0))
74 #define	SBLOCK		((mach_daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
75 
76 /*
77  * Addresses stored in inodes are capable of addressing fragments
78  * of `blocks'. File system blocks of at most size MAXBSIZE can
79  * be optionally broken into 2, 4, or 8 pieces, each of which is
80  * addressible; these pieces may be DEV_BSIZE, or some multiple of
81  * a DEV_BSIZE unit.
82  *
83  * Large files consist of exclusively large data blocks.  To avoid
84  * undue wasted disk space, the last data block of a small file may be
85  * allocated as only as many fragments of a large block as are
86  * necessary.  The file system format retains only a single pointer
87  * to such a fragment, which is a piece of a single large block that
88  * has been divided.  The size of such a fragment is determinable from
89  * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
90  *
91  * The file system records space availability at the fragment level;
92  * to determine block availability, aligned fragments are examined.
93  *
94  * The root inode is the root of the file system.
95  * Inode 0 can't be used for normal purposes and
96  * historically bad blocks were linked to inode 1,
97  * thus the root inode is 2. (inode 1 is no longer used for
98  * this purpose, however numerous dump tapes make this
99  * assumption, so we are stuck with it)
100  */
101 #define	ROOTINO		((mach_ino_t)2)	/* i number of all roots */
102 
103 /*
104  * MINBSIZE is the smallest allowable block size.
105  * In order to insure that it is possible to create files of size
106  * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
107  * MINBSIZE must be big enough to hold a cylinder group block,
108  * thus changes to (struct cg) must keep its size within MINBSIZE.
109  * Note that super blocks are always of size SBSIZE,
110  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
111  */
112 #define MINBSIZE	4096
113 
114 /*
115  * The path name on which the file system is mounted is maintained
116  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
117  * the super block for this name.
118  * The limit on the amount of summary information per file system
119  * is defined by MAXCSBUFS. It is currently parameterized for a
120  * maximum of two million cylinders.
121  */
122 #define MAXMNTLEN 512
123 #define MAXCSBUFS 32
124 
125 /*
126  * Per cylinder group information; summarized in blocks allocated
127  * from first cylinder group data blocks.  These blocks have to be
128  * read in from fs_csaddr (size fs_cssize) in addition to the
129  * super block.
130  *
131  * N.B. sizeof(struct csum) must be a power of two in order for
132  * the ``fs_cs'' macro to work (see below).
133  */
134 struct csum
135   {
136     int cs_ndir;		/* number of directories */
137     int cs_nbfree;		/* number of free blocks */
138     int cs_nifree;		/* number of free inodes */
139     int cs_nffree;		/* number of free frags */
140   };
141 
142 /*
143  * Super block for a file system.
144  */
145 #define	FS_MAGIC	0x011954
146 struct fs
147   {
148     int xxx1;			/* struct       fs *fs_link; */
149     int xxx2;			/* struct       fs *fs_rlink; */
150     mach_daddr_t fs_sblkno;	/* addr of super-block in filesys */
151     mach_daddr_t fs_cblkno;	/* offset of cyl-block in filesys */
152     mach_daddr_t fs_iblkno;	/* offset of inode-blocks in filesys */
153     mach_daddr_t fs_dblkno;	/* offset of first data after cg */
154     int fs_cgoffset;		/* cylinder group offset in cylinder */
155     int fs_cgmask;		/* used to calc mod fs_ntrak */
156     mach_time_t fs_time;	/* last time written */
157     int fs_size;		/* number of blocks in fs */
158     int fs_dsize;		/* number of data blocks in fs */
159     int fs_ncg;			/* number of cylinder groups */
160     int fs_bsize;		/* size of basic blocks in fs */
161     int fs_fsize;		/* size of frag blocks in fs */
162     int fs_frag;		/* number of frags in a block in fs */
163 /* these are configuration parameters */
164     int fs_minfree;		/* minimum percentage of free blocks */
165     int fs_rotdelay;		/* num of ms for optimal next block */
166     int fs_rps;			/* disk revolutions per second */
167 /* these fields can be computed from the others */
168     int fs_bmask;		/* ``blkoff'' calc of blk offsets */
169     int fs_fmask;		/* ``fragoff'' calc of frag offsets */
170     int fs_bshift;		/* ``lblkno'' calc of logical blkno */
171     int fs_fshift;		/* ``numfrags'' calc number of frags */
172 /* these are configuration parameters */
173     int fs_maxcontig;		/* max number of contiguous blks */
174     int fs_maxbpg;		/* max number of blks per cyl group */
175 /* these fields can be computed from the others */
176     int fs_fragshift;		/* block to frag shift */
177     int fs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
178     int fs_sbsize;		/* actual size of super block */
179     int fs_csmask;		/* csum block offset */
180     int fs_csshift;		/* csum block number */
181     int fs_nindir;		/* value of NINDIR */
182     int fs_inopb;		/* value of INOPB */
183     int fs_nspf;		/* value of NSPF */
184 /* yet another configuration parameter */
185     int fs_optim;		/* optimization preference, see below */
186 /* these fields are derived from the hardware */
187     int fs_npsect;		/* # sectors/track including spares */
188     int fs_interleave;		/* hardware sector interleave */
189     int fs_trackskew;		/* sector 0 skew, per track */
190     int fs_headswitch;		/* head switch time, usec */
191     int fs_trkseek;		/* track-to-track seek, usec */
192 /* sizes determined by number of cylinder groups and their sizes */
193     mach_daddr_t fs_csaddr;	/* blk addr of cyl grp summary area */
194     int fs_cssize;		/* size of cyl grp summary area */
195     int fs_cgsize;		/* cylinder group size */
196 /* these fields are derived from the hardware */
197     int fs_ntrak;		/* tracks per cylinder */
198     int fs_nsect;		/* sectors per track */
199     int fs_spc;			/* sectors per cylinder */
200 /* this comes from the disk driver partitioning */
201     int fs_ncyl;		/* cylinders in file system */
202 /* these fields can be computed from the others */
203     int fs_cpg;			/* cylinders per group */
204     int fs_ipg;			/* inodes per group */
205     int fs_fpg;			/* blocks per group * fs_frag */
206 /* this data must be re-computed after crashes */
207     struct csum fs_cstotal;	/* cylinder summary information */
208 /* these fields are cleared at mount time */
209     char fs_fmod;		/* super block modified flag */
210     char fs_clean;		/* file system is clean flag */
211     char fs_ronly;		/* mounted read-only flag */
212     char fs_flags;		/* currently unused flag */
213     char fs_fsmnt[MAXMNTLEN];	/* name mounted on */
214 /* these fields retain the current block allocation info */
215     int fs_cgrotor;		/* last cg searched */
216 #if 1
217     int was_fs_csp[MAXCSBUFS];
218 #else
219     struct csum *fs_csp[MAXCSBUFS];	/* list of fs_cs info buffers */
220 #endif
221     int fs_cpc;			/* cyl per cycle in postbl */
222     short fs_opostbl[16][8];	/* old rotation block list head */
223     long fs_sparecon[50];	/* reserved for future constants */
224     long fs_contigsumsize;	/* size of cluster summary array */
225     long fs_maxsymlinklen;	/* max length of an internal symlink */
226     long fs_inodefmt;		/* format of on-disk inodes */
227     quad fs_maxfilesize;	/* maximum representable file size */
228     quad fs_qbmask;		/* ~fs_bmask - for use with quad size */
229     quad fs_qfmask;		/* ~fs_fmask - for use with quad size */
230     long fs_state;		/* validate fs_clean field */
231     int fs_postblformat;	/* format of positional layout tables */
232     int fs_nrpos;		/* number of rotaional positions */
233     int fs_postbloff;		/* (short) rotation block list head */
234     int fs_rotbloff;		/* (u_char) blocks for each rotation */
235     int fs_magic;		/* magic number */
236     u_char fs_space[1];		/* list of blocks for each rotation */
237 /* actually longer */
238   };
239 /*
240  * Preference for optimization.
241  */
242 #define FS_OPTTIME	0	/* minimize allocation time */
243 #define FS_OPTSPACE	1	/* minimize disk fragmentation */
244 
245 /*
246  * Rotational layout table format types
247  */
248 #define FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
249 #define FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
250 /*
251  * Macros for access to superblock array structures
252  */
253 #define fs_postbl(fs, cylno) \
254     (((fs)->fs_postblformat == FS_42POSTBLFMT) \
255     ? ((fs)->fs_opostbl[cylno]) \
256     : ((short *)((char *)(fs) + (fs)->fs_postbloff) + (cylno) * (fs)->fs_nrpos))
257 #define fs_rotbl(fs) \
258     (((fs)->fs_postblformat == FS_42POSTBLFMT) \
259     ? ((fs)->fs_space) \
260     : ((u_char *)((char *)(fs) + (fs)->fs_rotbloff)))
261 
262 /*
263  * Convert cylinder group to base address of its global summary info.
264  *
265  * N.B. This macro assumes that sizeof(struct csum) is a power of two.
266  */
267 #define fs_cs(fs, indx) \
268 	fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask]
269 
270 /*
271  * Cylinder group block for a file system.
272  */
273 #define	CG_MAGIC	0x090255
274 struct cg
275   {
276     int xxx1;			/* struct       cg *cg_link; */
277     int cg_magic;		/* magic number */
278     mach_time_t cg_time;		/* time last written */
279     int cg_cgx;			/* we are the cgx'th cylinder group */
280     short cg_ncyl;		/* number of cyl's this cg */
281     short cg_niblk;		/* number of inode blocks this cg */
282     int cg_ndblk;		/* number of data blocks this cg */
283     struct csum cg_cs;		/* cylinder summary information */
284     int cg_rotor;		/* position of last used block */
285     int cg_frotor;		/* position of last used frag */
286     int cg_irotor;		/* position of last used inode */
287     int cg_frsum[MAXFRAG];	/* counts of available frags */
288     int cg_btotoff;		/* (long) block totals per cylinder */
289     int cg_boff;		/* (short) free block positions */
290     int cg_iusedoff;		/* (char) used inode map */
291     int cg_freeoff;		/* (u_char) free block map */
292     int cg_nextfreeoff;		/* (u_char) next available space */
293     int cg_sparecon[16];	/* reserved for future use */
294     u_char cg_space[1];		/* space for cylinder group maps */
295 /* actually longer */
296   };
297 /*
298  * Macros for access to cylinder group array structures
299  */
300 #define cg_blktot(cgp) \
301     (((cgp)->cg_magic != CG_MAGIC) \
302     ? (((struct ocg *)(cgp))->cg_btot) \
303     : ((int *)((char *)(cgp) + (cgp)->cg_btotoff)))
304 #define cg_blks(fs, cgp, cylno) \
305     (((cgp)->cg_magic != CG_MAGIC) \
306     ? (((struct ocg *)(cgp))->cg_b[cylno]) \
307     : ((short *)((char *)(cgp) + (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
308 #define cg_inosused(cgp) \
309     (((cgp)->cg_magic != CG_MAGIC) \
310     ? (((struct ocg *)(cgp))->cg_iused) \
311     : ((char *)((char *)(cgp) + (cgp)->cg_iusedoff)))
312 #define cg_blksfree(cgp) \
313     (((cgp)->cg_magic != CG_MAGIC) \
314     ? (((struct ocg *)(cgp))->cg_free) \
315     : ((u_char *)((char *)(cgp) + (cgp)->cg_freeoff)))
316 #define cg_chkmagic(cgp) \
317     ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
318 
319 /*
320  * The following structure is defined
321  * for compatibility with old file systems.
322  */
323 struct ocg
324   {
325     int xxx1;			/* struct       ocg *cg_link; */
326     int xxx2;			/* struct       ocg *cg_rlink; */
327     mach_time_t cg_time;	/* time last written */
328     int cg_cgx;			/* we are the cgx'th cylinder group */
329     short cg_ncyl;		/* number of cyl's this cg */
330     short cg_niblk;		/* number of inode blocks this cg */
331     int cg_ndblk;		/* number of data blocks this cg */
332     struct csum cg_cs;		/* cylinder summary information */
333     int cg_rotor;		/* position of last used block */
334     int cg_frotor;		/* position of last used frag */
335     int cg_irotor;		/* position of last used inode */
336     int cg_frsum[8];		/* counts of available frags */
337     int cg_btot[32];		/* block totals per cylinder */
338     short cg_b[32][8];		/* positions of free blocks */
339     char cg_iused[256];		/* used inode map */
340     int cg_magic;		/* magic number */
341     u_char cg_free[1];		/* free block map */
342 /* actually longer */
343   };
344 
345 /*
346  * Turn file system block numbers into disk block addresses.
347  * This maps file system blocks to device size blocks.
348  */
349 #define fsbtodb(fs, b)	((b) << (fs)->fs_fsbtodb)
350 #define	dbtofsb(fs, b)	((b) >> (fs)->fs_fsbtodb)
351 
352 /*
353  * Cylinder group macros to locate things in cylinder groups.
354  * They calc file system addresses of cylinder group data structures.
355  */
356 #define	cgbase(fs, c)	((mach_daddr_t)((fs)->fs_fpg * (c)))
357 #define cgstart(fs, c) \
358 	(cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
359 #define	cgsblock(fs, c)	(cgstart(fs, c) + (fs)->fs_sblkno)	/* super blk */
360 #define	cgtod(fs, c)	(cgstart(fs, c) + (fs)->fs_cblkno)	/* cg block */
361 #define	cgimin(fs, c)	(cgstart(fs, c) + (fs)->fs_iblkno)	/* inode blk */
362 #define	cgdmin(fs, c)	(cgstart(fs, c) + (fs)->fs_dblkno)	/* 1st data */
363 
364 /*
365  * Macros for handling inode numbers:
366  *     inode number to file system block offset.
367  *     inode number to cylinder group number.
368  *     inode number to file system block address.
369  */
370 #define	itoo(fs, x)	((x) % INOPB(fs))
371 #define	itog(fs, x)	((x) / (fs)->fs_ipg)
372 #define	itod(fs, x) \
373 	((mach_daddr_t)(cgimin(fs, itog(fs, x)) + \
374 	(blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
375 
376 /*
377  * Give cylinder group number for a file system block.
378  * Give cylinder group block number for a file system block.
379  */
380 #define	dtog(fs, d)	((d) / (fs)->fs_fpg)
381 #define	dtogd(fs, d)	((d) % (fs)->fs_fpg)
382 
383 /*
384  * Extract the bits for a block from a map.
385  * Compute the cylinder and rotational position of a cyl block addr.
386  */
387 #define blkmap(fs, map, loc) \
388     (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
389 #define cbtocylno(fs, bno) \
390     ((bno) * NSPF(fs) / (fs)->fs_spc)
391 #define cbtorpos(fs, bno) \
392     (((bno) * NSPF(fs) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
393      (bno) * NSPF(fs) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
394      (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
395 
396 /*
397  * The following macros optimize certain frequently calculated
398  * quantities by using shifts and masks in place of divisions
399  * modulos and multiplications.
400  */
401 #define blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
402 	((loc) & ~(fs)->fs_bmask)
403 #define fragoff(fs, loc)	/* calculates (loc % fs->fs_fsize) */ \
404 	((loc) & ~(fs)->fs_fmask)
405 #define lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
406 	((loc) >> (fs)->fs_bshift)
407 #define numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
408 	((loc) >> (fs)->fs_fshift)
409 #define blkroundup(fs, size)	/* calculates roundup(size, fs->fs_bsize) */ \
410 	(((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
411 #define fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
412 	(((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
413 #define fragstoblks(fs, frags)	/* calculates (frags / fs->fs_frag) */ \
414 	((frags) >> (fs)->fs_fragshift)
415 #define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
416 	((blks) << (fs)->fs_fragshift)
417 #define fragnum(fs, fsb)	/* calculates (fsb % fs->fs_frag) */ \
418 	((fsb) & ((fs)->fs_frag - 1))
419 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->fs_frag) */ \
420 	((fsb) &~ ((fs)->fs_frag - 1))
421 
422 /*
423  * Determine the number of available frags given a
424  * percentage to hold in reserve
425  */
426 #define freespace(fs, percentreserved) \
427 	(blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
428 	(fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
429 
430 /*
431  * Determining the size of a file block in the file system.
432  */
433 #define blksize(fs, ip, lbn) \
434 	(((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \
435 	    ? (fs)->fs_bsize \
436 	    : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
437 #define dblksize(fs, dip, lbn) \
438 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
439 	    ? (fs)->fs_bsize \
440 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
441 
442 /*
443  * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
444  */
445 #define	NSPB(fs)	((fs)->fs_nspf << (fs)->fs_fragshift)
446 #define	NSPF(fs)	((fs)->fs_nspf)
447 
448 /*
449  * INOPB is the number of inodes in a secondary storage block.
450  */
451 #define	INOPB(fs)	((fs)->fs_inopb)
452 #define	INOPF(fs)	((fs)->fs_inopb >> (fs)->fs_fragshift)
453 
454 /*
455  * NINDIR is the number of indirects in a file system block.
456  */
457 #define	NINDIR(fs)	((fs)->fs_nindir)
458