Subversion Repositories HelenOS

Rev

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

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