Subversion Repositories HelenOS

Rev

Rev 2587 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2587 Rev 2588
1
/*
1
/*
2
 * Copyright (c) 2007 Jakub Jermar
2
 * Copyright (c) 2007 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    vfs.c
34
 * @file    vfs.c
35
 * @brief   VFS_MOUNT method.
35
 * @brief   VFS_MOUNT method.
36
 */
36
 */
37
 
37
 
38
#include <ipc/ipc.h>
38
#include <ipc/ipc.h>
39
#include <async.h>
39
#include <async.h>
40
#include <errno.h>
40
#include <errno.h>
41
#include <stdlib.h>
41
#include <stdlib.h>
42
#include <string.h>
42
#include <string.h>
43
#include <bool.h>
43
#include <bool.h>
44
#include <futex.h>
44
#include <futex.h>
45
#include <libadt/list.h>
45
#include <libadt/list.h>
46
#include "vfs.h"
46
#include "vfs.h"
47
 
47
 
48
atomic_t rootfs_futex = FUTEX_INITIALIZER;
48
atomic_t rootfs_futex = FUTEX_INITIALIZER;
49
vfs_triplet_t rootfs = {
49
vfs_triplet_t rootfs = {
50
    .fs_handle = 0,
50
    .fs_handle = 0,
51
    .dev_handle = 0,
51
    .dev_handle = 0,
52
    .index = 0,
52
    .index = 0,
53
};
53
};
54
 
54
 
55
static int lookup_root(int fs_handle, int dev_handle, vfs_triplet_t *root)
55
static int lookup_root(int fs_handle, int dev_handle, vfs_triplet_t *root)
56
{
56
{
57
    vfs_pair_t altroot = {
57
    vfs_pair_t altroot = {
58
        .fs_handle = fs_handle,
58
        .fs_handle = fs_handle,
59
        .dev_handle = dev_handle,
59
        .dev_handle = dev_handle,
60
    };
60
    };
61
 
61
 
62
    return vfs_lookup_internal("/", strlen("/"), root, &altroot);
62
    return vfs_lookup_internal("/", strlen("/"), root, &altroot);
63
}
63
}
64
 
64
 
