Subversion Repositories HelenOS

Rev

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

Rev 4301 Rev 4302
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 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_ops.c
34
 * @file vfs_ops.c
35
 * @brief Operations that VFS offers to its clients.
35
 * @brief Operations that VFS offers to its clients.
36
 */
36
 */
37
 
37
 
38
#include "vfs.h"
38
#include "vfs.h"
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <async.h>
40
#include <async.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <stdlib.h>
43
#include <stdlib.h>
44
#include <string.h>
44
#include <string.h>
45
#include <bool.h>
45
#include <bool.h>
46
#include <futex.h>
46
#include <futex.h>
47
#include <rwlock.h>
47
#include <rwlock.h>
48
#include <libadt/list.h>
48
#include <libadt/list.h>
49
#include <unistd.h>
49
#include <unistd.h>
50
#include <ctype.h>
50
#include <ctype.h>
51
#include <fcntl.h>
51
#include <fcntl.h>
52
#include <assert.h>
52
#include <assert.h>
53
#include <vfs/canonify.h>
53
#include <vfs/canonify.h>
54
 
54
 
55
/* Forward declarations of static functions. */
55
/* Forward declarations of static functions. */
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
57
 
57
 
58
/** Pending mount structure. */
58
/** Pending mount structure. */
59
typedef struct {
59
typedef struct {
60
    link_t link;
60
    link_t link;
61
    char *fs_name;            /**< File system name */
61
    char *fs_name;            /**< File system name */
62
    char *mp;                 /**< Mount point */
62
    char *mp;                 /**< Mount point */
63
    ipc_callid_t callid;      /**< Call ID waiting for the mount */
63
    ipc_callid_t callid;      /**< Call ID waiting for the mount */
64
    ipc_callid_t rid;         /**< Request ID */
64
    ipc_callid_t rid;         /**< Request ID */
65
    dev_handle_t dev_handle;  /**< Device handle */
65
    dev_handle_t dev_handle;  /**< Device handle */
66
} pending_req_t;
66
} pending_req_t;
67
 
67
 
68
LIST_INITIALIZE(pending_req);
68
LIST_INITIALIZE(pending_req);
69
 
69
 
70
/**
70
/**
71
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
71
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
72
 * concurrent VFS operation which modifies the file system namespace.
72
 * concurrent VFS operation which modifies the file system namespace.
73
 */
73
 */
74
RWLOCK_INITIALIZE(namespace_rwlock);
74
RWLOCK_INITIALIZE(namespace_rwlock);
75
 
75
 
76
futex_t rootfs_futex = FUTEX_INITIALIZER;
76
futex_t rootfs_futex = FUTEX_INITIALIZER;
77
vfs_pair_t rootfs = {
77
vfs_pair_t rootfs = {
78
    .fs_handle = 0,
78
    .fs_handle = 0,
79
    .dev_handle = 0
79
    .dev_handle = 0
80
};
80
};
81
 
81
 
82
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
82
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
83
    fs_handle_t fs_handle, char *mp)
83
    fs_handle_t fs_handle, char *mp)
