Subversion Repositories HelenOS

Rev

Rev 4593 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2689 jermar 1
/*
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup fs
30
 * @{
4010 decky 31
 */
2689 jermar 32
 
33
/**
4010 decky 34
 * @file vfs_ops.c
35
 * @brief Operations that VFS offers to its clients.
2689 jermar 36
 */
37
 
2763 jermar 38
#include "vfs.h"
2689 jermar 39
#include <ipc/ipc.h>
40
#include <async.h>
41
#include <errno.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <bool.h>
4518 jermar 46
#include <fibril_sync.h>
4509 decky 47
#include <adt/list.h>
2689 jermar 48
#include <unistd.h>
49
#include <ctype.h>
2708 jermar 50
#include <fcntl.h>
2689 jermar 51
#include <assert.h>
2763 jermar 52
#include <vfs/canonify.h>
2689 jermar 53
 
2748 jermar 54
/* Forward declarations of static functions. */
2770 jermar 55
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
2748 jermar 56
 
2689 jermar 57
/**
58
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
59
 * concurrent VFS operation which modifies the file system namespace.
60
 */
4518 jermar 61
FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
2689 jermar 62
 
3109 jermar 63
vfs_pair_t rootfs = {
2689 jermar 64
    .fs_handle = 0,
3109 jermar 65
    .dev_handle = 0
2689 jermar 66
};
67
 
4010 decky 68
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
4305 jermar 69
    fs_handle_t fs_handle, char *mp, char *opts)
2689 jermar 70
{
4010 decky 71
    vfs_lookup_res_t mp_res;
4409 jermar 72
    vfs_lookup_res_t mr_res;
2689 jermar 73
    vfs_node_t *mp_node = NULL;
4409 jermar 74
    vfs_node_t *mr_node;
75
    fs_index_t rindex;
76
    size_t rsize;
77
    unsigned rlnkcnt;
4305 jermar 78
    ipcarg_t rc;
2958 jermar 79
    int phone;
4305 jermar 80
    aid_t msg;
81
    ipc_call_t answer;
4463 decky 82
 
4305 jermar 83
    /* Resolve the path to the mountpoint. */
4518 jermar 84
    fibril_rwlock_write_lock(&namespace_rwlock);
2689 jermar 85
    if (rootfs.fs_handle) {
2707 jermar 86
        /* We already have the root FS. */
4264 svoboda 87
        if (str_cmp(mp, "/") == 0) {
2788 jermar 88
            /* Trying to mount root FS over root FS */
4518 jermar 89
            fibril_rwlock_write_unlock(&namespace_rwlock);
4431 jermar 90
            ipc_answer_0(rid, EBUSY);
2788 jermar 91
            return;
92
        }
4010 decky 93
 
94
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
2689 jermar 95
        if (rc != EOK) {
2707 jermar 96
            /* The lookup failed for some reason. */
4518 jermar 97
            fibril_rwlock_write_unlock(&namespace_rwlock);
4431 jermar 98
            ipc_answer_0(rid, rc);
2689 jermar 99
            return;
100
        }
4010 decky 101
 
2691 jermar 102
        mp_node = vfs_node_get(&mp_res);
2689 jermar 103
        if (!mp_node) {
4518 jermar 104
            fibril_rwlock_write_unlock(&namespace_rwlock);
4431 jermar 105
            ipc_answer_0(rid, ENOMEM);
2689 jermar 106
            return;
107
        }
4010 decky 108
 
2689 jermar 109
        /*
110
         * Now we hold a reference to mp_node.
4584 jermar 111
         * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
2689 jermar 112
         * This prevents the mount point from being deleted.
113
         */
114
    } else {
2707 jermar 115
        /* We still don't have the root file system mounted. */
4264 svoboda 116
        if (str_cmp(mp, "/") == 0) {
2958 jermar 117
            /*
118
             * For this simple, but important case,
119
             * we are almost done.
120
             */
121
 
3109 jermar 122
            /* Tell the mountee that it is being mounted. */
123
            phone = vfs_grab_phone(fs_handle);
4584 jermar 124
            msg = async_send_1(phone, VFS_OUT_MOUNTED,
4305 jermar 125
                (ipcarg_t) dev_handle, &answer);
126
            /* send the mount options */
127
            rc = ipc_data_write_start(phone, (void *)opts,
128
                str_size(opts));
129
            if (rc != EOK) {
4551 jermar 130
                async_wait_for(msg, NULL);
4518 jermar 131
                vfs_release_phone(phone);
132
                fibril_rwlock_write_unlock(&namespace_rwlock);
4305 jermar 133
                ipc_answer_0(rid, rc);
134
                return;
135
            }
4551 jermar 136
            async_wait_for(msg, &rc);
4518 jermar 137
            vfs_release_phone(phone);
3352 jermar 138
 
139
            if (rc != EOK) {
4518 jermar 140
                fibril_rwlock_write_unlock(&namespace_rwlock);
3352 jermar 141
                ipc_answer_0(rid, rc);
142
                return;
3109 jermar 143
            }
4305 jermar 144
 
145
            rindex = (fs_index_t) IPC_GET_ARG1(answer);
146
            rsize = (size_t) IPC_GET_ARG2(answer);
147
            rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
4010 decky 148
 
3352 jermar 149
            mr_res.triplet.fs_handle = fs_handle;
150
            mr_res.triplet.dev_handle = dev_handle;
4305 jermar 151
            mr_res.triplet.index = rindex;
152
            mr_res.size = rsize;
153
            mr_res.lnkcnt = rlnkcnt;
3653 jermar 154
            mr_res.type = VFS_NODE_DIRECTORY;
4010 decky 155
 
3352 jermar 156
            rootfs.fs_handle = fs_handle;
157
            rootfs.dev_handle = dev_handle;
4010 decky 158
 
3352 jermar 159
            /* Add reference to the mounted root. */
160
            mr_node = vfs_node_get(&mr_res);
161
            assert(mr_node);
4010 decky 162
 
4518 jermar 163
            fibril_rwlock_write_unlock(&namespace_rwlock);
2958 jermar 164
            ipc_answer_0(rid, rc);
2689 jermar 165
            return;
166
        } else {
167
            /*
168
             * We can't resolve this without the root filesystem
169
             * being mounted first.
170
             */
4518 jermar 171
            fibril_rwlock_write_unlock(&namespace_rwlock);
2689 jermar 172
            ipc_answer_0(rid, ENOENT);
173
            return;
174
        }
175
    }
176
 
177
    /*
178
     * At this point, we have all necessary pieces: file system and device
3109 jermar 179
     * handles, and we know the mount point VFS node.
2689 jermar 180
     */
4010 decky 181
 
4409 jermar 182
    int mountee_phone = vfs_grab_phone(fs_handle);
183
    assert(mountee_phone >= 0);
184
 
2958 jermar 185
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
4584 jermar 186
    msg = async_send_4(phone, VFS_OUT_MOUNT,
2691 jermar 187
        (ipcarg_t) mp_res.triplet.dev_handle,
2957 jermar 188
        (ipcarg_t) mp_res.triplet.index,
3109 jermar 189
        (ipcarg_t) fs_handle,
4305 jermar 190
        (ipcarg_t) dev_handle, &answer);
4409 jermar 191
 
192
    /* send connection */
193
    rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
194
    if (rc != EOK) {
4551 jermar 195
        async_wait_for(msg, NULL);
196
        vfs_release_phone(mountee_phone);
4518 jermar 197
        vfs_release_phone(phone);
4409 jermar 198
        /* Mount failed, drop reference to mp_node. */
199
        if (mp_node)
200
            vfs_node_put(mp_node);
201
        ipc_answer_0(rid, rc);
4518 jermar 202
        fibril_rwlock_write_unlock(&namespace_rwlock);
4409 jermar 203
        return;
204
    }
4551 jermar 205
 
206
    vfs_release_phone(mountee_phone);
4409 jermar 207
 
4305 jermar 208
    /* send the mount options */
209
    rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
210
    if (rc != EOK) {
4551 jermar 211
        async_wait_for(msg, NULL);
4518 jermar 212
        vfs_release_phone(phone);
4305 jermar 213
        /* Mount failed, drop reference to mp_node. */
214
        if (mp_node)
215
            vfs_node_put(mp_node);
4518 jermar 216
        fibril_rwlock_write_unlock(&namespace_rwlock);
4305 jermar 217
        ipc_answer_0(rid, rc);
218
        return;
219
    }
4551 jermar 220
    async_wait_for(msg, &rc);
4518 jermar 221
    vfs_release_phone(phone);
4010 decky 222
 
4431 jermar 223
    if (rc == EOK) {
224
        rindex = (fs_index_t) IPC_GET_ARG1(answer);
225
        rsize = (size_t) IPC_GET_ARG2(answer);
226
        rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
227
 
228
        mr_res.triplet.fs_handle = fs_handle;
229
        mr_res.triplet.dev_handle = dev_handle;
230
        mr_res.triplet.index = rindex;
231
        mr_res.size = rsize;
232
        mr_res.lnkcnt = rlnkcnt;
233
        mr_res.type = VFS_NODE_DIRECTORY;
234
 
235
        /* Add reference to the mounted root. */
236
        mr_node = vfs_node_get(&mr_res);
237
        assert(mr_node);
238
    } else {
3109 jermar 239
        /* Mount failed, drop reference to mp_node. */
2689 jermar 240
        if (mp_node)
241
            vfs_node_put(mp_node);
242
    }
4409 jermar 243
 
2957 jermar 244
    ipc_answer_0(rid, rc);
4518 jermar 245
    fibril_rwlock_write_unlock(&namespace_rwlock);
2689 jermar 246
}
247
 
