1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file contains functions that are shared between host
6 // and target implementations of Blobfs.
7
8 #pragma once
9
10 #include <bitmap/raw-bitmap.h>
11 #include <bitmap/storage.h>
12 #include <fbl/algorithm.h>
13 #include <fbl/macros.h>
14 #include <fs/block-txn.h>
15 #include <zircon/types.h>
16
17 #include <assert.h>
18 #include <limits.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21
22 #include <blobfs/format.h>
23
24 namespace blobfs {
25
26 // The minimum number of blocks that must be saved by
27 // compression to consider on-disk compression before writeback.
28 constexpr uint64_t kCompressionMinBlocksSaved = 8;
29 constexpr uint64_t kCompressionMinBytesSaved = kCompressionMinBlocksSaved * kBlobfsBlockSize;
30
31 #ifdef __Fuchsia__
32 using RawBitmap = bitmap::RawBitmapGeneric<bitmap::VmoStorage>;
33 #else
34 using RawBitmap = bitmap::RawBitmapGeneric<bitmap::DefaultStorage>;
35 #endif
36
37 void* GetBlock(const RawBitmap& bitmap, uint32_t blkno);
38 void* GetBitBlock(const RawBitmap& bitmap, uint32_t* blkno_out, uint32_t bitno);
39
40 zx_status_t readblk(int fd, uint64_t bno, void* data);
41 zx_status_t writeblk(int fd, uint64_t bno, const void* data);
42 zx_status_t CheckSuperblock(const Superblock* info, uint64_t max);
43 zx_status_t GetBlockCount(int fd, uint64_t* out);
44 int Mkfs(int fd, uint64_t block_count);
45
46 uint32_t MerkleTreeBlocks(const Inode& blobNode);
47
48 // Get a pointer to the nth block of the bitmap.
GetRawBitmapData(const RawBitmap & bm,uint64_t n)49 inline void* GetRawBitmapData(const RawBitmap& bm, uint64_t n) {
50 assert(n * kBlobfsBlockSize < bm.size()); // Accessing beyond end of bitmap
51 assert(kBlobfsBlockSize <= (n + 1) * kBlobfsBlockSize); // Avoid overflow
52 return fs::GetBlock(kBlobfsBlockSize, bm.StorageUnsafe()->GetData(), n);
53 }
54
55 } // namespace blobfs
56