Subversion Repositories HelenOS

Rev

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