[BACK]Return to v7fs_superblock.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / fs / v7fs

File: [cvs.NetBSD.org] / src / sys / fs / v7fs / v7fs_superblock.c (download)

Revision 1.2, Mon Jul 18 21:51:49 2011 UTC (12 years, 8 months ago) by apb
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, tls-maxphys-base-20171202, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, thorpej-ifq-base, thorpej-ifq, thorpej-i2c-spi-conf2-base, thorpej-i2c-spi-conf2, thorpej-i2c-spi-conf-base, thorpej-i2c-spi-conf, thorpej-futex2-base, thorpej-futex2, thorpej-futex-base, thorpej-futex, thorpej-cfargs2-base, thorpej-cfargs2, thorpej-cfargs-base, thorpej-cfargs, thorpej-altq-separation-base, thorpej-altq-separation, rmind-smpnet-nbase, rmind-smpnet-base, rmind-smpnet, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, nick-nhusb-base-20170825, nick-nhusb-base-20170204, nick-nhusb-base-20161204, nick-nhusb-base-20161004, nick-nhusb-base-20160907, nick-nhusb-base-20160529, nick-nhusb-base-20160422, nick-nhusb-base-20160319, nick-nhusb-base-20151226, nick-nhusb-base-20150921, nick-nhusb-base-20150606, nick-nhusb-base-20150406, nick-nhusb-base, nick-nhusb, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-10-base, netbsd-10-0-RELEASE, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, localcount-20160914, khorben-n900, jmcneill-usbmp-pre-base2, jmcneill-usbmp-base9, jmcneill-usbmp-base8, jmcneill-usbmp-base7, jmcneill-usbmp-base6, jmcneill-usbmp-base5, jmcneill-usbmp-base4, jmcneill-usbmp-base3, jmcneill-usbmp-base2, jmcneill-usbmp-base10, jmcneill-usbmp-base, jmcneill-usbmp, jmcneill-audiomp3-base, jmcneill-audiomp3, jdolecek-ncq-base, jdolecek-ncq, isaki-audio2-base, isaki-audio2, is-mlppp-base, is-mlppp, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, bouyer-xenpvh-base2, bouyer-xenpvh-base1, bouyer-xenpvh-base, bouyer-xenpvh, bouyer-sunxi-drm-base, bouyer-sunxi-drm, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver, ad-namecache-base3, ad-namecache-base2, ad-namecache-base1, ad-namecache-base, ad-namecache, HEAD
Changes since 1.1: +6 -2 lines

In sources that get compiled into the tools version of makefs, add:

#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

This should fix cross-build problems, but I can't really test
that now, so I am not re-enabling the inclusion of v7fs support
in makefs.

/*	$NetBSD: v7fs_superblock.c,v 1.2 2011/07/18 21:51:49 apb Exp $	*/

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by UCHIYAMA Yasushi.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: v7fs_superblock.c,v 1.2 2011/07/18 21:51:49 apb Exp $");
#if defined _KERNEL_OPT
#include "opt_v7fs.h"
#endif

#ifdef _KERNEL
#include <sys/systm.h>
#include <sys/param.h>	/* errno */
#else
#include <stdio.h>
#include <string.h>
#include <errno.h>
#endif

#include "v7fs.h"
#include "v7fs_impl.h"
#include "v7fs_endian.h"
#include "v7fs_superblock.h"
#include "v7fs_inode.h"
#include "v7fs_datablock.h"

#ifdef V7FS_SUPERBLOCK_DEBUG
#define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
#define	DPRINTF_(fmt, args...)	printf(fmt, ##args)
#else
#define	DPRINTF(fmt, args...)	((void)0)
#define	DPRINTF_(fmt, args...)	((void)0)
#endif

static void v7fs_superblock_endian_convert(struct v7fs_self *,
    struct v7fs_superblock *, struct v7fs_superblock *);
static int v7fs_superblock_sanity(struct v7fs_self *);

/* Load superblock from disk. */
int
v7fs_superblock_load(struct v7fs_self *fs)
{
	struct v7fs_superblock *disksb;
	void *buf;
	int error;

	if (!(buf = scratch_read(fs, V7FS_SUPERBLOCK_SECTOR)))
		return EIO;
	disksb = (struct v7fs_superblock *)buf;
	v7fs_superblock_endian_convert(fs, &fs->superblock, disksb);
	scratch_free(fs, buf);

	if ((error = v7fs_superblock_sanity(fs)))
		return error;

	return 0;
}

/* Writeback superblock to disk. */
int
v7fs_superblock_writeback(struct v7fs_self *fs)
{
	struct v7fs_superblock *memsb = &fs->superblock;
	struct v7fs_superblock *disksb;
	void *buf;
	int error = 0;

	if (!memsb->modified)
		return 0;

	if (!(buf = scratch_read(fs, V7FS_SUPERBLOCK_SECTOR)))
		return EIO;
	disksb = (struct v7fs_superblock *)buf;
	v7fs_superblock_endian_convert(fs, disksb, memsb);
	if (!fs->io.write(fs->io.cookie, buf, V7FS_SUPERBLOCK_SECTOR))
		error = EIO;
	scratch_free(fs, buf);

	memsb->modified = 0;
	DPRINTF("done. %d\n", error);

	return error;
}

