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