Subversion Repositories HelenOS

Rev

Rev 2687 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2560 jermar 1
/*
2
 * Copyright (c) 2007 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
 * @{
31
 */
32
 
33
/**
2590 jermar 34
 * @file    vfs_open.c
2560 jermar 35
 * @brief   VFS_OPEN method.
36
 */
37
 
38
#include <ipc/ipc.h>
39
#include <async.h>
40
#include <errno.h>
2681 jermar 41
#include <rwlock.h>
2589 jermar 42
#include <sys/types.h>
2593 jermar 43
#include <stdlib.h>
2560 jermar 44
#include "vfs.h"
45
 
46
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
47
{
2593 jermar 48
    if (!vfs_files_init()) {
2619 jermar 49
        ipc_answer_0(rid, ENOMEM);
2589 jermar 50
        return;
51
    }
2590 jermar 52
 
53
    /*
54
     * The POSIX interface is open(path, flags, mode).
55
     * We can receive flags and mode along with the VFS_OPEN call; the path
56
     * will need to arrive in another call.
57
     */
58
    int flags = IPC_GET_ARG1(*request);
59
    int mode = IPC_GET_ARG2(*request);
2687 jermar 60
    size_t len;
2590 jermar 61
 
62
    ipc_callid_t callid;
63
 
2687 jermar 64
    if (!ipc_data_write_receive(&callid, &len)) {
2619 jermar 65
        ipc_answer_0(callid, EINVAL);
66
        ipc_answer_0(rid, EINVAL);
2590 jermar 67
        return;
68
    }
69
 
70
    /*
71
     * Now we are on the verge of accepting the path.
72
     *
73
     * There is one optimization we could do in the future: copy the path
74
     * directly into the PLB using some kind of a callback.
75
     */
2687 jermar 76
    char *path = malloc(len);
2590 jermar 77
 
78
    if (!path) {
2619 jermar 79
        ipc_answer_0(callid, ENOMEM);
80
        ipc_answer_0(rid, ENOMEM);
2590 jermar 81
        return;
82
    }
83
 
84
    int rc;
2687 jermar 85
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
2619 jermar 86
        ipc_answer_0(rid, rc);
2590 jermar 87
        free(path);
88
        return;
89
    }
90
 
91
    /*
2593 jermar 92
     * Avoid the race condition in which the file can be deleted before we
93
     * find/create-and-lock the VFS node corresponding to the looked-up
94
     * triplet.
95
     */
2688 jermar 96
    rwlock_read_lock(&namespace_rwlock);
2593 jermar 97
 
98
    /*
2590 jermar 99
     * The path is now populated and we can call vfs_lookup_internal().
100
     */
101
    vfs_triplet_t triplet;
2687 jermar 102
    size_t size;
103
    rc = vfs_lookup_internal(path, len, &triplet, &size, NULL);
2590 jermar 104
    if (rc) {
2688 jermar 105
        rwlock_read_unlock(&namespace_rwlock);
2619 jermar 106
        ipc_answer_0(rid, rc);
2590 jermar 107
        free(path);
108
        return;
109
    }
110
 
111
    /*
112
     * Path is no longer needed.
113
     */
114
    free(path);
115
 
2687 jermar 116
    vfs_node_t *node = vfs_node_get(&triplet, size);
2688 jermar 117
    rwlock_read_unlock(&namespace_rwlock);
2593 jermar 118
 
119
    /*
120
     * Get ourselves a file descriptor and the corresponding vfs_file_t
121
     * structure.
122
     */
123
    int fd = vfs_fd_alloc();
124
    if (fd < 0) {
125
        vfs_node_put(node);
2619 jermar 126
        ipc_answer_0(rid, fd);
2593 jermar 127
        return;
128
    }
129
    vfs_file_t *file = vfs_file_get(fd);
130
    file->node = node;
131
 
132
    /*
133
     * The following increase in reference count is for the fact that the
134
     * file is being opened and that a file structure is pointing to it.
135
     * It is necessary so that the file will not disappear when
136
     * vfs_node_put() is called. The reference will be dropped by the
137
     * respective VFS_CLOSE.
138
     */
139
    vfs_node_addref(node);
140
    vfs_node_put(node);
141
 
142
    /*
143
     * Success! Return the new file descriptor to the client.
144
     */
2619 jermar 145
    ipc_answer_1(rid, EOK, fd);
2560 jermar 146
}
147
 
148
/**
149
 * @}
150
 */