summaryrefslogtreecommitdiffstats
path: root/src/mds/Locker.h
blob: 3aff8db0bf17b9045380a3d0fef6c500da78f031 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// -*- 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_MDS_LOCKER_H
#define CEPH_MDS_LOCKER_H

#include "include/types.h"

#include "messages/MClientCaps.h"
#include "messages/MClientCapRelease.h"
#include "messages/MClientLease.h"
#include "messages/MLock.h"

#include "CInode.h"
#include "SimpleLock.h"
#include "MDSContext.h"
#include "Mutation.h"
#include "messages/MClientReply.h"

struct SnapRealm;

class MDSRank;
class Session;
class CDentry;
class Capability;
class SimpleLock;
class ScatterLock;
class LocalLockC;

class Locker {
public:
  Locker(MDSRank *m, MDCache *c);

  SimpleLock *get_lock(int lock_type, const MDSCacheObjectInfo &info);
  
  void dispatch(const cref_t<Message> &m);
  void handle_lock(const cref_t<MLock> &m);

  void tick();

  void nudge_log(SimpleLock *lock);

  bool acquire_locks(MDRequestRef& mdr,
		     MutationImpl::LockOpVec& lov,
		     CInode *auth_pin_freeze=NULL,
		     bool auth_pin_nonblocking=false);

  bool try_rdlock_snap_layout(CInode *in, MDRequestRef& mdr,
			      int n=0, bool want_layout=false);

  void notify_freeze_waiter(MDSCacheObject *o);
  void cancel_locking(MutationImpl *mut, std::set<CInode*> *pneed_issue);
  void drop_locks(MutationImpl *mut, std::set<CInode*> *pneed_issue=0);
  void set_xlocks_done(MutationImpl *mut, bool skip_dentry=false);
  void drop_non_rdlocks(MutationImpl *mut, std::set<CInode*> *pneed_issue=0);
  void drop_rdlocks_for_early_reply(MutationImpl *mut);
  void drop_locks_for_fragment_unfreeze(MutationImpl *mut);

  int get_cap_bit_for_lock_cache(int op);
  void create_lock_cache(MDRequestRef& mdr, CInode *diri, file_layout_t *dir_layout=nullptr);
  bool find_and_attach_lock_cache(MDRequestRef& mdr, CInode *diri);
  void invalidate_lock_caches(CDir *dir);
  void invalidate_lock_caches(SimpleLock *lock);
  void invalidate_lock_cache(MDLockCache *lock_cache);
  void eval_lock_caches(Capability *cap);
  void put_lock_cache(MDLockCache* lock_cache);

  void eval_gather(SimpleLock *lock, bool first=false, bool *need_issue=0, MDSContext::vec *pfinishers=0);
  void eval(SimpleLock *lock, bool *need_issue);
  void eval_any(SimpleLock *lock, bool *need_issue, MDSContext::vec *pfinishers=0, bool first=false) {
    if (!lock->is_stable())
      eval_gather(lock, first, need_issue, pfinishers);
    else if (lock->get_parent()->is_auth())
      eval(lock, need_issue);
  }

  void eval_scatter_gathers(CInode *in);

  void eval_cap_gather(CInode *in, std::set<CInode*> *issue_set=0);

  bool eval(CInode *in, int mask, bool caps_imported=false);
  void try_eval(MDSCacheObject *p, int mask);
  void try_eval(SimpleLock *lock, bool *pneed_issue);

  bool _rdlock_kick(SimpleLock *lock, bool as_anon);
  bool rdlock_try(SimpleLock *lock, client_t client);
  bool rdlock_start(SimpleLock *lock, MDRequestRef& mut, bool as_anon=false);
  void rdlock_finish(const MutationImpl::lock_iterator& it, MutationImpl *mut, bool *pneed_issue);
  bool rdlock_try_set(MutationImpl::LockOpVec& lov, MDRequestRef& mdr);
  bool rdlock_try_set(MutationImpl::LockOpVec& lov, MutationRef& mut);

  void wrlock_force(SimpleLock *lock, MutationRef& mut);
  bool wrlock_try(SimpleLock *lock, const MutationRef& mut, client_t client=-1);
  bool wrlock_start(const MutationImpl::LockOp &op, MDRequestRef& mut);
  void wrlock_finish(const MutationImpl::lock_iterator& it, MutationImpl *mut, bool *pneed_issue);

