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