Update.
[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 file system without actually worrying about how it was implemented
6 underneath.  For this last assignment, you will fill in the
7 implementation of the file system.  You will be working primarily in
8 the @file{filesys} directory.
9
10 You may build project 4 on top of project 2 or project 3.  In either
11 case, all of the functionality needed for project 2 must work in your
12 filesys submission.  If you build on project 3, then all of the project
13 3 functionality must work also, and you will need to edit
14 @file{filesys/Make.vars} to enable VM functionality.  You can receive up
15 to 5% extra credit if you do enable VM.
16
17 @menu
18 * Project 4 Background::        
19 * Project 4 Requirements::      
20 * Project 4 FAQ::             
21 @end menu
22
23 @node Project 4 Background
24 @section Background
25
26 @menu
27 * File System New Code::        
28 @end menu
29
30 @node File System New Code
31 @subsection New Code
32
33 Here are some files that are probably new to you.  These are in the
34 @file{filesys} directory except where indicated:
35
36 @table @file
37 @item fsutil.c
38 Simple utilities for the file system that are accessible from the
39 kernel command line.
40
41 @item filesys.h
42 @itemx filesys.c
43 Top-level interface to the file system.  @xref{Using the File System},
44 for an introduction.
45
46 @item directory.h
47 @itemx directory.c
48 Translates file names to inodes.  The directory data structure is
49 stored as a file.
50
51 @item inode.h
52 @itemx inode.c
53 Manages the data structure representing the layout of a
54 file's data on disk.
55
56 @item file.h
57 @itemx file.c
58 Translates file reads and writes to disk sector reads
59 and writes.
60
61 @item lib/kernel/bitmap.h
62 @itemx lib/kernel/bitmap.c
63 A bitmap data structure along with routines for reading and writing
64 the bitmap to disk files.
65 @end table
66
67 Our file system has a Unix-like interface, so you may also wish to
68 read the Unix man pages for @code{creat}, @code{open}, @code{close},
69 @code{read}, @code{write}, @code{lseek}, and @code{unlink}.  Our file
70 system has calls that are similar, but not identical, to these.  The
71 file system translates these calls into physical disk operations.  
72
73 All the basic functionality is there in the code above, so that the
74 file system is usable from the start, as you've seen
75 in the previous two projects.  However, it has severe limitations
76 which you will remove.
77
78 While most of your work will be in @file{filesys}, you should be
79 prepared for interactions with all previous parts (as usual).
80
81 @node Project 4 Requirements
82 @section Requirements
83
84 @menu
85 * Project 4 Design Document::   
86 * Indexed and Extensible Files::  
87 * Subdirectories::              
88 * Buffer Cache::                
89 * File System Synchronization::  
90 @end menu
91
92 @node Project 4 Design Document
93 @subsection Design Document
94
95 Before you turn in your project, you must copy @uref{filesys.tmpl, , the
96 project 4 design document template} into your source tree under the name
97 @file{pintos/src/filesys/DESIGNDOC} and fill it in.  We recommend that
98 you read the design document template before you start working on the
99 project.  @xref{Project Documentation}, for a sample design document
100 that goes along with a fictitious project.
101
102 @node Indexed and Extensible Files
103 @subsection Indexed and Extensible Files
104
105 The basic file system allocates files as a single extent, making it
106 vulnerable to external fragmentation.  Eliminate this problem by
107 modifying the on-disk inode structure.  In practice, this probably means using
108 an index structure with direct, indirect, and doubly indirect blocks.
109 (You are welcome to choose a different scheme as long as you explain the
110 rationale for it in your design documentation, and as long as it does
111 not suffer from external fragmentation.)
112
113 You can assume that the disk will not be larger than 8 MB.  You must
114 support files as large as the disk (minus metadata).  Each inode is
115 stored in one disk sector, limiting the number of block pointers that it
116 can contain.  Supporting 8 MB files will require you to implement
117 doubly-indirect blocks.
118
119 An extent-based file can only grow if it is followed by empty space, but
120 with indexed inodes file growth is possible whenever free space is
121 available.  Implement file growth.  In the basic file system, the file
122 size is specified when the file is created.  In UNIX and most other file
123 systems, a file is initially created with size 0 and is then expanded
124 every time a write is made off the end of the file.  Your file system
125 must allow this.
126
127 There should be no predetermined limit on the size of a file, except
128 that a disk cannot exceed the size of the disk (minus metadata).  This
129 also applies to the root directory file, which should now be allowed
130 to expand beyond its initial limit of 16 files.
131
132 The user is allowed to seek beyond the current end-of-file (EOF).  The
133 seek itself does not extend the file.  Writing at a position past EOF
134 extends the file to the position being written, and any gap between the
135 previous EOF and the start of the write must be filled with zeros.  A
136 read starting from a position past EOF returns no bytes.
137
138 Writing far beyond EOF can cause many blocks to be entirely zero.  Some
139 file systems allocate and write real data blocks for these implicitly
140 zeroed blocks.  Other file systems do not allocate these blocks at all
141 until they are explicitly written.  The latter file systems are said to
142 support ``sparse files.''  You may adopt either allocation strategy in
143 your file system.
144
145 @node Subdirectories
146 @subsection Subdirectories
147
148 Implement a hierarchical name space.  In the basic file system, all
149 files live in a single directory.  Modify this to allow directory
150 entries to point to files or to other directories.
151
152 Make sure that directories can expand beyond their original size just
153 as any other file can.  
154
155 The basic file system has a 14-character limit on file names.  You may
156 retain this limit for individual file name components, or may extend
157 it, at your option.  You must allow full path names to be
158 much longer than 14 characters.
159
160 The current directory is maintained separately for each process.  At
161 startup, the initial process's current directory is the root directory.
162 When one process starts another with the @code{exec} system call, the
163 child process inherits its parent's current directory.  After that, the
164 two processes' current directories are independent, so that either
165 changing its own current directory has no effect on the other.
166
167 Update the existing system calls so that, anywhere a file name is
168 provided by the caller, an absolute or relative path name may used.
169
170 Update the @code{remove} system call so that it can delete empty
171 directories in addition to regular files.  Directories can only be
172 deleted if they do not contain any files or subdirectories.
173
174 Implement the following new system calls:
175
176 @deftypefn {System Call} bool chdir (const char *@var{dir})
177 Changes the current working directory of the process to
178 @var{dir}, which may be relative or absolute.  Returns true if
179 successful, false on failure.
180 @end deftypefn
181
182 @deftypefn {System Call} bool mkdir (const char *@var{dir})
183 Creates the directory named @var{dir}, which may be
184 relative or absolute.  Returns true if successful, false on failure.
185 Fails if @var{dir} already exists or if any directory name in
186 @var{dir}, besides the last, does not already exist.  That is,
187 @code{mkdir("/a/b/c")} succeeds only if @file{/a/b} already exists and
188 @file{/a/b/c} does not.
189 @end deftypefn
190
191 @deftypefn {System Call} void lsdir (void)
192 Prints a list of files in the current directory to @code{stdout}, one
193 per line, in no particular order.
194 @end deftypefn
195
196 We have provided @command{ls} and @command{mkdir} user programs, which
197 are straightforward once the above syscalls are implemented.  In Unix,
198 these are programs rather than built-in shell commands, but
199 @command{cd} is a shell command.
200
201 The @code{pintos} @option{put} and @option{get} commands should now
202 accept full path names, assuming that the directories used in the
203 paths have already been created.  This should not require any extra
204 effort on your part.
205
206 You may support @file{.} and @file{..} for a small amount of extra
207 credit.
208
209 @node Buffer Cache
210 @subsection Buffer Cache
211
212 Modify the file system to keep a cache of file blocks.  When a request
213 is made to read or write a block, check to see if it is stored in the
214 cache, and if so, fetch it immediately from the cache without going to
215 disk.  Otherwise, fetch the block from disk into cache, evicting an
216 older entry if necessary.  You are limited to a cache no greater than 64
217 sectors in size.
218
219 Be sure to choose an intelligent cache replacement algorithm.
220 Experiment to see what combination of accessed, dirty, and other
221 information results in the best performance, as measured by the number
222 of disk accesses.  For example, metadata is generally more valuable to
223 cache than data.
224
225 You can keep a cached copy of the free map permanently in memory if you
226 like.  It doesn't have to count against the cache size.
227
228 The provided inode code uses a ``bounce buffer'' allocated with
229 @func{malloc} to translate the disk's sector-by-sector interface into
230 the system call interface's byte-by-byte interface.  You should get rid
231 of these bounce buffers.  Instead, copy data into and out of sectors in
232 the buffer cache directly.
233
234 Your implementation should also include the following features:
235
236 @table @b
237 @item write-behind:
238 Keep dirty blocks in the cache, instead of immediately writing modified
239 data to disk.  Write dirty blocks to disk whenever they are evicted.
240 Because write-behind makes your file system more fragile in the face of
241 crashes, in addition you should periodically write all dirty, cached
242 blocks back to disk.  The cache should also be written back to disk in
243 @func{filesys_done}, so that halting Pintos flushes the cache.
244
245 If you have @func{timer_sleep} from the first project working, this is
246 an excellent application for it.  If you're still using the base
247 implementation of @func{timer_sleep}, be aware that it busy-waits, which
248 is not an acceptable solution. If @func{timer_sleep}'s delays seem too
249 short or too long, reread the explanation of the @option{-r} option to
250 @command{pintos} (@pxref{Debugging versus Testing}).
251
252 @item read-ahead:
253 Your buffer cache should automatically fetch the next block of a file
254 into the cache when one block of a file is read, in case that block is
255 about to be read.
256
257 Read-ahead is only really useful when done asynchronously.  That means,
258 if a process requests disk block 1 from the file, it should block until disk
259 block 1 is read in, but once that read is complete, control should
260 return to the process immediately.  The read-ahead request for disk
261 block 2 should be handled asynchronously, in the background.
262 @end table
263
264 @strong{We recommend integrating the cache into your design early.}  In
265 the past, many groups have tried to tack the cache onto a design late in
266 the design process.  This is very difficult.  These groups have often
267 turned in projects that failed most or all of the tests.
268
269 @node File System Synchronization
270 @subsection Synchronization
271
272 The provided file system requires external synchronization, that is,
273 callers must ensure that only one thread can be running in the file
274 system code at once.  Your submission must adopt a finer-grained
275 synchronization strategy that does not require external synchronization.
276 To the extent possible, operations on independent entities should be
277 independent, so that they do not need to wait on each other.
278
279 Operations on different cache blocks must be independent.  In
280 particular, when I/O is required on a particular block, operations on
281 other blocks that do not require I/O should proceed without having to
282 wait for the I/O to complete.
283
284 Multiple processes must be able to access a single file at once.
285 Multiple reads of a single file must be able to complete without
286 waiting for one another.  When writing to a file does not extend the
287 file, multiple processes should also be able to write a single file at
288 once.  A read of a file by one process when the file is being written by
289 another process is allowed to show that none, all, or part of the write
290 has completed.  (However, after the @code{write} system call returns to
291 its caller, all subsequent readers must see the change.)  Similarly,
292 when two processes simultaneously write to the same part of a file,
293 their data may be interleaved.
294
295 On the other hand, extending a file and writing data into the new
296 section must be atomic.  Suppose processes A and B both have a given
297 file open and both are positioned at end-of-file.  If A reads and B
298 writes the file at the same time, A may read all, part, or none of what
299 B writes.  However, A may not read data other than what B writes, e.g.@:
300 if B's data is all nonzero bytes, A is not allowed to see any zeros.
301
302 Operations on different directories should take place concurrently.
303 Operations on the same directory may wait for one another.
304
305 @node Project 4 FAQ
306 @section FAQ
307
308 @table @b
309 @item How much code will I need to write?
310
311 Here's a summary of our reference solution, produced by the
312 @command{diffstat} program.  The final row gives total lines inserted
313 and deleted; a changed line counts as both an insertion and a deletion.
314
315 This summary is relative to the Pintos base code, but we started from
316 the reference solution to project 3.  Thus, the reference solution runs
317 with virtual memory enabled.  @xref{Project 3 FAQ}, for the summary
318 of project 3.
319
320 @verbatim
321  Makefile.build       |    5 
322  devices/timer.c      |   42 ++
323  filesys/Make.vars    |    6 
324  filesys/cache.c      |  473 +++++++++++++++++++++++++
325  filesys/cache.h      |   23 +
326  filesys/directory.c  |   99 ++++-
327  filesys/directory.h  |    3 
328  filesys/file.c       |    4 
329  filesys/filesys.c    |  194 +++++++++-
330  filesys/filesys.h    |    5 
331  filesys/free-map.c   |   45 +-
332  filesys/free-map.h   |    4 
333  filesys/fsutil.c     |    8 
334  filesys/inode.c      |  444 ++++++++++++++++++-----
335  filesys/inode.h      |   11 
336  threads/init.c       |    5 
337  threads/interrupt.c  |    2 
338  threads/thread.c     |   32 +
339  threads/thread.h     |   38 +-
340  userprog/exception.c |   12 
341  userprog/pagedir.c   |   10 
342  userprog/process.c   |  332 +++++++++++++----
343  userprog/syscall.c   |  582 ++++++++++++++++++++++++++++++-
344  userprog/syscall.h   |    1 
345  vm/frame.c           |  161 ++++++++
346  vm/frame.h           |   23 +
347  vm/page.c            |  297 +++++++++++++++
348  vm/page.h            |   50 ++
349  vm/swap.c            |   85 ++++
350  vm/swap.h            |   11 
351  30 files changed, 2721 insertions(+), 286 deletions(-)
352 @end verbatim
353
354 @item What extra credit opportunities are available?
355
356 You may implement Unix-style support for @file{.} and @file{..} in
357 relative paths in their projects.
358
359 You may submit with VM enabled.
360
361 @item Can @code{DISK_SECTOR_SIZE} change?
362
363 No, @code{DISK_SECTOR_SIZE} is fixed at 512.  This is a fixed property
364 of IDE disk hardware.
365
366 @item What's the directory separator character?
367
368 Forward slash (@samp{/}).
369 @end table
370
371 @menu
372 * Indexed Files FAQ::           
373 * Subdirectories FAQ::          
374 * Buffer Cache FAQ::            
375 @end menu
376
377 @node Indexed Files FAQ
378 @subsection Indexed Files FAQ
379
380 @table @b
381 @item What is the largest file size that we are supposed to support?
382
383 The disk we create will be 8 MB or smaller.  However, individual files
384 will have to be smaller than the disk to accommodate the metadata.
385 You'll need to consider this when deciding your inode organization.
386 @end table
387
388 @node Subdirectories FAQ
389 @subsection Subdirectories FAQ
390
391 @table @b
392 @item Why is @command{cd} a shell command?
393
394 The current directory of each process is independent.  A @command{cd}
395 program could change its own current directory, but that would have no
396 effect on the shell.  In fact, Unix-like systems don't provide any way
397 for one process to change another process's current working directory.
398 @end table
399
400 @node Buffer Cache FAQ
401 @subsection Buffer Cache FAQ
402
403 @table @b
404 @item Can we keep a @struct{inode_disk} inside @struct{inode}?
405
406 The goal of the 64-block limit is to bound the amount of cached file
407 system data.  If you keep a block of disk data---whether file data or
408 metadata---anywhere in kernel memory then you have to count it against
409 the 64-block limit.  The same rule applies to anything that's
410 ``similar'' to a block of disk data, such as a @struct{inode_disk}
411 without the @code{length} or @code{sector_cnt} members.
412
413 That means you'll have to change the way the inode implementation
414 accesses its corresponding on-disk inode right now, since it currently
415 just embeds a @struct{inode_disk} in @struct{inode} and reads the
416 corresponding sector from disk when it's created.  Keeping extra
417 copies of inodes would subvert the 64-block limitation that we place
418 on your cache.
419
420 You can store a pointer to inode data in @struct{inode}, if you want,
421 and you can store other information to help you find the inode when you
422 need it.  Similarly, you may store some metadata along each of your 64
423 cache entries.
424
425 You can keep a cached copy of the free map permanently in memory if you
426 like.  It doesn't have to count against the cache size.
427
428 @func{byte_to_sector} in @file{filesys/inode.c} uses the
429 @struct{inode_disk} directly, without first reading that sector from
430 wherever it was in the storage hierarchy.  This will no longer work.
431 You will need to change @func{inode_byte_to_sector} so that it reads the
432 @struct{inode_disk} from the storage hierarchy before using it.
433 @end table