aboutsummaryrefslogtreecommitdiffstats
path: root/core/multifs.c
blob: d979db6ff0bf569e4bd637c43516ca8b31cefd95 (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
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
/*
 * Copyright (C) 2013 Raphael S. Carvalho <raphael.scarv@gmail.com>
 * Copyright (C) 2015 Paulo Alcantara <pcacjr@zytor.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <stdio.h>
#include <assert.h>
#include <fs.h>
#include <ilog2.h>
#include <disk.h>
#include <cache.h>
#include <minmax.h>

#include "multifs.h"

#define DISKS_MAX 0x7f

struct part_node {
    int partition;
    struct fs_info *fs;
    struct part_node *next;
};

struct queue_head {
    struct part_node *first;
    struct part_node *last;
};

static struct queue_head *parts_info[DISKS_MAX];

static int add_fs(struct fs_info *fs, uint8_t diskno, uint8_t partno)
{
    struct queue_head *head = parts_info[diskno];
    struct part_node *node;

    node = malloc(sizeof(struct part_node));
    if (!node)
        return -1;
    node->fs = fs;
    node->next = NULL;
    node->partition = partno;

    if (!head) {
        head = malloc(sizeof(struct queue_head));
        if (!head) {
            free(node);
            return -1;
        }
        head->first = head->last = node;
        parts_info[diskno] = head;
        return 0;
    }
    head->last->next = node;
    head->last = node;
    return 0;
}

struct fs_info *multifs_get_fs(uint8_t diskno, uint8_t partno)
{
    struct part_node *i;

    for (i = parts_info[diskno]->first; i; i = i->next) {
        if (i->partition == partno)
            return i->fs;
    }
    return NULL;
}

static const char *get_num(const char *p, char delimiter, uint8_t *data)
{
    uint32_t n = 0;

    while (*p) {
        if (*p < '0' || *p > '9')
            break;
        n = (n * 10) + (*p - '0');
        p++;
        if (*p == delimiter) {
            p++; /* skip delimiter */
            *data = min(n, UINT8_MAX); /* avoid overflow */
            return p;
        }
    }
    return NULL;
}

int multifs_parse_path(const char **path, uint8_t *diskno, uint8_t *partno)
{
    const char *p = *path;
    static const char *cwd = ".";

    *diskno = *partno = 0;
    p++; /* Skip open parentheses */

    /* Get hd number (Range: 0 - (DISKS_MAX - 1)) */
    if (*p != 'h' || *(p + 1) != 'd')
        return -1;

    p += 2; /* Skip 'h' and 'd' */
    p = get_num(p, ',', diskno);
    if (!p)
        return -1;
    if (*diskno >= DISKS_MAX) {
        printf("MultiFS: disk number is out of range: 0-%d\n", DISKS_MAX - 1);
        return -1;
    }

    /* Get partition number (Range: 0 - 0xFF) */
    p = get_num(p, ')', partno);
    if (!p)
        return -1;

    if (*p == '\0') {
        /* Assume it's a cwd request */
        p = cwd;
    }

    *path = p;
    dprintf("MultiFS: disk: %u partition: %u path: %s\n", *diskno, *partno,
            *path);
    return 0;
}

int multifs_setup_fs_info(struct fs_info *fsp, uint8_t diskno, uint8_t partno,
                          void *priv)
{
    const struct fs_ops **ops;
    int blk_shift = -1;
    struct device *dev = NULL;

    /* set default name for the root directory */
    fsp->cwd_name[0] = '/';
    fsp->cwd_name[1] = '\0';

    ops = p_ops;
    while ((blk_shift < 0) && *ops) {
        /* set up the fs stucture */
        fsp->fs_ops = *ops;

        /*
         * This boldly assumes that we don't mix FS_NODEV filesystems
         * with FS_DEV filesystems...
         */
        if (fsp->fs_ops->fs_flags & FS_NODEV) {
            fsp->fs_dev = NULL;
        } else {
            if (!dev) {
                dev = device_init(priv);
                if (!dev)
                    return -1;
            }
            fsp->fs_dev = dev;
        }
        /* invoke the fs-specific init code */
        blk_shift = fsp->fs_ops->fs_init(fsp);
        ops++;
    }
    if (blk_shift < 0) {
        dprintf("%s: no valid file system found!\n", __func__);
        goto out_free;
    }

    if (add_fs(fsp, diskno, partno - 1))
        goto out_free;
    if (fsp->fs_dev && fsp->fs_dev->cache_data)
        cache_init(fsp->fs_dev, blk_shift);
    if (fsp->fs_ops->iget_root) {
        fsp->root = fsp->fs_ops->iget_root(fsp);
        fsp->cwd = get_inode(fsp->root);
    }
    return 0;

out_free:
    free(dev->disk);
    free(dev->cache_data);
    free(dev);
    return -1;
}

void multifs_restore_fs(void)
{
    this_fs = root_fs;
}

int multifs_switch_fs(const char **path)
{
    struct fs_info *fs;

    assert(path && *path);
    if ((*path)[0] != '(') {
        /* If so, don't need to restore chdir */
        if (this_fs == root_fs)
            return 0;

        fs = root_fs;
        goto ret;
    }

    fs = multifs_ops->get_fs_info(path);
    if (!fs)
        return -1;

ret:
    this_fs = fs;
    return 0;
}