Whamcloud - gitweb
LU-16335 test: add fail_abort_cleanup()
[fs/lustre-release.git] / lustre / mdt / mdt_recovery.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdt/mdt_recovery.c
32  *
33  * Lustre Metadata Target (mdt) recovery-related methods
34  *
35  * Author: Huang Hua <huanghua@clusterfs.com>
36  * Author: Pershin Mike <tappro@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include "mdt_internal.h"
42
43 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
44 {
45         struct lu_buf *buf;
46         struct mdt_thread_info *mti;
47
48         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
49         buf = &mti->mti_buf;
50         buf->lb_buf = area;
51         buf->lb_len = len;
52         return buf;
53 }
54
55 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
56                                    const void *area, ssize_t len)
57 {
58         struct lu_buf *buf;
59         struct mdt_thread_info *mti;
60
61         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
62         buf = &mti->mti_buf;
63
64         buf->lb_buf = (void *)area;
65         buf->lb_len = len;
66         return buf;
67 }
68
69 /* reconstruction code */
70 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
71 {
72         struct ptlrpc_service_part *svcpt;
73         struct obd_export *exp = req->rq_export;
74         struct list_head *tmp;
75         struct ptlrpc_reply_state *rs;
76         int i;
77
78         /* CAVEAT EMPTOR: spinlock order */
79         spin_lock(&exp->exp_lock);
80         list_for_each(tmp, &exp->exp_outstanding_replies) {
81                 rs = list_entry(tmp, struct ptlrpc_reply_state,
82                                     rs_exp_list);
83
84                 if (rs->rs_xid != req->rq_xid)
85                         continue;
86
87                 if (rs->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
88                         CERROR("%s: Resent req xid %llu has mismatched opc: new %d old %d\n",
89                                exp->exp_obd->obd_name, req->rq_xid,
90                                lustre_msg_get_opc(req->rq_reqmsg), rs->rs_opc);
91
92                 svcpt = rs->rs_svcpt;
93
94                 CDEBUG(D_HA,
95                        "Stealing %d locks from rs %p x%lld.t%lld o%d NID %s\n",
96                        rs->rs_nlocks, rs,
97                        rs->rs_xid, rs->rs_transno, rs->rs_opc,
98                        obd_export_nid2str(exp));
99
100                 spin_lock(&svcpt->scp_rep_lock);
101                 list_del_init(&rs->rs_exp_list);
102
103                 spin_lock(&rs->rs_lock);
104                 for (i = 0; i < rs->rs_nlocks; i++)
105                         ptlrpc_save_lock(req, &rs->rs_locks[i],
106                                          rs->rs_modes[i], rs->rs_no_ack,
107                                          rs->rs_convert_lock);
108                 rs->rs_nlocks = 0;
109
110                 DEBUG_REQ(D_HA, req, "stole locks for");
111                 ptlrpc_schedule_difficult_reply(rs);
112                 spin_unlock(&rs->rs_lock);
113
114                 spin_unlock(&svcpt->scp_rep_lock);
115                 break;
116         }
117         spin_unlock(&exp->exp_lock);
118
119         /* if exp_disconnected, decref stolen locks */
120         if (exp->exp_disconnected) {
121                 rs = req->rq_reply_state;
122
123                 for (i = 0; i < rs->rs_nlocks; i++)
124                         ldlm_lock_decref(&rs->rs_locks[i], rs->rs_modes[i]);
125
126                 rs->rs_nlocks = 0;
127         }
128 }
129
130 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
131                        struct tg_reply_data *trd)
132 {
133         struct lsd_reply_data *lrd;
134
135         LASSERT(trd != NULL);
136         lrd = &trd->trd_reply;
137
138         DEBUG_REQ(D_HA, req, "restoring transno");
139
140         req->rq_transno = lrd->lrd_transno;
141         req->rq_status = lrd->lrd_result;
142
143         lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
144
145         if (req->rq_status != 0)
146                 req->rq_transno = 0;
147         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
148         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
149
150         DEBUG_REQ(D_WARNING, req, "restoring transno");
151
152         mdt_steal_ack_locks(req);
153
154         return lrd->lrd_data;
155 }
156
157 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
158                              struct mdt_lock_handle *lhc)
159 {
160         struct ptlrpc_request *req = mdt_info_req(mti);
161
162         mdt_req_from_lrd(req, mti->mti_reply_data);
163 }
164
165 /**
166  * Generate fake attributes for a non-existing object
167  *
168  * While the client was waiting for the reply, the original transaction
169  * got committed and corresponding rep-ack lock got released, then another
170  * client was able to destroy the object. But we still need to send some
171  * attributes back. So we fake them and set nlink=0, so the client will
172  * be able to detect a non-existing object and drop it from the cache
173  * immediately.
174  *
175  * \param[out] ma       attributes to fill
176  */
177 static void mdt_fake_ma(struct md_attr *ma)
178 {
179         ma->ma_valid = MA_INODE;
180         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
181         ma->ma_attr.la_valid = LA_NLINK;
182         ma->ma_attr.la_mode = S_IFREG;
183 }
184
185 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
186                                    struct mdt_lock_handle *lhc)
187 {
188         struct ptlrpc_request  *req = mdt_info_req(mti);
189         struct obd_export *exp = req->rq_export;
190         struct mdt_device *mdt = mti->mti_mdt;
191         struct mdt_object *child;
192         struct mdt_body *body;
193         int rc;
194
195         mdt_req_from_lrd(req, mti->mti_reply_data);
196         if (req->rq_status)
197                 return;
198
199         /* if no error, so child was created with requested fid */
200         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
201         if (IS_ERR(child)) {
202                 rc = PTR_ERR(child);
203                 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
204                               "evicting client %s with export %s\n",
205                               PFID(mti->mti_rr.rr_fid2), rc,
206                               obd_uuid2str(&exp->exp_client_uuid),
207                               obd_export_nid2str(exp));
208                 mdt_export_evict(exp);
209                 RETURN_EXIT;
210         }
211
212         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
213         mti->mti_attr.ma_need = MA_INODE;
214         mti->mti_attr.ma_valid = 0;
215         rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
216         if (rc == -ENOENT) {
217                 mdt_fake_ma(&mti->mti_attr);
218         } else if (rc == -EREMOTE) {
219                 /* object was created on remote server */
220                 if (!mdt_is_dne_client(exp))
221                         /* Return -EIO for old client */
222                         rc = -EIO;
223
224                 req->rq_status = rc;
225                 body->mbo_valid |= OBD_MD_MDS;
226         }
227         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
228                            mdt_object_fid(child));
229         mdt_object_put(mti->mti_env, child);
230 }
231
232 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
233                                     struct mdt_lock_handle *lhc)
234 {
235         struct ptlrpc_request  *req = mdt_info_req(mti);
236         struct obd_export *exp = req->rq_export;
237         struct mdt_device *mdt = mti->mti_mdt;
238         struct mdt_object *obj;
239         struct mdt_body *body;
240         int rc;
241
242         mdt_req_from_lrd(req, mti->mti_reply_data);
243         if (req->rq_status)
244                 return;
245
246         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
247         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
248         if (IS_ERR(obj)) {
249                 rc = PTR_ERR(obj);
250                 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
251                               "evicting client %s with export %s\n",
252                               PFID(mti->mti_rr.rr_fid1), rc,
253                               obd_uuid2str(&exp->exp_client_uuid),
254                               obd_export_nid2str(exp));
255                 mdt_export_evict(exp);
256                 RETURN_EXIT;
257         }
258
259         mti->mti_attr.ma_need = MA_INODE;
260         mti->mti_attr.ma_valid = 0;
261
262         rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
263         if (rc == -ENOENT)
264                 mdt_fake_ma(&mti->mti_attr);
265         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
266                            mdt_object_fid(obj));
267
268         mdt_object_put(mti->mti_env, obj);
269 }
270
271 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
272                                   struct mdt_lock_handle *lhc);
273
274 static mdt_reconstructor reconstructors[REINT_MAX] = {
275         [REINT_SETATTR]  = mdt_reconstruct_setattr,
276         [REINT_CREATE]   = mdt_reconstruct_create,
277         [REINT_LINK]     = mdt_reconstruct_generic,
278         [REINT_UNLINK]   = mdt_reconstruct_generic,
279         [REINT_RENAME]   = mdt_reconstruct_generic,
280         [REINT_OPEN]     = mdt_reconstruct_open,
281         [REINT_SETXATTR] = mdt_reconstruct_generic,
282         [REINT_RMENTRY]  = mdt_reconstruct_generic,
283         [REINT_MIGRATE]  = mdt_reconstruct_generic,
284         [REINT_RESYNC]   = mdt_reconstruct_generic
285 };
286
287 void mdt_reconstruct(struct mdt_thread_info *mti, struct mdt_lock_handle *lhc)
288 {
289         mdt_reconstructor reconst;
290
291         ENTRY;
292         LASSERT(mti->mti_rr.rr_opcode < REINT_MAX &&
293                 (reconst = reconstructors[mti->mti_rr.rr_opcode]) != NULL);
294         reconst(mti, lhc);
295         EXIT;
296 }