Start work on filesystem code.
[pintos-anon] / src / filesys / filesys.c
1 #include "filesys.h"
2 #include "disk.h"
3 #include "directory.h"
4
5 static struct disk *disk;
6
7 static struct file free_map_file, root_dir_file;
8
9 #define FREE_MAP_SECTOR 0
10 #define ROOT_DIR_SECTOR 1
11
12 #define NUM_DIR_ENTRIES 10
13 #define ROOT_DIR_FILE_SIZE (sizeof (struct dir_entry) * NUM_DIR_ENTRIES)
14
15 static void
16 do_format (void)
17 {
18   struct bitmap free_map;
19   struct filehdr map_hdr, dir_hdr;
20   struct dir dir;
21
22   /* Create the initial bitmap and reserve sectors for the
23      free map and root directory file headers. */
24   bitmap_init (&free_map, disk_size (disk));
25   bitmap_mark (&free_map, FREE_MAP_SECTOR);
26   bitmap_mark (&free_map, ROOT_DIR_SECTOR);
27
28   /* Allocate data sector(s) for the free map file
29      and write its file header to disk. */
30   if (!filehdr_allocate (&map_hdr, bitmap_storage_size (&free_map)))
31     panic ("free map creation failed");
32   filehdr_write (&map_hdr, FREE_MAP_SECTOR);
33   filehdr_destroy (&map_hdr);
34
35   /* Allocate data sector(s) for the root directory file
36      and write its file header to disk. */
37   if (!filehdr_allocate (&dir_hdr, ROOT_DIR_FILE_SIZE))
38     panic ("root directory creation failed");
39   filehdr_write (&dir_hdr, FREE_MAP_SECTOR);
40   filehdr_destroy (&dir_hdr);
41
42   /* Write out the free map now that we have space reserved
43      for it. */
44   file_open (&free_map_file, FREE_MAP_SECTOR);
45   bitmapio_write (&free_map, free_map_file);
46   bitmap_destroy (&free_map);
47   file_close (&free_map_file);
48
49   /* Write out the root directory in the same way. */
50   file_open (&root_dir_file, ROOT_DIR_SECTOR);
51   if (!dir_init (&dir, NUM_DIR_ENTRIES))
52     panic ("can't initialize root directory");
53   dir_write (root_dir_file);
54   dir_destroy (&dir);
55   file_close (&free_map_file);
56 }
57
58 void
59 filesys_init (bool format) 
60 {
61   disk = disk_get (1);
62   if (disk == NULL)
63     panic ("ide1:1 not present, filesystem initialization failed");
64
65   if (format) 
66     do_format ();
67   
68   file_open (&free_map_file, FREE_MAP_SECTOR);
69   file_open (&root_dir_file, ROOT_DIR_SECTOR);
70 }
71
72 bool
73 filesys_create (const char *name, off_t initial_size) 
74 {
75   struct dir dir;
76   struct bitmap free_map;
77   disk_sector_no hdr_sector;
78   struct filehdr filehdr;
79   bool success = false;
80
81   /* Read the root directory. */
82   dir_init (&dir, NUM_DIR_ENTRIES);
83   dir_read (&dir, &root_dir_file);
84   if (dir_lookup (&dir, name, NULL)) 
85     goto exit1;
86
87   /* Allocate a block for the file header. */
88   bitmap_init (&free_map, disk_size (disk));
89   bitmapio_read (&free_map, &free_map_file);
90   hdr_sector = bitmap_find_and_set (&free_map);
91   if (hdr_sector == BITMAP_ERROR)
92     goto exit2;
93
94   /* Add the file to the directory. */
95   if (!dir_add (&dir, name, hdr_sector))
96     goto exit2;
97
98   /* Allocate space for the file. */
99   if (!filehdr_allocate (&filehdr, initial_size))
100     goto exit2;
101
102   /* Write everything back. */
103   filehdr_write (&filehdr, hdr_sector);
104   dir_write (&dir, &root_dir_file);
105   bitmapio_write (&free_map, &free_map_file);
106
107   success = true;
108
109   /* Clean up. */
110   filehdr_destroy (&filehdr);
111  exit2:
112   bitmap_destroy (&free_map);
113  exit1:
114   dir_destroy (&dir);
115
116   return success;
117 }
118
119 bool
120 filesys_remove (const char *name) 
121 {
122   struct dir dir;
123   disk_sector_no hdr_sector;
124   struct filehdr filehdr;
125   struct bitmap free_map;
126   bool success = false;
127
128   /* Read the root directory. */
129   dir_init (&dir, NUM_DIR_ENTRIES);
130   dir_read (&dir, &root_dir_file);
131   if (!dir_lookup (&dir, name, &hdr_sector))
132     goto exit;
133
134   /* Read the file header. */
135   filehdr_read (&filehdr, hdr_sector);
136
137   /* Allocate a block for the file header. */
138   bitmap_init (&free_map, disk_size (disk));
139   bitmapio_read (&free_map, &free_map_file);
140
141   /* Deallocate. */
142   filehdr_deallocate (&filehdr, &free_map);
143   bitmap_reset (&free_map, hdr_sector);
144   dir_remove (&dir, name);
145
146   /* Write everything back. */
147   bitmapio_write (&free_map, &free_map_file);
148   dir_write (&dir, &root_dir_file);
149
150   success = true;
151
152   /* Clean up. */
153   filehdr_destroy (&filehdr);
154   bitmap_destroy (&free_map);
155  exit:
156   dir_destroy (&dir);
157
158   return success;
159 }
160
161 #undef NDEBUG
162 #include "debug.h"
163 #include "file.h"
164
165 void
166 filesys_self_test (void)
167 {
168   static const char s[] = "This is a test string.";
169   struct file *file;
170   char s2[sizeof s];
171
172   ASSERT (filesys_create ("foo"));
173   ASSERT ((file = filesys_open ("foo")) != NULL);
174   ASSERT (file_write (file, s, sizeof s) == sizeof s);
175   ASSERT (file_tell (file) == sizeof s);
176   ASSERT (file_length (file) == sizeof s);
177   file_close (file);
178
179   ASSERT ((file = filesys_open ("foo")) != NULL);
180   ASSERT (file_read (file, s2, sizeof s2) == sizeof s2);
181   ASSERT (memcmp (s, s2, sizeof s) == 0);
182   ASSERT (file_tell (file) == sizeof s2);
183   ASSERT (file_length (file) == sizeof s2);
184   file_close (file);
185
186   ASSERT (filesys_remove ("foo"));
187 }