-#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. */
- bitmap_init (&free_map, disk_size (disk));
- 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);
-}
-
+#include "filesys/filesys.h"
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include "filesys/file.h"
+#include "filesys/free-map.h"
+#include "filesys/inode.h"
+#include "filesys/directory.h"
+#include "devices/disk.h"
+
+/* The disk that contains the filesystem. */
+struct disk *filesys_disk;
+
+static void do_format (void);
+
+/* Initializes the filesystem module.
+ If FORMAT is true, reformats the filesystem. */
void
filesys_init (bool format)
{
- disk = disk_get (1);
- if (disk == NULL)
- panic ("ide1:1 not present, filesystem initialization failed");
+ filesys_disk = disk_get (0, 1);
+ if (filesys_disk == NULL)
+ PANIC ("hd0:1 (hdb) not present, filesystem initialization failed");
+
+ inode_init ();
+ free_map_init ();
if (format)
do_format ();
-
- file_open (&free_map_file, FREE_MAP_SECTOR);
- file_open (&root_dir_file, ROOT_DIR_SECTOR);
+
+ free_map_open ();
}
+/* Shuts down the filesystem module, writing any unwritten data
+ to disk. */
+void
+filesys_done (void)
+{
+ free_map_close ();
+}
+\f
+/* Creates a file named NAME with the given INITIAL_SIZE.
+ Returns true if successful, false otherwise.
+ Fails if a file named NAME already exists,
+ or if internal memory allocation fails. */
bool
filesys_create (const char *name, off_t initial_size)
{
- struct dir dir;
- struct bitmap free_map;
- disk_sector_no hdr_sector;
- struct filehdr filehdr;
- bool success = false;
-
- /* Read the root directory. */
- dir_init (&dir, NUM_DIR_ENTRIES);
- dir_read (&dir, &root_dir_file);
- if (dir_lookup (&dir, name, NULL))
- goto exit1;
-
- /* Allocate a block for the file header. */
- bitmap_init (&free_map, disk_size (disk));
- bitmapio_read (&free_map, &free_map_file);
- hdr_sector = bitmap_find_and_set (&free_map);
- if (hdr_sector == BITMAP_ERROR)
- goto exit2;
-
- /* Add the file to the directory. */
- if (!dir_add (&dir, name, hdr_sector))
- goto exit2;
-
- /* Allocate space for the file. */
- if (!filehdr_allocate (&filehdr, initial_size))
- goto exit2;
-
- /* Write everything back. */
- filehdr_write (&filehdr, hdr_sector);
- dir_write (&dir, &root_dir_file);
- bitmapio_write (&free_map, &free_map_file);
-
- success = true;
-
- /* Clean up. */
- filehdr_destroy (&filehdr);
- exit2:
- bitmap_destroy (&free_map);
- exit1:
- dir_destroy (&dir);
+ struct dir *dir;
+ disk_sector_t inode_sector = 0;
+ bool success = (dir_open_root (&dir)
+ && free_map_allocate (1, &inode_sector)
+ && inode_create (inode_sector, initial_size)
+ && dir_add (dir, name, inode_sector));
+ if (!success && inode_sector != 0)
+ free_map_release (inode_sector, 1);
+ dir_close (dir);
return success;
}
+/* Opens the file with the given NAME.
+ Returns the new file if successful or a null pointer
+ otherwise.
+ Fails if no file named NAME exists,
+ or if an internal memory allocation fails. */
+struct file *
+filesys_open (const char *name)
+{
+ struct dir *dir;
+ struct inode *inode = NULL;
+
+ if (dir_open_root (&dir))
+ dir_lookup (dir, name, &inode);
+ dir_close (dir);
+
+ return file_open (inode);
+}
+
+/* Deletes the file named NAME.
+ Returns true if successful, false on failure.
+ Fails if no file named NAME exists,
+ or if an internal memory allocation fails. */
bool
filesys_remove (const char *name)
{
- struct dir dir;
- disk_sector_no hdr_sector;
- struct filehdr filehdr;
- struct bitmap free_map;
- bool success = false;
-
- /* Read the root directory. */
- dir_init (&dir, NUM_DIR_ENTRIES);
- dir_read (&dir, &root_dir_file);
- if (!dir_lookup (&dir, name, &hdr_sector))
- goto exit;
-
- /* Read the file header. */
- filehdr_read (&filehdr, hdr_sector);
-
- /* Allocate a block for the file header. */
- bitmap_init (&free_map, disk_size (disk));
- bitmapio_read (&free_map, &free_map_file);
-
- /* Deallocate. */
- filehdr_deallocate (&filehdr, &free_map);
- bitmap_reset (&free_map, hdr_sector);
- dir_remove (&dir, name);
-
- /* Write everything back. */
- bitmapio_write (&free_map, &free_map_file);
- dir_write (&dir, &root_dir_file);
-
- success = true;
-
- /* Clean up. */
- filehdr_destroy (&filehdr);
- bitmap_destroy (&free_map);
- exit:
- dir_destroy (&dir);
+ struct dir *dir = NULL;
+ bool success = (dir_open_root (&dir)
+ && dir_remove (dir, name));
+ dir_close (dir);
return success;
}
-#undef NDEBUG
-#include "debug.h"
-#include "file.h"
+/* Prints a list of files in the filesystem to the system
+ console.
+ Returns true if successful, false on failure,
+ which occurs only if an internal memory allocation fails. */
+bool
+filesys_list (void)
+{
+ struct dir *dir = NULL;
+ bool success = dir_open_root (&dir);
+ if (success)
+ dir_list (dir);
+ dir_close (dir);
+
+ return success;
+}
+\f
+static void must_succeed_function (int, bool) NO_INLINE;
+#define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
+/* Performs basic sanity checks on the filesystem.
+ The filesystem should not contain a file named `foo' when
+ called. */
void
filesys_self_test (void)
{
static const char s[] = "This is a test string.";
+ static const char zeros[sizeof s] = {0};
struct file *file;
char s2[sizeof s];
+ int i;
+
+ filesys_remove ("foo");
+ for (i = 0; i < 2; i++)
+ {
+ /* Create file and check that it contains zeros
+ throughout the created length. */
+ MUST_SUCCEED (filesys_create ("foo", sizeof s));
+ MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
+ MUST_SUCCEED (file_read (file, s2, sizeof s2) == sizeof s2);
+ MUST_SUCCEED (memcmp (s2, zeros, sizeof s) == 0);
+ MUST_SUCCEED (file_tell (file) == sizeof s);
+ MUST_SUCCEED (file_length (file) == sizeof s);
+ file_close (file);
+
+ /* Reopen file and write to it. */
+ MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
+ MUST_SUCCEED (file_write (file, s, sizeof s) == sizeof s);
+ MUST_SUCCEED (file_tell (file) == sizeof s);
+ MUST_SUCCEED (file_length (file) == sizeof s);
+ file_close (file);
+
+ /* Reopen file and verify that it reads back correctly.
+ Delete file while open to check proper semantics. */
+ MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
+ MUST_SUCCEED (filesys_remove ("foo"));
+ MUST_SUCCEED (filesys_open ("foo") == NULL);
+ MUST_SUCCEED (file_read (file, s2, sizeof s) == sizeof s);
+ MUST_SUCCEED (memcmp (s, s2, sizeof s) == 0);
+ MUST_SUCCEED (file_tell (file) == sizeof s);
+ MUST_SUCCEED (file_length (file) == sizeof s);
+ file_close (file);
+ }
+
+ printf ("filesys: self test ok\n");
+}
- ASSERT (filesys_create ("foo"));
- ASSERT ((file = filesys_open ("foo")) != NULL);
- ASSERT (file_write (file, s, sizeof s) == sizeof s);
- ASSERT (file_tell (file) == sizeof s);
- ASSERT (file_length (file) == sizeof s);
- file_close (file);
-
- ASSERT ((file = filesys_open ("foo")) != NULL);
- ASSERT (file_read (file, s2, sizeof s2) == sizeof s2);
- ASSERT (memcmp (s, s2, sizeof s) == 0);
- ASSERT (file_tell (file) == sizeof s2);
- ASSERT (file_length (file) == sizeof s2);
- file_close (file);
-
- ASSERT (filesys_remove ("foo"));
+/* If SUCCESS is false, panics with an error complaining about
+ LINE_NO. */
+static void
+must_succeed_function (int line_no, bool success)
+{
+ if (!success)
+ PANIC ("filesys_self_test: operation failed on line %d", line_no);
+}
+\f
+/* Formats the filesystem. */
+static void
+do_format (void)
+{
+ printf ("Formatting filesystem...");
+ free_map_create ();
+ if (!dir_create (ROOT_DIR_SECTOR, 16))
+ PANIC ("root directory creation failed");
+ free_map_close ();
+ printf ("done.\n");
}