Subversion Repositories HelenOS

Rev

Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4327 Rev 4581
Line 41... Line 41...
41
#include <errno.h>
41
#include <errno.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <stdlib.h>
43
#include <stdlib.h>
44
#include <string.h>
44
#include <string.h>
45
#include <bool.h>
45
#include <bool.h>
46
#include <futex.h>
-
 
47
#include <rwlock.h>
46
#include <fibril_sync.h>
48
#include <libadt/list.h>
47
#include <adt/list.h>
49
#include <unistd.h>
48
#include <unistd.h>
50
#include <ctype.h>
49
#include <ctype.h>
51
#include <fcntl.h>
50
#include <fcntl.h>
52
#include <assert.h>
51
#include <assert.h>
53
#include <vfs/canonify.h>
52
#include <vfs/canonify.h>
54
 
53
 
55
/* Forward declarations of static functions. */
54
/* Forward declarations of static functions. */
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
55
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
57
 
56
 
58
/** Pending mount structure. */
-
 
59
typedef struct {
-
 
60
    link_t link;
-
 
61
    char *fs_name;            /**< File system name */
-
 
62
    char *mp;                 /**< Mount point */
-
 
63
    char *opts;       /**< Mount options. */
-
 
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
 
-
 
69
LIST_INITIALIZE(pending_req);
-
 
70
 
-
 
71
/**
57
/**
72
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
58
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
73
 * concurrent VFS operation which modifies the file system namespace.
59
 * concurrent VFS operation which modifies the file system namespace.
74
 */
60
 */
75
RWLOCK_INITIALIZE(namespace_rwlock);
61
FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
76
 
62
 
77
futex_t rootfs_futex = FUTEX_INITIALIZER;
-
 
78
vfs_pair_t rootfs = {
63
vfs_pair_t rootfs = {
79
    .fs_handle = 0,
64
    .fs_handle = 0,
80
    .dev_handle = 0
65
    .dev_handle = 0
81
};
66
};
82
 
67
 
83
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
68
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
84
    fs_handle_t fs_handle, char *mp, char *opts)
69
    fs_handle_t fs_handle, char *mp, char *opts)
