summaryrefslogtreecommitdiffstats
blob: 8a5c62400be9ac92c8be58eec2b6ac944891510d (plain) (blame)
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
/*
 *  Copyright 2018 Google, Inc
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef _LIBDM_DMTABLE_H_
#define _LIBDM_DMTABLE_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "dm_target.h"

namespace android {
namespace dm {

class DmTable {
  public:
    DmTable() : num_sectors_(0){};

    // Adds a target to the device mapper table for a range specified in the target object.
    // The function will return 'true' if the target was successfully added and doesn't overlap with
    // any of the existing targets in the table. Gaps are allowed. The final check, including
    // overlaps and gaps are done before loading the table. Returns 'false' on failure.
    bool AddTarget(std::unique_ptr<DmTarget>&& target);

    // Removes a target from the table for the range specified in the target object. Returns 'false'
    // if the target name doesn't match with the one in the table. Returns 'true' if target is
    // successfully removed.
    bool RemoveTarget(std::unique_ptr<DmTarget>&& target);

    // Checks the table to make sure it is valid. i.e. Checks for range overlaps, range gaps
    // and returns 'true' if the table is ready to be loaded into kernel. Returns 'false' if the
    // table is malformed.
    bool valid() const;

    // Returns the toatl number of targets.
    size_t num_targets() const { return targets_.size(); }

    // Returns the total size represented by the table in terms of number of 512-byte sectors.
    // NOTE: This function will overlook if there are any gaps in the targets added in the table.
    uint64_t num_sectors() const;

    // Returns the string represntation of the table that is ready to be passed into the kernel
    // as part of the DM_TABLE_LOAD ioctl.
    std::string Serialize() const;

    ~DmTable() = default;

  private:
    // list of targets defined in this table sorted by
    // their start and end sectors.
    // Note: Overlapping targets MUST never be added in this list.
    std::vector<std::unique_ptr<DmTarget>> targets_;

    // Total size in terms of # of sectors, as calculated by looking at the last and the first
    // target in 'target_'.
    uint64_t num_sectors_;
};

}  // namespace dm
}  // namespace android

#endif /* _LIBDM_DMTABLE_H_ */