summaryrefslogtreecommitdiff
path: root/fs_mgr/fs_mgr_dm_linear.cpp
blob: 9046132f817a57309b62e004210441aee48d688b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "fs_mgr_dm_linear.h"

#include <inttypes.h>
#include <linux/dm-ioctl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <sstream>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <fs_mgr/file_wait.h>
#include <liblp/reader.h>

#include "fs_mgr_priv.h"

namespace android {
namespace fs_mgr {

using DeviceMapper = android::dm::DeviceMapper;
using DmTable = android::dm::DmTable;
using DmTarget = android::dm::DmTarget;
using DmTargetZero = android::dm::DmTargetZero;
using DmTargetLinear = android::dm::DmTargetLinear;

static bool GetPhysicalPartitionDevicePath(const CreateLogicalPartitionParams& params,
                                           const LpMetadataBlockDevice& block_device,
                                           const std::string& super_device, std::string* result) {
    // If the super device is the source of this block device's metadata,
    // make sure we use the correct super device (and not just "super",
    // which might not exist.)
    std::string name = GetBlockDevicePartitionName(block_device);
    if (android::base::StartsWith(name, "dm-")) {
        // Device-mapper nodes are not normally allowed in LpMetadata, since
        // they are not consistent across reboots. However for the purposes of
        // testing it's useful to handle them. For example when running DSUs,
        // userdata is a device-mapper device, and some stacking will result
        // when using libfiemap.
        *result = "/dev/block/" + name;
        return true;
    }

    auto opener = params.partition_opener;
    std::string dev_string = opener->GetDeviceString(name);
    if (GetMetadataSuperBlockDevice(*params.metadata) == &block_device) {
        dev_string = opener->GetDeviceString(super_device);
    }

    // Note: device-mapper will not accept symlinks, so we must use realpath
    // here. If the device string is a major:minor sequence, we don't need to
    // to call Realpath (it would not work anyway).
    if (android::base::StartsWith(dev_string, "/")) {
        if (!android::base::Realpath(dev_string, result)) {
            PERROR << "realpath: " << dev_string;
            return false;
        }
    } else {
        *result = dev_string;
    }
    return true;
}

bool CreateDmTableInternal(const CreateLogicalPartitionParams& params, DmTable* table) {
    const auto& super_device = params.block_device;

    uint64_t sector = 0;
    for (size_t i = 0; i < params.partition->num_extents; i++) {
        const auto& extent = params.metadata->extents[params.partition->first_extent_index + i];
        std::unique_ptr<DmTarget> target;
        switch (extent.target_type) {
            case LP_TARGET_TYPE_ZERO:
                target = std::make_unique<DmTargetZero>(sector, extent.num_sectors);
                break;
            case LP_TARGET_TYPE_LINEAR: {
                const auto& block_device = params.metadata->block_devices[extent.target_source];
                std::string dev_string;
                if (!GetPhysicalPartitionDevicePath(params, block_device, super_device,
                                                    &dev_string)) {
                    LOG(ERROR) << "Unable to complete device-mapper table, unknown block device";
                    return false;
                }
                target = std::make_unique<DmTargetLinear>(sector, extent.num_sectors, dev_string,
                                                          extent.target_data);
                break;
            }
            default:
                LOG(ERROR) << "Unknown target type in metadata: " << extent.target_type;
                return false;
        }
        if (!table->AddTarget(std::move(target))) {
            return false;
        }
        sector += extent.num_sectors;
    }
    if (params.partition->attributes & LP_PARTITION_ATTR_READONLY) {
        table->set_readonly(true);
    }
    if (params.force_writable) {
        table->set_readonly(false);
    }
    return true;
}

bool CreateDmTable(CreateLogicalPartitionParams params, DmTable* table) {
    CreateLogicalPartitionParams::OwnedData owned_data;
    if (!params.InitDefaults(&owned_data)) return false;
    return CreateDmTableInternal(params, table);
}

bool CreateLogicalPartitions(const std::string& block_device) {
    uint32_t slot = SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix());
    auto metadata = ReadMetadata(block_device.c_str(), slot);
    if (!metadata) {
        LOG(ERROR) << "Could not read partition table.";
        return true;
    }
    return CreateLogicalPartitions(*metadata.get(), block_device);
}

std::unique_ptr<LpMetadata> ReadCurrentMetadata(const std::string& block_device) {
    uint32_t slot = SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix());
    return ReadMetadata(block_device.c_str(), slot);
}

