Subversion Repositories HelenOS

Rev

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

Rev 3153 Rev 3219
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    vfs_ops.c
34
 * @file    vfs_ops.c
35
 * @brief   Operations that VFS offers to its clients.
35
 * @brief   Operations that VFS offers to its clients.
36
 */
36
 */
37
 
37
 
38
#include "vfs.h"
38
#include "vfs.h"
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <async.h>
40
#include <async.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <stdlib.h>
43
#include <stdlib.h>
44
#include <string.h>
44
#include <string.h>
45
#include <bool.h>
45
#include <bool.h>
46
#include <futex.h>
46
#include <futex.h>
47
#include <rwlock.h>
47
#include <rwlock.h>
48
#include <libadt/list.h>
48
#include <libadt/list.h>
49
#include <unistd.h>
49
#include <unistd.h>
50
#include <ctype.h>
50
#include <ctype.h>
51
#include <fcntl.h>
51
#include <fcntl.h>
52
#include <assert.h>
52
#include <assert.h>
53
#include <vfs/canonify.h>
53
#include <vfs/canonify.h>
54
 
54
 
55
/* Forward declarations of static functions. */
55
/* Forward declarations of static functions. */
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
57
 
57
 
58
/**
58
/**
59
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
59
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
60
 * concurrent VFS operation which modifies the file system namespace.
60
 * concurrent VFS operation which modifies the file system namespace.
61
 */
61
 */
62
RWLOCK_INITIALIZE(namespace_rwlock);
62
RWLOCK_INITIALIZE(namespace_rwlock);
63
 
63
 
64
futex_t rootfs_futex = FUTEX_INITIALIZER;
64
futex_t rootfs_futex = FUTEX_INITIALIZER;
65
vfs_pair_t rootfs = {
65
vfs_pair_t rootfs = {
66
    .fs_handle = 0,
66
    .fs_handle = 0,
67
    .dev_handle = 0
67
    .dev_handle = 0
68
};
68
};
69
 
69
 
