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