blob: ef37e54b1745e24576f5d8142da2e2a7b7581f41 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2007-2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#ifndef __EXT2_PRIV_H
25#define __EXT2_PRIV_H
26
27#include <lib/bio.h>
28#include <lib/bcache.h>
29#include <lib/fs.h>
30#include "ext2_fs.h"
31
32typedef uint32_t blocknum_t;
33typedef uint32_t inodenum_t;
34typedef uint32_t groupnum_t;
35
36typedef struct {
37 bdev_t *dev;
38 bcache_t cache;
39
40 struct ext2_super_block sb;
41 int s_group_count;
42 struct ext2_group_desc *gd;
43 struct ext2_inode root_inode;
44} ext2_t;
45
46struct cache_block {
47 blocknum_t num;
48 void *ptr;
49};
50
51/* open file handle */
52typedef struct {
53 ext2_t *ext2;
54
55 struct cache_block ind_cache[3]; // cache of indirect blocks as they're scanned
56 struct ext2_inode inode;
57} ext2_file_t;
58
59/* internal routines */
60int ext2_load_inode(ext2_t *ext2, inodenum_t num, struct ext2_inode *inode);
61int ext2_lookup(ext2_t *ext2, const char *path, inodenum_t *inum); // path to inode
62
63/* io */
64int ext2_read_block(ext2_t *ext2, void *buf, blocknum_t bnum);
65int ext2_get_block(ext2_t *ext2, void **ptr, blocknum_t bnum);
66int ext2_put_block(ext2_t *ext2, blocknum_t bnum);
67
68off_t ext2_file_len(ext2_t *ext2, struct ext2_inode *inode);
69ssize_t ext2_read_inode(ext2_t *ext2, struct ext2_inode *inode, void *buf, off_t offset, size_t len);
70int ext2_read_link(ext2_t *ext2, struct ext2_inode *inode, char *str, size_t len);
71
72/* fs api */
73status_t ext2_mount(bdev_t *dev, fscookie **cookie);
74status_t ext2_unmount(fscookie *cookie);
75status_t ext2_open_file(fscookie *cookie, const char *path, filecookie **fcookie);
76ssize_t ext2_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len);
77status_t ext2_close_file(filecookie *fcookie);
78status_t ext2_stat_file(filecookie *fcookie, struct file_stat *);
79
80/* mode stuff */
81#define S_IFMT 0170000
82#define S_IFIFO 0010000
83#define S_IFCHR 0020000
84#define S_IFDIR 0040000
85#define S_IFBLK 0060000
86#define S_IFREG 0100000
87#define S_IFLNK 0120000
88#define S_IFSOCK 0140000
89
90#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
91#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
92#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
93#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
94#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
95#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
96#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
97
98#endif
99