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 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);
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);
64 file_close (&free_map_file);
68 filesys_init (bool format)
70 filesys_disk = disk_get (1);
71 if (filesys_disk == NULL)
72 panic ("ide1:1 not present, filesystem initialization failed");
77 file_open (&free_map_file, FREE_MAP_SECTOR);
78 file_open (&root_dir_file, ROOT_DIR_SECTOR);
82 filesys_create (const char *name, off_t initial_size)
85 struct bitmap free_map;
86 disk_sector_no hdr_sector;
87 struct filehdr *filehdr;
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))
96 /* Allocate a block for the file header. */
97 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
99 bitmap_read (&free_map, &free_map_file);
100 hdr_sector = bitmap_find_and_set (&free_map);
101 if (hdr_sector == BITMAP_ERROR)
104 /* Add the file to the directory. */
105 if (!dir_add (&dir, name, hdr_sector))
108 /* Allocate space for the file. */
109 filehdr = filehdr_allocate (&free_map, initial_size);
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);
121 filehdr_destroy (filehdr);
123 bitmap_destroy (&free_map);
131 filesys_open (const char *name, struct file *file)
134 disk_sector_no hdr_sector;
135 bool success = false;
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);
147 filesys_remove (const char *name)
150 disk_sector_no hdr_sector;
151 struct filehdr *filehdr;
152 struct bitmap free_map;
153 bool success = false;
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))
161 /* Read the file header. */
162 filehdr = filehdr_read (hdr_sector);
166 /* Allocate a block for the file header. */
167 if (!bitmap_init (&free_map, disk_size (filesys_disk)))
169 bitmap_read (&free_map, &free_map_file);
172 filehdr_deallocate (filehdr, &free_map);
173 bitmap_reset (&free_map, hdr_sector);
174 dir_remove (&dir, name);
176 /* Write everything back. */
177 bitmap_write (&free_map, &free_map_file);
178 dir_write (&dir, &root_dir_file);
183 bitmap_destroy (&free_map);
185 filehdr_destroy (filehdr);
192 static void must_succeed_function (int, int) ATTRIBUTE((noinline));
195 must_succeed_function (int line_no, int success)
198 panic ("filesys_self_test: operation failed on line %d", line_no);
201 #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
204 filesys_self_test (void)
206 static const char s[] = "This is a test string.";
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);
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);
224 MUST_SUCCEED (filesys_remove ("foo"));
226 printk ("filesys: self test ok\n");