source: mainline/uspace/lib/c/generic/vfs/vfs.c@ bde5c04

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bde5c04 was 8e3498b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

vfs_read/write() should return error code separately from number of bytes transferred.

  • Property mode set to 100644
File size: 32.3 KB
Line 
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 libc
30 * @{
31 */
32/** @file
33 */
34
35#include <vfs/vfs.h>
36#include <vfs/canonify.h>
37#include <vfs/vfs_mtab.h>
38#include <vfs/vfs_sess.h>
39#include <macros.h>
40#include <stdlib.h>
41#include <stddef.h>
42#include <stdint.h>
43#include <ipc/services.h>
44#include <ns.h>
45#include <async.h>
46#include <fibril_synch.h>
47#include <errno.h>
48#include <assert.h>
49#include <str.h>
50#include <loc.h>
51#include <ipc/vfs.h>
52#include <ipc/loc.h>
53
54/*
55 * This file contains the implementation of the native HelenOS file system API.
56 *
57 * The API supports client-side file system roots, client-side IO cursors and
58 * uses file handles as a primary means to refer to files. In order to call the
59 * API functions, one just includes vfs/vfs.h.
60 *
61 * The API functions come in two main flavors:
62 *
63 * - functions that operate on integer file handles, such as:
64 * vfs_walk(), vfs_open(), vfs_read(), vfs_link(), ...
65 *
66 * - functions that operate on paths, such as:
67 * vfs_lookup(), vfs_link_path(), vfs_unlink_path(), vfs_rename_path(), ...
68 *
69 * There is usually a corresponding path function for each file handle function
70 * that exists mostly as a convenience wrapper, except for cases when only a
71 * path version exists due to file system consistency considerations (see
72 * vfs_rename_path()). Sometimes one of the versions does not make sense, in
73 * which case it is also omitted.
74 *
75 * Besides of that, the API provides some convenience wrappers for frequently
76 * performed pairs of operations, for example there is a combo API for
77 * vfs_lookup() and vfs_open(): vfs_lookup_open().
78 *
79 * Some of the functions here return a file handle that can be passed to other
80 * functions. Note that a file handle does not automatically represent a file
81 * from which one can read or to which one can write. In order to do so, the
82 * file handle must be opened first for reading/writing using vfs_open().
83 *
84 * All file handles, no matter whether opened or not, must be eventually
85 * returned to the system using vfs_put(). Non-returned file handles are in use
86 * and consume system resources.
87 *
88 * Functions that return int return an error code on error and do not
89 * set errno. Depending on function, success is signalled by returning either
90 * EOK or a non-negative file handle.
91 *
92 * An example life-cycle of a file handle is as follows:
93 *
94 * #include <vfs/vfs.h>
95 *
96 * int file = vfs_lookup("/foo/bar/foobar", WALK_REGULAR);
97 * if (file < 0)
98 * return file;
99 * int rc = vfs_open(file, MODE_READ);
100 * if (rc != EOK) {
101 * (void) vfs_put(file);
102 * return rc;
103 * }
104 * aoff64_t pos = 42;
105 * char buf[512];
106 * size_t nread;
107 * rc = vfs_read(file, &pos, buf, sizeof(buf), &nread);
108 * if (rc != EOK) {
109 * vfs_put(file);
110 * return rc;
111 * }
112 *
113 * // buf is now filled with nread bytes from file
114 *
115 * vfs_put(file);
116 */
117
118static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
119static async_sess_t *vfs_sess = NULL;
120
121static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
122
123static int cwd_fd = -1;
124static char *cwd_path = NULL;
125static size_t cwd_size = 0;
126
127static FIBRIL_MUTEX_INITIALIZE(root_mutex);
128static int root_fd = -1;
129
130static int get_parent_and_child(const char *path, char **child)
131{
132 size_t size;
133 char *apath = vfs_absolutize(path, &size);
134 if (!apath)
135 return ENOMEM;
136
137 char *slash = str_rchr(apath, L'/');
138 int parent;
139 if (slash == apath) {
140 parent = vfs_root();
141 *child = apath;
142 } else {
143 *slash = '\0';
144 parent = vfs_lookup(apath, WALK_DIRECTORY);
145 if (parent < 0) {
146 free(apath);
147 return parent;
148 }
149 *slash = '/';
150 *child = str_dup(slash);
151 free(apath);
152 if (!*child) {
153 vfs_put(parent);
154 return ENOMEM;
155 }
156 }
157
158 return parent;
159}
160
161/** Make a potentially relative path absolute
162 *
163 * This function coverts a current-working-directory-relative path into a
164 * well-formed, absolute path. The caller is responsible for deallocating the
165 * returned buffer.
166 *
167 * @param[in] path Path to be absolutized
168 * @param[out] retlen Length of the absolutized path
169 *
170 * @return New buffer holding the absolutized path or NULL
171 */
172char *vfs_absolutize(const char *path, size_t *retlen)
173{
174 char *ncwd_path;
175 char *ncwd_path_nc;
176
177 fibril_mutex_lock(&cwd_mutex);
178 size_t size = str_size(path);
179 if (*path != '/') {
180 if (cwd_path == NULL) {
181 fibril_mutex_unlock(&cwd_mutex);
182 return NULL;
183 }
184 ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
185 if (ncwd_path_nc == NULL) {
186 fibril_mutex_unlock(&cwd_mutex);
187 return NULL;
188 }
189 str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
190 ncwd_path_nc[cwd_size] = '/';
191 ncwd_path_nc[cwd_size + 1] = '\0';
192 } else {
193 ncwd_path_nc = malloc(size + 1);
194 if (ncwd_path_nc == NULL) {
195 fibril_mutex_unlock(&cwd_mutex);
196 return NULL;
197 }
198 ncwd_path_nc[0] = '\0';
199 }
200 str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
201 ncwd_path = canonify(ncwd_path_nc, retlen);
202 if (ncwd_path == NULL) {
203 fibril_mutex_unlock(&cwd_mutex);
204 free(ncwd_path_nc);
205 return NULL;
206 }
207 /*
208 * We need to clone ncwd_path because canonify() works in-place and thus
209 * the address in ncwd_path need not be the same as ncwd_path_nc, even
210 * though they both point into the same dynamically allocated buffer.
211 */
212 ncwd_path = str_dup(ncwd_path);
213 free(ncwd_path_nc);
214 if (ncwd_path == NULL) {
215 fibril_mutex_unlock(&cwd_mutex);
216 return NULL;
217 }
218 fibril_mutex_unlock(&cwd_mutex);
219 return ncwd_path;
220}
221
222/** Clone a file handle
223 *
224 * The caller can choose whether to clone an existing file handle into another
225 * already existing file handle (in which case it is first closed) or to a new
226 * file handle allocated either from low or high indices.
227 *
228 * @param file_from Source file handle
229 * @param file_to Destination file handle or -1
230 * @param high If file_to is -1, high controls whether the new file
231 * handle will be allocated from high indices
232 *
233 * @return New file handle on success or a negative error code
234 */
235int vfs_clone(int file_from, int file_to, bool high)
236{
237 async_exch_t *vfs_exch = vfs_exchange_begin();
238 int rc = async_req_3_0(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from,
239 (sysarg_t) file_to, (sysarg_t) high);
240 vfs_exchange_end(vfs_exch);
241 return rc;
242}
243
244/** Get current working directory path
245 *
246 * @param[out] buf Buffer
247 * @param size Size of @a buf
248 *
249 * @return EOK on success or a non-negative error code
250 */
251int vfs_cwd_get(char *buf, size_t size)
252{
253 fibril_mutex_lock(&cwd_mutex);
254
255 if ((cwd_size == 0) || (size < cwd_size + 1)) {
256 fibril_mutex_unlock(&cwd_mutex);
257 return ERANGE;
258 }
259
260 str_cpy(buf, size, cwd_path);
261 fibril_mutex_unlock(&cwd_mutex);
262
263 return EOK;
264}
265
266/** Change working directory
267 *
268 * @param path Path of the new working directory
269 *
270 * @return EOK on success or a negative error code
271 */
272int vfs_cwd_set(const char *path)
273{
274 size_t abs_size;
275 char *abs = vfs_absolutize(path, &abs_size);
276 if (!abs)
277 return ENOMEM;
278
279 int fd = vfs_lookup(abs, WALK_DIRECTORY);
280 if (fd < 0) {
281 free(abs);
282 return fd;
283 }
284
285 fibril_mutex_lock(&cwd_mutex);
286
287 if (cwd_fd >= 0)
288 vfs_put(cwd_fd);
289
290 if (cwd_path)
291 free(cwd_path);
292
293 cwd_fd = fd;
294 cwd_path = abs;
295 cwd_size = abs_size;
296
297 fibril_mutex_unlock(&cwd_mutex);
298 return EOK;
299}
300
301/** Start an async exchange on the VFS session
302 *
303 * @return New exchange
304 */
305async_exch_t *vfs_exchange_begin(void)
306{
307 fibril_mutex_lock(&vfs_mutex);
308
309 while (vfs_sess == NULL) {
310 vfs_sess = service_connect_blocking(SERVICE_VFS, INTERFACE_VFS,
311 0);
312 }
313
314 fibril_mutex_unlock(&vfs_mutex);
315
316 return async_exchange_begin(vfs_sess);
317}
318
319/** Finish an async exchange on the VFS session
320 *
321 * @param exch Exchange to be finished
322 */
323void vfs_exchange_end(async_exch_t *exch)
324{
325 async_exchange_end(exch);
326}
327
328/** Open session to service represented by a special file
329 *
330 * Given that the file referred to by @a file represents a service,
331 * open a session to that service.
332 *
333 * @param file File handle representing a service
334 * @param iface Interface to connect to (XXX Should be automatic)
335 *
336 * @return Session pointer on success.
337 * @return @c NULL or error.
338 */
339async_sess_t *vfs_fd_session(int file, iface_t iface)
340{
341 struct stat stat;
342 int rc = vfs_stat(file, &stat);
343 if (rc != 0)
344 return NULL;
345
346 if (stat.service == 0)
347 return NULL;
348
349 return loc_service_connect(stat.service, iface, 0);
350}
351
352/** Determine if a device contains the specified file system type. If so,
353 * return identification information.
354 *
355 * @param fs_name File system name
356 * @param serv Service representing the mountee
357 * @param info Place to store volume identification information
358 *
359 * @return EOK on success or a negative error code
360 */
361int vfs_fsprobe(const char *fs_name, service_id_t serv,
362 vfs_fs_probe_info_t *info)
363{
364 sysarg_t rc;
365
366 ipc_call_t answer;
367 async_exch_t *exch = vfs_exchange_begin();
368 aid_t req = async_send_1(exch, VFS_IN_FSPROBE, serv, &answer);
369
370 rc = async_data_write_start(exch, (void *) fs_name,
371 str_size(fs_name));
372
373 async_wait_for(req, &rc);
374
375 if (rc != EOK) {
376 vfs_exchange_end(exch);
377 return rc;
378 }
379
380 rc = async_data_read_start(exch, info, sizeof(*info));
381 vfs_exchange_end(exch);
382
383 return rc;
384}
385
386
387/** Return a list of currently available file system types
388 *
389 * @param fstypes Points to structure where list of filesystem types is
390 * stored. It is read as a null-terminated list of strings
391 * fstypes->fstypes[0..]. To free the list use vfs_fstypes_free().
392 *
393 * @return EOK on success or a negative error code
394 */
395int vfs_fstypes(vfs_fstypes_t *fstypes)
396{
397 sysarg_t size;
398 char *buf;
399 char dummybuf[1];
400 size_t count, i;
401
402 async_exch_t *exch = vfs_exchange_begin();
403 int rc = async_req_0_1(exch, VFS_IN_FSTYPES, &size);
404
405 if (rc != EOK) {
406 vfs_exchange_end(exch);
407 return rc;
408 }
409
410 buf = malloc(size);
411 if (buf == NULL) {
412 buf = dummybuf;
413 size = 1;
414 }
415
416 rc = async_data_read_start(exch, buf, size);
417 vfs_exchange_end(exch);
418
419 if (buf == dummybuf)
420 return ENOMEM;
421
422 /*
423 * Buffer should contain a number of null-terminated strings.
424 * Count them so that we can allocate an index
425 */
426 count = 0;
427 i = 0;
428 while (i < size) {
429 if (buf[i] == '\0')
430 ++count;
431 ++i;
432 }
433
434 if (count == 0) {
435 free(buf);
436 return EIO;
437 }
438
439 fstypes->fstypes = calloc(sizeof(char *), count + 1);
440 if (fstypes->fstypes == NULL) {
441 free(buf);
442 return ENOMEM;
443 }
444
445 /* Now fill the index */
446 if (buf[0] != '\0')
447 fstypes->fstypes[0] = &buf[0];
448 count = 0;
449 i = 0;
450 while (i < size) {
451 if (buf[i] == '\0')
452 fstypes->fstypes[++count] = &buf[i + 1];
453 ++i;
454 }
455 fstypes->fstypes[count] = NULL;
456 fstypes->buf = buf;
457 fstypes->size = size;
458
459 return rc;
460}
461
462/** Free list of file system types.
463 *
464 * @param fstypes List of file system types
465 */
466void vfs_fstypes_free(vfs_fstypes_t *fstypes)
467{
468 free(fstypes->buf);
469 fstypes->buf = NULL;
470 free(fstypes->fstypes);
471 fstypes->fstypes = NULL;
472 fstypes->size = 0;
473}
474
475/** Link a file or directory
476 *
477 * Create a new name and an empty file or an empty directory in a parent
478 * directory. If child with the same name already exists, the function returns
479 * a failure, the existing file remains untouched and no file system object
480 * is created.
481 *
482 * @param parent File handle of the parent directory node
483 * @param child New name to be linked
484 * @param kind Kind of the object to be created: KIND_FILE or
485 * KIND_DIRECTORY
486 * @param[out] linkedfd If not NULL, will receive a file handle to the linked
487 * child
488 * @return EOK on success or a negative error code
489 */
490int vfs_link(int parent, const char *child, vfs_file_kind_t kind, int *linkedfd)
491{
492 int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR;
493 int file = vfs_walk(parent, child, WALK_MUST_CREATE | flags);
494
495 if (file < 0)
496 return file;
497
498 if (linkedfd)
499 *linkedfd = file;
500 else
501 vfs_put(file);
502
503 return EOK;
504}
505
506/** Link a file or directory
507 *
508 * Create a new name and an empty file or an empty directory at given path.
509 * If a link with the same name already exists, the function returns
510 * a failure, the existing file remains untouched and no file system object
511 * is created.
512 *
513 * @param path New path to be linked
514 * @param kind Kind of the object to be created: KIND_FILE or
515 * KIND_DIRECTORY
516 * @param[out] linkedfd If not NULL, will receive a file handle to the linked
517 * child
518 * @return EOK on success or a negative error code
519 */
520int vfs_link_path(const char *path, vfs_file_kind_t kind, int *linkedfd)
521{
522 char *child;
523 int parent = get_parent_and_child(path, &child);
524 if (parent < 0)
525 return parent;
526
527 int rc = vfs_link(parent, child, kind, linkedfd);
528
529 free(child);
530 vfs_put(parent);
531 return rc;
532}
533
534/** Lookup a path relative to the local root
535 *
536 * @param path Path to be looked up
537 * @param flags Walk flags
538 *
539 * @return File handle representing the result on success or a negative
540 * error code on error
541 */
542int vfs_lookup(const char *path, int flags)
543{
544 size_t size;
545 char *p = vfs_absolutize(path, &size);
546 if (!p)
547 return ENOMEM;
548 int root = vfs_root();
549 if (root < 0) {
550 free(p);
551 return ENOENT;
552 }
553 int rc = vfs_walk(root, p, flags);
554 vfs_put(root);
555 free(p);
556 return rc;
557}
558
559/** Lookup a path relative to the local root and open the result
560 *
561 * This function is a convenience combo for vfs_lookup() and vfs_open().
562 *
563 * @param path Path to be looked up
564 * @param flags Walk flags
565 * @param mode Mode in which to open file in
566 *
567 * @return EOK on success or a negative error code
568 */
569int vfs_lookup_open(const char *path, int flags, int mode)
570{
571 int file = vfs_lookup(path, flags);
572 if (file < 0)
573 return file;
574
575 int rc = vfs_open(file, mode);
576 if (rc != EOK) {
577 vfs_put(file);
578 return rc;
579 }
580
581 return file;
582}
583
584/** Mount a file system
585 *
586 * @param[in] mp File handle representing the mount-point
587 * @param[in] fs_name File system name
588 * @param[in] serv Service representing the mountee
589 * @param[in] opts Mount options for the endpoint file system
590 * @param[in] flags Mount flags
591 * @param[in] instance Instance number of the file system server
592 * @param[out] mountedfd File handle of the mounted root if not NULL
593 *
594 * @return EOK on success or a negative error code
595 */
596int vfs_mount(int mp, const char *fs_name, service_id_t serv, const char *opts,
597 unsigned int flags, unsigned int instance, int *mountedfd)
598{
599 sysarg_t rc, rc1;
600
601 if (!mountedfd)
602 flags |= VFS_MOUNT_NO_REF;
603 if (mp < 0)
604 flags |= VFS_MOUNT_CONNECT_ONLY;
605
606 ipc_call_t answer;
607 async_exch_t *exch = vfs_exchange_begin();
608 aid_t req = async_send_4(exch, VFS_IN_MOUNT, mp, serv, flags, instance,
609 &answer);
610
611 rc1 = async_data_write_start(exch, (void *) opts, str_size(opts));
612 if (rc1 == EOK) {
613 rc1 = async_data_write_start(exch, (void *) fs_name,
614 str_size(fs_name));
615 }
616
617 vfs_exchange_end(exch);
618
619 async_wait_for(req, &rc);
620
621 if (mountedfd)
622 *mountedfd = (int) IPC_GET_ARG1(answer);
623
624 if (rc != EOK)
625 return rc;
626 return rc1;
627}
628
629/** Mount a file system
630 *
631 * @param[in] mp Path representing the mount-point
632 * @param[in] fs_name File system name
633 * @param[in] fqsn Fully qualified service name of the mountee
634 * @param[in] opts Mount options for the endpoint file system
635 * @param[in] flags Mount flags
636 * @param[in] instance Instance number of the file system server
637 *
638 * @return EOK on success or a negative error code
639 */
640int vfs_mount_path(const char *mp, const char *fs_name, const char *fqsn,
641 const char *opts, unsigned int flags, unsigned int instance)
642{
643 int null_id = -1;
644 char null[LOC_NAME_MAXLEN];
645
646 if (str_cmp(fqsn, "") == 0) {
647 /*
648 * No device specified, create a fresh null/%d device instead.
649 */
650 null_id = loc_null_create();
651
652 if (null_id == -1)
653 return ENOMEM;
654
655 snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
656 fqsn = null;
657 }
658
659 if (flags & IPC_FLAG_BLOCKING)
660 flags = VFS_MOUNT_BLOCKING;
661 else
662 flags = 0;
663
664 service_id_t service_id;
665 int res = loc_service_get_id(fqsn, &service_id, flags);
666 if (res != EOK) {
667 if (null_id != -1)
668 loc_null_destroy(null_id);
669
670 return res;
671 }
672
673 size_t mpa_size;
674 char *mpa = vfs_absolutize(mp, &mpa_size);
675 if (mpa == NULL) {
676 if (null_id != -1)
677 loc_null_destroy(null_id);
678
679 return ENOMEM;
680 }
681
682 fibril_mutex_lock(&root_mutex);
683
684 int rc;
685
686 if (str_cmp(mpa, "/") == 0) {
687 /* Mounting root. */
688
689 if (root_fd >= 0) {
690 fibril_mutex_unlock(&root_mutex);
691 if (null_id != -1)
692 loc_null_destroy(null_id);
693 return EBUSY;
694 }
695
696 int root;
697 rc = vfs_mount(-1, fs_name, service_id, opts, flags, instance,
698 &root);
699 if (rc == EOK)
700 root_fd = root;
701 } else {
702 if (root_fd < 0) {
703 fibril_mutex_unlock(&root_mutex);
704 if (null_id != -1)
705 loc_null_destroy(null_id);
706 return EINVAL;
707 }
708
709 int mpfd = vfs_walk(root_fd, mpa, WALK_DIRECTORY);
710 if (mpfd >= 0) {
711 rc = vfs_mount(mpfd, fs_name, service_id, opts, flags,
712 instance, NULL);
713 vfs_put(mpfd);
714 } else {
715 rc = mpfd;
716 }
717 }
718
719 fibril_mutex_unlock(&root_mutex);
720
721 if ((rc != EOK) && (null_id != -1))
722 loc_null_destroy(null_id);
723
724 return (int) rc;
725}
726
727
728/** Open a file handle for I/O
729 *
730 * @param file File handle to enable I/O on
731 * @param mode Mode in which to open file in
732 *
733 * @return EOK on success or a negative error code
734 */
735int vfs_open(int file, int mode)
736{
737 async_exch_t *exch = vfs_exchange_begin();
738 int rc = async_req_2_0(exch, VFS_IN_OPEN, file, mode);
739 vfs_exchange_end(exch);
740
741 return rc;
742}
743
744/** Pass a file handle to another VFS client
745 *
746 * @param vfs_exch Donor's VFS exchange
747 * @param file Donor's file handle to pass
748 * @param exch Exchange to the acceptor
749 *
750 * @return EOK on success or a negative error code
751 */
752int vfs_pass_handle(async_exch_t *vfs_exch, int file, async_exch_t *exch)
753{
754 return async_state_change_start(exch, VFS_PASS_HANDLE, (sysarg_t) file,
755 0, vfs_exch);
756}
757
758/** Stop working with a file handle
759 *
760 * @param file File handle to put
761 *
762 * @return EOK on success or a negative error code
763 */
764int vfs_put(int file)
765{
766 async_exch_t *exch = vfs_exchange_begin();
767 int rc = async_req_1_0(exch, VFS_IN_PUT, file);
768 vfs_exchange_end(exch);
769
770 return rc;
771}
772
773/** Receive a file handle from another VFS client
774 *
775 * @param high If true, the received file handle will be allocated from high
776 * indices
777 *
778 * @return EOK on success or a negative error code
779 */
780int vfs_receive_handle(bool high)
781{
782 ipc_callid_t callid;
783 if (!async_state_change_receive(&callid, NULL, NULL, NULL)) {
784 async_answer_0(callid, EINVAL);
785 return EINVAL;
786 }
787
788 async_exch_t *vfs_exch = vfs_exchange_begin();
789
790 async_state_change_finalize(callid, vfs_exch);
791
792 sysarg_t ret;
793 sysarg_t rc = async_req_1_1(vfs_exch, VFS_IN_WAIT_HANDLE, high, &ret);
794
795 async_exchange_end(vfs_exch);
796
797 if (rc != EOK)
798 return rc;
799 return ret;
800}
801
802/** Read data
803 *
804 * Read up to @a nbytes bytes from file if available. This function always reads
805 * all the available bytes up to @a nbytes.
806 *
807 * @param file File handle to read from
808 * @param[inout] pos Position to read from, updated by the actual bytes read
809 * @param buf Buffer, @a nbytes bytes long
810 * @param nbytes Number of bytes to read
811 * @param nread Place to store number of bytes actually read
812 *
813 * @return On success, EOK and @a *nread is filled with number
814 * of bytes actually read.
815 * @return On failure, an error code
816 */
817int vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread)
818{
819 ssize_t cnt = 0;
820 size_t nr = 0;
821 uint8_t *bp = (uint8_t *) buf;
822 int rc;
823
824 do {
825 bp += cnt;
826 nr += cnt;
827 *pos += cnt;
828 rc = vfs_read_short(file, *pos, bp, nbyte - nr, &cnt);
829 } while (rc == EOK && cnt > 0 && (nbyte - nr - cnt) > 0);
830
831 if (rc != EOK) {
832 *nread = nr;
833 return rc;
834 }
835
836 nr += cnt;
837 *pos += cnt;
838 *nread = nr;
839 return EOK;
840}
841
842/** Read bytes from a file
843 *
844 * Read up to @a nbyte bytes from file. The actual number of bytes read
845 * may be lower, but greater than zero if there are any bytes available.
846 * If there are no bytes available for reading, then the function will
847 * return success with zero bytes read.
848 *
849 * @param file File handle to read from
850 * @param[in] pos Position to read from
851 * @param buf Buffer to read from
852 * @param nbyte Maximum number of bytes to read
853 * @param[out] nread Actual number of bytes read (0 or more)
854 *
855 * @return EOK on success or a negative error code
856 */
857int vfs_read_short(int file, aoff64_t pos, void *buf, size_t nbyte,
858 ssize_t *nread)
859{
860 sysarg_t rc;
861 ipc_call_t answer;
862 aid_t req;
863
864 if (nbyte > DATA_XFER_LIMIT)
865 nbyte = DATA_XFER_LIMIT;
866
867 async_exch_t *exch = vfs_exchange_begin();
868
869 req = async_send_3(exch, VFS_IN_READ, file, LOWER32(pos),
870 UPPER32(pos), &answer);
871 rc = async_data_read_start(exch, (void *) buf, nbyte);
872
873 vfs_exchange_end(exch);
874
875 if (rc == EOK)
876 async_wait_for(req, &rc);
877 else
878 async_forget(req);
879
880 if (rc != EOK)
881 return rc;
882
883 *nread = (ssize_t) IPC_GET_ARG1(answer);
884 return EOK;
885}
886
887/** Rename a file or directory
888 *
889 * There is no file-handle-based variant to disallow attempts to introduce loops
890 * and breakage in the directory tree when relinking eg. a node under its own
891 * descendant. The path-based variant is not susceptible because the VFS can
892 * prevent this lexically by comparing the paths.
893 *
894 * @param old Old path
895 * @param new New path
896 *
897 * @return EOK on success or a negative error code
898 */
899int vfs_rename_path(const char *old, const char *new)
900{
901 sysarg_t rc;
902 sysarg_t rc_orig;
903 aid_t req;
904
905 size_t olda_size;
906 char *olda = vfs_absolutize(old, &olda_size);
907 if (olda == NULL)
908 return ENOMEM;
909
910 size_t newa_size;
911 char *newa = vfs_absolutize(new, &newa_size);
912 if (newa == NULL) {
913 free(olda);
914 return ENOMEM;
915 }
916
917 async_exch_t *exch = vfs_exchange_begin();
918 int root = vfs_root();
919 if (root < 0) {
920 free(olda);
921 free(newa);
922 return ENOENT;
923 }
924
925 req = async_send_1(exch, VFS_IN_RENAME, root, NULL);
926 rc = async_data_write_start(exch, olda, olda_size);
927 if (rc != EOK) {
928 vfs_exchange_end(exch);
929 free(olda);
930 free(newa);
931 vfs_put(root);
932 async_wait_for(req, &rc_orig);
933 if (rc_orig != EOK)
934 rc = rc_orig;
935 return rc;
936 }
937 rc = async_data_write_start(exch, newa, newa_size);
938 if (rc != EOK) {
939 vfs_exchange_end(exch);
940 free(olda);
941 free(newa);
942 vfs_put(root);
943 async_wait_for(req, &rc_orig);
944 if (rc_orig != EOK)
945 rc = rc_orig;
946 return rc;
947 }
948 vfs_exchange_end(exch);
949 free(olda);
950 free(newa);
951 vfs_put(root);
952 async_wait_for(req, &rc);
953
954 return rc;
955}
956
957/** Resize file to a specified length
958 *
959 * Resize file so that its size is exactly @a length.
960 *
961 * @param file File handle to resize
962 * @param length New length
963 *
964 * @return EOK on success or a negative error code
965 */
966int vfs_resize(int file, aoff64_t length)
967{
968 async_exch_t *exch = vfs_exchange_begin();
969 int rc = async_req_3_0(exch, VFS_IN_RESIZE, file, LOWER32(length),
970 UPPER32(length));
971 vfs_exchange_end(exch);
972
973 return rc;
974}
975
976/** Return a new file handle representing the local root
977 *
978 * @return A clone of the local root file handle or a negative error code
979 */
980int vfs_root(void)
981{
982 fibril_mutex_lock(&root_mutex);
983 int r;
984 if (root_fd < 0)
985 r = ENOENT;
986 else
987 r = vfs_clone(root_fd, -1, true);
988 fibril_mutex_unlock(&root_mutex);
989 return r;
990}
991
992/** Set a new local root
993 *
994 * Note that it is still possible to have file handles for other roots and pass
995 * them to the API functions. Functions like vfs_root() and vfs_lookup() will
996 * however consider the file set by this function to be the root.
997 *
998 * @param nroot The new local root file handle
999 */
1000void vfs_root_set(int nroot)
1001{
1002 fibril_mutex_lock(&root_mutex);
1003 if (root_fd >= 0)
1004 vfs_put(root_fd);
1005 root_fd = vfs_clone(nroot, -1, true);
1006 fibril_mutex_unlock(&root_mutex);
1007}
1008
1009/** Get file information
1010 *
1011 * @param file File handle to get information about
1012 * @param[out] stat Place to store file information
1013 *
1014 * @return EOK on success or a negative error code
1015 */
1016int vfs_stat(int file, struct stat *stat)
1017{
1018 sysarg_t rc;
1019 aid_t req;
1020
1021 async_exch_t *exch = vfs_exchange_begin();
1022
1023 req = async_send_1(exch, VFS_IN_STAT, file, NULL);
1024 rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
1025 if (rc != EOK) {
1026 vfs_exchange_end(exch);
1027
1028 sysarg_t rc_orig;
1029 async_wait_for(req, &rc_orig);
1030
1031 if (rc_orig != EOK)
1032 rc = rc_orig;
1033
1034 return rc;
1035 }
1036
1037 vfs_exchange_end(exch);
1038 async_wait_for(req, &rc);
1039
1040 return rc;
1041}
1042
1043/** Get file information
1044 *
1045 * @param path File path to get information about
1046 * @param[out] stat Place to store file information
1047 *
1048 * @return EOK on success or a negative error code
1049 */
1050int vfs_stat_path(const char *path, struct stat *stat)
1051{
1052 int file = vfs_lookup(path, 0);
1053 if (file < 0)
1054 return file;
1055
1056 int rc = vfs_stat(file, stat);
1057
1058 vfs_put(file);
1059
1060 return rc;
1061}
1062
1063/** Get filesystem statistics
1064 *
1065 * @param file File located on the queried file system
1066 * @param[out] st Buffer for storing information
1067 *
1068 * @return EOK on success or a negative error code
1069 */
1070int vfs_statfs(int file, struct statfs *st)
1071{
1072 sysarg_t rc, ret;
1073 aid_t req;
1074
1075 async_exch_t *exch = vfs_exchange_begin();
1076
1077 req = async_send_1(exch, VFS_IN_STATFS, file, NULL);
1078 rc = async_data_read_start(exch, (void *) st, sizeof(*st));
1079
1080 vfs_exchange_end(exch);
1081 async_wait_for(req, &ret);
1082
1083 rc = (ret != EOK ? ret : rc);
1084
1085 return rc;
1086}
1087
1088/** Get filesystem statistics
1089 *
1090 * @param file Path pointing to the queried file system
1091 * @param[out] st Buffer for storing information
1092 *
1093 * @return EOK on success or a negative error code
1094 */
1095int vfs_statfs_path(const char *path, struct statfs *st)
1096{
1097 int file = vfs_lookup(path, 0);
1098 if (file < 0)
1099 return file;
1100
1101 int rc = vfs_statfs(file, st);
1102
1103 vfs_put(file);
1104
1105 return rc;
1106}
1107
1108/** Synchronize file
1109 *
1110 * @param file File handle to synchronize
1111 *
1112 * @return EOK on success or a negative error code
1113 */
1114int vfs_sync(int file)
1115{
1116 async_exch_t *exch = vfs_exchange_begin();
1117 int rc = async_req_1_0(exch, VFS_IN_SYNC, file);
1118 vfs_exchange_end(exch);
1119
1120 return rc;
1121}
1122
1123/** Unlink a file or directory
1124 *
1125 * Unlink a name from a parent directory. The caller can supply the file handle
1126 * of the unlinked child in order to detect a possible race with vfs_link() and
1127 * avoid unlinking a wrong file. If the last link for a file or directory is
1128 * removed, the FS implementation will deallocate its resources.
1129 *
1130 * @param parent File handle of the parent directory node
1131 * @param child Old name to be unlinked
1132 * @param expect File handle of the unlinked child
1133 *
1134 * @return EOK on success or a negative error code
1135 */
1136int vfs_unlink(int parent, const char *child, int expect)
1137{
1138 sysarg_t rc;
1139 aid_t req;
1140
1141 async_exch_t *exch = vfs_exchange_begin();
1142
1143 req = async_send_2(exch, VFS_IN_UNLINK, parent, expect, NULL);
1144 rc = async_data_write_start(exch, child, str_size(child));
1145
1146 vfs_exchange_end(exch);
1147
1148 sysarg_t rc_orig;
1149 async_wait_for(req, &rc_orig);
1150
1151 if (rc_orig != EOK)
1152 return (int) rc_orig;
1153 return rc;
1154}
1155
1156/** Unlink a file or directory
1157 *
1158 * Unlink a path. If the last link for a file or directory is removed, the FS
1159 * implementation will deallocate its resources.
1160 *
1161 * @param path Old path to be unlinked
1162 *
1163 * @return EOK on success or a negative error code
1164 */
1165int vfs_unlink_path(const char *path)
1166{
1167 int expect = vfs_lookup(path, 0);
1168 if (expect < 0)
1169 return expect;
1170
1171 char *child;
1172 int parent = get_parent_and_child(path, &child);
1173 if (parent < 0) {
1174 vfs_put(expect);
1175 return parent;
1176 }
1177
1178 int rc = vfs_unlink(parent, child, expect);
1179
1180 free(child);
1181 vfs_put(parent);
1182 vfs_put(expect);
1183 return rc;
1184}
1185
1186/** Unmount a file system
1187 *
1188 * @param mp File handle representing the mount-point
1189 *
1190 * @return EOK on success or a negative error code
1191 */
1192int vfs_unmount(int mp)
1193{
1194 async_exch_t *exch = vfs_exchange_begin();
1195 int rc = async_req_1_0(exch, VFS_IN_UNMOUNT, mp);
1196 vfs_exchange_end(exch);
1197 return rc;
1198}
1199
1200/** Unmount a file system
1201 *
1202 * @param mpp Mount-point path
1203 *
1204 * @return EOK on success or a negative error code
1205 */
1206int vfs_unmount_path(const char *mpp)
1207{
1208 int mp = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY);
1209 if (mp < 0)
1210 return mp;
1211
1212 int rc = vfs_unmount(mp);
1213 vfs_put(mp);
1214 return rc;
1215}
1216
1217/** Walk a path starting in a parent node
1218 *
1219 * @param parent File handle of the parent node where the walk starts
1220 * @param path Parent-relative path to be walked
1221 * @param flags Flags influencing the walk
1222 *
1223 * @retrun File handle representing the result on success or
1224 * a negative error code on error
1225 */
1226int vfs_walk(int parent, const char *path, int flags)
1227{
1228 async_exch_t *exch = vfs_exchange_begin();
1229
1230 ipc_call_t answer;
1231 aid_t req = async_send_2(exch, VFS_IN_WALK, parent, flags, &answer);
1232 sysarg_t rc = async_data_write_start(exch, path, str_size(path));
1233 vfs_exchange_end(exch);
1234
1235 sysarg_t rc_orig;
1236 async_wait_for(req, &rc_orig);
1237
1238 if (rc_orig != EOK)
1239 return (int) rc_orig;
1240
1241 if (rc != EOK)
1242 return (int) rc;
1243
1244 return (int) IPC_GET_ARG1(answer);
1245}
1246
1247/** Write data
1248 *
1249 * This function fails if it cannot write exactly @a len bytes to the file.
1250 *
1251 * @param file File handle to write to
1252 * @param[inout] pos Position to write to, updated by the actual bytes
1253 * written
1254 * @param buf Data, @a nbytes bytes long
1255 * @param nbytes Number of bytes to write
1256 * @param nwritten Place to store number of bytes written
1257 *
1258 * @return On success, EOK, @a *nwr is filled with number
1259 * of bytes written
1260 * @return On failure, an error code
1261 */
1262int vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte,
1263 size_t *nwritten)
1264{
1265 ssize_t cnt = 0;
1266 ssize_t nwr = 0;
1267 const uint8_t *bp = (uint8_t *) buf;
1268 int rc;
1269
1270 do {
1271 bp += cnt;
1272 nwr += cnt;
1273 *pos += cnt;
1274 rc = vfs_write_short(file, *pos, bp, nbyte - nwr, &cnt);
1275 } while (rc == EOK && ((ssize_t )nbyte - nwr - cnt) > 0);
1276
1277 if (rc != EOK) {
1278 *nwritten = nwr;
1279 return rc;
1280 }
1281
1282 nwr += cnt;
1283 *pos += cnt;
1284 *nwritten = nwr;
1285 return EOK;
1286}
1287
1288/** Write bytes to a file
1289 *
1290 * Write up to @a nbyte bytes from file. The actual number of bytes written
1291 * may be lower, but greater than zero.
1292 *
1293 * @param file File handle to write to
1294 * @param[in] pos Position to write to
1295 * @param buf Buffer to write to
1296 * @param nbyte Maximum number of bytes to write
1297 * @param[out] nread Actual number of bytes written (0 or more)
1298 *
1299 * @return EOK on success or a negative error code
1300 */
1301int vfs_write_short(int file, aoff64_t pos, const void *buf, size_t nbyte,
1302 ssize_t *nwritten)
1303{
1304 sysarg_t rc;
1305 ipc_call_t answer;
1306 aid_t req;
1307
1308 if (nbyte > DATA_XFER_LIMIT)
1309 nbyte = DATA_XFER_LIMIT;
1310
1311 async_exch_t *exch = vfs_exchange_begin();
1312
1313 req = async_send_3(exch, VFS_IN_WRITE, file, LOWER32(pos),
1314 UPPER32(pos), &answer);
1315 rc = async_data_write_start(exch, (void *) buf, nbyte);
1316
1317 vfs_exchange_end(exch);
1318
1319 if (rc == EOK)
1320 async_wait_for(req, &rc);
1321 else
1322 async_forget(req);
1323
1324 if (rc != EOK)
1325 return rc;
1326
1327 *nwritten = (ssize_t) IPC_GET_ARG1(answer);
1328 return EOK;
1329}
1330
1331/** @}
1332 */
Note: See TracBrowser for help on using the repository browser.