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