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