Subversion Repositories HelenOS

Rev

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

Rev 4463 Rev 4509
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 <adt/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
    char *opts;       /**< Mount options. */
63
    char *opts;       /**< Mount options. */
64
    ipc_callid_t callid;      /**< Call ID waiting for the mount */
64
    ipc_callid_t callid;      /**< Call ID waiting for the mount */
65
    ipc_callid_t rid;         /**< Request ID */
65
    ipc_callid_t rid;         /**< Request ID */
66
    dev_handle_t dev_handle;  /**< Device handle */
66
    dev_handle_t dev_handle;  /**< Device handle */
67
} pending_req_t;
67
} pending_req_t;
68
 
68
 
69
LIST_INITIALIZE(pending_req);
69
LIST_INITIALIZE(pending_req);
70
 
70
 
71
/**
71
/**
72
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
72
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
73
 * concurrent VFS operation which modifies the file system namespace.
73
 * concurrent VFS operation which modifies the file system namespace.
74
 */
74
 */
75
RWLOCK_INITIALIZE(namespace_rwlock);
75
RWLOCK_INITIALIZE(namespace_rwlock);
76
 
76
 
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, char *opts)
83
    fs_handle_t fs_handle, char *mp, char *opts)
84
{
84
{
85
    vfs_lookup_res_t mp_res;
85
    vfs_lookup_res_t mp_res;
86
    vfs_lookup_res_t mr_res;
86
    vfs_lookup_res_t mr_res;
87
    vfs_node_t *mp_node = NULL;
87
    vfs_node_t *mp_node = NULL;
88
    vfs_node_t *mr_node;
88
    vfs_node_t *mr_node;
89
    fs_index_t rindex;
89
    fs_index_t rindex;
90
    size_t rsize;
90
    size_t rsize;
91
    unsigned rlnkcnt;
91
    unsigned rlnkcnt;
92
    ipcarg_t rc;
92
    ipcarg_t rc;
93
    int phone;
93
    int phone;
94
    aid_t msg;
94
    aid_t msg;
95
    ipc_call_t answer;
95
    ipc_call_t answer;
96
   
96
   
97
    /* Resolve the path to the mountpoint. */
97
    /* Resolve the path to the mountpoint. */
98
    rwlock_write_lock(&namespace_rwlock);
98
    rwlock_write_lock(&namespace_rwlock);
99
    if (rootfs.fs_handle) {
99
    if (rootfs.fs_handle) {
100
        /* We already have the root FS. */
100
        /* We already have the root FS. */
101
        if (str_cmp(mp, "/") == 0) {
101
        if (str_cmp(mp, "/") == 0) {
102
            /* Trying to mount root FS over root FS */
102
            /* Trying to mount root FS over root FS */
103
            ipc_answer_0(rid, EBUSY);
103
            ipc_answer_0(rid, EBUSY);
104
            rwlock_write_unlock(&namespace_rwlock);
104
            rwlock_write_unlock(&namespace_rwlock);
105
            return;
105
            return;
106
        }
106
        }
107
       
107
       
108
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
108
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
109
        if (rc != EOK) {
109
        if (rc != EOK) {
110
            /* The lookup failed for some reason. */
110
            /* The lookup failed for some reason. */
111
            ipc_answer_0(rid, rc);
111
            ipc_answer_0(rid, rc);
112
            rwlock_write_unlock(&namespace_rwlock);
112
            rwlock_write_unlock(&namespace_rwlock);
113
            return;
113
            return;
114
        }
114
        }
115
       
115
       
116
        mp_node = vfs_node_get(&mp_res);
116
        mp_node = vfs_node_get(&mp_res);
117
        if (!mp_node) {
117
        if (!mp_node) {
118
            ipc_answer_0(rid, ENOMEM);
118
            ipc_answer_0(rid, ENOMEM);
119
            rwlock_write_unlock(&namespace_rwlock);
119
            rwlock_write_unlock(&namespace_rwlock);
120
            return;
120
            return;
121
        }
121
        }
122
       
122
       
123
        /*
123
        /*
124
         * Now we hold a reference to mp_node.
124
         * Now we hold a reference to mp_node.
125
         * It will be dropped upon the corresponding VFS_UNMOUNT.
125
         * It will be dropped upon the corresponding VFS_UNMOUNT.
126
         * This prevents the mount point from being deleted.
126
         * This prevents the mount point from being deleted.
127
         */
127
         */
128
    } else {
128
    } else {
129
        /* We still don't have the root file system mounted. */
129
        /* We still don't have the root file system mounted. */
130
        if (str_cmp(mp, "/") == 0) {
130
        if (str_cmp(mp, "/") == 0) {
131
            /*
131
            /*
132
             * For this simple, but important case,
132
             * For this simple, but important case,
133
             * we are almost done.
133
             * we are almost done.
134
             */
134
             */
135
           
135
           
136
            /* Tell the mountee that it is being mounted. */
136
            /* Tell the mountee that it is being mounted. */
137
            phone = vfs_grab_phone(fs_handle);
137
            phone = vfs_grab_phone(fs_handle);
138
            msg = async_send_1(phone, VFS_MOUNTED,
138
            msg = async_send_1(phone, VFS_MOUNTED,
139
                (ipcarg_t) dev_handle, &answer);
139
                (ipcarg_t) dev_handle, &answer);
140
            /* send the mount options */
140
            /* send the mount options */
141
            rc = ipc_data_write_start(phone, (void *)opts,
141
            rc = ipc_data_write_start(phone, (void *)opts,
142
                str_size(opts));
142
                str_size(opts));
143
            if (rc != EOK) {
143
            if (rc != EOK) {
144
                async_wait_for(msg, NULL);
144
                async_wait_for(msg, NULL);
145
                vfs_release_phone(phone);
145
                vfs_release_phone(phone);
146
                ipc_answer_0(rid, rc);
146
                ipc_answer_0(rid, rc);
147
                rwlock_write_unlock(&namespace_rwlock);
147
                rwlock_write_unlock(&namespace_rwlock);
148
                return;
148
                return;
149
            }
149
            }
150
            async_wait_for(msg, &rc);
150
            async_wait_for(msg, &rc);
151
            vfs_release_phone(phone);
151
            vfs_release_phone(phone);
152
           
152
           
153
            if (rc != EOK) {
153
            if (rc != EOK) {
154
                ipc_answer_0(rid, rc);
154
                ipc_answer_0(rid, rc);
155
                rwlock_write_unlock(&namespace_rwlock);
155
                rwlock_write_unlock(&namespace_rwlock);
156
                return;
156
                return;
157
            }
157
            }
158
 
158
 
159
            rindex = (fs_index_t) IPC_GET_ARG1(answer);
159
            rindex = (fs_index_t) IPC_GET_ARG1(answer);
160
            rsize = (size_t) IPC_GET_ARG2(answer);
160
            rsize = (size_t) IPC_GET_ARG2(answer);
161
            rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
161
            rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
162
           
162
           
163
            mr_res.triplet.fs_handle = fs_handle;
163
            mr_res.triplet.fs_handle = fs_handle;
164
            mr_res.triplet.dev_handle = dev_handle;
164
            mr_res.triplet.dev_handle = dev_handle;
165
            mr_res.triplet.index = rindex;
165
            mr_res.triplet.index = rindex;
166
            mr_res.size = rsize;
166
            mr_res.size = rsize;
167
            mr_res.lnkcnt = rlnkcnt;
167
            mr_res.lnkcnt = rlnkcnt;
168
            mr_res.type = VFS_NODE_DIRECTORY;
168
            mr_res.type = VFS_NODE_DIRECTORY;
169
           
169
           
170
            rootfs.fs_handle = fs_handle;
170
            rootfs.fs_handle = fs_handle;
171
            rootfs.dev_handle = dev_handle;
171
            rootfs.dev_handle = dev_handle;
172
           
172
           
173
            /* Add reference to the mounted root. */
173
            /* Add reference to the mounted root. */
174
            mr_node = vfs_node_get(&mr_res);
174
            mr_node = vfs_node_get(&mr_res);
175
            assert(mr_node);
175
            assert(mr_node);
176
           
176
           
177
            ipc_answer_0(rid, rc);
177
            ipc_answer_0(rid, rc);
178
            rwlock_write_unlock(&namespace_rwlock);
178
            rwlock_write_unlock(&namespace_rwlock);
179
            return;
179
            return;
180
        } else {
180
        } else {
181
            /*
181
            /*
182
             * We can't resolve this without the root filesystem
182
             * We can't resolve this without the root filesystem
183
             * being mounted first.
183
             * being mounted first.
184
             */
184
             */
185
            ipc_answer_0(rid, ENOENT);
185
            ipc_answer_0(rid, ENOENT);
186
            rwlock_write_unlock(&namespace_rwlock);
186
            rwlock_write_unlock(&namespace_rwlock);
187
            return;
187
            return;
188
        }
188
        }
189
    }
189
    }
190
   
190
   
191
    /*
191
    /*
192
     * At this point, we have all necessary pieces: file system and device
192
     * At this point, we have all necessary pieces: file system and device
193
     * handles, and we know the mount point VFS node.
193
     * handles, and we know the mount point VFS node.
194
     */
194
     */
195
   
195
   
196
    int mountee_phone = vfs_grab_phone(fs_handle);
196
    int mountee_phone = vfs_grab_phone(fs_handle);
197
    assert(mountee_phone >= 0);
197
    assert(mountee_phone >= 0);
198
    vfs_release_phone(mountee_phone);
198
    vfs_release_phone(mountee_phone);
199
 
199
 
200
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
200
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
201
    msg = async_send_4(phone, VFS_MOUNT,
201
    msg = async_send_4(phone, VFS_MOUNT,
202
        (ipcarg_t) mp_res.triplet.dev_handle,
202
        (ipcarg_t) mp_res.triplet.dev_handle,
203
        (ipcarg_t) mp_res.triplet.index,
203
        (ipcarg_t) mp_res.triplet.index,
204
        (ipcarg_t) fs_handle,
204
        (ipcarg_t) fs_handle,
205
        (ipcarg_t) dev_handle, &answer);
205
        (ipcarg_t) dev_handle, &answer);
206
   
206
   
207
    /* send connection */
207
    /* send connection */
208
    rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
208
    rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
209
    if (rc != EOK) {
209
    if (rc != EOK) {
210
        async_wait_for(msg, NULL);
210
        async_wait_for(msg, NULL);
211
        vfs_release_phone(phone);
211
        vfs_release_phone(phone);
212
        /* Mount failed, drop reference to mp_node. */
212
        /* Mount failed, drop reference to mp_node. */
213
        if (mp_node)
213
        if (mp_node)
214
            vfs_node_put(mp_node);
214
            vfs_node_put(mp_node);
215
        ipc_answer_0(rid, rc);
215
        ipc_answer_0(rid, rc);
216
        rwlock_write_unlock(&namespace_rwlock);
216
        rwlock_write_unlock(&namespace_rwlock);
217
        return;
217
        return;
218
    }
218
    }
219
   
219
   
220
    /* send the mount options */
220
    /* send the mount options */
221
    rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
221
    rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
222
    if (rc != EOK) {
222
    if (rc != EOK) {
223
        async_wait_for(msg, NULL);
223
        async_wait_for(msg, NULL);
224
        vfs_release_phone(phone);
224
        vfs_release_phone(phone);
225
        /* Mount failed, drop reference to mp_node. */
225
        /* Mount failed, drop reference to mp_node. */
226
        if (mp_node)
226
        if (mp_node)
227
            vfs_node_put(mp_node);
227
            vfs_node_put(mp_node);
228
        ipc_answer_0(rid, rc);
228
        ipc_answer_0(rid, rc);
229
        rwlock_write_unlock(&namespace_rwlock);
229
        rwlock_write_unlock(&namespace_rwlock);
230
        return;
230
        return;
231
    }
231
    }
232
    async_wait_for(msg, &rc);
232
    async_wait_for(msg, &rc);
233
    vfs_release_phone(phone);
233
    vfs_release_phone(phone);
234
   
234
   
235
    if (rc == EOK) {
235
    if (rc == EOK) {
236
        rindex = (fs_index_t) IPC_GET_ARG1(answer);
236
        rindex = (fs_index_t) IPC_GET_ARG1(answer);
237
        rsize = (size_t) IPC_GET_ARG2(answer);
237
        rsize = (size_t) IPC_GET_ARG2(answer);
238
        rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
238
        rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
239
   
239
   
240
        mr_res.triplet.fs_handle = fs_handle;
240
        mr_res.triplet.fs_handle = fs_handle;
241
        mr_res.triplet.dev_handle = dev_handle;
241
        mr_res.triplet.dev_handle = dev_handle;
242
        mr_res.triplet.index = rindex;
242
        mr_res.triplet.index = rindex;
243
        mr_res.size = rsize;
243
        mr_res.size = rsize;
244
        mr_res.lnkcnt = rlnkcnt;
244
        mr_res.lnkcnt = rlnkcnt;
245
        mr_res.type = VFS_NODE_DIRECTORY;
245
        mr_res.type = VFS_NODE_DIRECTORY;
246
   
246
   
247
        /* Add reference to the mounted root. */
247
        /* Add reference to the mounted root. */
248
        mr_node = vfs_node_get(&mr_res);
248
        mr_node = vfs_node_get(&mr_res);
249
        assert(mr_node);
249
        assert(mr_node);
250
    } else {
250
    } else {
251
        /* Mount failed, drop reference to mp_node. */
251
        /* Mount failed, drop reference to mp_node. */
252
        if (mp_node)
252
        if (mp_node)
253
            vfs_node_put(mp_node);
253
            vfs_node_put(mp_node);
254
    }
254
    }
255
 
255
 
256
    ipc_answer_0(rid, rc);
256
    ipc_answer_0(rid, rc);
257
    rwlock_write_unlock(&namespace_rwlock);
257
    rwlock_write_unlock(&namespace_rwlock);
258
}
258
}
259
 
