source: mainline/uspace/lib/c/generic/vfs/vfs.c@ 79ae36dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 15.5 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/canonify.h>
36#include <vfs/vfs.h>
37#include <vfs/vfs_sess.h>
38#include <macros.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <dirent.h>
42#include <fcntl.h>
43#include <stdio.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <ipc/services.h>
47#include <ns.h>
48#include <async.h>
49#include <fibril_synch.h>
50#include <errno.h>
51#include <assert.h>
52#include <str.h>
53#include <devmap.h>
54#include <ipc/vfs.h>
55#include <ipc/devmap.h>
56
57static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
58static async_sess_t *vfs_sess = NULL;
59
60static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
61
62static int cwd_fd = -1;
63static char *cwd_path = NULL;
64static size_t cwd_size = 0;
65
66/** Start an async exchange on the VFS session.
67 *
68 * @return New exchange.
69 *
70 */
71static async_exch_t *vfs_exchange_begin(void)
72{
73 fibril_mutex_lock(&vfs_mutex);
74
75 while (vfs_sess == NULL)
76 vfs_sess = service_connect_blocking(EXCHANGE_PARALLEL, SERVICE_VFS,
77 0, 0);
78
79 fibril_mutex_unlock(&vfs_mutex);
80
81 return async_exchange_begin(vfs_sess);
82}
83
84/** Finish an async exchange on the VFS session.
85 *
86 * @param exch Exchange to be finished.
87 *
88 */
89static void vfs_exchange_end(async_exch_t *exch)
90{
91 async_exchange_end(exch);
92}
93
94char *absolutize(const char *path, size_t *retlen)
95{
96 char *ncwd_path;
97 char *ncwd_path_nc;
98
99 fibril_mutex_lock(&cwd_mutex);
100 size_t size = str_size(path);
101 if (*path != '/') {
102 if (!cwd_path) {
103 fibril_mutex_unlock(&cwd_mutex);
104 return NULL;
105 }
106 ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
107 if (!ncwd_path_nc) {
108 fibril_mutex_unlock(&cwd_mutex);
109 return NULL;
110 }
111 str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
112 ncwd_path_nc[cwd_size] = '/';
113 ncwd_path_nc[cwd_size + 1] = '\0';
114 } else {
115 ncwd_path_nc = malloc(size + 1);
116 if (!ncwd_path_nc) {
117 fibril_mutex_unlock(&cwd_mutex);
118 return NULL;
119 }
120 ncwd_path_nc[0] = '\0';
121 }
122 str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
123 ncwd_path = canonify(ncwd_path_nc, retlen);
124 if (!ncwd_path) {
125 fibril_mutex_unlock(&cwd_mutex);
126 free(ncwd_path_nc);
127 return NULL;
128 }
129 /*
130 * We need to clone ncwd_path because canonify() works in-place and thus
131 * the address in ncwd_path need not be the same as ncwd_path_nc, even
132 * though they both point into the same dynamically allocated buffer.
133 */
134 ncwd_path = str_dup(ncwd_path);
135 free(ncwd_path_nc);
136 if (!ncwd_path) {
137 fibril_mutex_unlock(&cwd_mutex);
138 return NULL;
139 }
140 fibril_mutex_unlock(&cwd_mutex);
141 return ncwd_path;
142}
143
144int mount(const char *fs_name, const char *mp, const char *fqdn,
145 const char *opts, unsigned int flags)
146{
147 int null_id = -1;
148 char null[DEVMAP_NAME_MAXLEN];
149
150 if (str_cmp(fqdn, "") == 0) {
151 /* No device specified, create a fresh
152 null/%d device instead */
153 null_id = devmap_null_create();
154
155 if (null_id == -1)
156 return ENOMEM;
157
158 snprintf(null, DEVMAP_NAME_MAXLEN, "null/%d", null_id);
159 fqdn = null;
160 }
161
162 devmap_handle_t devmap_handle;
163 int res = devmap_device_get_handle(fqdn, &devmap_handle, flags);
164 if (res != EOK) {
165 if (null_id != -1)
166 devmap_null_destroy(null_id);
167
168 return res;
169 }
170
171 size_t mpa_size;
172 char *mpa = absolutize(mp, &mpa_size);
173 if (!mpa) {
174 if (null_id != -1)
175 devmap_null_destroy(null_id);
176
177 return ENOMEM;
178 }
179
180 async_exch_t *exch = vfs_exchange_begin();
181
182 sysarg_t rc_orig;
183 aid_t req = async_send_2(exch, VFS_IN_MOUNT, devmap_handle, flags, NULL);
184 sysarg_t rc = async_data_write_start(exch, (void *) mpa, mpa_size);
185 if (rc != EOK) {
186 vfs_exchange_end(exch);
187 free(mpa);
188 async_wait_for(req, &rc_orig);
189
190 if (null_id != -1)
191 devmap_null_destroy(null_id);
192
193 if (rc_orig == EOK)
194 return (int) rc;
195 else
196 return (int) rc_orig;
197 }
198
199 rc = async_data_write_start(exch, (void *) opts, str_size(opts));
200 if (rc != EOK) {
201 vfs_exchange_end(exch);
202 free(mpa);
203 async_wait_for(req, &rc_orig);
204
205 if (null_id != -1)
206 devmap_null_destroy(null_id);
207
208 if (rc_orig == EOK)
209 return (int) rc;
210 else
211 return (int) rc_orig;
212 }
213
214 rc = async_data_write_start(exch, (void *) fs_name, str_size(fs_name));
215 if (rc != EOK) {
216 vfs_exchange_end(exch);
217 free(mpa);
218 async_wait_for(req, &rc_orig);
219
220 if (null_id != -1)
221 devmap_null_destroy(null_id);
222
223 if (rc_orig == EOK)
224 return (int) rc;
225 else
226 return (int) rc_orig;
227 }
228
229 /* Ask VFS whether it likes fs_name. */
230 rc = async_req_0_0(exch, VFS_IN_PING);
231 if (rc != EOK) {
232 vfs_exchange_end(exch);
233 free(mpa);
234 async_wait_for(req, &rc_orig);
235
236 if (null_id != -1)
237 devmap_null_destroy(null_id);
238
239 if (rc_orig == EOK)
240 return (int) rc;
241 else
242 return (int) rc_orig;
243 }
244
245 vfs_exchange_end(exch);
246 free(mpa);
247 async_wait_for(req, &rc);
248
249 if ((rc != EOK) && (null_id != -1))
250 devmap_null_destroy(null_id);
251
252 return (int) rc;
253}
254
255int unmount(const char *mp)
256{
257 sysarg_t rc;
258 sysarg_t rc_orig;
259 aid_t req;
260 size_t mpa_size;
261 char *mpa;
262
263 mpa = absolutize(mp, &mpa_size);
264 if (!mpa)
265 return ENOMEM;
266
267 async_exch_t *exch = vfs_exchange_begin();
268
269 req = async_send_0(exch, VFS_IN_UNMOUNT, NULL);
270 rc = async_data_write_start(exch, (void *) mpa, mpa_size);
271 if (rc != EOK) {
272 vfs_exchange_end(exch);
273 free(mpa);
274 async_wait_for(req, &rc_orig);
275 if (rc_orig == EOK)
276 return (int) rc;
277 else
278 return (int) rc_orig;
279 }
280
281
282 vfs_exchange_end(exch);
283 free(mpa);
284 async_wait_for(req, &rc);
285
286 return (int) rc;
287}
288
289static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag)
290{
291 async_exch_t *exch = vfs_exchange_begin();
292
293 ipc_call_t answer;
294 aid_t req = async_send_3(exch, VFS_IN_OPEN, lflag, oflag, 0, &answer);
295 sysarg_t rc = async_data_write_start(exch, abs, abs_size);
296
297 if (rc != EOK) {
298 vfs_exchange_end(exch);
299
300 sysarg_t rc_orig;
301 async_wait_for(req, &rc_orig);
302
303 if (rc_orig == EOK)
304 return (int) rc;
305 else
306 return (int) rc_orig;
307 }
308
309 vfs_exchange_end(exch);
310 async_wait_for(req, &rc);
311
312 if (rc != EOK)
313 return (int) rc;
314
315 return (int) IPC_GET_ARG1(answer);
316}
317
318int open(const char *path, int oflag, ...)
319{
320 size_t abs_size;
321 char *abs = absolutize(path, &abs_size);
322 if (!abs)
323 return ENOMEM;
324
325 int ret = open_internal(abs, abs_size, L_FILE, oflag);
326 free(abs);
327
328 return ret;
329}
330
331int open_node(fdi_node_t *node, int oflag)
332{
333 async_exch_t *exch = vfs_exchange_begin();
334
335 ipc_call_t answer;
336 aid_t req = async_send_4(exch, VFS_IN_OPEN_NODE, node->fs_handle,
337 node->devmap_handle, node->index, oflag, &answer);
338
339 vfs_exchange_end(exch);
340
341 sysarg_t rc;
342 async_wait_for(req, &rc);
343
344 if (rc != EOK)
345 return (int) rc;
346
347 return (int) IPC_GET_ARG1(answer);
348}
349
350int close(int fildes)
351{
352 sysarg_t rc;
353
354 async_exch_t *exch = vfs_exchange_begin();
355 rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
356 vfs_exchange_end(exch);
357
358 return (int) rc;
359}
360
361ssize_t read(int fildes, void *buf, size_t nbyte)
362{
363 sysarg_t rc;
364 ipc_call_t answer;
365 aid_t req;
366
367 async_exch_t *exch = vfs_exchange_begin();
368
369 req = async_send_1(exch, VFS_IN_READ, fildes, &answer);
370 rc = async_data_read_start(exch, (void *)buf, nbyte);
371 if (rc != EOK) {
372 vfs_exchange_end(exch);
373
374 sysarg_t rc_orig;
375 async_wait_for(req, &rc_orig);
376
377 if (rc_orig == EOK)
378 return (ssize_t) rc;
379 else
380 return (ssize_t) rc_orig;
381 }
382 vfs_exchange_end(exch);
383 async_wait_for(req, &rc);
384 if (rc == EOK)
385 return (ssize_t) IPC_GET_ARG1(answer);
386 else
387 return rc;
388}
389
390ssize_t write(int fildes, const void *buf, size_t nbyte)
391{
392 sysarg_t rc;
393 ipc_call_t answer;
394 aid_t req;
395
396 async_exch_t *exch = vfs_exchange_begin();
397
398 req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer);
399 rc = async_data_write_start(exch, (void *)buf, nbyte);
400 if (rc != EOK) {
401 vfs_exchange_end(exch);
402
403 sysarg_t rc_orig;
404 async_wait_for(req, &rc_orig);
405
406 if (rc_orig == EOK)
407 return (ssize_t) rc;
408 else
409 return (ssize_t) rc_orig;
410 }
411 vfs_exchange_end(exch);
412 async_wait_for(req, &rc);
413 if (rc == EOK)
414 return (ssize_t) IPC_GET_ARG1(answer);
415 else
416 return -1;
417}
418
419int fsync(int fildes)
420{
421 async_exch_t *exch = vfs_exchange_begin();
422 sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
423 vfs_exchange_end(exch);
424
425 return (int) rc;
426}
427
428off64_t lseek(int fildes, off64_t offset, int whence)
429{
430 async_exch_t *exch = vfs_exchange_begin();
431
432 sysarg_t newoff_lo;
433 sysarg_t newoff_hi;
434 sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,
435 LOWER32(offset), UPPER32(offset), whence,
436 &newoff_lo, &newoff_hi);
437
438 vfs_exchange_end(exch);
439
440 if (rc != EOK)
441 return (off64_t) -1;
442
443 return (off64_t) MERGE_LOUP32(newoff_lo, newoff_hi);
444}
445
446int ftruncate(int fildes, aoff64_t length)
447{
448 sysarg_t rc;
449
450 async_exch_t *exch = vfs_exchange_begin();
451 rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
452 LOWER32(length), UPPER32(length));
453 vfs_exchange_end(exch);
454
455 return (int) rc;
456}
457
458int fstat(int fildes, struct stat *stat)
459{
460 sysarg_t rc;
461 aid_t req;
462
463 async_exch_t *exch = vfs_exchange_begin();
464
465 req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
466 rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
467 if (rc != EOK) {
468 vfs_exchange_end(exch);
469
470 sysarg_t rc_orig;
471 async_wait_for(req, &rc_orig);
472
473 if (rc_orig == EOK)
474 return (ssize_t) rc;
475 else
476 return (ssize_t) rc_orig;
477 }
478 vfs_exchange_end(exch);
479 async_wait_for(req, &rc);
480
481 return rc;
482}
483
484int stat(const char *path, struct stat *stat)
485{
486 sysarg_t rc;
487 sysarg_t rc_orig;
488 aid_t req;
489
490 size_t pa_size;
491 char *pa = absolutize(path, &pa_size);
492 if (!pa)
493 return ENOMEM;
494
495 async_exch_t *exch = vfs_exchange_begin();
496
497 req = async_send_0(exch, VFS_IN_STAT, NULL);
498 rc = async_data_write_start(exch, pa, pa_size);
499 if (rc != EOK) {
500 vfs_exchange_end(exch);
501 free(pa);
502 async_wait_for(req, &rc_orig);
503 if (rc_orig == EOK)
504 return (int) rc;
505 else
506 return (int) rc_orig;
507 }
508 rc = async_data_read_start(exch, stat, sizeof(struct stat));
509 if (rc != EOK) {
510 vfs_exchange_end(exch);
511 free(pa);
512 async_wait_for(req, &rc_orig);
513 if (rc_orig == EOK)
514 return (int) rc;
515 else
516 return (int) rc_orig;
517 }
518 vfs_exchange_end(exch);
519 free(pa);
520 async_wait_for(req, &rc);
521 return rc;
522}
523
524DIR *opendir(const char *dirname)
525{
526 DIR *dirp = malloc(sizeof(DIR));
527 if (!dirp)
528 return NULL;
529
530 size_t abs_size;
531 char *abs = absolutize(dirname, &abs_size);
532 if (!abs) {
533 free(dirp);
534 return NULL;
535 }
536
537 int ret = open_internal(abs, abs_size, L_DIRECTORY, 0);
538 free(abs);
539
540 if (ret < 0) {
541 free(dirp);
542 return NULL;
543 }
544
545 dirp->fd = ret;
546 return dirp;
547}
548
549struct dirent *readdir(DIR *dirp)
550{
551 ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
552 if (len <= 0)
553 return NULL;
554 return &dirp->res;
555}
556
557void rewinddir(DIR *dirp)
558{
559 (void) lseek(dirp->fd, 0, SEEK_SET);
560}
561
562int closedir(DIR *dirp)
563{
564 (void) close(dirp->fd);
565 free(dirp);
566 return 0;
567}
568
569int mkdir(const char *path, mode_t mode)
570{
571 sysarg_t rc;
572 aid_t req;
573
574 size_t pa_size;
575 char *pa = absolutize(path, &pa_size);
576 if (!pa)
577 return ENOMEM;
578
579 async_exch_t *exch = vfs_exchange_begin();
580
581 req = async_send_1(exch, VFS_IN_MKDIR, mode, NULL);
582 rc = async_data_write_start(exch, pa, pa_size);
583 if (rc != EOK) {
584 vfs_exchange_end(exch);
585 free(pa);
586
587 sysarg_t rc_orig;
588 async_wait_for(req, &rc_orig);
589
590 if (rc_orig == EOK)
591 return (int) rc;
592 else
593 return (int) rc_orig;
594 }
595 vfs_exchange_end(exch);
596 free(pa);
597 async_wait_for(req, &rc);
598 return rc;
599}
600
601static int _unlink(const char *path, int lflag)
602{
603 sysarg_t rc;
604 aid_t req;
605
606 size_t pa_size;
607 char *pa = absolutize(path, &pa_size);
608 if (!pa)
609 return ENOMEM;
610
611 async_exch_t *exch = vfs_exchange_begin();
612
613 req = async_send_0(exch, VFS_IN_UNLINK, NULL);
614 rc = async_data_write_start(exch, pa, pa_size);
615 if (rc != EOK) {
616 vfs_exchange_end(exch);
617 free(pa);
618
619 sysarg_t rc_orig;
620 async_wait_for(req, &rc_orig);
621
622 if (rc_orig == EOK)
623 return (int) rc;
624 else
625 return (int) rc_orig;
626 }
627 vfs_exchange_end(exch);
628 free(pa);
629 async_wait_for(req, &rc);
630 return rc;
631}
632
633int unlink(const char *path)
634{
635 return _unlink(path, L_NONE);
636}
637
638int rmdir(const char *path)
639{
640 return _unlink(path, L_DIRECTORY);
641}
642
643int rename(const char *old, const char *new)
644{
645 sysarg_t rc;
646 sysarg_t rc_orig;
647 aid_t req;
648
649 size_t olda_size;
650 char *olda = absolutize(old, &olda_size);
651 if (!olda)
652 return ENOMEM;
653
654 size_t newa_size;
655 char *newa = absolutize(new, &newa_size);
656 if (!newa) {
657 free(olda);
658 return ENOMEM;
659 }
660
661 async_exch_t *exch = vfs_exchange_begin();
662
663 req = async_send_0(exch, VFS_IN_RENAME, NULL);
664 rc = async_data_write_start(exch, olda, olda_size);
665 if (rc != EOK) {
666 vfs_exchange_end(exch);
667 free(olda);
668 free(newa);
669 async_wait_for(req, &rc_orig);
670 if (rc_orig == EOK)
671 return (int) rc;
672 else
673 return (int) rc_orig;
674 }
675 rc = async_data_write_start(exch, newa, newa_size);
676 if (rc != EOK) {
677 vfs_exchange_end(exch);
678 free(olda);
679 free(newa);
680 async_wait_for(req, &rc_orig);
681 if (rc_orig == EOK)
682 return (int) rc;
683 else
684 return (int) rc_orig;
685 }
686 vfs_exchange_end(exch);
687 free(olda);
688 free(newa);
689 async_wait_for(req, &rc);
690 return rc;
691}
692
693int chdir(const char *path)
694{
695 size_t abs_size;
696 char *abs = absolutize(path, &abs_size);
697 if (!abs)
698 return ENOMEM;
699
700 int fd = open_internal(abs, abs_size, L_DIRECTORY, O_DESC);
701
702 if (fd < 0) {
703 free(abs);
704 return ENOENT;
705 }
706
707 fibril_mutex_lock(&cwd_mutex);
708
709 if (cwd_fd >= 0)
710 close(cwd_fd);
711
712
713 if (cwd_path)
714 free(cwd_path);
715
716 cwd_fd = fd;
717 cwd_path = abs;
718 cwd_size = abs_size;
719
720 fibril_mutex_unlock(&cwd_mutex);
721 return EOK;
722}
723
724char *getcwd(char *buf, size_t size)
725{
726 if (size == 0)
727 return NULL;
728
729 fibril_mutex_lock(&cwd_mutex);
730
731 if ((cwd_size == 0) || (size < cwd_size + 1)) {
732 fibril_mutex_unlock(&cwd_mutex);
733 return NULL;
734 }
735
736 str_cpy(buf, size, cwd_path);
737 fibril_mutex_unlock(&cwd_mutex);
738
739 return buf;
740}
741
742async_sess_t *fd_session(exch_mgmt_t mgmt, int fildes)
743{
744 struct stat stat;
745 int rc = fstat(fildes, &stat);
746 if (rc != 0) {
747 errno = rc;
748 return NULL;
749 }
750
751 if (!stat.device) {
752 errno = ENOENT;
753 return NULL;
754 }
755
756 return devmap_device_connect(mgmt, stat.device, 0);
757}
758
759int fd_node(int fildes, fdi_node_t *node)
760{
761 struct stat stat;
762 int rc = fstat(fildes, &stat);
763
764 if (rc == EOK) {
765 node->fs_handle = stat.fs_handle;
766 node->devmap_handle = stat.devmap_handle;
767 node->index = stat.index;
768 }
769
770 return rc;
771}
772
773int dup2(int oldfd, int newfd)
774{
775 async_exch_t *exch = vfs_exchange_begin();
776
777 sysarg_t ret;
778 sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
779
780 vfs_exchange_end(exch);
781
782 if (rc == EOK)
783 return (int) ret;
784
785 return (int) rc;
786}
787
788/** @}
789 */
Note: See TracBrowser for help on using the repository browser.