summaryrefslogtreecommitdiff
path: root/src/filesystem/trait_impl.rs
blob: 73ae8784a38311249604000c48e75cec89c7aef9 (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
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
use crate::disk::inode::InodeMode;
use crate::utils::{from_systime, time_now, to_fileattr, to_filetype};
use crate::utils::permissions::get_groups;
use crate::{AyaFS, TTL};
use fuser::{Filesystem, KernelConfig, ReplyAttr, ReplyData, ReplyDirectory, ReplyEmpty,
            ReplyEntry, ReplyLseek, ReplyOpen, Request, TimeOrNow,
};
use libc::{c_int, EACCES, EEXIST, ENAMETOOLONG, ENOENT, ENOSPC, ENOSYS, ENOTDIR, EPERM, R_OK, S_ISGID, S_ISUID, S_IXGRP, S_IXOTH, S_IXUSR, W_OK};
use log::debug;
use std::ffi::OsStr;
use std::time::SystemTime;
use fuser::TimeOrNow::{Now, SpecificTime};
use crate::utils::permissions::{check_access, clear_suid_sgid};

impl AyaFS {

}

impl Filesystem for AyaFS {
    fn init(&mut self, _req: &Request<'_>, _config: &mut KernelConfig) -> Result<(), c_int> {
        debug!("`init()");
        Ok(())
    }

    fn destroy(&mut self) {
        debug!("destroy()");
        // TODO 写回
    }

    fn lookup(&mut self, req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEntry) {
        if name.len() > 255 {
            reply.error(ENAMETOOLONG);
            return;
        }

        let parent = parent as usize;
        if let Some(parent_inode) = self.get_inode(parent) {
            if check_access(
                req.uid(),
                req.gid(),
                parent_inode.uid,
                parent_inode.gid,
                parent_inode.mode,
                R_OK
            ) {
                let parent_inode = parent_inode.clone();
                match self.lookup_name(&parent_inode, name) {
                    Ok((inode_index, _, inode)) => {
                        let attr = to_fileattr(inode_index as usize, &inode);
                        reply.entry(&TTL, &attr, 0);
                    },
                    Err(err_code) => reply.error(err_code)
                };
            } else {
                reply.error(EACCES);
            }
        } else {
            reply.error(ENOENT);
        }
    }

    fn getattr(&mut self, _req: &Request<'_>, ino: u64, reply: ReplyAttr) {
        debug!("Filesystem::getattr(ino: {})", ino);
        if let Some(inode) = self.get_inode(ino as usize) {
            reply.attr(&TTL, &to_fileattr(ino as usize, inode));
        } else {
            reply.error(ENOENT);
        }
    }

    fn setattr(
        &mut self,
        req: &Request<'_>,
        ino: u64,
        mode: Option<u32>,
        uid: Option<u32>,
        gid: Option<u32>,
        size: Option<u64>, // TODO 当 setattr 被 ftruncate invoke 时会设置 size
        atime: Option<TimeOrNow>,
        mtime: Option<TimeOrNow>,
        _ctime: Option<SystemTime>,
        fh: Option<u64>, // TODO 当 setattr 被 ftruncate invoke 时会提供 fh
        _crtime: Option<SystemTime>, // 忽略
        _chgtime: Option<SystemTime>, // 忽略
        _bkuptime: Option<SystemTime>, // 忽略
        _flags: Option<u32>, // 忽略
        reply: ReplyAttr,
    ) {
        if let Some(inode) = self.get_inode_mut(ino as usize) {
            // chmod
            if let Some(mode) = mode {
                debug!("chmod on inode {:#x?} mode {:o}", ino, mode);
                if req.uid() != 0 && req.uid() != inode.uid {
                    reply.error(EPERM);
                    return;
                } // uid == 0 (root) or uid == inode.uid (user itself)

                if req.uid() != 0 && req.gid() != inode.gid && !get_groups(req.pid()).contains(&inode.gid) {
                    inode.mode = InodeMode((mode & !S_ISGID) as u16);
                } else {
                    inode.mode = InodeMode(mode as u16);
                }

                inode.ctime = time_now();
                reply.attr(&TTL, &to_fileattr(ino as usize, inode));
                return;
            }

            // chown
            if uid.is_some() || gid.is_some() {
                debug!("chown on inode {:#x?} uid {:?} gid {:?}", ino, uid, gid);
                if let Some(uid) = uid {
                    // 虽然只有 root 可以 chown 但是 user chown 自己也是不报错的
                    if req.uid() != 0 && !(uid == inode.uid && uid == req.uid()) {
                        reply.error(EPERM);
                        return;
                    }
                }
                if let Some(gid) = gid {
                    // 只有 root 和 file owner 才可以 chgrp
                    if req.uid() != 0 && req.uid() != inode.uid {
                        reply.error(EPERM);
                        return;
                    }
                    // root 可以任意 chgrp, 非 root 只能修改到用户自己所在的组里
                    if req.gid() != 0 && !get_groups(req.pid()).contains(&gid) {
                        reply.error(EPERM);
                        return;
                    }
                }

                // 可执行文件要清除 suid & sgid
                if inode.mode.0 & (S_IXUSR | S_IXGRP | S_IXOTH) as u16 != 0 {
                    inode.mode = clear_suid_sgid(inode.mode);
                }

                // chown 要清除 suid
                if let Some(uid) = uid {
                    inode.uid = uid;
                    inode.mode.0 &= !S_ISUID as u16;
                }

                if let Some(gid) = gid {
                    inode.gid = gid;
                    if req.uid() != 0 {
                        inode.mode.0 &= !S_ISGID as u16;
                    }
                }
                inode.ctime = time_now();
                reply.attr(&TTL, &to_fileattr(ino as usize, inode));
                return;
            }

            // ftruncate
            if let Some(size) = size {
                debug!("ftruncate on inode {:#x?} size {:?}", ino, size);
                if let Some(file_handle) = fh {
                    // 有 file handle 的
                    todo!()
                } else {
                    // 没有 file handle 基于 inode 的
                    todo!()
                }
            }

            // time 相关
            if atime.is_some() || mtime.is_some() {
                let current_time = time_now();
                if let Some(atime) = atime {
                    debug!("utimensat on inode {:#x?}, atime {:?}", ino, atime);
                    // root 和 user 可以随意修改 atime, 其他用户只能 touch (即 atime == Now)
                    if req.uid() != 0 && req.uid() != inode.uid && atime != Now {
                        reply.error(EPERM);
                        return;
                    }

                    if req.uid() != 0 && req.uid() != inode.uid && check_access(
                        req.uid(),
                        req.gid(),
                        inode.uid,
                        inode.gid,
                        inode.mode,
                        R_OK, // atime 来说是 read TODO 对吗
                    ) {
                        reply.error(EACCES);
                        return;
                    }

                    inode.atime = match atime {
                        SpecificTime(time) => from_systime(time),
                        Now => current_time,
                    };
                    inode.ctime = current_time;
                }
                if let Some(mtime) = mtime {
                    debug!("utimensat on inode {:#x?}, mtime {:?}", ino, mtime);
                    // root 和 user 可以随意修改 mtime, 其他用户只能 mtime == Now
                    if req.uid() != 0 && req.uid() != inode.uid && mtime != Now {
                        reply.error(EPERM);
                        return;
                    }

                    if req.uid() != 0 && req.uid() != inode.uid && check_access(
                        req.uid(),
                        req.gid(),
                        inode.uid,
                        inode.gid,
                        inode.mode,
                        W_OK, // mtime 来说是 write
                    ) {
                        reply.error(EACCES);
                        return;
                    }

                    inode.mtime = match mtime {
                        SpecificTime(time) => from_systime(time),
                        Now => current_time,
                    };
                    inode.ctime = current_time;
                }
            }
            reply.attr(&TTL, &to_fileattr(ino as usize, inode));
            return;
        } else {
            reply.error(ENOENT);
        }
    }

    fn readlink(&mut self, _req: &Request<'_>, ino: u64, reply: ReplyData) {
        debug!("[Not Implemented] readlink(ino: {})", ino);
        reply.error(ENOSYS);
    }

    fn mknod(
        &mut self,
        req: &Request<'_>,
        parent: u64,
        name: &OsStr,
        mode: u32,
        _umask: u32, // umask 是用不到的
        _rdev: u32,  // the device number (only valid if created file is a device)
        reply: ReplyEntry,
    ) {
        debug!(
            "Filesystem::mknod(parent: {}, name: {:?}, mode: {}, umask: {})",
            parent, name, mode, _umask
        );

        if let Some(parent_inode) = self.get_inode(parent as usize) {
            if check_access(
                req.uid(),
                req.gid(),
                parent_inode.uid,
                parent_inode.gid,
                parent_inode.mode,
                W_OK
            ) {
                if parent_inode.is_dir() {
                    let parent_inode = parent_inode.clone();
                    // 如果已经存在, 返回 already exists
                    if self.lookup_name(&parent_inode, name).is_ok() {
                        reply.error(EEXIST);
                        return;
                    }
                    // 文件名长度超过 255, 返回 filename too long
                    if name.len() > 255 {
                        reply.error(ENAMETOOLONG);
                        return;
                    }

                    let mode = mode as u16;
                    if let Some((inode_index, inode)) = self.create_file(
                        mode,
                        req.uid(),
                        req.gid(),
                        0,
                    ) {
                        let file_attr = to_fileattr(inode_index, inode);
                        // TODO 把 inode 挂到 parent 下
                        self.update_inode(parent as usize, parent_inode);
                        // 前面 clone 了, 这里写回
                        reply.entry(&TTL, &file_attr, 0);
                    } else {
                        // create_inode 失败 -> no enough space
                        reply.error(ENOSPC);
                    }
                } else {
                    // parent 不是 IFDIR -> Not a directory
                    reply.error(ENOTDIR);
                }
            } else {
                reply.error(EACCES);
            }
        } else {
            // parent 不存在 -> No such file or directory
            reply.error(ENOENT);
        }
    }

    fn mkdir(
        &mut self,
        req: &Request<'_>,
        parent: u64,
        name: &OsStr,
        mode: u32,
        _umask: u32, // umask 应该也是用不到的
        reply: ReplyEntry,
    ) {
        if let Some(parent_inode) = self.get_inode(parent as usize) {
            if check_access(
                req.uid(),
                req.gid(),
                parent_inode.uid,
                parent_inode.gid,
                parent_inode.mode,
                W_OK
            ) {
                if parent_inode.is_dir() {
                    let parent_inode = parent_inode.clone();
                    // 如果已经存在, 返回 already exists
                    if self.lookup_name(&parent_inode, name).is_ok() {
                        reply.error(EEXIST);
                        return;
                    }
                    // 文件名长度超过 255, 返回 filename too long
                    if name.len() > 255 {
                        reply.error(ENAMETOOLONG);
                        return;
                    }

                    let mode = mode as u16;
                    if let Some((inode_index, inode)) = self.create_directory(
                        mode,
                        req.uid(),
                        req.gid(),
                        0,
                    ) {
                        let file_attr = to_fileattr(inode_index, inode);
                        // TODO 把 inode 挂到 parent 下
                        self.update_inode(parent as usize, parent_inode);
                        // 前面 clone 了, 这里写回
                        reply.entry(&TTL, &file_attr, 0);
                    } else {
                        // create_inode 失败 -> no enough space
                        reply.error(ENOSPC);
                    }
                } else {
                    // parent 不是 IFDIR -> Not a directory
                    reply.error(ENOTDIR);
                }
            } else {
                reply.error(EACCES);
            }
        } else {
            // parent 不存在 -> No such file or directory
            reply.error(ENOENT);
        }
    }

    fn unlink(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEmpty) {
        debug!("unlink(parent: {:#x?}, name: {:?})", parent, name,);
        if let Some(inode) = self.get_inode_mut(parent as usize) {
            if inode.is_file() {
                // TODO 找到这个 inode 并且删掉
                reply.ok();
            } else {
                reply.error(ENOENT);
            }
        } else {
            reply.error(ENOENT);
        }
    }

    fn rmdir(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEmpty) {
        debug!("rmdir(parent: {:#x?}, name: {:?}", parent, name,);
        if let Some(inode) = self.get_inode_mut(parent as usize) {
            if inode.is_file() {
                // TODO 找到这个 inode 并且删掉
                reply.ok();
            } else {
                reply.error(ENOTDIR);
            }
        } else {
            reply.error(ENOENT);
        }
    }

    fn read(
        &mut self,
        _req: &Request<'_>,
        ino: u64,
        _fh: u64,
        offset: i64,
        _size: u32,
        _flags: i32,
        _lock_owner: Option<u64>,
        reply: ReplyData,
    ) {
        todo!()
    }

    fn opendir(&mut self, _req: &Request<'_>, ino: u64, flags: i32, reply: ReplyOpen) {
        debug!("OPENDIR ino {:#x} flags {}", ino, flags);
        reply.opened(1, 0);
    }

    fn readdir(
        &mut self,
        req: &Request<'_>,
        ino: u64,
        _fh: u64, // 如果 invoke 它的 opendir 里有指定 fh, 那么这里会收到 fh 参数
        offset: i64,
        mut reply: ReplyDirectory,
    ) {
        if let Some(inode) = self.get_inode(ino as usize) {
            if inode.is_dir() {
                if check_access(
                    req.uid(),
                    req.gid(),
                    inode.uid,
                    inode.gid,
                    inode.mode,
                    R_OK,
                ) {
                    let inode = inode.clone();
                    let mut entry_index = offset as u32;

                    while let Some(dir_entry) = self.get_direntry(&inode, entry_index) {
                        debug!("reading inode {:#x} index {}", ino, entry_index);
                        if entry_index >= inode.size {
                            break;
                        }
                        // 最开头应该添加 `.` 和 `..`, 但这件事应该在 mkdir 的时候就在 inode 里加好
                        // reply.add 返回 true 说明 buffer 已满, 停止累加
                        if reply.add(
                            dir_entry.inode as u64,
                            entry_index as i64,
                            to_filetype(dir_entry.file_type).expect("file type should not be 0x0!"),
                            dir_entry.name(),
                        ) {
                            break;
                        }
                        entry_index += 1;
                    }
                    reply.ok();
                } else {
                    reply.error(EACCES);
                }
            } else {
                reply.error(ENOTDIR);
            }
        } else {
            reply.error(ENOENT);
        }
    }

    fn access(&mut self, req: &Request<'_>, ino: u64, mask: i32, reply: ReplyEmpty) {
        // mask:
        // - 要么是 libc::F_OK (aka 0), 检查文件是否存在
        // - 要么是 libc::R_OK,libc::W_OK,libc::X_OK (aka 4/2/1) 构成的 bitmask, 检查是否有对应权限
        debug!("Filesystem::getattr(ino: {}, mask: {})", ino, mask);

        if let Some(inode) = self.get_inode(ino as usize) {
            if mask == libc::F_OK // 只要检查是否存在
                || check_access(req.uid(), req.gid(), inode.uid, inode.gid, inode.mode, mask)
            // 需要检查 rwx 权限
            {
                reply.ok();
            } else {
                reply.error(EACCES);
            }
        } else {
            reply.error(ENOENT);
        }
    }

    fn lseek(
        &mut self,
        _req: &Request<'_>,
        ino: u64,
        fh: u64,
        offset: i64,
        whence: i32,
        reply: ReplyLseek,
    ) {
        reply.error(ENOSYS);
    }
}