84
{
84
{
85
    /* Resolve the path to the mountpoint. */
85
    /* Resolve the path to the mountpoint. */
86
    vfs_lookup_res_t mp_res;
86
    vfs_lookup_res_t mp_res;
87
    vfs_node_t *mp_node = NULL;
87
    vfs_node_t *mp_node = NULL;
88
    int rc;
88
    int rc;
89
    int phone;
89
    int phone;
90
    futex_down(&rootfs_futex);
90
    futex_down(&rootfs_futex);
91
    if (rootfs.fs_handle) {
91
    if (rootfs.fs_handle) {
92
        /* We already have the root FS. */
92
        /* We already have the root FS. */
93
        rwlock_write_lock(&namespace_rwlock);
93
        rwlock_write_lock(&namespace_rwlock);
94
        if (str_cmp(mp, "/") == 0) {
94
        if (str_cmp(mp, "/") == 0) {
95
            /* Trying to mount root FS over root FS */
95
            /* Trying to mount root FS over root FS */
96
            rwlock_write_unlock(&namespace_rwlock);
96
            rwlock_write_unlock(&namespace_rwlock);
97
            futex_up(&rootfs_futex);
97
            futex_up(&rootfs_futex);
98
            ipc_answer_0(rid, EBUSY);
98
            ipc_answer_0(rid, EBUSY);
99
            return;
99
            return;
100
        }
100
        }
101
       
101
       
102
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
102
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
103
        if (rc != EOK) {
103
        if (rc != EOK) {
104
            /* The lookup failed for some reason. */
104
            /* The lookup failed for some reason. */
105
            rwlock_write_unlock(&namespace_rwlock);
105
            rwlock_write_unlock(&namespace_rwlock);
106
            futex_up(&rootfs_futex);
106
            futex_up(&rootfs_futex);
107
            ipc_answer_0(rid, rc);
107
            ipc_answer_0(rid, rc);
108
            return;
108
            return;
109
        }
109
        }
110
       
110
       
111
        mp_node = vfs_node_get(&mp_res);
111
        mp_node = vfs_node_get(&mp_res);
112
        if (!mp_node) {
112
        if (!mp_node) {
113
            rwlock_write_unlock(&namespace_rwlock);
113
            rwlock_write_unlock(&namespace_rwlock);
114
            futex_up(&rootfs_futex);
114
            futex_up(&rootfs_futex);
115
            ipc_answer_0(rid, ENOMEM);
115
            ipc_answer_0(rid, ENOMEM);
116
            return;
116
            return;
117
        }
117
        }
118
       
118
       
119
        /*
119
        /*
120
         * Now we hold a reference to mp_node.
120
         * Now we hold a reference to mp_node.
121
         * It will be dropped upon the corresponding VFS_UNMOUNT.
121
         * It will be dropped upon the corresponding VFS_UNMOUNT.
122
         * This prevents the mount point from being deleted.
122
         * This prevents the mount point from being deleted.
123
         */
123
         */
124
        rwlock_write_unlock(&namespace_rwlock);
124
        rwlock_write_unlock(&namespace_rwlock);
125
    } else {
125
    } else {
126
        /* We still don't have the root file system mounted. */
126
        /* We still don't have the root file system mounted. */
127
        if (str_cmp(mp, "/") == 0) {
127
        if (str_cmp(mp, "/") == 0) {
128
            vfs_lookup_res_t mr_res;
128
            vfs_lookup_res_t mr_res;
129
            vfs_node_t *mr_node;
129
            vfs_node_t *mr_node;
130
            ipcarg_t rindex;
130
            ipcarg_t rindex;
131
            ipcarg_t rsize;
131
            ipcarg_t rsize;
132
            ipcarg_t rlnkcnt;
132
            ipcarg_t rlnkcnt;
133
           
133
           
134
            /*
134
            /*
135
             * For this simple, but important case,
135
             * For this simple, but important case,
136
             * we are almost done.
136
             * we are almost done.
137
             */
137
             */
138
           
138
           
139
            /* Tell the mountee that it is being mounted. */
139
            /* Tell the mountee that it is being mounted. */
140
            phone = vfs_grab_phone(fs_handle);
140
            phone = vfs_grab_phone(fs_handle);
141
            rc = async_req_1_3(phone, VFS_MOUNTED,
141
            rc = async_req_1_3(phone, VFS_MOUNTED,
142
                (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt);
142
                (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt);
143
            vfs_release_phone(phone);
143
            vfs_release_phone(phone);
144
           
144
           
145
            if (rc != EOK) {
145
            if (rc != EOK) {
146
                futex_up(&rootfs_futex);
146
                futex_up(&rootfs_futex);
147
                ipc_answer_0(rid, rc);
147
                ipc_answer_0(rid, rc);
148
                return;
148
                return;
149
            }
149
            }
150
           
150
           
151
            mr_res.triplet.fs_handle = fs_handle;
151
            mr_res.triplet.fs_handle = fs_handle;
152
            mr_res.triplet.dev_handle = dev_handle;
152
            mr_res.triplet.dev_handle = dev_handle;
153
            mr_res.triplet.index = (fs_index_t) rindex;
153
            mr_res.triplet.index = (fs_index_t) rindex;
154
            mr_res.size = (size_t) rsize;
154
            mr_res.size = (size_t) rsize;
155
            mr_res.lnkcnt = (unsigned) rlnkcnt;
155
            mr_res.lnkcnt = (unsigned) rlnkcnt;
156
            mr_res.type = VFS_NODE_DIRECTORY;
156
            mr_res.type = VFS_NODE_DIRECTORY;
157
           
157
           
158
            rootfs.fs_handle = fs_handle;
158
            rootfs.fs_handle = fs_handle;
159
            rootfs.dev_handle = dev_handle;
159
            rootfs.dev_handle = dev_handle;
160
            futex_up(&rootfs_futex);
160
            futex_up(&rootfs_futex);
161
           
161
           
162
            /* Add reference to the mounted root. */
162
            /* Add reference to the mounted root. */
163
            mr_node = vfs_node_get(&mr_res);
163
            mr_node = vfs_node_get(&mr_res);
164
            assert(mr_node);
164
            assert(mr_node);
165
           
165
           
166
            ipc_answer_0(rid, rc);
166
            ipc_answer_0(rid, rc);
167
            return;
167
            return;
168
        } else {
168
        } else {
169
            /*
169
            /*
170
             * We can't resolve this without the root filesystem
170
             * We can't resolve this without the root filesystem
171
             * being mounted first.
171
             * being mounted first.
172
             */
172
             */
173
            futex_up(&rootfs_futex);
173
            futex_up(&rootfs_futex);
174
            ipc_answer_0(rid, ENOENT);
174
            ipc_answer_0(rid, ENOENT);
175
            return;
175
            return;
176
        }
176
        }
177
    }
177
    }
178
    futex_up(&rootfs_futex);
178
    futex_up(&rootfs_futex);
179
   
179
   
180
    /*
180
    /*
181
     * At this point, we have all necessary pieces: file system and device
181
     * At this point, we have all necessary pieces: file system and device
182
     * handles, and we know the mount point VFS node.
182
     * handles, and we know the mount point VFS node.
183
     */
183
     */
184
   
184
   
185
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
185
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
186
    rc = async_req_4_0(phone, VFS_MOUNT,
186
    rc = async_req_4_0(phone, VFS_MOUNT,
187
        (ipcarg_t) mp_res.triplet.dev_handle,
187
        (ipcarg_t) mp_res.triplet.dev_handle,
188
        (ipcarg_t) mp_res.triplet.index,
188
        (ipcarg_t) mp_res.triplet.index,
189
        (ipcarg_t) fs_handle,
189
        (ipcarg_t) fs_handle,
190
        (ipcarg_t) dev_handle);
190
        (ipcarg_t) dev_handle);
191
    vfs_release_phone(phone);
191
    vfs_release_phone(phone);
192
   
192
   
193
    if (rc != EOK) {
193
    if (rc != EOK) {
194
        /* Mount failed, drop reference to mp_node. */
194
        /* Mount failed, drop reference to mp_node. */
195
        if (mp_node)
195
        if (mp_node)
196
            vfs_node_put(mp_node);
196
            vfs_node_put(mp_node);
197
    }
197
    }
198
   
198
   
199
    ipc_answer_0(rid, rc);
199
    ipc_answer_0(rid, rc);
200
}
200
}
201
 
201
 
202
/** Process pending mount requests */
202
/** Process pending mount requests */
203
void vfs_process_pending_mount()
203
void vfs_process_pending_mount()
204
{
204
{
205
    link_t *cur;
205
    link_t *cur;
206
   
206
   
207
loop:
207
loop:
208
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
208
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
209
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
209
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
210
       
210
       
211
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
211
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
212
        if (!fs_handle)
212
        if (!fs_handle)
213
            continue;
213
            continue;
214
       
214
       
215
        /* Acknowledge that we know fs_name. */
215
        /* Acknowledge that we know fs_name. */
216
        ipc_answer_0(pr->callid, EOK);
216
        ipc_answer_0(pr->callid, EOK);
217
       
217
       
218
        /* Do the mount */
218
        /* Do the mount */
219
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp);
219
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp);
220
       
220
       
221
        free(pr->fs_name);
221
        free(pr->fs_name);
222
        free(pr->mp);
222
        free(pr->mp);
223
        list_remove(cur);
223
        list_remove(cur);
224
        free(pr);
224
        free(pr);
225
        goto loop;
225
        goto loop;
226
    }
226
    }
227
}
227
}
228
 
228
 
229
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
229
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
230
{
230
{
231
    /*
231
    /*
232
     * We expect the library to do the device-name to device-handle
232
     * We expect the library to do the device-name to device-handle
233
     * translation for us, thus the device handle will arrive as ARG1
233
     * translation for us, thus the device handle will arrive as ARG1
234
     * in the request.
234
     * in the request.
235
     */
235
     */
236
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
236
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
237
   
237
   
238
    /*
238
    /*
239
     * Mount flags are passed as ARG2.
239
     * Mount flags are passed as ARG2.
240
     */
240
     */
241
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
241
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
242
   
242
   
243
    /*
243
    /*
244
     * For now, don't make use of ARG3, but it can be used to
244
     * For now, don't make use of ARG3, but it can be used to
245
     * carry mount options in the future.
245
     * carry mount options in the future.
246
     */
246
     */
247
   
247
   
248
    /* We want the client to send us the mount point. */
248
    /* We want the client to send us the mount point. */
249
    ipc_callid_t callid;
249
    ipc_callid_t callid;
250
    size_t size;
250
    size_t size;
251
    if (!ipc_data_write_receive(&callid, &size)) {
251
    if (!ipc_data_write_receive(&callid, &size)) {
252
        ipc_answer_0(callid, EINVAL);
252
        ipc_answer_0(callid, EINVAL);
253
        ipc_answer_0(rid, EINVAL);
253
        ipc_answer_0(rid, EINVAL);
254
        return;
254
        return;
255
    }
255
    }
256
   
256
   
257
    /* Check whether size is reasonable wrt. the mount point. */
257
    /* Check whether size is reasonable wrt. the mount point. */
258
    if ((size < 1) || (size > MAX_PATH_LEN)) {
258
    if ((size < 1) || (size > MAX_PATH_LEN)) {
259
        ipc_answer_0(callid, EINVAL);
259
        ipc_answer_0(callid, EINVAL);
260
        ipc_answer_0(rid, EINVAL);
260
        ipc_answer_0(rid, EINVAL);
261
        return;
261
        return;
262
    }
262
    }
263
   
263
   
264
    /* Allocate buffer for the mount point data being received. */
264
    /* Allocate buffer for the mount point data being received. */
265
    char *mp = malloc(size + 1);
265
    char *mp = malloc(size + 1);
266
    if (!mp) {
266
    if (!mp) {
267
        ipc_answer_0(callid, ENOMEM);
267
        ipc_answer_0(callid, ENOMEM);
268
        ipc_answer_0(rid, ENOMEM);
268
        ipc_answer_0(rid, ENOMEM);
269
        return;
269
        return;
270
    }
270
    }
271
   
271
   
272
    /* Deliver the mount point. */
272
    /* Deliver the mount point. */
273
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
273
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
274
    if (retval != EOK) {
274
    if (retval != EOK) {
275
        ipc_answer_0(rid, retval);
275
        ipc_answer_0(rid, retval);
276
        free(mp);
276
        free(mp);
277
        return;
277
        return;
278
    }
278
    }
279
    mp[size] = '\0';
279
    mp[size] = '\0';
280
   
280
   
281
    /*
281
    /*
282
     * Now, we expect the client to send us data with the name of the file
282
     * Now, we expect the client to send us data with the name of the file
283
     * system.
283
     * system.
284
     */
284
     */
285
    if (!ipc_data_write_receive(&callid, &size)) {
285
    if (!ipc_data_write_receive(&callid, &size)) {
286
        ipc_answer_0(callid, EINVAL);
286
        ipc_answer_0(callid, EINVAL);
287
        ipc_answer_0(rid, EINVAL);
287
        ipc_answer_0(rid, EINVAL);
288
        free(mp);
288
        free(mp);
289
        return;
289
        return;
290
    }
290
    }
291
   
291
   
292
    /*
292
    /*
293
     * Don't receive more than is necessary for storing a full file system
293
     * Don't receive more than is necessary for storing a full file system
294
     * name.
294
     * name.
295
     */
295
     */
296
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
296
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
297
        ipc_answer_0(callid, EINVAL);
297
        ipc_answer_0(callid, EINVAL);
298
        ipc_answer_0(rid, EINVAL);
298
        ipc_answer_0(rid, EINVAL);
299
        free(mp);
299
        free(mp);
300
        return;
300
        return;
301
    }
301
    }