259
 
260
/** Process pending mount requests */
260
/** Process pending mount requests */
261
void vfs_process_pending_mount(void)
261
void vfs_process_pending_mount(void)
262
{
262
{
263
    link_t *cur;
263
    link_t *cur;
264
   
264
   
265
loop:
265
loop:
266
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
266
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
267
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
267
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
268
       
268
       
269
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
269
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
270
        if (!fs_handle)
270
        if (!fs_handle)
271
            continue;
271
            continue;
272
       
272
       
273
        /* Acknowledge that we know fs_name. */
273
        /* Acknowledge that we know fs_name. */
274
        ipc_answer_0(pr->callid, EOK);
274
        ipc_answer_0(pr->callid, EOK);
275
       
275
       
276
        /* Do the mount */
276
        /* Do the mount */
277
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp,
277
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp,
278
            pr->opts);
278
            pr->opts);
279
       
279
       
280
        free(pr->fs_name);
280
        free(pr->fs_name);
281
        free(pr->mp);
281
        free(pr->mp);
282
        free(pr->opts);
282
        free(pr->opts);
283
        list_remove(cur);
283
        list_remove(cur);
284
        free(pr);
284
        free(pr);
285
        goto loop;
285
        goto loop;
286
    }
286
    }
287
}
287
}
288
 
288
 
289
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
289
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
290
{
290
{
291
    /*
291
    /*
292
     * We expect the library to do the device-name to device-handle
292
     * We expect the library to do the device-name to device-handle
293
     * translation for us, thus the device handle will arrive as ARG1
293
     * translation for us, thus the device handle will arrive as ARG1
294
     * in the request.
294
     * in the request.
295
     */
295
     */
296
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
296
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
297
   
297
   
298
    /*
298
    /*
299
     * Mount flags are passed as ARG2.
299
     * Mount flags are passed as ARG2.
300
     */
300
     */
301
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
301
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
302
   
302
   
303
    /*
303
    /*
304
     * For now, don't make use of ARG3, but it can be used to
304
     * For now, don't make use of ARG3, but it can be used to
305
     * carry mount options in the future.
305
     * carry mount options in the future.
306
     */
306
     */
307
   
307
   
308
    /* We want the client to send us the mount point. */
308
    /* We want the client to send us the mount point. */
309
    ipc_callid_t callid;
309
    ipc_callid_t callid;
310
    size_t size;
310
    size_t size;
311
    if (!ipc_data_write_receive(&callid, &size)) {
311
    if (!ipc_data_write_receive(&callid, &size)) {
312
        ipc_answer_0(callid, EINVAL);
312
        ipc_answer_0(callid, EINVAL);
313
        ipc_answer_0(rid, EINVAL);
313
        ipc_answer_0(rid, EINVAL);
314
        return;
314
        return;
315
    }
315
    }
316
   
316
   
317
    /* Check whether size is reasonable wrt. the mount point. */
317
    /* Check whether size is reasonable wrt. the mount point. */
318
    if ((size < 1) || (size > MAX_PATH_LEN)) {
318
    if ((size < 1) || (size > MAX_PATH_LEN)) {
319
        ipc_answer_0(callid, EINVAL);
319
        ipc_answer_0(callid, EINVAL);
320
        ipc_answer_0(rid, EINVAL);
320
        ipc_answer_0(rid, EINVAL);
321
        return;
321
        return;
322
    }
322
    }
323
   
323
   
324
    /* Allocate buffer for the mount point data being received. */
324
    /* Allocate buffer for the mount point data being received. */
325
    char *mp = malloc(size + 1);
325
    char *mp = malloc(size + 1);
326
    if (!mp) {
326
    if (!mp) {
327
        ipc_answer_0(callid, ENOMEM);
327
        ipc_answer_0(callid, ENOMEM);
328
        ipc_answer_0(rid, ENOMEM);
328
        ipc_answer_0(rid, ENOMEM);
329
        return;
329
        return;
330
    }
330
    }
331
   
331
   
332
    /* Deliver the mount point. */
332
    /* Deliver the mount point. */
333
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
333
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
334
    if (retval != EOK) {
334
    if (retval != EOK) {
335
        ipc_answer_0(rid, retval);
335
        ipc_answer_0(rid, retval);
336
        free(mp);
336
        free(mp);
337
        return;
337
        return;
338
    }
338
    }
339
    mp[size] = '\0';
339
    mp[size] = '\0';
340
   
340
   
341
    /* Now we expect to receive the mount options. */
341
    /* Now we expect to receive the mount options. */
342
    if (!ipc_data_write_receive(&callid, &size)) {
342
    if (!ipc_data_write_receive(&callid, &size)) {
343
        ipc_answer_0(callid, EINVAL);
343
        ipc_answer_0(callid, EINVAL);
344
        ipc_answer_0(rid, EINVAL);
344
        ipc_answer_0(rid, EINVAL);
345
        free(mp);
345
        free(mp);
346
        return;
346
        return;
347
    }
347
    }
348
 
348
 
349
    /* Check the offered options size. */
349
    /* Check the offered options size. */
350
    if (size < 0 || size > MAX_MNTOPTS_LEN) {
350
    if (size < 0 || size > MAX_MNTOPTS_LEN) {
351
        ipc_answer_0(callid, EINVAL);
351
        ipc_answer_0(callid, EINVAL);
352
        ipc_answer_0(rid, EINVAL);
352
        ipc_answer_0(rid, EINVAL);
353
        free(mp);
353
        free(mp);
354
        return;
354
        return;
355
    }
355
    }
356
 
356
 
357
    /* Allocate buffer for the mount options. */
357
    /* Allocate buffer for the mount options. */
358
    char *opts = (char *) malloc(size + 1);
358
    char *opts = (char *) malloc(size + 1);
359
    if (!opts) {
359
    if (!opts) {
360
        ipc_answer_0(callid, ENOMEM);
360
        ipc_answer_0(callid, ENOMEM);
361
        ipc_answer_0(rid, ENOMEM);
361
        ipc_answer_0(rid, ENOMEM);
362
        free(mp);
362
        free(mp);
363
        return;
363
        return;
364
    }
364
    }
365
 
365
 
366
    /* Deliver the mount options. */
366
    /* Deliver the mount options. */
367
    retval = ipc_data_write_finalize(callid, opts, size);
367
    retval = ipc_data_write_finalize(callid, opts, size);
368
    if (retval != EOK) {
368
    if (retval != EOK) {
369
        ipc_answer_0(rid, retval);
369
        ipc_answer_0(rid, retval);
370
        free(mp);
370
        free(mp);
371
        free(opts);
371
        free(opts);
372
        return;
372
        return;
373
    }
373
    }
374
    opts[size] = '\0';
374
    opts[size] = '\0';
375
   
375
   
376
    /*
376
    /*
377
     * Now, we expect the client to send us data with the name of the file
377
     * Now, we expect the client to send us data with the name of the file
378
     * system.
378
     * system.
379
     */
379
     */
380
    if (!ipc_data_write_receive(&callid, &size)) {
380
    if (!ipc_data_write_receive(&callid, &size)) {
381
        ipc_answer_0(callid, EINVAL);
381
        ipc_answer_0(callid, EINVAL);
382
        ipc_answer_0(rid, EINVAL);
382
        ipc_answer_0(rid, EINVAL);
383
        free(mp);
383
        free(mp);
384
        free(opts);
384
        free(opts);
385
        return;
385
        return;
386
    }
386
    }
387
   
387
   
388
    /*
388
    /*
389
     * Don't receive more than is necessary for storing a full file system
389
     * Don't receive more than is necessary for storing a full file system
390
     * name.
390
     * name.
391
     */
391
     */
392
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
392
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
393
        ipc_answer_0(callid, EINVAL);
393
        ipc_answer_0(callid, EINVAL);
394
        ipc_answer_0(rid, EINVAL);
394
        ipc_answer_0(rid, EINVAL);
395
        free(mp);
395
        free(mp);
396
        free(opts);
396
        free(opts);
397
        return;
397
        return;
398
    }
398
    }
