Whamcloud - gitweb
LU-11131 target: keep reply data bit set on failover
[fs/lustre-release.git] / lustre / target / tgt_lastrcvd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Lustre Unified Target
33  * These are common function to work with last_received file
34  *
35  * Author: Mikhail Pershin <mike.pershin@intel.com>
36  */
37 #include <obd.h>
38 #include <obd_class.h>
39 #include <lustre_fid.h>
40
41 #include "tgt_internal.h"
42
43 /** version recovery epoch */
44 #define LR_EPOCH_BITS   32
45
46 /* Allocate a bitmap for a chunk of reply data slots */
47 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
48 {
49         unsigned long *bm;
50
51         OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
52                         sizeof(long));
53         if (bm == NULL)
54                 return -ENOMEM;
55
56         spin_lock(&lut->lut_client_bitmap_lock);
57
58         if (lut->lut_reply_bitmap[chunk] != NULL) {
59                 /* someone else already allocated the bitmap for this chunk */
60                 spin_unlock(&lut->lut_client_bitmap_lock);
61                 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
62                          sizeof(long));
63                 return 0;
64         }
65
66         lut->lut_reply_bitmap[chunk] = bm;
67
68         spin_unlock(&lut->lut_client_bitmap_lock);
69
70         return 0;
71 }
72
73 /* Look for an available reply data slot in the bitmap
74  * of the target @lut
75  * Allocate bitmap chunk when first used
76  * XXX algo could be improved if this routine limits performance
77  */
78 static int tgt_find_free_reply_slot(struct lu_target *lut)
79 {
80         unsigned long *bmp;
81         int chunk = 0;
82         int rc;
83         int b;
84
85         for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
86                 /* allocate the bitmap chunk if necessary */
87                 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
88                         rc = tgt_bitmap_chunk_alloc(lut, chunk);
89                         if (rc != 0)
90                                 return rc;
91                 }
92                 bmp = lut->lut_reply_bitmap[chunk];
93
94                 /* look for an available slot in this chunk */
95                 do {
96                         b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
97                         if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
98                                 break;
99
100                         /* found one */
101                         if (test_and_set_bit(b, bmp) == 0)
102                                 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
103                 } while (true);
104         }
105
106         return -ENOSPC;
107 }
108
109 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
110  * of the target @lut
111  * Allocate the bitmap chunk if necessary
112  */
113 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
114 {
115         int chunk;
116         int b;
117         int rc;
118
119         chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
120         b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
121
122         LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
123         LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
124
125         /* allocate the bitmap chunk if necessary */
126         if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
127                 rc = tgt_bitmap_chunk_alloc(lut, chunk);
128                 if (rc != 0)
129                         return rc;
130         }
131
132         /* mark the slot 'used' in this chunk */
133         if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
134                 CERROR("%s: slot %d already set in bitmap\n",
135                        tgt_name(lut), idx);
136                 return -EALREADY;
137         }
138
139         return 0;
140 }
141
142
143 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
144  * of the target @lut
145  */
146 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
147 {
148         int chunk;
149         int b;
150
151         if (lut->lut_obd->obd_stopping)
152                 /*
153                  * in case of failover keep the bit set in order to
154                  * avoid overwriting slots in reply_data which might
155                  * be required by resent rpcs
156                  */
157                 return 0;
158         chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
159         b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
160
161         LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
162         LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
163
164         if (lut->lut_reply_bitmap[chunk] == NULL) {
165                 CERROR("%s: slot %d not allocated\n",
166                        tgt_name(lut), idx);
167                 return -ENOENT;
168         }
169
170         if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
171                 CERROR("%s: slot %d already clear in bitmap\n",
172                        tgt_name(lut), idx);
173                 return -EALREADY;
174         }
175
176         return 0;
177 }
178
179
180 /* Read header of reply_data file of target @tgt into structure @lrh */
181 static int tgt_reply_header_read(const struct lu_env *env,
182                                  struct lu_target *tgt,
183                                  struct lsd_reply_header *lrh)
184 {
185         int                      rc;
186         struct lsd_reply_header  buf;
187         struct tgt_thread_info  *tti = tgt_th_info(env);
188
189         tti->tti_off = 0;
190         tti->tti_buf.lb_buf = &buf;
191         tti->tti_buf.lb_len = sizeof(buf);
192
193         rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
194                             &tti->tti_off);
195         if (rc != 0)
196                 return rc;
197
198         lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
199         lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
200         lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
201
202         CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
203                "header_size=%d reply_size=%d\n",
204                 tgt->lut_obd->obd_name, REPLY_DATA,
205                 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
206
207         return 0;
208 }
209
210 /* Write header into replay_data file of target @tgt from structure @lrh */
211 static int tgt_reply_header_write(const struct lu_env *env,
212                                   struct lu_target *tgt,
213                                   struct lsd_reply_header *lrh)
214 {
215         int                      rc;
216         struct lsd_reply_header  buf;
217         struct tgt_thread_info  *tti = tgt_th_info(env);
218         struct thandle          *th;
219         struct dt_object        *dto;
220
221         CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
222                "header_size=%d reply_size=%d\n",
223                 tgt->lut_obd->obd_name, REPLY_DATA,
224                 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
225
226         if (tgt->lut_bottom->dd_rdonly)
227                 RETURN(0);
228
229         buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
230         buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
231         buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
232
233         th = dt_trans_create(env, tgt->lut_bottom);
234         if (IS_ERR(th))
235                 return PTR_ERR(th);
236         th->th_sync = 1;
237
238         tti->tti_off = 0;
239         tti->tti_buf.lb_buf = &buf;
240         tti->tti_buf.lb_len = sizeof(buf);
241
242         rc = dt_declare_record_write(env, tgt->lut_reply_data,
243                                      &tti->tti_buf, tti->tti_off, th);
244         if (rc)
245                 GOTO(out, rc);
246
247         rc = dt_trans_start(env, tgt->lut_bottom, th);
248         if (rc)
249                 GOTO(out, rc);
250
251         dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
252         rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
253 out:
254         dt_trans_stop(env, tgt->lut_bottom, th);
255         return rc;
256 }
257
258 /* Write the reply data @lrd into reply_data file of target @tgt
259  * at offset @off
260  */
261 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
262                                 struct lsd_reply_data *lrd, loff_t off,
263                                 struct thandle *th)
264 {
265         struct tgt_thread_info  *tti = tgt_th_info(env);
266         struct dt_object        *dto;
267         struct lsd_reply_data   *buf = &tti->tti_lrd;
268
269         lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
270
271         buf->lrd_transno         = cpu_to_le64(lrd->lrd_transno);
272         buf->lrd_xid             = cpu_to_le64(lrd->lrd_xid);
273         buf->lrd_data            = cpu_to_le64(lrd->lrd_data);
274         buf->lrd_result          = cpu_to_le32(lrd->lrd_result);
275         buf->lrd_client_gen      = cpu_to_le32(lrd->lrd_client_gen);
276
277         lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
278
279         tti->tti_off = off;
280         tti->tti_buf.lb_buf = buf;
281         tti->tti_buf.lb_len = sizeof(*buf);
282
283         dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
284         return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
285 }
286
287 /* Read the reply data from reply_data file of target @tgt at offset @off
288  * into structure @lrd
289  */
290 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
291                                struct lsd_reply_data *lrd, loff_t off)
292 {
293         int                      rc;
294         struct tgt_thread_info  *tti = tgt_th_info(env);
295         struct lsd_reply_data   *buf = &tti->tti_lrd;
296
297         tti->tti_off = off;
298         tti->tti_buf.lb_buf = buf;
299         tti->tti_buf.lb_len = sizeof(*buf);
300
301         rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
302                             &tti->tti_off);
303         if (rc != 0)
304                 return rc;
305
306         lrd->lrd_transno         = le64_to_cpu(buf->lrd_transno);
307         lrd->lrd_xid             = le64_to_cpu(buf->lrd_xid);
308         lrd->lrd_data            = le64_to_cpu(buf->lrd_data);
309         lrd->lrd_result          = le32_to_cpu(buf->lrd_result);
310         lrd->lrd_client_gen      = le32_to_cpu(buf->lrd_client_gen);
311
312         return 0;
313 }
314
315
316 /* Free the in-memory reply data structure @trd and release
317  * the corresponding slot in the reply_data file of target @lut
318  * Called with ted_lcd_lock held
319  */
320 static void tgt_free_reply_data(struct lu_target *lut,
321                                 struct tg_export_data *ted,
322                                 struct tg_reply_data *trd)
323 {
324         CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
325                "client gen %u, slot idx %d\n",
326                lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
327                trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
328                trd->trd_index);
329
330         LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
331
332         list_del(&trd->trd_list);
333         ted->ted_reply_cnt--;
334         if (lut != NULL)
335                 tgt_clear_reply_slot(lut, trd->trd_index);
336         OBD_FREE_PTR(trd);
337 }
338
339 /* Release the reply data @trd from target @lut
340  * The reply data with the highest transno for this export
341  * is retained to ensure correctness of target recovery
342  * Called with ted_lcd_lock held
343  */
344 static void tgt_release_reply_data(struct lu_target *lut,
345                                    struct tg_export_data *ted,
346                                    struct tg_reply_data *trd)
347 {
348         CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
349                "client gen %u, slot idx %d\n",
350                lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
351                trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
352                trd->trd_index);
353
354         LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
355
356         /* Do not free the reply data corresponding to the
357          * highest transno of this export.
358          * This ensures on-disk reply data is kept and
359          * last committed transno can be restored from disk in case
360          * of target recovery
361          */
362         if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
363                 /* free previous retained reply */
364                 if (ted->ted_reply_last != NULL)
365                         tgt_free_reply_data(lut, ted, ted->ted_reply_last);
366                 /* retain the reply */
367                 list_del_init(&trd->trd_list);
368                 ted->ted_reply_last = trd;
369         } else {
370                 tgt_free_reply_data(lut, ted, trd);
371         }
372 }
373
374 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
375 {
376         tti->tti_buf.lb_buf = &tti->tti_lsd;
377         tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
378         return &tti->tti_buf;
379 }
380
381 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
382 {
383         tti->tti_buf.lb_buf = &tti->tti_lcd;
384         tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
385         return &tti->tti_buf;
386 }
387
388 /**
389  * Allocate in-memory data for client slot related to export.
390  */
391 int tgt_client_alloc(struct obd_export *exp)
392 {
393         ENTRY;
394         LASSERT(exp != exp->exp_obd->obd_self_export);
395
396         spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
397         INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
398
399         OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
400         if (exp->exp_target_data.ted_lcd == NULL)
401                 RETURN(-ENOMEM);
402         /* Mark that slot is not yet valid, 0 doesn't work here */
403         exp->exp_target_data.ted_lr_idx = -1;
404         INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
405         mutex_init(&exp->exp_target_data.ted_lcd_lock);
406         RETURN(0);
407 }
408 EXPORT_SYMBOL(tgt_client_alloc);
409
410 /**
411  * Free in-memory data for client slot related to export.
412  */
413 void tgt_client_free(struct obd_export *exp)
414 {
415         struct tg_export_data   *ted = &exp->exp_target_data;
416         struct lu_target        *lut = class_exp2tgt(exp);
417         struct tg_reply_data    *trd, *tmp;
418
419         LASSERT(exp != exp->exp_obd->obd_self_export);
420
421         /* free reply data */
422         mutex_lock(&ted->ted_lcd_lock);
423         list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
424                 tgt_release_reply_data(lut, ted, trd);
425         }
426         if (ted->ted_reply_last != NULL) {
427                 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
428                 ted->ted_reply_last = NULL;
429         }
430         mutex_unlock(&ted->ted_lcd_lock);
431
432         if (!hlist_unhashed(&exp->exp_gen_hash))
433                 cfs_hash_del(exp->exp_obd->obd_gen_hash,
434                              &ted->ted_lcd->lcd_generation,
435                              &exp->exp_gen_hash);
436
437         OBD_FREE_PTR(ted->ted_lcd);
438         ted->ted_lcd = NULL;
439
440         /* Target may have been freed (see LU-7430)
441          * Slot may be not yet assigned */
442         if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
443             ted->ted_lr_idx < 0)
444                 return;
445
446         /* Clear bit when lcd is freed */
447         LASSERT(lut && lut->lut_client_bitmap);
448         if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
449                 CERROR("%s: client %u bit already clear in bitmap\n",
450                        exp->exp_obd->obd_name, ted->ted_lr_idx);
451                 LBUG();
452         }
453
454         if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
455                 atomic_dec(&lut->lut_num_clients);
456 }
457 EXPORT_SYMBOL(tgt_client_free);
458
459 static inline void tgt_check_lcd(const char *obd_name, int index,
460                                  struct lsd_client_data *lcd)
461 {
462         size_t uuid_size = sizeof(lcd->lcd_uuid);
463
464         if (strnlen((char*)lcd->lcd_uuid, uuid_size) == uuid_size) {
465                 lcd->lcd_uuid[uuid_size - 1] = '\0';
466
467                 LCONSOLE_ERROR("the client UUID (%s) on %s for exports stored in last_rcvd(index = %d) is bad!\n",
468                                lcd->lcd_uuid, obd_name, index);
469         }
470 }
471
472 static int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
473                                 struct lsd_client_data *lcd,
474                                 loff_t *off, int index)
475 {
476         struct tgt_thread_info  *tti = tgt_th_info(env);
477         int                      rc;
478
479         tti_buf_lcd(tti);
480         rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
481         if (rc == 0) {
482                 tgt_check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
483                 lcd_le_to_cpu(&tti->tti_lcd, lcd);
484                 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
485                 lcd->lcd_last_close_result =
486                         ptlrpc_status_ntoh(lcd->lcd_last_close_result);
487         }
488
489         CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = %llu"
490                ", last_xid = %llu, last_result = %u, last_data = %u, "
491                "last_close_transno = %llu, last_close_xid = %llu, "
492                "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
493                *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
494                lcd->lcd_last_result, lcd->lcd_last_data,
495                lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
496                lcd->lcd_last_close_result, rc);
497         return rc;
498 }
499
500 static int tgt_client_data_write(const struct lu_env *env,
501                                  struct lu_target *tgt,
502                                  struct lsd_client_data *lcd,
503                                  loff_t *off, struct thandle *th)
504 {
505         struct tgt_thread_info *tti = tgt_th_info(env);
506         struct dt_object        *dto;
507
508         lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
509         lcd->lcd_last_close_result =
510                 ptlrpc_status_hton(lcd->lcd_last_close_result);
511         lcd_cpu_to_le(lcd, &tti->tti_lcd);
512         tti_buf_lcd(tti);
513
514         dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
515         return dt_record_write(env, dto, &tti->tti_buf, off, th);
516 }
517
518 struct tgt_new_client_callback {
519         struct dt_txn_commit_cb  lncc_cb;
520         struct obd_export       *lncc_exp;
521 };
522
523 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
524                               struct dt_txn_commit_cb *cb, int err)
525 {
526         struct tgt_new_client_callback *ccb;
527
528         ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
529
530         LASSERT(ccb->lncc_exp->exp_obd);
531
532         CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
533                ccb->lncc_exp->exp_obd->obd_name,
534                ccb->lncc_exp->exp_client_uuid.uuid);
535
536         spin_lock(&ccb->lncc_exp->exp_lock);
537
538         ccb->lncc_exp->exp_need_sync = 0;
539
540         spin_unlock(&ccb->lncc_exp->exp_lock);
541         class_export_cb_put(ccb->lncc_exp);
542
543         OBD_FREE_PTR(ccb);
544 }
545
546 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
547 {
548         struct tgt_new_client_callback  *ccb;
549         struct dt_txn_commit_cb         *dcb;
550         int                              rc;
551
552         OBD_ALLOC_PTR(ccb);
553         if (ccb == NULL)
554                 return -ENOMEM;
555
556         ccb->lncc_exp = class_export_cb_get(exp);
557
558         dcb = &ccb->lncc_cb;
559         dcb->dcb_func = tgt_cb_new_client;
560         INIT_LIST_HEAD(&dcb->dcb_linkage);
561         strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
562
563         rc = dt_trans_cb_add(th, dcb);
564         if (rc) {
565                 class_export_cb_put(exp);
566                 OBD_FREE_PTR(ccb);
567         }
568         return rc;
569 }
570
571 /**
572  * Update client data in last_rcvd
573  */
574 static int tgt_client_data_update(const struct lu_env *env,
575                                   struct obd_export *exp)
576 {
577         struct tg_export_data   *ted = &exp->exp_target_data;
578         struct lu_target        *tgt = class_exp2tgt(exp);
579         struct tgt_thread_info  *tti = tgt_th_info(env);
580         struct thandle          *th;
581         int                      rc = 0;
582
583         ENTRY;
584
585         if (unlikely(tgt == NULL)) {
586                 CDEBUG(D_ERROR, "%s: No target for connected export\n",
587                           class_exp2obd(exp)->obd_name);
588                 RETURN(-EINVAL);
589         }
590
591         if (tgt->lut_bottom->dd_rdonly)
592                 RETURN(0);
593
594         th = dt_trans_create(env, tgt->lut_bottom);
595         if (IS_ERR(th))
596                 RETURN(PTR_ERR(th));
597
598         tti_buf_lcd(tti);
599         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
600                                      &tti->tti_buf,
601                                      ted->ted_lr_off, th);
602         if (rc)
603                 GOTO(out, rc);
604
605         rc = dt_trans_start_local(env, tgt->lut_bottom, th);
606         if (rc)
607                 GOTO(out, rc);
608
609         mutex_lock(&ted->ted_lcd_lock);
610
611         /*
612          * Until this operations will be committed the sync is needed
613          * for this export. This should be done _after_ starting the
614          * transaction so that many connecting clients will not bring
615          * server down with lots of sync writes.
616          */
617         rc = tgt_new_client_cb_add(th, exp);
618         if (rc) {
619                 /* can't add callback, do sync now */
620                 th->th_sync = 1;
621         } else {
622                 spin_lock(&exp->exp_lock);
623                 exp->exp_need_sync = 1;
624                 spin_unlock(&exp->exp_lock);
625         }
626
627         tti->tti_off = ted->ted_lr_off;
628         rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
629
630         mutex_unlock(&ted->ted_lcd_lock);
631
632         EXIT;
633 out:
634         dt_trans_stop(env, tgt->lut_bottom, th);
635         CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
636                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
637                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
638
639         return rc;
640 }
641
642 static int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
643 {
644         struct tgt_thread_info  *tti = tgt_th_info(env);
645         int                      rc;
646
647         tti->tti_off = 0;
648         tti_buf_lsd(tti);
649         rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
650                             &tti->tti_off);
651         if (rc == 0)
652                 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
653
654         CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
655                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
656                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
657         return rc;
658 }
659
660 static int tgt_server_data_write(const struct lu_env *env,
661                                  struct lu_target *tgt, struct thandle *th)
662 {
663         struct tgt_thread_info  *tti = tgt_th_info(env);
664         struct dt_object        *dto;
665         int                      rc;
666
667         ENTRY;
668
669         tti->tti_off = 0;
670         tti_buf_lsd(tti);
671         lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
672
673         dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
674         rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
675
676         CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
677                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
678                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
679
680         RETURN(rc);
681 }
682
683 /**
684  * Update server data in last_rcvd
685  */
686 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
687                            int sync)
688 {
689         struct tgt_thread_info  *tti = tgt_th_info(env);
690         struct thandle          *th;
691         int                      rc = 0;
692
693         ENTRY;
694
695         CDEBUG(D_SUPER,
696                "%s: mount_count is %llu, last_transno is %llu\n",
697                tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
698                tgt->lut_last_transno);
699
700         /* Always save latest transno to keep it fresh */
701         spin_lock(&tgt->lut_translock);
702         tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
703         spin_unlock(&tgt->lut_translock);
704
705         if (tgt->lut_bottom->dd_rdonly)
706                 RETURN(0);
707
708         th = dt_trans_create(env, tgt->lut_bottom);
709         if (IS_ERR(th))
710                 RETURN(PTR_ERR(th));
711
712         th->th_sync = sync;
713
714         tti_buf_lsd(tti);
715         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
716                                      &tti->tti_buf, tti->tti_off, th);
717         if (rc)
718                 GOTO(out, rc);
719
720         rc = dt_trans_start(env, tgt->lut_bottom, th);
721         if (rc)
722                 GOTO(out, rc);
723
724         rc = tgt_server_data_write(env, tgt, th);
725 out:
726         dt_trans_stop(env, tgt->lut_bottom, th);
727
728         CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
729                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
730                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
731         RETURN(rc);
732 }
733 EXPORT_SYMBOL(tgt_server_data_update);
734
735 static int tgt_truncate_last_rcvd(const struct lu_env *env,
736                                   struct lu_target *tgt, loff_t size)
737 {
738         struct dt_object *dt = tgt->lut_last_rcvd;
739         struct thandle   *th;
740         struct lu_attr    attr;
741         int               rc;
742
743         ENTRY;
744
745         if (tgt->lut_bottom->dd_rdonly)
746                 RETURN(0);
747
748         attr.la_size = size;
749         attr.la_valid = LA_SIZE;
750
751         th = dt_trans_create(env, tgt->lut_bottom);
752         if (IS_ERR(th))
753                 RETURN(PTR_ERR(th));
754         rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
755         if (rc)
756                 GOTO(cleanup, rc);
757         rc = dt_declare_attr_set(env, dt, &attr, th);
758         if (rc)
759                 GOTO(cleanup, rc);
760         rc = dt_trans_start_local(env, tgt->lut_bottom, th);
761         if (rc)
762                 GOTO(cleanup, rc);
763
764         rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
765         if (rc == 0)
766                 rc = dt_attr_set(env, dt, &attr, th);
767
768 cleanup:
769         dt_trans_stop(env, tgt->lut_bottom, th);
770
771         RETURN(rc);
772 }
773
774 static void tgt_client_epoch_update(const struct lu_env *env,
775                                     struct obd_export *exp)
776 {
777         struct lsd_client_data  *lcd = exp->exp_target_data.ted_lcd;
778         struct lu_target        *tgt = class_exp2tgt(exp);
779
780         LASSERT(tgt && tgt->lut_bottom);
781         /** VBR: set client last_epoch to current epoch */
782         if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
783                 return;
784         lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
785         tgt_client_data_update(env, exp);
786 }
787
788 /**
789  * Update boot epoch when recovery ends
790  */
791 void tgt_boot_epoch_update(struct lu_target *tgt)
792 {
793         struct lu_env            env;
794         struct ptlrpc_request   *req;
795         __u32                    start_epoch;
796         struct list_head         client_list;
797         int                      rc;
798
799         if (tgt->lut_obd->obd_stopping)
800                 return;
801
802         rc = lu_env_init(&env, LCT_DT_THREAD);
803         if (rc) {
804                 CERROR("%s: can't initialize environment: rc = %d\n",
805                         tgt->lut_obd->obd_name, rc);
806                 return;
807         }
808
809         spin_lock(&tgt->lut_translock);
810         start_epoch = (tgt->lut_last_transno >> LR_EPOCH_BITS) + 1;
811         tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
812         tgt->lut_lsd.lsd_start_epoch = start_epoch;
813         spin_unlock(&tgt->lut_translock);
814
815         INIT_LIST_HEAD(&client_list);
816         /**
817          * The recovery is not yet finished and final queue can still be updated
818          * with resend requests. Move final list to separate one for processing
819          */
820         spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
821         list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
822         spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
823
824         /**
825          * go through list of exports participated in recovery and
826          * set new epoch for them
827          */
828         list_for_each_entry(req, &client_list, rq_list) {
829                 LASSERT(!req->rq_export->exp_delayed);
830                 if (!req->rq_export->exp_vbr_failed)
831                         tgt_client_epoch_update(&env, req->rq_export);
832         }
833         /** return list back at once */
834         spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
835         list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
836         spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
837
838         /** Clear MULTI RPCS incompatibility flag if
839          * - target is MDT and
840          * - there is no client to recover or the recovery was aborted
841          */
842         if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
843             (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
844             tgt->lut_obd->obd_abort_recovery))
845                 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
846
847         /** update server epoch */
848         tgt_server_data_update(&env, tgt, 1);
849         lu_env_fini(&env);
850 }
851
852 /**
853  * commit callback, need to update last_committed value
854  */
855 struct tgt_last_committed_callback {
856         struct dt_txn_commit_cb  llcc_cb;
857         struct lu_target        *llcc_tgt;
858         struct obd_export       *llcc_exp;
859         __u64                    llcc_transno;
860 };
861
862 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
863                                   struct dt_txn_commit_cb *cb, int err)
864 {
865         struct tgt_last_committed_callback *ccb;
866
867         ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
868
869         LASSERT(ccb->llcc_exp);
870         LASSERT(ccb->llcc_tgt != NULL);
871         LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
872
873         /* error hit, don't update last committed to provide chance to
874          * replay data after fail */
875         if (err != 0)
876                 goto out;
877
878         /* Fast path w/o spinlock, if exp_last_committed was updated
879          * with higher transno, no need to take spinlock and check,
880          * also no need to update obd_last_committed. */
881         if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
882                 goto out;
883         spin_lock(&ccb->llcc_tgt->lut_translock);
884         if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
885                 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
886
887         if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
888                 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
889                 spin_unlock(&ccb->llcc_tgt->lut_translock);
890
891                 ptlrpc_commit_replies(ccb->llcc_exp);
892                 tgt_cancel_slc_locks(ccb->llcc_tgt, ccb->llcc_transno);
893         } else {
894                 spin_unlock(&ccb->llcc_tgt->lut_translock);
895         }
896
897         CDEBUG(D_HA, "%s: transno %lld is committed\n",
898                ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
899
900 out:
901         class_export_cb_put(ccb->llcc_exp);
902         OBD_FREE_PTR(ccb);
903 }
904
905 /**
906  * Add commit callback function, it returns a non-zero value to inform
907  * caller to use sync transaction if necessary.
908  */
909 static int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
910                                   struct obd_export *exp, __u64 transno)
911 {
912         struct tgt_last_committed_callback      *ccb;
913         struct dt_txn_commit_cb                 *dcb;
914         int                                      rc;
915
916         OBD_ALLOC_PTR(ccb);
917         if (ccb == NULL)
918                 return -ENOMEM;
919
920         ccb->llcc_tgt = tgt;
921         ccb->llcc_exp = class_export_cb_get(exp);
922         ccb->llcc_transno = transno;
923
924         dcb = &ccb->llcc_cb;
925         dcb->dcb_func = tgt_cb_last_committed;
926         INIT_LIST_HEAD(&dcb->dcb_linkage);
927         strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
928
929         rc = dt_trans_cb_add(th, dcb);
930         if (rc) {
931                 class_export_cb_put(exp);
932                 OBD_FREE_PTR(ccb);
933         }
934
935         if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
936                 /* report failure to force synchronous operation */
937                 return -EPERM;
938
939         /* if exp_need_sync is set, return non-zero value to force
940          * a sync transaction. */
941         return rc ? rc : exp->exp_need_sync;
942 }
943
944 /**
945  * Add new client to the last_rcvd upon new connection.
946  *
947  * We use a bitmap to locate a free space in the last_rcvd file and initialize
948  * tg_export_data.
949  */
950 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
951 {
952         struct tg_export_data   *ted = &exp->exp_target_data;
953         struct lu_target        *tgt = class_exp2tgt(exp);
954         int                      rc = 0, idx;
955
956         ENTRY;
957
958         LASSERT(tgt && tgt->lut_client_bitmap != NULL);
959         if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
960                 RETURN(0);
961
962         if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
963                 RETURN(0);
964
965         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
966          * there's no need for extra complication here
967          */
968         idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
969 repeat:
970         if (idx >= LR_MAX_CLIENTS ||
971             OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
972                 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
973                        tgt->lut_obd->obd_name,  idx);
974                 RETURN(-EOVERFLOW);
975         }
976         if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
977                 idx = find_next_zero_bit(tgt->lut_client_bitmap,
978                                              LR_MAX_CLIENTS, idx);
979                 goto repeat;
980         }
981
982         ted->ted_lr_idx = idx;
983         ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
984                           idx * tgt->lut_lsd.lsd_client_size;
985
986         LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
987
988         if (tgt_is_multimodrpcs_client(exp)) {
989                 /* Set MULTI RPCS incompatibility flag to prevent previous
990                  * Lustre versions to mount a target with reply_data file */
991                 atomic_inc(&tgt->lut_num_clients);
992                 if (!(tgt->lut_lsd.lsd_feature_incompat &
993                       OBD_INCOMPAT_MULTI_RPCS)) {
994                         tgt->lut_lsd.lsd_feature_incompat |=
995                                                         OBD_INCOMPAT_MULTI_RPCS;
996                         rc = tgt_server_data_update(env, tgt, 1);
997                         if (rc < 0) {
998                                 CERROR("%s: unable to set MULTI RPCS "
999                                        "incompatibility flag\n",
1000                                        exp->exp_obd->obd_name);
1001                                 RETURN(rc);
1002                         }
1003                 }
1004
1005                 /* assign client slot generation */
1006                 ted->ted_lcd->lcd_generation =
1007                                 atomic_inc_return(&tgt->lut_client_generation);
1008         } else {
1009                 ted->ted_lcd->lcd_generation = 0;
1010         }
1011
1012         CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
1013                "generation %d\n",
1014                tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1015                ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
1016
1017         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
1018                 RETURN(-ENOSPC);
1019
1020         rc = tgt_client_data_update(env, exp);
1021         if (rc)
1022                 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
1023                        tgt->lut_obd->obd_name, idx, rc);
1024
1025         RETURN(rc);
1026 }
1027 EXPORT_SYMBOL(tgt_client_new);
1028
1029 /* Add an existing client to the MDS in-memory state based on
1030  * a client that was previously found in the last_rcvd file and
1031  * already has an assigned slot (idx >= 0).
1032  *
1033  * It should not be possible to fail adding an existing client - otherwise
1034  * mdt_init_server_data() callsite needs to be fixed.
1035  */
1036 int tgt_client_add(const struct lu_env *env,  struct obd_export *exp, int idx)
1037 {
1038         struct tg_export_data   *ted = &exp->exp_target_data;
1039         struct lu_target        *tgt = class_exp2tgt(exp);
1040
1041         ENTRY;
1042
1043         LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1044         LASSERTF(idx >= 0, "%d\n", idx);
1045
1046         if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1047             exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1048                 RETURN(0);
1049
1050         if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1051                 CERROR("%s: client %d: bit already set in bitmap!!\n",
1052                        tgt->lut_obd->obd_name,  idx);
1053                 LBUG();
1054         }
1055         atomic_inc(&tgt->lut_num_clients);
1056
1057         CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1058                "generation %d\n",
1059                tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1060                ted->ted_lcd->lcd_generation);
1061
1062         ted->ted_lr_idx = idx;
1063         ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1064                           idx * tgt->lut_lsd.lsd_client_size;
1065
1066         mutex_init(&ted->ted_lcd_lock);
1067
1068         LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1069
1070         RETURN(0);
1071 }
1072
1073 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1074 {
1075         struct tg_export_data   *ted = &exp->exp_target_data;
1076         struct lu_target        *tgt = class_exp2tgt(exp);
1077         int                      rc;
1078
1079         ENTRY;
1080
1081         LASSERT(ted->ted_lcd);
1082
1083         if (unlikely(tgt == NULL)) {
1084                 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1085                        class_exp2obd(exp)->obd_name);
1086                 RETURN(-EINVAL);
1087         }
1088
1089         /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1090         if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1091                     (char *)tgt->lut_obd->obd_uuid.uuid) ||
1092             exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1093                 RETURN(0);
1094
1095         /* Slot may be not yet assigned, use case is race between Client
1096          * reconnect and forced eviction */
1097         if (ted->ted_lr_idx < 0) {
1098                 CWARN("%s: client with UUID '%s' not in bitmap\n",
1099                       tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1100                 RETURN(0);
1101         }
1102
1103         CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1104                tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1105                ted->ted_lcd->lcd_uuid);
1106
1107         /* Clear the bit _after_ zeroing out the client so we don't
1108            race with filter_client_add and zero out new clients.*/
1109         if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1110                 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1111                        tgt->lut_obd->obd_name, ted->ted_lr_idx);
1112                 LBUG();
1113         }
1114
1115         /* Do not erase record for recoverable client. */
1116         if (exp->exp_flags & OBD_OPT_FAILOVER)
1117                 RETURN(0);
1118
1119         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1120                 RETURN(0);
1121
1122         /* Make sure the server's last_transno is up to date.
1123          * This should be done before zeroing client slot so last_transno will
1124          * be in server data or in client data in case of failure */
1125         rc = tgt_server_data_update(env, tgt, 0);
1126         if (rc != 0) {
1127                 CERROR("%s: failed to update server data, skip client %s "
1128                        "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1129                        ted->ted_lcd->lcd_uuid, rc);
1130                 RETURN(rc);
1131         }
1132
1133         memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1134         rc = tgt_client_data_update(env, exp);
1135
1136         CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1137                "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1138                tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1139                ted->ted_lr_idx, ted->ted_lr_off, rc);
1140         RETURN(rc);
1141 }
1142 EXPORT_SYMBOL(tgt_client_del);
1143
1144 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1145                        struct tg_export_data *ted, struct tg_reply_data *trd,
1146                        struct thandle *th, bool update_lrd_file)
1147 {
1148         struct lsd_reply_data   *lrd;
1149         int     i;
1150
1151         lrd = &trd->trd_reply;
1152         /* update export last transno */
1153         mutex_lock(&ted->ted_lcd_lock);
1154         if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1155                 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1156         mutex_unlock(&ted->ted_lcd_lock);
1157
1158         /* find a empty slot */
1159         i = tgt_find_free_reply_slot(tgt);
1160         if (unlikely(i < 0)) {
1161                 CERROR("%s: couldn't find a slot for reply data: "
1162                        "rc = %d\n", tgt_name(tgt), i);
1163                 RETURN(i);
1164         }
1165         trd->trd_index = i;
1166
1167         if (update_lrd_file) {
1168                 loff_t  off;
1169                 int     rc;
1170
1171                 /* write reply data to disk */
1172                 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1173                 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1174                 if (unlikely(rc != 0)) {
1175                         CERROR("%s: can't update %s file: rc = %d\n",
1176                                tgt_name(tgt), REPLY_DATA, rc);
1177                         RETURN(rc);
1178                 }
1179         }
1180         /* add reply data to target export's reply list */
1181         mutex_lock(&ted->ted_lcd_lock);
1182         list_add(&trd->trd_list, &ted->ted_reply_list);
1183         ted->ted_reply_cnt++;
1184         if (ted->ted_reply_cnt > ted->ted_reply_max)
1185                 ted->ted_reply_max = ted->ted_reply_cnt;
1186         mutex_unlock(&ted->ted_lcd_lock);
1187
1188         CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1189                "tag %hu, client gen %u, slot idx %d\n",
1190                trd, lrd->lrd_xid, lrd->lrd_transno,
1191                trd->trd_tag, lrd->lrd_client_gen, i);
1192         RETURN(0);
1193 }
1194 EXPORT_SYMBOL(tgt_add_reply_data);
1195
1196 /*
1197  * last_rcvd & last_committed update callbacks
1198  */
1199 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1200                                 struct dt_object *obj, __u64 opdata,
1201                                 struct thandle *th, struct ptlrpc_request *req)
1202 {
1203         struct tgt_thread_info  *tti = tgt_th_info(env);
1204         struct tgt_session_info *tsi = tgt_ses_info(env);
1205         struct obd_export       *exp = tsi->tsi_exp;
1206         struct tg_export_data   *ted;
1207         __u64                   *transno_p;
1208         int                      rc = 0;
1209         bool                     lw_client;
1210
1211         ENTRY;
1212
1213
1214         LASSERT(exp != NULL);
1215         ted = &exp->exp_target_data;
1216
1217         lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1218         if (ted->ted_lr_idx < 0 && !lw_client)
1219                 /* ofd connect may cause transaction before export has
1220                  * last_rcvd slot */
1221                 RETURN(0);
1222
1223         if (req != NULL)
1224                 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1225         else
1226                 /* From update replay, tti_transno should be set already */
1227                 LASSERT(tti->tti_transno != 0);
1228
1229         spin_lock(&tgt->lut_translock);
1230         if (th->th_result != 0) {
1231                 if (tti->tti_transno != 0) {
1232                         CERROR("%s: replay transno %llu failed: rc = %d\n",
1233                                tgt_name(tgt), tti->tti_transno, th->th_result);
1234                 }
1235         } else if (tti->tti_transno == 0) {
1236                 tti->tti_transno = ++tgt->lut_last_transno;
1237         } else {
1238                 /* should be replay */
1239                 if (tti->tti_transno > tgt->lut_last_transno)
1240                         tgt->lut_last_transno = tti->tti_transno;
1241         }
1242         spin_unlock(&tgt->lut_translock);
1243
1244         /** VBR: set new versions */
1245         if (th->th_result == 0 && obj != NULL) {
1246                 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1247                 dt_version_set(env, dto, tti->tti_transno, th);
1248         }
1249
1250         /* filling reply data */
1251         CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1252                tti->tti_transno, tgt->lut_obd->obd_last_committed);
1253
1254         if (req != NULL) {
1255                 req->rq_transno = tti->tti_transno;
1256                 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1257         }
1258
1259         /* if can't add callback, do sync write */
1260         th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1261
1262         if (lw_client) {
1263                 /* All operations performed by LW clients are synchronous and
1264                  * we store the committed transno in the last_rcvd header */
1265                 spin_lock(&tgt->lut_translock);
1266                 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1267                         tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1268                         spin_unlock(&tgt->lut_translock);
1269                         /* Although lightweight (LW) connections have no slot
1270                          * in the last_rcvd, we still want to maintain
1271                          * the in-memory lsd_client_data structure in order to
1272                          * properly handle reply reconstruction. */
1273                         rc = tgt_server_data_write(env, tgt, th);
1274                 } else {
1275                         spin_unlock(&tgt->lut_translock);
1276                 }
1277         } else if (ted->ted_lr_off == 0) {
1278                 CERROR("%s: client idx %d has offset %lld\n",
1279                        tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1280                 RETURN(-EINVAL);
1281         }
1282
1283         /* Target that supports multiple reply data */
1284         if (tgt_is_multimodrpcs_client(exp)) {
1285                 struct tg_reply_data    *trd;
1286                 struct lsd_reply_data   *lrd;
1287                 __u64                   *pre_versions;
1288                 bool                    write_update;
1289
1290                 OBD_ALLOC_PTR(trd);
1291                 if (unlikely(trd == NULL))
1292                         RETURN(-ENOMEM);
1293
1294                 /* fill reply data information */
1295                 lrd = &trd->trd_reply;
1296                 lrd->lrd_transno = tti->tti_transno;
1297                 if (req != NULL) {
1298                         lrd->lrd_xid = req->rq_xid;
1299                         trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1300                         pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1301                         lrd->lrd_result = th->th_result;
1302                         lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1303                         write_update = true;
1304                 } else {
1305                         LASSERT(tsi->tsi_xid != 0);
1306                         lrd->lrd_xid = tsi->tsi_xid;
1307                         lrd->lrd_result = tsi->tsi_result;
1308                         lrd->lrd_client_gen = tsi->tsi_client_gen;
1309                         trd->trd_tag = 0;
1310                         pre_versions = NULL;
1311                         write_update = false;
1312                 }
1313
1314                 lrd->lrd_data = opdata;
1315                 if (pre_versions) {
1316                         trd->trd_pre_versions[0] = pre_versions[0];
1317                         trd->trd_pre_versions[1] = pre_versions[1];
1318                         trd->trd_pre_versions[2] = pre_versions[2];
1319                         trd->trd_pre_versions[3] = pre_versions[3];
1320                 }
1321
1322                 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1323                 if (rc < 0)
1324                         OBD_FREE_PTR(trd);
1325                 return rc;
1326         }
1327
1328         /* Enough for update replay, let's return */
1329         if (req == NULL)
1330                 RETURN(rc);
1331
1332         mutex_lock(&ted->ted_lcd_lock);
1333         LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1334         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1335                 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1336                 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1337                 ted->ted_lcd->lcd_last_close_result = th->th_result;
1338         } else {
1339                 /* VBR: save versions in last_rcvd for reconstruct. */
1340                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1341
1342                 if (pre_versions) {
1343                         ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1344                         ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1345                         ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1346                         ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1347                 }
1348                 transno_p = &ted->ted_lcd->lcd_last_transno;
1349                 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1350                 ted->ted_lcd->lcd_last_result = th->th_result;
1351                 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1352                  * see struct ldlm_reply->lock_policy_res1; */
1353                 ted->ted_lcd->lcd_last_data = opdata;
1354         }
1355
1356         /* Update transno in slot only if non-zero number, i.e. no errors */
1357         if (likely(tti->tti_transno != 0)) {
1358                 /* Don't overwrite bigger transaction number with lower one.
1359                  * That is not sign of problem in all cases, but in any case
1360                  * this value should be monotonically increased only. */
1361                 if (*transno_p > tti->tti_transno) {
1362                         if (!tgt->lut_no_reconstruct) {
1363                                 CERROR("%s: trying to overwrite bigger transno:"
1364                                        "on-disk: %llu, new: %llu replay: "
1365                                        "%d. See LU-617.\n", tgt_name(tgt),
1366                                        *transno_p, tti->tti_transno,
1367                                        req_is_replay(req));
1368                                 if (req_is_replay(req)) {
1369                                         spin_lock(&req->rq_export->exp_lock);
1370                                         req->rq_export->exp_vbr_failed = 1;
1371                                         spin_unlock(&req->rq_export->exp_lock);
1372                                 }
1373                                 mutex_unlock(&ted->ted_lcd_lock);
1374                                 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1375                         }
1376                 } else {
1377                         *transno_p = tti->tti_transno;
1378                 }
1379         }
1380
1381         if (!lw_client) {
1382                 tti->tti_off = ted->ted_lr_off;
1383                 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1384                         rc = -EIO;
1385                 else
1386                         rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1387                                                    &tti->tti_off, th);
1388                 if (rc < 0) {
1389                         mutex_unlock(&ted->ted_lcd_lock);
1390                         RETURN(rc);
1391                 }
1392         }
1393         mutex_unlock(&ted->ted_lcd_lock);
1394         RETURN(rc);
1395 }
1396
1397 /*
1398  * last_rcvd update for echo client simulation.
1399  * It updates last_rcvd client slot and version of object in
1400  * simple way but with all locks to simulate all drawbacks
1401  */
1402 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1403                                      struct lu_target *tgt,
1404                                      struct dt_object *obj,
1405                                      struct thandle *th,
1406                                      struct obd_export *exp)
1407 {
1408         struct tgt_thread_info  *tti = tgt_th_info(env);
1409         struct tg_export_data   *ted = &exp->exp_target_data;
1410         int                      rc = 0;
1411
1412         ENTRY;
1413
1414         tti->tti_transno = 0;
1415
1416         spin_lock(&tgt->lut_translock);
1417         if (th->th_result == 0)
1418                 tti->tti_transno = ++tgt->lut_last_transno;
1419         spin_unlock(&tgt->lut_translock);
1420
1421         /** VBR: set new versions */
1422         if (th->th_result == 0 && obj != NULL)
1423                 dt_version_set(env, obj, tti->tti_transno, th);
1424
1425         /* if can't add callback, do sync write */
1426         th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1427                                                 tti->tti_transno);
1428
1429         LASSERT(ted->ted_lr_off > 0);
1430
1431         mutex_lock(&ted->ted_lcd_lock);
1432         LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1433         ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1434         ted->ted_lcd->lcd_last_result = th->th_result;
1435
1436         tti->tti_off = ted->ted_lr_off;
1437         rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1438         mutex_unlock(&ted->ted_lcd_lock);
1439         RETURN(rc);
1440 }
1441
1442 static int tgt_clients_data_init(const struct lu_env *env,
1443                                  struct lu_target *tgt,
1444                                  unsigned long last_size)
1445 {
1446         struct obd_device       *obd = tgt->lut_obd;
1447         struct lr_server_data   *lsd = &tgt->lut_lsd;
1448         struct lsd_client_data  *lcd = NULL;
1449         struct tg_export_data   *ted;
1450         int                      cl_idx;
1451         int                      rc = 0;
1452         loff_t                   off = lsd->lsd_client_start;
1453         __u32                    generation = 0;
1454         struct cfs_hash         *hash = NULL;
1455
1456         ENTRY;
1457
1458         if (tgt->lut_bottom->dd_rdonly)
1459                 RETURN(0);
1460
1461         CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1462                  sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1463
1464         OBD_ALLOC_PTR(lcd);
1465         if (lcd == NULL)
1466                 RETURN(-ENOMEM);
1467
1468         hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1469         if (hash == NULL)
1470                 GOTO(err_out, rc = -ENODEV);
1471
1472         for (cl_idx = 0; off < last_size; cl_idx++) {
1473                 struct obd_export       *exp;
1474                 __u64                    last_transno;
1475
1476                 /* Don't assume off is incremented properly by
1477                  * read_record(), in case sizeof(*lcd)
1478                  * isn't the same as fsd->lsd_client_size.  */
1479                 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1480                 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1481                 if (rc) {
1482                         CERROR("%s: error reading last_rcvd %s idx %d off "
1483                                "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1484                                cl_idx, off, rc);
1485                         rc = 0;
1486                         break; /* read error shouldn't cause startup to fail */
1487                 }
1488
1489                 if (lcd->lcd_uuid[0] == '\0') {
1490                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1491                                cl_idx);
1492                         continue;
1493                 }
1494
1495                 last_transno = lcd_last_transno(lcd);
1496
1497                 /* These exports are cleaned up by disconnect, so they
1498                  * need to be set up like real exports as connect does.
1499                  */
1500                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1501                        " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1502                        cl_idx, last_transno, lsd->lsd_last_transno,
1503                        lcd_last_xid(lcd), lcd->lcd_generation);
1504
1505                 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1506                 if (IS_ERR(exp)) {
1507                         if (PTR_ERR(exp) == -EALREADY) {
1508                                 /* export already exists, zero out this one */
1509                                 CERROR("%s: Duplicate export %s!\n",
1510                                        tgt_name(tgt), lcd->lcd_uuid);
1511                                 continue;
1512                         }
1513                         GOTO(err_out, rc = PTR_ERR(exp));
1514                 }
1515
1516                 ted = &exp->exp_target_data;
1517                 *ted->ted_lcd = *lcd;
1518
1519                 rc = tgt_client_add(env, exp, cl_idx);
1520                 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1521                 /* VBR: set export last committed version */
1522                 exp->exp_last_committed = last_transno;
1523                 spin_lock(&exp->exp_lock);
1524                 exp->exp_connecting = 0;
1525                 exp->exp_in_recovery = 0;
1526                 spin_unlock(&exp->exp_lock);
1527                 obd->obd_max_recoverable_clients++;
1528
1529                 if (tgt->lut_lsd.lsd_feature_incompat &
1530                     OBD_INCOMPAT_MULTI_RPCS &&
1531                     lcd->lcd_generation != 0) {
1532                         /* compute the highest valid client generation */
1533                         generation = max(generation, lcd->lcd_generation);
1534                         /* fill client_generation <-> export hash table */
1535                         rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1536                                                  &exp->exp_gen_hash);
1537                         if (rc != 0) {
1538                                 CERROR("%s: duplicate export for client "
1539                                        "generation %u\n",
1540                                        tgt_name(tgt), lcd->lcd_generation);
1541                                 class_export_put(exp);
1542                                 GOTO(err_out, rc);
1543                         }
1544                 }
1545
1546                 class_export_put(exp);
1547
1548                 rc = rev_import_init(exp);
1549                 if (rc != 0) {
1550                         class_unlink_export(exp);
1551                         GOTO(err_out, rc);
1552                 }
1553
1554                 /* Need to check last_rcvd even for duplicated exports. */
1555                 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1556                        cl_idx, last_transno);
1557
1558                 spin_lock(&tgt->lut_translock);
1559                 tgt->lut_last_transno = max(last_transno,
1560                                             tgt->lut_last_transno);
1561                 spin_unlock(&tgt->lut_translock);
1562         }
1563
1564         /* record highest valid client generation */
1565         atomic_set(&tgt->lut_client_generation, generation);
1566
1567 err_out:
1568         if (hash != NULL)
1569                 cfs_hash_putref(hash);
1570         OBD_FREE_PTR(lcd);
1571         RETURN(rc);
1572 }
1573
1574 struct server_compat_data {
1575         __u32 rocompat;
1576         __u32 incompat;
1577         __u32 rocinit;
1578         __u32 incinit;
1579 };
1580
1581 static struct server_compat_data tgt_scd[] = {
1582         [LDD_F_SV_TYPE_MDT] = {
1583                 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1584                 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1585                             OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1586                             OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1587                             OBD_INCOMPAT_MULTI_RPCS,
1588                 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1589                 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1590                            OBD_INCOMPAT_MULTI_OI,
1591         },
1592         [LDD_F_SV_TYPE_OST] = {
1593                 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1594                 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1595                             OBD_INCOMPAT_FID,
1596                 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1597                 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1598         }
1599 };
1600
1601 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1602 {
1603         struct tgt_thread_info          *tti = tgt_th_info(env);
1604         struct lr_server_data           *lsd = &tgt->lut_lsd;
1605         unsigned long                    last_rcvd_size;
1606         __u32                            index;
1607         int                              rc, type;
1608
1609         rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1610         if (rc)
1611                 RETURN(rc);
1612
1613         last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1614
1615         /* ensure padding in the struct is the correct size */
1616         CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1617                  sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1618
1619         rc = server_name2index(tgt_name(tgt), &index, NULL);
1620         if (rc < 0) {
1621                 CERROR("%s: Can not get index from name: rc = %d\n",
1622                        tgt_name(tgt), rc);
1623                 RETURN(rc);
1624         }
1625         /* server_name2index() returns type */
1626         type = rc;
1627         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1628                 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1629                 RETURN(-EINVAL);
1630         }
1631
1632         /* last_rcvd on OST doesn't provide reconstruct support because there
1633          * may be up to 8 in-flight write requests per single slot in
1634          * last_rcvd client data
1635          */
1636         tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1637
1638         if (last_rcvd_size == 0) {
1639                 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1640
1641                 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1642                        sizeof(lsd->lsd_uuid));
1643                 lsd->lsd_last_transno = 0;
1644                 lsd->lsd_mount_count = 0;
1645                 lsd->lsd_server_size = LR_SERVER_SIZE;
1646                 lsd->lsd_client_start = LR_CLIENT_START;
1647                 lsd->lsd_client_size = LR_CLIENT_SIZE;
1648                 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1649                 lsd->lsd_osd_index = index;
1650                 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1651                 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1652         } else {
1653                 rc = tgt_server_data_read(env, tgt);
1654                 if (rc) {
1655                         CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1656                                tgt_name(tgt), rc);
1657                         RETURN(rc);
1658                 }
1659                 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1660                         if (tgt->lut_bottom->dd_rdonly) {
1661                                 /* Such difference may be caused by mounting
1662                                  * up snapshot with new fsname under rd_only
1663                                  * mode. But even if it was NOT, it will not
1664                                  * damage the system because of "rd_only". */
1665                                 memcpy(lsd->lsd_uuid,
1666                                        tgt->lut_obd->obd_uuid.uuid,
1667                                        sizeof(lsd->lsd_uuid));
1668                         } else {
1669                                 LCONSOLE_ERROR_MSG(0x157, "Trying to start "
1670                                                    "OBD %s using the wrong "
1671                                                    "disk %s. Were the /dev/ "
1672                                                    "assignments rearranged?\n",
1673                                                    tgt->lut_obd->obd_uuid.uuid,
1674                                                    lsd->lsd_uuid);
1675                                 RETURN(-EINVAL);
1676                         }
1677                 }
1678
1679                 if (lsd->lsd_osd_index != index) {
1680                         LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1681                                            "is different with the index %d in"
1682                                            "config log, It might be disk"
1683                                            "corruption!\n", tgt_name(tgt),
1684                                            lsd->lsd_osd_index, index);
1685                         RETURN(-EINVAL);
1686                 }
1687         }
1688
1689         if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1690                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1691                        tgt_name(tgt),
1692                        lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1693                 RETURN(-EINVAL);
1694         }
1695
1696         if (type == LDD_F_SV_TYPE_MDT)
1697                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1698
1699         if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1700                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1701                        tgt_name(tgt),
1702                        lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1703                 RETURN(-EINVAL);
1704         }
1705         /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1706         if (type == LDD_F_SV_TYPE_MDT &&
1707             !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1708                 if (last_rcvd_size > lsd->lsd_client_start) {
1709                         LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1710                                       "remove all clients for interop needs\n",
1711                                       tgt_name(tgt));
1712                         rc = tgt_truncate_last_rcvd(env, tgt,
1713                                                     lsd->lsd_client_start);
1714                         if (rc)
1715                                 RETURN(rc);
1716                         last_rcvd_size = lsd->lsd_client_start;
1717                 }
1718                 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1719                 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1720         }
1721
1722         spin_lock(&tgt->lut_translock);
1723         tgt->lut_last_transno = lsd->lsd_last_transno;
1724         spin_unlock(&tgt->lut_translock);
1725
1726         lsd->lsd_mount_count++;
1727
1728         CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1729         CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1730                tgt_name(tgt), tgt->lut_last_transno);
1731         CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1732                tgt_name(tgt), lsd->lsd_mount_count);
1733         CDEBUG(D_INODE, "%s: server data size: %u\n",
1734                tgt_name(tgt), lsd->lsd_server_size);
1735         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1736                tgt_name(tgt), lsd->lsd_client_start);
1737         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1738                tgt_name(tgt), lsd->lsd_client_size);
1739         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1740                tgt_name(tgt), last_rcvd_size);
1741         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1742                tgt_name(tgt), lsd->lsd_subdir_count);
1743         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1744                last_rcvd_size <= lsd->lsd_client_start ? 0 :
1745                (last_rcvd_size - lsd->lsd_client_start) /
1746                 lsd->lsd_client_size);
1747         CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1748
1749         if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1750             lsd->lsd_client_size == 0) {
1751                 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1752                 RETURN(-EINVAL);
1753         }
1754
1755         if (!tgt->lut_obd->obd_replayable)
1756                 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1757
1758         rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1759         if (rc < 0)
1760                 GOTO(err_client, rc);
1761
1762         spin_lock(&tgt->lut_translock);
1763         /* obd_last_committed is used for compatibility
1764          * with other lustre recovery code */
1765         tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1766         spin_unlock(&tgt->lut_translock);
1767
1768         tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1769         tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1770
1771         /* save it, so mount count and last_transno is current */
1772         rc = tgt_server_data_update(env, tgt, 0);
1773         if (rc < 0)
1774                 GOTO(err_client, rc);
1775
1776         RETURN(0);
1777
1778 err_client:
1779         class_disconnect_exports(tgt->lut_obd);
1780         return rc;
1781 }
1782
1783 /* add credits for last_rcvd update */
1784 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1785                      void *cookie)
1786 {
1787         struct lu_target        *tgt = cookie;
1788         struct tgt_session_info *tsi;
1789         struct tgt_thread_info  *tti = tgt_th_info(env);
1790         struct dt_object        *dto;
1791         int                      rc;
1792
1793         /* For readonly case, the caller should have got failure
1794          * when start the transaction. If the logic comes here,
1795          * there must be something wrong. */
1796         if (unlikely(tgt->lut_bottom->dd_rdonly)) {
1797                 dump_stack();
1798                 LBUG();
1799         }
1800
1801         /* if there is no session, then this transaction is not result of
1802          * request processing but some local operation */
1803         if (env->le_ses == NULL)
1804                 return 0;
1805
1806         LASSERT(tgt->lut_last_rcvd);
1807         tsi = tgt_ses_info(env);
1808         /* OFD may start transaction without export assigned */
1809         if (tsi->tsi_exp == NULL)
1810                 return 0;
1811
1812         if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1813                 /*
1814                  * Use maximum possible file offset for declaration to ensure
1815                  * ZFS will reserve enough credits for a write anywhere in this
1816                  * file, since we don't know where in the file the write will be
1817                  * because a replay slot has not been assigned.  This should be
1818                  * replaced by dmu_tx_hold_append() when available.
1819                  */
1820                 tti->tti_buf.lb_buf = NULL;
1821                 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1822                 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1823                 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1824                 if (rc)
1825                         return rc;
1826         } else {
1827                 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1828                 tti_buf_lcd(tti);
1829                 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1830                 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1831                                              tti->tti_off, th);
1832                 if (rc)
1833                         return rc;
1834         }
1835
1836         if (tsi->tsi_vbr_obj != NULL &&
1837             !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1838                 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1839                 rc = dt_declare_version_set(env, dto, th);
1840         }
1841
1842         return rc;
1843 }
1844
1845 /* Update last_rcvd records with latests transaction data */
1846 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1847                     void *cookie)
1848 {
1849         struct lu_target        *tgt = cookie;
1850         struct tgt_session_info *tsi;
1851         struct tgt_thread_info  *tti = tgt_th_info(env);
1852         struct dt_object        *obj = NULL;
1853         int                      rc;
1854         bool                     echo_client;
1855
1856         if (env->le_ses == NULL)
1857                 return 0;
1858
1859         tsi = tgt_ses_info(env);
1860         /* OFD may start transaction without export assigned */
1861         if (tsi->tsi_exp == NULL)
1862                 return 0;
1863
1864         echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1865
1866         if (tti->tti_has_trans && !echo_client) {
1867                 if (tti->tti_mult_trans == 0) {
1868                         CDEBUG(D_HA, "More than one transaction %llu\n",
1869                                tti->tti_transno);
1870                         RETURN(0);
1871                 }
1872                 /* we need another transno to be assigned */
1873                 tti->tti_transno = 0;
1874         } else if (th->th_result == 0) {
1875                 tti->tti_has_trans = 1;
1876         }
1877
1878         if (tsi->tsi_vbr_obj != NULL &&
1879             !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1880                 obj = tsi->tsi_vbr_obj;
1881         }
1882
1883         if (unlikely(echo_client)) /* echo client special case */
1884                 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1885                                                tsi->tsi_exp);
1886         else
1887                 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1888                                           tgt_ses_req(tsi));
1889         return rc;
1890 }
1891
1892 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1893 {
1894         struct tgt_thread_info  *tti = tgt_th_info(env);
1895         struct lsd_reply_data   *lrd = &tti->tti_lrd;
1896         unsigned long            reply_data_size;
1897         int                      rc;
1898         struct lsd_reply_header *lrh = NULL;
1899         struct lsd_client_data  *lcd = NULL;
1900         struct tg_reply_data    *trd = NULL;
1901         int                      idx;
1902         loff_t                   off;
1903         struct cfs_hash         *hash = NULL;
1904         struct obd_export       *exp;
1905         struct tg_export_data   *ted;
1906         int                      reply_data_recovered = 0;
1907
1908         rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1909         if (rc)
1910                 GOTO(out, rc);
1911         reply_data_size = (unsigned long)tti->tti_attr.la_size;
1912
1913         OBD_ALLOC_PTR(lrh);
1914         if (lrh == NULL)
1915                 GOTO(out, rc = -ENOMEM);
1916
1917         if (reply_data_size == 0) {
1918                 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1919                        tgt_name(tgt));
1920                 lrh->lrh_magic = LRH_MAGIC;
1921                 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1922                 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1923                 rc = tgt_reply_header_write(env, tgt, lrh);
1924                 if (rc) {
1925                         CERROR("%s: error writing %s: rc = %d\n",
1926                                tgt_name(tgt), REPLY_DATA, rc);
1927                         GOTO(out, rc);
1928                 }
1929         } else {
1930                 rc = tgt_reply_header_read(env, tgt, lrh);
1931                 if (rc) {
1932                         CERROR("%s: error reading %s: rc = %d\n",
1933                                tgt_name(tgt), REPLY_DATA, rc);
1934                         GOTO(out, rc);
1935                 }
1936                 if (lrh->lrh_magic != LRH_MAGIC ||
1937                     lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1938                     lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1939                         CERROR("%s: invalid header in %s\n",
1940                                tgt_name(tgt), REPLY_DATA);
1941                         GOTO(out, rc = -EINVAL);
1942                 }
1943
1944                 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1945                 if (hash == NULL)
1946                         GOTO(out, rc = -ENODEV);
1947
1948                 OBD_ALLOC_PTR(lcd);
1949                 if (lcd == NULL)
1950                         GOTO(out, rc = -ENOMEM);
1951
1952                 OBD_ALLOC_PTR(trd);
1953                 if (trd == NULL)
1954                         GOTO(out, rc = -ENOMEM);
1955
1956                 /* Load reply_data from disk */
1957                 for (idx = 0, off = sizeof(struct lsd_reply_header);
1958                      off < reply_data_size;
1959                      idx++, off += sizeof(struct lsd_reply_data)) {
1960                         rc = tgt_reply_data_read(env, tgt, lrd, off);
1961                         if (rc) {
1962                                 CERROR("%s: error reading %s: rc = %d\n",
1963                                        tgt_name(tgt), REPLY_DATA, rc);
1964                                 GOTO(out, rc);
1965                         }
1966
1967                         exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1968                         if (exp == NULL) {
1969                                 /* old reply data from a disconnected client */
1970                                 continue;
1971                         }
1972                         ted = &exp->exp_target_data;
1973                         mutex_lock(&ted->ted_lcd_lock);
1974
1975                         /* create in-memory reply_data and link it to
1976                          * target export's reply list */
1977                         rc = tgt_set_reply_slot(tgt, idx);
1978                         if (rc != 0) {
1979                                 mutex_unlock(&ted->ted_lcd_lock);
1980                                 GOTO(out, rc);
1981                         }
1982                         trd->trd_reply = *lrd;
1983                         trd->trd_pre_versions[0] = 0;
1984                         trd->trd_pre_versions[1] = 0;
1985                         trd->trd_pre_versions[2] = 0;
1986                         trd->trd_pre_versions[3] = 0;
1987                         trd->trd_index = idx;
1988                         trd->trd_tag = 0;
1989                         list_add(&trd->trd_list, &ted->ted_reply_list);
1990                         ted->ted_reply_cnt++;
1991                         if (ted->ted_reply_cnt > ted->ted_reply_max)
1992                                 ted->ted_reply_max = ted->ted_reply_cnt;
1993
1994                         CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1995                                "transno %llu, client gen %u, slot idx %d\n",
1996                                tgt_name(tgt), trd, lrd->lrd_xid,
1997                                lrd->lrd_transno, lrd->lrd_client_gen,
1998                                trd->trd_index);
1999
2000                         /* update export last committed transation */
2001                         exp->exp_last_committed = max(exp->exp_last_committed,
2002                                                       lrd->lrd_transno);
2003
2004                         mutex_unlock(&ted->ted_lcd_lock);
2005                         class_export_put(exp);
2006
2007                         /* update target last committed transaction */
2008                         spin_lock(&tgt->lut_translock);
2009                         tgt->lut_last_transno = max(tgt->lut_last_transno,
2010                                                     lrd->lrd_transno);
2011                         spin_unlock(&tgt->lut_translock);
2012
2013                         reply_data_recovered++;
2014
2015                         OBD_ALLOC_PTR(trd);
2016                         if (trd == NULL)
2017                                 GOTO(out, rc = -ENOMEM);
2018                 }
2019                 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
2020                        tgt_name(tgt), reply_data_recovered);
2021         }
2022
2023         spin_lock(&tgt->lut_translock);
2024         /* obd_last_committed is used for compatibility
2025          * with other lustre recovery code */
2026         tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
2027         spin_unlock(&tgt->lut_translock);
2028
2029         rc = 0;
2030
2031 out:
2032         if (hash != NULL)
2033                 cfs_hash_putref(hash);
2034         if (lcd != NULL)
2035                 OBD_FREE_PTR(lcd);
2036         if (trd != NULL)
2037                 OBD_FREE_PTR(trd);
2038         if (lrh != NULL)
2039                 OBD_FREE_PTR(lrh);
2040         return rc;
2041 }
2042
2043 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
2044                                               __u64 xid)
2045 {
2046         struct tg_reply_data    *found = NULL;
2047         struct tg_reply_data    *reply;
2048
2049         mutex_lock(&ted->ted_lcd_lock);
2050         list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
2051                 if (reply->trd_reply.lrd_xid == xid) {
2052                         found = reply;
2053                         break;
2054                 }
2055         }
2056         mutex_unlock(&ted->ted_lcd_lock);
2057         return found;
2058 }
2059 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
2060
2061 /* Look for a reply data matching specified request @req
2062  * A copy is returned in @trd if the pointer is not NULL
2063  */
2064 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2065 {
2066         struct tg_export_data   *ted = &req->rq_export->exp_target_data;
2067         struct tg_reply_data    *reply;
2068         bool                     found = false;
2069
2070         reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2071         if (reply != NULL) {
2072                 found = true;
2073                 if (trd != NULL)
2074                         *trd = *reply;
2075         }
2076
2077         CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2078                tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2079                found ? 1 : 0);
2080
2081         return found;
2082 }
2083 EXPORT_SYMBOL(tgt_lookup_reply);
2084
2085 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2086 {
2087         struct tg_export_data   *ted = &exp->exp_target_data;
2088         struct lu_target        *lut = class_exp2tgt(exp);
2089         struct tg_reply_data    *trd, *tmp;
2090
2091         mutex_lock(&ted->ted_lcd_lock);
2092         list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2093                 if (trd->trd_reply.lrd_xid > rcvd_xid)
2094                         continue;
2095                 ted->ted_release_xid++;
2096                 tgt_release_reply_data(lut, ted, trd);
2097         }
2098         mutex_unlock(&ted->ted_lcd_lock);
2099
2100         return 0;
2101 }
2102
2103 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2104 {
2105         struct tg_export_data   *ted = &exp->exp_target_data;
2106         struct lu_target        *lut = class_exp2tgt(exp);
2107         struct tg_reply_data    *trd, *tmp;
2108
2109         if (tag == 0)
2110                 return 0;
2111
2112         mutex_lock(&ted->ted_lcd_lock);
2113         list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2114                 if (trd->trd_tag != tag)
2115                         continue;
2116                 ted->ted_release_tag++;
2117                 tgt_release_reply_data(lut, ted, trd);
2118                 break;
2119         }
2120         mutex_unlock(&ted->ted_lcd_lock);
2121
2122         return 0;
2123 }
2124