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