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 | * @{
|
---|
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 int 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 | int vfs_op_clone(int oldfd, int newfd, bool desc)
|
---|
89 | {
|
---|
90 | int rc;
|
---|
91 |
|
---|
92 | /* If the file descriptors are the same, do nothing. */
|
---|
93 | if (oldfd == newfd)
|
---|
94 | return EOK;
|
---|
95 |
|
---|
96 | /* Lookup the file structure corresponding to fd. */
|
---|
97 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
98 | if (oldfile == NULL)
|
---|
99 | return EBADF;
|
---|
100 |
|
---|
101 | assert(oldfile->node != NULL);
|
---|
102 |
|
---|
103 | if (newfd != -1) {
|
---|
104 | /* Make sure newfd is closed. */
|
---|
105 | (void) vfs_fd_free(newfd);
|
---|
106 | /* Assign the old file to newfd. */
|
---|
107 | rc = vfs_fd_assign(oldfile, newfd);
|
---|
108 | } else {
|
---|
109 | vfs_file_t *newfile;
|
---|
110 | int newfd = vfs_fd_alloc(&newfile, desc);
|
---|
111 | if (newfd >= 0) {
|
---|
112 | newfile->node = oldfile->node;
|
---|
113 | newfile->permissions = oldfile->permissions;
|
---|
114 | vfs_node_addref(newfile->node);
|
---|
115 |
|
---|
116 | vfs_file_put(newfile);
|
---|
117 | }
|
---|
118 | rc = newfd;
|
---|
119 | }
|
---|
120 | vfs_file_put(oldfile);
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | int vfs_op_put(int fd)
|
---|
126 | {
|
---|
127 | return vfs_fd_free(fd);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static int 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 | sysarg_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 | int 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 | sysarg_t rc;
|
---|
197 | int 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 | int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
|
---|
227 | unsigned instance, const char *opts, const char *fs_name, int *outfd)
|
---|
228 | {
|
---|
229 | int rc;
|
---|
230 | vfs_file_t *mp = NULL;
|
---|
231 | vfs_file_t *file = NULL;
|
---|
232 | int 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 | fd = vfs_fd_alloc(&file, false);
|
---|
259 | if (fd < 0) {
|
---|
260 | rc = fd;
|
---|
261 | goto out;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | vfs_node_t *root = NULL;
|
---|
266 |
|
---|
267 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
268 |
|
---|
269 | rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
|
---|
270 | &root);
|
---|
271 | if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
272 | vfs_node_addref(mp->node);
|
---|
273 | vfs_node_addref(root);
|
---|
274 | mp->node->mount = root;
|
---|
275 | }
|
---|
276 |
|
---|
277 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
278 |
|
---|
279 | if (rc != EOK)
|
---|
280 | goto out;
|
---|
281 |
|
---|
282 | if (flags & VFS_MOUNT_NO_REF) {
|
---|
283 | vfs_node_delref(root);
|
---|
284 | } else {
|
---|
285 | assert(file != NULL);
|
---|
286 |
|
---|
287 | file->node = root;
|
---|
288 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
---|
289 | file->open_read = false;
|
---|
290 | file->open_write = false;
|
---|
291 | }
|
---|
292 |
|
---|
293 | out:
|
---|
294 | if (mp)
|
---|
295 | vfs_file_put(mp);
|
---|
296 | if (file)
|
---|
297 | vfs_file_put(file);
|
---|
298 |
|
---|
299 | if (rc != EOK && fd >= 0) {
|
---|
300 | vfs_fd_free(fd);
|
---|
301 | fd = 0;
|
---|
302 | }
|
---|
303 |
|
---|
304 | *outfd = fd;
|
---|
305 | return rc;
|
---|
306 | }
|
---|
307 |
|
---|
308 | int vfs_op_open(int fd, int mode)
|
---|
309 | {
|
---|
310 | if (mode == 0)
|
---|
311 | return EINVAL;
|
---|
312 |
|
---|
313 | vfs_file_t *file = vfs_file_get(fd);
|
---|
314 | if (!file)
|
---|
315 | return EBADF;
|
---|
316 |
|
---|
317 | if ((mode & ~file->permissions) != 0) {
|
---|
318 | vfs_file_put(file);
|
---|
319 | return EPERM;
|
---|
320 | }
|
---|
321 |
|
---|
322 | if (file->open_read || file->open_write) {
|
---|
323 | vfs_file_put(file);
|
---|
324 | return EBUSY;
|
---|
325 | }
|
---|
326 |
|
---|
327 | file->open_read = (mode & MODE_READ) != 0;
|
---|
328 | file->open_write = (mode & (MODE_WRITE | MODE_APPEND)) != 0;
|
---|
329 | file->append = (mode & MODE_APPEND) != 0;
|
---|
330 |
|
---|
331 | if (!file->open_read && !file->open_write) {
|
---|
332 | vfs_file_put(file);
|
---|
333 | return EINVAL;
|
---|
334 | }
|
---|
335 |
|
---|
336 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
---|
337 | file->open_read = file->open_write = false;
|
---|
338 | vfs_file_put(file);
|
---|
339 | return EINVAL;
|
---|
340 | }
|
---|
341 |
|
---|
342 | int rc = vfs_open_node_remote(file->node);
|
---|
343 | if (rc != EOK) {
|
---|
344 | file->open_read = file->open_write = false;
|
---|
345 | vfs_file_put(file);
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 |
|
---|
349 | vfs_file_put(file);
|
---|
350 | return EOK;
|
---|
351 | }
|
---|
352 |
|
---|
353 | typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t,
|
---|
354 | ipc_call_t *, bool, void *);
|
---|
355 |
|
---|
356 | static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
---|
357 | ipc_call_t *answer, bool read, void *data)
|
---|
358 | {
|
---|
359 | size_t *bytes = (size_t *) data;
|
---|
360 | int rc;
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
364 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
365 | * destination FS server. The call will be routed as if sent by
|
---|
366 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
367 | * don't have to bother.
|
---|
368 | */
|
---|
369 |
|
---|
370 | if (read) {
|
---|
371 | rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
|
---|
372 | file->node->service_id, file->node->index,
|
---|
373 | LOWER32(pos), UPPER32(pos), answer);
|
---|
374 | } else {
|
---|
375 | rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
|
---|
376 | file->node->service_id, file->node->index,
|
---|
377 | LOWER32(pos), UPPER32(pos), answer);
|
---|
378 | }
|
---|
379 |
|
---|
380 | *bytes = IPC_GET_ARG1(*answer);
|
---|
381 | return rc;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
---|
385 | ipc_call_t *answer, bool read, void *data)
|
---|
386 | {
|
---|
387 | rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
|
---|
388 |
|
---|
389 | if (exch == NULL)
|
---|
390 | return ENOENT;
|
---|
391 |
|
---|
392 | aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
|
---|
393 | file->node->service_id, file->node->index, LOWER32(pos),
|
---|
394 | UPPER32(pos), answer);
|
---|
395 | if (msg == 0)
|
---|
396 | return EINVAL;
|
---|
397 |
|
---|
398 | int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
|
---|
399 | if (retval != EOK) {
|
---|
400 | async_forget(msg);
|
---|
401 | return retval;
|
---|
402 | }
|
---|
403 |
|
---|
404 | sysarg_t rc;
|
---|
405 | async_wait_for(msg, &rc);
|
---|
406 |
|
---|
407 | chunk->size = IPC_GET_ARG1(*answer);
|
---|
408 |
|
---|
409 | return (int) rc;
|
---|
410 | }
|
---|
411 |
|
---|
412 | static int vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb,
|
---|
413 | void *ipc_cb_data)
|
---|
414 | {
|
---|
415 | /*
|
---|
416 | * The following code strongly depends on the fact that the files data
|
---|
417 | * structure can be only accessed by a single fibril and all file
|
---|
418 | * operations are serialized (i.e. the reads and writes cannot
|
---|
419 | * interleave and a file cannot be closed while it is being read).
|
---|
420 | *
|
---|
421 | * Additional synchronization needs to be added once the table of
|
---|
422 | * open files supports parallel access!
|
---|
423 | */
|
---|
424 |
|
---|
425 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
426 | vfs_file_t *file = vfs_file_get(fd);
|
---|
427 | if (!file)
|
---|
428 | return EBADF;
|
---|
429 |
|
---|
430 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
---|
431 | vfs_file_put(file);
|
---|
432 | return EINVAL;
|
---|
433 | }
|
---|
434 |
|
---|
435 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
436 | assert(fs_info);
|
---|
437 |
|
---|
438 | bool rlock = read ||
|
---|
439 | (fs_info->concurrent_read_write && fs_info->write_retains_size);
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Lock the file's node so that no other client can read/write to it at
|
---|
443 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
444 | * write implementation does not modify the file size.
|
---|
445 | */
|
---|
446 | if (rlock)
|
---|
447 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
448 | else
|
---|
449 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
450 |
|
---|
451 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
452 | /*
|
---|
453 | * Make sure that no one is modifying the namespace
|
---|
454 | * while we are in readdir().
|
---|
455 | */
|
---|
456 |
|
---|
457 | if (!read) {
|
---|
458 | if (rlock) {
|
---|
459 | fibril_rwlock_read_unlock(
|
---|
460 | &file->node->contents_rwlock);
|
---|
461 | } else {
|
---|
462 | fibril_rwlock_write_unlock(
|
---|
463 | &file->node->contents_rwlock);
|
---|
464 | }
|
---|
465 | vfs_file_put(file);
|
---|
466 | return EINVAL;
|
---|
467 | }
|
---|
468 |
|
---|
469 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
470 | }
|
---|
471 |
|
---|
472 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
473 |
|
---|
474 | if (!read && file->append)
|
---|
475 | pos = file->node->size;
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * Handle communication with the endpoint FS.
|
---|
479 | */
|
---|
480 | ipc_call_t answer;
|
---|
481 | int rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data);
|
---|
482 |
|
---|
483 | vfs_exchange_release(fs_exch);
|
---|
484 |
|
---|
485 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
486 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
487 |
|
---|
488 | /* Unlock the VFS node. */
|
---|
489 | if (rlock) {
|
---|
490 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
491 | } else {
|
---|
492 | /* Update the cached version of node's size. */
|
---|
493 | if (rc == EOK) {
|
---|
494 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
495 | IPC_GET_ARG3(answer));
|
---|
496 | }
|
---|
497 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
498 | }
|
---|
499 |
|
---|
500 | vfs_file_put(file);
|
---|
501 |
|
---|
502 | return rc;
|
---|
503 | }
|
---|
504 |
|
---|
505 | int vfs_rdwr_internal(int fd, aoff64_t pos, bool read, rdwr_io_chunk_t *chunk)
|
---|
506 | {
|
---|
507 | return vfs_rdwr(fd, pos, read, rdwr_ipc_internal, chunk);
|
---|
508 | }
|
---|
509 |
|
---|
510 | int vfs_op_read(int fd, aoff64_t pos, size_t *out_bytes)
|
---|
511 | {
|
---|
512 | return vfs_rdwr(fd, pos, true, rdwr_ipc_client, out_bytes);
|
---|
513 | }
|
---|
514 |
|
---|
515 | int vfs_op_rename(int basefd, char *old, char *new)
|
---|
516 | {
|
---|
517 | vfs_file_t *base_file = vfs_file_get(basefd);
|
---|
518 | if (!base_file)
|
---|
519 | return EBADF;
|
---|
520 |
|
---|
521 | vfs_node_t *base = base_file->node;
|
---|
522 | vfs_node_addref(base);
|
---|
523 | vfs_file_put(base_file);
|
---|
524 |
|
---|
525 | vfs_lookup_res_t base_lr;
|
---|
526 | vfs_lookup_res_t old_lr;
|
---|
527 | vfs_lookup_res_t new_lr_orig;
|
---|
528 | bool orig_unlinked = false;
|
---|
529 |
|
---|
530 | int rc;
|
---|
531 |
|
---|
532 | size_t shared = shared_path(old, new);
|
---|
533 |
|
---|
534 | /* Do not allow one path to be a prefix of the other. */
|
---|
535 | if (old[shared] == 0 || new[shared] == 0) {
|
---|
536 | vfs_node_put(base);
|
---|
537 | return EINVAL;
|
---|
538 | }
|
---|
539 | assert(old[shared] == '/');
|
---|
540 | assert(new[shared] == '/');
|
---|
541 |
|
---|
542 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
543 |
|
---|
544 | /* Resolve the shared portion of the path first. */
|
---|
545 | if (shared != 0) {
|
---|
546 | old[shared] = 0;
|
---|
547 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
---|
548 | if (rc != EOK) {
|
---|
549 | vfs_node_put(base);
|
---|
550 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
551 | return rc;
|
---|
552 | }
|
---|
553 |
|
---|
554 | vfs_node_put(base);
|
---|
555 | base = vfs_node_get(&base_lr);
|
---|
556 | if (!base) {
|
---|
557 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
558 | return ENOMEM;
|
---|
559 | }
|
---|
560 | old[shared] = '/';
|
---|
561 | old += shared;
|
---|
562 | new += shared;
|
---|
563 | }
|
---|
564 |
|
---|
565 | rc = vfs_lookup_internal(base, old, L_DISABLE_MOUNTS, &old_lr);
|
---|
566 | if (rc != EOK) {
|
---|
567 | vfs_node_put(base);
|
---|
568 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
569 | return rc;
|
---|
570 | }
|
---|
571 |
|
---|
572 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
573 | &new_lr_orig);
|
---|
574 | if (rc == EOK) {
|
---|
575 | orig_unlinked = true;
|
---|
576 | } else if (rc != ENOENT) {
|
---|
577 | vfs_node_put(base);
|
---|
578 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
579 | return rc;
|
---|
580 | }
|
---|
581 |
|
---|
582 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
---|
583 | if (rc != EOK) {
|
---|
584 | vfs_link_internal(base, old, &old_lr.triplet);
|
---|
585 | if (orig_unlinked)
|
---|
586 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
587 | vfs_node_put(base);
|
---|
588 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
589 | return rc;
|
---|
590 | }
|
---|
591 |
|
---|
592 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
593 | &old_lr);
|
---|
594 | if (rc != EOK) {
|
---|
595 | if (orig_unlinked)
|
---|
596 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
597 | vfs_node_put(base);
|
---|
598 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
599 | return rc;
|
---|
600 | }
|
---|
601 |
|
---|
602 | /* If the node is not held by anyone, try to destroy it. */
|
---|
603 | if (orig_unlinked) {
|
---|
604 | vfs_node_t *node = vfs_node_peek(&new_lr_orig);
|
---|
605 | if (!node)
|
---|
606 | out_destroy(&new_lr_orig.triplet);
|
---|
607 | else
|
---|
608 | vfs_node_put(node);
|
---|
609 | }
|
---|
610 |
|
---|
611 | vfs_node_put(base);
|
---|
612 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
613 | return EOK;
|
---|
614 | }
|
---|
615 |
|
---|
616 | int vfs_op_resize(int fd, int64_t size)
|
---|
617 | {
|
---|
618 | vfs_file_t *file = vfs_file_get(fd);
|
---|
619 | if (!file)
|
---|
620 | return EBADF;
|
---|
621 |
|
---|
622 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
623 |
|
---|
624 | int rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
625 | file->node->service_id, file->node->index, size);
|
---|
626 | if (rc == EOK)
|
---|
627 | file->node->size = size;
|
---|
628 |
|
---|
629 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
630 | vfs_file_put(file);
|
---|
631 | return rc;
|
---|
632 | }
|
---|
633 |
|
---|
634 | int vfs_op_stat(int fd)
|
---|
635 | {
|
---|
636 | vfs_file_t *file = vfs_file_get(fd);
|
---|
637 | if (!file)
|
---|
638 | return EBADF;
|
---|
639 |
|
---|
640 | vfs_node_t *node = file->node;
|
---|
641 |
|
---|
642 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
643 | int rc = async_data_read_forward_fast(exch, VFS_OUT_STAT,
|
---|
644 | node->service_id, node->index, true, 0, NULL);
|
---|
645 | vfs_exchange_release(exch);
|
---|
646 |
|
---|
647 | vfs_file_put(file);
|
---|
648 | return rc;
|
---|
649 | }
|
---|
650 |
|
---|
651 | int vfs_op_statfs(int fd)
|
---|
652 | {
|
---|
653 | vfs_file_t *file = vfs_file_get(fd);
|
---|
654 | if (!file)
|
---|
655 | return EBADF;
|
---|
656 |
|
---|
657 | vfs_node_t *node = file->node;
|
---|
658 |
|
---|
659 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
660 | int rc = async_data_read_forward_fast(exch, VFS_OUT_STATFS,
|
---|
661 | node->service_id, node->index, false, 0, NULL);
|
---|
662 | vfs_exchange_release(exch);
|
---|
663 |
|
---|
664 | vfs_file_put(file);
|
---|
665 | return rc;
|
---|
666 | }
|
---|
667 |
|
---|
668 | int vfs_op_sync(int fd)
|
---|
669 | {
|
---|
670 | vfs_file_t *file = vfs_file_get(fd);
|
---|
671 | if (!file)
|
---|
672 | return EBADF;
|
---|
673 |
|
---|
674 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
675 |
|
---|
676 | aid_t msg;
|
---|
677 | ipc_call_t answer;
|
---|
678 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
679 | file->node->index, &answer);
|
---|
680 |
|
---|
681 | vfs_exchange_release(fs_exch);
|
---|
682 |
|
---|
683 | sysarg_t rc;
|
---|
684 | async_wait_for(msg, &rc);
|
---|
685 |
|
---|
686 | vfs_file_put(file);
|
---|
687 | return rc;
|
---|
688 |
|
---|
689 | }
|
---|
690 |
|
---|
691 | static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
692 | fs_index_t index, aoff64_t size)
|
---|
693 | {
|
---|
694 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
695 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
696 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
697 | UPPER32(size));
|
---|
698 | vfs_exchange_release(exch);
|
---|
699 |
|
---|
700 | return (int) rc;
|
---|
701 | }
|
---|
702 |
|
---|
703 | int vfs_op_unlink(int parentfd, int expectfd, char *path)
|
---|
704 | {
|
---|
705 | int rc = EOK;
|
---|
706 | vfs_file_t *parent = NULL;
|
---|
707 | vfs_file_t *expect = NULL;
|
---|
708 |
|
---|
709 | if (parentfd == expectfd)
|
---|
710 | return EINVAL;
|
---|
711 |
|
---|
712 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * Files are retrieved in order of file descriptors, to prevent
|
---|
716 | * deadlock.
|
---|
717 | */
|
---|
718 | if (parentfd < expectfd) {
|
---|
719 | parent = vfs_file_get(parentfd);
|
---|
720 | if (!parent) {
|
---|
721 | rc = EBADF;
|
---|
722 | goto exit;
|
---|
723 | }
|
---|
724 | }
|
---|
725 |
|
---|
726 | if (expectfd >= 0) {
|
---|
727 | expect = vfs_file_get(expectfd);
|
---|
728 | if (!expect) {
|
---|
729 | rc = EBADF;
|
---|
730 | goto exit;
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | if (parentfd > expectfd) {
|
---|
735 | parent = vfs_file_get(parentfd);
|
---|
736 | if (!parent) {
|
---|
737 | rc = EBADF;
|
---|
738 | goto exit;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | assert(parent != NULL);
|
---|
743 |
|
---|
744 | if (expectfd >= 0) {
|
---|
745 | vfs_lookup_res_t lr;
|
---|
746 | rc = vfs_lookup_internal(parent->node, path, 0, &lr);
|
---|
747 | if (rc != EOK)
|
---|
748 | goto exit;
|
---|
749 |
|
---|
750 | vfs_node_t *found_node = vfs_node_peek(&lr);
|
---|
751 | vfs_node_put(found_node);
|
---|
752 | if (expect->node != found_node) {
|
---|
753 | rc = ENOENT;
|
---|
754 | goto exit;
|
---|
755 | }
|
---|
756 |
|
---|
757 | vfs_file_put(expect);
|
---|
758 | expect = NULL;
|
---|
759 | }
|
---|
760 |
|
---|
761 | vfs_lookup_res_t lr;
|
---|
762 | rc = vfs_lookup_internal(parent->node, path, L_UNLINK, &lr);
|
---|
763 | if (rc != EOK)
|
---|
764 | goto exit;
|
---|
765 |
|
---|
766 | /* If the node is not held by anyone, try to destroy it. */
|
---|
767 | vfs_node_t *node = vfs_node_peek(&lr);
|
---|
768 | if (!node)
|
---|
769 | out_destroy(&lr.triplet);
|
---|
770 | else
|
---|
771 | vfs_node_put(node);
|
---|
772 |
|
---|
773 | exit:
|
---|
774 | if (path)
|
---|
775 | free(path);
|
---|
776 | if (parent)
|
---|
777 | vfs_file_put(parent);
|
---|
778 | if (expect)
|
---|
779 | vfs_file_put(expect);
|
---|
780 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
781 | return rc;
|
---|
782 | }
|
---|
783 |
|
---|
784 | int vfs_op_unmount(int mpfd)
|
---|
785 | {
|
---|
786 | vfs_file_t *mp = vfs_file_get(mpfd);
|
---|
787 | if (mp == NULL)
|
---|
788 | return EBADF;
|
---|
789 |
|
---|
790 | if (mp->node->mount == NULL) {
|
---|
791 | vfs_file_put(mp);
|
---|
792 | return ENOENT;
|
---|
793 | }
|
---|
794 |
|
---|
795 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
796 |
|
---|
797 | /*
|
---|
798 | * Count the total number of references for the mounted file system. We
|
---|
799 | * are expecting at least one, which is held by the mount point.
|
---|
800 | * If we find more, it means that
|
---|
801 | * the file system cannot be gracefully unmounted at the moment because
|
---|
802 | * someone is working with it.
|
---|
803 | */
|
---|
804 | if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
|
---|
805 | mp->node->mount->service_id) != 1) {
|
---|
806 | vfs_file_put(mp);
|
---|
807 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
808 | return EBUSY;
|
---|
809 | }
|
---|
810 |
|
---|
811 | async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
|
---|
812 | int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
813 | mp->node->mount->service_id);
|
---|
814 | vfs_exchange_release(exch);
|
---|
815 |
|
---|
816 | if (rc != EOK) {
|
---|
817 | vfs_file_put(mp);
|
---|
818 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
819 | return rc;
|
---|
820 | }
|
---|
821 |
|
---|
822 | vfs_node_forget(mp->node->mount);
|
---|
823 | vfs_node_put(mp->node);
|
---|
824 | mp->node->mount = NULL;
|
---|
825 |
|
---|
826 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
827 |
|
---|
828 | vfs_file_put(mp);
|
---|
829 | return EOK;
|
---|
830 | }
|
---|
831 |
|
---|
832 | int vfs_op_wait_handle(bool high_fd)
|
---|
833 | {
|
---|
834 | return vfs_wait_handle_internal(high_fd);
|
---|
835 | }
|
---|
836 |
|
---|
837 | static inline bool walk_flags_valid(int flags)
|
---|
838 | {
|
---|
839 | if ((flags & ~WALK_ALL_FLAGS) != 0)
|
---|
840 | return false;
|
---|
841 | if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE))
|
---|
842 | return false;
|
---|
843 | if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY))
|
---|
844 | return false;
|
---|
845 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
|
---|
846 | if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR))
|
---|
847 | return false;
|
---|
848 | }
|
---|
849 | return true;
|
---|
850 | }
|
---|
851 |
|
---|
852 | static inline int walk_lookup_flags(int flags)
|
---|
853 | {
|
---|
854 | int lflags = 0;
|
---|
855 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE))
|
---|
856 | lflags |= L_CREATE;
|
---|
857 | if (flags & WALK_MUST_CREATE)
|
---|
858 | lflags |= L_EXCLUSIVE;
|
---|
859 | if (flags & WALK_REGULAR)
|
---|
860 | lflags |= L_FILE;
|
---|
861 | if (flags & WALK_DIRECTORY)
|
---|
862 | lflags |= L_DIRECTORY;
|
---|
863 | if (flags & WALK_MOUNT_POINT)
|
---|
864 | lflags |= L_MP;
|
---|
865 | return lflags;
|
---|
866 | }
|
---|
867 |
|
---|
868 | int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
|
---|
869 | {
|
---|
870 | if (!walk_flags_valid(flags))
|
---|
871 | return EINVAL;
|
---|
872 |
|
---|
873 | vfs_file_t *parent = vfs_file_get(parentfd);
|
---|
874 | if (!parent)
|
---|
875 | return EBADF;
|
---|
876 |
|
---|
877 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
878 |
|
---|
879 | vfs_lookup_res_t lr;
|
---|
880 | int rc = vfs_lookup_internal(parent->node, path,
|
---|
881 | walk_lookup_flags(flags), &lr);
|
---|
882 | if (rc != EOK) {
|
---|
883 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
884 | vfs_file_put(parent);
|
---|
885 | return rc;
|
---|
886 | }
|
---|
887 |
|
---|
888 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
889 | if (!node) {
|
---|
890 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
891 | vfs_file_put(parent);
|
---|
892 | return ENOMEM;
|
---|
893 | }
|
---|
894 |
|
---|
895 | vfs_file_t *file;
|
---|
896 | int fd = vfs_fd_alloc(&file, false);
|
---|
897 | if (fd < 0) {
|
---|
898 | vfs_node_put(node);
|
---|
899 | vfs_file_put(parent);
|
---|
900 | return fd;
|
---|
901 | }
|
---|
902 | assert(file != NULL);
|
---|
903 |
|
---|
904 | file->node = node;
|
---|
905 | file->permissions = parent->permissions;
|
---|
906 | file->open_read = false;
|
---|
907 | file->open_write = false;
|
---|
908 |
|
---|
909 | vfs_file_put(file);
|
---|
910 | vfs_file_put(parent);
|
---|
911 |
|
---|
912 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
913 |
|
---|
914 | *out_fd = fd;
|
---|
915 | return EOK;
|
---|
916 | }
|
---|
917 |
|
---|
918 | int vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes)
|
---|
919 | {
|
---|
920 | return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes);
|
---|
921 | }
|
---|
922 |
|
---|
923 | /**
|
---|
924 | * @}
|
---|
925 | */
|
---|