65
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
65
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
66
{
66
{
67
    int dev_handle;
67
    int dev_handle;
68
 
68
 
69
    /*
69
    /*
70
     * We expect the library to do the device-name to device-handle
70
     * We expect the library to do the device-name to device-handle
71
     * translation for us, thus the device handle will arrive as ARG1
71
     * translation for us, thus the device handle will arrive as ARG1
72
     * in the request.
72
     * in the request.
73
     */
73
     */
74
    dev_handle = IPC_GET_ARG1(*request);
74
    dev_handle = IPC_GET_ARG1(*request);
75
 
75
 
76
    /*
76
    /*
77
     * For now, don't make use of ARG2 and ARG3, but they can be used to
77
     * For now, don't make use of ARG2 and ARG3, but they can be used to
78
     * carry mount options in the future.
78
     * carry mount options in the future.
79
     */
79
     */
80
 
80
 
81
    /*
81
    /*
82
     * Now, we expect the client to send us data with the name of the file
82
     * Now, we expect the client to send us data with the name of the file
83
     * system and the path of the mountpoint.
83
     * system and the path of the mountpoint.
84
     */
84
     */
85
    ipc_callid_t callid;
85
    ipc_callid_t callid;
86
    ipc_call_t call;
86
    ipc_call_t call;
87
    size_t size;
87
    size_t size;
88
    if (!ipc_data_receive(&callid, &call, NULL, &size)) {
88
    if (!ipc_data_receive(&callid, &call, NULL, &size)) {
89
        ipc_answer_fast(callid, EINVAL, 0, 0);
89
        ipc_answer_fast_0(callid, EINVAL);
90
        ipc_answer_fast(rid, EINVAL, 0, 0);
90
        ipc_answer_fast_0(rid, EINVAL);
91
        return;
91
        return;
92
    }
92
    }
93
 
93
 
94
    /*
94
    /*
95
     * There is no sense in receiving data that can't hold a single
95
     * There is no sense in receiving data that can't hold a single
96
     * character of path. We won't accept data that exceed certain limits
96
     * character of path. We won't accept data that exceed certain limits
97
     * either.
97
     * either.
98
     */
98
     */
99
    if ((size < FS_NAME_MAXLEN + 1) ||
99
    if ((size < FS_NAME_MAXLEN + 1) ||
100
        (size > FS_NAME_MAXLEN + MAX_PATH_LEN)) {
100
        (size > FS_NAME_MAXLEN + MAX_PATH_LEN)) {
101
        ipc_answer_fast(callid, EINVAL, 0, 0);
101
        ipc_answer_fast_0(callid, EINVAL);
102
        ipc_answer_fast(rid, EINVAL, 0, 0);
102
        ipc_answer_fast_0(rid, EINVAL);
103
        return;
103
        return;
104
    }
104
    }
105
 
105
 
106
    /*
106
    /*
107
     * Allocate buffer for the data being received.
107
     * Allocate buffer for the data being received.
108
     */
108
     */
109
    uint8_t *buf;
109
    uint8_t *buf;
110
    buf = malloc(size);
110
    buf = malloc(size);
111
    if (!buf) {
111
    if (!buf) {
112
        ipc_answer_fast(callid, ENOMEM, 0, 0);
112
        ipc_answer_fast_0(callid, ENOMEM);
113
        ipc_answer_fast(rid, ENOMEM, 0, 0);
113
        ipc_answer_fast_0(rid, ENOMEM);
114
        return;
114
        return;
115
    }
115
    }
116
 
116
 
117
    /*
117
    /*
118
     * Deliver the data.
118
     * Deliver the data.
119
     */
119
     */
120
    (void) ipc_data_deliver(callid, &call, buf, size);
120
    (void) ipc_data_deliver(callid, &call, buf, size);
121
 
121
 
122
    char fs_name[FS_NAME_MAXLEN + 1];
122
    char fs_name[FS_NAME_MAXLEN + 1];
123
    memcpy(fs_name, buf, FS_NAME_MAXLEN);
123
    memcpy(fs_name, buf, FS_NAME_MAXLEN);
124
    fs_name[FS_NAME_MAXLEN] = '\0';
124
    fs_name[FS_NAME_MAXLEN] = '\0';
125
 
125
 
126
    /*
126
    /*
127
     * Check if we know a file system with the same name as is in fs_name.
127
     * Check if we know a file system with the same name as is in fs_name.
128
     * This will also give us its file system handle.
128
     * This will also give us its file system handle.
129
     */
129
     */
130
    int fs_handle = fs_name_to_handle(fs_name, true);
130
    int fs_handle = fs_name_to_handle(fs_name, true);
131
    if (!fs_handle) {
131
    if (!fs_handle) {
132
        free(buf);
132
        free(buf);
133
        ipc_answer_fast(rid, ENOENT, 0, 0);
133
        ipc_answer_fast_0(rid, ENOENT);
134
        return;
134
        return;
135
    }
135
    }
136
 
136
 
137
    /*
137
    /*
138
     * Lookup the root node of the filesystem being mounted.
138
     * Lookup the root node of the filesystem being mounted.
139
     */
139
     */
140
    int rc;
140
    int rc;
141
    vfs_triplet_t mounted_root;
141
    vfs_triplet_t mounted_root;
142
    rc = lookup_root(fs_handle, dev_handle, &mounted_root);
142
    rc = lookup_root(fs_handle, dev_handle, &mounted_root);
143
    if (rc != EOK) {
143
    if (rc != EOK) {
144
        free(buf);
144
        free(buf);
145
        ipc_answer_fast(rid, rc, 0, 0);
145
        ipc_answer_fast_0(rid, rc);
146
        return;
146
        return;
147
    }
147
    }
148
 
148
 
149
    /*
149
    /*
150
     * Finally, we need to resolve the path to the mountpoint.
150
     * Finally, we need to resolve the path to the mountpoint.
151
     */
151
     */
152
    vfs_triplet_t mp;
152
    vfs_triplet_t mp;
153
    futex_down(&rootfs_futex);
153
    futex_down(&rootfs_futex);
154
    if (rootfs.fs_handle) {
154
    if (rootfs.fs_handle) {
155
        /*
155
        /*
156
         * We already have the root FS.
156
         * We already have the root FS.
157
         */
157
         */
158
        rc = vfs_lookup_internal((char *) (buf + FS_NAME_MAXLEN),
158
        rc = vfs_lookup_internal((char *) (buf + FS_NAME_MAXLEN),
159
            size - FS_NAME_MAXLEN, &mp, NULL);
159
            size - FS_NAME_MAXLEN, &mp, NULL);
160
        if (rc != EOK) {
160
        if (rc != EOK) {
161
            /*
161
            /*
162
             * The lookup failed for some reason.
162
             * The lookup failed for some reason.
163
             */
163
             */
164
            futex_up(&rootfs_futex);
164
            futex_up(&rootfs_futex);
165
            free(buf);
165
            free(buf);
166
            ipc_answer_fast(rid, rc, 0, 0);
166
            ipc_answer_fast_0(rid, rc);
167
            return;
167
            return;
168
        }
168
        }
169
    } else {
169
    } else {
170
        /*
170
        /*
171
         * We still don't have the root file system mounted.
171
         * We still don't have the root file system mounted.
172
         */
172
         */
173
        if ((size - FS_NAME_MAXLEN == strlen("/")) &&
173
        if ((size - FS_NAME_MAXLEN == strlen("/")) &&
174
            (buf[FS_NAME_MAXLEN] == '/')) {
174
            (buf[FS_NAME_MAXLEN] == '/')) {
175
            /*
175
            /*
176
             * For this simple, but important case, we are done.
176
             * For this simple, but important case, we are done.
177
             */
177
             */
178
            rootfs = mounted_root;
178
            rootfs = mounted_root;
179
            futex_up(&rootfs_futex);
179
            futex_up(&rootfs_futex);
180
            free(buf);
180
            free(buf);
181
            ipc_answer_fast(rid, EOK, 0, 0);
181
            ipc_answer_fast_0(rid, EOK);
182
            return;
182
            return;
183
        } else {
183
        } else {
184
            /*
184
            /*
185
             * We can't resolve this without the root filesystem
185
             * We can't resolve this without the root filesystem
186
             * being mounted first.
186
             * being mounted first.
187
             */
187
             */
188
            futex_up(&rootfs_futex);
188
            futex_up(&rootfs_futex);
189
            free(buf);
189
            free(buf);
190
            ipc_answer_fast(rid, ENOENT, 0, 0);
190
            ipc_answer_fast_0(rid, ENOENT);
191
            return;
191
            return;
192
        }
192
        }
193
    }
193
    }
194
    futex_up(&rootfs_futex);
194
    futex_up(&rootfs_futex);
195
   
195
   
196
    free(buf);  /* The buffer is not needed anymore. */
196
    free(buf);  /* The buffer is not needed anymore. */
197
   
197
   
198
    /*
198
    /*
199
     * At this point, we have all necessary pieces: file system and device
199
     * At this point, we have all necessary pieces: file system and device
200
     * handles, and we know the mount point VFS node and also the root node
200
     * handles, and we know the mount point VFS node and also the root node
201
     * of the file system being mounted.
201
     * of the file system being mounted.
202
     */
202
     */
203
 
203
 
204
    int phone = vfs_grab_phone(mp.fs_handle);
204
    int phone = vfs_grab_phone(mp.fs_handle);
205
    /* Later we can use ARG3 to pass mode/flags. */
205
    /* Later we can use ARG3 to pass mode/flags. */
206
    aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle,
206
    aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle,
207
        (ipcarg_t) mp.index, 0, NULL);
207
        (ipcarg_t) mp.index, 0, NULL);