85
{
70
{
86
    vfs_lookup_res_t mp_res;
71
    vfs_lookup_res_t mp_res;
-
 
72
    vfs_lookup_res_t mr_res;
87
    vfs_node_t *mp_node = NULL;
73
    vfs_node_t *mp_node = NULL;
-
 
74
    vfs_node_t *mr_node;
-
 
75
    fs_index_t rindex;
-
 
76
    size_t rsize;
-
 
77
    unsigned rlnkcnt;
88
    ipcarg_t rc;
78
    ipcarg_t rc;
89
    int phone;
79
    int phone;
90
    aid_t msg;
80
    aid_t msg;
91
    ipc_call_t answer;
81
    ipc_call_t answer;
92
 
82
   
93
    /* Resolve the path to the mountpoint. */
83
    /* Resolve the path to the mountpoint. */
94
    futex_down(&rootfs_futex);
84
    fibril_rwlock_write_lock(&namespace_rwlock);
95
    if (rootfs.fs_handle) {
85
    if (rootfs.fs_handle) {
96
        /* We already have the root FS. */
86
        /* We already have the root FS. */
97
        rwlock_write_lock(&namespace_rwlock);
-
 
98
        if (str_cmp(mp, "/") == 0) {
87
        if (str_cmp(mp, "/") == 0) {
99
            /* Trying to mount root FS over root FS */
88
            /* Trying to mount root FS over root FS */
100
            rwlock_write_unlock(&namespace_rwlock);
89
            fibril_rwlock_write_unlock(&namespace_rwlock);
101
            futex_up(&rootfs_futex);
-
 
102
            ipc_answer_0(rid, EBUSY);
90
            ipc_answer_0(rid, EBUSY);
103
            return;
91
            return;
104
        }
92
        }
105
       
93
       
106
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
94
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
107
        if (rc != EOK) {
95
        if (rc != EOK) {
108
            /* The lookup failed for some reason. */
96
            /* The lookup failed for some reason. */
109
            rwlock_write_unlock(&namespace_rwlock);
97
            fibril_rwlock_write_unlock(&namespace_rwlock);
110
            futex_up(&rootfs_futex);
-
 
111
            ipc_answer_0(rid, rc);
98
            ipc_answer_0(rid, rc);
112
            return;
99
            return;
113
        }
100
        }
114
       
101
       
115
        mp_node = vfs_node_get(&mp_res);
102
        mp_node = vfs_node_get(&mp_res);
116
        if (!mp_node) {
103
        if (!mp_node) {
117
            rwlock_write_unlock(&namespace_rwlock);
104
            fibril_rwlock_write_unlock(&namespace_rwlock);
118
            futex_up(&rootfs_futex);
-
 
119
            ipc_answer_0(rid, ENOMEM);
105
            ipc_answer_0(rid, ENOMEM);
120
            return;
106
            return;
121
        }
107
        }
122
       
108
       
123
        /*
109
        /*
124
         * Now we hold a reference to mp_node.
110
         * Now we hold a reference to mp_node.
125
         * It will be dropped upon the corresponding VFS_UNMOUNT.
111
         * It will be dropped upon the corresponding VFS_UNMOUNT.
126
         * This prevents the mount point from being deleted.
112
         * This prevents the mount point from being deleted.
127
         */
113
         */
128
        rwlock_write_unlock(&namespace_rwlock);
-
 
129
    } else {
114
    } else {
130
        /* We still don't have the root file system mounted. */
115
        /* We still don't have the root file system mounted. */
131
        if (str_cmp(mp, "/") == 0) {
116
        if (str_cmp(mp, "/") == 0) {
132
            vfs_lookup_res_t mr_res;
-
 
133
            vfs_node_t *mr_node;
-
 
134
            fs_index_t rindex;
-
 
135
            size_t rsize;
-
 
136
            unsigned rlnkcnt;
-
 
137
           
-
 
138
            /*
117
            /*
139
             * For this simple, but important case,
118
             * For this simple, but important case,
140
             * we are almost done.
119
             * we are almost done.
141
             */
120
             */
142
           
121
           
Line 148... Line 127...
148
            rc = ipc_data_write_start(phone, (void *)opts,
127
            rc = ipc_data_write_start(phone, (void *)opts,
149
                str_size(opts));
128
                str_size(opts));
150
            if (rc != EOK) {
129
            if (rc != EOK) {
151
                async_wait_for(msg, NULL);
130
                async_wait_for(msg, NULL);
152
                vfs_release_phone(phone);
131
                vfs_release_phone(phone);
153
                futex_up(&rootfs_futex);
132
                fibril_rwlock_write_unlock(&namespace_rwlock);
154
                ipc_answer_0(rid, rc);
133
                ipc_answer_0(rid, rc);
155
                return;
134
                return;
156
            }
135
            }
157
            async_wait_for(msg, &rc);
136
            async_wait_for(msg, &rc);
158
            vfs_release_phone(phone);
137
            vfs_release_phone(phone);
159
           
138
           
160
            if (rc != EOK) {
139
            if (rc != EOK) {
161
                futex_up(&rootfs_futex);
140
                fibril_rwlock_write_unlock(&namespace_rwlock);
162
                ipc_answer_0(rid, rc);
141
                ipc_answer_0(rid, rc);
163
                return;
142
                return;
164
            }
143
            }
165
 
144
 
166
            rindex = (fs_index_t) IPC_GET_ARG1(answer);
145
            rindex = (fs_index_t) IPC_GET_ARG1(answer);
Line 174... Line 153...
174
            mr_res.lnkcnt = rlnkcnt;
153
            mr_res.lnkcnt = rlnkcnt;
175
            mr_res.type = VFS_NODE_DIRECTORY;
154
            mr_res.type = VFS_NODE_DIRECTORY;
176
           
155
           
177
            rootfs.fs_handle = fs_handle;
156
            rootfs.fs_handle = fs_handle;
178
            rootfs.dev_handle = dev_handle;
157
            rootfs.dev_handle = dev_handle;
179
            futex_up(&rootfs_futex);
-
 
180
           
158
           
181
            /* Add reference to the mounted root. */
159
            /* Add reference to the mounted root. */
182
            mr_node = vfs_node_get(&mr_res);
160
            mr_node = vfs_node_get(&mr_res);
183
            assert(mr_node);
161
            assert(mr_node);
184
           
162
           
-
 
163
            fibril_rwlock_write_unlock(&namespace_rwlock);
185
            ipc_answer_0(rid, rc);
164
            ipc_answer_0(rid, rc);
186
            return;
165
            return;
187
        } else {
166
        } else {
188
            /*
167
            /*
189
             * We can't resolve this without the root filesystem
168
             * We can't resolve this without the root filesystem
190
             * being mounted first.
169
             * being mounted first.
191
             */
170
             */
192
            futex_up(&rootfs_futex);
171
            fibril_rwlock_write_unlock(&namespace_rwlock);
193
            ipc_answer_0(rid, ENOENT);
172
            ipc_answer_0(rid, ENOENT);
194
            return;
173
            return;
195
        }
174
        }
196
    }
175
    }
197
    futex_up(&rootfs_futex);
-
 
198
   
176
   
199
    /*
177
    /*
200
     * At this point, we have all necessary pieces: file system and device
178
     * At this point, we have all necessary pieces: file system and device
201
     * handles, and we know the mount point VFS node.
179
     * handles, and we know the mount point VFS node.
202
     */
180
     */
203
   
181
   
-
 
182
    int mountee_phone = vfs_grab_phone(fs_handle);
-
 
183
    assert(mountee_phone >= 0);
-
 
184
 
204
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
185
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
205
    msg = async_send_4(phone, VFS_MOUNT,
186
    msg = async_send_4(phone, VFS_MOUNT,
206
        (ipcarg_t) mp_res.triplet.dev_handle,
187
        (ipcarg_t) mp_res.triplet.dev_handle,
207
        (ipcarg_t) mp_res.triplet.index,
188
        (ipcarg_t) mp_res.triplet.index,
208
        (ipcarg_t) fs_handle,
189
        (ipcarg_t) fs_handle,
209
        (ipcarg_t) dev_handle, &answer);
190
        (ipcarg_t) dev_handle, &answer);
-
 
191
   
-
 
192
    /* send connection */
-
 
193
    rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
-
 
194
    if (rc != EOK) {
-
 
195
        async_wait_for(msg, NULL);
-
 
196
        vfs_release_phone(mountee_phone);
-
 
197
        vfs_release_phone(phone);
-
 
198
        /* Mount failed, drop reference to mp_node. */
-
 
199
        if (mp_node)
-
 
200
            vfs_node_put(mp_node);
-
 
201
        ipc_answer_0(rid, rc);
-
 
202
        fibril_rwlock_write_unlock(&namespace_rwlock);
-
 
203
        return;
-
 
204
    }
-
 
205
 
-
 
206
    vfs_release_phone(mountee_phone);
-
 
207
   
210
    /* send the mount options */
208
    /* send the mount options */
211
    rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
209
    rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
212
    if (rc != EOK) {
210
    if (rc != EOK) {
213
        async_wait_for(msg, NULL);
211
        async_wait_for(msg, NULL);
214
        vfs_release_phone(phone);
212
        vfs_release_phone(phone);
215
        /* Mount failed, drop reference to mp_node. */
213
        /* Mount failed, drop reference to mp_node. */
216
        if (mp_node)
214
        if (mp_node)
217
            vfs_node_put(mp_node);
215
            vfs_node_put(mp_node);
-
 
216
        fibril_rwlock_write_unlock(&namespace_rwlock);
218
        ipc_answer_0(rid, rc);
217
        ipc_answer_0(rid, rc);
219
        return;
218
        return;
220
    }
219
    }
221
    async_wait_for(msg, &rc);
220
    async_wait_for(msg, &rc);
222
    vfs_release_phone(phone);
221
    vfs_release_phone(phone);
223
   
222
   
224
    if (rc != EOK) {
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 {
225
        /* Mount failed, drop reference to mp_node. */
239
        /* Mount failed, drop reference to mp_node. */
226
        if (mp_node)
240
        if (mp_node)
227
            vfs_node_put(mp_node);
241
            vfs_node_put(mp_node);
228
    }
242
    }
229
   
-
 
230
    ipc_answer_0(rid, rc);
-
 
231
}
-
 
232
 
243
 
233
/** Process pending mount requests */
-
 
234
void vfs_process_pending_mount()
-
 
235
{
-
 
236
    link_t *cur;
-
 
237
   
-
 
238
loop:
-
 
239
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
-
 
240
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
-
 
241
       
-
 
242
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
-
 
243
        if (!fs_handle)
-
 
244
            continue;
-
 
245
       
-
 
246
        /* Acknowledge that we know fs_name. */
-
 
247
        ipc_answer_0(pr->callid, EOK);
244
    ipc_answer_0(rid, rc);
248
       
-
 
249
        /* Do the mount */
-
 
250
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp,
245
    fibril_rwlock_write_unlock(&namespace_rwlock);
251
            pr->opts);
-
 
252
       
-
 
253
        free(pr->fs_name);
-
 
254
        free(pr->mp);
-
 
255
        free(pr->opts);
-
 
256
        list_remove(cur);
-
 
257
        free(pr);
-
 
258
        goto loop;
-
 
259
    }
