0,0 → 1,154 |
/* Superblock support. */ |
|
/* Methods: |
* init_super: initializes the superblock structure for later use |
* get_super: return pointer at the superblock |
* read_super: read a superblock |
*/ |
|
#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); |
|
int (*versions[TOTAL_VERSIONS])(int , int*, int*) = { |
v1, /* 0 = V1 version */ |
v2 /* 1 = V2 version */ |
}; |
|
int init_super_block() |
{ |
|
super_block = (super_block_t*)malloc(sizeof(super_block_t)); |
if (super_block != NULL) |
return TRUE; |
|
return FALSE; |
} |
|
super_block_t *get_super() |
{ |
return super_block; |
} |
|
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; |
} |
|
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; |
} |
|
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; |
} |
|
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; |
} |