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 vfs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file vfs_ops.c
|
---|
35 | * @brief Operations that VFS offers to its clients.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "vfs.h"
|
---|
39 | #include <macros.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <stdbool.h>
|
---|
47 | #include <fibril_synch.h>
|
---|
48 | #include <adt/list.h>
|
---|
49 | #include <ctype.h>
|
---|
50 | #include <assert.h>
|
---|
51 | #include <vfs/canonify.h>
|
---|
52 |
|
---|
53 | /* Forward declarations of static functions. */
|
---|
54 | static errno_t vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
|
---|
55 | aoff64_t);
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
59 | * concurrent VFS operation which modifies the file system namespace.
|
---|
60 | */
|
---|
61 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
62 |
|
---|
63 | static size_t shared_path(char *a, char *b)
|
---|
64 | {
|
---|
65 | size_t res = 0;
|
---|
66 |
|
---|
67 | while (a[res] == b[res] && a[res] != 0)
|
---|
68 | res++;
|
---|
69 |
|
---|
70 | if (a[res] == b[res])
|
---|
71 | return res;
|
---|
72 |
|
---|
73 | res--;
|
---|
74 | while (a[res] != '/')
|
---|
75 | res--;
|
---|
76 | return res;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* This call destroys the file if and only if there are no hard links left. */
|
---|
80 | static void out_destroy(vfs_triplet_t *file)
|
---|
81 | {
|
---|
82 | async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
|
---|
83 | async_msg_2(exch, VFS_OUT_DESTROY, (sysarg_t) file->service_id,
|
---|
84 | (sysarg_t) file->index);
|
---|
85 | vfs_exchange_release(exch);
|
---|
86 | }
|
---|
87 |
|
---|
88 | errno_t vfs_op_clone(int oldfd, int newfd, bool desc, int *out_fd)
|
---|
89 | {
|
---|
90 | errno_t rc;
|
---|
91 |
|
---|
92 | /* Lookup the file structure corresponding to fd. */
|
---|
93 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
94 | if (oldfile == NULL)
|
---|
95 | return EBADF;
|
---|
96 |
|
---|
97 | assert(oldfile->node != NULL);
|
---|
98 |
|
---|
99 | /* If the file descriptors are the same, do nothing. */
|
---|
100 | if (oldfd == newfd) {
|
---|
101 | vfs_file_put(oldfile);
|
---|
102 | return EOK;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (newfd != -1) {
|
---|
106 | /* Assign the old file to newfd. */
|
---|
107 | rc = vfs_fd_assign(oldfile, newfd);
|
---|
108 | *out_fd = newfd;
|
---|
109 | } else {
|
---|
110 | vfs_file_t *newfile;
|
---|
111 | rc = vfs_fd_alloc(&newfile, desc, out_fd);
|
---|
112 | if (rc == EOK) {
|
---|
113 | newfile->node = oldfile->node;
|
---|
114 | newfile->permissions = oldfile->permissions;
|
---|
115 | vfs_node_addref(newfile->node);
|
---|
116 |
|
---|
117 | vfs_file_put(newfile);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | vfs_file_put(oldfile);
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | errno_t vfs_op_put(int fd)
|
---|
126 | {
|
---|
127 | return vfs_fd_free(fd);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static errno_t vfs_connect_internal(service_id_t service_id, unsigned flags,
|
---|
131 | unsigned instance, const char *options, const char *fsname,
|
---|
132 | vfs_node_t **root)
|
---|
133 | {
|
---|
134 | fs_handle_t fs_handle = 0;
|
---|
135 |
|
---|
136 | fibril_mutex_lock(&fs_list_lock);
|
---|
137 | while (true) {
|
---|
138 | fs_handle = fs_name_to_handle(instance, fsname, false);
|
---|
139 |
|
---|
140 | if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING))
|
---|
141 | break;
|
---|
142 |
|
---|
143 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
---|
144 | }
|
---|
145 | fibril_mutex_unlock(&fs_list_lock);
|
---|
146 |
|
---|
147 | if (fs_handle == 0)
|
---|
148 | return ENOFS;
|
---|
149 |
|
---|
150 | /* Tell the mountee that it is being mounted. */
|
---|
151 | ipc_call_t answer;
|
---|
152 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
153 | aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
|
---|
154 | &answer);
|
---|
155 | /* Send the mount options */
|
---|
156 | errno_t rc = async_data_write_start(exch, options, str_size(options));
|
---|
157 | if (rc != EOK) {
|
---|
158 | async_forget(msg);
|
---|
159 | vfs_exchange_release(exch);
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 | async_wait_for(msg, &rc);
|
---|
164 | if (rc != EOK) {
|
---|
165 | vfs_exchange_release(exch);
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | vfs_lookup_res_t res;
|
---|
170 | res.triplet.fs_handle = fs_handle;
|
---|
171 | res.triplet.service_id = service_id;
|
---|
172 | res.triplet.index = (fs_index_t) ipc_get_arg1(&answer);
|
---|
173 | res.size = (int64_t) MERGE_LOUP32(ipc_get_arg2(&answer),
|
---|
174 | ipc_get_arg3(&answer));
|
---|
175 | res.type = VFS_NODE_DIRECTORY;
|
---|
176 |
|
---|
177 | /* Add reference to the mounted root. */
|
---|
178 | *root = vfs_node_get(&res);
|
---|
179 | if (!*root) {
|
---|
180 | aid_t msg = async_send_1(exch, VFS_OUT_UNMOUNTED,
|
---|
181 | (sysarg_t) service_id, NULL);
|
---|
182 | async_forget(msg);
|
---|
183 | vfs_exchange_release(exch);
|
---|
184 | return ENOMEM;
|
---|
185 | }
|
---|
186 |
|
---|
187 | vfs_exchange_release(exch);
|
---|
188 |
|
---|
189 | return EOK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | errno_t vfs_op_fsprobe(const char *fs_name, service_id_t sid,
|
---|
193 | vfs_fs_probe_info_t *info)
|
---|
194 | {
|
---|
195 | fs_handle_t fs_handle = 0;
|
---|
196 | errno_t rc;
|
---|
197 | errno_t retval;
|
---|
198 |
|
---|
199 | fibril_mutex_lock(&fs_list_lock);
|
---|
200 | fs_handle = fs_name_to_handle(0, fs_name, false);
|
---|
201 | fibril_mutex_unlock(&fs_list_lock);
|
---|
202 |
|
---|
203 | if (fs_handle == 0)
|
---|
204 | return ENOFS;
|
---|
205 |
|
---|
206 | /* Send probe request to the file system server */
|
---|
207 | ipc_call_t answer;
|
---|
208 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
209 | aid_t msg = async_send_1(exch, VFS_OUT_FSPROBE, (sysarg_t) sid,
|
---|
210 | &answer);
|
---|
211 | if (msg == 0)
|
---|
212 | return EINVAL;
|
---|
213 |
|
---|
214 | /* Read probe information */
|
---|
215 | retval = async_data_read_start(exch, info, sizeof(*info));
|
---|
216 | if (retval != EOK) {
|
---|
217 | async_forget(msg);
|
---|
218 | return retval;
|
---|
219 | }
|
---|
220 |
|
---|
221 | async_wait_for(msg, &rc);
|
---|
222 | vfs_exchange_release(exch);
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 | errno_t vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
|
---|
227 | unsigned instance, const char *opts, const char *fs_name, int *out_fd)
|
---|
228 | {
|
---|
229 | errno_t rc;
|
---|
230 | vfs_file_t *mp = NULL;
|
---|
231 | vfs_file_t *file = NULL;
|
---|
232 | *out_fd = -1;
|
---|
233 |
|
---|
234 | if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
235 | mp = vfs_file_get(mpfd);
|
---|
236 | if (mp == NULL) {
|
---|
237 | rc = EBADF;
|
---|
238 | goto out;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (mp->node->mount != NULL) {
|
---|
242 | rc = EBUSY;
|
---|
243 | goto out;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (mp->node->type != VFS_NODE_DIRECTORY) {
|
---|
247 | rc = ENOTDIR;
|
---|
248 | goto out;
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (vfs_node_has_children(mp->node)) {
|
---|
252 | rc = ENOTEMPTY;
|
---|
253 | goto out;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (!(flags & VFS_MOUNT_NO_REF)) {
|
---|
258 | rc = vfs_fd_alloc(&file, false, out_fd);
|
---|
259 | if (rc != EOK) {
|
---|
260 | goto out;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | vfs_node_t *root = NULL;
|
---|
265 |
|
---|
266 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
267 |
|
---|
268 | rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
|
---|
269 | &root);
|
---|
270 | if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
271 | vfs_node_addref(mp->node);
|
---|
272 | vfs_node_addref(root);
|
---|
273 | mp->node->mount = root;
|
---|
274 | }
|
---|
275 |
|
---|
276 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
277 |
|
---|
278 | if (rc != EOK)
|
---|
279 | goto out;
|
---|
280 |
|
---|
281 | if (flags & VFS_MOUNT_NO_REF) {
|
---|
282 | vfs_node_delref(root);
|
---|
283 | } else {
|
---|
284 | assert(file != NULL);
|
---|
285 |
|
---|
286 | file->node = root;
|
---|
287 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
---|
288 | file->open_read = false;
|
---|
289 | file->open_write = false;
|
---|
290 | }
|
---|
291 |
|
---|
292 | out:
|
---|
293 | if (mp)
|
---|
294 | vfs_file_put(mp);
|
---|
295 | if (file)
|
---|
296 | vfs_file_put(file);
|
---|
297 |
|
---|
298 | if (rc != EOK && *out_fd >= 0) {
|
---|
299 | vfs_fd_free(*out_fd);
|
---|
300 | *out_fd = -1;
|
---|
301 | }
|
---|
302 |
|
---|
303 | return rc;
|
---|
304 | }
|
---|
305 |
|
---|
306 | errno_t vfs_op_open(int fd, int mode)
|
---|
307 | {
|
---|
308 | if (mode == 0)
|
---|
309 | return EINVAL;
|
---|
310 |
|
---|
311 | vfs_file_t *file = vfs_file_get(fd);
|
---|
312 | if (!file)
|
---|
313 | return EBADF;
|
---|
314 |
|
---|
315 | if ((mode & ~file->permissions) != 0) {
|
---|
316 | vfs_file_put(file);
|
---|
317 | return EPERM;
|
---|
318 | }
|
---|
319 |
|
---|
320 | if (file->open_read || file->open_write) {
|
---|
321 | vfs_file_put(file);
|
---|
322 | return EBUSY;
|
---|
323 | }
|
---|
324 |
|
---|
325 | file->open_read = (mode & MODE_READ) != 0;
|
---|
326 | file->open_write = (mode & (MODE_WRITE | MODE_APPEND)) != 0;
|
---|
327 | file->append = (mode & MODE_APPEND) != 0;
|
---|
328 |
|
---|
329 | if (!file->open_read && !file->open_write) {
|
---|
330 | vfs_file_put(file);
|
---|
331 | return EINVAL;
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
---|
335 | file->open_read = file->open_write = false;
|
---|
336 | vfs_file_put(file);
|
---|
337 | return EINVAL;
|
---|
338 | }
|
---|
339 |
|
---|
340 | errno_t rc = vfs_open_node_remote(file->node);
|
---|
341 | if (rc != EOK) {
|
---|
342 | file->open_read = file->open_write = false;
|
---|
343 | vfs_file_put(file);
|
---|
344 | return rc;
|
---|
345 | }
|
---|
346 |
|
---|
347 | vfs_file_put(file);
|
---|
348 | return EOK;
|
---|
349 | }
|
---|
350 |
|
---|
351 | typedef errno_t (*rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t,
|
---|
352 | ipc_call_t *, bool, void *);
|
---|
353 |
|
---|
354 | static errno_t rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
---|
355 | ipc_call_t *answer, bool read, void *data)
|
---|
356 | {
|
---|
357 | size_t *bytes = (size_t *) data;
|
---|
358 | errno_t rc;
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
362 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
363 | * destination FS server. The call will be routed as if sent by
|
---|
364 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
365 | * don't have to bother.
|
---|
366 | */
|
---|
367 |
|
---|
368 | if (read) {
|
---|
369 | rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
|
---|
370 | file->node->service_id, file->node->index,
|
---|
371 | LOWER32(pos), UPPER32(pos), answer);
|
---|
372 | } else {
|
---|
373 | rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
|
---|
374 | file->node->service_id, file->node->index,
|
---|
375 | LOWER32(pos), UPPER32(pos), answer);
|
---|
376 | }
|
---|
377 |
|
---|
378 | *bytes = ipc_get_arg1(answer);
|
---|
379 | return rc;
|
---|
380 | }
|
---|
381 |
|
---|
382 | static errno_t rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
---|
383 | ipc_call_t *answer, bool read, void *data)
|
---|
384 | {
|
---|
385 | rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
|
---|
386 |
|
---|
387 | if (exch == NULL)
|
---|
388 | return ENOENT;
|
---|
389 |
|
---|
390 | aid_t msg = async_send_4(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
|
---|
391 | file->node->service_id, file->node->index, LOWER32(pos),
|
---|
392 | UPPER32(pos), answer);
|
---|
393 | if (msg == 0)
|
---|
394 | return EINVAL;
|
---|
395 |
|
---|
396 | errno_t retval = async_data_read_start(exch, chunk->buffer, chunk->size);
|
---|
397 | if (retval != EOK) {
|
---|
398 | async_forget(msg);
|
---|
399 | return retval;
|
---|
400 | }
|
---|
401 |
|
---|
402 | errno_t rc;
|
---|
403 | async_wait_for(msg, &rc);
|
---|
404 |
|
---|
405 | chunk->size = ipc_get_arg1(answer);
|
---|
406 |
|
---|
407 | return (errno_t) rc;
|
---|
408 | }
|
---|
409 |
|
---|
410 | static errno_t vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb,
|
---|
411 | void *ipc_cb_data)
|
---|
412 | {
|
---|
413 | /*
|
---|
414 | * The following code strongly depends on the fact that the files data
|
---|
415 | * structure can be only accessed by a single fibril and all file
|
---|
416 | * operations are serialized (i.e. the reads and writes cannot
|
---|
417 | * interleave and a file cannot be closed while it is being read).
|
---|
418 | *
|
---|
419 | * Additional synchronization needs to be added once the table of
|
---|
420 | * open files supports parallel access!
|
---|
421 | */
|
---|
422 |
|
---|
423 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
424 | vfs_file_t *file = vfs_file_get(fd);
|
---|
425 | if (!file)
|
---|
426 | return EBADF;
|
---|
427 |
|
---|
428 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
---|
429 | vfs_file_put(file);
|
---|
430 | return EINVAL;
|
---|
431 | }
|
---|
432 |
|
---|
433 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
434 | assert(fs_info);
|
---|
435 |
|
---|
436 | bool rlock = read ||
|
---|
437 | (fs_info->concurrent_read_write && fs_info->write_retains_size);
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * Lock the file's node so that no other client can read/write to it at
|
---|
441 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
442 | * write implementation does not modify the file size.
|
---|
443 | */
|
---|
444 | if (rlock)
|
---|
445 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
446 | else
|
---|
447 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
448 |
|
---|
449 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
450 | /*
|
---|
451 | * Make sure that no one is modifying the namespace
|
---|
452 | * while we are in readdir().
|
---|
453 | */
|
---|
454 |
|
---|
455 | if (!read) {
|
---|
456 | if (rlock) {
|
---|
457 | fibril_rwlock_read_unlock(
|
---|
458 | &file->node->contents_rwlock);
|
---|
459 | } else {
|
---|
460 | fibril_rwlock_write_unlock(
|
---|
461 | &file->node->contents_rwlock);
|
---|
462 | }
|
---|
463 | vfs_file_put(file);
|
---|
464 | return EINVAL;
|
---|
465 | }
|
---|
466 |
|
---|
467 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
468 | }
|
---|
469 |
|
---|
470 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
471 |
|
---|
472 | if (!read && file->append)
|
---|
473 | pos = file->node->size;
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Handle communication with the endpoint FS.
|
---|
477 | */
|
---|
478 | ipc_call_t answer;
|
---|
479 | errno_t rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data);
|
---|
480 |
|
---|
481 | vfs_exchange_release(fs_exch);
|
---|
482 |
|
---|
483 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
484 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
485 |
|
---|
486 | /* Unlock the VFS node. */
|
---|
487 | if (rlock) {
|
---|
488 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
489 | } else {
|
---|
490 | /* Update the cached version of node's size. */
|
---|
491 | if (rc == EOK) {
|
---|
492 | file->node->size = MERGE_LOUP32(ipc_get_arg2(&answer),
|
---|
493 | ipc_get_arg3(&answer));
|
---|
494 | }
|
---|
495 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
496 | }
|
---|
497 |
|
---|
498 | vfs_file_put(file);
|
---|
499 |
|
---|
500 | return rc;
|
---|
501 | }
|
---|
502 |
|
---|
503 | errno_t vfs_rdwr_internal(int fd, aoff64_t pos, bool read, rdwr_io_chunk_t *chunk)
|
---|
504 | {
|
---|
505 | return vfs_rdwr(fd, pos, read, rdwr_ipc_internal, chunk);
|
---|
506 | }
|
---|
507 |
|
---|
508 | errno_t vfs_op_read(int fd, aoff64_t pos, size_t *out_bytes)
|
---|
509 | {
|
---|
510 | return vfs_rdwr(fd, pos, true, rdwr_ipc_client, out_bytes);
|
---|
511 | }
|
---|
512 |
|
---|
513 | errno_t vfs_op_rename(int basefd, char *old, char *new)
|
---|
514 | {
|
---|
515 | vfs_file_t *base_file = vfs_file_get(basefd);
|
---|
516 | if (!base_file)
|
---|
517 | return EBADF;
|
---|
518 |
|
---|
519 | vfs_node_t *base = base_file->node;
|
---|
520 | vfs_node_addref(base);
|
---|
521 | vfs_file_put(base_file);
|
---|
522 |
|
---|
523 | vfs_lookup_res_t base_lr;
|
---|
524 | vfs_lookup_res_t old_lr;
|
---|
525 | vfs_lookup_res_t new_lr_orig;
|
---|
526 | bool orig_unlinked = false;
|
---|
527 |
|
---|
528 | errno_t rc;
|
---|
529 |
|
---|
530 | size_t shared = shared_path(old, new);
|
---|
531 |
|
---|
532 | /* Do not allow one path to be a prefix of the other. */
|
---|
533 | if (old[shared] == 0 || new[shared] == 0) {
|
---|
534 | vfs_node_put(base);
|
---|
535 | return EINVAL;
|
---|
536 | }
|
---|
537 | assert(old[shared] == '/');
|
---|
538 | assert(new[shared] == '/');
|
---|
539 |
|
---|
540 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
541 |
|
---|
542 | /* Resolve the shared portion of the path first. */
|
---|
543 | if (shared != 0) {
|
---|
544 | old[shared] = 0;
|
---|
545 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
---|
546 | if (rc != EOK) {
|
---|
547 | vfs_node_put(base);
|
---|
548 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
549 | return rc;
|
---|
550 | }
|
---|
551 |
|
---|
552 | vfs_node_put(base);
|
---|
553 | base = vfs_node_get(&base_lr);
|
---|
554 | if (!base) {
|
---|
555 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
556 | return ENOMEM;
|
---|
557 | }
|
---|
558 | old[shared] = '/';
|
---|
559 | old += shared;
|
---|
560 | new += shared;
|
---|
561 | }
|
---|
562 |
|
---|
563 | rc = vfs_lookup_internal(base, old, L_DISABLE_MOUNTS, &old_lr);
|
---|
564 | if (rc != EOK) {
|
---|
565 | vfs_node_put(base);
|
---|
566 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
567 | return rc;
|
---|
568 | }
|
---|
569 |
|
---|
570 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
571 | &new_lr_orig);
|
---|
572 | if (rc == EOK) {
|
---|
573 | orig_unlinked = true;
|
---|
574 | } else if (rc != ENOENT) {
|
---|
575 | vfs_node_put(base);
|
---|
576 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
577 | return rc;
|
---|
578 | }
|
---|
579 |
|
---|
580 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
---|
581 | if (rc != EOK) {
|
---|
582 | vfs_link_internal(base, old, &old_lr.triplet);
|
---|
583 | if (orig_unlinked)
|
---|
584 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
585 | vfs_node_put(base);
|
---|
586 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
587 | return rc;
|
---|
588 | }
|
---|
589 |
|
---|
590 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
591 | &old_lr);
|
---|
592 | if (rc != EOK) {
|
---|
593 | if (orig_unlinked)
|
---|
594 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
595 | vfs_node_put(base);
|
---|
596 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
597 | return rc;
|
---|
598 | }
|
---|
599 |
|
---|
600 | /* If the node is not held by anyone, try to destroy it. */
|
---|
601 | if (orig_unlinked) {
|
---|
602 | vfs_node_t *node = vfs_node_peek(&new_lr_orig);
|
---|
603 | if (!node)
|
---|
604 | out_destroy(&new_lr_orig.triplet);
|
---|
605 | else
|
---|
606 | vfs_node_put(node);
|
---|
607 | }
|
---|
608 |
|
---|
609 | vfs_node_put(base);
|
---|
610 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
611 | return EOK;
|
---|
612 | }
|
---|
613 |
|
---|
614 | errno_t vfs_op_resize(int fd, int64_t size)
|
---|
615 | {
|
---|
616 | vfs_file_t *file = vfs_file_get(fd);
|
---|
617 | if (!file)
|
---|
618 | return EBADF;
|
---|
619 |
|
---|
620 | if (!file->open_write || file->node->type != VFS_NODE_FILE) {
|
---|
621 | vfs_file_put(file);
|
---|
622 | return EINVAL;
|
---|
623 | }
|
---|
624 |
|
---|
625 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
626 |
|
---|
627 | errno_t rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
628 | file->node->service_id, file->node->index, size);
|
---|
629 | if (rc == EOK)
|
---|
630 | file->node->size = size;
|
---|
631 |
|
---|
632 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
633 | vfs_file_put(file);
|
---|
634 | return rc;
|
---|
635 | }
|
---|
636 |
|
---|
637 | errno_t vfs_op_stat(int fd)
|
---|
638 | {
|
---|
639 | vfs_file_t *file = vfs_file_get(fd);
|
---|
640 | if (!file)
|
---|
641 | return EBADF;
|
---|
642 |
|
---|
643 | vfs_node_t *node = file->node;
|
---|
644 |
|
---|
645 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
646 | errno_t rc = async_data_read_forward_3_0(exch, VFS_OUT_STAT,
|
---|
647 | node->service_id, node->index, true);
|
---|
648 | vfs_exchange_release(exch);
|
---|
649 |
|
---|
650 | vfs_file_put(file);
|
---|
651 | return rc;
|
---|
652 | }
|
---|
653 |
|
---|
654 | errno_t vfs_op_statfs(int fd)
|
---|
655 | {
|
---|
656 | vfs_file_t *file = vfs_file_get(fd);
|
---|
657 | if (!file)
|
---|
658 | return EBADF;
|
---|
659 |
|
---|
660 | vfs_node_t *node = file->node;
|
---|
661 |
|
---|
662 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
663 | errno_t rc = async_data_read_forward_3_0(exch, VFS_OUT_STATFS,
|
---|
664 | node->service_id, node->index, false);
|
---|
665 | vfs_exchange_release(exch);
|
---|
666 |
|
---|
667 | vfs_file_put(file);
|
---|
668 | return rc;
|
---|
669 | }
|
---|
670 |
|
---|
671 | errno_t vfs_op_sync(int fd)
|
---|
672 | {
|
---|
673 | vfs_file_t *file = vfs_file_get(fd);
|
---|
674 | if (!file)
|
---|
675 | return EBADF;
|
---|
676 |
|
---|
677 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
678 |
|
---|
679 | aid_t msg;
|
---|
680 | ipc_call_t answer;
|
---|
681 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
682 | file->node->index, &answer);
|
---|
683 |
|
---|
684 | vfs_exchange_release(fs_exch);
|
---|
685 |
|
---|
686 | errno_t rc;
|
---|
687 | async_wait_for(msg, &rc);
|
---|
688 |
|
---|
689 | vfs_file_put(file);
|
---|
690 | return rc;
|
---|
691 |
|
---|
692 | }
|
---|
693 |
|
---|
694 | static errno_t vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
695 | fs_index_t index, aoff64_t size)
|
---|
696 | {
|
---|
697 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
698 | errno_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
699 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
700 | UPPER32(size));
|
---|
701 | vfs_exchange_release(exch);
|
---|
702 |
|
---|
703 | return (errno_t) rc;
|
---|
704 | }
|
---|
705 |
|
---|
706 | errno_t vfs_op_unlink(int parentfd, int expectfd, char *path)
|
---|
707 | {
|
---|
708 | errno_t rc = EOK;
|
---|
709 | vfs_file_t *parent = NULL;
|
---|
710 | vfs_file_t *expect = NULL;
|
---|
711 |
|
---|
712 | if (parentfd == expectfd)
|
---|
713 | return EINVAL;
|
---|
714 |
|
---|
715 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
716 |
|
---|
717 | /*
|
---|
718 | * Files are retrieved in order of file descriptors, to prevent
|
---|
719 | * deadlock.
|
---|
720 | */
|
---|
721 | if (parentfd < expectfd) {
|
---|
722 | parent = vfs_file_get(parentfd);
|
---|
723 | if (!parent) {
|
---|
724 | rc = EBADF;
|
---|
725 | goto exit;
|
---|
726 | }
|
---|
727 | }
|
---|
728 |
|
---|
729 | if (expectfd >= 0) {
|
---|
730 | expect = vfs_file_get(expectfd);
|
---|
731 | if (!expect) {
|
---|
732 | rc = EBADF;
|
---|
733 | goto exit;
|
---|
734 | }
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (parentfd > expectfd) {
|
---|
738 | parent = vfs_file_get(parentfd);
|
---|
739 | if (!parent) {
|
---|
740 | rc = EBADF;
|
---|
741 | goto exit;
|
---|
742 | }
|
---|
743 | }
|
---|
744 |
|
---|
745 | assert(parent != NULL);
|
---|
746 |
|
---|
747 | if (expectfd >= 0) {
|
---|
748 | vfs_lookup_res_t lr;
|
---|
749 | rc = vfs_lookup_internal(parent->node, path, 0, &lr);
|
---|
750 | if (rc != EOK)
|
---|
751 | goto exit;
|
---|
752 |
|
---|
753 | vfs_node_t *found_node = vfs_node_peek(&lr);
|
---|
754 | vfs_node_put(found_node);
|
---|
755 | if (expect->node != found_node) {
|
---|
756 | rc = ENOENT;
|
---|
757 | goto exit;
|
---|
758 | }
|
---|
759 |
|
---|
760 | vfs_file_put(expect);
|
---|
761 | expect = NULL;
|
---|
762 | }
|
---|
763 |
|
---|
764 | vfs_lookup_res_t lr;
|
---|
765 | rc = vfs_lookup_internal(parent->node, path, L_UNLINK, &lr);
|
---|
766 | if (rc != EOK)
|
---|
767 | goto exit;
|
---|
768 |
|
---|
769 | /* If the node is not held by anyone, try to destroy it. */
|
---|
770 | vfs_node_t *node = vfs_node_peek(&lr);
|
---|
771 | if (!node)
|
---|
772 | out_destroy(&lr.triplet);
|
---|
773 | else
|
---|
774 | vfs_node_put(node);
|
---|
775 |
|
---|
776 | exit:
|
---|
777 | if (path)
|
---|
778 | free(path);
|
---|
779 | if (parent)
|
---|
780 | vfs_file_put(parent);
|
---|
781 | if (expect)
|
---|
782 | vfs_file_put(expect);
|
---|
783 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
784 | return rc;
|
---|
785 | }
|
---|
786 |
|
---|
787 | errno_t vfs_op_unmount(int mpfd)
|
---|
788 | {
|
---|
789 | vfs_file_t *mp = vfs_file_get(mpfd);
|
---|
790 | if (mp == NULL)
|
---|
791 | return EBADF;
|
---|
792 |
|
---|
793 | if (mp->node->mount == NULL) {
|
---|
794 | vfs_file_put(mp);
|
---|
795 | return ENOENT;
|
---|
796 | }
|
---|
797 |
|
---|
798 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
799 |
|
---|
800 | /*
|
---|
801 | * Count the total number of references for the mounted file system. We
|
---|
802 | * are expecting at least one, which is held by the mount point.
|
---|
803 | * If we find more, it means that
|
---|
804 | * the file system cannot be gracefully unmounted at the moment because
|
---|
805 | * someone is working with it.
|
---|
806 | */
|
---|
807 | if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
|
---|
808 | mp->node->mount->service_id) != 1) {
|
---|
809 | vfs_file_put(mp);
|
---|
810 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
811 | return EBUSY;
|
---|
812 | }
|
---|
813 |
|
---|
814 | async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
|
---|
815 | errno_t rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
816 | mp->node->mount->service_id);
|
---|
817 | vfs_exchange_release(exch);
|
---|
818 |
|
---|
819 | if (rc != EOK) {
|
---|
820 | vfs_file_put(mp);
|
---|
821 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
822 | return rc;
|
---|
823 | }
|
---|
824 |
|
---|
825 | vfs_node_forget(mp->node->mount);
|
---|
826 | vfs_node_put(mp->node);
|
---|
827 | mp->node->mount = NULL;
|
---|
828 |
|
---|
829 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
830 |
|
---|
831 | vfs_file_put(mp);
|
---|
832 | return EOK;
|
---|
833 | }
|
---|
834 |
|
---|
835 | errno_t vfs_op_wait_handle(bool high_fd, int *out_fd)
|
---|
836 | {
|
---|
837 | return vfs_wait_handle_internal(high_fd, out_fd);
|
---|
838 | }
|
---|
839 |
|
---|
840 | static inline bool walk_flags_valid(int flags)
|
---|
841 | {
|
---|
842 | if ((flags & ~WALK_ALL_FLAGS) != 0)
|
---|
843 | return false;
|
---|
844 | if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE))
|
---|
845 | return false;
|
---|
846 | if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY))
|
---|
847 | return false;
|
---|
848 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
|
---|
849 | if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR))
|
---|
850 | return false;
|
---|
851 | }
|
---|
852 | return true;
|
---|
853 | }
|
---|
854 |
|
---|
855 | static inline int walk_lookup_flags(int flags)
|
---|
856 | {
|
---|
857 | int lflags = 0;
|
---|
858 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE))
|
---|
859 | lflags |= L_CREATE;
|
---|
860 | if (flags & WALK_MUST_CREATE)
|
---|
861 | lflags |= L_EXCLUSIVE;
|
---|
862 | if (flags & WALK_REGULAR)
|
---|
863 | lflags |= L_FILE;
|
---|
864 | if (flags & WALK_DIRECTORY)
|
---|
865 | lflags |= L_DIRECTORY;
|
---|
866 | if (flags & WALK_MOUNT_POINT)
|
---|
867 | lflags |= L_MP;
|
---|
868 | return lflags;
|
---|
869 | }
|
---|
870 |
|
---|
871 | errno_t vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
|
---|
872 | {
|
---|
873 | if (!walk_flags_valid(flags))
|
---|
874 | return EINVAL;
|
---|
875 |
|
---|
876 | vfs_file_t *parent = vfs_file_get(parentfd);
|
---|
877 | if (!parent)
|
---|
878 | return EBADF;
|
---|
879 |
|
---|
880 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
881 |
|
---|
882 | vfs_lookup_res_t lr;
|
---|
883 | errno_t rc = vfs_lookup_internal(parent->node, path,
|
---|
884 | walk_lookup_flags(flags), &lr);
|
---|
885 | if (rc != EOK) {
|
---|
886 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
887 | vfs_file_put(parent);
|
---|
888 | return rc;
|
---|
889 | }
|
---|
890 |
|
---|
891 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
892 | if (!node) {
|
---|
893 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
894 | vfs_file_put(parent);
|
---|
895 | return ENOMEM;
|
---|
896 | }
|
---|
897 |
|
---|
898 | vfs_file_t *file;
|
---|
899 | rc = vfs_fd_alloc(&file, false, out_fd);
|
---|
900 | if (rc != EOK) {
|
---|
901 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
902 | vfs_node_put(node);
|
---|
903 | vfs_file_put(parent);
|
---|
904 | return rc;
|
---|
905 | }
|
---|
906 | assert(file != NULL);
|
---|
907 |
|
---|
908 | file->node = node;
|
---|
909 | file->permissions = parent->permissions;
|
---|
910 | file->open_read = false;
|
---|
911 | file->open_write = false;
|
---|
912 |
|
---|
913 | vfs_file_put(file);
|
---|
914 | vfs_file_put(parent);
|
---|
915 |
|
---|
916 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
917 |
|
---|
918 | return EOK;
|
---|
919 | }
|
---|
920 |
|
---|
921 | errno_t vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes)
|
---|
922 | {
|
---|
923 | return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes);
|
---|
924 | }
|
---|
925 |
|
---|
926 | /**
|
---|
927 | * @}
|
---|
928 | */
|
---|