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,
107 rs->rs_convert_lock);
110 DEBUG_REQ(D_HA, req, "stole locks for");
111 ptlrpc_schedule_difficult_reply(rs);
112 spin_unlock(&rs->rs_lock);
114 spin_unlock(&svcpt->scp_rep_lock);
117 spin_unlock(&exp->exp_lock);
119 /* if exp_disconnected, decref stolen locks */
120 if (exp->exp_disconnected) {
121 rs = req->rq_reply_state;
123 for (i = 0; i < rs->rs_nlocks; i++)
124 ldlm_lock_decref(&rs->rs_locks[i], rs->rs_modes[i]);
130 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
131 struct tg_reply_data *trd)
133 struct lsd_reply_data *lrd;
135 LASSERT(trd != NULL);
136 lrd = &trd->trd_reply;
138 DEBUG_REQ(D_HA, req, "restoring transno");
140 req->rq_transno = lrd->lrd_transno;
141 req->rq_status = lrd->lrd_result;
143 lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
145 if (req->rq_status != 0)
147 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
148 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
150 DEBUG_REQ(D_WARNING, req, "restoring transno");
152 mdt_steal_ack_locks(req);
154 return lrd->lrd_data;
157 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
158 struct mdt_lock_handle *lhc)
160 struct ptlrpc_request *req = mdt_info_req(mti);
162 mdt_req_from_lrd(req, mti->mti_reply_data);
166 * Generate fake attributes for a non-existing object
168 * While the client was waiting for the reply, the original transaction
169 * got committed and corresponding rep-ack lock got released, then another
170 * client was able to destroy the object. But we still need to send some
171 * attributes back. So we fake them and set nlink=0, so the client will
172 * be able to detect a non-existing object and drop it from the cache
175 * \param[out] ma attributes to fill
177 static void mdt_fake_ma(struct md_attr *ma)
179 ma->ma_valid = MA_INODE;
180 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
181 ma->ma_attr.la_valid = LA_NLINK;
182 ma->ma_attr.la_mode = S_IFREG;
185 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
186 struct mdt_lock_handle *lhc)
188 struct ptlrpc_request *req = mdt_info_req(mti);
189 struct obd_export *exp = req->rq_export;
190 struct mdt_device *mdt = mti->mti_mdt;
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 mti->mti_attr.ma_need = MA_INODE;
214 mti->mti_attr.ma_valid = 0;
215 rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
217 mdt_fake_ma(&mti->mti_attr);
218 } else if (rc == -EREMOTE) {
219 /* object was created on remote server */
220 if (!mdt_is_dne_client(exp))
221 /* Return -EIO for old client */
225 body->mbo_valid |= OBD_MD_MDS;
227 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
228 mdt_object_fid(child));
229 mdt_object_put(mti->mti_env, child);
232 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
233 struct mdt_lock_handle *lhc)
235 struct ptlrpc_request *req = mdt_info_req(mti);
236 struct obd_export *exp = req->rq_export;
237 struct mdt_device *mdt = mti->mti_mdt;
238 struct mdt_object *obj;
239 struct mdt_body *body;
242 mdt_req_from_lrd(req, mti->mti_reply_data);
246 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
247 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
250 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
251 "evicting client %s with export %s\n",
252 PFID(mti->mti_rr.rr_fid1), rc,
253 obd_uuid2str(&exp->exp_client_uuid),
254 obd_export_nid2str(exp));
255 mdt_export_evict(exp);
259 mti->mti_attr.ma_need = MA_INODE;
260 mti->mti_attr.ma_valid = 0;
262 rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
264 mdt_fake_ma(&mti->mti_attr);
265 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
266 mdt_object_fid(obj));
268 mdt_object_put(mti->mti_env, obj);
271 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
272 struct mdt_lock_handle *lhc);
274 static mdt_reconstructor reconstructors[REINT_MAX] = {
275 [REINT_SETATTR] = mdt_reconstruct_setattr,
276 [REINT_CREATE] = mdt_reconstruct_create,
277 [REINT_LINK] = mdt_reconstruct_generic,
278 [REINT_UNLINK] = mdt_reconstruct_generic,
279 [REINT_RENAME] = mdt_reconstruct_generic,
280 [REINT_OPEN] = mdt_reconstruct_open,
281 [REINT_SETXATTR] = mdt_reconstruct_generic,
282 [REINT_RMENTRY] = mdt_reconstruct_generic,
283 [REINT_MIGRATE] = mdt_reconstruct_generic,
284 [REINT_RESYNC] = mdt_reconstruct_generic
287 void mdt_reconstruct(struct mdt_thread_info *mti, struct mdt_lock_handle *lhc)
289 mdt_reconstructor reconst;
292 reconst = reconstructors[mti->mti_rr.rr_opcode];
293 LASSERT(reconst != NULL);