Whamcloud - gitweb
6c697930e356adf7f13a2be3a1b0ee524d30ba5c
[fs/lustre-release.git] / lustre / mdd / mdd_lfsck.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, Intel Corporation.
24  */
25 /*
26  * lustre/mdd/mdd_lfsck.c
27  *
28  * Top-level entry points into mdd module
29  *
30  * LFSCK controller, which scans the whole device through low layer
31  * iteration APIs, drives all lfsck compeonents, controls the speed.
32  *
33  * Author: Fan Yong <yong.fan@whamcloud.com>
34  */
35
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <lustre/lustre_idl.h>
42 #include <lustre_fid.h>
43
44 #include "mdd_internal.h"
45
46 static inline char *mdd_lfsck2name(struct md_lfsck *lfsck)
47 {
48         struct mdd_device *mdd;
49
50         mdd = container_of0(lfsck, struct mdd_device, mdd_lfsck);
51         return mdd2obd_dev(mdd)->obd_name;
52 }
53
54 void mdd_lfsck_set_speed(struct md_lfsck *lfsck, __u32 limit)
55 {
56         spin_lock(&lfsck->ml_lock);
57         lfsck->ml_speed_limit = limit;
58         if (limit != LFSCK_SPEED_NO_LIMIT) {
59                 if (limit > CFS_HZ) {
60                         lfsck->ml_sleep_rate = limit / CFS_HZ;
61                         lfsck->ml_sleep_jif = 1;
62                 } else {
63                         lfsck->ml_sleep_rate = 1;
64                         lfsck->ml_sleep_jif = CFS_HZ / limit;
65                 }
66         } else {
67                 lfsck->ml_sleep_jif = 0;
68                 lfsck->ml_sleep_rate = 0;
69         }
70         spin_unlock(&lfsck->ml_lock);
71 }
72
73 static void mdd_lfsck_control_speed(struct md_lfsck *lfsck)
74 {
75         struct ptlrpc_thread *thread = &lfsck->ml_thread;
76         struct l_wait_info    lwi;
77
78         if (lfsck->ml_sleep_jif > 0 &&
79             lfsck->ml_new_scanned >= lfsck->ml_sleep_rate) {
80                 spin_lock(&lfsck->ml_lock);
81                 if (likely(lfsck->ml_sleep_jif > 0 &&
82                            lfsck->ml_new_scanned >= lfsck->ml_sleep_rate)) {
83                         lwi = LWI_TIMEOUT_INTR(lfsck->ml_sleep_jif, NULL,
84                                                LWI_ON_SIGNAL_NOOP, NULL);
85                         spin_unlock(&lfsck->ml_lock);
86
87                         l_wait_event(thread->t_ctl_waitq,
88                                      !thread_is_running(thread),
89                                      &lwi);
90                         lfsck->ml_new_scanned = 0;
91                 } else {
92                         spin_unlock(&lfsck->ml_lock);
93                 }
94         }
95 }
96
97 static int mdd_lfsck_main(void *args)
98 {
99         struct lu_env            env;
100         struct md_lfsck         *lfsck  = (struct md_lfsck *)args;
101         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
102         struct dt_object        *obj    = lfsck->ml_it_obj;
103         const struct dt_it_ops  *iops   = &obj->do_index_ops->dio_it;
104         struct dt_it            *di;
105         struct lu_fid           *fid;
106         int                      rc;
107         ENTRY;
108
109         cfs_daemonize("lfsck");
110         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
111         if (rc != 0) {
112                 CERROR("%s: LFSCK, fail to init env, rc = %d\n",
113                        mdd_lfsck2name(lfsck), rc);
114                 GOTO(noenv, rc);
115         }
116
117         di = iops->init(&env, obj, lfsck->ml_args, BYPASS_CAPA);
118         if (IS_ERR(di)) {
119                 rc = PTR_ERR(di);
120                 CERROR("%s: LFSCK, fail to init iteration, rc = %d\n",
121                        mdd_lfsck2name(lfsck), rc);
122                 GOTO(fini_env, rc);
123         }
124
125         CDEBUG(D_LFSCK, "LFSCK: flags = 0x%x, pid = %d\n",
126                lfsck->ml_args, cfs_curproc_pid());
127
128         spin_lock(&lfsck->ml_lock);
129         thread_set_flags(thread, SVC_RUNNING);
130         spin_unlock(&lfsck->ml_lock);
131         cfs_waitq_broadcast(&thread->t_ctl_waitq);
132
133         /* The call iops->load() will unplug low layer iteration. */
134         rc = iops->load(&env, di, 0);
135         if (rc != 0)
136                 GOTO(out, rc);
137
138         CDEBUG(D_LFSCK, "LFSCK: iteration start: pos = %s\n",
139                (char *)iops->key(&env, di));
140
141         lfsck->ml_new_scanned = 0;
142         fid = &mdd_env_info(&env)->mti_fid;
143         while (rc == 0) {
144                 iops->rec(&env, di, (struct dt_rec *)fid, 0);
145
146                 /* XXX: here, perform LFSCK when some LFSCK component(s)
147                  *      introduced in the future. */
148                 lfsck->ml_new_scanned++;
149
150                 /* XXX: here, make checkpoint when some LFSCK component(s)
151                  *      introduced in the future. */
152
153                 /* Rate control. */
154                 mdd_lfsck_control_speed(lfsck);
155                 if (unlikely(!thread_is_running(thread)))
156                         GOTO(out, rc = 0);
157
158                 rc = iops->next(&env, di);
159         }
160
161         GOTO(out, rc);
162
163 out:
164         if (lfsck->ml_paused) {
165                 /* XXX: It is hack here: if the lfsck is still running when MDS
166                  *      umounts, it should be restarted automatically after MDS
167                  *      remounts up.
168                  *
169                  *      To support that, we need to record the lfsck status in
170                  *      the lfsck on-disk bookmark file. But now, there is not
171                  *      lfsck component under the lfsck framework. To avoid to
172                  *      introduce unnecessary bookmark incompatibility issues,
173                  *      we write nothing to the lfsck bookmark file now.
174                  *
175                  *      Instead, we will reuse dt_it_ops::put() method to notify
176                  *      low layer iterator to process such case.
177                  *
178                  *      It is just temporary solution, and will be replaced when
179                  *      some lfsck component is introduced in the future. */
180                 iops->put(&env, di);
181                 CDEBUG(D_LFSCK, "LFSCK: iteration pasued: pos = %s, rc = %d\n",
182                        (char *)iops->key(&env, di), rc);
183         } else {
184                 CDEBUG(D_LFSCK, "LFSCK: iteration stop: pos = %s, rc = %d\n",
185                        (char *)iops->key(&env, di), rc);
186         }
187         iops->fini(&env, di);
188
189 fini_env:
190         lu_env_fini(&env);
191
192 noenv:
193         spin_lock(&lfsck->ml_lock);
194         thread_set_flags(thread, SVC_STOPPED);
195         cfs_waitq_broadcast(&thread->t_ctl_waitq);
196         spin_unlock(&lfsck->ml_lock);
197         return rc;
198 }
199
200 int mdd_lfsck_start(const struct lu_env *env, struct md_lfsck *lfsck,
201                     struct lfsck_start *start)
202 {
203         struct ptlrpc_thread *thread  = &lfsck->ml_thread;
204         struct l_wait_info    lwi     = { 0 };
205         int                   rc      = 0;
206         __u16                 valid   = 0;
207         __u16                 flags   = 0;
208         ENTRY;
209
210         if (lfsck->ml_it_obj == NULL)
211                 RETURN(-ENOTSUPP);
212
213         mutex_lock(&lfsck->ml_mutex);
214         spin_lock(&lfsck->ml_lock);
215         if (thread_is_running(thread)) {
216                 spin_unlock(&lfsck->ml_lock);
217                 mutex_unlock(&lfsck->ml_mutex);
218                 RETURN(-EALREADY);
219         }
220
221         spin_unlock(&lfsck->ml_lock);
222         if (start->ls_valid & LSV_SPEED_LIMIT)
223                 mdd_lfsck_set_speed(lfsck, start->ls_speed_limit);
224
225         if (start->ls_valid & LSV_ERROR_HANDLE) {
226                 valid |= DOIV_ERROR_HANDLE;
227                 if (start->ls_flags & LPF_FAILOUT)
228                         flags |= DOIF_FAILOUT;
229         }
230
231         /* XXX: 1. low layer does not care 'dryrun'.
232          *      2. will process 'ls_active' when introduces LFSCK for layout
233          *         consistency, DNE consistency, and so on in the future. */
234         start->ls_active = 0;
235
236         if (start->ls_flags & LPF_RESET)
237                 flags |= DOIF_RESET;
238
239         if (start->ls_active != 0)
240                 flags |= DOIF_OUTUSED;
241
242         lfsck->ml_args = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
243         thread_set_flags(thread, 0);
244         rc = cfs_create_thread(mdd_lfsck_main, lfsck, 0);
245         if (rc < 0)
246                 CERROR("%s: cannot start LFSCK thread, rc = %d\n",
247                        mdd_lfsck2name(lfsck), rc);
248         else
249                 l_wait_event(thread->t_ctl_waitq,
250                              thread_is_running(thread) ||
251                              thread_is_stopped(thread),
252                              &lwi);
253         mutex_unlock(&lfsck->ml_mutex);
254
255         RETURN(rc < 0 ? rc : 0);
256 }
257
258 int mdd_lfsck_stop(const struct lu_env *env, struct md_lfsck *lfsck)
259 {
260         struct ptlrpc_thread *thread = &lfsck->ml_thread;
261         struct l_wait_info    lwi    = { 0 };
262         ENTRY;
263
264         mutex_lock(&lfsck->ml_mutex);
265         spin_lock(&lfsck->ml_lock);
266         if (thread_is_init(thread) || thread_is_stopped(thread)) {
267                 spin_unlock(&lfsck->ml_lock);
268                 mutex_unlock(&lfsck->ml_mutex);
269                 RETURN(-EALREADY);
270         }
271
272         thread_set_flags(thread, SVC_STOPPING);
273         spin_unlock(&lfsck->ml_lock);
274
275         cfs_waitq_broadcast(&thread->t_ctl_waitq);
276         l_wait_event(thread->t_ctl_waitq,
277                      thread_is_stopped(thread),
278                      &lwi);
279         mutex_unlock(&lfsck->ml_mutex);
280
281         RETURN(0);
282 }
283
284 const char lfsck_bookmark_name[] = "lfsck_bookmark";
285
286 static const struct lu_fid lfsck_it_fid = { .f_seq = FID_SEQ_LOCAL_FILE,
287                                             .f_oid = OTABLE_IT_OID,
288                                             .f_ver = 0 };
289
290 int mdd_lfsck_setup(const struct lu_env *env, struct mdd_device *mdd)
291 {
292         struct md_lfsck  *lfsck = &mdd->mdd_lfsck;
293         struct dt_object *obj;
294         int               rc;
295
296         memset(lfsck, 0, sizeof(*lfsck));
297         lfsck->ml_version = LFSCK_VERSION_V1;
298         cfs_waitq_init(&lfsck->ml_thread.t_ctl_waitq);
299         mutex_init(&lfsck->ml_mutex);
300         spin_lock_init(&lfsck->ml_lock);
301
302         obj = dt_store_open(env, mdd->mdd_child, "", lfsck_bookmark_name,
303                             &mdd_env_info(env)->mti_fid);
304         if (IS_ERR(obj))
305                 return PTR_ERR(obj);
306
307         lfsck->ml_bookmark_obj = obj;
308
309         obj = dt_locate(env, mdd->mdd_child, &lfsck_it_fid);
310         if (IS_ERR(obj))
311                 return PTR_ERR(obj);
312
313         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
314         if (rc != 0) {
315                 lu_object_put(env, &obj->do_lu);
316                 if (rc == -ENOTSUPP)
317                         rc = 0;
318                 return rc;
319         }
320
321         lfsck->ml_it_obj = obj;
322
323         return 0;
324 }
325
326 void mdd_lfsck_cleanup(const struct lu_env *env, struct mdd_device *mdd)
327 {
328         struct md_lfsck *lfsck = &mdd->mdd_lfsck;
329
330         if (lfsck->ml_it_obj != NULL) {
331                 lfsck->ml_paused = 1;
332                 mdd_lfsck_stop(env, lfsck);
333                 lu_object_put(env, &lfsck->ml_it_obj->do_lu);
334                 lfsck->ml_it_obj = NULL;
335         }
336
337         if (lfsck->ml_bookmark_obj != NULL) {
338                 lu_object_put(env, &lfsck->ml_bookmark_obj->do_lu);
339                 lfsck->ml_bookmark_obj = NULL;
340         }
341 }