summaryrefslogtreecommitdiff
path: root/sys/fs/v7fs/v7fs_io_user.c
blob: 907a78b50a6c80dff6b87d1792aa09417f33852c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*	$NetBSD: v7fs_io_user.c,v 1.4 2011/08/08 11:42:30 uch Exp $	*/

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by UCHIYAMA Yasushi.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: v7fs_io_user.c,v 1.4 2011/08/08 11:42:30 uch Exp $");
#endif /* not lint */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <err.h>
#include <sys/mman.h>
#include "v7fs.h"
#include "v7fs_endian.h"
#include "v7fs_impl.h"

#ifdef V7FS_IO_DEBUG
#define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
#else
#define	DPRINTF(fmt, args...)	((void)0)
#endif

struct local_io {
	int fd;
	size_t size;
	size_t blksz;
	uint8_t *addr;
} local;

static bool read_sector(void *, uint8_t *, daddr_t);
static bool write_sector(void *, uint8_t *, daddr_t);
static bool read_mmap(void *, uint8_t *, daddr_t);
static bool write_mmap(void *, uint8_t *, daddr_t);

int
v7fs_io_init(struct v7fs_self **fs, const struct v7fs_mount_device *mount,
    size_t block_size)
{
	struct v7fs_self *p;

	if (!(p = (struct v7fs_self *)malloc(sizeof(*p))))
		return ENOMEM;
	memset(p, 0, sizeof(*p));

	/* Endian */
	p->endian = mount->endian;
#ifdef V7FS_EI
	v7fs_endian_init(p);
#endif
	local.blksz = block_size;
	local.fd = mount->device.fd;
	local.size = mount->sectors * block_size;
	local.addr = mmap(NULL, local.size, PROT_READ | PROT_WRITE | PROT_NONE,
	    MAP_FILE | MAP_SHARED/*writeback*/, local.fd,  0);
	if (local.addr == MAP_FAILED) {
		local.addr = 0;
		p->io.read = read_sector;
		p->io.write = write_sector;
	} else {
		DPRINTF("mmaped addr=%p\n", local.addr);
		p->io.read = read_mmap;
		p->io.write = write_mmap;
	}

	p->io.cookie = &local;
	*fs = p;

	return 0;
}

void
v7fs_io_fini(struct v7fs_self *fs)
{
	struct local_io *lio = (struct local_io *)fs->io.cookie;

	if (lio->addr) {
		if (munmap(lio->addr, lio->size) != 0)
			warn(0);
	}
	fsync(lio->fd);

	free(fs);
}

static bool
read_sector(void *ctx, uint8_t *buf, daddr_t sector)
{
	struct local_io *lio = (struct local_io *)ctx;
	size_t blksz = lio->blksz;
	int fd = lio->fd;

	if ((lseek(fd, (off_t)sector * blksz, SEEK_SET) < 0) ||
	    (read(fd, buf, blksz) < (ssize_t)blksz)) {
		warn("sector=%ld\n", (long)sector);
		return false;
	}

	return true;
}

static bool
write_sector(void *ctx, uint8_t *buf, daddr_t sector)
{
	struct local_io *lio = (struct local_io *)ctx;
	size_t blksz = lio->blksz;
	int fd = lio->fd;

	if ((lseek(fd, (off_t)sector * blksz, SEEK_SET) < 0) ||
	    (write(fd, buf, blksz) < (ssize_t)blksz)) {
		warn("sector=%ld\n", (long)sector);
		return false;
	}

	return true;
}

static bool
read_mmap(void *ctx, uint8_t *buf, daddr_t sector)
{
	struct local_io *lio = (struct local_io *)ctx;
	size_t blksz = lio->blksz;

	memcpy(buf, lio->addr + sector * blksz, blksz);

	return true;
}

static bool
write_mmap(void *ctx, uint8_t *buf, daddr_t sector)
{
	struct local_io *lio = (struct local_io *)ctx;
	size_t blksz = lio->blksz;

	memcpy(lio->addr + sector * blksz, buf, blksz);

	return true;
}