4010 decky 248
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
249
{
250
    /*
251
     * We expect the library to do the device-name to device-handle
252
     * translation for us, thus the device handle will arrive as ARG1
253
     * in the request.
254
     */
255
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
256
 
257
    /*
258
     * Mount flags are passed as ARG2.
259
     */
260
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
261
 
262
    /*
263
     * For now, don't make use of ARG3, but it can be used to
264
     * carry mount options in the future.
265
     */
266
 
267
    /* We want the client to send us the mount point. */
268
    ipc_callid_t callid;
269
    size_t size;
270
    if (!ipc_data_write_receive(&callid, &size)) {
271
        ipc_answer_0(callid, EINVAL);
272
        ipc_answer_0(rid, EINVAL);
273
        return;
274
    }
275
 
276
    /* Check whether size is reasonable wrt. the mount point. */
277
    if ((size < 1) || (size > MAX_PATH_LEN)) {
278
        ipc_answer_0(callid, EINVAL);
279
        ipc_answer_0(rid, EINVAL);
280
        return;
281
    }
282
 
283
    /* Allocate buffer for the mount point data being received. */
284
    char *mp = malloc(size + 1);
285
    if (!mp) {
286
        ipc_answer_0(callid, ENOMEM);
287
        ipc_answer_0(rid, ENOMEM);
288
        return;
289
    }
290
 
291
    /* Deliver the mount point. */
292
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
293
    if (retval != EOK) {
4301 jermar 294
        ipc_answer_0(rid, retval);
4010 decky 295
        free(mp);
296
        return;
297
    }
298
    mp[size] = '\0';
299
 
4305 jermar 300
    /* Now we expect to receive the mount options. */
301
    if (!ipc_data_write_receive(&callid, &size)) {
302
        ipc_answer_0(callid, EINVAL);
303
        ipc_answer_0(rid, EINVAL);
304
        free(mp);
305
        return;
306
    }
307
 
308
    /* Check the offered options size. */
309
    if (size < 0 || size > MAX_MNTOPTS_LEN) {
310
        ipc_answer_0(callid, EINVAL);
311
        ipc_answer_0(rid, EINVAL);
312
        free(mp);
313
        return;
314
    }
315
 
316
    /* Allocate buffer for the mount options. */
317
    char *opts = (char *) malloc(size + 1);
318
    if (!opts) {
319
        ipc_answer_0(callid, ENOMEM);
320
        ipc_answer_0(rid, ENOMEM);
321
        free(mp);
322
        return;
323
    }
324
 
325
    /* Deliver the mount options. */
326
    retval = ipc_data_write_finalize(callid, opts, size);
327
    if (retval != EOK) {
328
        ipc_answer_0(rid, retval);
329
        free(mp);
330
        free(opts);
331
        return;
332
    }
333
    opts[size] = '\0';
334
 
4010 decky 335
    /*
336
     * Now, we expect the client to send us data with the name of the file
337
     * system.
338
     */
339
    if (!ipc_data_write_receive(&callid, &size)) {
340
        ipc_answer_0(callid, EINVAL);
341
        ipc_answer_0(rid, EINVAL);
342
        free(mp);
4305 jermar 343
        free(opts);
4010 decky 344
        return;
345
    }
346
 
347
    /*
348
     * Don't receive more than is necessary for storing a full file system
349
     * name.
350
     */
351
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
352
        ipc_answer_0(callid, EINVAL);
353
        ipc_answer_0(rid, EINVAL);
354
        free(mp);
4305 jermar 355
        free(opts);
4010 decky 356
        return;
357
    }
