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],
106 rs->rs_modes[i], rs->rs_no_ack);
109 DEBUG_REQ(D_HA, req, "stole locks for");
110 ptlrpc_schedule_difficult_reply(rs);
111 spin_unlock(&rs->rs_lock);
113 spin_unlock(&svcpt->scp_rep_lock);
116 spin_unlock(&exp->exp_lock);
118 /* if exp_disconnected, decref stolen locks */
119 if (exp->exp_disconnected) {
120 rs = req->rq_reply_state;
122 for (i = 0; i < rs->rs_nlocks; i++)
123 ldlm_lock_decref(&rs->rs_locks[i], rs->rs_modes[i]);
129 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
130 struct tg_reply_data *trd)
132 struct lsd_reply_data *lrd;
134 LASSERT(trd != NULL);
135 lrd = &trd->trd_reply;
137 DEBUG_REQ(D_HA, req, "restoring transno");
139 req->rq_transno = lrd->lrd_transno;
140 req->rq_status = lrd->lrd_result;
142 lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
144 if (req->rq_status != 0)
146 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
147 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
149 DEBUG_REQ(D_WARNING, req, "restoring transno");
151 mdt_steal_ack_locks(req);
153 return lrd->lrd_data;
156 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
157 struct mdt_lock_handle *lhc)
159 struct ptlrpc_request *req = mdt_info_req(mti);
161 mdt_req_from_lrd(req, mti->mti_reply_data);
165 * Generate fake attributes for a non-existing object
167 * While the client was waiting for the reply, the original transaction
168 * got committed and corresponding rep-ack lock got released, then another
169 * client was able to destroy the object. But we still need to send some
170 * attributes back. So we fake them and set nlink=0, so the client will
171 * be able to detect a non-existing object and drop it from the cache
174 * \param[out] ma attributes to fill
176 static void mdt_fake_ma(struct md_attr *ma)
178 ma->ma_valid = MA_INODE;
179 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
180 ma->ma_attr.la_valid = LA_NLINK;
181 ma->ma_attr.la_mode = S_IFREG;
184 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
185 struct mdt_lock_handle *lhc)
187 struct ptlrpc_request *req = mdt_info_req(mti);
188 struct obd_export *exp = req->rq_export;
189 struct mdt_device *mdt = mti->mti_mdt;
190 struct md_attr *ma = &mti->mti_attr;
191 struct mdt_object *child;
192 struct mdt_body *body;
195 mdt_req_from_lrd(req, mti->mti_reply_data);
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);
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);
212 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
213 ma->ma_need = MA_INODE;
214 if (S_ISDIR(ma->ma_attr.la_mode) &&
215 (mti->mti_spec.sp_cr_flags & MDS_MKDIR_LMV))
216 mdt_prep_ma_buf_from_rep(mti, child, ma, 0);
218 rc = mdt_attr_get_complex(mti, child, ma);
221 } else if (rc == -EREMOTE) {
222 /* object was created on remote server */
223 if (!mdt_is_dne_client(exp))
224 /* Return -EIO for old client */
228 body->mbo_valid |= OBD_MD_MDS;
230 if (ma->ma_valid & MA_LMV) {
231 body->mbo_eadatasize = ma->ma_lmv_size;
232 body->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
234 mdt_pack_attr2body(mti, body, &ma->ma_attr, mdt_object_fid(child));
235 mdt_object_put(mti->mti_env, child);
238 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
239 struct mdt_lock_handle *lhc)
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 *obj;
245 struct mdt_body *body;
248 mdt_req_from_lrd(req, mti->mti_reply_data);
252 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
253 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
256 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
257 "evicting client %s with export %s\n",
258 PFID(mti->mti_rr.rr_fid1), rc,
259 obd_uuid2str(&exp->exp_client_uuid),
260 obd_export_nid2str(exp));
261 mdt_export_evict(exp);
265 mti->mti_attr.ma_need = MA_INODE;
266 mti->mti_attr.ma_valid = 0;
268 rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
270 mdt_fake_ma(&mti->mti_attr);
271 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
272 mdt_object_fid(obj));
274 mdt_object_put(mti->mti_env, obj);
277 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
278 struct mdt_lock_handle *lhc);
280 static mdt_reconstructor reconstructors[REINT_MAX] = {
281 [REINT_SETATTR] = mdt_reconstruct_setattr,
282 [REINT_CREATE] = mdt_reconstruct_create,
283 [REINT_LINK] = mdt_reconstruct_generic,
284 [REINT_UNLINK] = mdt_reconstruct_generic,
285 [REINT_RENAME] = mdt_reconstruct_generic,
286 [REINT_OPEN] = mdt_reconstruct_open,
287 [REINT_SETXATTR] = mdt_reconstruct_generic,
288 [REINT_RMENTRY] = mdt_reconstruct_generic,
289 [REINT_MIGRATE] = mdt_reconstruct_generic,
290 [REINT_RESYNC] = mdt_reconstruct_generic
293 void mdt_reconstruct(struct mdt_thread_info *mti, struct mdt_lock_handle *lhc)
295 mdt_reconstructor reconst;
298 reconst = reconstructors[mti->mti_rr.rr_opcode];
299 LASSERT(reconst != NULL);