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