Rev 2690 | Rev 2693 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2690 | Rev 2691 | ||
---|---|---|---|
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 <ipc/services.h> |
39 | #include <ipc/services.h> |
40 | #include <async.h> |
40 | #include <async.h> |
41 | #include <fibril.h> |
41 | #include <fibril.h> |
42 | #include <errno.h> |
42 | #include <errno.h> |
43 | #include <stdio.h> |
43 | #include <stdio.h> |
44 | #include <stdlib.h> |
44 | #include <stdlib.h> |
45 | #include <string.h> |
45 | #include <string.h> |
46 | #include <bool.h> |
46 | #include <bool.h> |
47 | #include <futex.h> |
47 | #include <futex.h> |
48 | #include <rwlock.h> |
48 | #include <rwlock.h> |
49 | #include <libadt/list.h> |
49 | #include <libadt/list.h> |
50 | #include <unistd.h> |
50 | #include <unistd.h> |
51 | #include <ctype.h> |
51 | #include <ctype.h> |
52 | #include <as.h> |
52 | #include <as.h> |
53 | #include <assert.h> |
53 | #include <assert.h> |
54 | #include <atomic.h> |
54 | #include <atomic.h> |
55 | #include "vfs.h" |
55 | #include "vfs.h" |
56 | 56 | ||
57 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
57 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
58 | 58 | ||
59 | /** |
59 | /** |
60 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
60 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
61 | * concurrent VFS operation which modifies the file system namespace. |
61 | * concurrent VFS operation which modifies the file system namespace. |
62 | */ |
62 | */ |
63 | RWLOCK_INITIALIZE(namespace_rwlock); |
63 | RWLOCK_INITIALIZE(namespace_rwlock); |
64 | 64 | ||
65 | atomic_t plb_futex = FUTEX_INITIALIZER; |
65 | atomic_t plb_futex = FUTEX_INITIALIZER; |
66 | link_t plb_head; /**< PLB entry ring buffer. */ |
66 | link_t plb_head; /**< PLB entry ring buffer. */ |
67 | uint8_t *plb = NULL; |
67 | uint8_t *plb = NULL; |
68 | 68 | ||
69 | /** Perform a path lookup. |
69 | /** Perform a path lookup. |
70 | * |
70 | * |
71 | * @param path Path to be resolved; it needn't be an ASCIIZ string. |
71 | * @param path Path to be resolved; it needn't be an ASCIIZ string. |
72 | * @param len Number of path characters pointed by path. |
72 | * @param len Number of path characters pointed by path. |
73 | * @param result Empty node structure where the result will be stored. |
73 | * @param result Empty structure where the lookup result will be stored. |
74 | * @param size Storage where the size of the node will be stored. Can |
- | |
75 | * be NULL. |
- | |
76 | * @param altroot If non-empty, will be used instead of rootfs as the root |
74 | * @param altroot If non-empty, will be used instead of rootfs as the root |
77 | * of the whole VFS tree. |
75 | * of the whole VFS tree. |
78 | * |
76 | * |
79 | * @return EOK on success or an error code from errno.h. |
77 | * @return EOK on success or an error code from errno.h. |
80 | */ |
78 | */ |
81 | int vfs_lookup_internal(char *path, size_t len, vfs_triplet_t *result, |
79 | int vfs_lookup_internal(char *path, size_t len, vfs_lookup_res_t *result, |
82 | size_t *size, vfs_pair_t *altroot) |
80 | vfs_pair_t *altroot) |
83 | { |
81 | { |
84 | vfs_pair_t *root; |
82 | vfs_pair_t *root; |
85 | 83 | ||
86 | if (!len) |
84 | if (!len) |
87 | return EINVAL; |
85 | return EINVAL; |
88 | 86 | ||
89 | if (altroot) |
87 | if (altroot) |
90 | root = altroot; |
88 | root = altroot; |
91 | else |
89 | else |
92 | root = (vfs_pair_t *) &rootfs; |
90 | root = (vfs_pair_t *) &rootfs; |
93 | 91 | ||
94 | if (!root->fs_handle) |
92 | if (!root->fs_handle) |
95 | return ENOENT; |
93 | return ENOENT; |
96 | 94 | ||
97 | futex_down(&plb_futex); |
95 | futex_down(&plb_futex); |
98 | 96 | ||
99 | plb_entry_t entry; |
97 | plb_entry_t entry; |
100 | link_initialize(&entry.plb_link); |
98 | link_initialize(&entry.plb_link); |
101 | entry.len = len; |
99 | entry.len = len; |
102 | 100 | ||
103 | off_t first; /* the first free index */ |
101 | off_t first; /* the first free index */ |
104 | off_t last; /* the last free index */ |
102 | off_t last; /* the last free index */ |
105 | 103 | ||
106 | if (list_empty(&plb_head)) { |
104 | if (list_empty(&plb_head)) { |
107 | first = 0; |
105 | first = 0; |
108 | last = PLB_SIZE - 1; |
106 | last = PLB_SIZE - 1; |
109 | } else { |
107 | } else { |
110 | plb_entry_t *oldest = list_get_instance(plb_head.next, |
108 | plb_entry_t *oldest = list_get_instance(plb_head.next, |
111 | plb_entry_t, plb_link); |
109 | plb_entry_t, plb_link); |
112 | plb_entry_t *newest = list_get_instance(plb_head.prev, |
110 | plb_entry_t *newest = list_get_instance(plb_head.prev, |
113 | plb_entry_t, plb_link); |
111 | plb_entry_t, plb_link); |
114 | 112 | ||
115 | first = (newest->index + newest->len) % PLB_SIZE; |
113 | first = (newest->index + newest->len) % PLB_SIZE; |
116 | last = (oldest->index - 1) % PLB_SIZE; |
114 | last = (oldest->index - 1) % PLB_SIZE; |
117 | } |
115 | } |
118 | 116 | ||
119 | if (first <= last) { |
117 | if (first <= last) { |
120 | if ((last - first) + 1 < len) { |
118 | if ((last - first) + 1 < len) { |
121 | /* |
119 | /* |
122 | * The buffer cannot absorb the path. |
120 | * The buffer cannot absorb the path. |
123 | */ |
121 | */ |
124 | futex_up(&plb_futex); |
122 | futex_up(&plb_futex); |
125 | return ELIMIT; |
123 | return ELIMIT; |
126 | } |
124 | } |
127 | } else { |
125 | } else { |
128 | if (PLB_SIZE - ((first - last) + 1) < len) { |
126 | if (PLB_SIZE - ((first - last) + 1) < len) { |
129 | /* |
127 | /* |
130 | * The buffer cannot absorb the path. |
128 | * The buffer cannot absorb the path. |
131 | */ |
129 | */ |
132 | futex_up(&plb_futex); |
130 | futex_up(&plb_futex); |
133 | return ELIMIT; |
131 | return ELIMIT; |
134 | } |
132 | } |
135 | } |
133 | } |
136 | 134 | ||
137 | /* |
135 | /* |
138 | * We know the first free index in PLB and we also know that there is |
136 | * We know the first free index in PLB and we also know that there is |
139 | * enough space in the buffer to hold our path. |
137 | * enough space in the buffer to hold our path. |
140 | */ |
138 | */ |
141 | 139 | ||
142 | entry.index = first; |
140 | entry.index = first; |
143 | entry.len = len; |
141 | entry.len = len; |
144 | 142 | ||
145 | /* |
143 | /* |
146 | * Claim PLB space by inserting the entry into the PLB entry ring |
144 | * Claim PLB space by inserting the entry into the PLB entry ring |
147 | * buffer. |
145 | * buffer. |
148 | */ |
146 | */ |
149 | list_append(&entry.plb_link, &plb_head); |
147 | list_append(&entry.plb_link, &plb_head); |
150 | 148 | ||
151 | futex_up(&plb_futex); |
149 | futex_up(&plb_futex); |
152 | 150 | ||
153 | /* |
151 | /* |
154 | * Copy the path into PLB. |
152 | * Copy the path into PLB. |
155 | */ |
153 | */ |
156 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1); |
154 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1); |
157 | size_t cnt2 = len - cnt1; |
155 | size_t cnt2 = len - cnt1; |
158 | 156 | ||
159 | memcpy(&plb[first], path, cnt1); |
157 | memcpy(&plb[first], path, cnt1); |
160 | memcpy(plb, &path[cnt1], cnt2); |
158 | memcpy(plb, &path[cnt1], cnt2); |
161 | 159 | ||
162 | ipc_call_t answer; |
160 | ipc_call_t answer; |
163 | int phone = vfs_grab_phone(root->fs_handle); |
161 | int phone = vfs_grab_phone(root->fs_handle); |
164 | aid_t req = async_send_3(phone, VFS_LOOKUP, (ipcarg_t) first, |
162 | aid_t req = async_send_3(phone, VFS_LOOKUP, (ipcarg_t) first, |
165 | (ipcarg_t) (first + len - 1) % PLB_SIZE, |
163 | (ipcarg_t) (first + len - 1) % PLB_SIZE, |
166 | (ipcarg_t) root->dev_handle, &answer); |
164 | (ipcarg_t) root->dev_handle, &answer); |
167 | vfs_release_phone(phone); |
165 | vfs_release_phone(phone); |
168 | 166 | ||
169 | ipcarg_t rc; |
167 | ipcarg_t rc; |
170 | async_wait_for(req, &rc); |
168 | async_wait_for(req, &rc); |
171 | 169 | ||
172 | futex_down(&plb_futex); |
170 | futex_down(&plb_futex); |
173 | list_remove(&entry.plb_link); |
171 | list_remove(&entry.plb_link); |
174 | /* |
172 | /* |
175 | * Erasing the path from PLB will come handy for debugging purposes. |
173 | * Erasing the path from PLB will come handy for debugging purposes. |
176 | */ |
174 | */ |
177 | memset(&plb[first], 0, cnt1); |
175 | memset(&plb[first], 0, cnt1); |
178 | memset(plb, 0, cnt2); |
176 | memset(plb, 0, cnt2); |
179 | futex_up(&plb_futex); |
177 | futex_up(&plb_futex); |
180 | 178 | ||
181 | if (rc == EOK) { |
179 | if (rc == EOK) { |
182 | result->fs_handle = (int) IPC_GET_ARG1(answer); |
180 | result->triplet.fs_handle = (int) IPC_GET_ARG1(answer); |
183 | result->dev_handle = (int) IPC_GET_ARG2(answer); |
181 | result->triplet.dev_handle = (int) IPC_GET_ARG2(answer); |
184 | result->index = (int) IPC_GET_ARG3(answer); |
182 | result->triplet.index = (int) IPC_GET_ARG3(answer); |
185 | if (size) |
- | |
186 | *size = (size_t) IPC_GET_ARG4(answer); |
183 | result->size = (size_t) IPC_GET_ARG4(answer); |
187 | } |
184 | } |
188 | 185 | ||
189 | return rc; |
186 | return rc; |
190 | } |
187 | } |
191 | 188 | ||
192 | atomic_t rootfs_futex = FUTEX_INITIALIZER; |
189 | atomic_t rootfs_futex = FUTEX_INITIALIZER; |
193 | vfs_triplet_t rootfs = { |
190 | vfs_triplet_t rootfs = { |
194 | .fs_handle = 0, |
191 | .fs_handle = 0, |
195 | .dev_handle = 0, |
192 | .dev_handle = 0, |
196 | .index = 0, |
193 | .index = 0, |
197 | }; |
194 | }; |
198 | 195 | ||
199 | static int lookup_root(int fs_handle, int dev_handle, vfs_triplet_t *root, |
196 | static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result) |
200 | size_t *size) |
- | |
201 | { |
197 | { |
202 | vfs_pair_t altroot = { |
198 | vfs_pair_t altroot = { |
203 | .fs_handle = fs_handle, |
199 | .fs_handle = fs_handle, |
204 | .dev_handle = dev_handle, |
200 | .dev_handle = dev_handle, |
205 | }; |
201 | }; |
206 | 202 | ||
207 | return vfs_lookup_internal("/", strlen("/"), root, size, &altroot); |
203 | return vfs_lookup_internal("/", strlen("/"), result, &altroot); |
208 | } |
204 | } |
209 | 205 | ||
210 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
206 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
211 | { |
207 | { |
212 | int dev_handle; |
208 | int dev_handle; |
213 | vfs_node_t *mp_node = NULL; |
209 | vfs_node_t *mp_node = NULL; |
214 | 210 | ||
215 | /* |
211 | /* |
216 | * We expect the library to do the device-name to device-handle |
212 | * We expect the library to do the device-name to device-handle |
217 | * translation for us, thus the device handle will arrive as ARG1 |
213 | * translation for us, thus the device handle will arrive as ARG1 |
218 | * in the request. |
214 | * in the request. |
219 | */ |
215 | */ |
220 | dev_handle = IPC_GET_ARG1(*request); |
216 | dev_handle = IPC_GET_ARG1(*request); |
221 | 217 | ||
222 | /* |
218 | /* |
223 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
219 | * For now, don't make use of ARG2 and ARG3, but they can be used to |
224 | * carry mount options in the future. |
220 | * carry mount options in the future. |
225 | */ |
221 | */ |
226 | 222 | ||
227 | ipc_callid_t callid; |
223 | ipc_callid_t callid; |
228 | size_t size; |
224 | size_t size; |
229 | 225 | ||
230 | /* |
226 | /* |
231 | * Now, we expect the client to send us data with the name of the file |
227 | * Now, we expect the client to send us data with the name of the file |
232 | * system. |
228 | * system. |
233 | */ |
229 | */ |
234 | if (!ipc_data_write_receive(&callid, &size)) { |
230 | if (!ipc_data_write_receive(&callid, &size)) { |
235 | ipc_answer_0(callid, EINVAL); |
231 | ipc_answer_0(callid, EINVAL); |
236 | ipc_answer_0(rid, EINVAL); |
232 | ipc_answer_0(rid, EINVAL); |
237 | return; |
233 | return; |
238 | } |
234 | } |
239 | 235 | ||
240 | /* |
236 | /* |
241 | * Don't receive more than is necessary for storing a full file system |
237 | * Don't receive more than is necessary for storing a full file system |
242 | * name. |
238 | * name. |
243 | */ |
239 | */ |
244 | if (size < 1 || size > FS_NAME_MAXLEN) { |
240 | if (size < 1 || size > FS_NAME_MAXLEN) { |
245 | ipc_answer_0(callid, EINVAL); |
241 | ipc_answer_0(callid, EINVAL); |
246 | ipc_answer_0(rid, EINVAL); |
242 | ipc_answer_0(rid, EINVAL); |
247 | return; |
243 | return; |
248 | } |
244 | } |
249 | 245 | ||
250 | /* |
246 | /* |
251 | * Deliver the file system name. |
247 | * Deliver the file system name. |
252 | */ |
248 | */ |
253 | char fs_name[FS_NAME_MAXLEN + 1]; |
249 | char fs_name[FS_NAME_MAXLEN + 1]; |
254 | (void) ipc_data_write_finalize(callid, fs_name, size); |
250 | (void) ipc_data_write_finalize(callid, fs_name, size); |
255 | fs_name[size] = '\0'; |
251 | fs_name[size] = '\0'; |
256 | 252 | ||
257 | /* |
253 | /* |
258 | * Check if we know a file system with the same name as is in fs_name. |
254 | * Check if we know a file system with the same name as is in fs_name. |
259 | * This will also give us its file system handle. |
255 | * This will also give us its file system handle. |
260 | */ |
256 | */ |
261 | int fs_handle = fs_name_to_handle(fs_name, true); |
257 | int fs_handle = fs_name_to_handle(fs_name, true); |
262 | if (!fs_handle) { |
258 | if (!fs_handle) { |
263 | ipc_answer_0(rid, ENOENT); |
259 | ipc_answer_0(rid, ENOENT); |
264 | return; |
260 | return; |
265 | } |
261 | } |
266 | 262 | ||
267 | /* |
263 | /* |
268 | * Now, we want the client to send us the mount point. |
264 | * Now, we want the client to send us the mount point. |
269 | */ |
265 | */ |
270 | if (!ipc_data_write_receive(&callid, &size)) { |
266 | if (!ipc_data_write_receive(&callid, &size)) { |
271 | ipc_answer_0(callid, EINVAL); |
267 | ipc_answer_0(callid, EINVAL); |
272 | ipc_answer_0(rid, EINVAL); |
268 | ipc_answer_0(rid, EINVAL); |
273 | return; |
269 | return; |
274 | } |
270 | } |
275 | 271 | ||
276 | /* |
272 | /* |
277 | * Check whether size is reasonable wrt. the mount point. |
273 | * Check whether size is reasonable wrt. the mount point. |
278 | */ |
274 | */ |
279 | if (size < 1 || size > MAX_PATH_LEN) { |
275 | if (size < 1 || size > MAX_PATH_LEN) { |
280 | ipc_answer_0(callid, EINVAL); |
276 | ipc_answer_0(callid, EINVAL); |
281 | ipc_answer_0(rid, EINVAL); |
277 | ipc_answer_0(rid, EINVAL); |
282 | return; |
278 | return; |
283 | } |
279 | } |
284 | /* |
280 | /* |
285 | * Allocate buffer for the mount point data being received. |
281 | * Allocate buffer for the mount point data being received. |
286 | */ |
282 | */ |
287 | uint8_t *buf; |
283 | uint8_t *buf; |
288 | buf = malloc(size); |
284 | buf = malloc(size); |
289 | if (!buf) { |
285 | if (!buf) { |
290 | ipc_answer_0(callid, ENOMEM); |
286 | ipc_answer_0(callid, ENOMEM); |
291 | ipc_answer_0(rid, ENOMEM); |
287 | ipc_answer_0(rid, ENOMEM); |
292 | return; |
288 | return; |
293 | } |
289 | } |
294 | 290 | ||
295 | /* |
291 | /* |
296 | * Deliver the mount point. |
292 | * Deliver the mount point. |
297 | */ |
293 | */ |
298 | (void) ipc_data_write_finalize(callid, buf, size); |
294 | (void) ipc_data_write_finalize(callid, buf, size); |
299 | 295 | ||
300 | /* |
296 | /* |
301 | * Lookup the root node of the filesystem being mounted. |
297 | * Lookup the root node of the filesystem being mounted. |
302 | * In this case, we don't need to take the namespace_futex as the root |
298 | * In this case, we don't need to take the namespace_futex as the root |
303 | * node cannot be removed. However, we do take a reference to it so |
299 | * node cannot be removed. However, we do take a reference to it so |
304 | * that we can track how many times it has been mounted. |
300 | * that we can track how many times it has been mounted. |
305 | */ |
301 | */ |
306 | int rc; |
302 | int rc; |
307 | vfs_triplet_t mounted_root; |
303 | vfs_lookup_res_t mr_res; |
308 | size_t mrsz; |
- | |
309 | rc = lookup_root(fs_handle, dev_handle, &mounted_root, &mrsz); |
304 | rc = lookup_root(fs_handle, dev_handle, &mr_res); |
310 | if (rc != EOK) { |
305 | if (rc != EOK) { |
311 | free(buf); |
306 | free(buf); |
312 | ipc_answer_0(rid, rc); |
307 | ipc_answer_0(rid, rc); |
313 | return; |
308 | return; |
314 | } |
309 | } |
315 | vfs_node_t *mr_node = vfs_node_get(&mounted_root, mrsz); |
310 | vfs_node_t *mr_node = vfs_node_get(&mr_res); |
316 | if (!mr_node) { |
311 | if (!mr_node) { |
317 | free(buf); |
312 | free(buf); |
318 | ipc_answer_0(rid, ENOMEM); |
313 | ipc_answer_0(rid, ENOMEM); |
319 | return; |
314 | return; |
320 | } |
315 | } |
321 | 316 | ||
322 | /* |
317 | /* |
323 | * Finally, we need to resolve the path to the mountpoint. |
318 | * Finally, we need to resolve the path to the mountpoint. |
324 | */ |
319 | */ |
325 | vfs_triplet_t mp; |
320 | vfs_lookup_res_t mp_res; |
326 | size_t mpsz; |
- | |
327 | futex_down(&rootfs_futex); |
321 | futex_down(&rootfs_futex); |
328 | if (rootfs.fs_handle) { |
322 | if (rootfs.fs_handle) { |
329 | /* |
323 | /* |
330 | * We already have the root FS. |
324 | * We already have the root FS. |
331 | */ |
325 | */ |
332 | rwlock_write_lock(&namespace_rwlock); |
326 | rwlock_write_lock(&namespace_rwlock); |
333 | rc = vfs_lookup_internal(buf, size, &mp, &mpsz, NULL); |
327 | rc = vfs_lookup_internal(buf, size, &mp_res, NULL); |
334 | if (rc != EOK) { |
328 | if (rc != EOK) { |
335 | /* |
329 | /* |
336 | * The lookup failed for some reason. |
330 | * The lookup failed for some reason. |
337 | */ |
331 | */ |
338 | rwlock_write_unlock(&namespace_rwlock); |
332 | rwlock_write_unlock(&namespace_rwlock); |
339 | futex_up(&rootfs_futex); |
333 | futex_up(&rootfs_futex); |
340 | vfs_node_put(mr_node); /* failed -> drop reference */ |
334 | vfs_node_put(mr_node); /* failed -> drop reference */ |
341 | free(buf); |
335 | free(buf); |
342 | ipc_answer_0(rid, rc); |
336 | ipc_answer_0(rid, rc); |
343 | return; |
337 | return; |
344 | } |
338 | } |
345 | mp_node = vfs_node_get(&mp, mpsz); |
339 | mp_node = vfs_node_get(&mp_res); |
346 | if (!mp_node) { |
340 | if (!mp_node) { |
347 | rwlock_write_unlock(&namespace_rwlock); |
341 | rwlock_write_unlock(&namespace_rwlock); |
348 | futex_up(&rootfs_futex); |
342 | futex_up(&rootfs_futex); |
349 | vfs_node_put(mr_node); /* failed -> drop reference */ |
343 | vfs_node_put(mr_node); /* failed -> drop reference */ |
350 | free(buf); |
344 | free(buf); |
351 | ipc_answer_0(rid, ENOMEM); |
345 | ipc_answer_0(rid, ENOMEM); |
352 | return; |
346 | return; |
353 | } |
347 | } |
354 | /* |
348 | /* |
355 | * Now we hold a reference to mp_node. |
349 | * Now we hold a reference to mp_node. |
356 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
350 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
357 | * This prevents the mount point from being deleted. |
351 | * This prevents the mount point from being deleted. |
358 | */ |
352 | */ |
359 | rwlock_write_unlock(&namespace_rwlock); |
353 | rwlock_write_unlock(&namespace_rwlock); |
360 | } else { |
354 | } else { |
361 | /* |
355 | /* |
362 | * We still don't have the root file system mounted. |
356 | * We still don't have the root file system mounted. |
363 | */ |
357 | */ |
364 | if ((size == 1) && (buf[0] == '/')) { |
358 | if ((size == 1) && (buf[0] == '/')) { |
365 | /* |
359 | /* |
366 | * For this simple, but important case, we are done. |
360 | * For this simple, but important case, we are done. |
367 | */ |
361 | */ |
368 | rootfs = mounted_root; |
362 | rootfs = mr_res.triplet; |
369 | futex_up(&rootfs_futex); |
363 | futex_up(&rootfs_futex); |
370 | free(buf); |
364 | free(buf); |
371 | ipc_answer_0(rid, EOK); |
365 | ipc_answer_0(rid, EOK); |
372 | return; |
366 | return; |
373 | } else { |
367 | } else { |
374 | /* |
368 | /* |
375 | * We can't resolve this without the root filesystem |
369 | * We can't resolve this without the root filesystem |
376 | * being mounted first. |
370 | * being mounted first. |
377 | */ |
371 | */ |
378 | futex_up(&rootfs_futex); |
372 | futex_up(&rootfs_futex); |
379 | free(buf); |
373 | free(buf); |
380 | vfs_node_put(mr_node); /* failed -> drop reference */ |
374 | vfs_node_put(mr_node); /* failed -> drop reference */ |
381 | ipc_answer_0(rid, ENOENT); |
375 | ipc_answer_0(rid, ENOENT); |
382 | return; |
376 | return; |
383 | } |
377 | } |
384 | } |
378 | } |
385 | futex_up(&rootfs_futex); |
379 | futex_up(&rootfs_futex); |
386 | 380 | ||
387 | free(buf); /* The buffer is not needed anymore. */ |
381 | free(buf); /* The buffer is not needed anymore. */ |
388 | 382 | ||
389 | /* |
383 | /* |
390 | * At this point, we have all necessary pieces: file system and device |
384 | * At this point, we have all necessary pieces: file system and device |
391 | * handles, and we know the mount point VFS node and also the root node |
385 | * handles, and we know the mount point VFS node and also the root node |
392 | * of the file system being mounted. |
386 | * of the file system being mounted. |
393 | */ |
387 | */ |
394 | 388 | ||
395 | int phone = vfs_grab_phone(mp.fs_handle); |
389 | int phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
396 | /* Later we can use ARG3 to pass mode/flags. */ |
390 | /* Later we can use ARG3 to pass mode/flags. */ |
397 | aid_t req1 = async_send_3(phone, VFS_MOUNT, (ipcarg_t) mp.dev_handle, |
391 | aid_t req1 = async_send_3(phone, VFS_MOUNT, |
- | 392 | (ipcarg_t) mp_res.triplet.dev_handle, |
|
398 | (ipcarg_t) mp.index, 0, NULL); |
393 | (ipcarg_t) mp_res.triplet.index, 0, NULL); |
399 | /* The second call uses the same method. */ |
394 | /* The second call uses the same method. */ |
400 | aid_t req2 = async_send_3(phone, VFS_MOUNT, |
395 | aid_t req2 = async_send_3(phone, VFS_MOUNT, |
401 | (ipcarg_t) mounted_root.fs_handle, |
396 | (ipcarg_t) mr_res.triplet.fs_handle, |
402 | (ipcarg_t) mounted_root.dev_handle, (ipcarg_t) mounted_root.index, |
397 | (ipcarg_t) mr_res.triplet.dev_handle, |
403 | NULL); |
398 | (ipcarg_t) mr_res.triplet.index, NULL); |
404 | vfs_release_phone(phone); |
399 | vfs_release_phone(phone); |
405 | 400 | ||
406 | ipcarg_t rc1; |
401 | ipcarg_t rc1; |
407 | ipcarg_t rc2; |
402 | ipcarg_t rc2; |
408 | async_wait_for(req1, &rc1); |
403 | async_wait_for(req1, &rc1); |
409 | async_wait_for(req2, &rc2); |
404 | async_wait_for(req2, &rc2); |
410 | 405 | ||
411 | if ((rc1 != EOK) || (rc2 != EOK)) { |
406 | if ((rc1 != EOK) || (rc2 != EOK)) { |
412 | /* Mount failed, drop references to mr_node and mp_node. */ |
407 | /* Mount failed, drop references to mr_node and mp_node. */ |
413 | vfs_node_put(mr_node); |
408 | vfs_node_put(mr_node); |
414 | if (mp_node) |
409 | if (mp_node) |
415 | vfs_node_put(mp_node); |
410 | vfs_node_put(mp_node); |
416 | } |
411 | } |
417 | 412 | ||
418 | if (rc2 == EOK) |
413 | if (rc2 == EOK) |
419 | ipc_answer_0(rid, rc1); |
414 | ipc_answer_0(rid, rc1); |
420 | else if (rc1 == EOK) |
415 | else if (rc1 == EOK) |
421 | ipc_answer_0(rid, rc2); |
416 | ipc_answer_0(rid, rc2); |
422 | else |
417 | else |
423 | ipc_answer_0(rid, rc1); |
418 | ipc_answer_0(rid, rc1); |
424 | } |
419 | } |
425 | 420 | ||
426 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
421 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
427 | { |
422 | { |
428 | if (!vfs_files_init()) { |
423 | if (!vfs_files_init()) { |
429 | ipc_answer_0(rid, ENOMEM); |
424 | ipc_answer_0(rid, ENOMEM); |
430 | return; |
425 | return; |
431 | } |
426 | } |
432 | 427 | ||
433 | /* |
428 | /* |
434 | * The POSIX interface is open(path, flags, mode). |
429 | * The POSIX interface is open(path, flags, mode). |
435 | * We can receive flags and mode along with the VFS_OPEN call; the path |
430 | * We can receive flags and mode along with the VFS_OPEN call; the path |
436 | * will need to arrive in another call. |
431 | * will need to arrive in another call. |
437 | */ |
432 | */ |
438 | int flags = IPC_GET_ARG1(*request); |
433 | int flags = IPC_GET_ARG1(*request); |
439 | int mode = IPC_GET_ARG2(*request); |
434 | int mode = IPC_GET_ARG2(*request); |
440 | size_t len; |
435 | size_t len; |
441 | 436 | ||
442 | ipc_callid_t callid; |
437 | ipc_callid_t callid; |
443 | 438 | ||
444 | if (!ipc_data_write_receive(&callid, &len)) { |
439 | if (!ipc_data_write_receive(&callid, &len)) { |
445 | ipc_answer_0(callid, EINVAL); |
440 | ipc_answer_0(callid, EINVAL); |
446 | ipc_answer_0(rid, EINVAL); |
441 | ipc_answer_0(rid, EINVAL); |
447 | return; |
442 | return; |
448 | } |
443 | } |
449 | 444 | ||
450 | /* |
445 | /* |
451 | * Now we are on the verge of accepting the path. |
446 | * Now we are on the verge of accepting the path. |
452 | * |
447 | * |
453 | * There is one optimization we could do in the future: copy the path |
448 | * There is one optimization we could do in the future: copy the path |
454 | * directly into the PLB using some kind of a callback. |
449 | * directly into the PLB using some kind of a callback. |
455 | */ |
450 | */ |
456 | char *path = malloc(len); |
451 | char *path = malloc(len); |
457 | 452 | ||
458 | if (!path) { |
453 | if (!path) { |
459 | ipc_answer_0(callid, ENOMEM); |
454 | ipc_answer_0(callid, ENOMEM); |
460 | ipc_answer_0(rid, ENOMEM); |
455 | ipc_answer_0(rid, ENOMEM); |
461 | return; |
456 | return; |
462 | } |
457 | } |
463 | 458 | ||
464 | int rc; |
459 | int rc; |
465 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
460 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
466 | ipc_answer_0(rid, rc); |
461 | ipc_answer_0(rid, rc); |
467 | free(path); |
462 | free(path); |
468 | return; |
463 | return; |
469 | } |
464 | } |
470 | 465 | ||
471 | /* |
466 | /* |
472 | * Avoid the race condition in which the file can be deleted before we |
467 | * Avoid the race condition in which the file can be deleted before we |
473 | * find/create-and-lock the VFS node corresponding to the looked-up |
468 | * find/create-and-lock the VFS node corresponding to the looked-up |
474 | * triplet. |
469 | * triplet. |
475 | */ |
470 | */ |
476 | rwlock_read_lock(&namespace_rwlock); |
471 | rwlock_read_lock(&namespace_rwlock); |
477 | 472 | ||
478 | /* |
473 | /* |
479 | * The path is now populated and we can call vfs_lookup_internal(). |
474 | * The path is now populated and we can call vfs_lookup_internal(). |
480 | */ |
475 | */ |
481 | vfs_triplet_t triplet; |
476 | vfs_lookup_res_t lr; |
482 | size_t size; |
- | |
483 | rc = vfs_lookup_internal(path, len, &triplet, &size, NULL); |
477 | rc = vfs_lookup_internal(path, len, &lr, NULL); |
484 | if (rc) { |
478 | if (rc) { |
485 | rwlock_read_unlock(&namespace_rwlock); |
479 | rwlock_read_unlock(&namespace_rwlock); |
486 | ipc_answer_0(rid, rc); |
480 | ipc_answer_0(rid, rc); |
487 | free(path); |
481 | free(path); |
488 | return; |
482 | return; |
489 | } |
483 | } |
490 | 484 | ||
491 | /* |
485 | /* |
492 | * Path is no longer needed. |
486 | * Path is no longer needed. |
493 | */ |
487 | */ |
494 | free(path); |
488 | free(path); |
495 | 489 | ||
496 | vfs_node_t *node = vfs_node_get(&triplet, size); |
490 | vfs_node_t *node = vfs_node_get(&lr); |
497 | rwlock_read_unlock(&namespace_rwlock); |
491 | rwlock_read_unlock(&namespace_rwlock); |
498 | 492 | ||
499 | /* |
493 | /* |
500 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
494 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
501 | * structure. |
495 | * structure. |
502 | */ |
496 | */ |
503 | int fd = vfs_fd_alloc(); |
497 | int fd = vfs_fd_alloc(); |
504 | if (fd < 0) { |
498 | if (fd < 0) { |
505 | vfs_node_put(node); |
499 | vfs_node_put(node); |
506 | ipc_answer_0(rid, fd); |
500 | ipc_answer_0(rid, fd); |
507 | return; |
501 | return; |
508 | } |
502 | } |
509 | vfs_file_t *file = vfs_file_get(fd); |
503 | vfs_file_t *file = vfs_file_get(fd); |
510 | file->node = node; |
504 | file->node = node; |
511 | 505 | ||
512 | /* |
506 | /* |
513 | * The following increase in reference count is for the fact that the |
507 | * The following increase in reference count is for the fact that the |
514 | * file is being opened and that a file structure is pointing to it. |
508 | * file is being opened and that a file structure is pointing to it. |
515 | * It is necessary so that the file will not disappear when |
509 | * It is necessary so that the file will not disappear when |
516 | * vfs_node_put() is called. The reference will be dropped by the |
510 | * vfs_node_put() is called. The reference will be dropped by the |
517 | * respective VFS_CLOSE. |
511 | * respective VFS_CLOSE. |
518 | */ |
512 | */ |
519 | vfs_node_addref(node); |
513 | vfs_node_addref(node); |
520 | vfs_node_put(node); |
514 | vfs_node_put(node); |
521 | 515 | ||
522 | /* |
516 | /* |
523 | * Success! Return the new file descriptor to the client. |
517 | * Success! Return the new file descriptor to the client. |
524 | */ |
518 | */ |
525 | ipc_answer_1(rid, EOK, fd); |
519 | ipc_answer_1(rid, EOK, fd); |
526 | } |
520 | } |
527 | 521 | ||
528 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
522 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
529 | { |
523 | { |
530 | 524 | ||
531 | /* |
525 | /* |
532 | * The following code strongly depends on the fact that the files data |
526 | * The following code strongly depends on the fact that the files data |
533 | * structure can be only accessed by a single fibril and all file |
527 | * structure can be only accessed by a single fibril and all file |
534 | * operations are serialized (i.e. the reads and writes cannot |
528 | * operations are serialized (i.e. the reads and writes cannot |
535 | * interleave and a file cannot be closed while it is being read). |
529 | * interleave and a file cannot be closed while it is being read). |
536 | * |
530 | * |
537 | * Additional synchronization needs to be added once the table of |
531 | * Additional synchronization needs to be added once the table of |
538 | * open files supports parallel access! |
532 | * open files supports parallel access! |
539 | */ |
533 | */ |
540 | 534 | ||
541 | int fd = IPC_GET_ARG1(*request); |
535 | int fd = IPC_GET_ARG1(*request); |
542 | 536 | ||
543 | /* |
537 | /* |
544 | * Lookup the file structure corresponding to the file descriptor. |
538 | * Lookup the file structure corresponding to the file descriptor. |
545 | */ |
539 | */ |
546 | vfs_file_t *file = vfs_file_get(fd); |
540 | vfs_file_t *file = vfs_file_get(fd); |
547 | if (!file) { |
541 | if (!file) { |
548 | ipc_answer_0(rid, ENOENT); |
542 | ipc_answer_0(rid, ENOENT); |
549 | return; |
543 | return; |
550 | } |
544 | } |
551 | 545 | ||
552 | /* |
546 | /* |
553 | * Now we need to receive a call with client's |
547 | * Now we need to receive a call with client's |
554 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
548 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
555 | */ |
549 | */ |
556 | ipc_callid_t callid; |
550 | ipc_callid_t callid; |
557 | int res; |
551 | int res; |
558 | if (read) |
552 | if (read) |
559 | res = ipc_data_read_receive(&callid, NULL); |
553 | res = ipc_data_read_receive(&callid, NULL); |
560 | else |
554 | else |
561 | res = ipc_data_write_receive(&callid, NULL); |
555 | res = ipc_data_write_receive(&callid, NULL); |
562 | if (!res) { |
556 | if (!res) { |
563 | ipc_answer_0(callid, EINVAL); |
557 | ipc_answer_0(callid, EINVAL); |
564 | ipc_answer_0(rid, EINVAL); |
558 | ipc_answer_0(rid, EINVAL); |
565 | return; |
559 | return; |
566 | } |
560 | } |
567 | 561 | ||
568 | /* |
562 | /* |
569 | * Lock the open file structure so that no other thread can manipulate |
563 | * Lock the open file structure so that no other thread can manipulate |
570 | * the same open file at a time. |
564 | * the same open file at a time. |
571 | */ |
565 | */ |
572 | futex_down(&file->lock); |
566 | futex_down(&file->lock); |
573 | 567 | ||
574 | /* |
568 | /* |
575 | * Lock the file's node so that no other client can read/write to it at |
569 | * Lock the file's node so that no other client can read/write to it at |
576 | * the same time. |
570 | * the same time. |
577 | */ |
571 | */ |
578 | if (read) |
572 | if (read) |
579 | rwlock_read_lock(&file->node->contents_rwlock); |
573 | rwlock_read_lock(&file->node->contents_rwlock); |
580 | else |
574 | else |
581 | rwlock_write_lock(&file->node->contents_rwlock); |
575 | rwlock_write_lock(&file->node->contents_rwlock); |
582 | 576 | ||
583 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
577 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
584 | 578 | ||
585 | /* |
579 | /* |
586 | * Make a VFS_READ/VFS_WRITE request at the destination FS server. |
580 | * Make a VFS_READ/VFS_WRITE request at the destination FS server. |
587 | */ |
581 | */ |
588 | aid_t msg; |
582 | aid_t msg; |
589 | ipc_call_t answer; |
583 | ipc_call_t answer; |
590 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
584 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
591 | file->node->dev_handle, file->node->index, file->pos, &answer); |
585 | file->node->dev_handle, file->node->index, file->pos, &answer); |
592 | 586 | ||
593 | /* |
587 | /* |
594 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
588 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
595 | * destination FS server. The call will be routed as if sent by |
589 | * destination FS server. The call will be routed as if sent by |
596 | * ourselves. Note that call arguments are immutable in this case so we |
590 | * ourselves. Note that call arguments are immutable in this case so we |
597 | * don't have to bother. |
591 | * don't have to bother. |
598 | */ |
592 | */ |
599 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
593 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
600 | 594 | ||
601 | vfs_release_phone(fs_phone); |
595 | vfs_release_phone(fs_phone); |
602 | 596 | ||
603 | /* |
597 | /* |
604 | * Wait for reply from the FS server. |
598 | * Wait for reply from the FS server. |
605 | */ |
599 | */ |
606 | ipcarg_t rc; |
600 | ipcarg_t rc; |
607 | async_wait_for(msg, &rc); |
601 | async_wait_for(msg, &rc); |
608 | size_t bytes = IPC_GET_ARG1(answer); |
602 | size_t bytes = IPC_GET_ARG1(answer); |
609 | 603 | ||
610 | /* |
604 | /* |
611 | * Unlock the VFS node. |
605 | * Unlock the VFS node. |
612 | */ |
606 | */ |
613 | if (read) |
607 | if (read) |
614 | rwlock_read_unlock(&file->node->contents_rwlock); |
608 | rwlock_read_unlock(&file->node->contents_rwlock); |
615 | else { |
609 | else { |
616 | /* Update the cached version of node's size. */ |
610 | /* Update the cached version of node's size. */ |
617 | file->node->size = IPC_GET_ARG2(answer); |
611 | file->node->size = IPC_GET_ARG2(answer); |
618 | rwlock_write_unlock(&file->node->contents_rwlock); |
612 | rwlock_write_unlock(&file->node->contents_rwlock); |
619 | } |
613 | } |
620 | 614 | ||
621 | /* |
615 | /* |
622 | * Update the position pointer and unlock the open file. |
616 | * Update the position pointer and unlock the open file. |
623 | */ |
617 | */ |
624 | file->pos += bytes; |
618 | file->pos += bytes; |
625 | futex_up(&file->lock); |
619 | futex_up(&file->lock); |
626 | 620 | ||
627 | /* |
621 | /* |
628 | * FS server's reply is the final result of the whole operation we |
622 | * FS server's reply is the final result of the whole operation we |
629 | * return to the client. |
623 | * return to the client. |
630 | */ |
624 | */ |
631 | ipc_answer_1(rid, rc, bytes); |
625 | ipc_answer_1(rid, rc, bytes); |
632 | } |
626 | } |
633 | 627 | ||
634 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
628 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
635 | { |
629 | { |
636 | vfs_rdwr(rid, request, true); |
630 | vfs_rdwr(rid, request, true); |
637 | } |
631 | } |
638 | 632 | ||
639 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
633 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
640 | { |
634 | { |
641 | vfs_rdwr(rid, request, false); |
635 | vfs_rdwr(rid, request, false); |
642 | } |
636 | } |
643 | 637 | ||
644 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
638 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
645 | { |
639 | { |
646 | int fd = (int) IPC_GET_ARG1(*request); |
640 | int fd = (int) IPC_GET_ARG1(*request); |
647 | off_t off = (off_t) IPC_GET_ARG2(*request); |
641 | off_t off = (off_t) IPC_GET_ARG2(*request); |
648 | int whence = (int) IPC_GET_ARG3(*request); |
642 | int whence = (int) IPC_GET_ARG3(*request); |
649 | 643 | ||
650 | 644 | ||
651 | /* |
645 | /* |
652 | * Lookup the file structure corresponding to the file descriptor. |
646 | * Lookup the file structure corresponding to the file descriptor. |
653 | */ |
647 | */ |
654 | vfs_file_t *file = vfs_file_get(fd); |
648 | vfs_file_t *file = vfs_file_get(fd); |
655 | if (!file) { |
649 | if (!file) { |
656 | ipc_answer_0(rid, ENOENT); |
650 | ipc_answer_0(rid, ENOENT); |
657 | return; |
651 | return; |
658 | } |
652 | } |
659 | 653 | ||
660 | off_t newpos; |
654 | off_t newpos; |
661 | futex_down(&file->lock); |
655 | futex_down(&file->lock); |
662 | if (whence == SEEK_SET) { |
656 | if (whence == SEEK_SET) { |
663 | file->pos = off; |
657 | file->pos = off; |
664 | futex_up(&file->lock); |
658 | futex_up(&file->lock); |
665 | ipc_answer_1(rid, EOK, off); |
659 | ipc_answer_1(rid, EOK, off); |
666 | return; |
660 | return; |
667 | } |
661 | } |
668 | if (whence == SEEK_CUR) { |
662 | if (whence == SEEK_CUR) { |
669 | if (file->pos + off < file->pos) { |
663 | if (file->pos + off < file->pos) { |
670 | futex_up(&file->lock); |
664 | futex_up(&file->lock); |
671 | ipc_answer_0(rid, EOVERFLOW); |
665 | ipc_answer_0(rid, EOVERFLOW); |
672 | return; |
666 | return; |
673 | } |
667 | } |
674 | file->pos += off; |
668 | file->pos += off; |
675 | newpos = file->pos; |
669 | newpos = file->pos; |
676 | futex_up(&file->lock); |
670 | futex_up(&file->lock); |
677 | ipc_answer_1(rid, EOK, newpos); |
671 | ipc_answer_1(rid, EOK, newpos); |
678 | return; |
672 | return; |
679 | } |
673 | } |
680 | if (whence == SEEK_END) { |
674 | if (whence == SEEK_END) { |
681 | rwlock_read_lock(&file->node->contents_rwlock); |
675 | rwlock_read_lock(&file->node->contents_rwlock); |
682 | size_t size = file->node->size; |
676 | size_t size = file->node->size; |
683 | rwlock_read_unlock(&file->node->contents_rwlock); |
677 | rwlock_read_unlock(&file->node->contents_rwlock); |
684 | if (size + off < size) { |
678 | if (size + off < size) { |
685 | futex_up(&file->lock); |
679 | futex_up(&file->lock); |
686 | ipc_answer_0(rid, EOVERFLOW); |
680 | ipc_answer_0(rid, EOVERFLOW); |
687 | return; |
681 | return; |
688 | } |
682 | } |
689 | newpos = size + off; |
683 | newpos = size + off; |
690 | futex_up(&file->lock); |
684 | futex_up(&file->lock); |
691 | ipc_answer_1(rid, EOK, newpos); |
685 | ipc_answer_1(rid, EOK, newpos); |
692 | return; |
686 | return; |
693 | } |
687 | } |
694 | futex_up(&file->lock); |
688 | futex_up(&file->lock); |
695 | ipc_answer_0(rid, EINVAL); |
689 | ipc_answer_0(rid, EINVAL); |
696 | } |
690 | } |
697 | 691 | ||
698 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
692 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
699 | link_t fs_head; |
693 | link_t fs_head; |
700 | 694 | ||
701 | atomic_t fs_handle_next = { |
695 | atomic_t fs_handle_next = { |
702 | .count = 1 |
696 | .count = 1 |
703 | }; |
697 | }; |
704 | 698 | ||
705 | /** Verify the VFS info structure. |
699 | /** Verify the VFS info structure. |
706 | * |
700 | * |
707 | * @param info Info structure to be verified. |
701 | * @param info Info structure to be verified. |
708 | * |
702 | * |
709 | * @return Non-zero if the info structure is sane, zero otherwise. |
703 | * @return Non-zero if the info structure is sane, zero otherwise. |
710 | */ |
704 | */ |
711 | static bool vfs_info_sane(vfs_info_t *info) |
705 | static bool vfs_info_sane(vfs_info_t *info) |
712 | { |
706 | { |
713 | int i; |
707 | int i; |
714 | 708 | ||
715 | /* |
709 | /* |
716 | * Check if the name is non-empty and is composed solely of ASCII |
710 | * Check if the name is non-empty and is composed solely of ASCII |
717 | * characters [a-z]+[a-z0-9_-]*. |
711 | * characters [a-z]+[a-z0-9_-]*. |
718 | */ |
712 | */ |
719 | if (!islower(info->name[0])) { |
713 | if (!islower(info->name[0])) { |
720 | dprintf("The name doesn't start with a lowercase character.\n"); |
714 | dprintf("The name doesn't start with a lowercase character.\n"); |
721 | return false; |
715 | return false; |
722 | } |
716 | } |
723 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
717 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
724 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
718 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
725 | (info->name[i] != '-') && (info->name[i] != '_')) { |
719 | (info->name[i] != '-') && (info->name[i] != '_')) { |
726 | if (info->name[i] == '\0') { |
720 | if (info->name[i] == '\0') { |
727 | break; |
721 | break; |
728 | } else { |
722 | } else { |
729 | dprintf("The name contains illegal " |
723 | dprintf("The name contains illegal " |
730 | "characters.\n"); |
724 | "characters.\n"); |
731 | return false; |
725 | return false; |
732 | } |
726 | } |
733 | } |
727 | } |
734 | } |
728 | } |
735 | /* |
729 | /* |
736 | * This check is not redundant. It ensures that the name is |
730 | * This check is not redundant. It ensures that the name is |
737 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used. |
731 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used. |
738 | */ |
732 | */ |
739 | if (info->name[i] != '\0') { |
733 | if (info->name[i] != '\0') { |
740 | dprintf("The name is not properly NULL-terminated.\n"); |
734 | dprintf("The name is not properly NULL-terminated.\n"); |
741 | return false; |
735 | return false; |
742 | } |
736 | } |
743 | 737 | ||
744 | 738 | ||
745 | /* |
739 | /* |
746 | * Check if the FS implements mandatory VFS operations. |
740 | * Check if the FS implements mandatory VFS operations. |
747 | */ |
741 | */ |
748 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) { |
742 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) { |
749 | dprintf("Operation VFS_LOOKUP not defined by the client.\n"); |
743 | dprintf("Operation VFS_LOOKUP not defined by the client.\n"); |
750 | return false; |
744 | return false; |
751 | } |
745 | } |
752 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) { |
746 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) { |
753 | dprintf("Operation VFS_OPEN not defined by the client.\n"); |
747 | dprintf("Operation VFS_OPEN not defined by the client.\n"); |
754 | return false; |
748 | return false; |
755 | } |
749 | } |
756 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) { |
750 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) { |
757 | dprintf("Operation VFS_CLOSE not defined by the client.\n"); |
751 | dprintf("Operation VFS_CLOSE not defined by the client.\n"); |
758 | return false; |
752 | return false; |
759 | } |
753 | } |
760 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) { |
754 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) { |
761 | dprintf("Operation VFS_READ not defined by the client.\n"); |
755 | dprintf("Operation VFS_READ not defined by the client.\n"); |
762 | return false; |
756 | return false; |
763 | } |
757 | } |
764 | 758 | ||
765 | /* |
759 | /* |
766 | * Check if each operation is either not defined, defined or default. |
760 | * Check if each operation is either not defined, defined or default. |
767 | */ |
761 | */ |
768 | for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) { |
762 | for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) { |
769 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) && |
763 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) && |
770 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) && |
764 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) && |
771 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) { |
765 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) { |
772 | dprintf("Operation info not understood.\n"); |
766 | dprintf("Operation info not understood.\n"); |
773 | return false; |
767 | return false; |
774 | } |
768 | } |
775 | } |
769 | } |
776 | return true; |
770 | return true; |
777 | } |
771 | } |
778 | 772 | ||
779 | /** VFS_REGISTER protocol function. |
773 | /** VFS_REGISTER protocol function. |
780 | * |
774 | * |
781 | * @param rid Hash of the call with the request. |
775 | * @param rid Hash of the call with the request. |
782 | * @param request Call structure with the request. |
776 | * @param request Call structure with the request. |
783 | */ |
777 | */ |
784 | void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
778 | void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
785 | { |
779 | { |
786 | ipc_callid_t callid; |
780 | ipc_callid_t callid; |
787 | ipc_call_t call; |
781 | ipc_call_t call; |
788 | int rc; |
782 | int rc; |
789 | size_t size; |
783 | size_t size; |
790 | 784 | ||
791 | dprintf("Processing VFS_REGISTER request received from %p.\n", |
785 | dprintf("Processing VFS_REGISTER request received from %p.\n", |
792 | request->in_phone_hash); |
786 | request->in_phone_hash); |
793 | 787 | ||
794 | /* |
788 | /* |
795 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
789 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
796 | * VFS info structure from the client FS. |
790 | * VFS info structure from the client FS. |
797 | */ |
791 | */ |
798 | if (!ipc_data_write_receive(&callid, &size)) { |
792 | if (!ipc_data_write_receive(&callid, &size)) { |
799 | /* |
793 | /* |
800 | * The client doesn't obey the same protocol as we do. |
794 | * The client doesn't obey the same protocol as we do. |
801 | */ |
795 | */ |
802 | dprintf("Receiving of VFS info failed.\n"); |
796 | dprintf("Receiving of VFS info failed.\n"); |
803 | ipc_answer_0(callid, EINVAL); |
797 | ipc_answer_0(callid, EINVAL); |
804 | ipc_answer_0(rid, EINVAL); |
798 | ipc_answer_0(rid, EINVAL); |
805 | return; |
799 | return; |
806 | } |
800 | } |
807 | 801 | ||
808 | dprintf("VFS info received, size = %d\n", size); |
802 | dprintf("VFS info received, size = %d\n", size); |
809 | 803 | ||
810 | /* |
804 | /* |
811 | * We know the size of the VFS info structure. See if the client |
805 | * We know the size of the VFS info structure. See if the client |
812 | * understands this easy concept too. |
806 | * understands this easy concept too. |
813 | */ |
807 | */ |
814 | if (size != sizeof(vfs_info_t)) { |
808 | if (size != sizeof(vfs_info_t)) { |
815 | /* |
809 | /* |
816 | * The client is sending us something, which cannot be |
810 | * The client is sending us something, which cannot be |
817 | * the info structure. |
811 | * the info structure. |
818 | */ |
812 | */ |
819 | dprintf("Received VFS info has bad size.\n"); |
813 | dprintf("Received VFS info has bad size.\n"); |
820 | ipc_answer_0(callid, EINVAL); |
814 | ipc_answer_0(callid, EINVAL); |
821 | ipc_answer_0(rid, EINVAL); |
815 | ipc_answer_0(rid, EINVAL); |
822 | return; |
816 | return; |
823 | } |
817 | } |
824 | 818 | ||
825 | /* |
819 | /* |
826 | * Allocate and initialize a buffer for the fs_info structure. |
820 | * Allocate and initialize a buffer for the fs_info structure. |
827 | */ |
821 | */ |
828 | fs_info_t *fs_info; |
822 | fs_info_t *fs_info; |
829 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
823 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
830 | if (!fs_info) { |
824 | if (!fs_info) { |
831 | dprintf("Could not allocate memory for FS info.\n"); |
825 | dprintf("Could not allocate memory for FS info.\n"); |
832 | ipc_answer_0(callid, ENOMEM); |
826 | ipc_answer_0(callid, ENOMEM); |
833 | ipc_answer_0(rid, ENOMEM); |
827 | ipc_answer_0(rid, ENOMEM); |
834 | return; |
828 | return; |
835 | } |
829 | } |
836 | link_initialize(&fs_info->fs_link); |
830 | link_initialize(&fs_info->fs_link); |
837 | futex_initialize(&fs_info->phone_futex, 1); |
831 | futex_initialize(&fs_info->phone_futex, 1); |
838 | 832 | ||
839 | rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size); |
833 | rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size); |
840 | if (rc != EOK) { |
834 | if (rc != EOK) { |
841 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n", |
835 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n", |
842 | rc); |
836 | rc); |
843 | free(fs_info); |
837 | free(fs_info); |
844 | ipc_answer_0(callid, rc); |
838 | ipc_answer_0(callid, rc); |
845 | ipc_answer_0(rid, rc); |
839 | ipc_answer_0(rid, rc); |
846 | return; |
840 | return; |
847 | } |
841 | } |
848 | 842 | ||
849 | dprintf("VFS info delivered.\n"); |
843 | dprintf("VFS info delivered.\n"); |
850 | 844 | ||
851 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
845 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
852 | free(fs_info); |
846 | free(fs_info); |
853 | ipc_answer_0(callid, EINVAL); |
847 | ipc_answer_0(callid, EINVAL); |
854 | ipc_answer_0(rid, EINVAL); |
848 | ipc_answer_0(rid, EINVAL); |
855 | return; |
849 | return; |
856 | } |
850 | } |
857 | 851 | ||
858 | futex_down(&fs_head_futex); |
852 | futex_down(&fs_head_futex); |
859 | 853 | ||
860 | /* |
854 | /* |
861 | * Check for duplicit registrations. |
855 | * Check for duplicit registrations. |
862 | */ |
856 | */ |
863 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) { |
857 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) { |
864 | /* |
858 | /* |
865 | * We already register a fs like this. |
859 | * We already register a fs like this. |
866 | */ |
860 | */ |
867 | dprintf("FS is already registered.\n"); |
861 | dprintf("FS is already registered.\n"); |
868 | futex_up(&fs_head_futex); |
862 | futex_up(&fs_head_futex); |
869 | free(fs_info); |
863 | free(fs_info); |
870 | ipc_answer_0(callid, EEXISTS); |
864 | ipc_answer_0(callid, EEXISTS); |
871 | ipc_answer_0(rid, EEXISTS); |
865 | ipc_answer_0(rid, EEXISTS); |
872 | return; |
866 | return; |
873 | } |
867 | } |
874 | 868 | ||
875 | /* |
869 | /* |
876 | * Add fs_info to the list of registered FS's. |
870 | * Add fs_info to the list of registered FS's. |
877 | */ |
871 | */ |
878 | dprintf("Inserting FS into the list of registered file systems.\n"); |
872 | dprintf("Inserting FS into the list of registered file systems.\n"); |
879 | list_append(&fs_info->fs_link, &fs_head); |
873 | list_append(&fs_info->fs_link, &fs_head); |
880 | 874 | ||
881 | /* |
875 | /* |
882 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so |
876 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so |
883 | * that a callback connection is created and we have a phone through |
877 | * that a callback connection is created and we have a phone through |
884 | * which to forward VFS requests to it. |
878 | * which to forward VFS requests to it. |
885 | */ |
879 | */ |
886 | callid = async_get_call(&call); |
880 | callid = async_get_call(&call); |
887 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
881 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
888 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
882 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
889 | list_remove(&fs_info->fs_link); |
883 | list_remove(&fs_info->fs_link); |
890 | futex_up(&fs_head_futex); |
884 | futex_up(&fs_head_futex); |
891 | free(fs_info); |
885 | free(fs_info); |
892 | ipc_answer_0(callid, EINVAL); |
886 | ipc_answer_0(callid, EINVAL); |
893 | ipc_answer_0(rid, EINVAL); |
887 | ipc_answer_0(rid, EINVAL); |
894 | return; |
888 | return; |
895 | } |
889 | } |
896 | fs_info->phone = IPC_GET_ARG5(call); |
890 | fs_info->phone = IPC_GET_ARG5(call); |
897 | ipc_answer_0(callid, EOK); |
891 | ipc_answer_0(callid, EOK); |
898 | 892 | ||
899 | dprintf("Callback connection to FS created.\n"); |
893 | dprintf("Callback connection to FS created.\n"); |
900 | 894 | ||
901 | /* |
895 | /* |
902 | * The client will want us to send him the address space area with PLB. |
896 | * The client will want us to send him the address space area with PLB. |
903 | */ |
897 | */ |
904 | 898 | ||
905 | if (!ipc_share_in_receive(&callid, &size)) { |
899 | if (!ipc_share_in_receive(&callid, &size)) { |
906 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
900 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
907 | list_remove(&fs_info->fs_link); |
901 | list_remove(&fs_info->fs_link); |
908 | futex_up(&fs_head_futex); |
902 | futex_up(&fs_head_futex); |
909 | ipc_hangup(fs_info->phone); |
903 | ipc_hangup(fs_info->phone); |
910 | free(fs_info); |
904 | free(fs_info); |
911 | ipc_answer_0(callid, EINVAL); |
905 | ipc_answer_0(callid, EINVAL); |
912 | ipc_answer_0(rid, EINVAL); |
906 | ipc_answer_0(rid, EINVAL); |
913 | return; |
907 | return; |
914 | } |
908 | } |
915 | 909 | ||
916 | /* |
910 | /* |
917 | * We can only send the client address space area PLB_SIZE bytes long. |
911 | * We can only send the client address space area PLB_SIZE bytes long. |
918 | */ |
912 | */ |
919 | if (size != PLB_SIZE) { |
913 | if (size != PLB_SIZE) { |
920 | dprintf("Client suggests wrong size of PFB, size = %d\n", size); |
914 | dprintf("Client suggests wrong size of PFB, size = %d\n", size); |
921 | list_remove(&fs_info->fs_link); |
915 | list_remove(&fs_info->fs_link); |
922 | futex_up(&fs_head_futex); |
916 | futex_up(&fs_head_futex); |
923 | ipc_hangup(fs_info->phone); |
917 | ipc_hangup(fs_info->phone); |
924 | free(fs_info); |
918 | free(fs_info); |
925 | ipc_answer_0(callid, EINVAL); |
919 | ipc_answer_0(callid, EINVAL); |
926 | ipc_answer_0(rid, EINVAL); |
920 | ipc_answer_0(rid, EINVAL); |
927 | return; |
921 | return; |
928 | } |
922 | } |
929 | 923 | ||
930 | /* |
924 | /* |
931 | * Commit to read-only sharing the PLB with the client. |
925 | * Commit to read-only sharing the PLB with the client. |
932 | */ |
926 | */ |
933 | (void) ipc_share_in_finalize(callid, plb, |
927 | (void) ipc_share_in_finalize(callid, plb, |
934 | AS_AREA_READ | AS_AREA_CACHEABLE); |
928 | AS_AREA_READ | AS_AREA_CACHEABLE); |
935 | 929 | ||
936 | dprintf("Sharing PLB.\n"); |
930 | dprintf("Sharing PLB.\n"); |
937 | 931 | ||
938 | /* |
932 | /* |
939 | * That was it. The FS has been registered. |
933 | * That was it. The FS has been registered. |
940 | * In reply to the VFS_REGISTER request, we assign the client file |
934 | * In reply to the VFS_REGISTER request, we assign the client file |
941 | * system a global file system handle. |
935 | * system a global file system handle. |
942 | */ |
936 | */ |
943 | fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next); |
937 | fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next); |
944 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle); |
938 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle); |
945 | 939 | ||
946 | futex_up(&fs_head_futex); |
940 | futex_up(&fs_head_futex); |
947 | 941 | ||
948 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n", |
942 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n", |
949 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle); |
943 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle); |
950 | } |
944 | } |
951 | 945 | ||
952 | /** For a given file system handle, implement policy for allocating a phone. |
946 | /** For a given file system handle, implement policy for allocating a phone. |
953 | * |
947 | * |
954 | * @param handle File system handle. |
948 | * @param handle File system handle. |
955 | * |
949 | * |
956 | * @return Phone over which a multi-call request can be safely |
950 | * @return Phone over which a multi-call request can be safely |
957 | * sent. Return 0 if no phone was found. |
951 | * sent. Return 0 if no phone was found. |
958 | */ |
952 | */ |
959 | int vfs_grab_phone(int handle) |
953 | int vfs_grab_phone(int handle) |
960 | { |
954 | { |
961 | /* |
955 | /* |
962 | * For now, we don't try to be very clever and very fast. |
956 | * For now, we don't try to be very clever and very fast. |
963 | * We simply lookup the phone in the fs_head list. We currently don't |
957 | * We simply lookup the phone in the fs_head list. We currently don't |
964 | * open any additional phones (even though that itself would be pretty |
958 | * open any additional phones (even though that itself would be pretty |
965 | * straightforward; housekeeping multiple open phones to a FS task would |
959 | * straightforward; housekeeping multiple open phones to a FS task would |
966 | * be more demanding). Instead, we simply take the respective |
960 | * be more demanding). Instead, we simply take the respective |
967 | * phone_futex and keep it until vfs_release_phone(). |
961 | * phone_futex and keep it until vfs_release_phone(). |
968 | */ |
962 | */ |
969 | futex_down(&fs_head_futex); |
963 | futex_down(&fs_head_futex); |
970 | link_t *cur; |
964 | link_t *cur; |
971 | fs_info_t *fs; |
965 | fs_info_t *fs; |
972 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
966 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
973 | fs = list_get_instance(cur, fs_info_t, fs_link); |
967 | fs = list_get_instance(cur, fs_info_t, fs_link); |
974 | if (fs->fs_handle == handle) { |
968 | if (fs->fs_handle == handle) { |
975 | futex_up(&fs_head_futex); |
969 | futex_up(&fs_head_futex); |
976 | /* |
970 | /* |
977 | * For now, take the futex unconditionally. |
971 | * For now, take the futex unconditionally. |
978 | * Oh yeah, serialization rocks. |
972 | * Oh yeah, serialization rocks. |
979 | * It will be up'ed in vfs_release_phone(). |
973 | * It will be up'ed in vfs_release_phone(). |
980 | */ |
974 | */ |
981 | futex_down(&fs->phone_futex); |
975 | futex_down(&fs->phone_futex); |
982 | /* |
976 | /* |
983 | * Avoid deadlock with other fibrils in the same thread |
977 | * Avoid deadlock with other fibrils in the same thread |
984 | * by disabling fibril preemption. |
978 | * by disabling fibril preemption. |
985 | */ |
979 | */ |
986 | fibril_inc_sercount(); |
980 | fibril_inc_sercount(); |
987 | return fs->phone; |
981 | return fs->phone; |
988 | } |
982 | } |
989 | } |
983 | } |
990 | futex_up(&fs_head_futex); |
984 | futex_up(&fs_head_futex); |
991 | return 0; |
985 | return 0; |
992 | } |
986 | } |
993 | 987 | ||
994 | /** Tell VFS that the phone is in use for any request. |
988 | /** Tell VFS that the phone is in use for any request. |
995 | * |
989 | * |
996 | * @param phone Phone to FS task. |
990 | * @param phone Phone to FS task. |
997 | */ |
991 | */ |
998 | void vfs_release_phone(int phone) |
992 | void vfs_release_phone(int phone) |
999 | { |
993 | { |
1000 | bool found = false; |
994 | bool found = false; |
1001 | 995 | ||
1002 | /* |
996 | /* |
1003 | * Undo the fibril_inc_sercount() done in vfs_grab_phone(). |
997 | * Undo the fibril_inc_sercount() done in vfs_grab_phone(). |
1004 | */ |
998 | */ |
1005 | fibril_dec_sercount(); |
999 | fibril_dec_sercount(); |
1006 | 1000 | ||
1007 | futex_down(&fs_head_futex); |
1001 | futex_down(&fs_head_futex); |
1008 | link_t *cur; |
1002 | link_t *cur; |
1009 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
1003 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
1010 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
1004 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
1011 | if (fs->phone == phone) { |
1005 | if (fs->phone == phone) { |
1012 | found = true; |
1006 | found = true; |
1013 | futex_up(&fs_head_futex); |
1007 | futex_up(&fs_head_futex); |
1014 | futex_up(&fs->phone_futex); |
1008 | futex_up(&fs->phone_futex); |
1015 | return; |
1009 | return; |
1016 | } |
1010 | } |
1017 | } |
1011 | } |
1018 | futex_up(&fs_head_futex); |
1012 | futex_up(&fs_head_futex); |
1019 | 1013 | ||
1020 | /* |
1014 | /* |
1021 | * Not good to get here. |
1015 | * Not good to get here. |
1022 | */ |
1016 | */ |
1023 | assert(found == true); |
1017 | assert(found == true); |
1024 | } |
1018 | } |
1025 | 1019 | ||
1026 | /** Convert file system name to its handle. |
1020 | /** Convert file system name to its handle. |
1027 | * |
1021 | * |
1028 | * @param name File system name. |
1022 | * @param name File system name. |
1029 | * @param lock If true, the function will down and up the |
1023 | * @param lock If true, the function will down and up the |
1030 | * fs_head_futex. |
1024 | * fs_head_futex. |
1031 | * |
1025 | * |
1032 | * @return File system handle or zero if file system not found. |
1026 | * @return File system handle or zero if file system not found. |
1033 | */ |
1027 | */ |
1034 | int fs_name_to_handle(char *name, bool lock) |
1028 | int fs_name_to_handle(char *name, bool lock) |
1035 | { |
1029 | { |
1036 | int handle = 0; |
1030 | int handle = 0; |
1037 | 1031 | ||
1038 | if (lock) |
1032 | if (lock) |
1039 | futex_down(&fs_head_futex); |
1033 | futex_down(&fs_head_futex); |
1040 | link_t *cur; |
1034 | link_t *cur; |
1041 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
1035 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
1042 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
1036 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
1043 | if (strncmp(fs->vfs_info.name, name, |
1037 | if (strncmp(fs->vfs_info.name, name, |
1044 | sizeof(fs->vfs_info.name)) == 0) { |
1038 | sizeof(fs->vfs_info.name)) == 0) { |
1045 | handle = fs->fs_handle; |
1039 | handle = fs->fs_handle; |
1046 | break; |
1040 | break; |
1047 | } |
1041 | } |
1048 | } |
1042 | } |
1049 | if (lock) |
1043 | if (lock) |
1050 | futex_up(&fs_head_futex); |
1044 | futex_up(&fs_head_futex); |
1051 | return handle; |
1045 | return handle; |
1052 | } |
1046 | } |
1053 | 1047 | ||
1054 | /** |
1048 | /** |
1055 | * @} |
1049 | * @} |
1056 | */ |
1050 | */ |
1057 | 1051 |