302
   
302
   
303
    /*
303
    /*
304
     * Allocate buffer for file system name.
304
     * Allocate buffer for file system name.
305
     */
305
     */
306
    char *fs_name = (char *) malloc(size + 1);
306
    char *fs_name = (char *) malloc(size + 1);
307
    if (fs_name == NULL) {
307
    if (fs_name == NULL) {
308
        ipc_answer_0(callid, ENOMEM);
308
        ipc_answer_0(callid, ENOMEM);
309
        ipc_answer_0(rid, ENOMEM);
309
        ipc_answer_0(rid, ENOMEM);
310
        free(mp);
310
        free(mp);
311
        return;
311
        return;
312
    }
312
    }
313
   
313
   
314
    /* Deliver the file system name. */
314
    /* Deliver the file system name. */
315
    retval = ipc_data_write_finalize(callid, fs_name, size);
315
    retval = ipc_data_write_finalize(callid, fs_name, size);
316
    if (retval != EOK) {
316
    if (retval != EOK) {
317
        ipc_answer_0(rid, retval);
317
        ipc_answer_0(rid, retval);
318
        free(mp);
318
        free(mp);
319
        free(fs_name);
319
        free(fs_name);
320
        return;
320
        return;
321
    }
321
    }
322
    fs_name[size] = '\0';
322
    fs_name[size] = '\0';
-
 
323
 
-
 
324
    /*
-
 
325
     * Wait for IPC_M_PING so that we can return an error if we don't know
-
 
326
     * fs_name.
-
 
327
     */
-
 
328
    ipc_call_t data;
-
 
329
    callid = async_get_call(&data);
-
 
330
    if (IPC_GET_METHOD(data) != IPC_M_PING) {
-
 
331
        ipc_answer_0(callid, ENOTSUP);
-
 
332
        ipc_answer_0(rid, ENOTSUP);
-
 
333
        free(mp);
-
 
334
        free(fs_name);
-
 
335
        return;
323
   
336
    }
-
 
337
 
324
    /*
338
    /*
325
     * Check if we know a file system with the same name as is in fs_name.
339
     * Check if we know a file system with the same name as is in fs_name.
326
     * This will also give us its file system handle.
340
     * This will also give us its file system handle.
327
     */
341
     */
328
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
342
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
329
    if (!fs_handle) {
343
    if (!fs_handle) {
330
        if (flags & IPC_FLAG_BLOCKING) {
344
        if (flags & IPC_FLAG_BLOCKING) {
-
 
345
            pending_req_t *pr;
-
 
346
 
331
            /* Blocking mount, add to pending list */
347
            /* Blocking mount, add to pending list */
332
            pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
348
            pr = (pending_req_t *) malloc(sizeof(pending_req_t));
333
            if (!pr) {
349
            if (!pr) {
334
                ipc_answer_0(callid, ENOMEM);
350
                ipc_answer_0(callid, ENOMEM);
335
                ipc_answer_0(rid, ENOMEM);
351
                ipc_answer_0(rid, ENOMEM);
336
                free(mp);
352
                free(mp);
337
                free(fs_name);
353
                free(fs_name);
338
                return;
354
                return;
339
            }
355
            }
340
           
356
           
341
            pr->fs_name = fs_name;
357
            pr->fs_name = fs_name;
342
            pr->mp = mp;
358
            pr->mp = mp;
343
            pr->callid = callid;
359
            pr->callid = callid;
344
            pr->rid = rid;
360
            pr->rid = rid;
345
            pr->dev_handle = dev_handle;
361
            pr->dev_handle = dev_handle;
346
            link_initialize(&pr->link);
362
            link_initialize(&pr->link);
347
            list_append(&pr->link, &pending_req);
363
            list_append(&pr->link, &pending_req);
348
            return;
364
            return;
349
        }
365
        }
350
       
366
       
351
        ipc_answer_0(callid, ENOENT);
367
        ipc_answer_0(callid, ENOENT);
352
        ipc_answer_0(rid, ENOENT);
368
        ipc_answer_0(rid, ENOENT);
353
        free(mp);
369
        free(mp);
354
        free(fs_name);
370
        free(fs_name);
355
        return;
371
        return;
356
    }
372
    }
357
   
373
   
358
    /* Acknowledge that we know fs_name. */
374
    /* Acknowledge that we know fs_name. */
359
    ipc_answer_0(callid, EOK);
375
    ipc_answer_0(callid, EOK);
360
   
376
   
361
    /* Do the mount */
377
    /* Do the mount */
362
    vfs_mount_internal(rid, dev_handle, fs_handle, mp);
378
    vfs_mount_internal(rid, dev_handle, fs_handle, mp);
363
    free(mp);
379
    free(mp);
364
    free(fs_name);
380
    free(fs_name);
365
}
381
}
366
 
382
 