70
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
70
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
71
{
71
{
72
    dev_handle_t dev_handle;
72
    dev_handle_t dev_handle;
73
    vfs_node_t *mp_node = NULL;
73
    vfs_node_t *mp_node = NULL;
74
    int rc;
74
    int rc;
75
    int phone;
75
    int phone;
76
 
76
 
77
    /*
77
    /*
78
     * We expect the library to do the device-name to device-handle
78
     * We expect the library to do the device-name to device-handle
79
     * translation for us, thus the device handle will arrive as ARG1
79
     * translation for us, thus the device handle will arrive as ARG1
80
     * in the request.
80
     * in the request.
81
     */
81
     */
82
    dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
82
    dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
83
 
83
 
84
    /*
84
    /*
85
     * For now, don't make use of ARG2 and ARG3, but they can be used to
85
     * For now, don't make use of ARG2 and ARG3, but they can be used to
86
     * carry mount options in the future.
86
     * carry mount options in the future.
87
     */
87
     */
88
 
88
 
89
    ipc_callid_t callid;
89
    ipc_callid_t callid;
90
    size_t size;
90
    size_t size;
91
 
91
 
92
    /*
92
    /*
93
     * Now, we expect the client to send us data with the name of the file
93
     * Now, we expect the client to send us data with the name of the file
94
     * system.
94
     * system.
95
     */
95
     */
96
    if (!ipc_data_write_receive(&callid, &size)) {
96
    if (!ipc_data_write_receive(&callid, &size)) {
97
        ipc_answer_0(callid, EINVAL);
97
        ipc_answer_0(callid, EINVAL);
98
        ipc_answer_0(rid, EINVAL);
98
        ipc_answer_0(rid, EINVAL);
99
        return;
99
        return;
100
    }
100
    }
101
 
101
 
102
    /*
102
    /*
103
     * Don't receive more than is necessary for storing a full file system
103
     * Don't receive more than is necessary for storing a full file system
104
     * name.
104
     * name.
105
     */
105
     */
106
    if (size < 1 || size > FS_NAME_MAXLEN) {
106
    if (size < 1 || size > FS_NAME_MAXLEN) {
107
        ipc_answer_0(callid, EINVAL);
107
        ipc_answer_0(callid, EINVAL);
108
        ipc_answer_0(rid, EINVAL);
108
        ipc_answer_0(rid, EINVAL);
109
        return;
109
        return;
110
    }
110
    }
111
 
111
 
112
    /* Deliver the file system name. */
112
    /* Deliver the file system name. */
113
    char fs_name[FS_NAME_MAXLEN + 1];
113
    char fs_name[FS_NAME_MAXLEN + 1];
114
    (void) ipc_data_write_finalize(callid, fs_name, size);
114
    (void) ipc_data_write_finalize(callid, fs_name, size);
115
    fs_name[size] = '\0';
115
    fs_name[size] = '\0';
116
   
116
   
117
    /*
117
    /*
118
     * Check if we know a file system with the same name as is in fs_name.
118
     * Check if we know a file system with the same name as is in fs_name.
119
     * This will also give us its file system handle.
119
     * This will also give us its file system handle.
120
     */
120
     */
121
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
121
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
122
    if (!fs_handle) {
122
    if (!fs_handle) {
123
        ipc_answer_0(rid, ENOENT);
123
        ipc_answer_0(rid, ENOENT);
124
        return;
124
        return;
125
    }
125
    }
126
 
126
 
127
    /* Now, we want the client to send us the mount point. */
127
    /* Now, we want the client to send us the mount point. */
128
    if (!ipc_data_write_receive(&callid, &size)) {
128
    if (!ipc_data_write_receive(&callid, &size)) {
129
        ipc_answer_0(callid, EINVAL);
129
        ipc_answer_0(callid, EINVAL);
130
        ipc_answer_0(rid, EINVAL);
130
        ipc_answer_0(rid, EINVAL);
131
        return;
131
        return;
132
    }
132
    }
133
 
133
 
134
    /* Check whether size is reasonable wrt. the mount point. */
134
    /* Check whether size is reasonable wrt. the mount point. */
135
    if (size < 1 || size > MAX_PATH_LEN) {
135
    if (size < 1 || size > MAX_PATH_LEN) {
136
        ipc_answer_0(callid, EINVAL);
136
        ipc_answer_0(callid, EINVAL);
137
        ipc_answer_0(rid, EINVAL);
137
        ipc_answer_0(rid, EINVAL);
138
        return;
138
        return;
139
    }
139
    }
140
    /* Allocate buffer for the mount point data being received. */
140
    /* Allocate buffer for the mount point data being received. */
141
    uint8_t *buf;
141
    uint8_t *buf;
142
    buf = malloc(size + 1);
142
    buf = malloc(size + 1);
143
    if (!buf) {
143
    if (!buf) {
144
        ipc_answer_0(callid, ENOMEM);
144
        ipc_answer_0(callid, ENOMEM);
145
        ipc_answer_0(rid, ENOMEM);
145
        ipc_answer_0(rid, ENOMEM);
146
        return;
146
        return;
147
    }
147
    }
148
 
148
 
149
    /* Deliver the mount point. */
149
    /* Deliver the mount point. */
150
    (void) ipc_data_write_finalize(callid, buf, size);
150
    (void) ipc_data_write_finalize(callid, buf, size);
151
    buf[size] = '\0';
151
    buf[size] = '\0';
152
 
152
 
153
    /* Resolve the path to the mountpoint. */
153
    /* Resolve the path to the mountpoint. */
154
    vfs_lookup_res_t mp_res;
154
    vfs_lookup_res_t mp_res;
155
    futex_down(&rootfs_futex);
155
    futex_down(&rootfs_futex);
156
    if (rootfs.fs_handle) {
156
    if (rootfs.fs_handle) {
157
        /* We already have the root FS. */
157
        /* We already have the root FS. */
158
        rwlock_write_lock(&namespace_rwlock);
158
        rwlock_write_lock(&namespace_rwlock);
159
        if ((size == 1) && (buf[0] == '/')) {
159
        if ((size == 1) && (buf[0] == '/')) {
160
            /* Trying to mount root FS over root FS */
160
            /* Trying to mount root FS over root FS */
161
            rwlock_write_unlock(&namespace_rwlock);
161
            rwlock_write_unlock(&namespace_rwlock);
162
            futex_up(&rootfs_futex);
162
            futex_up(&rootfs_futex);
163
            free(buf);
163
            free(buf);
164
            ipc_answer_0(rid, EBUSY);
164
            ipc_answer_0(rid, EBUSY);
165
            return;
165
            return;
166
        }
166
        }
167
        rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL);
167
        rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL);
168
        if (rc != EOK) {
168
        if (rc != EOK) {
169
            /* The lookup failed for some reason. */
169
            /* The lookup failed for some reason. */
170
            rwlock_write_unlock(&namespace_rwlock);
170
            rwlock_write_unlock(&namespace_rwlock);
171
            futex_up(&rootfs_futex);
171
            futex_up(&rootfs_futex);
172
            free(buf);
172
            free(buf);
173
            ipc_answer_0(rid, rc);
173
            ipc_answer_0(rid, rc);
174
            return;
174
            return;
175
        }
175
        }
176
        mp_node = vfs_node_get(&mp_res);
176
        mp_node = vfs_node_get(&mp_res);
177
        if (!mp_node) {
177
        if (!mp_node) {
178
            rwlock_write_unlock(&namespace_rwlock);
178
            rwlock_write_unlock(&namespace_rwlock);
179
            futex_up(&rootfs_futex);
179
            futex_up(&rootfs_futex);
180
            free(buf);
180
            free(buf);
181
            ipc_answer_0(rid, ENOMEM);
181
            ipc_answer_0(rid, ENOMEM);
182
            return;
182
            return;
183
        }
183
        }
184
        /*
184
        /*
185
         * Now we hold a reference to mp_node.
185
         * Now we hold a reference to mp_node.
186
         * It will be dropped upon the corresponding VFS_UNMOUNT.
186
         * It will be dropped upon the corresponding VFS_UNMOUNT.
187
         * This prevents the mount point from being deleted.
187
         * This prevents the mount point from being deleted.
188
         */
188
         */
189
        rwlock_write_unlock(&namespace_rwlock);
189
        rwlock_write_unlock(&namespace_rwlock);
190
    } else {
190
    } else {
191
        /* We still don't have the root file system mounted. */
191
        /* We still don't have the root file system mounted. */
192
        if ((size == 1) && (buf[0] == '/')) {
192
        if ((size == 1) && (buf[0] == '/')) {
193
            /*
193
            /*
194
             * For this simple, but important case,
194
             * For this simple, but important case,
195
             * we are almost done.
195
             * we are almost done.
196
             */
196
             */
197
            free(buf);
197
            free(buf);
198
           
198
           
199
            /* Tell the mountee that it is being mounted. */
199
            /* Tell the mountee that it is being mounted. */
200
            phone = vfs_grab_phone(fs_handle);
200
            phone = vfs_grab_phone(fs_handle);
201
            rc = async_req_1_0(phone, VFS_MOUNTED,
201
            rc = async_req_1_0(phone, VFS_MOUNTED,
202
                (ipcarg_t) dev_handle);
202
                (ipcarg_t) dev_handle);
203
            vfs_release_phone(phone);
203
            vfs_release_phone(phone);
204
 
204
 
205
            if (rc == EOK) {
205
            if (rc == EOK) {
206
                rootfs.fs_handle = fs_handle;
206
                rootfs.fs_handle = fs_handle;
207
                rootfs.dev_handle = dev_handle;
207
                rootfs.dev_handle = dev_handle;
208
            }
208
            }
209
 
209
 
210
            futex_up(&rootfs_futex);
210
            futex_up(&rootfs_futex);
211
            ipc_answer_0(rid, rc);
211
            ipc_answer_0(rid, rc);
212
            return;
212
            return;
213
        } else {
213
        } else {
214
            /*
214
            /*
215
             * We can't resolve this without the root filesystem
215
             * We can't resolve this without the root filesystem
216
             * being mounted first.
216
             * being mounted first.
217
             */
217
             */
218
            futex_up(&rootfs_futex);
218
            futex_up(&rootfs_futex);
219
            free(buf);
219
            free(buf);
220
            ipc_answer_0(rid, ENOENT);
220
            ipc_answer_0(rid, ENOENT);
221
            return;
221
            return;
222
        }
222
        }
223
    }
223
    }
224
    futex_up(&rootfs_futex);
224
    futex_up(&rootfs_futex);
225
   
225
   
226
    free(buf);  /* The buffer is not needed anymore. */
226
    free(buf);  /* The buffer is not needed anymore. */
227
   
227
   
228
    /*
228
    /*
229
     * At this point, we have all necessary pieces: file system and device
229
     * At this point, we have all necessary pieces: file system and device
230
     * handles, and we know the mount point VFS node.
230
     * handles, and we know the mount point VFS node.
231
     */
231
     */
232
 
