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