367
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
383
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
368
{
384
{
369
    if (!vfs_files_init()) {
385
    if (!vfs_files_init()) {
370
        ipc_answer_0(rid, ENOMEM);
386
        ipc_answer_0(rid, ENOMEM);
371
        return;
387
        return;
372
    }
388
    }
373
 
389
 
374
    /*
390
    /*
375
     * The POSIX interface is open(path, oflag, mode).
391
     * The POSIX interface is open(path, oflag, mode).
376
     * We can receive oflags and mode along with the VFS_OPEN call; the path
392
     * We can receive oflags and mode along with the VFS_OPEN call; the path
377
     * will need to arrive in another call.
393
     * will need to arrive in another call.
378
     *
394
     *
379
     * We also receive one private, non-POSIX set of flags called lflag
395
     * We also receive one private, non-POSIX set of flags called lflag
380
     * used to pass information to vfs_lookup_internal().
396
     * used to pass information to vfs_lookup_internal().
381
     */
397
     */
382
    int lflag = IPC_GET_ARG1(*request);
398
    int lflag = IPC_GET_ARG1(*request);
383
    int oflag = IPC_GET_ARG2(*request);
399
    int oflag = IPC_GET_ARG2(*request);
384
    int mode = IPC_GET_ARG3(*request);
400
    int mode = IPC_GET_ARG3(*request);
385
    size_t len;
401
    size_t len;
386
 
402
 
387
    /*
403
    /*
388
     * Make sure that we are called with exactly one of L_FILE and
404
     * Make sure that we are called with exactly one of L_FILE and
389
     * L_DIRECTORY.
405
     * L_DIRECTORY.
390
     */
406
     */
391
    if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
407
    if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
392
        (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
408
        (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
393
        ipc_answer_0(rid, EINVAL);
409
        ipc_answer_0(rid, EINVAL);
394
        return;
410
        return;
395
    }
411
    }
396
 
412
 
397
    if (oflag & O_CREAT)
413
    if (oflag & O_CREAT)
398
        lflag |= L_CREATE;
414
        lflag |= L_CREATE;
399
    if (oflag & O_EXCL)
415
    if (oflag & O_EXCL)
400
        lflag |= L_EXCLUSIVE;
416
        lflag |= L_EXCLUSIVE;
401
 
417
 
402
    ipc_callid_t callid;
418
    ipc_callid_t callid;
403
 
419
 
404
    if (!ipc_data_write_receive(&callid, &len)) {
420
    if (!ipc_data_write_receive(&callid, &len)) {
405
        ipc_answer_0(callid, EINVAL);
421
        ipc_answer_0(callid, EINVAL);
406
        ipc_answer_0(rid, EINVAL);
422
        ipc_answer_0(rid, EINVAL);
407
        return;
423
        return;
408
    }
424
    }
409
    char *path = malloc(len + 1);
425
    char *path = malloc(len + 1);
410
    if (!path) {
426
    if (!path) {
411
        ipc_answer_0(callid, ENOMEM);
427
        ipc_answer_0(callid, ENOMEM);
412
        ipc_answer_0(rid, ENOMEM);
428
        ipc_answer_0(rid, ENOMEM);
413
        return;
429
        return;
414
    }
430
    }
415
    int rc;
431
    int rc;
416
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
432
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
417
        ipc_answer_0(rid, rc);
433
        ipc_answer_0(rid, rc);
418
        free(path);
434
        free(path);
419
        return;
435
        return;
420
    }
436
    }
421
    path[len] = '\0';
437
    path[len] = '\0';
422
   
438
   
423
    /*
439
    /*
424
     * Avoid the race condition in which the file can be deleted before we
440
     * Avoid the race condition in which the file can be deleted before we
425
     * find/create-and-lock the VFS node corresponding to the looked-up
441
     * find/create-and-lock the VFS node corresponding to the looked-up
426
     * triplet.
442
     * triplet.
427
     */
443
     */
428
    if (lflag & L_CREATE)
444
    if (lflag & L_CREATE)
429
        rwlock_write_lock(&namespace_rwlock);
445
        rwlock_write_lock(&namespace_rwlock);
430
    else
446
    else
431
        rwlock_read_lock(&namespace_rwlock);
447
        rwlock_read_lock(&namespace_rwlock);
432
 
448
 
433
    /* The path is now populated and we can call vfs_lookup_internal(). */
449
    /* The path is now populated and we can call vfs_lookup_internal(). */
434
    vfs_lookup_res_t lr;
450
    vfs_lookup_res_t lr;
435
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
451
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
436
    if (rc) {
452
    if (rc) {
437
        if (lflag & L_CREATE)
453
        if (lflag & L_CREATE)
438
            rwlock_write_unlock(&namespace_rwlock);
454
            rwlock_write_unlock(&namespace_rwlock);
439
        else
455
        else
440
            rwlock_read_unlock(&namespace_rwlock);
456
            rwlock_read_unlock(&namespace_rwlock);
441
        ipc_answer_0(rid, rc);
457
        ipc_answer_0(rid, rc);
442
        free(path);
458
        free(path);
443
        return;
459
        return;
444
    }
460
    }
445
 
461
 
446
    /* Path is no longer needed. */
462
    /* Path is no longer needed. */
447
    free(path);
463
    free(path);
448
 
464
 
449
    vfs_node_t *node = vfs_node_get(&lr);
465
    vfs_node_t *node = vfs_node_get(&lr);
450
    if (lflag & L_CREATE)
466
    if (lflag & L_CREATE)
451
        rwlock_write_unlock(&namespace_rwlock);
467
        rwlock_write_unlock(&namespace_rwlock);
452
    else
468
    else
453
        rwlock_read_unlock(&namespace_rwlock);
469
        rwlock_read_unlock(&namespace_rwlock);
454
 
470
 
455
    /* Truncate the file if requested and if necessary. */
471
    /* Truncate the file if requested and if necessary. */
456
    if (oflag & O_TRUNC) {
472
    if (oflag & O_TRUNC) {
457
        rwlock_write_lock(&node->contents_rwlock);
473
        rwlock_write_lock(&node->contents_rwlock);
458
        if (node->size) {
474
        if (node->size) {
459
            rc = vfs_truncate_internal(node->fs_handle,
475
            rc = vfs_truncate_internal(node->fs_handle,
460
                node->dev_handle, node->index, 0);
476
                node->dev_handle, node->index, 0);
461
            if (rc) {
477
            if (rc) {
462
                rwlock_write_unlock(&node->contents_rwlock);
478
                rwlock_write_unlock(&node->contents_rwlock);
463
                vfs_node_put(node);
479
                vfs_node_put(node);
464
                ipc_answer_0(rid, rc);
480
                ipc_answer_0(rid, rc);
465
                return;
481
                return;
466
            }
482
            }
467
            node->size = 0;
483
            node->size = 0;
468
        }
484
        }
469
        rwlock_write_unlock(&node->contents_rwlock);
485
        rwlock_write_unlock(&node->contents_rwlock);
470
    }
486
    }
471
 
487
 
472
    /*
488
    /*
473
     * Get ourselves a file descriptor and the corresponding vfs_file_t
489
     * Get ourselves a file descriptor and the corresponding vfs_file_t
474
     * structure.
490
     * structure.
475
     */
491
     */
476
    int fd = vfs_fd_alloc();
492
    int fd = vfs_fd_alloc();
477
    if (fd < 0) {
493
    if (fd < 0) {
478
        vfs_node_put(node);
494
        vfs_node_put(node);
479
        ipc_answer_0(rid, fd);
495
        ipc_answer_0(rid, fd);
480
        return;
496
        return;
481
    }
497
    }
482
    vfs_file_t *file = vfs_file_get(fd);
498
    vfs_file_t *file = vfs_file_get(fd);
483
    file->node = node;
499
    file->node = node;
484
    if (oflag & O_APPEND)
500
    if (oflag & O_APPEND)
485
        file->append = true;
501
        file->append = true;
486
 
502
 
487
    /*
503
    /*
488
     * The following increase in reference count is for the fact that the
504
     * The following increase in reference count is for the fact that the
489
     * file is being opened and that a file structure is pointing to it.
505
     * file is being opened and that a file structure is pointing to it.
490
     * It is necessary so that the file will not disappear when
506
     * It is necessary so that the file will not disappear when
491
     * vfs_node_put() is called. The reference will be dropped by the
507
     * vfs_node_put() is called. The reference will be dropped by the
492
     * respective VFS_CLOSE.
508
     * respective VFS_CLOSE.
493
     */
509
     */
494
    vfs_node_addref(node);
510
    vfs_node_addref(node);
495
    vfs_node_put(node);