bool CreateLogicalPartitions(const LpMetadata& metadata, const std::string& super_device) {
    CreateLogicalPartitionParams params = {
            .block_device = super_device,
            .metadata = &metadata,
    };
    for (const auto& partition : metadata.partitions) {
        if (!partition.num_extents) {
            LINFO << "Skipping zero-length logical partition: " << GetPartitionName(partition);
            continue;
        }
        if (partition.attributes & LP_PARTITION_ATTR_DISABLED) {
            LINFO << "Skipping disabled partition: " << GetPartitionName(partition);
            continue;
        }

        params.partition = &partition;

        std::string ignore_path;
        if (!CreateLogicalPartition(params, &ignore_path)) {
            LERROR << "Could not create logical partition: " << GetPartitionName(partition);
            return false;
        }
    }
    return true;
}

bool CreateLogicalPartitionParams::InitDefaults(CreateLogicalPartitionParams::OwnedData* owned) {
    if (block_device.empty()) {
        LOG(ERROR) << "block_device is required for CreateLogicalPartition";
        return false;
    }

    if (!partition_opener) {
        owned->partition_opener = std::make_unique<PartitionOpener>();
        partition_opener = owned->partition_opener.get();
    }

    // Read metadata if needed.
    if (!metadata) {
        if (!metadata_slot) {
            LOG(ERROR) << "Either metadata or a metadata slot must be specified.";
            return false;
        }
        auto slot = *metadata_slot;
        if (owned->metadata = ReadMetadata(*partition_opener, block_device, slot);
            !owned->metadata) {
            LOG(ERROR) << "Could not read partition table for: " << block_device;
            return false;
        }
        metadata = owned->metadata.get();
    }

    // Find the partition by name if needed.
    if (!partition) {
        for (const auto& metadata_partition : metadata->partitions) {
            if (android::fs_mgr::GetPartitionName(metadata_partition) == partition_name) {
                partition = &metadata_partition;
                break;
            }
        }
    }
    if (!partition) {
        LERROR << "Could not find any partition with name: " << partition_name;
        return false;
    }
    if (partition_name.empty()) {
        partition_name = android::fs_mgr::GetPartitionName(*partition);
    } else if (partition_name != android::fs_mgr::GetPartitionName(*partition)) {
        LERROR << "Inconsistent partition_name " << partition_name << " with partition "
               << android::fs_mgr::GetPartitionName(*partition);
        return false;
    }

    if (device_name.empty()) {
        device_name = partition_name;
    }

    return true;
}

bool CreateLogicalPartition(CreateLogicalPartitionParams params, std::string* path) {
    CreateLogicalPartitionParams::OwnedData owned_data;
    if (!params.InitDefaults(&owned_data)) return false;

    DmTable table;
    if (!CreateDmTableInternal(params, &table)) {
        return false;
    }

    DeviceMapper& dm = DeviceMapper::Instance();
    if (!dm.CreateDevice(params.device_name, table, path, params.timeout_ms)) {
        return false;
    }
    LINFO << "Created logical partition " << params.device_name << " on device " << *path;
    return true;
}

std::string CreateLogicalPartitionParams::GetDeviceName() const {
    if (!device_name.empty()) return device_name;
    return GetPartitionName();
}

std::string CreateLogicalPartitionParams::GetPartitionName() const {
    if (!partition_name.empty()) return partition_name;
    if (partition) return android::fs_mgr::GetPartitionName(*partition);
    return "<unknown partition>";
}

bool UnmapDevice(const std::string& name) {
    DeviceMapper& dm = DeviceMapper::Instance();
    if (!dm.DeleteDevice(name)) {
        return false;
    }
    return true;
}

bool DestroyLogicalPartition(const std::string& name) {
    if (!UnmapDevice(name)) {
        return false;
    }
    LINFO << "Unmapped logical partition " << name;
    return true;
}

}  // namespace fs_mgr
}  // namespace android