1 1.4 uch /* $NetBSD: v7fs_io_user.c,v 1.4 2011/08/08 11:42:30 uch Exp $ */ 2 1.1 uch 3 1.1 uch /*- 4 1.1 uch * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 1.1 uch * All rights reserved. 6 1.1 uch * 7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation 8 1.1 uch * by UCHIYAMA Yasushi. 9 1.1 uch * 10 1.1 uch * Redistribution and use in source and binary forms, with or without 11 1.1 uch * modification, are permitted provided that the following conditions 12 1.1 uch * are met: 13 1.1 uch * 1. Redistributions of source code must retain the above copyright 14 1.1 uch * notice, this list of conditions and the following disclaimer. 15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 uch * notice, this list of conditions and the following disclaimer in the 17 1.1 uch * documentation and/or other materials provided with the distribution. 18 1.1 uch * 19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 uch * POSSIBILITY OF SUCH DAMAGE. 30 1.1 uch */ 31 1.1 uch 32 1.2 apb #if HAVE_NBTOOL_CONFIG_H 33 1.2 apb #include "nbtool_config.h" 34 1.2 apb #endif 35 1.2 apb 36 1.1 uch #include <sys/cdefs.h> 37 1.1 uch #ifndef lint 38 1.4 uch __RCSID("$NetBSD: v7fs_io_user.c,v 1.4 2011/08/08 11:42:30 uch Exp $"); 39 1.1 uch #endif /* not lint */ 40 1.1 uch 41 1.1 uch #include <stdio.h> 42 1.1 uch #include <stdlib.h> 43 1.1 uch #include <string.h> 44 1.1 uch #include <errno.h> 45 1.1 uch #include <unistd.h> 46 1.1 uch #include <err.h> 47 1.1 uch #include <sys/mman.h> 48 1.1 uch #include "v7fs.h" 49 1.1 uch #include "v7fs_endian.h" 50 1.1 uch #include "v7fs_impl.h" 51 1.1 uch 52 1.1 uch #ifdef V7FS_IO_DEBUG 53 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args) 54 1.1 uch #else 55 1.1 uch #define DPRINTF(fmt, args...) ((void)0) 56 1.1 uch #endif 57 1.1 uch 58 1.1 uch struct local_io { 59 1.1 uch int fd; 60 1.1 uch size_t size; 61 1.1 uch size_t blksz; 62 1.1 uch uint8_t *addr; 63 1.1 uch } local; 64 1.1 uch 65 1.1 uch static bool read_sector(void *, uint8_t *, daddr_t); 66 1.1 uch static bool write_sector(void *, uint8_t *, daddr_t); 67 1.1 uch static bool read_mmap(void *, uint8_t *, daddr_t); 68 1.1 uch static bool write_mmap(void *, uint8_t *, daddr_t); 69 1.1 uch 70 1.1 uch int 71 1.1 uch v7fs_io_init(struct v7fs_self **fs, const struct v7fs_mount_device *mount, 72 1.1 uch size_t block_size) 73 1.1 uch { 74 1.1 uch struct v7fs_self *p; 75 1.1 uch 76 1.1 uch if (!(p = (struct v7fs_self *)malloc(sizeof(*p)))) 77 1.1 uch return ENOMEM; 78 1.1 uch memset(p, 0, sizeof(*p)); 79 1.1 uch 80 1.1 uch /* Endian */ 81 1.1 uch p->endian = mount->endian; 82 1.1 uch #ifdef V7FS_EI 83 1.1 uch v7fs_endian_init(p); 84 1.1 uch #endif 85 1.1 uch local.blksz = block_size; 86 1.1 uch local.fd = mount->device.fd; 87 1.1 uch local.size = mount->sectors * block_size; 88 1.1 uch local.addr = mmap(NULL, local.size, PROT_READ | PROT_WRITE | PROT_NONE, 89 1.1 uch MAP_FILE | MAP_SHARED/*writeback*/, local.fd, 0); 90 1.1 uch if (local.addr == MAP_FAILED) { 91 1.1 uch local.addr = 0; 92 1.1 uch p->io.read = read_sector; 93 1.1 uch p->io.write = write_sector; 94 1.1 uch } else { 95 1.1 uch DPRINTF("mmaped addr=%p\n", local.addr); 96 1.1 uch p->io.read = read_mmap; 97 1.1 uch p->io.write = write_mmap; 98 1.1 uch } 99 1.1 uch 100 1.1 uch p->io.cookie = &local; 101 1.1 uch *fs = p; 102 1.1 uch 103 1.1 uch return 0; 104 1.1 uch } 105 1.1 uch 106 1.1 uch void 107 1.1 uch v7fs_io_fini(struct v7fs_self *fs) 108 1.1 uch { 109 1.1 uch struct local_io *lio = (struct local_io *)fs->io.cookie; 110 1.1 uch 111 1.1 uch if (lio->addr) { 112 1.1 uch if (munmap(lio->addr, lio->size) != 0) 113 1.1 uch warn(0); 114 1.1 uch } 115 1.1 uch fsync(lio->fd); 116 1.1 uch 117 1.1 uch free(fs); 118 1.1 uch } 119 1.1 uch 120 1.1 uch static bool 121 1.1 uch read_sector(void *ctx, uint8_t *buf, daddr_t sector) 122 1.1 uch { 123 1.1 uch struct local_io *lio = (struct local_io *)ctx; 124 1.1 uch size_t blksz = lio->blksz; 125 1.1 uch int fd = lio->fd; 126 1.1 uch 127 1.3 dholland if ((lseek(fd, (off_t)sector * blksz, SEEK_SET) < 0) || 128 1.1 uch (read(fd, buf, blksz) < (ssize_t)blksz)) { 129 1.1 uch warn("sector=%ld\n", (long)sector); 130 1.1 uch return false; 131 1.1 uch } 132 1.1 uch 133 1.1 uch return true; 134 1.1 uch } 135 1.1 uch 136 1.1 uch static bool 137 1.1 uch write_sector(void *ctx, uint8_t *buf, daddr_t sector) 138 1.1 uch { 139 1.1 uch struct local_io *lio = (struct local_io *)ctx; 140 1.1 uch size_t blksz = lio->blksz; 141 1.1 uch int fd = lio->fd; 142 1.1 uch 143 1.3 dholland if ((lseek(fd, (off_t)sector * blksz, SEEK_SET) < 0) || 144 1.1 uch (write(fd, buf, blksz) < (ssize_t)blksz)) { 145 1.1 uch warn("sector=%ld\n", (long)sector); 146 1.1 uch return false; 147 1.1 uch } 148 1.1 uch 149 1.1 uch return true; 150 1.1 uch } 151 1.1 uch 152 1.1 uch static bool 153 1.1 uch read_mmap(void *ctx, uint8_t *buf, daddr_t sector) 154 1.1 uch { 155 1.1 uch struct local_io *lio = (struct local_io *)ctx; 156 1.1 uch size_t blksz = lio->blksz; 157 1.1 uch 158 1.1 uch memcpy(buf, lio->addr + sector * blksz, blksz); 159 1.1 uch 160 1.1 uch return true; 161 1.1 uch } 162 1.1 uch 163 1.1 uch static bool 164 1.1 uch write_mmap(void *ctx, uint8_t *buf, daddr_t sector) 165 1.1 uch { 166 1.1 uch struct local_io *lio = (struct local_io *)ctx; 167 1.1 uch size_t blksz = lio->blksz; 168 1.1 uch 169 1.1 uch memcpy(lio->addr + sector * blksz, buf, blksz); 170 1.1 uch 171 1.1 uch return true; 172 1.1 uch } 173