]> git.proxmox.com Git - ceph.git/blob - ceph/src/mds/Mutation.h
1c5b95371882ea2909ac99bac07f0e13a8a93692
[ceph.git] / ceph / src / mds / Mutation.h
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 #ifndef CEPH_MDS_MUTATION_H
16 #define CEPH_MDS_MUTATION_H
17
18 #include "include/interval_set.h"
19 #include "include/elist.h"
20 #include "include/filepath.h"
21
22 #include "MDSCacheObject.h"
23 #include "MDSContext.h"
24
25 #include "SimpleLock.h"
26 #include "Capability.h"
27 #include "BatchOp.h"
28
29 #include "common/TrackedOp.h"
30 #include "messages/MClientRequest.h"
31 #include "messages/MMDSPeerRequest.h"
32 #include "messages/MClientReply.h"
33
34 class LogSegment;
35 class CInode;
36 class CDir;
37 class CDentry;
38 class Session;
39 class ScatterLock;
40 struct sr_t;
41 struct MDLockCache;
42
43 struct MutationImpl : public TrackedOp {
44 public:
45 // -- my pins and auth_pins --
46 struct ObjectState {
47 bool pinned = false;
48 bool auth_pinned = false;
49 mds_rank_t remote_auth_pinned = MDS_RANK_NONE;
50 };
51
52 // held locks
53 struct LockOp {
54 enum {
55 RDLOCK = 1,
56 WRLOCK = 2,
57 XLOCK = 4,
58 REMOTE_WRLOCK = 8,
59 STATE_PIN = 16, // no RW after locked, just pin lock state
60 };
61
62 LockOp(SimpleLock *l, unsigned f=0, mds_rank_t t=MDS_RANK_NONE) :
63 lock(l), flags(f), wrlock_target(t) {}
64
65 bool is_rdlock() const { return !!(flags & RDLOCK); }
66 bool is_xlock() const { return !!(flags & XLOCK); }
67 bool is_wrlock() const { return !!(flags & WRLOCK); }
68 void clear_wrlock() const { flags &= ~WRLOCK; }
69 bool is_remote_wrlock() const { return !!(flags & REMOTE_WRLOCK); }
70 void clear_remote_wrlock() const {
71 flags &= ~REMOTE_WRLOCK;
72 wrlock_target = MDS_RANK_NONE;
73 }
74 bool is_state_pin() const { return !!(flags & STATE_PIN); }
75 bool operator<(const LockOp& r) const {
76 return lock < r.lock;
77 }
78
79 SimpleLock* lock;
80 mutable unsigned flags;
81 mutable mds_rank_t wrlock_target;
82 };
83
84 struct LockOpVec : public std::vector<LockOp> {
85 LockOpVec() {
86 reserve(32);
87 }
88
89 void add_rdlock(SimpleLock *lock) {
90 emplace_back(lock, LockOp::RDLOCK);
91 }
92 void erase_rdlock(SimpleLock *lock);
93 void add_xlock(SimpleLock *lock, int idx=-1) {
94 if (idx >= 0)
95 emplace(cbegin() + idx, lock, LockOp::XLOCK);
96 else
97 emplace_back(lock, LockOp::XLOCK);
98 }
99 void add_wrlock(SimpleLock *lock, int idx=-1) {
100 if (idx >= 0)
101 emplace(cbegin() + idx, lock, LockOp::WRLOCK);
102 else
103 emplace_back(lock, LockOp::WRLOCK);
104 }
105 void add_remote_wrlock(SimpleLock *lock, mds_rank_t rank) {
106 ceph_assert(rank != MDS_RANK_NONE);
107 emplace_back(lock, LockOp::REMOTE_WRLOCK, rank);
108 }
109 void lock_scatter_gather(SimpleLock *lock) {
110 emplace_back(lock, LockOp::WRLOCK | LockOp::STATE_PIN);
111 }
112 void sort_and_merge();
113 };
114
115 using lock_set = std::set<LockOp>;
116 using lock_iterator = lock_set::iterator;
117
118 // keep our default values synced with MDRequestParam's
119 MutationImpl() : TrackedOp(nullptr, utime_t()) {}
120 MutationImpl(OpTracker *tracker, utime_t initiated,
121 const metareqid_t &ri, __u32 att=0, mds_rank_t peer_to=MDS_RANK_NONE)
122 : TrackedOp(tracker, initiated),
123 reqid(ri), attempt(att),
124 peer_to_mds(peer_to) {}
125 ~MutationImpl() override {
126 ceph_assert(!locking);
127 ceph_assert(!lock_cache);
128 ceph_assert(num_pins == 0);
129 ceph_assert(num_auth_pins == 0);
130 }
131
132 const ObjectState* find_object_state(MDSCacheObject *obj) const {
133 auto it = object_states.find(obj);
134 return it != object_states.end() ? &it->second : nullptr;
135 }
136
137 bool is_any_remote_auth_pin() const { return num_remote_auth_pins > 0; }
138
139 void disable_lock_cache() {
140 lock_cache_disabled = true;
141 }
142
143 lock_iterator emplace_lock(SimpleLock *l, unsigned f=0, mds_rank_t t=MDS_RANK_NONE) {
144 last_locked = l;
145 return locks.emplace(l, f, t).first;
146 }
147
148 bool is_rdlocked(SimpleLock *lock) const;
149 bool is_wrlocked(SimpleLock *lock) const;
150 bool is_xlocked(SimpleLock *lock) const {
151 auto it = locks.find(lock);
152 return it != locks.end() && it->is_xlock();
153 }
154 bool is_remote_wrlocked(SimpleLock *lock) const {
155 auto it = locks.find(lock);
156 return it != locks.end() && it->is_remote_wrlock();
157 }
158 bool is_last_locked(SimpleLock *lock) const {
159 return lock == last_locked;
160 }
161
162 bool is_leader() const { return peer_to_mds == MDS_RANK_NONE; }
163 bool is_peer() const { return peer_to_mds != MDS_RANK_NONE; }
164
165 client_t get_client() const {
166 if (reqid.name.is_client())
167 return client_t(reqid.name.num());
168 return -1;
169 }
170
171 void set_mds_stamp(utime_t t) {
172 mds_stamp = t;
173 }
174 utime_t get_mds_stamp() const {
175 return mds_stamp;
176 }
177 void set_op_stamp(utime_t t) {
178 op_stamp = t;
179 }
180 utime_t get_op_stamp() const {
181 if (op_stamp != utime_t())
182 return op_stamp;
183 return get_mds_stamp();
184 }
185
186 // pin items in cache
187 void pin(MDSCacheObject *object);
188 void unpin(MDSCacheObject *object);
189 void set_stickydirs(CInode *in);
190 void put_stickydirs();
191 void drop_pins();
192
193 void start_locking(SimpleLock *lock, int target=-1);
194 void finish_locking(SimpleLock *lock);
195
196 // auth pins
197 bool is_auth_pinned(MDSCacheObject *object) const;
198 void auth_pin(MDSCacheObject *object);
199 void auth_unpin(MDSCacheObject *object);
200 void drop_local_auth_pins();
201 void set_remote_auth_pinned(MDSCacheObject* object, mds_rank_t from);
202 void _clear_remote_auth_pinned(ObjectState& stat);
203
204 void add_projected_node(MDSCacheObject* obj) {
205 projected_nodes.insert(obj);
206 }
207 void remove_projected_node(MDSCacheObject* obj) {
208 projected_nodes.erase(obj);
209 }
210 bool is_projected(MDSCacheObject *obj) const {
211 return projected_nodes.count(obj);
212 }
213 void add_updated_lock(ScatterLock *lock);
214 void add_cow_inode(CInode *in);
215 void add_cow_dentry(CDentry *dn);
216 void apply();
217 void cleanup();
218
219 virtual void print(std::ostream &out) const {
220 out << "mutation(" << this << ")";
221 }
222
223 virtual void dump(ceph::Formatter *f) const {}
224 void _dump_op_descriptor_unlocked(std::ostream& stream) const override;
225
226 metareqid_t reqid;
227 __u32 attempt = 0; // which attempt for this request
228 LogSegment *ls = nullptr; // the log segment i'm committing to
229
230 // flag mutation as peer
231 mds_rank_t peer_to_mds = MDS_RANK_NONE; // this is a peer request if >= 0.
232
233 ceph::unordered_map<MDSCacheObject*, ObjectState> object_states;
234 int num_pins = 0;
235 int num_auth_pins = 0;
236 int num_remote_auth_pins = 0;
237 // cache pins (so things don't expire)
238 CInode* stickydiri = nullptr;
239
240 lock_set locks; // full ordering
241 MDLockCache* lock_cache = nullptr;
242 bool lock_cache_disabled = false;
243 SimpleLock *last_locked = nullptr;
244 // Lock we are currently trying to acquire. If we give up for some reason,
245 // be sure to eval() this.
246 SimpleLock *locking = nullptr;
247 mds_rank_t locking_target_mds = -1;
248
249 // if this flag is set, do not attempt to acquire further locks.
250 // (useful for wrlock, which may be a moving auth target)
251 enum {
252 SNAP_LOCKED = 1,
253 SNAP2_LOCKED = 2,
254 PATH_LOCKED = 4,
255 ALL_LOCKED = 8,
256 };
257 int locking_state = 0;
258
259 bool committing = false;
260 bool aborted = false;
261 bool killed = false;
262
263 // for applying projected inode changes
264 std::set<MDSCacheObject*> projected_nodes;
265 std::list<ScatterLock*> updated_locks;
266
267 std::list<CInode*> dirty_cow_inodes;
268 std::list<std::pair<CDentry*,version_t> > dirty_cow_dentries;
269
270 private:
271 utime_t mds_stamp; ///< mds-local timestamp (real time)
272 utime_t op_stamp; ///< op timestamp (client provided)
273 };
274
275 /**
276 * MDRequestImpl: state we track for requests we are currently processing.
277 * mostly information about locks held, so that we can drop them all
278 * the request is finished or forwarded. see request_*().
279 */
280 struct MDRequestImpl : public MutationImpl {
281 // TrackedOp stuff
282 typedef boost::intrusive_ptr<MDRequestImpl> Ref;
283
284 // break rarely-used fields into a separately allocated structure
285 // to save memory for most ops
286 struct More {
287 More() {}
288
289 int peer_error = 0;
290 std::set<mds_rank_t> peers; // mds nodes that have peer requests to me (implies client_request)
291 std::set<mds_rank_t> waiting_on_peer; // peers i'm waiting for peerreq replies from.
292
293 // for rename/link/unlink
294 std::set<mds_rank_t> witnessed; // nodes who have journaled a RenamePrepare
295 std::map<MDSCacheObject*,version_t> pvmap;
296
297 bool has_journaled_peers = false;
298 bool peer_update_journaled = false;
299 bool peer_rolling_back = false;
300
301 // for rename
302 std::set<mds_rank_t> extra_witnesses; // replica list from srcdn auth (rename)
303 mds_rank_t srcdn_auth_mds = MDS_RANK_NONE;
304 ceph::buffer::list inode_import;
305 version_t inode_import_v = 0;
306 CInode* rename_inode = nullptr;
307 bool is_freeze_authpin = false;
308 bool is_ambiguous_auth = false;
309 bool is_remote_frozen_authpin = false;
310 bool is_inode_exporter = false;
311
312 std::map<client_t, std::pair<Session*, uint64_t> > imported_session_map;
313 std::map<CInode*, std::map<client_t,Capability::Export> > cap_imports;
314
315 // for lock/flock
316 bool flock_was_waiting = false;
317
318 // for snaps
319 version_t stid = 0;
320 ceph::buffer::list snapidbl;
321
322 sr_t *srci_srnode = nullptr;
323 sr_t *desti_srnode = nullptr;
324
325 // called when peer commits or aborts
326 Context *peer_commit = nullptr;
327 ceph::buffer::list rollback_bl;
328
329 MDSContext::vec waiting_for_finish;
330
331 // export & fragment
332 CDir* export_dir = nullptr;
333 dirfrag_t fragment_base;
334
335 // for internal ops doing lookup
336 filepath filepath1;
337 filepath filepath2;
338 } *_more = nullptr;
339
340 // ---------------------------------------------------
341 struct Params {
342 // keep these default values synced to MutationImpl's
343 Params() {}
344 const utime_t& get_recv_stamp() const {
345 return initiated;
346 }
347 const utime_t& get_throttle_stamp() const {
348 return throttled;
349 }
350 const utime_t& get_recv_complete_stamp() const {
351 return all_read;
352 }
353 const utime_t& get_dispatch_stamp() const {
354 return dispatched;
355 }
356 metareqid_t reqid;
357 __u32 attempt = 0;
358 ceph::cref_t<MClientRequest> client_req;
359 ceph::cref_t<Message> triggering_peer_req;
360 mds_rank_t peer_to = MDS_RANK_NONE;
361 utime_t initiated;
362 utime_t throttled, all_read, dispatched;
363 int internal_op = -1;
364 };
365 MDRequestImpl(const Params* params, OpTracker *tracker) :
366 MutationImpl(tracker, params->initiated,
367 params->reqid, params->attempt, params->peer_to),
368 item_session_request(this), client_request(params->client_req),
369 internal_op(params->internal_op) {}
370 ~MDRequestImpl() override;
371
372 More* more();
373 bool has_more() const;
374 bool has_witnesses();
375 bool peer_did_prepare();
376 bool peer_rolling_back();
377 bool freeze_auth_pin(CInode *inode);
378 void unfreeze_auth_pin(bool clear_inode=false);
379 void set_remote_frozen_auth_pin(CInode *inode);
380 bool can_auth_pin(MDSCacheObject *object);
381 void drop_local_auth_pins();
382 void set_ambiguous_auth(CInode *inode);
383 void clear_ambiguous_auth();
384 const filepath& get_filepath();
385 const filepath& get_filepath2();
386 void set_filepath(const filepath& fp);
387 void set_filepath2(const filepath& fp);
388 bool is_queued_for_replay() const;
389 int compare_paths();
390
391 bool can_batch();
392 bool is_batch_head() {
393 return batch_op_map != nullptr;
394 }
395 std::unique_ptr<BatchOp> release_batch_op();
396
397 void print(std::ostream &out) const override;
398 void dump(ceph::Formatter *f) const override;
399
400 ceph::cref_t<MClientRequest> release_client_request();
401 void reset_peer_request(const ceph::cref_t<MMDSPeerRequest>& req=nullptr);
402
403 Session *session = nullptr;
404 elist<MDRequestImpl*>::item item_session_request; // if not on list, op is aborted.
405
406 // -- i am a client (leader) request
407 ceph::cref_t<MClientRequest> client_request; // client request (if any)
408
409 // tree and depth info of path1 and path2
410 inodeno_t dir_root[2] = {0, 0};
411 int dir_depth[2] = {-1, -1};
412 file_layout_t dir_layout;
413 // store up to two sets of dn vectors, inode pointers, for request path1 and path2.
414 std::vector<CDentry*> dn[2];
415 CInode *in[2] = {};
416 CDentry *straydn = nullptr;
417 snapid_t snapid = CEPH_NOSNAP;
418
419 CInode *tracei = nullptr;
420 CDentry *tracedn = nullptr;
421
422 inodeno_t alloc_ino = 0, used_prealloc_ino = 0;
423 interval_set<inodeno_t> prealloc_inos;
424
425 int snap_caps = 0;
426 int getattr_caps = 0; ///< caps requested by getattr
427 bool no_early_reply = false;
428 bool did_early_reply = false;
429 bool o_trunc = false; ///< request is an O_TRUNC mutation
430 bool has_completed = false; ///< request has already completed
431
432 ceph::buffer::list reply_extra_bl;
433
434 // inos we did a embedded cap release on, and may need to eval if we haven't since reissued
435 std::map<vinodeno_t, ceph_seq_t> cap_releases;
436
437 // -- i am a peer request
438 ceph::cref_t<MMDSPeerRequest> peer_request; // peer request (if one is pending; implies peer == true)
439
440 // -- i am an internal op
441 int internal_op;
442 Context *internal_op_finish = nullptr;
443 void *internal_op_private = nullptr;
444
445 // indicates how may retries of request have been made
446 int retry = 0;
447
448 std::map<int, std::unique_ptr<BatchOp> > *batch_op_map = nullptr;
449
450 // indicator for vxattr osdmap update
451 bool waited_for_osdmap = false;
452
453 protected:
454 void _dump(ceph::Formatter *f) const override;
455 void _dump_op_descriptor_unlocked(std::ostream& stream) const override;
456 private:
457 mutable ceph::spinlock msg_lock;
458 };
459
460 struct MDPeerUpdate {
461 MDPeerUpdate(int oo, ceph::buffer::list &rbl) :
462 origop(oo) {
463 rollback = std::move(rbl);
464 }
465 ~MDPeerUpdate() {
466 if (waiter)
467 waiter->complete(0);
468 }
469 int origop;
470 ceph::buffer::list rollback;
471 Context *waiter = nullptr;
472 std::set<CInode*> olddirs;
473 std::set<CInode*> unlinked;
474 };
475
476 struct MDLockCacheItem {
477 MDLockCache *parent = nullptr;
478 elist<MDLockCacheItem*>::item item_lock;
479 };
480
481 struct MDLockCache : public MutationImpl {
482 using LockItem = MDLockCacheItem;
483
484 struct DirItem {
485 MDLockCache *parent = nullptr;
486 elist<DirItem*>::item item_dir;
487 };
488
489 MDLockCache(Capability *cap, int op) :
490 MutationImpl(), diri(cap->get_inode()), client_cap(cap), opcode(op) {
491 client_cap->lock_caches.push_back(&item_cap_lock_cache);
492 }
493
494 CInode *get_dir_inode() { return diri; }
495 void set_dir_layout(file_layout_t& layout) {
496 dir_layout = layout;
497 }
498 const file_layout_t& get_dir_layout() const {
499 return dir_layout;
500 }
501
502 void attach_locks();
503 void attach_dirfrags(std::vector<CDir*>&& dfv);
504 void detach_locks();
505 void detach_dirfrags();
506
507 CInode *diri;
508 Capability *client_cap;
509 int opcode;
510 file_layout_t dir_layout;
511
512 elist<MDLockCache*>::item item_cap_lock_cache;
513
514 // link myself to locked locks
515 std::unique_ptr<LockItem[]> items_lock;
516
517 // link myself to auth-pinned dirfrags
518 std::unique_ptr<DirItem[]> items_dir;
519 std::vector<CDir*> auth_pinned_dirfrags;
520
521 int ref = 1;
522 bool invalidating = false;
523 };
524
525 typedef boost::intrusive_ptr<MutationImpl> MutationRef;
526 typedef boost::intrusive_ptr<MDRequestImpl> MDRequestRef;
527
528 inline std::ostream& operator<<(std::ostream &out, const MutationImpl &mut)
529 {
530 mut.print(out);
531 return out;
532 }
533 #endif