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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_recovery.c
38 * Lustre Metadata Target (mdt) recovery-related methods
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Pershin Mike <tappro@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include "mdt_internal.h"
48 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
51 struct mdt_thread_info *mti;
53 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
60 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
61 const void *area, ssize_t len)
64 struct mdt_thread_info *mti;
66 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
69 buf->lb_buf = (void *)area;
75 * last_rcvd & last_committed update callbacks
77 extern struct lu_context_key mdt_thread_key;
79 /* This callback notifies MDT that transaction was done. This is needed by
80 * mdt_save_lock() only. It is similar to new target code and will be removed
81 * as mdt_save_lock() will be converted to use target structures */
82 static int mdt_txn_stop_cb(const struct lu_env *env,
83 struct thandle *txn, void *cookie)
85 struct mdt_thread_info *mti;
87 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
90 if (mti->mti_has_trans)
91 CDEBUG(D_INFO, "More than one transaction\n");
93 mti->mti_has_trans = 1;
97 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
98 struct obd_device *obd, struct lustre_sb_info *lsi)
104 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
107 /* prepare transactions callbacks */
108 mdt->mdt_txn_cb.dtc_txn_start = NULL;
109 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
110 mdt->mdt_txn_cb.dtc_txn_commit = NULL;
111 mdt->mdt_txn_cb.dtc_cookie = NULL;
112 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
113 INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
115 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
120 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
124 /* Remove transaction callback */
125 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
129 /* reconstruction code */
130 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
132 struct ptlrpc_service_part *svcpt;
133 struct obd_export *exp = req->rq_export;
134 struct list_head *tmp;
135 struct ptlrpc_reply_state *oldrep;
138 /* CAVEAT EMPTOR: spinlock order */
139 spin_lock(&exp->exp_lock);
140 list_for_each(tmp, &exp->exp_outstanding_replies) {
141 oldrep = list_entry(tmp, struct ptlrpc_reply_state,
144 if (oldrep->rs_xid != req->rq_xid)
147 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
148 CERROR ("Resent req xid "LPU64" has mismatched opc: "
149 "new %d old %d\n", req->rq_xid,
150 lustre_msg_get_opc(req->rq_reqmsg),
153 svcpt = oldrep->rs_svcpt;
154 spin_lock(&svcpt->scp_rep_lock);
156 list_del_init(&oldrep->rs_exp_list);
158 CDEBUG(D_HA, "Stealing %d locks from rs %p x"LPD64".t"LPD64
160 oldrep->rs_nlocks, oldrep,
161 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
162 libcfs_nid2str(exp->exp_connection->c_peer.nid));
164 for (i = 0; i < oldrep->rs_nlocks; i++)
165 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
166 oldrep->rs_modes[i], 0);
167 oldrep->rs_nlocks = 0;
169 DEBUG_REQ(D_HA, req, "stole locks for");
170 spin_lock(&oldrep->rs_lock);
171 ptlrpc_schedule_difficult_reply(oldrep);
172 spin_unlock(&oldrep->rs_lock);
174 spin_unlock(&svcpt->scp_rep_lock);
177 spin_unlock(&exp->exp_lock);
180 __u64 mdt_req_from_lrd(struct ptlrpc_request *req,
181 struct tg_reply_data *trd)
183 struct lsd_reply_data *lrd;
185 LASSERT(trd != NULL);
186 lrd = &trd->trd_reply;
188 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
189 lrd->lrd_transno, lrd->lrd_result);
191 req->rq_transno = lrd->lrd_transno;
192 req->rq_status = lrd->lrd_result;
194 lustre_msg_set_versions(req->rq_repmsg, trd->trd_pre_versions);
196 if (req->rq_status != 0)
198 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
199 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
201 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
202 req->rq_transno, req->rq_status);
204 mdt_steal_ack_locks(req);
206 return lrd->lrd_data;
210 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
211 struct mdt_lock_handle *lhc)
213 struct ptlrpc_request *req = mdt_info_req(mti);
215 mdt_req_from_lrd(req, mti->mti_reply_data);
220 * Generate fake attributes for a non-existing object
222 * While the client was waiting for the reply, the original transaction
223 * got committed and corresponding rep-ack lock got released, then another
224 * client was able to destroy the object. But we still need to send some
225 * attributes back. So we fake them and set nlink=0, so the client will
226 * be able to detect a non-existing object and drop it from the cache
229 * \param[out] ma attributes to fill
231 static void mdt_fake_ma(struct md_attr *ma)
233 ma->ma_valid = MA_INODE;
234 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
235 ma->ma_attr.la_valid = LA_NLINK;
236 ma->ma_attr.la_mode = S_IFREG;
239 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
240 struct mdt_lock_handle *lhc)
242 struct ptlrpc_request *req = mdt_info_req(mti);
243 struct obd_export *exp = req->rq_export;
244 struct mdt_device *mdt = mti->mti_mdt;
245 struct mdt_object *child;
246 struct mdt_body *body;
249 mdt_req_from_lrd(req, mti->mti_reply_data);
253 /* if no error, so child was created with requested fid */
254 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
257 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
258 "evicting client %s with export %s\n",
259 PFID(mti->mti_rr.rr_fid2), rc,
260 obd_uuid2str(&exp->exp_client_uuid),
261 obd_export_nid2str(exp));
262 mdt_export_evict(exp);
266 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
267 mti->mti_attr.ma_need = MA_INODE;
268 mti->mti_attr.ma_valid = 0;
269 rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
271 mdt_fake_ma(&mti->mti_attr);
272 } else if (rc == -EREMOTE) {
273 /* object was created on remote server */
274 if (!mdt_is_dne_client(exp))
275 /* Return -EIO for old client */
279 body->mbo_valid |= OBD_MD_MDS;
281 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
282 mdt_object_fid(child));
283 mdt_object_put(mti->mti_env, child);
286 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
287 struct mdt_lock_handle *lhc)
289 struct ptlrpc_request *req = mdt_info_req(mti);
290 struct obd_export *exp = req->rq_export;
291 struct mdt_device *mdt = mti->mti_mdt;
292 struct mdt_object *obj;
293 struct mdt_body *body;
296 mdt_req_from_lrd(req, mti->mti_reply_data);
300 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
301 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
304 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
305 "evicting client %s with export %s\n",
306 PFID(mti->mti_rr.rr_fid1), rc,
307 obd_uuid2str(&exp->exp_client_uuid),
308 obd_export_nid2str(exp));
309 mdt_export_evict(exp);
313 mti->mti_attr.ma_need = MA_INODE;
314 mti->mti_attr.ma_valid = 0;
316 rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
318 mdt_fake_ma(&mti->mti_attr);
319 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
320 mdt_object_fid(obj));
322 mdt_object_put(mti->mti_env, obj);
325 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
326 struct mdt_lock_handle *lhc);
328 static mdt_reconstructor reconstructors[REINT_MAX] = {
329 [REINT_SETATTR] = mdt_reconstruct_setattr,
330 [REINT_CREATE] = mdt_reconstruct_create,
331 [REINT_LINK] = mdt_reconstruct_generic,
332 [REINT_UNLINK] = mdt_reconstruct_generic,
333 [REINT_RENAME] = mdt_reconstruct_generic,
334 [REINT_OPEN] = mdt_reconstruct_open,
335 [REINT_SETXATTR] = mdt_reconstruct_generic,
336 [REINT_RMENTRY] = mdt_reconstruct_generic,
337 [REINT_MIGRATE] = mdt_reconstruct_generic
340 void mdt_reconstruct(struct mdt_thread_info *mti,
341 struct mdt_lock_handle *lhc)
343 mdt_reconstructor reconst;
345 LASSERT(mti->mti_rr.rr_opcode < REINT_MAX &&
346 (reconst = reconstructors[mti->mti_rr.rr_opcode]) != NULL);