-
 
260
}
246
}
261
 
247
 
262
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
248
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
263
{
249
{
264
    /*
250
    /*
Line 410... Line 396...
410
 
396
 
411
    /*
397
    /*
412
     * Check if we know a file system with the same name as is in fs_name.
398
     * Check if we know a file system with the same name as is in fs_name.
413
     * This will also give us its file system handle.
399
     * This will also give us its file system handle.
414
     */
400
     */
-
 
401
    fibril_mutex_lock(&fs_head_lock);
-
 
402
    fs_handle_t fs_handle;
-
 
403
recheck:
415
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
404
    fs_handle = fs_name_to_handle(fs_name, false);
416
    if (!fs_handle) {
405
    if (!fs_handle) {
417
        if (flags & IPC_FLAG_BLOCKING) {
406
        if (flags & IPC_FLAG_BLOCKING) {
418
            pending_req_t *pr;
-
 
419
 
-
 
420
            /* Blocking mount, add to pending list */
-
 
421
            pr = (pending_req_t *) malloc(sizeof(pending_req_t));
407
            fibril_condvar_wait(&fs_head_cv, &fs_head_lock);
422
            if (!pr) {
-
 
423
                ipc_answer_0(callid, ENOMEM);
-
 
424
                ipc_answer_0(rid, ENOMEM);
-
 
425
                free(mp);
408
            goto recheck;
426
                free(fs_name);
-
 
427
                free(opts);
-
 
428
                return;
-
 
429
            }
-
 
430
           
-
 
431
            pr->fs_name = fs_name;
-
 
432
            pr->mp = mp;
-
 
433
            pr->opts = opts;
-
 
434
            pr->callid = callid;
-
 
435
            pr->rid = rid;
-
 
436
            pr->dev_handle = dev_handle;
-
 
437
            link_initialize(&pr->link);
-
 
438
            list_append(&pr->link, &pending_req);
-
 
439
            return;
-
 
440
        }
409
        }
441
       
410
       
-
 
411
        fibril_mutex_unlock(&fs_head_lock);
442
        ipc_answer_0(callid, ENOENT);
412
        ipc_answer_0(callid, ENOENT);
443
        ipc_answer_0(rid, ENOENT);
413
        ipc_answer_0(rid, ENOENT);
444
        free(mp);
414
        free(mp);
445
        free(fs_name);
415
        free(fs_name);
446
        free(opts);
416
        free(opts);
447
        return;
417
        return;
448
    }
418
    }
-
 
419
    fibril_mutex_unlock(&fs_head_lock);
449
   
420
   
450
    /* Acknowledge that we know fs_name. */
421
    /* Acknowledge that we know fs_name. */
451
    ipc_answer_0(callid, EOK);
422
    ipc_answer_0(callid, EOK);
452
   
423
   
453
    /* Do the mount */
424
    /* Do the mount */
Line 461... Line 432...
461
{
432
{
462
    if (!vfs_files_init()) {
433
    if (!vfs_files_init()) {
463
        ipc_answer_0(rid, ENOMEM);
434
        ipc_answer_0(rid, ENOMEM);
464
        return;
435
        return;
465
    }
436
    }
466
 
437
   
467
    /*
438
    /*
468
     * The POSIX interface is open(path, oflag, mode).
439
     * The POSIX interface is open(path, oflag, mode).
469
     * We can receive oflags and mode along with the VFS_OPEN call; the path
440
     * We can receive oflags and mode along with the VFS_OPEN call; the path
470
     * will need to arrive in another call.
441
     * will need to arrive in another call.
471
     *
442
     *
Line 474... Line 445...
474
     */
445
     */
475
    int lflag = IPC_GET_ARG1(*request);
446
    int lflag = IPC_GET_ARG1(*request);
476
    int oflag = IPC_GET_ARG2(*request);
447
    int oflag = IPC_GET_ARG2(*request);
477
    int mode = IPC_GET_ARG3(*request);
448
    int mode = IPC_GET_ARG3(*request);
478
    size_t len;
449
    size_t len;
479
 
450
   
480
    /*
451
    /*
481
     * Make sure that we are called with exactly one of L_FILE and
452
     * Make sure that we are called with exactly one of L_FILE and
482
     * L_DIRECTORY.
453
     * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
483
     */
454
     */
484
    if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
455
    if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
485
        (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
456
        ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
-
 
457
        ((lflag & L_OPEN) != 0)) {
486
        ipc_answer_0(rid, EINVAL);
458
        ipc_answer_0(rid, EINVAL);
487
        return;
459
        return;
488
    }
460
    }
489
 
461
   
490
    if (oflag & O_CREAT)
462
    if (oflag & O_CREAT)
491
        lflag |= L_CREATE;
463
        lflag |= L_CREATE;
492
    if (oflag & O_EXCL)
464
    if (oflag & O_EXCL)
493
        lflag |= L_EXCLUSIVE;
465
        lflag |= L_EXCLUSIVE;
494
 
466
   
495
    ipc_callid_t callid;
467
    ipc_callid_t callid;
496
 
-
 
497
    if (!ipc_data_write_receive(&callid, &len)) {
468
    if (!ipc_data_write_receive(&callid, &len)) {
498
        ipc_answer_0(callid, EINVAL);
469
        ipc_answer_0(callid, EINVAL);
499
        ipc_answer_0(rid, EINVAL);
470
        ipc_answer_0(rid, EINVAL);
500
        return;
471
        return;
501
    }
472
    }
-
 
473
   
502
    char *path = malloc(len + 1);
474
    char *path = malloc(len + 1);
503
    if (!path) {
475
    if (!path) {
504
        ipc_answer_0(callid, ENOMEM);
476
        ipc_answer_0(callid, ENOMEM);
505
        ipc_answer_0(rid, ENOMEM);
477
        ipc_answer_0(rid, ENOMEM);
506
        return;
478
        return;
507
    }
479
    }
-
 
480
   
508
    int rc;
481
    int rc;
509
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
482
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
510
        ipc_answer_0(rid, rc);
483
        ipc_answer_0(rid, rc);
511
        free(path);
484
        free(path);
512
        return;
485
        return;
Line 517... Line 490...
517
     * Avoid the race condition in which the file can be deleted before we
490
     * Avoid the race condition in which the file can be deleted before we
518
     * find/create-and-lock the VFS node corresponding to the looked-up
491
     * find/create-and-lock the VFS node corresponding to the looked-up
519
     * triplet.
492
     * triplet.
520
     */
493
     */
521
    if (lflag & L_CREATE)
494
    if (lflag & L_CREATE)
522
        rwlock_write_lock(&namespace_rwlock);
495
        fibril_rwlock_write_lock(&namespace_rwlock);
523
    else
496
    else
524
        rwlock_read_lock(&namespace_rwlock);
497
        fibril_rwlock_read_lock(&namespace_rwlock);
525
 
498
   
526
    /* The path is now populated and we can call vfs_lookup_internal(). */
499
    /* The path is now populated and we can call vfs_lookup_internal(). */
527
    vfs_lookup_res_t lr;
500
    vfs_lookup_res_t lr;
528
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
501
    rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
529
    if (rc) {
502
    if (rc != EOK) {
530
        if (lflag & L_CREATE)
503
        if (lflag & L_CREATE)
531
            rwlock_write_unlock(&namespace_rwlock);
504
            fibril_rwlock_write_unlock(&namespace_rwlock);
532
        else
505
        else
533
            rwlock_read_unlock(&namespace_rwlock);
506
            fibril_rwlock_read_unlock(&namespace_rwlock);
534
        ipc_answer_0(rid, rc);
507
        ipc_answer_0(rid, rc);
535
        free(path);
508
        free(path);
536
        return;
509
        return;
537
    }
510
    }
538
 
511
   
539
    /* Path is no longer needed. */
512
    /* Path is no longer needed. */
540
    free(path);
513
    free(path);
541
 
514
   
542
    vfs_node_t *node = vfs_node_get(&lr);
515
    vfs_node_t *node = vfs_node_get(&lr);
543
    if (lflag & L_CREATE)
516
    if (lflag & L_CREATE)
544
        rwlock_write_unlock(&namespace_rwlock);
517
        fibril_rwlock_write_unlock(&namespace_rwlock);
545
    else
518
    else
546
        rwlock_read_unlock(&namespace_rwlock);
519
        fibril_rwlock_read_unlock(&namespace_rwlock);
547
 
520
   
548
    /* Truncate the file if requested and if necessary. */
521
    /* Truncate the file if requested and if necessary. */
549
    if (oflag & O_TRUNC) {
522
    if (oflag & O_TRUNC) {
550
        rwlock_write_lock(&node->contents_rwlock);
523
        fibril_rwlock_write_lock(&node->contents_rwlock);
551
        if (node->size) {
524
        if (node->size) {
552
            rc = vfs_truncate_internal(node->fs_handle,
525
            rc = vfs_truncate_internal(node->fs_handle,
553
                node->dev_handle, node->index, 0);
526
                node->dev_handle, node->index, 0);
554
            if (rc) {
527
            if (rc) {
555
                rwlock_write_unlock(&node->contents_rwlock);
528
                fibril_rwlock_write_unlock(&node->contents_rwlock);
556
                vfs_node_put(node);
529
                vfs_node_put(node);
557
                ipc_answer_0(rid, rc);
530
                ipc_answer_0(rid, rc);
558
                return;
531
                return;
559
            }
532
            }
560
            node->size = 0;
533
            node->size = 0;
561
        }
534
        }
562
        rwlock_write_unlock(&node->contents_rwlock);
535
        fibril_rwlock_write_unlock(&node->contents_rwlock);
563
    }
536
    }
564
 
537
   
565
    /*
538
    /*
566
     * Get ourselves a file descriptor and the corresponding vfs_file_t
539
     * Get ourselves a file descriptor and the corresponding vfs_file_t
567
     * structure.
540
     * structure.
568
     */
541
     */
569
    int fd = vfs_fd_alloc();
542
    int fd = vfs_fd_alloc();
Line 572... Line 545...
572
        ipc_answer_0(rid, fd);
545
        ipc_answer_0(rid, fd);
573
        return;
546
        return;
574
    }
547
    }
575
    vfs_file_t *file = vfs_file_get(fd);
548
    vfs_file_t *file = vfs_file_get(fd);
576
    file->node = node;
549
    file->node = node;
577
    if (oflag & O_APPEND)
550
    if (oflag & O_APPEND)
578
        file->append = true;
551
        file->append = true;
579
 
552
   
580
    /*
553
    /*
581
     * The following increase in reference count is for the fact that the
554
     * The following increase in reference count is for the fact that the
582
     * file is being opened and that a file structure is pointing to it.
555
     * file is being opened and that a file structure is pointing to it.
583
     * It is necessary so that the file will not disappear when
556
     * It is necessary so that the file will not disappear when
584
     * vfs_node_put() is called. The reference will be dropped by the
557
     * vfs_node_put() is called. The reference will be dropped by the
585
     * respective VFS_CLOSE.
558
     * respective VFS_CLOSE.
586
     */
559
     */
587
    vfs_node_addref(node);
560
    vfs_node_addref(node);
588
    vfs_node_put(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
}
589
 
566
 
-
 
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
   
-
 
576
    /*
-
 
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
   
-
 
586
    fibril_rwlock_read_lock(&namespace_rwlock);
-
 
587
   
-
 
588
    int rc = vfs_open_node_internal(&lr);
-
 
589
    if (rc != EOK) {
-
 
590
        fibril_rwlock_read_unlock(&namespace_rwlock);
-
 
591
        ipc_answer_0(rid, rc);
-
 
592
        return;
-
 
593
    }
-
 
594
   
-
 
595
    vfs_node_t *node = vfs_node_get(&lr);
-
 
596
    fibril_rwlock_read_unlock(&namespace_rwlock);
-
 
597
   
-
 
598
    /* Truncate the file if requested and if necessary. */
-
 
599
    if (oflag & O_TRUNC) {
-
 
600
        fibril_rwlock_write_lock(&node->contents_rwlock);
-
 
601
        if (node->size) {
-
 
602
            rc = vfs_truncate_internal(node->fs_handle,
-
 
603
                node->dev_handle, node->index, 0);
-
 
604
            if (rc) {
-
 
605
                fibril_rwlock_write_unlock(&node->contents_rwlock);
-
 
606
                vfs_node_put(node);
-
 
607
                ipc_answer_0(rid, rc);
-
 
608
                return;
-
 
609
            }
-
 
610
            node->size = 0;
-
 
611
        }
-
 
612
        fibril_rwlock_write_unlock(&node->contents_rwlock);
-
 
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
    /*
-
 
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
-
 
635
     * respective VFS_CLOSE.
-
 
636
     */
-
 
637
    vfs_node_addref(node);
-
 
638
    vfs_node_put(node);
-
 
639
   
590
    /* Success! Return the new file descriptor to the client. */
640
    /* Success! Return the new file descriptor to the client. */
591
    ipc_answer_1(rid, EOK, fd);
641
    ipc_answer_1(rid, EOK, fd);
592
}
642
}
593
 
