00001 // See the end of this file for license information. 00002 00003 #ifndef TORSION_STORAGE_H 00004 #define TORSION_STORAGE_H 00005 00006 #include "blockdev.h" 00007 #include "blockmap.h" 00008 #include "segment.h" 00009 #include "virtmem.h" 00010 #include "addrmap.h" 00011 00013 00022 class Superblock { 00023 public: 00024 unsigned int signature; 00025 unsigned int checkpoint_counter; 00026 unsigned int block_directory; 00027 unsigned int segment_directory; 00028 unsigned int blocks_per_segment; 00029 unsigned int curr_empty_segment; 00030 static const unsigned int CORRECT_SIGNATURE = 0xbb66; 00031 00033 static Superblock* 00034 create(unsigned int block_size) { 00035 return (Superblock*)virtual_mem.alloc(block_size); 00036 } 00037 }; 00038 00041 00050 class Storage_system { 00051 protected: 00052 Block_device* disk; 00053 Segment curr_segment; 00054 00055 unsigned int first_block; 00056 unsigned int blocks_per_segment; 00057 unsigned int blocks_per_page; 00058 unsigned int checkpoint_counter; 00059 unsigned int curr_superblock; 00060 Block_map block_map; 00061 Segment_map segment_map; 00062 bool enabled; 00063 00064 public: 00068 void 00069 format(unsigned int first_block_to_use = 0, unsigned int segment_size = 0); 00070 00071 // Determine whether a valid Torsion storage system begins on disk at 00072 // the given block number. 00073 bool 00074 is_formatted(unsigned int first_block_to_use = 0); 00075 00078 void 00079 init(unsigned int first_block_to_use = 0); 00080 00084 void 00085 write_superblock(unsigned int block_directory = 0, 00086 unsigned int segment_directory = 0, 00087 unsigned int curr_empty_segment = 0); 00088 00090 void 00091 checkpoint(); 00092 00096 unsigned int 00097 log_page(void* page, bool update_block_map = true); 00098 00102 unsigned int 00103 log_map(Address_map& map); 00104 00108 unsigned int* 00109 load_table(Address_map& map, unsigned int dir_index); 00110 00113 void 00114 load_page(void* virtual_page); 00115 00117 void 00118 write_segment(); 00119 00121 void 00122 write_segment_if_full(); 00123 00125 inline void 00126 set_device(Block_device* device) { 00127 disk = device; 00128 } 00129 00133 inline unsigned int 00134 block_to_segment_index(unsigned int segment_block) { 00135 // - 2 because of the two superblocks 00136 return (segment_block - first_block - 2) / blocks_per_segment; 00137 } 00138 00140 inline unsigned int 00141 segment_index_to_block(unsigned int segment_index) { 00142 // + 2 because of the two superblocks 00143 return segment_index * blocks_per_segment + first_block + 2; 00144 } 00145 00147 inline void 00148 enable() { 00149 enabled = true; 00150 } 00151 00153 inline void 00154 disable() { 00155 enabled = false; 00156 } 00157 }; 00158 00159 #endif 00160 00161 /* Torsion Operating System, Copyright (C) 2000-2002 Dan Helfman 00162 * 00163 * This program is free software; you can redistribute it and/or modify it 00164 * under the terms of the GNU General Public License as published by the 00165 * Free Software Foundation; either version 2 of the License, or (at your 00166 * option) any later version. 00167 * 00168 * This program is distributed in the hope that it will be useful, but 00169 * WITHOUT ANY WARRANTY; without even the implied warranty of 00170 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00171 * General Public License for more details (in the COPYING file). 00172 * 00173 * You should have received a copy of the GNU General Public License along 00174 * with this program; if not, write to the Free Software Foundation, Inc., 00175 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00176 */
Torsion Operating System, Copyright (C) 2000-2002 Dan Helfman