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