643
 
594
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
644
void vfs_node(ipc_callid_t rid, ipc_call_t *request)
595
{
645
{
596
    int fd = IPC_GET_ARG1(*request);
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
    ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle,
-
 
656
        file->node->index);
-
 
657
}
-
 
658
 
-
 
659
void vfs_device(ipc_callid_t rid, ipc_call_t *request)
-
 
660
{
-
 
661
    int fd = IPC_GET_ARG1(*request);
-
 
662
   
-
 
663
    /* Lookup the file structure corresponding to the file descriptor. */
-
 
664
    vfs_file_t *file = vfs_file_get(fd);
-
 
665
    if (!file) {
-
 
666
        ipc_answer_0(rid, ENOENT);
-
 
667
        return;
-
 
668
    }
-
 
669
   
-
 
670
    /*
-
 
671
     * Lock the open file structure so that no other thread can manipulate
-
 
672
     * the same open file at a time.
-
 
673
     */
-
 
674
    fibril_mutex_lock(&file->lock);
-
 
675
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
-
 
676
   
-
 
677
    /* Make a VFS_DEVICE request at the destination FS server. */
-
 
678
    aid_t msg;
-
 
679
    ipc_call_t answer;
-
 
680
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
-
 
681
        file->node->dev_handle, file->node->index, &answer);
