summaryrefslogtreecommitdiff
path: root/src/disk/allocation.rs
blob: bde201475d927899b457ec5662d846b461359459 (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use crate::disk::block::{DataBlock, DoubleIndirectBlock, IndirectBlock, TripleIndirectBlock};
use crate::disk::inode::Inode;
use crate::memory::cached_block::convert;
use crate::AyaFS;

impl AyaFS {
    /// 为 Inode 分配新 block, 返回 block 的编号
    pub(crate) fn allocate_block_for(&mut self, inode: &mut Inode) -> Option<u32> {
        // 先看这个 inode 的 direct block 有没有空闲
        for index in inode.direct.iter_mut() {
            if !self.data_bitmap.query(*index as usize) {
                let block_index = self.data_bitmap.allocate().unwrap() as u32;
                // println!("allocating {} for direct", block_index);
                *index = block_index;
                inode.n_blocks += 1;
                // 当调用 get_inode_mut 拿出 &mut Inode 的时候对应的 block 在 cache 里已经脏了
                return Some(block_index);
            }
        }

        // direct block 全部分配完了, 先检查 indirect block 有没有分配, 没有就分配一个
        if !self.data_bitmap.query(inode.single_indirect as usize) {
            inode.single_indirect = self
                .data_bitmap
                .allocate()
                .expect("No free space for new block") as u32;
            // println!("allocating {} for indirect", inode.single_indirect);
        }
        // 在 indirect block 里尝试分配
        if let Some(block_index) = self.allocate_in_indirect(inode.single_indirect) {
            // println!("allocating {} in indirect", block_index);
            inode.n_blocks += 1;
            return Some(block_index);
        }

        // direct & indirect block 全部分配完了, 先检查 double indirect block 有没有分配, 没有就分配一个
        if !self.data_bitmap.query(inode.double_indirect as usize) {
            inode.double_indirect = self
                .data_bitmap
                .allocate()
                .expect("No free space for new block") as u32;
            // println!("allocating {} for double indirect", inode.double_indirect);
        }
        // 在 double indirect block 里尝试分配
        if let Some(block_index) = self.alloc_in_double_indirect(inode.double_indirect) {
            // println!("allocating {} in double indirect", block_index);
            inode.n_blocks += 1;
            return Some(block_index);
        }

        // direct, indirect & double indirect block 全部分配完了, 先检查 triple indirect block 有没有分配, 没有就分配一个
        if !self.data_bitmap.query(inode.triple_indirect as usize) {
            inode.triple_indirect = self
                .data_bitmap
                .allocate()
                .expect("No free space for new block") as u32;
            // println!("allocating {} for triple indirect", inode.triple_indirect);
        }
        // 在 double indirect block 里尝试分配
        if let Some(block_index) = self.alloc_in_triple_indirect(inode.triple_indirect) {
            // println!("allocating {} in triple indirect", block_index);
            inode.n_blocks += 1;
            return Some(block_index);
        }
        None
    }

    fn allocate_in_indirect(&mut self, indirect_entry: u32) -> Option<u32> {
        // 取出 single indirect block, 尝试在里面分配
        let indirect_entry = indirect_entry as usize;
        // println!("finding indirect block with number {}, bitmap says {}", indirect_entry, self.data_bitmap.query(indirect_entry));

        if let Some(block) = self
            .get_block(indirect_entry)
            .map(convert::<DataBlock, IndirectBlock>)
        {
            // println!("found indirect block with number {}", indirect_entry);
            let mut indirect_block = block.clone();
            for entry in indirect_block.block.entries.iter_mut() {
                if self.data_bitmap.query(*entry as usize) == false {
                    indirect_block.dirty = true; // 把这个块标记为 dirty
                    let block_index = self.data_bitmap.allocate().expect("No free space") as u32;
                    *entry = block_index;
                    self.update_block(indirect_block);
                    return Some(block_index);
                }
            }
        }
        None
    }

    fn alloc_in_double_indirect(&mut self, double_indirect_entry: u32) -> Option<u32> {
        let double_indirect_entry = double_indirect_entry as usize;

        if let Some(block) = self
            .get_block(double_indirect_entry)
            .map(convert::<DataBlock, DoubleIndirectBlock>)
        {
            let mut double_indirect_block = block.clone();
            for indirect_entry in double_indirect_block.block.indirect.iter_mut() {
                if self.data_bitmap.query(*indirect_entry as usize) == false {
                    double_indirect_block.dirty = true;
                    *indirect_entry = self.data_bitmap.allocate().expect("No free space") as u32;
                }

                if let Some(block_index) = self.allocate_in_indirect(*indirect_entry) {
                    if double_indirect_block.dirty {
                        self.update_block(double_indirect_block);
                    }
                    return Some(block_index);
                }
            }
        }
        None
    }

    fn alloc_in_triple_indirect(&mut self, triple_indirect_entry: u32) -> Option<u32> {
        let triple_indirect_entry = triple_indirect_entry as usize;

        if let Some(block) = self
            .get_block(triple_indirect_entry)
            .map(convert::<DataBlock, TripleIndirectBlock>)
        {
            let mut triple_indirect_block = block.clone();
            for double_indirect_entry in triple_indirect_block.block.double_indirect.iter_mut() {
                if self.data_bitmap.query(*double_indirect_entry as usize) == false {
                    triple_indirect_block.dirty = true;
                    *double_indirect_entry =
                        self.data_bitmap.allocate().expect("No free space") as u32;
                }
                if let Some(block_index) = self.alloc_in_double_indirect(*double_indirect_entry) {
                    if triple_indirect_block.dirty {
                        self.update_block(triple_indirect_block);
                    }
                    return Some(block_index);
                }
            }
        }
        None
    }
}

impl AyaFS {
    /// 从 inode 中删去最后一个 block
    pub(crate) fn deallocate_block_for(&mut self, inode: &mut Inode) -> Option<u32> {
        // 如果 triple indirect 块存在, 则尝试从中销毁一个块
        if self.data_bitmap.query(inode.triple_indirect as usize) {
            if let Some(block_index) = self.deallocate_from_triple_indirect(inode.triple_indirect) {
                inode.n_blocks -= 1;
                return Some(block_index); // 销毁成功, 直接返回
            } else {
                // 销毁失败, 说明 triple indirect 空了, 把它也销毁.
                self.data_bitmap.deallocate(inode.triple_indirect as usize);
                inode.triple_indirect = 0; // 这个地方理论上应该不用?
            }
        }
        // 如果 double indirect 块存在, 则从其中销毁
        if self.data_bitmap.query(inode.double_indirect as usize) {
            if let Some(block_index) = self.deallocate_from_double_indirect(inode.double_indirect) {
                inode.n_blocks -= 1;
                return Some(block_index);
            } else {
                self.data_bitmap.deallocate(inode.double_indirect as usize);
                inode.double_indirect = 0; // 这个地方理论上应该不用?
            }
        }
        // 如果 indirect 块存在, 则从其中销毁
        if self.data_bitmap.query(inode.single_indirect as usize) {
            if let Some(block_index) = self.deallocate_from_indirect(inode.single_indirect) {
                inode.n_blocks -= 1;
                return Some(block_index);
            } else {
                self.data_bitmap.deallocate(inode.single_indirect as usize);
                inode.single_indirect = 0; // 这个地方理论上应该不用?
            }
        }
        // 都没有,直接从 direct 块中销毁
        for entry in inode.direct.iter_mut().rev() {
            if self.data_bitmap.query(*entry as usize) {
                let index = std::mem::replace(entry, 0); // let index = *entry; *entry = 0;
                inode.n_blocks -= 1;
                self.data_bitmap.deallocate(index as usize);
                return Some(index);
            }
        }
        None
    }

    fn deallocate_from_triple_indirect(&mut self, triple_indirect_entry: u32) -> Option<u32> {
        let triple_indirect_entry = triple_indirect_entry as usize;
        if let Some(triple_indirect_block) = self
            .get_block(triple_indirect_entry)
            .map(convert::<DataBlock, TripleIndirectBlock>)
        {
            let mut triple_indirect_block = triple_indirect_block.clone();
            let mut block_modified = false;
            for double_indirect_entry in
                triple_indirect_block.block.double_indirect.iter_mut().rev()
            {
                // 如果这个位置的 double indirect 存在
                if self.data_bitmap.query(*double_indirect_entry as usize) {
                    // 尝试从中销毁一个块
                    if let Some(block_index) =
                        self.deallocate_from_double_indirect(*double_indirect_entry)
                    {
                        if block_modified {
                            self.update_block(triple_indirect_block);
                        }
                        return Some(block_index); // 成功则直接返回
                    } else {
                        // 失败则把这个 double indirect 销毁
                        let double_indirect_entry_to_deallocate =
                            std::mem::replace(double_indirect_entry, 0);
                        self.data_bitmap
                            .deallocate(double_indirect_entry_to_deallocate as usize);
                        triple_indirect_block.dirty = true;
                        block_modified = true;
                    }
                }
            }
            if block_modified {
                self.update_block(triple_indirect_block);
            }
        }
        None
    }

    fn deallocate_from_double_indirect(&mut self, double_indirect_entry: u32) -> Option<u32> {
        let double_indirect_entry = double_indirect_entry as usize;
        if let Some(double_indirect_block) = self
            .get_block(double_indirect_entry)
            .map(convert::<DataBlock, DoubleIndirectBlock>)
        {
            let mut double_indirect_block = double_indirect_block.clone();
            let mut block_modified = false;
            for indirect_entry in double_indirect_block.block.indirect.iter_mut().rev() {
                // 如果这个位置的 indirect 存在
                if self.data_bitmap.query(*indirect_entry as usize) {
                    // 尝试从中销毁一个块
                    if let Some(block_index) = self.deallocate_from_indirect(*indirect_entry) {
                        if block_modified {
                            self.update_block(double_indirect_block);
                        }
                        return Some(block_index); // 成功则直接返回
                    } else {
                        // 失败则把这个 indirect 销毁
                        let indirect_entry_to_deallocate = std::mem::replace(indirect_entry, 0);
                        self.data_bitmap
                            .deallocate(indirect_entry_to_deallocate as usize);
                        double_indirect_block.dirty = true;
                        block_modified = true;
                    }
                }
            }
            if block_modified {
                self.update_block(double_indirect_block);
            }
        }
        None
    }

    fn deallocate_from_indirect(&mut self, indirect_entry: u32) -> Option<u32> {
        let indirect_entry = indirect_entry as usize;
        if let Some(indirect_block) = self
            .get_block(indirect_entry)
            .map(convert::<DataBlock, IndirectBlock>)
        {
            let mut indirect_block = indirect_block.clone();
            // 遍历 indirect block 里的每个 block
            for entry in indirect_block.block.entries.iter_mut().rev() {
                // 如果这个 block 存在, 销毁它
                if self.data_bitmap.query(*entry as usize) {
                    let entry_to_deallocate = std::mem::replace(entry, 0);

                    self.data_bitmap.deallocate(entry_to_deallocate as usize);
                    indirect_block.dirty = true;
                    self.update_block(indirect_block);

                    return Some(entry_to_deallocate);
                }
            }
        }
        None
    }
}