Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mds / Locker.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_LOCKER_H
16 #define CEPH_MDS_LOCKER_H
17
18 #include "include/types.h"
19
20 #include <map>
21 #include <list>
22 #include <set>
23 using std::map;
24 using std::list;
25 using std::set;
26
27 class MDSRank;
28 class Session;
29 class CInode;
30 class CDentry;
31 struct SnapRealm;
32
33 class Message;
34
35 class MLock;
36
37 class Capability;
38
39 class SimpleLock;
40 class ScatterLock;
41 class LocalLock;
42
43 #include "SimpleLock.h"
44 #include "Mutation.h"
45
46 class Locker {
47 private:
48   MDSRank *mds;
49   MDCache *mdcache;
50  
51  public:
52   Locker(MDSRank *m, MDCache *c) : mds(m), mdcache(c) {}  
53
54   SimpleLock *get_lock(int lock_type, MDSCacheObjectInfo &info);
55   
56   void dispatch(Message *m);
57   void handle_lock(MLock *m);
58
59   void tick();
60
61   void nudge_log(SimpleLock *lock);
62
63 protected:
64   void send_lock_message(SimpleLock *lock, int msg);
65   void send_lock_message(SimpleLock *lock, int msg, const bufferlist &data);
66
67   // -- locks --
68   void _drop_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue);
69   void _drop_non_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue);
70 public:
71   void include_snap_rdlocks(set<SimpleLock*>& rdlocks, CInode *in);
72   void include_snap_rdlocks_wlayout(set<SimpleLock*>& rdlocks, CInode *in,
73                                     file_layout_t **layout);
74
75   bool acquire_locks(MDRequestRef& mdr,
76                      set<SimpleLock*> &rdlocks,
77                      set<SimpleLock*> &wrlocks,
78                      set<SimpleLock*> &xlocks,
79                      map<SimpleLock*,mds_rank_t> *remote_wrlocks=NULL,
80                      CInode *auth_pin_freeze=NULL,
81                      bool auth_pin_nonblock=false);
82
83   void notify_freeze_waiter(MDSCacheObject *o);
84   void cancel_locking(MutationImpl *mut, set<CInode*> *pneed_issue);
85   void drop_locks(MutationImpl *mut, set<CInode*> *pneed_issue=0);
86   void set_xlocks_done(MutationImpl *mut, bool skip_dentry=false);
87   void drop_non_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue=0);
88   void drop_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue=0);
89
90   void eval_gather(SimpleLock *lock, bool first=false, bool *need_issue=0, list<MDSInternalContextBase*> *pfinishers=0);
91   void eval(SimpleLock *lock, bool *need_issue);
92   void eval_any(SimpleLock *lock, bool *need_issue, list<MDSInternalContextBase*> *pfinishers=0, bool first=false) {
93     if (!lock->is_stable())
94       eval_gather(lock, first, need_issue, pfinishers);
95     else if (lock->get_parent()->is_auth())
96       eval(lock, need_issue);
97   }
98
99   void eval_scatter_gathers(CInode *in);
100
101   void eval_cap_gather(CInode *in, set<CInode*> *issue_set=0);
102
103   bool eval(CInode *in, int mask, bool caps_imported=false);
104   void try_eval(MDSCacheObject *p, int mask);
105   void try_eval(SimpleLock *lock, bool *pneed_issue);
106
107   bool _rdlock_kick(SimpleLock *lock, bool as_anon);
108   bool rdlock_try(SimpleLock *lock, client_t client, MDSInternalContextBase *c);
109   bool rdlock_start(SimpleLock *lock, MDRequestRef& mut, bool as_anon=false);
110   void rdlock_finish(SimpleLock *lock, MutationImpl *mut, bool *pneed_issue);
111   bool can_rdlock_set(set<SimpleLock*>& locks);
112   bool rdlock_try_set(set<SimpleLock*>& locks);
113   void rdlock_take_set(set<SimpleLock*>& locks, MutationRef& mut);
114
115   void wrlock_force(SimpleLock *lock, MutationRef& mut);
116   bool wrlock_start(SimpleLock *lock, MDRequestRef& mut, bool nowait=false);
117   void wrlock_finish(SimpleLock *lock, MutationImpl *mut, bool *pneed_issue);
118
119   void remote_wrlock_start(SimpleLock *lock, mds_rank_t target, MDRequestRef& mut);
120   void remote_wrlock_finish(SimpleLock *lock, mds_rank_t target, MutationImpl *mut);
121
122   bool xlock_start(SimpleLock *lock, MDRequestRef& mut);
123   void _finish_xlock(SimpleLock *lock, client_t xlocker, bool *pneed_issue);
124   void xlock_finish(SimpleLock *lock, MutationImpl *mut, bool *pneed_issue);
125
126   void xlock_export(SimpleLock *lock, MutationImpl *mut);
127   void xlock_import(SimpleLock *lock);
128
129
130   // simple
131 public:
132   void try_simple_eval(SimpleLock *lock);
133   bool simple_rdlock_try(SimpleLock *lock, MDSInternalContextBase *con);
134 protected:
135   void simple_eval(SimpleLock *lock, bool *need_issue);
136   void handle_simple_lock(SimpleLock *lock, MLock *m);
137
138 public:
139   bool simple_sync(SimpleLock *lock, bool *need_issue=0);
140 protected:
141   void simple_lock(SimpleLock *lock, bool *need_issue=0);
142   void simple_excl(SimpleLock *lock, bool *need_issue=0);
143   void simple_xlock(SimpleLock *lock);
144
145
146   // scatter
147 public:
148   void scatter_eval(ScatterLock *lock, bool *need_issue);        // public for MDCache::adjust_subtree_auth()
149
150   void scatter_tick();
151   void scatter_nudge(ScatterLock *lock, MDSInternalContextBase *c, bool forcelockchange=false);
152
153 protected:
154   void handle_scatter_lock(ScatterLock *lock, MLock *m);
155   bool scatter_scatter_fastpath(ScatterLock *lock);
156   void scatter_scatter(ScatterLock *lock, bool nowait=false);
157   void scatter_tempsync(ScatterLock *lock, bool *need_issue=0);
158
159   void scatter_writebehind(ScatterLock *lock);
160
161   void scatter_writebehind_finish(ScatterLock *lock, MutationRef& mut);
162
163   xlist<ScatterLock*> updated_scatterlocks;
164 public:
165   void mark_updated_scatterlock(ScatterLock *lock);
166
167
168   void handle_reqrdlock(SimpleLock *lock, MLock *m);
169
170
171
172   // caps
173
174   // when to defer processing client cap release or writeback due to being
175   // frozen.  the condition must be consistent across handle_client_caps and
176   // process_request_cap_release to preserve ordering.
177   bool should_defer_client_cap_frozen(CInode *in);
178
179   void process_request_cap_release(MDRequestRef& mdr, client_t client, const ceph_mds_request_release& r,
180                                    const string &dname);
181
182   void kick_cap_releases(MDRequestRef& mdr);
183   void kick_issue_caps(CInode *in, client_t client, ceph_seq_t seq);
184
185   void remove_client_cap(CInode *in, client_t client);
186
187   void get_late_revoking_clients(std::list<client_t> *result) const;
188   bool any_late_revoking_caps(xlist<Capability*> const &revoking) const;
189
190  protected:
191   bool _need_flush_mdlog(CInode *in, int wanted_caps);
192   void adjust_cap_wanted(Capability *cap, int wanted, int issue_seq);
193   void handle_client_caps(class MClientCaps *m);
194   void _update_cap_fields(CInode *in, int dirty, MClientCaps *m, inode_t *pi);
195   void _do_snap_update(CInode *in, snapid_t snap, int dirty, snapid_t follows, client_t client, MClientCaps *m, MClientCaps *ack);
196   void _do_null_snapflush(CInode *head_in, client_t client, snapid_t last=CEPH_NOSNAP);
197   bool _do_cap_update(CInode *in, Capability *cap, int dirty, snapid_t follows, MClientCaps *m,
198                       MClientCaps *ack=0, bool *need_flush=NULL);
199   void handle_client_cap_release(class MClientCapRelease *m);
200   void _do_cap_release(client_t client, inodeno_t ino, uint64_t cap_id, ceph_seq_t mseq, ceph_seq_t seq);
201   void caps_tick();
202
203   // Maintain a global list to quickly find if any caps are late revoking
204   xlist<Capability*> revoking_caps;
205   // Maintain a per-client list to find clients responsible for late ones quickly
206   std::map<client_t, xlist<Capability*> > revoking_caps_by_client;
207
208   // local
209 public:
210   void local_wrlock_grab(LocalLock *lock, MutationRef& mut);
211 protected:
212   bool local_wrlock_start(LocalLock *lock, MDRequestRef& mut);
213   void local_wrlock_finish(LocalLock *lock, MutationImpl *mut);
214   bool local_xlock_start(LocalLock *lock, MDRequestRef& mut);
215   void local_xlock_finish(LocalLock *lock, MutationImpl *mut);
216
217
218   // file
219 public:
220   void file_eval(ScatterLock *lock, bool *need_issue);
221 protected:
222   void handle_file_lock(ScatterLock *lock, MLock *m);
223   void scatter_mix(ScatterLock *lock, bool *need_issue=0);
224   void file_excl(ScatterLock *lock, bool *need_issue=0);
225   void file_xsyn(SimpleLock *lock, bool *need_issue=0);
226
227 public:
228   void file_recover(ScatterLock *lock);
229
230 private:
231   xlist<ScatterLock*> updated_filelocks;
232 public:
233   void mark_updated_Filelock(ScatterLock *lock);
234
235   // -- file i/o --
236 public:
237   version_t issue_file_data_version(CInode *in);
238   Capability* issue_new_caps(CInode *in, int mode, Session *session, SnapRealm *conrealm, bool is_replay);
239   bool issue_caps(CInode *in, Capability *only_cap=0);
240   void issue_caps_set(set<CInode*>& inset);
241   void issue_truncate(CInode *in);
242   void revoke_stale_caps(Session *session);
243   void revoke_stale_caps(Capability *cap);
244   void resume_stale_caps(Session *session);
245   void remove_stale_leases(Session *session);
246
247 public:
248   void request_inode_file_caps(CInode *in);
249 protected:
250   void handle_inode_file_caps(class MInodeFileCaps *m);
251
252   void file_update_finish(CInode *in, MutationRef& mut, bool share_max, bool issue_client_cap,
253                           client_t client, MClientCaps *ack);
254 private:
255   uint64_t calc_new_max_size(inode_t *pi, uint64_t size);
256 public:
257   void calc_new_client_ranges(CInode *in, uint64_t size,
258                               map<client_t, client_writeable_range_t>* new_ranges,
259                               bool *max_increased);
260   bool check_inode_max_size(CInode *in, bool force_wrlock=false,
261                             uint64_t newmax=0, uint64_t newsize=0,
262                             utime_t mtime=utime_t());
263   void share_inode_max_size(CInode *in, Capability *only_cap=0);
264
265 private:
266   friend class C_MDL_CheckMaxSize;
267   friend class C_MDL_RequestInodeFileCaps;
268   friend class C_Locker_FileUpdate_finish;
269   friend class C_Locker_RetryCapRelease;
270   friend class C_Locker_Eval;
271   friend class C_Locker_ScatterWB;
272   friend class LockerContext;
273   friend class LockerLogContext;
274
275   
276   // -- client leases --
277 public:
278   void handle_client_lease(struct MClientLease *m);
279
280   void issue_client_lease(CDentry *dn, client_t client, bufferlist &bl, utime_t now, Session *session);
281   void revoke_client_leases(SimpleLock *lock);
282 };
283
284
285 #endif