PKUOS - Pintos
Pintos source browser for PKU Operating System course
free-map.c
Go to the documentation of this file.
1#include "filesys/free-map.h"
2#include <bitmap.h>
3#include <debug.h>
4#include "filesys/file.h"
5#include "filesys/filesys.h"
6#include "filesys/inode.h"
7
8static struct file *free_map_file; /**< Free map file. */
9static struct bitmap *free_map; /**< Free map, one bit per sector. */
10
11/** Initializes the free map. */
12void
14{
16 if (free_map == NULL)
17 PANIC ("bitmap creation failed--file system device is too large");
20}
21
22/** Allocates CNT consecutive sectors from the free map and stores
23 the first into *SECTORP.
24 Returns true if successful, false if not enough consecutive
25 sectors were available or if the free_map file could not be
26 written. */
27bool
28free_map_allocate (size_t cnt, block_sector_t *sectorp)
29{
30 block_sector_t sector = bitmap_scan_and_flip (free_map, 0, cnt, false);
31 if (sector != BITMAP_ERROR
33 && !bitmap_write (free_map, free_map_file))
34 {
35 bitmap_set_multiple (free_map, sector, cnt, false);
36 sector = BITMAP_ERROR;
37 }
38 if (sector != BITMAP_ERROR)
39 *sectorp = sector;
40 return sector != BITMAP_ERROR;
41}
42
43/** Makes CNT sectors starting at SECTOR available for use. */
44void
45free_map_release (block_sector_t sector, size_t cnt)
46{
47 ASSERT (bitmap_all (free_map, sector, cnt));
48 bitmap_set_multiple (free_map, sector, cnt, false);
49 bitmap_write (free_map, free_map_file);
50}
51
52/** Opens the free map file and reads it from disk. */
53void
55{
57 if (free_map_file == NULL)
58 PANIC ("can't open free map");
59 if (!bitmap_read (free_map, free_map_file))
60 PANIC ("can't read free map");
61}
62
63/** Writes the free map to disk and closes the free map file. */
64void
66{
68}
69
70/** Creates a new free map file on disk and writes the free map to
71 it. */
72void
74{
75 /* Create inode. */
76 if (!inode_create (FREE_MAP_SECTOR, bitmap_file_size (free_map)))
77 PANIC ("free map creation failed");
78
79 /* Write bitmap to file. */
81 if (free_map_file == NULL)
82 PANIC ("can't open free map");
83 if (!bitmap_write (free_map, free_map_file))
84 PANIC ("can't write free map");
85}
void bitmap_set_multiple(struct bitmap *b, size_t start, size_t cnt, bool value)
Sets the CNT bits starting at START in B to VALUE.
Definition: bitmap.c:218
size_t bitmap_scan_and_flip(struct bitmap *b, size_t start, size_t cnt, bool value)
Finds the first group of CNT consecutive bits in B at or after START that are all set to VALUE,...
Definition: bitmap.c:320
bool bitmap_all(const struct bitmap *b, size_t start, size_t cnt)
Returns true if every bit in B between START and START + CNT, exclusive, is set to true,...
Definition: bitmap.c:284
void bitmap_mark(struct bitmap *b, size_t bit_idx)
Atomically sets the bit numbered BIT_IDX in B to true.
Definition: bitmap.c:157
struct bitmap * bitmap_create(size_t bit_cnt)
Creation and destruction.
Definition: bitmap.c:79
#define BITMAP_ERROR
Finding set or unset bits.
Definition: bitmap.h:36
block_sector_t block_size(struct block *block)
Returns the number of sectors in BLOCK.
Definition: block.c:144
uint32_t block_sector_t
Index of a block device sector.
Definition: block.h:15
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
#define PANIC(...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
Definition: debug.h:14
void file_close(struct file *file)
Closes FILE.
Definition: file.c:46
struct file * file_open(struct inode *inode)
Opens a file for the given INODE, of which it takes ownership, and returns the new file.
Definition: file.c:18
struct block * fs_device
Partition that contains the file system.
Definition: filesys.c:11
#define ROOT_DIR_SECTOR
Root directory file inode sector.
Definition: filesys.h:9
#define FREE_MAP_SECTOR
Sectors of system file inodes.
Definition: filesys.h:8
void free_map_init(void)
Initializes the free map.
Definition: free-map.c:13
void free_map_release(block_sector_t sector, size_t cnt)
Makes CNT sectors starting at SECTOR available for use.
Definition: free-map.c:45
void free_map_open(void)
Opens the free map file and reads it from disk.
Definition: free-map.c:54
void free_map_create(void)
Creates a new free map file on disk and writes the free map to it.
Definition: free-map.c:73
bool free_map_allocate(size_t cnt, block_sector_t *sectorp)
Allocates CNT consecutive sectors from the free map and stores the first into *SECTORP.
Definition: free-map.c:28
static struct file * free_map_file
Free map file.
Definition: free-map.c:8
static struct bitmap * free_map
Free map, one bit per sector.
Definition: free-map.c:9
void free_map_close(void)
Writes the free map to disk and closes the free map file.
Definition: free-map.c:65
struct inode * inode_open(block_sector_t sector)
Reads an inode from SECTOR and returns a ‘struct inode’ that contains it.
Definition: inode.c:112
bool inode_create(block_sector_t sector, off_t length)
Initializes an inode with LENGTH bytes of data and writes the new inode to sector SECTOR on the file ...
Definition: inode.c:73
#define NULL
Definition: stddef.h:4
From the outside, a bitmap is an array of bits.
Definition: bitmap.c:28
An open file.
Definition: file.c:8