Subversion Repositories HelenOS

Rev

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

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