Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2381 → Rev 2382

/branches/fs/uspace/fs/super.c
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;
}
/branches/fs/uspace/fs/super.h
0,0 → 1,50
/*
* The s_ninodes field gives the number of inodes available
* for files and directories, including the root directory. Inode 0 is
* on the disk, but not used. Thus s_ninodes = 4 means that 5 bits will be
* used in the bit map, bit 0, which is always 1 and not used, and bits 1-4
* for files and directories. The disk layout is:
*
* Item # blocks
* boot block 1
* super block 1
* inode map s_imap_blocks
* zone map s_zmap_blocks
* inodes (s_ninodes + 'inodes per block' - 1)/'inodes per block'
* unused whatever is needed to fill out the current zone
* data zones (s_zones - s_firstdatazone) << s_log_zone_size
*
*/
 
#ifndef _SUPER_H
#define _SUPER_H
 
#define NIL_SUPER (super_block_t *) 0
 
typedef struct {
ino_t s_ninodes; /* # usable inodes on the minor device */
zone1_t s_nzones; /* total device size, including bit maps etc */
short s_imap_blocks; /* # of blocks used by inode bit map */
short s_zmap_blocks; /* # of blocks used by zone bit map */
zone1_t s_firstdatazone; /* number of first data zone */
short s_log_zone_size; /* log2 of blocks/zone */
offset_t s_max_size; /* maximum file size on this device */
short s_magic; /* magic number to recognize super-blocks */
short s_pad; /* try to avoid compiler-dependent padding */
zone_t s_zones; /* number of zones (replaces s_nzones in V2) */
/* The following items are only used when the super_block is in memory. */
unsigned s_inodes_per_block; /* precalculated from magic number */
int s_native; /* set to 1 iff not byte swapped file system */
int s_version; /* file system version, zero means bad magic */
int s_extend; /* 1 if file system support 30 chars in file name */
int s_ndzones; /* # direct zones in an inode */
int s_nindirs; /* # indirect zones per indirect block */
bit_t s_isearch; /* inodes below this bit number are in use */
bit_t s_zsearch; /* all zones below this bit number are in use*/
} super_block_t;
 
extern super_block_t* super_block;
 
#endif /* _SUPER_H */