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