Home | History | Annotate | Line # | Download | only in librumpuser
rumpfiber_bio.c revision 1.1.6.2
      1  1.1.6.2  tls /*-
      2  1.1.6.2  tls  * Copyright (c) 2014 Antti Kantee.  All Rights Reserved.
      3  1.1.6.2  tls  *
      4  1.1.6.2  tls  * Redistribution and use in source and binary forms, with or without
      5  1.1.6.2  tls  * modification, are permitted provided that the following conditions
      6  1.1.6.2  tls  * are met:
      7  1.1.6.2  tls  * 1. Redistributions of source code must retain the above copyright
      8  1.1.6.2  tls  *    notice, this list of conditions and the following disclaimer.
      9  1.1.6.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1.6.2  tls  *    notice, this list of conditions and the following disclaimer in the
     11  1.1.6.2  tls  *    documentation and/or other materials provided with the distribution.
     12  1.1.6.2  tls  *
     13  1.1.6.2  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     14  1.1.6.2  tls  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  1.1.6.2  tls  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  1.1.6.2  tls  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     17  1.1.6.2  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  1.1.6.2  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     19  1.1.6.2  tls  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20  1.1.6.2  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  1.1.6.2  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  1.1.6.2  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  1.1.6.2  tls  * SUCH DAMAGE.
     24  1.1.6.2  tls  */
     25  1.1.6.2  tls 
     26  1.1.6.2  tls #include "rumpuser_port.h"
     27  1.1.6.2  tls 
     28  1.1.6.2  tls #if !defined(lint)
     29  1.1.6.2  tls __RCSID("$NetBSD: rumpfiber_bio.c,v 1.1.6.2 2014/08/20 00:02:21 tls Exp $");
     30  1.1.6.2  tls #endif /* !lint */
     31  1.1.6.2  tls 
     32  1.1.6.2  tls #include <sys/types.h>
     33  1.1.6.2  tls 
     34  1.1.6.2  tls #include <stdint.h>
     35  1.1.6.2  tls #include <unistd.h>
     36  1.1.6.2  tls 
     37  1.1.6.2  tls #include <rump/rumpuser.h>
     38  1.1.6.2  tls 
     39  1.1.6.2  tls #include "rumpuser_int.h"
     40  1.1.6.2  tls 
     41  1.1.6.2  tls void
     42  1.1.6.2  tls rumpuser_bio(int fd, int op, void *data, size_t dlen, int64_t doff,
     43  1.1.6.2  tls 	rump_biodone_fn biodone, void *bioarg)
     44  1.1.6.2  tls {
     45  1.1.6.2  tls 	ssize_t rv;
     46  1.1.6.2  tls 	int error = 0;
     47  1.1.6.2  tls 
     48  1.1.6.2  tls 	if (op & RUMPUSER_BIO_READ) {
     49  1.1.6.2  tls 		if ((rv = pread(fd, data, dlen, doff)) == -1)
     50  1.1.6.2  tls 			error = errno;
     51  1.1.6.2  tls 	} else {
     52  1.1.6.2  tls 		if ((rv = pwrite(fd, data, dlen, doff)) == -1)
     53  1.1.6.2  tls 			error = errno;
     54  1.1.6.2  tls 		if (error == 0 && (op & RUMPUSER_BIO_SYNC)) {
     55  1.1.6.2  tls #ifdef __NetBSD__
     56  1.1.6.2  tls 			fsync_range(fd, FDATASYNC, doff, dlen);
     57  1.1.6.2  tls #else
     58  1.1.6.2  tls 			fsync(fd);
     59  1.1.6.2  tls #endif
     60  1.1.6.2  tls 		}
     61  1.1.6.2  tls 	}
     62  1.1.6.2  tls 	if (rv == -1)
     63  1.1.6.2  tls 		rv = 0;
     64  1.1.6.2  tls 	biodone(bioarg, (size_t)rv, error);
     65  1.1.6.2  tls }
     66