1 /*
2  * Copyright (c) 2007-2015 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <lib/bio.h>
11 #include <lib/bcache.h>
12 #include <lib/fs.h>
13 #include "ext2_fs.h"
14 
15 typedef uint32_t blocknum_t;
16 typedef uint32_t inodenum_t;
17 typedef uint32_t groupnum_t;
18 
19 typedef struct {
20     bdev_t *dev;
21     bcache_t cache;
22 
23     struct ext2_super_block sb;
24     int s_group_count;
25     struct ext2_group_desc *gd;
26     struct ext2_inode root_inode;
27 } ext2_t;
28 
29 struct cache_block {
30     blocknum_t num;
31     void *ptr;
32 };
33 
34 /* open file handle */
35 typedef struct {
36     ext2_t *ext2;
37 
38     struct cache_block ind_cache[3]; // cache of indirect blocks as they're scanned
39     struct ext2_inode inode;
40 } ext2_file_t;
41 
42 /* internal routines */
43 int ext2_load_inode(ext2_t *ext2, inodenum_t num, struct ext2_inode *inode);
44 int ext2_lookup(ext2_t *ext2, const char *path, inodenum_t *inum); // path to inode
45 
46 /* io */
47 int ext2_read_block(ext2_t *ext2, void *buf, blocknum_t bnum);
48 int ext2_get_block(ext2_t *ext2, void **ptr, blocknum_t bnum);
49 int ext2_put_block(ext2_t *ext2, blocknum_t bnum);
50 
51 off_t ext2_file_len(ext2_t *ext2, struct ext2_inode *inode);
52 ssize_t ext2_read_inode(ext2_t *ext2, struct ext2_inode *inode, void *buf, off_t offset, size_t len);
53 int ext2_read_link(ext2_t *ext2, struct ext2_inode *inode, char *str, size_t len);
54 
55 /* fs api */
56 status_t ext2_mount(bdev_t *dev, fscookie **cookie);
57 status_t ext2_unmount(fscookie *cookie);
58 status_t ext2_open_file(fscookie *cookie, const char *path, filecookie **fcookie);
59 ssize_t ext2_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len);
60 status_t ext2_close_file(filecookie *fcookie);
61 status_t ext2_stat_file(filecookie *fcookie, struct file_stat *);
62 
63 /* mode stuff */
64 #define S_IFMT      0170000
65 #define S_IFIFO     0010000
66 #define S_IFCHR     0020000
67 #define S_IFDIR     0040000
68 #define S_IFBLK     0060000
69 #define S_IFREG     0100000
70 #define S_IFLNK     0120000
71 #define S_IFSOCK    0140000
72 
73 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
74 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
75 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
76 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
77 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
78 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
79 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
80 
81