-
 
682
 
-
 
683
    /* Wait for reply from the FS server. */
-
 
684
    ipcarg_t rc;
-
 
685
    async_wait_for(msg, &rc);
-
 
686
 
-
 
687
    vfs_release_phone(fs_phone);
-
 
688
    fibril_mutex_unlock(&file->lock);
-
 
689
   
-
 
690
    ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer));
-
 
691
}
-
 
692
 
-
 
693
void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
-
 
694
{
-
 
695
    int fd = IPC_GET_ARG1(*request);
-
 
696
   
-
 
697
    /* Lookup the file structure corresponding to the file descriptor. */
597
    int rc = vfs_fd_free(fd);
698
    vfs_file_t *file = vfs_file_get(fd);
-
 
699
    if (!file) {
-
 
700
        ipc_answer_0(rid, ENOENT);
-
 
701
        return;
-
 
702
    }
-
 
703
   
-
 
704
    /*
-
 
705
     * Lock the open file structure so that no other thread can manipulate
-
 
706
     * the same open file at a time.
-
 
707
     */
-
 
708
    fibril_mutex_lock(&file->lock);
-
 
709
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
-
 
710
   
-
 
711
    /* Make a VFS_SYMC request at the destination FS server. */
-
 
712
    aid_t msg;
-
 
713
    ipc_call_t answer;
-
 
714
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
-
 
715
        file->node->dev_handle, file->node->index, &answer);
-
 
716
 
-
 
717
    /* Wait for reply from the FS server. */
-
 
718
    ipcarg_t rc;
-
 
719
    async_wait_for(msg, &rc);
-
 
720
   
-
 
721
    vfs_release_phone(fs_phone);
-
 
722
    fibril_mutex_unlock(&file->lock);
-
 
723
   
598
    ipc_answer_0(rid, rc);
724
    ipc_answer_0(rid, rc);
599
}
725
}
600
 
726
 
-
 
727
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
-
 
728
{
-
 
729
    int fd = IPC_GET_ARG1(*request);
-
 
730
   
-
 
731
    /* Lookup the file structure corresponding to the file descriptor. */
-
 
732
    vfs_file_t *file = vfs_file_get(fd);
-
 
733
    if (!file) {
-
 
734
        ipc_answer_0(rid, ENOENT);
-
 
735
        return;
-
 
736
    }
-
 
737
   
-
 
738
    /*
-
 
739
     * Lock the open file structure so that no other thread can manipulate
-
 
740
     * the same open file at a time.
-
 
741
     */
-
 
742
    fibril_mutex_lock(&file->lock);
-
 
743
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
-
 
744
   
-
 
745
    /* Make a VFS_CLOSE request at the destination FS server. */
-
 
746
    aid_t msg;
-
 
747
    ipc_call_t answer;
-
 
748
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
-
 
749
        file->node->dev_handle, file->node->index, &answer);
-
 
750
 
-
 
751
    /* Wait for reply from the FS server. */
-
 
752
    ipcarg_t rc;
-
 
753
    async_wait_for(msg, &rc);
-
 
754
 
-
 
755
    vfs_release_phone(fs_phone);
-
 
756
    fibril_mutex_unlock(&file->lock);
-
 
757
   
-
 
758
    int retval = IPC_GET_ARG1(answer);
-
 
759
    if (retval != EOK)
-
 
760
        ipc_answer_0(rid, retval);
-
 
761
   
-
 
762
    retval = vfs_fd_free(fd);
-
 
763
    ipc_answer_0(rid, retval);
-
 
764
}
-
 
765
 
