Whamcloud - gitweb
b=17194
[fs/lustre-release.git] / lustre / mdt / mdt_recovery.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 static int mdt_server_data_update(const struct lu_env *env,
52                                   struct mdt_device *mdt,
53                                   int need_sync);
54
55 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
56 {
57         struct lu_buf *buf;
58         struct mdt_thread_info *mti;
59
60         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
61         buf = &mti->mti_buf;
62         buf->lb_buf = area;
63         buf->lb_len = len;
64         return buf;
65 }
66
67 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
68                                    const void *area, ssize_t len)
69 {
70         struct lu_buf *buf;
71         struct mdt_thread_info *mti;
72
73         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
74         buf = &mti->mti_buf;
75
76         buf->lb_buf = (void *)area;
77         buf->lb_len = len;
78         return buf;
79 }
80
81 int mdt_record_read(const struct lu_env *env,
82                     struct dt_object *dt, struct lu_buf *buf, loff_t *pos)
83 {
84         int rc;
85
86         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
87
88         rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
89
90         if (rc == buf->lb_len)
91                 rc = 0;
92         else if (rc >= 0)
93                 rc = -EFAULT;
94         return rc;
95 }
96
97 int mdt_record_write(const struct lu_env *env,
98                      struct dt_object *dt, const struct lu_buf *buf,
99                      loff_t *pos, struct thandle *th)
100 {
101         int rc;
102
103         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
104         LASSERT(th != NULL);
105         rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
106         if (rc == buf->lb_len)
107                 rc = 0;
108         else if (rc >= 0)
109                 rc = -EFAULT;
110         return rc;
111 }
112
113 static inline int mdt_trans_credit_get(const struct lu_env *env,
114                                        struct mdt_device *mdt,
115                                        enum mdt_txn_op op)
116 {
117         struct dt_device *dev = mdt->mdt_bottom;
118         int cr;
119         switch (op) {
120                 case MDT_TXN_CAPA_KEYS_WRITE_OP:
121                 case MDT_TXN_LAST_RCVD_WRITE_OP:
122                         cr = dev->dd_ops->dt_credit_get(env,
123                                                         dev,
124                                                         DTO_WRITE_BLOCK);
125                 break;
126                 default:
127                         LBUG();
128         }
129         return cr;
130 }
131
132 void mdt_trans_credit_init(const struct lu_env *env,
133                            struct mdt_device *mdt,
134                            enum mdt_txn_op op)
135 {
136         struct mdt_thread_info *mti;
137         struct txn_param *p;
138         int cr;
139
140         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
141         p = &mti->mti_txn_param;
142
143         cr = mdt_trans_credit_get(env, mdt, op);
144         txn_param_init(p, cr);
145 }
146
147 struct thandle* mdt_trans_start(const struct lu_env *env,
148                                 struct mdt_device *mdt)
149 {
150         struct mdt_thread_info *mti;
151         struct txn_param *p;
152
153         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
154         p = &mti->mti_txn_param;
155
156         /* export can require sync operations */
157         if (mti->mti_exp != NULL)
158                 p->tp_sync = mti->mti_exp->exp_need_sync;
159
160         return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
161 }
162
163 void mdt_trans_stop(const struct lu_env *env,
164                     struct mdt_device *mdt, struct thandle *th)
165 {
166         mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
167 }
168
169 /* last_rcvd handling */
170 static inline void lsd_le_to_cpu(struct lr_server_data *buf,
171                                  struct lr_server_data *lsd)
172 {
173         memcpy(lsd->lsd_uuid, buf->lsd_uuid, sizeof (lsd->lsd_uuid));
174         lsd->lsd_last_transno     = le64_to_cpu(buf->lsd_last_transno);
175         lsd->lsd_mount_count      = le64_to_cpu(buf->lsd_mount_count);
176         lsd->lsd_feature_compat   = le32_to_cpu(buf->lsd_feature_compat);
177         lsd->lsd_feature_rocompat = le32_to_cpu(buf->lsd_feature_rocompat);
178         lsd->lsd_feature_incompat = le32_to_cpu(buf->lsd_feature_incompat);
179         lsd->lsd_server_size      = le32_to_cpu(buf->lsd_server_size);
180         lsd->lsd_client_start     = le32_to_cpu(buf->lsd_client_start);
181         lsd->lsd_client_size      = le16_to_cpu(buf->lsd_client_size);
182 }
183
184 static inline void lsd_cpu_to_le(struct lr_server_data *lsd,
185                                  struct lr_server_data *buf)
186 {
187         memcpy(buf->lsd_uuid, lsd->lsd_uuid, sizeof (lsd->lsd_uuid));
188         buf->lsd_last_transno     = cpu_to_le64(lsd->lsd_last_transno);
189         buf->lsd_mount_count      = cpu_to_le64(lsd->lsd_mount_count);
190         buf->lsd_feature_compat   = cpu_to_le32(lsd->lsd_feature_compat);
191         buf->lsd_feature_rocompat = cpu_to_le32(lsd->lsd_feature_rocompat);
192         buf->lsd_feature_incompat = cpu_to_le32(lsd->lsd_feature_incompat);
193         buf->lsd_server_size      = cpu_to_le32(lsd->lsd_server_size);
194         buf->lsd_client_start     = cpu_to_le32(lsd->lsd_client_start);
195         buf->lsd_client_size      = cpu_to_le16(lsd->lsd_client_size);
196 }
197
198 static inline void lcd_le_to_cpu(struct lsd_client_data *buf,
199                                  struct lsd_client_data *lcd)
200 {
201         memcpy(lcd->lcd_uuid, buf->lcd_uuid, sizeof (lcd->lcd_uuid));
202         lcd->lcd_last_transno       = le64_to_cpu(buf->lcd_last_transno);
203         lcd->lcd_last_xid           = le64_to_cpu(buf->lcd_last_xid);
204         lcd->lcd_last_result        = le32_to_cpu(buf->lcd_last_result);
205         lcd->lcd_last_data          = le32_to_cpu(buf->lcd_last_data);
206         lcd->lcd_last_close_transno = le64_to_cpu(buf->lcd_last_close_transno);
207         lcd->lcd_last_close_xid     = le64_to_cpu(buf->lcd_last_close_xid);
208         lcd->lcd_last_close_result  = le32_to_cpu(buf->lcd_last_close_result);
209 }
210
211 static inline void lcd_cpu_to_le(struct lsd_client_data *lcd,
212                                  struct lsd_client_data *buf)
213 {
214         memcpy(buf->lcd_uuid, lcd->lcd_uuid, sizeof (lcd->lcd_uuid));
215         buf->lcd_last_transno       = cpu_to_le64(lcd->lcd_last_transno);
216         buf->lcd_last_xid           = cpu_to_le64(lcd->lcd_last_xid);
217         buf->lcd_last_result        = cpu_to_le32(lcd->lcd_last_result);
218         buf->lcd_last_data          = cpu_to_le32(lcd->lcd_last_data);
219         buf->lcd_last_close_transno = cpu_to_le64(lcd->lcd_last_close_transno);
220         buf->lcd_last_close_xid     = cpu_to_le64(lcd->lcd_last_close_xid);
221         buf->lcd_last_close_result  = cpu_to_le32(lcd->lcd_last_close_result);
222 }
223
224 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
225                                             struct mdt_device *mdt)
226 {
227         struct mdt_thread_info *mti;
228         int rc;
229
230         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
231
232         mti->mti_off = 0;
233         rc = mdt_record_read(env, mdt->mdt_last_rcvd,
234                              mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
235                              &mti->mti_off);
236         if (rc == 0)
237                 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
238
239         CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
240                        "uuid = %s\n"
241                        "last_transno = "LPU64"\n",
242                         rc, mdt->mdt_lsd.lsd_uuid,
243                         mdt->mdt_lsd.lsd_last_transno);
244         return rc;
245 }
246
247 static void mdt_client_cb(const struct mdt_device *mdt, __u64 transno,
248                           void *data, int err)
249 {
250         struct obd_device *obd = mdt2obd_dev(mdt);
251         target_client_add_cb(obd, transno, data, err);
252 }
253
254 static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
255                                              struct mdt_device *mdt,
256                                              int need_sync)
257 {
258         struct mdt_thread_info *mti;
259         struct thandle *th;
260         int rc;
261         ENTRY;
262
263         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
264
265         if (mti->mti_exp) {
266                 spin_lock(&mti->mti_exp->exp_lock);
267                 mti->mti_exp->exp_need_sync = need_sync;
268                 spin_unlock(&mti->mti_exp->exp_lock);
269         }
270         mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
271         th = mdt_trans_start(env, mdt);
272         if (IS_ERR(th))
273                 RETURN(PTR_ERR(th));
274
275         mti->mti_off = 0;
276         lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
277
278         if (need_sync && mti->mti_exp)
279                 mdt_trans_add_cb(th, mdt_client_cb, mti->mti_exp);
280
281         rc = mdt_record_write(env, mdt->mdt_last_rcvd,
282                               mdt_buf_const(env, &mti->mti_lsd,
283                                             sizeof(mti->mti_lsd)),
284                               &mti->mti_off, th);
285
286         mdt_trans_stop(env, mdt, th);
287
288         CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
289                "uuid = %s\nlast_transno = "LPU64"\n",
290                rc, mdt->mdt_lsd.lsd_uuid, mdt->mdt_lsd.lsd_last_transno);
291
292         RETURN(rc);
293 }
294
295 static int mdt_last_rcvd_read(const struct lu_env *env,
296                               struct mdt_device *mdt,
297                               struct lsd_client_data *lcd, loff_t *off)
298 {
299         struct mdt_thread_info *mti;
300         struct lsd_client_data *tmp;
301         int rc;
302
303         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
304         tmp = &mti->mti_lcd;
305         rc = mdt_record_read(env, mdt->mdt_last_rcvd,
306                              mdt_buf(env, tmp, sizeof(*tmp)), off);
307         if (rc == 0)
308                 lcd_le_to_cpu(tmp, lcd);
309
310         CDEBUG(D_INFO, "read lcd @%d rc = %d:\n"
311                        "uuid = %s\n"
312                        "last_transno = "LPU64"\n"
313                        "last_xid = "LPU64"\n"
314                        "last_result = %u\n"
315                        "last_data = %u\n"
316                        "last_close_transno = "LPU64"\n"
317                        "last_close_xid = "LPU64"\n"
318                        "last_close_result = %u\n",
319                         (int)(*off - sizeof(*tmp)),
320                         rc,
321                         lcd->lcd_uuid,
322                         lcd->lcd_last_transno,
323                         lcd->lcd_last_xid,
324                         lcd->lcd_last_result,
325                         lcd->lcd_last_data,
326                         lcd->lcd_last_close_transno,
327                         lcd->lcd_last_close_xid,
328                         lcd->lcd_last_close_result);
329         return rc;
330 }
331
332 static int mdt_last_rcvd_write(const struct lu_env *env,
333                                struct mdt_device *mdt,
334                                struct lsd_client_data *lcd,
335                                loff_t *off, struct thandle *th)
336 {
337         struct mdt_thread_info *mti;
338         struct lsd_client_data *tmp;
339         int rc;
340
341         LASSERT(th != NULL);
342         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
343         tmp = &mti->mti_lcd;
344
345         lcd_cpu_to_le(lcd, tmp);
346
347         rc = mdt_record_write(env, mdt->mdt_last_rcvd,
348                               mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
349
350         CDEBUG(D_INFO, "write lcd @%d rc = %d:\n"
351                        "uuid = %s\n"
352                        "last_transno = "LPU64"\n"
353                        "last_xid = "LPU64"\n"
354                        "last_result = %u\n"
355                        "last_data = %u\n"
356                        "last_close_transno = "LPU64"\n"
357                        "last_close_xid = "LPU64"\n"
358                        "last_close_result = %u\n",
359                         (int)(*off - sizeof(*tmp)),
360                         rc,
361                         lcd->lcd_uuid,
362                         lcd->lcd_last_transno,
363                         lcd->lcd_last_xid,
364                         lcd->lcd_last_result,
365                         lcd->lcd_last_data,
366                         lcd->lcd_last_close_transno,
367                         lcd->lcd_last_close_xid,
368                         lcd->lcd_last_close_result);
369         return rc;
370 }
371
372 static int mdt_clients_data_init(const struct lu_env *env,
373                                  struct mdt_device *mdt,
374                                  unsigned long last_size)
375 {
376         struct lr_server_data  *lsd = &mdt->mdt_lsd;
377         struct lsd_client_data *lcd = NULL;
378         struct obd_device      *obd = mdt2obd_dev(mdt);
379         loff_t off;
380         int cl_idx;
381         int rc = 0;
382         ENTRY;
383
384         /* When we do a clean MDS shutdown, we save the last_transno into
385          * the header.  If we find clients with higher last_transno values
386          * then those clients may need recovery done. */
387         LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
388         for (cl_idx = 0, off = lsd->lsd_client_start;
389              off < last_size; cl_idx++) {
390                 __u64 last_transno;
391                 struct obd_export *exp;
392
393                 if (!lcd) {
394                         OBD_ALLOC_PTR(lcd);
395                         if (!lcd)
396                                 RETURN(-ENOMEM);
397                 }
398
399                 off = lsd->lsd_client_start +
400                         cl_idx * lsd->lsd_client_size;
401
402                 rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
403                 if (rc) {
404                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
405                                LAST_RCVD, cl_idx, off, rc);
406                         rc = 0;
407                         break; /* read error shouldn't cause startup to fail */
408                 }
409
410                 if (lcd->lcd_uuid[0] == '\0') {
411                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
412                                cl_idx);
413                         continue;
414                 }
415
416                 last_transno = lcd_last_transno(lcd);
417
418                 /* These exports are cleaned up by mdt_obd_disconnect(), so
419                  * they need to be set up like real exports as
420                  * mdt_obd_connect() does.
421                  */
422                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
423                        " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
424                        last_transno, lsd->lsd_last_transno,
425                        lcd_last_xid(lcd));
426
427                 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
428                 if (IS_ERR(exp)) {
429                         if (PTR_ERR(exp) == -EALREADY) {
430                                 /* export already exists, zero out this one */
431                                 lcd->lcd_uuid[0] = '\0';
432                         } else
433                                 GOTO(err_client, rc = PTR_ERR(exp));
434                 } else {
435                         struct mdt_thread_info *mti;
436                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
437                         LASSERT(mti != NULL);
438                         mti->mti_exp = exp;
439                         exp->exp_mdt_data.med_lcd = lcd;
440                         rc = mdt_client_add(env, mdt, cl_idx);
441                         /* can't fail existing */
442                         LASSERTF(rc == 0, "rc = %d\n", rc);
443                         lcd = NULL;
444                         spin_lock(&exp->exp_lock);
445                         exp->exp_connecting = 0;
446                         exp->exp_in_recovery = 0;
447                         spin_unlock(&exp->exp_lock);
448                         obd->obd_max_recoverable_clients++;
449                         class_export_put(exp);
450                 }
451
452                 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
453                        cl_idx, last_transno);
454                 /* protect __u64 value update */
455                 spin_lock(&mdt->mdt_transno_lock);
456                 mdt->mdt_last_transno = max(last_transno,
457                                             mdt->mdt_last_transno);
458                 spin_unlock(&mdt->mdt_transno_lock);
459         }
460
461 err_client:
462         if (lcd)
463                 OBD_FREE_PTR(lcd);
464         RETURN(rc);
465 }
466
467 static int mdt_server_data_init(const struct lu_env *env,
468                                 struct mdt_device *mdt,
469                                 struct lustre_sb_info *lsi)
470 {
471         struct lr_server_data  *lsd = &mdt->mdt_lsd;
472         struct lsd_client_data *lcd = NULL;
473         struct obd_device      *obd = mdt2obd_dev(mdt);
474         struct mdt_thread_info *mti;
475         struct dt_object       *obj;
476         struct lu_attr         *la;
477         struct lustre_disk_data  *ldd;
478         unsigned long last_rcvd_size;
479         __u64 mount_count;
480         int rc;
481         ENTRY;
482
483         /* ensure padding in the struct is the correct size */
484         CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
485                 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
486         CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
487                 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
488
489         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
490         LASSERT(mti != NULL);
491         la = &mti->mti_attr.ma_attr;
492
493         obj = mdt->mdt_last_rcvd;
494         rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
495         if (rc)
496                 RETURN(rc);
497
498         last_rcvd_size = (unsigned long)la->la_size;
499
500         if (last_rcvd_size == 0) {
501                 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
502
503                 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
504                        sizeof(lsd->lsd_uuid));
505                 lsd->lsd_last_transno = 0;
506                 lsd->lsd_mount_count = 0;
507                 lsd->lsd_server_size = LR_SERVER_SIZE;
508                 lsd->lsd_client_start = LR_CLIENT_START;
509                 lsd->lsd_client_size = LR_CLIENT_SIZE;
510                 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
511                 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
512                                                        OBD_INCOMPAT_COMMON_LR;
513         } else {
514                 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
515                 rc = mdt_last_rcvd_header_read(env, mdt);
516                 if (rc) {
517                         CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
518                         GOTO(out, rc);
519                 }
520                 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
521                         LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
522                                            "the wrong disk %s. Were the /dev/ "
523                                            "assignments rearranged?\n",
524                                            obd->obd_uuid.uuid, lsd->lsd_uuid);
525                         GOTO(out, rc = -EINVAL);
526                 }
527         }
528         mount_count = lsd->lsd_mount_count;
529
530         ldd = lsi->lsi_ldd;
531
532         if (ldd->ldd_flags & LDD_F_IAM_DIR)
533                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
534
535         lsd->lsd_feature_compat = OBD_COMPAT_MDT;
536         lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
537
538         spin_lock(&mdt->mdt_transno_lock);
539         mdt->mdt_last_transno = lsd->lsd_last_transno;
540         spin_unlock(&mdt->mdt_transno_lock);
541
542         CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
543         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
544                obd->obd_name, mdt->mdt_last_transno);
545         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
546                obd->obd_name, mount_count + 1);
547         CDEBUG(D_INODE, "%s: server data size: %u\n",
548                obd->obd_name, lsd->lsd_server_size);
549         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
550                obd->obd_name, lsd->lsd_client_start);
551         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
552                obd->obd_name, lsd->lsd_client_size);
553         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
554                obd->obd_name, last_rcvd_size);
555         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
556                last_rcvd_size <= lsd->lsd_client_start ? 0 :
557                (last_rcvd_size - lsd->lsd_client_start) /
558                 lsd->lsd_client_size);
559         CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
560
561         if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
562             !lsd->lsd_client_size) {
563                 CERROR("Bad last_rcvd contents!\n");
564                 GOTO(out, rc = -EINVAL);
565         }
566
567         rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
568         if (rc)
569                 GOTO(err_client, rc);
570
571         spin_lock(&mdt->mdt_transno_lock);
572         /* obd_last_committed is used for compatibility
573          * with other lustre recovery code */
574         obd->obd_last_committed = mdt->mdt_last_transno;
575         spin_unlock(&mdt->mdt_transno_lock);
576
577         mdt->mdt_mount_count++;
578         lsd->lsd_mount_count = mdt->mdt_mount_count;
579
580         /* save it, so mount count and last_transno is current */
581         rc = mdt_server_data_update(env, mdt, (mti->mti_exp && 
582                                                mti->mti_exp->exp_need_sync));
583         if (rc)
584                 GOTO(err_client, rc);
585
586         RETURN(0);
587
588 err_client:
589         target_recovery_fini(obd);
590 out:
591         return rc;
592 }
593
594 static int mdt_server_data_update(const struct lu_env *env,
595                                   struct mdt_device *mdt,
596                                   int need_sync)
597 {
598         int rc = 0;
599         ENTRY;
600
601         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
602                mdt->mdt_mount_count, mdt->mdt_last_transno);
603
604         spin_lock(&mdt->mdt_transno_lock);
605         mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
606         spin_unlock(&mdt->mdt_transno_lock);
607
608         /*
609          * This may be called from difficult reply handler and
610          * mdt->mdt_last_rcvd may be NULL that time.
611          */
612         if (mdt->mdt_last_rcvd != NULL)
613                 rc = mdt_last_rcvd_header_write(env, mdt, need_sync);
614         RETURN(rc);
615 }
616
617 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
618 {
619         unsigned long *bitmap = mdt->mdt_client_bitmap;
620         struct mdt_thread_info *mti;
621         struct mdt_export_data *med;
622         struct lsd_client_data *lcd;
623         struct lr_server_data  *lsd = &mdt->mdt_lsd;
624         struct obd_device *obd = mdt2obd_dev(mdt);
625         struct thandle *th;
626         loff_t off;
627         int rc;
628         int cl_idx;
629         ENTRY;
630
631         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
632         LASSERT(mti != NULL);
633
634         med = &mti->mti_exp->exp_mdt_data;
635         lcd = med->med_lcd;
636
637         LASSERT(bitmap != NULL);
638         if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
639                 RETURN(0);
640
641         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
642          * there's no need for extra complication here
643          */
644         spin_lock(&mdt->mdt_client_bitmap_lock);
645         cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
646         if (cl_idx >= LR_MAX_CLIENTS ||
647             OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
648                 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
649                        cl_idx);
650                 spin_unlock(&mdt->mdt_client_bitmap_lock);
651                 RETURN(-EOVERFLOW);
652         }
653         set_bit(cl_idx, bitmap);
654         spin_unlock(&mdt->mdt_client_bitmap_lock);
655
656         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
657                cl_idx, med->med_lcd->lcd_uuid);
658
659         med->med_lr_idx = cl_idx;
660         med->med_lr_off = lsd->lsd_client_start +
661                           (cl_idx * lsd->lsd_client_size);
662         init_mutex(&med->med_lcd_lock);
663
664         LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
665
666         /* Write new client data. */
667         off = med->med_lr_off;
668         mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
669
670         th = mdt_trans_start(env, mdt);
671         if (IS_ERR(th))
672                 RETURN(PTR_ERR(th));
673
674         /* 
675          * Until this operations will be committed the sync is needed
676          * for this export. This should be done _after_ starting the
677          * transaction so that many connecting clients will not bring
678          * server down with lots of sync writes. 
679          */
680         mdt_trans_add_cb(th, mdt_client_cb, mti->mti_exp);
681         spin_lock(&mti->mti_exp->exp_lock);
682         mti->mti_exp->exp_need_sync = 1;
683         spin_unlock(&mti->mti_exp->exp_lock);
684
685         rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
686         CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
687                cl_idx, med->med_lr_off, sizeof(*lcd));
688         mdt_trans_stop(env, mdt, th);
689
690         RETURN(rc);
691 }
692
693 /* Add client data to the MDS.  We use a bitmap to locate a free space
694  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
695  * Otherwise, we just have to read the data from the last_rcvd file and
696  * we know its offset.
697  *
698  * It should not be possible to fail adding an existing client - otherwise
699  * mdt_init_server_data() callsite needs to be fixed.
700  */
701 int mdt_client_add(const struct lu_env *env,
702                    struct mdt_device *mdt, int cl_idx)
703 {
704         struct mdt_thread_info *mti;
705         struct mdt_export_data *med;
706         unsigned long *bitmap = mdt->mdt_client_bitmap;
707         struct obd_device *obd = mdt2obd_dev(mdt);
708         struct lr_server_data *lsd = &mdt->mdt_lsd;
709         int rc = 0;
710         ENTRY;
711
712         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
713         LASSERT(mti != NULL);
714
715         med = &mti->mti_exp->exp_mdt_data;
716
717         LASSERT(bitmap != NULL);
718         LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
719
720         if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
721                 RETURN(0);
722
723         spin_lock(&mdt->mdt_client_bitmap_lock);
724         if (test_and_set_bit(cl_idx, bitmap)) {
725                 CERROR("MDS client %d: bit already set in bitmap!!\n",
726                        cl_idx);
727                 LBUG();
728         }
729         spin_unlock(&mdt->mdt_client_bitmap_lock);
730
731         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
732                cl_idx, med->med_lcd->lcd_uuid);
733
734         med->med_lr_idx = cl_idx;
735         med->med_lr_off = lsd->lsd_client_start +
736                           (cl_idx * lsd->lsd_client_size);
737         init_mutex(&med->med_lcd_lock);
738
739         LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
740
741         RETURN(rc);
742 }
743
744 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
745 {
746         struct mdt_thread_info *mti;
747         struct mdt_export_data *med;
748         struct lsd_client_data *lcd;
749         struct obd_device      *obd = mdt2obd_dev(mdt);
750         struct obd_export      *exp;
751         struct thandle         *th;
752         int                     need_sync;
753         loff_t                  off;
754         int                     rc = 0;
755         ENTRY;
756
757         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
758         LASSERT(mti != NULL);
759
760         exp = mti->mti_exp;
761         med = &exp->exp_mdt_data;
762         lcd = med->med_lcd;
763         if (!lcd)
764                 RETURN(0);
765
766         /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
767         if (!strcmp(lcd->lcd_uuid, obd->obd_uuid.uuid))
768                 GOTO(free, 0);
769
770         CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
771                med->med_lr_idx, med->med_lr_off);
772
773         off = med->med_lr_off;
774
775         /*
776          * Don't clear med_lr_idx here as it is likely also unset.  At worst we
777          * leak a client slot that will be cleaned on the next recovery.
778          */
779         if (off <= 0) {
780                 CERROR("client idx %d has offset %lld\n",
781                         med->med_lr_idx, off);
782                 GOTO(free, rc = -EINVAL);
783         }
784
785         /*
786          * Clear the bit _after_ zeroing out the client so we don't race with
787          * mdt_client_add and zero out new clients.
788          */
789         if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
790                 CERROR("MDT client %u: bit already clear in bitmap!!\n",
791                        med->med_lr_idx);
792                 LBUG();
793         }
794
795         /* Don't force sync on disconnect if aborting recovery,
796          * or it does num_clients * num_osts.  b=17194 */
797         need_sync = (!exp->exp_libclient || exp->exp_need_sync) &&
798                      !(exp->exp_flags & OBD_OPT_ABORT_RECOV);
799
800         /*
801          * This may be called from difficult reply handler path and
802          * mdt->mdt_last_rcvd may be NULL that time.
803          */
804         if (mdt->mdt_last_rcvd != NULL) {
805                 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
806
807                 spin_lock(&exp->exp_lock);
808                 exp->exp_need_sync = need_sync;
809                 spin_unlock(&exp->exp_lock);
810
811                 th = mdt_trans_start(env, mdt);
812                 if (IS_ERR(th))
813                         GOTO(free, rc = PTR_ERR(th));
814
815                 if (need_sync) {
816                         /* 
817                          * Until this operations will be committed the sync
818                          * is needed for this export. 
819                          */
820                         mdt_trans_add_cb(th, mdt_client_cb, exp);
821                 }
822
823                 mutex_down(&med->med_lcd_lock);
824                 memset(lcd, 0, sizeof *lcd);
825
826                 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
827                 mutex_up(&med->med_lcd_lock);
828                 mdt_trans_stop(env, mdt, th);
829         }
830
831         CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
832                "%s %ssync rc %d\n",  med->med_lr_idx, LAST_RCVD, 
833                need_sync ? "" : "a", rc);
834
835         spin_lock(&mdt->mdt_client_bitmap_lock);
836         clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
837         spin_unlock(&mdt->mdt_client_bitmap_lock);
838
839         /* 
840          * Make sure the server's last_transno is up to date. Do this
841          * after the client is freed so we know all the client's
842          * transactions have been committed. 
843          */
844         mdt_server_data_update(env, mdt, need_sync);
845
846         EXIT;
847 free:
848         OBD_FREE_PTR(lcd);
849         med->med_lcd = NULL;
850         return 0;
851 }
852
853 /*
854  * last_rcvd & last_committed update callbacks
855  */
856 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
857                                 struct thandle *th)
858 {
859         struct mdt_device *mdt = mti->mti_mdt;
860         struct ptlrpc_request *req = mdt_info_req(mti);
861         struct mdt_export_data *med;
862         struct lsd_client_data *lcd;
863         loff_t off;
864         int err;
865         __s32 rc = th->th_result;
866         __u64 *transno_p;
867
868         ENTRY;
869         LASSERT(req);
870         LASSERT(req->rq_export);
871         LASSERT(mdt);
872         med = &req->rq_export->exp_mdt_data;
873         LASSERT(med);
874         lcd = med->med_lcd;
875         /* if the export has already been failed, we have no last_rcvd slot */
876         if (req->rq_export->exp_failed) {
877                 CWARN("commit transaction for disconnected client %s: rc %d\n",
878                       req->rq_export->exp_client_uuid.uuid, rc);
879                 if (rc == 0)
880                         rc = -ENOTCONN;
881                 RETURN(rc);
882         }
883
884         off = med->med_lr_off;
885         mutex_down(&med->med_lcd_lock);
886         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
887             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
888                 transno_p = &lcd->lcd_last_close_transno;
889                 lcd->lcd_last_close_xid = req->rq_xid;
890                 lcd->lcd_last_close_result = rc;
891         } else {
892                 transno_p = &lcd->lcd_last_transno;
893                 lcd->lcd_last_xid = req->rq_xid;
894                 lcd->lcd_last_result = rc;
895                 /*XXX: save intent_disposition in mdt_thread_info?
896                  * also there is bug - intent_dispostion is __u64,
897                  * see struct ldlm_reply->lock_policy_res1; */
898                 lcd->lcd_last_data = mti->mti_opdata;
899         }
900
901         /*
902          * When we store zero transno in lcd we can lost last transno value
903          * because lcd contains 0, but lsd is not yet written
904          * The server data should be updated also if the latest
905          * transno is rewritten by zero. See the bug 11125 for details.
906          */
907         if (mti->mti_transno == 0 &&
908             *transno_p == mdt->mdt_last_transno)
909                 mdt_server_data_update(mti->mti_env, mdt, 
910                                       (mti->mti_exp && 
911                                        mti->mti_exp->exp_need_sync));
912
913         *transno_p = mti->mti_transno;
914
915         if (off <= 0) {
916                 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
917                 err = -EINVAL;
918         } else {
919                 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
920         }
921         mutex_up(&med->med_lcd_lock);
922         RETURN(err);
923 }
924
925 extern struct lu_context_key mdt_thread_key;
926
927 /* add credits for last_rcvd update */
928 static int mdt_txn_start_cb(const struct lu_env *env,
929                             struct txn_param *param, void *cookie)
930 {
931         struct mdt_device *mdt = cookie;
932
933         param->tp_credits += mdt_trans_credit_get(env, mdt,
934                                                   MDT_TXN_LAST_RCVD_WRITE_OP);
935         return 0;
936 }
937
938 /* Update last_rcvd records with latests transaction data */
939 static int mdt_txn_stop_cb(const struct lu_env *env,
940                            struct thandle *txn, void *cookie)
941 {
942         struct mdt_device *mdt = cookie;
943         struct mdt_txn_info *txi;
944         struct mdt_thread_info *mti;
945         struct ptlrpc_request *req;
946
947         /* transno in two contexts - for commit_cb and for thread */
948         txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
949         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
950         req = mdt_info_req(mti);
951
952         if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
953                 txi->txi_transno = 0;
954                 mti->mti_no_need_trans = 0;
955                 return 0;
956         }
957
958         if (mti->mti_has_trans) {
959                 /* XXX: currently there are allowed cases, but the wrong cases
960                  * are also possible, so better check is needed here */
961                 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
962                        mti->mti_transno);
963                 return 0;
964         }
965
966         mti->mti_has_trans = 1;
967         spin_lock(&mdt->mdt_transno_lock);
968         if (txn->th_result != 0) {
969                 if (mti->mti_transno != 0) {
970                         CERROR("Replay transno "LPU64" failed: rc %i\n",
971                                mti->mti_transno, txn->th_result);
972                         mti->mti_transno = 0;
973                 }
974         } else if (mti->mti_transno == 0) {
975                 mti->mti_transno = ++ mdt->mdt_last_transno;
976         } else {
977                 /* should be replay */
978                 if (mti->mti_transno > mdt->mdt_last_transno)
979                         mdt->mdt_last_transno = mti->mti_transno;
980         }
981
982         /* sometimes the reply message has not been successfully packed */
983         LASSERT(req != NULL && req->rq_repmsg != NULL);
984
985         /* filling reply data */
986         CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
987                mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
988
989         req->rq_transno = mti->mti_transno;
990         lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
991         lustre_msg_set_last_xid(req->rq_repmsg,
992                          lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
993         /* save transno for the commit callback */
994         txi->txi_transno = mti->mti_transno;
995         spin_unlock(&mdt->mdt_transno_lock);
996
997         return mdt_last_rcvd_update(mti, txn);
998 }
999
1000 /* commit callback, need to update last_committed value */
1001 static int mdt_txn_commit_cb(const struct lu_env *env,
1002                              struct thandle *txn, void *cookie)
1003 {
1004         struct mdt_device *mdt = cookie;
1005         struct obd_device *obd = mdt2obd_dev(mdt);
1006         struct mdt_txn_info *txi;
1007         int i;
1008
1009         txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
1010
1011         /* copy of obd_transno_commit_cb() but with locking */
1012         spin_lock(&mdt->mdt_transno_lock);
1013         if (txi->txi_transno > obd->obd_last_committed) {
1014                 obd->obd_last_committed = txi->txi_transno;
1015                 spin_unlock(&mdt->mdt_transno_lock);
1016                 ptlrpc_commit_replies(obd);
1017         } else
1018                 spin_unlock(&mdt->mdt_transno_lock);
1019
1020         if (txi->txi_transno)
1021                 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
1022                        obd->obd_name, txi->txi_transno);
1023
1024         /* iterate through all additional callbacks */
1025         for (i = 0; i < txi->txi_cb_count; i++) {
1026                 txi->txi_cb[i].mdt_cb_func(mdt, txi->txi_transno,
1027                                            txi->txi_cb[i].mdt_cb_data, 0);
1028         }
1029         return 0;
1030 }
1031
1032 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
1033                  struct obd_device *obd,
1034                  struct lustre_sb_info *lsi)
1035 {
1036         struct lu_fid fid;
1037         struct dt_object *o;
1038         int rc = 0;
1039         ENTRY;
1040
1041         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
1042                 RETURN(-ENOENT);
1043
1044         /* prepare transactions callbacks */
1045         mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
1046         mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
1047         mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
1048         mdt->mdt_txn_cb.dtc_cookie = mdt;
1049         CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
1050
1051         dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1052
1053         o = dt_store_open(env, mdt->mdt_bottom, "", LAST_RCVD, &fid);
1054         if (!IS_ERR(o)) {
1055                 mdt->mdt_last_rcvd = o;
1056                 rc = mdt_server_data_init(env, mdt, lsi);
1057                 if (rc)
1058                         GOTO(put_last_rcvd, rc);
1059         } else {
1060                 rc = PTR_ERR(o);
1061                 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
1062                 RETURN(rc);
1063         }
1064
1065         o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
1066         if (!IS_ERR(o)) {
1067                 mdt->mdt_ck_obj = o;
1068                 rc = mdt_capa_keys_init(env, mdt);
1069                 if (rc)
1070                         GOTO(put_ck_object, rc);
1071         } else {
1072                 rc = PTR_ERR(o);
1073                 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
1074                 GOTO(put_last_rcvd, rc);
1075         }
1076         RETURN(0);
1077
1078 put_ck_object:
1079         lu_object_put(env, &o->do_lu);
1080         mdt->mdt_ck_obj = NULL;
1081 put_last_rcvd:
1082         lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1083         mdt->mdt_last_rcvd = NULL;
1084         return rc;
1085 }
1086
1087 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
1088 {
1089         ENTRY;
1090
1091         /* Remove transaction callback */
1092         dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1093         if (mdt->mdt_last_rcvd)
1094                 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1095         mdt->mdt_last_rcvd = NULL;
1096         if (mdt->mdt_ck_obj)
1097                 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
1098         mdt->mdt_ck_obj = NULL;
1099         EXIT;
1100 }
1101
1102 /* reconstruction code */
1103 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1104 {
1105         struct obd_export         *exp = req->rq_export;
1106         struct list_head          *tmp;
1107         struct ptlrpc_reply_state *oldrep;
1108         struct ptlrpc_service     *svc;
1109         int                        i;
1110
1111         /* CAVEAT EMPTOR: spinlock order */
1112         spin_lock(&exp->exp_lock);
1113         list_for_each (tmp, &exp->exp_outstanding_replies) {
1114                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1115
1116                 if (oldrep->rs_xid != req->rq_xid)
1117                         continue;
1118
1119                 if (lustre_msg_get_opc(oldrep->rs_msg) !=
1120                     lustre_msg_get_opc(req->rq_reqmsg))
1121                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
1122                                 "new %d old %d\n", req->rq_xid,
1123                                 lustre_msg_get_opc(req->rq_reqmsg),
1124                                 lustre_msg_get_opc(oldrep->rs_msg));
1125
1126                 svc = oldrep->rs_service;
1127                 spin_lock (&svc->srv_lock);
1128
1129                 list_del_init (&oldrep->rs_exp_list);
1130
1131                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1132                       " o%d NID %s\n",
1133                       oldrep->rs_nlocks, oldrep,
1134                       oldrep->rs_xid, oldrep->rs_transno,
1135                       lustre_msg_get_opc(oldrep->rs_msg),
1136                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
1137
1138                 for (i = 0; i < oldrep->rs_nlocks; i++)
1139                         ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1140                                          oldrep->rs_modes[i], 0);
1141                 oldrep->rs_nlocks = 0;
1142
1143                 DEBUG_REQ(D_HA, req, "stole locks for");
1144                 spin_lock(&oldrep->rs_lock);
1145                 ptlrpc_schedule_difficult_reply (oldrep);
1146                 spin_unlock(&oldrep->rs_lock);
1147
1148                 spin_unlock (&svc->srv_lock);
1149                 break;
1150         }
1151         spin_unlock(&exp->exp_lock);
1152 }
1153
1154 void mdt_req_from_lcd(struct ptlrpc_request *req,
1155                       struct lsd_client_data *lcd)
1156 {
1157         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1158                   lcd->lcd_last_transno, lcd->lcd_last_result);
1159
1160         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1161             lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1162                 req->rq_transno = lcd->lcd_last_close_transno;
1163                 req->rq_status = lcd->lcd_last_close_result;
1164                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1165                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1166         } else {
1167                 req->rq_transno = lcd->lcd_last_transno;
1168                 req->rq_status = lcd->lcd_last_result;
1169                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1170                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1171         }
1172         mdt_steal_ack_locks(req);
1173 }
1174
1175 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1176                              struct mdt_lock_handle *lhc)
1177 {
1178         struct ptlrpc_request *req = mdt_info_req(mti);
1179         struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1180
1181         return mdt_req_from_lcd(req, med->med_lcd);
1182 }
1183
1184 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1185                                    struct mdt_lock_handle *lhc)
1186 {
1187         struct ptlrpc_request  *req = mdt_info_req(mti);
1188         struct obd_export *exp = req->rq_export;
1189         struct mdt_export_data *med = &exp->exp_mdt_data;
1190         struct mdt_device *mdt = mti->mti_mdt;
1191         struct mdt_object *child;
1192         struct mdt_body *body;
1193         int rc;
1194
1195         mdt_req_from_lcd(req, med->med_lcd);
1196         if (req->rq_status)
1197                 return;
1198
1199         /* if no error, so child was created with requested fid */
1200         child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1201         if (IS_ERR(child)) {
1202                 rc = PTR_ERR(child);
1203                 LCONSOLE_WARN("Child "DFID" lookup error %d."
1204                               " Evicting client %s with export %s.\n",
1205                               PFID(mdt_object_fid(child)), rc,
1206                               obd_uuid2str(&exp->exp_client_uuid),
1207                               obd_export_nid2str(exp));
1208                 mdt_export_evict(exp);
1209                 EXIT;
1210                 return;
1211         }
1212
1213         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1214         mti->mti_attr.ma_need = MA_INODE;
1215         mti->mti_attr.ma_valid = 0;
1216         rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1217         if (rc == -EREMOTE) {
1218                 /* object was created on remote server */
1219                 req->rq_status = rc;
1220                 body->valid |= OBD_MD_MDS;
1221         }
1222         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1223                            mdt_object_fid(child));
1224         mdt_object_put(mti->mti_env, child);
1225 }
1226
1227 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1228                                     struct mdt_lock_handle *lhc)
1229 {
1230         struct ptlrpc_request  *req = mdt_info_req(mti);
1231         struct obd_export *exp = req->rq_export;
1232         struct mdt_export_data *med = &exp->exp_mdt_data;
1233         struct mdt_device *mdt = mti->mti_mdt;
1234         struct mdt_object *obj;
1235         struct mdt_body *body;
1236
1237         mdt_req_from_lcd(req, med->med_lcd);
1238         if (req->rq_status)
1239                 return;
1240
1241         body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1242         obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1243         if (IS_ERR(obj)) {
1244                 int rc = PTR_ERR(obj);
1245                 LCONSOLE_WARN(""DFID" lookup error %d."
1246                               " Evicting client %s with export %s.\n",
1247                               PFID(mdt_object_fid(obj)), rc,
1248                               obd_uuid2str(&exp->exp_client_uuid),
1249                               obd_export_nid2str(exp));
1250                 mdt_export_evict(exp);
1251                 EXIT;
1252                 return;
1253         }
1254         mti->mti_attr.ma_need = MA_INODE;
1255         mti->mti_attr.ma_valid = 0;
1256         mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1257         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1258                            mdt_object_fid(obj));
1259         if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1260                 struct mdt_file_data *mfd;
1261                 struct mdt_body *repbody;
1262
1263                 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1264                 repbody->ioepoch = obj->mot_ioepoch;
1265                 spin_lock(&med->med_open_lock);
1266                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1267                         if (mfd->mfd_xid == req->rq_xid)
1268                                 break;
1269                 }
1270                 LASSERT(&mfd->mfd_list != &med->med_open_head);
1271                 spin_unlock(&med->med_open_lock);
1272                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1273         }
1274
1275         mdt_object_put(mti->mti_env, obj);
1276 }
1277
1278 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1279                                   struct mdt_lock_handle *lhc);
1280
1281 static mdt_reconstructor reconstructors[REINT_MAX] = {
1282         [REINT_SETATTR]  = mdt_reconstruct_setattr,
1283         [REINT_CREATE]   = mdt_reconstruct_create,
1284         [REINT_LINK]     = mdt_reconstruct_generic,
1285         [REINT_UNLINK]   = mdt_reconstruct_generic,
1286         [REINT_RENAME]   = mdt_reconstruct_generic,
1287         [REINT_OPEN]     = mdt_reconstruct_open,
1288         [REINT_SETXATTR] = mdt_reconstruct_generic
1289 };
1290
1291 void mdt_reconstruct(struct mdt_thread_info *mti,
1292                      struct mdt_lock_handle *lhc)
1293 {
1294         ENTRY;
1295         reconstructors[mti->mti_rr.rr_opcode](mti, lhc);
1296         EXIT;
1297 }