511
    vfs_node_put(node);
496
 
512
 
497
    /* Success! Return the new file descriptor to the client. */
513
    /* Success! Return the new file descriptor to the client. */
498
    ipc_answer_1(rid, EOK, fd);
514
    ipc_answer_1(rid, EOK, fd);
499
}
515
}
500
 
516
 
501
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
517
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
502
{
518
{
503
    int fd = IPC_GET_ARG1(*request);
519
    int fd = IPC_GET_ARG1(*request);
504
    int rc = vfs_fd_free(fd);
520
    int rc = vfs_fd_free(fd);
505
    ipc_answer_0(rid, rc);
521
    ipc_answer_0(rid, rc);
506
}
522
}
507
 
523
 
508
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
524
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
509
{
525
{
510
 
526
 
511
    /*
527
    /*
512
     * The following code strongly depends on the fact that the files data
528
     * The following code strongly depends on the fact that the files data
513
     * structure can be only accessed by a single fibril and all file
529
     * structure can be only accessed by a single fibril and all file
514
     * operations are serialized (i.e. the reads and writes cannot
530
     * operations are serialized (i.e. the reads and writes cannot
515
     * interleave and a file cannot be closed while it is being read).
531
     * interleave and a file cannot be closed while it is being read).
516
     *
532
     *
517
     * Additional synchronization needs to be added once the table of
533
     * Additional synchronization needs to be added once the table of
518
     * open files supports parallel access!
534
     * open files supports parallel access!
519
     */
535
     */
520
 
536
 
521
    int fd = IPC_GET_ARG1(*request);
537
    int fd = IPC_GET_ARG1(*request);
522
   
538
   
523
    /* Lookup the file structure corresponding to the file descriptor. */
539
    /* Lookup the file structure corresponding to the file descriptor. */
524
    vfs_file_t *file = vfs_file_get(fd);
540
    vfs_file_t *file = vfs_file_get(fd);
525
    if (!file) {
541
    if (!file) {
526
        ipc_answer_0(rid, ENOENT);
542
        ipc_answer_0(rid, ENOENT);
527
        return;
543
        return;
528
    }
544
    }
529
   
545
   
530
    /*
546
    /*
531
     * Now we need to receive a call with client's
547
     * Now we need to receive a call with client's
532
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
548
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
533
     */
549
     */
534
    ipc_callid_t callid;
550
    ipc_callid_t callid;
535
    int res;
551
    int res;
536
    if (read)
552
    if (read)
537
        res = ipc_data_read_receive(&callid, NULL);
553
        res = ipc_data_read_receive(&callid, NULL);
538
    else
554
    else
539
        res = ipc_data_write_receive(&callid, NULL);
555
        res = ipc_data_write_receive(&callid, NULL);
540
    if (!res) {
556
    if (!res) {
541
        ipc_answer_0(callid, EINVAL);
557
        ipc_answer_0(callid, EINVAL);
542
        ipc_answer_0(rid, EINVAL);
558
        ipc_answer_0(rid, EINVAL);
543
        return;
559
        return;
544
    }
560
    }
545
   
561
   
546
    /*
562
    /*
547
     * Lock the open file structure so that no other thread can manipulate
563
     * Lock the open file structure so that no other thread can manipulate
548
     * the same open file at a time.
564
     * the same open file at a time.
549
     */
565
     */
550
    futex_down(&file->lock);
566
    futex_down(&file->lock);
551
 
567
 
552
    /*
568
    /*
553
     * Lock the file's node so that no other client can read/write to it at
569
     * Lock the file's node so that no other client can read/write to it at
554
     * the same time.
570
     * the same time.
555
     */
571
     */
556
    if (read)
572
    if (read)
557
        rwlock_read_lock(&file->node->contents_rwlock);
573
        rwlock_read_lock(&file->node->contents_rwlock);
558
    else
574
    else
559
        rwlock_write_lock(&file->node->contents_rwlock);
575
        rwlock_write_lock(&file->node->contents_rwlock);
560
 
576
 
561
    if (file->node->type == VFS_NODE_DIRECTORY) {
577
    if (file->node->type == VFS_NODE_DIRECTORY) {
562
        /*
578
        /*
563
         * Make sure that no one is modifying the namespace
579
         * Make sure that no one is modifying the namespace
564
         * while we are in readdir().
580
         * while we are in readdir().
565
         */
581
         */
566
        assert(read);
582
        assert(read);
567
        rwlock_read_lock(&namespace_rwlock);
583
        rwlock_read_lock(&namespace_rwlock);
568
    }
584
    }
569
   
585
   
570
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
586
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
571
   
587
   
572
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
588
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
573
    aid_t msg;
589
    aid_t msg;
574
    ipc_call_t answer;
590
    ipc_call_t answer;
575
    if (!read && file->append)
591
    if (!read && file->append)
576
        file->pos = file->node->size;
592
        file->pos = file->node->size;
577
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
593
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
578
        file->node->dev_handle, file->node->index, file->pos, &answer);
594
        file->node->dev_handle, file->node->index, file->pos, &answer);
579
   
595
   
580
    /*
596
    /*
581
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
597
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
582
     * destination FS server. The call will be routed as if sent by
598
     * destination FS server. The call will be routed as if sent by
583
     * ourselves. Note that call arguments are immutable in this case so we
599
     * ourselves. Note that call arguments are immutable in this case so we
584
     * don't have to bother.
600
     * don't have to bother.
585
     */
601
     */
586
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
602
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
587
   
603
   
588
    vfs_release_phone(fs_phone);
604
    vfs_release_phone(fs_phone);
589
   
605
   
590
    /* Wait for reply from the FS server. */
606
    /* Wait for reply from the FS server. */
591
    ipcarg_t rc;
607
    ipcarg_t rc;
592
    async_wait_for(msg, &rc);
608
    async_wait_for(msg, &rc);
593
    size_t bytes = IPC_GET_ARG1(answer);
609
    size_t bytes = IPC_GET_ARG1(answer);
594
 
610
 
595
    if (file->node->type == VFS_NODE_DIRECTORY)
611
    if (file->node->type == VFS_NODE_DIRECTORY)
596
        rwlock_read_unlock(&namespace_rwlock);
612
        rwlock_read_unlock(&namespace_rwlock);
597
   
613
   
598
    /* Unlock the VFS node. */
614
    /* Unlock the VFS node. */
599
    if (read)
615
    if (read)
600
        rwlock_read_unlock(&file->node->contents_rwlock);
616
        rwlock_read_unlock(&file->node->contents_rwlock);
601
    else {
617
    else {
602
        /* Update the cached version of node's size. */
618
        /* Update the cached version of node's size. */
603
        if (rc == EOK)
619
        if (rc == EOK)
604
            file->node->size = IPC_GET_ARG2(answer);
620
            file->node->size = IPC_GET_ARG2(answer);
605
        rwlock_write_unlock(&file->node->contents_rwlock);
621
        rwlock_write_unlock(&file->node->contents_rwlock);
606
    }
622
    }
607
   
623
   
608
    /* Update the position pointer and unlock the open file. */
624
    /* Update the position pointer and unlock the open file. */
609
    if (rc == EOK)
625
    if (rc == EOK)
610
        file->pos += bytes;
626
        file->pos += bytes;
611
    futex_up(&file->lock);
627
    futex_up(&file->lock);
612
   
628
   
613
    /*
629
    /*
614
     * FS server's reply is the final result of the whole operation we
630
     * FS server's reply is the final result of the whole operation we
615
     * return to the client.
631
     * return to the client.
616
     */
632
     */
