bfs_sysvbfs.c revision 1.2 1 /* $NetBSD: bfs_sysvbfs.c,v 1.2 2006/05/14 21:31:52 elad 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.2 2006/05/14 21:31:52 elad 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 <fs/sysvbfs/bfs.h>
49
50 struct bc_io_ops {
51 struct sector_io_ops io;
52 struct vnode *vp;
53 kauth_cred_t cred;
54 };
55
56 #define STATIC
57
58 STATIC boolean_t bc_read_n(void *, uint8_t *, daddr_t, int);
59 STATIC boolean_t bc_read(void *, uint8_t *, daddr_t);
60 STATIC boolean_t bc_write_n(void *, uint8_t *, daddr_t, int);
61 STATIC boolean_t bc_write(void *, uint8_t *, daddr_t);
62
63 int
64 sysvbfs_bfs_init(struct bfs **bfsp, struct vnode *vp)
65 {
66 struct bc_io_ops *bio;
67
68 if ((bio = malloc(sizeof(*bio), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
69 printf("can't allocate I/O ops.\n");
70 return ENOMEM;
71 }
72
73 bio->io.read = bc_read;
74 bio->io.read_n = bc_read_n;
75 bio->io.write = bc_write;
76 bio->io.write_n = bc_write_n;
77 bio->vp = vp;
78 bio->cred = NOCRED; /* sysvbfs layer check cred. */
79
80 return bfs_init2(bfsp, 0, (struct sector_io_ops *)bio, FALSE);
81 }
82
83 void
84 sysvbfs_bfs_fini(struct bfs *bfs)
85 {
86
87 free(bfs->io, M_TEMP);
88 bfs_fini(bfs);
89 }
90
91 STATIC boolean_t
92 bc_read_n(void *self, uint8_t *buf, daddr_t block, int count)
93 {
94 int i;
95
96 for (i = 0; i < count; i++) {
97 if (!bc_read(self, buf, block))
98 return FALSE;
99 buf += DEV_BSIZE;
100 block++;
101 }
102
103 return TRUE;
104 }
105
106 STATIC boolean_t
107 bc_read(void *self, uint8_t *buf, daddr_t block)
108 {
109 struct bc_io_ops *bio = self;
110 struct buf *bp = NULL;
111
112 if (bread(bio->vp, block, DEV_BSIZE, bio->cred, &bp) != 0)
113 goto error_exit;
114 memcpy(buf, bp->b_data, DEV_BSIZE);
115 brelse(bp);
116
117 return TRUE;
118 error_exit:
119 printf("%s: block %lld read failed.\n", __FUNCTION__, block);
120
121 if (bp != NULL)
122 brelse(bp);
123 return FALSE;
124 }
125
126 STATIC boolean_t
127 bc_write_n(void *self, uint8_t *buf, daddr_t block, int count)
128 {
129 int i;
130
131 for (i = 0; i < count; i++) {
132 if (!bc_write(self, buf, block))
133 return FALSE;
134 buf += DEV_BSIZE;
135 block++;
136 }
137
138 return TRUE;
139 }
140
141 STATIC boolean_t
142 bc_write(void *self, uint8_t *buf, daddr_t block)
143 {
144 struct bc_io_ops *bio = self;
145 struct buf *bp;
146
147 #if 0
148 printf("%s: block=%lld\n", __FUNCTION__, block);
149 #endif
150 if ((bp = getblk(bio->vp, block, DEV_BSIZE, 0, 0)) == 0) {
151 printf("getblk failed.\n");
152 return FALSE;
153 }
154 memcpy(bp->b_data, buf, DEV_BSIZE);
155
156 if (bwrite(bp) != 0) {
157 printf("bwrite failed.\n");
158 return FALSE;
159 }
160
161 return TRUE;
162 }
163