Subversion Repositories HelenOS

Rev

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

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