Subversion Repositories HelenOS

Rev

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

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