Whamcloud - gitweb
5358bfebbe8bd47c547b28afb18d5eeb2a0144d0
[fs/lustre-release.git] / lustre / mdt / mdt_recovery.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_recovery.c
37  *
38  * Lustre Metadata Target (mdt) recovery-related methods
39  *
40  * Author: Huang Hua <huanghua@clusterfs.com>
41  * Author: Pershin Mike <tappro@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include "mdt_internal.h"
47
48 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
49 {
50         struct lu_buf *buf;
51         struct mdt_thread_info *mti;
52
53         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
54         buf = &mti->mti_buf;
55         buf->lb_buf = area;
56         buf->lb_len = len;
57         return buf;
58 }
59
60 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
61                                    const void *area, ssize_t len)
62 {
63         struct lu_buf *buf;
64         struct mdt_thread_info *mti;
65
66         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
67         buf = &mti->mti_buf;
68
69         buf->lb_buf = (void *)area;
70         buf->lb_len = len;
71         return buf;
72 }
73
74 /*
75  * last_rcvd & last_committed update callbacks
76  */
77 extern struct lu_context_key mdt_thread_key;
78
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)
84 {
85         struct mdt_thread_info  *mti;
86
87         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
88         LASSERT(mti);
89
90         if (mti->mti_has_trans)
91                 CDEBUG(D_INFO, "More than one transaction\n");
92         else
93                 mti->mti_has_trans = 1;
94         return 0;
95 }
96
97 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
98                  struct obd_device *obd, struct lustre_sb_info *lsi)
99 {
100         int rc = 0;
101
102         ENTRY;
103
104         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
105                 RETURN(-ENOENT);
106
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);
114
115         dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
116
117         RETURN(rc);
118 }
119
120 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
121 {
122         ENTRY;
123
124         /* Remove transaction callback */
125         dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
126         if (mdt->mdt_ck_obj)
127                 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
128         mdt->mdt_ck_obj = NULL;
129         EXIT;
130 }
131
132 /* reconstruction code */
133 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
134 {
135         struct ptlrpc_service_part *svcpt;
136         struct obd_export          *exp = req->rq_export;
137         struct list_head           *tmp;
138         struct ptlrpc_reply_state  *oldrep;
139         int                         i;
140
141         /* CAVEAT EMPTOR: spinlock order */
142         spin_lock(&exp->exp_lock);
143         list_for_each(tmp, &exp->exp_outstanding_replies) {
144                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,
145                                     rs_exp_list);
146
147                 if (oldrep->rs_xid != req->rq_xid)
148                         continue;
149
150                 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
151                         CERROR ("Resent req xid "LPU64" has mismatched opc: "
152                                 "new %d old %d\n", req->rq_xid,
153                                 lustre_msg_get_opc(req->rq_reqmsg),
154                                 oldrep->rs_opc);
155
156                 svcpt = oldrep->rs_svcpt;
157                 spin_lock(&svcpt->scp_rep_lock);
158
159                 list_del_init(&oldrep->rs_exp_list);
160
161                 CDEBUG(D_HA, "Stealing %d locks from rs %p x"LPD64".t"LPD64
162                        " o%d NID %s\n",
163                        oldrep->rs_nlocks, oldrep,
164                        oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
165                        libcfs_nid2str(exp->exp_connection->c_peer.nid));
166
167                 for (i = 0; i < oldrep->rs_nlocks; i++)
168                         ptlrpc_save_lock(req, &oldrep->rs_locks[i],
169                                          oldrep->rs_modes[i], 0);
170                 oldrep->rs_nlocks = 0;
171
172                 DEBUG_REQ(D_HA, req, "stole locks for");
173                 spin_lock(&oldrep->rs_lock);
174                 ptlrpc_schedule_difficult_reply(oldrep);
175                 spin_unlock(&oldrep->rs_lock);
176
177                 spin_unlock(&svcpt->scp_rep_lock);
178                 break;
179         }
180         spin_unlock(&exp->exp_lock);
181 }
182
183 /**
184  * VBR: restore versions
185  */
186 static void mdt_vbr_reconstruct(struct ptlrpc_request *req,
187                                 struct lsd_client_data *lcd)
188 {
189         __u64 pre_versions[4] = {0};
190         pre_versions[0] = lcd->lcd_pre_versions[0];
191         pre_versions[1] = lcd->lcd_pre_versions[1];
192         pre_versions[2] = lcd->lcd_pre_versions[2];
193         pre_versions[3] = lcd->lcd_pre_versions[3];
194         lustre_msg_set_versions(req->rq_repmsg, pre_versions);
195 }
196
197 void mdt_req_from_lcd(struct ptlrpc_request *req, struct lsd_client_data *lcd)
198 {
199         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
200                   lcd->lcd_last_transno, lcd->lcd_last_result);
201
202         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
203             lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
204                 req->rq_transno = lcd->lcd_last_close_transno;
205                 req->rq_status = lcd->lcd_last_close_result;
206         } else {
207                 req->rq_transno = lcd->lcd_last_transno;
208                 req->rq_status = lcd->lcd_last_result;
209                 mdt_vbr_reconstruct(req, lcd);
210         }
211         if (req->rq_status != 0)
212                 req->rq_transno = 0;
213         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
214         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
215         DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
216                   req->rq_transno, req->rq_status);
217
218         mdt_steal_ack_locks(req);
219 }
220
221 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
222                              struct mdt_lock_handle *lhc)
223 {
224         struct ptlrpc_request *req = mdt_info_req(mti);
225         struct tg_export_data *ted = &req->rq_export->exp_target_data;
226
227         return mdt_req_from_lcd(req, ted->ted_lcd);
228 }
229
230 /**
231  * Generate fake attributes for a non-existing object
232  *
233  * While the client was waiting for the reply, the original transaction
234  * got committed and corresponding rep-ack lock got released, then another
235  * client was able to destroy the object. But we still need to send some
236  * attributes back. So we fake them and set nlink=0, so the client will
237  * be able to detect a non-existing object and drop it from the cache
238  * immediately.
239  *
240  * \param[out] ma       attributes to fill
241  */
242 static void mdt_fake_ma(struct md_attr *ma)
243 {
244         ma->ma_valid = MA_INODE;
245         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
246         ma->ma_attr.la_valid = LA_NLINK;
247         ma->ma_attr.la_mode = S_IFREG;
248 }
249
250 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
251                                    struct mdt_lock_handle *lhc)
252 {
253         struct ptlrpc_request  *req = mdt_info_req(mti);
254         struct obd_export *exp = req->rq_export;
255         struct tg_export_data *ted = &exp->exp_target_data;
256         struct mdt_device *mdt = mti->mti_mdt;
257         struct mdt_object *child;
258         struct mdt_body *body;
259         int rc;
260
261         mdt_req_from_lcd(req, ted->ted_lcd);
262         if (req->rq_status)
263                 return;
264
265         /* if no error, so child was created with requested fid */
266         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
267         if (IS_ERR(child)) {
268                 rc = PTR_ERR(child);
269                 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
270                               "evicting client %s with export %s\n",
271                               PFID(mti->mti_rr.rr_fid2), rc,
272                               obd_uuid2str(&exp->exp_client_uuid),
273                               obd_export_nid2str(exp));
274                 mdt_export_evict(exp);
275                 RETURN_EXIT;
276         }
277
278         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
279         mti->mti_attr.ma_need = MA_INODE;
280         mti->mti_attr.ma_valid = 0;
281         rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
282         if (rc == -ENOENT) {
283                 mdt_fake_ma(&mti->mti_attr);
284         } else if (rc == -EREMOTE) {
285                 /* object was created on remote server */
286                 if (!mdt_is_dne_client(exp))
287                         /* Return -EIO for old client */
288                         rc = -EIO;
289
290                 req->rq_status = rc;
291                 body->mbo_valid |= OBD_MD_MDS;
292         }
293         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
294                            mdt_object_fid(child));
295         mdt_object_put(mti->mti_env, child);
296 }
297
298 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
299                                     struct mdt_lock_handle *lhc)
300 {
301         struct ptlrpc_request  *req = mdt_info_req(mti);
302         struct obd_export *exp = req->rq_export;
303         struct mdt_export_data *med = &exp->exp_mdt_data;
304         struct mdt_device *mdt = mti->mti_mdt;
305         struct mdt_object *obj;
306         struct mdt_body *body;
307         int rc;
308
309         mdt_req_from_lcd(req, med->med_ted.ted_lcd);
310         if (req->rq_status)
311                 return;
312
313         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
314         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
315         if (IS_ERR(obj)) {
316                 rc = PTR_ERR(obj);
317                 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
318                               "evicting client %s with export %s\n",
319                               PFID(mti->mti_rr.rr_fid1), rc,
320                               obd_uuid2str(&exp->exp_client_uuid),
321                               obd_export_nid2str(exp));
322                 mdt_export_evict(exp);
323                 RETURN_EXIT;
324         }
325
326         mti->mti_attr.ma_need = MA_INODE;
327         mti->mti_attr.ma_valid = 0;
328
329         rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
330         if (rc == -ENOENT)
331                 mdt_fake_ma(&mti->mti_attr);
332         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
333                            mdt_object_fid(obj));
334         if (mti->mti_ioepoch && (mti->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
335                 struct mdt_file_data *mfd;
336                 struct mdt_body *repbody;
337
338                 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
339                 repbody->mbo_ioepoch = obj->mot_ioepoch;
340                 spin_lock(&med->med_open_lock);
341                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
342                         if (mfd->mfd_xid == req->rq_xid)
343                                 break;
344                 }
345                 LASSERT(&mfd->mfd_list != &med->med_open_head);
346                 spin_unlock(&med->med_open_lock);
347                 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
348         }
349
350         mdt_object_put(mti->mti_env, obj);
351 }
352
353 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
354                                   struct mdt_lock_handle *lhc);
355
356 static mdt_reconstructor reconstructors[REINT_MAX] = {
357         [REINT_SETATTR]  = mdt_reconstruct_setattr,
358         [REINT_CREATE]   = mdt_reconstruct_create,
359         [REINT_LINK]     = mdt_reconstruct_generic,
360         [REINT_UNLINK]   = mdt_reconstruct_generic,
361         [REINT_RENAME]   = mdt_reconstruct_generic,
362         [REINT_OPEN]     = mdt_reconstruct_open,
363         [REINT_SETXATTR] = mdt_reconstruct_generic
364 };
365
366 void mdt_reconstruct(struct mdt_thread_info *mti,
367                      struct mdt_lock_handle *lhc)
368 {
369         ENTRY;
370         reconstructors[mti->mti_rr.rr_opcode](mti, lhc);
371         EXIT;
372 }