399
   
399
   
400
    /*
400
    /*
401
     * Allocate buffer for file system name.
401
     * Allocate buffer for file system name.
402
     */
402
     */
403
    char *fs_name = (char *) malloc(size + 1);
403
    char *fs_name = (char *) malloc(size + 1);
404
    if (fs_name == NULL) {
404
    if (fs_name == NULL) {
405
        ipc_answer_0(callid, ENOMEM);
405
        ipc_answer_0(callid, ENOMEM);
406
        ipc_answer_0(rid, ENOMEM);
406
        ipc_answer_0(rid, ENOMEM);
407
        free(mp);
407
        free(mp);
408
        free(opts);
408
        free(opts);
409
        return;
409
        return;
410
    }
410
    }
411
   
411
   
412
    /* Deliver the file system name. */
412
    /* Deliver the file system name. */
413
    retval = ipc_data_write_finalize(callid, fs_name, size);
413
    retval = ipc_data_write_finalize(callid, fs_name, size);
414
    if (retval != EOK) {
414
    if (retval != EOK) {
415
        ipc_answer_0(rid, retval);
415
        ipc_answer_0(rid, retval);
416
        free(mp);
416
        free(mp);
417
        free(opts);
417
        free(opts);
418
        free(fs_name);
418
        free(fs_name);
419
        return;
419
        return;
420
    }
420
    }
421
    fs_name[size] = '\0';
421
    fs_name[size] = '\0';
422
 
422
 
423
    /*
423
    /*
424
     * Wait for IPC_M_PING so that we can return an error if we don't know
424
     * Wait for IPC_M_PING so that we can return an error if we don't know
425
     * fs_name.
425
     * fs_name.
426
     */
426
     */
427
    ipc_call_t data;
427
    ipc_call_t data;
428
    callid = async_get_call(&data);
428
    callid = async_get_call(&data);
429
    if (IPC_GET_METHOD(data) != IPC_M_PING) {
429
    if (IPC_GET_METHOD(data) != IPC_M_PING) {
430
        ipc_answer_0(callid, ENOTSUP);
430
        ipc_answer_0(callid, ENOTSUP);
431
        ipc_answer_0(rid, ENOTSUP);
431
        ipc_answer_0(rid, ENOTSUP);
432
        free(mp);
432
        free(mp);
433
        free(opts);
433
        free(opts);
434
        free(fs_name);
434
        free(fs_name);
435
        return;
435
        return;
436
    }
436
    }
437
 
437
 
438
    /*
438
    /*
439
     * Check if we know a file system with the same name as is in fs_name.
439
     * Check if we know a file system with the same name as is in fs_name.
440
     * This will also give us its file system handle.
440
     * This will also give us its file system handle.
441
     */
441
     */
442
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
442
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
443
    if (!fs_handle) {
443
    if (!fs_handle) {
444
        if (flags & IPC_FLAG_BLOCKING) {
444
        if (flags & IPC_FLAG_BLOCKING) {
445
            pending_req_t *pr;
445
            pending_req_t *pr;
446
 
446
 
447
            /* Blocking mount, add to pending list */
447
            /* Blocking mount, add to pending list */
448
            pr = (pending_req_t *) malloc(sizeof(pending_req_t));
448
            pr = (pending_req_t *) malloc(sizeof(pending_req_t));
449
            if (!pr) {
449
            if (!pr) {
450
                ipc_answer_0(callid, ENOMEM);
450
                ipc_answer_0(callid, ENOMEM);
451
                ipc_answer_0(rid, ENOMEM);
451
                ipc_answer_0(rid, ENOMEM);
452
                free(mp);
452
                free(mp);
453
                free(fs_name);
453
                free(fs_name);
454
                free(opts);
454
                free(opts);
455
                return;
455
                return;
456
            }
456
            }
457
           
457
           
458
            pr->fs_name = fs_name;
458
            pr->fs_name = fs_name;
459
            pr->mp = mp;
459
            pr->mp = mp;
460
            pr->opts = opts;
460
            pr->opts = opts;
461
            pr->callid = callid;
461
            pr->callid = callid;
462
            pr->rid = rid;
462
            pr->rid = rid;
463
            pr->dev_handle = dev_handle;
463
            pr->dev_handle = dev_handle;
464
            link_initialize(&pr->link);
464
            link_initialize(&pr->link);
465
            list_append(&pr->link, &pending_req);
465
            list_append(&pr->link, &pending_req);
466
            return;
466
            return;
467
        }
467
        }
468
       
468
       
469
        ipc_answer_0(callid, ENOENT);
469
        ipc_answer_0(callid, ENOENT);
470
        ipc_answer_0(rid, ENOENT);
470
        ipc_answer_0(rid, ENOENT);
471
        free(mp);
471
        free(mp);
472
        free(fs_name);
472
        free(fs_name);
473
        free(opts);
473
        free(opts);
474
        return;
474
        return;
475
    }
475
    }
476
   
476
   
477
    /* Acknowledge that we know fs_name. */
477
    /* Acknowledge that we know fs_name. */
478
    ipc_answer_0(callid, EOK);
478
    ipc_answer_0(callid, EOK);
479
   
479
   
480
    /* Do the mount */
480
    /* Do the mount */
481
    vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
481
    vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
482
    free(mp);
482
    free(mp);
483
    free(fs_name);
483
    free(fs_name);
484
    free(opts);
484
    free(opts);
485
}
485
}
486
 
486
 
487
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
487
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
488
{
488
{
489
    if (!vfs_files_init()) {
489
    if (!vfs_files_init()) {
490
        ipc_answer_0(rid, ENOMEM);
490
        ipc_answer_0(rid, ENOMEM);
491
        return;
491
        return;
492
    }
492
    }
493
   
493
   
494
    /*
494
    /*
495
     * The POSIX interface is open(path, oflag, mode).
495
     * The POSIX interface is open(path, oflag, mode).
496
     * We can receive oflags and mode along with the VFS_OPEN call; the path
496
     * We can receive oflags and mode along with the VFS_OPEN call; the path
497
     * will need to arrive in another call.
497
     * will need to arrive in another call.
498
     *
498
     *
499
     * We also receive one private, non-POSIX set of flags called lflag
499
     * We also receive one private, non-POSIX set of flags called lflag
500
     * used to pass information to vfs_lookup_internal().
500
     * used to pass information to vfs_lookup_internal().
501
     */
501
     */
502
    int lflag = IPC_GET_ARG1(*request);
502
    int lflag = IPC_GET_ARG1(*request);
503
    int oflag = IPC_GET_ARG2(*request);
503
    int oflag = IPC_GET_ARG2(*request);
504
    int mode = IPC_GET_ARG3(*request);
504
    int mode = IPC_GET_ARG3(*request);
505
    size_t len;
505
    size_t len;
506
   
506
   
507
    /*
507
    /*
508
     * Make sure that we are called with exactly one of L_FILE and
508
     * Make sure that we are called with exactly one of L_FILE and
509
     * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
509
     * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
510
     */
510
     */
511
    if (((lflag & (L_FILE | L_DIRECTORY)) == 0)
511
    if (((lflag & (L_FILE | L_DIRECTORY)) == 0)
512
        || ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY))
512
        || ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY))
513
        || ((lflag & L_OPEN) != 0)) {
513
        || ((lflag & L_OPEN) != 0)) {
514
        ipc_answer_0(rid, EINVAL);
514
        ipc_answer_0(rid, EINVAL);
515
        return;
515
        return;
516
    }
516
    }
517
   
517
   
518
    if (oflag & O_CREAT)
518
    if (oflag & O_CREAT)
519
        lflag |= L_CREATE;
519
        lflag |= L_CREATE;
520
    if (oflag & O_EXCL)
520
    if (oflag & O_EXCL)
521
        lflag |= L_EXCLUSIVE;
521
        lflag |= L_EXCLUSIVE;
522
   
522
   
523
    ipc_callid_t callid;
523
    ipc_callid_t callid;
524
    if (!ipc_data_write_receive(&callid, &len)) {
524
    if (!ipc_data_write_receive(&callid, &len)) {
525
        ipc_answer_0(callid, EINVAL);
525
        ipc_answer_0(callid, EINVAL);
526
        ipc_answer_0(rid, EINVAL);
526
        ipc_answer_0(rid, EINVAL);
527
        return;
527
        return;
528
    }
528
    }
529
   
529
   
530
    char *path = malloc(len + 1);
530
    char *path = malloc(len + 1);
531
    if (!path) {
531
    if (!path) {
532
        ipc_answer_0(callid, ENOMEM);
532
        ipc_answer_0(callid, ENOMEM);
533
        ipc_answer_0(rid, ENOMEM);
533
        ipc_answer_0(rid, ENOMEM);
534
        return;
534
        return;
535
    }
535
    }
536
   
536
   
537
    int rc;
537
    int rc;
