-#include "filesys.h"
-#include "disk.h"
-#include "directory.h"
-
-static struct disk *disk;
-
-static struct file free_map_file, root_dir_file;
-
-#define FREE_MAP_SECTOR 0
-#define ROOT_DIR_SECTOR 1
-
-#define NUM_DIR_ENTRIES 10
-#define ROOT_DIR_FILE_SIZE (sizeof (struct dir_entry) * NUM_DIR_ENTRIES)
-
-static void
-do_format (void)
-{
- struct bitmap free_map;
- struct filehdr map_hdr, dir_hdr;
- struct dir dir;
-
- /* Create the initial bitmap and reserve sectors for the
- free map and root directory file headers. */
- if (!bitmap_init (&free_map, disk_size (disk)))
- panic ("bitmap creation failed");
- bitmap_mark (&free_map, FREE_MAP_SECTOR);
- bitmap_mark (&free_map, ROOT_DIR_SECTOR);
-
- /* Allocate data sector(s) for the free map file
- and write its file header to disk. */
- if (!filehdr_allocate (&map_hdr, bitmap_storage_size (&free_map)))
- panic ("free map creation failed");
- filehdr_write (&map_hdr, FREE_MAP_SECTOR);
- filehdr_destroy (&map_hdr);
-
- /* Allocate data sector(s) for the root directory file
- and write its file header to disk. */
- if (!filehdr_allocate (&dir_hdr, ROOT_DIR_FILE_SIZE))
- panic ("root directory creation failed");
- filehdr_write (&dir_hdr, FREE_MAP_SECTOR);
- filehdr_destroy (&dir_hdr);
-
- /* Write out the free map now that we have space reserved
- for it. */
- file_open (&free_map_file, FREE_MAP_SECTOR);
- bitmapio_write (&free_map, free_map_file);
- bitmap_destroy (&free_map);
- file_close (&free_map_file);
-
- /* Write out the root directory in the same way. */
- file_open (&root_dir_file, ROOT_DIR_SECTOR);
- if (!dir_init (&dir, NUM_DIR_ENTRIES))
- panic ("can't initialize root directory");
- dir_write (root_dir_file);
- dir_destroy (&dir);
- file_close (&free_map_file);
-}
-
+/* This file is derived from source code for the Nachos
+ instructional operating system. The Nachos copyright notice
+ is reproduced in full below. */
+
+/* Copyright (c) 1992-1996 The Regents of the University of California.
+ All rights reserved.
+
+ Permission to use, copy, modify, and distribute this software
+ and its documentation for any purpose, without fee, and
+ without written agreement is hereby granted, provided that the
+ above copyright notice and the following two paragraphs appear
+ in all copies of this software.
+
+ IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO
+ ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
+ CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE
+ AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA
+ HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
+ BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
+ PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
+ MODIFICATIONS.
+*/
+
+#include "filesys/filesys.h"
+#include <bitmap.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include "filesys/file.h"
+#include "filesys/inode.h"
+#include "filesys/directory.h"
+#include "devices/disk.h"
+
+/* Filesystem.
+
+ For the purposes of the "user processes" assignment (project
+ 2), please treat all the code in the filesys directory as a
+ black box. No changes should be needed. For that project, a
+ single lock external to the filesystem code suffices.
+
+ The filesystem consists of a set of files. Each file has a
+ header called an `index node' or `inode', represented by
+ struct inode, that is stored by itself in a single sector (see
+ inode.h). The header contains the file's length in bytes and
+ an array that lists the sector numbers for the file's
+ contents.
+
+ Two files are special. The first special file is the free
+ map, whose inode is always stored in sector 0
+ (FREE_MAP_SECTOR). The free map stores a bitmap (see
+ lib/bitmap.h) that contains one bit for each sector on the
+ disk. Each bit that corresponds to a sector within a file is
+ set to true, and the other bits, which are not part of any
+ file, are set to false.
+
+ The second special file is the root directory, whose inode is
+ always stored in sector 1 (ROOT_DIR_SECTOR). The root
+ directory file stores an array of `struct dir_entry' (see
+ directory.h), each of which, if it is in use, associates a
+ filename with the sector of the file's inode.
+
+ The filesystem implemented here has the following limitations:
+
+ - No synchronization. Concurrent accesses will interfere
+ with one another, so external synchronization is needed.
+
+ - File size is fixed at creation time. Because the root
+ directory is represented as a file, the number of files
+ that may be created is also limited.
+
+ - No indirect blocks. This limits maximum file size to the
+ number of sector pointers that fit in a single inode
+ times the size of a sector, or 126 * 512 == 63 kB given
+ 32-bit sizes and 512-byte sectors.
+
+ - No subdirectories.
+
+ - Filenames limited to 14 characters.
+
+ - A system crash mid-operation may corrupt the disk in a way
+ that cannot be repaired automatically. No `fsck' tool is
+ provided in any case.
+
+ However one important feature is included:
+
+ - Unix-like semantics for filesys_remove() are implemented.
+ That is, if a file is open when it is removed, its blocks
+ are not deallocated and it may still be accessed by the
+ threads that have it open until the last one closes it. */
+
+/* Sectors of system file inodes. */
+#define FREE_MAP_SECTOR 0 /* Free map file inode sector. */
+#define ROOT_DIR_SECTOR 1 /* Root directory file inode sector. */
+
+/* Root directory. */
+#define NUM_DIR_ENTRIES 10 /* Maximum number of directory entries. */
+
+/* The disk that contains the filesystem. */
+struct disk *filesys_disk;
+
+/* The free map and root directory files.
+ These files are opened by filesys_init() and never closed. */
+struct file *free_map_file, *root_dir_file;
+
+static void do_format (void);
+
+/* Initializes the filesystem module.
+ If FORMAT is true, reformats the filesystem. */