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