538
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
538
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
539
        ipc_answer_0(rid, rc);
539
        ipc_answer_0(rid, rc);
540
        free(path);
540
        free(path);
541
        return;
541
        return;
542
    }
542
    }
543
    path[len] = '\0';
543
    path[len] = '\0';
544
   
544
   
545
    /*
545
    /*
546
     * Avoid the race condition in which the file can be deleted before we
546
     * Avoid the race condition in which the file can be deleted before we
547
     * find/create-and-lock the VFS node corresponding to the looked-up
547
     * find/create-and-lock the VFS node corresponding to the looked-up
548
     * triplet.
548
     * triplet.
549
     */
549
     */
550
    if (lflag & L_CREATE)
550
    if (lflag & L_CREATE)
551
        rwlock_write_lock(&namespace_rwlock);
551
        rwlock_write_lock(&namespace_rwlock);
552
    else
552
    else
553
        rwlock_read_lock(&namespace_rwlock);
553
        rwlock_read_lock(&namespace_rwlock);
554
   
554
   
555
    /* The path is now populated and we can call vfs_lookup_internal(). */
555
    /* The path is now populated and we can call vfs_lookup_internal(). */
556
    vfs_lookup_res_t lr;
556
    vfs_lookup_res_t lr;
557
    rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
557
    rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
558
    if (rc != EOK) {
558
    if (rc != EOK) {
559
        if (lflag & L_CREATE)
559
        if (lflag & L_CREATE)
560
            rwlock_write_unlock(&namespace_rwlock);
560
            rwlock_write_unlock(&namespace_rwlock);
561
        else
561
        else
562
            rwlock_read_unlock(&namespace_rwlock);
562
            rwlock_read_unlock(&namespace_rwlock);
563
        ipc_answer_0(rid, rc);
563
        ipc_answer_0(rid, rc);
564
        free(path);
564
        free(path);
565
        return;
565
        return;
566
    }
566
    }
567
   
567
   
568
    /* Path is no longer needed. */
568
    /* Path is no longer needed. */
569
    free(path);
569
    free(path);
570
   
570
   
571
    vfs_node_t *node = vfs_node_get(&lr);
571
    vfs_node_t *node = vfs_node_get(&lr);
572
    if (lflag & L_CREATE)
572
    if (lflag & L_CREATE)
573
        rwlock_write_unlock(&namespace_rwlock);
573
        rwlock_write_unlock(&namespace_rwlock);
574
    else
574
    else
575
        rwlock_read_unlock(&namespace_rwlock);
575
        rwlock_read_unlock(&namespace_rwlock);
576
   
576
   
577
    /* Truncate the file if requested and if necessary. */
577
    /* Truncate the file if requested and if necessary. */
578
    if (oflag & O_TRUNC) {
578
    if (oflag & O_TRUNC) {
579
        rwlock_write_lock(&node->contents_rwlock);
579
        rwlock_write_lock(&node->contents_rwlock);
580
        if (node->size) {
580
        if (node->size) {
581
            rc = vfs_truncate_internal(node->fs_handle,
581
            rc = vfs_truncate_internal(node->fs_handle,
582
                node->dev_handle, node->index, 0);
582
                node->dev_handle, node->index, 0);
583
            if (rc) {
583
            if (rc) {
584
                rwlock_write_unlock(&node->contents_rwlock);
584
                rwlock_write_unlock(&node->contents_rwlock);
585
                vfs_node_put(node);
585
                vfs_node_put(node);
586
                ipc_answer_0(rid, rc);
586
                ipc_answer_0(rid, rc);
587
                return;
587
                return;
588
            }
588
            }
589
            node->size = 0;
589
            node->size = 0;
590
        }
590
        }
591
        rwlock_write_unlock(&node->contents_rwlock);
591
        rwlock_write_unlock(&node->contents_rwlock);
592
    }
592
    }
593
   
593
   
594
    /*
594
    /*
595
     * Get ourselves a file descriptor and the corresponding vfs_file_t
595
     * Get ourselves a file descriptor and the corresponding vfs_file_t
596
     * structure.
596
     * structure.
597
     */
597
     */
598
    int fd = vfs_fd_alloc();
598
    int fd = vfs_fd_alloc();
599
    if (fd < 0) {
599
    if (fd < 0) {
600
        vfs_node_put(node);
600
        vfs_node_put(node);
601
        ipc_answer_0(rid, fd);
601
        ipc_answer_0(rid, fd);
602
        return;
602
        return;
603
    }
603
    }
604
    vfs_file_t *file = vfs_file_get(fd);
604
    vfs_file_t *file = vfs_file_get(fd);
605
    file->node = node;
605
    file->node = node;
606
    if (oflag & O_APPEND)
606
    if (oflag & O_APPEND)
607
        file->append = true;
607
        file->append = true;
608
   
608
   
609
    /*
609
    /*
610
     * The following increase in reference count is for the fact that the
610
     * The following increase in reference count is for the fact that the
611
     * file is being opened and that a file structure is pointing to it.
611
     * file is being opened and that a file structure is pointing to it.
612
     * It is necessary so that the file will not disappear when
612
     * It is necessary so that the file will not disappear when
613
     * vfs_node_put() is called. The reference will be dropped by the
613
     * vfs_node_put() is called. The reference will be dropped by the
614
     * respective VFS_CLOSE.
614
     * respective VFS_CLOSE.
615
     */
615
     */
616
    vfs_node_addref(node);
616
    vfs_node_addref(node);
617
    vfs_node_put(node);
617
    vfs_node_put(node);
618
   
618
   
619
    /* Success! Return the new file descriptor to the client. */
619
    /* Success! Return the new file descriptor to the client. */
620
    ipc_answer_1(rid, EOK, fd);
620
    ipc_answer_1(rid, EOK, fd);
621
}
621
}
622
 
622
 
623
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
623
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
624
{
624
{
625
    // FIXME: check for sanity of the supplied fs, dev and index
625
    // FIXME: check for sanity of the supplied fs, dev and index
626
   
626
   
627
    if (!vfs_files_init()) {
627
    if (!vfs_files_init()) {
628
        ipc_answer_0(rid, ENOMEM);
628
        ipc_answer_0(rid, ENOMEM);
629
        return;
629
        return;
630
    }
630
    }
631
   
631
   
632
    /*
632
    /*
633
     * The interface is open_node(fs, dev, index, oflag).
633
     * The interface is open_node(fs, dev, index, oflag).
634
     */
634
     */
635
    vfs_lookup_res_t lr;
635
    vfs_lookup_res_t lr;
636
   
636
   
637
    lr.triplet.fs_handle = IPC_GET_ARG1(*request);
637
    lr.triplet.fs_handle = IPC_GET_ARG1(*request);
638
    lr.triplet.dev_handle = IPC_GET_ARG2(*request);
638
    lr.triplet.dev_handle = IPC_GET_ARG2(*request);
639
    lr.triplet.index = IPC_GET_ARG3(*request);
639
    lr.triplet.index = IPC_GET_ARG3(*request);
640
    int oflag = IPC_GET_ARG4(*request);
640
    int oflag = IPC_GET_ARG4(*request);
641
   
641
   
642
    rwlock_read_lock(&namespace_rwlock);
642
    rwlock_read_lock(&namespace_rwlock);
643
   
643
   
644
    int rc = vfs_open_node_internal(&lr);
644
    int rc = vfs_open_node_internal(&lr);
645
    if (rc != EOK) {
645
    if (rc != EOK) {
646
        rwlock_read_unlock(&namespace_rwlock);
646
        rwlock_read_unlock(&namespace_rwlock);
647
        ipc_answer_0(rid, rc);
647
        ipc_answer_0(rid, rc);
648
        return;
648
        return;
649
    }
649
    }
650
   
650
   
651
    vfs_node_t *node = vfs_node_get(&lr);
651
    vfs_node_t *node = vfs_node_get(&lr);
652
    rwlock_read_unlock(&namespace_rwlock);
652
    rwlock_read_unlock(&namespace_rwlock);
653
   
653
   
654
    /* Truncate the file if requested and if necessary. */
654
    /* Truncate the file if requested and if necessary. */
655
    if (oflag & O_TRUNC) {
655
    if (oflag & O_TRUNC) {
656
        rwlock_write_lock(&node->contents_rwlock);
656
        rwlock_write_lock(&node->contents_rwlock);
657
        if (node->size) {
657
        if (node->size) {
658
            rc = vfs_truncate_internal(node->fs_handle,
658
            rc = vfs_truncate_internal(node->fs_handle,
659
                node->dev_handle, node->index, 0);
659
                node->dev_handle, node->index, 0);
660
            if (rc) {
660
            if (rc) {
661
                rwlock_write_unlock(&node->contents_rwlock);
661
                rwlock_write_unlock(&node->contents_rwlock);
662
                vfs_node_put(node);
662
                vfs_node_put(node);
663
                ipc_answer_0(rid, rc);
663
                ipc_answer_0(rid, rc);
664
                return;
664
                return;
665
            }
665
            }
666
            node->size = 0;
666
            node->size = 0;
667
        }
667
        }
668
        rwlock_write_unlock(&node->contents_rwlock);
668
        rwlock_write_unlock(&node->contents_rwlock);
669
    }
669
    }
670
   
670
   
671
    /*
671
    /*
672
     * Get ourselves a file descriptor and the corresponding vfs_file_t
672
     * Get ourselves a file descriptor and the corresponding vfs_file_t
673
     * structure.
673
     * structure.
674
     */
674
     */
675
    int fd = vfs_fd_alloc();
675
    int fd = vfs_fd_alloc();
676
    if (fd < 0) {
676
    if (fd < 0) {
677
        vfs_node_put(node);
677
        vfs_node_put(node);
678
        ipc_answer_0(rid, fd);
678
        ipc_answer_0(rid, fd);
679
        return;
679
        return;
680
    }
680
    }
681
    vfs_file_t *file = vfs_file_get(fd);
681
    vfs_file_t *file = vfs_file_get(fd);
682
    file->node = node;
682
    file->node = node;
683
    if (oflag & O_APPEND)
683
    if (oflag & O_APPEND)
684
        file->append = true;
684
        file->append = true;
685
   
685
   
686
    /*
686
    /*
687
     * The following increase in reference count is for the fact that the
687
     * The following increase in reference count is for the fact that the
688
     * file is being opened and that a file structure is pointing to it.
688
     * file is being opened and that a file structure is pointing to it.
689
     * It is necessary so that the file will not disappear when
689
     * It is necessary so that the file will not disappear when
690
     * vfs_node_put() is called. The reference will be dropped by the
690
     * vfs_node_put() is called. The reference will be dropped by the
691
     * respective VFS_CLOSE.
691
     * respective VFS_CLOSE.
692
     */
692
     */
693
    vfs_node_addref(node);
693
    vfs_node_addref(node);
694
    vfs_node_put(node);
694
    vfs_node_put(node);
695
   
695
   
696
    /* Success! Return the new file descriptor to the client. */
696
    /* Success! Return the new file descriptor to the client. */
697
    ipc_answer_1(rid, EOK, fd);
697
    ipc_answer_1(rid, EOK, fd);
698
}
698
}
699
 
