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
use std::ffi::OsStr;

use fuse::{
    FileAttr, FileType, Filesystem, ReplyAttr, ReplyData, ReplyDirectory, ReplyEmpty, ReplyEntry,
    ReplyOpen, ReplyWrite, ReplyXattr, Request,
};
use libc::{c_int, ENOENT};
use log::info;
use time::Timespec;

use crate::Oramfs;

const TTL: Timespec = Timespec { sec: 1, nsec: 0 };
const UNIX_EPOCH: Timespec = Timespec { sec: 0, nsec: 0 };
pub const BIG_FILE_NAME: &str = "oram";
const MOUNTPOINT_INO: u64 = 1;
const BIG_FILE_INO: u64 = 2;

/// Attributes of the private directory
const PRIVATE_DIR_ATTR: FileAttr = FileAttr {
    ino: MOUNTPOINT_INO,
    size: 4096,
    blocks: 8,
    atime: UNIX_EPOCH,
    mtime: UNIX_EPOCH,
    ctime: UNIX_EPOCH,
    crtime: UNIX_EPOCH,
    kind: FileType::Directory,
    perm: 0o755,
    nlink: 2,
    uid: 0,
    gid: 0,
    rdev: 0,
    flags: 0,
};

/// Attributes of the "oram" file within the private directory
const BIG_FILE_ATTR: FileAttr = FileAttr {
    ino: BIG_FILE_INO,
    size: 10_000_000,
    blocks: 1,
    atime: UNIX_EPOCH,
    mtime: UNIX_EPOCH,
    ctime: UNIX_EPOCH,
    crtime: UNIX_EPOCH,
    kind: FileType::RegularFile,
    perm: 0o644,
    nlink: 1,
    uid: 0,
    gid: 0,
    rdev: 0,
    flags: 0,
};

/// Implement the FUSE operations necessary to get the ORAMFS working
impl Filesystem for Oramfs<'_> {
    fn init(&mut self, _req: &Request) -> Result<(), c_int> {
        Ok(())
    }

    fn lookup(&mut self, _req: &Request, parent: u64, name: &OsStr, reply: ReplyEntry) {
        if parent == MOUNTPOINT_INO && name.to_str() == Some(BIG_FILE_NAME) {
            let mut attr = BIG_FILE_ATTR;
            attr.size = self.oram_size;
            reply.entry(&TTL, &attr, 0);
        } else {
            reply.error(ENOENT);
        }
    }

    fn getattr(&mut self, _req: &Request, ino: u64, reply: ReplyAttr) {
        match ino {
            MOUNTPOINT_INO => reply.attr(&TTL, &PRIVATE_DIR_ATTR),
            BIG_FILE_INO => {
                let mut attr = BIG_FILE_ATTR;
                attr.size = self.oram_size;
                reply.attr(&TTL, &attr)
            }
            _ => reply.error(ENOENT),
        }
    }

    fn setattr(
        &mut self,
        _req: &Request,
        ino: u64,
        _mode: Option<u32>,
        _uid: Option<u32>,
        _gid: Option<u32>,
        _size: Option<u64>,
        _atime: Option<Timespec>,
        _mtime: Option<Timespec>,
        _fh: Option<u64>,
        _crtime: Option<Timespec>,
        _chgtime: Option<Timespec>,
        _bkuptime: Option<Timespec>,
        _flags: Option<u32>,
        reply: ReplyAttr,
    ) {
        match ino {
            MOUNTPOINT_INO => reply.attr(&TTL, &PRIVATE_DIR_ATTR),
            BIG_FILE_INO => {
                let mut attr = BIG_FILE_ATTR;
                attr.size = self.oram_size;
                reply.attr(&TTL, &attr)
            }
            _ => reply.error(ENOENT),
        }
    }

    fn open(&mut self, _req: &Request, ino: u64, _flags: u32, reply: ReplyOpen) {
        match ino {
            BIG_FILE_INO => reply.opened(0, 0),
            _ => reply.error(ENOENT),
        }
    }

    fn read(
        &mut self,
        _req: &Request,
        ino: u64,
        _fh: u64,
        offset: i64,
        size: u32,
        reply: ReplyData,
    ) {
        match ino {
            BIG_FILE_INO => {
                let bytes_read = self.split_read(size, offset);
                self.oram.post_op();
                reply.data(bytes_read.as_slice());
            }
            _ => reply.error(ENOENT),
        }
    }

    fn write(
        &mut self,
        _req: &Request,
        ino: u64,
        _fh: u64,
        offset: i64,
        data: &[u8],
        _flags: u32,
        reply: ReplyWrite,
    ) {
        match ino {
            BIG_FILE_INO => {
                let bytes_written = self.split_write(offset, data);
                self.oram.post_op();
                reply.written(bytes_written);
            }
            _ => reply.error(ENOENT),
        }
    }

    fn flush(&mut self, _req: &Request, _ino: u64, _fh: u64, _lock_owner: u64, reply: ReplyEmpty) {
        reply.ok();
    }

    fn release(
        &mut self,
        _req: &Request,
        _ino: u64,
        _fh: u64,
        _flags: u32,
        _lock_owner: u64,
        _flush: bool,
        reply: ReplyEmpty,
    ) {
        info!("FS Cleanup...");
        self.cleanup();
        reply.ok();
    }

    fn fsync(&mut self, _req: &Request, _ino: u64, _fh: u64, _datasync: bool, reply: ReplyEmpty) {
        reply.ok();
    }

    fn readdir(
        &mut self,
        _req: &Request,
        ino: u64,
        _fh: u64,
        offset: i64,
        mut reply: ReplyDirectory,
    ) {
        match ino {
            MOUNTPOINT_INO => {
                let entries = vec![
                    (MOUNTPOINT_INO, FileType::Directory, "."),
                    (MOUNTPOINT_INO, FileType::Directory, ".."),
                    (BIG_FILE_INO, FileType::RegularFile, BIG_FILE_NAME),
                ];

                for (i, entry) in entries.into_iter().enumerate().skip(offset as usize) {
                    // i + 1 means the index of the next entry
                    reply.add(entry.0, (i + 1) as i64, entry.1, entry.2);
                }
                reply.ok();
            }
            _ => reply.error(ENOENT),
        }
    }

    fn getxattr(&mut self, _req: &Request, ino: u64, _name: &OsStr, _size: u32, reply: ReplyXattr) {
        match ino {
            BIG_FILE_INO => reply.error(ENOENT),
            _ => reply.error(ENOENT),
        };
    }

    fn access(&mut self, _req: &Request, _ino: u64, _mask: u32, reply: ReplyEmpty) {
        reply.ok();
    }
}