617
    ipc_answer_1(rid, rc, bytes);
633
    ipc_answer_1(rid, rc, bytes);
618
}
634
}
619
 
635
 
620
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
636
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
621
{
637
{
622
    vfs_rdwr(rid, request, true);
638
    vfs_rdwr(rid, request, true);
623
}
639
}
624
 
640
 
625
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
641
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
626
{
642
{
627
    vfs_rdwr(rid, request, false);
643
    vfs_rdwr(rid, request, false);
628
}
644
}
629
 
645
 
630
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
646
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
631
{
647
{
632
    int fd = (int) IPC_GET_ARG1(*request);
648
    int fd = (int) IPC_GET_ARG1(*request);
633
    off_t off = (off_t) IPC_GET_ARG2(*request);
649
    off_t off = (off_t) IPC_GET_ARG2(*request);
634
    int whence = (int) IPC_GET_ARG3(*request);
650
    int whence = (int) IPC_GET_ARG3(*request);
635
 
651
 
636
 
652
 
637
    /* Lookup the file structure corresponding to the file descriptor. */
653
    /* Lookup the file structure corresponding to the file descriptor. */
638
    vfs_file_t *file = vfs_file_get(fd);
654
    vfs_file_t *file = vfs_file_get(fd);
639
    if (!file) {
655
    if (!file) {
640
        ipc_answer_0(rid, ENOENT);
656
        ipc_answer_0(rid, ENOENT);
641
        return;
657
        return;
642
    }
658
    }
643
 
659
 
644
    off_t newpos;
660
    off_t newpos;
645
    futex_down(&file->lock);
661
    futex_down(&file->lock);
646
    if (whence == SEEK_SET) {
662
    if (whence == SEEK_SET) {
647
        file->pos = off;
663
        file->pos = off;
648
        futex_up(&file->lock);
664
        futex_up(&file->lock);
649
        ipc_answer_1(rid, EOK, off);
665
        ipc_answer_1(rid, EOK, off);
650
        return;
666
        return;
651
    }
667
    }
652
    if (whence == SEEK_CUR) {
668
    if (whence == SEEK_CUR) {
653
        if (file->pos + off < file->pos) {
669
        if (file->pos + off < file->pos) {
654
            futex_up(&file->lock);
670
            futex_up(&file->lock);
655
            ipc_answer_0(rid, EOVERFLOW);
671
            ipc_answer_0(rid, EOVERFLOW);
656
            return;
672
            return;
657
        }
673
        }
658
        file->pos += off;
674
        file->pos += off;
659
        newpos = file->pos;
675
        newpos = file->pos;
660
        futex_up(&file->lock);
676
        futex_up(&file->lock);
661
        ipc_answer_1(rid, EOK, newpos);
677
        ipc_answer_1(rid, EOK, newpos);
662
        return;
678
        return;
663
    }
679
    }
664
    if (whence == SEEK_END) {
680
    if (whence == SEEK_END) {
665
        rwlock_read_lock(&file->node->contents_rwlock);
681
        rwlock_read_lock(&file->node->contents_rwlock);
666
        size_t size = file->node->size;
682
        size_t size = file->node->size;
667
        rwlock_read_unlock(&file->node->contents_rwlock);
683
        rwlock_read_unlock(&file->node->contents_rwlock);
668
        if (size + off < size) {
684
        if (size + off < size) {
669
            futex_up(&file->lock);
685
            futex_up(&file->lock);
670
            ipc_answer_0(rid, EOVERFLOW);
686
            ipc_answer_0(rid, EOVERFLOW);
671
            return;
687
            return;
672
        }
688
        }
673
        newpos = size + off;
689
        newpos = size + off;
674
        futex_up(&file->lock);
690
        futex_up(&file->lock);
675
        ipc_answer_1(rid, EOK, newpos);
691
        ipc_answer_1(rid, EOK, newpos);
676
        return;
692
        return;
677
    }
693
    }
678
    futex_up(&file->lock);
694
    futex_up(&file->lock);
679
    ipc_answer_0(rid, EINVAL);
695
    ipc_answer_0(rid, EINVAL);
680
}
696
}
681
 
697
 
682
int
698
int
683
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
699
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
684
    fs_index_t index, size_t size)
700
    fs_index_t index, size_t size)
685
{
701
{
686
    ipcarg_t rc;
702
    ipcarg_t rc;
687
    int fs_phone;
703
    int fs_phone;
688
   
704
   
689
    fs_phone = vfs_grab_phone(fs_handle);
705
    fs_phone = vfs_grab_phone(fs_handle);
690
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
706
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
691
        (ipcarg_t)index, (ipcarg_t)size);
707
        (ipcarg_t)index, (ipcarg_t)size);
692
    vfs_release_phone(fs_phone);
708
    vfs_release_phone(fs_phone);
693
    return (int)rc;
709
    return (int)rc;
694
}
710
}
695
 
711
 
696
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
712
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
697
{
713
{
698
    int fd = IPC_GET_ARG1(*request);
714
    int fd = IPC_GET_ARG1(*request);
699
    size_t size = IPC_GET_ARG2(*request);
715
    size_t size = IPC_GET_ARG2(*request);
700
    int rc;
716
    int rc;
701
 
717
 
702
    vfs_file_t *file = vfs_file_get(fd);
718
    vfs_file_t *file = vfs_file_get(fd);
703
    if (!file) {
719
    if (!file) {
704
        ipc_answer_0(rid, ENOENT);
720
        ipc_answer_0(rid, ENOENT);
705
        return;
721
        return;
706
    }
722
    }
707
    futex_down(&file->lock);
723
    futex_down(&file->lock);
708
 
724
 
709
    rwlock_write_lock(&file->node->contents_rwlock);
725
    rwlock_write_lock(&file->node->contents_rwlock);
710
    rc = vfs_truncate_internal(file->node->fs_handle,
726
    rc = vfs_truncate_internal(file->node->fs_handle,
711
        file->node->dev_handle, file->node->index, size);
727
        file->node->dev_handle, file->node->index, size);
712
    if (rc == EOK)
728
    if (rc == EOK)
713
        file->node->size = size;
729
        file->node->size = size;
714
    rwlock_write_unlock(&file->node->contents_rwlock);
730
    rwlock_write_unlock(&file->node->contents_rwlock);
715
 
731
 
716
    futex_up(&file->lock);
732
    futex_up(&file->lock);
717
    ipc_answer_0(rid, (ipcarg_t)rc);
733
    ipc_answer_0(rid, (ipcarg_t)rc);
718
}
734
}
719
 
735
 
720
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
736
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
721
{
737
{
722
    int mode = IPC_GET_ARG1(*request);
738
    int mode = IPC_GET_ARG1(*request);
723
 
739
 
724
    size_t len;
740
    size_t len;
725
    ipc_callid_t callid;
741
    ipc_callid_t callid;
726
 
742
 
727
    if (!ipc_data_write_receive(&callid, &len)) {
743
    if (!ipc_data_write_receive(&callid, &len)) {
728
        ipc_answer_0(callid, EINVAL);
744
        ipc_answer_0(callid, EINVAL);
729
        ipc_answer_0(rid, EINVAL);
745
        ipc_answer_0(rid, EINVAL);
730
        return;
746
        return;
731
    }
747
    }
732
    char *path = malloc(len + 1);
748
    char *path = malloc(len + 1);
733
    if (!path) {
749
    if (!path) {
734
        ipc_answer_0(callid, ENOMEM);
750
        ipc_answer_0(callid, ENOMEM);
735
        ipc_answer_0(rid, ENOMEM);
751
        ipc_answer_0(rid, ENOMEM);
736
        return;
752
        return;
737
    }
753
    }
738
    int rc;
754
    int rc;
739
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
755
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
740
        ipc_answer_0(rid, rc);
756
        ipc_answer_0(rid, rc);
741
        free(path);
757
        free(path);
742
        return;
758
        return;
743
    }
759
    }
