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