Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_file.c revision 1.2
      1  1.2   pooka /*	$NetBSD: rumpuser_file.c,v 1.2 2014/08/24 14:37:31 pooka Exp $	*/
      2  1.2   pooka 
      3  1.1  justin /*
      4  1.1  justin  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
      5  1.1  justin  *
      6  1.1  justin  * Redistribution and use in source and binary forms, with or without
      7  1.1  justin  * modification, are permitted provided that the following conditions
      8  1.1  justin  * are met:
      9  1.1  justin  * 1. Redistributions of source code must retain the above copyright
     10  1.1  justin  *    notice, this list of conditions and the following disclaimer.
     11  1.1  justin  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  justin  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  justin  *    documentation and/or other materials provided with the distribution.
     14  1.1  justin  *
     15  1.1  justin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  justin  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  justin  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  justin  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  justin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  justin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  justin  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  justin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  justin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  justin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  justin  * SUCH DAMAGE.
     26  1.1  justin  */
     27  1.1  justin 
     28  1.1  justin /* NOTE this code will move to a new driver in the next hypercall revision */
     29  1.1  justin 
     30  1.1  justin #include "rumpuser_port.h"
     31  1.1  justin 
     32  1.1  justin #if !defined(lint)
     33  1.2   pooka __RCSID("$NetBSD: rumpuser_file.c,v 1.2 2014/08/24 14:37:31 pooka Exp $");
     34  1.1  justin #endif /* !lint */
     35  1.1  justin 
     36  1.1  justin #include <sys/ioctl.h>
     37  1.1  justin #include <sys/mman.h>
     38  1.1  justin #include <sys/uio.h>
     39  1.1  justin #include <sys/stat.h>
     40  1.1  justin #include <sys/time.h>
     41  1.1  justin #include <sys/types.h>
     42  1.1  justin 
     43  1.1  justin #ifdef __NetBSD__
     44  1.1  justin #include <sys/disk.h>
     45  1.1  justin #include <sys/disklabel.h>
     46  1.1  justin #include <sys/dkio.h>
     47  1.1  justin #endif
     48  1.1  justin 
     49  1.1  justin #if defined(__NetBSD__) || defined(__FreeBSD__) || \
     50  1.1  justin     defined(__DragonFly__) || defined(__APPLE__)
     51  1.1  justin #define	__BSD__
     52  1.1  justin #endif
     53  1.1  justin 
     54  1.1  justin #if defined(__BSD__)
     55  1.1  justin #include <sys/sysctl.h>
     56  1.1  justin #endif
     57  1.1  justin 
     58  1.1  justin #include <assert.h>
     59  1.1  justin #include <errno.h>
     60  1.1  justin #include <fcntl.h>
     61  1.1  justin #include <netdb.h>
     62  1.1  justin #include <stdarg.h>
     63  1.1  justin #include <stdint.h>
     64  1.1  justin #include <stdio.h>
     65  1.1  justin #include <stdlib.h>
     66  1.1  justin #include <string.h>
     67  1.1  justin #include <unistd.h>
     68  1.1  justin 
     69  1.1  justin #include <rump/rumpuser.h>
     70  1.1  justin 
     71  1.1  justin #include "rumpuser_int.h"
     72  1.1  justin 
     73  1.1  justin int
     74  1.1  justin rumpuser_getfileinfo(const char *path, uint64_t *sizep, int *ftp)
     75  1.1  justin {
     76  1.1  justin 	struct stat sb;
     77  1.1  justin 	uint64_t size = 0;
     78  1.1  justin 	int needsdev = 0, rv = 0, ft = 0;
     79  1.1  justin 	int fd = -1;
     80  1.1  justin 
     81  1.1  justin 	if (stat(path, &sb) == -1) {
     82  1.1  justin 		rv = errno;
     83  1.1  justin 		goto out;
     84  1.1  justin 	}
     85  1.1  justin 
     86  1.1  justin 	switch (sb.st_mode & S_IFMT) {
     87  1.1  justin 	case S_IFDIR:
     88  1.1  justin 		ft = RUMPUSER_FT_DIR;
     89  1.1  justin 		break;
     90  1.1  justin 	case S_IFREG:
     91  1.1  justin 		ft = RUMPUSER_FT_REG;
     92  1.1  justin 		break;
     93  1.1  justin 	case S_IFBLK:
     94  1.1  justin 		ft = RUMPUSER_FT_BLK;
     95  1.1  justin 		needsdev = 1;
     96  1.1  justin 		break;
     97  1.1  justin 	case S_IFCHR:
     98  1.1  justin 		ft = RUMPUSER_FT_CHR;
     99  1.1  justin 		needsdev = 1;
    100  1.1  justin 		break;
    101  1.1  justin 	default:
    102  1.1  justin 		ft = RUMPUSER_FT_OTHER;
    103  1.1  justin 		break;
    104  1.1  justin 	}
    105  1.1  justin 
    106  1.1  justin 	if (!needsdev) {
    107  1.1  justin 		size = sb.st_size;
    108  1.1  justin 	} else if (sizep) {
    109  1.1  justin 		/*
    110  1.1  justin 		 * Welcome to the jungle.  Of course querying the kernel
    111  1.1  justin 		 * for a device partition size is supposed to be far from
    112  1.1  justin 		 * trivial.  On NetBSD we use ioctl.  On $other platform
    113  1.1  justin 		 * we have a problem.  We try "the lseek trick" and just
    114  1.1  justin 		 * fail if that fails.  Platform specific code can later
    115  1.1  justin 		 * be written here if appropriate.
    116  1.1  justin 		 *
    117  1.1  justin 		 * On NetBSD we hope and pray that for block devices nobody
    118  1.1  justin 		 * else is holding them open, because otherwise the kernel
    119  1.1  justin 		 * will not permit us to open it.  Thankfully, this is
    120  1.1  justin 		 * usually called only in bootstrap and then we can
    121  1.1  justin 		 * forget about it.
    122  1.1  justin 		 */
    123  1.1  justin #ifndef __NetBSD__
    124  1.1  justin 		off_t off;
    125  1.1  justin 
    126  1.1  justin 		fd = open(path, O_RDONLY);
    127  1.1  justin 		if (fd == -1) {
    128  1.1  justin 			rv = errno;
    129  1.1  justin 			goto out;
    130  1.1  justin 		}
    131  1.1  justin 
    132  1.1  justin 		off = lseek(fd, 0, SEEK_END);
    133  1.1  justin 		if (off != 0) {
    134  1.1  justin 			size = off;
    135  1.1  justin 			goto out;
    136  1.1  justin 		}
    137  1.1  justin 		fprintf(stderr, "error: device size query not implemented on "
    138  1.1  justin 		    "this platform\n");
    139  1.1  justin 		rv = EOPNOTSUPP;
    140  1.1  justin 		goto out;
    141  1.1  justin #else
    142  1.1  justin 		struct disklabel lab;
    143  1.1  justin 		struct partition *parta;
    144  1.1  justin 		struct dkwedge_info dkw;
    145  1.1  justin 
    146  1.1  justin 		fd = open(path, O_RDONLY);
    147  1.1  justin 		if (fd == -1) {
    148  1.1  justin 			rv = errno;
    149  1.1  justin 			goto out;
    150  1.1  justin 		}
    151  1.1  justin 
    152  1.1  justin 		if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
    153  1.1  justin 			parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
    154  1.1  justin 			size = (uint64_t)lab.d_secsize * parta->p_size;
    155  1.1  justin 			goto out;
    156  1.1  justin 		}
    157  1.1  justin 
    158  1.1  justin 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    159  1.1  justin 			/*
    160  1.1  justin 			 * XXX: should use DIOCGDISKINFO to query
    161  1.1  justin 			 * sector size, but that requires proplib,
    162  1.1  justin 			 * so just don't bother for now.  it's nice
    163  1.1  justin 			 * that something as difficult as figuring out
    164  1.1  justin 			 * a partition's size has been made so easy.
    165  1.1  justin 			 */
    166  1.1  justin 			size = dkw.dkw_size << DEV_BSHIFT;
    167  1.1  justin 			goto out;
    168  1.1  justin 		}
    169  1.1  justin 
    170  1.1  justin 		rv = errno;
    171  1.1  justin #endif /* __NetBSD__ */
    172  1.1  justin 	}
    173  1.1  justin 
    174  1.1  justin  out:
    175  1.1  justin 	if (rv == 0 && sizep)
    176  1.1  justin 		*sizep = size;
    177  1.1  justin 	if (rv == 0 && ftp)
    178  1.1  justin 		*ftp = ft;
    179  1.1  justin 	if (fd != -1)
    180  1.1  justin 		close(fd);
    181  1.1  justin 
    182  1.1  justin 	ET(rv);
    183  1.1  justin }
    184  1.1  justin 
    185  1.1  justin int
    186  1.1  justin rumpuser_open(const char *path, int ruflags, int *fdp)
    187  1.1  justin {
    188  1.1  justin 	int fd, flags, rv;
    189  1.1  justin 
    190  1.1  justin 	switch (ruflags & RUMPUSER_OPEN_ACCMODE) {
    191  1.1  justin 	case RUMPUSER_OPEN_RDONLY:
    192  1.1  justin 		flags = O_RDONLY;
    193  1.1  justin 		break;
    194  1.1  justin 	case RUMPUSER_OPEN_WRONLY:
    195  1.1  justin 		flags = O_WRONLY;
    196  1.1  justin 		break;
    197  1.1  justin 	case RUMPUSER_OPEN_RDWR:
    198  1.1  justin 		flags = O_RDWR;
    199  1.1  justin 		break;
    200  1.1  justin 	default:
    201  1.1  justin 		rv = EINVAL;
    202  1.1  justin 		goto out;
    203  1.1  justin 	}
    204  1.1  justin 
    205  1.1  justin #define TESTSET(_ru_, _h_) if (ruflags & _ru_) flags |= _h_;
    206  1.1  justin 	TESTSET(RUMPUSER_OPEN_CREATE, O_CREAT);
    207  1.1  justin 	TESTSET(RUMPUSER_OPEN_EXCL, O_EXCL);
    208  1.1  justin #undef TESTSET
    209  1.1  justin 
    210  1.1  justin 	KLOCK_WRAP(fd = open(path, flags, 0644));
    211  1.1  justin 	if (fd == -1) {
    212  1.1  justin 		rv = errno;
    213  1.1  justin 	} else {
    214  1.1  justin 		*fdp = fd;
    215  1.1  justin 		rv = 0;
    216  1.1  justin 	}
    217  1.1  justin 
    218  1.1  justin  out:
    219  1.1  justin 	ET(rv);
    220  1.1  justin }
    221  1.1  justin 
    222  1.1  justin int
    223  1.1  justin rumpuser_close(int fd)
    224  1.1  justin {
    225  1.1  justin 	int nlocks;
    226  1.1  justin 
    227  1.1  justin 	rumpkern_unsched(&nlocks, NULL);
    228  1.1  justin 	fsync(fd);
    229  1.1  justin 	close(fd);
    230  1.1  justin 	rumpkern_sched(nlocks, NULL);
    231  1.1  justin 
    232  1.1  justin 	ET(0);
    233  1.1  justin }
    234  1.1  justin 
    235  1.1  justin /*
    236  1.1  justin  * Assume "struct rumpuser_iovec" and "struct iovec" are the same.
    237  1.1  justin  * If you encounter POSIX platforms where they aren't, add some
    238  1.1  justin  * translation for iovlen > 1.
    239  1.1  justin  */
    240  1.1  justin int
    241  1.1  justin rumpuser_iovread(int fd, struct rumpuser_iovec *ruiov, size_t iovlen,
    242  1.1  justin 	int64_t roff, size_t *retp)
    243  1.1  justin {
    244  1.1  justin 	struct iovec *iov = (struct iovec *)ruiov;
    245  1.1  justin 	off_t off = (off_t)roff;
    246  1.1  justin 	ssize_t nn;
    247  1.1  justin 	int rv;
    248  1.1  justin 
    249  1.1  justin 	if (off == RUMPUSER_IOV_NOSEEK) {
    250  1.1  justin 		KLOCK_WRAP(nn = readv(fd, iov, iovlen));
    251  1.1  justin 	} else {
    252  1.1  justin 		int nlocks;
    253  1.1  justin 
    254  1.1  justin 		rumpkern_unsched(&nlocks, NULL);
    255  1.1  justin 		if (lseek(fd, off, SEEK_SET) == off) {
    256  1.1  justin 			nn = readv(fd, iov, iovlen);
    257  1.1  justin 		} else {
    258  1.1  justin 			nn = -1;
    259  1.1  justin 		}
    260  1.1  justin 		rumpkern_sched(nlocks, NULL);
    261  1.1  justin 	}
    262  1.1  justin 
    263  1.1  justin 	if (nn == -1) {
    264  1.1  justin 		rv = errno;
    265  1.1  justin 	} else {
    266  1.1  justin 		*retp = (size_t)nn;
    267  1.1  justin 		rv = 0;
    268  1.1  justin 	}
    269  1.1  justin 
    270  1.1  justin 	ET(rv);
    271  1.1  justin }
    272  1.1  justin 
    273  1.1  justin int
    274  1.1  justin rumpuser_iovwrite(int fd, const struct rumpuser_iovec *ruiov, size_t iovlen,
    275  1.1  justin 	int64_t roff, size_t *retp)
    276  1.1  justin {
    277  1.1  justin 	const struct iovec *iov = (const struct iovec *)ruiov;
    278  1.1  justin 	off_t off = (off_t)roff;
    279  1.1  justin 	ssize_t nn;
    280  1.1  justin 	int rv;
    281  1.1  justin 
    282  1.1  justin 	if (off == RUMPUSER_IOV_NOSEEK) {
    283  1.1  justin 		KLOCK_WRAP(nn = writev(fd, iov, iovlen));
    284  1.1  justin 	} else {
    285  1.1  justin 		int nlocks;
    286  1.1  justin 
    287  1.1  justin 		rumpkern_unsched(&nlocks, NULL);
    288  1.1  justin 		if (lseek(fd, off, SEEK_SET) == off) {
    289  1.1  justin 			nn = writev(fd, iov, iovlen);
    290  1.1  justin 		} else {
    291  1.1  justin 			nn = -1;
    292  1.1  justin 		}
    293  1.1  justin 		rumpkern_sched(nlocks, NULL);
    294  1.1  justin 	}
    295  1.1  justin 
    296  1.1  justin 	if (nn == -1) {
    297  1.1  justin 		rv = errno;
    298  1.1  justin 	} else {
    299  1.1  justin 		*retp = (size_t)nn;
    300  1.1  justin 		rv = 0;
    301  1.1  justin 	}
    302  1.1  justin 
    303  1.1  justin 	ET(rv);
    304  1.1  justin }
    305  1.1  justin 
    306  1.1  justin int
    307  1.1  justin rumpuser_syncfd(int fd, int flags, uint64_t start, uint64_t len)
    308  1.1  justin {
    309  1.1  justin 	int rv = 0;
    310  1.1  justin 
    311  1.1  justin 	/*
    312  1.1  justin 	 * For now, assume fd is regular file and does not care
    313  1.1  justin 	 * about read syncing
    314  1.1  justin 	 */
    315  1.1  justin 	if ((flags & RUMPUSER_SYNCFD_BOTH) == 0) {
    316  1.1  justin 		rv = EINVAL;
    317  1.1  justin 		goto out;
    318  1.1  justin 	}
    319  1.1  justin 	if ((flags & RUMPUSER_SYNCFD_WRITE) == 0) {
    320  1.1  justin 		rv = 0;
    321  1.1  justin 		goto out;
    322  1.1  justin 	}
    323  1.1  justin 
    324  1.1  justin #ifdef __NetBSD__
    325  1.1  justin 	{
    326  1.1  justin 	int fsflags = FDATASYNC;
    327  1.1  justin 
    328  1.1  justin 	if (fsflags & RUMPUSER_SYNCFD_SYNC)
    329  1.1  justin 		fsflags |= FDISKSYNC;
    330  1.1  justin 	if (fsync_range(fd, fsflags, start, len) == -1)
    331  1.1  justin 		rv = errno;
    332  1.1  justin 	}
    333  1.1  justin #else
    334  1.1  justin 	/* el-simplo */
    335  1.1  justin 	if (fsync(fd) == -1)
    336  1.1  justin 		rv = errno;
    337  1.1  justin #endif
    338  1.1  justin 
    339  1.1  justin  out:
    340  1.1  justin 	ET(rv);
    341  1.1  justin }
    342