summaryrefslogtreecommitdiffstats
path: root/src/mds/SnapServer.h
blob: f0a92ce8b558b86bf8e4b06b4646f55fc154efa5 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#ifndef CEPH_SNAPSERVER_H
#define CEPH_SNAPSERVER_H

#include "MDSTableServer.h"
#include "snap.h"

class MDSRank;
class MonClient;

class SnapServer : public MDSTableServer {
protected:
  MonClient *mon_client = nullptr;
  snapid_t last_snap = 0;
  snapid_t last_created, last_destroyed;
  snapid_t snaprealm_v2_since;
  map<snapid_t, SnapInfo> snaps;
  map<int, set<snapid_t> > need_to_purge;
  
  map<version_t, SnapInfo> pending_update;
  map<version_t, pair<snapid_t,snapid_t> > pending_destroy; // (removed_snap, seq)
  set<version_t>           pending_noop;

  version_t last_checked_osdmap;

  bool root_scrubbed = false; // all snaprealms under root are converted?
  bool mdsdir_scrubbed = false; // all snaprealms under ~mds0 are converted?

  void encode_server_state(bufferlist& bl) const override {
    ENCODE_START(5, 3, bl);
    encode(last_snap, bl);
    encode(snaps, bl);
    encode(need_to_purge, bl);
    encode(pending_update, bl);
    encode(pending_destroy, bl);
    encode(pending_noop, bl);
    encode(last_created, bl);
    encode(last_destroyed, bl);
    encode(snaprealm_v2_since, bl);
    ENCODE_FINISH(bl);
  }
  void decode_server_state(bufferlist::const_iterator& bl) override {
    DECODE_START_LEGACY_COMPAT_LEN(5, 3, 3, bl);
    decode(last_snap, bl);
    decode(snaps, bl);
    decode(need_to_purge, bl);
    decode(pending_update, bl);
    if (struct_v >= 2)
      decode(pending_destroy, bl);
    else {
      map<version_t, snapid_t> t;
      decode(t, bl);
      for (map<version_t, snapid_t>::iterator p = t.begin(); p != t.end(); ++p)
	pending_destroy[p->first].first = p->second; 
    } 
    decode(pending_noop, bl);
    if (struct_v >= 4) {
      decode(last_created, bl);
      decode(last_destroyed, bl);
    } else {
      last_created = last_snap;
      last_destroyed = last_snap;
    }
    if (struct_v >= 5)
      decode(snaprealm_v2_since, bl);
    else
      snaprealm_v2_since = CEPH_NOSNAP;

    DECODE_FINISH(bl);
  }

  // server bits
  void _prepare(const bufferlist &bl, uint64_t reqid, mds_rank_t bymds, bufferlist &out) override;
  void _get_reply_buffer(version_t tid, bufferlist *pbl) const override;
  void _commit(version_t tid, MMDSTableRequest::const_ref req) override;
  void _rollback(version_t tid) override;
  void _server_update(bufferlist& bl) override;
  bool _notify_prep(version_t tid) override;
  void handle_query(const MMDSTableRequest::const_ref &m) override;

public:
  SnapServer(MDSRank *m, MonClient *monc)
    : MDSTableServer(m, TABLE_SNAP), mon_client(monc), last_checked_osdmap(0) {}
  SnapServer() : MDSTableServer(NULL, TABLE_SNAP), last_checked_osdmap(0) {}

  void reset_state() override;

  bool upgrade_format() {
    // upgraded from old filesystem
    ceph_assert(is_active());
    ceph_assert(last_snap > 0);
    bool upgraded = false;
    if (get_version() == 0) {
      // version 0 confuses snapclient code
      reset();
      upgraded = true;
    }
    if (snaprealm_v2_since == CEPH_NOSNAP) {
      // new snapshots will have new format snaprealms
      snaprealm_v2_since = last_snap + 1;
      upgraded = true;
    }
    return upgraded;
  }

  void check_osd_map(bool force);

  void mark_base_recursively_scrubbed(inodeno_t ino) {
    if (ino ==  MDS_INO_ROOT)
      root_scrubbed = true;
    else if (ino == MDS_INO_MDSDIR(rank))
      mdsdir_scrubbed = true;
    else
      ceph_abort();
  }
  bool can_allow_multimds_snaps() const {
    return (root_scrubbed && mdsdir_scrubbed) ||
	   snaps.empty() || snaps.begin()->first >= snaprealm_v2_since;
  }

  void encode(bufferlist& bl) const {
    encode_server_state(bl);
  }
  void decode(bufferlist::const_iterator& bl) {
    decode_server_state(bl);
  }

  void dump(Formatter *f) const;
  static void generate_test_instances(list<SnapServer*>& ls);

  bool force_update(snapid_t last, snapid_t v2_since,
		    map<snapid_t, SnapInfo>& _snaps);
};
WRITE_CLASS_ENCODER(SnapServer)

#endif