Whamcloud - gitweb
2674d5e0ca62baaa98a3a6e01c54cc8262f01552
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_recovery.c
33  *
34  * Lustre Metadata Target (mdt) recovery-related methods
35  *
36  * Author: Huang Hua <huanghua@clusterfs.com>
37  * Author: Pershin Mike <tappro@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_MDS
41
42 #include "mdt_internal.h"
43
44 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
45 {
46         struct lu_buf *buf;
47         struct mdt_thread_info *mti;
48
49         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
50         buf = &mti->mti_buf;
51         buf->lb_buf = area;
52         buf->lb_len = len;
53         return buf;
54 }
55
56 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
57                                    const void *area, ssize_t len)
58 {
59         struct lu_buf *buf;
60         struct mdt_thread_info *mti;
61
62         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
63         buf = &mti->mti_buf;
64
65         buf->lb_buf = (void *)area;
66         buf->lb_len = len;
67         return buf;
68 }
69
70 /* This callback notifies MDT that transaction was done. This is needed by
71  * mdt_save_lock() only. It is similar to new target code and will be removed
72  * as mdt_save_lock() will be converted to use target structures
73  */
74 static int mdt_txn_stop_cb(const struct lu_env *env,
75                            struct thandle *txn, void *cookie)
76 {
77         struct mdt_thread_info *mti;
78
79         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
80         LASSERT(mti);
81
82         if (mti->mti_has_trans)
83                 CDEBUG(D_INFO, "More than one transaction\n");
84         else
85                 mti->mti_has_trans = 1;
86         return 0;
87 }
88
89 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
90                  struct obd_device *obd, struct lustre_sb_info *lsi)
91 {
92         int rc = 0;
93
94         ENTRY;
95
96         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
97                 RETURN(-ENOENT);
98
99         /* prepare transactions callbacks */
100         mdt->mdt_txn_cb.dtc_txn_start = NULL;
101         mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
102         mdt->mdt_txn_cb.dtc_cookie = NULL;
103         mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
104         INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
105
106         dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
107
108         RETURN(rc);
109 }
110
111 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
112 {
113         ENTRY;
114
115         /* Remove transaction callback */
116         dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
117         EXIT;
118 }
119
120 /* reconstruction code */
121 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
122 {
123         struct ptlrpc_service_part *svcpt;
124         struct obd_export *exp = req->rq_export;
125         struct list_head *tmp;
126         struct ptlrpc_reply_state *rs;
127         int i;
128
129         /* CAVEAT EMPTOR: spinlock order */
130         spin_lock(&exp->exp_lock);
131         list_for_each(tmp, &exp->exp_outstanding_replies) {
132                 rs = list_entry(tmp, struct ptlrpc_reply_state,
133                                     rs_exp_list);
134
135                 if (rs->rs_xid != req->rq_xid)
136                         continue;
137
138                 if (rs->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
139                         CERROR("%s: Resent req xid %llu has mismatched opc: new %d old %d\n",
140                                exp->exp_obd->obd_name, req->rq_xid,
141                                lustre_msg_get_opc(req->rq_reqmsg), rs->rs_opc);
142
143                 svcpt = rs->rs_svcpt;
144
145                 CDEBUG(D_HA,
146                        "Stealing %d locks from rs %p x%lld.t%lld o%d NID %s\n",
147                        rs->rs_nlocks, rs,
148                        rs->rs_xid, rs->rs_transno, rs->rs_opc,
149                        obd_export_nid2str(exp));
150
151                 spin_lock(&svcpt->scp_rep_lock);
152                 list_del_init(&rs->rs_exp_list);
153
154                 spin_lock(&rs->rs_lock);
155                 for (i = 0; i < rs->rs_nlocks; i++)
156                         ptlrpc_save_lock(req, &rs->rs_locks[i],
157                                          rs->rs_modes[i], rs->rs_no_ack,
158                                          rs->rs_convert_lock);
159                 rs->rs_nlocks = 0;
160
161                 DEBUG_REQ(D_HA, req, "stole locks for");
162                 ptlrpc_schedule_difficult_reply(rs);
163                 spin_unlock(&rs->rs_lock);
164
165                 spin_unlock(&svcpt->scp_rep_lock);
166                 break;
167         }
168         spin_unlock(&exp->exp_lock);
169
170         /* if exp_disconnected, decref stolen locks */
171         if (exp->exp_disconnected) {
172                 rs = req->rq_reply_state;
173
174                 for (i = 0; i < rs->rs_nlocks; i++)
175                         ldlm_lock_decref(&rs->rs_locks[i], rs->rs_modes[i]);
176
177                 rs->rs_nlocks = 0;
178         }
179 }
180
181 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
182                        struct tg_reply_data *trd)
183 {
184         struct lsd_reply_data *lrd;
185
186         LASSERT(trd != NULL);
187         lrd = &trd->trd_reply;
188
189         DEBUG_REQ(D_HA, req, "restoring transno %lld/status %d",
190                   lrd->lrd_transno, lrd->lrd_result);
191
192         req->rq_transno = lrd->lrd_transno;
193         req->rq_status = lrd->lrd_result;
194
195         lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
196
197         if (req->rq_status != 0)
198                 req->rq_transno = 0;
199         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
200         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
201
202         DEBUG_REQ(D_RPCTRACE, req, "restoring transno %lld/status %d",
203                   req->rq_transno, req->rq_status);
204
205         mdt_steal_ack_locks(req);
206
207         return lrd->lrd_data;
208 }
209
210 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
211                              struct mdt_lock_handle *lhc)
212 {
213         struct ptlrpc_request *req = mdt_info_req(mti);
214
215         mdt_req_from_lrd(req, mti->mti_reply_data);
216 }
217
218 /**
219  * Generate fake attributes for a non-existing object
220  *
221  * While the client was waiting for the reply, the original transaction
222  * got committed and corresponding rep-ack lock got released, then another
223  * client was able to destroy the object. But we still need to send some
224  * attributes back. So we fake them and set nlink=0, so the client will
225  * be able to detect a non-existing object and drop it from the cache
226  * immediately.
227  *
228  * \param[out] ma       attributes to fill
229  */
230 static void mdt_fake_ma(struct md_attr *ma)
231 {
232         ma->ma_valid = MA_INODE;
233         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
234         ma->ma_attr.la_valid = LA_NLINK;
235         ma->ma_attr.la_mode = S_IFREG;
236 }
237
238 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
239                                    struct mdt_lock_handle *lhc)
240 {
241         struct ptlrpc_request  *req = mdt_info_req(mti);
242         struct obd_export *exp = req->rq_export;
243         struct mdt_device *mdt = mti->mti_mdt;
244         struct mdt_object *child;
245         struct mdt_body *body;
246         int rc;
247
248         mdt_req_from_lrd(req, mti->mti_reply_data);
249         if (req->rq_status)
250                 return;
251
252         /* if no error, so child was created with requested fid */
253         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
254         if (IS_ERR(child)) {
255                 rc = PTR_ERR(child);
256                 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
257                               "evicting client %s with export %s\n",
258                               PFID(mti->mti_rr.rr_fid2), rc,
259                               obd_uuid2str(&exp->exp_client_uuid),
260                               obd_export_nid2str(exp));
261                 mdt_export_evict(exp);
262                 RETURN_EXIT;
263         }
264
265         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
266         mti->mti_attr.ma_need = MA_INODE;
267         mti->mti_attr.ma_valid = 0;
268         rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
269         if (rc == -ENOENT) {
270                 mdt_fake_ma(&mti->mti_attr);
271         } else if (rc == -EREMOTE) {
272                 /* object was created on remote server */
273                 if (!mdt_is_dne_client(exp))
274                         /* Return -EIO for old client */
275                         rc = -EIO;
276
277                 req->rq_status = rc;
278                 body->mbo_valid |= OBD_MD_MDS;
279         }
280         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
281                            mdt_object_fid(child));
282         mdt_object_put(mti->mti_env, child);
283 }
284
285 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
286                                     struct mdt_lock_handle *lhc)
287 {
288         struct ptlrpc_request  *req = mdt_info_req(mti);
289         struct obd_export *exp = req->rq_export;
290         struct mdt_device *mdt = mti->mti_mdt;
291         struct mdt_object *obj;
292         struct mdt_body *body;
293         int rc;
294
295         mdt_req_from_lrd(req, mti->mti_reply_data);
296         if (req->rq_status)
297                 return;
298
299         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
300         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
301         if (IS_ERR(obj)) {
302                 rc = PTR_ERR(obj);
303                 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
304                               "evicting client %s with export %s\n",
305                               PFID(mti->mti_rr.rr_fid1), rc,
306                               obd_uuid2str(&exp->exp_client_uuid),
307                               obd_export_nid2str(exp));
308                 mdt_export_evict(exp);
309                 RETURN_EXIT;
310         }
311
312         mti->mti_attr.ma_need = MA_INODE;
313         mti->mti_attr.ma_valid = 0;
314
315         rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
316         if (rc == -ENOENT)
317                 mdt_fake_ma(&mti->mti_attr);
318         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
319                            mdt_object_fid(obj));
320
321         mdt_object_put(mti->mti_env, obj);
322 }
323
324 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
325                                   struct mdt_lock_handle *lhc);
326
327 static mdt_reconstructor reconstructors[REINT_MAX] = {
328         [REINT_SETATTR]  = mdt_reconstruct_setattr,
329         [REINT_CREATE]   = mdt_reconstruct_create,
330         [REINT_LINK]     = mdt_reconstruct_generic,
331         [REINT_UNLINK]   = mdt_reconstruct_generic,
332         [REINT_RENAME]   = mdt_reconstruct_generic,
333         [REINT_OPEN]     = mdt_reconstruct_open,
334         [REINT_SETXATTR] = mdt_reconstruct_generic,
335         [REINT_RMENTRY]  = mdt_reconstruct_generic,
336         [REINT_MIGRATE]  = mdt_reconstruct_generic,
337         [REINT_RESYNC]   = mdt_reconstruct_generic
338 };
339
340 void mdt_reconstruct(struct mdt_thread_info *mti, struct mdt_lock_handle *lhc)
341 {
342         mdt_reconstructor reconst;
343
344         ENTRY;
345         LASSERT(mti->mti_rr.rr_opcode < REINT_MAX &&
346                 (reconst = reconstructors[mti->mti_rr.rr_opcode]) != NULL);
347         reconst(mti, lhc);
348         EXIT;
349 }