699
 
700
void vfs_node(ipc_callid_t rid, ipc_call_t *request)
700
void vfs_node(ipc_callid_t rid, ipc_call_t *request)
701
{
701
{
702
    int fd = IPC_GET_ARG1(*request);
702
    int fd = IPC_GET_ARG1(*request);
703
   
703
   
704
    /* Lookup the file structure corresponding to the file descriptor. */
704
    /* Lookup the file structure corresponding to the file descriptor. */
705
    vfs_file_t *file = vfs_file_get(fd);
705
    vfs_file_t *file = vfs_file_get(fd);
706
    if (!file) {
706
    if (!file) {
707
        ipc_answer_0(rid, ENOENT);
707
        ipc_answer_0(rid, ENOENT);
708
        return;
708
        return;
709
    }
709
    }
710
   
710
   
711
    ipc_answer_3(rid, EOK, file->node->fs_handle,
711
    ipc_answer_3(rid, EOK, file->node->fs_handle,
712
        file->node->dev_handle, file->node->index);
712
        file->node->dev_handle, file->node->index);
713
}
713
}
714
 
714
 
715
void vfs_device(ipc_callid_t rid, ipc_call_t *request)
715
void vfs_device(ipc_callid_t rid, ipc_call_t *request)
716
{
716
{
717
    int fd = IPC_GET_ARG1(*request);
717
    int fd = IPC_GET_ARG1(*request);
718
   
718
   
719
    /* Lookup the file structure corresponding to the file descriptor. */
719
    /* Lookup the file structure corresponding to the file descriptor. */
720
    vfs_file_t *file = vfs_file_get(fd);
720
    vfs_file_t *file = vfs_file_get(fd);
721
    if (!file) {
721
    if (!file) {
722
        ipc_answer_0(rid, ENOENT);
722
        ipc_answer_0(rid, ENOENT);
723
        return;
723
        return;
724
    }
724
    }
725
   
725
   
726
    /*
726
    /*
727
     * Lock the open file structure so that no other thread can manipulate
727
     * Lock the open file structure so that no other thread can manipulate
728
     * the same open file at a time.
728
     * the same open file at a time.
729
     */
729
     */
730
    futex_down(&file->lock);
730
    futex_down(&file->lock);
731
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
731
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
732
   
732
   
733
    /* Make a VFS_DEVICE request at the destination FS server. */
733
    /* Make a VFS_DEVICE request at the destination FS server. */
734
    aid_t msg;
734
    aid_t msg;
735
    ipc_call_t answer;
735
    ipc_call_t answer;
736
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
736
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
737
        file->node->dev_handle, file->node->index, &answer);
737
        file->node->dev_handle, file->node->index, &answer);
738
   
738
   
739
    /* Wait for reply from the FS server. */
739
    /* Wait for reply from the FS server. */
740
    ipcarg_t rc;
740
    ipcarg_t rc;
741
    async_wait_for(msg, &rc);
741
    async_wait_for(msg, &rc);
742
   
742
   
743
    vfs_release_phone(fs_phone);
743
    vfs_release_phone(fs_phone);
744
    futex_up(&file->lock);
744
    futex_up(&file->lock);
745
   
745
   
746
    ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer));
746
    ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer));
747
}
747
}
748
 
748
 
749
void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
749
void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
750
{
750
{
751
    int fd = IPC_GET_ARG1(*request);
751
    int fd = IPC_GET_ARG1(*request);
752
   
752
   
753
    /* Lookup the file structure corresponding to the file descriptor. */
753
    /* Lookup the file structure corresponding to the file descriptor. */
754
    vfs_file_t *file = vfs_file_get(fd);
754
    vfs_file_t *file = vfs_file_get(fd);
755
    if (!file) {
755
    if (!file) {
756
        ipc_answer_0(rid, ENOENT);
756
        ipc_answer_0(rid, ENOENT);
757
        return;
757
        return;
758
    }
758
    }
759
   
759
   
760
    /*
760
    /*
761
     * Lock the open file structure so that no other thread can manipulate
761
     * Lock the open file structure so that no other thread can manipulate
762
     * the same open file at a time.
762
     * the same open file at a time.
763
     */
763
     */
764
    futex_down(&file->lock);
764
    futex_down(&file->lock);
765
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
765
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
766
   
766
   
767
    /* Make a VFS_SYMC request at the destination FS server. */
767
    /* Make a VFS_SYMC request at the destination FS server. */
768
    aid_t msg;
768
    aid_t msg;
769
    ipc_call_t answer;
769
    ipc_call_t answer;
770
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
770
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
771
        file->node->dev_handle, file->node->index, &answer);
771
        file->node->dev_handle, file->node->index, &answer);
772
   
772
   
773
    /* Wait for reply from the FS server. */
773
    /* Wait for reply from the FS server. */
774
    ipcarg_t rc;
774
    ipcarg_t rc;
775
    async_wait_for(msg, &rc);
775
    async_wait_for(msg, &rc);
776
   
776
   
777
    vfs_release_phone(fs_phone);
777
    vfs_release_phone(fs_phone);
778
    futex_up(&file->lock);
778
    futex_up(&file->lock);
779
   
779
   
780
    ipc_answer_0(rid, rc);
780
    ipc_answer_0(rid, rc);
781
}
781
}
782
 
782
 
783
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
783
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
784
{
784
{
785
    int fd = IPC_GET_ARG1(*request);
785
    int fd = IPC_GET_ARG1(*request);
786
   
786
   
787
    /* Lookup the file structure corresponding to the file descriptor. */
787
    /* Lookup the file structure corresponding to the file descriptor. */
788
    vfs_file_t *file = vfs_file_get(fd);
788
    vfs_file_t *file = vfs_file_get(fd);
789
    if (!file) {
789
    if (!file) {
790
        ipc_answer_0(rid, ENOENT);
790
        ipc_answer_0(rid, ENOENT);
791
        return;
791
        return;
792
    }
792
    }
793
   
793
   
794
    /*
794
    /*
795
     * Lock the open file structure so that no other thread can manipulate
795
     * Lock the open file structure so that no other thread can manipulate
796
     * the same open file at a time.
796
     * the same open file at a time.
797
     */
797
     */
798
    futex_down(&file->lock);
798
    futex_down(&file->lock);
799
   
799
   
800
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
800
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
801
   
801
   
802
    /* Make a VFS_CLOSE request at the destination FS server. */
802
    /* Make a VFS_CLOSE request at the destination FS server. */
803
    aid_t msg;
803
    aid_t msg;
804
    ipc_call_t answer;
804
    ipc_call_t answer;
805
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
805
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
806
        file->node->dev_handle, file->node->index, &answer);
806
        file->node->dev_handle, file->node->index, &answer);
807
   
807
   
808
    /* Wait for reply from the FS server. */
808
    /* Wait for reply from the FS server. */
809
    ipcarg_t rc;
809
    ipcarg_t rc;
810
    async_wait_for(msg, &rc);
810
    async_wait_for(msg, &rc);
811
   
811
   
812
    vfs_release_phone(fs_phone);
812
    vfs_release_phone(fs_phone);
813
    futex_up(&file->lock);
813
    futex_up(&file->lock);
814
   
814
   
815
    int retval = IPC_GET_ARG1(answer);
815
    int retval = IPC_GET_ARG1(answer);
816
    if (retval != EOK)
816
    if (retval != EOK)
817
        ipc_answer_0(rid, retval);
817
        ipc_answer_0(rid, retval);
818
   
818
   
819
    retval = vfs_fd_free(fd);
819
    retval = vfs_fd_free(fd);
820
    ipc_answer_0(rid, retval);
820
    ipc_answer_0(rid, retval);
821
}
821
}
822
 
822
 
