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