Subversion Repositories HelenOS

Rev

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

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