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