X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffilesys%2Ffilesys.c;h=7a53f5ffd924ca7b1b384fd9b46b3256e14f30e6;hb=a03618133f7df0954802a470a4bee7674f7aed45;hp=0ca8e9eb8cef71372934df32c9c148e2065f1fcf;hpb=615bf3b3d2a8573ed6fb9ddc0055745e163ac999;p=pintos-anon diff --git a/src/filesys/filesys.c b/src/filesys/filesys.c index 0ca8e9e..7a53f5f 100644 --- a/src/filesys/filesys.c +++ b/src/filesys/filesys.c @@ -6,21 +6,20 @@ #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; +/* Partition that contains the file system. */ +struct block *fs_device; static void do_format (void); -/* Initializes the filesystem module. - If FORMAT is true, reformats the filesystem. */ +/* Initializes the file system module. + If FORMAT is true, reformats the file system. */ void filesys_init (bool format) { - filesys_disk = disk_get (0, 1); - if (filesys_disk == NULL) - PANIC ("hd0:1 (hdb) not present, filesystem initialization failed"); + fs_device = block_get_role (BLOCK_FILESYS); + if (fs_device == NULL) + PANIC ("No file system device found, can't initialize file system."); inode_init (); free_map_init (); @@ -31,7 +30,7 @@ filesys_init (bool format) free_map_open (); } -/* Shuts down the filesystem module, writing any unwritten data +/* Shuts down the file system module, writing any unwritten data to disk. */ void filesys_done (void) @@ -46,9 +45,9 @@ filesys_done (void) bool filesys_create (const char *name, off_t initial_size) { - struct dir *dir; - disk_sector_t inode_sector = 0; - bool success = (dir_open_root (&dir) + block_sector_t inode_sector = 0; + struct dir *dir = dir_open_root (); + bool success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size) && dir_add (dir, name, inode_sector)); @@ -67,10 +66,10 @@ filesys_create (const char *name, off_t initial_size) struct file * filesys_open (const char *name) { - struct dir *dir; + struct dir *dir = dir_open_root (); struct inode *inode = NULL; - if (dir_open_root (&dir)) + if (dir != NULL) dir_lookup (dir, name, &inode); dir_close (dir); @@ -84,94 +83,18 @@ filesys_open (const char *name) bool filesys_remove (const char *name) { - struct dir *dir = NULL; - bool success = (dir_open_root (&dir) - && dir_remove (dir, name)); + struct dir *dir = dir_open_root (); + bool success = dir != NULL && dir_remove (dir, name); dir_close (dir); return success; } - -/* 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; -} - -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"); -} - -/* 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); -} -/* Formats the filesystem. */ +/* Formats the file system. */ static void do_format (void) { - printf ("Formatting filesystem..."); + printf ("Formatting file system..."); free_map_create (); if (!dir_create (ROOT_DIR_SECTOR, 16)) PANIC ("root directory creation failed");