823
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
823
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
824
{
824
{
825
 
825
 
826
    /*
826
    /*
827
     * The following code strongly depends on the fact that the files data
827
     * The following code strongly depends on the fact that the files data
828
     * structure can be only accessed by a single fibril and all file
828
     * structure can be only accessed by a single fibril and all file
829
     * operations are serialized (i.e. the reads and writes cannot
829
     * operations are serialized (i.e. the reads and writes cannot
830
     * interleave and a file cannot be closed while it is being read).
830
     * interleave and a file cannot be closed while it is being read).
831
     *
831
     *
832
     * Additional synchronization needs to be added once the table of
832
     * Additional synchronization needs to be added once the table of
833
     * open files supports parallel access!
833
     * open files supports parallel access!
834
     */
834
     */
835
 
835
 
836
    int fd = IPC_GET_ARG1(*request);
836
    int fd = IPC_GET_ARG1(*request);
837
   
837
   
838
    /* Lookup the file structure corresponding to the file descriptor. */
838
    /* Lookup the file structure corresponding to the file descriptor. */
839
    vfs_file_t *file = vfs_file_get(fd);
839
    vfs_file_t *file = vfs_file_get(fd);
840
    if (!file) {
840
    if (!file) {
841
        ipc_answer_0(rid, ENOENT);
841
        ipc_answer_0(rid, ENOENT);
842
        return;
842
        return;
843
    }
843
    }
844
   
844
   
845
    /*
845
    /*
846
     * Now we need to receive a call with client's
846
     * Now we need to receive a call with client's
847
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
847
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
848
     */
848
     */
849
    ipc_callid_t callid;
849
    ipc_callid_t callid;
850
    int res;
850
    int res;
851
    if (read)
851
    if (read)
852
        res = ipc_data_read_receive(&callid, NULL);
852
        res = ipc_data_read_receive(&callid, NULL);
853
    else
853
    else
854
        res = ipc_data_write_receive(&callid, NULL);
854
        res = ipc_data_write_receive(&callid, NULL);
855
    if (!res) {
855
    if (!res) {
856
        ipc_answer_0(callid, EINVAL);
856
        ipc_answer_0(callid, EINVAL);
857
        ipc_answer_0(rid, EINVAL);
857
        ipc_answer_0(rid, EINVAL);
858
        return;
858
        return;
859
    }
859
    }
860
   
860
   
861
    /*
861
    /*
862
     * Lock the open file structure so that no other thread can manipulate
862
     * Lock the open file structure so that no other thread can manipulate
863
     * the same open file at a time.
863
     * the same open file at a time.
864
     */
864
     */
865
    futex_down(&file->lock);
865
    futex_down(&file->lock);
866
 
866
 
867
    /*
867
    /*
868
     * Lock the file's node so that no other client can read/write to it at
868
     * Lock the file's node so that no other client can read/write to it at
869
     * the same time.
869
     * the same time.
870
     */
870
     */
871
    if (read)
871
    if (read)
872
        rwlock_read_lock(&file->node->contents_rwlock);
872
        rwlock_read_lock(&file->node->contents_rwlock);
873
    else
873
    else
874
        rwlock_write_lock(&file->node->contents_rwlock);
874
        rwlock_write_lock(&file->node->contents_rwlock);
875
 
875
 
876
    if (file->node->type == VFS_NODE_DIRECTORY) {
876
    if (file->node->type == VFS_NODE_DIRECTORY) {
877
        /*
877
        /*
878
         * Make sure that no one is modifying the namespace
878
         * Make sure that no one is modifying the namespace
879
         * while we are in readdir().
879
         * while we are in readdir().
880
         */
880
         */
881
        assert(read);
881
        assert(read);
882
        rwlock_read_lock(&namespace_rwlock);
882
        rwlock_read_lock(&namespace_rwlock);
883
    }
883
    }
884
   
884
   
885
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
885
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
886
   
886
   
887
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
887
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
888
    aid_t msg;
888
    aid_t msg;
889
    ipc_call_t answer;
889
    ipc_call_t answer;
890
    if (!read && file->append)
890
    if (!read && file->append)
891
        file->pos = file->node->size;
891
        file->pos = file->node->size;
892
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
892
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
893
        file->node->dev_handle, file->node->index, file->pos, &answer);
893
        file->node->dev_handle, file->node->index, file->pos, &answer);
894
   
894
   
895
    /*
895
    /*
896
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
896
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
897
     * destination FS server. The call will be routed as if sent by
897
     * destination FS server. The call will be routed as if sent by
898
     * ourselves. Note that call arguments are immutable in this case so we
898
     * ourselves. Note that call arguments are immutable in this case so we
899
     * don't have to bother.
899
     * don't have to bother.
900
     */
900
     */
901
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
901
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
902
   
902
   
903
    /* Wait for reply from the FS server. */
903
    /* Wait for reply from the FS server. */
904
    ipcarg_t rc;
904
    ipcarg_t rc;
905
    async_wait_for(msg, &rc);
905
    async_wait_for(msg, &rc);
906
   
906
   
907
    vfs_release_phone(fs_phone);
907
    vfs_release_phone(fs_phone);
908
   
908
   
909
    size_t bytes = IPC_GET_ARG1(answer);
909
    size_t bytes = IPC_GET_ARG1(answer);
910
 
910
 
911
    if (file->node->type == VFS_NODE_DIRECTORY)
911
    if (file->node->type == VFS_NODE_DIRECTORY)
912
        rwlock_read_unlock(&namespace_rwlock);
912
        rwlock_read_unlock(&namespace_rwlock);
913
   
913
   
914
    /* Unlock the VFS node. */
914
    /* Unlock the VFS node. */
915
    if (read)
915
    if (read)
916
        rwlock_read_unlock(&file->node->contents_rwlock);
916
        rwlock_read_unlock(&file->node->contents_rwlock);
917
    else {
917
    else {
918
        /* Update the cached version of node's size. */
918
        /* Update the cached version of node's size. */
919
        if (rc == EOK)
919
        if (rc == EOK)
920
            file->node->size = IPC_GET_ARG2(answer);
920
            file->node->size = IPC_GET_ARG2(answer);
921
        rwlock_write_unlock(&file->node->contents_rwlock);
921
        rwlock_write_unlock(&file->node->contents_rwlock);
922
    }
922
    }
923
   
923
   
924
    /* Update the position pointer and unlock the open file. */
924
    /* Update the position pointer and unlock the open file. */
925
    if (rc == EOK)
925
    if (rc == EOK)
926
        file->pos += bytes;
926
        file->pos += bytes;
927
    futex_up(&file->lock);
927
    futex_up(&file->lock);
928
   
928
   
929
    /*
929
    /*
930
     * FS server's reply is the final result of the whole operation we
930
     * FS server's reply is the final result of the whole operation we
931
     * return to the client.
931
     * return to the client.
932
     */
932
     */
933
    ipc_answer_1(rid, rc, bytes);
933
    ipc_answer_1(rid, rc, bytes);
934
}
934
}
935
 
935
 
936
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
936
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
937
{
937
{
938
    vfs_rdwr(rid, request, true);
938
    vfs_rdwr(rid, request, true);
939
}
939
}
940
 
940
 
941
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
941
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
942
{
942
{
943
    vfs_rdwr(rid, request, false);
943
    vfs_rdwr(rid, request, false);
944
}
944
}
945
 
945
 
946
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
946
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
947
{
947
{
948
    int fd = (int) IPC_GET_ARG1(*request);
948
    int fd = (int) IPC_GET_ARG1(*request);
949
    off_t off = (off_t) IPC_GET_ARG2(*request);
949
    off_t off = (off_t) IPC_GET_ARG2(*request);
950
    int whence = (int) IPC_GET_ARG3(*request);
950
    int whence = (int) IPC_GET_ARG3(*request);
951
 
951
 
952
 
952
 
953
    /* Lookup the file structure corresponding to the file descriptor. */
953
    /* Lookup the file structure corresponding to the file descriptor. */
954
    vfs_file_t *file = vfs_file_get(fd);
954
    vfs_file_t *file = vfs_file_get(fd);
955
    if (!file) {
955
    if (!file) {
956
        ipc_answer_0(rid, ENOENT);
956
        ipc_answer_0(rid, ENOENT);
957
        return;
957
        return;
958
    }
958
    }
959
 
959
 
960
    off_t newpos;
960
    off_t newpos;
961
    futex_down(&file->lock);
961
    futex_down(&file->lock);
962
    if (whence == SEEK_SET) {
962
    if (whence == SEEK_SET) {
963
        file->pos = off;
963
        file->pos = off;
964
        futex_up(&file->lock);
964
        futex_up(&file->lock);
965
        ipc_answer_1(rid, EOK, off);
965
        ipc_answer_1(rid, EOK, off);
966
        return;
966
        return;
967
    }
967
    }
968
    if (whence == SEEK_CUR) {
968
    if (whence == SEEK_CUR) {
969
        if (file->pos + off < file->pos) {
969
        if (file->pos + off < file->pos) {
970
            futex_up(&file->lock);
970
            futex_up(&file->lock);
971
            ipc_answer_0(rid, EOVERFLOW);
971
            ipc_answer_0(rid, EOVERFLOW);
972
            return;
972
            return;
973
        }
973
        }
974
        file->pos += off;
974
        file->pos += off;
975
        newpos = file->pos;
975
        newpos = file->pos;
976
        futex_up(&file->lock);
976
        futex_up(&file->lock);
977
        ipc_answer_1(rid, EOK, newpos);
977
        ipc_answer_1(rid, EOK, newpos);
978
        return;
978
        return;
979
    }
979
    }
980
    if (whence == SEEK_END) {
980
    if (whence == SEEK_END) {
981
        rwlock_read_lock(&file->node->contents_rwlock);
981
        rwlock_read_lock(&file->node->contents_rwlock);
982
        size_t size = file->node->size;
982
        size_t size = file->node->size;
983
        rwlock_read_unlock(&file->node->contents_rwlock);
983
        rwlock_read_unlock(&file->node->contents_rwlock);
984
        if (size + off < size) {
984
        if (size + off < size) {
985
            futex_up(&file->lock);
985
            futex_up(&file->lock);
986
            ipc_answer_0(rid, EOVERFLOW);
986
            ipc_answer_0(rid, EOVERFLOW);
987
            return;
987
            return;
988
        }
988
        }
989
        newpos = size + off;
989
        newpos = size + off;
990
        futex_up(&file->lock);
990
        futex_up(&file->lock);
991
        ipc_answer_1(rid, EOK, newpos);
991
        ipc_answer_1(rid, EOK, newpos);
992
        return;
992
        return;
993
    }
993
    }
994
    futex_up(&file->lock);
994
    futex_up(&file->lock);
995
    ipc_answer_0(rid, EINVAL);
995
    ipc_answer_0(rid, EINVAL);
996
}
996
}
997
 
997
 
998
int
998
int
999
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
999
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
1000
    fs_index_t index, size_t size)
1000
    fs_index_t index, size_t size)
1001
{
1001
{
1002
    ipcarg_t rc;
1002
    ipcarg_t rc;
1003
    int fs_phone;
1003
    int fs_phone;
1004
   
1004
   
1005
    fs_phone = vfs_grab_phone(fs_handle);
1005
    fs_phone = vfs_grab_phone(fs_handle);
1006
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
1006
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
1007
        (ipcarg_t)index, (ipcarg_t)size);
1007
        (ipcarg_t)index, (ipcarg_t)size);
1008
    vfs_release_phone(fs_phone);
1008
    vfs_release_phone(fs_phone);
1009
    return (int)rc;
1009
    return (int)rc;
1010
}
1010
}
1011
 
1011
 
