Clarify.
[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.
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 Problem 4-1 Large Files
88 @section Problem 4-1: Large Files
89
90 Modify the file system to allow the maximum size of a file to be as
91 large as the disk.  You can assume that the disk will not be larger
92 than 8 MB.  In the basic file system, each file is limited to a file
93 size of just under 64 kB.  Each file has a header called an index node
94 or @dfn{inode} (represented by @struct{inode}) that is a table of
95 direct pointers to the disk blocks for that file.  Since the inode is
96 stored in one disk sector, the maximum size of a file is limited by
97 the number of pointers that will fit in one disk sector.  Increasing
98 the limit to 8 MB will require you to implement doubly-indirect
99 blocks.
100
101 @node Problem 4-2 File Growth
102 @section Problem 4-2: File Growth
103
104 Implement extensible files.  In the basic file system, the file size
105 is specified when the file is created.  One advantage of this is that
106 the inode data structure, once created, never changes.  In UNIX and
107 most other file systems, a file is initially created with size 0 and
108 is then expanded every time a write is made off the end of the file.
109 Modify the file system to allow this.  As one test case, allow the
110 root directory file to expand beyond its current limit of ten files.
111 Make sure that concurrent accesses to the inode remain properly
112 synchronized.
113
114 @node Problem 4-3 Subdirectories
115 @section Problem 4-3: Subdirectories
116
117 Implement a hierarchical name space.  In the basic file system, all
118 files live in a single directory.  Modify this to allow directories to
119 point to either files or other directories.  To do this, you will need
120 to implement routines that parse path names into a sequence of
121 directories, as well as routines that change the current working
122 directory and that list the contents of the current directory.  For
123 performance, allow concurrent updates to different directories, but
124 use mutual exclusion to ensure that updates to the same directory are
125 performed atomically (for example, to ensure that a file is deleted
126 only once).
127
128 Make sure that directories can expand beyond their original size just
129 as any other file can.
130
131 Update the existing system calls so that, anywhere a file name is
132 provided by the caller, an absolute or relative path name may used.
133 Also, implement the following new system calls:
134
135 @table @code
136 @item SYS_chdir
137 @itemx bool chdir (const char *@var{dir})
138 Attempts to change the current working directory of the process to
139 @var{dir}, which may be either relative or absolute.  Returns true if
140 successful, false on failure.
141
142 @item SYS_mkdir
143 @itemx bool mkdir (const char *dir)
144 Attempts to create the directory named @var{dir}, which may be either
145 relative or absolute.  Returns true if successful, false on failure.
146
147 @item SYS_lsdir
148 @itemx void lsdir (void)
149 Prints a list of files in the current directory to @code{stdout}, one
150 per line.
151 @end table
152
153 Also write the @command{ls} and @command{mkdir} user programs.  This
154 is straightforward once the above syscalls are implemented.  In Unix,
155 these are programs rather than built-in shell commands, but
156 @command{cd} is a shell command.  (Why?)
157
158 @node Problem 4-4 Buffer Cache
159 @section Problem 4-4: Buffer Cache
160
161 Modify the file system to keep a cache of file blocks.  When a request
162 is made to read or write a block, check to see if it is stored in the
163 cache, and if so, fetch it immediately from the cache without going to
164 disk.  (Otherwise, fetch the block from disk into cache, evicting an
165 older entry if necessary.)  You are limited to a cache no greater than
166 64 sectors in size.  Be sure to choose an intelligent cache
167 replacement algorithm.  Experiment to see what combination of accessed,
168 dirty, and other information results in the best performance, as
169 measured by the number of disk accesses.  (For example, metadata is
170 generally more valuable to cache than data.)  Document your
171 replacement algorithm in your design document.
172
173 The provided file system code uses a ``bounce buffer'' in @struct{file}
174 to translate the disk's sector-by-sector interface into the system call
175 interface's byte-by-byte interface.  It needs per-file buffers because,
176 without them, there's no other good place to put sector
177 data.@footnote{The stack is not a good place because large objects
178 should not be allocated on the stack.  A 512-byte sector is pushing the
179 limit there.}  As part of implementing the buffer cache, you should get
180 rid of these bounce buffers.  Instead, copy data into and out of sectors
181 in the buffer cache directly.  You will probably need some
182 synchronization to prevent sectors from being evicted from the cache
183 while you are using them.
184
185 In addition to the basic file caching scheme, your implementation
186 should also include the following enhancements:
187
188 @table @b
189 @item write-behind:
190 Instead of always immediately writing modified data to disk, dirty
191 blocks can be kept in the cache and written out sometime later.  Your
192 buffer cache should write behind whenever a block is evicted from the
193 cache.
194
195 @item read-ahead:
196 Your buffer cache should automatically fetch the next block of a file
197 into the cache when one block of a file is read, in case that block is
198 about to be read.
199 @end table
200
201 For each of these three optimizations, design a file I/O workload that
202 is likely to benefit from the enhancement, explain why you expect it
203 to perform better than on the original file system implementation, and
204 demonstrate the performance improvement.
205
206 Note that write-behind makes your filesystem more fragile in the face
207 of crashes.  Therefore, you should
208 periodically write all cached blocks to disk.  If you have
209 @func{timer_sleep} from the first project working, this is an
210 excellent application for it.
211
212 Likewise, read-ahead is only really useful when done asynchronously.
213 That is, if a process wants disk block 1 from the file, it needs to
214 block until disk block 1 is read in, but once that read is complete,
215 control should return to the process immediately while the read
216 request for disk block 2 is handled asynchronously.  In other words,
217 the process will block to wait for disk block 1, but should not block
218 waiting for disk block 2.
219
220 When you're implementing this, please make sure you have a scheme for
221 making any read-ahead and write-behind threads halt when Pintos is
222 ``done'' (when the user program has completed, etc), so that Pintos
223 will halt normally and the disk contents will be consistent.
224
225 @node File System Design Document Requirements
226 @section Design Document Requirements
227
228 As always, submit a design document file summarizing your design.  Be
229 sure to cover the following points:
230
231 @itemize @bullet
232 @item
233 How did you structure your inodes? How many blocks did you access
234 directly, via single-indirection, and/or via double-indirection?  Why?
235
236 @item
237 How did you structure your buffer cache? How did you perform a lookup
238 in the cache? How did you choose elements to evict from the cache?
239
240 @item
241 How and when did you flush the cache?
242 @end itemize
243
244 @node File System FAQ
245 @section FAQ
246
247 @enumerate 1
248 @item
249 @b{What extra credit opportunities are available for this assignment?}
250
251 @itemize @bullet
252 @item
253 We'll give out extra credit to groups that implement Unix-style
254 support for @file{.} and @file{..} in relative paths in their projects.
255
256 @item
257 We'll give some extra credit if you submit with VM enabled.  If you do
258 this, make sure you show us that you can run multiple programs
259 concurrently.  A particularly good demonstration is running
260 @file{capitalize} (with a reduced words file that fits comfortably on
261 your disk, of course).  So submit a file system disk that contains a
262 VM-heavy program like @file{capitalize}, so we can try it out.  And also
263 include the results in your test case file.
264
265 We feel that you will be much more satisfied with your cs140 ``final
266 product'' if you can get your VM working with your file system.  It's
267 also a great stress test for your FS, but obviously you have to be
268 pretty confident with your VM if you're going to submit this extra
269 credit, since you'll still lose points for failing FS-related tests,
270 even if the problem is in your VM code.
271
272 @item
273 A point of extra credit can be assigned if a user can recursively
274 remove directories from the shell command prompt.  Note that the
275 typical semantic is to just fail if a directory is not empty.
276 @end itemize
277
278 Make sure that you discuss any extra credit in your @file{README}
279 file.  We're likely to miss it if it gets buried in your design
280 document.
281
282 @item
283 @b{What exec modes for running Pintos do I absolutely need to
284 support?}
285
286 You also need to support the @option{-f}, @option{-ci}, @option{-co},
287 and @option{-ex} flags individually, and you need to handle them when
288 they're combined, like this: @samp{pintos -f -ci shell 12345 -ex
289 "shell"}.  Thus, you should be able to treat the above as equivalent to:
290
291 @example
292 pintos -f
293 pintos -ci shell 12345
294 pintos -ex "shell"
295 @end example
296
297 If you don't change the filesystem interface, then this should already
298 be implemented properly in @file{threads/init.c} and
299 @file{filesys/fsutil.c}.
300
301 You must also implement the @option{-q} option and make sure that data
302 gets flushed out to disk properly when it is used.
303
304 @item
305 @b{Will you test our file system with a different @code{DISK_SECTOR_SIZE}?}
306
307 No, @code{DISK_SECTOR_SIZE} is fixed at 512.  This is a fixed property
308 of IDE disk hardware.
309
310 @item
311 @b{Will the @struct{inode} take up space on the disk too?}
312
313 Yes.  Anything stored in @struct{inode} takes up space on disk,
314 so you must include this in your calculation of how many entires will
315 fit in a single disk sector.
316
317 @item
318 @b{What's the directory separator character?}
319
320 Forward slash (@samp{/}).
321 @end enumerate
322
323 @menu
324 * Problem 4-2 File Growth FAQ::  
325 * Problem 4-3 Subdirectory FAQ::  
326 * Problem 4-4 Buffer Cache FAQ::  
327 @end menu
328
329 @node Problem 4-2 File Growth FAQ
330 @subsection Problem 4-2: File Growth FAQ
331
332 @enumerate 1
333 @item
334 @b{What is the largest file size that we are supposed to support?}
335
336 The disk we create will be 8 MB or smaller.  However, individual files
337 will have to be smaller than the disk to accommodate the metadata.
338 You'll need to consider this when deciding your @struct{inode}
339 organization.
340 @end enumerate
341
342 @node Problem 4-3 Subdirectory FAQ
343 @subsection Problem 4-3: Subdirectory FAQ
344
345 @enumerate 1
346 @item
347 @b{What's the answer to the question in the spec about why
348 @command{ls} and @command{mkdir} are user programs, while @command{cd}
349 is a shell command?}
350
351 Each process maintains its own current working directory, so it's much
352 easier to change the current working directory of the shell process if
353 @command{cd} is implemented as a shell command rather than as another
354 user process.  In fact, Unix-like systems don't provide any way for
355 one process to change another process's current working directory.
356
357 @item
358 @b{When the spec states that directories should be able to grow beyond
359 ten files, does this mean that there can still be a set maximum number
360 of files per directory that is greater than ten, or should directories
361 now support unlimited growth (bounded by the maximum supported file
362 size)?}
363
364 We're looking for directories that can support arbitrarily large
365 numbers of files.  Now that directories can grow, we want you to
366 remove the concept of a preset maximum file limit.
367
368 @item
369 @b{When should the @code{lsdir} system call return?}
370
371 The @code{lsdir} system call should not return until after the
372 directory has been printed.  Here's a code fragment, and the desired
373 output:
374
375 @example
376 printf ("Start of directory\n");
377 lsdir ();
378 printf ("End of directory\n");
379 @end example
380
381 This code should create the following output:
382
383 @example
384 Start of directory
385 ...  directory contents ...
386 End of directory
387 @end example
388
389 @item
390 @b{Do we have to implement both absolute and relative pathnames?}
391
392 Yes.  Implementing @file{.} and @file{..} is extra credit, though.
393
394 @item
395 @b{Should @func{remove} also be able to remove directories?}
396
397 Yes.  The @code{remove} system call should handle removal of both
398 regular files and directories.  You may assume that directories can
399 only be deleted if they are empty, as in Unix.
400 @end enumerate
401
402 @node Problem 4-4 Buffer Cache FAQ
403 @subsection Problem 4-4: Buffer Cache FAQ
404
405 @enumerate 1
406 @item
407 @b{We're limited to a 64-block cache, but can we also keep a copy of
408 each @struct{inode} for an open file inside @struct{file},
409 the way the stub code does?}
410
411 No, you shouldn't keep any disk sectors stored anywhere outside the
412 cache.  That means you'll have to change the way the file
413 implementation accesses its corresponding inode right now, since it
414 currently just creates a new @struct{inode} in its constructor
415 and reads the corresponding sector in from disk when it's created.
416
417 There are two reasons for not storing inodes in @struct{file}.
418 First, keeping extra copies of inodes would be cheating the 64-block
419 limitation that we place on your cache.  Second, if two processes have
420 the same file open, you will create a huge synchronization headache
421 for yourself if each @struct{file} has its own copy of the inode.
422
423 Note that you can store pointers to inodes in @struct{file} if
424 you want, and you can store some other small amount of information to
425 help you find the inode when you need it.
426
427 Similarly, if you want to store one block of data plus some small
428 amount of metadata for each of your 64 cache entries, that's fine.
429
430 @item
431 @b{But why can't we store copies of inodes in @struct{file}? We
432 don't understand the answer to the previous question.}
433
434 The issue regarding storing @struct{inode}s has to do with
435 implementation of the buffer cache.  Basically, you can't store a
436 @code{struct inode *} in @struct{inode}.  Each time you need
437 to read a @struct{inode}, you'll have to get it either from the
438 buffer cache or from disk.
439
440 If you look at @func{file_read_at}, it uses the inode directly
441 without having first read in that sector from wherever it was in the
442 storage hierarchy.  You are no longer allowed to do this.  You will
443 need to change @code{file_read_at} (and similar functions) so that it
444 reads the inode from the storage hierarchy before using it.
445 @end enumerate