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