Subversion Repositories HelenOS

Rev

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

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