Sync usage with man page.
[netbsd-mini2440.git] / sys / fs / sysvbfs / bfs_sysvbfs.c
blobf0e199fbf453d54be36e31d2501cd3cd698e25b3
1 /* $NetBSD: bfs_sysvbfs.c,v 1.9 2008/04/28 20:24:02 martin Exp $ */
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: bfs_sysvbfs.c,v 1.9 2008/04/28 20:24:02 martin Exp $");
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/malloc.h>
41 #include <sys/kauth.h>
42 #include <fs/sysvbfs/bfs.h>
44 struct bc_io_ops {
45 struct sector_io_ops io;
46 struct vnode *vp;
47 kauth_cred_t cred;
50 #define STATIC
52 STATIC bool bc_read_n(void *, uint8_t *, daddr_t, int);
53 STATIC bool bc_read(void *, uint8_t *, daddr_t);
54 STATIC bool bc_write_n(void *, uint8_t *, daddr_t, int);
55 STATIC bool bc_write(void *, uint8_t *, daddr_t);
57 int
58 sysvbfs_bfs_init(struct bfs **bfsp, struct vnode *vp)
60 struct bc_io_ops *bio;
62 if ((bio = malloc(sizeof(*bio), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
63 printf("can't allocate I/O ops.\n");
64 return ENOMEM;
67 bio->io.read = bc_read;
68 bio->io.read_n = bc_read_n;
69 bio->io.write = bc_write;
70 bio->io.write_n = bc_write_n;
71 bio->vp = vp;
72 bio->cred = NOCRED; /* sysvbfs layer check cred. */
74 return bfs_init2(bfsp, 0, (struct sector_io_ops *)bio, false);
77 void
78 sysvbfs_bfs_fini(struct bfs *bfs)
81 free(bfs->io, M_TEMP);
82 bfs_fini(bfs);
85 STATIC bool
86 bc_read_n(void *self, uint8_t *buf, daddr_t block, int count)
88 int i;
90 for (i = 0; i < count; i++) {
91 if (!bc_read(self, buf, block))
92 return false;
93 buf += DEV_BSIZE;
94 block++;
97 return true;
100 STATIC bool
101 bc_read(void *self, uint8_t *buf, daddr_t block)
103 struct bc_io_ops *bio = self;
104 struct buf *bp = NULL;
106 if (bread(bio->vp, block, DEV_BSIZE, bio->cred, 0, &bp) != 0)
107 goto error_exit;
108 memcpy(buf, bp->b_data, DEV_BSIZE);
109 brelse(bp, 0);
111 return true;
112 error_exit:
113 printf("%s: block %lld read failed.\n", __func__,
114 (long long int)block);
116 if (bp != NULL)
117 brelse(bp, 0);
118 return false;
121 STATIC bool
122 bc_write_n(void *self, uint8_t *buf, daddr_t block, int count)
124 int i;
126 for (i = 0; i < count; i++) {
127 if (!bc_write(self, buf, block))
128 return false;
129 buf += DEV_BSIZE;
130 block++;
133 return true;
136 STATIC bool
137 bc_write(void *self, uint8_t *buf, daddr_t block)
139 struct bc_io_ops *bio = self;
140 struct buf *bp;
142 #if 0
143 printf("%s: block=%lld\n", __func__, block);
144 #endif
145 if ((bp = getblk(bio->vp, block, DEV_BSIZE, 0, 0)) == 0) {
146 printf("getblk failed.\n");
147 return false;
149 memcpy(bp->b_data, buf, DEV_BSIZE);
151 if (bwrite(bp) != 0) {
152 printf("bwrite failed.\n");
153 return false;
156 return true;