-
Notifications
You must be signed in to change notification settings - Fork 0
/
free-map.c
executable file
·85 lines (77 loc) · 2.36 KB
/
free-map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "filesys/free-map.h"
#include <bitmap.h>
#include <debug.h>
#include "filesys/file.h"
#include "filesys/filesys.h"
#include "filesys/inode.h"
static struct file *free_map_file; /* Free map file. */
static struct bitmap *free_map; /* Free map, one bit per sector. */
/* Initializes the free map. */
void
free_map_init (void)
{
free_map = bitmap_create (block_size (fs_device));
if (free_map == NULL)
PANIC ("bitmap creation failed--file system device is too large");
bitmap_mark (free_map, FREE_MAP_SECTOR);
bitmap_mark (free_map, ROOT_DIR_SECTOR);
}
/* Allocates CNT consecutive sectors from the free map and stores
the first into *SECTORP.
Returns true if successful, false if not enough consecutive
sectors were available or if the free_map file could not be
written. */
bool
free_map_allocate (size_t cnt, block_sector_t *sectorp)
{
block_sector_t sector = bitmap_scan_and_flip (free_map, 0, cnt, false);
if (sector != BITMAP_ERROR
&& free_map_file != NULL
&& !bitmap_write (free_map, free_map_file))
{
bitmap_set_multiple (free_map, sector, cnt, false);
sector = BITMAP_ERROR;
}
if (sector != BITMAP_ERROR)
*sectorp = sector;
return sector != BITMAP_ERROR;
}
/* Makes CNT sectors starting at SECTOR available for use. */
void
free_map_release (block_sector_t sector, size_t cnt)
{
ASSERT (bitmap_all (free_map, sector, cnt));
bitmap_set_multiple (free_map, sector, cnt, false);
bitmap_write (free_map, free_map_file);
}
/* Opens the free map file and reads it from disk. */
void
free_map_open (void)
{
free_map_file = file_open (inode_open (FREE_MAP_SECTOR));
if (free_map_file == NULL)
PANIC ("can't open free map");
if (!bitmap_read (free_map, free_map_file))
PANIC ("can't read free map");
}
/* Writes the free map to disk and closes the free map file. */
void
free_map_close (void)
{
file_close (free_map_file);
}
/* Creates a new free map file on disk and writes the free map to
it. */
void
free_map_create (void)
{
/* Create inode. */
if (!inode_create (FREE_MAP_SECTOR, bitmap_file_size (free_map)))
PANIC ("free map creation failed");
/* Write bitmap to file. */
free_map_file = file_open (inode_open (FREE_MAP_SECTOR));
if (free_map_file == NULL)
PANIC ("can't open free map");
if (!bitmap_write (free_map, free_map_file))
PANIC ("can't write free map");
}