232
 
233
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
233
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
234
    rc = async_req_4_0(phone, VFS_MOUNT,
234
    rc = async_req_4_0(phone, VFS_MOUNT,
235
        (ipcarg_t) mp_res.triplet.dev_handle,
235
        (ipcarg_t) mp_res.triplet.dev_handle,
236
        (ipcarg_t) mp_res.triplet.index,
236
        (ipcarg_t) mp_res.triplet.index,
237
        (ipcarg_t) fs_handle,
237
        (ipcarg_t) fs_handle,
238
        (ipcarg_t) dev_handle);
238
        (ipcarg_t) dev_handle);
239
    vfs_release_phone(phone);
239
    vfs_release_phone(phone);
240
 
240
 
241
    if (rc != EOK) {
241
    if (rc != EOK) {
242
        /* Mount failed, drop reference to mp_node. */
242
        /* Mount failed, drop reference to mp_node. */
243
        if (mp_node)
243
        if (mp_node)
244
            vfs_node_put(mp_node);
244
            vfs_node_put(mp_node);
245
    }
245
    }
246
   
246
   
247
    ipc_answer_0(rid, rc);
247
    ipc_answer_0(rid, rc);
248
}
248
}
249
 
249
 
250
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
250
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
251
{
251
{
252
    if (!vfs_files_init()) {
252
    if (!vfs_files_init()) {
253
        ipc_answer_0(rid, ENOMEM);
253
        ipc_answer_0(rid, ENOMEM);
254
        return;
254
        return;
255
    }
255
    }
256
 
256
 
257
    /*
257
    /*
258
     * The POSIX interface is open(path, oflag, mode).
258
     * The POSIX interface is open(path, oflag, mode).
259
     * We can receive oflags and mode along with the VFS_OPEN call; the path
259
     * We can receive oflags and mode along with the VFS_OPEN call; the path
260
     * will need to arrive in another call.
260
     * will need to arrive in another call.
261
     *
261
     *
262
     * We also receive one private, non-POSIX set of flags called lflag
262
     * We also receive one private, non-POSIX set of flags called lflag
263
     * used to pass information to vfs_lookup_internal().
263
     * used to pass information to vfs_lookup_internal().
264
     */
264
     */
265
    int lflag = IPC_GET_ARG1(*request);
265
    int lflag = IPC_GET_ARG1(*request);
266
    int oflag = IPC_GET_ARG2(*request);
266
    int oflag = IPC_GET_ARG2(*request);
267
    int mode = IPC_GET_ARG3(*request);
267
    int mode = IPC_GET_ARG3(*request);
268
    size_t len;
268
    size_t len;
269
 
269
 
270
    if (oflag & O_CREAT)
270
    if (oflag & O_CREAT)
271
        lflag |= L_CREATE;
271
        lflag |= L_CREATE;
272
    if (oflag & O_EXCL)
272
    if (oflag & O_EXCL)
273
        lflag |= L_EXCLUSIVE;
273
        lflag |= L_EXCLUSIVE;
274
 
274
 
275
    ipc_callid_t callid;
275
    ipc_callid_t callid;
276
 
276
 
277
    if (!ipc_data_write_receive(&callid, &len)) {
277
    if (!ipc_data_write_receive(&callid, &len)) {
278
        ipc_answer_0(callid, EINVAL);
278
        ipc_answer_0(callid, EINVAL);
279
        ipc_answer_0(rid, EINVAL);
279
        ipc_answer_0(rid, EINVAL);
280
        return;
280
        return;
281
    }
281
    }
282
    char *path = malloc(len + 1);
282
    char *path = malloc(len + 1);
283
    if (!path) {
283
    if (!path) {
284
        ipc_answer_0(callid, ENOMEM);
284
        ipc_answer_0(callid, ENOMEM);
285
        ipc_answer_0(rid, ENOMEM);
285
        ipc_answer_0(rid, ENOMEM);
286
        return;
286
        return;
287
    }
287
    }
288
    int rc;
288
    int rc;
289
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
289
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
290
        ipc_answer_0(rid, rc);
290
        ipc_answer_0(rid, rc);
291
        free(path);
291
        free(path);
292
        return;
292
        return;
293
    }
293
    }
294
    path[len] = '\0';
294
    path[len] = '\0';
295
   
295
   
296
    /*
296
    /*
297
     * Avoid the race condition in which the file can be deleted before we
297
     * Avoid the race condition in which the file can be deleted before we
298
     * find/create-and-lock the VFS node corresponding to the looked-up
298
     * find/create-and-lock the VFS node corresponding to the looked-up
299
     * triplet.
299
     * triplet.
300
     */
300
     */
301
    if (lflag & L_CREATE)
301
    if (lflag & L_CREATE)
302
        rwlock_write_lock(&namespace_rwlock);
302
        rwlock_write_lock(&namespace_rwlock);
303
    else
303
    else
304
        rwlock_read_lock(&namespace_rwlock);
304
        rwlock_read_lock(&namespace_rwlock);
305
 
305
 
306
    /* The path is now populated and we can call vfs_lookup_internal(). */
306
    /* The path is now populated and we can call vfs_lookup_internal(). */
307
    vfs_lookup_res_t lr;
307
    vfs_lookup_res_t lr;
308
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
308
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
309
    if (rc) {
309
    if (rc) {
310
        if (lflag & L_CREATE)
310
        if (lflag & L_CREATE)
311
            rwlock_write_unlock(&namespace_rwlock);
311
            rwlock_write_unlock(&namespace_rwlock);
312
        else
312
        else
313
            rwlock_read_unlock(&namespace_rwlock);
313
            rwlock_read_unlock(&namespace_rwlock);
314
        ipc_answer_0(rid, rc);
314
        ipc_answer_0(rid, rc);
315
        free(path);
315
        free(path);
316
        return;
316
        return;
317
    }
317
    }
318
 
318
 
319
    /* Path is no longer needed. */
319
    /* Path is no longer needed. */
320
    free(path);
320
    free(path);
321
 
321
 
322
    vfs_node_t *node = vfs_node_get(&lr);
322
    vfs_node_t *node = vfs_node_get(&lr);
323
    if (lflag & L_CREATE)
323
    if (lflag & L_CREATE)
324
        rwlock_write_unlock(&namespace_rwlock);
324
        rwlock_write_unlock(&namespace_rwlock);
325
    else
325
    else
326
        rwlock_read_unlock(&namespace_rwlock);
326
        rwlock_read_unlock(&namespace_rwlock);
327
 
327
 
