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 /* 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);
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));
38 panic ("free map creation failed--disk is too large");
39 filehdr_write (map_hdr, FREE_MAP_SECTOR);
40 filehdr_destroy (map_hdr);
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);
46 panic ("root directory creation failed");
47 filehdr_write (dir_hdr, ROOT_DIR_SECTOR);
48 filehdr_destroy (dir_hdr);
50 /* Write out the free map now that we have space reserved
52 if (!file_open (&free_map_file, FREE_MAP_SECTOR))
53 panic ("can't open free map file");
54 bitmap_write (&free_map, &free_map_file);
55 bitmap_destroy (&free_map);
56 file_close (&free_map_file);
58 /* Write out the root directory in the same way. */
59 if (!file_open (&root_dir_file, ROOT_DIR_SECTOR))
60 panic ("can't open root directory");
61 if (!dir_init (&dir, NUM_DIR_ENTRIES))
62 panic ("can't initialize root directory");
63 dir_write (&dir, &root_dir_file);
65 file_close (&free_map_file);
69 filesys_init (bool format)
71 filesys_disk = disk_get (1);
72 if (filesys_disk == NULL)
73 panic ("ide1:1 not present, filesystem initialization failed");
78 if (!file_open (&free_map_file, FREE_MAP_SECTOR))
79 panic ("can't open free map file");
80 if (!file_open (&root_dir_file, ROOT_DIR_SECTOR))
81 panic ("can't open root dir file");
85 filesys_create (const char *name, off_t initial_size)
88 struct bitmap free_map;
89 disk_sector_no hdr_sector;
90 struct filehdr *filehdr;
93 /* Read the root directory. */
94 if (!dir_init (&dir, NUM_DIR_ENTRIES))
96 dir_read (&dir, &root_dir_file);
97 if (dir_lookup (&dir, name, NULL))
100 /* Allocate a block for the file header. */
101 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
103 bitmap_read (&free_map, &free_map_file);
104 hdr_sector = bitmap_find_and_set (&free_map);
105 if (hdr_sector == BITMAP_ERROR)
108 /* Add the file to the directory. */
109 if (!dir_add (&dir, name, hdr_sector))
112 /* Allocate space for the file. */
113 filehdr = filehdr_allocate (&free_map, initial_size);
117 /* Write everything back. */
118 filehdr_write (filehdr, hdr_sector);
119 dir_write (&dir, &root_dir_file);
120 bitmap_write (&free_map, &free_map_file);
125 filehdr_destroy (filehdr);
127 bitmap_destroy (&free_map);
135 filesys_open (const char *name, struct file *file)
138 disk_sector_no hdr_sector;
139 bool success = false;
141 if (!dir_init (&dir, NUM_DIR_ENTRIES))
143 dir_read (&dir, &root_dir_file);
144 if (dir_lookup (&dir, name, &hdr_sector))
145 success = file_open (file, hdr_sector);
152 filesys_remove (const char *name)
155 disk_sector_no hdr_sector;
156 struct filehdr *filehdr;
157 struct bitmap free_map;
158 bool success = false;
160 /* Read the root directory. */
161 if (!dir_init (&dir, NUM_DIR_ENTRIES))
163 dir_read (&dir, &root_dir_file);
164 if (!dir_lookup (&dir, name, &hdr_sector))
167 /* Read the file header. */
168 filehdr = filehdr_read (hdr_sector);
172 /* Allocate a block for the file header. */
173 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
175 bitmap_read (&free_map, &free_map_file);
178 filehdr_deallocate (filehdr, &free_map);
179 bitmap_reset (&free_map, hdr_sector);
180 dir_remove (&dir, name);
182 /* Write everything back. */
183 bitmap_write (&free_map, &free_map_file);
184 dir_write (&dir, &root_dir_file);
189 bitmap_destroy (&free_map);
191 filehdr_destroy (filehdr);
198 static void must_succeed_function (int, int) ATTRIBUTE((noinline));
201 must_succeed_function (int line_no, int success)
204 panic ("filesys_self_test: operation failed on line %d", line_no);
207 #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
210 filesys_self_test (void)
212 static const char s[] = "This is a test string.";
216 MUST_SUCCEED (filesys_create ("foo", sizeof s));
217 MUST_SUCCEED (filesys_open ("foo", &file));
218 MUST_SUCCEED (file_write (&file, s, sizeof s) == sizeof s);
219 MUST_SUCCEED (file_tell (&file) == sizeof s);
220 MUST_SUCCEED (file_length (&file) == sizeof s);
223 MUST_SUCCEED (filesys_open ("foo", &file));
224 MUST_SUCCEED (file_read (&file, s2, sizeof s2) == sizeof s2);
225 MUST_SUCCEED (memcmp (s, s2, sizeof s) == 0);
226 MUST_SUCCEED (file_tell (&file) == sizeof s2);
227 MUST_SUCCEED (file_length (&file) == sizeof s2);
230 MUST_SUCCEED (filesys_remove ("foo"));
232 printk ("filesys: self test ok\n");