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