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