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