summaryrefslogtreecommitdiffstats
path: root/src/librbd/cache/pwl/LogMap.h
blob: a053078961755336ae4e40ae32c67d23b2a0f805 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_LIBRBD_CACHE_RWL_LOG_MAP_H
#define CEPH_LIBRBD_CACHE_RWL_LOG_MAP_H

#include "librbd/BlockGuard.h"
#include <list>

namespace librbd {
namespace cache {
namespace pwl {

/**
 * WriteLogMap: maps block extents to GenericWriteLogEntries
 *
 * A WriteLogMapEntry (based on LogMapEntry) refers to a portion of a GenericWriteLogEntry
 */
template <typename T>
class LogMapEntry {
public:
  BlockExtent block_extent;
  std::shared_ptr<T> log_entry;

  LogMapEntry(BlockExtent block_extent,
              std::shared_ptr<T> log_entry = nullptr);
  LogMapEntry(std::shared_ptr<T> log_entry);

  template <typename U>
  friend std::ostream &operator<<(std::ostream &os,
                                  LogMapEntry<U> &e);
};

template <typename T>
using LogMapEntries = std::list<LogMapEntry<T>>;

template <typename T>
class LogMap {
public:
  LogMap(CephContext *cct);
  LogMap(const LogMap&) = delete;
  LogMap &operator=(const LogMap&) = delete;

  void add_log_entry(std::shared_ptr<T> log_entry);
  void add_log_entries(std::list<std::shared_ptr<T>> &log_entries);
  void remove_log_entry(std::shared_ptr<T> log_entry);
  void remove_log_entries(std::list<std::shared_ptr<T>> &log_entries);
  std::list<std::shared_ptr<T>> find_log_entries(BlockExtent block_extent);
  LogMapEntries<T> find_map_entries(BlockExtent block_extent);

private:
  void add_log_entry_locked(std::shared_ptr<T> log_entry);
  void remove_log_entry_locked(std::shared_ptr<T> log_entry);
  void add_map_entry_locked(LogMapEntry<T> &map_entry);
  void remove_map_entry_locked(LogMapEntry<T> &map_entry);
  void adjust_map_entry_locked(LogMapEntry<T> &map_entry, BlockExtent &new_extent);
  void split_map_entry_locked(LogMapEntry<T> &map_entry, BlockExtent &removed_extent);
  std::list<std::shared_ptr<T>> find_log_entries_locked(const BlockExtent &block_extent);
  LogMapEntries<T> find_map_entries_locked(const BlockExtent &block_extent);

  using LogMapEntryT = LogMapEntry<T>;

  class LogMapEntryCompare {
  public:
    bool operator()(const LogMapEntryT &lhs,
                    const LogMapEntryT &rhs) const;
  };

  using BlockExtentToLogMapEntries = std::set<LogMapEntryT,
                                              LogMapEntryCompare>;

  CephContext *m_cct;
  ceph::mutex m_lock;
  BlockExtentToLogMapEntries m_block_to_log_entry_map;
};

} //namespace pwl
} //namespace cache
} //namespace librbd

#endif //CEPH_LIBRBD_CACHE_RWL_LOG_MAP_H