Clean up disk layer.
[pintos-anon] / src / filesys / filesys.c
1 #include "filesys.h"
2 #include "bitmap.h"
3 #include "debug.h"
4 #include "directory.h"
5 #include "disk.h"
6 #include "file.h"
7 #include "filehdr.h"
8 #include "lib.h"
9
10 #define FREE_MAP_SECTOR 0
11 #define ROOT_DIR_SECTOR 1
12
13 #define NUM_DIR_ENTRIES 10
14 #define ROOT_DIR_FILE_SIZE (sizeof (struct dir_entry) * NUM_DIR_ENTRIES)
15
16 struct disk *filesys_disk;
17
18 static struct file free_map_file, root_dir_file;
19
20 static void
21 do_format (void)
22 {
23   struct bitmap free_map;
24   struct filehdr *map_hdr, *dir_hdr;
25   struct dir dir;
26
27   printk ("Formatting filesystem...");
28
29   /* Create the initial bitmap and reserve sectors for the
30      free map and root directory file headers. */
31   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
32     PANIC ("bitmap creation failed--disk is too large");
33   bitmap_mark (&free_map, FREE_MAP_SECTOR);
34   bitmap_mark (&free_map, ROOT_DIR_SECTOR);
35
36   /* Allocate data sector(s) for the free map file
37      and write its file header to disk. */
38   map_hdr = filehdr_allocate (&free_map, bitmap_storage_size (&free_map));
39   if (map_hdr == NULL)
40     PANIC ("free map creation failed--disk is too large");
41   filehdr_write (map_hdr, FREE_MAP_SECTOR);
42   filehdr_destroy (map_hdr);
43
44   /* Allocate data sector(s) for the root directory file
45      and write its file header to disk. */
46   dir_hdr = filehdr_allocate (&free_map, ROOT_DIR_FILE_SIZE);
47   if (dir_hdr == NULL)
48     PANIC ("root directory creation failed");
49   filehdr_write (dir_hdr, ROOT_DIR_SECTOR);
50   filehdr_destroy (dir_hdr);
51
52   /* Write out the free map now that we have space reserved
53      for it. */
54   if (!file_open (&free_map_file, FREE_MAP_SECTOR))
55     PANIC ("can't open free map file");
56   bitmap_write (&free_map, &free_map_file);
57   bitmap_destroy (&free_map);
58   file_close (&free_map_file);
59
60   /* Write out the root directory in the same way. */
61   if (!file_open (&root_dir_file, ROOT_DIR_SECTOR))
62     PANIC ("can't open root directory");
63   if (!dir_init (&dir, NUM_DIR_ENTRIES))
64     PANIC ("can't initialize root directory");
65   dir_write (&dir, &root_dir_file);
66   dir_destroy (&dir);
67   file_close (&free_map_file);
68
69   printk ("done.\n");
70 }
71
72 void
73 filesys_init (bool format) 
74 {
75   filesys_disk = disk_get (0, 1);
76   if (filesys_disk == NULL)
77     PANIC ("hd0:1 (hdb) not present, filesystem initialization failed");
78
79   if (format) 
80     do_format ();
81   
82   if (!file_open (&free_map_file, FREE_MAP_SECTOR))
83     PANIC ("can't open free map file");
84   if (!file_open (&root_dir_file, ROOT_DIR_SECTOR))
85     PANIC ("can't open root dir file");
86 }
87
88 bool
89 filesys_create (const char *name, off_t initial_size) 
90 {
91   struct dir dir;
92   struct bitmap free_map;
93   disk_sector_no hdr_sector;
94   struct filehdr *filehdr;
95   bool success = false;
96
97   /* Read the root directory. */
98   if (!dir_init (&dir, NUM_DIR_ENTRIES))
99     return false;
100   dir_read (&dir, &root_dir_file);
101   if (dir_lookup (&dir, name, NULL)) 
102     goto exit1;
103
104   /* Allocate a block for the file header. */
105   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
106     goto exit1;
107   bitmap_read (&free_map, &free_map_file);
108   hdr_sector = bitmap_find_and_set (&free_map);
109   if (hdr_sector == BITMAP_ERROR)
110     goto exit2;
111
112   /* Add the file to the directory. */
113   if (!dir_add (&dir, name, hdr_sector))
114     goto exit2;
115
116   /* Allocate space for the file. */
117   filehdr = filehdr_allocate (&free_map, initial_size);
118   if (filehdr == NULL)
119     goto exit2;
120
121   /* Write everything back. */
122   filehdr_write (filehdr, hdr_sector);
123   dir_write (&dir, &root_dir_file);
124   bitmap_write (&free_map, &free_map_file);
125
126   success = true;
127
128   /* Clean up. */
129   filehdr_destroy (filehdr);
130  exit2:
131   bitmap_destroy (&free_map);
132  exit1:
133   dir_destroy (&dir);
134
135   return success;
136 }
137
138 bool
139 filesys_open (const char *name, struct file *file)
140 {
141   struct dir dir;
142   disk_sector_no hdr_sector;
143   bool success = false;
144
145   if (!dir_init (&dir, NUM_DIR_ENTRIES))
146     return false;
147   dir_read (&dir, &root_dir_file);
148   if (dir_lookup (&dir, name, &hdr_sector))
149     success = file_open (file, hdr_sector);
150   
151   dir_destroy (&dir);
152   return success;
153 }
154
155 bool
156 filesys_remove (const char *name) 
157 {
158   struct dir dir;
159   disk_sector_no hdr_sector;
160   struct filehdr *filehdr;
161   struct bitmap free_map;
162   bool success = false;
163
164   /* Read the root directory. */
165   if (!dir_init (&dir, NUM_DIR_ENTRIES))
166     return false;
167   dir_read (&dir, &root_dir_file);
168   if (!dir_lookup (&dir, name, &hdr_sector))
169     goto exit1;
170
171   /* Read the file header. */
172   filehdr = filehdr_read (hdr_sector);
173   if (filehdr == NULL)
174     goto exit1;
175
176   /* Allocate a block for the file header. */
177   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
178     goto exit2;
179   bitmap_read (&free_map, &free_map_file);
180
181   /* Deallocate. */
182   filehdr_deallocate (filehdr, &free_map);
183   bitmap_reset (&free_map, hdr_sector);
184   dir_remove (&dir, name);
185
186   /* Write everything back. */
187   bitmap_write (&free_map, &free_map_file);
188   dir_write (&dir, &root_dir_file);
189
190   success = true;
191
192   /* Clean up. */
193   bitmap_destroy (&free_map);
194  exit2:
195   filehdr_destroy (filehdr);
196  exit1:
197   dir_destroy (&dir);
198
199   return success;
200 }
201
202 bool
203 filesys_list (void) 
204 {
205   struct dir dir;
206
207   if (!dir_init (&dir, NUM_DIR_ENTRIES))
208     return false;
209   dir_read (&dir, &root_dir_file);
210   dir_list (&dir);
211   dir_destroy (&dir);
212
213   return true;
214 }
215
216 bool
217 filesys_dump (void) 
218 {
219   struct bitmap free_map;
220   struct dir dir;  
221
222   printk ("Free map:\n");
223   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
224     return false;
225   bitmap_read (&free_map, &free_map_file);
226   bitmap_dump (&free_map);
227   bitmap_destroy (&free_map);
228   printk ("\n");
229   
230   if (!dir_init (&dir, NUM_DIR_ENTRIES))
231     return false;
232   dir_read (&dir, &root_dir_file);
233   dir_dump (&dir);
234   dir_destroy (&dir);
235
236   return true;
237 }
238
239 static void must_succeed_function (int, int) NO_INLINE;
240
241 static void 
242 must_succeed_function (int line_no, int success) 
243 {
244   if (!success)
245     PANIC ("filesys_self_test: operation failed on line %d", line_no);
246 }
247
248 #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
249
250 void
251 filesys_self_test (void)
252 {
253   static const char s[] = "This is a test string.";
254   struct file file;
255   char s2[sizeof s];
256
257   MUST_SUCCEED (filesys_create ("foo", sizeof s));
258   MUST_SUCCEED (filesys_open ("foo", &file));
259   MUST_SUCCEED (file_write (&file, s, sizeof s) == sizeof s);
260   MUST_SUCCEED (file_tell (&file) == sizeof s);
261   MUST_SUCCEED (file_length (&file) == sizeof s);
262   file_close (&file);
263
264   MUST_SUCCEED (filesys_open ("foo", &file));
265   MUST_SUCCEED (file_read (&file, s2, sizeof s2) == sizeof s2);
266   MUST_SUCCEED (memcmp (s, s2, sizeof s) == 0);
267   MUST_SUCCEED (file_tell (&file) == sizeof s2);
268   MUST_SUCCEED (file_length (&file) == sizeof s2);
269   file_close (&file);
270
271   MUST_SUCCEED (filesys_remove ("foo"));
272
273   printk ("filesys: self test ok\n");
274 }