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