Whamcloud - gitweb
LU-3963 libcfs: convert lod, mdt, and gss to linux list api
[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, 2013, 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 void mdt_trans_stop(const struct lu_env *env,
75                     struct mdt_device *mdt, struct thandle *th)
76 {
77         dt_trans_stop(env, mdt->mdt_bottom, th);
78 }
79
80 /*
81  * last_rcvd & last_committed update callbacks
82  */
83 extern struct lu_context_key mdt_thread_key;
84
85 /* This callback notifies MDT that transaction was done. This is needed by
86  * mdt_save_lock() only. It is similar to new target code and will be removed
87  * as mdt_save_lock() will be converted to use target structures */
88 static int mdt_txn_stop_cb(const struct lu_env *env,
89                            struct thandle *txn, void *cookie)
90 {
91         struct mdt_thread_info  *mti;
92
93         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
94         LASSERT(mti);
95
96         if (mti->mti_has_trans)
97                 CDEBUG(D_INFO, "More than one transaction\n");
98         else
99                 mti->mti_has_trans = 1;
100         return 0;
101 }
102
103 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
104                  struct obd_device *obd, struct lustre_sb_info *lsi)
105 {
106         int rc = 0;
107
108         ENTRY;
109
110         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
111                 RETURN(-ENOENT);
112
113         /* prepare transactions callbacks */
114         mdt->mdt_txn_cb.dtc_txn_start = NULL;
115         mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
116         mdt->mdt_txn_cb.dtc_txn_commit = NULL;
117         mdt->mdt_txn_cb.dtc_cookie = NULL;
118         mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
119         INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
120
121         dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
122
123         RETURN(rc);
124 }
125
126 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
127 {
128         ENTRY;
129
130         /* Remove transaction callback */
131         dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
132         if (mdt->mdt_ck_obj)
133                 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
134         mdt->mdt_ck_obj = NULL;
135         EXIT;
136 }
137
138 /* reconstruction code */
139 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
140 {
141         struct ptlrpc_service_part *svcpt;
142         struct obd_export          *exp = req->rq_export;
143         struct list_head           *tmp;
144         struct ptlrpc_reply_state  *oldrep;
145         int                         i;
146
147         /* CAVEAT EMPTOR: spinlock order */
148         spin_lock(&exp->exp_lock);
149         list_for_each(tmp, &exp->exp_outstanding_replies) {
150                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,
151                                     rs_exp_list);
152
153                 if (oldrep->rs_xid != req->rq_xid)
154                         continue;
155
156                 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
157                         CERROR ("Resent req xid "LPU64" has mismatched opc: "
158                                 "new %d old %d\n", req->rq_xid,
159                                 lustre_msg_get_opc(req->rq_reqmsg),
160                                 oldrep->rs_opc);
161
162                 svcpt = oldrep->rs_svcpt;
163                 spin_lock(&svcpt->scp_rep_lock);
164
165                 list_del_init(&oldrep->rs_exp_list);
166
167                 CDEBUG(D_HA, "Stealing %d locks from rs %p x"LPD64".t"LPD64
168                        " o%d NID %s\n",
169                        oldrep->rs_nlocks, oldrep,
170                        oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
171                        libcfs_nid2str(exp->exp_connection->c_peer.nid));
172
173                 for (i = 0; i < oldrep->rs_nlocks; i++)
174                         ptlrpc_save_lock(req, &oldrep->rs_locks[i],
175                                          oldrep->rs_modes[i], 0);
176                 oldrep->rs_nlocks = 0;
177
178                 DEBUG_REQ(D_HA, req, "stole locks for");
179                 spin_lock(&oldrep->rs_lock);
180                 ptlrpc_schedule_difficult_reply(oldrep);
181                 spin_unlock(&oldrep->rs_lock);
182
183                 spin_unlock(&svcpt->scp_rep_lock);
184                 break;
185         }
186         spin_unlock(&exp->exp_lock);
187 }
188
189 /**
190  * VBR: restore versions
191  */
192 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
193                          struct lsd_client_data *lcd)
194 {
195         __u64 pre_versions[4] = {0};
196         pre_versions[0] = lcd->lcd_pre_versions[0];
197         pre_versions[1] = lcd->lcd_pre_versions[1];
198         pre_versions[2] = lcd->lcd_pre_versions[2];
199         pre_versions[3] = lcd->lcd_pre_versions[3];
200         lustre_msg_set_versions(req->rq_repmsg, pre_versions);
201 }
202
203 void mdt_req_from_lcd(struct ptlrpc_request *req,
204                       struct lsd_client_data *lcd)
205 {
206         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
207                   lcd->lcd_last_transno, lcd->lcd_last_result);
208
209         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
210             lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
211                 req->rq_transno = lcd->lcd_last_close_transno;
212                 req->rq_status = lcd->lcd_last_close_result;
213         } else {
214                 req->rq_transno = lcd->lcd_last_transno;
215                 req->rq_status = lcd->lcd_last_result;
216                 mdt_vbr_reconstruct(req, lcd);
217         }
218         if (req->rq_status != 0)
219                 req->rq_transno = 0;
220         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
221         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
222         DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
223                   req->rq_transno, req->rq_status);
224
225         mdt_steal_ack_locks(req);
226 }
227
228 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
229                              struct mdt_lock_handle *lhc)
230 {
231         struct ptlrpc_request *req = mdt_info_req(mti);
232         struct tg_export_data *ted = &req->rq_export->exp_target_data;
233
234         return mdt_req_from_lcd(req, ted->ted_lcd);
235 }
236
237 /**
238  * Generate fake attributes for a non-existing object
239  *
240  * While the client was waiting for the reply, the original transaction
241  * got committed and corresponding rep-ack lock got released, then another
242  * client was able to destroy the object. But we still need to send some
243  * attributes back. So we fake them and set nlink=0, so the client will
244  * be able to detect a non-existing object and drop it from the cache
245  * immediately.
246  *
247  * \param[out] ma       attributes to fill
248  */
249 static void mdt_fake_ma(struct md_attr *ma)
250 {
251         ma->ma_valid = MA_INODE;
252         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
253         ma->ma_attr.la_valid = LA_NLINK;
254         ma->ma_attr.la_mode = S_IFREG;
255 }
256
257 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
258                                    struct mdt_lock_handle *lhc)
259 {
260         struct ptlrpc_request  *req = mdt_info_req(mti);
261         struct obd_export *exp = req->rq_export;
262         struct tg_export_data *ted = &exp->exp_target_data;
263         struct mdt_device *mdt = mti->mti_mdt;
264         struct mdt_object *child;
265         struct mdt_body *body;
266         int rc;
267
268         mdt_req_from_lcd(req, ted->ted_lcd);
269         if (req->rq_status)
270                 return;
271
272         /* if no error, so child was created with requested fid */
273         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
274         if (IS_ERR(child)) {
275                 rc = PTR_ERR(child);
276                 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
277                               "evicting client %s with export %s\n",
278                               PFID(mti->mti_rr.rr_fid2), rc,
279                               obd_uuid2str(&exp->exp_client_uuid),
280                               obd_export_nid2str(exp));
281                 mdt_export_evict(exp);
282                 RETURN_EXIT;
283         }
284
285         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
286         mti->mti_attr.ma_need = MA_INODE;
287         mti->mti_attr.ma_valid = 0;
288         rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
289         if (rc == -ENOENT) {
290                 mdt_fake_ma(&mti->mti_attr);
291         } else if (rc == -EREMOTE) {
292                 /* object was created on remote server */
293                 if (!mdt_is_dne_client(exp))
294                         /* Return -EIO for old client */
295                         rc = -EIO;
296
297                 req->rq_status = rc;
298                 body->mbo_valid |= OBD_MD_MDS;
299         }
300         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
301                            mdt_object_fid(child));
302         mdt_object_put(mti->mti_env, child);
303 }
304
305 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
306                                     struct mdt_lock_handle *lhc)
307 {
308         struct ptlrpc_request  *req = mdt_info_req(mti);
309         struct obd_export *exp = req->rq_export;
310         struct mdt_export_data *med = &exp->exp_mdt_data;
311         struct mdt_device *mdt = mti->mti_mdt;
312         struct mdt_object *obj;
313         struct mdt_body *body;
314         int rc;
315
316         mdt_req_from_lcd(req, med->med_ted.ted_lcd);
317         if (req->rq_status)
318                 return;
319
320         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
321         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
322         if (IS_ERR(obj)) {
323                 rc = PTR_ERR(obj);
324                 LCONSOLE_WARN("cannot lookup "DFID": rc = %d; "
325                               "evicting client %s with export %s\n",
326                               PFID(mti->mti_rr.rr_fid1), rc,
327                               obd_uuid2str(&exp->exp_client_uuid),
328                               obd_export_nid2str(exp));
329                 mdt_export_evict(exp);
330                 RETURN_EXIT;
331         }
332
333         mti->mti_attr.ma_need = MA_INODE;
334         mti->mti_attr.ma_valid = 0;
335
336         rc = mdt_attr_get_complex(mti, obj, &mti->mti_attr);
337         if (rc == -ENOENT)
338                 mdt_fake_ma(&mti->mti_attr);
339         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
340                            mdt_object_fid(obj));
341         if (mti->mti_ioepoch && (mti->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
342                 struct mdt_file_data *mfd;
343                 struct mdt_body *repbody;
344
345                 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
346                 repbody->mbo_ioepoch = obj->mot_ioepoch;
347                 spin_lock(&med->med_open_lock);
348                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
349                         if (mfd->mfd_xid == req->rq_xid)
350                                 break;
351                 }
352                 LASSERT(&mfd->mfd_list != &med->med_open_head);
353                 spin_unlock(&med->med_open_lock);
354                 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
355         }
356
357         mdt_object_put(mti->mti_env, obj);
358 }
359
360 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
361                                   struct mdt_lock_handle *lhc);
362
363 static mdt_reconstructor reconstructors[REINT_MAX] = {
364         [REINT_SETATTR]  = mdt_reconstruct_setattr,
365         [REINT_CREATE]   = mdt_reconstruct_create,
366         [REINT_LINK]     = mdt_reconstruct_generic,
367         [REINT_UNLINK]   = mdt_reconstruct_generic,
368         [REINT_RENAME]   = mdt_reconstruct_generic,
369         [REINT_OPEN]     = mdt_reconstruct_open,
370         [REINT_SETXATTR] = mdt_reconstruct_generic
371 };
372
373 void mdt_reconstruct(struct mdt_thread_info *mti,
374                      struct mdt_lock_handle *lhc)
375 {
376         ENTRY;
377         reconstructors[mti->mti_rr.rr_opcode](mti, lhc);
378         EXIT;
379 }