summaryrefslogtreecommitdiff
path: root/src/fs/fs.cc
blob: b2b91c278470698b0b431bac32df96806d9cc31d (plain)
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
#include "fs/fs.h"
#include "kernel/riscv.h"
#include "drivers/uart.h"
#include "kernel/spinlock.h"
#include "kernel/proc.h"
#include "fs/fat32.h"
#include "lib/string.h"

static Inode *global_root_inode = nullptr;

static const char *skipelem(const char *path, char *name)
{
    const char *s;
    int len;

    while (*path == '/')
        path++;
    if (*path == 0)
        return nullptr;

    s = path;
    while (*path != '/' && *path != 0)
        path++;

    len = path - s;
    if (len >= 128)
        len = 127;

    memmove(name, s, len);
    name[len] = 0;

    while (*path == '/')
        path++;
    return path;
}

void Inode::getattr(struct kstat *st)
{
    st->dev = dev;
    st->ino = inum;
    st->type = type;
    st->nlink = nlink;
    st->size = size;

    st->uid = 0;
    st->gid = 0;
    st->mode = 0;
}

namespace VFS
{
    void init()
    {
        Drivers::uart_puts("[VFS] Inode subsystem initialized.\n");
    }

    void iput(Inode *ip)
    {
        if (ip == nullptr)
            return;

        ip->lock.acquire();
        if(ip->ref_cnt < 1)
        {
            Drivers::uart_puts("panic: iput ref\n");
            while (1)
                ;
        }
        ip->ref_cnt--;
        int ref = ip->ref_cnt;
        ip->lock.release();

        if (ref == 0)
        {
            delete ip;
        }
    }

    Inode* idup(Inode *ip)
    {
        if (ip == nullptr)
            return nullptr;

        ip->lock.acquire();
        ip->ref_cnt++;
        ip->lock.release();
        return ip;
    }

    void ilock(Inode *ip)
    {
        if (ip == 0 || ip->ref_cnt < 1)
        {
            Drivers::uart_puts("panic: ilock\n");
            while (1)
                ;
        }

        ip->lock.acquire();

    }

    void iunlock(Inode *ip)
    {
        if (ip == 0 || !ip->lock.holding() || ip->ref_cnt < 1)
        {
            Drivers::uart_puts("panic: iunlock\n");
            while (1)
                ;
        }
        ip->lock.release();
    }

    static void iunlockput(Inode *ip)
    {
        iunlock(ip);
        iput(ip);
    }

    void mount_root(Inode *root)
    {
        if (global_root_inode)
        {
            Drivers::uart_puts("VFS: Root already mounted!\n");
            return;
        }
        global_root_inode = root;
        Drivers::uart_puts("[VFS] Root filesystem mounted.\n");
    }

    Inode *get_root()
    {
        if (global_root_inode == nullptr)
        {
            Drivers::uart_puts("[VFS] Lazy init: Mounting FAT32...\n");
            fat32_fs.init();
            mount_root(fat32_fs.root());
        }
        if (!global_root_inode)
            return nullptr;
        return idup(global_root_inode);
    }

    Inode *namei(const char *path)
    {
        char name[128];
        Inode *ip, *next;
        struct Proc *p = myproc();

        if (*path == '/')
        {
            ip = get_root();
        }
        else
        {
            if (p->cwd == nullptr)
            {
                ip = get_root();
            }
            else
            {
                ip = idup(p->cwd);
            }
        }

        if (ip == nullptr)
            return nullptr;

        while ((path = skipelem(path, name)) != nullptr)
        {
            ilock(ip);

            if (ip->type != T_DIR)
            {
                iunlockput(ip);
                return nullptr;
            }

            next = ip->lookup(name);
            iunlockput(ip);

            ip = next;
            if (ip == nullptr)
                return nullptr;
        }

        return ip;
    }

} // namespace VFS