-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_ctx.c
91 lines (76 loc) · 2.8 KB
/
fs_ctx.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
86
87
88
89
90
91
/*
* This code is provided solely for the personal and private use of students
* taking the CSC369H course at the University of Toronto. Copying for purposes
* other than this use is expressly prohibited. All forms of distribution of
* this code, including but not limited to public repositories on GitHub,
* GitLab, Bitbucket, or any other online platform, whether as given or with
* any changes, are expressly prohibited.
*
* Authors: Alexey Khrabrov, Karen Reid, Angela Demke Brown
*
* All of the files in this directory and all subdirectories are:
* Copyright (c) 2022 Angela Demke Brown
*/
/**
* CSC369 Assignment 4 - File system runtime context implementation.
*/
#include "fs_ctx.h"
/**
* Initialize file system context.
*
* @param fs pointer to the context to initialize.
* @param image pointer to the start of the image.
* @param size image size in bytes.
* @return true on success; false on failure (e.g. invalid superblock).
*/
bool fs_ctx_init(fs_ctx *fs, void *image, size_t size)
{
// Check if the file system image can be mounted and initialize its
// runtime state.
fs->image = image;
fs->size = size;
/** VSFS Superblock is first block on disk, so the pointer to the
* superblock is the same as the pointer to the start of the
* mmap'd disk image.
*/
fs->sb = (vsfs_superblock *)image;
/** We're very trusting. If the magic number looks good, we'll go
* ahead and mount the file system (and try to use it).
* You may want to add more sanity checking to make sure the disk
* image appears to be a valid VSFS file system.
*/
if (fs->sb->sb_magic != VSFS_MAGIC) {
return false;
}
/** VSFS Inode bitmap pointer
* The block number of the inode bitmap is VSFS_IMAP_BLKNUM;
* we multiply by the block size to get the offset in bytes from the
* start of the mmap'd disk image.
*/
fs->ibmap = (bitmap_t *)(image + VSFS_IMAP_BLKNUM * VSFS_BLOCK_SIZE);
/** VSFS Data block bitmap pointer
* Similar calculation as inode bitmap.
*/
fs->dbmap = (bitmap_t *)(image + VSFS_DMAP_BLKNUM * VSFS_BLOCK_SIZE);
/** VSFS Inode table pointer
* Similar calculation as for bitmaps.
*/
fs->itable = (vsfs_inode *)(image + VSFS_ITBL_BLKNUM * VSFS_BLOCK_SIZE);
// TODO: Initialize anything else that you add to the fs context.
/** Number of directories possible for a block */
fs->num_d_db = div_round_up(VSFS_BLOCK_SIZE, sizeof(vsfs_dentry));
/** Number of block numbers that can fit in a block */
fs->num_blk_per_b = div_round_up(VSFS_BLOCK_SIZE, sizeof(vsfs_blk_t));
return true;
}
/**
* Destroy file system context.
* Must cleanup all the resources created in fs_ctx_init().
*
* @param fs pointer to the context to clean up
*/
void fs_ctx_destroy(fs_ctx *fs)
{
//TODO: cleanup any other resources allocated in fs_ctx_init()
(void)fs;
}