Subversion Repositories HelenOS

Rev

Rev 2404 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 1987,1997, Prentice Hall
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use of the MINIX operating system in source and
  6.  * binary forms, with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  
  12.  * - Redistributions in binary form must reproduce the above
  13.  *   copyright notice, this list of conditions and the following
  14.  *   disclaimer in the documentation and/or other materials provided
  15.  *   with the distribution.
  16.  
  17.  * - Neither the name of Prentice Hall nor the names of the software
  18.  *   authors or contributors may be used to endorse or promote
  19.  *   products derived from this software without specific prior
  20.  *   written permission.
  21.  
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  23.  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26.  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  27.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  32.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  33.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  */
  35.  
  36. /** @addtogroup FileSystemImpl
  37. * @{
  38. */
  39.  
  40. /**
  41.  * @file    utility.c
  42.  * @brief   Superblock support.
  43.  */
  44.  
  45. #include <string.h>
  46. #include "fs.h"
  47. #include "block.h"
  48. #include "inode.h"
  49. #include "super.h"
  50. #include <stdio.h>
  51.  
  52.  
  53. super_block_t* super_block;
  54.      
  55. static int v1(int magic, int *version, int *extend);
  56. static int v2(int magic ,int*version, int *extend);
  57. static int get_native(int magic, int *version, int *extend);
  58.  
  59. /**
  60.  * File system versions
  61.  */
  62. int (*versions[TOTAL_VERSIONS])(int , int*, int*)  = {
  63.         v1, /* 0 = V1 version */
  64.         v2  /* 1 = V2 version */
  65.     };
  66.  
  67. /**
  68.  * Initializes the superblock structure for later use
  69.  */
  70. int init_super_block()
  71. {
  72.     super_block = (super_block_t*)malloc(sizeof(super_block_t));
  73.     if (super_block != NULL)
  74.         return TRUE;
  75.  
  76.         return FALSE;  
  77. }
  78.  
  79. /**
  80.  * Return pointer at the superblock
  81.  */
  82. super_block_t *get_super()
  83. {  
  84.     return super_block;
  85. }
  86.  
  87. /**
  88.  * Read a superblock
  89.  */
  90. int read_super(register super_block_t *sp)
  91. {
  92.     /* Read a superblock. */
  93.    
  94.     register block_t *bp;
  95.     int magic, version, extend, native;
  96.  
  97.        
  98.     bp = get_block(SUPER_BLOCK);
  99.     memcpy((void *)sp, (void *)(bp->b.b__data), (size_t)SUPER_SIZE);
  100.     magic = sp->s_magic;        /* determines file system type */
  101.    
  102.  
  103.     /* Get file system version and type. */
  104.     native = get_native(magic, &version, &extend);
  105.  
  106.     if (native == FS_EINVAL) {
  107.         print_console("Unknown version of file system\n");
  108.         print_console("Magic is bad\n");
  109.         return FS_EINVAL;  
  110.     }
  111.      
  112.     /* In V1, the device size was kept in a short, s_nzones, which limited
  113.        * devices to 32K zones.  For V2, it was decided to keep the size as a
  114.        * long.  However, just changing s_nzones to a long would not work, since
  115.        * then the position of s_magic in the super block would not be the same
  116.        * in V1 and V2 file systems, and there would be no way to tell whether
  117.        * a newly mounted file system was V1 or V2.  The solution was to introduce
  118.        * a new variable, s_zones, and copy the size there.
  119.        *
  120.        * Calculate some other numbers that depend on the version here too, to
  121.        * hide some of the differences.
  122.        */
  123.  
  124.     if (version == V1) {
  125.         sp->s_zones = sp->s_nzones;     /* only V1 needs this copy */
  126.         sp->s_inodes_per_block = V1_INODES_PER_BLOCK;
  127.         sp->s_ndzones = V1_NR_DZONES;
  128.         sp->s_nindirs = V1_INDIRECTS;
  129.     }
  130.     else {
  131.         sp->s_inodes_per_block = V2_INODES_PER_BLOCK;
  132.         sp->s_ndzones = V2_NR_DZONES;
  133.         sp->s_nindirs = V2_INDIRECTS;
  134.     }
  135.    
  136.     sp->s_isearch = 0;  /* inode searches initially start at 0 */
  137.     sp->s_zsearch = 0;  /* zone searches initially start at 0 */
  138.     sp->s_version = version;
  139.     sp->s_native  = native;    
  140.     sp->s_extend = extend;
  141.  
  142.    
  143.     /* Make a few basic checks to see if super block looks reasonable. */
  144.     if (sp->s_imap_blocks < 1 || sp->s_zmap_blocks < 1
  145.         || sp->s_ninodes < 1 || sp->s_zones < 1
  146.         || (unsigned) sp->s_log_zone_size > 4) {
  147.         return FS_EINVAL;
  148.     }  
  149.      
  150.     return OK;
  151. }
  152.  
  153. /**
  154.  * Test the superblock against MINIX FS v1
  155.  */
  156. int v1(int magic, int* version, int *extend)
  157. {
  158.    
  159.     if (magic == SUPER_MAGIC || magic == conv2(BYTE_SWAP,SUPER_MAGIC)) {
  160.        
  161.         *version = V1; *extend = FALSE;
  162.         return TRUE;
  163.     }
  164.     if (magic == SUPER_MAGIC2 || magic == conv2(BYTE_SWAP,SUPER_MAGIC2)) {
  165.         *version = V1; *extend = TRUE;
  166.         return TRUE;
  167.     }
  168.  
  169.     return FALSE;
  170. }
  171.  
  172. /**
  173.  * Test the superblock against MINIX FS v2
  174.  */
  175. int v2(int magic, int *version, int *extend)
  176. {
  177.    
  178.     if (magic == SUPER_V2 || magic == conv2(BYTE_SWAP,SUPER_V2)) {
  179.         *version = V2; *extend = FALSE;
  180.         return TRUE;
  181.     }
  182.    
  183.     if (magic == SUPER_V2E || magic == conv2(BYTE_SWAP,SUPER_V2E)) {
  184.         *version = V2; *extend = TRUE;
  185.         return TRUE;
  186.     }
  187.  
  188.     return FALSE;
  189. }
  190.  
  191. /**
  192.  * Check the file system version and type
  193.  */
  194. int get_native(int magic, int *version, int *extend)
  195. {  
  196.     int i, result;
  197.    
  198.     /* Get file system version and type */
  199.     for (i = 0; i < TOTAL_VERSIONS; i++) {
  200.         result = versions[i](magic, version, extend);
  201.         if (result) {
  202.             return result;
  203.         }
  204.     }
  205.  
  206.     return FS_EINVAL;
  207. }
  208.  
  209. /**
  210.  * }
  211.  */
  212.  
  213.