]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- | |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | ||
16 | #ifndef CEPH_CAPABILITY_H | |
17 | #define CEPH_CAPABILITY_H | |
18 | ||
19 | #include "include/buffer_fwd.h" | |
20 | #include "include/counter.h" | |
21 | #include "include/mempool.h" | |
22 | #include "include/xlist.h" | |
23 | #include "include/elist.h" | |
24 | ||
25 | #include "common/config.h" | |
26 | ||
27 | #include "mdstypes.h" | |
28 | ||
29 | ||
30 | /* | |
31 | ||
32 | Capability protocol notes. | |
33 | ||
34 | - two types of cap events from mds -> client: | |
35 | - cap "issue" in a MClientReply, or an MClientCaps IMPORT op. | |
36 | - cap "update" (revocation or grant) .. an MClientCaps message. | |
37 | - if client has cap, the mds should have it too. | |
38 | ||
39 | - if client has no dirty data, it can release it without waiting for an mds ack. | |
40 | - client may thus get a cap _update_ and not have the cap. ignore it. | |
41 | ||
42 | - mds should track seq of last issue. any release | |
43 | attempt will only succeed if the client has seen the latest. | |
44 | ||
45 | - a UPDATE updates the clients issued caps, wanted, etc. it may also flush dirty metadata. | |
46 | - 'caps' are which caps the client retains. | |
47 | - if 0, client wishes to release the cap | |
48 | - 'wanted' is which caps the client wants. | |
49 | - 'dirty' is which metadata is to be written. | |
50 | - client gets a FLUSH_ACK with matching dirty flags indicating which caps were written. | |
51 | ||
52 | - a FLUSH_ACK acks a FLUSH. | |
53 | - 'dirty' is the _original_ FLUSH's dirty (i.e., which metadata was written back) | |
54 | - 'seq' is the _original_ FLUSH's seq. | |
55 | - 'caps' is the _original_ FLUSH's caps (not actually important) | |
56 | - client can conclude that (dirty & ~caps) bits were successfully cleaned. | |
57 | ||
58 | - a FLUSHSNAP flushes snapshot metadata. | |
59 | - 'dirty' indicates which caps, were dirty, if any. | |
60 | - mds writes metadata. if dirty!=0, replies with FLUSHSNAP_ACK. | |
61 | ||
62 | */ | |
63 | ||
64 | class CInode; | |
65 | class Session; | |
66 | class MDLockCache; | |
67 | ||
68 | namespace ceph { | |
69 | class Formatter; | |
70 | } | |
71 | ||
72 | class Capability : public Counter<Capability> { | |
73 | public: | |
74 | MEMPOOL_CLASS_HELPERS(); | |
75 | ||
76 | struct Export { | |
77 | Export() {} | |
78 | Export(int64_t id, int w, int i, int p, snapid_t cf, | |
79 | ceph_seq_t s, ceph_seq_t m, utime_t lis, unsigned st) : | |
80 | cap_id(id), wanted(w), issued(i), pending(p), client_follows(cf), | |
81 | seq(s), mseq(m), last_issue_stamp(lis), state(st) {} | |
82 | void encode(ceph::buffer::list &bl) const; | |
83 | void decode(ceph::buffer::list::const_iterator &p); | |
84 | void dump(ceph::Formatter *f) const; | |
85 | static void generate_test_instances(std::list<Export*>& ls); | |
86 | ||
87 | int64_t cap_id = 0; | |
88 | int32_t wanted = 0; | |
89 | int32_t issued = 0; | |
90 | int32_t pending = 0; | |
91 | snapid_t client_follows; | |
92 | ceph_seq_t seq = 0; | |
93 | ceph_seq_t mseq = 0; | |
94 | utime_t last_issue_stamp; | |
95 | uint32_t state = 0; | |
96 | }; | |
97 | struct Import { | |
98 | Import() {} | |
99 | Import(int64_t i, ceph_seq_t s, ceph_seq_t m) : cap_id(i), issue_seq(s), mseq(m) {} | |
100 | void encode(ceph::buffer::list &bl) const; | |
101 | void decode(ceph::buffer::list::const_iterator &p); | |
102 | void dump(ceph::Formatter *f) const; | |
103 | ||
104 | int64_t cap_id = 0; | |
105 | ceph_seq_t issue_seq = 0; | |
106 | ceph_seq_t mseq = 0; | |
107 | }; | |
108 | struct revoke_info { | |
109 | revoke_info() {} | |
110 | revoke_info(__u32 b, ceph_seq_t s, ceph_seq_t li) : before(b), seq(s), last_issue(li) {} | |
111 | void encode(ceph::buffer::list& bl) const; | |
112 | void decode(ceph::buffer::list::const_iterator& bl); | |
113 | void dump(ceph::Formatter *f) const; | |
114 | static void generate_test_instances(std::list<revoke_info*>& ls); | |
115 | ||
116 | __u32 before = 0; | |
117 | ceph_seq_t seq = 0; | |
118 | ceph_seq_t last_issue = 0; | |
119 | }; | |
120 | ||
121 | const static unsigned STATE_NOTABLE = (1<<0); | |
122 | const static unsigned STATE_NEW = (1<<1); | |
123 | const static unsigned STATE_IMPORTING = (1<<2); | |
124 | const static unsigned STATE_NEEDSNAPFLUSH = (1<<3); | |
125 | const static unsigned STATE_CLIENTWRITEABLE = (1<<4); | |
126 | const static unsigned STATE_NOINLINE = (1<<5); | |
127 | const static unsigned STATE_NOPOOLNS = (1<<6); | |
128 | const static unsigned STATE_NOQUOTA = (1<<7); | |
129 | ||
130 | const static unsigned MASK_STATE_EXPORTED = | |
131 | (STATE_CLIENTWRITEABLE | STATE_NOINLINE | STATE_NOPOOLNS | STATE_NOQUOTA); | |
132 | ||
133 | Capability(CInode *i=nullptr, Session *s=nullptr, uint64_t id=0); | |
134 | Capability(const Capability& other) = delete; | |
135 | ||
136 | const Capability& operator=(const Capability& other) = delete; | |
137 | ||
138 | int pending() const { | |
139 | return _pending; | |
140 | } | |
141 | int issued() const { | |
142 | return _issued; | |
143 | } | |
144 | int revoking() const { | |
145 | return _issued & ~_pending; | |
146 | } | |
147 | ceph_seq_t issue(unsigned c, bool reval=false) { | |
148 | if (reval) | |
149 | revalidate(); | |
150 | ||
151 | if (_pending & ~c) { | |
152 | // revoking (and maybe adding) bits. note caps prior to this revocation | |
153 | _revokes.emplace_back(_pending, last_sent, last_issue); | |
154 | _pending = c; | |
155 | _issued |= c; | |
156 | if (!is_notable()) | |
157 | mark_notable(); | |
158 | } else if (~_pending & c) { | |
159 | // adding bits only. remove obsolete revocations? | |
160 | _pending |= c; | |
161 | _issued |= c; | |
162 | // drop old _revokes with no bits we don't have | |
163 | while (!_revokes.empty() && | |
164 | (_revokes.back().before & ~_pending) == 0) | |
165 | _revokes.pop_back(); | |
166 | } else { | |
167 | // no change. | |
168 | ceph_assert(_pending == c); | |
169 | } | |
170 | //last_issue = | |
171 | inc_last_seq(); | |
172 | return last_sent; | |
173 | } | |
174 | ceph_seq_t issue_norevoke(unsigned c, bool reval=false) { | |
175 | if (reval) | |
176 | revalidate(); | |
177 | ||
178 | _pending |= c; | |
179 | _issued |= c; | |
180 | clear_new(); | |
181 | ||
182 | inc_last_seq(); | |
183 | return last_sent; | |
184 | } | |
185 | int confirm_receipt(ceph_seq_t seq, unsigned caps); | |
186 | // we may get a release racing with revocations, which means our revokes will be ignored | |
187 | // by the client. clean them out of our _revokes history so we don't wait on them. | |
188 | void clean_revoke_from(ceph_seq_t li) { | |
189 | bool changed = false; | |
190 | while (!_revokes.empty() && _revokes.front().last_issue <= li) { | |
191 | _revokes.pop_front(); | |
192 | changed = true; | |
193 | } | |
194 | if (changed) { | |
195 | bool was_revoking = (_issued & ~_pending); | |
196 | calc_issued(); | |
197 | if (was_revoking && _issued == _pending) { | |
198 | item_revoking_caps.remove_myself(); | |
199 | item_client_revoking_caps.remove_myself(); | |
200 | maybe_clear_notable(); | |
201 | } | |
202 | } | |
203 | } | |
204 | ceph_seq_t get_mseq() const { return mseq; } | |
205 | void inc_mseq() { mseq++; } | |
206 | ||
207 | utime_t get_last_issue_stamp() const { return last_issue_stamp; } | |
208 | utime_t get_last_revoke_stamp() const { return last_revoke_stamp; } | |
209 | ||
210 | void set_last_issue() { last_issue = last_sent; } | |
211 | void set_last_issue_stamp(utime_t t) { last_issue_stamp = t; } | |
212 | void set_last_revoke_stamp(utime_t t) { last_revoke_stamp = t; } | |
213 | void reset_num_revoke_warnings() { num_revoke_warnings = 0; } | |
214 | void inc_num_revoke_warnings() { ++num_revoke_warnings; } | |
215 | unsigned get_num_revoke_warnings() const { return num_revoke_warnings; } | |
216 | ||
217 | void set_cap_id(uint64_t i) { cap_id = i; } | |
218 | uint64_t get_cap_id() const { return cap_id; } | |
219 | ||
220 | //ceph_seq_t get_last_issue() { return last_issue; } | |
221 | ||
222 | bool is_suppress() const { return suppress > 0; } | |
223 | void inc_suppress() { suppress++; } | |
224 | void dec_suppress() { suppress--; } | |
225 | ||
226 | static bool is_wanted_notable(int wanted) { | |
227 | return wanted & (CEPH_CAP_ANY_WR|CEPH_CAP_FILE_WR|CEPH_CAP_FILE_RD); | |
228 | } | |
229 | bool is_wanted_notable() const { | |
230 | return is_wanted_notable(wanted()); | |
231 | } | |
232 | bool is_notable() const { return state & STATE_NOTABLE; } | |
233 | ||
234 | bool is_stale() const; | |
235 | bool is_valid() const; | |
236 | bool is_new() const { return state & STATE_NEW; } | |
237 | void mark_new() { state |= STATE_NEW; } | |
238 | void clear_new() { state &= ~STATE_NEW; } | |
239 | bool is_importing() const { return state & STATE_IMPORTING; } | |
240 | void mark_importing() { state |= STATE_IMPORTING; } | |
241 | void clear_importing() { state &= ~STATE_IMPORTING; } | |
242 | bool need_snapflush() const { return state & STATE_NEEDSNAPFLUSH; } | |
243 | void mark_needsnapflush() { state |= STATE_NEEDSNAPFLUSH; } | |
244 | void clear_needsnapflush() { state &= ~STATE_NEEDSNAPFLUSH; } | |
245 | ||
246 | bool is_clientwriteable() const { return state & STATE_CLIENTWRITEABLE; } | |
247 | void mark_clientwriteable() { | |
248 | if (!is_clientwriteable()) { | |
249 | state |= STATE_CLIENTWRITEABLE; | |
250 | if (!is_notable()) | |
251 | mark_notable(); | |
252 | } | |
253 | } | |
254 | void clear_clientwriteable() { | |
255 | if (is_clientwriteable()) { | |
256 | state &= ~STATE_CLIENTWRITEABLE; | |
257 | maybe_clear_notable(); | |
258 | } | |
259 | } | |
260 | ||
261 | bool is_noinline() const { return state & STATE_NOINLINE; } | |
262 | bool is_nopoolns() const { return state & STATE_NOPOOLNS; } | |
263 | bool is_noquota() const { return state & STATE_NOQUOTA; } | |
264 | ||
265 | CInode *get_inode() const { return inode; } | |
266 | Session *get_session() const { return session; } | |
267 | client_t get_client() const; | |
268 | ||
269 | // caps this client wants to hold | |
270 | int wanted() const { return _wanted; } | |
271 | void set_wanted(int w); | |
272 | ||
273 | void inc_last_seq() { last_sent++; } | |
274 | ceph_seq_t get_last_seq() const { | |
275 | return last_sent; | |
276 | } | |
277 | ceph_seq_t get_last_issue() const { return last_issue; } | |
278 | ||
279 | void reset_seq() { | |
280 | last_sent = 0; | |
281 | last_issue = 0; | |
282 | } | |
283 | ||
284 | // -- exports -- | |
285 | Export make_export() const { | |
286 | return Export(cap_id, wanted(), issued(), pending(), client_follows, get_last_seq(), mseq+1, last_issue_stamp, state); | |
287 | } | |
288 | void merge(const Export& other, bool auth_cap) { | |
289 | // issued + pending | |
290 | int newpending = other.pending | pending(); | |
291 | if (other.issued & ~newpending) | |
292 | issue(other.issued | newpending); | |
293 | else | |
294 | issue(newpending); | |
295 | last_issue_stamp = other.last_issue_stamp; | |
296 | ||
297 | client_follows = other.client_follows; | |
298 | ||
299 | state |= other.state & MASK_STATE_EXPORTED; | |
300 | if ((other.state & STATE_CLIENTWRITEABLE) && !is_notable()) | |
301 | mark_notable(); | |
302 | ||
303 | // wanted | |
304 | set_wanted(wanted() | other.wanted); | |
305 | if (auth_cap) | |
306 | mseq = other.mseq; | |
307 | } | |
308 | void merge(int otherwanted, int otherissued) { | |
309 | // issued + pending | |
310 | int newpending = pending(); | |
311 | if (otherissued & ~newpending) | |
312 | issue(otherissued | newpending); | |
313 | else | |
314 | issue(newpending); | |
315 | ||
316 | // wanted | |
317 | set_wanted(wanted() | otherwanted); | |
318 | } | |
319 | ||
320 | int revoke() { | |
321 | if (revoking()) | |
322 | return confirm_receipt(last_sent, pending()); | |
323 | return 0; | |
324 | } | |
325 | ||
326 | // serializers | |
327 | void encode(ceph::buffer::list &bl) const; | |
328 | void decode(ceph::buffer::list::const_iterator &bl); | |
329 | void dump(ceph::Formatter *f) const; | |
330 | static void generate_test_instances(std::list<Capability*>& ls); | |
331 | ||
332 | snapid_t client_follows = 0; | |
333 | version_t client_xattr_version = 0; | |
334 | version_t client_inline_version = 0; | |
335 | int64_t last_rbytes = 0; | |
336 | int64_t last_rsize = 0; | |
337 | ||
338 | xlist<Capability*>::item item_session_caps; | |
339 | xlist<Capability*>::item item_snaprealm_caps; | |
340 | xlist<Capability*>::item item_revoking_caps; | |
341 | xlist<Capability*>::item item_client_revoking_caps; | |
342 | ||
343 | elist<MDLockCache*> lock_caches; | |
344 | int get_lock_cache_allowed() const { return lock_cache_allowed; } | |
345 | void set_lock_cache_allowed(int c) { lock_cache_allowed |= c; } | |
346 | void clear_lock_cache_allowed(int c) { lock_cache_allowed &= ~c; } | |
347 | ||
348 | private: | |
349 | void calc_issued() { | |
350 | _issued = _pending; | |
351 | for (const auto &r : _revokes) { | |
352 | _issued |= r.before; | |
353 | } | |
354 | } | |
355 | ||
356 | void revalidate(); | |
357 | ||
358 | void mark_notable(); | |
359 | void maybe_clear_notable(); | |
360 | ||
361 | CInode *inode; | |
362 | Session *session; | |
363 | ||
364 | uint64_t cap_id; | |
365 | uint32_t cap_gen; | |
366 | ||
367 | __u32 _wanted = 0; // what the client wants (ideally) | |
368 | ||
369 | utime_t last_issue_stamp; | |
370 | utime_t last_revoke_stamp; | |
371 | unsigned num_revoke_warnings = 0; | |
372 | ||
373 | // track in-flight caps -------------- | |
374 | // - add new caps to _pending | |
375 | // - track revocations in _revokes list | |
376 | __u32 _pending = 0, _issued = 0; | |
377 | mempool::mds_co::list<revoke_info> _revokes; | |
378 | ||
379 | ceph_seq_t last_sent = 0; | |
380 | ceph_seq_t last_issue = 0; | |
381 | ceph_seq_t mseq = 0; | |
382 | ||
383 | int suppress = 0; | |
384 | uint32_t state = 0; | |
385 | ||
386 | int lock_cache_allowed = 0; | |
387 | }; | |
388 | ||
389 | WRITE_CLASS_ENCODER(Capability::Export) | |
390 | WRITE_CLASS_ENCODER(Capability::Import) | |
391 | WRITE_CLASS_ENCODER(Capability::revoke_info) | |
392 | WRITE_CLASS_ENCODER(Capability) | |
393 | ||
394 | ||
395 | ||
396 | #endif |