Rev 2698 | Rev 2707 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2698 | Rev 2700 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 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_ops.c |
34 | * @file vfs_ops.c |
35 | * @brief Operations that VFS offers to its clients. |
35 | * @brief Operations that VFS offers to its clients. |
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 <stdio.h> |
41 | #include <stdio.h> |
42 | #include <stdlib.h> |
42 | #include <stdlib.h> |
43 | #include <string.h> |
43 | #include <string.h> |
44 | #include <bool.h> |
44 | #include <bool.h> |
45 | #include <futex.h> |
45 | #include <futex.h> |
46 | #include <rwlock.h> |
46 | #include <rwlock.h> |
47 | #include <libadt/list.h> |
47 | #include <libadt/list.h> |
48 | #include <unistd.h> |
48 | #include <unistd.h> |
49 | #include <ctype.h> |
49 | #include <ctype.h> |
50 | #include <assert.h> |
50 | #include <assert.h> |
51 | #include <atomic.h> |
51 | #include <atomic.h> |
52 | #include "vfs.h" |
52 | #include "vfs.h" |
53 | 53 | ||
54 | /** |
54 | /** |
55 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
55 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
56 | * concurrent VFS operation which modifies the file system namespace. |
56 | * concurrent VFS operation which modifies the file system namespace. |
57 | */ |
57 | */ |
58 | RWLOCK_INITIALIZE(namespace_rwlock); |
58 | RWLOCK_INITIALIZE(namespace_rwlock); |
59 | 59 | ||
60 | atomic_t rootfs_futex = FUTEX_INITIALIZER; |
60 | atomic_t rootfs_futex = FUTEX_INITIALIZER; |
61 | vfs_triplet_t rootfs = { |
61 | vfs_triplet_t rootfs = { |
62 | .fs_handle = 0, |
62 | .fs_handle = 0, |
63 | .dev_handle = 0, |
63 | .dev_handle = 0, |
64 | .index = 0, |
64 | .index = 0, |
65 | }; |
65 | }; |
66 | 66 | ||
67 | static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result) |
67 | static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result) |
68 | { |
68 | { |
69 | vfs_pair_t altroot = { |
69 | vfs_pair_t altroot = { |
70 | .fs_handle = fs_handle, |
70 | .fs_handle = fs_handle, |
71 | .dev_handle = dev_handle, |
71 | .dev_handle = dev_handle, |
72 | }; |
72 | }; |
73 | 73 | ||
74 | return vfs_lookup_internal("/", strlen("/"), result, &altroot); |
74 | return vfs_lookup_internal("/", strlen("/"), L_DIRECTORY, result, |
- | 75 | &altroot); |
|
75 | } |
76 | } |
76 | 77 | ||
77 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
78 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
78 | { |
79 | { |
79 | int dev_handle; |
80 | int dev_handle; |
80 | vfs_node_t *mp_node = NULL; |
81 | vfs_node_t *mp_node = NULL; |
81 | 82 | ||
82 | /* |
83 | /* |
83 | * We expect the library to do the device-name to device-handle |
84 | * We expect the library to do the device-name to device-handle |
84 | * translation for us, thus the device handle will arrive as ARG1 |
85 | * translation for us, thus the device handle will arrive as ARG1 |
85 | * in the request. |
86 | * in the request. |
86 | */ |
87 | */ |
87 | dev_handle = IPC_GET_ARG1(*request); |
88 | dev_handle = IPC_GET_ARG1(*request); |
88 | 89 | ||
89 | /* |
90 | /* |
90 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
91 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
91 | * carry mount options in the future. |
92 | * carry mount options in the future. |
92 | */ |
93 | */ |
93 | 94 | ||
94 | ipc_callid_t callid; |
95 | ipc_callid_t callid; |
95 | size_t size; |
96 | size_t size; |
96 | 97 | ||
97 | /* |
98 | /* |
98 | * Now, we expect the client to send us data with the name of the file |
99 | * Now, we expect the client to send us data with the name of the file |
99 | * system. |
100 | * system. |
100 | */ |
101 | */ |
101 | if (!ipc_data_write_receive(&callid, &size)) { |
102 | if (!ipc_data_write_receive(&callid, &size)) { |
102 | ipc_answer_0(callid, EINVAL); |
103 | ipc_answer_0(callid, EINVAL); |
103 | ipc_answer_0(rid, EINVAL); |
104 | ipc_answer_0(rid, EINVAL); |
104 | return; |
105 | return; |
105 | } |
106 | } |
106 | 107 | ||
107 | /* |
108 | /* |
108 | * Don't receive more than is necessary for storing a full file system |
109 | * Don't receive more than is necessary for storing a full file system |
109 | * name. |
110 | * name. |
110 | */ |
111 | */ |
111 | if (size < 1 || size > FS_NAME_MAXLEN) { |
112 | if (size < 1 || size > FS_NAME_MAXLEN) { |
112 | ipc_answer_0(callid, EINVAL); |
113 | ipc_answer_0(callid, EINVAL); |
113 | ipc_answer_0(rid, EINVAL); |
114 | ipc_answer_0(rid, EINVAL); |
114 | return; |
115 | return; |
115 | } |
116 | } |
116 | 117 | ||
117 | /* |
118 | /* |
118 | * Deliver the file system name. |
119 | * Deliver the file system name. |
119 | */ |
120 | */ |
120 | char fs_name[FS_NAME_MAXLEN + 1]; |
121 | char fs_name[FS_NAME_MAXLEN + 1]; |
121 | (void) ipc_data_write_finalize(callid, fs_name, size); |
122 | (void) ipc_data_write_finalize(callid, fs_name, size); |
122 | fs_name[size] = '\0'; |
123 | fs_name[size] = '\0'; |
123 | 124 | ||
124 | /* |
125 | /* |
125 | * Check if we know a file system with the same name as is in fs_name. |
126 | * Check if we know a file system with the same name as is in fs_name. |
126 | * This will also give us its file system handle. |
127 | * This will also give us its file system handle. |
127 | */ |
128 | */ |
128 | int fs_handle = fs_name_to_handle(fs_name, true); |
129 | int fs_handle = fs_name_to_handle(fs_name, true); |
129 | if (!fs_handle) { |
130 | if (!fs_handle) { |
130 | ipc_answer_0(rid, ENOENT); |
131 | ipc_answer_0(rid, ENOENT); |
131 | return; |
132 | return; |
132 | } |
133 | } |
133 | 134 | ||
134 | /* |
135 | /* |
135 | * Now, we want the client to send us the mount point. |
136 | * Now, we want the client to send us the mount point. |
136 | */ |
137 | */ |
137 | if (!ipc_data_write_receive(&callid, &size)) { |
138 | if (!ipc_data_write_receive(&callid, &size)) { |
138 | ipc_answer_0(callid, EINVAL); |
139 | ipc_answer_0(callid, EINVAL); |
139 | ipc_answer_0(rid, EINVAL); |
140 | ipc_answer_0(rid, EINVAL); |
140 | return; |
141 | return; |
141 | } |
142 | } |
142 | 143 | ||
143 | /* |
144 | /* |
144 | * Check whether size is reasonable wrt. the mount point. |
145 | * Check whether size is reasonable wrt. the mount point. |
145 | */ |
146 | */ |
146 | if (size < 1 || size > MAX_PATH_LEN) { |
147 | if (size < 1 || size > MAX_PATH_LEN) { |
147 | ipc_answer_0(callid, EINVAL); |
148 | ipc_answer_0(callid, EINVAL); |
148 | ipc_answer_0(rid, EINVAL); |
149 | ipc_answer_0(rid, EINVAL); |
149 | return; |
150 | return; |
150 | } |
151 | } |
151 | /* |
152 | /* |
152 | * Allocate buffer for the mount point data being received. |
153 | * Allocate buffer for the mount point data being received. |
153 | */ |
154 | */ |
154 | uint8_t *buf; |
155 | uint8_t *buf; |
155 | buf = malloc(size); |
156 | buf = malloc(size); |
156 | if (!buf) { |
157 | if (!buf) { |
157 | ipc_answer_0(callid, ENOMEM); |
158 | ipc_answer_0(callid, ENOMEM); |
158 | ipc_answer_0(rid, ENOMEM); |
159 | ipc_answer_0(rid, ENOMEM); |
159 | return; |
160 | return; |
160 | } |
161 | } |
161 | 162 | ||
162 | /* |
163 | /* |
163 | * Deliver the mount point. |
164 | * Deliver the mount point. |
164 | */ |
165 | */ |
165 | (void) ipc_data_write_finalize(callid, buf, size); |
166 | (void) ipc_data_write_finalize(callid, buf, size); |
166 | 167 | ||
167 | /* |
168 | /* |
168 | * Lookup the root node of the filesystem being mounted. |
169 | * Lookup the root node of the filesystem being mounted. |
169 | * In this case, we don't need to take the namespace_futex as the root |
170 | * In this case, we don't need to take the namespace_futex as the root |
170 | * node cannot be removed. However, we do take a reference to it so |
171 | * node cannot be removed. However, we do take a reference to it so |
171 | * that we can track how many times it has been mounted. |
172 | * that we can track how many times it has been mounted. |
172 | */ |
173 | */ |
173 | int rc; |
174 | int rc; |
174 | vfs_lookup_res_t mr_res; |
175 | vfs_lookup_res_t mr_res; |
175 | rc = lookup_root(fs_handle, dev_handle, &mr_res); |
176 | rc = lookup_root(fs_handle, dev_handle, &mr_res); |
176 | if (rc != EOK) { |
177 | if (rc != EOK) { |
177 | free(buf); |
178 | free(buf); |
178 | ipc_answer_0(rid, rc); |
179 | ipc_answer_0(rid, rc); |
179 | return; |
180 | return; |
180 | } |
181 | } |
181 | vfs_node_t *mr_node = vfs_node_get(&mr_res); |
182 | vfs_node_t *mr_node = vfs_node_get(&mr_res); |
182 | if (!mr_node) { |
183 | if (!mr_node) { |
183 | free(buf); |
184 | free(buf); |
184 | ipc_answer_0(rid, ENOMEM); |
185 | ipc_answer_0(rid, ENOMEM); |
185 | return; |
186 | return; |
186 | } |
187 | } |
187 | 188 | ||
188 | /* |
189 | /* |
189 | * Finally, we need to resolve the path to the mountpoint. |
190 | * Finally, we need to resolve the path to the mountpoint. |
190 | */ |
191 | */ |
191 | vfs_lookup_res_t mp_res; |
192 | vfs_lookup_res_t mp_res; |
192 | futex_down(&rootfs_futex); |
193 | futex_down(&rootfs_futex); |
193 | if (rootfs.fs_handle) { |
194 | if (rootfs.fs_handle) { |
194 | /* |
195 | /* |
195 | * We already have the root FS. |
196 | * We already have the root FS. |
196 | */ |
197 | */ |
197 | rwlock_write_lock(&namespace_rwlock); |
198 | rwlock_write_lock(&namespace_rwlock); |
198 | rc = vfs_lookup_internal(buf, size, &mp_res, NULL); |
199 | rc = vfs_lookup_internal(buf, size, L_DIRECTORY, &mp_res, |
- | 200 | NULL); |
|
199 | if (rc != EOK) { |
201 | if (rc != EOK) { |
200 | /* |
202 | /* |
201 | * The lookup failed for some reason. |
203 | * The lookup failed for some reason. |
202 | */ |
204 | */ |
203 | rwlock_write_unlock(&namespace_rwlock); |
205 | rwlock_write_unlock(&namespace_rwlock); |
204 | futex_up(&rootfs_futex); |
206 | futex_up(&rootfs_futex); |
205 | vfs_node_put(mr_node); /* failed -> drop reference */ |
207 | vfs_node_put(mr_node); /* failed -> drop reference */ |
206 | free(buf); |
208 | free(buf); |
207 | ipc_answer_0(rid, rc); |
209 | ipc_answer_0(rid, rc); |
208 | return; |
210 | return; |
209 | } |
211 | } |
210 | mp_node = vfs_node_get(&mp_res); |
212 | mp_node = vfs_node_get(&mp_res); |
211 | if (!mp_node) { |
213 | if (!mp_node) { |
212 | rwlock_write_unlock(&namespace_rwlock); |
214 | rwlock_write_unlock(&namespace_rwlock); |
213 | futex_up(&rootfs_futex); |
215 | futex_up(&rootfs_futex); |
214 | vfs_node_put(mr_node); /* failed -> drop reference */ |
216 | vfs_node_put(mr_node); /* failed -> drop reference */ |
215 | free(buf); |
217 | free(buf); |
216 | ipc_answer_0(rid, ENOMEM); |
218 | ipc_answer_0(rid, ENOMEM); |
217 | return; |
219 | return; |
218 | } |
220 | } |
219 | /* |
221 | /* |
220 | * Now we hold a reference to mp_node. |
222 | * Now we hold a reference to mp_node. |
221 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
223 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
222 | * This prevents the mount point from being deleted. |
224 | * This prevents the mount point from being deleted. |
223 | */ |
225 | */ |
224 | rwlock_write_unlock(&namespace_rwlock); |
226 | rwlock_write_unlock(&namespace_rwlock); |
225 | } else { |
227 | } else { |
226 | /* |
228 | /* |
227 | * We still don't have the root file system mounted. |
229 | * We still don't have the root file system mounted. |
228 | */ |
230 | */ |
229 | if ((size == 1) && (buf[0] == '/')) { |
231 | if ((size == 1) && (buf[0] == '/')) { |
230 | /* |
232 | /* |
231 | * For this simple, but important case, we are done. |
233 | * For this simple, but important case, we are done. |
232 | */ |
234 | */ |
233 | rootfs = mr_res.triplet; |
235 | rootfs = mr_res.triplet; |
234 | futex_up(&rootfs_futex); |
236 | futex_up(&rootfs_futex); |
235 | free(buf); |
237 | free(buf); |
236 | ipc_answer_0(rid, EOK); |
238 | ipc_answer_0(rid, EOK); |
237 | return; |
239 | return; |
238 | } else { |
240 | } else { |
239 | /* |
241 | /* |
240 | * We can't resolve this without the root filesystem |
242 | * We can't resolve this without the root filesystem |
241 | * being mounted first. |
243 | * being mounted first. |
242 | */ |
244 | */ |
243 | futex_up(&rootfs_futex); |
245 | futex_up(&rootfs_futex); |
244 | free(buf); |
246 | free(buf); |
245 | vfs_node_put(mr_node); /* failed -> drop reference */ |
247 | vfs_node_put(mr_node); /* failed -> drop reference */ |
246 | ipc_answer_0(rid, ENOENT); |
248 | ipc_answer_0(rid, ENOENT); |
247 | return; |
249 | return; |
248 | } |
250 | } |
249 | } |
251 | } |
250 | futex_up(&rootfs_futex); |
252 | futex_up(&rootfs_futex); |
251 | 253 | ||
252 | free(buf); /* The buffer is not needed anymore. */ |
254 | free(buf); /* The buffer is not needed anymore. */ |
253 | 255 | ||
254 | /* |
256 | /* |
255 | * At this point, we have all necessary pieces: file system and device |
257 | * At this point, we have all necessary pieces: file system and device |
256 | * handles, and we know the mount point VFS node and also the root node |
258 | * handles, and we know the mount point VFS node and also the root node |
257 | * of the file system being mounted. |
259 | * of the file system being mounted. |
258 | */ |
260 | */ |
259 | 261 | ||
260 | int phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
262 | int phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
261 | /* Later we can use ARG3 to pass mode/flags. */ |
263 | /* Later we can use ARG3 to pass mode/flags. */ |
262 | aid_t req1 = async_send_3(phone, VFS_MOUNT, |
264 | aid_t req1 = async_send_3(phone, VFS_MOUNT, |
263 | (ipcarg_t) mp_res.triplet.dev_handle, |
265 | (ipcarg_t) mp_res.triplet.dev_handle, |
264 | (ipcarg_t) mp_res.triplet.index, 0, NULL); |
266 | (ipcarg_t) mp_res.triplet.index, 0, NULL); |
265 | /* The second call uses the same method. */ |
267 | /* The second call uses the same method. */ |
266 | aid_t req2 = async_send_3(phone, VFS_MOUNT, |
268 | aid_t req2 = async_send_3(phone, VFS_MOUNT, |
267 | (ipcarg_t) mr_res.triplet.fs_handle, |
269 | (ipcarg_t) mr_res.triplet.fs_handle, |
268 | (ipcarg_t) mr_res.triplet.dev_handle, |
270 | (ipcarg_t) mr_res.triplet.dev_handle, |
269 | (ipcarg_t) mr_res.triplet.index, NULL); |
271 | (ipcarg_t) mr_res.triplet.index, NULL); |
270 | vfs_release_phone(phone); |
272 | vfs_release_phone(phone); |
271 | 273 | ||
272 | ipcarg_t rc1; |
274 | ipcarg_t rc1; |
273 | ipcarg_t rc2; |
275 | ipcarg_t rc2; |
274 | async_wait_for(req1, &rc1); |
276 | async_wait_for(req1, &rc1); |
275 | async_wait_for(req2, &rc2); |
277 | async_wait_for(req2, &rc2); |
276 | 278 | ||
277 | if ((rc1 != EOK) || (rc2 != EOK)) { |
279 | if ((rc1 != EOK) || (rc2 != EOK)) { |
278 | /* Mount failed, drop references to mr_node and mp_node. */ |
280 | /* Mount failed, drop references to mr_node and mp_node. */ |
279 | vfs_node_put(mr_node); |
281 | vfs_node_put(mr_node); |
280 | if (mp_node) |
282 | if (mp_node) |
281 | vfs_node_put(mp_node); |
283 | vfs_node_put(mp_node); |
282 | } |
284 | } |
283 | 285 | ||
284 | if (rc2 == EOK) |
286 | if (rc2 == EOK) |
285 | ipc_answer_0(rid, rc1); |
287 | ipc_answer_0(rid, rc1); |
286 | else if (rc1 == EOK) |
288 | else if (rc1 == EOK) |
287 | ipc_answer_0(rid, rc2); |
289 | ipc_answer_0(rid, rc2); |
288 | else |
290 | else |
289 | ipc_answer_0(rid, rc1); |
291 | ipc_answer_0(rid, rc1); |
290 | } |
292 | } |
291 | 293 | ||
292 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
294 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
293 | { |
295 | { |
294 | if (!vfs_files_init()) { |
296 | if (!vfs_files_init()) { |
295 | ipc_answer_0(rid, ENOMEM); |
297 | ipc_answer_0(rid, ENOMEM); |
296 | return; |
298 | return; |
297 | } |
299 | } |
298 | 300 | ||
299 | /* |
301 | /* |
300 | * The POSIX interface is open(path, flags, mode). |
302 | * The POSIX interface is open(path, oflag, mode). |
301 | * We can receive flags and mode along with the VFS_OPEN call; the path |
303 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
302 | * will need to arrive in another call. |
304 | * will need to arrive in another call. |
- | 305 | * |
|
- | 306 | * We also receive one private, non-POSIX set of flags called lflag |
|
- | 307 | * used to pass information to vfs_lookup_internal(). |
|
303 | */ |
308 | */ |
304 | int flags = IPC_GET_ARG1(*request); |
309 | int lflag = IPC_GET_ARG1(*request); |
- | 310 | int oflag = IPC_GET_ARG2(*request); |
|
305 | int mode = IPC_GET_ARG2(*request); |
311 | int mode = IPC_GET_ARG3(*request); |
306 | size_t len; |
312 | size_t len; |
307 | 313 | ||
308 | ipc_callid_t callid; |
314 | ipc_callid_t callid; |
309 | 315 | ||
310 | if (!ipc_data_write_receive(&callid, &len)) { |
316 | if (!ipc_data_write_receive(&callid, &len)) { |
311 | ipc_answer_0(callid, EINVAL); |
317 | ipc_answer_0(callid, EINVAL); |
312 | ipc_answer_0(rid, EINVAL); |
318 | ipc_answer_0(rid, EINVAL); |
313 | return; |
319 | return; |
314 | } |
320 | } |
315 | 321 | ||
316 | /* |
322 | /* |
317 | * Now we are on the verge of accepting the path. |
323 | * Now we are on the verge of accepting the path. |
318 | * |
324 | * |
319 | * There is one optimization we could do in the future: copy the path |
325 | * There is one optimization we could do in the future: copy the path |
320 | * directly into the PLB using some kind of a callback. |
326 | * directly into the PLB using some kind of a callback. |
321 | */ |
327 | */ |
322 | char *path = malloc(len); |
328 | char *path = malloc(len); |
323 | 329 | ||
324 | if (!path) { |
330 | if (!path) { |
325 | ipc_answer_0(callid, ENOMEM); |
331 | ipc_answer_0(callid, ENOMEM); |
326 | ipc_answer_0(rid, ENOMEM); |
332 | ipc_answer_0(rid, ENOMEM); |
327 | return; |
333 | return; |
328 | } |
334 | } |
329 | 335 | ||
330 | int rc; |
336 | int rc; |
331 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
337 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
332 | ipc_answer_0(rid, rc); |
338 | ipc_answer_0(rid, rc); |
333 | free(path); |
339 | free(path); |
334 | return; |
340 | return; |
335 | } |
341 | } |
336 | 342 | ||
337 | /* |
343 | /* |
338 | * Avoid the race condition in which the file can be deleted before we |
344 | * Avoid the race condition in which the file can be deleted before we |
339 | * find/create-and-lock the VFS node corresponding to the looked-up |
345 | * find/create-and-lock the VFS node corresponding to the looked-up |
340 | * triplet. |
346 | * triplet. |
341 | */ |
347 | */ |
342 | rwlock_read_lock(&namespace_rwlock); |
348 | rwlock_read_lock(&namespace_rwlock); |
343 | 349 | ||
344 | /* |
350 | /* |
345 | * The path is now populated and we can call vfs_lookup_internal(). |
351 | * The path is now populated and we can call vfs_lookup_internal(). |
346 | */ |
352 | */ |
347 | vfs_lookup_res_t lr; |
353 | vfs_lookup_res_t lr; |
348 | rc = vfs_lookup_internal(path, len, &lr, NULL); |
354 | rc = vfs_lookup_internal(path, len, lflag, &lr, NULL); |
349 | if (rc) { |
355 | if (rc) { |
350 | rwlock_read_unlock(&namespace_rwlock); |
356 | rwlock_read_unlock(&namespace_rwlock); |
351 | ipc_answer_0(rid, rc); |
357 | ipc_answer_0(rid, rc); |
352 | free(path); |
358 | free(path); |
353 | return; |
359 | return; |
354 | } |
360 | } |
355 | 361 | ||
356 | /* |
362 | /* |
357 | * Path is no longer needed. |
363 | * Path is no longer needed. |
358 | */ |
364 | */ |
359 | free(path); |
365 | free(path); |
360 | 366 | ||
361 | vfs_node_t *node = vfs_node_get(&lr); |
367 | vfs_node_t *node = vfs_node_get(&lr); |
362 | rwlock_read_unlock(&namespace_rwlock); |
368 | rwlock_read_unlock(&namespace_rwlock); |
363 | 369 | ||
364 | /* |
370 | /* |
365 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
371 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
366 | * structure. |
372 | * structure. |
367 | */ |
373 | */ |
368 | int fd = vfs_fd_alloc(); |
374 | int fd = vfs_fd_alloc(); |
369 | if (fd < 0) { |
375 | if (fd < 0) { |
370 | vfs_node_put(node); |
376 | vfs_node_put(node); |
371 | ipc_answer_0(rid, fd); |
377 | ipc_answer_0(rid, fd); |
372 | return; |
378 | return; |
373 | } |
379 | } |
374 | vfs_file_t *file = vfs_file_get(fd); |
380 | vfs_file_t *file = vfs_file_get(fd); |
375 | file->node = node; |
381 | file->node = node; |
376 | 382 | ||
377 | /* |
383 | /* |
378 | * The following increase in reference count is for the fact that the |
384 | * The following increase in reference count is for the fact that the |
379 | * file is being opened and that a file structure is pointing to it. |
385 | * file is being opened and that a file structure is pointing to it. |
380 | * It is necessary so that the file will not disappear when |
386 | * It is necessary so that the file will not disappear when |
381 | * vfs_node_put() is called. The reference will be dropped by the |
387 | * vfs_node_put() is called. The reference will be dropped by the |
382 | * respective VFS_CLOSE. |
388 | * respective VFS_CLOSE. |
383 | */ |
389 | */ |
384 | vfs_node_addref(node); |
390 | vfs_node_addref(node); |
385 | vfs_node_put(node); |
391 | vfs_node_put(node); |
386 | 392 | ||
387 | /* |
393 | /* |
388 | * Success! Return the new file descriptor to the client. |
394 | * Success! Return the new file descriptor to the client. |
389 | */ |
395 | */ |
390 | ipc_answer_1(rid, EOK, fd); |
396 | ipc_answer_1(rid, EOK, fd); |
391 | } |
397 | } |
392 | 398 | ||
393 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
399 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
394 | { |
400 | { |
395 | 401 | ||
396 | /* |
402 | /* |
397 | * The following code strongly depends on the fact that the files data |
403 | * The following code strongly depends on the fact that the files data |
398 | * structure can be only accessed by a single fibril and all file |
404 | * structure can be only accessed by a single fibril and all file |
399 | * operations are serialized (i.e. the reads and writes cannot |
405 | * operations are serialized (i.e. the reads and writes cannot |
400 | * interleave and a file cannot be closed while it is being read). |
406 | * interleave and a file cannot be closed while it is being read). |
401 | * |
407 | * |
402 | * Additional synchronization needs to be added once the table of |
408 | * Additional synchronization needs to be added once the table of |
403 | * open files supports parallel access! |
409 | * open files supports parallel access! |
404 | */ |
410 | */ |
405 | 411 | ||
406 | int fd = IPC_GET_ARG1(*request); |
412 | int fd = IPC_GET_ARG1(*request); |
407 | 413 | ||
408 | /* |
414 | /* |
409 | * Lookup the file structure corresponding to the file descriptor. |
415 | * Lookup the file structure corresponding to the file descriptor. |
410 | */ |
416 | */ |
411 | vfs_file_t *file = vfs_file_get(fd); |
417 | vfs_file_t *file = vfs_file_get(fd); |
412 | if (!file) { |
418 | if (!file) { |
413 | ipc_answer_0(rid, ENOENT); |
419 | ipc_answer_0(rid, ENOENT); |
414 | return; |
420 | return; |
415 | } |
421 | } |
416 | 422 | ||
417 | /* |
423 | /* |
418 | * Now we need to receive a call with client's |
424 | * Now we need to receive a call with client's |
419 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
425 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
420 | */ |
426 | */ |
421 | ipc_callid_t callid; |
427 | ipc_callid_t callid; |
422 | int res; |
428 | int res; |
423 | if (read) |
429 | if (read) |
424 | res = ipc_data_read_receive(&callid, NULL); |
430 | res = ipc_data_read_receive(&callid, NULL); |
425 | else |
431 | else |
426 | res = ipc_data_write_receive(&callid, NULL); |
432 | res = ipc_data_write_receive(&callid, NULL); |
427 | if (!res) { |
433 | if (!res) { |
428 | ipc_answer_0(callid, EINVAL); |
434 | ipc_answer_0(callid, EINVAL); |
429 | ipc_answer_0(rid, EINVAL); |
435 | ipc_answer_0(rid, EINVAL); |
430 | return; |
436 | return; |
431 | } |
437 | } |
432 | 438 | ||
433 | /* |
439 | /* |
434 | * Lock the open file structure so that no other thread can manipulate |
440 | * Lock the open file structure so that no other thread can manipulate |
435 | * the same open file at a time. |
441 | * the same open file at a time. |
436 | */ |
442 | */ |
437 | futex_down(&file->lock); |
443 | futex_down(&file->lock); |
438 | 444 | ||
439 | /* |
445 | /* |
440 | * Lock the file's node so that no other client can read/write to it at |
446 | * Lock the file's node so that no other client can read/write to it at |
441 | * the same time. |
447 | * the same time. |
442 | */ |
448 | */ |
443 | if (read) |
449 | if (read) |
444 | rwlock_read_lock(&file->node->contents_rwlock); |
450 | rwlock_read_lock(&file->node->contents_rwlock); |
445 | else |
451 | else |
446 | rwlock_write_lock(&file->node->contents_rwlock); |
452 | rwlock_write_lock(&file->node->contents_rwlock); |
447 | 453 | ||
448 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
454 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
449 | 455 | ||
450 | /* |
456 | /* |
451 | * Make a VFS_READ/VFS_WRITE request at the destination FS server. |
457 | * Make a VFS_READ/VFS_WRITE request at the destination FS server. |
452 | */ |
458 | */ |
453 | aid_t msg; |
459 | aid_t msg; |
454 | ipc_call_t answer; |
460 | ipc_call_t answer; |
455 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
461 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
456 | file->node->dev_handle, file->node->index, file->pos, &answer); |
462 | file->node->dev_handle, file->node->index, file->pos, &answer); |
457 | 463 | ||
458 | /* |
464 | /* |
459 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
465 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
460 | * destination FS server. The call will be routed as if sent by |
466 | * destination FS server. The call will be routed as if sent by |
461 | * ourselves. Note that call arguments are immutable in this case so we |
467 | * ourselves. Note that call arguments are immutable in this case so we |
462 | * don't have to bother. |
468 | * don't have to bother. |
463 | */ |
469 | */ |
464 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
470 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
465 | 471 | ||
466 | vfs_release_phone(fs_phone); |
472 | vfs_release_phone(fs_phone); |
467 | 473 | ||
468 | /* |
474 | /* |
469 | * Wait for reply from the FS server. |
475 | * Wait for reply from the FS server. |
470 | */ |
476 | */ |
471 | ipcarg_t rc; |
477 | ipcarg_t rc; |
472 | async_wait_for(msg, &rc); |
478 | async_wait_for(msg, &rc); |
473 | size_t bytes = IPC_GET_ARG1(answer); |
479 | size_t bytes = IPC_GET_ARG1(answer); |
474 | 480 | ||
475 | /* |
481 | /* |
476 | * Unlock the VFS node. |
482 | * Unlock the VFS node. |
477 | */ |
483 | */ |
478 | if (read) |
484 | if (read) |
479 | rwlock_read_unlock(&file->node->contents_rwlock); |
485 | rwlock_read_unlock(&file->node->contents_rwlock); |
480 | else { |
486 | else { |
481 | /* Update the cached version of node's size. */ |
487 | /* Update the cached version of node's size. */ |
482 | file->node->size = IPC_GET_ARG2(answer); |
488 | file->node->size = IPC_GET_ARG2(answer); |
483 | rwlock_write_unlock(&file->node->contents_rwlock); |
489 | rwlock_write_unlock(&file->node->contents_rwlock); |
484 | } |
490 | } |
485 | 491 | ||
486 | /* |
492 | /* |
487 | * Update the position pointer and unlock the open file. |
493 | * Update the position pointer and unlock the open file. |
488 | */ |
494 | */ |
489 | file->pos += bytes; |
495 | file->pos += bytes; |
490 | futex_up(&file->lock); |
496 | futex_up(&file->lock); |
491 | 497 | ||
492 | /* |
498 | /* |
493 | * FS server's reply is the final result of the whole operation we |
499 | * FS server's reply is the final result of the whole operation we |
494 | * return to the client. |
500 | * return to the client. |
495 | */ |
501 | */ |
496 | ipc_answer_1(rid, rc, bytes); |
502 | ipc_answer_1(rid, rc, bytes); |
497 | } |
503 | } |
498 | 504 | ||
499 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
505 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
500 | { |
506 | { |
501 | vfs_rdwr(rid, request, true); |
507 | vfs_rdwr(rid, request, true); |
502 | } |
508 | } |
503 | 509 | ||
504 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
510 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
505 | { |
511 | { |
506 | vfs_rdwr(rid, request, false); |
512 | vfs_rdwr(rid, request, false); |
507 | } |
513 | } |
508 | 514 | ||
509 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
515 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
510 | { |
516 | { |
511 | int fd = (int) IPC_GET_ARG1(*request); |
517 | int fd = (int) IPC_GET_ARG1(*request); |
512 | off_t off = (off_t) IPC_GET_ARG2(*request); |
518 | off_t off = (off_t) IPC_GET_ARG2(*request); |
513 | int whence = (int) IPC_GET_ARG3(*request); |
519 | int whence = (int) IPC_GET_ARG3(*request); |
514 | 520 | ||
515 | 521 | ||
516 | /* |
522 | /* |
517 | * Lookup the file structure corresponding to the file descriptor. |
523 | * Lookup the file structure corresponding to the file descriptor. |
518 | */ |
524 | */ |
519 | vfs_file_t *file = vfs_file_get(fd); |
525 | vfs_file_t *file = vfs_file_get(fd); |
520 | if (!file) { |
526 | if (!file) { |
521 | ipc_answer_0(rid, ENOENT); |
527 | ipc_answer_0(rid, ENOENT); |
522 | return; |
528 | return; |
523 | } |
529 | } |
524 | 530 | ||
525 | off_t newpos; |
531 | off_t newpos; |
526 | futex_down(&file->lock); |
532 | futex_down(&file->lock); |
527 | if (whence == SEEK_SET) { |
533 | if (whence == SEEK_SET) { |
528 | file->pos = off; |
534 | file->pos = off; |
529 | futex_up(&file->lock); |
535 | futex_up(&file->lock); |
530 | ipc_answer_1(rid, EOK, off); |
536 | ipc_answer_1(rid, EOK, off); |
531 | return; |
537 | return; |
532 | } |
538 | } |
533 | if (whence == SEEK_CUR) { |
539 | if (whence == SEEK_CUR) { |
534 | if (file->pos + off < file->pos) { |
540 | if (file->pos + off < file->pos) { |
535 | futex_up(&file->lock); |
541 | futex_up(&file->lock); |
536 | ipc_answer_0(rid, EOVERFLOW); |
542 | ipc_answer_0(rid, EOVERFLOW); |
537 | return; |
543 | return; |
538 | } |
544 | } |
539 | file->pos += off; |
545 | file->pos += off; |
540 | newpos = file->pos; |
546 | newpos = file->pos; |
541 | futex_up(&file->lock); |
547 | futex_up(&file->lock); |
542 | ipc_answer_1(rid, EOK, newpos); |
548 | ipc_answer_1(rid, EOK, newpos); |
543 | return; |
549 | return; |
544 | } |
550 | } |
545 | if (whence == SEEK_END) { |
551 | if (whence == SEEK_END) { |
546 | rwlock_read_lock(&file->node->contents_rwlock); |
552 | rwlock_read_lock(&file->node->contents_rwlock); |
547 | size_t size = file->node->size; |
553 | size_t size = file->node->size; |
548 | rwlock_read_unlock(&file->node->contents_rwlock); |
554 | rwlock_read_unlock(&file->node->contents_rwlock); |
549 | if (size + off < size) { |
555 | if (size + off < size) { |
550 | futex_up(&file->lock); |
556 | futex_up(&file->lock); |
551 | ipc_answer_0(rid, EOVERFLOW); |
557 | ipc_answer_0(rid, EOVERFLOW); |
552 | return; |
558 | return; |
553 | } |
559 | } |
554 | newpos = size + off; |
560 | newpos = size + off; |
555 | futex_up(&file->lock); |
561 | futex_up(&file->lock); |
556 | ipc_answer_1(rid, EOK, newpos); |
562 | ipc_answer_1(rid, EOK, newpos); |
557 | return; |
563 | return; |
558 | } |
564 | } |
559 | futex_up(&file->lock); |
565 | futex_up(&file->lock); |
560 | ipc_answer_0(rid, EINVAL); |
566 | ipc_answer_0(rid, EINVAL); |
561 | } |
567 | } |
562 | 568 | ||
563 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
569 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
564 | { |
570 | { |
565 | int fd = IPC_GET_ARG1(*request); |
571 | int fd = IPC_GET_ARG1(*request); |
566 | size_t size = IPC_GET_ARG2(*request); |
572 | size_t size = IPC_GET_ARG2(*request); |
567 | ipcarg_t rc; |
573 | ipcarg_t rc; |
568 | 574 | ||
569 | vfs_file_t *file = vfs_file_get(fd); |
575 | vfs_file_t *file = vfs_file_get(fd); |
570 | if (!file) { |
576 | if (!file) { |
571 | ipc_answer_0(rid, ENOENT); |
577 | ipc_answer_0(rid, ENOENT); |
572 | return; |
578 | return; |
573 | } |
579 | } |
574 | futex_down(&file->lock); |
580 | futex_down(&file->lock); |
575 | 581 | ||
576 | rwlock_write_lock(&file->node->contents_rwlock); |
582 | rwlock_write_lock(&file->node->contents_rwlock); |
577 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
583 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
578 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)file->node->dev_handle, |
584 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)file->node->dev_handle, |
579 | (ipcarg_t)file->node->index, (ipcarg_t)size); |
585 | (ipcarg_t)file->node->index, (ipcarg_t)size); |
580 | vfs_release_phone(fs_phone); |
586 | vfs_release_phone(fs_phone); |
581 | if (rc == EOK) |
587 | if (rc == EOK) |
582 | file->node->size = size; |
588 | file->node->size = size; |
583 | rwlock_write_unlock(&file->node->contents_rwlock); |
589 | rwlock_write_unlock(&file->node->contents_rwlock); |
584 | 590 | ||
585 | futex_up(&file->lock); |
591 | futex_up(&file->lock); |
586 | ipc_answer_0(rid, rc); |
592 | ipc_answer_0(rid, rc); |
587 | } |
593 | } |
588 | 594 | ||
589 | /** |
595 | /** |
590 | * @} |
596 | * @} |
591 | */ |
597 | */ |
592 | 598 |