Subversion Repositories HelenOS

Rev

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

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