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