summaryrefslogtreecommitdiffstats
path: root/src/messages/MClientReconnect.h
blob: ba3b0fb2cdac385fdc07276f1baebea0ce3e4238 (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
// -*- 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_MCLIENTRECONNECT_H
#define CEPH_MCLIENTRECONNECT_H

#include "msg/Message.h"
#include "mds/mdstypes.h"
#include "include/ceph_features.h"


class MClientReconnect : public MessageInstance<MClientReconnect> {
public:
  friend factory;
private:
  static constexpr int HEAD_VERSION = 5;
  static constexpr int COMPAT_VERSION = 4;

public:
  map<inodeno_t, cap_reconnect_t> caps; // only head inodes
  vector<snaprealm_reconnect_t> realms;
  bool more = false;

  MClientReconnect() :
    MessageInstance(CEPH_MSG_CLIENT_RECONNECT, HEAD_VERSION, COMPAT_VERSION) {}
private:
  ~MClientReconnect() override {}

  size_t cap_size = 0;
  size_t realm_size = 0;
  size_t approx_size = sizeof(__u32) + sizeof(__u32) + 1;

  void calc_item_size() {
    using ceph::encode;
    {
      bufferlist bl;
      inodeno_t ino;
      cap_reconnect_t cr;
      encode(ino, bl);
      encode(cr, bl);
      cap_size = bl.length();
    }
    {
      bufferlist bl;
      snaprealm_reconnect_t sr;
      encode(sr, bl);
      realm_size = bl.length();
    }
  }

public:
  std::string_view get_type_name() const override { return "client_reconnect"; }
  void print(ostream& out) const override {
    out << "client_reconnect("
	<< caps.size() << " caps " << realms.size() << " realms )";
  }

  // Force to use old encoding.
  // Use connection's features to choose encoding if version is set to 0.
  void set_encoding_version(int v) {
    header.version = v;
    if (v <= 3)
      header.compat_version = 0;
  }
  size_t get_approx_size() {
    return approx_size;
  }
  void mark_more() { more = true; }
  bool has_more() const { return more; }

  void add_cap(inodeno_t ino, uint64_t cap_id, inodeno_t pathbase, const string& path,
	       int wanted, int issued, inodeno_t sr, snapid_t sf, bufferlist& lb)
  {
    caps[ino] = cap_reconnect_t(cap_id, pathbase, path, wanted, issued, sr, sf, lb);
    if (!cap_size)
      calc_item_size();
    approx_size += cap_size + path.length() + lb.length();
  }
  void add_snaprealm(inodeno_t ino, snapid_t seq, inodeno_t parent) {
    snaprealm_reconnect_t r;
    r.realm.ino = ino;
    r.realm.seq = seq;
    r.realm.parent = parent;
    realms.push_back(r);
    if (!realm_size)
      calc_item_size();
    approx_size += realm_size;
  }

  void encode_payload(uint64_t features) override {
    if (header.version == 0) {
      if (features & CEPH_FEATURE_MDSENC)
	header.version = 3;
      else if (features & CEPH_FEATURE_FLOCK)
	header.version = 2;
      else
	header.version = 1;
    }

    using ceph::encode;
    data.clear();

    if (header.version >= 4) {
	encode(caps, data);
	encode(realms, data);
	encode(more, data);
    } else {
      // compat crap
      if (header.version == 3) {
	encode(caps, data);
      } else if (header.version == 2) {
	__u32 n = caps.size();
	encode(n, data);
	for (auto& p : caps) {
	  encode(p.first, data);
	  p.second.encode_old(data);
	}
      } else {
	map<inodeno_t, old_cap_reconnect_t> ocaps;
	for (auto& p : caps) {
	  ocaps[p.first] = p.second;
	encode(ocaps, data);
      }
      for (auto& r : realms)
	r.encode_old(data);
      }
    }
  }
  void decode_payload() override {
    auto p = data.cbegin();
    if (header.version >= 4) {
      decode(caps, p);
      decode(realms, p);
      if (header.version >= 5)
	decode(more, p);
    } else {
      // compat crap
      if (header.version == 3) {
	decode(caps, p);
      } else if (header.version == 2) {
	__u32 n;
	decode(n, p);
	inodeno_t ino;
	while (n--) {
	  decode(ino, p);
	  caps[ino].decode_old(p);
	}
      } else {
	map<inodeno_t, old_cap_reconnect_t> ocaps;
	decode(ocaps, p);
	for (auto &q : ocaps)
	  caps[q.first] = q.second;
      }
      while (!p.end()) {
	realms.push_back(snaprealm_reconnect_t());
	realms.back().decode_old(p);
      }
    }
  }
};


#endif