Remove advertising header from share/
[dragonfly.git] / share / doc / smm / 05.fastfs / 3.t
1 .\" Copyright (c) 1986, 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 .\"     @(#)3.t 8.1 (Berkeley) 6/8/93
29 .\"
30 .ds RH New file system
31 .NH
32 New file system organization
33 .PP
34 In the new file system organization (as in the
35 old file system organization),
36 each disk drive contains one or more file systems.
37 A file system is described by its super-block,
38 located at the beginning of the file system's disk partition.
39 Because the super-block contains critical data,
40 it is replicated to protect against catastrophic loss.
41 This is done when the file system is created;
42 since the super-block data does not change,
43 the copies need not be referenced unless a head crash
44 or other hard disk error causes the default super-block
45 to be unusable.
46 .PP
47 To insure that it is possible to create files as large as
48 .if n 2 ** 32
49 .if t $2 sup 32$
50 bytes with only two levels of indirection,
51 the minimum size of a file system block is 4096 bytes.
52 The size of file system blocks can be any power of two
53 greater than or equal to 4096.
54 The block size of a file system is recorded in the 
55 file system's super-block
56 so it is possible for file systems with different block sizes
57 to be simultaneously accessible on the same system.
58 The block size must be decided at the time that
59 the file system is created;
60 it cannot be subsequently changed without rebuilding the file system.
61 .PP
62 The new file system organization divides a disk partition
63 into one or more areas called
64 .I "cylinder groups".
65 A cylinder group is comprised of one or more consecutive
66 cylinders on a disk.
67 Associated with each cylinder group is some bookkeeping information
68 that includes a redundant copy of the super-block,
69 space for inodes,
70 a bit map describing available blocks in the cylinder group,
71 and summary information describing the usage of data blocks
72 within the cylinder group.
73 The bit map of available blocks in the cylinder group replaces
74 the traditional file system's free list.
75 For each cylinder group a static number of inodes
76 is allocated at file system creation time.
77 The default policy is to allocate one inode for each 2048
78 bytes of space in the cylinder group, expecting this
79 to be far more than will ever be needed. 
80 .PP
81 All the cylinder group bookkeeping information could be
82 placed at the beginning of each cylinder group.
83 However if this approach were used,
84 all the redundant information would be on the top platter.
85 A single hardware failure that destroyed the top platter
86 could cause the loss of all redundant copies of the super-block.
87 Thus the cylinder group bookkeeping information
88 begins at a varying offset from the beginning of the cylinder group.
89 The offset for each successive cylinder group is calculated to be
90 about one track further from the beginning of the cylinder group
91 than the preceding cylinder group.
92 In this way the redundant
93 information spirals down into the pack so that any single track, cylinder,
94 or platter can be lost without losing all copies of the super-block.
95 Except for the first cylinder group,
96 the space between the beginning of the cylinder group
97 and the beginning of the cylinder group information
98 is used for data blocks.\(dg
99 .FS
100 \(dg While it appears that the first cylinder group could be laid
101 out with its super-block at the ``known'' location,
102 this would not work for file systems
103 with blocks sizes of 16 kilobytes or greater.
104 This is because of a requirement that the first 8 kilobytes of the disk
105 be reserved for a bootstrap program and a separate requirement that
106 the cylinder group information begin on a file system block boundary.
107 To start the cylinder group on a file system block boundary,
108 file systems with block sizes larger than 8 kilobytes 
109 would have to leave an empty space between the end of
110 the boot block and the beginning of the cylinder group.
111 Without knowing the size of the file system blocks,
112 the system would not know what roundup function to use
113 to find the beginning of the first cylinder group.
114 .FE
115 .NH 2
116 Optimizing storage utilization
117 .PP
118 Data is laid out so that larger blocks can be transferred
119 in a single disk transaction, greatly increasing file system throughput.
120 As an example, consider a file in the new file system
121 composed of 4096 byte data blocks.
122 In the old file system this file would be composed of 1024 byte blocks.
123 By increasing the block size, disk accesses in the new file
124 system may transfer up to four times as much information per
125 disk transaction.
126 In large files, several
127 4096 byte blocks may be allocated from the same cylinder so that
128 even larger data transfers are possible before requiring a seek.
129 .PP
130 The main problem with 
131 larger blocks is that most UNIX
132 file systems are composed of many small files.
133 A uniformly large block size wastes space.
134 Table 1 shows the effect of file system
135 block size on the amount of wasted space in the file system.
136 The files measured to obtain these figures reside on
137 one of our time sharing
138 systems that has roughly 1.2 gigabytes of on-line storage.
139 The measurements are based on the active user file systems containing
140 about 920 megabytes of formatted space.
141 .KF
142 .DS B
143 .TS
144 box;
145 l|l|l
146 a|n|l.
147 Space used      % waste Organization
148 _
149 775.2 Mb        0.0     Data only, no separation between files
150 807.8 Mb        4.2     Data only, each file starts on 512 byte boundary
151 828.7 Mb        6.9     Data + inodes, 512 byte block UNIX file system
152 866.5 Mb        11.8    Data + inodes, 1024 byte block UNIX file system
153 948.5 Mb        22.4    Data + inodes, 2048 byte block UNIX file system
154 1128.3 Mb       45.6    Data + inodes, 4096 byte block UNIX file system
155 .TE
156 Table 1 \- Amount of wasted space as a function of block size.
157 .DE
158 .KE
159 The space wasted is calculated to be the percentage of space
160 on the disk not containing user data.
161 As the block size on the disk
162 increases, the waste rises quickly, to an intolerable
163 45.6% waste with 4096 byte file system blocks.
164 .PP
165 To be able to use large blocks without undue waste,
166 small files must be stored in a more efficient way.
167 The new file system accomplishes this goal by allowing the division
168 of a single file system block into one or more
169 .I "fragments".
170 The file system fragment size is specified
171 at the time that the file system is created;
172 each file system block can optionally be broken into
173 2, 4, or 8 fragments, each of which is addressable.
174 The lower bound on the size of these fragments is constrained
175 by the disk sector size,
176 typically 512 bytes.
177 The block map associated with each cylinder group
178 records the space available in a cylinder group
179 at the fragment level;
180 to determine if a block is available, aligned fragments are examined.
181 Figure 1 shows a piece of a map from a 4096/1024 file system.
182 .KF
183 .DS B
184 .TS
185 box;
186 l|c c c c.
187 Bits in map     XXXX    XXOO    OOXX    OOOO
188 Fragment numbers        0-3     4-7     8-11    12-15
189 Block numbers   0       1       2       3
190 .TE
191 Figure 1 \- Example layout of blocks and fragments in a 4096/1024 file system.
192 .DE
193 .KE
194 Each bit in the map records the status of a fragment;
195 an ``X'' shows that the fragment is in use,
196 while a ``O'' shows that the fragment is available for allocation.
197 In this example,
198 fragments 0\-5, 10, and 11 are in use,
199 while fragments 6\-9, and 12\-15 are free.
200 Fragments of adjoining blocks cannot be used as a full block,
201 even if they are large enough.
202 In this example,
203 fragments 6\-9 cannot be allocated as a full block;
204 only fragments 12\-15 can be coalesced into a full block.
205 .PP
206 On a file system with a block size of 4096 bytes
207 and a fragment size of 1024 bytes,
208 a file is represented by zero or more 4096 byte blocks of data,
209 and possibly a single fragmented block.
210 If a file system block must be fragmented to obtain
211 space for a small amount of data,
212 the remaining fragments of the block are made
213 available for allocation to other files.
214 As an example consider an 11000 byte file stored on
215 a 4096/1024 byte file system.
216 This file would uses two full size blocks and one
217 three fragment portion of another block.
218 If no block with three aligned fragments is
219 available at the time the file is created,
220 a full size block is split yielding the necessary
221 fragments and a single unused fragment.
222 This remaining fragment can be allocated to another file as needed.
223 .PP
224 Space is allocated to a file when a program does a \fIwrite\fP
225 system call.
226 Each time data is written to a file, the system checks to see if
227 the size of the file has increased*.
228 .FS
229 * A program may be overwriting data in the middle of an existing file
230 in which case space would already have been allocated.
231 .FE
232 If the file needs to be expanded to hold the new data,
233 one of three conditions exists:
234 .IP 1)
235 There is enough space left in an already allocated
236 block or fragment to hold the new data.
237 The new data is written into the available space.
238 .IP 2)
239 The file contains no fragmented blocks (and the last
240 block in the file
241 contains insufficient space to hold the new data).
242 If space exists in a block already allocated,
243 the space is filled with new data.
244 If the remainder of the new data contains more than
245 a full block of data, a full block is allocated and
246 the first full block of new data is written there.
247 This process is repeated until less than a full block
248 of new data remains.
249 If the remaining new data to be written will
250 fit in less than a full block,
251 a block with the necessary fragments is located,
252 otherwise a full block is located.
253 The remaining new data is written into the located space.
254 .IP 3)
255 The file contains one or more fragments (and the 
256 fragments contain insufficient space to hold the new data).
257 If the size of the new data plus the size of the data
258 already in the fragments exceeds the size of a full block,
259 a new block is allocated.
260 The contents of the fragments are copied
261 to the beginning of the block
262 and the remainder of the block is filled with new data.
263 The process then continues as in (2) above.
264 Otherwise, if the new data to be written will
265 fit in less than a full block,
266 a block with the necessary fragments is located,
267 otherwise a full block is located.
268 The contents of the existing fragments
269 appended with the new data
270 are written into the allocated space.
271 .PP
272 The problem with expanding a file one fragment at a
273 a time is that data may be copied many times as a 
274 fragmented block expands to a full block.
275 Fragment reallocation can be minimized
276 if the user program writes a full block at a time,
277 except for a partial block at the end of the file.
278 Since file systems with different block sizes may reside on
279 the same system,
280 the file system interface has been extended to provide
281 application programs the optimal size for a read or write.
282 For files the optimal size is the block size of the file system
283 on which the file is being accessed.
284 For other objects, such as pipes and sockets,
285 the optimal size is the underlying buffer size.
286 This feature is used by the Standard
287 Input/Output Library,
288 a package used by most user programs.
289 This feature is also used by
290 certain system utilities such as archivers and loaders
291 that do their own input and output management
292 and need the highest possible file system bandwidth.
293 .PP
294 The amount of wasted space in the 4096/1024 byte new file system
295 organization is empirically observed to be about the same as in the
296 1024 byte old file system organization.
297 A file system with 4096 byte blocks and 512 byte fragments
298 has about the same amount of wasted space as the 512 byte
299 block UNIX file system.
300 The new file system uses less space
301 than the 512 byte or 1024 byte
302 file systems for indexing information for
303 large files and the same amount of space
304 for small files.
305 These savings are offset by the need to use
306 more space for keeping track of available free blocks.
307 The net result is about the same disk utilization
308 when a new file system's fragment size
309 equals an old file system's block size.
310 .PP
311 In order for the layout policies to be effective,
312 a file system cannot be kept completely full.
313 For each file system there is a parameter, termed
314 the free space reserve, that
315 gives the minimum acceptable percentage of file system
316 blocks that should be free.
317 If the number of free blocks drops below this level
318 only the system administrator can continue to allocate blocks.
319 The value of this parameter may be changed at any time,
320 even when the file system is mounted and active.
321 The transfer rates that appear in section 4 were measured on file
322 systems kept less than 90% full (a reserve of 10%).
323 If the number of free blocks falls to zero,
324 the file system throughput tends to be cut in half,
325 because of the inability of the file system to localize
326 blocks in a file.
327 If a file system's performance degrades because
328 of overfilling, it may be restored by removing
329 files until the amount of free space once again
330 reaches the minimum acceptable level.
331 Access rates for files created during periods of little
332 free space may be restored by moving their data once enough
333 space is available.
334 The free space reserve must be added to the
335 percentage of waste when comparing the organizations given
336 in Table 1.
337 Thus, the percentage of waste in
338 an old 1024 byte UNIX file system is roughly
339 comparable to a new 4096/512 byte file system
340 with the free space reserve set at 5%.
341 (Compare 11.8% wasted with the old file system
342 to 6.9% waste + 5% reserved space in the
343 new file system.)
344 .NH 2 
345 File system parameterization
346 .PP
347 Except for the initial creation of the free list,
348 the old file system ignores the parameters of the underlying hardware.
349 It has no information about either the physical characteristics
350 of the mass storage device,
351 or the hardware that interacts with it.
352 A goal of the new file system is to parameterize the 
353 processor capabilities and
354 mass storage characteristics
355 so that blocks can be allocated in an
356 optimum configuration-dependent way. 
357 Parameters used include the speed of the processor,
358 the hardware support for mass storage transfers,
359 and the characteristics of the mass storage devices.
360 Disk technology is constantly improving and
361 a given installation can have several different disk technologies
362 running on a single processor.
363 Each file system is parameterized so that it can be
364 adapted to the characteristics of the disk on which
365 it is placed.
366 .PP
367 For mass storage devices such as disks,
368 the new file system tries to allocate new blocks
369 on the same cylinder as the previous block in the same file. 
370 Optimally, these new blocks will also be 
371 rotationally well positioned.
372 The distance between ``rotationally optimal'' blocks varies greatly;
373 it can be a consecutive block
374 or a rotationally delayed block
375 depending on system characteristics.
376 On a processor with an input/output channel that does not require
377 any processor intervention between mass storage transfer requests,
378 two consecutive disk blocks can often be accessed
379 without suffering lost time because of an intervening disk revolution.
380 For processors without input/output channels,
381 the main processor must field an interrupt and
382 prepare for a new disk transfer.
383 The expected time to service this interrupt and
384 schedule a new disk transfer depends on the
385 speed of the main processor.
386 .PP
387 The physical characteristics of each disk include
388 the number of blocks per track and the rate at which
389 the disk spins.
390 The allocation routines use this information to calculate
391 the number of milliseconds required to skip over a block.
392 The characteristics of the processor include
393 the expected time to service an interrupt and schedule a
394 new disk transfer.
395 Given a block allocated to a file,
396 the allocation routines calculate the number of blocks to
397 skip over so that the next block in the file will
398 come into position under the disk head in the expected
399 amount of time that it takes to start a new
400 disk transfer operation.
401 For programs that sequentially access large amounts of data,
402 this strategy minimizes the amount of time spent waiting for
403 the disk to position itself.
404 .PP
405 To ease the calculation of finding rotationally optimal blocks,
406 the cylinder group summary information includes
407 a count of the available blocks in a cylinder
408 group at different rotational positions.
409 Eight rotational positions are distinguished,
410 so the resolution of the
411 summary information is 2 milliseconds for a typical 3600
412 revolution per minute drive.
413 The super-block contains a vector of lists called
414 .I "rotational layout tables".
415 The vector is indexed by rotational position.
416 Each component of the vector
417 lists the index into the block map for every data block contained
418 in its rotational position.
419 When looking for an allocatable block,
420 the system first looks through the summary counts for a rotational
421 position with a non-zero block count.
422 It then uses the index of the rotational position to find the appropriate
423 list to use to index through
424 only the relevant parts of the block map to find a free block.
425 .PP
426 The parameter that defines the
427 minimum number of milliseconds between the completion of a data
428 transfer and the initiation of
429 another data transfer on the same cylinder
430 can be changed at any time,
431 even when the file system is mounted and active.
432 If a file system is parameterized to lay out blocks with
433 a rotational separation of 2 milliseconds,
434 and the disk pack is then moved to a system that has a
435 processor requiring 4 milliseconds to schedule a disk operation,
436 the throughput will drop precipitously because of lost disk revolutions
437 on nearly every block.
438 If the eventual target machine is known, 
439 the file system can be parameterized for it
440 even though it is initially created on a different processor.
441 Even if the move is not known in advance,
442 the rotational layout delay can be reconfigured after the disk is moved
443 so that all further allocation is done based on the
444 characteristics of the new host.
445 .NH 2
446 Layout policies
447 .PP
448 The file system layout policies are divided into two distinct parts.
449 At the top level are global policies that use file system
450 wide summary information to make decisions regarding
451 the placement of new inodes and data blocks.
452 These routines are responsible for deciding the
453 placement of new directories and files.
454 They also calculate rotationally optimal block layouts,
455 and decide when to force a long seek to a new cylinder group
456 because there are insufficient blocks left
457 in the current cylinder group to do reasonable layouts.
458 Below the global policy routines are
459 the local allocation routines that use a locally optimal scheme to
460 lay out data blocks.
461 .PP
462 Two methods for improving file system performance are to increase
463 the locality of reference to minimize seek latency 
464 as described by [Trivedi80], and
465 to improve the layout of data to make larger transfers possible
466 as described by [Nevalainen77].
467 The global layout policies try to improve performance
468 by clustering related information.
469 They cannot attempt to localize all data references,
470 but must also try to spread unrelated data
471 among different cylinder groups.
472 If too much localization is attempted,
473 the local cylinder group may run out of space
474 forcing the data to be scattered to non-local cylinder groups.
475 Taken to an extreme,
476 total localization can result in a single huge cluster of data
477 resembling the old file system.
478 The global policies try to balance the two conflicting
479 goals of localizing data that is concurrently accessed
480 while spreading out unrelated data.
481 .PP
482 One allocatable resource is inodes.
483 Inodes are used to describe both files and directories.
484 Inodes of files in the same directory are frequently accessed together.
485 For example, the ``list directory'' command often accesses 
486 the inode for each file in a directory.
487 The layout policy tries to place all the inodes of
488 files in a directory in the same cylinder group.
489 To ensure that files are distributed throughout the disk,
490 a different policy is used for directory allocation.
491 A new directory is placed in a cylinder group that has a greater
492 than average number of free inodes,
493 and the smallest number of directories already in it.
494 The intent of this policy is to allow the inode clustering policy
495 to succeed most of the time.
496 The allocation of inodes within a cylinder group is done using a
497 next free strategy.
498 Although this allocates the inodes randomly within a cylinder group,
499 all the inodes for a particular cylinder group can be read with
500 8 to 16 disk transfers.
501 (At most 16 disk transfers are required because a cylinder
502 group may have no more than 2048 inodes.)
503 This puts a small and constant upper bound on the number of
504 disk transfers required to access the inodes
505 for all the files in a directory.
506 In contrast, the old file system typically requires
507 one disk transfer to fetch the inode for each file in a directory.
508 .PP
509 The other major resource is data blocks.
510 Since data blocks for a file are typically accessed together,
511 the policy routines try to place all data
512 blocks for a file in the same cylinder group,
513 preferably at rotationally optimal positions in the same cylinder.
514 The problem with allocating all the data blocks
515 in the same cylinder group is that large files will
516 quickly use up available space in the cylinder group,
517 forcing a spill over to other areas.
518 Further, using all the space in a cylinder group
519 causes future allocations for any file in the cylinder group
520 to also spill to other areas.
521 Ideally none of the cylinder groups should ever become completely full.
522 The heuristic solution chosen is to
523 redirect block allocation
524 to a different cylinder group
525 when a file exceeds 48 kilobytes,
526 and at every megabyte thereafter.*
527 .FS
528 * The first spill over point at 48 kilobytes is the point
529 at which a file on a 4096 byte block file system first
530 requires a single indirect block.  This appears to be
531 a natural first point at which to redirect block allocation.
532 The other spillover points are chosen with the intent of
533 forcing block allocation to be redirected when a
534 file has used about 25% of the data blocks in a cylinder group.
535 In observing the new file system in day to day use, the heuristics appear
536 to work well in minimizing the number of completely filled
537 cylinder groups.
538 .FE
539 The newly chosen cylinder group is selected from those cylinder
540 groups that have a greater than average number of free blocks left.
541 Although big files tend to be spread out over the disk,
542 a megabyte of data is typically accessible before
543 a long seek must be performed,
544 and the cost of one long seek per megabyte is small.
545 .PP
546 The global policy routines call local allocation routines with 
547 requests for specific blocks.
548 The local allocation routines will
549 always allocate the requested block 
550 if it is free, otherwise it
551 allocates a free block of the requested size that is
552 rotationally closest to the requested block.
553 If the global layout policies had complete information,
554 they could always request unused blocks and
555 the allocation routines would be reduced to simple bookkeeping.
556 However, maintaining complete information is costly;
557 thus the implementation of the global layout policy 
558 uses heuristics that employ only partial information.
559 .PP
560 If a requested block is not available, the local allocator uses
561 a four level allocation strategy:
562 .IP 1)
563 Use the next available block rotationally closest
564 to the requested block on the same cylinder.  It is assumed
565 here that head switching time is zero.  On disk 
566 controllers where this is not the case, it may be possible
567 to incorporate the time required to switch between disk platters
568 when constructing the rotational layout tables.  This, however,
569 has not yet been tried.
570 .IP 2)
571 If there are no blocks available on the same cylinder,
572 use a block within the same cylinder group.
573 .IP 3)
574 If that cylinder group is entirely full, 
575 quadratically hash the cylinder group number to choose
576 another cylinder group to look for a free block.
577 .IP 4)
578 Finally if the hash fails, apply an exhaustive search
579 to all cylinder groups.
580 .PP
581 Quadratic hash is used because of its speed in finding
582 unused slots in nearly full hash tables [Knuth75].
583 File systems that are parameterized to maintain at least
584 10% free space rarely use this strategy.
585 File systems that are run without maintaining any free
586 space typically have so few free blocks that almost any
587 allocation is random;
588 the most important characteristic of
589 the strategy used under such conditions is that the strategy be fast.
590 .ds RH Performance
591 .sp 2
592 .ne 1i