Whamcloud - gitweb
LU-6158 mdt: always shrink_capsule in getxattr_all
[fs/lustre-release.git] / lustre / ptlrpc / recover.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/recover.c
37  *
38  * Author: Mike Shaver <shaver@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include <libcfs/libcfs.h>
43 #include <obd_support.h>
44 #include <lustre_ha.h>
45 #include <lustre_net.h>
46 #include <lustre_import.h>
47 #include <lustre_export.h>
48 #include <obd.h>
49 #include <obd_class.h>
50 #include <libcfs/list.h>
51
52 #include "ptlrpc_internal.h"
53
54 /**
55  * Start recovery on disconnected import.
56  * This is done by just attempting a connect
57  */
58 void ptlrpc_initiate_recovery(struct obd_import *imp)
59 {
60         ENTRY;
61
62         CDEBUG(D_HA, "%s: starting recovery\n", obd2cli_tgt(imp->imp_obd));
63         ptlrpc_connect_import(imp);
64
65         EXIT;
66 }
67
68 /**
69  * Identify what request from replay list needs to be replayed next
70  * (based on what we have already replayed) and send it to server.
71  */
72 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
73 {
74         int rc = 0;
75         struct list_head *tmp, *pos;
76         struct ptlrpc_request *req = NULL;
77         __u64 last_transno;
78         ENTRY;
79
80         *inflight = 0;
81
82         /* It might have committed some after we last spoke, so make sure we
83          * get rid of them now.
84          */
85         spin_lock(&imp->imp_lock);
86         imp->imp_last_transno_checked = 0;
87         ptlrpc_free_committed(imp);
88         last_transno = imp->imp_last_replay_transno;
89         spin_unlock(&imp->imp_lock);
90
91         CDEBUG(D_HA, "import %p from %s committed "LPU64" last "LPU64"\n",
92                imp, obd2cli_tgt(imp->imp_obd),
93                imp->imp_peer_committed_transno, last_transno);
94
95         /* Do I need to hold a lock across this iteration?  We shouldn't be
96          * racing with any additions to the list, because we're in recovery
97          * and are therefore not processing additional requests to add.  Calls
98          * to ptlrpc_free_committed might commit requests, but nothing "newer"
99          * than the one we're replaying (it can't be committed until it's
100          * replayed, and we're doing that here).  l_f_e_safe protects against
101          * problems with the current request being committed, in the unlikely
102          * event of that race.  So, in conclusion, I think that it's safe to
103          * perform this list-walk without the imp_lock held.
104          *
105          * But, the {mdc,osc}_replay_open callbacks both iterate
106          * request lists, and have comments saying they assume the
107          * imp_lock is being held by ptlrpc_replay, but it's not. it's
108          * just a little race...
109          */
110
111         /* Replay all the committed open requests on committed_list first */
112         if (!list_empty(&imp->imp_committed_list)) {
113                 tmp = imp->imp_committed_list.prev;
114                 req = list_entry(tmp, struct ptlrpc_request,
115                                      rq_replay_list);
116
117                 /* The last request on committed_list hasn't been replayed */
118                 if (req->rq_transno > last_transno) {
119                         /* Since the imp_committed_list is immutable before
120                          * all of it's requests being replayed, it's safe to
121                          * use a cursor to accelerate the search */
122                         if (!imp->imp_resend_replay ||
123                             imp->imp_replay_cursor == &imp->imp_committed_list)
124                                 imp->imp_replay_cursor =
125                                         imp->imp_replay_cursor->next;
126
127                         while (imp->imp_replay_cursor !=
128                                &imp->imp_committed_list) {
129                                 req = list_entry(imp->imp_replay_cursor,
130                                                      struct ptlrpc_request,
131                                                      rq_replay_list);
132                                 if (req->rq_transno > last_transno)
133                                         break;
134
135                                 req = NULL;
136                                 imp->imp_replay_cursor =
137                                         imp->imp_replay_cursor->next;
138                         }
139                 } else {
140                         /* All requests on committed_list have been replayed */
141                         imp->imp_replay_cursor = &imp->imp_committed_list;
142                         req = NULL;
143                 }
144         }
145
146         /* All the requests in committed list have been replayed, let's replay
147          * the imp_replay_list */
148         if (req == NULL) {
149                 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
150                         req = list_entry(tmp, struct ptlrpc_request,
151                                              rq_replay_list);
152
153                         if (req->rq_transno > last_transno)
154                                 break;
155                         req = NULL;
156                 }
157         }
158
159         /* If need to resend the last sent transno (because a reconnect
160          * has occurred), then stop on the matching req and send it again.
161          * If, however, the last sent transno has been committed then we 
162          * continue replay from the next request. */
163         if (req != NULL && imp->imp_resend_replay)
164                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
165
166         spin_lock(&imp->imp_lock);
167         imp->imp_resend_replay = 0;
168         spin_unlock(&imp->imp_lock);
169
170         if (req != NULL) {
171                 rc = ptlrpc_replay_req(req);
172                 if (rc) {
173                         CERROR("recovery replay error %d for req "
174                                LPU64"\n", rc, req->rq_xid);
175                         RETURN(rc);
176                 }
177                 *inflight = 1;
178         }
179         RETURN(rc);
180 }
181
182 /**
183  * Schedule resending of request on sending_list. This is done after
184  * we completed replaying of requests and locks.
185  */
186 int ptlrpc_resend(struct obd_import *imp)
187 {
188         struct ptlrpc_request *req, *next;
189
190         ENTRY;
191
192         /* As long as we're in recovery, nothing should be added to the sending
193          * list, so we don't need to hold the lock during this iteration and
194          * resend process.
195          */
196         /* Well... what if lctl recover is called twice at the same time?
197          */
198         spin_lock(&imp->imp_lock);
199         if (imp->imp_state != LUSTRE_IMP_RECOVER) {
200                 spin_unlock(&imp->imp_lock);
201                 RETURN(-1);
202         }
203
204         list_for_each_entry_safe(req, next, &imp->imp_sending_list, rq_list) {
205                 LASSERTF((long)req > PAGE_CACHE_SIZE && req != LP_POISON,
206                          "req %p bad\n", req);
207                 LASSERTF(req->rq_type != LI_POISON, "req %p freed\n", req);
208
209                 /* If the request is allowed to be sent during replay and it
210                  * is not timeout yet, then it does not need to be resent. */
211                 if (!ptlrpc_no_resend(req) &&
212                     (req->rq_timedout || !req->rq_allow_replay))
213                         ptlrpc_resend_req(req);
214         }
215         spin_unlock(&imp->imp_lock);
216
217         RETURN(0);
218 }
219
220 /**
221  * Go through all requests in delayed list and wake their threads
222  * for resending
223  */
224 void ptlrpc_wake_delayed(struct obd_import *imp)
225 {
226         struct list_head *tmp, *pos;
227         struct ptlrpc_request *req;
228
229         spin_lock(&imp->imp_lock);
230         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
231                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
232
233                 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
234                 ptlrpc_client_wake_req(req);
235         }
236         spin_unlock(&imp->imp_lock);
237 }
238
239 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
240 {
241         struct obd_import *imp = failed_req->rq_import;
242         ENTRY;
243
244         CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
245                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
246                imp->imp_connection->c_remote_uuid.uuid);
247
248         if (ptlrpc_set_import_discon(imp,
249                               lustre_msg_get_conn_cnt(failed_req->rq_reqmsg))) {
250                 if (!imp->imp_replayable) {
251                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
252                                "auto-deactivating\n",
253                                obd2cli_tgt(imp->imp_obd),
254                                imp->imp_connection->c_remote_uuid.uuid,
255                                imp->imp_obd->obd_name);
256                         ptlrpc_deactivate_import(imp);
257                 }
258                 /* to control recovery via lctl {disable|enable}_recovery */
259                 if (imp->imp_deactive == 0)
260                         ptlrpc_connect_import(imp);
261         }
262
263         /* Wait for recovery to complete and resend. If evicted, then
264            this request will be errored out later.*/
265         spin_lock(&failed_req->rq_lock);
266         if (!failed_req->rq_no_resend)
267                 failed_req->rq_resend = 1;
268         spin_unlock(&failed_req->rq_lock);
269
270         EXIT;
271 }
272
273 /**
274  * Administratively active/deactive a client. 
275  * This should only be called by the ioctl interface, currently
276  *  - the lctl deactivate and activate commands
277  *  - echo 0/1 >> /proc/osc/XXX/active
278  *  - client umount -f (ll_umount_begin)
279  */
280 int ptlrpc_set_import_active(struct obd_import *imp, int active)
281 {
282         struct obd_device *obd = imp->imp_obd;
283         int rc = 0;
284
285         ENTRY;
286         LASSERT(obd);
287
288         /* When deactivating, mark import invalid, and abort in-flight
289          * requests. */
290         if (!active) {
291                 LCONSOLE_WARN("setting import %s INACTIVE by administrator "
292                               "request\n", obd2cli_tgt(imp->imp_obd));
293
294                 /* set before invalidate to avoid messages about imp_inval
295                  * set without imp_deactive in ptlrpc_import_delay_req */
296                 spin_lock(&imp->imp_lock);
297                 imp->imp_deactive = 1;
298                 spin_unlock(&imp->imp_lock);
299
300                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DEACTIVATE);
301
302                 ptlrpc_invalidate_import(imp);
303         }
304
305         /* When activating, mark import valid, and attempt recovery */
306         if (active) {
307                 CDEBUG(D_HA, "setting import %s VALID\n",
308                        obd2cli_tgt(imp->imp_obd));
309
310                 spin_lock(&imp->imp_lock);
311                 imp->imp_deactive = 0;
312                 spin_unlock(&imp->imp_lock);
313                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_ACTIVATE);
314
315                 rc = ptlrpc_recover_import(imp, NULL, 0);
316         }
317
318         RETURN(rc);
319 }
320 EXPORT_SYMBOL(ptlrpc_set_import_active);
321
322 /* Attempt to reconnect an import */
323 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid, int async)
324 {
325         int rc = 0;
326         ENTRY;
327
328         spin_lock(&imp->imp_lock);
329         if (imp->imp_state == LUSTRE_IMP_NEW || imp->imp_deactive ||
330             atomic_read(&imp->imp_inval_count))
331                 rc = -EINVAL;
332         spin_unlock(&imp->imp_lock);
333         if (rc)
334                 GOTO(out, rc);
335
336         /* force import to be disconnected. */
337         ptlrpc_set_import_discon(imp, 0);
338
339         if (new_uuid) {
340                 struct obd_uuid uuid;
341
342                 /* intruct import to use new uuid */
343                 obd_str2uuid(&uuid, new_uuid);
344                 rc = import_set_conn_priority(imp, &uuid);
345                 if (rc)
346                         GOTO(out, rc);
347         }
348
349         /* Check if reconnect is already in progress */
350         spin_lock(&imp->imp_lock);
351         if (imp->imp_state != LUSTRE_IMP_DISCON) {
352                 imp->imp_force_verify = 1;
353                 rc = -EALREADY;
354         }
355         spin_unlock(&imp->imp_lock);
356         if (rc)
357                 GOTO(out, rc);
358
359         rc = ptlrpc_connect_import(imp);
360         if (rc)
361                 GOTO(out, rc);
362
363         if (!async) {
364                 struct l_wait_info lwi;
365                 int secs = cfs_time_seconds(obd_timeout);
366
367                 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
368                        obd2cli_tgt(imp->imp_obd), secs);
369
370                 lwi = LWI_TIMEOUT(secs, NULL, NULL);
371                 rc = l_wait_event(imp->imp_recovery_waitq,
372                                   !ptlrpc_import_in_recovery(imp), &lwi);
373                 CDEBUG(D_HA, "%s: recovery finished\n",
374                        obd2cli_tgt(imp->imp_obd));
375         }
376         EXIT;
377
378 out:
379         return rc;
380 }
381 EXPORT_SYMBOL(ptlrpc_recover_import);
382
383 int ptlrpc_import_in_recovery(struct obd_import *imp)
384 {
385         int in_recovery = 1;
386
387         spin_lock(&imp->imp_lock);
388         if (imp->imp_state == LUSTRE_IMP_FULL ||
389             imp->imp_state == LUSTRE_IMP_CLOSED ||
390             imp->imp_state == LUSTRE_IMP_DISCON ||
391             imp->imp_obd->obd_no_recov)
392                 in_recovery = 0;
393         spin_unlock(&imp->imp_lock);
394
395         return in_recovery;
396 }