358
 
359
    /*
360
     * Allocate buffer for file system name.
361
     */
362
    char *fs_name = (char *) malloc(size + 1);
363
    if (fs_name == NULL) {
364
        ipc_answer_0(callid, ENOMEM);
4301 jermar 365
        ipc_answer_0(rid, ENOMEM);
4010 decky 366
        free(mp);
4305 jermar 367
        free(opts);
4010 decky 368
        return;
369
    }
370
 
371
    /* Deliver the file system name. */
372
    retval = ipc_data_write_finalize(callid, fs_name, size);
373
    if (retval != EOK) {
4301 jermar 374
        ipc_answer_0(rid, retval);
4010 decky 375
        free(mp);
4305 jermar 376
        free(opts);
4010 decky 377
        free(fs_name);
378
        return;
379
    }
380
    fs_name[size] = '\0';
4302 jermar 381
 
4010 decky 382
    /*
4302 jermar 383
     * Wait for IPC_M_PING so that we can return an error if we don't know
384
     * fs_name.
385
     */
386
    ipc_call_t data;
387
    callid = async_get_call(&data);
388
    if (IPC_GET_METHOD(data) != IPC_M_PING) {
389
        ipc_answer_0(callid, ENOTSUP);
390
        ipc_answer_0(rid, ENOTSUP);
391
        free(mp);
4305 jermar 392
        free(opts);
4302 jermar 393
        free(fs_name);
394
        return;
395
    }
396
 
397
    /*
4010 decky 398
     * Check if we know a file system with the same name as is in fs_name.
399
     * This will also give us its file system handle.
400
     */
4539 jermar 401
    fibril_mutex_lock(&fs_head_lock);
4566 jermar 402
    fs_handle_t fs_handle;
403
recheck:
404
    fs_handle = fs_name_to_handle(fs_name, false);
4010 decky 405
    if (!fs_handle) {
406
        if (flags & IPC_FLAG_BLOCKING) {
4566 jermar 407
            fibril_condvar_wait(&fs_head_cv, &fs_head_lock);
408
            goto recheck;
4010 decky 409
        }
410
 
4539 jermar 411
        fibril_mutex_unlock(&fs_head_lock);
4010 decky 412
        ipc_answer_0(callid, ENOENT);
413
        ipc_answer_0(rid, ENOENT);
414
        free(mp);
415
        free(fs_name);
4305 jermar 416
        free(opts);
4010 decky 417
        return;
418
    }
4539 jermar 419
    fibril_mutex_unlock(&fs_head_lock);
4010 decky 420
 
421
    /* Acknowledge that we know fs_name. */
422
    ipc_answer_0(callid, EOK);
423
 
424
    /* Do the mount */
4305 jermar 425
    vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
4010 decky 426
    free(mp);
427
    free(fs_name);
4305 jermar 428
    free(opts);
4010 decky 429
}
430
 
2689 jermar 431
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
432
{
433
    if (!vfs_files_init()) {
434
        ipc_answer_0(rid, ENOMEM);
435
        return;
436
    }
4463 decky 437
 
2689 jermar 438
    /*
2700 jermar 439
     * The POSIX interface is open(path, oflag, mode).
4584 jermar 440
     * We can receive oflags and mode along with the VFS_IN_OPEN call;
441
     * the path will need to arrive in another call.
2700 jermar 442
     *
443
     * We also receive one private, non-POSIX set of flags called lflag
444
     * used to pass information to vfs_lookup_internal().
2689 jermar 445
     */
2700 jermar 446
    int lflag = IPC_GET_ARG1(*request);
447
    int oflag = IPC_GET_ARG2(*request);
448
    int mode = IPC_GET_ARG3(*request);
2689 jermar 449
    size_t len;
4463 decky 450
 
3653 jermar 451
    /*
452
     * Make sure that we are called with exactly one of L_FILE and
4463 decky 453
     * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
3653 jermar 454
     */
4518 jermar 455
    if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
456
        ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
457
        ((lflag & L_OPEN) != 0)) {
3653 jermar 458
        ipc_answer_0(rid, EINVAL);
459
        return;
460
    }
4463 decky 461
 
2708 jermar 462
    if (oflag & O_CREAT)
463
        lflag |= L_CREATE;
464
    if (oflag & O_EXCL)
465
        lflag |= L_EXCLUSIVE;
4463 decky 466
 
2689 jermar 467
    ipc_callid_t callid;
468
    if (!ipc_data_write_receive(&callid, &len)) {
469
        ipc_answer_0(callid, EINVAL);
470
        ipc_answer_0(rid, EINVAL);
471
        return;
472
    }
4463 decky 473
 
2752 jermar 474
    char *path = malloc(len + 1);
2689 jermar 475
    if (!path) {
476
        ipc_answer_0(callid, ENOMEM);
477
        ipc_answer_0(rid, ENOMEM);
478
        return;
479
    }
4463 decky 480
 
2689 jermar 481
    int rc;
482
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
483
        ipc_answer_0(rid, rc);
484
        free(path);
485
        return;
486
    }
2752 jermar 487
    path[len] = '\0';
2689 jermar 488
 
489
    /*
490
     * Avoid the race condition in which the file can be deleted before we
491
     * find/create-and-lock the VFS node corresponding to the looked-up
492
     * triplet.
493
     */
2708 jermar 494
    if (lflag & L_CREATE)
4518 jermar 495
        fibril_rwlock_write_lock(&namespace_rwlock);
2708 jermar 496
    else
4518 jermar 497
        fibril_rwlock_read_lock(&namespace_rwlock);
4463 decky 498
 
