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