Whamcloud - gitweb
LU-1943 fld: Simplify transaction handling in FID/FLD
[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, 2012, Whamcloud, Inc.
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 static int mdt_clients_data_init(const struct lu_env *env,
81                                  struct mdt_device *mdt,
82                                  unsigned long last_size)
83 {
84         struct lr_server_data  *lsd = &mdt->mdt_lut.lut_lsd;
85         struct lsd_client_data *lcd;
86         struct obd_device      *obd = mdt2obd_dev(mdt);
87         loff_t off;
88         int cl_idx;
89         int rc = 0;
90         ENTRY;
91
92         OBD_ALLOC_PTR(lcd);
93         if (!lcd)
94                 RETURN(-ENOMEM);
95
96         /* When we do a clean MDS shutdown, we save the last_transno into
97          * the header.  If we find clients with higher last_transno values
98          * then those clients may need recovery done. */
99         LASSERT(cfs_atomic_read(&obd->obd_req_replay_clients) == 0);
100         for (cl_idx = 0, off = lsd->lsd_client_start;
101              off < last_size; cl_idx++) {
102                 __u64 last_transno;
103                 struct obd_export *exp;
104                 struct mdt_thread_info *mti;
105
106                 off = lsd->lsd_client_start +
107                         cl_idx * lsd->lsd_client_size;
108
109                 rc = lut_client_data_read(env, &mdt->mdt_lut, lcd, &off, cl_idx);
110                 if (rc) {
111                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
112                                LAST_RCVD, cl_idx, off, rc);
113                         rc = 0;
114                         break; /* read error shouldn't cause startup to fail */
115                 }
116
117                 if (lcd->lcd_uuid[0] == '\0') {
118                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
119                                cl_idx);
120                         continue;
121                 }
122
123                 last_transno = lcd_last_transno(lcd);
124
125                 /* These exports are cleaned up by mdt_obd_disconnect(), so
126                  * they need to be set up like real exports as
127                  * mdt_obd_connect() does.
128                  */
129                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
130                        " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
131                        last_transno, lsd->lsd_last_transno,
132                        lcd_last_xid(lcd));
133
134                 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
135                 if (IS_ERR(exp)) {
136                         if (PTR_ERR(exp) == -EALREADY) {
137                                 /* export already exists, zero out this one */
138                                 CERROR("Duplicate export %s!\n", lcd->lcd_uuid);
139                                 continue;
140                         }
141                         GOTO(err_client, rc = PTR_ERR(exp));
142                 }
143
144                 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
145                 LASSERT(mti != NULL);
146                 mti->mti_exp = exp;
147                 /* copy on-disk lcd to the export */
148                 *exp->exp_target_data.ted_lcd = *lcd;
149                 rc = lut_client_add(env, exp, cl_idx);
150                 /* can't fail existing */
151                 LASSERTF(rc == 0, "rc = %d\n", rc);
152                 /* VBR: set export last committed version */
153                 exp->exp_last_committed = last_transno;
154                 cfs_spin_lock(&exp->exp_lock);
155                 exp->exp_connecting = 0;
156                 exp->exp_in_recovery = 0;
157                 cfs_spin_unlock(&exp->exp_lock);
158                 obd->obd_max_recoverable_clients++;
159                 class_export_put(exp);
160
161                 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
162                        cl_idx, last_transno);
163                 /* protect __u64 value update */
164                 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
165                 mdt->mdt_lut.lut_last_transno = max(last_transno,
166                                                 mdt->mdt_lut.lut_last_transno);
167                 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
168         }
169
170 err_client:
171         OBD_FREE_PTR(lcd);
172         RETURN(rc);
173 }
174
175 static int mdt_server_data_init(const struct lu_env *env,
176                                 struct mdt_device *mdt,
177                                 struct lustre_sb_info *lsi)
178 {
179         struct lr_server_data  *lsd = &mdt->mdt_lut.lut_lsd;
180         struct lsd_client_data *lcd = NULL;
181         struct obd_device      *obd = mdt2obd_dev(mdt);
182         struct mdt_thread_info *mti;
183         struct dt_object       *obj;
184         struct lu_attr         *la;
185         unsigned long last_rcvd_size;
186         __u64 mount_count;
187         int rc;
188         ENTRY;
189
190         /* ensure padding in the struct is the correct size */
191         CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
192                 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
193         CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
194                 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
195
196         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
197         LASSERT(mti != NULL);
198         la = &mti->mti_attr.ma_attr;
199
200         obj = mdt->mdt_lut.lut_last_rcvd;
201         rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
202         if (rc)
203                 RETURN(rc);
204
205         last_rcvd_size = (unsigned long)la->la_size;
206
207         if (last_rcvd_size == 0) {
208                 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
209
210                 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
211                        sizeof(lsd->lsd_uuid));
212                 lsd->lsd_last_transno = 0;
213                 lsd->lsd_mount_count = 0;
214                 lsd->lsd_server_size = LR_SERVER_SIZE;
215                 lsd->lsd_client_start = LR_CLIENT_START;
216                 lsd->lsd_client_size = LR_CLIENT_SIZE;
217                 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
218                 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
219                 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
220                                             OBD_INCOMPAT_COMMON_LR |
221                                             OBD_INCOMPAT_MULTI_OI;
222         } else {
223                 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
224                 rc = lut_server_data_read(env, &mdt->mdt_lut);
225                 if (rc) {
226                         CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
227                         GOTO(out, rc);
228                 }
229                 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
230                         LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
231                                            "the wrong disk %s. Were the /dev/ "
232                                            "assignments rearranged?\n",
233                                            obd->obd_uuid.uuid, lsd->lsd_uuid);
234                         GOTO(out, rc = -EINVAL);
235                 }
236                 lsd->lsd_feature_compat |= OBD_COMPAT_MDT;
237                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_MDT |
238                                              OBD_INCOMPAT_COMMON_LR;
239         }
240         mount_count = lsd->lsd_mount_count;
241
242         if (lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP) {
243                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
244                        obd->obd_name,
245                        lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP);
246                 GOTO(out, rc = -EINVAL);
247         }
248         if (lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP) {
249                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
250                        obd->obd_name,
251                        lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP);
252                 /* XXX: Do something like remount filesystem read-only */
253                 GOTO(out, rc = -EINVAL);
254         }
255         /** Interop: evict all clients at first boot with 1.8 last_rcvd */
256         if (!(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
257                 if (last_rcvd_size > lsd->lsd_client_start) {
258                         LCONSOLE_WARN("Mounting %s at first time on 1.8 FS, "
259                                       "remove all clients for interop needs\n",
260                                       obd->obd_name);
261                         rc = lut_truncate_last_rcvd(env, &mdt->mdt_lut,
262                                                     lsd->lsd_client_start);
263                         if (rc)
264                                 GOTO(out, rc);
265                         last_rcvd_size = lsd->lsd_client_start;
266                 }
267                 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
268                 lsd->lsd_feature_compat |= OBD_COMPAT_20;
269         }
270
271         if (lsi->lsi_flags & LDD_F_IAM_DIR)
272                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
273
274         lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
275
276         cfs_spin_lock(&mdt->mdt_lut.lut_translock);
277         mdt->mdt_lut.lut_last_transno = lsd->lsd_last_transno;
278         cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
279
280         CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
281         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
282                obd->obd_name, mdt->mdt_lut.lut_last_transno);
283         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
284                obd->obd_name, mount_count + 1);
285         CDEBUG(D_INODE, "%s: server data size: %u\n",
286                obd->obd_name, lsd->lsd_server_size);
287         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
288                obd->obd_name, lsd->lsd_client_start);
289         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
290                obd->obd_name, lsd->lsd_client_size);
291         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
292                obd->obd_name, last_rcvd_size);
293         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
294                last_rcvd_size <= lsd->lsd_client_start ? 0 :
295                (last_rcvd_size - lsd->lsd_client_start) /
296                 lsd->lsd_client_size);
297         CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
298
299         if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
300             !lsd->lsd_client_size) {
301                 CERROR("Bad last_rcvd contents!\n");
302                 GOTO(out, rc = -EINVAL);
303         }
304
305         rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
306         if (rc)
307                 GOTO(err_client, rc);
308
309         cfs_spin_lock(&mdt->mdt_lut.lut_translock);
310         /* obd_last_committed is used for compatibility
311          * with other lustre recovery code */
312         obd->obd_last_committed = mdt->mdt_lut.lut_last_transno;
313         cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
314
315         obd->u.obt.obt_mount_count = mount_count + 1;
316         obd->u.obt.obt_instance = (__u32)obd->u.obt.obt_mount_count;
317         lsd->lsd_mount_count = obd->u.obt.obt_mount_count;
318
319         /* save it, so mount count and last_transno is current */
320         rc = lut_server_data_update(env, &mdt->mdt_lut, 0);
321         if (rc)
322                 GOTO(err_client, rc);
323
324         RETURN(0);
325
326 err_client:
327         class_disconnect_exports(obd);
328 out:
329         return rc;
330 }
331
332 /*
333  * last_rcvd & last_committed update callbacks
334  */
335 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
336                                 struct thandle *th)
337 {
338         struct mdt_device *mdt = mti->mti_mdt;
339         struct ptlrpc_request *req = mdt_info_req(mti);
340         struct tg_export_data *ted;
341         struct lsd_client_data *lcd;
342         loff_t off;
343         int err;
344         __s32 rc = th->th_result;
345
346         ENTRY;
347         LASSERT(req);
348         LASSERT(req->rq_export);
349         LASSERT(mdt);
350         ted = &req->rq_export->exp_target_data;
351         LASSERT(ted);
352
353         cfs_mutex_lock(&ted->ted_lcd_lock);
354         lcd = ted->ted_lcd;
355         /* if the export has already been disconnected, we have no last_rcvd slot,
356          * update server data with latest transno then */
357         if (lcd == NULL) {
358                 cfs_mutex_unlock(&ted->ted_lcd_lock);
359                 CWARN("commit transaction for disconnected client %s: rc %d\n",
360                       req->rq_export->exp_client_uuid.uuid, rc);
361                 err = lut_server_data_write(mti->mti_env, &mdt->mdt_lut, th);
362                 RETURN(err);
363         }
364
365         off = ted->ted_lr_off;
366         LASSERT(ergo(mti->mti_transno == 0, rc != 0));
367         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
368             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
369                 if (mti->mti_transno != 0) {
370                         if (lcd->lcd_last_close_transno > mti->mti_transno) {
371                                 CERROR("Trying to overwrite bigger transno:"
372                                        "on-disk: "LPU64", new: "LPU64" "
373                                        "replay: %d. see LU-617.\n",
374                                        lcd->lcd_last_close_transno,
375                                        mti->mti_transno, req_is_replay(req));
376                                 if (req_is_replay(req)) {
377                                         cfs_spin_lock(&req->rq_export->exp_lock);
378                                         req->rq_export->exp_vbr_failed = 1;
379                                         cfs_spin_unlock(&req->rq_export->exp_lock);
380                                 }
381                                 cfs_mutex_unlock(&ted->ted_lcd_lock);
382                                 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
383                         }
384                         lcd->lcd_last_close_transno = mti->mti_transno;
385                 }
386                 lcd->lcd_last_close_xid = req->rq_xid;
387                 lcd->lcd_last_close_result = rc;
388         } else {
389                 /* VBR: save versions in last_rcvd for reconstruct. */
390                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
391                 if (pre_versions) {
392                         lcd->lcd_pre_versions[0] = pre_versions[0];
393                         lcd->lcd_pre_versions[1] = pre_versions[1];
394                         lcd->lcd_pre_versions[2] = pre_versions[2];
395                         lcd->lcd_pre_versions[3] = pre_versions[3];
396                 }
397                 if (mti->mti_transno != 0) {
398                         if (lcd->lcd_last_transno > mti->mti_transno) {
399                                 CERROR("Trying to overwrite bigger transno:"
400                                        "on-disk: "LPU64", new: "LPU64" "
401                                        "replay: %d. see LU-617.\n",
402                                        lcd->lcd_last_transno,
403                                        mti->mti_transno, req_is_replay(req));
404                                 if (req_is_replay(req)) {
405                                         cfs_spin_lock(&req->rq_export->exp_lock);
406                                         req->rq_export->exp_vbr_failed = 1;
407                                         cfs_spin_unlock(&req->rq_export->exp_lock);
408                                 }
409                                 cfs_mutex_unlock(&ted->ted_lcd_lock);
410                                 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
411                         }
412                         lcd->lcd_last_transno = mti->mti_transno;
413                 }
414                 lcd->lcd_last_xid = req->rq_xid;
415                 lcd->lcd_last_result = rc;
416                 /*XXX: save intent_disposition in mdt_thread_info?
417                  * also there is bug - intent_dispostion is __u64,
418                  * see struct ldlm_reply->lock_policy_res1; */
419                 lcd->lcd_last_data = mti->mti_opdata;
420         }
421
422         if ((mti->mti_exp->exp_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0) {
423                 /* Although lightweight (LW) connections have no slot in
424                  * last_rcvd, we still want to maintain the in-memory
425                  * lsd_client_data structure in order to properly handle reply
426                  * reconstruction. */
427                 struct lu_target        *tg = &mdt->mdt_lut;
428                 bool                     update = false;
429
430                 cfs_mutex_unlock(&ted->ted_lcd_lock);
431                 err = 0;
432
433                 /* All operations performed by LW clients are synchronous and
434                  * we store the committed transno in the last_rcvd header */
435                 cfs_spin_lock(&tg->lut_translock);
436                 if (mti->mti_transno > tg->lut_lsd.lsd_last_transno) {
437                         tg->lut_lsd.lsd_last_transno = mti->mti_transno;
438                         update = true;
439                 }
440                 cfs_spin_unlock(&tg->lut_translock);
441
442                 if (update)
443                         err = lut_server_data_write(mti->mti_env, tg, th);
444         } else if (off <= 0) {
445                 CERROR("%s: client idx %d has offset %lld\n",
446                        mdt2obd_dev(mdt)->obd_name, ted->ted_lr_idx, off);
447                 cfs_mutex_unlock(&ted->ted_lcd_lock);
448                 err = -EINVAL;
449         } else {
450                 err = lut_client_data_write(mti->mti_env, &mdt->mdt_lut, lcd,
451                                             &off, th);
452                 cfs_mutex_unlock(&ted->ted_lcd_lock);
453         }
454         RETURN(err);
455 }
456
457 extern struct lu_context_key mdt_thread_key;
458
459 /* add credits for last_rcvd update */
460 static int mdt_txn_start_cb(const struct lu_env *env,
461                             struct thandle *th, void *cookie)
462 {
463         struct mdt_device *mdt = cookie;
464         struct mdt_thread_info *mti;
465         int rc;
466         ENTRY;
467
468         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
469
470         LASSERT(mdt->mdt_lut.lut_last_rcvd);
471         if (mti->mti_exp == NULL)
472                 RETURN(0);
473
474         rc = dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
475                                      sizeof(struct lsd_client_data),
476                                      mti->mti_exp->exp_target_data.ted_lr_off,
477                                      th);
478         if (rc)
479                 return rc;
480
481         rc = dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
482                                      sizeof(struct lr_server_data), 0, th);
483         if (rc)
484                 return rc;
485
486         if (mti->mti_mos != NULL)
487                 rc = dt_declare_version_set(env, mdt_obj2dt(mti->mti_mos), th);
488
489         return rc;
490 }
491
492 /* Update last_rcvd records with latests transaction data */
493 static int mdt_txn_stop_cb(const struct lu_env *env,
494                            struct thandle *txn, void *cookie)
495 {
496         struct mdt_device *mdt = cookie;
497         struct mdt_thread_info *mti;
498         struct ptlrpc_request *req;
499
500         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
501         req = mdt_info_req(mti);
502
503         if (mti->mti_mdt == NULL || req == NULL)
504                 return 0;
505
506         if (mti->mti_has_trans) {
507                 /* XXX: currently there are allowed cases, but the wrong cases
508                  * are also possible, so better check is needed here */
509                 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
510                        mti->mti_transno);
511                 return 0;
512         }
513
514         mti->mti_has_trans = 1;
515         cfs_spin_lock(&mdt->mdt_lut.lut_translock);
516         if (txn->th_result != 0) {
517                 if (mti->mti_transno != 0) {
518                         CERROR("Replay transno "LPU64" failed: rc %d\n",
519                                mti->mti_transno, txn->th_result);
520                 }
521         } else if (mti->mti_transno == 0) {
522                 mti->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
523         } else {
524                 /* should be replay */
525                 if (mti->mti_transno > mdt->mdt_lut.lut_last_transno)
526                         mdt->mdt_lut.lut_last_transno = mti->mti_transno;
527         }
528         cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
529         /* sometimes the reply message has not been successfully packed */
530         LASSERT(req != NULL && req->rq_repmsg != NULL);
531
532         /** VBR: set new versions */
533         if (txn->th_result == 0 && mti->mti_mos != NULL) {
534                 dt_version_set(env, mdt_obj2dt(mti->mti_mos),
535                                mti->mti_transno, txn);
536                 mti->mti_mos = NULL;
537         }
538
539         /* filling reply data */
540         CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
541                mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
542
543         req->rq_transno = mti->mti_transno;
544         lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
545         /* if can't add callback, do sync write */
546         txn->th_sync |= !!lut_last_commit_cb_add(txn, &mdt->mdt_lut,
547                                                  mti->mti_exp,
548                                                  mti->mti_transno);
549         return mdt_last_rcvd_update(mti, txn);
550 }
551
552 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
553                  struct obd_device *obd,
554                  struct lustre_sb_info *lsi)
555 {
556         int rc = 0;
557         ENTRY;
558
559         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
560                 RETURN(-ENOENT);
561
562         /* prepare transactions callbacks */
563         mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
564         mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
565         mdt->mdt_txn_cb.dtc_txn_commit = NULL;
566         mdt->mdt_txn_cb.dtc_cookie = mdt;
567         mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
568         CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
569
570         dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
571
572         rc = mdt_server_data_init(env, mdt, lsi);
573
574         RETURN(rc);
575 }
576
577 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
578 {
579         ENTRY;
580
581         /* Remove transaction callback */
582         dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
583         if (mdt->mdt_ck_obj)
584                 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
585         mdt->mdt_ck_obj = NULL;
586         EXIT;
587 }
588
589 /* reconstruction code */
590 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
591 {
592         struct ptlrpc_service_part *svcpt;
593         struct obd_export         *exp = req->rq_export;
594         cfs_list_t                *tmp;
595         struct ptlrpc_reply_state *oldrep;
596         int                        i;
597
598         /* CAVEAT EMPTOR: spinlock order */
599         cfs_spin_lock(&exp->exp_lock);
600         cfs_list_for_each (tmp, &exp->exp_outstanding_replies) {
601                 oldrep = cfs_list_entry(tmp, struct ptlrpc_reply_state,
602                                         rs_exp_list);
603
604                 if (oldrep->rs_xid != req->rq_xid)
605                         continue;
606
607                 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
608                         CERROR ("Resent req xid "LPU64" has mismatched opc: "
609                                 "new %d old %d\n", req->rq_xid,
610                                 lustre_msg_get_opc(req->rq_reqmsg),
611                                 oldrep->rs_opc);
612
613                 svcpt = oldrep->rs_svcpt;
614                 cfs_spin_lock(&svcpt->scp_rep_lock);
615
616                 cfs_list_del_init (&oldrep->rs_exp_list);
617
618                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
619                       " o%d NID %s\n",
620                       oldrep->rs_nlocks, oldrep,
621                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
622                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
623
624                 for (i = 0; i < oldrep->rs_nlocks; i++)
625                         ptlrpc_save_lock(req, &oldrep->rs_locks[i],
626                                          oldrep->rs_modes[i], 0);
627                 oldrep->rs_nlocks = 0;
628
629                 DEBUG_REQ(D_HA, req, "stole locks for");
630                 cfs_spin_lock(&oldrep->rs_lock);
631                 ptlrpc_schedule_difficult_reply (oldrep);
632                 cfs_spin_unlock(&oldrep->rs_lock);
633
634                 cfs_spin_unlock(&svcpt->scp_rep_lock);
635                 break;
636         }
637         cfs_spin_unlock(&exp->exp_lock);
638 }
639
640 /**
641  * VBR: restore versions
642  */
643 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
644                          struct lsd_client_data *lcd)
645 {
646         __u64 pre_versions[4] = {0};
647         pre_versions[0] = lcd->lcd_pre_versions[0];
648         pre_versions[1] = lcd->lcd_pre_versions[1];
649         pre_versions[2] = lcd->lcd_pre_versions[2];
650         pre_versions[3] = lcd->lcd_pre_versions[3];
651         lustre_msg_set_versions(req->rq_repmsg, pre_versions);
652 }
653
654 void mdt_req_from_lcd(struct ptlrpc_request *req,
655                       struct lsd_client_data *lcd)
656 {
657         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
658                   lcd->lcd_last_transno, lcd->lcd_last_result);
659
660         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
661             lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
662                 req->rq_transno = lcd->lcd_last_close_transno;
663                 req->rq_status = lcd->lcd_last_close_result;
664         } else {
665                 req->rq_transno = lcd->lcd_last_transno;
666                 req->rq_status = lcd->lcd_last_result;
667                 mdt_vbr_reconstruct(req, lcd);
668         }
669         if (req->rq_status != 0)
670                 req->rq_transno = 0;
671         lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
672         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
673         DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
674                   req->rq_transno, req->rq_status);
675
676         mdt_steal_ack_locks(req);
677 }
678
679 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
680                              struct mdt_lock_handle *lhc)
681 {
682         struct ptlrpc_request *req = mdt_info_req(mti);
683         struct tg_export_data *ted = &req->rq_export->exp_target_data;
684
685         return mdt_req_from_lcd(req, ted->ted_lcd);
686 }
687
688 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
689                                    struct mdt_lock_handle *lhc)
690 {
691         struct ptlrpc_request  *req = mdt_info_req(mti);
692         struct obd_export *exp = req->rq_export;
693         struct tg_export_data *ted = &exp->exp_target_data;
694         struct mdt_device *mdt = mti->mti_mdt;
695         struct mdt_object *child;
696         struct mdt_body *body;
697         int rc;
698
699         mdt_req_from_lcd(req, ted->ted_lcd);
700         if (req->rq_status)
701                 return;
702
703         /* if no error, so child was created with requested fid */
704         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
705         if (IS_ERR(child)) {
706                 rc = PTR_ERR(child);
707                 LCONSOLE_WARN("Child "DFID" lookup error %d."
708                               " Evicting client %s with export %s.\n",
709                               PFID(mdt_object_fid(child)), rc,
710                               obd_uuid2str(&exp->exp_client_uuid),
711                               obd_export_nid2str(exp));
712                 mdt_export_evict(exp);
713                 EXIT;
714                 return;
715         }
716
717         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
718         mti->mti_attr.ma_need = MA_INODE;
719         mti->mti_attr.ma_valid = 0;
720         rc = mdt_attr_get_complex(mti, child, &mti->mti_attr);
721         if (rc == -EREMOTE) {
722                 /* object was created on remote server */
723                 req->rq_status = rc;
724                 body->valid |= OBD_MD_MDS;
725         }
726         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
727                            mdt_object_fid(child));
728         mdt_object_put(mti->mti_env, child);
729 }
730
731 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
732                                     struct mdt_lock_handle *lhc)
733 {
734         struct ptlrpc_request  *req = mdt_info_req(mti);
735         struct obd_export *exp = req->rq_export;
736         struct mdt_export_data *med = &exp->exp_mdt_data;
737         struct mdt_device *mdt = mti->mti_mdt;
738         struct mdt_object *obj;
739         struct mdt_body *body;
740
741         mdt_req_from_lcd(req, med->med_ted.ted_lcd);
742         if (req->rq_status)
743                 return;
744
745         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
746         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
747         if (IS_ERR(obj)) {
748                 int rc = PTR_ERR(obj);
749                 LCONSOLE_WARN(""DFID" lookup error %d."
750                               " Evicting client %s with export %s.\n",
751                               PFID(mdt_object_fid(obj)), rc,
752                               obd_uuid2str(&exp->exp_client_uuid),
753                               obd_export_nid2str(exp));
754                 mdt_export_evict(exp);
755                 EXIT;
756                 return;
757         }
758         mti->mti_attr.ma_need = MA_INODE;
759         mti->mti_attr.ma_valid = 0;
760         mdt_attr_get_complex(mti, obj, &mti->mti_attr);
761         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
762                            mdt_object_fid(obj));
763         if (mti->mti_ioepoch && (mti->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
764                 struct mdt_file_data *mfd;
765                 struct mdt_body *repbody;
766
767                 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
768                 repbody->ioepoch = obj->mot_ioepoch;
769                 cfs_spin_lock(&med->med_open_lock);
770                 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
771                         if (mfd->mfd_xid == req->rq_xid)
772                                 break;
773                 }
774                 LASSERT(&mfd->mfd_list != &med->med_open_head);
775                 cfs_spin_unlock(&med->med_open_lock);
776                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
777         }
778
779         mdt_object_put(mti->mti_env, obj);
780 }
781
782 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
783                                   struct mdt_lock_handle *lhc);
784
785 static mdt_reconstructor reconstructors[REINT_MAX] = {
786         [REINT_SETATTR]  = mdt_reconstruct_setattr,
787         [REINT_CREATE]   = mdt_reconstruct_create,
788         [REINT_LINK]     = mdt_reconstruct_generic,
789         [REINT_UNLINK]   = mdt_reconstruct_generic,
790         [REINT_RENAME]   = mdt_reconstruct_generic,
791         [REINT_OPEN]     = mdt_reconstruct_open,
792         [REINT_SETXATTR] = mdt_reconstruct_generic
793 };
794
795 void mdt_reconstruct(struct mdt_thread_info *mti,
796                      struct mdt_lock_handle *lhc)
797 {
798         ENTRY;
799         reconstructors[mti->mti_rr.rr_opcode](mti, lhc);
800         EXIT;
801 }