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