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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::alloc::Layout;
use easy_fs::{BlockDevice, Cache, CacheManager};
use spin::Mutex;

use alloc::alloc::{alloc, dealloc};
use alloc::collections::BTreeMap;
use easy_fs::BLOCK_SZ;
use lazy_static::*;
#[allow(unused)]
use spin::RwLock;

use crate::board::BlockDeviceImpl;
use crate::drivers::BLOCK_DEVICE;

pub struct BlockCache {
    pub cache: &'static mut [u8; BLOCK_SZ],
    block_id: usize,
    block_device: Arc<dyn BlockDevice>,
    modified: bool,
}

const BLOCK_CACHE_LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(BLOCK_SZ, 1) };

impl BlockCache {
    /// Load a new BlockCache from disk.
    pub fn new(block_id: usize, block_device: Arc<dyn BlockDevice>) -> Self {
        let cache: &'static mut [u8; BLOCK_SZ] = unsafe {
            (alloc(BLOCK_CACHE_LAYOUT) as *mut [u8; BLOCK_SZ])
                .as_mut()
                .unwrap()
        };
        //crate::println!("cache ptr: {:?}", cache.as_ptr());
        block_device.read_block(block_id, cache.as_mut_slice());
        Self {
            cache,
            block_id,
            block_device,
            modified: false,
        }
    }

    fn addr_of_offset(&self, offset: usize) -> usize {
        &self.cache[offset] as *const _ as usize
    }

    pub fn get_ref<T>(&self, offset: usize) -> &T
    where
        T: Sized,
    {
        let type_size = core::mem::size_of::<T>();
        assert!(offset + type_size <= BLOCK_SZ);
        let addr = self.addr_of_offset(offset);
        unsafe { &*(addr as *const T) }
    }

    pub fn get_mut<T>(&mut self, offset: usize) -> &mut T
    where
        T: Sized,
    {
        let type_size = core::mem::size_of::<T>();
        assert!(offset + type_size <= BLOCK_SZ);
        self.modified = true;
        let addr = self.addr_of_offset(offset);
        unsafe { &mut *(addr as *mut T) }
    }

    pub fn read<T, V>(&self, offset: usize, f: impl FnOnce(&T) -> V) -> V {
        f(self.get_ref(offset))
    }

    pub fn modify<T, V>(&mut self, offset: usize, f: impl FnOnce(&mut T) -> V) -> V {
        f(self.get_mut(offset))
    }

    pub fn sync(&mut self) {
        if self.modified {
            //println!("drop cache, id = {}", self.block_id);
            self.modified = false;
            self.block_device
                .write_block(self.block_id, self.cache.as_slice());
        }
    }
}

impl Drop for BlockCache {
    fn drop(&mut self) {
        self.sync();
        unsafe { dealloc(self.cache.as_mut_ptr(), BLOCK_CACHE_LAYOUT) };
    }
}

// 0-info扇区
// 1-2 FAT1
// 3-4 FAT2
// 5-7 DirEntry
// 8-19 DATA

//const DIRENT_CACHE_SIZE: usize = 4;
pub struct BlockCacheManager {
    start_sec: usize,
    limit: usize,
    map: BTreeMap<usize, Arc<RwLock<BlockCache>>>,
}

impl BlockCacheManager {
    pub fn new(limit: usize) -> Self {
        Self {
            start_sec: 0,
            limit,
            map: BTreeMap::new(),
        }
    }

    pub fn read_block_cache(
        &self,
        block_id: usize,
        //block_device: Arc<dyn BlockDevice>,
    ) -> Option<Arc<RwLock<BlockCache>>> {
        if let Some(block_cache) = self.map.get(&block_id) {
            Some(block_cache.clone())
        } else {
            None
        }
    }

    pub fn get_block_cache(
        &mut self,
        block_id: usize,
        block_device: Arc<dyn BlockDevice>,
    ) -> Arc<RwLock<BlockCache>> {
        if let Some(block_cache) = self.map.get(&block_id) {
            block_cache.clone()
        } else {
            // substitute
            if self.map.len() == self.limit
            /*BLOCK_CACHE_SIZE*/
            {
                // from front to tail
                let idx = match self.map.iter().find(|(_, arc)| Arc::strong_count(arc) == 1) {
                    Some((&idx, _)) => idx,
                    None => panic!("Run out of BlockCache!"),
                };
                //crate::println!("incoming_block: {}, replaced_block: {}", block_id, idx);
                self.map.remove(&idx);
            }
            // load block into mem and push back
            let block_cache = Arc::new(RwLock::new(BlockCache::new(
                block_id,
                Arc::clone(&block_device),
            )));
            self.map.insert(block_id, Arc::clone(&block_cache));
            //println!("blkcache: {:?}", block_cache.read().cache);
            block_cache
        }
    }

    pub fn drop_all(&mut self) {
        self.map.clear();
    }
}

lazy_static! {
    pub static ref DATA_BLOCK_CACHE_MANAGER: RwLock<BlockCacheManager> =
        RwLock::new(BlockCacheManager::new(1024));
}

