1 .\" $NetBSD: 2.t,v 1.2 1995/03/18 14:56:08 cgd Exp $
3 .\" Copyright (c) 1982, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)2.t 8.1 (Berkeley) 6/5/93
32 .ds RH Overview of the file system
34 Overview of the file system
36 The file system is discussed in detail in [Mckusick84];
37 this section gives a brief overview.
41 A file system is described by its
43 The super-block is built when the file system is created (\c
47 contains the basic parameters of the file system,
48 such as the number of data blocks it contains
49 and a count of the maximum number of files.
50 Because the super-block contains critical data,
52 replicates it to protect against catastrophic loss.
54 .I "default super block"
55 always resides at a fixed offset from the beginning
56 of the file system's disk partition.
58 .I "redundant super blocks"
59 are not referenced unless a head crash
60 or other hard disk error causes the default super-block
62 The redundant blocks are sprinkled throughout the disk partition.
64 Within the file system are files.
65 Certain files are distinguished as directories and contain collections
66 of pointers to files that may themselves be directories.
67 Every file has a descriptor associated with it called an
69 The inode contains information describing ownership of the file,
70 time stamps indicating modification and access times for the file,
71 and an array of indices pointing to the data blocks for the file.
73 we assume that the first 12 blocks
74 of the file are directly referenced by values stored
75 in the inode structure itself\(dg.
77 \(dgThe actual number may vary from system to system, but is usually in
80 The inode structure may also contain references to indirect blocks
81 containing further data block indices.
82 In a file system with a 4096 byte block size, a singly indirect
83 block contains 1024 further block addresses,
84 a doubly indirect block contains 1024 addresses of further single indirect
86 and a triply indirect block contains 1024 addresses of further doubly indirect
87 blocks (the triple indirect block is never needed in practice).
89 In order to create files with up to
91 using only two levels of indirection,
92 the minimum size of a file system block is 4096 bytes.
93 The size of file system blocks can be any power of two
94 greater than or equal to 4096.
95 The block size of the file system is maintained in the super-block,
96 so it is possible for file systems of different block sizes
97 to be accessible simultaneously on the same system.
98 The block size must be decided when
100 creates the file system;
101 the block size cannot be subsequently
102 changed without rebuilding the file system.
106 Associated with the super block is non replicated
107 .I "summary information" .
108 The summary information changes
109 as the file system is modified.
110 The summary information contains
111 the number of blocks, fragments, inodes and directories in the file system.
115 The file system partitions the disk into one or more areas called
116 .I "cylinder groups".
117 A cylinder group is comprised of one or more consecutive
119 Each cylinder group includes inode slots for files, a
121 describing available blocks in the cylinder group,
122 and summary information describing the usage of data blocks
123 within the cylinder group.
124 A fixed number of inodes is allocated for each cylinder group
125 when the file system is created.
126 The current policy is to allocate one inode for each 2048
128 this is expected to be far more inodes than will ever be needed.
130 All the cylinder group bookkeeping information could be
131 placed at the beginning of each cylinder group.
132 However if this approach were used,
133 all the redundant information would be on the top platter.
134 A single hardware failure that destroyed the top platter
135 could cause the loss of all copies of the redundant super-blocks.
136 Thus the cylinder group bookkeeping information
137 begins at a floating offset from the beginning of the cylinder group.
141 cylinder group is about one track further
142 from the beginning of the cylinder group
148 information spirals down into the pack;
149 any single track, cylinder,
150 or platter can be lost without losing all copies of the super-blocks.
151 Except for the first cylinder group,
152 the space between the beginning of the cylinder group
153 and the beginning of the cylinder group information stores data.
157 To avoid waste in storing small files,
158 the file system space allocator divides a single
159 file system block into one or more
161 The fragmentation of the file system is specified
162 when the file system is created;
163 each file system block can be optionally broken into
164 2, 4, or 8 addressable fragments.
165 The lower bound on the size of these fragments is constrained
166 by the disk sector size;
167 typically 512 bytes is the lower bound on fragment size.
168 The block map associated with each cylinder group
169 records the space availability at the fragment level.
170 Aligned fragments are examined
171 to determine block availability.
173 On a file system with a block size of 4096 bytes
174 and a fragment size of 1024 bytes,
175 a file is represented by zero or more 4096 byte blocks of data,
176 and possibly a single fragmented block.
177 If a file system block must be fragmented to obtain
178 space for a small amount of data,
179 the remainder of the block is made available for allocation
182 consider an 11000 byte file stored on
183 a 4096/1024 byte file system.
184 This file uses two full size blocks and a 3072 byte fragment.
185 If no fragments with at least 3072 bytes
186 are available when the file is created,
187 a full size block is split yielding the necessary 3072 byte
188 fragment and an unused 1024 byte fragment.
189 This remaining fragment can be allocated to another file, as needed.
191 Updates to the file system
193 Every working day hundreds of files
194 are created, modified, and removed.
195 Every time a file is modified,
196 the operating system performs a
197 series of file system updates.
198 These updates, when written on disk, yield a consistent file system.
199 The file system stages
200 all modifications of critical information;
202 either be completed or cleanly backed out after a crash.
203 Knowing the information that is first written to the file system,
204 deterministic procedures can be developed to
205 repair a corrupted file system.
206 To understand this process,
207 the order that the update
208 requests were being honored must first be understood.
210 When a user program does an operation to change the file system,
213 the data to be written is copied into an internal
215 buffer in the kernel.
216 Normally, the disk update is handled asynchronously;
217 the user process is allowed to proceed even though
218 the data has not yet been written to the disk.
220 along with the inode information reflecting the change,
221 is eventually written out to disk.
222 The real disk write may not happen until long after the
224 system call has returned.
225 Thus at any given time, the file system,
226 as it resides on the disk,
227 lags the state of the file system represented by the in-core information.
229 The disk information is updated to reflect the in-core information
230 when the buffer is required for another use,
233 is done (at 30 second intervals) by
234 .I "/etc/update" "(8),"
235 or by manual operator intervention with the
238 If the system is halted without writing out the in-core information,
239 the file system on the disk will be in an inconsistent state.
241 If all updates are done asynchronously, several serious
242 inconsistencies can arise.
243 One inconsistency is that a block may be claimed by two inodes.
244 Such an inconsistency can occur when the system is halted before
245 the pointer to the block in the old inode has been cleared
246 in the copy of the old inode on the disk,
247 and after the pointer to the block in the new inode has been written out
248 to the copy of the new inode on the disk.
250 there is no deterministic method for deciding
251 which inode should really claim the block.
252 A similar problem can arise with a multiply claimed inode.
254 The problem with asynchronous inode updates
255 can be avoided by doing all inode deallocations synchronously.
257 inodes and indirect blocks are written to the disk synchronously
258 (\fIi.e.\fP the process blocks until the information is
259 really written to disk)
260 when they are being deallocated.
261 Similarly inodes are kept consistent by synchronously
262 deleting, adding, or changing directory entries.
263 .ds RH Fixing corrupted file systems