1 #include "filesys/free-map.h"
4 #include "filesys/file.h"
5 #include "filesys/filesys.h"
6 #include "filesys/inode.h"
8 static struct file *free_map_file; /* Free map file. */
9 static struct bitmap *free_map; /* Free map, one bit per disk sector. */
11 /* Initializes the free map. */
15 free_map = bitmap_create (disk_size (filesys_disk));
17 PANIC ("bitmap creation failed--disk is too large");
18 bitmap_mark (free_map, FREE_MAP_SECTOR);
19 bitmap_mark (free_map, ROOT_DIR_SECTOR);
22 /* Allocates CNT consecutive sectors from the free map and stores
23 the first into *SECTORP.
24 Returns true if successful, false if all sectors were
27 free_map_allocate (size_t cnt, disk_sector_t *sectorp)
29 disk_sector_t sector = bitmap_scan_and_flip (free_map, 0, cnt, false);
30 if (sector != BITMAP_ERROR
31 && free_map_file != NULL
32 && !bitmap_write (free_map, free_map_file))
34 bitmap_set_multiple (free_map, sector, cnt, false);
35 sector = BITMAP_ERROR;
37 if (sector != BITMAP_ERROR)
39 return sector != BITMAP_ERROR;
42 /* Makes CNT sectors starting at SECTOR available for use. */
44 free_map_release (disk_sector_t sector, size_t cnt)
46 ASSERT (bitmap_all (free_map, sector, cnt));
47 bitmap_set_multiple (free_map, sector, cnt, false);
48 bitmap_write (free_map, free_map_file);
51 /* Opens the free map file and reads it from disk. */
55 free_map_file = file_open (inode_open (FREE_MAP_SECTOR));
56 if (free_map_file == NULL)
57 PANIC ("can't open free map");
58 if (!bitmap_read (free_map, free_map_file))
59 PANIC ("can't read free map");
62 /* Writes the free map to disk and closes the free map file. */
66 file_close (free_map_file);
69 /* Creates a new free map file on disk and writes the free map to
72 free_map_create (void)
75 if (!inode_create (FREE_MAP_SECTOR, bitmap_file_size (free_map)))
76 PANIC ("free map creation failed");
78 /* Write bitmap to file. */
79 free_map_file = file_open (inode_open (FREE_MAP_SECTOR));
80 if (free_map_file == NULL)
81 PANIC ("can't open free map");
82 if (!bitmap_write (free_map, free_map_file))
83 PANIC ("can't write free map");