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