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