2707 jermar 499
    /* The path is now populated and we can call vfs_lookup_internal(). */
2691 jermar 500
    vfs_lookup_res_t lr;
4463 decky 501
    rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
502
    if (rc != EOK) {
2708 jermar 503
        if (lflag & L_CREATE)
4518 jermar 504
            fibril_rwlock_write_unlock(&namespace_rwlock);
2708 jermar 505
        else
4518 jermar 506
            fibril_rwlock_read_unlock(&namespace_rwlock);
2689 jermar 507
        ipc_answer_0(rid, rc);
508
        free(path);
509
        return;
510
    }
4463 decky 511
 
2748 jermar 512
    /* Path is no longer needed. */
2689 jermar 513
    free(path);
4463 decky 514
 
2691 jermar 515
    vfs_node_t *node = vfs_node_get(&lr);
2708 jermar 516
    if (lflag & L_CREATE)
4518 jermar 517
        fibril_rwlock_write_unlock(&namespace_rwlock);
2708 jermar 518
    else
4518 jermar 519
        fibril_rwlock_read_unlock(&namespace_rwlock);
4463 decky 520
 
2748 jermar 521
    /* Truncate the file if requested and if necessary. */
522
    if (oflag & O_TRUNC) {
4518 jermar 523
        fibril_rwlock_write_lock(&node->contents_rwlock);
2748 jermar 524
        if (node->size) {
525
            rc = vfs_truncate_internal(node->fs_handle,
526
                node->dev_handle, node->index, 0);
527
            if (rc) {
4518 jermar 528
                fibril_rwlock_write_unlock(&node->contents_rwlock);
2748 jermar 529
                vfs_node_put(node);
530
                ipc_answer_0(rid, rc);
531
                return;
532
            }
533
            node->size = 0;
534
        }
4518 jermar 535
        fibril_rwlock_write_unlock(&node->contents_rwlock);
2748 jermar 536
    }
4463 decky 537
 
2689 jermar 538
    /*
539
     * Get ourselves a file descriptor and the corresponding vfs_file_t
540
     * structure.
541
     */
542
    int fd = vfs_fd_alloc();
543
    if (fd < 0) {
544
        vfs_node_put(node);
545
        ipc_answer_0(rid, fd);
546
        return;
547
    }
548
    vfs_file_t *file = vfs_file_get(fd);
549
    file->node = node;
4463 decky 550
    if (oflag & O_APPEND)
2709 jermar 551
        file->append = true;
4463 decky 552
 
553
    /*
554
     * The following increase in reference count is for the fact that the
555
     * file is being opened and that a file structure is pointing to it.
556
     * It is necessary so that the file will not disappear when
557
     * vfs_node_put() is called. The reference will be dropped by the
4584 jermar 558
     * respective VFS_IN_CLOSE.
4463 decky 559
     */
560
    vfs_node_addref(node);
561
    vfs_node_put(node);
562
 
563
    /* Success! Return the new file descriptor to the client. */
564
    ipc_answer_1(rid, EOK, fd);
565
}
2689 jermar 566
 
4463 decky 567
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
568
{
569
    // FIXME: check for sanity of the supplied fs, dev and index
570
 
571
    if (!vfs_files_init()) {
572
        ipc_answer_0(rid, ENOMEM);
573
        return;
574
    }
575
 
2689 jermar 576
    /*
4463 decky 577
     * The interface is open_node(fs, dev, index, oflag).
578
     */
579
    vfs_lookup_res_t lr;
580
 
581
    lr.triplet.fs_handle = IPC_GET_ARG1(*request);
582
    lr.triplet.dev_handle = IPC_GET_ARG2(*request);
583
    lr.triplet.index = IPC_GET_ARG3(*request);
584
    int oflag = IPC_GET_ARG4(*request);
585
 
4518 jermar 586
    fibril_rwlock_read_lock(&namespace_rwlock);
4463 decky 587
 
588
    int rc = vfs_open_node_internal(&lr);
589
    if (rc != EOK) {
4518 jermar 590
        fibril_rwlock_read_unlock(&namespace_rwlock);
4463 decky 591
        ipc_answer_0(rid, rc);
592
        return;
593
    }
594
 
595
    vfs_node_t *node = vfs_node_get(&lr);
4518 jermar 596
    fibril_rwlock_read_unlock(&namespace_rwlock);
4463 decky 597
 
598
    /* Truncate the file if requested and if necessary. */
599
    if (oflag & O_TRUNC) {
4518 jermar 600
        fibril_rwlock_write_lock(&node->contents_rwlock);
4463 decky 601
        if (node->size) {
602
            rc = vfs_truncate_internal(node->fs_handle,
603
                node->dev_handle, node->index, 0);
604
            if (rc) {
4518 jermar 605
                fibril_rwlock_write_unlock(&node->contents_rwlock);
4463 decky 606
                vfs_node_put(node);
607
                ipc_answer_0(rid, rc);
608
                return;
609
            }
610
            node->size = 0;
611
        }
4518 jermar 612
        fibril_rwlock_write_unlock(&node->contents_rwlock);
4463 decky 613
    }
614
 
615
    /*
616
     * Get ourselves a file descriptor and the corresponding vfs_file_t
617
     * structure.
618
     */
619
    int fd = vfs_fd_alloc();
620
    if (fd < 0) {
621
        vfs_node_put(node);
622
        ipc_answer_0(rid, fd);
623
        return;
624
    }
625
    vfs_file_t *file = vfs_file_get(fd);
626
    file->node = node;
627
    if (oflag & O_APPEND)
628
        file->append = true;
629
 
630
    /*
2689 jermar 631
     * The following increase in reference count is for the fact that the
632
     * file is being opened and that a file structure is pointing to it.
633
     * It is necessary so that the file will not disappear when
634
     * vfs_node_put() is called. The reference will be dropped by the
4584 jermar 635
     * respective VFS_IN_CLOSE.
2689 jermar 636
     */
637
    vfs_node_addref(node);
638
    vfs_node_put(node);
4463 decky 639
 
2707 jermar 640
    /* Success! Return the new file descriptor to the client. */
2689 jermar 641
    ipc_answer_1(rid, EOK, fd);
642
}
643
 