744
    path[len] = '\0';
760
    path[len] = '\0';
745
   
761
   
746
    rwlock_write_lock(&namespace_rwlock);
762
    rwlock_write_lock(&namespace_rwlock);
747
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
763
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
748
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
764
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
749
    rwlock_write_unlock(&namespace_rwlock);
765
    rwlock_write_unlock(&namespace_rwlock);
750
    free(path);
766
    free(path);
751
    ipc_answer_0(rid, rc);
767
    ipc_answer_0(rid, rc);
752
}
768
}
753
 
769
 
754
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
770
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
755
{
771
{
756
    int lflag = IPC_GET_ARG1(*request);
772
    int lflag = IPC_GET_ARG1(*request);
757
 
773
 
758
    size_t len;
774
    size_t len;
759
    ipc_callid_t callid;
775
    ipc_callid_t callid;
760
 
776
 
761
    if (!ipc_data_write_receive(&callid, &len)) {
777
    if (!ipc_data_write_receive(&callid, &len)) {
762
        ipc_answer_0(callid, EINVAL);
778
        ipc_answer_0(callid, EINVAL);
763
        ipc_answer_0(rid, EINVAL);
779
        ipc_answer_0(rid, EINVAL);
764
        return;
780
        return;
765
    }
781
    }
766
    char *path = malloc(len + 1);
782
    char *path = malloc(len + 1);
767
    if (!path) {
783
    if (!path) {
768
        ipc_answer_0(callid, ENOMEM);
784
        ipc_answer_0(callid, ENOMEM);
769
        ipc_answer_0(rid, ENOMEM);
785
        ipc_answer_0(rid, ENOMEM);
770
        return;
786
        return;
771
    }
787
    }
772
    int rc;
788
    int rc;
773
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
789
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
774
        ipc_answer_0(rid, rc);
790
        ipc_answer_0(rid, rc);
775
        free(path);
791
        free(path);
776
        return;
792
        return;
777
    }
793
    }
778
    path[len] = '\0';
794
    path[len] = '\0';
779
   
795
   
780
    rwlock_write_lock(&namespace_rwlock);
796
    rwlock_write_lock(&namespace_rwlock);
781
    lflag &= L_DIRECTORY;   /* sanitize lflag */
797
    lflag &= L_DIRECTORY;   /* sanitize lflag */
782
    vfs_lookup_res_t lr;
798
    vfs_lookup_res_t lr;
783
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
799
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
784
    free(path);
800
    free(path);
785
    if (rc != EOK) {
801
    if (rc != EOK) {
786
        rwlock_write_unlock(&namespace_rwlock);
802
        rwlock_write_unlock(&namespace_rwlock);
787
        ipc_answer_0(rid, rc);
803
        ipc_answer_0(rid, rc);
788
        return;
804
        return;
789
    }
805
    }
790
 
806
 
791
    /*
807
    /*
792
     * The name has already been unlinked by vfs_lookup_internal().
808
     * The name has already been unlinked by vfs_lookup_internal().
793
     * We have to get and put the VFS node to ensure that it is
809
     * We have to get and put the VFS node to ensure that it is
794
     * VFS_DESTROY'ed after the last reference to it is dropped.
810
     * VFS_DESTROY'ed after the last reference to it is dropped.
795
     */
811
     */
796
    vfs_node_t *node = vfs_node_get(&lr);
812
    vfs_node_t *node = vfs_node_get(&lr);
797
    futex_down(&nodes_futex);
813
    futex_down(&nodes_futex);
798
    node->lnkcnt--;
814
    node->lnkcnt--;
799
    futex_up(&nodes_futex);
815
    futex_up(&nodes_futex);
800
    rwlock_write_unlock(&namespace_rwlock);
816
    rwlock_write_unlock(&namespace_rwlock);
801
    vfs_node_put(node);
817
    vfs_node_put(node);
802
    ipc_answer_0(rid, EOK);
818
    ipc_answer_0(rid, EOK);
803
}
819
}
804
 
820
 
805
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
821
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
806
{
822
{
807
    size_t olen, nlen;
823
    size_t olen, nlen;
808
    ipc_callid_t callid;
824
    ipc_callid_t callid;
809
    int rc;
825
    int rc;
810
 
826
 
811
    /* Retrieve the old path. */
827
    /* Retrieve the old path. */
812
    if (!ipc_data_write_receive(&callid, &olen)) {
828
    if (!ipc_data_write_receive(&callid, &olen)) {
813
        ipc_answer_0(callid, EINVAL);
829
        ipc_answer_0(callid, EINVAL);
814
        ipc_answer_0(rid, EINVAL);
830
        ipc_answer_0(rid, EINVAL);
815
        return;
831
        return;
816
    }
832
    }
817
    char *old = malloc(olen + 1);
833
    char *old = malloc(olen + 1);
818
    if (!old) {
834
    if (!old) {
819
        ipc_answer_0(callid, ENOMEM);
835
        ipc_answer_0(callid, ENOMEM);
820
        ipc_answer_0(rid, ENOMEM);
836
        ipc_answer_0(rid, ENOMEM);
821
        return;
837
        return;
822
    }
838
    }
823
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
839
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
824
        ipc_answer_0(rid, rc);
840
        ipc_answer_0(rid, rc);
825
        free(old);
841
        free(old);
826
        return;
842
        return;
827
    }
843
    }
828
    old[olen] = '\0';
844
    old[olen] = '\0';
829
   
845
   
830
    /* Retrieve the new path. */
846
    /* Retrieve the new path. */
831
    if (!ipc_data_write_receive(&callid, &nlen)) {
847
    if (!ipc_data_write_receive(&callid, &nlen)) {
832
        ipc_answer_0(callid, EINVAL);
848
        ipc_answer_0(callid, EINVAL);
833
        ipc_answer_0(rid, EINVAL);
849
        ipc_answer_0(rid, EINVAL);
834
        free(old);
850
        free(old);
835
        return;
851
        return;
836
    }
852
    }
837
    char *new = malloc(nlen + 1);
853
    char *new = malloc(nlen + 1);
838
    if (!new) {
854
    if (!new) {
839
        ipc_answer_0(callid, ENOMEM);
855
        ipc_answer_0(callid, ENOMEM);
840
        ipc_answer_0(rid, ENOMEM);
856
        ipc_answer_0(rid, ENOMEM);
841
        free(old);
857
        free(old);
842
        return;
858
        return;
843
    }
859
    }
844
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
860
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
845
        ipc_answer_0(rid, rc);
861
        ipc_answer_0(rid, rc);
846
        free(old);
862
        free(old);
847
        free(new);
863
        free(new);
848
        return;
864
        return;
849
    }
865
    }
850
    new[nlen] = '\0';
866
    new[nlen] = '\0';
851
 
867
 
852
    char *oldc = canonify(old, &olen);
868
    char *oldc = canonify(old, &olen);
853
    char *newc = canonify(new, &nlen);
869
    char *newc = canonify(new, &nlen);
854
    if (!oldc || !newc) {
870
    if (!oldc || !newc) {
855
        ipc_answer_0(rid, EINVAL);
871
        ipc_answer_0(rid, EINVAL);
856
        free(old);
872
        free(old);
857
        free(new);
873
        free(new);
858
        return;
874
        return;
859
    }
875
    }
