-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.rs
More file actions
1248 lines (1113 loc) · 37.7 KB
/
Copy pathfile.rs
File metadata and controls
1248 lines (1113 loc) · 37.7 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use alloc::{
boxed::Box,
collections::BTreeMap,
sync::{Arc, Weak},
vec::Vec,
};
#[cfg(feature = "times")]
use core::sync::atomic::{AtomicU8, Ordering};
use core::{num::NonZeroUsize, ops::Range, task::Context};
use axalloc::global_allocator;
use axfs_ng_vfs::{
FileNode, Location, NodeFlags, NodePermission, NodeType, VfsError, VfsResult, path::Path,
};
use axhal::mem::{PhysAddr, VirtAddr, virt_to_phys};
use axio::{SeekFrom, prelude::*};
use axpoll::{IoEvents, Pollable};
use axsync::Mutex;
use lru::LruCache;
use spin::{Lazy, Mutex as SpinMutex, RwLock};
use super::FsContext;
bitflags::bitflags! {
#[derive(Debug, Clone, Copy)]
pub struct FileFlags: u8 {
const READ = 1;
const WRITE = 2;
const EXECUTE = 4;
const APPEND = 8;
const PATH = 16;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct FileCacheKey {
fs_id: usize,
inode: u64,
}
fn filesystem_id(loc: &Location) -> usize {
loc.filesystem() as *const dyn axfs_ng_vfs::FilesystemOps as *const () as usize
}
fn file_cache_key(loc: &Location) -> FileCacheKey {
FileCacheKey {
fs_id: filesystem_id(loc),
inode: loc.inode(),
}
}
fn prune_file_shared_states(registry: &mut BTreeMap<FileCacheKey, Weak<CachedFileShared>>) {
registry.retain(|_, state| state.strong_count() > 0);
}
static FILE_SHARED_STATES: Lazy<SpinMutex<BTreeMap<FileCacheKey, Weak<CachedFileShared>>>> =
Lazy::new(|| SpinMutex::new(BTreeMap::new()));
/// Results returned by [`OpenOptions::open`].
pub enum OpenResult {
File(File),
Dir(Location),
}
impl OpenResult {
pub fn into_file(self) -> VfsResult<File> {
match self {
Self::File(file) => Ok(file),
Self::Dir(_) => Err(VfsError::IsADirectory),
}
}
pub fn into_dir(self) -> VfsResult<Location> {
match self {
Self::Dir(dir) => Ok(dir),
Self::File(_) => Err(VfsError::NotADirectory),
}
}
pub fn into_location(self) -> Location {
match self {
Self::File(file) => file.location().clone(),
Self::Dir(dir) => dir,
}
}
}
/// Options and flags which can be used to configure how a file is opened.
#[derive(Debug, Clone)]
pub struct OpenOptions {
// generic
read: bool,
write: bool,
append: bool,
truncate: bool,
create: bool,
create_new: bool,
directory: bool,
no_follow: bool,
direct: bool,
user: Option<(u32, u32)>,
path: bool,
node_type: NodeType,
// system-specific
mode: u32,
}
impl OpenOptions {
/// Creates a blank new set of options ready for configuration.
pub fn new() -> Self {
Self {
// generic
read: false,
write: false,
append: false,
truncate: false,
create: false,
create_new: false,
directory: false,
no_follow: false,
direct: false,
user: None,
path: false,
node_type: NodeType::RegularFile,
// system-specific
mode: 0o666,
}
}
/// Sets the option for read access.
pub fn read(&mut self, read: bool) -> &mut Self {
self.read = read;
self
}
/// Sets the option for write access.
pub fn write(&mut self, write: bool) -> &mut Self {
self.write = write;
self
}
/// Sets the option for the append mode.
pub fn append(&mut self, append: bool) -> &mut Self {
self.append = append;
self
}
/// Sets the option for truncating a previous file.
pub fn truncate(&mut self, truncate: bool) -> &mut Self {
self.truncate = truncate;
self
}
/// Sets the option to create a new file, or open it if it already exists.
pub fn create(&mut self, create: bool) -> &mut Self {
self.create = create;
self
}
/// Sets the option to create a new file, failing if it already exists.
pub fn create_new(&mut self, create_new: bool) -> &mut Self {
self.create_new = create_new;
self
}
/// Sets the option to open directory instead.
pub fn directory(&mut self, directory: bool) -> &mut Self {
self.directory = directory;
self
}
/// Sets the option to not follow symlinks.
pub fn no_follow(&mut self, no_follow: bool) -> &mut Self {
self.no_follow = no_follow;
self
}
/// Sets the option to open the file with direct I/O.\
pub fn direct(&mut self, direct: bool) -> &mut Self {
self.direct = direct;
self
}
/// Sets the user and group id to open the file with.
pub fn user(&mut self, uid: u32, gid: u32) -> &mut Self {
self.user = Some((uid, gid));
self
}
/// Sets the option for path only access.
pub fn path(&mut self, path: bool) -> &mut Self {
self.path = path;
self
}
/// Sets the node type for the file.
///
/// This will only be used if the file is created.
pub fn node_type(&mut self, node_type: NodeType) -> &mut Self {
self.node_type = node_type;
self
}
/// Sets the mode bits that a new file will be created with.
pub fn mode(&mut self, mode: u32) -> &mut Self {
self.mode = mode;
self
}
fn _open(&self, loc: Location) -> VfsResult<OpenResult> {
let flags = self.to_flags()?;
if loc.is_dir() && (self.create || self.create_new || flags.contains(FileFlags::WRITE)) {
return Err(VfsError::IsADirectory);
}
if self.directory {
if flags.contains(FileFlags::WRITE) {
return Err(VfsError::IsADirectory);
}
loc.check_is_dir()?;
}
if self.truncate && loc.metadata()?.node_type == NodeType::RegularFile {
loc.entry().as_file()?.set_len(0)?;
}
Ok(if loc.is_dir() {
OpenResult::Dir(loc)
} else {
// TODO(mivik): is this correct?
let non_cacheable_type = matches!(
loc.metadata()?.node_type,
NodeType::CharacterDevice
| NodeType::BlockDevice
| NodeType::Fifo
| NodeType::Socket
);
let direct = non_cacheable_type
|| self.path
|| self.direct
|| loc.flags().contains(NodeFlags::NON_CACHEABLE);
let backend = if !direct || loc.flags().contains(NodeFlags::ALWAYS_CACHE) {
FileBackend::new_cached(loc)
} else {
FileBackend::new_direct(loc)
};
OpenResult::File(File::new(backend, flags))
})
}
pub fn open_loc(&self, loc: Location) -> VfsResult<OpenResult> {
if !self.is_valid() {
return Err(VfsError::InvalidInput);
}
self._open(loc)
}
pub fn open(&self, context: &FsContext, path: impl AsRef<Path>) -> VfsResult<OpenResult> {
if !self.is_valid() {
return Err(VfsError::InvalidInput);
}
let loc = match context.resolve_parent(path.as_ref()) {
Ok((parent, name)) => {
let mut loc = parent.open_file(
&name,
&axfs_ng_vfs::OpenOptions {
create: self.create,
create_new: self.create_new,
node_type: self.node_type,
permission: NodePermission::from_bits_truncate(self.mode as _),
user: self.user.or(context.credentials),
},
)?;
if !self.no_follow {
loc = context
.with_current_dir(parent)?
.try_resolve_symlink(loc, &mut 0)?;
}
loc
}
Err(VfsError::InvalidInput) => {
// root directory
context.root_dir().clone()
}
Err(err) => return Err(err),
};
self._open(loc)
}
pub(crate) fn to_flags(&self) -> VfsResult<FileFlags> {
Ok(match (self.read, self.write, self.append) {
(true, false, false) => FileFlags::READ,
(false, true, false) => FileFlags::WRITE,
(true, true, false) => FileFlags::READ | FileFlags::WRITE,
(false, _, true) => FileFlags::WRITE | FileFlags::APPEND,
(true, _, true) => FileFlags::READ | FileFlags::WRITE | FileFlags::APPEND,
(false, false, false) => return Err(VfsError::InvalidInput),
} | if self.path {
FileFlags::PATH
} else {
FileFlags::empty()
})
}
pub(crate) fn is_valid(&self) -> bool {
if !self.read && !self.write && !self.append {
return true;
}
match (self.write, self.append) {
(true, false) => {}
(false, false) => {
if self.truncate {
return false;
}
}
(_, true) => {
if self.truncate && !self.create_new {
return false;
}
}
}
true
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
const PAGE_SIZE: usize = 4096;
#[derive(Debug)]
pub struct PageCache {
addr: VirtAddr,
dirty: bool,
}
impl PageCache {
fn new(skip_zero: bool) -> VfsResult<Self> {
let addr = global_allocator()
.alloc_pages(1, PAGE_SIZE)
.inspect_err(|err| {
warn!("Failed to allocate page cache: {:?}", err);
})
.map_err(|_| VfsError::StorageFull)?;
if !skip_zero {
unsafe { core::ptr::write_bytes(addr as *mut u8, 0, PAGE_SIZE) };
}
Ok(Self {
addr: addr.into(),
dirty: false,
})
}
pub fn paddr(&self) -> PhysAddr {
virt_to_phys(self.addr)
}
pub fn mark_dirty(&mut self) {
self.dirty = true;
}
pub fn data(&mut self) -> &mut [u8] {
unsafe { core::slice::from_raw_parts_mut(self.addr.as_mut_ptr(), PAGE_SIZE) }
}
}
impl Drop for PageCache {
fn drop(&mut self) {
if self.dirty {
warn!("dirty page dropped without flushing");
}
global_allocator().dealloc_pages(self.addr.as_usize(), 1);
}
}
struct EvictListener {
listener: Box<dyn Fn(u32, &PageCache) + Send + Sync>,
}
struct CachedFileShared {
page_cache: Mutex<LruCache<u32, PageCache>>,
evict_listeners: Mutex<Vec<Arc<EvictListener>>>,
io_lock: RwLock<()>,
}
impl CachedFileShared {
pub fn new(in_memory: bool) -> Self {
Self {
page_cache: if in_memory {
Mutex::new(LruCache::unbounded())
} else {
Mutex::new(LruCache::new(NonZeroUsize::new(16384).unwrap()))
},
evict_listeners: Mutex::new(Vec::new()),
io_lock: RwLock::new(()),
}
}
fn evict_listeners_snapshot(&self) -> Vec<Arc<EvictListener>> {
self.evict_listeners.lock().clone()
}
fn evict_cache(&self, file: &FileNode, pn: u32, page: &mut PageCache) -> VfsResult<()> {
let listeners = self.evict_listeners_snapshot();
for listener in listeners.iter() {
(listener.listener)(pn, page);
}
if page.dirty {
let page_start = pn as u64 * PAGE_SIZE as u64;
let len = (file.len()?.saturating_sub(page_start)).min(PAGE_SIZE as u64) as usize;
if len > 0 {
file.write_at(&page.data()[..len], page_start)?;
}
page.dirty = false;
}
Ok(())
}
fn flush_dirty_pages(&self, file: &FileNode) -> VfsResult<()> {
const MAX_COALESCE_PAGES: usize = 32; // Limit contiguous writes to 128KB
let file_len = file.len()?;
let mut guard = self.page_cache.lock();
let mut dirty_pns: Vec<u32> = guard
.iter()
.filter(|(_, page)| page.dirty)
.map(|(pn, _)| *pn)
.collect();
dirty_pns.sort_unstable();
if dirty_pns.is_empty() {
return Ok(());
}
let mut i = 0;
while i < dirty_pns.len() {
let mut j = i + 1;
while j < dirty_pns.len()
&& dirty_pns[j] == dirty_pns[j - 1] + 1
&& (j - i) < MAX_COALESCE_PAGES
{
j += 1;
}
let span_pns = &dirty_pns[i..j];
let start_pn = span_pns[0];
let mut combined_data = Vec::new();
for &pn in span_pns {
if let Some(page) = guard.get_mut(&pn) {
let page_start = pn as u64 * PAGE_SIZE as u64;
let len = (file_len.saturating_sub(page_start)).min(PAGE_SIZE as u64) as usize;
if len > 0 {
combined_data.extend_from_slice(&page.data()[..len]);
}
}
}
if !combined_data.is_empty() {
let written = file.write_at(&combined_data, start_pn as u64 * PAGE_SIZE as u64)?;
let mut bytes_marked = 0;
for &pn in span_pns {
if let Some(page) = guard.get_mut(&pn) {
let page_start = pn as u64 * PAGE_SIZE as u64;
let len = (file_len.saturating_sub(page_start)).min(PAGE_SIZE as u64) as usize;
if bytes_marked + len <= written {
bytes_marked += len;
page.dirty = false;
} else {
break;
}
}
}
} else {
for &pn in span_pns {
if let Some(page) = guard.get_mut(&pn) {
page.dirty = false;
}
}
}
i = j;
}
Ok(())
}
#[allow(dead_code)]
fn discard_pages(
&self,
file: &FileNode,
keys: Vec<u32>,
write_back_dirty: bool,
) -> VfsResult<()> {
let mut guard = self.page_cache.lock();
for pn in keys {
let Some(mut page) = guard.pop(&pn) else {
continue;
};
if page.dirty && write_back_dirty {
if let Err(err) = self.evict_cache(file, pn, &mut page) {
guard.put(pn, page);
return Err(err);
}
} else {
let listeners = self.evict_listeners_snapshot();
for listener in listeners.iter() {
(listener.listener)(pn, &page);
}
page.dirty = false;
}
}
Ok(())
}
fn discard_all_pages(&self, file: &FileNode, write_back_dirty: bool) -> VfsResult<()> {
let mut guard = self.page_cache.lock();
while let Some((pn, mut page)) = guard.pop_lru() {
if page.dirty && write_back_dirty {
if let Err(err) = self.evict_cache(file, pn, &mut page) {
guard.put(pn, page);
return Err(err);
}
} else {
let listeners = self.evict_listeners_snapshot();
for listener in listeners.iter() {
(listener.listener)(pn, &page);
}
page.dirty = false;
}
}
Ok(())
}
}
impl Drop for CachedFileShared {
fn drop(&mut self) {
let mut guard = self.page_cache.lock();
while let Some((_pn, page)) = guard.pop_lru() {
if page.dirty {
warn!("dirty page dropped without flushing");
}
drop(page);
}
}
}
fn shared_file_state(location: &Location) -> Arc<CachedFileShared> {
let key = file_cache_key(location);
let in_memory = location.filesystem().name() == "tmpfs";
let mut registry = FILE_SHARED_STATES.lock();
prune_file_shared_states(&mut registry);
if let Some(state) = registry.get(&key).and_then(Weak::upgrade) {
return state;
}
let state = Arc::new(CachedFileShared::new(in_memory));
registry.insert(key, Arc::downgrade(&state));
state
}
enum FileUserData {
Weak(Weak<CachedFileShared>),
Strong(Arc<CachedFileShared>),
}
impl FileUserData {
fn get(&self) -> Option<Arc<CachedFileShared>> {
match self {
FileUserData::Weak(weak) => weak.upgrade(),
FileUserData::Strong(strong) => Some(strong.clone()),
}
}
}
#[derive(Clone)]
pub struct CachedFile {
inner: Location,
shared: Arc<CachedFileShared>,
in_memory: bool,
}
impl Drop for CachedFile {
fn drop(&mut self) {
if Arc::strong_count(&self.shared) == 1 {
if let Ok(file) = self.inner.entry().as_file() {
let _ = self.flush_dirty_pages(file);
}
}
}
}
impl CachedFile {
pub fn get_or_create(location: Location) -> Self {
let in_memory = location.filesystem().name() == "tmpfs";
let mut guard = location.user_data();
let shared = if let Some(shared) = guard.get::<FileUserData>().and_then(|it| it.get()) {
shared
} else {
let shared = shared_file_state(&location);
let user_data = if in_memory {
FileUserData::Strong(shared.clone())
} else {
FileUserData::Weak(Arc::downgrade(&shared))
};
guard.insert(user_data);
shared
};
drop(guard);
Self {
inner: location,
shared,
in_memory,
}
}
pub fn ptr_eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
pub fn in_memory(&self) -> bool {
self.in_memory
}
pub fn add_evict_listener<F>(&self, listener: F) -> usize
where
F: Fn(u32, &PageCache) + Send + Sync + 'static,
{
let pointer = Arc::new(EvictListener {
listener: Box::new(listener),
});
let handle = Arc::as_ptr(&pointer) as usize;
self.shared.evict_listeners.lock().push(pointer);
handle
}
pub unsafe fn remove_evict_listener(&self, handle: usize) {
let mut guard = self.shared.evict_listeners.lock();
if let Some(pos) = guard
.iter()
.position(|listener| Arc::as_ptr(listener) as usize == handle)
{
guard.remove(pos);
}
}
fn evict_cache(&self, file: &FileNode, pn: u32, page: &mut PageCache) -> VfsResult<()> {
let listeners = self.shared.evict_listeners_snapshot();
for listener in listeners.iter() {
(listener.listener)(pn, page);
}
if page.dirty {
let page_start = pn as u64 * PAGE_SIZE as u64;
let len = (file.len()?.saturating_sub(page_start)).min(PAGE_SIZE as u64) as usize;
if len > 0 {
file.write_at(&page.data()[..len], page_start)?;
}
page.dirty = false;
}
Ok(())
}
fn flush_dirty_pages(&self, file: &FileNode) -> VfsResult<()> {
self.shared.flush_dirty_pages(file)
}
fn discard_pages(
&self,
file: &FileNode,
keys: Vec<u32>,
write_back_dirty: bool,
) -> VfsResult<()> {
let mut guard = self.shared.page_cache.lock();
for pn in keys {
let Some(mut page) = guard.pop(&pn) else {
continue;
};
if page.dirty && write_back_dirty {
if let Err(err) = self.evict_cache(file, pn, &mut page) {
guard.put(pn, page);
return Err(err);
}
} else {
let listeners = self.shared.evict_listeners_snapshot();
for listener in listeners.iter() {
(listener.listener)(pn, &page);
}
page.dirty = false;
}
}
Ok(())
}
fn page_or_insert<'a>(
&self,
file: &FileNode,
cache: &'a mut LruCache<u32, PageCache>,
pn: u32,
skip_read: bool,
) -> VfsResult<(&'a mut PageCache, Option<(u32, PageCache)>)> {
// TODO: Matching the result of `get_mut` confuses compiler. See
// https://users.rust-lang.org/t/return-do-not-release-mutable-borrow/55757.
if cache.contains(&pn) {
return Ok((cache.get_mut(&pn).unwrap(), None));
}
let mut evicted = None;
if cache.len() == cache.cap().get() {
// Cache is full, remove the least recently used page
if let Some((pn, mut page)) = cache.pop_lru() {
if let Err(err) = self.evict_cache(file, pn, &mut page) {
cache.put(pn, page);
return Err(err);
}
evicted = Some((pn, page));
}
}
// Page not in cache, read it
let mut page = PageCache::new(skip_read)?;
if self.in_memory {
if !skip_read {
page.data().fill(0);
}
} else if !skip_read {
file.read_at(page.data(), pn as u64 * PAGE_SIZE as u64)?;
}
cache.put(pn, page);
Ok((cache.get_mut(&pn).unwrap(), evicted))
}
pub fn with_page<R>(&self, pn: u32, f: impl FnOnce(Option<&mut PageCache>) -> R) -> R {
let _guard = self.shared.io_lock.read();
f(self.shared.page_cache.lock().get_mut(&pn))
}
pub fn with_page_or_insert<R>(
&self,
pn: u32,
f: impl FnOnce(&mut PageCache, Option<(u32, PageCache)>) -> VfsResult<R>,
) -> VfsResult<R> {
let _guard = self.shared.io_lock.write();
let mut guard = self.shared.page_cache.lock();
let (page, evicted) = self.page_or_insert(self.inner.entry().as_file()?, &mut guard, pn, false)?;
f(page, evicted)
}
fn with_pages<T>(
&self,
range: Range<u64>,
is_write: bool,
page_initial: impl FnOnce(&FileNode) -> VfsResult<T>,
mut page_each: impl FnMut(T, &mut PageCache, Range<usize>) -> VfsResult<T>,
) -> VfsResult<T> {
let file = self.inner.entry().as_file()?;
let mut initial = page_initial(file)?;
let start_page = (range.start / PAGE_SIZE as u64) as u32;
let end_page = range.end.div_ceil(PAGE_SIZE as u64) as u32;
let mut page_offset = (range.start % PAGE_SIZE as u64) as usize;
for pn in start_page..end_page {
let page_start = pn as u64 * PAGE_SIZE as u64;
let page_end = (range.end - page_start).min(PAGE_SIZE as u64) as usize;
let skip_read = is_write && (page_offset == 0) && (page_end == PAGE_SIZE);
let mut guard = self.shared.page_cache.lock();
let page = self.page_or_insert(file, &mut guard, pn, skip_read)?.0;
initial = page_each(
initial,
page,
page_offset..page_end,
)?;
page_offset = 0;
}
Ok(initial)
}
pub fn read_at(&self, mut dst: impl Write + IoBufMut, offset: u64) -> VfsResult<usize> {
let _guard = self.shared.io_lock.read();
let len = self.inner.len()?;
let end = (offset + dst.remaining_mut() as u64).min(len);
if end <= offset {
return Ok(0);
}
self.with_pages(
offset..end,
false,
|_| Ok(0),
|read, page, range| {
let len = range.end - range.start;
dst.write(&page.data()[range.start..range.end])?;
Ok(read + len)
},
)
}
fn write_at_locked(&self, mut buf: impl Read + IoBuf, offset: u64) -> VfsResult<usize> {
let end = offset + buf.remaining() as u64;
self.with_pages(
offset..end,
true,
|file| {
if end > file.len()? {
file.set_len(end)?;
}
Ok(0)
},
|written, page, range| {
let len = range.end - range.start;
buf.read(&mut page.data()[range.start..range.end])?;
if !self.in_memory {
page.dirty = true;
}
Ok(written + len)
},
)
}
pub fn write_at(&self, buf: impl Read + IoBuf, offset: u64) -> VfsResult<usize> {
let _guard = self.shared.io_lock.write();
self.write_at_locked(buf, offset)
}
pub fn append(&self, buf: impl Read + IoBuf) -> VfsResult<(usize, u64)> {
let _guard = self.shared.io_lock.write();
let file = self.inner.entry().as_file()?;
let len = file.len()?;
self.write_at_locked(buf, len)
.map(|written| (written, len + written as u64))
}
pub fn set_len(&self, len: u64) -> VfsResult<()> {
let _guard = self.shared.io_lock.write();
let file = self.inner.entry().as_file()?;
let old_len = file.len()?;
file.set_len(len)?;
let old_last_page = (old_len / PAGE_SIZE as u64) as u32;
let new_last_page = (len / PAGE_SIZE as u64) as u32;
if old_len < len {
let mut guard = self.shared.page_cache.lock();
if let Some(page) = guard.get_mut(&old_last_page) {
let page_start = old_last_page as u64 * PAGE_SIZE as u64;
let old_page_offset = (old_len - page_start) as usize;
let new_page_offset = (len - page_start).min(PAGE_SIZE as u64) as usize;
page.data()[old_page_offset..new_page_offset].fill(0);
}
} else if old_last_page > new_last_page {
// For truncating, we need to remove all pages that are beyond the
// new length
// TODO(mivik): can this be more efficient?
let mut guard = self.shared.page_cache.lock();
if let Some(page) = guard.get_mut(&new_last_page) {
let page_start = new_last_page as u64 * PAGE_SIZE as u64;
let new_page_offset = (len - page_start) as usize;
page.data()[new_page_offset..].fill(0);
}
let keys = guard
.iter()
.map(|(k, _)| *k)
.filter(|it| *it > new_last_page)
.collect::<Vec<_>>();
drop(guard);
self.discard_pages(file, keys, false)?;
}
Ok(())
}
pub fn sync(&self, data_only: bool) -> VfsResult<()> {
if self.in_memory {
return Ok(());
}
let _guard = self.shared.io_lock.write();
let file = self.inner.entry().as_file()?;
self.flush_dirty_pages(file)?;
file.sync(data_only)?;
Ok(())
}
pub fn location(&self) -> &Location {
&self.inner
}
/// Returns the physical address of the page at the given page index.
///
/// If the page is not in the cache, it will be read from the file.
pub fn get_shared_page_paddr(&self, pn: u32) -> VfsResult<PhysAddr> {
self.with_page_or_insert(pn, |page, _| Ok(page.paddr()))
}
/// Marks the page at the given page index as dirty.
pub fn mark_page_dirty(&self, pn: u32) -> VfsResult<()> {
self.with_page(pn, |page| {
if let Some(page) = page {
if !self.in_memory {
page.mark_dirty();
}
}
});
Ok(())
}
}
/// Low-level interface for file operations.
#[derive(Clone)]
pub enum FileBackend {
Cached(CachedFile),
Direct(Location),
}
impl FileBackend {
pub(crate) fn new_direct(location: Location) -> Self {
Self::Direct(location)
}
pub(crate) fn new_cached(location: Location) -> Self {
Self::Cached(CachedFile::get_or_create(location))
}
pub fn read_at(&self, mut dst: impl Write + IoBufMut, mut offset: u64) -> VfsResult<usize> {
match self {
Self::Cached(cached) => cached.read_at(dst, offset),
Self::Direct(loc) => {
let shared = shared_file_state(loc);
let _guard = shared.io_lock.read();
dst.read_from(&mut axio::read_fn(|buf| {
loc.entry().as_file()?.read_at(buf, offset).inspect(|read| {
offset += *read as u64;
})
}))
}
}
}
pub fn write_at(&self, mut src: impl Read + IoBuf, mut offset: u64) -> VfsResult<usize> {
match self {
Self::Cached(cached) => cached.write_at(src, offset),
Self::Direct(loc) => {
let shared = shared_file_state(loc);
let _guard = shared.io_lock.write();
let file = loc.entry().as_file()?;
shared.flush_dirty_pages(file)?;
let result = src.write_to(&mut axio::write_fn(|buf| {
file.write_at(buf, offset).inspect(|written| {
offset += *written as u64;
})
}));
let invalidate = shared.discard_all_pages(file, false);
match (result, invalidate) {
(Ok(written), Ok(())) => Ok(written),
(Err(err), Ok(())) => Err(err),
(Ok(_), Err(err)) => Err(err),
(Err(err), Err(_)) => Err(err),
}
}
}
}
pub fn append(&self, mut src: impl Read + IoBuf) -> VfsResult<(usize, u64)> {
match self {
Self::Cached(cached) => cached.append(src),
Self::Direct(loc) => {
let shared = shared_file_state(loc);
let _guard = shared.io_lock.write();
let file = loc.entry().as_file()?;
shared.flush_dirty_pages(file)?;
let mut end = 0;
let result = src.write_to(&mut axio::write_fn(|buf| {
file.append(buf).map(|(n, offset)| {
end = offset;
n
})
}));
let invalidate = shared.discard_all_pages(file, false);
match (result, invalidate) {
(Ok(n), Ok(())) => Ok((n, end)),
(Err(err), Ok(())) => Err(err),
(Ok(_), Err(err)) => Err(err),
(Err(err), Err(_)) => Err(err),
}
}
}
}
pub fn location(&self) -> &Location {
match self {
Self::Cached(cached) => cached.location(),
Self::Direct(loc) => loc,
}
}