Working filesystem.
[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   /* Create the initial bitmap and reserve sectors for the
28      free map and root directory file headers. */
29   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
30     panic ("bitmap creation failed--disk is too large");
31   bitmap_mark (&free_map, FREE_MAP_SECTOR);
32   bitmap_mark (&free_map, ROOT_DIR_SECTOR);
33
34   /* Allocate data sector(s) for the free map file
35      and write its file header to disk. */
36   map_hdr = filehdr_allocate (&free_map, bitmap_storage_size (&free_map));
37   if (map_hdr == NULL)
38     panic ("free map creation failed--disk is too large");
39   filehdr_write (map_hdr, FREE_MAP_SECTOR);
40   filehdr_destroy (map_hdr);
41
42   /* Allocate data sector(s) for the root directory file
43      and write its file header to disk. */
44   dir_hdr = filehdr_allocate (&free_map, ROOT_DIR_FILE_SIZE);
45   if (dir_hdr == NULL)
46     panic ("root directory creation failed");
47   filehdr_write (dir_hdr, ROOT_DIR_SECTOR);
48   filehdr_destroy (dir_hdr);
49
50   /* Write out the free map now that we have space reserved
51      for it. */
52   file_open (&free_map_file, FREE_MAP_SECTOR);
53   bitmap_write (&free_map, &free_map_file);
54   bitmap_destroy (&free_map);
55   file_close (&free_map_file);
56
57   /* Write out the root directory in the same way. */
58   if (!file_open (&root_dir_file, ROOT_DIR_SECTOR))
59     panic ("can't open root directory");
60   if (!dir_init (&dir, NUM_DIR_ENTRIES))
61     panic ("can't initialize root directory");
62   dir_write (&dir, &root_dir_file);
63   dir_destroy (&dir);
64   file_close (&free_map_file);
65 }
66
67 void
68 filesys_init (bool format) 
69 {
70   filesys_disk = disk_get (1);
71   if (filesys_disk == NULL)
72     panic ("ide1:1 not present, filesystem initialization failed");
73
74   if (format) 
75     do_format ();
76   
77   file_open (&free_map_file, FREE_MAP_SECTOR);
78   file_open (&root_dir_file, ROOT_DIR_SECTOR);
79 }
80
81 bool
82 filesys_create (const char *name, off_t initial_size) 
83 {
84   struct dir dir;
85   struct bitmap free_map;
86   disk_sector_no hdr_sector;
87   struct filehdr *filehdr;
88   bool success = false;
89
90   /* Read the root directory. */
91   dir_init (&dir, NUM_DIR_ENTRIES);
92   dir_read (&dir, &root_dir_file);
93   if (dir_lookup (&dir, name, NULL)) 
94     goto exit1;
95
96   /* Allocate a block for the file header. */
97   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
98     goto exit1;
99   bitmap_read (&free_map, &free_map_file);
100   hdr_sector = bitmap_find_and_set (&free_map);
101   if (hdr_sector == BITMAP_ERROR)
102     goto exit2;
103
104   /* Add the file to the directory. */
105   if (!dir_add (&dir, name, hdr_sector))
106     goto exit2;
107
108   /* Allocate space for the file. */
109   filehdr = filehdr_allocate (&free_map, initial_size);
110   if (filehdr == NULL)
111     goto exit2;
112
113   /* Write everything back. */
114   filehdr_write (filehdr, hdr_sector);
115   dir_write (&dir, &root_dir_file);
116   bitmap_write (&free_map, &free_map_file);
117
118   success = true;
119
120   /* Clean up. */
121   filehdr_destroy (filehdr);
122  exit2:
123   bitmap_destroy (&free_map);
124  exit1:
125   dir_destroy (&dir);
126
127   return success;
128 }
129
130 bool
131 filesys_open (const char *name, struct file *file)
132 {
133   struct dir dir;
134   disk_sector_no hdr_sector;
135   bool success = false;
136
137   dir_init (&dir, NUM_DIR_ENTRIES);
138   dir_read (&dir, &root_dir_file);
139   if (dir_lookup (&dir, name, &hdr_sector))
140     success = file_open (file, hdr_sector);
141   
142   dir_destroy (&dir);
143   return success;
144 }
145
146 bool
147 filesys_remove (const char *name) 
148 {
149   struct dir dir;
150   disk_sector_no hdr_sector;
151   struct filehdr *filehdr;
152   struct bitmap free_map;
153   bool success = false;
154
155   /* Read the root directory. */
156   dir_init (&dir, NUM_DIR_ENTRIES);
157   dir_read (&dir, &root_dir_file);
158   if (!dir_lookup (&dir, name, &hdr_sector))
159     goto exit1;
160
161   /* Read the file header. */
162   filehdr = filehdr_read (hdr_sector);
163   if (filehdr == NULL)
164     goto exit1;
165
166   /* Allocate a block for the file header. */
167   if (!bitmap_init (&free_map, disk_size (filesys_disk)))
168     goto exit2;
169   bitmap_read (&free_map, &free_map_file);
170
171   /* Deallocate. */
172   filehdr_deallocate (filehdr, &free_map);
173   bitmap_reset (&free_map, hdr_sector);
174   dir_remove (&dir, name);
175
176   /* Write everything back. */
177   bitmap_write (&free_map, &free_map_file);
178   dir_write (&dir, &root_dir_file);
179
180   success = true;
181
182   /* Clean up. */
183   bitmap_destroy (&free_map);
184  exit2:
185   filehdr_destroy (filehdr);
186  exit1:
187   dir_destroy (&dir);
188
189   return success;
190 }
191
192 static void must_succeed_function (int, int) ATTRIBUTE((noinline));
193
194 static void 
195 must_succeed_function (int line_no, int success) 
196 {
197   if (!success)
198     panic ("filesys_self_test: operation failed on line %d", line_no);
199 }
200
201 #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
202
203 void
204 filesys_self_test (void)
205 {
206   static const char s[] = "This is a test string.";
207   struct file file;
208   char s2[sizeof s];
209
210   MUST_SUCCEED (filesys_create ("foo", sizeof s));
211   MUST_SUCCEED (filesys_open ("foo", &file));
212   MUST_SUCCEED (file_write (&file, s, sizeof s) == sizeof s);
213   MUST_SUCCEED (file_tell (&file) == sizeof s);
214   MUST_SUCCEED (file_length (&file) == sizeof s);
215   file_close (&file);
216
217   MUST_SUCCEED (filesys_open ("foo", &file));
218   MUST_SUCCEED (file_read (&file, s2, sizeof s2) == sizeof s2);
219   MUST_SUCCEED (memcmp (s, s2, sizeof s) == 0);
220   MUST_SUCCEED (file_tell (&file) == sizeof s2);
221   MUST_SUCCEED (file_length (&file) == sizeof s2);
222   file_close (&file);
223
224   MUST_SUCCEED (filesys_remove ("foo"));
225
226   printk ("filesys: self test ok\n");
227 }