860
    oldc[olen] = '\0';
876
    oldc[olen] = '\0';
861
    newc[nlen] = '\0';
877
    newc[nlen] = '\0';
862
    if (!str_lcmp(newc, oldc, str_length(oldc))) {
878
    if (!str_lcmp(newc, oldc, str_length(oldc))) {
863
        /* oldc is a prefix of newc */
879
        /* oldc is a prefix of newc */
864
        ipc_answer_0(rid, EINVAL);
880
        ipc_answer_0(rid, EINVAL);
865
        free(old);
881
        free(old);
866
        free(new);
882
        free(new);
867
        return;
883
        return;
868
    }
884
    }
869
   
885
   
870
    vfs_lookup_res_t old_lr;
886
    vfs_lookup_res_t old_lr;
871
    vfs_lookup_res_t new_lr;
887
    vfs_lookup_res_t new_lr;
872
    vfs_lookup_res_t new_par_lr;
888
    vfs_lookup_res_t new_par_lr;
873
    rwlock_write_lock(&namespace_rwlock);
889
    rwlock_write_lock(&namespace_rwlock);
874
    /* Lookup the node belonging to the old file name. */
890
    /* Lookup the node belonging to the old file name. */
875
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
891
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
876
    if (rc != EOK) {
892
    if (rc != EOK) {
877
        rwlock_write_unlock(&namespace_rwlock);
893
        rwlock_write_unlock(&namespace_rwlock);
878
        ipc_answer_0(rid, rc);
894
        ipc_answer_0(rid, rc);
879
        free(old);
895
        free(old);
880
        free(new);
896
        free(new);
881
        return;
897
        return;
882
    }
898
    }
883
    vfs_node_t *old_node = vfs_node_get(&old_lr);
899
    vfs_node_t *old_node = vfs_node_get(&old_lr);
884
    if (!old_node) {
900
    if (!old_node) {
885
        rwlock_write_unlock(&namespace_rwlock);
901
        rwlock_write_unlock(&namespace_rwlock);
886
        ipc_answer_0(rid, ENOMEM);
902
        ipc_answer_0(rid, ENOMEM);
887
        free(old);
903
        free(old);
888
        free(new);
904
        free(new);
889
        return;
905
        return;
890
    }
906
    }
891
    /* Lookup parent of the new file name. */
907
    /* Lookup parent of the new file name. */
892
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
908
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
893
    if (rc != EOK) {
909
    if (rc != EOK) {
894
        rwlock_write_unlock(&namespace_rwlock);
910
        rwlock_write_unlock(&namespace_rwlock);
895
        ipc_answer_0(rid, rc);
911
        ipc_answer_0(rid, rc);
896
        free(old);
912
        free(old);
897
        free(new);
913
        free(new);
898
        return;
914
        return;
899
    }
915
    }
900
    /* Check whether linking to the same file system instance. */
916
    /* Check whether linking to the same file system instance. */
901
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
917
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
902
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
918
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
903
        rwlock_write_unlock(&namespace_rwlock);
919
        rwlock_write_unlock(&namespace_rwlock);
904
        ipc_answer_0(rid, EXDEV);   /* different file systems */
920
        ipc_answer_0(rid, EXDEV);   /* different file systems */
905
        free(old);
921
        free(old);
906
        free(new);
922
        free(new);
907
        return;
923
        return;
908
    }
924
    }
909
    /* Destroy the old link for the new name. */
925
    /* Destroy the old link for the new name. */
910
    vfs_node_t *new_node = NULL;
926
    vfs_node_t *new_node = NULL;
911
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
927
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
912
    switch (rc) {
928
    switch (rc) {
913
    case ENOENT:
929
    case ENOENT:
914
        /* simply not in our way */
930
        /* simply not in our way */
915
        break;
931
        break;
916
    case EOK:
932
    case EOK:
917
        new_node = vfs_node_get(&new_lr);
933
        new_node = vfs_node_get(&new_lr);
918
        if (!new_node) {
934
        if (!new_node) {
919
            rwlock_write_unlock(&namespace_rwlock);
935
            rwlock_write_unlock(&namespace_rwlock);
920
            ipc_answer_0(rid, ENOMEM);
936
            ipc_answer_0(rid, ENOMEM);
921
            free(old);
937
            free(old);
922
            free(new);
938
            free(new);
923
            return;
939
            return;
924
        }
940
        }
925
        futex_down(&nodes_futex);
941
        futex_down(&nodes_futex);
926
        new_node->lnkcnt--;
942
        new_node->lnkcnt--;
927
        futex_up(&nodes_futex);
943
        futex_up(&nodes_futex);
928
        break;
944
        break;
929
    default:
945
    default:
930
        rwlock_write_unlock(&namespace_rwlock);
946
        rwlock_write_unlock(&namespace_rwlock);
931
        ipc_answer_0(rid, ENOTEMPTY);
947
        ipc_answer_0(rid, ENOTEMPTY);
932
        free(old);
948
        free(old);
933
        free(new);
949
        free(new);
934
        return;
950
        return;
935
    }
951
    }
936
    /* Create the new link for the new name. */
952
    /* Create the new link for the new name. */
937
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
953
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
938
    if (rc != EOK) {
954
    if (rc != EOK) {
939
        rwlock_write_unlock(&namespace_rwlock);
955
        rwlock_write_unlock(&namespace_rwlock);
940
        if (new_node)
956
        if (new_node)
941
            vfs_node_put(new_node);
957
            vfs_node_put(new_node);
942
        ipc_answer_0(rid, rc);
958
        ipc_answer_0(rid, rc);
943
        free(old);
959
        free(old);
944
        free(new);
960
        free(new);
945
        return;
961
        return;
946
    }
962
    }
947
    futex_down(&nodes_futex);
963
    futex_down(&nodes_futex);
948
    old_node->lnkcnt++;
964
    old_node->lnkcnt++;
949
    futex_up(&nodes_futex);
965
    futex_up(&nodes_futex);
950
    /* Destroy the link for the old name. */
966
    /* Destroy the link for the old name. */
951
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
967
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
952
    if (rc != EOK) {
968
    if (rc != EOK) {
953
        rwlock_write_unlock(&namespace_rwlock);
969
        rwlock_write_unlock(&namespace_rwlock);
954
        vfs_node_put(old_node);
970
        vfs_node_put(old_node);
955
        if (new_node)
971
        if (new_node)
956
            vfs_node_put(new_node);
972
            vfs_node_put(new_node);
957
        ipc_answer_0(rid, rc);
973
        ipc_answer_0(rid, rc);
958
        free(old);
974
        free(old);
959
        free(new);
975
        free(new);
960
        return;
976
        return;
961
    }
977
    }
962
    futex_down(&nodes_futex);
978
    futex_down(&nodes_futex);
963
    old_node->lnkcnt--;
979
    old_node->lnkcnt--;
964
    futex_up(&nodes_futex);
980
    futex_up(&nodes_futex);
965
    rwlock_write_unlock(&namespace_rwlock);
981
    rwlock_write_unlock(&namespace_rwlock);
966
    vfs_node_put(old_node);
982
    vfs_node_put(old_node);
967
    if (new_node)
983
    if (new_node)
968
        vfs_node_put(new_node);
984
        vfs_node_put(new_node);
969
    free(old);
985
    free(old);
970
    free(new);
986
    free(new);
971
    ipc_answer_0(rid, EOK);
987
    ipc_answer_0(rid, EOK);
972
}
988
}
973
 
989
 
974
/**
990
/**
975
 * @}
991
 * @}
976
 */
992
 */
977
 
993