1012
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
1012
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
1013
{
1013
{
1014
    int fd = IPC_GET_ARG1(*request);
1014
    int fd = IPC_GET_ARG1(*request);
1015
    size_t size = IPC_GET_ARG2(*request);
1015
    size_t size = IPC_GET_ARG2(*request);
1016
    int rc;
1016
    int rc;
1017
 
1017
 
1018
    vfs_file_t *file = vfs_file_get(fd);
1018
    vfs_file_t *file = vfs_file_get(fd);
1019
    if (!file) {
1019
    if (!file) {
1020
        ipc_answer_0(rid, ENOENT);
1020
        ipc_answer_0(rid, ENOENT);
1021
        return;
1021
        return;
1022
    }
1022
    }
1023
    futex_down(&file->lock);
1023
    futex_down(&file->lock);
1024
 
1024
 
1025
    rwlock_write_lock(&file->node->contents_rwlock);
1025
    rwlock_write_lock(&file->node->contents_rwlock);
1026
    rc = vfs_truncate_internal(file->node->fs_handle,
1026
    rc = vfs_truncate_internal(file->node->fs_handle,
1027
        file->node->dev_handle, file->node->index, size);
1027
        file->node->dev_handle, file->node->index, size);
1028
    if (rc == EOK)
1028
    if (rc == EOK)
1029
        file->node->size = size;
1029
        file->node->size = size;
1030
    rwlock_write_unlock(&file->node->contents_rwlock);
1030
    rwlock_write_unlock(&file->node->contents_rwlock);
1031
 
1031
 
1032
    futex_up(&file->lock);
1032
    futex_up(&file->lock);
1033
    ipc_answer_0(rid, (ipcarg_t)rc);
1033
    ipc_answer_0(rid, (ipcarg_t)rc);
1034
}
1034
}
1035
 
1035
 
1036
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1036
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1037
{
1037
{
1038
    int mode = IPC_GET_ARG1(*request);
1038
    int mode = IPC_GET_ARG1(*request);
1039
 
1039
 
1040
    size_t len;
1040
    size_t len;
1041
    ipc_callid_t callid;
1041
    ipc_callid_t callid;
1042
 
1042
 
1043
    if (!ipc_data_write_receive(&callid, &len)) {
1043
    if (!ipc_data_write_receive(&callid, &len)) {
1044
        ipc_answer_0(callid, EINVAL);
1044
        ipc_answer_0(callid, EINVAL);
1045
        ipc_answer_0(rid, EINVAL);
1045
        ipc_answer_0(rid, EINVAL);
1046
        return;
1046
        return;
1047
    }
1047
    }
1048
    char *path = malloc(len + 1);
1048
    char *path = malloc(len + 1);
1049
    if (!path) {
1049
    if (!path) {
1050
        ipc_answer_0(callid, ENOMEM);
1050
        ipc_answer_0(callid, ENOMEM);
1051
        ipc_answer_0(rid, ENOMEM);
1051
        ipc_answer_0(rid, ENOMEM);
1052
        return;
1052
        return;
1053
    }
1053
    }
1054
    int rc;
1054
    int rc;
1055
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1055
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1056
        ipc_answer_0(rid, rc);
1056
        ipc_answer_0(rid, rc);
1057
        free(path);
1057
        free(path);
1058
        return;
1058
        return;
1059
    }
1059
    }
1060
    path[len] = '\0';
1060
    path[len] = '\0';
1061
   
1061
   
1062
    rwlock_write_lock(&namespace_rwlock);
1062
    rwlock_write_lock(&namespace_rwlock);
1063
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
1063
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
1064
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
1064
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
1065
    rwlock_write_unlock(&namespace_rwlock);
1065
    rwlock_write_unlock(&namespace_rwlock);
1066
    free(path);
1066
    free(path);
1067
    ipc_answer_0(rid, rc);
1067
    ipc_answer_0(rid, rc);
1068
}
1068
}
1069
 
1069
 
1070
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1070
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1071
{
1071
{
1072
    int lflag = IPC_GET_ARG1(*request);
1072
    int lflag = IPC_GET_ARG1(*request);
1073
 
1073
 
1074
    size_t len;
1074
    size_t len;
1075
    ipc_callid_t callid;
1075
    ipc_callid_t callid;
1076
 
1076
 
1077
    if (!ipc_data_write_receive(&callid, &len)) {
1077
    if (!ipc_data_write_receive(&callid, &len)) {
1078
        ipc_answer_0(callid, EINVAL);
1078
        ipc_answer_0(callid, EINVAL);
1079
        ipc_answer_0(rid, EINVAL);
1079
        ipc_answer_0(rid, EINVAL);
1080
        return;
1080
        return;
1081
    }
1081
    }
1082
    char *path = malloc(len + 1);
1082
    char *path = malloc(len + 1);
1083
    if (!path) {
1083
    if (!path) {
1084
        ipc_answer_0(callid, ENOMEM);
1084
        ipc_answer_0(callid, ENOMEM);
1085
        ipc_answer_0(rid, ENOMEM);
1085
        ipc_answer_0(rid, ENOMEM);
1086
        return;
1086
        return;
1087
    }
1087
    }
1088
    int rc;
1088
    int rc;
1089
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1089
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1090
        ipc_answer_0(rid, rc);
1090
        ipc_answer_0(rid, rc);
1091
        free(path);
1091
        free(path);
1092
        return;
1092
        return;
1093
    }
1093
    }
1094
    path[len] = '\0';
1094
    path[len] = '\0';
1095
   
1095
   
1096
    rwlock_write_lock(&namespace_rwlock);
1096
    rwlock_write_lock(&namespace_rwlock);
1097
    lflag &= L_DIRECTORY;   /* sanitize lflag */
1097
    lflag &= L_DIRECTORY;   /* sanitize lflag */
1098
    vfs_lookup_res_t lr;
1098
    vfs_lookup_res_t lr;
1099
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1099
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1100
    free(path);
1100
    free(path);
1101
    if (rc != EOK) {
1101
    if (rc != EOK) {
1102
        rwlock_write_unlock(&namespace_rwlock);
1102
        rwlock_write_unlock(&namespace_rwlock);
1103
        ipc_answer_0(rid, rc);
1103
        ipc_answer_0(rid, rc);
1104
        return;
1104
        return;
1105
    }
1105
    }
1106
 
1106
 
1107
    /*
1107
    /*
1108
     * The name has already been unlinked by vfs_lookup_internal().
1108
     * The name has already been unlinked by vfs_lookup_internal().
1109
     * We have to get and put the VFS node to ensure that it is
1109
     * We have to get and put the VFS node to ensure that it is
1110
     * VFS_DESTROY'ed after the last reference to it is dropped.
1110
     * VFS_DESTROY'ed after the last reference to it is dropped.
1111
     */
1111
     */
1112
    vfs_node_t *node = vfs_node_get(&lr);
1112
    vfs_node_t *node = vfs_node_get(&lr);
1113
    futex_down(&nodes_futex);
1113
    futex_down(&nodes_futex);
1114
    node->lnkcnt--;
1114
    node->lnkcnt--;
1115
    futex_up(&nodes_futex);
1115
    futex_up(&nodes_futex);
1116
    rwlock_write_unlock(&namespace_rwlock);
1116
    rwlock_write_unlock(&namespace_rwlock);
1117
    vfs_node_put(node);
1117
    vfs_node_put(node);
1118
    ipc_answer_0(rid, EOK);
1118
    ipc_answer_0(rid, EOK);
1119
}
1119
}
1120
 
1120
 
1121
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1121
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1122
{
1122
{
1123
    size_t olen, nlen;
1123
    size_t olen, nlen;
1124
    ipc_callid_t callid;
1124
    ipc_callid_t callid;
1125
    int rc;
1125
    int rc;
1126
 
1126
 
1127
    /* Retrieve the old path. */
1127
    /* Retrieve the old path. */
1128
    if (!ipc_data_write_receive(&callid, &olen)) {
1128
    if (!ipc_data_write_receive(&callid, &olen)) {
1129
        ipc_answer_0(callid, EINVAL);
1129
        ipc_answer_0(callid, EINVAL);
1130
        ipc_answer_0(rid, EINVAL);
1130
        ipc_answer_0(rid, EINVAL);
1131
        return;
1131
        return;
1132
    }
1132
    }
1133
    char *old = malloc(olen + 1);
1133
    char *old = malloc(olen + 1);
1134
    if (!old) {
1134
    if (!old) {
1135
        ipc_answer_0(callid, ENOMEM);
1135
        ipc_answer_0(callid, ENOMEM);
1136
        ipc_answer_0(rid, ENOMEM);
1136
        ipc_answer_0(rid, ENOMEM);
1137
        return;
1137
        return;
1138
    }
1138
    }
1139
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
1139
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
1140
        ipc_answer_0(rid, rc);
1140
        ipc_answer_0(rid, rc);
1141
        free(old);
1141
        free(old);
1142
        return;
1142
        return;
1143
    }
1143
    }
1144
    old[olen] = '\0';
1144
    old[olen] = '\0';
1145
   
1145
   
1146
    /* Retrieve the new path. */
1146
    /* Retrieve the new path. */
1147
    if (!ipc_data_write_receive(&callid, &nlen)) {
1147
    if (!ipc_data_write_receive(&callid, &nlen)) {
1148
        ipc_answer_0(callid, EINVAL);
1148
        ipc_answer_0(callid, EINVAL);
1149
        ipc_answer_0(rid, EINVAL);
1149
        ipc_answer_0(rid, EINVAL);
1150
        free(old);
1150
        free(old);
1151
        return;
1151
        return;
1152
    }
1152
    }
1153
    char *new = malloc(nlen + 1);
1153
    char *new = malloc(nlen + 1);
1154
    if (!new) {
1154
    if (!new) {
1155
        ipc_answer_0(callid, ENOMEM);
1155
        ipc_answer_0(callid, ENOMEM);
1156
        ipc_answer_0(rid, ENOMEM);
1156
        ipc_answer_0(rid, ENOMEM);
1157
        free(old);
1157
        free(old);
1158
        return;
1158
        return;
1159
    }
1159
    }
1160
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
1160
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
1161
        ipc_answer_0(rid, rc);
1161
        ipc_answer_0(rid, rc);
1162
        free(old);
1162
        free(old);
1163
        free(new);
1163
        free(new);
1164
        return;
1164
        return;
1165
    }
1165
    }
1166
    new[nlen] = '\0';
1166
    new[nlen] = '\0';
1167
 
1167
 
1168
    char *oldc = canonify(old, &olen);
