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