601
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
766
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
602
{
767
{
603
 
768
 
604
    /*
769
    /*
605
     * The following code strongly depends on the fact that the files data
770
     * The following code strongly depends on the fact that the files data
Line 638... Line 803...
638
   
803
   
639
    /*
804
    /*
640
     * Lock the open file structure so that no other thread can manipulate
805
     * Lock the open file structure so that no other thread can manipulate
641
     * the same open file at a time.
806
     * the same open file at a time.
642
     */
807
     */
643
    futex_down(&file->lock);
808
    fibril_mutex_lock(&file->lock);
644
 
809
 
645
    /*
810
    /*
646
     * Lock the file's node so that no other client can read/write to it at
811
     * Lock the file's node so that no other client can read/write to it at
647
     * the same time.
812
     * the same time.
648
     */
813
     */
649
    if (read)
814
    if (read)
650
        rwlock_read_lock(&file->node->contents_rwlock);
815
        fibril_rwlock_read_lock(&file->node->contents_rwlock);
651
    else
816
    else
652
        rwlock_write_lock(&file->node->contents_rwlock);
817
        fibril_rwlock_write_lock(&file->node->contents_rwlock);
653
 
818
 
654
    if (file->node->type == VFS_NODE_DIRECTORY) {
819
    if (file->node->type == VFS_NODE_DIRECTORY) {
655
        /*
820
        /*
656
         * Make sure that no one is modifying the namespace
821
         * Make sure that no one is modifying the namespace
657
         * while we are in readdir().
822
         * while we are in readdir().
658
         */
823
         */
659
        assert(read);
824
        assert(read);
660
        rwlock_read_lock(&namespace_rwlock);
825
        fibril_rwlock_read_lock(&namespace_rwlock);
661
    }
826
    }
662
   
827
   
663
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
828
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
664
   
829
   
665
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
830
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
Line 675... Line 840...
675
     * destination FS server. The call will be routed as if sent by
840
     * destination FS server. The call will be routed as if sent by
676
     * ourselves. Note that call arguments are immutable in this case so we
841
     * ourselves. Note that call arguments are immutable in this case so we
677
     * don't have to bother.
842
     * don't have to bother.
678
     */
843
     */
679
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
844
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
680
   
-
 
681
    vfs_release_phone(fs_phone);
-
 
682
   
845
 
683
    /* Wait for reply from the FS server. */
846
    /* Wait for reply from the FS server. */
684
    ipcarg_t rc;
847
    ipcarg_t rc;
685
    async_wait_for(msg, &rc);
848
    async_wait_for(msg, &rc);
-
 
849
   
-
 
850
    vfs_release_phone(fs_phone);
-
 
851
   
686
    size_t bytes = IPC_GET_ARG1(answer);
852
    size_t bytes = IPC_GET_ARG1(answer);
687
 
853
 
688
    if (file->node->type == VFS_NODE_DIRECTORY)
854
    if (file->node->type == VFS_NODE_DIRECTORY)
689
        rwlock_read_unlock(&namespace_rwlock);
855
        fibril_rwlock_read_unlock(&namespace_rwlock);
690
   
856
   
691
    /* Unlock the VFS node. */
857
    /* Unlock the VFS node. */
692
    if (read)
858
    if (read)
693
        rwlock_read_unlock(&file->node->contents_rwlock);
859
        fibril_rwlock_read_unlock(&file->node->contents_rwlock);
694
    else {
860
    else {
695
        /* Update the cached version of node's size. */
861
        /* Update the cached version of node's size. */
696
        if (rc == EOK)
862
        if (rc == EOK)
697
            file->node->size = IPC_GET_ARG2(answer);
863
            file->node->size = IPC_GET_ARG2(answer);
698
        rwlock_write_unlock(&file->node->contents_rwlock);
864
        fibril_rwlock_write_unlock(&file->node->contents_rwlock);
699
    }
865
    }
700
   
866
   
701
    /* Update the position pointer and unlock the open file. */
867
    /* Update the position pointer and unlock the open file. */
702
    if (rc == EOK)
868
    if (rc == EOK)
703
        file->pos += bytes;
869
        file->pos += bytes;
704
    futex_up(&file->lock);
870
    fibril_mutex_unlock(&file->lock);
705
   
871
   
706
    /*
872
    /*
707
     * FS server's reply is the final result of the whole operation we
873
     * FS server's reply is the final result of the whole operation we
708
     * return to the client.
874
     * return to the client.
709
     */
875
     */
Line 733... Line 899...
733
        ipc_answer_0(rid, ENOENT);
899
        ipc_answer_0(rid, ENOENT);
734
        return;
900
        return;
735
    }
901
    }
736
 
902
 
737
    off_t newpos;
903
    off_t newpos;
738
    futex_down(&file->lock);
904
    fibril_mutex_lock(&file->lock);
739
    if (whence == SEEK_SET) {
905
    if (whence == SEEK_SET) {
740
        file->pos = off;
906
        file->pos = off;
741
        futex_up(&file->lock);
907
        fibril_mutex_unlock(&file->lock);
742
        ipc_answer_1(rid, EOK, off);
908
        ipc_answer_1(rid, EOK, off);
743
        return;
909
        return;
744
    }
910
    }
745
    if (whence == SEEK_CUR) {
911
    if (whence == SEEK_CUR) {
746
        if (file->pos + off < file->pos) {
912
        if (file->pos + off < file->pos) {
747
            futex_up(&file->lock);
913
            fibril_mutex_unlock(&file->lock);
748
            ipc_answer_0(rid, EOVERFLOW);
914
            ipc_answer_0(rid, EOVERFLOW);
749
            return;
915
            return;
750
        }
916
        }
751
        file->pos += off;
917
        file->pos += off;
752
        newpos = file->pos;
918
        newpos = file->pos;
753
        futex_up(&file->lock);
919
        fibril_mutex_unlock(&file->lock);
754
        ipc_answer_1(rid, EOK, newpos);
920
        ipc_answer_1(rid, EOK, newpos);
755
        return;
921
        return;
756
    }
922
    }
757
    if (whence == SEEK_END) {
923
    if (whence == SEEK_END) {
758
        rwlock_read_lock(&file->node->contents_rwlock);
924
        fibril_rwlock_read_lock(&file->node->contents_rwlock);
759
        size_t size = file->node->size;
925
        size_t size = file->node->size;
760
        rwlock_read_unlock(&file->node->contents_rwlock);
926
        fibril_rwlock_read_unlock(&file->node->contents_rwlock);
761
        if (size + off < size) {
927
        if (size + off < size) {
762
            futex_up(&file->lock);
928
            fibril_mutex_unlock(&file->lock);
763
            ipc_answer_0(rid, EOVERFLOW);
929
            ipc_answer_0(rid, EOVERFLOW);
764
            return;
930
            return;
765
        }
931
        }
766
        newpos = size + off;
932
        newpos = size + off;
767
        futex_up(&file->lock);
933
        fibril_mutex_unlock(&file->lock);
768
        ipc_answer_1(rid, EOK, newpos);
934
        ipc_answer_1(rid, EOK, newpos);
769
        return;
935
        return;
770
    }
936
    }
771
    futex_up(&file->lock);
937
    fibril_mutex_unlock(&file->lock);
772
    ipc_answer_0(rid, EINVAL);
938
    ipc_answer_0(rid, EINVAL);
773
}
939
}
774
 
940
 
775
int
941
int
776
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
942
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
Line 795... Line 961...
795
    vfs_file_t *file = vfs_file_get(fd);
961
    vfs_file_t *file = vfs_file_get(fd);
796
    if (!file) {
962
    if (!file) {
797
        ipc_answer_0(rid, ENOENT);
963
        ipc_answer_0(rid, ENOENT);
798
        return;
964
        return;
799
    }
965
    }
800
    futex_down(&file->lock);
966
    fibril_mutex_lock(&file->lock);
801
 
967
 
