1 /* $NetBSD: bfs_sysvbfs.c,v 1.8 2007/12/15 00:39:35 perry 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.8 2007/12/15 00:39:35 perry 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 bool bc_read_n(void *, uint8_t *, daddr_t, int); 60 STATIC bool bc_read(void *, uint8_t *, daddr_t); 61 STATIC bool bc_write_n(void *, uint8_t *, daddr_t, int); 62 STATIC bool 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 bool 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 bool 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, 0); 117 118 return true; 119 error_exit: 120 printf("%s: block %lld read failed.\n", __func__, 121 (long long int)block); 122 123 if (bp != NULL) 124 brelse(bp, 0); 125 return false; 126 } 127 128 STATIC bool 129 bc_write_n(void *self, uint8_t *buf, daddr_t block, int count) 130 { 131 int i; 132 133 for (i = 0; i < count; i++) { 134 if (!bc_write(self, buf, block)) 135 return false; 136 buf += DEV_BSIZE; 137 block++; 138 } 139 140 return true; 141 } 142 143 STATIC bool 144 bc_write(void *self, uint8_t *buf, daddr_t block) 145 { 146 struct bc_io_ops *bio = self; 147 struct buf *bp; 148 149 #if 0 150 printf("%s: block=%lld\n", __func__, block); 151 #endif 152 if ((bp = getblk(bio->vp, block, DEV_BSIZE, 0, 0)) == 0) { 153 printf("getblk failed.\n"); 154 return false; 155 } 156 memcpy(bp->b_data, buf, DEV_BSIZE); 157 158 if (bwrite(bp) != 0) { 159 printf("bwrite failed.\n"); 160 return false; 161 } 162 163 return true; 164 } 165