4463 decky 644
void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
645
{
646
    int fd = IPC_GET_ARG1(*request);
647
 
648
    /* Lookup the file structure corresponding to the file descriptor. */
649
    vfs_file_t *file = vfs_file_get(fd);
650
    if (!file) {
651
        ipc_answer_0(rid, ENOENT);
652
        return;
653
    }
654
 
655
    /*
656
     * Lock the open file structure so that no other thread can manipulate
657
     * the same open file at a time.
658
     */
4518 jermar 659
    fibril_mutex_lock(&file->lock);
4463 decky 660
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
661
 
4584 jermar 662
    /* Make a VFS_OUT_SYMC request at the destination FS server. */
4463 decky 663
    aid_t msg;
664
    ipc_call_t answer;
4584 jermar 665
    msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle,
666
        file->node->index, &answer);
4518 jermar 667
 
4463 decky 668
    /* Wait for reply from the FS server. */
669
    ipcarg_t rc;
670
    async_wait_for(msg, &rc);
671
 
4551 jermar 672
    vfs_release_phone(fs_phone);
4518 jermar 673
    fibril_mutex_unlock(&file->lock);
4463 decky 674
 
3215 jermar 675
    ipc_answer_0(rid, rc);
2734 jermar 676
}
677
 
4463 decky 678
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
679
{
680
    int fd = IPC_GET_ARG1(*request);
681
 
682
    /* Lookup the file structure corresponding to the file descriptor. */
683
    vfs_file_t *file = vfs_file_get(fd);
684
    if (!file) {
685
        ipc_answer_0(rid, ENOENT);
686
        return;
687
    }
688
 
689
    /*
690
     * Lock the open file structure so that no other thread can manipulate
691
     * the same open file at a time.
692
     */
4518 jermar 693
    fibril_mutex_lock(&file->lock);
4463 decky 694
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
695
 
4584 jermar 696
    /* Make a VFS_OUT_CLOSE request at the destination FS server. */
4463 decky 697
    aid_t msg;
698
    ipc_call_t answer;
4584 jermar 699
    msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle,
700
        file->node->index, &answer);
4518 jermar 701
 
4463 decky 702
    /* Wait for reply from the FS server. */
703
    ipcarg_t rc;
704
    async_wait_for(msg, &rc);
4551 jermar 705
 
706
    vfs_release_phone(fs_phone);
4518 jermar 707
    fibril_mutex_unlock(&file->lock);
4463 decky 708
 
709
    int retval = IPC_GET_ARG1(answer);
710
    if (retval != EOK)
711
        ipc_answer_0(rid, retval);
712
 
713
    retval = vfs_fd_free(fd);
714
    ipc_answer_0(rid, retval);
715
}
716
 
2689 jermar 717
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
718
{
719
 
720
    /*
721
     * The following code strongly depends on the fact that the files data
722
     * structure can be only accessed by a single fibril and all file
723
     * operations are serialized (i.e. the reads and writes cannot
724
     * interleave and a file cannot be closed while it is being read).
725
     *
726
     * Additional synchronization needs to be added once the table of
727
     * open files supports parallel access!
728
     */
729
 
730
    int fd = IPC_GET_ARG1(*request);
3079 decky 731
 
2707 jermar 732
    /* Lookup the file structure corresponding to the file descriptor. */
2689 jermar 733
    vfs_file_t *file = vfs_file_get(fd);
734
    if (!file) {
735
        ipc_answer_0(rid, ENOENT);
736
        return;
737
    }
3079 decky 738
 
2689 jermar 739
    /*
740
     * Now we need to receive a call with client's
741
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
742
     */
743
    ipc_callid_t callid;
744
    int res;
745
    if (read)
746
        res = ipc_data_read_receive(&callid, NULL);
747
    else
748
        res = ipc_data_write_receive(&callid, NULL);
749
    if (!res) {
750
        ipc_answer_0(callid, EINVAL);
751
        ipc_answer_0(rid, EINVAL);
752
        return;
753
    }
3079 decky 754
 
2689 jermar 755
    /*
756
     * Lock the open file structure so that no other thread can manipulate
757
     * the same open file at a time.
758
     */
4518 jermar 759
    fibril_mutex_lock(&file->lock);
3653 jermar 760
 
2689 jermar 761
    /*
762
     * Lock the file's node so that no other client can read/write to it at
763
     * the same time.
764
     */
765
    if (read)
4518 jermar 766
        fibril_rwlock_read_lock(&file->node->contents_rwlock);
2689 jermar 767
    else
4518 jermar 768
        fibril_rwlock_write_lock(&file->node->contents_rwlock);
3653 jermar 769
 
770
    if (file->node->type == VFS_NODE_DIRECTORY) {
771
        /*
772
         * Make sure that no one is modifying the namespace
773
         * while we are in readdir().
774
         */
775
        assert(read);
4518 jermar 776
        fibril_rwlock_read_lock(&namespace_rwlock);
3653 jermar 777
    }
3079 decky 778
 
2689 jermar 779
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
780
 
2707 jermar 781
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
2689 jermar 782
    aid_t msg;
783
    ipc_call_t answer;
2709 jermar 784
    if (!read && file->append)
785
        file->pos = file->node->size;
4584 jermar 786
    msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE,
2689 jermar 787
        file->node->dev_handle, file->node->index, file->pos, &answer);
788
 
789
    /*
790
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
791
     * destination FS server. The call will be routed as if sent by
792
     * ourselves. Note that call arguments are immutable in this case so we
793
     * don't have to bother.
794
     */
795
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
4518 jermar 796
 
2707 jermar 797
    /* Wait for reply from the FS server. */
2689 jermar 798
    ipcarg_t rc;
799
    async_wait_for(msg, &rc);
4463 decky 800
 
4551 jermar 801
    vfs_release_phone(fs_phone);
802
 
2689 jermar 803
    size_t bytes = IPC_GET_ARG1(answer);
3653 jermar 804
 
805
    if (file->node->type == VFS_NODE_DIRECTORY)
4518 jermar 806
        fibril_rwlock_read_unlock(&namespace_rwlock);
3079 decky 807
 
2707 jermar 808
    /* Unlock the VFS node. */
2689 jermar 809
    if (read)
4518 jermar 810
        fibril_rwlock_read_unlock(&file->node->contents_rwlock);