328
    /* Truncate the file if requested and if necessary. */
328
    /* Truncate the file if requested and if necessary. */
329
    if (oflag & O_TRUNC) {
329
    if (oflag & O_TRUNC) {
330
        rwlock_write_lock(&node->contents_rwlock);
330
        rwlock_write_lock(&node->contents_rwlock);
331
        if (node->size) {
331
        if (node->size) {
332
            rc = vfs_truncate_internal(node->fs_handle,
332
            rc = vfs_truncate_internal(node->fs_handle,
333
                node->dev_handle, node->index, 0);
333
                node->dev_handle, node->index, 0);
334
            if (rc) {
334
            if (rc) {
335
                rwlock_write_unlock(&node->contents_rwlock);
335
                rwlock_write_unlock(&node->contents_rwlock);
336
                vfs_node_put(node);
336
                vfs_node_put(node);
337
                ipc_answer_0(rid, rc);
337
                ipc_answer_0(rid, rc);
338
                return;
338
                return;
339
            }
339
            }
340
            node->size = 0;
340
            node->size = 0;
341
        }
341
        }
342
        rwlock_write_unlock(&node->contents_rwlock);
342
        rwlock_write_unlock(&node->contents_rwlock);
343
    }
343
    }
344
 
344
 
345
    /*
345
    /*
346
     * Get ourselves a file descriptor and the corresponding vfs_file_t
346
     * Get ourselves a file descriptor and the corresponding vfs_file_t
347
     * structure.
347
     * structure.
348
     */
348
     */
349
    int fd = vfs_fd_alloc();
349
    int fd = vfs_fd_alloc();
350
    if (fd < 0) {
350
    if (fd < 0) {
351
        vfs_node_put(node);
351
        vfs_node_put(node);
352
        ipc_answer_0(rid, fd);
352
        ipc_answer_0(rid, fd);
353
        return;
353
        return;
354
    }
354
    }
355
    vfs_file_t *file = vfs_file_get(fd);
355
    vfs_file_t *file = vfs_file_get(fd);
356
    file->node = node;
356
    file->node = node;
357
    if (oflag & O_APPEND)
357
    if (oflag & O_APPEND)
358
        file->append = true;
358
        file->append = true;
359
 
359
 
360
    /*
360
    /*
361
     * The following increase in reference count is for the fact that the
361
     * The following increase in reference count is for the fact that the
362
     * file is being opened and that a file structure is pointing to it.
362
     * file is being opened and that a file structure is pointing to it.
363
     * It is necessary so that the file will not disappear when
363
     * It is necessary so that the file will not disappear when
364
     * vfs_node_put() is called. The reference will be dropped by the
364
     * vfs_node_put() is called. The reference will be dropped by the
365
     * respective VFS_CLOSE.
365
     * respective VFS_CLOSE.
366
     */
366
     */
367
    vfs_node_addref(node);
367
    vfs_node_addref(node);
368
    vfs_node_put(node);
368
    vfs_node_put(node);
369
 
369
 
370
    /* Success! Return the new file descriptor to the client. */
370
    /* Success! Return the new file descriptor to the client. */
371
    ipc_answer_1(rid, EOK, fd);
371
    ipc_answer_1(rid, EOK, fd);
372
}
372
}
373
 
373
 
374
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
374
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
375
{
375
{
376
    int fd = IPC_GET_ARG1(*request);
376
    int fd = IPC_GET_ARG1(*request);
377
    if (fd >= MAX_OPEN_FILES) {
-
 
378
        ipc_answer_0(rid, EBADF);
-
 
379
        return;
-
 
380
    }
-
 
381
    vfs_fd_free(fd);
377
    int rc = vfs_fd_free(fd);
382
    ipc_answer_0(rid, EOK);
378
    ipc_answer_0(rid, rc);
383
}
379
}
384
 
380
 
385
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
381
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
386
{
382
{
387
 
383
 
388
    /*
384
    /*
389
     * The following code strongly depends on the fact that the files data
385
     * The following code strongly depends on the fact that the files data
390
     * structure can be only accessed by a single fibril and all file
386
     * structure can be only accessed by a single fibril and all file
391
     * operations are serialized (i.e. the reads and writes cannot
387
     * operations are serialized (i.e. the reads and writes cannot
392
     * interleave and a file cannot be closed while it is being read).
388
     * interleave and a file cannot be closed while it is being read).
393
     *
389
     *
394
     * Additional synchronization needs to be added once the table of
390
     * Additional synchronization needs to be added once the table of
395
     * open files supports parallel access!
391
     * open files supports parallel access!
396
     */
392
     */
397
 
393
 
398
    int fd = IPC_GET_ARG1(*request);
394
    int fd = IPC_GET_ARG1(*request);
399
   
395
   
400
    /* Lookup the file structure corresponding to the file descriptor. */
396
    /* Lookup the file structure corresponding to the file descriptor. */
401
    vfs_file_t *file = vfs_file_get(fd);
397
    vfs_file_t *file = vfs_file_get(fd);
402
    if (!file) {
398
    if (!file) {
403
        ipc_answer_0(rid, ENOENT);
399
        ipc_answer_0(rid, ENOENT);
404
        return;
400
        return;
405
    }
401
    }
406
   
402
   
407
    /*
403
    /*
408
     * Now we need to receive a call with client's
404
     * Now we need to receive a call with client's
409
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
405
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
410
     */
406
     */
411
    ipc_callid_t callid;
407
    ipc_callid_t callid;
412
    int res;
408
    int res;
413
    if (read)
409
    if (read)
414
        res = ipc_data_read_receive(&callid, NULL);
410
        res = ipc_data_read_receive(&callid, NULL);
415
    else
411
    else
416
        res = ipc_data_write_receive(&callid, NULL);
412
        res = ipc_data_write_receive(&callid, NULL);
417
    if (!res) {
413
    if (!res) {
418
        ipc_answer_0(callid, EINVAL);
414
        ipc_answer_0(callid, EINVAL);
419
        ipc_answer_0(rid, EINVAL);
415
        ipc_answer_0(rid, EINVAL);
420
        return;
416
        return;
421
    }
417
    }
422
   
418
   
423
    /*
419
    /*
424
     * Lock the open file structure so that no other thread can manipulate
420
     * Lock the open file structure so that no other thread can manipulate
425
     * the same open file at a time.
421
     * the same open file at a time.
426
     */
422
     */
427
    futex_down(&file->lock);
423
    futex_down(&file->lock);
428
   
424
   
429
    /*
425
    /*
430
     * Lock the file's node so that no other client can read/write to it at
426
     * Lock the file's node so that no other client can read/write to it at
431
     * the same time.
427
     * the same time.
432
     */
428
     */
433
    if (read)
429
    if (read)
434
        rwlock_read_lock(&file->node->contents_rwlock);
430
        rwlock_read_lock(&file->node->contents_rwlock);
435
    else
431
    else
436
        rwlock_write_lock(&file->node->contents_rwlock);
432
        rwlock_write_lock(&file->node->contents_rwlock);
437
   
433
   
438
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
434
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
439
   
435
   
440
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
436
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
441
    aid_t msg;
437
    aid_t msg;
442
    ipc_call_t answer;
438
    ipc_call_t answer;
443
    if (!read && file->append)
439
    if (!read && file->append)
444
        file->pos = file->node->size;
440
        file->pos = file->node->size;
445
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
441
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
446
        file->node->dev_handle, file->node->index, file->pos, &answer);
442
        file->node->dev_handle, file->node->index, file->pos, &answer);
