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, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Lustre Unified Target
33 * These are common function to work with last_received file
35 * Author: Mikhail Pershin <mike.pershin@intel.com>
38 #include <obd_class.h>
39 #include <lustre_fid.h>
41 #include "tgt_internal.h"
44 /* Allocate a bitmap for a chunk of reply data slots */
45 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
49 OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
54 spin_lock(&lut->lut_client_bitmap_lock);
56 if (lut->lut_reply_bitmap[chunk] != NULL) {
57 /* someone else already allocated the bitmap for this chunk */
58 spin_unlock(&lut->lut_client_bitmap_lock);
59 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
64 lut->lut_reply_bitmap[chunk] = bm;
66 spin_unlock(&lut->lut_client_bitmap_lock);
71 /* Look for an available reply data slot in the bitmap
73 * Allocate bitmap chunk when first used
74 * XXX algo could be improved if this routine limits performance
76 static int tgt_find_free_reply_slot(struct lu_target *lut)
83 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
84 /* allocate the bitmap chunk if necessary */
85 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
86 rc = tgt_bitmap_chunk_alloc(lut, chunk);
90 bmp = lut->lut_reply_bitmap[chunk];
92 /* look for an available slot in this chunk */
94 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
95 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
99 if (test_and_set_bit(b, bmp) == 0)
100 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
107 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
109 * Allocate the bitmap chunk if necessary
111 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
117 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
118 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
120 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
121 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
123 /* allocate the bitmap chunk if necessary */
124 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
125 rc = tgt_bitmap_chunk_alloc(lut, chunk);
130 /* mark the slot 'used' in this chunk */
131 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
132 CERROR("%s: slot %d already set in bitmap\n",
141 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
144 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
149 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
150 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
152 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
153 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
155 if (lut->lut_reply_bitmap[chunk] == NULL) {
156 CERROR("%s: slot %d not allocated\n",
161 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
162 CERROR("%s: slot %d already clear in bitmap\n",
171 /* Read header of reply_data file of target @tgt into structure @lrh */
172 static int tgt_reply_header_read(const struct lu_env *env,
173 struct lu_target *tgt,
174 struct lsd_reply_header *lrh)
177 struct lsd_reply_header buf;
178 struct tgt_thread_info *tti = tgt_th_info(env);
181 tti->tti_buf.lb_buf = &buf;
182 tti->tti_buf.lb_len = sizeof(buf);
184 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
189 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
190 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
191 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
193 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
194 "header_size=%d reply_size=%d\n",
195 tgt->lut_obd->obd_name, REPLY_DATA,
196 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
201 /* Write header into replay_data file of target @tgt from structure @lrh */
202 static int tgt_reply_header_write(const struct lu_env *env,
203 struct lu_target *tgt,
204 struct lsd_reply_header *lrh)
207 struct lsd_reply_header buf;
208 struct tgt_thread_info *tti = tgt_th_info(env);
210 struct dt_object *dto;
212 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
213 "header_size=%d reply_size=%d\n",
214 tgt->lut_obd->obd_name, REPLY_DATA,
215 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
217 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
218 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
219 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
221 th = dt_trans_create(env, tgt->lut_bottom);
227 tti->tti_buf.lb_buf = &buf;
228 tti->tti_buf.lb_len = sizeof(buf);
230 rc = dt_declare_record_write(env, tgt->lut_reply_data,
231 &tti->tti_buf, tti->tti_off, th);
235 rc = dt_trans_start(env, tgt->lut_bottom, th);
239 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
240 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
242 dt_trans_stop(env, tgt->lut_bottom, th);
246 /* Write the reply data @lrd into reply_data file of target @tgt
249 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
250 struct lsd_reply_data *lrd, loff_t off,
253 struct tgt_thread_info *tti = tgt_th_info(env);
254 struct dt_object *dto;
255 struct lsd_reply_data *buf = &tti->tti_lrd;
257 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
259 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
260 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
261 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
262 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
263 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
265 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
268 tti->tti_buf.lb_buf = buf;
269 tti->tti_buf.lb_len = sizeof(*buf);
271 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
272 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
275 /* Read the reply data from reply_data file of target @tgt at offset @off
276 * into structure @lrd
278 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
279 struct lsd_reply_data *lrd, loff_t off)
282 struct tgt_thread_info *tti = tgt_th_info(env);
283 struct lsd_reply_data *buf = &tti->tti_lrd;
286 tti->tti_buf.lb_buf = buf;
287 tti->tti_buf.lb_len = sizeof(*buf);
289 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
294 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
295 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
296 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
297 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
298 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
304 /* Free the in-memory reply data structure @trd and release
305 * the corresponding slot in the reply_data file of target @lut
306 * Called with ted_lcd_lock held
308 static void tgt_free_reply_data(struct lu_target *lut,
309 struct tg_export_data *ted,
310 struct tg_reply_data *trd)
312 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
313 "client gen %u, slot idx %d\n",
314 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
315 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
318 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
320 list_del(&trd->trd_list);
321 ted->ted_reply_cnt--;
323 tgt_clear_reply_slot(lut, trd->trd_index);
327 /* Release the reply data @trd from target @lut
328 * The reply data with the highest transno for this export
329 * is retained to ensure correctness of target recovery
330 * Called with ted_lcd_lock held
332 static void tgt_release_reply_data(struct lu_target *lut,
333 struct tg_export_data *ted,
334 struct tg_reply_data *trd)
336 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
337 "client gen %u, slot idx %d\n",
338 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
339 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
342 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
344 /* Do not free the reply data corresponding to the
345 * highest transno of this export.
346 * This ensures on-disk reply data is kept and
347 * last committed transno can be restored from disk in case
350 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
351 /* free previous retained reply */
352 if (ted->ted_reply_last != NULL)
353 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
354 /* retain the reply */
355 list_del_init(&trd->trd_list);
356 ted->ted_reply_last = trd;
358 tgt_free_reply_data(lut, ted, trd);
362 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
364 tti->tti_buf.lb_buf = &tti->tti_lsd;
365 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
366 return &tti->tti_buf;
369 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
371 tti->tti_buf.lb_buf = &tti->tti_lcd;
372 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
373 return &tti->tti_buf;
377 * Allocate in-memory data for client slot related to export.
379 int tgt_client_alloc(struct obd_export *exp)
382 LASSERT(exp != exp->exp_obd->obd_self_export);
384 spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
385 INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
387 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
388 if (exp->exp_target_data.ted_lcd == NULL)
390 /* Mark that slot is not yet valid, 0 doesn't work here */
391 exp->exp_target_data.ted_lr_idx = -1;
392 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
393 mutex_init(&exp->exp_target_data.ted_lcd_lock);
396 EXPORT_SYMBOL(tgt_client_alloc);
399 * Free in-memory data for client slot related to export.
401 void tgt_client_free(struct obd_export *exp)
403 struct tg_export_data *ted = &exp->exp_target_data;
404 struct lu_target *lut = class_exp2tgt(exp);
405 struct tg_reply_data *trd, *tmp;
407 LASSERT(exp != exp->exp_obd->obd_self_export);
409 /* free reply data */
410 mutex_lock(&ted->ted_lcd_lock);
411 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
412 tgt_release_reply_data(lut, ted, trd);
414 if (ted->ted_reply_last != NULL) {
415 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
416 ted->ted_reply_last = NULL;
418 mutex_unlock(&ted->ted_lcd_lock);
420 if (!hlist_unhashed(&exp->exp_gen_hash))
421 cfs_hash_del(exp->exp_obd->obd_gen_hash,
422 &ted->ted_lcd->lcd_generation,
425 OBD_FREE_PTR(ted->ted_lcd);
428 /* Target may have been freed (see LU-7430)
429 * Slot may be not yet assigned */
430 if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
434 /* Clear bit when lcd is freed */
435 LASSERT(lut && lut->lut_client_bitmap);
436 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
437 CERROR("%s: client %u bit already clear in bitmap\n",
438 exp->exp_obd->obd_name, ted->ted_lr_idx);
442 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
443 atomic_dec(&lut->lut_num_clients);
445 EXPORT_SYMBOL(tgt_client_free);
447 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
448 struct lsd_client_data *lcd, loff_t *off, int index)
450 struct tgt_thread_info *tti = tgt_th_info(env);
454 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
456 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
457 lcd_le_to_cpu(&tti->tti_lcd, lcd);
458 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
459 lcd->lcd_last_close_result =
460 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
463 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = %llu"
464 ", last_xid = %llu, last_result = %u, last_data = %u, "
465 "last_close_transno = %llu, last_close_xid = %llu, "
466 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
467 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
468 lcd->lcd_last_result, lcd->lcd_last_data,
469 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
470 lcd->lcd_last_close_result, rc);
474 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
475 struct lsd_client_data *lcd, loff_t *off,
478 struct tgt_thread_info *tti = tgt_th_info(env);
479 struct dt_object *dto;
481 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
482 lcd->lcd_last_close_result =
483 ptlrpc_status_hton(lcd->lcd_last_close_result);
484 lcd_cpu_to_le(lcd, &tti->tti_lcd);
487 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
488 return dt_record_write(env, dto, &tti->tti_buf, off, th);
492 * Update client data in last_rcvd
494 static int tgt_client_data_update(const struct lu_env *env,
495 struct obd_export *exp)
497 struct tg_export_data *ted = &exp->exp_target_data;
498 struct lu_target *tgt = class_exp2tgt(exp);
499 struct tgt_thread_info *tti = tgt_th_info(env);
505 if (unlikely(tgt == NULL)) {
506 CDEBUG(D_ERROR, "%s: No target for connected export\n",
507 class_exp2obd(exp)->obd_name);
511 th = dt_trans_create(env, tgt->lut_bottom);
516 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
518 ted->ted_lr_off, th);
522 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
526 mutex_lock(&ted->ted_lcd_lock);
529 * Until this operations will be committed the sync is needed
530 * for this export. This should be done _after_ starting the
531 * transaction so that many connecting clients will not bring
532 * server down with lots of sync writes.
534 rc = tgt_new_client_cb_add(th, exp);
536 /* can't add callback, do sync now */
539 spin_lock(&exp->exp_lock);
540 exp->exp_need_sync = 1;
541 spin_unlock(&exp->exp_lock);
544 tti->tti_off = ted->ted_lr_off;
545 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
547 mutex_unlock(&ted->ted_lcd_lock);
551 dt_trans_stop(env, tgt->lut_bottom, th);
552 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
553 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
554 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
559 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
561 struct tgt_thread_info *tti = tgt_th_info(env);
566 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
569 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
571 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
572 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
573 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
577 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
580 struct tgt_thread_info *tti = tgt_th_info(env);
581 struct dt_object *dto;
588 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
590 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
591 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
593 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
594 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
595 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
601 * Update server data in last_rcvd
603 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
606 struct tgt_thread_info *tti = tgt_th_info(env);
613 "%s: mount_count is %llu, last_transno is %llu\n",
614 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
615 tgt->lut_last_transno);
617 /* Always save latest transno to keep it fresh */
618 spin_lock(&tgt->lut_translock);
619 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
620 spin_unlock(&tgt->lut_translock);
622 th = dt_trans_create(env, tgt->lut_bottom);
629 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
630 &tti->tti_buf, tti->tti_off, th);
634 rc = dt_trans_start(env, tgt->lut_bottom, th);
638 rc = tgt_server_data_write(env, tgt, th);
640 dt_trans_stop(env, tgt->lut_bottom, th);
642 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
643 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
644 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
647 EXPORT_SYMBOL(tgt_server_data_update);
649 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
652 struct dt_object *dt = tgt->lut_last_rcvd;
660 attr.la_valid = LA_SIZE;
662 th = dt_trans_create(env, tgt->lut_bottom);
665 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
668 rc = dt_declare_attr_set(env, dt, &attr, th);
671 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
675 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
677 rc = dt_attr_set(env, dt, &attr, th);
680 dt_trans_stop(env, tgt->lut_bottom, th);
685 static void tgt_client_epoch_update(const struct lu_env *env,
686 struct obd_export *exp)
688 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
689 struct lu_target *tgt = class_exp2tgt(exp);
691 LASSERT(tgt && tgt->lut_bottom);
692 /** VBR: set client last_epoch to current epoch */
693 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
695 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
696 tgt_client_data_update(env, exp);
700 * Update boot epoch when recovery ends
702 void tgt_boot_epoch_update(struct lu_target *tgt)
705 struct ptlrpc_request *req;
707 struct list_head client_list;
710 if (tgt->lut_obd->obd_stopping)
713 rc = lu_env_init(&env, LCT_DT_THREAD);
715 CERROR("%s: can't initialize environment: rc = %d\n",
716 tgt->lut_obd->obd_name, rc);
720 spin_lock(&tgt->lut_translock);
721 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
722 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
723 tgt->lut_lsd.lsd_start_epoch = start_epoch;
724 spin_unlock(&tgt->lut_translock);
726 INIT_LIST_HEAD(&client_list);
728 * The recovery is not yet finished and final queue can still be updated
729 * with resend requests. Move final list to separate one for processing
731 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
732 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
733 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
736 * go through list of exports participated in recovery and
737 * set new epoch for them
739 list_for_each_entry(req, &client_list, rq_list) {
740 LASSERT(!req->rq_export->exp_delayed);
741 if (!req->rq_export->exp_vbr_failed)
742 tgt_client_epoch_update(&env, req->rq_export);
744 /** return list back at once */
745 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
746 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
747 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
749 /** Clear MULTI RPCS incompatibility flag if
750 * - target is MDT and
751 * - there is no client to recover or the recovery was aborted
753 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
754 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
755 tgt->lut_obd->obd_abort_recovery))
756 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
758 /** update server epoch */
759 tgt_server_data_update(&env, tgt, 1);
764 * commit callback, need to update last_committed value
766 struct tgt_last_committed_callback {
767 struct dt_txn_commit_cb llcc_cb;
768 struct lu_target *llcc_tgt;
769 struct obd_export *llcc_exp;
773 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
774 struct dt_txn_commit_cb *cb, int err)
776 struct tgt_last_committed_callback *ccb;
778 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
780 LASSERT(ccb->llcc_exp);
781 LASSERT(ccb->llcc_tgt != NULL);
782 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
784 /* error hit, don't update last committed to provide chance to
785 * replay data after fail */
789 /* Fast path w/o spinlock, if exp_last_committed was updated
790 * with higher transno, no need to take spinlock and check,
791 * also no need to update obd_last_committed. */
792 if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
794 spin_lock(&ccb->llcc_tgt->lut_translock);
795 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
796 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
798 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
799 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
800 spin_unlock(&ccb->llcc_tgt->lut_translock);
802 ptlrpc_commit_replies(ccb->llcc_exp);
803 tgt_cancel_slc_locks(ccb->llcc_tgt, ccb->llcc_transno);
805 spin_unlock(&ccb->llcc_tgt->lut_translock);
808 CDEBUG(D_HA, "%s: transno %lld is committed\n",
809 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
812 class_export_cb_put(ccb->llcc_exp);
817 * Add commit callback function, it returns a non-zero value to inform
818 * caller to use sync transaction if necessary.
820 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
821 struct obd_export *exp, __u64 transno)
823 struct tgt_last_committed_callback *ccb;
824 struct dt_txn_commit_cb *dcb;
832 ccb->llcc_exp = class_export_cb_get(exp);
833 ccb->llcc_transno = transno;
836 dcb->dcb_func = tgt_cb_last_committed;
837 INIT_LIST_HEAD(&dcb->dcb_linkage);
838 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
840 rc = dt_trans_cb_add(th, dcb);
842 class_export_cb_put(exp);
846 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
847 /* report failure to force synchronous operation */
850 /* if exp_need_sync is set, return non-zero value to force
851 * a sync transaction. */
852 return rc ? rc : exp->exp_need_sync;
855 struct tgt_new_client_callback {
856 struct dt_txn_commit_cb lncc_cb;
857 struct obd_export *lncc_exp;
860 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
861 struct dt_txn_commit_cb *cb, int err)
863 struct tgt_new_client_callback *ccb;
865 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
867 LASSERT(ccb->lncc_exp->exp_obd);
869 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
870 ccb->lncc_exp->exp_obd->obd_name,
871 ccb->lncc_exp->exp_client_uuid.uuid);
873 spin_lock(&ccb->lncc_exp->exp_lock);
875 ccb->lncc_exp->exp_need_sync = 0;
877 spin_unlock(&ccb->lncc_exp->exp_lock);
878 class_export_cb_put(ccb->lncc_exp);
883 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
885 struct tgt_new_client_callback *ccb;
886 struct dt_txn_commit_cb *dcb;
893 ccb->lncc_exp = class_export_cb_get(exp);
896 dcb->dcb_func = tgt_cb_new_client;
897 INIT_LIST_HEAD(&dcb->dcb_linkage);
898 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
900 rc = dt_trans_cb_add(th, dcb);
902 class_export_cb_put(exp);
909 * Add new client to the last_rcvd upon new connection.
911 * We use a bitmap to locate a free space in the last_rcvd file and initialize
914 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
916 struct tg_export_data *ted = &exp->exp_target_data;
917 struct lu_target *tgt = class_exp2tgt(exp);
922 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
923 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
926 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
929 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
930 * there's no need for extra complication here
932 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
934 if (idx >= LR_MAX_CLIENTS ||
935 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
936 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
937 tgt->lut_obd->obd_name, idx);
940 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
941 idx = find_next_zero_bit(tgt->lut_client_bitmap,
942 LR_MAX_CLIENTS, idx);
946 ted->ted_lr_idx = idx;
947 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
948 idx * tgt->lut_lsd.lsd_client_size;
950 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
952 if (tgt_is_multimodrpcs_client(exp)) {
953 /* Set MULTI RPCS incompatibility flag to prevent previous
954 * Lustre versions to mount a target with reply_data file */
955 atomic_inc(&tgt->lut_num_clients);
956 if (!(tgt->lut_lsd.lsd_feature_incompat &
957 OBD_INCOMPAT_MULTI_RPCS)) {
958 tgt->lut_lsd.lsd_feature_incompat |=
959 OBD_INCOMPAT_MULTI_RPCS;
960 rc = tgt_server_data_update(env, tgt, 1);
962 CERROR("%s: unable to set MULTI RPCS "
963 "incompatibility flag\n",
964 exp->exp_obd->obd_name);
969 /* assign client slot generation */
970 ted->ted_lcd->lcd_generation =
971 atomic_inc_return(&tgt->lut_client_generation);
973 ted->ted_lcd->lcd_generation = 0;
976 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
978 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
979 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
981 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
984 rc = tgt_client_data_update(env, exp);
986 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
987 tgt->lut_obd->obd_name, idx, rc);
991 EXPORT_SYMBOL(tgt_client_new);
993 /* Add an existing client to the MDS in-memory state based on
994 * a client that was previously found in the last_rcvd file and
995 * already has an assigned slot (idx >= 0).
997 * It should not be possible to fail adding an existing client - otherwise
998 * mdt_init_server_data() callsite needs to be fixed.
1000 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
1002 struct tg_export_data *ted = &exp->exp_target_data;
1003 struct lu_target *tgt = class_exp2tgt(exp);
1007 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1008 LASSERTF(idx >= 0, "%d\n", idx);
1010 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1011 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1014 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1015 CERROR("%s: client %d: bit already set in bitmap!!\n",
1016 tgt->lut_obd->obd_name, idx);
1019 atomic_inc(&tgt->lut_num_clients);
1021 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1023 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1024 ted->ted_lcd->lcd_generation);
1026 ted->ted_lr_idx = idx;
1027 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1028 idx * tgt->lut_lsd.lsd_client_size;
1030 mutex_init(&ted->ted_lcd_lock);
1032 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1037 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1039 struct tg_export_data *ted = &exp->exp_target_data;
1040 struct lu_target *tgt = class_exp2tgt(exp);
1045 LASSERT(ted->ted_lcd);
1047 if (unlikely(tgt == NULL)) {
1048 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1049 class_exp2obd(exp)->obd_name);
1053 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1054 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1055 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1056 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1059 /* Slot may be not yet assigned, use case is race between Client
1060 * reconnect and forced eviction */
1061 if (ted->ted_lr_idx < 0) {
1062 CWARN("%s: client with UUID '%s' not in bitmap\n",
1063 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1067 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1068 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1069 ted->ted_lcd->lcd_uuid);
1071 /* Clear the bit _after_ zeroing out the client so we don't
1072 race with filter_client_add and zero out new clients.*/
1073 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1074 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1075 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1079 /* Do not erase record for recoverable client. */
1080 if (exp->exp_flags & OBD_OPT_FAILOVER)
1083 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1086 /* Make sure the server's last_transno is up to date.
1087 * This should be done before zeroing client slot so last_transno will
1088 * be in server data or in client data in case of failure */
1089 rc = tgt_server_data_update(env, tgt, 0);
1091 CERROR("%s: failed to update server data, skip client %s "
1092 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1093 ted->ted_lcd->lcd_uuid, rc);
1097 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1098 rc = tgt_client_data_update(env, exp);
1100 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1101 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1102 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1103 ted->ted_lr_idx, ted->ted_lr_off, rc);
1106 EXPORT_SYMBOL(tgt_client_del);
1108 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1109 struct tg_export_data *ted, struct tg_reply_data *trd,
1110 struct thandle *th, bool update_lrd_file)
1112 struct lsd_reply_data *lrd;
1115 lrd = &trd->trd_reply;
1116 /* update export last transno */
1117 mutex_lock(&ted->ted_lcd_lock);
1118 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1119 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1120 mutex_unlock(&ted->ted_lcd_lock);
1122 /* find a empty slot */
1123 i = tgt_find_free_reply_slot(tgt);
1124 if (unlikely(i < 0)) {
1125 CERROR("%s: couldn't find a slot for reply data: "
1126 "rc = %d\n", tgt_name(tgt), i);
1131 if (update_lrd_file) {
1135 /* write reply data to disk */
1136 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1137 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1138 if (unlikely(rc != 0)) {
1139 CERROR("%s: can't update %s file: rc = %d\n",
1140 tgt_name(tgt), REPLY_DATA, rc);
1144 /* add reply data to target export's reply list */
1145 mutex_lock(&ted->ted_lcd_lock);
1146 list_add(&trd->trd_list, &ted->ted_reply_list);
1147 ted->ted_reply_cnt++;
1148 if (ted->ted_reply_cnt > ted->ted_reply_max)
1149 ted->ted_reply_max = ted->ted_reply_cnt;
1150 mutex_unlock(&ted->ted_lcd_lock);
1152 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1153 "tag %hu, client gen %u, slot idx %d\n",
1154 trd, lrd->lrd_xid, lrd->lrd_transno,
1155 trd->trd_tag, lrd->lrd_client_gen, i);
1158 EXPORT_SYMBOL(tgt_add_reply_data);
1161 * last_rcvd & last_committed update callbacks
1163 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1164 struct dt_object *obj, __u64 opdata,
1165 struct thandle *th, struct ptlrpc_request *req)
1167 struct tgt_thread_info *tti = tgt_th_info(env);
1168 struct tgt_session_info *tsi = tgt_ses_info(env);
1169 struct obd_export *exp = tsi->tsi_exp;
1170 struct tg_export_data *ted;
1178 LASSERT(exp != NULL);
1179 ted = &exp->exp_target_data;
1181 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1182 if (ted->ted_lr_idx < 0 && !lw_client)
1183 /* ofd connect may cause transaction before export has
1188 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1190 /* From update replay, tti_transno should be set already */
1191 LASSERT(tti->tti_transno != 0);
1193 spin_lock(&tgt->lut_translock);
1194 if (th->th_result != 0) {
1195 if (tti->tti_transno != 0) {
1196 CERROR("%s: replay transno %llu failed: rc = %d\n",
1197 tgt_name(tgt), tti->tti_transno, th->th_result);
1199 } else if (tti->tti_transno == 0) {
1200 tti->tti_transno = ++tgt->lut_last_transno;
1202 /* should be replay */
1203 if (tti->tti_transno > tgt->lut_last_transno)
1204 tgt->lut_last_transno = tti->tti_transno;
1206 spin_unlock(&tgt->lut_translock);
1208 /** VBR: set new versions */
1209 if (th->th_result == 0 && obj != NULL) {
1210 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1211 dt_version_set(env, dto, tti->tti_transno, th);
1214 /* filling reply data */
1215 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1216 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1219 req->rq_transno = tti->tti_transno;
1220 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1223 /* if can't add callback, do sync write */
1224 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1227 /* All operations performed by LW clients are synchronous and
1228 * we store the committed transno in the last_rcvd header */
1229 spin_lock(&tgt->lut_translock);
1230 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1231 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1232 spin_unlock(&tgt->lut_translock);
1233 /* Although lightweight (LW) connections have no slot
1234 * in the last_rcvd, we still want to maintain
1235 * the in-memory lsd_client_data structure in order to
1236 * properly handle reply reconstruction. */
1237 rc = tgt_server_data_write(env, tgt, th);
1239 spin_unlock(&tgt->lut_translock);
1241 } else if (ted->ted_lr_off == 0) {
1242 CERROR("%s: client idx %d has offset %lld\n",
1243 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1247 /* Target that supports multiple reply data */
1248 if (tgt_is_multimodrpcs_client(exp)) {
1249 struct tg_reply_data *trd;
1250 struct lsd_reply_data *lrd;
1251 __u64 *pre_versions;
1255 if (unlikely(trd == NULL))
1258 /* fill reply data information */
1259 lrd = &trd->trd_reply;
1260 lrd->lrd_transno = tti->tti_transno;
1262 lrd->lrd_xid = req->rq_xid;
1263 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1264 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1265 lrd->lrd_result = th->th_result;
1266 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1267 write_update = true;
1269 LASSERT(tsi->tsi_xid != 0);
1270 lrd->lrd_xid = tsi->tsi_xid;
1271 lrd->lrd_result = tsi->tsi_result;
1272 lrd->lrd_client_gen = tsi->tsi_client_gen;
1274 pre_versions = NULL;
1275 write_update = false;
1278 lrd->lrd_data = opdata;
1280 trd->trd_pre_versions[0] = pre_versions[0];
1281 trd->trd_pre_versions[1] = pre_versions[1];
1282 trd->trd_pre_versions[2] = pre_versions[2];
1283 trd->trd_pre_versions[3] = pre_versions[3];
1286 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1292 /* Enough for update replay, let's return */
1296 mutex_lock(&ted->ted_lcd_lock);
1297 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1298 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1299 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1300 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1301 ted->ted_lcd->lcd_last_close_result = th->th_result;
1303 /* VBR: save versions in last_rcvd for reconstruct. */
1304 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1307 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1308 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1309 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1310 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1312 transno_p = &ted->ted_lcd->lcd_last_transno;
1313 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1314 ted->ted_lcd->lcd_last_result = th->th_result;
1315 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1316 * see struct ldlm_reply->lock_policy_res1; */
1317 ted->ted_lcd->lcd_last_data = opdata;
1320 /* Update transno in slot only if non-zero number, i.e. no errors */
1321 if (likely(tti->tti_transno != 0)) {
1322 /* Don't overwrite bigger transaction number with lower one.
1323 * That is not sign of problem in all cases, but in any case
1324 * this value should be monotonically increased only. */
1325 if (*transno_p > tti->tti_transno) {
1326 if (!tgt->lut_no_reconstruct) {
1327 CERROR("%s: trying to overwrite bigger transno:"
1328 "on-disk: %llu, new: %llu replay: "
1329 "%d. See LU-617.\n", tgt_name(tgt),
1330 *transno_p, tti->tti_transno,
1331 req_is_replay(req));
1332 if (req_is_replay(req)) {
1333 spin_lock(&req->rq_export->exp_lock);
1334 req->rq_export->exp_vbr_failed = 1;
1335 spin_unlock(&req->rq_export->exp_lock);
1337 mutex_unlock(&ted->ted_lcd_lock);
1338 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1341 *transno_p = tti->tti_transno;
1346 tti->tti_off = ted->ted_lr_off;
1347 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1350 rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1353 mutex_unlock(&ted->ted_lcd_lock);
1357 mutex_unlock(&ted->ted_lcd_lock);
1362 * last_rcvd update for echo client simulation.
1363 * It updates last_rcvd client slot and version of object in
1364 * simple way but with all locks to simulate all drawbacks
1366 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1367 struct lu_target *tgt,
1368 struct dt_object *obj,
1370 struct obd_export *exp)
1372 struct tgt_thread_info *tti = tgt_th_info(env);
1373 struct tg_export_data *ted = &exp->exp_target_data;
1378 tti->tti_transno = 0;
1380 spin_lock(&tgt->lut_translock);
1381 if (th->th_result == 0)
1382 tti->tti_transno = ++tgt->lut_last_transno;
1383 spin_unlock(&tgt->lut_translock);
1385 /** VBR: set new versions */
1386 if (th->th_result == 0 && obj != NULL)
1387 dt_version_set(env, obj, tti->tti_transno, th);
1389 /* if can't add callback, do sync write */
1390 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1393 LASSERT(ted->ted_lr_off > 0);
1395 mutex_lock(&ted->ted_lcd_lock);
1396 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1397 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1398 ted->ted_lcd->lcd_last_result = th->th_result;
1400 tti->tti_off = ted->ted_lr_off;
1401 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1402 mutex_unlock(&ted->ted_lcd_lock);
1406 static int tgt_clients_data_init(const struct lu_env *env,
1407 struct lu_target *tgt,
1408 unsigned long last_size)
1410 struct obd_device *obd = tgt->lut_obd;
1411 struct lr_server_data *lsd = &tgt->lut_lsd;
1412 struct lsd_client_data *lcd = NULL;
1413 struct tg_export_data *ted;
1416 loff_t off = lsd->lsd_client_start;
1417 __u32 generation = 0;
1418 struct cfs_hash *hash = NULL;
1422 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1423 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1429 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1431 GOTO(err_out, rc = -ENODEV);
1433 for (cl_idx = 0; off < last_size; cl_idx++) {
1434 struct obd_export *exp;
1437 /* Don't assume off is incremented properly by
1438 * read_record(), in case sizeof(*lcd)
1439 * isn't the same as fsd->lsd_client_size. */
1440 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1441 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1443 CERROR("%s: error reading last_rcvd %s idx %d off "
1444 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1447 break; /* read error shouldn't cause startup to fail */
1450 if (lcd->lcd_uuid[0] == '\0') {
1451 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1456 last_transno = lcd_last_transno(lcd);
1458 /* These exports are cleaned up by disconnect, so they
1459 * need to be set up like real exports as connect does.
1461 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1462 " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1463 cl_idx, last_transno, lsd->lsd_last_transno,
1464 lcd_last_xid(lcd), lcd->lcd_generation);
1466 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1468 if (PTR_ERR(exp) == -EALREADY) {
1469 /* export already exists, zero out this one */
1470 CERROR("%s: Duplicate export %s!\n",
1471 tgt_name(tgt), lcd->lcd_uuid);
1474 GOTO(err_out, rc = PTR_ERR(exp));
1477 ted = &exp->exp_target_data;
1478 *ted->ted_lcd = *lcd;
1480 rc = tgt_client_add(env, exp, cl_idx);
1481 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1482 /* VBR: set export last committed version */
1483 exp->exp_last_committed = last_transno;
1484 spin_lock(&exp->exp_lock);
1485 exp->exp_connecting = 0;
1486 exp->exp_in_recovery = 0;
1487 spin_unlock(&exp->exp_lock);
1488 obd->obd_max_recoverable_clients++;
1490 if (tgt->lut_lsd.lsd_feature_incompat &
1491 OBD_INCOMPAT_MULTI_RPCS &&
1492 lcd->lcd_generation != 0) {
1493 /* compute the highest valid client generation */
1494 generation = max(generation, lcd->lcd_generation);
1495 /* fill client_generation <-> export hash table */
1496 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1497 &exp->exp_gen_hash);
1499 CERROR("%s: duplicate export for client "
1501 tgt_name(tgt), lcd->lcd_generation);
1502 class_export_put(exp);
1507 class_export_put(exp);
1509 rc = rev_import_init(exp);
1511 class_unlink_export(exp);
1515 /* Need to check last_rcvd even for duplicated exports. */
1516 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1517 cl_idx, last_transno);
1519 spin_lock(&tgt->lut_translock);
1520 tgt->lut_last_transno = max(last_transno,
1521 tgt->lut_last_transno);
1522 spin_unlock(&tgt->lut_translock);
1525 /* record highest valid client generation */
1526 atomic_set(&tgt->lut_client_generation, generation);
1530 cfs_hash_putref(hash);
1535 struct server_compat_data {
1542 static struct server_compat_data tgt_scd[] = {
1543 [LDD_F_SV_TYPE_MDT] = {
1544 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1545 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1546 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1547 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1548 OBD_INCOMPAT_MULTI_RPCS,
1549 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1550 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1551 OBD_INCOMPAT_MULTI_OI,
1553 [LDD_F_SV_TYPE_OST] = {
1554 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1555 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1557 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1558 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1562 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1564 struct tgt_thread_info *tti = tgt_th_info(env);
1565 struct lr_server_data *lsd = &tgt->lut_lsd;
1566 unsigned long last_rcvd_size;
1570 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1574 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1576 /* ensure padding in the struct is the correct size */
1577 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1578 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1580 rc = server_name2index(tgt_name(tgt), &index, NULL);
1582 CERROR("%s: Can not get index from name: rc = %d\n",
1586 /* server_name2index() returns type */
1588 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1589 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1593 /* last_rcvd on OST doesn't provide reconstruct support because there
1594 * may be up to 8 in-flight write requests per single slot in
1595 * last_rcvd client data
1597 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1599 if (last_rcvd_size == 0) {
1600 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1602 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1603 sizeof(lsd->lsd_uuid));
1604 lsd->lsd_last_transno = 0;
1605 lsd->lsd_mount_count = 0;
1606 lsd->lsd_server_size = LR_SERVER_SIZE;
1607 lsd->lsd_client_start = LR_CLIENT_START;
1608 lsd->lsd_client_size = LR_CLIENT_SIZE;
1609 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1610 lsd->lsd_osd_index = index;
1611 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1612 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1614 rc = tgt_server_data_read(env, tgt);
1616 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1620 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1621 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1622 "using the wrong disk %s. Were the"
1623 " /dev/ assignments rearranged?\n",
1624 tgt->lut_obd->obd_uuid.uuid,
1629 if (lsd->lsd_osd_index != index) {
1630 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1631 "is different with the index %d in"
1632 "config log, It might be disk"
1633 "corruption!\n", tgt_name(tgt),
1634 lsd->lsd_osd_index, index);
1639 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1640 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1642 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1646 if (type == LDD_F_SV_TYPE_MDT)
1647 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1649 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1650 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1652 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1655 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1656 if (type == LDD_F_SV_TYPE_MDT &&
1657 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1658 if (last_rcvd_size > lsd->lsd_client_start) {
1659 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1660 "remove all clients for interop needs\n",
1662 rc = tgt_truncate_last_rcvd(env, tgt,
1663 lsd->lsd_client_start);
1666 last_rcvd_size = lsd->lsd_client_start;
1668 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1669 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1672 spin_lock(&tgt->lut_translock);
1673 tgt->lut_last_transno = lsd->lsd_last_transno;
1674 spin_unlock(&tgt->lut_translock);
1676 lsd->lsd_mount_count++;
1678 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1679 CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1680 tgt_name(tgt), tgt->lut_last_transno);
1681 CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1682 tgt_name(tgt), lsd->lsd_mount_count);
1683 CDEBUG(D_INODE, "%s: server data size: %u\n",
1684 tgt_name(tgt), lsd->lsd_server_size);
1685 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1686 tgt_name(tgt), lsd->lsd_client_start);
1687 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1688 tgt_name(tgt), lsd->lsd_client_size);
1689 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1690 tgt_name(tgt), last_rcvd_size);
1691 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1692 tgt_name(tgt), lsd->lsd_subdir_count);
1693 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1694 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1695 (last_rcvd_size - lsd->lsd_client_start) /
1696 lsd->lsd_client_size);
1697 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1699 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1700 lsd->lsd_client_size == 0) {
1701 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1705 if (!tgt->lut_obd->obd_replayable)
1706 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1708 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1710 GOTO(err_client, rc);
1712 spin_lock(&tgt->lut_translock);
1713 /* obd_last_committed is used for compatibility
1714 * with other lustre recovery code */
1715 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1716 spin_unlock(&tgt->lut_translock);
1718 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1719 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1721 /* save it, so mount count and last_transno is current */
1722 rc = tgt_server_data_update(env, tgt, 0);
1724 GOTO(err_client, rc);
1729 class_disconnect_exports(tgt->lut_obd);
1733 /* add credits for last_rcvd update */
1734 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1737 struct lu_target *tgt = cookie;
1738 struct tgt_session_info *tsi;
1739 struct tgt_thread_info *tti = tgt_th_info(env);
1740 struct dt_object *dto;
1743 /* if there is no session, then this transaction is not result of
1744 * request processing but some local operation */
1745 if (env->le_ses == NULL)
1748 LASSERT(tgt->lut_last_rcvd);
1749 tsi = tgt_ses_info(env);
1750 /* OFD may start transaction without export assigned */
1751 if (tsi->tsi_exp == NULL)
1754 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1756 * Use maximum possible file offset for declaration to ensure
1757 * ZFS will reserve enough credits for a write anywhere in this
1758 * file, since we don't know where in the file the write will be
1759 * because a replay slot has not been assigned. This should be
1760 * replaced by dmu_tx_hold_append() when available.
1762 tti->tti_buf.lb_buf = NULL;
1763 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1764 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1765 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1769 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1771 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1772 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1778 if (tsi->tsi_vbr_obj != NULL &&
1779 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1780 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1781 rc = dt_declare_version_set(env, dto, th);
1787 /* Update last_rcvd records with latests transaction data */
1788 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1791 struct lu_target *tgt = cookie;
1792 struct tgt_session_info *tsi;
1793 struct tgt_thread_info *tti = tgt_th_info(env);
1794 struct dt_object *obj = NULL;
1798 if (env->le_ses == NULL)
1801 tsi = tgt_ses_info(env);
1802 /* OFD may start transaction without export assigned */
1803 if (tsi->tsi_exp == NULL)
1806 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1808 if (tti->tti_has_trans && !echo_client) {
1809 if (tti->tti_mult_trans == 0) {
1810 CDEBUG(D_HA, "More than one transaction %llu\n",
1814 /* we need another transno to be assigned */
1815 tti->tti_transno = 0;
1816 } else if (th->th_result == 0) {
1817 tti->tti_has_trans = 1;
1820 if (tsi->tsi_vbr_obj != NULL &&
1821 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1822 obj = tsi->tsi_vbr_obj;
1825 if (unlikely(echo_client)) /* echo client special case */
1826 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1829 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1834 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1836 struct tgt_thread_info *tti = tgt_th_info(env);
1837 struct lsd_reply_data *lrd = &tti->tti_lrd;
1838 unsigned long reply_data_size;
1840 struct lsd_reply_header *lrh = NULL;
1841 struct lsd_client_data *lcd = NULL;
1842 struct tg_reply_data *trd = NULL;
1845 struct cfs_hash *hash = NULL;
1846 struct obd_export *exp;
1847 struct tg_export_data *ted;
1848 int reply_data_recovered = 0;
1850 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1853 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1857 GOTO(out, rc = -ENOMEM);
1859 if (reply_data_size == 0) {
1860 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1862 lrh->lrh_magic = LRH_MAGIC;
1863 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1864 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1865 rc = tgt_reply_header_write(env, tgt, lrh);
1867 CERROR("%s: error writing %s: rc = %d\n",
1868 tgt_name(tgt), REPLY_DATA, rc);
1872 rc = tgt_reply_header_read(env, tgt, lrh);
1874 CERROR("%s: error reading %s: rc = %d\n",
1875 tgt_name(tgt), REPLY_DATA, rc);
1878 if (lrh->lrh_magic != LRH_MAGIC ||
1879 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1880 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1881 CERROR("%s: invalid header in %s\n",
1882 tgt_name(tgt), REPLY_DATA);
1883 GOTO(out, rc = -EINVAL);
1886 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1888 GOTO(out, rc = -ENODEV);
1892 GOTO(out, rc = -ENOMEM);
1896 GOTO(out, rc = -ENOMEM);
1898 /* Load reply_data from disk */
1899 for (idx = 0, off = sizeof(struct lsd_reply_header);
1900 off < reply_data_size;
1901 idx++, off += sizeof(struct lsd_reply_data)) {
1902 rc = tgt_reply_data_read(env, tgt, lrd, off);
1904 CERROR("%s: error reading %s: rc = %d\n",
1905 tgt_name(tgt), REPLY_DATA, rc);
1909 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1911 /* old reply data from a disconnected client */
1914 ted = &exp->exp_target_data;
1915 mutex_lock(&ted->ted_lcd_lock);
1917 /* create in-memory reply_data and link it to
1918 * target export's reply list */
1919 rc = tgt_set_reply_slot(tgt, idx);
1921 mutex_unlock(&ted->ted_lcd_lock);
1924 trd->trd_reply = *lrd;
1925 trd->trd_pre_versions[0] = 0;
1926 trd->trd_pre_versions[1] = 0;
1927 trd->trd_pre_versions[2] = 0;
1928 trd->trd_pre_versions[3] = 0;
1929 trd->trd_index = idx;
1931 list_add(&trd->trd_list, &ted->ted_reply_list);
1932 ted->ted_reply_cnt++;
1933 if (ted->ted_reply_cnt > ted->ted_reply_max)
1934 ted->ted_reply_max = ted->ted_reply_cnt;
1936 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1937 "transno %llu, client gen %u, slot idx %d\n",
1938 tgt_name(tgt), trd, lrd->lrd_xid,
1939 lrd->lrd_transno, lrd->lrd_client_gen,
1942 /* update export last committed transation */
1943 exp->exp_last_committed = max(exp->exp_last_committed,
1946 mutex_unlock(&ted->ted_lcd_lock);
1947 class_export_put(exp);
1949 /* update target last committed transaction */
1950 spin_lock(&tgt->lut_translock);
1951 tgt->lut_last_transno = max(tgt->lut_last_transno,
1953 spin_unlock(&tgt->lut_translock);
1955 reply_data_recovered++;
1959 GOTO(out, rc = -ENOMEM);
1961 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1962 tgt_name(tgt), reply_data_recovered);
1965 spin_lock(&tgt->lut_translock);
1966 /* obd_last_committed is used for compatibility
1967 * with other lustre recovery code */
1968 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1969 spin_unlock(&tgt->lut_translock);
1975 cfs_hash_putref(hash);
1985 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
1988 struct tg_reply_data *found = NULL;
1989 struct tg_reply_data *reply;
1991 mutex_lock(&ted->ted_lcd_lock);
1992 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
1993 if (reply->trd_reply.lrd_xid == xid) {
1998 mutex_unlock(&ted->ted_lcd_lock);
2001 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
2003 /* Look for a reply data matching specified request @req
2004 * A copy is returned in @trd if the pointer is not NULL
2006 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2008 struct tg_export_data *ted = &req->rq_export->exp_target_data;
2009 struct tg_reply_data *reply;
2012 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2013 if (reply != NULL) {
2019 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2020 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2025 EXPORT_SYMBOL(tgt_lookup_reply);
2027 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2029 struct tg_export_data *ted = &exp->exp_target_data;
2030 struct lu_target *lut = class_exp2tgt(exp);
2031 struct tg_reply_data *trd, *tmp;
2033 mutex_lock(&ted->ted_lcd_lock);
2034 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2035 if (trd->trd_reply.lrd_xid > rcvd_xid)
2037 ted->ted_release_xid++;
2038 tgt_release_reply_data(lut, ted, trd);
2040 mutex_unlock(&ted->ted_lcd_lock);
2045 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2047 struct tg_export_data *ted = &exp->exp_target_data;
2048 struct lu_target *lut = class_exp2tgt(exp);
2049 struct tg_reply_data *trd, *tmp;
2054 mutex_lock(&ted->ted_lcd_lock);
2055 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2056 if (trd->trd_tag != tag)
2058 ted->ted_release_tag++;
2059 tgt_release_reply_data(lut, ted, trd);
2062 mutex_unlock(&ted->ted_lcd_lock);