2689 jermar 811
    else {
812
        /* Update the cached version of node's size. */
2710 jermar 813
        if (rc == EOK)
814
            file->node->size = IPC_GET_ARG2(answer);
4518 jermar 815
        fibril_rwlock_write_unlock(&file->node->contents_rwlock);
2689 jermar 816
    }
3079 decky 817
 
2707 jermar 818
    /* Update the position pointer and unlock the open file. */
2710 jermar 819
    if (rc == EOK)
820
        file->pos += bytes;
4518 jermar 821
    fibril_mutex_unlock(&file->lock);
3079 decky 822
 
2689 jermar 823
    /*
824
     * FS server's reply is the final result of the whole operation we
825
     * return to the client.
826
     */
827
    ipc_answer_1(rid, rc, bytes);
828
}
829
 
830
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
831
{
832
    vfs_rdwr(rid, request, true);
833
}
834
 
835
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
836
{
837
    vfs_rdwr(rid, request, false);
838
}
839
 
840
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
841
{
842
    int fd = (int) IPC_GET_ARG1(*request);
843
    off_t off = (off_t) IPC_GET_ARG2(*request);
844
    int whence = (int) IPC_GET_ARG3(*request);
845
 
846
 
2707 jermar 847
    /* Lookup the file structure corresponding to the file descriptor. */
2689 jermar 848
    vfs_file_t *file = vfs_file_get(fd);
849
    if (!file) {
850
        ipc_answer_0(rid, ENOENT);
851
        return;
852
    }
853
 
854
    off_t newpos;
4518 jermar 855
    fibril_mutex_lock(&file->lock);
2689 jermar 856
    if (whence == SEEK_SET) {
857
        file->pos = off;
4518 jermar 858
        fibril_mutex_unlock(&file->lock);
2689 jermar 859
        ipc_answer_1(rid, EOK, off);
860
        return;
861
    }
862
    if (whence == SEEK_CUR) {
863
        if (file->pos + off < file->pos) {
4518 jermar 864
            fibril_mutex_unlock(&file->lock);
2689 jermar 865
            ipc_answer_0(rid, EOVERFLOW);
866
            return;
867
        }
868
        file->pos += off;
869
        newpos = file->pos;
4518 jermar 870
        fibril_mutex_unlock(&file->lock);
2689 jermar 871
        ipc_answer_1(rid, EOK, newpos);
872
        return;
873
    }
874
    if (whence == SEEK_END) {
4518 jermar 875
        fibril_rwlock_read_lock(&file->node->contents_rwlock);
2689 jermar 876
        size_t size = file->node->size;
4518 jermar 877
        fibril_rwlock_read_unlock(&file->node->contents_rwlock);
2689 jermar 878
        if (size + off < size) {
4518 jermar 879
            fibril_mutex_unlock(&file->lock);
2689 jermar 880
            ipc_answer_0(rid, EOVERFLOW);
881
            return;
882
        }
883
        newpos = size + off;
4518 jermar 884
        fibril_mutex_unlock(&file->lock);
2689 jermar 885
        ipc_answer_1(rid, EOK, newpos);
886
        return;
887
    }
4518 jermar 888
    fibril_mutex_unlock(&file->lock);
2689 jermar 889
    ipc_answer_0(rid, EINVAL);
890
}
891
 
2770 jermar 892
int
893
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
894
    fs_index_t index, size_t size)
2748 jermar 895
{
896
    ipcarg_t rc;
897
    int fs_phone;
898
 
899
    fs_phone = vfs_grab_phone(fs_handle);
4584 jermar 900
    rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle,
2748 jermar 901
        (ipcarg_t)index, (ipcarg_t)size);
902
    vfs_release_phone(fs_phone);
903
    return (int)rc;
904
}
905
 
2693 jermar 906
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
907
{
908
    int fd = IPC_GET_ARG1(*request);
909
    size_t size = IPC_GET_ARG2(*request);
2748 jermar 910
    int rc;
2693 jermar 911
 
912
    vfs_file_t *file = vfs_file_get(fd);
913
    if (!file) {
914
        ipc_answer_0(rid, ENOENT);
915
        return;
916
    }
4518 jermar 917
    fibril_mutex_lock(&file->lock);
2693 jermar 918
 
4518 jermar 919
    fibril_rwlock_write_lock(&file->node->contents_rwlock);
2748 jermar 920
    rc = vfs_truncate_internal(file->node->fs_handle,
921
        file->node->dev_handle, file->node->index, size);
2693 jermar 922
    if (rc == EOK)
923
        file->node->size = size;
4518 jermar 924
    fibril_rwlock_write_unlock(&file->node->contents_rwlock);
2693 jermar 925
 
4518 jermar 926
    fibril_mutex_unlock(&file->lock);
2748 jermar 927
    ipc_answer_0(rid, (ipcarg_t)rc);
2693 jermar 928
}
929
 
4585 jermar 930
void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
931
{
932
    int fd = IPC_GET_ARG1(*request);
933
    size_t size = IPC_GET_ARG2(*request);
934
    ipcarg_t rc;
935
 
936
    vfs_file_t *file = vfs_file_get(fd);
937
    if (!file) {
938
        ipc_answer_0(rid, ENOENT);
939
        return;
940
    }
941
 
942
    ipc_callid_t callid;
943
    if (!ipc_data_read_receive(&callid, NULL)) {
944
        ipc_answer_0(callid, EINVAL);
945
        ipc_answer_0(rid, EINVAL);
946
        return;
947
    }
948
 
949
    fibril_mutex_lock(&file->lock);
950
 
951
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
952
 
953
    aid_t msg;
954
    msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle,
955
        file->node->index, true, NULL);
956
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
957
    async_wait_for(msg, &rc);
958
    vfs_release_phone(fs_phone);
959
 
960
    fibril_mutex_unlock(&file->lock);
961
    ipc_answer_0(rid, rc);
962
}
963
 
964
void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
965
{
4587 jermar 966
    size_t len;
967
    ipc_callid_t callid;
968
 
969
    if (!ipc_data_write_receive(&callid, &len)) {
970
        ipc_answer_0(callid, EINVAL);
971
        ipc_answer_0(rid, EINVAL);
972
        return;
973
    }
974
    char *path = malloc(len + 1);
975
    if (!path) {
976
        ipc_answer_0(callid, ENOMEM);
977
        ipc_answer_0(rid, ENOMEM);
978
        return;
979
    }
980
    int rc;
981
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
982
        ipc_answer_0(rid, rc);
983
        free(path);
984
        return;
985
    }
