1*b9bf0e6cSchristos /* $NetBSD: v7fs_io.c,v 1.3 2013/06/28 14:49:14 christos Exp $ */
29255b46fSuch
39255b46fSuch /*-
49255b46fSuch * Copyright (c) 2011 The NetBSD Foundation, Inc.
59255b46fSuch * All rights reserved.
69255b46fSuch *
79255b46fSuch * This code is derived from software contributed to The NetBSD Foundation
89255b46fSuch * by UCHIYAMA Yasushi.
99255b46fSuch *
109255b46fSuch * Redistribution and use in source and binary forms, with or without
119255b46fSuch * modification, are permitted provided that the following conditions
129255b46fSuch * are met:
139255b46fSuch * 1. Redistributions of source code must retain the above copyright
149255b46fSuch * notice, this list of conditions and the following disclaimer.
159255b46fSuch * 2. Redistributions in binary form must reproduce the above copyright
169255b46fSuch * notice, this list of conditions and the following disclaimer in the
179255b46fSuch * documentation and/or other materials provided with the distribution.
189255b46fSuch *
199255b46fSuch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209255b46fSuch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219255b46fSuch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229255b46fSuch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239255b46fSuch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249255b46fSuch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259255b46fSuch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269255b46fSuch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279255b46fSuch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289255b46fSuch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299255b46fSuch * POSSIBILITY OF SUCH DAMAGE.
309255b46fSuch */
319255b46fSuch
32f1ca1ce2Sapb #if HAVE_NBTOOL_CONFIG_H
33f1ca1ce2Sapb #include "nbtool_config.h"
34f1ca1ce2Sapb #endif
35f1ca1ce2Sapb
369255b46fSuch #include <sys/cdefs.h>
37*b9bf0e6cSchristos __KERNEL_RCSID(0, "$NetBSD: v7fs_io.c,v 1.3 2013/06/28 14:49:14 christos Exp $");
389255b46fSuch #if defined _KERNEL_OPT
399255b46fSuch #include "opt_v7fs.h"
409255b46fSuch #endif
419255b46fSuch
429255b46fSuch #ifdef _KERNEL
439255b46fSuch #include <sys/param.h>
449255b46fSuch #else
459255b46fSuch #include <stdio.h>
469255b46fSuch #include <errno.h>
479255b46fSuch #include <stdlib.h>
489255b46fSuch #endif
499255b46fSuch
509255b46fSuch #include "v7fs.h"
519255b46fSuch #include "v7fs_impl.h"
529255b46fSuch
539255b46fSuch #if defined _KERNEL
549255b46fSuch #define STATIC_BUFFER
559255b46fSuch #endif
569255b46fSuch
579255b46fSuch #ifdef V7FS_IO_DEBUG
589255b46fSuch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
599255b46fSuch #else
609255b46fSuch #define DPRINTF(fmt, args...) ((void)0)
619255b46fSuch #endif
629255b46fSuch
639255b46fSuch void *
scratch_read(struct v7fs_self * fs,daddr_t blk)649255b46fSuch scratch_read(struct v7fs_self *fs, daddr_t blk)
659255b46fSuch {
669255b46fSuch #ifdef STATIC_BUFFER
679255b46fSuch int i;
689255b46fSuch MEM_LOCK(fs);
699255b46fSuch for (i = 0; i < V7FS_SELF_NSCRATCH; i++) {
709255b46fSuch if (fs->scratch_free & (1 << i)) {
719255b46fSuch fs->scratch_free &= ~(1 << i);
729255b46fSuch break;
739255b46fSuch }
749255b46fSuch }
759255b46fSuch if (i == V7FS_SELF_NSCRATCH) {
769255b46fSuch DPRINTF("No scratch area. increase V7FS_SELF_NSCRATCH\n");
779255b46fSuch assert(0);
789255b46fSuch MEM_UNLOCK(fs);
799255b46fSuch return NULL;
809255b46fSuch }
819255b46fSuch
829255b46fSuch if (!fs->io.read(fs->io.cookie, fs->scratch[i], blk)) {
839255b46fSuch DPRINTF("*** I/O error block %ld\n", (long)blk);
849255b46fSuch fs->scratch_free |= (1 << i);
859255b46fSuch MEM_UNLOCK(fs);
869255b46fSuch return NULL;
879255b46fSuch }
889255b46fSuch MEM_UNLOCK(fs);
899255b46fSuch /* Statistic */
909255b46fSuch int n;
919255b46fSuch if ((n = scratch_remain(fs)) < fs->scratch_remain)
929255b46fSuch fs->scratch_remain = n;
939255b46fSuch
949255b46fSuch return fs->scratch[i];
959255b46fSuch #else
969255b46fSuch uint8_t *buf = malloc(V7FS_BSIZE);
979255b46fSuch if (!fs->io.read(fs->io.cookie, buf, blk)) {
989255b46fSuch DPRINTF("*** I/O error block %ld\n",(long)blk);
99*b9bf0e6cSchristos free(buf);
1009255b46fSuch return NULL;
1019255b46fSuch }
1029255b46fSuch return buf;
1039255b46fSuch #endif
1049255b46fSuch }
1059255b46fSuch
1069255b46fSuch int
scratch_remain(const struct v7fs_self * fs)1079255b46fSuch scratch_remain(const struct v7fs_self *fs)
1089255b46fSuch {
1099255b46fSuch #ifdef STATIC_BUFFER
1109255b46fSuch int nfree;
1119255b46fSuch int i;
1129255b46fSuch MEM_LOCK(fs);
1139255b46fSuch for (i = 0, nfree = 0; i < V7FS_SELF_NSCRATCH; i++) {
1149255b46fSuch if (fs->scratch_free & (1 << i)) {
1159255b46fSuch nfree++;
1169255b46fSuch }
1179255b46fSuch }
1189255b46fSuch MEM_UNLOCK(fs);
1199255b46fSuch return nfree;
1209255b46fSuch #else
1219255b46fSuch return -1;
1229255b46fSuch #endif
1239255b46fSuch }
1249255b46fSuch
1259255b46fSuch void
scratch_free(struct v7fs_self * fs __unused,void * p)1269255b46fSuch scratch_free(struct v7fs_self *fs __unused, void *p)
1279255b46fSuch {
1289255b46fSuch #ifdef STATIC_BUFFER
1299255b46fSuch int i;
1309255b46fSuch MEM_LOCK(fs);
1319255b46fSuch for (i = 0; i < V7FS_SELF_NSCRATCH; i++)
1329255b46fSuch if (fs->scratch[i] == p) {
1339255b46fSuch fs->scratch_free |= (1 << i);
1349255b46fSuch break;
1359255b46fSuch }
1369255b46fSuch MEM_UNLOCK(fs);
1379255b46fSuch assert(i != V7FS_SELF_NSCRATCH);
1389255b46fSuch #else
1399255b46fSuch free(p);
1409255b46fSuch #endif
1419255b46fSuch }
142