Merge branch 'vendor/LIBRESSL'
[dragonfly.git] / share / doc / smm / 03.fsck / 2.t
1 .\" Copyright (c) 1982, 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 .\"     @(#)2.t 8.1 (Berkeley) 6/5/93
29 .\"
30 .ds RH Overview of the file system
31 .NH
32 Overview of the file system
33 .PP
34 The file system is discussed in detail in [Mckusick84];
35 this section gives a brief overview.
36 .NH 2
37 Superblock
38 .PP
39 A file system is described by its
40 .I "super-block" .
41 The super-block is built when the file system is created (\c
42 .I newfs (8))
43 and never changes.
44 The super-block
45 contains the basic parameters of the file system,
46 such as the number of data blocks it contains
47 and a count of the maximum number of files.
48 Because the super-block contains critical data,
49 .I newfs
50 replicates it to protect against catastrophic loss.
51 The
52 .I "default super block"
53 always resides at a fixed offset from the beginning
54 of the file system's disk partition.
55 The
56 .I "redundant super blocks"
57 are not referenced unless a head crash
58 or other hard disk error causes the default super-block
59 to be unusable.
60 The redundant blocks are sprinkled throughout the disk partition.
61 .PP
62 Within the file system are files.
63 Certain files are distinguished as directories and contain collections
64 of pointers to files that may themselves be directories.
65 Every file has a descriptor associated with it called an
66 .I "inode".
67 The inode contains information describing ownership of the file,
68 time stamps indicating modification and access times for the file,
69 and an array of indices pointing to the data blocks for the file.
70 In this section,
71 we assume that the first 12 blocks
72 of the file are directly referenced by values stored
73 in the inode structure itself\(dg.
74 .FS
75 \(dgThe actual number may vary from system to system, but is usually in
76 the range 5-13.
77 .FE
78 The inode structure may also contain references to indirect blocks
79 containing further data block indices.
80 In a file system with a 4096 byte block size, a singly indirect
81 block contains 1024 further block addresses,
82 a doubly indirect block contains 1024 addresses of further single indirect
83 blocks,
84 and a triply indirect block contains 1024 addresses of further doubly indirect
85 blocks (the triple indirect block is never needed in practice).
86 .PP
87 In order to create files with up to
88 2\(ua32 bytes,
89 using only two levels of indirection,
90 the minimum size of a file system block is 4096 bytes.
91 The size of file system blocks can be any power of two
92 greater than or equal to 4096.
93 The block size of the file system is maintained in the super-block,
94 so it is possible for file systems of different block sizes
95 to be accessible simultaneously on the same system.
96 The block size must be decided when
97 .I newfs
98 creates the file system;
99 the block size cannot be subsequently
100 changed without rebuilding the file system.
101 .NH 2
102 Summary information
103 .PP
104 Associated with the super block is non replicated
105 .I "summary information" .
106 The summary information changes
107 as the file system is modified.
108 The summary information contains
109 the number of blocks, fragments, inodes and directories in the file system.
110 .NH 2
111 Cylinder groups
112 .PP
113 The file system partitions the disk into one or more areas called
114 .I "cylinder groups".
115 A cylinder group is comprised of one or more consecutive
116 cylinders on a disk.
117 Each cylinder group includes inode slots for files, a
118 .I "block map"
119 describing available blocks in the cylinder group,
120 and summary information describing the usage of data blocks
121 within the cylinder group.
122 A fixed number of inodes is allocated for each cylinder group
123 when the file system is created.
124 The current policy is to allocate one inode for each 2048
125 bytes of disk space;
126 this is expected to be far more inodes than will ever be needed.
127 .PP
128 All the cylinder group bookkeeping information could be
129 placed at the beginning of each cylinder group.
130 However if this approach were used,
131 all the redundant information would be on the top platter.
132 A single hardware failure that destroyed the top platter
133 could cause the loss of all copies of the redundant super-blocks.
134 Thus the cylinder group bookkeeping information
135 begins at a floating offset from the beginning of the cylinder group.
136 The offset for
137 the
138 .I "i+1" st
139 cylinder group is about one track further
140 from the beginning of the cylinder group
141 than it was for the
142 .I "i" th
143 cylinder group.
144 In this way,
145 the redundant
146 information spirals down into the pack;
147 any single track, cylinder,
148 or platter can be lost without losing all copies of the super-blocks.
149 Except for the first cylinder group,
150 the space between the beginning of the cylinder group
151 and the beginning of the cylinder group information stores data.
152 .NH 2
153 Fragments
154 .PP
155 To avoid waste in storing small files,
156 the file system space allocator divides a single
157 file system block into one or more
158 .I "fragments".
159 The fragmentation of the file system is specified
160 when the file system is created;
161 each file system block can be optionally broken into
162 2, 4, or 8 addressable fragments.
163 The lower bound on the size of these fragments is constrained
164 by the disk sector size;
165 typically 512 bytes is the lower bound on fragment size.
166 The block map associated with each cylinder group
167 records the space availability at the fragment level.
168 Aligned fragments are examined
169 to determine block availability.
170 .PP
171 On a file system with a block size of 4096 bytes
172 and a fragment size of 1024 bytes,
173 a file is represented by zero or more 4096 byte blocks of data,
174 and possibly a single fragmented block.
175 If a file system block must be fragmented to obtain
176 space for a small amount of data,
177 the remainder of the block is made available for allocation
178 to other files.
179 For example,
180 consider an 11000 byte file stored on
181 a 4096/1024 byte file system.
182 This file uses two full size blocks and a 3072 byte fragment.
183 If no fragments with at least 3072 bytes
184 are available when the file is created,
185 a full size block is split yielding the necessary 3072 byte
186 fragment and an unused 1024 byte fragment.
187 This remaining fragment can be allocated to another file, as needed.
188 .NH 2
189 Updates to the file system
190 .PP
191 Every working day hundreds of files
192 are created, modified, and removed.
193 Every time a file is modified,
194 the operating system performs a
195 series of file system updates.
196 These updates, when written on disk, yield a consistent file system.
197 The file system stages
198 all modifications of critical information;
199 modification can
200 either be completed or cleanly backed out after a crash.
201 Knowing the information that is first written to the file system,
202 deterministic procedures can be developed to
203 repair a corrupted file system.
204 To understand this process,
205 the order that the update
206 requests were being honored must first be understood.
207 .PP
208 When a user program does an operation to change the file system,
209 such as a
210 .I write ,
211 the data to be written is copied into an internal
212 .I "in-core"
213 buffer in the kernel.
214 Normally, the disk update is handled asynchronously;
215 the user process is allowed to proceed even though
216 the data has not yet been written to the disk.
217 The data,
218 along with the inode information reflecting the change,
219 is eventually written out to disk.
220 The real disk write may not happen until long after the
221 .I write
222 system call has returned.
223 Thus at any given time, the file system,
224 as it resides on the disk,
225 lags the state of the file system represented by the in-core information.
226 .PP
227 The disk information is updated to reflect the in-core information
228 when the buffer is required for another use,
229 when a
230 .I sync (2)
231 is done (at 30 second intervals) by
232 .I "/etc/update" "(8),"
233 or by manual operator intervention with the
234 .I sync (8)
235 command.
236 If the system is halted without writing out the in-core information,
237 the file system on the disk will be in an inconsistent state.
238 .PP
239 If all updates are done asynchronously, several serious
240 inconsistencies can arise.
241 One inconsistency is that a block may be claimed by two inodes.
242 Such an inconsistency can occur when the system is halted before
243 the pointer to the block in the old inode has been cleared
244 in the copy of the old inode on the disk,
245 and after the pointer to the block in the new inode has been written out
246 to the copy of the new inode on the disk.
247 Here,
248 there is no deterministic method for deciding
249 which inode should really claim the block.
250 A similar problem can arise with a multiply claimed inode.
251 .PP
252 The problem with asynchronous inode updates
253 can be avoided by doing all inode deallocations synchronously.
254 Consequently,
255 inodes and indirect blocks are written to the disk synchronously
256 (\fIi.e.\fP the process blocks until the information is
257 really written to disk)
258 when they are being deallocated.
259 Similarly inodes are kept consistent by synchronously
260 deleting, adding, or changing directory entries.
261 .ds RH Fixing corrupted file systems