minor optimization
[unix-history] / usr / src / sbin / fsck / SMM.doc / 3.t
CommitLineData
a38b2411
KM
1.\" Copyright (c) 1982 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
145bc69d 5.\" @(#)3.t 4.3 (Berkeley) %G%
a38b2411
KM
6.\"
7.ds RH Fixing corrupted file systems
8.NH
9Fixing corrupted file systems
10.PP
11A file system
12can become corrupted in several ways.
13The most common of these ways are
14improper shutdown procedures
15and hardware failures.
16.PP
17File systems may become corrupted during an
18.I "unclean halt" .
19This happens when proper shutdown
20procedures are not observed,
21physically write-protecting a mounted file system,
22or a mounted file system is taken off-line.
23The most common operator procedural failure is forgetting to
24.I sync
25the system before halting the CPU.
26.PP
27File systems may become further corrupted if proper startup
28procedures are not observed, e.g.,
29not checking a file system for inconsistencies,
30and not repairing inconsistencies.
31Allowing a corrupted file system to be used (and, thus, to be modified
32further) can be disastrous.
33.PP
34Any piece of hardware can fail at any time.
35Failures
36can be as subtle as a bad block
37on a disk pack, or as blatant as a non-functional disk-controller.
38.NH 2
39Detecting and correcting corruption
40.PP
41Normally
42.I fsck
43is run non-interactively.
44In this mode it will only fix
45corruptions that are expected to occur from an unclean halt.
46These actions are a proper subset of the actions that
47.I fsck
48will take when it is running interactively.
49Throughout this paper we assume that
50.I fsck
51is being run interactively,
52and all possible errors can be encountered.
53When an inconsistency is discovered in this mode,
54.I fsck
55reports the inconsistency for the operator to
56chose a corrective action.
57.PP
58A quiescent\(dd
59.FS
60\(dd I.e., unmounted and not being written on.
61.FE
62file system may be checked for structural integrity
63by performing consistency checks on the
64redundant data intrinsic to a file system.
65The redundant data is either read from
66the file system,
67or computed from other known values.
68The file system
69.B must
70be in a quiescent state when
71.I fsck
72is run,
73since
74.I fsck
75is a multi-pass program.
76.PP
77In the following sections,
78we discuss methods to discover inconsistencies
79and possible corrective actions
80for the cylinder group blocks, the inodes, the indirect blocks, and
81the data blocks containing directory entries.
82.NH 2
83Super-block checking
84.PP
85The most commonly corrupted item in a file system
86is the summary information
87associated with the super-block.
88The summary information is prone to corruption
89because it is modified with every change to the file
90system's blocks or inodes,
91and is usually corrupted
92after an unclean halt.
93.PP
94The super-block is checked for inconsistencies
95involving file-system size, number of inodes,
96free-block count, and the free-inode count.
97The file-system size must be larger than the
98number of blocks used by the super-block
99and the number of blocks used by the list of inodes.
100The file-system size and layout information
101are the most critical pieces of information for
102.I fsck .
103While there is no way to actually check these sizes,
104since they are statically determined by
105.I newfs ,
106.I fsck
107can check that these sizes are within reasonable bounds.
108All other file system checks require that these sizes be correct.
109If
110.I fsck
111detects corruption in the static parameters of the default super-block,
112.I fsck
113requests the operator to specify the location of an alternate super-block.
114.NH 2
115Free block checking
116.PP
117.I Fsck
118checks that all the blocks
119marked as free in the cylinder group block maps
120are not claimed by any files.
121When all the blocks have been initially accounted for,
122.I fsck
123checks that
124the number of free blocks
125plus the number of blocks claimed by the inodes
126equals the total number of blocks in the file system.
127.PP
128If anything is wrong with the block allocation maps,
129.I fsck
130will rebuild them,
131based on the list it has computed of allocated blocks.
132.PP
133The summary information associated with the super-block
134counts the total number of free blocks within the file system.
135.I Fsck
136compares this count to the
137number of free blocks it found within the file system.
138If the two counts do not agree, then
139.I fsck
140replaces the incorrect count in the summary information
141by the actual free-block count.
142.PP
143The summary information
144counts the total number of free inodes within the file system.
145.I Fsck
146compares this count to the number
147of free inodes it found within the file system.
148If the two counts do not agree, then
149.I fsck
150replaces the incorrect count in the
151summary information by the actual free-inode count.
152.NH 2
153Checking the inode state
154.PP
155An individual inode is not as likely to be corrupted as
156the allocation information.
157However, because of the great number of active inodes,
158a few of the inodes are usually corrupted.
159.PP
160The list of inodes in the file system
161is checked sequentially starting with inode 2
162(inode 0 marks unused inodes;
163inode 1 is saved for future generations)
164and progressing through the last inode in the file system.
165The state of each inode is checked for
166inconsistencies involving format and type,
167link count,
168duplicate blocks,
169bad blocks,
170and inode size.
171.PP
172Each inode contains a mode word.
173This mode word describes the type and state of the inode.
174Inodes must be one of six types:
175regular inode, directory inode, symbolic link inode,
176special block inode, special character inode, or socket inode.
177Inodes may be found in one of three allocation states:
178unallocated, allocated, and neither unallocated nor allocated.
179This last state suggests an incorrectly formated inode.
180An inode can get in this state if
181bad data is written into the inode list.
182The only possible corrective action is for
183.I fsck
184is to clear the inode.
185.NH 2
186Inode links
187.PP
188Each inode counts the
189total number of directory entries
190linked to the inode.
191.I Fsck
192verifies the link count of each inode
193by starting at the root of the file system,
194and descending through the directory structure.
195The actual link count for each inode
196is calculated during the descent.
197.PP
198If the stored link count is non-zero and the actual
199link count is zero,
200then no directory entry appears for the inode.
201If this happens,
202.I fsck
203will place the disconnected file in the
204.I lost+found
205directory.
206If the stored and actual link counts are non-zero and unequal,
207a directory entry may have been added or removed without the inode being
208updated.
209If this happens,
210.I fsck
211replaces the incorrect stored link count by the actual link count.
212.PP
213Each inode contains a list,
214or pointers to
215lists (indirect blocks),
216of all the blocks claimed by the inode.
217Since indirect blocks are owned by an inode,
218inconsistencies in indirect blocks directly
219affect the inode that owns it.
220.PP
221.I Fsck
222compares each block number claimed by an inode
223against a list of already allocated blocks.
224If another inode already claims a block number,
225then the block number is added to a list of
226.I "duplicate blocks" .
227Otherwise, the list of allocated blocks
228is updated to include the block number.
229.PP
230If there are any duplicate blocks,
231.I fsck
232will perform a partial second
233pass over the inode list
234to find the inode of the duplicated block.
235The second pass is needed,
236since without examining the files associated with
237these inodes for correct content,
238not enough information is available
239to determine which inode is corrupted and should be cleared.
240If this condition does arise
241(only hardware failure will cause it),
242then the inode with the earliest
243modify time is usually incorrect,
244and should be cleared.
245If this happens,
246.I fsck
247prompts the operator to clear both inodes.
248The operator must decide which one should be kept
249and which one should be cleared.
250.PP
251.I Fsck
252checks the range of each block number claimed by an inode.
253If the block number is
254lower than the first data block in the file system,
255or greater than the last data block,
256then the block number is a
257.I "bad block number" .
258Many bad blocks in an inode are usually caused by
259an indirect block that was not written to the file system,
260a condition which can only occur if there has been a hardware failure.
261If an inode contains bad block numbers,
262.I fsck
263prompts the operator to clear it.
264.NH 2
265Inode data size
266.PP
267Each inode contains a count of the number of data blocks
268that it contains.
269The number of actual data blocks
270is the sum of the allocated data blocks
271and the indirect blocks.
272.I Fsck
273computes the actual number of data blocks
274and compares that block count against
275the actual number of blocks the inode claims.
276If an inode contains an incorrect count
277.I fsck
278prompts the operator to fix it.
279.PP
280Each inode contains a thirty-two bit size field.
281The size is the number of data bytes
282in the file associated with the inode.
283The consistency of the byte size field is roughly checked
284by computing from the size field the maximum number of blocks
285that should be associated with the inode,
286and comparing that expected block count against
287the actual number of blocks the inode claims.
288.NH 2
289Checking the data associated with an inode
290.PP
291An inode can directly or indirectly
292reference three kinds of data blocks.
293All referenced blocks must be the same kind.
294The three types of data blocks are:
295plain data blocks, symbolic link data blocks, and directory data blocks.
296Plain data blocks
297contain the information stored in a file;
298symbolic link data blocks
299contain the path name stored in a link.
300Directory data blocks contain directory entries.
301.I Fsck
302can only check the validity of directory data blocks.
303.PP
304Each directory data block is checked for
305several types of inconsistencies.
306These inconsistencies include
307directory inode numbers pointing to unallocated inodes,
308directory inode numbers that are greater than
309the number of inodes in the file system,
310incorrect directory inode numbers for ``\fB.\fP'' and ``\fB..\fP'',
311and directories that are not attached to the file system.
312If the inode number in a directory data block
313references an unallocated inode,
314then
315.I fsck
316will remove that directory entry.
317Again,
318this condition can only arise when there has been a hardware failure.
319.PP
320If a directory entry inode number references
321outside the inode list, then
322.I fsck
323will remove that directory entry.
324This condition occurs if bad data is written into a directory data block.
325.PP
326The directory inode number entry for ``\fB.\fP''
327must be the first entry in the directory data block.
328The inode number for ``\fB.\fP''
329must reference itself;
330e.g., it must equal the inode number
331for the directory data block.
332The directory inode number entry
333for ``\fB..\fP'' must be
334the second entry in the directory data block.
335Its value must equal the inode number for the
336parent of the directory entry
337(or the inode number of the directory
338data block if the directory is the
339root directory).
340If the directory inode numbers are
341incorrect,
342.I fsck
343will replace them with the correct values.
145bc69d
KM
344If there are multiple hard links to a directory,
345the first one encountered is considered the real parent
346to which ``\fB..\fP'' should point;
347\fIfsck\P recommends deletion for the subsequently discovered names.
a38b2411
KM
348.NH 2
349File system connectivity
350.PP
351.I Fsck
352checks the general connectivity of the file system.
353If directories are not linked into the file system, then
354.I fsck
355links the directory back into the file system in the
356.I lost+found
357directory.
358This condition only occurs when there has been a hardware failure.
6a1194d8 359.ds RH "References"
a38b2411
KM
360.SH
361\s+2Acknowledgements\s0
362.PP
363I thank Bill Joy, Sam Leffler, Robert Elz and Dennis Ritchie
364for their suggestions and help in implementing the new file system.
365Thanks also to Robert Henry for his editorial input to
366get this document together.
367Finally we thank our sponsors,
368the National Science Foundation under grant MCS80-05144,
369and the Defense Advance Research Projects Agency (DoD) under
370Arpa Order No. 4031 monitored by Naval Electronic System Command under
371Contract No. N00039-82-C-0235. (Kirk McKusick, July 1983)
372.PP
373I would like to thank Larry A. Wehr for advice that lead
374to the first version of
375.I fsck
376and Rick B. Brandt for adapting
377.I fsck
378to
379UNIX/TS. (T. Kowalski, July 1979)
380.sp 2
381.SH
382\s+2References\s0
383.LP
384.IP [Dolotta78] 20
385Dolotta, T. A., and Olsson, S. B. eds.,
6a1194d8
KM
386.I "UNIX User's Manual, Edition 1.1\^" ,
387January 1978.
a38b2411
KM
388.IP [Joy83] 20
389Joy, W., Cooper, E., Fabry, R., Leffler, S., McKusick, M., and Mosher, D.
6a1194d8
KM
3904.2BSD System Manual,
391.I "University of California at Berkeley" ,
392.I "Computer Systems Research Group Technical Report"
393#4, 1982.
394.IP [McKusick84] 20
a38b2411 395McKusick, M., Joy, W., Leffler, S., and Fabry, R.
6a1194d8
KM
396A Fast File System for UNIX,
397\fIACM Transactions on Computer Systems 2\fP, 3.
398pp. 181-197, August 1984.
a38b2411
KM
399.IP [Ritchie78] 20
400Ritchie, D. M., and Thompson, K.,
401The UNIX Time-Sharing System,
402.I "The Bell System Technical Journal"
403.B 57 ,
4046 (July-August 1978, Part 2), pp. 1905-29.
405.IP [Thompson78] 20
406Thompson, K.,
407UNIX Implementation,
408.I "The Bell System Technical Journal\^"
409.B 57 ,
4106 (July-August 1978, Part 2), pp. 1931-46.
411.ds RH Appendix A \- Fsck Error Conditions
412.bp