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