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