/*
 * Copyright (c) 2003 Juli Mallett.  All rights reserved.
 *
 * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
 * FreeBSD project.  Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistribution of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistribution 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 AUTHOR ``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 AUTHOR 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.
 */

#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/disklabel.h>
#include <sys/stat.h>

#include <ufsmount.h>
#include <dinode.h>
#include <fs.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <libufs.h>

void cgswapen(void *buf)
{
	int i;
	struct cg *c = (struct cg *)buf;
	c->cg_firstfield      = (int32_t)bswap_32(c->cg_firstfield);
	c->cg_magic           = (int32_t)bswap_32(c->cg_magic);
	c->cg_old_time        = (int32_t)bswap_32(c->cg_old_time);
	c->cg_cgx             = (int32_t)bswap_32(c->cg_cgx);
	c->cg_old_ncyl        = (int16_t)bswap_16(c->cg_old_ncyl);
	c->cg_old_niblk       = (int16_t)bswap_16(c->cg_old_niblk);
	c->cg_ndblk           = (int32_t)bswap_32(c->cg_ndblk);
	c->cg_cs.cs_ndir      = (int32_t)bswap_32(c->cg_cs.cs_ndir);
	c->cg_cs.cs_nbfree    = (int32_t)bswap_32(c->cg_cs.cs_nbfree);
	c->cg_cs.cs_nifree    = (int32_t)bswap_32(c->cg_cs.cs_nifree);
	c->cg_cs.cs_nffree    = (int32_t)bswap_32(c->cg_cs.cs_nffree);
	c->cg_rotor           = (int32_t)bswap_32(c->cg_rotor);
	c->cg_frotor          = (int32_t)bswap_32(c->cg_frotor);
	c->cg_irotor          = (int32_t)bswap_32(c->cg_irotor);
	for (i = 0; i < MAXFRAG; i++)
	  c->cg_frsum[i]      = (int32_t)bswap_32(c->cg_frsum[i]);
	c->cg_old_btotoff     = (int32_t)bswap_32(c->cg_old_btotoff);
	c->cg_old_boff        = (int32_t)bswap_32(c->cg_old_boff);
	c->cg_iusedoff        = (int32_t)bswap_32(c->cg_iusedoff);
	c->cg_freeoff         = (int32_t)bswap_32(c->cg_freeoff);
	c->cg_nextfreeoff     = (int32_t)bswap_32(c->cg_nextfreeoff);
	c->cg_clustersumoff   = (int32_t)bswap_32(c->cg_clustersumoff);
	c->cg_clusteroff      = (int32_t)bswap_32(c->cg_clusteroff);
	c->cg_nclusterblks    = (int32_t)bswap_32(c->cg_nclusterblks);
	c->cg_niblk           = (int32_t)bswap_32(c->cg_niblk);
	c->cg_initediblk      = (int32_t)bswap_32(c->cg_initediblk);
	for (i = 0; i < 2; i++)
	  c->cg_sparecon32[i] = (int32_t)bswap_32(c->cg_sparecon32[i]);
	c->cg_time            = (int64_t)bswap_64(c->cg_time);
	for (i = 0; i < 3; i++)
	  c->cg_sparecon64[i] = (int64_t)bswap_64(c->cg_sparecon64[i]);
}

int
cgread(struct uufsd *disk)
{
	return (cgread1(disk, disk->d_ccg++));
}

int
cgread1(struct uufsd *disk, int c)
{
	struct fs *fs;
	off_t ccg;

	fs = &disk->d_fs;

	if (c >= fs->fs_ncg) {
		return (0);
	}
	ccg = fsbtodb(fs, cgtod(fs, c)) * disk->d_bsize;
	if (bread(disk, fsbtodb(fs, cgtod(fs, c)), disk->d_cgunion.d_buf,
	    fs->fs_bsize) == -1) {
		ERROR(disk, "unable to read cylinder group");
		return (-1);
	}
	
	if (disk->d_is_BE)
	  cgswapen(disk->d_cgunion.d_buf);
	
	disk->d_lcg = c;
	return (1);
}

int
cgwrite1(struct uufsd *disk, int c)
{
	struct fs *fs;

	fs = &disk->d_fs;
	if (disk->d_is_BE)
	  cgswapen(disk->d_cgunion.d_buf);
	
	if (bwrite(disk, fsbtodb(fs, cgtod(fs, c)),
	    disk->d_cgunion.d_buf, fs->fs_bsize) == -1) {
		ERROR(disk, "unable to write cylinder group");
		return (-1);
	}
	return (0);
}
