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