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