Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[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], rs->rs_no_ack);
106                 rs->rs_nlocks = 0;
107
108                 DEBUG_REQ(D_HA, req, "stole locks for");
109                 ptlrpc_schedule_difficult_reply(rs);
110                 spin_unlock(&rs->rs_lock);
111
112                 spin_unlock(&svcpt->scp_rep_lock);
113                 break;
114         }
115         spin_unlock(&exp->exp_lock);
116
117         /* if exp_disconnected, decref stolen locks */
118         if (exp->exp_disconnected) {
119                 rs = req->rq_reply_state;
120
121                 for (i = 0; i < rs->rs_nlocks; i++)
122                         ldlm_lock_decref(&rs->rs_locks[i], LCK_TXN);
123
124                 rs->rs_nlocks = 0;
125         }
126 }
127
128 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
129                        struct tg_reply_data *trd)
130 {
131         struct lsd_reply_data *lrd;
132
133         LASSERT(trd != NULL);
134         lrd = &trd->trd_reply;
135
136         DEBUG_REQ(D_HA, req, "restoring transno");
137
138         req->rq_transno = lrd->lrd_transno;
139         req->rq_status = lrd->lrd_result;
140
141         lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
142
143         if (req->rq_status != 0)
144                 req->rq_transno = 0;
145         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
146         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
147
148         DEBUG_REQ(D_WARNING, req, "restoring transno");
149
150         mdt_steal_ack_locks(req);
151
152         return lrd->lrd_data;
153 }
154
155 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
156                              struct mdt_lock_handle *lhc)
157 {
158         struct ptlrpc_request *req = mdt_info_req(mti);
159
160         mdt_req_from_lrd(req, mti->mti_reply_data);
161 }
162
163 /**
164  * Generate fake attributes for a non-existing object
165  *
166  * While the client was waiting for the reply, the original transaction
167  * got committed and corresponding rep-ack lock got released, then another
168  * client was able to destroy the object. But we still need to send some
169  * attributes back. So we fake them and set nlink=0, so the client will
170  * be able to detect a non-existing object and drop it from the cache
171  * immediately.
172  *
173  * \param[out] ma       attributes to fill
174  */
175 static void mdt_fake_ma(struct md_attr *ma)
176 {
177         ma->ma_valid = MA_INODE;
178         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
179         ma->ma_attr.la_valid = LA_NLINK;
180         ma->ma_attr.la_mode = S_IFREG;
181 }
182
183 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
184                                    struct mdt_lock_handle *lhc)
185 {
186         struct ptlrpc_request  *req = mdt_info_req(mti);
187         struct obd_export *exp = req->rq_export;
188         struct mdt_device *mdt = mti->mti_mdt;
189         struct md_attr *ma = &mti->mti_attr;
190         struct mdt_object *child;
191         struct mdt_body *body;
192         int rc;
193
194         ENTRY;
195
196         mdt_req_from_lrd(req, mti->mti_reply_data);
197         if (req->rq_status)
198                 return;
199
200         /* if no error, so child was created with requested fid */
201         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
202         if (IS_ERR(child)) {
203                 rc = PTR_ERR(child);
204                 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
205                               "evicting client %s with export %s\n",
206                               PFID(mti->mti_rr.rr_fid2), rc,
207                               obd_uuid2str(&exp->exp_client_uuid),
208                               obd_export_nid2str(exp));
209                 mdt_export_evict(exp);
210                 RETURN_EXIT;
211         }
212
213         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
214         ma->ma_need = MA_INODE;
215         if (S_ISDIR(ma->ma_attr.la_mode) &&
216             (mti->mti_spec.sp_cr_flags & MDS_MKDIR_LMV))
217                 mdt_prep_ma_buf_from_rep(mti, child, ma, 0);
218         ma->ma_valid = 0;
219         rc = mdt_attr_get_complex(mti, child, ma);
220         if (rc == -ENOENT) {
221                 mdt_fake_ma(ma);
222         } else if (rc == -EREMOTE) {
223                 /* object was created on remote server */
224                 if (!mdt_is_dne_client(exp))
225                         /* Return -EIO for old client */
226                         rc = -EIO;
227
228                 req->rq_status = rc;
229                 body->mbo_valid |= OBD_MD_MDS;
230         }
231         if (ma->ma_valid & MA_LMV) {
232                 body->mbo_eadatasize = ma->ma_lmv_size;
233                 body->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
234         }
235         mdt_pack_attr2body(mti, body, &ma->ma_attr, mdt_object_fid(child));
236         mdt_object_put(mti->mti_env, child);
237
238         RETURN_EXIT;
239 }
240
241 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
242                                     struct mdt_lock_handle *lhc)
243 {
244         struct ptlrpc_request  *req = mdt_info_req(mti);
245         struct obd_export *exp = req->rq_export;
246         struct mdt_device *mdt = mti->mti_mdt;
247         struct mdt_object *obj;
248         struct mdt_body *body;
249         int rc;
250
251         mdt_req_from_lrd(req, mti->mti_reply_data);
252         if (req->rq_status)
253                 return;
254
255         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
256         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
257         if (IS_ERR(obj)) {
258                 rc = PTR_ERR(obj);
259                 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
260                               "evicting client %s with export %s\n",
261                               PFID(mti->mti_rr.rr_fid1), rc,
262                               obd_uuid2str(&exp->exp_client_uuid),
263                               obd_export_nid2str(exp));
264                 mdt_export_evict(exp);
265                 RETURN_EXIT;
266         }
267
268         mti->mti_attr.ma_need = MA_INODE;
269         mti->mti_attr.ma_valid = 0;
270
271         rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
272         if (rc == -ENOENT)
273                 mdt_fake_ma(&mti->mti_attr);
274         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
275                            mdt_object_fid(obj));
276
277         mdt_object_put(mti->mti_env, obj);
278 }
279
280 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
281                                   struct mdt_lock_handle *lhc);
282
283 static mdt_reconstructor reconstructors[REINT_MAX] = {
284         [REINT_SETATTR]  = mdt_reconstruct_setattr,
285         [REINT_CREATE]   = mdt_reconstruct_create,
286         [REINT_LINK]     = mdt_reconstruct_generic,
287         [REINT_UNLINK]   = mdt_reconstruct_generic,
288         [REINT_RENAME]   = mdt_reconstruct_generic,
289         [REINT_OPEN]     = mdt_reconstruct_open,
290         [REINT_SETXATTR] = mdt_reconstruct_generic,
291         [REINT_RMENTRY]  = mdt_reconstruct_generic,
292         [REINT_MIGRATE]  = mdt_reconstruct_generic,
293         [REINT_RESYNC]   = mdt_reconstruct_generic
294 };
295
296 void mdt_reconstruct(struct mdt_thread_info *mti, struct mdt_lock_handle *lhc)
297 {
298         mdt_reconstructor reconst;
299
300         ENTRY;
301         reconst = reconstructors[mti->mti_rr.rr_opcode];
302         LASSERT(reconst != NULL);
303         reconst(mti, lhc);
304         EXIT;
305 }