986
    path[len] = '\0';
987
 
988
    if (!ipc_data_read_receive(&callid, NULL)) {
989
        free(path);
990
        ipc_answer_0(callid, EINVAL);
991
        ipc_answer_0(rid, EINVAL);
992
        return;
993
    }
994
 
995
    vfs_lookup_res_t lr;
996
    fibril_rwlock_read_lock(&namespace_rwlock);
997
    rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
998
    free(path);
999
    if (rc != EOK) {
1000
        fibril_rwlock_read_unlock(&namespace_rwlock);
1001
        ipc_answer_0(callid, rc);
1002
        ipc_answer_0(rid, rc);
1003
        return;
1004
    }
1005
    vfs_node_t *node = vfs_node_get(&lr);
1006
    if (!node) {
1007
        fibril_rwlock_read_unlock(&namespace_rwlock);
1008
        ipc_answer_0(callid, ENOMEM);
1009
        ipc_answer_0(rid, ENOMEM);
1010
        return;
1011
    }
1012
 
1013
    fibril_rwlock_read_unlock(&namespace_rwlock);
1014
 
1015
    int fs_phone = vfs_grab_phone(node->fs_handle);
1016
    aid_t msg;
1017
    msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle,
1018
        node->index, false, NULL);
1019
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
4593 decky 1020
 
1021
    ipcarg_t rv;
1022
    async_wait_for(msg, &rv);
4587 jermar 1023
    vfs_release_phone(fs_phone);
1024
 
4593 decky 1025
    ipc_answer_0(rid, rv);
4587 jermar 1026
 
1027
    vfs_node_put(node);
4585 jermar 1028
}
1029
 
2707 jermar 1030
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1031
{
1032
    int mode = IPC_GET_ARG1(*request);
2735 jermar 1033
 
2707 jermar 1034
    size_t len;
1035
    ipc_callid_t callid;
1036
 
1037
    if (!ipc_data_write_receive(&callid, &len)) {
1038
        ipc_answer_0(callid, EINVAL);
1039
        ipc_answer_0(rid, EINVAL);
1040
        return;
1041
    }
2752 jermar 1042
    char *path = malloc(len + 1);
2707 jermar 1043
    if (!path) {
1044
        ipc_answer_0(callid, ENOMEM);
1045
        ipc_answer_0(rid, ENOMEM);
1046
        return;
1047
    }
1048
    int rc;
1049
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1050
        ipc_answer_0(rid, rc);
1051
        free(path);
1052
        return;
1053
    }
2752 jermar 1054
    path[len] = '\0';
2707 jermar 1055
 
4518 jermar 1056
    fibril_rwlock_write_lock(&namespace_rwlock);
2707 jermar 1057
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
2752 jermar 1058
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
4518 jermar 1059
    fibril_rwlock_write_unlock(&namespace_rwlock);
2707 jermar 1060
    free(path);
1061
    ipc_answer_0(rid, rc);
1062
}
1063
 
2735 jermar 1064
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1065
{
1066
    int lflag = IPC_GET_ARG1(*request);
1067
 
1068
    size_t len;
1069
    ipc_callid_t callid;
1070
 
1071
    if (!ipc_data_write_receive(&callid, &len)) {
1072
        ipc_answer_0(callid, EINVAL);
1073
        ipc_answer_0(rid, EINVAL);
1074
        return;
1075
    }
2752 jermar 1076
    char *path = malloc(len + 1);
2735 jermar 1077
    if (!path) {
1078
        ipc_answer_0(callid, ENOMEM);
1079
        ipc_answer_0(rid, ENOMEM);
1080
        return;
1081
    }
1082
    int rc;
1083
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1084
        ipc_answer_0(rid, rc);
1085
        free(path);
1086
        return;
1087
    }
2752 jermar 1088
    path[len] = '\0';
2735 jermar 1089
 
4518 jermar 1090
    fibril_rwlock_write_lock(&namespace_rwlock);
2735 jermar 1091
    lflag &= L_DIRECTORY;   /* sanitize lflag */
1092
    vfs_lookup_res_t lr;
2763 jermar 1093
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
2735 jermar 1094
    free(path);
1095
    if (rc != EOK) {
4518 jermar 1096
        fibril_rwlock_write_unlock(&namespace_rwlock);
2735 jermar 1097
        ipc_answer_0(rid, rc);
1098
        return;
1099
    }
1100
 
1101
    /*
1102
     * The name has already been unlinked by vfs_lookup_internal().
1103
     * We have to get and put the VFS node to ensure that it is
4584 jermar 1104
     * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
2735 jermar 1105
     */
1106
    vfs_node_t *node = vfs_node_get(&lr);
4555 jermar 1107
    fibril_mutex_lock(&nodes_mutex);
2735 jermar 1108
    node->lnkcnt--;
4555 jermar 1109
    fibril_mutex_unlock(&nodes_mutex);
4518 jermar 1110
    fibril_rwlock_write_unlock(&namespace_rwlock);
2735 jermar 1111
    vfs_node_put(node);
1112
    ipc_answer_0(rid, EOK);
1113
}
1114
 
2763 jermar 1115
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1116
{
4279 svoboda 1117
    size_t olen, nlen;
2763 jermar 1118
    ipc_callid_t callid;
1119
    int rc;
1120
 
1121
    /* Retrieve the old path. */
4279 svoboda 1122
    if (!ipc_data_write_receive(&callid, &olen)) {
2763 jermar 1123
        ipc_answer_0(callid, EINVAL);
1124
        ipc_answer_0(rid, EINVAL);
1125
        return;
1126
    }
4279 svoboda 1127
    char *old = malloc(olen + 1);
2763 jermar 1128
    if (!old) {
1129
        ipc_answer_0(callid, ENOMEM);
1130
        ipc_answer_0(rid, ENOMEM);
1131
        return;
1132
    }
4279 svoboda 1133
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
2763 jermar 1134
        ipc_answer_0(rid, rc);
1135
        free(old);
1136
        return;
1137
    }
4279 svoboda 1138
    old[olen] = '\0';
2763 jermar 1139
 
1140
    /* Retrieve the new path. */