802
    rwlock_write_lock(&file->node->contents_rwlock);
968
    fibril_rwlock_write_lock(&file->node->contents_rwlock);
803
    rc = vfs_truncate_internal(file->node->fs_handle,
969
    rc = vfs_truncate_internal(file->node->fs_handle,
804
        file->node->dev_handle, file->node->index, size);
970
        file->node->dev_handle, file->node->index, size);
805
    if (rc == EOK)
971
    if (rc == EOK)
806
        file->node->size = size;
972
        file->node->size = size;
807
    rwlock_write_unlock(&file->node->contents_rwlock);
973
    fibril_rwlock_write_unlock(&file->node->contents_rwlock);
808
 
974
 
809
    futex_up(&file->lock);
975
    fibril_mutex_unlock(&file->lock);
810
    ipc_answer_0(rid, (ipcarg_t)rc);
976
    ipc_answer_0(rid, (ipcarg_t)rc);
811
}
977
}
812
 
978
 
813
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
979
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
814
{
980
{
Line 834... Line 1000...
834
        free(path);
1000
        free(path);
835
        return;
1001
        return;
836
    }
1002
    }
837
    path[len] = '\0';
1003
    path[len] = '\0';
838
   
1004
   
839
    rwlock_write_lock(&namespace_rwlock);
1005
    fibril_rwlock_write_lock(&namespace_rwlock);
840
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
1006
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
841
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
1007
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
842
    rwlock_write_unlock(&namespace_rwlock);
1008
    fibril_rwlock_write_unlock(&namespace_rwlock);
843
    free(path);
1009
    free(path);
844
    ipc_answer_0(rid, rc);
1010
    ipc_answer_0(rid, rc);
845
}
1011
}
846
 
1012
 
847
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1013
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
Line 868... Line 1034...
868
        free(path);
1034
        free(path);
869
        return;
1035
        return;
870
    }
1036
    }
871
    path[len] = '\0';
1037
    path[len] = '\0';
872
   
1038
   
873
    rwlock_write_lock(&namespace_rwlock);
1039
    fibril_rwlock_write_lock(&namespace_rwlock);
874
    lflag &= L_DIRECTORY;   /* sanitize lflag */
1040
    lflag &= L_DIRECTORY;   /* sanitize lflag */
875
    vfs_lookup_res_t lr;
1041
    vfs_lookup_res_t lr;
876
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1042
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
877
    free(path);
1043
    free(path);
878
    if (rc != EOK) {
1044
    if (rc != EOK) {
879
        rwlock_write_unlock(&namespace_rwlock);
1045
        fibril_rwlock_write_unlock(&namespace_rwlock);
880
        ipc_answer_0(rid, rc);
1046
        ipc_answer_0(rid, rc);
881
        return;
1047
        return;
882
    }
1048
    }
883
 
1049
 
884
    /*
1050
    /*
885
     * The name has already been unlinked by vfs_lookup_internal().
1051
     * The name has already been unlinked by vfs_lookup_internal().
886
     * We have to get and put the VFS node to ensure that it is
1052
     * We have to get and put the VFS node to ensure that it is
887
     * VFS_DESTROY'ed after the last reference to it is dropped.
1053
     * VFS_DESTROY'ed after the last reference to it is dropped.
888
     */
1054
     */
889
    vfs_node_t *node = vfs_node_get(&lr);
1055
    vfs_node_t *node = vfs_node_get(&lr);
890
    futex_down(&nodes_futex);
1056
    fibril_mutex_lock(&nodes_mutex);
891
    node->lnkcnt--;
1057
    node->lnkcnt--;
892
    futex_up(&nodes_futex);
1058
    fibril_mutex_unlock(&nodes_mutex);
893
    rwlock_write_unlock(&namespace_rwlock);
1059
    fibril_rwlock_write_unlock(&namespace_rwlock);
894
    vfs_node_put(node);
1060
    vfs_node_put(node);
895
    ipc_answer_0(rid, EOK);
1061
    ipc_answer_0(rid, EOK);
896
}
1062
}
897
 
1063
 
898
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1064
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
Line 950... Line 1116...
950
        free(new);
1116
        free(new);
951
        return;
1117
        return;
952
    }
1118
    }
953
    oldc[olen] = '\0';
1119
    oldc[olen] = '\0';
954
    newc[nlen] = '\0';
1120
    newc[nlen] = '\0';
955
    if (!str_lcmp(newc, oldc, str_length(oldc))) {
1121
    if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
-
 
1122
        ((newc[str_length(oldc)] == '/') ||
-
 
1123
        (str_length(oldc) == 1) ||
-
 
1124
        (str_length(oldc) == str_length(newc)))) {
-
 
1125
            /*
956
        /* oldc is a prefix of newc */
1126
         * oldc is a prefix of newc and either
-
 
1127
         * - newc continues with a / where oldc ends, or
-
 
1128
         * - oldc was / itself, or
-
 
1129
         * - oldc and newc are equal.
-
 
1130
         */
957
        ipc_answer_0(rid, EINVAL);
1131
        ipc_answer_0(rid, EINVAL);
958
        free(old);
1132
        free(old);
959
        free(new);
1133
        free(new);
960
        return;
1134
        return;
961
    }
1135
    }
962
   
1136
   
963
    vfs_lookup_res_t old_lr;
1137
    vfs_lookup_res_t old_lr;
964
    vfs_lookup_res_t new_lr;
1138
    vfs_lookup_res_t new_lr;
965
    vfs_lookup_res_t new_par_lr;
1139
    vfs_lookup_res_t new_par_lr;
966
    rwlock_write_lock(&namespace_rwlock);
1140
    fibril_rwlock_write_lock(&namespace_rwlock);
967
    /* Lookup the node belonging to the old file name. */
1141
    /* Lookup the node belonging to the old file name. */
968
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1142
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
969
    if (rc != EOK) {
1143
    if (rc != EOK) {
970
        rwlock_write_unlock(&namespace_rwlock);
1144
        fibril_rwlock_write_unlock(&namespace_rwlock);
971
        ipc_answer_0(rid, rc);
1145
        ipc_answer_0(rid, rc);
972
        free(old);
1146
        free(old);
973
        free(new);
1147
        free(new);
974
        return;
1148
        return;
975
    }
1149
    }
976
    vfs_node_t *old_node = vfs_node_get(&old_lr);
1150
    vfs_node_t *old_node = vfs_node_get(&old_lr);