  void remote_wrlock_start(SimpleLock *lock, mds_rank_t target, MDRequestRef& mut);
  void remote_wrlock_finish(const MutationImpl::lock_iterator& it, MutationImpl *mut);

  bool xlock_start(SimpleLock *lock, MDRequestRef& mut);
  void _finish_xlock(SimpleLock *lock, client_t xlocker, bool *pneed_issue);
  void xlock_finish(const MutationImpl::lock_iterator& it, MutationImpl *mut, bool *pneed_issue);

  void xlock_export(const MutationImpl::lock_iterator& it, MutationImpl *mut);
  void xlock_import(SimpleLock *lock);
  void xlock_downgrade(SimpleLock *lock, MutationImpl *mut);

  void try_simple_eval(SimpleLock *lock);
  bool simple_rdlock_try(SimpleLock *lock, MDSContext *con);

  bool simple_sync(SimpleLock *lock, bool *need_issue=0);

   // scatter
  void scatter_eval(ScatterLock *lock, bool *need_issue);        // public for MDCache::adjust_subtree_auth()

  void scatter_tick();
  void scatter_nudge(ScatterLock *lock, MDSContext *c, bool forcelockchange=false);

  void mark_updated_scatterlock(ScatterLock *lock);

  void handle_reqrdlock(SimpleLock *lock, const cref_t<MLock> &m);

  // caps

  // when to defer processing client cap release or writeback due to being
  // frozen.  the condition must be consistent across handle_client_caps and
  // process_request_cap_release to preserve ordering.
  bool should_defer_client_cap_frozen(CInode *in);

  void process_request_cap_release(MDRequestRef& mdr, client_t client, const ceph_mds_request_release& r,
				   std::string_view dname);

  void kick_cap_releases(MDRequestRef& mdr);
  void kick_issue_caps(CInode *in, client_t client, ceph_seq_t seq);

  void remove_client_cap(CInode *in, Capability *cap, bool kill=false);

  std::set<client_t> get_late_revoking_clients(double timeout) const;

  void snapflush_nudge(CInode *in);
  void mark_need_snapflush_inode(CInode *in);
  bool is_revoking_any_caps_from(client_t client);

  // local
  void local_wrlock_grab(LocalLockC *lock, MutationRef& mut);

  // file
  void file_eval(ScatterLock *lock, bool *need_issue);
  void file_recover(ScatterLock *lock);

  void mark_updated_Filelock(ScatterLock *lock);

  // -- file i/o --
  version_t issue_file_data_version(CInode *in);
  Capability* issue_new_caps(CInode *in, int mode, MDRequestRef& mdr, SnapRealm *conrealm);
  int get_allowed_caps(CInode *in, Capability *cap, int &all_allowed,
                       int &loner_allowed, int &xlocker_allowed);
  int issue_caps(CInode *in, Capability *only_cap=0);
  void issue_caps_set(std::set<CInode*>& inset);
  void issue_truncate(CInode *in);
  void revoke_stale_cap(CInode *in, client_t client);
  bool revoke_stale_caps(Session *session);
  void resume_stale_caps(Session *session);
  void remove_stale_leases(Session *session);

  void request_inode_file_caps(CInode *in);

  bool check_client_ranges(CInode *in, uint64_t size);
  bool calc_new_client_ranges(CInode *in, uint64_t size,
			      bool *max_increased=nullptr);
  bool check_inode_max_size(CInode *in, bool force_wrlock=false,
                            uint64_t newmax=0, uint64_t newsize=0,
			    utime_t mtime=utime_t());
  void share_inode_max_size(CInode *in, Capability *only_cap=0);

  // -- client leases --
  void handle_client_lease(const cref_t<MClientLease> &m);

  void issue_client_lease(CDentry *dn, CInode *in, MDRequestRef &mdr, utime_t now, bufferlist &bl);
  void revoke_client_leases(SimpleLock *lock);
  static void encode_lease(bufferlist& bl, const session_info_t& info, const LeaseStat& ls);

protected:
  void send_lock_message(SimpleLock *lock, int msg);
  void send_lock_message(SimpleLock *lock, int msg, const bufferlist &data);

