-
Notifications
You must be signed in to change notification settings - Fork 13
/
imap.rs
168 lines (151 loc) · 5.39 KB
/
imap.rs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use core::mem;
use static_assertions::const_assert;
use super::SegManager;
use crate::{
bio::{Buf, BufData},
hal::hal,
param::{BSIZE, IMAPSIZE},
proc::KernelCtx,
};
// Number of entries in each on-disk imap block.
pub const NENTRY: usize = BSIZE / 4;
/// On-disk structure for each imap block.
/// Stores the disk block number for each inum.
#[repr(C)]
#[derive(Clone)]
struct DImapBlock {
entry: [u32; NENTRY],
}
impl<'s> From<&'s BufData> for &'s DImapBlock {
fn from(b: &'s BufData) -> Self {
const_assert!(mem::size_of::<DImapBlock>() <= mem::size_of::<BufData>());
const_assert!(mem::align_of::<BufData>() % mem::align_of::<DImapBlock>() == 0);
unsafe { &*(b.as_ptr() as *const DImapBlock) }
}
}
impl<'s> From<&'s mut BufData> for &'s mut DImapBlock {
fn from(b: &'s mut BufData) -> Self {
const_assert!(mem::size_of::<DImapBlock>() <= mem::size_of::<BufData>());
const_assert!(mem::align_of::<BufData>() % mem::align_of::<DImapBlock>() == 0);
unsafe { &mut *(b.as_mut_ptr() as *mut DImapBlock) }
}
}
/// Stores the address of each imap block.
pub struct Imap {
dev_no: u32,
ninodes: u32,
addr: [u32; IMAPSIZE],
}
impl Imap {
pub fn new(dev_no: u32, ninodes: u32, addr: [u32; IMAPSIZE]) -> Self {
Self {
dev_no,
ninodes,
addr,
}
}
/// For the inode with inode number `inum`,
/// returns where the inode's mapping is stored in the imap in the form of (imap block number, offset within block).
fn get_imap_block_no(&self, inum: u32) -> (usize, usize) {
(inum as usize / NENTRY, inum as usize % NENTRY)
}
/// Returns the `block_no`th block of the imap.
fn get_imap_block(&self, block_no: usize, ctx: &KernelCtx<'_, '_>) -> Buf {
hal().disk().read(self.dev_no, self.addr[block_no], ctx)
}
/// Returns the disk block number of the imap's `n`th block.
///
/// # Note
///
/// This method should be used only inside the cleaner.
/// Usually, you should use `Imap::{get, set}` instead of this method.
///
/// # Panic
///
/// Panics if the imap does not have an `n`th block.
pub fn get_nth_block(&self, n: usize) -> u32 {
assert!(n < IMAPSIZE);
self.addr[n]
}
/// Returns the imap in the on-disk format.
/// This should be written at the checkpoint of the disk.
pub fn dimap(&self) -> [u32; IMAPSIZE] {
self.addr
}
/// Returns an unused inum.
pub fn get_empty_inum(&self, ctx: &KernelCtx<'_, '_>) -> Option<u32> {
for i in 0..IMAPSIZE {
let buf = self.get_imap_block(i, ctx);
let imap_block: &DImapBlock = buf.data().into();
for j in 0..NENTRY {
let inum = (i * NENTRY + j) as u32;
// inum: (0, ninodes)
if inum != 0 && inum < self.ninodes && imap_block.entry[j] == 0 {
buf.free(ctx);
return Some(inum);
}
}
buf.free(ctx);
}
None
}
/// For the inode with inode number `inum`, returns the disk_block_no of it.
pub fn get(&self, inum: u32, ctx: &KernelCtx<'_, '_>) -> u32 {
assert!(
0 < inum && inum < ctx.kernel().fs().superblock().ninodes(),
"invalid inum"
);
let (block_no, offset) = self.get_imap_block_no(inum);
let buf = self.get_imap_block(block_no, ctx);
const_assert!(mem::size_of::<DImapBlock>() <= mem::size_of::<BufData>());
const_assert!(mem::align_of::<BufData>() % mem::align_of::<DImapBlock>() == 0);
let imap_block: &DImapBlock = buf.data().into();
let res = imap_block.entry[offset];
buf.free(ctx);
res
}
/// Copies the imap's `block_no`th block to the segment.
/// Returns the `Buf` of the new imap block if success.
pub fn update(
&mut self,
block_no: u32,
seg: &mut SegManager,
ctx: &KernelCtx<'_, '_>,
) -> Option<Buf> {
seg.get_or_add_updated_imap_block(block_no, ctx)
.map(|(mut buf, addr)| {
let block_no = block_no as usize;
if addr != self.addr[block_no] {
// Copy the imap block content from old imap block.
let old_buf = self.get_imap_block(block_no, ctx);
buf.data_mut().copy_from(old_buf.data());
// Update imap mapping.
self.addr[block_no] = addr;
old_buf.free(ctx);
}
buf
})
}
/// For the inode with inode number `inum`, updates its mapping in the imap to disk_block_no.
/// Then, we append the new imap block to the segment.
/// Returns true if successful. Otherwise, returns false.
pub fn set(
&mut self,
inum: u32,
disk_block_no: u32,
seg: &mut SegManager,
ctx: &KernelCtx<'_, '_>,
) -> bool {
assert!(0 < inum && inum < self.ninodes, "invalid inum");
let (block_no, offset) = self.get_imap_block_no(inum);
if let Some(mut buf) = self.update(block_no as u32, seg, ctx) {
// Update entry.
let imap_block: &mut DImapBlock = buf.data_mut().into();
imap_block.entry[offset] = disk_block_no;
buf.free(ctx);
true
} else {
false
}
}
}