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