1/*	$NetBSD: bfs_sysvbfs.c,v 1.9 2008/04/28 20:24:02 martin Exp $	*/
2
3/*-
4 * Copyright (c) 2004 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
34__KERNEL_RCSID(0, "$NetBSD: bfs_sysvbfs.c,v 1.9 2008/04/28 20:24:02 martin Exp $");
35
36#include <sys/param.h>
37#include <sys/types.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/malloc.h>
41#include <sys/kauth.h>
42#include <fs/sysvbfs/bfs.h>
43
44struct bc_io_ops {
45	struct sector_io_ops io;
46	struct vnode *vp;
47	kauth_cred_t cred;
48};
49
50#define	STATIC
51
52STATIC bool bc_read_n(void *, uint8_t *, daddr_t, int);
53STATIC bool bc_read(void *, uint8_t *, daddr_t);
54STATIC bool bc_write_n(void *, uint8_t *, daddr_t, int);
55STATIC bool bc_write(void *, uint8_t *, daddr_t);
56
57int
58sysvbfs_bfs_init(struct bfs **bfsp, struct vnode *vp)
59{
60	struct bc_io_ops *bio;
61
62	if ((bio = malloc(sizeof(*bio), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
63		printf("can't allocate I/O ops.\n");
64		return ENOMEM;
65	}
66
67	bio->io.read = bc_read;
68	bio->io.read_n = bc_read_n;
69	bio->io.write = bc_write;
70	bio->io.write_n = bc_write_n;
71	bio->vp = vp;
72	bio->cred = NOCRED;	/* sysvbfs layer check cred. */
73
74	return bfs_init2(bfsp, 0, (struct sector_io_ops *)bio, false);
75}
76
77void
78sysvbfs_bfs_fini(struct bfs *bfs)
79{
80
81	free(bfs->io, M_TEMP);
82	bfs_fini(bfs);
83}
84
85STATIC bool
86bc_read_n(void *self, uint8_t *buf, daddr_t block, int count)
87{
88	int i;
89
90	for (i = 0; i < count; i++) {
91		if (!bc_read(self, buf, block))
92			return false;
93		buf += DEV_BSIZE;
94		block++;
95	}
96
97	return true;
98}
99
100STATIC bool
101bc_read(void *self, uint8_t *buf, daddr_t block)
102{
103	struct bc_io_ops *bio = self;
104	struct buf *bp = NULL;
105
106	if (bread(bio->vp, block, DEV_BSIZE, bio->cred, 0, &bp) != 0)
107		goto error_exit;
108	memcpy(buf, bp->b_data, DEV_BSIZE);
109	brelse(bp, 0);
110
111	return true;
112 error_exit:
113	printf("%s: block %lld read failed.\n", __func__,
114	    (long long int)block);
115
116	if (bp != NULL)
117		brelse(bp, 0);
118	return false;
119}
120
121STATIC bool
122bc_write_n(void *self, uint8_t *buf, daddr_t block, int count)
123{
124	int i;
125
126	for (i = 0; i < count; i++) {
127		if (!bc_write(self, buf, block))
128			return false;
129		buf += DEV_BSIZE;
130		block++;
131	}
132
133	return true;
134}
135
136STATIC bool
137bc_write(void *self, uint8_t *buf, daddr_t block)
138{
139	struct bc_io_ops *bio = self;
140	struct buf *bp;
141
142#if 0
143	printf("%s: block=%lld\n", __func__, block);
144#endif
145	if ((bp = getblk(bio->vp, block, DEV_BSIZE, 0, 0)) == 0) {
146		printf("getblk failed.\n");
147		return false;
148	}
149	memcpy(bp->b_data, buf, DEV_BSIZE);
150
151	if (bwrite(bp) != 0) {
152		printf("bwrite failed.\n");
153		return false;
154	}
155
156	return true;
157}
158