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