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