Rev 4539 | Rev 4566 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2689 | jermar | 1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup fs |
||
30 | * @{ |
||
4010 | decky | 31 | */ |
2689 | jermar | 32 | |
33 | /** |
||
4010 | decky | 34 | * @file vfs_ops.c |
35 | * @brief Operations that VFS offers to its clients. |
||
2689 | jermar | 36 | */ |
37 | |||
2763 | jermar | 38 | #include "vfs.h" |
2689 | jermar | 39 | #include <ipc/ipc.h> |
40 | #include <async.h> |
||
41 | #include <errno.h> |
||
42 | #include <stdio.h> |
||
43 | #include <stdlib.h> |
||
44 | #include <string.h> |
||
45 | #include <bool.h> |
||
46 | #include <futex.h> |
||
4518 | jermar | 47 | #include <fibril_sync.h> |
4509 | decky | 48 | #include <adt/list.h> |
2689 | jermar | 49 | #include <unistd.h> |
50 | #include <ctype.h> |
||
2708 | jermar | 51 | #include <fcntl.h> |
2689 | jermar | 52 | #include <assert.h> |
2763 | jermar | 53 | #include <vfs/canonify.h> |
2689 | jermar | 54 | |
2748 | jermar | 55 | /* Forward declarations of static functions. */ |
2770 | jermar | 56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t); |
2748 | jermar | 57 | |
4010 | decky | 58 | /** Pending mount structure. */ |
59 | typedef struct { |
||
60 | link_t link; |
||
61 | char *fs_name; /**< File system name */ |
||
62 | char *mp; /**< Mount point */ |
||
4305 | jermar | 63 | char *opts; /**< Mount options. */ |
4010 | decky | 64 | ipc_callid_t callid; /**< Call ID waiting for the mount */ |
65 | ipc_callid_t rid; /**< Request ID */ |
||
66 | dev_handle_t dev_handle; /**< Device handle */ |
||
67 | } pending_req_t; |
||
68 | |||
4539 | jermar | 69 | FIBRIL_CONDVAR_INITIALIZE(pending_cv); |
70 | bool pending_new_fs = false; /**< True if a new file system was mounted. */ |
||
4010 | decky | 71 | LIST_INITIALIZE(pending_req); |
72 | |||
2689 | jermar | 73 | /** |
74 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
||
75 | * concurrent VFS operation which modifies the file system namespace. |
||
76 | */ |
||
4518 | jermar | 77 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock); |
2689 | jermar | 78 | |
3109 | jermar | 79 | vfs_pair_t rootfs = { |
2689 | jermar | 80 | .fs_handle = 0, |
3109 | jermar | 81 | .dev_handle = 0 |
2689 | jermar | 82 | }; |
83 | |||
4010 | decky | 84 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
4305 | jermar | 85 | fs_handle_t fs_handle, char *mp, char *opts) |
2689 | jermar | 86 | { |
4010 | decky | 87 | vfs_lookup_res_t mp_res; |
4409 | jermar | 88 | vfs_lookup_res_t mr_res; |
2689 | jermar | 89 | vfs_node_t *mp_node = NULL; |
4409 | jermar | 90 | vfs_node_t *mr_node; |
91 | fs_index_t rindex; |
||
92 | size_t rsize; |
||
93 | unsigned rlnkcnt; |
||
4305 | jermar | 94 | ipcarg_t rc; |
2958 | jermar | 95 | int phone; |
4305 | jermar | 96 | aid_t msg; |
97 | ipc_call_t answer; |
||
4463 | decky | 98 | |
4305 | jermar | 99 | /* Resolve the path to the mountpoint. */ |
4518 | jermar | 100 | fibril_rwlock_write_lock(&namespace_rwlock); |
2689 | jermar | 101 | if (rootfs.fs_handle) { |
2707 | jermar | 102 | /* We already have the root FS. */ |
4264 | svoboda | 103 | if (str_cmp(mp, "/") == 0) { |
2788 | jermar | 104 | /* Trying to mount root FS over root FS */ |
4518 | jermar | 105 | fibril_rwlock_write_unlock(&namespace_rwlock); |
4431 | jermar | 106 | ipc_answer_0(rid, EBUSY); |
2788 | jermar | 107 | return; |
108 | } |
||
4010 | decky | 109 | |
110 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
||
2689 | jermar | 111 | if (rc != EOK) { |
2707 | jermar | 112 | /* The lookup failed for some reason. */ |
4518 | jermar | 113 | fibril_rwlock_write_unlock(&namespace_rwlock); |
4431 | jermar | 114 | ipc_answer_0(rid, rc); |
2689 | jermar | 115 | return; |
116 | } |
||
4010 | decky | 117 | |
2691 | jermar | 118 | mp_node = vfs_node_get(&mp_res); |
2689 | jermar | 119 | if (!mp_node) { |
4518 | jermar | 120 | fibril_rwlock_write_unlock(&namespace_rwlock); |
4431 | jermar | 121 | ipc_answer_0(rid, ENOMEM); |
2689 | jermar | 122 | return; |
123 | } |
||
4010 | decky | 124 | |
2689 | jermar | 125 | /* |
126 | * Now we hold a reference to mp_node. |
||
127 | * It will be dropped upon the corresponding VFS_UNMOUNT. |
||
128 | * This prevents the mount point from being deleted. |
||
129 | */ |
||
130 | } else { |
||
2707 | jermar | 131 | /* We still don't have the root file system mounted. */ |
4264 | svoboda | 132 | if (str_cmp(mp, "/") == 0) { |
2958 | jermar | 133 | /* |
134 | * For this simple, but important case, |
||
135 | * we are almost done. |
||
136 | */ |
||
137 | |||
3109 | jermar | 138 | /* Tell the mountee that it is being mounted. */ |
139 | phone = vfs_grab_phone(fs_handle); |
||
4305 | jermar | 140 | msg = async_send_1(phone, VFS_MOUNTED, |
141 | (ipcarg_t) dev_handle, &answer); |
||
142 | /* send the mount options */ |
||
143 | rc = ipc_data_write_start(phone, (void *)opts, |
||
144 | str_size(opts)); |
||
145 | if (rc != EOK) { |
||
4551 | jermar | 146 | async_wait_for(msg, NULL); |
4518 | jermar | 147 | vfs_release_phone(phone); |
148 | fibril_rwlock_write_unlock(&namespace_rwlock); |
||
4305 | jermar | 149 | ipc_answer_0(rid, rc); |
150 | return; |
||
151 | } |
||
4551 | jermar | 152 | async_wait_for(msg, &rc); |
4518 | jermar | 153 | vfs_release_phone(phone); |
3352 | jermar | 154 | |
155 | if (rc != EOK) { |
||
4518 | jermar | 156 | fibril_rwlock_write_unlock(&namespace_rwlock); |
3352 | jermar | 157 | ipc_answer_0(rid, rc); |
158 | return; |
||
3109 | jermar | 159 | } |
4305 | jermar | 160 | |
161 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
||
162 | rsize = (size_t) IPC_GET_ARG2(answer); |
||
163 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
||
4010 | decky | 164 | |
3352 | jermar | 165 | mr_res.triplet.fs_handle = fs_handle; |
166 | mr_res.triplet.dev_handle = dev_handle; |
||
4305 | jermar | 167 | mr_res.triplet.index = rindex; |
168 | mr_res.size = rsize; |
||
169 | mr_res.lnkcnt = rlnkcnt; |
||
3653 | jermar | 170 | mr_res.type = VFS_NODE_DIRECTORY; |
4010 | decky | 171 | |
3352 | jermar | 172 | rootfs.fs_handle = fs_handle; |
173 | rootfs.dev_handle = dev_handle; |
||
4010 | decky | 174 | |
3352 | jermar | 175 | /* Add reference to the mounted root. */ |
176 | mr_node = vfs_node_get(&mr_res); |
||
177 | assert(mr_node); |
||
4010 | decky | 178 | |
4518 | jermar | 179 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2958 | jermar | 180 | ipc_answer_0(rid, rc); |
2689 | jermar | 181 | return; |
182 | } else { |
||
183 | /* |
||
184 | * We can't resolve this without the root filesystem |
||
185 | * being mounted first. |
||
186 | */ |
||
4518 | jermar | 187 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2689 | jermar | 188 | ipc_answer_0(rid, ENOENT); |
189 | return; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /* |
||
194 | * At this point, we have all necessary pieces: file system and device |
||
3109 | jermar | 195 | * handles, and we know the mount point VFS node. |
2689 | jermar | 196 | */ |
4010 | decky | 197 | |
4409 | jermar | 198 | int mountee_phone = vfs_grab_phone(fs_handle); |
199 | assert(mountee_phone >= 0); |
||
200 | |||
2958 | jermar | 201 | phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
4305 | jermar | 202 | msg = async_send_4(phone, VFS_MOUNT, |
2691 | jermar | 203 | (ipcarg_t) mp_res.triplet.dev_handle, |
2957 | jermar | 204 | (ipcarg_t) mp_res.triplet.index, |
3109 | jermar | 205 | (ipcarg_t) fs_handle, |
4305 | jermar | 206 | (ipcarg_t) dev_handle, &answer); |
4409 | jermar | 207 | |
208 | /* send connection */ |
||
209 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
||
210 | if (rc != EOK) { |
||
4551 | jermar | 211 | async_wait_for(msg, NULL); |
212 | vfs_release_phone(mountee_phone); |
||
4518 | jermar | 213 | vfs_release_phone(phone); |
4409 | jermar | 214 | /* Mount failed, drop reference to mp_node. */ |
215 | if (mp_node) |
||
216 | vfs_node_put(mp_node); |
||
217 | ipc_answer_0(rid, rc); |
||
4518 | jermar | 218 | fibril_rwlock_write_unlock(&namespace_rwlock); |
4409 | jermar | 219 | return; |
220 | } |
||
4551 | jermar | 221 | |
222 | vfs_release_phone(mountee_phone); |
||
4409 | jermar | 223 | |
4305 | jermar | 224 | /* send the mount options */ |
225 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
||
226 | if (rc != EOK) { |
||
4551 | jermar | 227 | async_wait_for(msg, NULL); |
4518 | jermar | 228 | vfs_release_phone(phone); |
4305 | jermar | 229 | /* Mount failed, drop reference to mp_node. */ |
230 | if (mp_node) |
||
231 | vfs_node_put(mp_node); |
||
4518 | jermar | 232 | fibril_rwlock_write_unlock(&namespace_rwlock); |
4305 | jermar | 233 | ipc_answer_0(rid, rc); |
234 | return; |
||
235 | } |
||
4551 | jermar | 236 | async_wait_for(msg, &rc); |
4518 | jermar | 237 | vfs_release_phone(phone); |
4010 | decky | 238 | |
4431 | jermar | 239 | if (rc == EOK) { |
240 | rindex = (fs_index_t) IPC_GET_ARG1(answer); |
||
241 | rsize = (size_t) IPC_GET_ARG2(answer); |
||
242 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
||
243 | |||
244 | mr_res.triplet.fs_handle = fs_handle; |
||
245 | mr_res.triplet.dev_handle = dev_handle; |
||
246 | mr_res.triplet.index = rindex; |
||
247 | mr_res.size = rsize; |
||
248 | mr_res.lnkcnt = rlnkcnt; |
||
249 | mr_res.type = VFS_NODE_DIRECTORY; |
||
250 | |||
251 | /* Add reference to the mounted root. */ |
||
252 | mr_node = vfs_node_get(&mr_res); |
||
253 | assert(mr_node); |
||
254 | } else { |
||
3109 | jermar | 255 | /* Mount failed, drop reference to mp_node. */ |
2689 | jermar | 256 | if (mp_node) |
257 | vfs_node_put(mp_node); |
||
258 | } |
||
4409 | jermar | 259 | |
2957 | jermar | 260 | ipc_answer_0(rid, rc); |
4518 | jermar | 261 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2689 | jermar | 262 | } |
263 | |||
4010 | decky | 264 | /** Process pending mount requests */ |
4437 | jermar | 265 | void vfs_process_pending_mount(void) |
4010 | decky | 266 | { |
267 | link_t *cur; |
||
268 | |||
4539 | jermar | 269 | while (true) { |
270 | fibril_mutex_lock(&fs_head_lock); |
||
271 | while (!pending_new_fs) |
||
272 | fibril_condvar_wait(&pending_cv, &fs_head_lock); |
||
273 | rescan: |
||
274 | for (cur = pending_req.next; cur != &pending_req; |
||
275 | cur = cur->next) { |
||
276 | pending_req_t *pr = list_get_instance(cur, |
||
277 | pending_req_t, link); |
||
278 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, |
||
279 | false); |
||
280 | if (!fs_handle) |
||
281 | continue; |
||
4010 | decky | 282 | |
4539 | jermar | 283 | /* Acknowledge that we know fs_name. */ |
284 | ipc_answer_0(pr->callid, EOK); |
||
4010 | decky | 285 | |
4539 | jermar | 286 | list_remove(cur); |
287 | fibril_mutex_unlock(&fs_head_lock); |
||
288 | |||
289 | /* Do the mount */ |
||
290 | vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, |
||
291 | pr->mp, pr->opts); |
||
292 | |||
293 | free(pr->fs_name); |
||
294 | free(pr->mp); |
||
295 | free(pr->opts); |
||
296 | free(pr); |
||
4010 | decky | 297 | |
4539 | jermar | 298 | fibril_mutex_lock(&fs_head_lock); |
299 | goto rescan; |
||
300 | } |
||
301 | pending_new_fs = false; |
||
302 | fibril_mutex_unlock(&fs_head_lock); |
||
4010 | decky | 303 | } |
304 | } |
||
305 | |||
306 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
||
307 | { |
||
308 | /* |
||
309 | * We expect the library to do the device-name to device-handle |
||
310 | * translation for us, thus the device handle will arrive as ARG1 |
||
311 | * in the request. |
||
312 | */ |
||
313 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
314 | |||
315 | /* |
||
316 | * Mount flags are passed as ARG2. |
||
317 | */ |
||
318 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
||
319 | |||
320 | /* |
||
321 | * For now, don't make use of ARG3, but it can be used to |
||
322 | * carry mount options in the future. |
||
323 | */ |
||
324 | |||
325 | /* We want the client to send us the mount point. */ |
||
326 | ipc_callid_t callid; |
||
327 | size_t size; |
||
328 | if (!ipc_data_write_receive(&callid, &size)) { |
||
329 | ipc_answer_0(callid, EINVAL); |
||
330 | ipc_answer_0(rid, EINVAL); |
||
331 | return; |
||
332 | } |
||
333 | |||
334 | /* Check whether size is reasonable wrt. the mount point. */ |
||
335 | if ((size < 1) || (size > MAX_PATH_LEN)) { |
||
336 | ipc_answer_0(callid, EINVAL); |
||
337 | ipc_answer_0(rid, EINVAL); |
||
338 | return; |
||
339 | } |
||
340 | |||
341 | /* Allocate buffer for the mount point data being received. */ |
||
342 | char *mp = malloc(size + 1); |
||
343 | if (!mp) { |
||
344 | ipc_answer_0(callid, ENOMEM); |
||
345 | ipc_answer_0(rid, ENOMEM); |
||
346 | return; |
||
347 | } |
||
348 | |||
349 | /* Deliver the mount point. */ |
||
350 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
||
351 | if (retval != EOK) { |
||
4301 | jermar | 352 | ipc_answer_0(rid, retval); |
4010 | decky | 353 | free(mp); |
354 | return; |
||
355 | } |
||
356 | mp[size] = '\0'; |
||
357 | |||
4305 | jermar | 358 | /* Now we expect to receive the mount options. */ |
359 | if (!ipc_data_write_receive(&callid, &size)) { |
||
360 | ipc_answer_0(callid, EINVAL); |
||
361 | ipc_answer_0(rid, EINVAL); |
||
362 | free(mp); |
||
363 | return; |
||
364 | } |
||
365 | |||
366 | /* Check the offered options size. */ |
||
367 | if (size < 0 || size > MAX_MNTOPTS_LEN) { |
||
368 | ipc_answer_0(callid, EINVAL); |
||
369 | ipc_answer_0(rid, EINVAL); |
||
370 | free(mp); |
||
371 | return; |
||
372 | } |
||
373 | |||
374 | /* Allocate buffer for the mount options. */ |
||
375 | char *opts = (char *) malloc(size + 1); |
||
376 | if (!opts) { |
||
377 | ipc_answer_0(callid, ENOMEM); |
||
378 | ipc_answer_0(rid, ENOMEM); |
||
379 | free(mp); |
||
380 | return; |
||
381 | } |
||
382 | |||
383 | /* Deliver the mount options. */ |
||
384 | retval = ipc_data_write_finalize(callid, opts, size); |
||
385 | if (retval != EOK) { |
||
386 | ipc_answer_0(rid, retval); |
||
387 | free(mp); |
||
388 | free(opts); |
||
389 | return; |
||
390 | } |
||
391 | opts[size] = '\0'; |
||
392 | |||
4010 | decky | 393 | /* |
394 | * Now, we expect the client to send us data with the name of the file |
||
395 | * system. |
||
396 | */ |
||
397 | if (!ipc_data_write_receive(&callid, &size)) { |
||
398 | ipc_answer_0(callid, EINVAL); |
||
399 | ipc_answer_0(rid, EINVAL); |
||
400 | free(mp); |
||
4305 | jermar | 401 | free(opts); |
4010 | decky | 402 | return; |
403 | } |
||
404 | |||
405 | /* |
||
406 | * Don't receive more than is necessary for storing a full file system |
||
407 | * name. |
||
408 | */ |
||
409 | if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
||
410 | ipc_answer_0(callid, EINVAL); |
||
411 | ipc_answer_0(rid, EINVAL); |
||
412 | free(mp); |
||
4305 | jermar | 413 | free(opts); |
4010 | decky | 414 | return; |
415 | } |
||
416 | |||
417 | /* |
||
418 | * Allocate buffer for file system name. |
||
419 | */ |
||
420 | char *fs_name = (char *) malloc(size + 1); |
||
421 | if (fs_name == NULL) { |
||
422 | ipc_answer_0(callid, ENOMEM); |
||
4301 | jermar | 423 | ipc_answer_0(rid, ENOMEM); |
4010 | decky | 424 | free(mp); |
4305 | jermar | 425 | free(opts); |
4010 | decky | 426 | return; |
427 | } |
||
428 | |||
429 | /* Deliver the file system name. */ |
||
430 | retval = ipc_data_write_finalize(callid, fs_name, size); |
||
431 | if (retval != EOK) { |
||
4301 | jermar | 432 | ipc_answer_0(rid, retval); |
4010 | decky | 433 | free(mp); |
4305 | jermar | 434 | free(opts); |
4010 | decky | 435 | free(fs_name); |
436 | return; |
||
437 | } |
||
438 | fs_name[size] = '\0'; |
||
4302 | jermar | 439 | |
4010 | decky | 440 | /* |
4302 | jermar | 441 | * Wait for IPC_M_PING so that we can return an error if we don't know |
442 | * fs_name. |
||
443 | */ |
||
444 | ipc_call_t data; |
||
445 | callid = async_get_call(&data); |
||
446 | if (IPC_GET_METHOD(data) != IPC_M_PING) { |
||
447 | ipc_answer_0(callid, ENOTSUP); |
||
448 | ipc_answer_0(rid, ENOTSUP); |
||
449 | free(mp); |
||
4305 | jermar | 450 | free(opts); |
4302 | jermar | 451 | free(fs_name); |
452 | return; |
||
453 | } |
||
454 | |||
455 | /* |
||
4010 | decky | 456 | * Check if we know a file system with the same name as is in fs_name. |
457 | * This will also give us its file system handle. |
||
458 | */ |
||
4539 | jermar | 459 | fibril_mutex_lock(&fs_head_lock); |
460 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, false); |
||
4010 | decky | 461 | if (!fs_handle) { |
462 | if (flags & IPC_FLAG_BLOCKING) { |
||
4302 | jermar | 463 | pending_req_t *pr; |
464 | |||
4010 | decky | 465 | /* Blocking mount, add to pending list */ |
4302 | jermar | 466 | pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
4010 | decky | 467 | if (!pr) { |
4539 | jermar | 468 | fibril_mutex_unlock(&fs_head_lock); |
4010 | decky | 469 | ipc_answer_0(callid, ENOMEM); |
470 | ipc_answer_0(rid, ENOMEM); |
||
471 | free(mp); |
||
472 | free(fs_name); |
||
4305 | jermar | 473 | free(opts); |
4010 | decky | 474 | return; |
475 | } |
||
476 | |||
477 | pr->fs_name = fs_name; |
||
478 | pr->mp = mp; |
||
4305 | jermar | 479 | pr->opts = opts; |
4010 | decky | 480 | pr->callid = callid; |
481 | pr->rid = rid; |
||
482 | pr->dev_handle = dev_handle; |
||
4159 | jermar | 483 | link_initialize(&pr->link); |
4010 | decky | 484 | list_append(&pr->link, &pending_req); |
4539 | jermar | 485 | fibril_mutex_unlock(&fs_head_lock); |
4010 | decky | 486 | return; |
487 | } |
||
488 | |||
4539 | jermar | 489 | fibril_mutex_unlock(&fs_head_lock); |
4010 | decky | 490 | ipc_answer_0(callid, ENOENT); |
491 | ipc_answer_0(rid, ENOENT); |
||
492 | free(mp); |
||
493 | free(fs_name); |
||
4305 | jermar | 494 | free(opts); |
4010 | decky | 495 | return; |
496 | } |
||
4539 | jermar | 497 | fibril_mutex_unlock(&fs_head_lock); |
4010 | decky | 498 | |
499 | /* Acknowledge that we know fs_name. */ |
||
500 | ipc_answer_0(callid, EOK); |
||
501 | |||
502 | /* Do the mount */ |
||
4305 | jermar | 503 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts); |
4010 | decky | 504 | free(mp); |
505 | free(fs_name); |
||
4305 | jermar | 506 | free(opts); |
4010 | decky | 507 | } |
508 | |||
2689 | jermar | 509 | void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
510 | { |
||
511 | if (!vfs_files_init()) { |
||
512 | ipc_answer_0(rid, ENOMEM); |
||
513 | return; |
||
514 | } |
||
4463 | decky | 515 | |
2689 | jermar | 516 | /* |
2700 | jermar | 517 | * The POSIX interface is open(path, oflag, mode). |
518 | * We can receive oflags and mode along with the VFS_OPEN call; the path |
||
2689 | jermar | 519 | * will need to arrive in another call. |
2700 | jermar | 520 | * |
521 | * We also receive one private, non-POSIX set of flags called lflag |
||
522 | * used to pass information to vfs_lookup_internal(). |
||
2689 | jermar | 523 | */ |
2700 | jermar | 524 | int lflag = IPC_GET_ARG1(*request); |
525 | int oflag = IPC_GET_ARG2(*request); |
||
526 | int mode = IPC_GET_ARG3(*request); |
||
2689 | jermar | 527 | size_t len; |
4463 | decky | 528 | |
3653 | jermar | 529 | /* |
530 | * Make sure that we are called with exactly one of L_FILE and |
||
4463 | decky | 531 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
3653 | jermar | 532 | */ |
4518 | jermar | 533 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
534 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
||
535 | ((lflag & L_OPEN) != 0)) { |
||
3653 | jermar | 536 | ipc_answer_0(rid, EINVAL); |
537 | return; |
||
538 | } |
||
4463 | decky | 539 | |
2708 | jermar | 540 | if (oflag & O_CREAT) |
541 | lflag |= L_CREATE; |
||
542 | if (oflag & O_EXCL) |
||
543 | lflag |= L_EXCLUSIVE; |
||
4463 | decky | 544 | |
2689 | jermar | 545 | ipc_callid_t callid; |
546 | if (!ipc_data_write_receive(&callid, &len)) { |
||
547 | ipc_answer_0(callid, EINVAL); |
||
548 | ipc_answer_0(rid, EINVAL); |
||
549 | return; |
||
550 | } |
||
4463 | decky | 551 | |
2752 | jermar | 552 | char *path = malloc(len + 1); |
2689 | jermar | 553 | if (!path) { |
554 | ipc_answer_0(callid, ENOMEM); |
||
555 | ipc_answer_0(rid, ENOMEM); |
||
556 | return; |
||
557 | } |
||
4463 | decky | 558 | |
2689 | jermar | 559 | int rc; |
560 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
||
561 | ipc_answer_0(rid, rc); |
||
562 | free(path); |
||
563 | return; |
||
564 | } |
||
2752 | jermar | 565 | path[len] = '\0'; |
2689 | jermar | 566 | |
567 | /* |
||
568 | * Avoid the race condition in which the file can be deleted before we |
||
569 | * find/create-and-lock the VFS node corresponding to the looked-up |
||
570 | * triplet. |
||
571 | */ |
||
2708 | jermar | 572 | if (lflag & L_CREATE) |
4518 | jermar | 573 | fibril_rwlock_write_lock(&namespace_rwlock); |
2708 | jermar | 574 | else |
4518 | jermar | 575 | fibril_rwlock_read_lock(&namespace_rwlock); |
4463 | decky | 576 | |
2707 | jermar | 577 | /* The path is now populated and we can call vfs_lookup_internal(). */ |
2691 | jermar | 578 | vfs_lookup_res_t lr; |
4463 | decky | 579 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
580 | if (rc != EOK) { |
||
2708 | jermar | 581 | if (lflag & L_CREATE) |
4518 | jermar | 582 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2708 | jermar | 583 | else |
4518 | jermar | 584 | fibril_rwlock_read_unlock(&namespace_rwlock); |
2689 | jermar | 585 | ipc_answer_0(rid, rc); |
586 | free(path); |
||
587 | return; |
||
588 | } |
||
4463 | decky | 589 | |
2748 | jermar | 590 | /* Path is no longer needed. */ |
2689 | jermar | 591 | free(path); |
4463 | decky | 592 | |
2691 | jermar | 593 | vfs_node_t *node = vfs_node_get(&lr); |
2708 | jermar | 594 | if (lflag & L_CREATE) |
4518 | jermar | 595 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2708 | jermar | 596 | else |
4518 | jermar | 597 | fibril_rwlock_read_unlock(&namespace_rwlock); |
4463 | decky | 598 | |
2748 | jermar | 599 | /* Truncate the file if requested and if necessary. */ |
600 | if (oflag & O_TRUNC) { |
||
4518 | jermar | 601 | fibril_rwlock_write_lock(&node->contents_rwlock); |
2748 | jermar | 602 | if (node->size) { |
603 | rc = vfs_truncate_internal(node->fs_handle, |
||
604 | node->dev_handle, node->index, 0); |
||
605 | if (rc) { |
||
4518 | jermar | 606 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
2748 | jermar | 607 | vfs_node_put(node); |
608 | ipc_answer_0(rid, rc); |
||
609 | return; |
||
610 | } |
||
611 | node->size = 0; |
||
612 | } |
||
4518 | jermar | 613 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
2748 | jermar | 614 | } |
4463 | decky | 615 | |
2689 | jermar | 616 | /* |
617 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
||
618 | * structure. |
||
619 | */ |
||
620 | int fd = vfs_fd_alloc(); |
||
621 | if (fd < 0) { |
||
622 | vfs_node_put(node); |
||
623 | ipc_answer_0(rid, fd); |
||
624 | return; |
||
625 | } |
||
626 | vfs_file_t *file = vfs_file_get(fd); |
||
627 | file->node = node; |
||
4463 | decky | 628 | if (oflag & O_APPEND) |
2709 | jermar | 629 | file->append = true; |
4463 | decky | 630 | |
631 | /* |
||
632 | * The following increase in reference count is for the fact that the |
||
633 | * file is being opened and that a file structure is pointing to it. |
||
634 | * It is necessary so that the file will not disappear when |
||
635 | * vfs_node_put() is called. The reference will be dropped by the |
||
636 | * respective VFS_CLOSE. |
||
637 | */ |
||
638 | vfs_node_addref(node); |
||
639 | vfs_node_put(node); |
||
640 | |||
641 | /* Success! Return the new file descriptor to the client. */ |
||
642 | ipc_answer_1(rid, EOK, fd); |
||
643 | } |
||
2689 | jermar | 644 | |
4463 | decky | 645 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
646 | { |
||
647 | // FIXME: check for sanity of the supplied fs, dev and index |
||
648 | |||
649 | if (!vfs_files_init()) { |
||
650 | ipc_answer_0(rid, ENOMEM); |
||
651 | return; |
||
652 | } |
||
653 | |||
2689 | jermar | 654 | /* |
4463 | decky | 655 | * The interface is open_node(fs, dev, index, oflag). |
656 | */ |
||
657 | vfs_lookup_res_t lr; |
||
658 | |||
659 | lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
||
660 | lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
||
661 | lr.triplet.index = IPC_GET_ARG3(*request); |
||
662 | int oflag = IPC_GET_ARG4(*request); |
||
663 | |||
4518 | jermar | 664 | fibril_rwlock_read_lock(&namespace_rwlock); |
4463 | decky | 665 | |
666 | int rc = vfs_open_node_internal(&lr); |
||
667 | if (rc != EOK) { |
||
4518 | jermar | 668 | fibril_rwlock_read_unlock(&namespace_rwlock); |
4463 | decky | 669 | ipc_answer_0(rid, rc); |
670 | return; |
||
671 | } |
||
672 | |||
673 | vfs_node_t *node = vfs_node_get(&lr); |
||
4518 | jermar | 674 | fibril_rwlock_read_unlock(&namespace_rwlock); |
4463 | decky | 675 | |
676 | /* Truncate the file if requested and if necessary. */ |
||
677 | if (oflag & O_TRUNC) { |
||
4518 | jermar | 678 | fibril_rwlock_write_lock(&node->contents_rwlock); |
4463 | decky | 679 | if (node->size) { |
680 | rc = vfs_truncate_internal(node->fs_handle, |
||
681 | node->dev_handle, node->index, 0); |
||
682 | if (rc) { |
||
4518 | jermar | 683 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
4463 | decky | 684 | vfs_node_put(node); |
685 | ipc_answer_0(rid, rc); |
||
686 | return; |
||
687 | } |
||
688 | node->size = 0; |
||
689 | } |
||
4518 | jermar | 690 | fibril_rwlock_write_unlock(&node->contents_rwlock); |
4463 | decky | 691 | } |
692 | |||
693 | /* |
||
694 | * Get ourselves a file descriptor and the corresponding vfs_file_t |
||
695 | * structure. |
||
696 | */ |
||
697 | int fd = vfs_fd_alloc(); |
||
698 | if (fd < 0) { |
||
699 | vfs_node_put(node); |
||
700 | ipc_answer_0(rid, fd); |
||
701 | return; |
||
702 | } |
||
703 | vfs_file_t *file = vfs_file_get(fd); |
||
704 | file->node = node; |
||
705 | if (oflag & O_APPEND) |
||
706 | file->append = true; |
||
707 | |||
708 | /* |
||
2689 | jermar | 709 | * The following increase in reference count is for the fact that the |
710 | * file is being opened and that a file structure is pointing to it. |
||
711 | * It is necessary so that the file will not disappear when |
||
712 | * vfs_node_put() is called. The reference will be dropped by the |
||
713 | * respective VFS_CLOSE. |
||
714 | */ |
||
715 | vfs_node_addref(node); |
||
716 | vfs_node_put(node); |
||
4463 | decky | 717 | |
2707 | jermar | 718 | /* Success! Return the new file descriptor to the client. */ |
2689 | jermar | 719 | ipc_answer_1(rid, EOK, fd); |
720 | } |
||
721 | |||
4463 | decky | 722 | void vfs_node(ipc_callid_t rid, ipc_call_t *request) |
2734 | jermar | 723 | { |
724 | int fd = IPC_GET_ARG1(*request); |
||
4463 | decky | 725 | |
726 | /* Lookup the file structure corresponding to the file descriptor. */ |
||
727 | vfs_file_t *file = vfs_file_get(fd); |
||
728 | if (!file) { |
||
729 | ipc_answer_0(rid, ENOENT); |
||
730 | return; |
||
731 | } |
||
732 | |||
4518 | jermar | 733 | ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle, |
734 | file->node->index); |
||
4463 | decky | 735 | } |
736 | |||
737 | void vfs_device(ipc_callid_t rid, ipc_call_t *request) |
||
738 | { |
||
739 | int fd = IPC_GET_ARG1(*request); |
||
740 | |||
741 | /* Lookup the file structure corresponding to the file descriptor. */ |
||
742 | vfs_file_t *file = vfs_file_get(fd); |
||
743 | if (!file) { |
||
744 | ipc_answer_0(rid, ENOENT); |
||
745 | return; |
||
746 | } |
||
747 | |||
748 | /* |
||
749 | * Lock the open file structure so that no other thread can manipulate |
||
750 | * the same open file at a time. |
||
751 | */ |
||
4518 | jermar | 752 | fibril_mutex_lock(&file->lock); |
4463 | decky | 753 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
754 | |||
755 | /* Make a VFS_DEVICE request at the destination FS server. */ |
||
756 | aid_t msg; |
||
757 | ipc_call_t answer; |
||
758 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
||
759 | file->node->dev_handle, file->node->index, &answer); |
||
4518 | jermar | 760 | |
4463 | decky | 761 | /* Wait for reply from the FS server. */ |
762 | ipcarg_t rc; |
||
763 | async_wait_for(msg, &rc); |
||
4551 | jermar | 764 | |
765 | vfs_release_phone(fs_phone); |
||
4518 | jermar | 766 | fibril_mutex_unlock(&file->lock); |
4463 | decky | 767 | |
768 | ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer)); |
||
769 | } |
||
770 | |||
771 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
||
772 | { |
||
773 | int fd = IPC_GET_ARG1(*request); |
||
774 | |||
775 | /* Lookup the file structure corresponding to the file descriptor. */ |
||
776 | vfs_file_t *file = vfs_file_get(fd); |
||
777 | if (!file) { |
||
778 | ipc_answer_0(rid, ENOENT); |
||
779 | return; |
||
780 | } |
||
781 | |||
782 | /* |
||
783 | * Lock the open file structure so that no other thread can manipulate |
||
784 | * the same open file at a time. |
||
785 | */ |
||
4518 | jermar | 786 | fibril_mutex_lock(&file->lock); |
4463 | decky | 787 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
788 | |||
789 | /* Make a VFS_SYMC request at the destination FS server. */ |
||
790 | aid_t msg; |
||
791 | ipc_call_t answer; |
||
792 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
||
793 | file->node->dev_handle, file->node->index, &answer); |
||
4518 | jermar | 794 | |
4463 | decky | 795 | /* Wait for reply from the FS server. */ |
796 | ipcarg_t rc; |
||
797 | async_wait_for(msg, &rc); |
||
798 | |||
4551 | jermar | 799 | vfs_release_phone(fs_phone); |
4518 | jermar | 800 | fibril_mutex_unlock(&file->lock); |
4463 | decky | 801 | |
3215 | jermar | 802 | ipc_answer_0(rid, rc); |
2734 | jermar | 803 | } |
804 | |||
4463 | decky | 805 | void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
806 | { |
||
807 | int fd = IPC_GET_ARG1(*request); |
||
808 | |||
809 | /* Lookup the file structure corresponding to the file descriptor. */ |
||
810 | vfs_file_t *file = vfs_file_get(fd); |
||
811 | if (!file) { |
||
812 | ipc_answer_0(rid, ENOENT); |
||
813 | return; |
||
814 | } |
||
815 | |||
816 | /* |
||
817 | * Lock the open file structure so that no other thread can manipulate |
||
818 | * the same open file at a time. |
||
819 | */ |
||
4518 | jermar | 820 | fibril_mutex_lock(&file->lock); |
4463 | decky | 821 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
822 | |||
823 | /* Make a VFS_CLOSE request at the destination FS server. */ |
||
824 | aid_t msg; |
||
825 | ipc_call_t answer; |
||
826 | msg = async_send_2(fs_phone, IPC_GET_METHOD(*request), |
||
827 | file->node->dev_handle, file->node->index, &answer); |
||
4518 | jermar | 828 | |
4463 | decky | 829 | /* Wait for reply from the FS server. */ |
830 | ipcarg_t rc; |
||
831 | async_wait_for(msg, &rc); |
||
4551 | jermar | 832 | |
833 | vfs_release_phone(fs_phone); |
||
4518 | jermar | 834 | fibril_mutex_unlock(&file->lock); |
4463 | decky | 835 | |
836 | int retval = IPC_GET_ARG1(answer); |
||
837 | if (retval != EOK) |
||
838 | ipc_answer_0(rid, retval); |
||
839 | |||
840 | retval = vfs_fd_free(fd); |
||
841 | ipc_answer_0(rid, retval); |
||
842 | } |
||
843 | |||
2689 | jermar | 844 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
845 | { |
||
846 | |||
847 | /* |
||
848 | * The following code strongly depends on the fact that the files data |
||
849 | * structure can be only accessed by a single fibril and all file |
||
850 | * operations are serialized (i.e. the reads and writes cannot |
||
851 | * interleave and a file cannot be closed while it is being read). |
||
852 | * |
||
853 | * Additional synchronization needs to be added once the table of |
||
854 | * open files supports parallel access! |
||
855 | */ |
||
856 | |||
857 | int fd = IPC_GET_ARG1(*request); |
||
3079 | decky | 858 | |
2707 | jermar | 859 | /* Lookup the file structure corresponding to the file descriptor. */ |
2689 | jermar | 860 | vfs_file_t *file = vfs_file_get(fd); |
861 | if (!file) { |
||
862 | ipc_answer_0(rid, ENOENT); |
||
863 | return; |
||
864 | } |
||
3079 | decky | 865 | |
2689 | jermar | 866 | /* |
867 | * Now we need to receive a call with client's |
||
868 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
||
869 | */ |
||
870 | ipc_callid_t callid; |
||
871 | int res; |
||
872 | if (read) |
||
873 | res = ipc_data_read_receive(&callid, NULL); |
||
874 | else |
||
875 | res = ipc_data_write_receive(&callid, NULL); |
||
876 | if (!res) { |
||
877 | ipc_answer_0(callid, EINVAL); |
||
878 | ipc_answer_0(rid, EINVAL); |
||
879 | return; |
||
880 | } |
||
3079 | decky | 881 | |
2689 | jermar | 882 | /* |
883 | * Lock the open file structure so that no other thread can manipulate |
||
884 | * the same open file at a time. |
||
885 | */ |
||
4518 | jermar | 886 | fibril_mutex_lock(&file->lock); |
3653 | jermar | 887 | |
2689 | jermar | 888 | /* |
889 | * Lock the file's node so that no other client can read/write to it at |
||
890 | * the same time. |
||
891 | */ |
||
892 | if (read) |
||
4518 | jermar | 893 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
2689 | jermar | 894 | else |
4518 | jermar | 895 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
3653 | jermar | 896 | |
897 | if (file->node->type == VFS_NODE_DIRECTORY) { |
||
898 | /* |
||
899 | * Make sure that no one is modifying the namespace |
||
900 | * while we are in readdir(). |
||
901 | */ |
||
902 | assert(read); |
||
4518 | jermar | 903 | fibril_rwlock_read_lock(&namespace_rwlock); |
3653 | jermar | 904 | } |
3079 | decky | 905 | |
2689 | jermar | 906 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
907 | |||
2707 | jermar | 908 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */ |
2689 | jermar | 909 | aid_t msg; |
910 | ipc_call_t answer; |
||
2709 | jermar | 911 | if (!read && file->append) |
912 | file->pos = file->node->size; |
||
2689 | jermar | 913 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
914 | file->node->dev_handle, file->node->index, file->pos, &answer); |
||
915 | |||
916 | /* |
||
917 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
||
918 | * destination FS server. The call will be routed as if sent by |
||
919 | * ourselves. Note that call arguments are immutable in this case so we |
||
920 | * don't have to bother. |
||
921 | */ |
||
922 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
||
4518 | jermar | 923 | |
2707 | jermar | 924 | /* Wait for reply from the FS server. */ |
2689 | jermar | 925 | ipcarg_t rc; |
926 | async_wait_for(msg, &rc); |
||
4463 | decky | 927 | |
4551 | jermar | 928 | vfs_release_phone(fs_phone); |
929 | |||
2689 | jermar | 930 | size_t bytes = IPC_GET_ARG1(answer); |
3653 | jermar | 931 | |
932 | if (file->node->type == VFS_NODE_DIRECTORY) |
||
4518 | jermar | 933 | fibril_rwlock_read_unlock(&namespace_rwlock); |
3079 | decky | 934 | |
2707 | jermar | 935 | /* Unlock the VFS node. */ |
2689 | jermar | 936 | if (read) |
4518 | jermar | 937 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
2689 | jermar | 938 | else { |
939 | /* Update the cached version of node's size. */ |
||
2710 | jermar | 940 | if (rc == EOK) |
941 | file->node->size = IPC_GET_ARG2(answer); |
||
4518 | jermar | 942 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
2689 | jermar | 943 | } |
3079 | decky | 944 | |
2707 | jermar | 945 | /* Update the position pointer and unlock the open file. */ |
2710 | jermar | 946 | if (rc == EOK) |
947 | file->pos += bytes; |
||
4518 | jermar | 948 | fibril_mutex_unlock(&file->lock); |
3079 | decky | 949 | |
2689 | jermar | 950 | /* |
951 | * FS server's reply is the final result of the whole operation we |
||
952 | * return to the client. |
||
953 | */ |
||
954 | ipc_answer_1(rid, rc, bytes); |
||
955 | } |
||
956 | |||
957 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
||
958 | { |
||
959 | vfs_rdwr(rid, request, true); |
||
960 | } |
||
961 | |||
962 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
||
963 | { |
||
964 | vfs_rdwr(rid, request, false); |
||
965 | } |
||
966 | |||
967 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
||
968 | { |
||
969 | int fd = (int) IPC_GET_ARG1(*request); |
||
970 | off_t off = (off_t) IPC_GET_ARG2(*request); |
||
971 | int whence = (int) IPC_GET_ARG3(*request); |
||
972 | |||
973 | |||
2707 | jermar | 974 | /* Lookup the file structure corresponding to the file descriptor. */ |
2689 | jermar | 975 | vfs_file_t *file = vfs_file_get(fd); |
976 | if (!file) { |
||
977 | ipc_answer_0(rid, ENOENT); |
||
978 | return; |
||
979 | } |
||
980 | |||
981 | off_t newpos; |
||
4518 | jermar | 982 | fibril_mutex_lock(&file->lock); |
2689 | jermar | 983 | if (whence == SEEK_SET) { |
984 | file->pos = off; |
||
4518 | jermar | 985 | fibril_mutex_unlock(&file->lock); |
2689 | jermar | 986 | ipc_answer_1(rid, EOK, off); |
987 | return; |
||
988 | } |
||
989 | if (whence == SEEK_CUR) { |
||
990 | if (file->pos + off < file->pos) { |
||
4518 | jermar | 991 | fibril_mutex_unlock(&file->lock); |
2689 | jermar | 992 | ipc_answer_0(rid, EOVERFLOW); |
993 | return; |
||
994 | } |
||
995 | file->pos += off; |
||
996 | newpos = file->pos; |
||
4518 | jermar | 997 | fibril_mutex_unlock(&file->lock); |
2689 | jermar | 998 | ipc_answer_1(rid, EOK, newpos); |
999 | return; |
||
1000 | } |
||
1001 | if (whence == SEEK_END) { |
||
4518 | jermar | 1002 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
2689 | jermar | 1003 | size_t size = file->node->size; |
4518 | jermar | 1004 | fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
2689 | jermar | 1005 | if (size + off < size) { |
4518 | jermar | 1006 | fibril_mutex_unlock(&file->lock); |
2689 | jermar | 1007 | ipc_answer_0(rid, EOVERFLOW); |
1008 | return; |
||
1009 | } |
||
1010 | newpos = size + off; |
||
4518 | jermar | 1011 | fibril_mutex_unlock(&file->lock); |
2689 | jermar | 1012 | ipc_answer_1(rid, EOK, newpos); |
1013 | return; |
||
1014 | } |
||
4518 | jermar | 1015 | fibril_mutex_unlock(&file->lock); |
2689 | jermar | 1016 | ipc_answer_0(rid, EINVAL); |
1017 | } |
||
1018 | |||
2770 | jermar | 1019 | int |
1020 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle, |
||
1021 | fs_index_t index, size_t size) |
||
2748 | jermar | 1022 | { |
1023 | ipcarg_t rc; |
||
1024 | int fs_phone; |
||
1025 | |||
1026 | fs_phone = vfs_grab_phone(fs_handle); |
||
1027 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
||
1028 | (ipcarg_t)index, (ipcarg_t)size); |
||
1029 | vfs_release_phone(fs_phone); |
||
1030 | return (int)rc; |
||
1031 | } |
||
1032 | |||
2693 | jermar | 1033 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
1034 | { |
||
1035 | int fd = IPC_GET_ARG1(*request); |
||
1036 | size_t size = IPC_GET_ARG2(*request); |
||
2748 | jermar | 1037 | int rc; |
2693 | jermar | 1038 | |
1039 | vfs_file_t *file = vfs_file_get(fd); |
||
1040 | if (!file) { |
||
1041 | ipc_answer_0(rid, ENOENT); |
||
1042 | return; |
||
1043 | } |
||
4518 | jermar | 1044 | fibril_mutex_lock(&file->lock); |
2693 | jermar | 1045 | |
4518 | jermar | 1046 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
2748 | jermar | 1047 | rc = vfs_truncate_internal(file->node->fs_handle, |
1048 | file->node->dev_handle, file->node->index, size); |
||
2693 | jermar | 1049 | if (rc == EOK) |
1050 | file->node->size = size; |
||
4518 | jermar | 1051 | fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
2693 | jermar | 1052 | |
4518 | jermar | 1053 | fibril_mutex_unlock(&file->lock); |
2748 | jermar | 1054 | ipc_answer_0(rid, (ipcarg_t)rc); |
2693 | jermar | 1055 | } |
1056 | |||
2707 | jermar | 1057 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
1058 | { |
||
1059 | int mode = IPC_GET_ARG1(*request); |
||
2735 | jermar | 1060 | |
2707 | jermar | 1061 | size_t len; |
1062 | ipc_callid_t callid; |
||
1063 | |||
1064 | if (!ipc_data_write_receive(&callid, &len)) { |
||
1065 | ipc_answer_0(callid, EINVAL); |
||
1066 | ipc_answer_0(rid, EINVAL); |
||
1067 | return; |
||
1068 | } |
||
2752 | jermar | 1069 | char *path = malloc(len + 1); |
2707 | jermar | 1070 | if (!path) { |
1071 | ipc_answer_0(callid, ENOMEM); |
||
1072 | ipc_answer_0(rid, ENOMEM); |
||
1073 | return; |
||
1074 | } |
||
1075 | int rc; |
||
1076 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
||
1077 | ipc_answer_0(rid, rc); |
||
1078 | free(path); |
||
1079 | return; |
||
1080 | } |
||
2752 | jermar | 1081 | path[len] = '\0'; |
2707 | jermar | 1082 | |
4518 | jermar | 1083 | fibril_rwlock_write_lock(&namespace_rwlock); |
2707 | jermar | 1084 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
2752 | jermar | 1085 | rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
4518 | jermar | 1086 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2707 | jermar | 1087 | free(path); |
1088 | ipc_answer_0(rid, rc); |
||
1089 | } |
||
1090 | |||
2735 | jermar | 1091 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request) |
1092 | { |
||
1093 | int lflag = IPC_GET_ARG1(*request); |
||
1094 | |||
1095 | size_t len; |
||
1096 | ipc_callid_t callid; |
||
1097 | |||
1098 | if (!ipc_data_write_receive(&callid, &len)) { |
||
1099 | ipc_answer_0(callid, EINVAL); |
||
1100 | ipc_answer_0(rid, EINVAL); |
||
1101 | return; |
||
1102 | } |
||
2752 | jermar | 1103 | char *path = malloc(len + 1); |
2735 | jermar | 1104 | if (!path) { |
1105 | ipc_answer_0(callid, ENOMEM); |
||
1106 | ipc_answer_0(rid, ENOMEM); |
||
1107 | return; |
||
1108 | } |
||
1109 | int rc; |
||
1110 | if ((rc = ipc_data_write_finalize(callid, path, len))) { |
||
1111 | ipc_answer_0(rid, rc); |
||
1112 | free(path); |
||
1113 | return; |
||
1114 | } |
||
2752 | jermar | 1115 | path[len] = '\0'; |
2735 | jermar | 1116 | |
4518 | jermar | 1117 | fibril_rwlock_write_lock(&namespace_rwlock); |
2735 | jermar | 1118 | lflag &= L_DIRECTORY; /* sanitize lflag */ |
1119 | vfs_lookup_res_t lr; |
||
2763 | jermar | 1120 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL); |
2735 | jermar | 1121 | free(path); |
1122 | if (rc != EOK) { |
||
4518 | jermar | 1123 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2735 | jermar | 1124 | ipc_answer_0(rid, rc); |
1125 | return; |
||
1126 | } |
||
1127 | |||
1128 | /* |
||
1129 | * The name has already been unlinked by vfs_lookup_internal(). |
||
1130 | * We have to get and put the VFS node to ensure that it is |
||
2742 | jermar | 1131 | * VFS_DESTROY'ed after the last reference to it is dropped. |
2735 | jermar | 1132 | */ |
1133 | vfs_node_t *node = vfs_node_get(&lr); |
||
2766 | jermar | 1134 | futex_down(&nodes_futex); |
2735 | jermar | 1135 | node->lnkcnt--; |
2766 | jermar | 1136 | futex_up(&nodes_futex); |
4518 | jermar | 1137 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2735 | jermar | 1138 | vfs_node_put(node); |
1139 | ipc_answer_0(rid, EOK); |
||
1140 | } |
||
1141 | |||
2763 | jermar | 1142 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
1143 | { |
||
4279 | svoboda | 1144 | size_t olen, nlen; |
2763 | jermar | 1145 | ipc_callid_t callid; |
1146 | int rc; |
||
1147 | |||
1148 | /* Retrieve the old path. */ |
||
4279 | svoboda | 1149 | if (!ipc_data_write_receive(&callid, &olen)) { |
2763 | jermar | 1150 | ipc_answer_0(callid, EINVAL); |
1151 | ipc_answer_0(rid, EINVAL); |
||
1152 | return; |
||
1153 | } |
||
4279 | svoboda | 1154 | char *old = malloc(olen + 1); |
2763 | jermar | 1155 | if (!old) { |
1156 | ipc_answer_0(callid, ENOMEM); |
||
1157 | ipc_answer_0(rid, ENOMEM); |
||
1158 | return; |
||
1159 | } |
||
4279 | svoboda | 1160 | if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
2763 | jermar | 1161 | ipc_answer_0(rid, rc); |
1162 | free(old); |
||
1163 | return; |
||
1164 | } |
||
4279 | svoboda | 1165 | old[olen] = '\0'; |
2763 | jermar | 1166 | |
1167 | /* Retrieve the new path. */ |
||
4279 | svoboda | 1168 | if (!ipc_data_write_receive(&callid, &nlen)) { |
2763 | jermar | 1169 | ipc_answer_0(callid, EINVAL); |
1170 | ipc_answer_0(rid, EINVAL); |
||
1171 | free(old); |
||
1172 | return; |
||
1173 | } |
||
4279 | svoboda | 1174 | char *new = malloc(nlen + 1); |
2763 | jermar | 1175 | if (!new) { |
1176 | ipc_answer_0(callid, ENOMEM); |
||
1177 | ipc_answer_0(rid, ENOMEM); |
||
1178 | free(old); |
||
1179 | return; |
||
1180 | } |
||
4279 | svoboda | 1181 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
2763 | jermar | 1182 | ipc_answer_0(rid, rc); |
1183 | free(old); |
||
1184 | free(new); |
||
1185 | return; |
||
1186 | } |
||
4279 | svoboda | 1187 | new[nlen] = '\0'; |
2763 | jermar | 1188 | |
4279 | svoboda | 1189 | char *oldc = canonify(old, &olen); |
1190 | char *newc = canonify(new, &nlen); |
||
2763 | jermar | 1191 | if (!oldc || !newc) { |
1192 | ipc_answer_0(rid, EINVAL); |
||
1193 | free(old); |
||
1194 | free(new); |
||
1195 | return; |
||
1196 | } |
||
4279 | svoboda | 1197 | oldc[olen] = '\0'; |
1198 | newc[nlen] = '\0'; |
||
4366 | jermar | 1199 | if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
1200 | ((newc[str_length(oldc)] == '/') || |
||
1201 | (str_length(oldc) == 1) || |
||
1202 | (str_length(oldc) == str_length(newc)))) { |
||
1203 | /* |
||
1204 | * oldc is a prefix of newc and either |
||
1205 | * - newc continues with a / where oldc ends, or |
||
1206 | * - oldc was / itself, or |
||
1207 | * - oldc and newc are equal. |
||
1208 | */ |
||
2763 | jermar | 1209 | ipc_answer_0(rid, EINVAL); |
1210 | free(old); |
||
1211 | free(new); |
||
1212 | return; |
||
1213 | } |
||
1214 | |||
1215 | vfs_lookup_res_t old_lr; |
||
1216 | vfs_lookup_res_t new_lr; |
||
1217 | vfs_lookup_res_t new_par_lr; |
||
4518 | jermar | 1218 | fibril_rwlock_write_lock(&namespace_rwlock); |
2763 | jermar | 1219 | /* Lookup the node belonging to the old file name. */ |
1220 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL); |
||
1221 | if (rc != EOK) { |
||
4518 | jermar | 1222 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1223 | ipc_answer_0(rid, rc); |
1224 | free(old); |
||
1225 | free(new); |
||
1226 | return; |
||
1227 | } |
||
1228 | vfs_node_t *old_node = vfs_node_get(&old_lr); |
||
1229 | if (!old_node) { |
||
4518 | jermar | 1230 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1231 | ipc_answer_0(rid, ENOMEM); |
1232 | free(old); |
||
1233 | free(new); |
||
1234 | return; |
||
1235 | } |
||
4368 | jermar | 1236 | /* Determine the path to the parent of the node with the new name. */ |
1237 | char *parentc = str_dup(newc); |
||
1238 | if (!parentc) { |
||
4518 | jermar | 1239 | fibril_rwlock_write_unlock(&namespace_rwlock); |
4368 | jermar | 1240 | ipc_answer_0(rid, rc); |
1241 | free(old); |
||
1242 | free(new); |
||
1243 | return; |
||
1244 | } |
||
4427 | jermar | 1245 | char *lastsl = str_rchr(parentc + 1, '/'); |
4368 | jermar | 1246 | if (lastsl) |
1247 | *lastsl = '\0'; |
||
1248 | else |
||
1249 | parentc[1] = '\0'; |
||
2763 | jermar | 1250 | /* Lookup parent of the new file name. */ |
4368 | jermar | 1251 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
1252 | free(parentc); /* not needed anymore */ |
||
2763 | jermar | 1253 | if (rc != EOK) { |
4518 | jermar | 1254 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1255 | ipc_answer_0(rid, rc); |
1256 | free(old); |
||
1257 | free(new); |
||
1258 | return; |
||
1259 | } |
||
1260 | /* Check whether linking to the same file system instance. */ |
||
1261 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) || |
||
1262 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) { |
||
4518 | jermar | 1263 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1264 | ipc_answer_0(rid, EXDEV); /* different file systems */ |
1265 | free(old); |
||
1266 | free(new); |
||
1267 | return; |
||
1268 | } |
||
1269 | /* Destroy the old link for the new name. */ |
||
1270 | vfs_node_t *new_node = NULL; |
||
1271 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL); |
||
1272 | switch (rc) { |
||
1273 | case ENOENT: |
||
1274 | /* simply not in our way */ |
||
1275 | break; |
||
1276 | case EOK: |
||
1277 | new_node = vfs_node_get(&new_lr); |
||
1278 | if (!new_node) { |
||
4518 | jermar | 1279 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1280 | ipc_answer_0(rid, ENOMEM); |
1281 | free(old); |
||
1282 | free(new); |
||
1283 | return; |
||
1284 | } |
||
2766 | jermar | 1285 | futex_down(&nodes_futex); |
2763 | jermar | 1286 | new_node->lnkcnt--; |
2766 | jermar | 1287 | futex_up(&nodes_futex); |
2763 | jermar | 1288 | break; |
1289 | default: |
||
4518 | jermar | 1290 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1291 | ipc_answer_0(rid, ENOTEMPTY); |
1292 | free(old); |
||
1293 | free(new); |
||
1294 | return; |
||
1295 | } |
||
1296 | /* Create the new link for the new name. */ |
||
1297 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index); |
||
1298 | if (rc != EOK) { |
||
4518 | jermar | 1299 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1300 | if (new_node) |
1301 | vfs_node_put(new_node); |
||
1302 | ipc_answer_0(rid, rc); |
||
1303 | free(old); |
||
1304 | free(new); |
||
1305 | return; |
||
1306 | } |
||
2766 | jermar | 1307 | futex_down(&nodes_futex); |
2763 | jermar | 1308 | old_node->lnkcnt++; |
2766 | jermar | 1309 | futex_up(&nodes_futex); |
2763 | jermar | 1310 | /* Destroy the link for the old name. */ |
1311 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
||
1312 | if (rc != EOK) { |
||
4518 | jermar | 1313 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1314 | vfs_node_put(old_node); |
1315 | if (new_node) |
||
1316 | vfs_node_put(new_node); |
||
1317 | ipc_answer_0(rid, rc); |
||
1318 | free(old); |
||
1319 | free(new); |
||
1320 | return; |
||
1321 | } |
||
2766 | jermar | 1322 | futex_down(&nodes_futex); |
2763 | jermar | 1323 | old_node->lnkcnt--; |
2766 | jermar | 1324 | futex_up(&nodes_futex); |
4518 | jermar | 1325 | fibril_rwlock_write_unlock(&namespace_rwlock); |
2763 | jermar | 1326 | vfs_node_put(old_node); |
1327 | if (new_node) |
||
1328 | vfs_node_put(new_node); |
||
1329 | free(old); |
||
1330 | free(new); |
||
1331 | ipc_answer_0(rid, EOK); |
||
1332 | } |
||
1333 | |||
2689 | jermar | 1334 | /** |
1335 | * @} |
||
4463 | decky | 1336 | */ |