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