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