• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2018 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <string>
22 
23 #include <android-base/unique_fd.h>
24 
25 namespace android {
26 namespace fs_mgr {
27 
28 struct BlockDeviceInfo {
BlockDeviceInfoBlockDeviceInfo29     BlockDeviceInfo() : size(0), alignment(0), alignment_offset(0), logical_block_size(0) {}
BlockDeviceInfoBlockDeviceInfo30     BlockDeviceInfo(const std::string& partition_name, uint64_t size, uint32_t alignment,
31                     uint32_t alignment_offset, uint32_t logical_block_size)
32         : size(size),
33           alignment(alignment),
34           alignment_offset(alignment_offset),
35           logical_block_size(logical_block_size),
36           partition_name(partition_name) {}
37     // Size of the block device, in bytes.
38     uint64_t size;
39     // Optimal target alignment, in bytes. Partition extents will be aligned to
40     // this value by default. This value must be 0 or a multiple of 512.
41     uint32_t alignment;
42     // Alignment offset to parent device (if any), in bytes. The sector at
43     // |alignment_offset| on the target device is correctly aligned on its
44     // parent device. This value must be 0 or a multiple of 512.
45     uint32_t alignment_offset;
46     // Block size, for aligning extent sizes and partition sizes.
47     uint32_t logical_block_size;
48     // The physical partition name for this block device, as it would appear in
49     // the GPT or under /dev/block/by-name.
50     std::string partition_name;
51 };
52 
53 // Test-friendly interface for interacting with partitions.
54 class IPartitionOpener {
55   public:
56     virtual ~IPartitionOpener() = default;
57 
58     // Open the given named physical partition with the provided open() flags.
59     // The name can be an absolute path if the full path is already known.
60     virtual android::base::unique_fd Open(const std::string& partition_name, int flags) const = 0;
61 
62     // Return block device information about the given named physical partition.
63     // The name can be an absolute path if the full path is already known.
64     virtual bool GetInfo(const std::string& partition_name, BlockDeviceInfo* info) const = 0;
65 
66     // Return a path that can be used to pass the block device to device-mapper.
67     // This must either result in an absolute path, or a major:minor device
68     // sequence.
69     virtual std::string GetDeviceString(const std::string& partition_name) const = 0;
70 };
71 
72 // Helper class to implement IPartitionOpener. If |partition_name| is not an
73 // absolute path, /dev/block/by-name/ will be prepended.
74 class PartitionOpener : public IPartitionOpener {
75   public:
76     virtual android::base::unique_fd Open(const std::string& partition_name,
77                                           int flags) const override;
78     virtual bool GetInfo(const std::string& partition_name, BlockDeviceInfo* info) const override;
79     virtual std::string GetDeviceString(const std::string& partition_name) const override;
80 };
81 
82 }  // namespace fs_mgr
83 }  // namespace android
84