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