1 /* $NetBSD: v7fs_io_user.c,v 1.4 2011/08/08 11:42:30 uch Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: v7fs_io_user.c,v 1.4 2011/08/08 11:42:30 uch Exp $");
49 #include "v7fs_endian.h"
50 #include "v7fs_impl.h"
53 #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
55 #define DPRINTF(fmt, args...) ((void)0)
65 static bool read_sector(void *, uint8_t *, daddr_t
);
66 static bool write_sector(void *, uint8_t *, daddr_t
);
67 static bool read_mmap(void *, uint8_t *, daddr_t
);
68 static bool write_mmap(void *, uint8_t *, daddr_t
);
71 v7fs_io_init(struct v7fs_self
**fs
, const struct v7fs_mount_device
*mount
,
76 if (!(p
= (struct v7fs_self
*)malloc(sizeof(*p
))))
78 memset(p
, 0, sizeof(*p
));
81 p
->endian
= mount
->endian
;
85 local
.blksz
= block_size
;
86 local
.fd
= mount
->device
.fd
;
87 local
.size
= mount
->sectors
* block_size
;
88 local
.addr
= mmap(NULL
, local
.size
, PROT_READ
| PROT_WRITE
| PROT_NONE
,
89 MAP_FILE
| MAP_SHARED
/*writeback*/, local
.fd
, 0);
90 if (local
.addr
== MAP_FAILED
) {
92 p
->io
.read
= read_sector
;
93 p
->io
.write
= write_sector
;
95 DPRINTF("mmaped addr=%p\n", local
.addr
);
96 p
->io
.read
= read_mmap
;
97 p
->io
.write
= write_mmap
;
100 p
->io
.cookie
= &local
;
107 v7fs_io_fini(struct v7fs_self
*fs
)
109 struct local_io
*lio
= (struct local_io
*)fs
->io
.cookie
;
112 if (munmap(lio
->addr
, lio
->size
) != 0)
121 read_sector(void *ctx
, uint8_t *buf
, daddr_t sector
)
123 struct local_io
*lio
= (struct local_io
*)ctx
;
124 size_t blksz
= lio
->blksz
;
127 if ((lseek(fd
, (off_t
)sector
* blksz
, SEEK_SET
) < 0) ||
128 (read(fd
, buf
, blksz
) < (ssize_t
)blksz
)) {
129 warn("sector=%ld\n", (long)sector
);
137 write_sector(void *ctx
, uint8_t *buf
, daddr_t sector
)
139 struct local_io
*lio
= (struct local_io
*)ctx
;
140 size_t blksz
= lio
->blksz
;
143 if ((lseek(fd
, (off_t
)sector
* blksz
, SEEK_SET
) < 0) ||
144 (write(fd
, buf
, blksz
) < (ssize_t
)blksz
)) {
145 warn("sector=%ld\n", (long)sector
);
153 read_mmap(void *ctx
, uint8_t *buf
, daddr_t sector
)
155 struct local_io
*lio
= (struct local_io
*)ctx
;
156 size_t blksz
= lio
->blksz
;
158 memcpy(buf
, lio
->addr
+ sector
* blksz
, blksz
);
164 write_mmap(void *ctx
, uint8_t *buf
, daddr_t sector
)
166 struct local_io
*lio
= (struct local_io
*)ctx
;
167 size_t blksz
= lio
->blksz
;
169 memcpy(lio
->addr
+ sector
* blksz
, buf
, blksz
);