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