447
   
443
   
448
    /*
444
    /*
449
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
445
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
450
     * destination FS server. The call will be routed as if sent by
446
     * destination FS server. The call will be routed as if sent by
451
     * ourselves. Note that call arguments are immutable in this case so we
447
     * ourselves. Note that call arguments are immutable in this case so we
452
     * don't have to bother.
448
     * don't have to bother.
453
     */
449
     */
454
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
450
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
455
   
451
   
456
    vfs_release_phone(fs_phone);
452
    vfs_release_phone(fs_phone);
457
   
453
   
458
    /* Wait for reply from the FS server. */
454
    /* Wait for reply from the FS server. */
459
    ipcarg_t rc;
455
    ipcarg_t rc;
460
    async_wait_for(msg, &rc);
456
    async_wait_for(msg, &rc);
461
    size_t bytes = IPC_GET_ARG1(answer);
457
    size_t bytes = IPC_GET_ARG1(answer);
462
   
458
   
463
    /* Unlock the VFS node. */
459
    /* Unlock the VFS node. */
464
    if (read)
460
    if (read)
465
        rwlock_read_unlock(&file->node->contents_rwlock);
461
        rwlock_read_unlock(&file->node->contents_rwlock);
466
    else {
462
    else {
467
        /* Update the cached version of node's size. */
463
        /* Update the cached version of node's size. */
468
        if (rc == EOK)
464
        if (rc == EOK)
469
            file->node->size = IPC_GET_ARG2(answer);
465
            file->node->size = IPC_GET_ARG2(answer);
470
        rwlock_write_unlock(&file->node->contents_rwlock);
466
        rwlock_write_unlock(&file->node->contents_rwlock);
471
    }
467
    }
472
   
468
   
473
    /* Update the position pointer and unlock the open file. */
469
    /* Update the position pointer and unlock the open file. */
474
    if (rc == EOK)
470
    if (rc == EOK)
475
        file->pos += bytes;
471
        file->pos += bytes;
476
    futex_up(&file->lock);
472
    futex_up(&file->lock);
477
   
473
   
478
    /*
474
    /*
479
     * FS server's reply is the final result of the whole operation we
475
     * FS server's reply is the final result of the whole operation we
480
     * return to the client.
476
     * return to the client.
481
     */
477
     */
482
    ipc_answer_1(rid, rc, bytes);
478
    ipc_answer_1(rid, rc, bytes);
483
}
479
}
484
 
480
 
485
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
481
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
486
{
482
{
487
    vfs_rdwr(rid, request, true);
483
    vfs_rdwr(rid, request, true);
488
}
484
}
489
 
485
 
490
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
486
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
491
{
487
{
492
    vfs_rdwr(rid, request, false);
488
    vfs_rdwr(rid, request, false);
493
}
489
}
494
 
490
 
495
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
491
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
496
{
492
{
497
    int fd = (int) IPC_GET_ARG1(*request);
493
    int fd = (int) IPC_GET_ARG1(*request);
498
    off_t off = (off_t) IPC_GET_ARG2(*request);
494
    off_t off = (off_t) IPC_GET_ARG2(*request);
499
    int whence = (int) IPC_GET_ARG3(*request);
495
    int whence = (int) IPC_GET_ARG3(*request);
500
 
496
 
501
 
497
 
502
    /* Lookup the file structure corresponding to the file descriptor. */
498
    /* Lookup the file structure corresponding to the file descriptor. */
503
    vfs_file_t *file = vfs_file_get(fd);
499
    vfs_file_t *file = vfs_file_get(fd);
504
    if (!file) {
500
    if (!file) {
505
        ipc_answer_0(rid, ENOENT);
501
        ipc_answer_0(rid, ENOENT);
506
        return;
502
        return;
507
    }
503
    }
508
 
504
 
509
    off_t newpos;
505
    off_t newpos;
510
    futex_down(&file->lock);
506
    futex_down(&file->lock);
511
    if (whence == SEEK_SET) {
507
    if (whence == SEEK_SET) {
512
        file->pos = off;
508
        file->pos = off;
513
        futex_up(&file->lock);
509
        futex_up(&file->lock);
514
        ipc_answer_1(rid, EOK, off);
510
        ipc_answer_1(rid, EOK, off);
515
        return;
511
        return;
516
    }
512
    }
517
    if (whence == SEEK_CUR) {
513
    if (whence == SEEK_CUR) {
518
        if (file->pos + off < file->pos) {
514
        if (file->pos + off < file->pos) {
519
            futex_up(&file->lock);
515
            futex_up(&file->lock);
520
            ipc_answer_0(rid, EOVERFLOW);
516
            ipc_answer_0(rid, EOVERFLOW);
521
            return;
517
            return;
522
        }
518
        }
523
        file->pos += off;
519
        file->pos += off;
524
        newpos = file->pos;
520
        newpos = file->pos;
525
        futex_up(&file->lock);
521
        futex_up(&file->lock);
526
        ipc_answer_1(rid, EOK, newpos);
522
        ipc_answer_1(rid, EOK, newpos);
527
        return;
523
        return;
528
    }
524
    }
529
    if (whence == SEEK_END) {
525
    if (whence == SEEK_END) {
530
        rwlock_read_lock(&file->node->contents_rwlock);
526
        rwlock_read_lock(&file->node->contents_rwlock);
531
        size_t size = file->node->size;
527
        size_t size = file->node->size;
532
        rwlock_read_unlock(&file->node->contents_rwlock);
528
        rwlock_read_unlock(&file->node->contents_rwlock);
533
        if (size + off < size) {
529
        if (size + off < size) {
534
            futex_up(&file->lock);
530
            futex_up(&file->lock);
535
            ipc_answer_0(rid, EOVERFLOW);
531
            ipc_answer_0(rid, EOVERFLOW);
536
            return;
532
            return;
537
        }
533
        }
538
        newpos = size + off;
534
        newpos = size + off;
539
        futex_up(&file->lock);
535
        futex_up(&file->lock);
540
        ipc_answer_1(rid, EOK, newpos);
536
        ipc_answer_1(rid, EOK, newpos);
541
        return;
537
        return;
542
    }
538
    }
543
    futex_up(&file->lock);
539
    futex_up(&file->lock);
544
    ipc_answer_0(rid, EINVAL);
540
    ipc_answer_0(rid, EINVAL);
545
}
541
}
546
 
