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