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