542
 
547
int
543
int
548
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
544
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
549
    fs_index_t index, size_t size)
545
    fs_index_t index, size_t size)
550
{
546
{
551
    ipcarg_t rc;
547
    ipcarg_t rc;
552
    int fs_phone;
548
    int fs_phone;
553
   
549
   
554
    fs_phone = vfs_grab_phone(fs_handle);
550
    fs_phone = vfs_grab_phone(fs_handle);
555
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
551
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
556
        (ipcarg_t)index, (ipcarg_t)size);
552
        (ipcarg_t)index, (ipcarg_t)size);
557
    vfs_release_phone(fs_phone);
553
    vfs_release_phone(fs_phone);
558
    return (int)rc;
554
    return (int)rc;
559
}
555
}
560
 
556
 
561
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
557
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
562
{
558
{
563
    int fd = IPC_GET_ARG1(*request);
559
    int fd = IPC_GET_ARG1(*request);
564
    size_t size = IPC_GET_ARG2(*request);
560
    size_t size = IPC_GET_ARG2(*request);
565
    int rc;
561
    int rc;
566
 
562
 
567
    vfs_file_t *file = vfs_file_get(fd);
563
    vfs_file_t *file = vfs_file_get(fd);
568
    if (!file) {
564
    if (!file) {
569
        ipc_answer_0(rid, ENOENT);
565
        ipc_answer_0(rid, ENOENT);
570
        return;
566
        return;
571
    }
567
    }
572
    futex_down(&file->lock);
568
    futex_down(&file->lock);
573
 
569
 
574
    rwlock_write_lock(&file->node->contents_rwlock);
570
    rwlock_write_lock(&file->node->contents_rwlock);
575
    rc = vfs_truncate_internal(file->node->fs_handle,
571
    rc = vfs_truncate_internal(file->node->fs_handle,
576
        file->node->dev_handle, file->node->index, size);
572
        file->node->dev_handle, file->node->index, size);
577
    if (rc == EOK)
573
    if (rc == EOK)
578
        file->node->size = size;
574
        file->node->size = size;
579
    rwlock_write_unlock(&file->node->contents_rwlock);
575
    rwlock_write_unlock(&file->node->contents_rwlock);
580
 
576
 
581
    futex_up(&file->lock);
577
    futex_up(&file->lock);
582
    ipc_answer_0(rid, (ipcarg_t)rc);
578
    ipc_answer_0(rid, (ipcarg_t)rc);
583
}
579
}
584
 
580
 
585
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
581
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
586
{
582
{
587
    int mode = IPC_GET_ARG1(*request);
583
    int mode = IPC_GET_ARG1(*request);
588
 
584
 
589
    size_t len;
585
    size_t len;
590
    ipc_callid_t callid;
586
    ipc_callid_t callid;
591
 
587
 
592
    if (!ipc_data_write_receive(&callid, &len)) {
588
    if (!ipc_data_write_receive(&callid, &len)) {
593
        ipc_answer_0(callid, EINVAL);
589
        ipc_answer_0(callid, EINVAL);
594
        ipc_answer_0(rid, EINVAL);
590
        ipc_answer_0(rid, EINVAL);
595
        return;
591
        return;
596
    }
592
    }
597
    char *path = malloc(len + 1);
593
    char *path = malloc(len + 1);
598
    if (!path) {
594
    if (!path) {
599
        ipc_answer_0(callid, ENOMEM);
595
        ipc_answer_0(callid, ENOMEM);
600
        ipc_answer_0(rid, ENOMEM);
596
        ipc_answer_0(rid, ENOMEM);
601
        return;
597
        return;
602
    }
598
    }
603
    int rc;
599
    int rc;
604
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
600
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
605
        ipc_answer_0(rid, rc);
601
        ipc_answer_0(rid, rc);
606
        free(path);
602
        free(path);
607
        return;
603
        return;
608
    }
604
    }
609
    path[len] = '\0';
605
    path[len] = '\0';
610
   
606
   
611
    rwlock_write_lock(&namespace_rwlock);
607
    rwlock_write_lock(&namespace_rwlock);
612
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
608
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
613
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
609
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
614
    rwlock_write_unlock(&namespace_rwlock);
610
    rwlock_write_unlock(&namespace_rwlock);
615
    free(path);
611
    free(path);
616
    ipc_answer_0(rid, rc);
612
    ipc_answer_0(rid, rc);
617
}
613
}
618
 
614
 
619
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
615
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
620
{
616
{
621
    int lflag = IPC_GET_ARG1(*request);
617
    int lflag = IPC_GET_ARG1(*request);
622
 
618
 
623
    size_t len;
619
    size_t len;
624
    ipc_callid_t callid;
620
    ipc_callid_t callid;
625
 
621
 
626
    if (!ipc_data_write_receive(&callid, &len)) {
622
    if (!ipc_data_write_receive(&callid, &len)) {
627
        ipc_answer_0(callid, EINVAL);
623
        ipc_answer_0(callid, EINVAL);
628
        ipc_answer_0(rid, EINVAL);
624
        ipc_answer_0(rid, EINVAL);
629
        return;
625
        return;
630
    }
626
    }
631
    char *path = malloc(len + 1);
627
    char *path = malloc(len + 1);
632
    if (!path) {
628
    if (!path) {
633
        ipc_answer_0(callid, ENOMEM);
629
        ipc_answer_0(callid, ENOMEM);
634
        ipc_answer_0(rid, ENOMEM);
630
        ipc_answer_0(rid, ENOMEM);
635
        return;
631
        return;
636
    }
632
    }
637
    int rc;
633
    int rc;
638
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
634
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
639
        ipc_answer_0(rid, rc);
635
        ipc_answer_0(rid, rc);
640
        free(path);
636
        free(path);
641
        return;
637
        return;
642
    }
638
    }
