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