bfs_sysvbfs.c revision 1.3 1 /* $NetBSD: bfs_sysvbfs.c,v 1.3 2006/05/15 11:16:16 yamt 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40
41 __KERNEL_RCSID(0, "$NetBSD: bfs_sysvbfs.c,v 1.3 2006/05/15 11:16:16 yamt Exp $");
42
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/buf.h>
47 #include <sys/malloc.h>
48 #include <sys/kauth.h>
49 #include <fs/sysvbfs/bfs.h>
50
51 struct bc_io_ops {
52 struct sector_io_ops io;
53 struct vnode *vp;
54 kauth_cred_t cred;
55 };
56
57 #define STATIC
58
59 STATIC boolean_t bc_read_n(void *, uint8_t *, daddr_t, int);
60 STATIC boolean_t bc_read(void *, uint8_t *, daddr_t);
61 STATIC boolean_t bc_write_n(void *, uint8_t *, daddr_t, int);
62 STATIC boolean_t bc_write(void *, uint8_t *, daddr_t);
63
64 int
65 sysvbfs_bfs_init(struct bfs **bfsp, struct vnode *vp)
66 {
67 struct bc_io_ops *bio;
68
69 if ((bio = malloc(sizeof(*bio), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
70 printf("can't allocate I/O ops.\n");
71 return ENOMEM;
72 }
73
74 bio->io.read = bc_read;
75 bio->io.read_n = bc_read_n;
76 bio->io.write = bc_write;
77 bio->io.write_n = bc_write_n;
78 bio->vp = vp;
79 bio->cred = NOCRED; /* sysvbfs layer check cred. */
80
81 return bfs_init2(bfsp, 0, (struct sector_io_ops *)bio, FALSE);
82 }
83
84 void
85 sysvbfs_bfs_fini(struct bfs *bfs)
86 {
87
88 free(bfs->io, M_TEMP);
89 bfs_fini(bfs);
90 }
91
92 STATIC boolean_t
93 bc_read_n(void *self, uint8_t *buf, daddr_t block, int count)
94 {
95 int i;
96
97 for (i = 0; i < count; i++) {
98 if (!bc_read(self, buf, block))
99 return FALSE;
100 buf += DEV_BSIZE;
101 block++;
102 }
103
104 return TRUE;
105 }
106
107 STATIC boolean_t
108 bc_read(void *self, uint8_t *buf, daddr_t block)
109 {
110 struct bc_io_ops *bio = self;
111 struct buf *bp = NULL;
112
113 if (bread(bio->vp, block, DEV_BSIZE, bio->cred, &bp) != 0)
114 goto error_exit;
115 memcpy(buf, bp->b_data, DEV_BSIZE);
116 brelse(bp);
117
118 return TRUE;
119 error_exit:
120 printf("%s: block %lld read failed.\n", __FUNCTION__, block);
121
122 if (bp != NULL)
123 brelse(bp);
124 return FALSE;
125 }
126
127 STATIC boolean_t
128 bc_write_n(void *self, uint8_t *buf, daddr_t block, int count)
129 {
130 int i;
131
132 for (i = 0; i < count; i++) {
133 if (!bc_write(self, buf, block))
134 return FALSE;
135 buf += DEV_BSIZE;
136 block++;
137 }
138
139 return TRUE;
140 }
141
142 STATIC boolean_t
143 bc_write(void *self, uint8_t *buf, daddr_t block)
144 {
145 struct bc_io_ops *bio = self;
146 struct buf *bp;
147
148 #if 0
149 printf("%s: block=%lld\n", __FUNCTION__, block);
150 #endif
151 if ((bp = getblk(bio->vp, block, DEV_BSIZE, 0, 0)) == 0) {
152 printf("getblk failed.\n");
153 return FALSE;
154 }
155 memcpy(bp->b_data, buf, DEV_BSIZE);
156
157 if (bwrite(bp) != 0) {
158 printf("bwrite failed.\n");
159 return FALSE;
160 }
161
162 return TRUE;
163 }
164