1168
    char *oldc = canonify(old, &olen);
1169
    char *newc = canonify(new, &nlen);
1169
    char *newc = canonify(new, &nlen);
1170
    if (!oldc || !newc) {
1170
    if (!oldc || !newc) {
1171
        ipc_answer_0(rid, EINVAL);
1171
        ipc_answer_0(rid, EINVAL);
1172
        free(old);
1172
        free(old);
1173
        free(new);
1173
        free(new);
1174
        return;
1174
        return;
1175
    }
1175
    }
1176
    oldc[olen] = '\0';
1176
    oldc[olen] = '\0';
1177
    newc[nlen] = '\0';
1177
    newc[nlen] = '\0';
1178
    if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1178
    if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1179
        ((newc[str_length(oldc)] == '/') ||
1179
        ((newc[str_length(oldc)] == '/') ||
1180
        (str_length(oldc) == 1) ||
1180
        (str_length(oldc) == 1) ||
1181
        (str_length(oldc) == str_length(newc)))) {
1181
        (str_length(oldc) == str_length(newc)))) {
1182
            /*
1182
            /*
1183
         * oldc is a prefix of newc and either
1183
         * oldc is a prefix of newc and either
1184
         * - newc continues with a / where oldc ends, or
1184
         * - newc continues with a / where oldc ends, or
1185
         * - oldc was / itself, or
1185
         * - oldc was / itself, or
1186
         * - oldc and newc are equal.
1186
         * - oldc and newc are equal.
1187
         */
1187
         */
1188
        ipc_answer_0(rid, EINVAL);
1188
        ipc_answer_0(rid, EINVAL);
1189
        free(old);
1189
        free(old);
1190
        free(new);
1190
        free(new);
1191
        return;
1191
        return;
1192
    }
1192
    }
1193
   
1193
   
1194
    vfs_lookup_res_t old_lr;
1194
    vfs_lookup_res_t old_lr;
1195
    vfs_lookup_res_t new_lr;
1195
    vfs_lookup_res_t new_lr;
1196
    vfs_lookup_res_t new_par_lr;
1196
    vfs_lookup_res_t new_par_lr;
1197
    rwlock_write_lock(&namespace_rwlock);
1197
    rwlock_write_lock(&namespace_rwlock);
1198
    /* Lookup the node belonging to the old file name. */
1198
    /* Lookup the node belonging to the old file name. */
1199
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1199
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1200
    if (rc != EOK) {
1200
    if (rc != EOK) {
1201
        rwlock_write_unlock(&namespace_rwlock);
1201
        rwlock_write_unlock(&namespace_rwlock);
1202
        ipc_answer_0(rid, rc);
1202
        ipc_answer_0(rid, rc);
1203
        free(old);
1203
        free(old);
1204
        free(new);
1204
        free(new);
1205
        return;
1205
        return;
1206
    }
1206
    }
1207
    vfs_node_t *old_node = vfs_node_get(&old_lr);
1207
    vfs_node_t *old_node = vfs_node_get(&old_lr);
1208
    if (!old_node) {
1208
    if (!old_node) {
1209
        rwlock_write_unlock(&namespace_rwlock);
1209
        rwlock_write_unlock(&namespace_rwlock);
1210
        ipc_answer_0(rid, ENOMEM);
1210
        ipc_answer_0(rid, ENOMEM);
1211
        free(old);
1211
        free(old);
1212
        free(new);
1212
        free(new);
1213
        return;
1213
        return;
1214
    }
1214
    }
1215
    /* Determine the path to the parent of the node with the new name. */
1215
    /* Determine the path to the parent of the node with the new name. */
1216
    char *parentc = str_dup(newc);
1216
    char *parentc = str_dup(newc);
1217
    if (!parentc) {
1217
    if (!parentc) {
1218
        rwlock_write_unlock(&namespace_rwlock);
1218
        rwlock_write_unlock(&namespace_rwlock);
1219
        ipc_answer_0(rid, rc);
1219
        ipc_answer_0(rid, rc);
1220
        free(old);
1220
        free(old);
1221
        free(new);
1221
        free(new);
1222
        return;
1222
        return;
1223
    }
1223
    }
1224
    char *lastsl = str_rchr(parentc + 1, '/');
1224
    char *lastsl = str_rchr(parentc + 1, '/');
1225
    if (lastsl)
1225
    if (lastsl)
1226
        *lastsl = '\0';
1226
        *lastsl = '\0';
1227
    else
1227
    else
1228
        parentc[1] = '\0';
1228
        parentc[1] = '\0';
1229
    /* Lookup parent of the new file name. */
1229
    /* Lookup parent of the new file name. */
1230
    rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1230
    rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1231
    free(parentc);  /* not needed anymore */
1231
    free(parentc);  /* not needed anymore */
1232
    if (rc != EOK) {
1232
    if (rc != EOK) {
1233
        rwlock_write_unlock(&namespace_rwlock);
1233
        rwlock_write_unlock(&namespace_rwlock);
1234
        ipc_answer_0(rid, rc);
1234
        ipc_answer_0(rid, rc);
1235
        free(old);
1235
        free(old);
1236
        free(new);
1236
        free(new);
1237
        return;
1237
        return;
1238
    }
1238
    }
1239
    /* Check whether linking to the same file system instance. */
1239
    /* Check whether linking to the same file system instance. */
1240
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1240
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1241
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
1241
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
1242
        rwlock_write_unlock(&namespace_rwlock);
1242
        rwlock_write_unlock(&namespace_rwlock);
1243
        ipc_answer_0(rid, EXDEV);   /* different file systems */
1243
        ipc_answer_0(rid, EXDEV);   /* different file systems */
1244
        free(old);
1244
        free(old);
1245
        free(new);
1245
        free(new);
1246
        return;
1246
        return;
1247
    }
1247
    }
1248
    /* Destroy the old link for the new name. */
1248
    /* Destroy the old link for the new name. */
1249
    vfs_node_t *new_node = NULL;
1249
    vfs_node_t *new_node = NULL;
1250
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1250
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1251
    switch (rc) {
1251
    switch (rc) {
1252
    case ENOENT:
1252
    case ENOENT:
1253
        /* simply not in our way */
1253
        /* simply not in our way */
1254
        break;
1254
        break;
1255
    case EOK:
1255
    case EOK:
1256
        new_node = vfs_node_get(&new_lr);
1256
        new_node = vfs_node_get(&new_lr);
1257
        if (!new_node) {
1257
        if (!new_node) {
1258
            rwlock_write_unlock(&namespace_rwlock);
1258
            rwlock_write_unlock(&namespace_rwlock);
1259
            ipc_answer_0(rid, ENOMEM);
1259
            ipc_answer_0(rid, ENOMEM);
1260
            free(old);
1260
            free(old);
1261
            free(new);
1261
            free(new);
1262
            return;
1262
            return;
1263
        }
1263
        }
1264
        futex_down(&nodes_futex);
1264
        futex_down(&nodes_futex);
1265
        new_node->lnkcnt--;
1265
        new_node->lnkcnt--;
1266
        futex_up(&nodes_futex);
1266
        futex_up(&nodes_futex);
1267
        break;
1267
        break;
1268
    default:
1268
    default:
1269
        rwlock_write_unlock(&namespace_rwlock);
1269
        rwlock_write_unlock(&namespace_rwlock);
1270
        ipc_answer_0(rid, ENOTEMPTY);
1270
        ipc_answer_0(rid, ENOTEMPTY);
1271
        free(old);
1271
        free(old);
1272
        free(new);
1272
        free(new);
1273
        return;
1273
        return;
1274
    }
1274
    }
1275
    /* Create the new link for the new name. */
1275
    /* Create the new link for the new name. */
1276
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1276
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1277
    if (rc != EOK) {
1277
    if (rc != EOK) {
1278
        rwlock_write_unlock(&namespace_rwlock);
1278
        rwlock_write_unlock(&namespace_rwlock);
1279
        if (new_node)
1279
        if (new_node)
1280
            vfs_node_put(new_node);
1280
            vfs_node_put(new_node);
1281
        ipc_answer_0(rid, rc);
1281
        ipc_answer_0(rid, rc);
1282
        free(old);
1282
        free(old);
1283
        free(new);
1283
        free(new);
1284
        return;
1284
        return;
1285
    }
1285
    }
1286
    futex_down(&nodes_futex);
1286
    futex_down(&nodes_futex);
1287
    old_node->lnkcnt++;
1287
    old_node->lnkcnt++;
1288
    futex_up(&nodes_futex);
1288
    futex_up(&nodes_futex);
1289
    /* Destroy the link for the old name. */
1289
    /* Destroy the link for the old name. */
1290
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1290
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1291
    if (rc != EOK) {
1291
    if (rc != EOK) {
1292
        rwlock_write_unlock(&namespace_rwlock);
1292
        rwlock_write_unlock(&namespace_rwlock);
1293
        vfs_node_put(old_node);
1293
        vfs_node_put(old_node);
1294
        if (new_node)
1294
        if (new_node)
1295
            vfs_node_put(new_node);
1295
            vfs_node_put(new_node);
1296
        ipc_answer_0(rid, rc);
1296
        ipc_answer_0(rid, rc);
1297
        free(old);
1297
        free(old);
1298
        free(new);
1298
        free(new);
1299
        return;
1299
        return;
1300
    }
1300
    }
1301
    futex_down(&nodes_futex);
1301
    futex_down(&nodes_futex);
1302
    old_node->lnkcnt--;
1302
    old_node->lnkcnt--;
1303
    futex_up(&nodes_futex);
1303
    futex_up(&nodes_futex);
1304
    rwlock_write_unlock(&namespace_rwlock);
1304
    rwlock_write_unlock(&namespace_rwlock);
1305
    vfs_node_put(old_node);
1305
    vfs_node_put(old_node);
1306
    if (new_node)
1306
    if (new_node)
1307
        vfs_node_put(new_node);
1307
        vfs_node_put(new_node);
1308
    free(old);
1308
    free(old);
1309
    free(new);
1309
    free(new);
1310
    ipc_answer_0(rid, EOK);
1310
    ipc_answer_0(rid, EOK);
1311
}
1311
}
1312
 
1312
 
1313
/**
1313
/**
1314
 * @}
1314
 * @}
1315
 */
1315
 */
1316
 
1316