643
    path[len] = '\0';
639
    path[len] = '\0';
644
   
640
   
645
    rwlock_write_lock(&namespace_rwlock);
641
    rwlock_write_lock(&namespace_rwlock);
646
    lflag &= L_DIRECTORY;   /* sanitize lflag */
642
    lflag &= L_DIRECTORY;   /* sanitize lflag */
647
    vfs_lookup_res_t lr;
643
    vfs_lookup_res_t lr;
648
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
644
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
649
    free(path);
645
    free(path);
650
    if (rc != EOK) {
646
    if (rc != EOK) {
651
        rwlock_write_unlock(&namespace_rwlock);
647
        rwlock_write_unlock(&namespace_rwlock);
652
        ipc_answer_0(rid, rc);
648
        ipc_answer_0(rid, rc);
653
        return;
649
        return;
654
    }
650
    }
655
 
651
 
656
    /*
652
    /*
657
     * The name has already been unlinked by vfs_lookup_internal().
653
     * The name has already been unlinked by vfs_lookup_internal().
658
     * We have to get and put the VFS node to ensure that it is
654
     * We have to get and put the VFS node to ensure that it is
659
     * VFS_DESTROY'ed after the last reference to it is dropped.
655
     * VFS_DESTROY'ed after the last reference to it is dropped.
660
     */
656
     */
661
    vfs_node_t *node = vfs_node_get(&lr);
657
    vfs_node_t *node = vfs_node_get(&lr);
662
    futex_down(&nodes_futex);
658
    futex_down(&nodes_futex);
663
    node->lnkcnt--;
659
    node->lnkcnt--;
664
    futex_up(&nodes_futex);
660
    futex_up(&nodes_futex);
665
    rwlock_write_unlock(&namespace_rwlock);
661
    rwlock_write_unlock(&namespace_rwlock);
666
    vfs_node_put(node);
662
    vfs_node_put(node);
667
    ipc_answer_0(rid, EOK);
663
    ipc_answer_0(rid, EOK);
668
}
664
}
669
 
665
 
670
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
666
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
671
{
667
{
672
    size_t len;
668
    size_t len;
673
    ipc_callid_t callid;
669
    ipc_callid_t callid;
674
    int rc;
670
    int rc;
675
 
671
 
676
    /* Retrieve the old path. */
672
    /* Retrieve the old path. */
677
    if (!ipc_data_write_receive(&callid, &len)) {
673
    if (!ipc_data_write_receive(&callid, &len)) {
678
        ipc_answer_0(callid, EINVAL);
674
        ipc_answer_0(callid, EINVAL);
679
        ipc_answer_0(rid, EINVAL);
675
        ipc_answer_0(rid, EINVAL);
680
        return;
676
        return;
681
    }
677
    }
682
    char *old = malloc(len + 1);
678
    char *old = malloc(len + 1);
683
    if (!old) {
679
    if (!old) {
684
        ipc_answer_0(callid, ENOMEM);
680
        ipc_answer_0(callid, ENOMEM);
685
        ipc_answer_0(rid, ENOMEM);
681
        ipc_answer_0(rid, ENOMEM);
686
        return;
682
        return;
687
    }
683
    }
688
    if ((rc = ipc_data_write_finalize(callid, old, len))) {
684
    if ((rc = ipc_data_write_finalize(callid, old, len))) {
689
        ipc_answer_0(rid, rc);
685
        ipc_answer_0(rid, rc);
690
        free(old);
686
        free(old);
691
        return;
687
        return;
692
    }
688
    }
693
    old[len] = '\0';
689
    old[len] = '\0';
694
   
690
   
695
    /* Retrieve the new path. */
691
    /* Retrieve the new path. */
696
    if (!ipc_data_write_receive(&callid, &len)) {
692
    if (!ipc_data_write_receive(&callid, &len)) {
697
        ipc_answer_0(callid, EINVAL);
693
        ipc_answer_0(callid, EINVAL);
698
        ipc_answer_0(rid, EINVAL);
694
        ipc_answer_0(rid, EINVAL);
699
        free(old);
695
        free(old);
700
        return;
696
        return;
701
    }
697
    }
702
    char *new = malloc(len + 1);
698
    char *new = malloc(len + 1);
703
    if (!new) {
699
    if (!new) {
704
        ipc_answer_0(callid, ENOMEM);
700
        ipc_answer_0(callid, ENOMEM);
705
        ipc_answer_0(rid, ENOMEM);
701
        ipc_answer_0(rid, ENOMEM);
706
        free(old);
702
        free(old);
707
        return;
703
        return;
708
    }
704
    }
709
    if ((rc = ipc_data_write_finalize(callid, new, len))) {
705
    if ((rc = ipc_data_write_finalize(callid, new, len))) {
710
        ipc_answer_0(rid, rc);
706
        ipc_answer_0(rid, rc);
711
        free(old);
707
        free(old);
712
        free(new);
708
        free(new);
713
        return;
709
        return;
714
    }
710
    }
715
    new[len] = '\0';
711
    new[len] = '\0';
716
 
712
 
717
    char *oldc = canonify(old, &len);
713
    char *oldc = canonify(old, &len);
718
    char *newc = canonify(new, NULL);
714
    char *newc = canonify(new, NULL);
719
    if (!oldc || !newc) {
715
    if (!oldc || !newc) {
720
        ipc_answer_0(rid, EINVAL);
716
        ipc_answer_0(rid, EINVAL);
721
        free(old);
717
        free(old);
722
        free(new);
718
        free(new);
723
        return;
719
        return;
724
    }
720
    }
725
    if (!strncmp(newc, oldc, len)) {
721
    if (!strncmp(newc, oldc, len)) {
726
        /* oldc is a prefix of newc */
722
        /* oldc is a prefix of newc */
727
        ipc_answer_0(rid, EINVAL);
723
        ipc_answer_0(rid, EINVAL);
728
        free(old);
724
        free(old);
729
        free(new);
725
        free(new);
730
        return;
726
        return;
731
    }
727
    }
732
   
728
   
733
    vfs_lookup_res_t old_lr;
729
    vfs_lookup_res_t old_lr;
734
    vfs_lookup_res_t new_lr;
730
    vfs_lookup_res_t new_lr;
735
    vfs_lookup_res_t new_par_lr;
731
    vfs_lookup_res_t new_par_lr;
736
    rwlock_write_lock(&namespace_rwlock);
732
    rwlock_write_lock(&namespace_rwlock);
737
    /* Lookup the node belonging to the old file name. */
733
    /* Lookup the node belonging to the old file name. */
738
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
734
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
739
    if (rc != EOK) {
735
    if (rc != EOK) {
740
        rwlock_write_unlock(&namespace_rwlock);
736
        rwlock_write_unlock(&namespace_rwlock);
741
        ipc_answer_0(rid, rc);
737
        ipc_answer_0(rid, rc);
742
        free(old);
738
        free(old);
743
        free(new);
739
        free(new);
744
        return;
740
        return;
745
    }
741
    }
746
    vfs_node_t *old_node = vfs_node_get(&old_lr);
742
    vfs_node_t *old_node = vfs_node_get(&old_lr);
747
    if (!old_node) {
743
    if (!old_node) {
748
        rwlock_write_unlock(&namespace_rwlock);
744
        rwlock_write_unlock(&namespace_rwlock);
749
        ipc_answer_0(rid, ENOMEM);
745
        ipc_answer_0(rid, ENOMEM);
750
        free(old);
746
        free(old);
751
        free(new);
747
        free(new);
752
        return;
748
        return;
753
    }
749
    }
754
    /* Lookup parent of the new file name. */
750
    /* Lookup parent of the new file name. */
755
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
751
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
756
    if (rc != EOK) {
752
    if (rc != EOK) {
757
        rwlock_write_unlock(&namespace_rwlock);
753
        rwlock_write_unlock(&namespace_rwlock);
758
        ipc_answer_0(rid, rc);
754
        ipc_answer_0(rid, rc);
759
        free(old);
755
        free(old);
760
        free(new);
756
        free(new);
761
        return;
757
        return;
762
    }
758
    }
763
    /* Check whether linking to the same file system instance. */
759
    /* Check whether linking to the same file system instance. */
764
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
760
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
765
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
761
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
766
        rwlock_write_unlock(&namespace_rwlock);
762
        rwlock_write_unlock(&namespace_rwlock);
767
        ipc_answer_0(rid, EXDEV);   /* different file systems */
763
        ipc_answer_0(rid, EXDEV);   /* different file systems */
768
        free(old);
764
        free(old);
769
        free(new);
765
        free(new);
770
        return;
766
        return;
771
    }
767
    }
