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, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_recovery.c
34 * Lustre Metadata Target (mdt) recovery-related methods
36 * Author: Huang Hua <huanghua@clusterfs.com>
37 * Author: Pershin Mike <tappro@clusterfs.com>
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include "mdt_internal.h"
44 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
47 struct mdt_thread_info *mti;
49 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
56 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
57 const void *area, ssize_t len)
60 struct mdt_thread_info *mti;
62 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
65 buf->lb_buf = (void *)area;
71 * last_rcvd & last_committed update callbacks
73 extern struct lu_context_key mdt_thread_key;
75 /* This callback notifies MDT that transaction was done. This is needed by
76 * mdt_save_lock() only. It is similar to new target code and will be removed
77 * as mdt_save_lock() will be converted to use target structures */
78 static int mdt_txn_stop_cb(const struct lu_env *env,
79 struct thandle *txn, void *cookie)
81 struct mdt_thread_info *mti;
83 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
86 if (mti->mti_has_trans)
87 CDEBUG(D_INFO, "More than one transaction\n");
89 mti->mti_has_trans = 1;
93 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
94 struct obd_device *obd, struct lustre_sb_info *lsi)
100 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
103 /* prepare transactions callbacks */
104 mdt->mdt_txn_cb.dtc_txn_start = NULL;
105 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
106 mdt->mdt_txn_cb.dtc_txn_commit = NULL;
107 mdt->mdt_txn_cb.dtc_cookie = NULL;
108 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
109 INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
111 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
116 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
120 /* Remove transaction callback */
121 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
125 /* reconstruction code */
126 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
128 struct ptlrpc_service_part *svcpt;
129 struct obd_export *exp = req->rq_export;
130 struct list_head *tmp;
131 struct ptlrpc_reply_state *rs;
134 /* CAVEAT EMPTOR: spinlock order */
135 spin_lock(&exp->exp_lock);
136 list_for_each(tmp, &exp->exp_outstanding_replies) {
137 rs = list_entry(tmp, struct ptlrpc_reply_state,
140 if (rs->rs_xid != req->rq_xid)
143 if (rs->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
144 CERROR("%s: Resent req xid %llu has mismatched opc: "
145 "new %d old %d\n", exp->exp_obd->obd_name,
146 req->rq_xid, lustre_msg_get_opc(req->rq_reqmsg),
149 svcpt = rs->rs_svcpt;
151 CDEBUG(D_HA, "Stealing %d locks from rs %p x%lld.t%lld"
154 rs->rs_xid, rs->rs_transno, rs->rs_opc,
155 libcfs_nid2str(exp->exp_connection->c_peer.nid));
157 spin_lock(&svcpt->scp_rep_lock);
158 list_del_init(&rs->rs_exp_list);
160 spin_lock(&rs->rs_lock);
161 for (i = 0; i < rs->rs_nlocks; i++)
162 ptlrpc_save_lock(req, &rs->rs_locks[i],
163 rs->rs_modes[i], rs->rs_no_ack,
164 rs->rs_convert_lock);
167 DEBUG_REQ(D_HA, req, "stole locks for");
168 ptlrpc_schedule_difficult_reply(rs);
169 spin_unlock(&rs->rs_lock);
171 spin_unlock(&svcpt->scp_rep_lock);
174 spin_unlock(&exp->exp_lock);
176 /* if exp_disconnected, decref stolen locks */
177 if (exp->exp_disconnected) {
178 rs = req->rq_reply_state;
180 for (i = 0; i < rs->rs_nlocks; i++)
181 ldlm_lock_decref(&rs->rs_locks[i], rs->rs_modes[i]);
187 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
188 struct tg_reply_data *trd)
190 struct lsd_reply_data *lrd;
192 LASSERT(trd != NULL);
193 lrd = &trd->trd_reply;
195 DEBUG_REQ(D_HA, req, "restoring transno %lld/status %d",
196 lrd->lrd_transno, lrd->lrd_result);
198 req->rq_transno = lrd->lrd_transno;
199 req->rq_status = lrd->lrd_result;
201 lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
203 if (req->rq_status != 0)
205 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
206 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
208 DEBUG_REQ(D_RPCTRACE, req, "restoring transno %lld/status %d",
209 req->rq_transno, req->rq_status);
211 mdt_steal_ack_locks(req);
213 return lrd->lrd_data;
217 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
218 struct mdt_lock_handle *lhc)
220 struct ptlrpc_request *req = mdt_info_req(mti);
222 mdt_req_from_lrd(req, mti->mti_reply_data);
227 * Generate fake attributes for a non-existing object
229 * While the client was waiting for the reply, the original transaction
230 * got committed and corresponding rep-ack lock got released, then another
231 * client was able to destroy the object. But we still need to send some
232 * attributes back. So we fake them and set nlink=0, so the client will
233 * be able to detect a non-existing object and drop it from the cache
236 * \param[out] ma attributes to fill
238 static void mdt_fake_ma(struct md_attr *ma)
240 ma->ma_valid = MA_INODE;
241 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
242 ma->ma_attr.la_valid = LA_NLINK;
243 ma->ma_attr.la_mode = S_IFREG;
246 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
247 struct mdt_lock_handle *lhc)
249 struct ptlrpc_request *req = mdt_info_req(mti);
250 struct obd_export *exp = req->rq_export;
251 struct mdt_device *mdt = mti->mti_mdt;
252 struct mdt_object *child;
253 struct mdt_body *body;
256 mdt_req_from_lrd(req, mti->mti_reply_data);
260 /* if no error, so child was created with requested fid */
261 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
264 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
265 "evicting client %s with export %s\n",
266 PFID(mti->mti_rr.rr_fid2), rc,
267 obd_uuid2str(&exp->exp_client_uuid),
268 obd_export_nid2str(exp));
269 mdt_export_evict(exp);
273 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
274 mti->mti_attr.ma_need = MA_INODE;
275 mti->mti_attr.ma_valid = 0;
276 rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
278 mdt_fake_ma(&mti->mti_attr);
279 } else if (rc == -EREMOTE) {
280 /* object was created on remote server */
281 if (!mdt_is_dne_client(exp))
282 /* Return -EIO for old client */
286 body->mbo_valid |= OBD_MD_MDS;
288 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
289 mdt_object_fid(child));
290 mdt_object_put(mti->mti_env, child);
293 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
294 struct mdt_lock_handle *lhc)
296 struct ptlrpc_request *req = mdt_info_req(mti);
297 struct obd_export *exp = req->rq_export;
298 struct mdt_device *mdt = mti->mti_mdt;
299 struct mdt_object *obj;
300 struct mdt_body *body;
303 mdt_req_from_lrd(req, mti->mti_reply_data);
307 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
308 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
311 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
312 "evicting client %s with export %s\n",
313 PFID(mti->mti_rr.rr_fid1), rc,
314 obd_uuid2str(&exp->exp_client_uuid),
315 obd_export_nid2str(exp));
316 mdt_export_evict(exp);
320 mti->mti_attr.ma_need = MA_INODE;
321 mti->mti_attr.ma_valid = 0;
323 rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
325 mdt_fake_ma(&mti->mti_attr);
326 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
327 mdt_object_fid(obj));
329 mdt_object_put(mti->mti_env, obj);
332 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
333 struct mdt_lock_handle *lhc);
335 static mdt_reconstructor reconstructors[REINT_MAX] = {
336 [REINT_SETATTR] = mdt_reconstruct_setattr,
337 [REINT_CREATE] = mdt_reconstruct_create,
338 [REINT_LINK] = mdt_reconstruct_generic,
339 [REINT_UNLINK] = mdt_reconstruct_generic,
340 [REINT_RENAME] = mdt_reconstruct_generic,
341 [REINT_OPEN] = mdt_reconstruct_open,
342 [REINT_SETXATTR] = mdt_reconstruct_generic,
343 [REINT_RMENTRY] = mdt_reconstruct_generic,
344 [REINT_MIGRATE] = mdt_reconstruct_generic
347 void mdt_reconstruct(struct mdt_thread_info *mti,
348 struct mdt_lock_handle *lhc)
350 mdt_reconstructor reconst;
352 LASSERT(mti->mti_rr.rr_opcode < REINT_MAX &&
353 (reconst = reconstructors[mti->mti_rr.rr_opcode]) != NULL);