11#define INODE_MAGIC 0x494e4f44
50 if (pos < inode->data.length)
84 disk_inode =
calloc (1,
sizeof *disk_inode);
85 if (disk_inode !=
NULL)
98 for (i = 0; i < sectors; i++)
204 off_t bytes_read = 0;
216 int min_left = inode_left < sector_left ? inode_left : sector_left;
219 int chunk_size = size < min_left ? size : min_left;
239 memcpy (
buffer + bytes_read, bounce + sector_ofs, chunk_size);
244 offset += chunk_size;
245 bytes_read += chunk_size;
262 off_t bytes_written = 0;
277 int min_left = inode_left < sector_left ? inode_left : sector_left;
280 int chunk_size = size < min_left ? size : min_left;
302 if (sector_ofs > 0 || chunk_size < sector_left)
306 memcpy (bounce + sector_ofs,
buffer + bytes_written, chunk_size);
312 offset += chunk_size;
313 bytes_written += chunk_size;
317 return bytes_written;
void block_read(struct block *block, block_sector_t sector, void *buffer)
Reads sector SECTOR from BLOCK into BUFFER, which must have room for BLOCK_SECTOR_SIZE bytes.
void block_write(struct block *block, block_sector_t sector, const void *buffer)
Write sector SECTOR to BLOCK from BUFFER, which must contain BLOCK_SECTOR_SIZE bytes.
uint32_t block_sector_t
Index of a block device sector.
#define BLOCK_SECTOR_SIZE
Size of a block device sector in bytes.
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
struct block * fs_device
Partition that contains the file system.
void free_map_release(block_sector_t sector, size_t cnt)
Makes CNT sectors starting at SECTOR available for use.
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.
off_t inode_write_at(struct inode *inode, const void *buffer_, off_t size, off_t offset)
Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
off_t inode_read_at(struct inode *inode, void *buffer_, off_t size, off_t offset)
Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
struct inode * inode_reopen(struct inode *inode)
Reopens and returns INODE.
off_t inode_length(const struct inode *inode)
Returns the length, in bytes, of INODE's data.
#define INODE_MAGIC
Identifies an inode.
void inode_close(struct inode *inode)
Closes INODE and writes it to disk.
struct inode * inode_open(block_sector_t sector)
Reads an inode from SECTOR and returns a ‘struct inode’ that contains it.
static block_sector_t byte_to_sector(const struct inode *inode, off_t pos)
Returns the block device sector that contains byte offset POS within INODE.
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 ...
void inode_deny_write(struct inode *inode)
Disables writes to INODE.
block_sector_t inode_get_inumber(const struct inode *inode)
Returns INODE's inode number.
void inode_init(void)
Initializes the inode module.
void inode_remove(struct inode *inode)
Marks INODE to be deleted when it is closed by the last caller who has it open.
static size_t bytes_to_sectors(off_t size)
Returns the number of sectors to allocate for an inode SIZE bytes long.
static struct list open_inodes
List of open inodes, so that opening a single inode twice returns the same ‘struct inode’.
void inode_allow_write(struct inode *inode)
Re-enables writes to INODE.
struct list_elem * list_begin(struct list *list)
Returns the beginning of LIST.
struct list_elem * list_remove(struct list_elem *elem)
Removes ELEM from its list and returns the element that followed it.
void list_push_front(struct list *list, struct list_elem *elem)
Inserts ELEM at the beginning of LIST, so that it becomes the front in LIST.
struct list_elem * list_end(struct list *list)
Returns LIST's tail.
void list_init(struct list *list)
Initializes LIST as an empty list.
struct list_elem * list_next(struct list_elem *elem)
Returns the element after ELEM in its list.
#define list_entry(LIST_ELEM, STRUCT, MEMBER)
Converts pointer to list element LIST_ELEM into a pointer to the structure that LIST_ELEM is embedded...
void * calloc(size_t a, size_t b)
Allocates and return A times B bytes initialized to zeroes.
void * malloc(size_t size)
Obtains and returns a new block of at least SIZE bytes.
void free(void *p)
Frees block P, which must have been previously allocated with malloc(), calloc(), or realloc().
int32_t off_t
An offset within a file.
#define DIV_ROUND_UP(X, STEP)
Yields X divided by STEP, rounded up.
void * memset(void *dst_, int value, size_t size)
Sets the SIZE bytes in DST to VALUE.
void * memcpy(void *dst_, const void *src_, size_t size)
Copies SIZE bytes from SRC to DST, which must not overlap.
block_sector_t start
First data sector.
off_t length
File size in bytes.
unsigned magic
Magic number.
uint32_t unused[125]
Not used.
struct list_elem elem
Element in inode list.
struct inode_disk data
Inode content.
bool removed
True if deleted, false otherwise.
block_sector_t sector
Sector number of disk location.
int deny_write_cnt
0: writes ok, >0: deny writes.
int open_cnt
Number of openers.