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