4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre Unified Target
32 * These are common function to work with last_received file
34 * Author: Mikhail Pershin <mike.pershin@intel.com>
37 #include <obd_class.h>
38 #include <lustre_fid.h>
40 #include "tgt_internal.h"
42 /** version recovery epoch */
43 #define LR_EPOCH_BITS 32
45 /* Allocate a bitmap for a chunk of reply data slots */
46 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
50 OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
55 spin_lock(&lut->lut_client_bitmap_lock);
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) *
65 lut->lut_reply_bitmap[chunk] = bm;
67 spin_unlock(&lut->lut_client_bitmap_lock);
72 /* Look for an available reply data slot in the bitmap
74 * Allocate bitmap chunk when first used
75 * XXX algo could be improved if this routine limits performance
77 static int tgt_find_free_reply_slot(struct lu_target *lut)
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);
91 bmp = lut->lut_reply_bitmap[chunk];
93 /* look for an available slot in this chunk */
95 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
96 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
100 if (test_and_set_bit(b, bmp) == 0)
101 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
108 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
110 * Allocate the bitmap chunk if necessary
112 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
118 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
119 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
121 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
122 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
124 /* allocate the bitmap chunk if necessary */
125 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
126 rc = tgt_bitmap_chunk_alloc(lut, chunk);
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",
142 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
145 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
150 if (lut->lut_obd->obd_stopping)
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
157 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
158 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
160 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
161 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
163 if (lut->lut_reply_bitmap[chunk] == NULL) {
164 CERROR("%s: slot %d not allocated\n",
169 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
170 CERROR("%s: slot %d already clear in bitmap\n",
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)
185 struct lsd_reply_header buf;
186 struct tgt_thread_info *tti = tgt_th_info(env);
189 tti->tti_buf.lb_buf = &buf;
190 tti->tti_buf.lb_len = sizeof(buf);
192 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
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);
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);
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)
215 struct lsd_reply_header buf;
216 struct tgt_thread_info *tti = tgt_th_info(env);
218 struct dt_object *dto;
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);
225 if (tgt->lut_bottom->dd_rdonly)
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);
232 th = dt_trans_create(env, tgt->lut_bottom);
238 tti->tti_buf.lb_buf = &buf;
239 tti->tti_buf.lb_len = sizeof(buf);
241 rc = dt_declare_record_write(env, tgt->lut_reply_data,
242 &tti->tti_buf, tti->tti_off, th);
246 rc = dt_trans_start(env, tgt->lut_bottom, th);
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);
253 dt_trans_stop(env, tgt->lut_bottom, th);
257 /* Write the reply data @lrd into reply_data file of target @tgt
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,
264 struct tgt_thread_info *tti = tgt_th_info(env);
265 struct dt_object *dto;
266 struct lsd_reply_data *buf = &tti->tti_lrd;
268 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
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);
276 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
279 tti->tti_buf.lb_buf = buf;
280 tti->tti_buf.lb_len = sizeof(*buf);
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);
286 /* Read the reply data from reply_data file of target @tgt at offset @off
287 * into structure @lrd
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)
293 struct tgt_thread_info *tti = tgt_th_info(env);
294 struct lsd_reply_data *buf = &tti->tti_lrd;
297 tti->tti_buf.lb_buf = buf;
298 tti->tti_buf.lb_len = sizeof(*buf);
300 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
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);
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
319 static void tgt_free_reply_data(struct lu_target *lut,
320 struct tg_export_data *ted,
321 struct tg_reply_data *trd)
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,
329 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
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);
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
343 static void tgt_release_reply_data(struct lu_target *lut,
344 struct tg_export_data *ted,
345 struct tg_reply_data *trd)
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,
353 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
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
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;
369 tgt_free_reply_data(lut, ted, trd);
373 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
375 tti->tti_buf.lb_buf = &tti->tti_lsd;
376 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
377 return &tti->tti_buf;
380 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
382 tti->tti_buf.lb_buf = &tti->tti_lcd;
383 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
384 return &tti->tti_buf;
388 * Allocate in-memory data for client slot related to export.
390 int tgt_client_alloc(struct obd_export *exp)
393 LASSERT(exp != exp->exp_obd->obd_self_export);
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);
400 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
401 if (exp->exp_target_data.ted_lcd == NULL)
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);
409 EXPORT_SYMBOL(tgt_client_alloc);
412 * Free in-memory data for client slot related to export.
414 void tgt_client_free(struct obd_export *exp)
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;
420 LASSERT(exp != exp->exp_obd->obd_self_export);
422 tgt_fmd_cleanup(exp);
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);
429 if (ted->ted_reply_last != NULL) {
430 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
431 ted->ted_reply_last = NULL;
433 mutex_unlock(&ted->ted_lcd_lock);
435 if (!hlist_unhashed(&exp->exp_gen_hash))
436 cfs_hash_del(exp->exp_obd->obd_gen_hash,
437 &ted->ted_lcd->lcd_generation,
440 OBD_FREE_PTR(ted->ted_lcd);
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 ||
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);
457 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
458 atomic_dec(&lut->lut_num_clients);
460 EXPORT_SYMBOL(tgt_client_free);
462 static inline void tgt_check_lcd(const char *obd_name, int index,
463 struct lsd_client_data *lcd)
465 size_t uuid_size = sizeof(lcd->lcd_uuid);
467 if (strnlen((char*)lcd->lcd_uuid, uuid_size) == uuid_size) {
468 lcd->lcd_uuid[uuid_size - 1] = '\0';
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);
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)
479 struct tgt_thread_info *tti = tgt_th_info(env);
483 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
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);
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);
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)
508 struct tgt_thread_info *tti = tgt_th_info(env);
509 struct dt_object *dto;
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);
517 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
518 return dt_record_write(env, dto, &tti->tti_buf, off, th);
521 struct tgt_new_client_callback {
522 struct dt_txn_commit_cb lncc_cb;
523 struct obd_export *lncc_exp;
526 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
527 struct dt_txn_commit_cb *cb, int err)
529 struct tgt_new_client_callback *ccb;
531 ccb = container_of(cb, struct tgt_new_client_callback, lncc_cb);
533 LASSERT(ccb->lncc_exp->exp_obd);
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);
539 spin_lock(&ccb->lncc_exp->exp_lock);
541 ccb->lncc_exp->exp_need_sync = 0;
543 spin_unlock(&ccb->lncc_exp->exp_lock);
544 class_export_cb_put(ccb->lncc_exp);
549 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
551 struct tgt_new_client_callback *ccb;
552 struct dt_txn_commit_cb *dcb;
559 ccb->lncc_exp = class_export_cb_get(exp);
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));
566 rc = dt_trans_cb_add(th, dcb);
568 class_export_cb_put(exp);
575 * Update client data in last_rcvd
577 static int tgt_client_data_update(const struct lu_env *env,
578 struct obd_export *exp)
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);
588 if (unlikely(tgt == NULL)) {
589 CDEBUG(D_ERROR, "%s: No target for connected export\n",
590 class_exp2obd(exp)->obd_name);
594 if (tgt->lut_bottom->dd_rdonly)
597 th = dt_trans_create(env, tgt->lut_bottom);
602 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
604 ted->ted_lr_off, th);
608 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
612 mutex_lock(&ted->ted_lcd_lock);
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.
620 rc = tgt_new_client_cb_add(th, exp);
622 /* can't add callback, do sync now */
625 spin_lock(&exp->exp_lock);
626 exp->exp_need_sync = 1;
627 spin_unlock(&exp->exp_lock);
630 tti->tti_off = ted->ted_lr_off;
631 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
633 mutex_unlock(&ted->ted_lcd_lock);
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);
645 static int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
647 struct tgt_thread_info *tti = tgt_th_info(env);
652 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
655 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
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);
663 static int tgt_server_data_write(const struct lu_env *env,
664 struct lu_target *tgt, struct thandle *th)
666 struct tgt_thread_info *tti = tgt_th_info(env);
667 struct dt_object *dto;
674 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
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);
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);
687 * Update server data in last_rcvd
689 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
692 struct tgt_thread_info *tti = tgt_th_info(env);
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);
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);
708 if (tgt->lut_bottom->dd_rdonly)
711 th = dt_trans_create(env, tgt->lut_bottom);
718 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
719 &tti->tti_buf, tti->tti_off, th);
723 rc = dt_trans_start(env, tgt->lut_bottom, th);
727 rc = tgt_server_data_write(env, tgt, th);
729 dt_trans_stop(env, tgt->lut_bottom, th);
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);
736 EXPORT_SYMBOL(tgt_server_data_update);
738 static int tgt_truncate_last_rcvd(const struct lu_env *env,
739 struct lu_target *tgt, loff_t size)
741 struct dt_object *dt = tgt->lut_last_rcvd;
748 if (tgt->lut_bottom->dd_rdonly)
752 attr.la_valid = LA_SIZE;
754 th = dt_trans_create(env, tgt->lut_bottom);
757 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
760 rc = dt_declare_attr_set(env, dt, &attr, th);
763 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
767 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
769 rc = dt_attr_set(env, dt, &attr, th);
772 dt_trans_stop(env, tgt->lut_bottom, th);
777 static void tgt_client_epoch_update(const struct lu_env *env,
778 struct obd_export *exp)
780 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
781 struct lu_target *tgt = class_exp2tgt(exp);
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)
787 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
788 tgt_client_data_update(env, exp);
792 * Update boot epoch when recovery ends
794 void tgt_boot_epoch_update(struct lu_target *tgt)
797 struct ptlrpc_request *req;
799 LIST_HEAD(client_list);
802 if (tgt->lut_obd->obd_stopping)
805 rc = lu_env_init(&env, LCT_DT_THREAD);
807 CERROR("%s: can't initialize environment: rc = %d\n",
808 tgt->lut_obd->obd_name, rc);
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);
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
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);
827 * go through list of exports participated in recovery and
828 * set new epoch for them
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);
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);
840 /** Clear MULTI RPCS incompatibility flag if
841 * - target is MDT and
842 * - there is no client to recover or the recovery was aborted
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;
849 /** update server epoch */
850 tgt_server_data_update(&env, tgt, 1);
855 * commit callback, need to update last_committed value
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;
864 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
865 struct dt_txn_commit_cb *cb, int err)
867 struct tgt_last_committed_callback *ccb;
869 ccb = container_of(cb, struct tgt_last_committed_callback, llcc_cb);
871 LASSERT(ccb->llcc_exp);
872 LASSERT(ccb->llcc_tgt != NULL);
873 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
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);
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);
886 /* error hit, don't update last committed to provide chance to
887 * replay data after fail */
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)
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;
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);
904 ptlrpc_commit_replies(ccb->llcc_exp);
905 tgt_cancel_slc_locks(ccb->llcc_tgt, ccb->llcc_transno);
907 spin_unlock(&ccb->llcc_tgt->lut_translock);
910 CDEBUG(D_HA, "%s: transno %lld is committed\n",
911 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
914 class_export_cb_put(ccb->llcc_exp);
919 * Add commit callback function, it returns a non-zero value to inform
920 * caller to use sync transaction if necessary.
922 static int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
923 struct obd_export *exp, __u64 transno)
925 struct tgt_last_committed_callback *ccb;
926 struct dt_txn_commit_cb *dcb;
934 ccb->llcc_exp = class_export_cb_get(exp);
935 ccb->llcc_transno = transno;
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));
942 rc = dt_trans_cb_add(th, dcb);
944 class_export_cb_put(exp);
948 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
949 /* report failure to force synchronous operation */
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;
957 static int tgt_is_local_client(const struct lu_env *env,
958 struct obd_export *exp)
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);
965 if (exp_connect_flags(exp) & OBD_CONNECT_MDS ||
966 exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS)
968 if (tgt->lut_local_recovery)
972 lnet_nid4_to_nid(req->rq_peer.nid, &nid);
973 if (!LNetIsPeerLocal(&nid))
980 * Add new client to the last_rcvd upon new connection.
982 * We use a bitmap to locate a free space in the last_rcvd file and initialize
985 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
987 struct tg_export_data *ted = &exp->exp_target_data;
988 struct lu_target *tgt = class_exp2tgt(exp);
993 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
994 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
997 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1000 if (tgt_is_local_client(env, exp)) {
1001 LCONSOLE_WARN("%s: local client %s w/o recovery\n",
1002 exp->exp_obd->obd_name, ted->ted_lcd->lcd_uuid);
1003 exp->exp_no_recovery = 1;
1007 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
1008 * there's no need for extra complication here
1010 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
1012 if (idx >= LR_MAX_CLIENTS ||
1013 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
1014 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
1015 tgt->lut_obd->obd_name, idx);
1018 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1019 idx = find_next_zero_bit(tgt->lut_client_bitmap,
1020 LR_MAX_CLIENTS, idx);
1024 ted->ted_lr_idx = idx;
1025 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1026 idx * tgt->lut_lsd.lsd_client_size;
1028 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1030 if (tgt_is_multimodrpcs_client(exp)) {
1031 /* Set MULTI RPCS incompatibility flag to prevent previous
1032 * Lustre versions to mount a target with reply_data file */
1033 atomic_inc(&tgt->lut_num_clients);
1034 if (!(tgt->lut_lsd.lsd_feature_incompat &
1035 OBD_INCOMPAT_MULTI_RPCS)) {
1036 tgt->lut_lsd.lsd_feature_incompat |=
1037 OBD_INCOMPAT_MULTI_RPCS;
1038 rc = tgt_server_data_update(env, tgt, 1);
1040 CERROR("%s: unable to set MULTI RPCS "
1041 "incompatibility flag\n",
1042 exp->exp_obd->obd_name);
1047 /* assign client slot generation */
1048 ted->ted_lcd->lcd_generation =
1049 atomic_inc_return(&tgt->lut_client_generation);
1051 ted->ted_lcd->lcd_generation = 0;
1054 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
1056 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1057 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
1059 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
1062 rc = tgt_client_data_update(env, exp);
1064 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
1065 tgt->lut_obd->obd_name, idx, rc);
1069 EXPORT_SYMBOL(tgt_client_new);
1071 /* Add an existing client to the MDS in-memory state based on
1072 * a client that was previously found in the last_rcvd file and
1073 * already has an assigned slot (idx >= 0).
1075 * It should not be possible to fail adding an existing client - otherwise
1076 * mdt_init_server_data() callsite needs to be fixed.
1078 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
1080 struct tg_export_data *ted = &exp->exp_target_data;
1081 struct lu_target *tgt = class_exp2tgt(exp);
1085 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1086 LASSERTF(idx >= 0, "%d\n", idx);
1088 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1089 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1092 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1093 CERROR("%s: client %d: bit already set in bitmap!!\n",
1094 tgt->lut_obd->obd_name, idx);
1097 atomic_inc(&tgt->lut_num_clients);
1099 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1101 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1102 ted->ted_lcd->lcd_generation);
1104 ted->ted_lr_idx = idx;
1105 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1106 idx * tgt->lut_lsd.lsd_client_size;
1108 mutex_init(&ted->ted_lcd_lock);
1110 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1115 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1117 struct tg_export_data *ted = &exp->exp_target_data;
1118 struct lu_target *tgt = class_exp2tgt(exp);
1123 LASSERT(ted->ted_lcd);
1125 if (unlikely(tgt == NULL)) {
1126 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1127 class_exp2obd(exp)->obd_name);
1131 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1132 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1133 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1134 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT ||
1135 exp->exp_no_recovery)
1138 /* Slot may be not yet assigned, use case is race between Client
1139 * reconnect and forced eviction */
1140 if (ted->ted_lr_idx < 0) {
1141 CWARN("%s: client with UUID '%s' not in bitmap\n",
1142 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1146 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1147 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1148 ted->ted_lcd->lcd_uuid);
1150 /* Clear the bit _after_ zeroing out the client so we don't
1151 race with filter_client_add and zero out new clients.*/
1152 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1153 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1154 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1158 /* Do not erase record for recoverable client. */
1159 if (exp->exp_flags & OBD_OPT_FAILOVER)
1162 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1165 /* Make sure the server's last_transno is up to date.
1166 * This should be done before zeroing client slot so last_transno will
1167 * be in server data or in client data in case of failure */
1168 rc = tgt_server_data_update(env, tgt, 0);
1170 CERROR("%s: failed to update server data, skip client %s "
1171 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1172 ted->ted_lcd->lcd_uuid, rc);
1176 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1177 rc = tgt_client_data_update(env, exp);
1179 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1180 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1181 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1182 ted->ted_lr_idx, ted->ted_lr_off, rc);
1185 EXPORT_SYMBOL(tgt_client_del);
1187 static void tgt_clean_by_tag(struct obd_export *exp, __u64 xid, __u16 tag)
1189 struct tg_export_data *ted = &exp->exp_target_data;
1190 struct lu_target *lut = class_exp2tgt(exp);
1191 struct tg_reply_data *trd, *tmp;
1196 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
1197 if (trd->trd_tag != tag)
1200 LASSERT(ergo(tgt_is_increasing_xid_client(exp),
1201 trd->trd_reply.lrd_xid <= xid));
1203 ted->ted_release_tag++;
1204 tgt_release_reply_data(lut, ted, trd);
1208 static int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1209 struct tg_export_data *ted, struct tg_reply_data *trd,
1210 struct ptlrpc_request *req,
1211 struct thandle *th, bool update_lrd_file)
1213 struct lsd_reply_data *lrd;
1217 lrd = &trd->trd_reply;
1218 /* update export last transno */
1219 mutex_lock(&ted->ted_lcd_lock);
1220 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1221 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1222 mutex_unlock(&ted->ted_lcd_lock);
1225 /* find a empty slot */
1226 i = tgt_find_free_reply_slot(tgt);
1227 if (unlikely(i < 0)) {
1228 CERROR("%s: couldn't find a slot for reply data: "
1229 "rc = %d\n", tgt_name(tgt), i);
1234 if (update_lrd_file) {
1237 /* write reply data to disk */
1238 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1239 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1240 if (unlikely(rc != 0)) {
1241 CERROR("%s: can't update %s file: rc = %d\n",
1242 tgt_name(tgt), REPLY_DATA, rc);
1243 GOTO(free_slot, rc);
1247 trd->trd_index = TRD_INDEX_MEMORY;
1250 /* add reply data to target export's reply list */
1251 mutex_lock(&ted->ted_lcd_lock);
1253 int exclude = tgt_is_increasing_xid_client(req->rq_export) ?
1254 MSG_REPLAY : MSG_REPLAY|MSG_RESENT;
1256 if (req->rq_obsolete) {
1258 "drop reply data update for obsolete req xid=%llu,"
1259 "transno=%llu, tag=%hu\n", req->rq_xid,
1260 lrd->lrd_transno, trd->trd_tag);
1261 mutex_unlock(&ted->ted_lcd_lock);
1262 GOTO(free_slot, rc = -EBADR);
1265 if (!(lustre_msg_get_flags(req->rq_reqmsg) & exclude))
1266 tgt_clean_by_tag(req->rq_export, req->rq_xid,
1269 list_add(&trd->trd_list, &ted->ted_reply_list);
1270 ted->ted_reply_cnt++;
1271 if (ted->ted_reply_cnt > ted->ted_reply_max)
1272 ted->ted_reply_max = ted->ted_reply_cnt;
1273 mutex_unlock(&ted->ted_lcd_lock);
1275 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1276 "tag %hu, client gen %u, slot idx %d\n",
1277 trd, lrd->lrd_xid, lrd->lrd_transno,
1278 trd->trd_tag, lrd->lrd_client_gen, trd->trd_index);
1284 tgt_clear_reply_slot(tgt, trd->trd_index);
1288 int tgt_mk_reply_data(const struct lu_env *env,
1289 struct lu_target *tgt,
1290 struct tg_export_data *ted,
1291 struct ptlrpc_request *req,
1297 struct tg_reply_data *trd;
1298 struct lsd_reply_data *lrd;
1299 __u64 *pre_versions = NULL;
1301 struct tgt_session_info *tsi = NULL;
1304 if (unlikely(trd == NULL))
1308 tsi = tgt_ses_info(env);
1310 /* fill reply data information */
1311 lrd = &trd->trd_reply;
1312 lrd->lrd_transno = transno;
1314 lrd->lrd_xid = req->rq_xid;
1315 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1316 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1318 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1319 lrd->lrd_result = th->th_result;
1322 LASSERT(env != NULL);
1323 LASSERT(tsi->tsi_xid != 0);
1325 lrd->lrd_xid = tsi->tsi_xid;
1326 lrd->lrd_result = tsi->tsi_result;
1327 lrd->lrd_client_gen = tsi->tsi_client_gen;
1330 lrd->lrd_data = opdata;
1332 trd->trd_pre_versions[0] = pre_versions[0];
1333 trd->trd_pre_versions[1] = pre_versions[1];
1334 trd->trd_pre_versions[2] = pre_versions[2];
1335 trd->trd_pre_versions[3] = pre_versions[3];
1338 if (tsi && tsi->tsi_open_obj)
1339 trd->trd_object = *lu_object_fid(&tsi->tsi_open_obj->do_lu);
1341 rc = tgt_add_reply_data(env, tgt, ted, trd, req,
1351 EXPORT_SYMBOL(tgt_mk_reply_data);
1354 * last_rcvd & last_committed update callbacks
1356 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1357 struct dt_object *obj, __u64 opdata,
1358 struct thandle *th, struct ptlrpc_request *req)
1360 struct tgt_thread_info *tti = tgt_th_info(env);
1361 struct tgt_session_info *tsi = tgt_ses_info(env);
1362 struct obd_export *exp = tsi->tsi_exp;
1363 struct tg_export_data *ted;
1371 LASSERT(exp != NULL);
1372 ted = &exp->exp_target_data;
1374 /* Some clients don't support recovery, and they don't have last_rcvd
1376 * 1. lightweight clients.
1377 * 2. local clients on MDS which doesn't enable "localrecov".
1378 * 3. OFD connect may cause transaction before export has last_rcvd
1381 if (ted->ted_lr_idx < 0)
1385 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1387 /* From update replay, tti_transno should be set already */
1388 LASSERT(tti->tti_transno != 0);
1390 spin_lock(&tgt->lut_translock);
1391 if (th->th_result != 0) {
1392 if (tti->tti_transno != 0) {
1393 CERROR("%s: replay transno %llu failed: rc = %d\n",
1394 tgt_name(tgt), tti->tti_transno, th->th_result);
1396 } else if (tti->tti_transno == 0) {
1397 tti->tti_transno = ++tgt->lut_last_transno;
1399 /* should be replay */
1400 if (tti->tti_transno > tgt->lut_last_transno)
1401 tgt->lut_last_transno = tti->tti_transno;
1403 spin_unlock(&tgt->lut_translock);
1405 /** VBR: set new versions */
1406 if (th->th_result == 0 && obj != NULL) {
1407 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1408 dt_version_set(env, dto, tti->tti_transno, th);
1411 /* filling reply data */
1412 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1413 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1416 req->rq_transno = tti->tti_transno;
1417 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1420 /* if can't add callback, do sync write */
1421 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1424 /* store transno in the last_rcvd header */
1425 spin_lock(&tgt->lut_translock);
1426 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1427 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1428 spin_unlock(&tgt->lut_translock);
1429 /* Although current connection doesn't have slot
1430 * in the last_rcvd, we still want to maintain
1431 * the in-memory lsd_client_data structure in order to
1432 * properly handle reply reconstruction. */
1433 rc = tgt_server_data_write(env, tgt, th);
1435 spin_unlock(&tgt->lut_translock);
1437 } else if (ted->ted_lr_off == 0) {
1438 CERROR("%s: client idx %d has offset %lld\n",
1439 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1443 /* Target that supports multiple reply data */
1444 if (tgt_is_multimodrpcs_client(exp)) {
1445 return tgt_mk_reply_data(env, tgt, ted, req, opdata, th,
1446 !!(req != NULL), tti->tti_transno);
1449 /* Enough for update replay, let's return */
1453 mutex_lock(&ted->ted_lcd_lock);
1454 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1455 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1456 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1457 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1458 ted->ted_lcd->lcd_last_close_result = th->th_result;
1460 /* VBR: save versions in last_rcvd for reconstruct. */
1461 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1464 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1465 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1466 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1467 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1469 transno_p = &ted->ted_lcd->lcd_last_transno;
1470 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1471 ted->ted_lcd->lcd_last_result = th->th_result;
1472 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1473 * see struct ldlm_reply->lock_policy_res1; */
1474 ted->ted_lcd->lcd_last_data = opdata;
1477 /* Update transno in slot only if non-zero number, i.e. no errors */
1478 if (likely(tti->tti_transno != 0)) {
1479 /* Don't overwrite bigger transaction number with lower one.
1480 * That is not sign of problem in all cases, but in any case
1481 * this value should be monotonically increased only. */
1482 if (*transno_p > tti->tti_transno) {
1483 if (!tgt->lut_no_reconstruct) {
1484 CERROR("%s: trying to overwrite bigger transno:"
1485 "on-disk: %llu, new: %llu replay: "
1486 "%d. See LU-617.\n", tgt_name(tgt),
1487 *transno_p, tti->tti_transno,
1488 req_is_replay(req));
1489 if (req_is_replay(req)) {
1490 spin_lock(&req->rq_export->exp_lock);
1491 req->rq_export->exp_vbr_failed = 1;
1492 spin_unlock(&req->rq_export->exp_lock);
1494 mutex_unlock(&ted->ted_lcd_lock);
1495 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1498 *transno_p = tti->tti_transno;
1503 tti->tti_off = ted->ted_lr_off;
1504 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1507 rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1510 mutex_unlock(&ted->ted_lcd_lock);
1514 mutex_unlock(&ted->ted_lcd_lock);
1519 * last_rcvd update for echo client simulation.
1520 * It updates last_rcvd client slot and version of object in
1521 * simple way but with all locks to simulate all drawbacks
1523 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1524 struct lu_target *tgt,
1525 struct dt_object *obj,
1527 struct obd_export *exp)
1529 struct tgt_thread_info *tti = tgt_th_info(env);
1530 struct tg_export_data *ted = &exp->exp_target_data;
1535 tti->tti_transno = 0;
1537 spin_lock(&tgt->lut_translock);
1538 if (th->th_result == 0)
1539 tti->tti_transno = ++tgt->lut_last_transno;
1540 spin_unlock(&tgt->lut_translock);
1542 /** VBR: set new versions */
1543 if (th->th_result == 0 && obj != NULL)
1544 dt_version_set(env, obj, tti->tti_transno, th);
1546 /* if can't add callback, do sync write */
1547 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1550 LASSERT(ted->ted_lr_off > 0);
1552 mutex_lock(&ted->ted_lcd_lock);
1553 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1554 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1555 ted->ted_lcd->lcd_last_result = th->th_result;
1557 tti->tti_off = ted->ted_lr_off;
1558 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1559 mutex_unlock(&ted->ted_lcd_lock);
1563 static int tgt_clients_data_init(const struct lu_env *env,
1564 struct lu_target *tgt,
1565 unsigned long last_size)
1567 struct obd_device *obd = tgt->lut_obd;
1568 struct lr_server_data *lsd = &tgt->lut_lsd;
1569 struct lsd_client_data *lcd = NULL;
1570 struct tg_export_data *ted;
1573 loff_t off = lsd->lsd_client_start;
1574 __u32 generation = 0;
1575 struct cfs_hash *hash = NULL;
1579 if (tgt->lut_bottom->dd_rdonly)
1582 BUILD_BUG_ON(offsetof(struct lsd_client_data, lcd_padding) +
1583 sizeof(lcd->lcd_padding) != LR_CLIENT_SIZE);
1589 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1591 GOTO(err_out, rc = -ENODEV);
1593 for (cl_idx = 0; off < last_size; cl_idx++) {
1594 struct obd_export *exp;
1597 /* Don't assume off is incremented properly by
1598 * read_record(), in case sizeof(*lcd)
1599 * isn't the same as fsd->lsd_client_size. */
1600 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1601 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1603 CERROR("%s: error reading last_rcvd %s idx %d off "
1604 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1607 break; /* read error shouldn't cause startup to fail */
1610 if (lcd->lcd_uuid[0] == '\0') {
1611 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1616 last_transno = lcd_last_transno(lcd);
1618 /* These exports are cleaned up by disconnect, so they
1619 * need to be set up like real exports as connect does.
1621 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1622 " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1623 cl_idx, last_transno, lsd->lsd_last_transno,
1624 lcd_last_xid(lcd), lcd->lcd_generation);
1626 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1628 if (PTR_ERR(exp) == -EALREADY) {
1629 /* export already exists, zero out this one */
1630 CERROR("%s: Duplicate export %s!\n",
1631 tgt_name(tgt), lcd->lcd_uuid);
1634 GOTO(err_out, rc = PTR_ERR(exp));
1637 ted = &exp->exp_target_data;
1638 *ted->ted_lcd = *lcd;
1640 rc = tgt_client_add(env, exp, cl_idx);
1641 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1642 /* VBR: set export last committed version */
1643 exp->exp_last_committed = last_transno;
1644 spin_lock(&exp->exp_lock);
1645 exp->exp_connecting = 0;
1646 exp->exp_in_recovery = 0;
1647 spin_unlock(&exp->exp_lock);
1648 atomic_inc(&obd->obd_max_recoverable_clients);
1650 if (tgt->lut_lsd.lsd_feature_incompat &
1651 OBD_INCOMPAT_MULTI_RPCS &&
1652 lcd->lcd_generation != 0) {
1653 /* compute the highest valid client generation */
1654 generation = max(generation, lcd->lcd_generation);
1655 /* fill client_generation <-> export hash table */
1656 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1657 &exp->exp_gen_hash);
1659 CERROR("%s: duplicate export for client "
1661 tgt_name(tgt), lcd->lcd_generation);
1662 class_export_put(exp);
1667 class_export_put(exp);
1669 rc = rev_import_init(exp);
1671 class_unlink_export(exp);
1675 /* Need to check last_rcvd even for duplicated exports. */
1676 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1677 cl_idx, last_transno);
1679 spin_lock(&tgt->lut_translock);
1680 tgt->lut_last_transno = max(last_transno,
1681 tgt->lut_last_transno);
1682 spin_unlock(&tgt->lut_translock);
1685 /* record highest valid client generation */
1686 atomic_set(&tgt->lut_client_generation, generation);
1690 cfs_hash_putref(hash);
1695 struct server_compat_data {
1702 static struct server_compat_data tgt_scd[] = {
1703 [LDD_F_SV_TYPE_MDT] = {
1704 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1705 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1706 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1707 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1708 OBD_INCOMPAT_MULTI_RPCS,
1709 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1710 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1711 OBD_INCOMPAT_MULTI_OI,
1713 [LDD_F_SV_TYPE_OST] = {
1714 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1715 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1717 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1718 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1722 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1724 struct tgt_thread_info *tti = tgt_th_info(env);
1725 struct lr_server_data *lsd = &tgt->lut_lsd;
1726 unsigned long last_rcvd_size;
1730 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1734 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1736 /* ensure padding in the struct is the correct size */
1737 BUILD_BUG_ON(offsetof(struct lr_server_data, lsd_padding) +
1738 sizeof(lsd->lsd_padding) != LR_SERVER_SIZE);
1740 rc = server_name2index(tgt_name(tgt), &index, NULL);
1742 CERROR("%s: Can not get index from name: rc = %d\n",
1746 /* server_name2index() returns type */
1748 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1749 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1753 /* last_rcvd on OST doesn't provide reconstruct support because there
1754 * may be up to 8 in-flight write requests per single slot in
1755 * last_rcvd client data
1757 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1759 if (last_rcvd_size == 0) {
1760 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1762 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1763 sizeof(lsd->lsd_uuid));
1764 lsd->lsd_last_transno = 0;
1765 lsd->lsd_mount_count = 0;
1766 lsd->lsd_server_size = LR_SERVER_SIZE;
1767 lsd->lsd_client_start = LR_CLIENT_START;
1768 lsd->lsd_client_size = LR_CLIENT_SIZE;
1769 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1770 lsd->lsd_osd_index = index;
1771 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1772 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1774 rc = tgt_server_data_read(env, tgt);
1776 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1780 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1781 if (tgt->lut_bottom->dd_rdonly) {
1782 /* Such difference may be caused by mounting
1783 * up snapshot with new fsname under rd_only
1784 * mode. But even if it was NOT, it will not
1785 * damage the system because of "rd_only". */
1786 memcpy(lsd->lsd_uuid,
1787 tgt->lut_obd->obd_uuid.uuid,
1788 sizeof(lsd->lsd_uuid));
1790 LCONSOLE_ERROR_MSG(0x157, "Trying to start "
1791 "OBD %s using the wrong "
1792 "disk %s. Were the /dev/ "
1793 "assignments rearranged?\n",
1794 tgt->lut_obd->obd_uuid.uuid,
1800 if (lsd->lsd_osd_index != index) {
1801 LCONSOLE_ERROR_MSG(0x157,
1802 "%s: index %d in last rcvd is different with the index %d in config log, It might be disk corruption!\n",
1804 lsd->lsd_osd_index, index);
1809 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1810 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1812 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1816 if (type == LDD_F_SV_TYPE_MDT)
1817 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1819 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1820 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1822 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1825 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1826 if (type == LDD_F_SV_TYPE_MDT &&
1827 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1828 if (last_rcvd_size > lsd->lsd_client_start) {
1829 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1830 "remove all clients for interop needs\n",
1832 rc = tgt_truncate_last_rcvd(env, tgt,
1833 lsd->lsd_client_start);
1836 last_rcvd_size = lsd->lsd_client_start;
1838 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1839 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1842 spin_lock(&tgt->lut_translock);
1843 tgt->lut_last_transno = lsd->lsd_last_transno;
1844 spin_unlock(&tgt->lut_translock);
1846 lsd->lsd_mount_count++;
1848 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1849 CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1850 tgt_name(tgt), tgt->lut_last_transno);
1851 CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1852 tgt_name(tgt), lsd->lsd_mount_count);
1853 CDEBUG(D_INODE, "%s: server data size: %u\n",
1854 tgt_name(tgt), lsd->lsd_server_size);
1855 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1856 tgt_name(tgt), lsd->lsd_client_start);
1857 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1858 tgt_name(tgt), lsd->lsd_client_size);
1859 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1860 tgt_name(tgt), last_rcvd_size);
1861 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1862 tgt_name(tgt), lsd->lsd_subdir_count);
1863 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1864 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1865 (last_rcvd_size - lsd->lsd_client_start) /
1866 lsd->lsd_client_size);
1867 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1869 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1870 lsd->lsd_client_size == 0) {
1871 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1875 if (!tgt->lut_obd->obd_replayable)
1876 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1878 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1880 GOTO(err_client, rc);
1882 spin_lock(&tgt->lut_translock);
1883 /* obd_last_committed is used for compatibility
1884 * with other lustre recovery code */
1885 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1886 spin_unlock(&tgt->lut_translock);
1888 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1889 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1891 /* save it, so mount count and last_transno is current */
1892 rc = tgt_server_data_update(env, tgt, 0);
1894 GOTO(err_client, rc);
1899 class_disconnect_exports(tgt->lut_obd);
1903 /* add credits for last_rcvd update */
1904 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1907 struct lu_target *tgt = cookie;
1908 struct tgt_session_info *tsi;
1909 struct tgt_thread_info *tti = tgt_th_info(env);
1910 struct dt_object *dto;
1913 /* For readonly case, the caller should have got failure
1914 * when start the transaction. If the logic comes here,
1915 * there must be something wrong. */
1916 if (unlikely(tgt->lut_bottom->dd_rdonly)) {
1921 /* if there is no session, then this transaction is not result of
1922 * request processing but some local operation */
1923 if (env->le_ses == NULL)
1926 LASSERT(tgt->lut_last_rcvd);
1927 tsi = tgt_ses_info(env);
1928 /* OFD may start transaction without export assigned */
1929 if (tsi->tsi_exp == NULL)
1932 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1934 * Use maximum possible file offset for declaration to ensure
1935 * ZFS will reserve enough credits for a write anywhere in this
1936 * file, since we don't know where in the file the write will be
1937 * because a replay slot has not been assigned. This should be
1938 * replaced by dmu_tx_hold_append() when available.
1940 tti->tti_buf.lb_buf = NULL;
1941 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1942 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1943 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1947 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1949 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1950 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1956 if (tsi->tsi_vbr_obj != NULL &&
1957 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1958 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1959 rc = dt_declare_version_set(env, dto, th);
1965 /* Update last_rcvd records with latests transaction data */
1966 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1969 struct lu_target *tgt = cookie;
1970 struct tgt_session_info *tsi;
1971 struct tgt_thread_info *tti = tgt_th_info(env);
1972 struct dt_object *obj = NULL;
1976 if (env->le_ses == NULL)
1979 tsi = tgt_ses_info(env);
1980 /* OFD may start transaction without export assigned */
1981 if (tsi->tsi_exp == NULL)
1984 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1986 if (tti->tti_has_trans && !echo_client) {
1987 if (tti->tti_mult_trans == 0) {
1988 CDEBUG(D_HA, "More than one transaction %llu\n",
1992 /* we need another transno to be assigned */
1993 tti->tti_transno = 0;
1994 } else if (th->th_result == 0) {
1995 tti->tti_has_trans = 1;
1998 if (tsi->tsi_vbr_obj != NULL &&
1999 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
2000 obj = tsi->tsi_vbr_obj;
2003 if (unlikely(echo_client)) /* echo client special case */
2004 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
2007 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
2012 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
2014 struct tgt_thread_info *tti = tgt_th_info(env);
2015 struct lsd_reply_data *lrd = &tti->tti_lrd;
2016 unsigned long reply_data_size;
2018 struct lsd_reply_header *lrh = NULL;
2019 struct tg_reply_data *trd = NULL;
2022 struct cfs_hash *hash = NULL;
2023 struct obd_export *exp;
2024 struct tg_export_data *ted;
2025 int reply_data_recovered = 0;
2027 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
2030 reply_data_size = (unsigned long)tti->tti_attr.la_size;
2034 GOTO(out, rc = -ENOMEM);
2036 if (reply_data_size == 0) {
2037 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
2039 lrh->lrh_magic = LRH_MAGIC;
2040 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
2041 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
2042 rc = tgt_reply_header_write(env, tgt, lrh);
2044 CERROR("%s: error writing %s: rc = %d\n",
2045 tgt_name(tgt), REPLY_DATA, rc);
2049 rc = tgt_reply_header_read(env, tgt, lrh);
2051 CERROR("%s: error reading %s: rc = %d\n",
2052 tgt_name(tgt), REPLY_DATA, rc);
2055 if (lrh->lrh_magic != LRH_MAGIC ||
2056 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
2057 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
2058 CERROR("%s: invalid header in %s\n",
2059 tgt_name(tgt), REPLY_DATA);
2060 GOTO(out, rc = -EINVAL);
2063 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
2065 GOTO(out, rc = -ENODEV);
2069 GOTO(out, rc = -ENOMEM);
2071 /* Load reply_data from disk */
2072 for (idx = 0, off = sizeof(struct lsd_reply_header);
2073 off < reply_data_size;
2074 idx++, off += sizeof(struct lsd_reply_data)) {
2075 rc = tgt_reply_data_read(env, tgt, lrd, off);
2077 CERROR("%s: error reading %s: rc = %d\n",
2078 tgt_name(tgt), REPLY_DATA, rc);
2082 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
2084 /* old reply data from a disconnected client */
2087 ted = &exp->exp_target_data;
2088 mutex_lock(&ted->ted_lcd_lock);
2090 /* create in-memory reply_data and link it to
2091 * target export's reply list */
2092 rc = tgt_set_reply_slot(tgt, idx);
2094 mutex_unlock(&ted->ted_lcd_lock);
2097 trd->trd_reply = *lrd;
2098 trd->trd_pre_versions[0] = 0;
2099 trd->trd_pre_versions[1] = 0;
2100 trd->trd_pre_versions[2] = 0;
2101 trd->trd_pre_versions[3] = 0;
2102 trd->trd_index = idx;
2104 fid_zero(&trd->trd_object);
2105 list_add(&trd->trd_list, &ted->ted_reply_list);
2106 ted->ted_reply_cnt++;
2107 if (ted->ted_reply_cnt > ted->ted_reply_max)
2108 ted->ted_reply_max = ted->ted_reply_cnt;
2110 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
2111 "transno %llu, client gen %u, slot idx %d\n",
2112 tgt_name(tgt), trd, lrd->lrd_xid,
2113 lrd->lrd_transno, lrd->lrd_client_gen,
2116 /* update export last committed transation */
2117 exp->exp_last_committed = max(exp->exp_last_committed,
2119 /* Update lcd_last_transno as well for check in
2120 * tgt_release_reply_data() or the latest client
2121 * transno can be lost.
2123 ted->ted_lcd->lcd_last_transno =
2124 max(ted->ted_lcd->lcd_last_transno,
2125 exp->exp_last_committed);
2127 mutex_unlock(&ted->ted_lcd_lock);
2128 class_export_put(exp);
2130 /* update target last committed transaction */
2131 spin_lock(&tgt->lut_translock);
2132 tgt->lut_last_transno = max(tgt->lut_last_transno,
2134 spin_unlock(&tgt->lut_translock);
2136 reply_data_recovered++;
2140 GOTO(out, rc = -ENOMEM);
2142 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
2143 tgt_name(tgt), reply_data_recovered);
2146 spin_lock(&tgt->lut_translock);
2147 /* obd_last_committed is used for compatibility
2148 * with other lustre recovery code */
2149 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
2150 spin_unlock(&tgt->lut_translock);
2156 cfs_hash_putref(hash);
2164 static int tgt_check_lookup_req(struct ptlrpc_request *req, int lookup,
2165 struct tg_reply_data *trd)
2167 struct tg_export_data *ted = &req->rq_export->exp_target_data;
2168 struct lu_target *lut = class_exp2tgt(req->rq_export);
2169 __u16 tag = lustre_msg_get_tag(req->rq_reqmsg);
2171 struct tg_reply_data *reply;
2172 bool check_increasing;
2177 check_increasing = tgt_is_increasing_xid_client(req->rq_export) &&
2178 !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY);
2179 if (!lookup && !check_increasing)
2182 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
2183 if (lookup && reply->trd_reply.lrd_xid == req->rq_xid) {
2188 } else if (check_increasing && reply->trd_tag == tag &&
2189 reply->trd_reply.lrd_xid > req->rq_xid) {
2191 CERROR("%s: busy tag=%u req_xid=%llu, trd=%p: xid=%llu transno=%llu client_gen=%u slot_idx=%d: rc = %d\n",
2192 tgt_name(lut), tag, req->rq_xid, trd,
2193 reply->trd_reply.lrd_xid,
2194 reply->trd_reply.lrd_transno,
2195 reply->trd_reply.lrd_client_gen,
2196 reply->trd_index, rc);
2204 /* Look for a reply data matching specified request @req
2205 * A copy is returned in @trd if the pointer is not NULL
2207 int tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2209 struct tg_export_data *ted = &req->rq_export->exp_target_data;
2211 bool not_replay = !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY);
2213 mutex_lock(&ted->ted_lcd_lock);
2214 if (not_replay && req->rq_xid <= req->rq_export->exp_last_xid) {
2215 /* A check for the last_xid is needed here in case there is
2216 * no reply data is left in the list. It may happen if another
2217 * RPC on another slot increased the last_xid between our
2218 * process_req_last_xid & tgt_lookup_reply calls */
2221 found = tgt_check_lookup_req(req, 1, trd);
2223 mutex_unlock(&ted->ted_lcd_lock);
2225 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d last_xid %llu\n",
2226 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid, found,
2227 req->rq_export->exp_last_xid);
2231 EXPORT_SYMBOL(tgt_lookup_reply);
2233 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2235 struct tg_export_data *ted = &exp->exp_target_data;
2236 struct lu_target *lut = class_exp2tgt(exp);
2237 struct tg_reply_data *trd, *tmp;
2240 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2241 if (trd->trd_reply.lrd_xid > rcvd_xid)
2243 ted->ted_release_xid++;
2244 tgt_release_reply_data(lut, ted, trd);
2250 int tgt_handle_tag(struct ptlrpc_request *req)
2252 return tgt_check_lookup_req(req, 0, NULL);