4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_recovery.c
33 * Lustre Metadata Target (mdt) recovery-related methods
35 * Author: Huang Hua <huanghua@clusterfs.com>
36 * Author: Pershin Mike <tappro@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include "mdt_internal.h"
43 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
46 struct mdt_thread_info *mti;
48 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
55 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
56 const void *area, ssize_t len)
59 struct mdt_thread_info *mti;
61 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
64 buf->lb_buf = (void *)area;
69 /* reconstruction code */
70 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
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;
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,
84 if (rs->rs_xid != req->rq_xid)
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);
95 "Stealing %d locks from rs %p x%lld.t%lld o%d NID %s\n",
97 rs->rs_xid, rs->rs_transno, rs->rs_opc,
98 obd_export_nid2str(exp));
100 spin_lock(&svcpt->scp_rep_lock);
101 list_del_init(&rs->rs_exp_list);
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);
108 DEBUG_REQ(D_HA, req, "stole locks for");
109 ptlrpc_schedule_difficult_reply(rs);
110 spin_unlock(&rs->rs_lock);
112 spin_unlock(&svcpt->scp_rep_lock);
115 spin_unlock(&exp->exp_lock);
117 /* if exp_disconnected, decref stolen locks */
118 if (exp->exp_disconnected) {
119 rs = req->rq_reply_state;
121 for (i = 0; i < rs->rs_nlocks; i++)
122 ldlm_lock_decref(&rs->rs_locks[i], LCK_TXN);
128 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
129 struct tg_reply_data *trd)
131 struct lsd_reply_data *lrd;
133 LASSERT(trd != NULL);
134 lrd = &trd->trd_reply;
136 DEBUG_REQ(D_HA, req, "restoring transno");
138 req->rq_transno = lrd->lrd_transno;
139 req->rq_status = lrd->lrd_result;
141 lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
143 if (req->rq_status != 0)
145 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
146 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
148 DEBUG_REQ(D_WARNING, req, "restoring transno");
150 mdt_steal_ack_locks(req);
152 return lrd->lrd_data;
155 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
156 struct mdt_lock_handle *lhc)
158 struct ptlrpc_request *req = mdt_info_req(mti);
160 mdt_req_from_lrd(req, mti->mti_reply_data);
164 * Generate fake attributes for a non-existing object
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
173 * \param[out] ma attributes to fill
175 static void mdt_fake_ma(struct md_attr *ma)
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;
183 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
184 struct mdt_lock_handle *lhc)
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;
194 mdt_req_from_lrd(req, mti->mti_reply_data);
198 /* if no error, so child was created with requested fid */
199 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
202 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
203 "evicting client %s with export %s\n",
204 PFID(mti->mti_rr.rr_fid2), rc,
205 obd_uuid2str(&exp->exp_client_uuid),
206 obd_export_nid2str(exp));
207 mdt_export_evict(exp);
211 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
212 ma->ma_need = MA_INODE;
213 if (S_ISDIR(ma->ma_attr.la_mode) &&
214 (mti->mti_spec.sp_cr_flags & MDS_MKDIR_LMV))
215 mdt_prep_ma_buf_from_rep(mti, child, ma, 0);
217 rc = mdt_attr_get_complex(mti, child, ma);
220 } else if (rc == -EREMOTE) {
221 /* object was created on remote server */
222 if (!mdt_is_dne_client(exp))
223 /* Return -EIO for old client */
227 body->mbo_valid |= OBD_MD_MDS;
229 if (ma->ma_valid & MA_LMV) {
230 body->mbo_eadatasize = ma->ma_lmv_size;
231 body->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
233 mdt_pack_attr2body(mti, body, &ma->ma_attr, mdt_object_fid(child));
234 mdt_object_put(mti->mti_env, child);
237 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
238 struct mdt_lock_handle *lhc)
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 *obj;
244 struct mdt_body *body;
247 mdt_req_from_lrd(req, mti->mti_reply_data);
251 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
252 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
255 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
256 "evicting client %s with export %s\n",
257 PFID(mti->mti_rr.rr_fid1), rc,
258 obd_uuid2str(&exp->exp_client_uuid),
259 obd_export_nid2str(exp));
260 mdt_export_evict(exp);
264 mti->mti_attr.ma_need = MA_INODE;
265 mti->mti_attr.ma_valid = 0;
267 rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
269 mdt_fake_ma(&mti->mti_attr);
270 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
271 mdt_object_fid(obj));
273 mdt_object_put(mti->mti_env, obj);
276 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
277 struct mdt_lock_handle *lhc);
279 static mdt_reconstructor reconstructors[REINT_MAX] = {
280 [REINT_SETATTR] = mdt_reconstruct_setattr,
281 [REINT_CREATE] = mdt_reconstruct_create,
282 [REINT_LINK] = mdt_reconstruct_generic,
283 [REINT_UNLINK] = mdt_reconstruct_generic,
284 [REINT_RENAME] = mdt_reconstruct_generic,
285 [REINT_OPEN] = mdt_reconstruct_open,
286 [REINT_SETXATTR] = mdt_reconstruct_generic,
287 [REINT_RMENTRY] = mdt_reconstruct_generic,
288 [REINT_MIGRATE] = mdt_reconstruct_generic,
289 [REINT_RESYNC] = mdt_reconstruct_generic
292 void mdt_reconstruct(struct mdt_thread_info *mti, struct mdt_lock_handle *lhc)
294 mdt_reconstructor reconst;
297 reconst = reconstructors[mti->mti_rr.rr_opcode];
298 LASSERT(reconst != NULL);