10 #define FREE_MAP_SECTOR 0
11 #define ROOT_DIR_SECTOR 1
13 #define NUM_DIR_ENTRIES 10
14 #define ROOT_DIR_FILE_SIZE (sizeof (struct dir_entry) * NUM_DIR_ENTRIES)
16 struct disk *filesys_disk;
18 static struct file free_map_file, root_dir_file;
23 struct bitmap free_map;
24 struct filehdr *map_hdr, *dir_hdr;
27 printk ("Formatting filesystem...");
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);
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));
40 PANIC ("free map creation failed--disk is too large");
41 filehdr_write (map_hdr, FREE_MAP_SECTOR);
42 filehdr_destroy (map_hdr);
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);
48 PANIC ("root directory creation failed");
49 filehdr_write (dir_hdr, ROOT_DIR_SECTOR);
50 filehdr_destroy (dir_hdr);
52 /* Write out the free map now that we have space reserved
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);
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);
67 file_close (&free_map_file);
73 filesys_init (bool format)
75 filesys_disk = disk_get (1);
76 if (filesys_disk == NULL)
77 PANIC ("ide1:1 not present, filesystem initialization failed");
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");
89 filesys_create (const char *name, off_t initial_size)
92 struct bitmap free_map;
93 disk_sector_no hdr_sector;
94 struct filehdr *filehdr;
97 /* Read the root directory. */
98 if (!dir_init (&dir, NUM_DIR_ENTRIES))
100 dir_read (&dir, &root_dir_file);
101 if (dir_lookup (&dir, name, NULL))
104 /* Allocate a block for the file header. */
105 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
107 bitmap_read (&free_map, &free_map_file);
108 hdr_sector = bitmap_find_and_set (&free_map);
109 if (hdr_sector == BITMAP_ERROR)
112 /* Add the file to the directory. */
113 if (!dir_add (&dir, name, hdr_sector))
116 /* Allocate space for the file. */
117 filehdr = filehdr_allocate (&free_map, initial_size);
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);
129 filehdr_destroy (filehdr);
131 bitmap_destroy (&free_map);
139 filesys_open (const char *name, struct file *file)
142 disk_sector_no hdr_sector;
143 bool success = false;
145 if (!dir_init (&dir, NUM_DIR_ENTRIES))
147 dir_read (&dir, &root_dir_file);
148 if (dir_lookup (&dir, name, &hdr_sector))
149 success = file_open (file, hdr_sector);
156 filesys_remove (const char *name)
159 disk_sector_no hdr_sector;
160 struct filehdr *filehdr;
161 struct bitmap free_map;
162 bool success = false;
164 /* Read the root directory. */
165 if (!dir_init (&dir, NUM_DIR_ENTRIES))
167 dir_read (&dir, &root_dir_file);
168 if (!dir_lookup (&dir, name, &hdr_sector))
171 /* Read the file header. */
172 filehdr = filehdr_read (hdr_sector);
176 /* Allocate a block for the file header. */
177 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
179 bitmap_read (&free_map, &free_map_file);
182 filehdr_deallocate (filehdr, &free_map);
183 bitmap_reset (&free_map, hdr_sector);
184 dir_remove (&dir, name);
186 /* Write everything back. */
187 bitmap_write (&free_map, &free_map_file);
188 dir_write (&dir, &root_dir_file);
193 bitmap_destroy (&free_map);
195 filehdr_destroy (filehdr);
207 if (!dir_init (&dir, NUM_DIR_ENTRIES))
209 dir_read (&dir, &root_dir_file);
219 struct bitmap free_map;
222 printk ("Free map:\n");
223 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
225 bitmap_read (&free_map, &free_map_file);
226 bitmap_dump (&free_map);
227 bitmap_destroy (&free_map);
230 if (!dir_init (&dir, NUM_DIR_ENTRIES))
232 dir_read (&dir, &root_dir_file);
239 static void must_succeed_function (int, int) NO_INLINE;
242 must_succeed_function (int line_no, int success)
245 PANIC ("filesys_self_test: operation failed on line %d", line_no);
248 #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
251 filesys_self_test (void)
253 static const char s[] = "This is a test string.";
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);
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);
271 MUST_SUCCEED (filesys_remove ("foo"));
273 printk ("filesys: self test ok\n");