772
    /* Destroy the old link for the new name. */
768
    /* Destroy the old link for the new name. */
773
    vfs_node_t *new_node = NULL;
769
    vfs_node_t *new_node = NULL;
774
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
770
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
775
    switch (rc) {
771
    switch (rc) {
776
    case ENOENT:
772
    case ENOENT:
777
        /* simply not in our way */
773
        /* simply not in our way */
778
        break;
774
        break;
779
    case EOK:
775
    case EOK:
780
        new_node = vfs_node_get(&new_lr);
776
        new_node = vfs_node_get(&new_lr);
781
        if (!new_node) {
777
        if (!new_node) {
782
            rwlock_write_unlock(&namespace_rwlock);
778
            rwlock_write_unlock(&namespace_rwlock);
783
            ipc_answer_0(rid, ENOMEM);
779
            ipc_answer_0(rid, ENOMEM);
784
            free(old);
780
            free(old);
785
            free(new);
781
            free(new);
786
            return;
782
            return;
787
        }
783
        }
788
        futex_down(&nodes_futex);
784
        futex_down(&nodes_futex);
789
        new_node->lnkcnt--;
785
        new_node->lnkcnt--;
790
        futex_up(&nodes_futex);
786
        futex_up(&nodes_futex);
791
        break;
787
        break;
792
    default:
788
    default:
793
        rwlock_write_unlock(&namespace_rwlock);
789
        rwlock_write_unlock(&namespace_rwlock);
794
        ipc_answer_0(rid, ENOTEMPTY);
790
        ipc_answer_0(rid, ENOTEMPTY);
795
        free(old);
791
        free(old);
796
        free(new);
792
        free(new);
797
        return;
793
        return;
798
    }
794
    }
799
    /* Create the new link for the new name. */
795
    /* Create the new link for the new name. */
800
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
796
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
801
    if (rc != EOK) {
797
    if (rc != EOK) {
802
        rwlock_write_unlock(&namespace_rwlock);
798
        rwlock_write_unlock(&namespace_rwlock);
803
        if (new_node)
799
        if (new_node)
804
            vfs_node_put(new_node);
800
            vfs_node_put(new_node);
805
        ipc_answer_0(rid, rc);
801
        ipc_answer_0(rid, rc);
806
        free(old);
802
        free(old);
807
        free(new);
803
        free(new);
808
        return;
804
        return;
809
    }
805
    }
810
    futex_down(&nodes_futex);
806
    futex_down(&nodes_futex);
811
    old_node->lnkcnt++;
807
    old_node->lnkcnt++;
812
    futex_up(&nodes_futex);
808
    futex_up(&nodes_futex);
813
    /* Destroy the link for the old name. */
809
    /* Destroy the link for the old name. */
814
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
810
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
815
    if (rc != EOK) {
811
    if (rc != EOK) {
816
        rwlock_write_unlock(&namespace_rwlock);
812
        rwlock_write_unlock(&namespace_rwlock);
817
        vfs_node_put(old_node);
813
        vfs_node_put(old_node);
818
        if (new_node)
814
        if (new_node)
819
            vfs_node_put(new_node);
815
            vfs_node_put(new_node);
820
        ipc_answer_0(rid, rc);
816
        ipc_answer_0(rid, rc);
821
        free(old);
817
        free(old);
822
        free(new);
818
        free(new);
823
        return;
819
        return;
824
    }
820
    }
825
    futex_down(&nodes_futex);
821
    futex_down(&nodes_futex);
826
    old_node->lnkcnt--;
822
    old_node->lnkcnt--;
827
    futex_up(&nodes_futex);
823
    futex_up(&nodes_futex);
828
    rwlock_write_unlock(&namespace_rwlock);
824
    rwlock_write_unlock(&namespace_rwlock);
829
    vfs_node_put(old_node);
825
    vfs_node_put(old_node);
830
    if (new_node)
826
    if (new_node)
831
        vfs_node_put(new_node);
827
        vfs_node_put(new_node);
832
    free(old);
828
    free(old);
833
    free(new);
829
    free(new);
834
    ipc_answer_0(rid, EOK);
830
    ipc_answer_0(rid, EOK);
835
}
831
}
836
 
832
 
837
/**
833
/**
838
 * @}
834
 * @}
839
 */
835
 */
840
 
836