+\f
+/* Formats the filesystem. */
+static void
+do_format (void)
+{
+ struct bitmap free_map;
+ struct filehdr *map_hdr, *dir_hdr;
+ struct dir dir;
+
+ printk ("Formatting filesystem...");
+
+ /* Create the initial bitmap and reserve sectors for the
+ free map and root directory file headers. */
+ if (!bitmap_init (&free_map, disk_size (filesys_disk)))
+ PANIC ("bitmap creation failed--disk is too large");
+ bitmap_mark (&free_map, FREE_MAP_SECTOR);
+ bitmap_mark (&free_map, ROOT_DIR_SECTOR);
+
+ /* Allocate data sector(s) for the free map file
+ and write its file header to disk. */
+ map_hdr = filehdr_allocate (&free_map, bitmap_file_size (&free_map));
+ if (map_hdr == NULL)
+ PANIC ("free map creation failed--disk is too large");
+ filehdr_write (map_hdr, FREE_MAP_SECTOR);
+ filehdr_destroy (map_hdr);
+
+ /* Allocate data sector(s) for the root directory file
+ and write its file header to disk. */
+ dir_hdr = filehdr_allocate (&free_map, ROOT_DIR_FILE_SIZE);
+ if (dir_hdr == NULL)
+ PANIC ("root directory creation failed");
+ filehdr_write (dir_hdr, ROOT_DIR_SECTOR);
+ filehdr_destroy (dir_hdr);
+
+ /* Write out the free map now that we have space reserved
+ for it. */
+ if (!file_open (&free_map_file, FREE_MAP_SECTOR))
+ PANIC ("can't open free map file");
+ bitmap_write (&free_map, &free_map_file);
+ bitmap_destroy (&free_map);
+ file_close (&free_map_file);
+
+ /* Write out the root directory in the same way. */
+ if (!file_open (&root_dir_file, ROOT_DIR_SECTOR))
+ PANIC ("can't open root directory");
+ if (!dir_init (&dir, NUM_DIR_ENTRIES))
+ PANIC ("can't initialize root directory");
+ dir_write (&dir, &root_dir_file);
+ dir_destroy (&dir);
+ file_close (&free_map_file);
+
+ printk ("done.\n");
+}
+
+/* If SUCCESS is false, panics with an error complaining about
+ LINE_NO. */
+static void
+must_succeed_function (int line_no, bool success)
+{
+ if (!success)
+ PANIC ("filesys_self_test: operation failed on line %d", line_no);
+}