Switch the base file system from direct-indexed inodes to extents.
[pintos-anon] / doc / filesys.texi
1 @node Project 4--File Systems, References, Project 3--Virtual Memory, Top
2 @chapter Project 4: File Systems
3
4 In the previous two assignments, you made extensive use of a
5 filesystem without actually worrying about how it was implemented
6 underneath.  For this last assignment, you will fill in the
7 implementation of the filesystem.  You will be working primarily in
8 the @file{filesys} directory.
9
10 You should build on the code you wrote for the previous assignments.
11 However, if you wish, you may turn off your VM features, as they are
12 not vital to making the filesystem work.  (You will need to edit
13 @file{filesys/Makefile.vars} to fully disable VM.)  All of the
14 functionality needed for project 2 (argument passing, syscalls and
15 multiprogramming) must work in your filesys submission.
16
17 On the other hand, one of the particular charms of working on
18 operating systems is being able to use what you build, and building
19 full-featured systems.  Therefore, you should strive to make all the
20 parts work together so that you can run VM and your filesystem at the
21 same time.  Plus, keeping VM is a great way to stress-test your
22 filesystem implementation.
23
24 Your submission should define @code{THREAD_JOIN_IMPLEMENTED} in
25 @file{constants.h} (@pxref{Conditional Compilation}).
26
27 @menu
28 * File System New Code::        
29 * File System Synchronization::  
30 * Problem 4-1 Indexed Files::     
31 * Problem 4-2 File Growth::     
32 * Problem 4-3 Subdirectories::  
33 * Problem 4-4 Buffer Cache::    
34 * File System Design Document Requirements::  
35 * File System FAQ::             
36 @end menu
37
38 @node File System New Code
39 @section New Code
40
41 Here are some files that are probably new to you.  These are in the
42 @file{filesys} directory except where indicated:
43
44 @table @file
45 @item fsutil.c
46 Simple utilities for the filesystem that are accessible from the
47 kernel command line.
48
49 @item filesys.h
50 @itemx filesys.c
51 Top-level interface to the file system.  Please read the long comment
52 near the top of @file{filesys.c}, which introduces some details of the
53 file system code as provided.
54
55 @item directory.h
56 @itemx directory.c
57 Translates file names to inodes.  The directory data structure is
58 stored as a file.
59
60 @item inode.h
61 @itemx inode.c
62 Manages the data structure representing the layout of a
63 file's data on disk.
64
65 @item file.h
66 @itemx file.c
67 Translates file reads and writes to disk sector reads
68 and writes.
69
70 @item lib/kernel/bitmap.h
71 @itemx lib/kernel/bitmap.c
72 A bitmap data structure along with routines for reading and writing
73 the bitmap to disk files.
74 @end table
75
76 Our file system has a Unix-like interface, so you may also wish to
77 read the Unix man pages for @code{creat}, @code{open}, @code{close},
78 @code{read}, @code{write}, @code{lseek}, and @code{unlink}.  Our file
79 system has calls that are similar, but not identical, to these.  The
80 file system translates these calls into physical disk operations.  
81
82 All the basic functionality is there in the code above, so that the
83 filesystem is usable right off the bat.  In fact, you've been using it
84 in the previous two projects.  However, it has severe limitations
85 which you will remove.
86
87 While most of your work will be in @file{filesys}, you should be
88 prepared for interactions with all previous parts (as usual).
89
90 @node File System Synchronization
91 @section Synchronization
92
93 The provided file system requires external synchronization, that is,
94 callers must ensure that only one thread can be running in the file
95 system code at once.  Your submission should use a finer-grained
96 synchronization strategy.  You will need to consider synchronization
97 issues for each type of file system object.  The provided code uses the
98 following strategies:
99
100 @itemize @bullet
101 @item
102 The free map and root directory are read each time they are needed for
103 an operation, and if they are modified, they are written back before the
104 operation completes.  Thus, the free map is always consistent from an
105 external viewpoint.
106
107 @item
108 Inodes are immutable in the provided file system, that is, their content
109 never changes between creation and deletion.  Furthermore, only one copy
110 of an inode's data is maintained in memory at once, even if the file is
111 open in multiple contexts.
112
113 @item
114 File data doesn't have to be consistent because it's just not part of
115 the model.  In Unix and many other operating systems, a read of a file
116 by one process when the file is being written by another process can
117 show inconsistent results: it can show that none, all, or part of the
118 write has completed.  (However, after the write system call returns to
119 its caller, all subsequent readers must see the change.)  Similarly,
120 when two threads write to the same part of a file at the same time,
121 their data may be interleaved.  External synchronization of the provided
122 file system ensures that reads and writes are fully serialized, but your
123 file system doesn't have to maintain full serialization as long as it
124 follows the rules above.
125 @end itemize
126
127 @node Problem 4-1 Indexed Files
128 @section Problem 4-1: Indexed Files
129
130 The basic file system allocates files as a single extent, making it
131 vulnerable to external fragmentation.  Eliminate this problem by
132 modifying the inode structure.  In practice, this probably means using
133 an index structure with direct, indirect, and doubly indirect blocks.
134 (You are welcome to choose a different scheme as long as you explain the
135 rationale for it in your design documentation, and as long as it does
136 not suffer from external fragmentation.)
137
138 You can assume that the disk will not be larger than 8 MB.  You must
139 support files as large as the disk (minus metadata).  Each inode is
140 stored in one disk sector, limiting the number of block pointers that it
141 can contain.  Supporting 8 MB files will require you to implement
142 doubly-indirect blocks.
143
144 @node Problem 4-2 File Growth
145 @section Problem 4-2: File Growth
146
147 Implement extensible files.  In the basic file system, the file size
148 is specified when the file is created.  One advantage of this is that
149 the inode data structure, once created, never changes.  In UNIX and
150 most other file systems, a file is initially created with size 0 and
151 is then expanded every time a write is made off the end of the file.
152 Modify the file system to allow this.  As one test case, allow the
153 root directory file to expand beyond its current limit of ten files.
154 Make sure that concurrent accesses to the inode remain properly
155 synchronized.
156
157 The user is allowed to seek beyond the current end-of-file (EOF).  The
158 seek itself does not extend the file.  Writing at a position past EOF
159 extends the file to the position being written, and any gap between the
160 previous EOF and the start of the write must be filled with zeros.  A
161 read past EOF returns zero bytes.
162
163 Writing far beyond EOF can cause many blocks to be entirely zero.  Some
164 file systems allocate and write real data blocks for these implicitly
165 zeroed blocks.  Other file systems do not allocate these blocks at all
166 until they are explicitly written.  The latter file systems are said to
167 support ``sparse files.''  You may adopt either allocation strategy in
168 your file system.
169
170 @node Problem 4-3 Subdirectories
171 @section Problem 4-3: Subdirectories
172
173 Implement a hierarchical name space.  In the basic file system, all
174 files live in a single directory.  Modify this to allow directories to
175 point to either files or other directories.  To do this, you will need
176 to implement routines that parse path names into a sequence of
177 directories, as well as routines that change the current working
178 directory and that list the contents of the current directory.  For
179 performance, allow concurrent updates to different directories, but
180 use mutual exclusion to ensure that updates to the same directory are
181 performed atomically (for example, to ensure that a file is deleted
182 only once).
183
184 Make sure that directories can expand beyond their original size just
185 as any other file can.
186
187 Each process has its own current directory.  When one process starts
188 another with the @code{exec} system call, the child process inherits its
189 parent's current directory.  After that, the two processes' current
190 directories are independent, so that either changing its own current
191 directory has no effect on the other.
192
193 Update the existing system calls so that, anywhere a file name is
194 provided by the caller, an absolute or relative path name may used.
195 Also, implement the following new system calls:
196
197 @table @code
198 @item SYS_chdir
199 @itemx bool chdir (const char *@var{dir})
200 Attempts to change the current working directory of the process to
201 @var{dir}, which may be either relative or absolute.  Returns true if
202 successful, false on failure.
203
204 @item SYS_mkdir
205 @itemx bool mkdir (const char *dir)
206 Attempts to create the directory named @var{dir}, which may be either
207 relative or absolute.  Returns true if successful, false on failure.
208
209 @item SYS_lsdir
210 @itemx void lsdir (void)
211 Prints a list of files in the current directory to @code{stdout}, one
212 per line.
213 @end table
214
215 Also write the @command{ls} and @command{mkdir} user programs.  This
216 is straightforward once the above syscalls are implemented.  In Unix,
217 these are programs rather than built-in shell commands, but
218 @command{cd} is a shell command.  (Why?)
219
220 @node Problem 4-4 Buffer Cache
221 @section Problem 4-4: Buffer Cache
222
223 Modify the file system to keep a cache of file blocks.  When a request
224 is made to read or write a block, check to see if it is stored in the
225 cache, and if so, fetch it immediately from the cache without going to
226 disk.  (Otherwise, fetch the block from disk into cache, evicting an
227 older entry if necessary.)  You are limited to a cache no greater than
228 64 sectors in size.  Be sure to choose an intelligent cache
229 replacement algorithm.  Experiment to see what combination of accessed,
230 dirty, and other information results in the best performance, as
231 measured by the number of disk accesses.  (For example, metadata is
232 generally more valuable to cache than data.)  Document your
233 replacement algorithm in your design document.
234
235 The provided file system code uses a ``bounce buffer'' in @struct{file}
236 to translate the disk's sector-by-sector interface into the system call
237 interface's byte-by-byte interface.  It needs per-file buffers because,
238 without them, there's no other good place to put sector
239 data.@footnote{The stack is not a good place because large objects
240 should not be allocated on the stack.  A 512-byte sector is pushing the
241 limit there.}  As part of implementing the buffer cache, you should get
242 rid of these bounce buffers.  Instead, copy data into and out of sectors
243 in the buffer cache directly.  You will probably need some
244 synchronization to prevent sectors from being evicted from the cache
245 while you are using them.
246
247 In addition to the basic file caching scheme, your implementation
248 should also include the following enhancements:
249
250 @table @b
251 @item write-behind:
252 Instead of always immediately writing modified data to disk, dirty
253 blocks can be kept in the cache and written out sometime later.  Your
254 buffer cache should write behind whenever a block is evicted from the
255 cache.
256
257 @item read-ahead:
258 Your buffer cache should automatically fetch the next block of a file
259 into the cache when one block of a file is read, in case that block is
260 about to be read.
261 @end table
262
263 For each of these three optimizations, design a file I/O workload that
264 is likely to benefit from the enhancement, explain why you expect it
265 to perform better than on the original file system implementation, and
266 demonstrate the performance improvement.
267
268 Note that write-behind makes your filesystem more fragile in the face
269 of crashes.  Therefore, you should
270 periodically write all cached blocks to disk.  If you have
271 @func{timer_sleep} from the first project working, this is an
272 excellent application for it.  (If you're still using the base
273 implementation of @func{timer_sleep}, be aware that it busy-waits, which
274 is not an acceptable solution.) If @func{timer_sleep}'s delays seem too
275 short or too long, reread the explanation of the @option{-r} option to
276 @command{pintos} (@pxref{Debugging versus Testing}).
277
278 Likewise, read-ahead is only really useful when done asynchronously.
279 That is, if a process wants disk block 1 from the file, it needs to
280 block until disk block 1 is read in, but once that read is complete,
281 control should return to the process immediately while the read
282 request for disk block 2 is handled asynchronously.  In other words,
283 the process will block to wait for disk block 1, but should not block
284 waiting for disk block 2.
285
286 When you're implementing this, please make sure you have a scheme for
287 making any read-ahead and write-behind threads halt when Pintos is
288 ``done'' (when the user program has completed, etc), so that Pintos
289 will halt normally and the disk contents will be consistent.
290
291 @node File System Design Document Requirements
292 @section Design Document Requirements
293
294 As always, submit a design document file summarizing your design.  Be
295 sure to cover the following points:
296
297 @itemize @bullet
298 @item
299 How did you choose to synchronize file system operations?
300
301 @item
302 How did you structure your inodes? How many blocks did you access
303 directly, via single-indirection, and/or via double-indirection?  Why?
304
305 @item
306 How did you structure your buffer cache? How did you perform a lookup
307 in the cache? How did you choose elements to evict from the cache?
308
309 @item
310 How and when did you flush the cache?
311 @end itemize
312
313 @node File System FAQ
314 @section FAQ
315
316 @enumerate 1
317 @item
318 @b{What extra credit opportunities are available for this assignment?}
319
320 @itemize @bullet
321 @item
322 We'll give out extra credit to groups that implement Unix-style
323 support for @file{.} and @file{..} in relative paths in their projects.
324
325 @item
326 We'll give some extra credit if you submit with VM enabled.  If you do
327 this, make sure you show us that you can run multiple programs
328 concurrently.  A particularly good demonstration is running
329 @file{capitalize} (with a reduced words file that fits comfortably on
330 your disk, of course).  So submit a file system disk that contains a
331 VM-heavy program like @file{capitalize}, so we can try it out.  And also
332 include the results in your test case file.
333
334 We feel that you will be much more satisfied with your cs140 ``final
335 product'' if you can get your VM working with your file system.  It's
336 also a great stress test for your FS, but obviously you have to be
337 pretty confident with your VM if you're going to submit this extra
338 credit, since you'll still lose points for failing FS-related tests,
339 even if the problem is in your VM code.
340
341 @item
342 A point of extra credit can be assigned if a user can recursively
343 remove directories from the shell command prompt.  Note that the
344 typical semantic is to just fail if a directory is not empty.
345 @end itemize
346
347 Make sure that you discuss any extra credit in your @file{README}
348 file.  We're likely to miss it if it gets buried in your design
349 document.
350
351 @item
352 @b{What exec modes for running Pintos do I absolutely need to
353 support?}
354
355 You also need to support the @option{-f}, @option{-ci}, @option{-co},
356 and @option{-ex} flags individually, and you need to handle them when
357 they're combined, like this: @samp{pintos -f -ci shell 12345 -ex
358 "shell"}.  Thus, you should be able to treat the above as equivalent to:
359
360 @example
361 pintos -f
362 pintos -ci shell 12345
363 pintos -ex "shell"
364 @end example
365
366 If you don't change the filesystem interface, then this should already
367 be implemented properly in @file{threads/init.c} and
368 @file{filesys/fsutil.c}.
369
370 You must also implement the @option{-q} option and make sure that data
371 gets flushed out to disk properly when it is used.
372
373 @item
374 @b{Will you test our file system with a different @code{DISK_SECTOR_SIZE}?}
375
376 No, @code{DISK_SECTOR_SIZE} is fixed at 512.  This is a fixed property
377 of IDE disk hardware.
378
379 @item
380 @b{Will the @struct{inode} take up space on the disk too?}
381
382 Yes.  Anything stored in @struct{inode} takes up space on disk,
383 so you must include this in your calculation of how many entires will
384 fit in a single disk sector.
385
386 @item
387 @b{What's the directory separator character?}
388
389 Forward slash (@samp{/}).
390 @end enumerate
391
392 @menu
393 * Problem 4-2 File Growth FAQ::  
394 * Problem 4-3 Subdirectory FAQ::  
395 * Problem 4-4 Buffer Cache FAQ::  
396 @end menu
397
398 @node Problem 4-2 File Growth FAQ
399 @subsection Problem 4-2: File Growth FAQ
400
401 @enumerate 1
402 @item
403 @b{What is the largest file size that we are supposed to support?}
404
405 The disk we create will be 8 MB or smaller.  However, individual files
406 will have to be smaller than the disk to accommodate the metadata.
407 You'll need to consider this when deciding your @struct{inode}
408 organization.
409 @end enumerate
410
411 @node Problem 4-3 Subdirectory FAQ
412 @subsection Problem 4-3: Subdirectory FAQ
413
414 @enumerate 1
415 @item
416 @b{What's the answer to the question in the spec about why
417 @command{ls} and @command{mkdir} are user programs, while @command{cd}
418 is a shell command?}
419
420 Each process maintains its own current working directory, so it's much
421 easier to change the current working directory of the shell process if
422 @command{cd} is implemented as a shell command rather than as another
423 user process.  In fact, Unix-like systems don't provide any way for
424 one process to change another process's current working directory.
425
426 @item
427 @b{When the spec states that directories should be able to grow beyond
428 ten files, does this mean that there can still be a set maximum number
429 of files per directory that is greater than ten, or should directories
430 now support unlimited growth (bounded by the maximum supported file
431 size)?}
432
433 We're looking for directories that can support arbitrarily large
434 numbers of files.  Now that directories can grow, we want you to
435 remove the concept of a preset maximum file limit.
436
437 @item
438 @b{When should the @code{lsdir} system call return?}
439
440 The @code{lsdir} system call should not return until after the
441 directory has been printed.  Here's a code fragment, and the desired
442 output:
443
444 @example
445 printf ("Start of directory\n");
446 lsdir ();
447 printf ("End of directory\n");
448 @end example
449
450 This code should create the following output:
451
452 @example
453 Start of directory
454 ...  directory contents ...
455 End of directory
456 @end example
457
458 @item
459 @b{Do we have to implement both absolute and relative pathnames?}
460
461 Yes.  Implementing @file{.} and @file{..} is extra credit, though.
462
463 @item
464 @b{Should @func{remove} also be able to remove directories?}
465
466 Yes.  The @code{remove} system call should handle removal of both
467 regular files and directories.  You may assume that directories can
468 only be deleted if they are empty, as in Unix.
469 @end enumerate
470
471 @node Problem 4-4 Buffer Cache FAQ
472 @subsection Problem 4-4: Buffer Cache FAQ
473
474 @enumerate 1
475 @item
476 @b{We're limited to a 64-block cache, but can we also keep an
477 @struct{inode_disk} inside @struct{inode}, the way the provided code
478 does?}
479
480 The goal of the 64-block limit is to bound the amount of cached file
481 system data.  If you keep a block of disk data---whether file data or
482 metadata---anywhere in kernel memory then you have to count it against
483 the 64-block limit.  The same rule applies to anything that's
484 ``similar'' to a block of disk data, such as a @struct{inode_disk}
485 without the @code{length} or @code{sector_cnt} members.
486
487 You can keep a cached copy of the free map in memory permanently if you
488 like.  It doesn't have to count against the cache size.
489
490 That means you'll have to change the way the inode implementation
491 accesses its corresponding on-disk inode right now, since it currently
492 just embeds a @struct{inode_disk} in @struct{inode} and reads the
493 corresponding sector in from disk when it's created.  Keeping extra
494 copies of inodes would be cheating the 64-block limitation that we place
495 on your cache.
496
497 You can store pointers to inode data in @struct{inode}, if you want, and
498 you can store some other small amount of information to help you find
499 the inode when you need it.  Similarly, if you want to store one block
500 of data plus some small amount of metadata for each of your 64 cache
501 entries, that's fine.
502
503 If you look at @func{inode_byte_to_sector}, it uses the
504 @struct{inode_disk} directly without having first read in that sector
505 from wherever it was in the storage hierarchy.  This will no longer
506 work.  You will need to change @func{inode_byte_to_sector} so that it
507 reads the @struct{inode_disk} from the storage hierarchy before using
508 it.
509 @end enumerate