208
    /* The second call uses the same method. */
208
    /* The second call uses the same method. */
209
    aid_t req2 = async_send_3(phone, VFS_MOUNT,
209
    aid_t req2 = async_send_3(phone, VFS_MOUNT,
210
        (ipcarg_t) mounted_root.fs_handle,
210
        (ipcarg_t) mounted_root.fs_handle,
211
        (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index,
211
        (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index,
212
        NULL);
212
        NULL);
213
 
213
 
214
    ipcarg_t rc1;
214
    ipcarg_t rc1;
215
    ipcarg_t rc2;
215
    ipcarg_t rc2;
216
    async_wait_for(req1, &rc1);
216
    async_wait_for(req1, &rc1);
217
    async_wait_for(req2, &rc2);
217
    async_wait_for(req2, &rc2);
218
    vfs_release_phone(phone);
218
    vfs_release_phone(phone);
219
 
219
 
220
    if (rc2 == EOK)
220
    if (rc2 == EOK)
221
        ipc_answer_fast(rid, rc1, 0, 0);
221
        ipc_answer_fast_0(rid, rc1);
222
    else if (rc1 == EOK)
222
    else if (rc1 == EOK)
223
        ipc_answer_fast(rid, rc2, 0, 0);
223
        ipc_answer_fast_0(rid, rc2);
224
    else
224
    else
225
        ipc_answer_fast(rid, rc1, 0, 0);
225
        ipc_answer_fast_0(rid, rc1);
226
}
226
}
227
 
227
 
228
/**
228
/**
229
 * @}
229
 * @}
230
 */
230
 */
231
 
231