lazy_static! {
    pub static ref INFO_CACHE_MANAGER: RwLock<BlockCacheManager> =
        RwLock::new(BlockCacheManager::new(128));
}

#[derive(PartialEq, Copy, Clone, Debug)]
pub enum CacheMode {
    READ,
    WRITE,
}

//pub fn read_block_cache(block_id:usize, rw_mode:CacheMode);

/* 仅用于访问文件数据块,不包括目录项 */
pub fn get_block_cache(
    block_id: usize,
    block_device: Arc<dyn BlockDevice>,
    rw_mode: CacheMode,
) -> Arc<RwLock<BlockCache>> {
    if rw_mode == CacheMode::READ {
        // make sure the blk is in cache
        if let Some(blk) = INFO_CACHE_MANAGER.read().read_block_cache(block_id) {
            return blk;
        }
        DATA_BLOCK_CACHE_MANAGER
            .write()
            .get_block_cache(block_id, block_device);
        DATA_BLOCK_CACHE_MANAGER
            .read()
            .read_block_cache(block_id)
            .unwrap()
    } else {
        if let Some(blk) = INFO_CACHE_MANAGER.read().read_block_cache(block_id) {
            return blk;
        }
        DATA_BLOCK_CACHE_MANAGER
            .write()
            .get_block_cache(block_id, block_device)
    }
}

/* 用于访问保留扇区,以及目录项 */
pub fn get_info_cache(
    block_id: usize,
    block_device: Arc<dyn BlockDevice>,
    rw_mode: CacheMode,
) -> Arc<RwLock<BlockCache>> {
    if rw_mode == CacheMode::READ {
        // make sure the blk is in cache
        if let Some(blk) = DATA_BLOCK_CACHE_MANAGER.read().read_block_cache(block_id) {
            return blk;
        }
        INFO_CACHE_MANAGER
            .write()
            .get_block_cache(block_id, block_device);
        INFO_CACHE_MANAGER
            .read()
            .read_block_cache(block_id)
            .unwrap()
    } else {
        if let Some(blk) = DATA_BLOCK_CACHE_MANAGER.read().read_block_cache(block_id) {
            return blk;
        }
        INFO_CACHE_MANAGER
            .write()
            .get_block_cache(block_id, block_device)
    }
}

pub fn write_to_dev() {
    INFO_CACHE_MANAGER.write().drop_all();
    DATA_BLOCK_CACHE_MANAGER.write().drop_all();
}
/*
那个谁别问我为什么这段代码写的这么难看也别动这些代码
这都是你害得
*/
pub struct InfoCacheMgrWrapper {
    empty: (),
}

pub struct DataCacheMgrWrapper {
    empty: (),
}

pub struct InfoBlockCacheWrapper {
    block_id: usize,
}
pub struct DataBlockCacheWrapper {
    block_id: usize,
}
impl DataBlockCacheWrapper {
    fn new(block_id: usize) -> Self {
        Self { block_id }
    }
}
impl InfoBlockCacheWrapper {
    fn new(block_id: usize) -> Self {
        Self { block_id }
    }
}

macro_rules! BlockCacheImpl {
    ($cache:ident,$func:ident) => {
        impl Cache for $cache {
            fn read<T, V>(&self, offset: usize, f: impl FnOnce(&T) -> V) -> V {
                $func(self.block_id, BLOCK_DEVICE.clone(), CacheMode::READ)
                    .read()
                    .read(offset, f)
            }

            fn modify<T, V>(&mut self, offset: usize, f: impl FnOnce(&mut T) -> V) -> V {
                $func(self.block_id, BLOCK_DEVICE.clone(), CacheMode::READ)
                    .write()
                    .modify(offset, f)
            }
        }
    };
}
macro_rules! CacheMgrImpl {
    ($mgr:ident,$cache:ident) => {
        impl CacheManager for $mgr {
            const CACHE_SZ: usize = BLOCK_SZ;

            type CacheType = $cache;

            fn new() -> Self
            where
                Self: Sized,
            {
                Self { empty: () }
            }
            fn try_get_block_cache(
                &mut self,
                block_id: usize,
                inner_cache_id: usize,
            ) -> Option<Arc<Mutex<Self::CacheType>>> {
                None
            }
            fn get_block_cache<FUNC>(
                &mut self,
                block_id: usize,
                inner_cache_id: usize,
                neighbor: FUNC,
                block_device: Arc<dyn BlockDevice>,
            ) -> Arc<spin::Mutex<Self::CacheType>>
            where
                FUNC: Fn() -> Vec<usize>,
            {
                Arc::new(Mutex::new($cache::new(block_id)))
            }
        }
    };
}

CacheMgrImpl!(DataCacheMgrWrapper, DataBlockCacheWrapper);

BlockCacheImpl!(DataBlockCacheWrapper, get_block_cache);

CacheMgrImpl!(InfoCacheMgrWrapper, InfoBlockCacheWrapper);

BlockCacheImpl!(InfoBlockCacheWrapper, get_info_cache);