1 /* bfs - BFS definitions and helper functions 2 * 3 * Copyright 2001-2004, Axel Dörfler, axeld@pinc-software.de. 4 * Parts of this code is based on work previously done by Marcus Overhagen. 5 * 6 * This file may be used under the terms of the MIT License. 7 */ 8 #ifndef BFS_H 9 #define BFS_H 10 11 12 #include "bfs_endian.h" 13 #include "system_dependencies.h" 14 15 16 #ifdef _BOOT_MODE 17 namespace BFS { 18 #endif 19 20 // ToDo: temporary fix! (missing but public ioctls) 21 #define IOCTL_FILE_UNCACHED_IO 10000 22 23 struct block_run { 24 int32 allocation_group; 25 uint16 start; 26 uint16 length; 27 28 int32 AllocationGroup() const { return BFS_ENDIAN_TO_HOST_INT32(allocation_group); } 29 uint16 Start() const { return BFS_ENDIAN_TO_HOST_INT16(start); } 30 uint16 Length() const { return BFS_ENDIAN_TO_HOST_INT16(length); } 31 32 inline bool operator==(const block_run &run) const; 33 inline bool operator!=(const block_run &run) const; 34 inline bool IsZero() const; 35 inline bool MergeableWith(block_run run) const; 36 inline void SetTo(int32 group, uint16 start, uint16 length = 1); 37 38 inline static block_run Run(int32 group, uint16 start, uint16 length = 1); 39 // can't have a constructor because it's used in a union 40 } _PACKED; 41 42 typedef block_run inode_addr; 43 44 // Since the block_run::length field spans 16 bits, the largest number of 45 // blocks covered by a block_run is 65535 (as long as we don't want to 46 // break compatibility and take a zero length for 65536). 47 #define MAX_BLOCK_RUN_LENGTH 65535 48 49 //************************************** 50 51 52 #define BFS_DISK_NAME_LENGTH 32 53 54 struct disk_super_block { 55 char name[BFS_DISK_NAME_LENGTH]; 56 int32 magic1; 57 int32 fs_byte_order; 58 uint32 block_size; 59 uint32 block_shift; 60 off_t num_blocks; 61 off_t used_blocks; 62 int32 inode_size; 63 int32 magic2; 64 int32 blocks_per_ag; 65 int32 ag_shift; 66 int32 num_ags; 67 int32 flags; 68 block_run log_blocks; 69 off_t log_start; 70 off_t log_end; 71 int32 magic3; 72 inode_addr root_dir; 73 inode_addr indices; 74 int32 pad[8]; 75 76 int32 Magic1() const { return BFS_ENDIAN_TO_HOST_INT32(magic1); } 77 int32 Magic2() const { return BFS_ENDIAN_TO_HOST_INT32(magic2); } 78 int32 Magic3() const { return BFS_ENDIAN_TO_HOST_INT32(magic3); } 79 int32 ByteOrder() const { return BFS_ENDIAN_TO_HOST_INT32(fs_byte_order); } 80 uint32 BlockSize() const { return BFS_ENDIAN_TO_HOST_INT32(block_size); } 81 uint32 BlockShift() const { return BFS_ENDIAN_TO_HOST_INT32(block_shift); } 82 off_t NumBlocks() const { return BFS_ENDIAN_TO_HOST_INT64(num_blocks); } 83 off_t UsedBlocks() const { return BFS_ENDIAN_TO_HOST_INT64(used_blocks); } 84 int32 InodeSize() const { return BFS_ENDIAN_TO_HOST_INT32(inode_size); } 85 int32 BlocksPerAllocationGroup() const { return BFS_ENDIAN_TO_HOST_INT32(blocks_per_ag); } 86 int32 AllocationGroups() const { return BFS_ENDIAN_TO_HOST_INT32(num_ags); } 87 int32 AllocationGroupShift() const { return BFS_ENDIAN_TO_HOST_INT32(ag_shift); } 88 int32 Flags() const { return BFS_ENDIAN_TO_HOST_INT32(flags); } 89 off_t LogStart() const { return BFS_ENDIAN_TO_HOST_INT64(log_start); } 90 off_t LogEnd() const { return BFS_ENDIAN_TO_HOST_INT64(log_end); } 91 92 // implemented in Volume.cpp: 93 bool IsValid(); 94 void Initialize(const char *name, off_t numBlocks, uint32 blockSize); 95 } _PACKED; 96 97 #define SUPER_BLOCK_FS_LENDIAN 'BIGE' /* BIGE */ 98 99 #define SUPER_BLOCK_MAGIC1 'BFS1' /* BFS1 */ 100 #define SUPER_BLOCK_MAGIC2 0xdd121031 101 #define SUPER_BLOCK_MAGIC3 0x15b6830e 102 103 #define SUPER_BLOCK_DISK_CLEAN 'CLEN' /* CLEN */ 104 #define SUPER_BLOCK_DISK_DIRTY 'DIRT' /* DIRT */ 105 106 //************************************** 107 108 #define NUM_DIRECT_BLOCKS 12 109 110 struct data_stream { 111 block_run direct[NUM_DIRECT_BLOCKS]; 112 off_t max_direct_range; 113 block_run indirect; 114 off_t max_indirect_range; 115 block_run double_indirect; 116 off_t max_double_indirect_range; 117 off_t size; 118 119 off_t MaxDirectRange() const { return BFS_ENDIAN_TO_HOST_INT64(max_direct_range); } 120 off_t MaxIndirectRange() const { return BFS_ENDIAN_TO_HOST_INT64(max_indirect_range); } 121 off_t MaxDoubleIndirectRange() const { return BFS_ENDIAN_TO_HOST_INT64(max_double_indirect_range); } 122 off_t Size() const { return BFS_ENDIAN_TO_HOST_INT64(size); } 123 } _PACKED; 124 125 // This defines the size of the indirect and double indirect 126 // blocks. Note: the code may not work correctly at some places 127 // if this value is changed (it's not tested). 128 #define NUM_ARRAY_BLOCKS 4 129 #define ARRAY_BLOCKS_SHIFT 2 130 #define INDIRECT_BLOCKS_SHIFT (ARRAY_BLOCKS_SHIFT + ARRAY_BLOCKS_SHIFT) 131 132 //************************************** 133 134 struct bfs_inode; 135 136 struct small_data { 137 uint32 type; 138 uint16 name_size; 139 uint16 data_size; 140 char name[0]; // name_size long, followed by data 141 142 uint32 Type() const { return BFS_ENDIAN_TO_HOST_INT32(type); } 143 uint16 NameSize() const { return BFS_ENDIAN_TO_HOST_INT16(name_size); } 144 uint16 DataSize() const { return BFS_ENDIAN_TO_HOST_INT16(data_size); } 145 146 inline char *Name() const; 147 inline uint8 *Data() const; 148 inline uint32 Size() const; 149 inline small_data *Next() const; 150 inline bool IsLast(const bfs_inode *inode) const; 151 } _PACKED; 152 153 // the file name is part of the small_data structure 154 #define FILE_NAME_TYPE 'CSTR' 155 #define FILE_NAME_NAME 0x13 156 #define FILE_NAME_NAME_LENGTH 1 157 158 159 //************************************** 160 161 class Volume; 162 163 #define SHORT_SYMLINK_NAME_LENGTH 144 // length incl. terminating '\0' 164 165 struct bfs_inode { 166 int32 magic1; 167 inode_addr inode_num; 168 int32 uid; 169 int32 gid; 170 int32 mode; // see sys/stat.h 171 int32 flags; 172 bigtime_t create_time; 173 bigtime_t last_modified_time; 174 inode_addr parent; 175 inode_addr attributes; 176 uint32 type; // attribute type 177 178 int32 inode_size; 179 uint32 etc; // a pointer to the Inode object during construction 180 181 union { 182 data_stream data; 183 char short_symlink[SHORT_SYMLINK_NAME_LENGTH]; 184 }; 185 int32 pad[4]; 186 187 small_data small_data_start[0]; 188 189 int32 Magic1() const { return BFS_ENDIAN_TO_HOST_INT32(magic1); } 190 int32 UserID() const { return BFS_ENDIAN_TO_HOST_INT32(uid); } 191 int32 GroupID() const { return BFS_ENDIAN_TO_HOST_INT32(gid); } 192 int32 Mode() const { return BFS_ENDIAN_TO_HOST_INT32(mode); } 193 int32 Flags() const { return BFS_ENDIAN_TO_HOST_INT32(flags); } 194 int32 Type() const { return BFS_ENDIAN_TO_HOST_INT32(type); } 195 int32 InodeSize() const { return BFS_ENDIAN_TO_HOST_INT32(inode_size); } 196 bigtime_t LastModifiedTime() const { return BFS_ENDIAN_TO_HOST_INT64(last_modified_time); } 197 bigtime_t CreateTime() const { return BFS_ENDIAN_TO_HOST_INT64(create_time); } 198 small_data *SmallDataStart() { return small_data_start; } 199 200 status_t InitCheck(Volume *volume); 201 // defined in Inode.cpp 202 } _PACKED; 203 204 #define INODE_MAGIC1 0x3bbe0ad9 205 #define INODE_TIME_SHIFT 16 206 #define INODE_TIME_MASK 0xffff 207 #define INODE_FILE_NAME_LENGTH 256 208 209 enum inode_flags { 210 INODE_IN_USE = 0x00000001, // always set 211 INODE_ATTR_INODE = 0x00000004, 212 INODE_LOGGED = 0x00000008, // log changes to the data stream 213 INODE_DELETED = 0x00000010, 214 INODE_NOT_READY = 0x00000020, // used during Inode construction 215 INODE_LONG_SYMLINK = 0x00000040, // symlink in data stream 216 217 INODE_PERMANENT_FLAGS = 0x0000ffff, 218 219 INODE_WAS_WRITTEN = 0x00020000, 220 INODE_NO_TRANSACTION = 0x00040000, 221 INODE_DONT_FREE_SPACE = 0x00080000, // only used by the "chkbfs" functionality 222 INODE_CHKBFS_RUNNING = 0x00200000, 223 }; 224 225 //************************************** 226 227 struct file_cookie { 228 bigtime_t last_notification; 229 off_t last_size; 230 int open_mode; 231 }; 232 233 // notify every second if the file size has changed 234 #define INODE_NOTIFICATION_INTERVAL 1000000LL 235 236 //************************************** 237 238 239 inline int32 240 divide_roundup(int32 num,int32 divisor) 241 { 242 return (num + divisor - 1) / divisor; 243 } 244 245 inline int64 246 divide_roundup(int64 num,int32 divisor) 247 { 248 return (num + divisor - 1) / divisor; 249 } 250 251 inline int 252 get_shift(uint64 i) 253 { 254 int c; 255 c = 0; 256 while (i > 1) { 257 i >>= 1; 258 c++; 259 } 260 return c; 261 } 262 263 inline int32 264 round_up(uint32 data) 265 { 266 // rounds up to the next off_t boundary 267 return (data + sizeof(off_t) - 1) & ~(sizeof(off_t) - 1); 268 } 269 270 271 /************************ block_run inline functions ************************/ 272 // #pragma mark - 273 274 275 inline bool 276 block_run::operator==(const block_run &run) const 277 { 278 return allocation_group == run.allocation_group 279 && start == run.start 280 && length == run.length; 281 } 282 283 284 inline bool 285 block_run::operator!=(const block_run &run) const 286 { 287 return allocation_group != run.allocation_group 288 || start != run.start 289 || length != run.length; 290 } 291 292 293 inline bool 294 block_run::IsZero() const 295 { 296 return allocation_group == 0 && start == 0 && length == 0; 297 } 298 299 300 inline bool 301 block_run::MergeableWith(block_run run) const 302 { 303 // 65535 is the maximum allowed run size for BFS 304 return allocation_group == run.allocation_group 305 && Start() + Length() == run.Start() 306 && (uint32)Length() + run.Length() <= MAX_BLOCK_RUN_LENGTH; 307 } 308 309 310 inline void 311 block_run::SetTo(int32 _group,uint16 _start,uint16 _length) 312 { 313 allocation_group = HOST_ENDIAN_TO_BFS_INT32(_group); 314 start = HOST_ENDIAN_TO_BFS_INT16(_start); 315 length = HOST_ENDIAN_TO_BFS_INT16(_length); 316 } 317 318 319 inline block_run 320 block_run::Run(int32 group, uint16 start, uint16 length) 321 { 322 block_run run; 323 run.allocation_group = HOST_ENDIAN_TO_BFS_INT32(group); 324 run.start = HOST_ENDIAN_TO_BFS_INT16(start); 325 run.length = HOST_ENDIAN_TO_BFS_INT16(length); 326 return run; 327 } 328 329 330 /************************ small_data inline functions ************************/ 331 // #pragma mark - 332 333 334 inline char * 335 small_data::Name() const 336 { 337 return const_cast<char *>(name); 338 } 339 340 341 inline uint8 * 342 small_data::Data() const 343 { 344 return (uint8 *)Name() + NameSize() + 3; 345 } 346 347 348 inline uint32 349 small_data::Size() const 350 { 351 return sizeof(small_data) + NameSize() + 3 + DataSize() + 1; 352 } 353 354 355 inline small_data * 356 small_data::Next() const 357 { 358 return (small_data *)((uint8 *)this + Size()); 359 } 360 361 362 inline bool 363 small_data::IsLast(const bfs_inode *inode) const 364 { 365 // we need to check the location first, because if name_size is already beyond 366 // the block, we would touch invalid memory (although that can't cause wrong 367 // results) 368 return (addr_t)this > (addr_t)inode + inode->InodeSize() - sizeof(small_data) || name_size == 0; 369 } 370 371 #ifdef _BOOT_MODE 372 } // namespace BFS 373 #endif 374 375 #endif /* BFS_H */ 376