source: mainline/uspace/srv/vfs/vfs_open.c@ 7f3e3e7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f3e3e7 was 320c884, checked in by Jakub Jermar <jakub@…>, 18 years ago

A lot of more VFS prototyping.
VFS_OPEN gets reasonably complete, fix a limitation that prevented file
structures from being shared by multiple file descriptors, add functions for
file descriptor management, introduce unlink_futex and two new VFS operations
VFS_UNLINK and VFS_RENAME.

  • Property mode set to 100644
File size: 3.9 KB
Line 
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/**
34 * @file vfs_open.c
35 * @brief VFS_OPEN method.
36 */
37
38#include <ipc/ipc.h>
39#include <async.h>
40#include <errno.h>
41#include <futex.h>
42#include <sys/types.h>
43#include <stdlib.h>
44#include "vfs.h"
45
46void vfs_open(ipc_callid_t rid, ipc_call_t *request)
47{
48 if (!vfs_files_init()) {
49 ipc_answer_fast_0(rid, ENOMEM);
50 return;
51 }
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);
60 size_t size;
61
62 ipc_callid_t callid;
63 ipc_call_t call;
64
65 if (!ipc_data_receive(&callid, &call, NULL, &size)) {
66 ipc_answer_fast_0(callid, EINVAL);
67 ipc_answer_fast_0(rid, EINVAL);
68 return;
69 }
70
71 /*
72 * Now we are on the verge of accepting the path.
73 *
74 * There is one optimization we could do in the future: copy the path
75 * directly into the PLB using some kind of a callback.
76 */
77 char *path = malloc(size);
78
79 if (!path) {
80 ipc_answer_fast_0(callid, ENOMEM);
81 ipc_answer_fast_0(rid, ENOMEM);
82 return;
83 }
84
85 int rc;
86 if ((rc = ipc_data_deliver(callid, &call, path, size))) {
87 ipc_answer_fast_0(rid, rc);
88 free(path);
89 return;
90 }
91
92 /*
93 * 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
95 * triplet.
96 */
97 futex_down(&unlink_futex);
98
99 /*
100 * The path is now populated and we can call vfs_lookup_internal().
101 */
102 vfs_triplet_t triplet;
103 rc = vfs_lookup_internal(path, size, &triplet, NULL);
104 if (rc) {
105 futex_up(&unlink_futex);
106 ipc_answer_fast_0(rid, rc);
107 free(path);
108 return;
109 }
110
111 /*
112 * Path is no longer needed.
113 */
114 free(path);
115
116 vfs_node_t *node = vfs_node_get(&triplet);
117 futex_up(&unlink_futex);
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);
126 ipc_answer_fast_0(rid, fd);
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 */
145 ipc_answer_fast_1(rid, EOK, fd);
146}
147
148/**
149 * @}
150 */
Note: See TracBrowser for help on using the repository browser.