1*9f988b79SJean-Baptiste Boric /* $NetBSD: v7fs_io.c,v 1.3 2013/06/28 14:49:14 christos Exp $ */
2*9f988b79SJean-Baptiste Boric
3*9f988b79SJean-Baptiste Boric /*-
4*9f988b79SJean-Baptiste Boric * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*9f988b79SJean-Baptiste Boric * All rights reserved.
6*9f988b79SJean-Baptiste Boric *
7*9f988b79SJean-Baptiste Boric * This code is derived from software contributed to The NetBSD Foundation
8*9f988b79SJean-Baptiste Boric * by UCHIYAMA Yasushi.
9*9f988b79SJean-Baptiste Boric *
10*9f988b79SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
11*9f988b79SJean-Baptiste Boric * modification, are permitted provided that the following conditions
12*9f988b79SJean-Baptiste Boric * are met:
13*9f988b79SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
14*9f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
15*9f988b79SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
16*9f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
17*9f988b79SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
18*9f988b79SJean-Baptiste Boric *
19*9f988b79SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*9f988b79SJean-Baptiste Boric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*9f988b79SJean-Baptiste Boric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*9f988b79SJean-Baptiste Boric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*9f988b79SJean-Baptiste Boric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*9f988b79SJean-Baptiste Boric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*9f988b79SJean-Baptiste Boric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*9f988b79SJean-Baptiste Boric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*9f988b79SJean-Baptiste Boric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*9f988b79SJean-Baptiste Boric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*9f988b79SJean-Baptiste Boric * POSSIBILITY OF SUCH DAMAGE.
30*9f988b79SJean-Baptiste Boric */
31*9f988b79SJean-Baptiste Boric
32*9f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
33*9f988b79SJean-Baptiste Boric #include "nbtool_config.h"
34*9f988b79SJean-Baptiste Boric #endif
35*9f988b79SJean-Baptiste Boric
36*9f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
37*9f988b79SJean-Baptiste Boric __KERNEL_RCSID(0, "$NetBSD: v7fs_io.c,v 1.3 2013/06/28 14:49:14 christos Exp $");
38*9f988b79SJean-Baptiste Boric #if defined _KERNEL_OPT
39*9f988b79SJean-Baptiste Boric #include "opt_v7fs.h"
40*9f988b79SJean-Baptiste Boric #endif
41*9f988b79SJean-Baptiste Boric
42*9f988b79SJean-Baptiste Boric #ifdef _KERNEL
43*9f988b79SJean-Baptiste Boric #include <sys/param.h>
44*9f988b79SJean-Baptiste Boric #else
45*9f988b79SJean-Baptiste Boric #include <stdio.h>
46*9f988b79SJean-Baptiste Boric #include <errno.h>
47*9f988b79SJean-Baptiste Boric #include <stdlib.h>
48*9f988b79SJean-Baptiste Boric #endif
49*9f988b79SJean-Baptiste Boric
50*9f988b79SJean-Baptiste Boric #include "v7fs.h"
51*9f988b79SJean-Baptiste Boric #include "v7fs_impl.h"
52*9f988b79SJean-Baptiste Boric
53*9f988b79SJean-Baptiste Boric #if defined _KERNEL
54*9f988b79SJean-Baptiste Boric #define STATIC_BUFFER
55*9f988b79SJean-Baptiste Boric #endif
56*9f988b79SJean-Baptiste Boric
57*9f988b79SJean-Baptiste Boric #ifdef V7FS_IO_DEBUG
58*9f988b79SJean-Baptiste Boric #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
59*9f988b79SJean-Baptiste Boric #else
60*9f988b79SJean-Baptiste Boric #define DPRINTF(fmt, args...) ((void)0)
61*9f988b79SJean-Baptiste Boric #endif
62*9f988b79SJean-Baptiste Boric
63*9f988b79SJean-Baptiste Boric void *
scratch_read(struct v7fs_self * fs,daddr_t blk)64*9f988b79SJean-Baptiste Boric scratch_read(struct v7fs_self *fs, daddr_t blk)
65*9f988b79SJean-Baptiste Boric {
66*9f988b79SJean-Baptiste Boric #ifdef STATIC_BUFFER
67*9f988b79SJean-Baptiste Boric int i;
68*9f988b79SJean-Baptiste Boric MEM_LOCK(fs);
69*9f988b79SJean-Baptiste Boric for (i = 0; i < V7FS_SELF_NSCRATCH; i++) {
70*9f988b79SJean-Baptiste Boric if (fs->scratch_free & (1 << i)) {
71*9f988b79SJean-Baptiste Boric fs->scratch_free &= ~(1 << i);
72*9f988b79SJean-Baptiste Boric break;
73*9f988b79SJean-Baptiste Boric }
74*9f988b79SJean-Baptiste Boric }
75*9f988b79SJean-Baptiste Boric if (i == V7FS_SELF_NSCRATCH) {
76*9f988b79SJean-Baptiste Boric DPRINTF("No scratch area. increase V7FS_SELF_NSCRATCH\n");
77*9f988b79SJean-Baptiste Boric assert(0);
78*9f988b79SJean-Baptiste Boric MEM_UNLOCK(fs);
79*9f988b79SJean-Baptiste Boric return NULL;
80*9f988b79SJean-Baptiste Boric }
81*9f988b79SJean-Baptiste Boric
82*9f988b79SJean-Baptiste Boric if (!fs->io.read(fs->io.cookie, fs->scratch[i], blk)) {
83*9f988b79SJean-Baptiste Boric DPRINTF("*** I/O error block %ld\n", (long)blk);
84*9f988b79SJean-Baptiste Boric fs->scratch_free |= (1 << i);
85*9f988b79SJean-Baptiste Boric MEM_UNLOCK(fs);
86*9f988b79SJean-Baptiste Boric return NULL;
87*9f988b79SJean-Baptiste Boric }
88*9f988b79SJean-Baptiste Boric MEM_UNLOCK(fs);
89*9f988b79SJean-Baptiste Boric /* Statistic */
90*9f988b79SJean-Baptiste Boric int n;
91*9f988b79SJean-Baptiste Boric if ((n = scratch_remain(fs)) < fs->scratch_remain)
92*9f988b79SJean-Baptiste Boric fs->scratch_remain = n;
93*9f988b79SJean-Baptiste Boric
94*9f988b79SJean-Baptiste Boric return fs->scratch[i];
95*9f988b79SJean-Baptiste Boric #else
96*9f988b79SJean-Baptiste Boric uint8_t *buf = malloc(V7FS_BSIZE);
97*9f988b79SJean-Baptiste Boric if (!fs->io.read(fs->io.cookie, buf, blk)) {
98*9f988b79SJean-Baptiste Boric DPRINTF("*** I/O error block %ld\n",(long)blk);
99*9f988b79SJean-Baptiste Boric free(buf);
100*9f988b79SJean-Baptiste Boric return NULL;
101*9f988b79SJean-Baptiste Boric }
102*9f988b79SJean-Baptiste Boric return buf;
103*9f988b79SJean-Baptiste Boric #endif
104*9f988b79SJean-Baptiste Boric }
105*9f988b79SJean-Baptiste Boric
106*9f988b79SJean-Baptiste Boric int
scratch_remain(const struct v7fs_self * fs)107*9f988b79SJean-Baptiste Boric scratch_remain(const struct v7fs_self *fs)
108*9f988b79SJean-Baptiste Boric {
109*9f988b79SJean-Baptiste Boric #ifdef STATIC_BUFFER
110*9f988b79SJean-Baptiste Boric int nfree;
111*9f988b79SJean-Baptiste Boric int i;
112*9f988b79SJean-Baptiste Boric MEM_LOCK(fs);
113*9f988b79SJean-Baptiste Boric for (i = 0, nfree = 0; i < V7FS_SELF_NSCRATCH; i++) {
114*9f988b79SJean-Baptiste Boric if (fs->scratch_free & (1 << i)) {
115*9f988b79SJean-Baptiste Boric nfree++;
116*9f988b79SJean-Baptiste Boric }
117*9f988b79SJean-Baptiste Boric }
118*9f988b79SJean-Baptiste Boric MEM_UNLOCK(fs);
119*9f988b79SJean-Baptiste Boric return nfree;
120*9f988b79SJean-Baptiste Boric #else
121*9f988b79SJean-Baptiste Boric return -1;
122*9f988b79SJean-Baptiste Boric #endif
123*9f988b79SJean-Baptiste Boric }
124*9f988b79SJean-Baptiste Boric
125*9f988b79SJean-Baptiste Boric void
scratch_free(struct v7fs_self * fs __unused,void * p)126*9f988b79SJean-Baptiste Boric scratch_free(struct v7fs_self *fs __unused, void *p)
127*9f988b79SJean-Baptiste Boric {
128*9f988b79SJean-Baptiste Boric #ifdef STATIC_BUFFER
129*9f988b79SJean-Baptiste Boric int i;
130*9f988b79SJean-Baptiste Boric MEM_LOCK(fs);
131*9f988b79SJean-Baptiste Boric for (i = 0; i < V7FS_SELF_NSCRATCH; i++)
132*9f988b79SJean-Baptiste Boric if (fs->scratch[i] == p) {
133*9f988b79SJean-Baptiste Boric fs->scratch_free |= (1 << i);
134*9f988b79SJean-Baptiste Boric break;
135*9f988b79SJean-Baptiste Boric }
136*9f988b79SJean-Baptiste Boric MEM_UNLOCK(fs);
137*9f988b79SJean-Baptiste Boric assert(i != V7FS_SELF_NSCRATCH);
138*9f988b79SJean-Baptiste Boric #else
139*9f988b79SJean-Baptiste Boric free(p);
140*9f988b79SJean-Baptiste Boric #endif
141*9f988b79SJean-Baptiste Boric }
142