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