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(int64 time) 242 { return time >> INODE_TIME_SHIFT; } 243 static uint32 ToNsecs(int64 time) 244 { return (time & INODE_TIME_MASK) << 14; } 245 // the 16 bits internal resolution shifted by 14 gives us 2^30 246 // which is roughly 10^9, the maximum value in nanoseconds 247 } _PACKED; 248 249 enum inode_flags { 250 INODE_IN_USE = 0x00000001, // always set 251 INODE_ATTR_INODE = 0x00000004, 252 INODE_LOGGED = 0x00000008, // log changes to the data stream 253 INODE_DELETED = 0x00000010, 254 INODE_NOT_READY = 0x00000020, // used during Inode construction 255 INODE_LONG_SYMLINK = 0x00000040, // symlink in data stream 256 257 INODE_PERMANENT_FLAGS = 0x0000ffff, 258 259 INODE_WAS_WRITTEN = 0x00020000, 260 INODE_IN_TRANSACTION = 0x00040000, 261 262 // The rest is only used by the file system check functionality 263 INODE_DONT_FREE_SPACE = 0x00080000 264 }; 265 266 267 //************************************** 268 269 struct file_cookie { 270 bigtime_t last_notification; 271 off_t last_size; 272 int open_mode; 273 }; 274 275 #define BFS_OPEN_MODE_USER_MASK 0x7fffffff 276 #define BFS_OPEN_MODE_CHECKING 0x80000000 277 278 // notify every second if the file size has changed 279 #define INODE_NOTIFICATION_INTERVAL 1000000LL 280 281 282 /*! Converts the nano seconds given to the internal 16 bit resolution that 283 BFS uses. If \a time is zero, 12 bits will get a monotonically increasing 284 number. For all other values, only the lower 4 bits are changed this way. 285 286 This is done to decrease the number of duplicate time values, which speeds 287 up the way BFS handles the time indices. 288 */ 289 inline uint32 290 unique_from_nsec(uint32 time) 291 { 292 static vint32 number; 293 if (time != 0) 294 return (((time + 16383) >> 14) & INODE_TIME_MASK) | (++number & 0xf); 295 296 return ++number & 0xfff; 297 } 298 299 300 // #pragma mark - block_run inline functions 301 302 303 inline bool 304 block_run::operator==(const block_run &run) const 305 { 306 return allocation_group == run.allocation_group 307 && start == run.start 308 && length == run.length; 309 } 310 311 312 inline bool 313 block_run::operator!=(const block_run &run) const 314 { 315 return allocation_group != run.allocation_group 316 || start != run.start 317 || length != run.length; 318 } 319 320 321 inline bool 322 block_run::IsZero() const 323 { 324 return allocation_group == 0 && start == 0 && length == 0; 325 } 326 327 328 inline bool 329 block_run::MergeableWith(block_run run) const 330 { 331 // 65535 is the maximum allowed run size for BFS 332 return allocation_group == run.allocation_group 333 && Start() + Length() == run.Start() 334 && (uint32)Length() + run.Length() <= MAX_BLOCK_RUN_LENGTH; 335 } 336 337 338 inline void 339 block_run::SetTo(int32 _group,uint16 _start,uint16 _length) 340 { 341 allocation_group = HOST_ENDIAN_TO_BFS_INT32(_group); 342 start = HOST_ENDIAN_TO_BFS_INT16(_start); 343 length = HOST_ENDIAN_TO_BFS_INT16(_length); 344 } 345 346 347 inline block_run 348 block_run::Run(int32 group, uint16 start, uint16 length) 349 { 350 block_run run; 351 run.allocation_group = HOST_ENDIAN_TO_BFS_INT32(group); 352 run.start = HOST_ENDIAN_TO_BFS_INT16(start); 353 run.length = HOST_ENDIAN_TO_BFS_INT16(length); 354 return run; 355 } 356 357 358 // #pragma mark - small_data inline functions 359 360 361 inline char* 362 small_data::Name() const 363 { 364 return const_cast<char*>(name); 365 } 366 367 368 inline uint8* 369 small_data::Data() const 370 { 371 return (uint8*)Name() + NameSize() + 3; 372 } 373 374 375 inline uint32 376 small_data::Size() const 377 { 378 return sizeof(small_data) + NameSize() + 3 + DataSize() + 1; 379 } 380 381 382 inline small_data* 383 small_data::Next() const 384 { 385 return (small_data*)((uint8*)this + Size()); 386 } 387 388 389 inline bool 390 small_data::IsLast(const bfs_inode* inode) const 391 { 392 // we need to check the location first, because if name_size is already beyond 393 // the block, we would touch invalid memory (although that can't cause wrong 394 // results) 395 return (addr_t)this > (addr_t)inode 396 + inode->InodeSize() - sizeof(small_data) || name_size == 0; 397 } 398 399 #ifdef _BOOT_MODE 400 } // namespace BFS 401 #endif 402 403 #endif /* BFS_H */ 404