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