]> git.proxmox.com Git - ceph.git/blob - ceph/src/mds/ScrubStack.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / mds / ScrubStack.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) 2014 Red Hat
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 SCRUBSTACK_H_
16 #define SCRUBSTACK_H_
17
18 #include "CDir.h"
19 #include "CDentry.h"
20 #include "CInode.h"
21 #include "MDSContext.h"
22 #include "ScrubHeader.h"
23
24 #include "common/LogClient.h"
25 #include "include/elist.h"
26 #include "messages/MMDSScrub.h"
27 #include "messages/MMDSScrubStats.h"
28
29 class MDCache;
30 class Finisher;
31
32 class ScrubStack {
33 public:
34 ScrubStack(MDCache *mdc, LogChannelRef &clog, Finisher *finisher_) :
35 mdcache(mdc),
36 clog(clog),
37 finisher(finisher_),
38 scrub_stack(member_offset(MDSCacheObject, item_scrub)),
39 scrub_waiting(member_offset(MDSCacheObject, item_scrub)) {}
40 ~ScrubStack() {
41 ceph_assert(scrub_stack.empty());
42 ceph_assert(!scrubs_in_progress);
43 }
44 /**
45 * Put the inode at either the top or bottom of the stack, with the
46 * given scrub params, and kick off more scrubbing.
47 * @param in The inode to scrub
48 * @param header The ScrubHeader propagated from wherever this scrub
49 */
50 int enqueue(CInode *in, ScrubHeaderRef& header, bool top);
51 /**
52 * Abort an ongoing scrub operation. The abort operation could be
53 * delayed if there are in-progress scrub operations on going. The
54 * caller should provide a context which is completed after all
55 * in-progress scrub operations are completed and pending inodes
56 * are removed from the scrub stack (with the context callbacks for
57 * inodes completed with -CEPHFS_ECANCELED).
58 * @param on_finish Context callback to invoke after abort
59 */
60 void scrub_abort(Context *on_finish);
61
62 /**
63 * Pause scrub operations. Similar to abort, pause is delayed if
64 * there are in-progress scrub operations on going. The caller
65 * should provide a context which is completed after all in-progress
66 * scrub operations are completed. Subsequent scrub operations are
67 * queued until scrub is resumed.
68 * @param on_finish Context callback to invoke after pause
69 */
70 void scrub_pause(Context *on_finish);
71
72 /**
73 * Resume a paused scrub. Unlike abort or pause, this is instantaneous.
74 * Pending pause operations are cancelled (context callbacks are
75 * invoked with -CEPHFS_ECANCELED).
76 * @returns 0 (success) if resumed, -CEPHFS_EINVAL if an abort is in-progress.
77 */
78 bool scrub_resume();
79
80 /**
81 * Get the current scrub status as human readable string. Some basic
82 * information is returned such as number of inodes pending abort/pause.
83 */
84 void scrub_status(Formatter *f);
85
86 /**
87 * Get a high level scrub status summary such as current scrub state
88 * and scrub paths.
89 */
90 std::string_view scrub_summary();
91
92 static bool is_idle(std::string_view state_str) {
93 return state_str == "idle";
94 }
95
96 bool is_scrubbing() const { return !scrub_stack.empty(); }
97
98 void advance_scrub_status();
99
100 void handle_mds_failure(mds_rank_t mds);
101
102 void dispatch(const cref_t<Message> &m);
103
104 MDCache *mdcache;
105
106 protected:
107
108 // reference to global cluster log client
109 LogChannelRef &clog;
110
111 /// A finisher needed so that we don't re-enter kick_off_scrubs
112 Finisher *finisher;
113
114 /// The stack of inodes we want to scrub
115 elist<MDSCacheObject*> scrub_stack;
116 elist<MDSCacheObject*> scrub_waiting;
117 /// current number of dentries we're actually scrubbing
118 int scrubs_in_progress = 0;
119 int stack_size = 0;
120
121 struct scrub_remote_t {
122 std::string tag;
123 std::set<mds_rank_t> gather_set;
124 };
125 std::map<CInode*, scrub_remote_t> remote_scrubs;
126
127 unsigned scrub_epoch = 2;
128 unsigned scrub_epoch_fully_acked = 0;
129 unsigned scrub_epoch_last_abort = 2;
130 // check if any mds is aborting scrub after mds.0 starts
131 bool scrub_any_peer_aborting = true;
132
133 struct scrub_stat_t {
134 unsigned epoch_acked = 0;
135 std::set<std::string> scrubbing_tags;
136 bool aborting = false;
137 };
138 std::vector<scrub_stat_t> mds_scrub_stats;
139
140 std::map<std::string, ScrubHeaderRef> scrubbing_map;
141
142 friend class C_RetryScrub;
143 private:
144 // scrub abort is _not_ a state, rather it's an operation that's
145 // performed after in-progress scrubs are finished.
146 enum State {
147 STATE_RUNNING = 0,
148 STATE_IDLE,
149 STATE_PAUSING,
150 STATE_PAUSED,
151 };
152 friend std::ostream &operator<<(std::ostream &os, const State &state);
153
154 friend class C_InodeValidated;
155
156 int _enqueue(MDSCacheObject *obj, ScrubHeaderRef& header, bool top);
157 /**
158 * Remove the inode/dirfrag from the stack.
159 */
160 inline void dequeue(MDSCacheObject *obj);
161
162 /**
163 * Kick off as many scrubs as are appropriate, based on the current
164 * state of the stack.
165 */
166 void kick_off_scrubs();
167
168 /**
169 * Move the inode/dirfrag that can't be scrubbed immediately
170 * from scrub queue to waiting list.
171 */
172 void add_to_waiting(MDSCacheObject *obj);
173 /**
174 * Move the inode/dirfrag back to scrub queue.
175 */
176 void remove_from_waiting(MDSCacheObject *obj, bool kick=true);
177 /**
178 * Validate authority of the inode. If current mds is not auth of the inode,
179 * forword scrub to auth mds.
180 */
181 bool validate_inode_auth(CInode *in);
182
183 /**
184 * Scrub a file inode.
185 * @param in The inode to scrub
186 */
187 void scrub_file_inode(CInode *in);
188
189 /**
190 * Callback from completion of CInode::validate_disk_state
191 * @param in The inode we were validating
192 * @param r The return status from validate_disk_state
193 * @param result Populated results from validate_disk_state
194 */
195 void _validate_inode_done(CInode *in, int r,
196 const CInode::validated_data &result);
197
198 /**
199 * Scrub a directory inode. It queues child dirfrags, then does
200 * final scrub of the inode.
201 *
202 * @param in The directory indoe to scrub
203 * @param added_children set to true if we pushed some of our children
204 * @param done set to true if we started to do final scrub
205 */
206 void scrub_dir_inode(CInode *in, bool *added_children, bool *done);
207 /**
208 * Scrub a dirfrag. It queues child dentries, then does final
209 * scrub of the dirfrag.
210 *
211 * @param dir The dirfrag to scrub (must be auth)
212 * @param done set to true if we started to do final scrub
213 */
214 void scrub_dirfrag(CDir *dir, bool *done);
215 /**
216 * Scrub a directory-representing dentry.
217 *
218 * @param in The directory inode we're doing final scrub on.
219 */
220 void scrub_dir_inode_final(CInode *in);
221 /**
222 * Set scrub state
223 * @param next_state State to move the scrub to.
224 */
225 void set_state(State next_state);
226
227 /**
228 * Is scrub in one of transition states (running, pausing)
229 */
230 bool scrub_in_transition_state();
231
232 /**
233 * complete queued up contexts
234 * @param r return value to complete contexts.
235 */
236 void complete_control_contexts(int r);
237
238 /**
239 * ask peer mds (rank > 0) to abort/pause/resume scrubs
240 */
241 void send_state_message(int op);
242
243 /**
244 * Abort pending scrubs for inodes waiting in the inode stack.
245 * Completion context is complete with -CEPHFS_ECANCELED.
246 */
247 void abort_pending_scrubs();
248
249 /**
250 * Return path for a given inode.
251 * @param in inode to make path entry.
252 */
253 std::string scrub_inode_path(CInode *in) {
254 std::string path;
255 in->make_path_string(path, true);
256 return (path.empty() ? "/" : path.c_str());
257 }
258
259 /**
260 * Send scrub information (queued/finished scrub path and summary)
261 * to cluster log.
262 * @param in inode for which scrub has been queued or finished.
263 */
264 void clog_scrub_summary(CInode *in=nullptr);
265
266 void handle_scrub(const cref_t<MMDSScrub> &m);
267 void handle_scrub_stats(const cref_t<MMDSScrubStats> &m);
268
269 State state = STATE_IDLE;
270 bool clear_stack = false;
271
272 // list of pending context completions for asynchronous scrub
273 // control operations.
274 std::vector<Context *> control_ctxs;
275 };
276
277 #endif /* SCRUBSTACK_H_ */