4279 svoboda 1141
    if (!ipc_data_write_receive(&callid, &nlen)) {
2763 jermar 1142
        ipc_answer_0(callid, EINVAL);
1143
        ipc_answer_0(rid, EINVAL);
1144
        free(old);
1145
        return;
1146
    }
4279 svoboda 1147
    char *new = malloc(nlen + 1);
2763 jermar 1148
    if (!new) {
1149
        ipc_answer_0(callid, ENOMEM);
1150
        ipc_answer_0(rid, ENOMEM);
1151
        free(old);
1152
        return;
1153
    }
4279 svoboda 1154
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
2763 jermar 1155
        ipc_answer_0(rid, rc);
1156
        free(old);
1157
        free(new);
1158
        return;
1159
    }
4279 svoboda 1160
    new[nlen] = '\0';
2763 jermar 1161
 
4279 svoboda 1162
    char *oldc = canonify(old, &olen);
1163
    char *newc = canonify(new, &nlen);
2763 jermar 1164
    if (!oldc || !newc) {
1165
        ipc_answer_0(rid, EINVAL);
1166
        free(old);
1167
        free(new);
1168
        return;
1169
    }
4279 svoboda 1170
    oldc[olen] = '\0';
1171
    newc[nlen] = '\0';
4366 jermar 1172
    if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1173
        ((newc[str_length(oldc)] == '/') ||
1174
        (str_length(oldc) == 1) ||
1175
        (str_length(oldc) == str_length(newc)))) {
1176
            /*
1177
         * oldc is a prefix of newc and either
1178
         * - newc continues with a / where oldc ends, or
1179
         * - oldc was / itself, or
1180
         * - oldc and newc are equal.
1181
         */
2763 jermar 1182
        ipc_answer_0(rid, EINVAL);
1183
        free(old);
1184
        free(new);
1185
        return;
1186
    }
1187
 
1188
    vfs_lookup_res_t old_lr;
1189
    vfs_lookup_res_t new_lr;
1190
    vfs_lookup_res_t new_par_lr;
4518 jermar 1191
    fibril_rwlock_write_lock(&namespace_rwlock);
2763 jermar 1192
    /* Lookup the node belonging to the old file name. */
1193
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1194
    if (rc != EOK) {
4518 jermar 1195
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1196
        ipc_answer_0(rid, rc);
1197
        free(old);
1198
        free(new);
1199
        return;
1200
    }
1201
    vfs_node_t *old_node = vfs_node_get(&old_lr);
1202
    if (!old_node) {
4518 jermar 1203
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1204
        ipc_answer_0(rid, ENOMEM);
1205
        free(old);
1206
        free(new);
1207
        return;
1208
    }
4368 jermar 1209
    /* Determine the path to the parent of the node with the new name. */
1210
    char *parentc = str_dup(newc);
1211
    if (!parentc) {
4518 jermar 1212
        fibril_rwlock_write_unlock(&namespace_rwlock);
4368 jermar 1213
        ipc_answer_0(rid, rc);
1214
        free(old);
1215
        free(new);
1216
        return;
1217
    }
4427 jermar 1218
    char *lastsl = str_rchr(parentc + 1, '/');
4368 jermar 1219
    if (lastsl)
1220
        *lastsl = '\0';
1221
    else
1222
        parentc[1] = '\0';
2763 jermar 1223
    /* Lookup parent of the new file name. */
4368 jermar 1224
    rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1225
    free(parentc);  /* not needed anymore */
2763 jermar 1226
    if (rc != EOK) {
4518 jermar 1227
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1228
        ipc_answer_0(rid, rc);
1229
        free(old);
1230
        free(new);
1231
        return;
1232
    }
1233
    /* Check whether linking to the same file system instance. */
1234
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1235
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
4518 jermar 1236
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1237
        ipc_answer_0(rid, EXDEV);   /* different file systems */
1238
        free(old);
1239
        free(new);
1240
        return;
1241
    }
1242
    /* Destroy the old link for the new name. */
1243
    vfs_node_t *new_node = NULL;
1244
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1245
    switch (rc) {
1246
    case ENOENT:
1247
        /* simply not in our way */
1248
        break;
1249
    case EOK:
1250
        new_node = vfs_node_get(&new_lr);
1251
        if (!new_node) {
4518 jermar 1252
            fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1253
            ipc_answer_0(rid, ENOMEM);
1254
            free(old);
1255
            free(new);
1256
            return;
1257
        }
4555 jermar 1258
        fibril_mutex_lock(&nodes_mutex);
2763 jermar 1259
        new_node->lnkcnt--;
4555 jermar 1260
        fibril_mutex_unlock(&nodes_mutex);
2763 jermar 1261
        break;
1262
    default:
4518 jermar 1263
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1264
        ipc_answer_0(rid, ENOTEMPTY);
1265
        free(old);
1266
        free(new);
1267
        return;
1268
    }
1269
    /* Create the new link for the new name. */
1270
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1271
    if (rc != EOK) {
4518 jermar 1272
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1273
        if (new_node)
1274
            vfs_node_put(new_node);
1275
        ipc_answer_0(rid, rc);
1276
        free(old);
1277
        free(new);
1278
        return;
1279
    }
4555 jermar 1280
    fibril_mutex_lock(&nodes_mutex);
2763 jermar 1281
    old_node->lnkcnt++;
4555 jermar 1282
    fibril_mutex_unlock(&nodes_mutex);
2763 jermar 1283
    /* Destroy the link for the old name. */
1284
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1285
    if (rc != EOK) {
4518 jermar 1286
        fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1287
        vfs_node_put(old_node);
1288
        if (new_node)
1289
            vfs_node_put(new_node);
1290
        ipc_answer_0(rid, rc);
1291
        free(old);
1292
        free(new);
1293
        return;
1294
    }
4555 jermar 1295
    fibril_mutex_lock(&nodes_mutex);
2763 jermar 1296
    old_node->lnkcnt--;
4555 jermar 1297
    fibril_mutex_unlock(&nodes_mutex);
4518 jermar 1298
    fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1299
    vfs_node_put(old_node);
1300
    if (new_node)
1301
        vfs_node_put(new_node);
1302
    free(old);
1303
    free(new);
1304
    ipc_answer_0(rid, EOK);
1305
}
1306
 
2689 jermar 1307
/**
1308
 * @}
4463 decky 1309
 */