/* Check endian mismatch. */
static int
v7fs_superblock_sanity(struct v7fs_self *fs)
{
	const struct v7fs_superblock *sb = &fs->superblock;
	void *buf = 0;

	if ((sb->volume_size < 128) || /* smaller than 64KB. */
	    (sb->datablock_start_sector > sb->volume_size) ||
	    (sb->nfreeinode > V7FS_MAX_FREEINODE) ||
	    (sb->nfreeblock > V7FS_MAX_FREEBLOCK) ||
	    (sb->update_time < 0) ||
	    (sb->total_freeblock > sb->volume_size) ||
	    ((sb->nfreeinode == 0) && (sb->nfreeblock == 0) &&
		(sb->total_freeblock == 0) && (sb->total_freeinode == 0)) ||
	    (!(buf = scratch_read(fs, sb->volume_size - 1)))) {
		DPRINTF("invalid super block.\n");
		return EINVAL;
	}
	if (buf)
		scratch_free(fs, buf);

	return 0;
}

/* Fill free block to superblock cache. */
int
v7fs_freeblock_update(struct v7fs_self *fs, v7fs_daddr_t blk)
{
	/* Assume superblock is locked by caller. */
	struct v7fs_superblock *sb = &fs->superblock;
	struct v7fs_freeblock *fb;
	void *buf;
	int error;

	/* Read next freeblock table from disk. */
	if (!datablock_number_sanity(fs, blk) || !(buf = scratch_read(fs, blk)))
		return EIO;

	/* Update in-core superblock freelist. */
	fb = (struct v7fs_freeblock *)buf;
	if ((error = v7fs_freeblock_endian_convert(fs, fb))) {
		scratch_free(fs, buf);
		return error;
	}
	DPRINTF("freeblock table#%d, nfree=%d\n", blk, fb->nfreeblock);

	memcpy(sb->freeblock, fb->freeblock, sizeof(blk) * fb->nfreeblock);
	sb->nfreeblock = fb->nfreeblock;
	sb->modified = true;
	scratch_free(fs, buf);

	return 0;
}

int
v7fs_freeblock_endian_convert(struct v7fs_self *fs __unused,
    struct v7fs_freeblock *fb __unused)
{
#ifdef V7FS_EI
	int i;
	int16_t nfree;

	nfree = V7FS_VAL16(fs, fb->nfreeblock);
	if (nfree <= 0 || nfree > V7FS_MAX_FREEBLOCK) {
		DPRINTF("invalid freeblock list. %d (max=%d)\n", nfree,
		    V7FS_MAX_FREEBLOCK);
		return ENOSPC;
	}
	fb->nfreeblock = nfree;

	for (i = 0; i < nfree; i++) {
		fb->freeblock[i] = V7FS_VAL32(fs, fb->freeblock[i]);
	}
#endif /* V7FS_EI */

	return 0;
}

/* Fill free inode to superblock cache. */
int
v7fs_freeinode_update(struct v7fs_self *fs)
{
	/* Assume superblock is locked by caller. */
	struct v7fs_superblock *sb = &fs->superblock;
	v7fs_ino_t *freeinode = sb->freeinode;
	size_t i, j, k;
	v7fs_ino_t ino;

	/* Loop over all inode list. */
	for (i = V7FS_ILIST_SECTOR, ino = 1/* inode start from 1*/, k = 0;
	    i < sb->datablock_start_sector; i++) {
		struct v7fs_inode_diskimage *di;
		void *buf;
		if (!(buf = scratch_read(fs, i))) {
			DPRINTF("block %zu I/O error.\n", i);
			ino += V7FS_INODE_PER_BLOCK;
			continue;
		}
		di = (struct v7fs_inode_diskimage *)buf;

		for (j = 0;
		    (j < V7FS_INODE_PER_BLOCK) && (k < V7FS_MAX_FREEINODE);
		    j++, di++, ino++) {
			if (v7fs_inode_allocated(di))
				continue;
			DPRINTF("free inode%d\n", ino);
			freeinode[k++] = ino;
		}
		scratch_free(fs, buf);
	}
	sb->nfreeinode = k;

	return 0;
}

static void
v7fs_superblock_endian_convert(struct v7fs_self *fs __unused,
    struct v7fs_superblock *to,  struct v7fs_superblock *from)
{
#ifdef V7FS_EI
#define	conv16(m)	(to->m = V7FS_VAL16(fs, from->m))
#define	conv32(m)	(to->m = V7FS_VAL32(fs, from->m))
	int i;

	conv16(datablock_start_sector);
	conv32(volume_size);
	conv16(nfreeblock);
	v7fs_daddr_t *dfrom = from->freeblock;
	v7fs_daddr_t *dto = to->freeblock;
	for (i = 0; i < V7FS_MAX_FREEBLOCK; i++, dfrom++, dto++)
		*dto = V7FS_VAL32(fs, *dfrom);

	conv16(nfreeinode);
	v7fs_ino_t *ifrom = from->freeinode;
	v7fs_ino_t *ito = to->freeinode;
	for (i = 0; i < V7FS_MAX_FREEINODE; i++, ifrom++, ito++)
		*ito = V7FS_VAL16(fs, *ifrom);

	conv32(update_time);
	conv32(total_freeblock);
	conv16(total_freeinode);
#undef conv16
#undef conv32
#else /* V7FS_EI */
	memcpy(to, from , sizeof(*to));
#endif /* V7FS_EI */
}