Subversion Repositories HelenOS

Rev

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

Rev 2593 Rev 2619
1
/*
1
/*
2
 * Copyright (c) 2007 Jakub Jermar
2
 * Copyright (c) 2007 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    vfs_open.c
34
 * @file    vfs_open.c
35
 * @brief   VFS_OPEN method.
35
 * @brief   VFS_OPEN method.
36
 */
36
 */
37
 
37
 
38
#include <ipc/ipc.h>
38
#include <ipc/ipc.h>
39
#include <async.h>
39
#include <async.h>
40
#include <errno.h>
40
#include <errno.h>
41
#include <futex.h>
41
#include <futex.h>
42
#include <sys/types.h>
42
#include <sys/types.h>
43
#include <stdlib.h>
43
#include <stdlib.h>
44
#include "vfs.h"
44
#include "vfs.h"
45
 
45
 
46
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
46
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
47
{
47
{
48
    if (!vfs_files_init()) {
48
    if (!vfs_files_init()) {
49
        ipc_answer_fast_0(rid, ENOMEM);
49
        ipc_answer_0(rid, ENOMEM);
50
        return;
50
        return;
51
    }
51
    }
52
 
52
 
53
    /*
53
    /*
54
     * The POSIX interface is open(path, flags, mode).
54
     * The POSIX interface is open(path, flags, mode).
55
     * We can receive flags and mode along with the VFS_OPEN call; the path
55
     * We can receive flags and mode along with the VFS_OPEN call; the path
56
     * will need to arrive in another call.
56
     * will need to arrive in another call.
57
     */
57
     */
58
    int flags = IPC_GET_ARG1(*request);
58
    int flags = IPC_GET_ARG1(*request);
59
    int mode = IPC_GET_ARG2(*request);
59
    int mode = IPC_GET_ARG2(*request);
60
    size_t size;
60
    size_t size;
61
 
61
 
62
    ipc_callid_t callid;
62
    ipc_callid_t callid;
63
    ipc_call_t call;
-
 
64
 
63
 
65
    if (!ipc_data_receive(&callid, &call, NULL, &size)) {
64
    if (!ipc_data_receive(&callid, NULL, &size)) {
66
        ipc_answer_fast_0(callid, EINVAL);
65
        ipc_answer_0(callid, EINVAL);
67
        ipc_answer_fast_0(rid, EINVAL);
66
        ipc_answer_0(rid, EINVAL);
68
        return;
67
        return;
69
    }
68
    }
70
 
69
 
71
    /*
70
    /*
72
     * Now we are on the verge of accepting the path.
71
     * Now we are on the verge of accepting the path.
73
     *
72
     *
74
     * There is one optimization we could do in the future: copy the path
73
     * There is one optimization we could do in the future: copy the path
75
     * directly into the PLB using some kind of a callback.
74
     * directly into the PLB using some kind of a callback.
76
     */
75
     */
77
    char *path = malloc(size);
76
    char *path = malloc(size);
78
   
77
   
79
    if (!path) {
78
    if (!path) {
80
        ipc_answer_fast_0(callid, ENOMEM);
79
        ipc_answer_0(callid, ENOMEM);
81
        ipc_answer_fast_0(rid, ENOMEM);
80
        ipc_answer_0(rid, ENOMEM);
82
        return;
81
        return;
83
    }
82
    }
84
 
83
 
85
    int rc;
84
    int rc;
86
    if ((rc = ipc_data_deliver(callid, &call, path, size))) {
85
    if ((rc = ipc_data_deliver(callid, path, size))) {
87
        ipc_answer_fast_0(rid, rc);
86
        ipc_answer_0(rid, rc);
88
        free(path);
87
        free(path);
89
        return;
88
        return;
90
    }
89
    }
91
   
90
   
92
    /*
91
    /*
93
     * Avoid the race condition in which the file can be deleted before we
92
     * Avoid the race condition in which the file can be deleted before we
94
     * find/create-and-lock the VFS node corresponding to the looked-up
93
     * find/create-and-lock the VFS node corresponding to the looked-up
95
     * triplet.
94
     * triplet.
96
     */
95
     */
97
    futex_down(&unlink_futex);
96
    futex_down(&unlink_futex);
98
 
97
 
99
    /*
98
    /*
100
     * The path is now populated and we can call vfs_lookup_internal().
99
     * The path is now populated and we can call vfs_lookup_internal().
101
     */
100
     */
102
    vfs_triplet_t triplet;
101
    vfs_triplet_t triplet;
103
    rc = vfs_lookup_internal(path, size, &triplet, NULL);
102
    rc = vfs_lookup_internal(path, size, &triplet, NULL);
104
    if (rc) {
103
    if (rc) {
105
        futex_up(&unlink_futex);
104
        futex_up(&unlink_futex);
106
        ipc_answer_fast_0(rid, rc);
105
        ipc_answer_0(rid, rc);
107
        free(path);
106
        free(path);
108
        return;
107
        return;
109
    }
108
    }
110
 
109
 
111
    /*
110
    /*
112
     * Path is no longer needed.
111
     * Path is no longer needed.
113
     */
112
     */
114
    free(path);
113
    free(path);
115
 
114
 
116
    vfs_node_t *node = vfs_node_get(&triplet);
115
    vfs_node_t *node = vfs_node_get(&triplet);
117
    futex_up(&unlink_futex);
116
    futex_up(&unlink_futex);
118
 
117
 
119
    /*
118
    /*
120
     * Get ourselves a file descriptor and the corresponding vfs_file_t
119
     * Get ourselves a file descriptor and the corresponding vfs_file_t
121
     * structure.
120
     * structure.
122
     */
121
     */
123
    int fd = vfs_fd_alloc();
122
    int fd = vfs_fd_alloc();
124
    if (fd < 0) {
123
    if (fd < 0) {
125
        vfs_node_put(node);
124
        vfs_node_put(node);
126
        ipc_answer_fast_0(rid, fd);
125
        ipc_answer_0(rid, fd);
127
        return;
126
        return;
128
    }
127
    }
129
    vfs_file_t *file = vfs_file_get(fd);
128
    vfs_file_t *file = vfs_file_get(fd);
130
    file->node = node;
129
    file->node = node;
131
 
130
 
132
    /*
131
    /*
133
     * The following increase in reference count is for the fact that the
132
     * 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.
133
     * 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
134
     * It is necessary so that the file will not disappear when
136
     * vfs_node_put() is called. The reference will be dropped by the
135
     * vfs_node_put() is called. The reference will be dropped by the
137
     * respective VFS_CLOSE.
136
     * respective VFS_CLOSE.
138
     */
137
     */
139
    vfs_node_addref(node);
138
    vfs_node_addref(node);
140
    vfs_node_put(node);
139
    vfs_node_put(node);
141
 
140
 
142
    /*
141
    /*
143
     * Success! Return the new file descriptor to the client.
142
     * Success! Return the new file descriptor to the client.
144
     */
143
     */
145
    ipc_answer_fast_1(rid, EOK, fd);
144
    ipc_answer_1(rid, EOK, fd);
146
}
145
}
147
 
146
 
148
/**
147
/**
149
 * @}
148
 * @}
150
 */
149
 */
151
 
150