  // -- locks --
  void _drop_locks(MutationImpl *mut, std::set<CInode*> *pneed_issue, bool drop_rdlocks);

  void simple_eval(SimpleLock *lock, bool *need_issue);
  void handle_simple_lock(SimpleLock *lock, const cref_t<MLock> &m);

  void simple_lock(SimpleLock *lock, bool *need_issue=0);
  void simple_excl(SimpleLock *lock, bool *need_issue=0);
  void simple_xlock(SimpleLock *lock);

  void handle_scatter_lock(ScatterLock *lock, const cref_t<MLock> &m);
  bool scatter_scatter_fastpath(ScatterLock *lock);
  void scatter_scatter(ScatterLock *lock, bool nowait=false);
  void scatter_tempsync(ScatterLock *lock, bool *need_issue=0);

  void scatter_writebehind(ScatterLock *lock);

  void scatter_writebehind_finish(ScatterLock *lock, MutationRef& mut);

  bool _need_flush_mdlog(CInode *in, int wanted_caps, bool lock_state_any=false);
  void adjust_cap_wanted(Capability *cap, int wanted, int issue_seq);
  void handle_client_caps(const cref_t<MClientCaps> &m);
  void _update_cap_fields(CInode *in, int dirty, const cref_t<MClientCaps> &m, CInode::mempool_inode *pi);
  void _do_snap_update(CInode *in, snapid_t snap, int dirty, snapid_t follows, client_t client, const cref_t<MClientCaps> &m, const ref_t<MClientCaps> &ack);
  void _do_null_snapflush(CInode *head_in, client_t client, snapid_t last=CEPH_NOSNAP);
  bool _do_cap_update(CInode *in, Capability *cap, int dirty, snapid_t follows, const cref_t<MClientCaps> &m,
		      const ref_t<MClientCaps> &ack, bool *need_flush=NULL);
  void handle_client_cap_release(const cref_t<MClientCapRelease> &m);
  void _do_cap_release(client_t client, inodeno_t ino, uint64_t cap_id, ceph_seq_t mseq, ceph_seq_t seq);
  void caps_tick();

  bool local_wrlock_start(LocalLockC *lock, MDRequestRef& mut);
  void local_wrlock_finish(const MutationImpl::lock_iterator& it, MutationImpl *mut);
  bool local_xlock_start(LocalLockC *lock, MDRequestRef& mut);
  void local_xlock_finish(const MutationImpl::lock_iterator& it, MutationImpl *mut);

  void handle_file_lock(ScatterLock *lock, const cref_t<MLock> &m);
  void scatter_mix(ScatterLock *lock, bool *need_issue=0);
  void file_excl(ScatterLock *lock, bool *need_issue=0);
  void file_xsyn(SimpleLock *lock, bool *need_issue=0);

  void handle_inode_file_caps(const cref_t<MInodeFileCaps> &m);

  void file_update_finish(CInode *in, MutationRef& mut, unsigned flags,
			  client_t client, const ref_t<MClientCaps> &ack);

  xlist<ScatterLock*> updated_scatterlocks;

  // Maintain a global list to quickly find if any caps are late revoking
  xlist<Capability*> revoking_caps;
  // Maintain a per-client list to find clients responsible for late ones quickly
  std::map<client_t, xlist<Capability*> > revoking_caps_by_client;

  elist<CInode*> need_snapflush_inodes;

private:
  friend class C_MDL_CheckMaxSize;
  friend class C_MDL_RequestInodeFileCaps;
  friend class C_Locker_FileUpdate_finish;
  friend class C_Locker_RetryCapRelease;
  friend class C_Locker_Eval;
  friend class C_Locker_ScatterWB;
  friend class LockerContext;
  friend class LockerLogContext;

  bool any_late_revoking_caps(xlist<Capability*> const &revoking, double timeout) const;
  uint64_t calc_new_max_size(const CInode::inode_const_ptr& pi, uint64_t size);

  MDSRank *mds;
  MDCache *mdcache;
  xlist<ScatterLock*> updated_filelocks;
};
#endif