xref: /freebsd/share/man/man5/fs.5 (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. 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.5	8.2 (Berkeley) 4/19/94
33.\" $FreeBSD$
34.\"
35.Dd October 31, 2006
36.Dt FS 5
37.Os
38.Sh NAME
39.Nm fs ,
40.Nm inode
41.Nd format of file system volume
42.Sh SYNOPSIS
43.In sys/param.h
44.In ufs/ffs/fs.h
45.Pp
46.In sys/types.h
47.In sys/lock.h
48.In sys/extattr.h
49.In sys/acl.h
50.In ufs/ufs/quota.h
51.In ufs/ufs/dinode.h
52.In ufs/ufs/extattr.h
53.Sh DESCRIPTION
54The files
55.In fs.h
56and
57.In inode.h
58declare several structures, defined variables and macros
59which are used to create and manage the underlying format of
60file system objects on random access devices (disks).
61.Pp
62The block size and number of blocks which
63comprise a file system are parameters of the file system.
64Sectors beginning at
65.Dv BBLOCK
66and continuing for
67.Dv BBSIZE
68are used
69for a disklabel and for some hardware primary
70and secondary bootstrapping programs.
71.Pp
72The actual file system begins at sector
73.Dv SBLOCK
74with the
75.Em super-block
76that is of size
77.Dv SBLOCKSIZE .
78The following structure describes the super-block and is
79from the file
80.In ufs/ffs/fs.h :
81.Bd -literal
82/*
83 * Super block for an FFS filesystem.
84 */
85struct fs {
86	int32_t	 fs_firstfield;	   /* historic filesystem linked list, */
87	int32_t	 fs_unused_1;      /*     used for incore super blocks */
88	int32_t	 fs_sblkno;        /* offset of super-block in filesys */
89	int32_t	 fs_cblkno;        /* offset of cyl-block in filesys */
90	int32_t	 fs_iblkno;        /* offset of inode-blocks in filesys */
91	int32_t	 fs_dblkno;        /* offset of first data after cg */
92	int32_t	 fs_old_cgoffset;  /* cylinder group offset in cylinder */
93	int32_t	 fs_old_cgmask;    /* used to calc mod fs_ntrak */
94	int32_t  fs_old_time;      /* last time written */
95	int32_t	 fs_old_size;      /* number of blocks in fs */
96	int32_t	 fs_old_dsize;     /* number of data blocks in fs */
97	int32_t	 fs_ncg;           /* number of cylinder groups */
98	int32_t	 fs_bsize;         /* size of basic blocks in fs */
99	int32_t	 fs_fsize;         /* size of frag blocks in fs */
100	int32_t	 fs_frag;          /* number of frags in a block in fs */
101/* these are configuration parameters */
102	int32_t	 fs_minfree;       /* minimum percentage of free blocks */
103	int32_t	 fs_old_rotdelay;  /* num of ms for optimal next block */
104	int32_t	 fs_old_rps;       /* disk revolutions per second */
105/* these fields can be computed from the others */
106	int32_t	 fs_bmask;         /* ``blkoff'' calc of blk offsets */
107	int32_t	 fs_fmask;         /* ``fragoff'' calc of frag offsets */
108	int32_t	 fs_bshift;        /* ``lblkno'' calc of logical blkno */
109	int32_t	 fs_fshift;        /* ``numfrags'' calc number of frags */
110/* these are configuration parameters */
111	int32_t	 fs_maxcontig;     /* max number of contiguous blks */
112	int32_t	 fs_maxbpg;        /* max number of blks per cyl group */
113/* these fields can be computed from the others */
114	int32_t	 fs_fragshift;     /* block to frag shift */
115	int32_t	 fs_fsbtodb;       /* fsbtodb and dbtofsb shift constant */
116	int32_t	 fs_sbsize;        /* actual size of super block */
117	int32_t	 fs_spare1[2];     /* old fs_csmask */
118	                           /* old fs_csshift */
119	int32_t	 fs_nindir;        /* value of NINDIR */
120	int32_t	 fs_inopb;         /* value of INOPB */
121	int32_t	 fs_old_nspf;      /* value of NSPF */
122/* yet another configuration parameter */
123	int32_t	 fs_optim;         /* optimization preference, see below */
124	int32_t	 fs_old_npsect;    /* # sectors/track including spares */
125	int32_t	 fs_old_interleave; /* hardware sector interleave */
126	int32_t	 fs_old_trackskew; /* sector 0 skew, per track */
127	int32_t	 fs_id[2];         /* unique filesystem id */
128/* sizes determined by number of cylinder groups and their sizes */
129	int32_t	 fs_old_csaddr;	   /* blk addr of cyl grp summary area */
130	int32_t	 fs_cssize;        /* size of cyl grp summary area */
131	int32_t	 fs_cgsize;        /* cylinder group size */
132	int32_t	 fs_spare2;        /* old fs_ntrak */
133	int32_t	 fs_old_nsect;     /* sectors per track */
134	int32_t  fs_old_spc;       /* sectors per cylinder */
135	int32_t	 fs_old_ncyl;      /* cylinders in filesystem */
136	int32_t	 fs_old_cpg;       /* cylinders per group */
137	int32_t	 fs_ipg;           /* inodes per group */
138	int32_t	 fs_fpg;           /* blocks per group * fs_frag */
139/* this data must be re-computed after crashes */
140	struct	csum fs_old_cstotal; /* cylinder summary information */
141/* these fields are cleared at mount time */
142	int8_t   fs_fmod;          /* super block modified flag */
143	int8_t   fs_clean;         /* filesystem is clean flag */
144	int8_t 	 fs_ronly;         /* mounted read-only flag */
145	int8_t   fs_old_flags;     /* old FS_ flags */
146	u_char	 fs_fsmnt[MAXMNTLEN]; /* name mounted on */
147	u_char	 fs_volname[MAXVOLLEN]; /* volume name */
148	u_int64_t fs_swuid;        /* system-wide uid */
149	int32_t  fs_pad;           /* due to alignment of fs_swuid */
150/* these fields retain the current block allocation info */
151	int32_t	 fs_cgrotor;       /* last cg searched */
152	void 	*fs_ocsp[NOCSPTRS]; /* padding; was list of fs_cs buffers */
153	u_int8_t *fs_contigdirs;   /* # of contiguously allocated dirs */
154	struct	csum *fs_csp;      /* cg summary info buffer for fs_cs */
155	int32_t	*fs_maxcluster;    /* max cluster in each cyl group */
156	u_int	*fs_active;        /* used by snapshots to track fs */
157	int32_t	 fs_old_cpc;       /* cyl per cycle in postbl */
158	int32_t	 fs_maxbsize;      /* maximum blocking factor permitted */
159	int64_t	 fs_unrefs;        /* number of unreferenced inodes */
160	int64_t	 fs_sparecon64[16]; /* old rotation block list head */
161	int64_t	 fs_sblockloc;     /* byte offset of standard superblock */
162	struct	csum_total fs_cstotal;  /* cylinder summary information */
163	ufs_time_t fs_time;        /* last time written */
164	int64_t	 fs_size;          /* number of blocks in fs */
165	int64_t	 fs_dsize;         /* number of data blocks in fs */
166	ufs2_daddr_t fs_csaddr;    /* blk addr of cyl grp summary area */
167	int64_t	 fs_pendingblocks; /* blocks in process of being freed */
168	int32_t	 fs_pendinginodes; /* inodes in process of being freed */
169	int32_t	 fs_snapinum[FSMAXSNAP]; /* list of snapshot inode numbers */
170	int32_t	 fs_avgfilesize;   /* expected average file size */
171	int32_t	 fs_avgfpdir;      /* expected # of files per directory */
172	int32_t	 fs_save_cgsize;   /* save real cg size to use fs_bsize */
173	int32_t	 fs_sparecon32[26]; /* reserved for future constants */
174	int32_t  fs_flags;         /* see FS_ flags below */
175	int32_t	 fs_contigsumsize; /* size of cluster summary array */
176	int32_t	 fs_maxsymlinklen; /* max length of an internal symlink */
177	int32_t	 fs_old_inodefmt;  /* format of on-disk inodes */
178	u_int64_t fs_maxfilesize;  /* maximum representable file size */
179	int64_t	 fs_qbmask;        /* ~fs_bmask for use with 64-bit size */
180	int64_t	 fs_qfmask;        /* ~fs_fmask for use with 64-bit size */
181	int32_t	 fs_state;         /* validate fs_clean field */
182	int32_t	 fs_old_postblformat; /* format of positional layout tables */
183	int32_t	 fs_old_nrpos;     /* number of rotational positions */
184	int32_t	 fs_spare5[2];     /* old fs_postbloff */
185	                           /* old fs_rotbloff */
186	int32_t	 fs_magic;         /* magic number */
187};
188
189/*
190 * Filesystem identification
191 */
192#define	FS_UFS1_MAGIC	0x011954    /* UFS1 fast filesystem magic number */
193#define	FS_UFS2_MAGIC	0x19540119  /* UFS2 fast filesystem magic number */
194#define	FS_OKAY		0x7c269d38  /* superblock checksum */
195#define FS_42INODEFMT	-1      /* 4.2BSD inode format */
196#define FS_44INODEFMT	2       /* 4.4BSD inode format */
197
198/*
199 * Preference for optimization.
200 */
201#define FS_OPTTIME	0	/* minimize allocation time */
202#define FS_OPTSPACE	1	/* minimize disk fragmentation */
203.Ed
204.Pp
205Each disk drive contains some number of file systems.
206A file system consists of a number of cylinder groups.
207Each cylinder group has inodes and data.
208.Pp
209A file system is described by its super-block, which in turn
210describes the cylinder groups.
211The super-block is critical
212data and is replicated in each cylinder group to protect against
213catastrophic loss.
214This is done at file system creation
215time and the critical
216super-block data does not change, so the copies need not be
217referenced further unless disaster strikes.
218.Pp
219Addresses stored in inodes are capable of addressing fragments
220of `blocks'.
221File system blocks of at most size
222.Dv MAXBSIZE
223can
224be optionally broken into 2, 4, or 8 pieces, each of which is
225addressable; these pieces may be
226.Dv DEV_BSIZE ,
227or some multiple of
228a
229.Dv DEV_BSIZE
230unit.
231.Pp
232Large files consist of exclusively large data blocks.
233To avoid
234undue wasted disk space, the last data block of a small file is
235allocated as only as many fragments of a large block as are
236necessary.
237The file system format retains only a single pointer
238to such a fragment, which is a piece of a single large block that
239has been divided.
240The size of such a fragment is determinable from
241information in the inode, using the
242.Fn blksize fs ip lbn
243macro.
244.Pp
245The file system records space availability at the fragment level;
246to determine block availability, aligned fragments are examined.
247.Pp
248The root inode is the root of the file system.
249Inode 0 cannot be used for normal purposes and
250historically bad blocks were linked to inode 1,
251thus the root inode is 2 (inode 1 is no longer used for
252this purpose, however numerous dump tapes make this
253assumption, so we are stuck with it).
254.Pp
255The
256.Fa fs_minfree
257element gives the minimum acceptable percentage of file system
258blocks that may be free.
259If the freelist drops below this level
260only the super-user may continue to allocate blocks.
261The
262.Fa fs_minfree
263element
264may be set to 0 if no reserve of free blocks is deemed necessary,
265however severe performance degradations will be observed if the
266file system is run at greater than 90% full; thus the default
267value of
268.Fa fs_minfree
269is 10%.
270.Pp
271Empirically the best trade-off between block fragmentation and
272overall disk utilization at a loading of 90% comes with a
273fragmentation of 8, thus the default fragment size is an eighth
274of the block size.
275.Pp
276The element
277.Fa fs_optim
278specifies whether the file system should try to minimize the time spent
279allocating blocks, or if it should attempt to minimize the space
280fragmentation on the disk.
281If the value of fs_minfree (see above) is less than 10%,
282then the file system defaults to optimizing for space to avoid
283running out of full sized blocks.
284If the value of minfree is greater than or equal to 10%,
285fragmentation is unlikely to be problematical, and
286the file system defaults to optimizing for time.
287.Pp
288.Em Cylinder group related limits :
289Each cylinder keeps track of the availability of blocks at different
290rotational positions, so that sequential blocks can be laid out
291with minimum rotational latency.
292With the default of 8 distinguished
293rotational positions, the resolution of the
294summary information is 2ms for a typical 3600 rpm drive.
295.Pp
296The element
297.Fa fs_old_rotdelay
298gives the minimum number of milliseconds to initiate
299another disk transfer on the same cylinder.
300It is used in determining the rotationally optimal
301layout for disk blocks within a file;
302the default value for
303.Fa fs_old_rotdelay
304is 2ms.
305.Pp
306Each file system has a statically allocated number of inodes.
307An inode is allocated for each
308.Dv NBPI
309bytes of disk space.
310The inode allocation strategy is extremely conservative.
311.Pp
312.Dv MINBSIZE
313is the smallest allowable block size.
314With a
315.Dv MINBSIZE
316of 4096
317it is possible to create files of size
3182^32 with only two levels of indirection.
319.Dv MINBSIZE
320must be big enough to hold a cylinder group block,
321thus changes to
322.Pq Fa struct cg
323must keep its size within
324.Dv MINBSIZE .
325Note that super-blocks are never more than size
326.Dv SBLOCKSIZE .
327.Pp
328The path name on which the file system is mounted is maintained in
329.Fa fs_fsmnt .
330.Dv MAXMNTLEN
331defines the amount of space allocated in
332the super-block for this name.
333The limit on the amount of summary information per file system
334is defined by
335.Dv MAXCSBUFS .
336For a 4096 byte block size, it is currently parameterized for a
337maximum of two million cylinders.
338.Pp
339Per cylinder group information is summarized in blocks allocated
340from the first cylinder group's data blocks.
341These blocks are read in from
342.Fa fs_csaddr
343(size
344.Fa fs_cssize )
345in addition to the super-block.
346.Pp
347.Sy N.B. :
348.Fn sizeof "struct csum"
349must be a power of two in order for
350the
351.Fn fs_cs
352macro to work.
353.Pp
354The
355.Em "Super-block for a file system" :
356The size of the rotational layout tables
357is limited by the fact that the super-block is of size
358.Dv SBLOCKSIZE .
359The size of these tables is
360.Em inversely
361proportional to the block
362size of the file system.
363The size of the tables is
364increased when sector sizes are not powers of two,
365as this increases the number of cylinders
366included before the rotational pattern repeats
367.Pq Fa fs_cpc .
368The size of the rotational layout
369tables is derived from the number of bytes remaining in
370.Pq Fa struct fs .
371.Pp
372The number of blocks of data per cylinder group
373is limited because cylinder groups are at most one block.
374The inode and free block tables
375must fit into a single block after deducting space for
376the cylinder group structure
377.Pq Fa struct cg .
378.Pp
379The
380.Em Inode :
381The inode is the focus of all file activity in the
382.Ux
383file system.
384There is a unique inode allocated
385for each active file,
386each current directory, each mounted-on file,
387text file, and the root.
388An inode is `named' by its device/i-number pair.
389For further information, see the include file
390.In ufs/ufs/inode.h .
391.Pp
392The format of an external attribute is defined by the extattr structure:
393.Bd -literal
394struct extattr {
395	int32_t	ea_length;	    /* length of this attribute */
396	int8_t	ea_namespace;	    /* name space of this attribute */
397	int8_t	ea_contentpadlen;   /* padding at end of attribute */
398	int8_t	ea_namelength;	    /* length of attribute name */
399	char	ea_name[1];	    /* null-terminated attribute name */
400	/* extended attribute content follows */
401};
402.Ed
403.Pp
404Several macros are defined to manipulate these structures.
405Each macro takes a pointer to an extattr structure.
406.Bl -tag -width ".Dv EXTATTR_SET_LENGTHS(eap, size)"
407.It Dv EXTATTR_NEXT(eap)
408Returns a pointer to the next extended attribute following
409.Fa eap .
410.It Dv EXTATTR_CONTENT(eap)
411Returns a pointer to the extended attribute content referenced by
412.Fa eap .
413.It Dv EXTATTR_CONTENT_SIZE(eap)
414Returns the size of the extended attribute content referenced by
415.Fa eap .
416.It Dv EXTATTR_SET_LENGTHS(eap, size)
417Called with the size of the attribute content after initializing
418the attribute name to calculate and set the
419.Fa ea_length ,
420.Fa ea_namelength ,
421and
422.Fa ea_contentpadlen
423fields of the extended attribute structure.
424.El
425.Pp
426The following code identifies an ACL:
427.Bd -literal
428	if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
429	    !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME) {
430		aclp = EXTATTR_CONTENT(eap);
431		acllen = EXTATTR_CONTENT_SIZE(eap);
432		...
433	}
434.Ed
435.Pp
436The following code creates an extended attribute
437containing a copy of a structure
438.Fa mygif :
439.Bd -literal
440	eap->ea_namespace = EXTATTR_NAMESPACE_USER;
441	strcpy(eap->ea_name, "filepic.gif");
442	EXTATTR_SET_LENGTHS(eap, sizeof(struct mygif));
443	memcpy(EXTATTR_CONTENT(eap), &mygif, sizeof(struct mygif));
444.Ed
445.Sh HISTORY
446A super-block structure named filsys appeared in
447.At v6 .
448The file system described in this manual appeared
449in
450.Bx 4.2 .
451