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