977
    if (!old_node) {
1151
    if (!old_node) {
978
        rwlock_write_unlock(&namespace_rwlock);
1152
        fibril_rwlock_write_unlock(&namespace_rwlock);
979
        ipc_answer_0(rid, ENOMEM);
1153
        ipc_answer_0(rid, ENOMEM);
980
        free(old);
1154
        free(old);
981
        free(new);
1155
        free(new);
982
        return;
1156
        return;
983
    }
1157
    }
-
 
1158
    /* Determine the path to the parent of the node with the new name. */
-
 
1159
    char *parentc = str_dup(newc);
-
 
1160
    if (!parentc) {
-
 
1161
        fibril_rwlock_write_unlock(&namespace_rwlock);
-
 
1162
        ipc_answer_0(rid, rc);
-
 
1163
        free(old);
-
 
1164
        free(new);
-
 
1165
        return;
-
 
1166
    }
-
 
1167
    char *lastsl = str_rchr(parentc + 1, '/');
-
 
1168
    if (lastsl)
-
 
1169
        *lastsl = '\0';
-
 
1170
    else
-
 
1171
        parentc[1] = '\0';
984
    /* Lookup parent of the new file name. */
1172
    /* Lookup parent of the new file name. */
985
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
1173
    rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
-
 
1174
    free(parentc);  /* not needed anymore */
986
    if (rc != EOK) {
1175
    if (rc != EOK) {
987
        rwlock_write_unlock(&namespace_rwlock);
1176
        fibril_rwlock_write_unlock(&namespace_rwlock);
988
        ipc_answer_0(rid, rc);
1177
        ipc_answer_0(rid, rc);
989
        free(old);
1178
        free(old);
990
        free(new);
1179
        free(new);
991
        return;
1180
        return;
992
    }
1181
    }
993
    /* Check whether linking to the same file system instance. */
1182
    /* Check whether linking to the same file system instance. */
994
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1183
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
995
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
1184
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
996
        rwlock_write_unlock(&namespace_rwlock);
1185
        fibril_rwlock_write_unlock(&namespace_rwlock);
997
        ipc_answer_0(rid, EXDEV);   /* different file systems */
1186
        ipc_answer_0(rid, EXDEV);   /* different file systems */
998
        free(old);
1187
        free(old);
999
        free(new);
1188
        free(new);
1000
        return;
1189
        return;
1001
    }
1190
    }
Line 1007... Line 1196...
1007
        /* simply not in our way */
1196
        /* simply not in our way */
1008
        break;
1197
        break;
1009
    case EOK:
1198
    case EOK:
1010
        new_node = vfs_node_get(&new_lr);
1199
        new_node = vfs_node_get(&new_lr);
1011
        if (!new_node) {
1200
        if (!new_node) {
1012
            rwlock_write_unlock(&namespace_rwlock);
1201
            fibril_rwlock_write_unlock(&namespace_rwlock);
1013
            ipc_answer_0(rid, ENOMEM);
1202
            ipc_answer_0(rid, ENOMEM);
1014
            free(old);
1203
            free(old);
1015
            free(new);
1204
            free(new);
1016
            return;
1205
            return;
1017
        }
1206
        }
1018
        futex_down(&nodes_futex);
1207
        fibril_mutex_lock(&nodes_mutex);
1019
        new_node->lnkcnt--;
1208
        new_node->lnkcnt--;
1020
        futex_up(&nodes_futex);
1209
        fibril_mutex_unlock(&nodes_mutex);
1021
        break;
1210
        break;
1022
    default:
1211
    default:
1023
        rwlock_write_unlock(&namespace_rwlock);
1212
        fibril_rwlock_write_unlock(&namespace_rwlock);
1024
        ipc_answer_0(rid, ENOTEMPTY);
1213
        ipc_answer_0(rid, ENOTEMPTY);
1025
        free(old);
1214
        free(old);
1026
        free(new);
1215
        free(new);
1027
        return;
1216
        return;
1028
    }
1217
    }
1029
    /* Create the new link for the new name. */
1218
    /* Create the new link for the new name. */
1030
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1219
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1031
    if (rc != EOK) {
1220
    if (rc != EOK) {
1032
        rwlock_write_unlock(&namespace_rwlock);
1221
        fibril_rwlock_write_unlock(&namespace_rwlock);
1033
        if (new_node)
1222
        if (new_node)
1034
            vfs_node_put(new_node);
1223
            vfs_node_put(new_node);
1035
        ipc_answer_0(rid, rc);
1224
        ipc_answer_0(rid, rc);
1036
        free(old);
1225
        free(old);
1037
        free(new);
1226
        free(new);
1038
        return;
1227
        return;
1039
    }
1228
    }
1040
    futex_down(&nodes_futex);
1229
    fibril_mutex_lock(&nodes_mutex);
1041
    old_node->lnkcnt++;
1230
    old_node->lnkcnt++;
1042
    futex_up(&nodes_futex);
1231
    fibril_mutex_unlock(&nodes_mutex);
1043
    /* Destroy the link for the old name. */
1232
    /* Destroy the link for the old name. */
1044
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1233
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1045
    if (rc != EOK) {
1234
    if (rc != EOK) {
1046
        rwlock_write_unlock(&namespace_rwlock);
1235
        fibril_rwlock_write_unlock(&namespace_rwlock);
1047
        vfs_node_put(old_node);
1236
        vfs_node_put(old_node);
1048
        if (new_node)
1237
        if (new_node)
1049
            vfs_node_put(new_node);
1238
            vfs_node_put(new_node);
1050
        ipc_answer_0(rid, rc);
1239
        ipc_answer_0(rid, rc);
1051
        free(old);
1240
        free(old);
1052
        free(new);
1241
        free(new);
1053
        return;
1242
        return;
1054
    }
1243
    }
1055
    futex_down(&nodes_futex);
1244
    fibril_mutex_lock(&nodes_mutex);
1056
    old_node->lnkcnt--;
1245
    old_node->lnkcnt--;
1057
    futex_up(&nodes_futex);
1246
    fibril_mutex_unlock(&nodes_mutex);
1058
    rwlock_write_unlock(&namespace_rwlock);
1247
    fibril_rwlock_write_unlock(&namespace_rwlock);
1059
    vfs_node_put(old_node);
1248
    vfs_node_put(old_node);
1060
    if (new_node)
1249
    if (new_node)
1061
        vfs_node_put(new_node);
1250
        vfs_node_put(new_node);
1062
    free(old);
1251
    free(old);
1063
    free(new);
1252
    free(new);
1064
    ipc_answer_0(rid, EOK);
1253
    ipc_answer_0(rid, EOK);
1065
}
1254
}
1066
 
1255
 
1067
/**
1256
/**
1068
 * @}
1257
 * @}
1069
 */
1258
 */