/*
* Copyright (c) 1987,1997, Prentice Hall
* All rights reserved.
*
* Redistribution and use of the MINIX operating system in source and
* binary forms, with or without modification, are permitted provided
* that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of Prentice Hall nor the names of the software
* authors or contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, 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 PRENTICE HALL OR ANY AUTHORS 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.
*/
/** @addtogroup FileSystemImpl
* @{
*/
/**
* @file utility.c
* @brief Superblock support.
*/
#include <string.h>
#include "fs.h"
#include "block.h"
#include "inode.h"
#include "super.h"
#include <stdio.h>
super_block_t* super_block;
static int v1(int magic, int *version, int *extend);
static int v2(int magic ,int*version, int *extend);
static int get_native(int magic, int *version, int *extend);
/**
* File system versions
*/
int (*versions[TOTAL_VERSIONS])(int , int*, int*) = {
v1, /* 0 = V1 version */
v2 /* 1 = V2 version */
};
/**
* Initializes the superblock structure for later use
*/
int init_super_block()
{
super_block
= (super_block_t
*)malloc(sizeof(super_block_t
));
if (super_block != NULL)
return TRUE;
return FALSE;
}
/**
* Return pointer at the superblock
*/
super_block_t *get_super()
{
return super_block;
}
/**
* Read a superblock
*/
int read_super(register super_block_t *sp)
{
/* Read a superblock. */
register block_t *bp;
int magic, version, extend, native;
bp = get_block(SUPER_BLOCK);
memcpy((void *)sp
, (void *)(bp
->b.
b__data), (size_t)SUPER_SIZE
);
magic = sp->s_magic; /* determines file system type */
/* Get file system version and type. */
native = get_native(magic, &version, &extend);
if (native == FS_EINVAL) {
print_console("Unknown version of file system\n");
print_console("Magic is bad\n");
return FS_EINVAL;
}
/* In V1, the device size was kept in a short, s_nzones, which limited
* devices to 32K zones. For V2, it was decided to keep the size as a
* long. However, just changing s_nzones to a long would not work, since
* then the position of s_magic in the super block would not be the same
* in V1 and V2 file systems, and there would be no way to tell whether
* a newly mounted file system was V1 or V2. The solution was to introduce
* a new variable, s_zones, and copy the size there.
*
* Calculate some other numbers that depend on the version here too, to
* hide some of the differences.
*/
if (version == V1) {
sp->s_zones = sp->s_nzones; /* only V1 needs this copy */
sp->s_inodes_per_block = V1_INODES_PER_BLOCK;
sp->s_ndzones = V1_NR_DZONES;
sp->s_nindirs = V1_INDIRECTS;
}
else {
sp->s_inodes_per_block = V2_INODES_PER_BLOCK;
sp->s_ndzones = V2_NR_DZONES;
sp->s_nindirs = V2_INDIRECTS;
}
sp->s_isearch = 0; /* inode searches initially start at 0 */
sp->s_zsearch = 0; /* zone searches initially start at 0 */
sp->s_version = version;
sp->s_native = native;
sp->s_extend = extend;
/* Make a few basic checks to see if super block looks reasonable. */
if (sp->s_imap_blocks < 1 || sp->s_zmap_blocks < 1
|| sp->s_ninodes < 1 || sp->s_zones < 1
|| (unsigned) sp->s_log_zone_size > 4) {
return FS_EINVAL;
}
return OK;
}
/**
* Test the superblock against MINIX FS v1
*/
int v1(int magic, int* version, int *extend)
{
if (magic == SUPER_MAGIC || magic == conv2(BYTE_SWAP,SUPER_MAGIC)) {
*version = V1; *extend = FALSE;
return TRUE;
}
if (magic == SUPER_MAGIC2 || magic == conv2(BYTE_SWAP,SUPER_MAGIC2)) {
*version = V1; *extend = TRUE;
return TRUE;
}
return FALSE;
}
/**
* Test the superblock against MINIX FS v2
*/
int v2(int magic, int *version, int *extend)
{
if (magic == SUPER_V2 || magic == conv2(BYTE_SWAP,SUPER_V2)) {
*version = V2; *extend = FALSE;
return TRUE;
}
if (magic == SUPER_V2E || magic == conv2(BYTE_SWAP,SUPER_V2E)) {
*version = V2; *extend = TRUE;
return TRUE;
}
return FALSE;
}
/**
* Check the file system version and type
*/
int get_native(int magic, int *version, int *extend)
{
int i, result;
/* Get file system version and type */
for (i = 0; i < TOTAL_VERSIONS; i++) {
result = versions[i](magic, version, extend);
if (result) {
return result;
}
}
return FS_EINVAL;
}
/**
* }
*/