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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Lustre Unified Target
37 * These are common function to work with last_received file
39 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 #include <obd_class.h>
43 #include <lustre_fid.h>
45 #include "tgt_internal.h"
48 /* Allocate a bitmap for a chunk of reply data slots */
49 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
53 OBD_ALLOC(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) * sizeof(long));
57 spin_lock(&lut->lut_client_bitmap_lock);
59 if (lut->lut_reply_bitmap[chunk] != NULL) {
60 /* someone else already allocated the bitmap for this chunk */
61 spin_unlock(&lut->lut_client_bitmap_lock);
62 OBD_FREE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
67 lut->lut_reply_bitmap[chunk] = bm;
69 spin_unlock(&lut->lut_client_bitmap_lock);
74 /* Look for an available reply data slot in the bitmap
76 * Allocate bitmap chunk when first used
77 * XXX algo could be improved if this routine limits performance
79 static int tgt_find_free_reply_slot(struct lu_target *lut)
86 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
87 /* allocate the bitmap chunk if necessary */
88 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
89 rc = tgt_bitmap_chunk_alloc(lut, chunk);
93 bmp = lut->lut_reply_bitmap[chunk];
95 /* look for an available slot in this chunk */
97 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
98 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
102 if (test_and_set_bit(b, bmp) == 0)
103 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
110 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
112 * Allocate the bitmap chunk if necessary
114 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
120 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
121 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
123 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
124 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
126 /* allocate the bitmap chunk if necessary */
127 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
128 rc = tgt_bitmap_chunk_alloc(lut, chunk);
133 /* mark the slot 'used' in this chunk */
134 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
135 CERROR("%s: slot %d already set in bitmap\n",
144 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
147 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
152 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
153 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
155 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
156 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
158 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
159 CERROR("%s: slot %d already clear in bitmap\n",
168 /* Read header of reply_data file of target @tgt into structure @lrh */
169 static int tgt_reply_header_read(const struct lu_env *env,
170 struct lu_target *tgt,
171 struct lsd_reply_header *lrh)
174 struct lsd_reply_header buf;
175 struct tgt_thread_info *tti = tgt_th_info(env);
178 tti->tti_buf.lb_buf = &buf;
179 tti->tti_buf.lb_len = sizeof(buf);
181 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
186 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
187 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
188 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
190 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
191 "header_size=%d reply_size=%d\n",
192 tgt->lut_obd->obd_name, REPLY_DATA,
193 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
198 /* Write header into replay_data file of target @tgt from structure @lrh */
199 static int tgt_reply_header_write(const struct lu_env *env,
200 struct lu_target *tgt,
201 struct lsd_reply_header *lrh)
204 struct lsd_reply_header buf;
205 struct tgt_thread_info *tti = tgt_th_info(env);
207 struct dt_object *dto;
209 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
210 "header_size=%d reply_size=%d\n",
211 tgt->lut_obd->obd_name, REPLY_DATA,
212 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
214 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
215 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
216 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
218 th = dt_trans_create(env, tgt->lut_bottom);
224 tti->tti_buf.lb_buf = &buf;
225 tti->tti_buf.lb_len = sizeof(buf);
227 rc = dt_declare_record_write(env, tgt->lut_reply_data,
228 &tti->tti_buf, tti->tti_off, th);
232 rc = dt_trans_start(env, tgt->lut_bottom, th);
236 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
237 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
239 dt_trans_stop(env, tgt->lut_bottom, th);
243 /* Write the reply data @lrd into reply_data file of target @tgt
246 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
247 struct lsd_reply_data *lrd, loff_t off,
250 struct tgt_thread_info *tti = tgt_th_info(env);
251 struct dt_object *dto;
252 struct lsd_reply_data *buf = &tti->tti_lrd;
254 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
256 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
257 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
258 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
259 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
260 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
262 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
265 tti->tti_buf.lb_buf = buf;
266 tti->tti_buf.lb_len = sizeof(*buf);
268 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
269 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
272 /* Read the reply data from reply_data file of target @tgt at offset @off
273 * into structure @lrd
275 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
276 struct lsd_reply_data *lrd, loff_t off)
279 struct tgt_thread_info *tti = tgt_th_info(env);
280 struct lsd_reply_data *buf = &tti->tti_lrd;
283 tti->tti_buf.lb_buf = buf;
284 tti->tti_buf.lb_len = sizeof(*buf);
286 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
291 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
292 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
293 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
294 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
295 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
301 /* Free the in-memory reply data structure @trd and release
302 * the corresponding slot in the reply_data file of target @lut
303 * Called with ted_lcd_lock held
305 static void tgt_free_reply_data(struct lu_target *lut,
306 struct tg_export_data *ted,
307 struct tg_reply_data *trd)
309 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
310 "client gen %u, slot idx %d\n",
311 tgt_name(lut), trd, trd->trd_reply.lrd_xid,
312 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
315 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
317 list_del(&trd->trd_list);
318 ted->ted_reply_cnt--;
319 tgt_clear_reply_slot(lut, trd->trd_index);
323 /* Release the reply data @trd from target @lut
324 * The reply data with the highest transno for this export
325 * is retained to ensure correctness of target recovery
326 * Called with ted_lcd_lock held
328 static void tgt_release_reply_data(struct lu_target *lut,
329 struct tg_export_data *ted,
330 struct tg_reply_data *trd)
332 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
333 "client gen %u, slot idx %d\n",
334 tgt_name(lut), trd, trd->trd_reply.lrd_xid,
335 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
338 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
340 /* Do not free the reply data corresponding to the
341 * highest transno of this export.
342 * This ensures on-disk reply data is kept and
343 * last committed transno can be restored from disk in case
346 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
347 /* free previous retained reply */
348 if (ted->ted_reply_last != NULL)
349 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
350 /* retain the reply */
351 list_del_init(&trd->trd_list);
352 ted->ted_reply_last = trd;
354 tgt_free_reply_data(lut, ted, trd);
358 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
360 tti->tti_buf.lb_buf = &tti->tti_lsd;
361 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
362 return &tti->tti_buf;
365 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
367 tti->tti_buf.lb_buf = &tti->tti_lcd;
368 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
369 return &tti->tti_buf;
373 * Allocate in-memory data for client slot related to export.
375 int tgt_client_alloc(struct obd_export *exp)
378 LASSERT(exp != exp->exp_obd->obd_self_export);
380 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
381 if (exp->exp_target_data.ted_lcd == NULL)
383 /* Mark that slot is not yet valid, 0 doesn't work here */
384 exp->exp_target_data.ted_lr_idx = -1;
385 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
386 mutex_init(&exp->exp_target_data.ted_lcd_lock);
389 EXPORT_SYMBOL(tgt_client_alloc);
392 * Free in-memory data for client slot related to export.
394 void tgt_client_free(struct obd_export *exp)
396 struct tg_export_data *ted = &exp->exp_target_data;
397 struct lu_target *lut = class_exp2tgt(exp);
398 struct tg_reply_data *trd, *tmp;
400 LASSERT(exp != exp->exp_obd->obd_self_export);
402 /* free reply data */
403 mutex_lock(&ted->ted_lcd_lock);
404 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
405 tgt_release_reply_data(lut, ted, trd);
407 if (ted->ted_reply_last != NULL) {
408 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
409 ted->ted_reply_last = NULL;
411 mutex_unlock(&ted->ted_lcd_lock);
413 if (!hlist_unhashed(&exp->exp_gen_hash))
414 cfs_hash_del(exp->exp_obd->obd_gen_hash,
415 &ted->ted_lcd->lcd_generation,
418 OBD_FREE_PTR(ted->ted_lcd);
421 /* Slot may be not yet assigned */
422 if (ted->ted_lr_idx < 0)
424 /* Clear bit when lcd is freed */
425 LASSERT(lut && lut->lut_client_bitmap);
426 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
427 CERROR("%s: client %u bit already clear in bitmap\n",
428 exp->exp_obd->obd_name, ted->ted_lr_idx);
432 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
433 atomic_dec(&lut->lut_num_clients);
435 EXPORT_SYMBOL(tgt_client_free);
437 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
438 struct lsd_client_data *lcd, loff_t *off, int index)
440 struct tgt_thread_info *tti = tgt_th_info(env);
444 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
446 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
447 lcd_le_to_cpu(&tti->tti_lcd, lcd);
448 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
449 lcd->lcd_last_close_result =
450 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
453 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = "LPU64
454 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
455 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
456 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
457 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
458 lcd->lcd_last_result, lcd->lcd_last_data,
459 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
460 lcd->lcd_last_close_result, rc);
464 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
465 struct lsd_client_data *lcd, loff_t *off,
468 struct tgt_thread_info *tti = tgt_th_info(env);
469 struct dt_object *dto;
471 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
472 lcd->lcd_last_close_result =
473 ptlrpc_status_hton(lcd->lcd_last_close_result);
474 lcd_cpu_to_le(lcd, &tti->tti_lcd);
477 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
478 return dt_record_write(env, dto, &tti->tti_buf, off, th);
482 * Update client data in last_rcvd
484 static int tgt_client_data_update(const struct lu_env *env,
485 struct obd_export *exp)
487 struct tg_export_data *ted = &exp->exp_target_data;
488 struct lu_target *tgt = class_exp2tgt(exp);
489 struct tgt_thread_info *tti = tgt_th_info(env);
495 if (unlikely(tgt == NULL)) {
496 CDEBUG(D_ERROR, "%s: No target for connected export\n",
497 class_exp2obd(exp)->obd_name);
501 th = dt_trans_create(env, tgt->lut_bottom);
506 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
508 ted->ted_lr_off, th);
512 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
516 * Until this operations will be committed the sync is needed
517 * for this export. This should be done _after_ starting the
518 * transaction so that many connecting clients will not bring
519 * server down with lots of sync writes.
521 rc = tgt_new_client_cb_add(th, exp);
523 /* can't add callback, do sync now */
526 spin_lock(&exp->exp_lock);
527 exp->exp_need_sync = 1;
528 spin_unlock(&exp->exp_lock);
531 tti->tti_off = ted->ted_lr_off;
532 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
535 dt_trans_stop(env, tgt->lut_bottom, th);
536 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
537 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
538 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
543 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
545 struct tgt_thread_info *tti = tgt_th_info(env);
550 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
553 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
555 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
556 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
557 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
561 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
564 struct tgt_thread_info *tti = tgt_th_info(env);
565 struct dt_object *dto;
572 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
574 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
575 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
577 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
578 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
579 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
585 * Update server data in last_rcvd
587 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
590 struct tgt_thread_info *tti = tgt_th_info(env);
597 "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
598 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
599 tgt->lut_last_transno);
601 /* Always save latest transno to keep it fresh */
602 spin_lock(&tgt->lut_translock);
603 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
604 spin_unlock(&tgt->lut_translock);
606 th = dt_trans_create(env, tgt->lut_bottom);
613 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
614 &tti->tti_buf, tti->tti_off, th);
618 rc = dt_trans_start(env, tgt->lut_bottom, th);
622 rc = tgt_server_data_write(env, tgt, th);
624 dt_trans_stop(env, tgt->lut_bottom, th);
626 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
627 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
628 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
631 EXPORT_SYMBOL(tgt_server_data_update);
633 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
636 struct dt_object *dt = tgt->lut_last_rcvd;
644 attr.la_valid = LA_SIZE;
646 th = dt_trans_create(env, tgt->lut_bottom);
649 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
652 rc = dt_declare_attr_set(env, dt, &attr, th);
655 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
659 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
661 rc = dt_attr_set(env, dt, &attr, th);
664 dt_trans_stop(env, tgt->lut_bottom, th);
669 static void tgt_client_epoch_update(const struct lu_env *env,
670 struct obd_export *exp)
672 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
673 struct lu_target *tgt = class_exp2tgt(exp);
675 LASSERT(tgt && tgt->lut_bottom);
676 /** VBR: set client last_epoch to current epoch */
677 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
679 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
680 tgt_client_data_update(env, exp);
684 * Update boot epoch when recovery ends
686 void tgt_boot_epoch_update(struct lu_target *tgt)
689 struct ptlrpc_request *req;
691 struct list_head client_list;
694 if (tgt->lut_obd->obd_stopping)
697 rc = lu_env_init(&env, LCT_DT_THREAD);
699 CERROR("%s: can't initialize environment: rc = %d\n",
700 tgt->lut_obd->obd_name, rc);
704 spin_lock(&tgt->lut_translock);
705 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
706 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
707 tgt->lut_lsd.lsd_start_epoch = start_epoch;
708 spin_unlock(&tgt->lut_translock);
710 INIT_LIST_HEAD(&client_list);
712 * The recovery is not yet finished and final queue can still be updated
713 * with resend requests. Move final list to separate one for processing
715 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
716 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
717 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
720 * go through list of exports participated in recovery and
721 * set new epoch for them
723 list_for_each_entry(req, &client_list, rq_list) {
724 LASSERT(!req->rq_export->exp_delayed);
725 if (!req->rq_export->exp_vbr_failed)
726 tgt_client_epoch_update(&env, req->rq_export);
728 /** return list back at once */
729 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
730 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
731 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
733 /** Clear MULTI RPCS incompatibility flag if
734 * - target is MDT and
735 * - there is no client to recover or the recovery was aborted
737 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
738 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
739 tgt->lut_obd->obd_abort_recovery))
740 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
742 /** update server epoch */
743 tgt_server_data_update(&env, tgt, 1);
748 * commit callback, need to update last_commited value
750 struct tgt_last_committed_callback {
751 struct dt_txn_commit_cb llcc_cb;
752 struct lu_target *llcc_tgt;
753 struct obd_export *llcc_exp;
757 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
758 struct dt_txn_commit_cb *cb, int err)
760 struct tgt_last_committed_callback *ccb;
762 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
764 LASSERT(ccb->llcc_tgt != NULL);
765 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
767 spin_lock(&ccb->llcc_tgt->lut_translock);
768 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
769 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
771 LASSERT(ccb->llcc_exp);
772 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
773 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
774 spin_unlock(&ccb->llcc_tgt->lut_translock);
775 ptlrpc_commit_replies(ccb->llcc_exp);
777 spin_unlock(&ccb->llcc_tgt->lut_translock);
779 class_export_cb_put(ccb->llcc_exp);
780 if (ccb->llcc_transno)
781 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
782 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
786 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
787 struct obd_export *exp, __u64 transno)
789 struct tgt_last_committed_callback *ccb;
790 struct dt_txn_commit_cb *dcb;
798 ccb->llcc_exp = class_export_cb_get(exp);
799 ccb->llcc_transno = transno;
802 dcb->dcb_func = tgt_cb_last_committed;
803 INIT_LIST_HEAD(&dcb->dcb_linkage);
804 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
806 rc = dt_trans_cb_add(th, dcb);
808 class_export_cb_put(exp);
812 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
813 /* report failure to force synchronous operation */
819 struct tgt_new_client_callback {
820 struct dt_txn_commit_cb lncc_cb;
821 struct obd_export *lncc_exp;
824 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
825 struct dt_txn_commit_cb *cb, int err)
827 struct tgt_new_client_callback *ccb;
829 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
831 LASSERT(ccb->lncc_exp->exp_obd);
833 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
834 ccb->lncc_exp->exp_obd->obd_name,
835 ccb->lncc_exp->exp_client_uuid.uuid);
837 spin_lock(&ccb->lncc_exp->exp_lock);
839 ccb->lncc_exp->exp_need_sync = 0;
841 spin_unlock(&ccb->lncc_exp->exp_lock);
842 class_export_cb_put(ccb->lncc_exp);
847 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
849 struct tgt_new_client_callback *ccb;
850 struct dt_txn_commit_cb *dcb;
857 ccb->lncc_exp = class_export_cb_get(exp);
860 dcb->dcb_func = tgt_cb_new_client;
861 INIT_LIST_HEAD(&dcb->dcb_linkage);
862 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
864 rc = dt_trans_cb_add(th, dcb);
866 class_export_cb_put(exp);
873 * Add new client to the last_rcvd upon new connection.
875 * We use a bitmap to locate a free space in the last_rcvd file and initialize
878 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
880 struct tg_export_data *ted = &exp->exp_target_data;
881 struct lu_target *tgt = class_exp2tgt(exp);
886 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
887 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
890 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
893 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
894 * there's no need for extra complication here
896 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
898 if (idx >= LR_MAX_CLIENTS ||
899 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
900 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
901 tgt->lut_obd->obd_name, idx);
904 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
905 idx = find_next_zero_bit(tgt->lut_client_bitmap,
906 LR_MAX_CLIENTS, idx);
910 ted->ted_lr_idx = idx;
911 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
912 idx * tgt->lut_lsd.lsd_client_size;
914 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
916 if (tgt_is_multimodrpcs_client(exp)) {
917 /* Set MULTI RPCS incompatibility flag to prevent previous
918 * Lustre versions to mount a target with reply_data file */
919 atomic_inc(&tgt->lut_num_clients);
920 if (!(tgt->lut_lsd.lsd_feature_incompat &
921 OBD_INCOMPAT_MULTI_RPCS)) {
922 tgt->lut_lsd.lsd_feature_incompat |=
923 OBD_INCOMPAT_MULTI_RPCS;
924 rc = tgt_server_data_update(env, tgt, 1);
926 CERROR("%s: unable to set MULTI RPCS "
927 "incompatibility flag\n",
928 exp->exp_obd->obd_name);
933 /* assign client slot generation */
934 ted->ted_lcd->lcd_generation =
935 atomic_inc_return(&tgt->lut_client_generation);
937 ted->ted_lcd->lcd_generation = 0;
940 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
942 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
943 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
945 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
948 rc = tgt_client_data_update(env, exp);
950 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
951 tgt->lut_obd->obd_name, idx, rc);
955 EXPORT_SYMBOL(tgt_client_new);
957 /* Add an existing client to the MDS in-memory state based on
958 * a client that was previously found in the last_rcvd file and
959 * already has an assigned slot (idx >= 0).
961 * It should not be possible to fail adding an existing client - otherwise
962 * mdt_init_server_data() callsite needs to be fixed.
964 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
966 struct tg_export_data *ted = &exp->exp_target_data;
967 struct lu_target *tgt = class_exp2tgt(exp);
971 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
972 LASSERTF(idx >= 0, "%d\n", idx);
974 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
975 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
978 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
979 CERROR("%s: client %d: bit already set in bitmap!!\n",
980 tgt->lut_obd->obd_name, idx);
983 atomic_inc(&tgt->lut_num_clients);
985 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
987 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
988 ted->ted_lcd->lcd_generation);
990 ted->ted_lr_idx = idx;
991 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
992 idx * tgt->lut_lsd.lsd_client_size;
994 mutex_init(&ted->ted_lcd_lock);
996 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1001 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1003 struct tg_export_data *ted = &exp->exp_target_data;
1004 struct lu_target *tgt = class_exp2tgt(exp);
1009 LASSERT(ted->ted_lcd);
1011 if (unlikely(tgt == NULL)) {
1012 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1013 class_exp2obd(exp)->obd_name);
1017 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1018 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1019 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1020 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1023 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1024 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1025 ted->ted_lcd->lcd_uuid);
1027 /* Clear the bit _after_ zeroing out the client so we don't
1028 race with filter_client_add and zero out new clients.*/
1029 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1030 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1031 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1035 /* Do not erase record for recoverable client. */
1036 if (exp->exp_flags & OBD_OPT_FAILOVER)
1039 /* Make sure the server's last_transno is up to date.
1040 * This should be done before zeroing client slot so last_transno will
1041 * be in server data or in client data in case of failure */
1042 rc = tgt_server_data_update(env, tgt, 0);
1044 CERROR("%s: failed to update server data, skip client %s "
1045 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1046 ted->ted_lcd->lcd_uuid, rc);
1050 mutex_lock(&ted->ted_lcd_lock);
1051 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1052 rc = tgt_client_data_update(env, exp);
1053 mutex_unlock(&ted->ted_lcd_lock);
1055 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1056 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1057 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1058 ted->ted_lr_idx, ted->ted_lr_off, rc);
1061 EXPORT_SYMBOL(tgt_client_del);
1064 * last_rcvd & last_committed update callbacks
1066 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1067 struct dt_object *obj, __u64 opdata,
1068 struct thandle *th, struct ptlrpc_request *req)
1070 struct tgt_thread_info *tti = tgt_th_info(env);
1071 struct tg_export_data *ted;
1074 bool lw_client, update = false;
1078 ted = &req->rq_export->exp_target_data;
1080 lw_client = exp_connect_flags(req->rq_export) & OBD_CONNECT_LIGHTWEIGHT;
1081 if (ted->ted_lr_idx < 0 && !lw_client)
1082 /* ofd connect may cause transaction before export has
1086 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1088 spin_lock(&tgt->lut_translock);
1089 if (th->th_result != 0) {
1090 if (tti->tti_transno != 0) {
1091 CERROR("%s: replay transno "LPU64" failed: rc = %d\n",
1092 tgt_name(tgt), tti->tti_transno, th->th_result);
1094 } else if (tti->tti_transno == 0) {
1095 tti->tti_transno = ++tgt->lut_last_transno;
1097 /* should be replay */
1098 if (tti->tti_transno > tgt->lut_last_transno)
1099 tgt->lut_last_transno = tti->tti_transno;
1101 spin_unlock(&tgt->lut_translock);
1103 /** VBR: set new versions */
1104 if (th->th_result == 0 && obj != NULL) {
1105 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1106 dt_version_set(env, dto, tti->tti_transno, th);
1109 /* filling reply data */
1110 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
1111 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1113 req->rq_transno = tti->tti_transno;
1114 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1116 /* if can't add callback, do sync write */
1117 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, req->rq_export,
1121 /* All operations performed by LW clients are synchronous and
1122 * we store the committed transno in the last_rcvd header */
1123 spin_lock(&tgt->lut_translock);
1124 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1125 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1128 spin_unlock(&tgt->lut_translock);
1129 /* Although lightweight (LW) connections have no slot in
1130 * last_rcvd, we still want to maintain the in-memory
1131 * lsd_client_data structure in order to properly handle reply
1132 * reconstruction. */
1133 } else if (ted->ted_lr_off == 0) {
1134 CERROR("%s: client idx %d has offset %lld\n",
1135 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1139 /* if the export has already been disconnected, we have no last_rcvd
1140 * slot, update server data with latest transno then */
1141 if (ted->ted_lcd == NULL) {
1142 CWARN("commit transaction for disconnected client %s: rc %d\n",
1143 req->rq_export->exp_client_uuid.uuid, rc);
1144 GOTO(srv_update, rc = 0);
1147 /* Target that supports multiple reply data */
1148 if (tgt_is_multimodrpcs_client(req->rq_export)) {
1149 struct tg_reply_data *trd;
1150 struct lsd_reply_data *lrd;
1151 __u64 *pre_versions;
1156 if (unlikely(trd == NULL))
1157 GOTO(srv_update, rc = -ENOMEM);
1159 /* update export last transno */
1160 mutex_lock(&ted->ted_lcd_lock);
1161 if (tti->tti_transno > ted->ted_lcd->lcd_last_transno)
1162 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1163 mutex_unlock(&ted->ted_lcd_lock);
1165 /* fill reply data information */
1166 lrd = &trd->trd_reply;
1167 lrd->lrd_transno = tti->tti_transno;
1168 lrd->lrd_xid = req->rq_xid;
1169 lrd->lrd_result = th->th_result;
1170 lrd->lrd_data = opdata;
1171 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1172 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1173 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1175 trd->trd_pre_versions[0] = pre_versions[0];
1176 trd->trd_pre_versions[1] = pre_versions[1];
1177 trd->trd_pre_versions[2] = pre_versions[2];
1178 trd->trd_pre_versions[3] = pre_versions[3];
1181 /* find a empty slot */
1182 i = tgt_find_free_reply_slot(tgt);
1183 if (unlikely(i < 0)) {
1184 CERROR("%s: couldn't find a slot for reply data: "
1185 "rc = %d\n", tgt_name(tgt), i);
1186 GOTO(srv_update, rc = i);
1190 /* write reply data to disk */
1191 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1192 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1193 if (unlikely(rc != 0)) {
1194 CERROR("%s: can't update %s file: rc = %d\n",
1195 tgt_name(tgt), REPLY_DATA, rc);
1199 /* add reply data to target export's reply list */
1200 mutex_lock(&ted->ted_lcd_lock);
1201 list_add(&trd->trd_list, &ted->ted_reply_list);
1202 ted->ted_reply_cnt++;
1203 if (ted->ted_reply_cnt > ted->ted_reply_max)
1204 ted->ted_reply_max = ted->ted_reply_cnt;
1205 mutex_unlock(&ted->ted_lcd_lock);
1207 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1208 "tag %hu, client gen %u, slot idx %d\n",
1209 trd, lrd->lrd_xid, lrd->lrd_transno,
1210 trd->trd_tag, lrd->lrd_client_gen, i);
1212 GOTO(srv_update, rc = 0);
1215 mutex_lock(&ted->ted_lcd_lock);
1216 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1217 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1218 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1219 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1220 ted->ted_lcd->lcd_last_close_result = th->th_result;
1222 /* VBR: save versions in last_rcvd for reconstruct. */
1223 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1226 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1227 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1228 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1229 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1231 transno_p = &ted->ted_lcd->lcd_last_transno;
1232 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1233 ted->ted_lcd->lcd_last_result = th->th_result;
1234 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1235 * see struct ldlm_reply->lock_policy_res1; */
1236 ted->ted_lcd->lcd_last_data = opdata;
1239 /* Update transno in slot only if non-zero number, i.e. no errors */
1240 if (likely(tti->tti_transno != 0)) {
1241 if (*transno_p > tti->tti_transno &&
1242 !tgt->lut_no_reconstruct) {
1243 CERROR("%s: trying to overwrite bigger transno:"
1244 "on-disk: "LPU64", new: "LPU64" replay: %d. "
1245 "see LU-617.\n", tgt_name(tgt), *transno_p,
1246 tti->tti_transno, req_is_replay(req));
1247 if (req_is_replay(req)) {
1248 spin_lock(&req->rq_export->exp_lock);
1249 req->rq_export->exp_vbr_failed = 1;
1250 spin_unlock(&req->rq_export->exp_lock);
1252 mutex_unlock(&ted->ted_lcd_lock);
1253 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1255 *transno_p = tti->tti_transno;
1259 tti->tti_off = ted->ted_lr_off;
1260 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1262 mutex_unlock(&ted->ted_lcd_lock);
1266 mutex_unlock(&ted->ted_lcd_lock);
1270 rc = tgt_server_data_write(env, tgt, th);
1275 * last_rcvd update for echo client simulation.
1276 * It updates last_rcvd client slot and version of object in
1277 * simple way but with all locks to simulate all drawbacks
1279 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1280 struct lu_target *tgt,
1281 struct dt_object *obj,
1283 struct obd_export *exp)
1285 struct tgt_thread_info *tti = tgt_th_info(env);
1286 struct tg_export_data *ted = &exp->exp_target_data;
1291 tti->tti_transno = 0;
1293 spin_lock(&tgt->lut_translock);
1294 if (th->th_result == 0)
1295 tti->tti_transno = ++tgt->lut_last_transno;
1296 spin_unlock(&tgt->lut_translock);
1298 /** VBR: set new versions */
1299 if (th->th_result == 0 && obj != NULL)
1300 dt_version_set(env, obj, tti->tti_transno, th);
1302 /* if can't add callback, do sync write */
1303 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1306 LASSERT(ted->ted_lr_off > 0);
1308 mutex_lock(&ted->ted_lcd_lock);
1309 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1310 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1311 ted->ted_lcd->lcd_last_result = th->th_result;
1313 tti->tti_off = ted->ted_lr_off;
1314 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1315 mutex_unlock(&ted->ted_lcd_lock);
1319 static int tgt_clients_data_init(const struct lu_env *env,
1320 struct lu_target *tgt,
1321 unsigned long last_size)
1323 struct obd_device *obd = tgt->lut_obd;
1324 struct lr_server_data *lsd = &tgt->lut_lsd;
1325 struct lsd_client_data *lcd = NULL;
1326 struct tg_export_data *ted;
1329 loff_t off = lsd->lsd_client_start;
1330 __u32 generation = 0;
1331 struct cfs_hash *hash = NULL;
1335 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1336 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1342 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1344 GOTO(err_out, rc = -ENODEV);
1346 for (cl_idx = 0; off < last_size; cl_idx++) {
1347 struct obd_export *exp;
1350 /* Don't assume off is incremented properly by
1351 * read_record(), in case sizeof(*lcd)
1352 * isn't the same as fsd->lsd_client_size. */
1353 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1354 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1356 CERROR("%s: error reading last_rcvd %s idx %d off "
1357 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1360 break; /* read error shouldn't cause startup to fail */
1363 if (lcd->lcd_uuid[0] == '\0') {
1364 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1369 last_transno = lcd_last_transno(lcd);
1371 /* These exports are cleaned up by disconnect, so they
1372 * need to be set up like real exports as connect does.
1374 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
1375 " srv lr: "LPU64" lx: "LPU64" gen %u\n", lcd->lcd_uuid,
1376 cl_idx, last_transno, lsd->lsd_last_transno,
1377 lcd_last_xid(lcd), lcd->lcd_generation);
1379 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1381 if (PTR_ERR(exp) == -EALREADY) {
1382 /* export already exists, zero out this one */
1383 CERROR("%s: Duplicate export %s!\n",
1384 tgt_name(tgt), lcd->lcd_uuid);
1387 GOTO(err_out, rc = PTR_ERR(exp));
1390 ted = &exp->exp_target_data;
1391 *ted->ted_lcd = *lcd;
1393 rc = tgt_client_add(env, exp, cl_idx);
1394 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1395 /* VBR: set export last committed version */
1396 exp->exp_last_committed = last_transno;
1397 spin_lock(&exp->exp_lock);
1398 exp->exp_connecting = 0;
1399 exp->exp_in_recovery = 0;
1400 spin_unlock(&exp->exp_lock);
1401 obd->obd_max_recoverable_clients++;
1403 if (tgt->lut_lsd.lsd_feature_incompat &
1404 OBD_INCOMPAT_MULTI_RPCS &&
1405 lcd->lcd_generation != 0) {
1406 /* compute the highest valid client generation */
1407 generation = max(generation, lcd->lcd_generation);
1409 /* fill client_generation <-> export hash table */
1410 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1411 &exp->exp_gen_hash);
1413 CERROR("%s: duplicate export for client "
1415 tgt_name(tgt), lcd->lcd_generation);
1416 class_export_put(exp);
1421 class_export_put(exp);
1423 /* Need to check last_rcvd even for duplicated exports. */
1424 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
1425 cl_idx, last_transno);
1427 spin_lock(&tgt->lut_translock);
1428 tgt->lut_last_transno = max(last_transno,
1429 tgt->lut_last_transno);
1430 spin_unlock(&tgt->lut_translock);
1433 /* record highest valid client generation */
1434 atomic_set(&tgt->lut_client_generation, generation);
1438 cfs_hash_putref(hash);
1443 struct server_compat_data {
1450 static struct server_compat_data tgt_scd[] = {
1451 [LDD_F_SV_TYPE_MDT] = {
1452 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1453 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1454 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1455 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1456 OBD_INCOMPAT_MULTI_RPCS,
1457 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1458 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1459 OBD_INCOMPAT_MULTI_OI,
1461 [LDD_F_SV_TYPE_OST] = {
1462 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1463 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1465 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1466 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1470 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1472 struct tgt_thread_info *tti = tgt_th_info(env);
1473 struct lr_server_data *lsd = &tgt->lut_lsd;
1474 unsigned long last_rcvd_size;
1478 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1482 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1484 /* ensure padding in the struct is the correct size */
1485 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1486 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1488 rc = server_name2index(tgt_name(tgt), &index, NULL);
1490 CERROR("%s: Can not get index from name: rc = %d\n",
1494 /* server_name2index() returns type */
1496 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1497 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1501 /* last_rcvd on OST doesn't provide reconstruct support because there
1502 * may be up to 8 in-flight write requests per single slot in
1503 * last_rcvd client data
1505 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1507 if (last_rcvd_size == 0) {
1508 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1510 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1511 sizeof(lsd->lsd_uuid));
1512 lsd->lsd_last_transno = 0;
1513 lsd->lsd_mount_count = 0;
1514 lsd->lsd_server_size = LR_SERVER_SIZE;
1515 lsd->lsd_client_start = LR_CLIENT_START;
1516 lsd->lsd_client_size = LR_CLIENT_SIZE;
1517 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1518 lsd->lsd_osd_index = index;
1519 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1520 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1522 rc = tgt_server_data_read(env, tgt);
1524 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1528 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1529 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1530 "using the wrong disk %s. Were the"
1531 " /dev/ assignments rearranged?\n",
1532 tgt->lut_obd->obd_uuid.uuid,
1537 if (lsd->lsd_osd_index != index) {
1538 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1539 "is different with the index %d in"
1540 "config log, It might be disk"
1541 "corruption!\n", tgt_name(tgt),
1542 lsd->lsd_osd_index, index);
1547 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1548 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1550 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1554 if (type == LDD_F_SV_TYPE_MDT)
1555 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1557 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1558 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1560 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1563 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1564 if (type == LDD_F_SV_TYPE_MDT &&
1565 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1566 if (last_rcvd_size > lsd->lsd_client_start) {
1567 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1568 "remove all clients for interop needs\n",
1570 rc = tgt_truncate_last_rcvd(env, tgt,
1571 lsd->lsd_client_start);
1574 last_rcvd_size = lsd->lsd_client_start;
1576 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1577 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1580 spin_lock(&tgt->lut_translock);
1581 tgt->lut_last_transno = lsd->lsd_last_transno;
1582 spin_unlock(&tgt->lut_translock);
1584 lsd->lsd_mount_count++;
1586 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1587 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
1588 tgt_name(tgt), tgt->lut_last_transno);
1589 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
1590 tgt_name(tgt), lsd->lsd_mount_count);
1591 CDEBUG(D_INODE, "%s: server data size: %u\n",
1592 tgt_name(tgt), lsd->lsd_server_size);
1593 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1594 tgt_name(tgt), lsd->lsd_client_start);
1595 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1596 tgt_name(tgt), lsd->lsd_client_size);
1597 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1598 tgt_name(tgt), last_rcvd_size);
1599 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1600 tgt_name(tgt), lsd->lsd_subdir_count);
1601 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1602 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1603 (last_rcvd_size - lsd->lsd_client_start) /
1604 lsd->lsd_client_size);
1605 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1607 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1608 lsd->lsd_client_size == 0) {
1609 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1613 if (!tgt->lut_obd->obd_replayable)
1614 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1616 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1618 GOTO(err_client, rc);
1620 spin_lock(&tgt->lut_translock);
1621 /* obd_last_committed is used for compatibility
1622 * with other lustre recovery code */
1623 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1624 spin_unlock(&tgt->lut_translock);
1626 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1627 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1629 /* save it, so mount count and last_transno is current */
1630 rc = tgt_server_data_update(env, tgt, 0);
1632 GOTO(err_client, rc);
1637 class_disconnect_exports(tgt->lut_obd);
1641 /* add credits for last_rcvd update */
1642 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1645 struct lu_target *tgt = cookie;
1646 struct tgt_session_info *tsi;
1647 struct tgt_thread_info *tti = tgt_th_info(env);
1648 struct dt_object *dto;
1651 /* if there is no session, then this transaction is not result of
1652 * request processing but some local operation */
1653 if (env->le_ses == NULL)
1656 LASSERT(tgt->lut_last_rcvd);
1657 tsi = tgt_ses_info(env);
1658 /* OFD may start transaction without export assigned */
1659 if (tsi->tsi_exp == NULL)
1662 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1665 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1666 tsi->tsi_exp->exp_target_data.ted_lr_off,
1672 rc = dt_declare_record_write(env, dto, &tti->tti_buf, 0, th);
1676 if (tsi->tsi_vbr_obj != NULL &&
1677 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1678 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1679 rc = dt_declare_version_set(env, dto, th);
1685 /* Update last_rcvd records with latests transaction data */
1686 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1689 struct lu_target *tgt = cookie;
1690 struct tgt_session_info *tsi;
1691 struct tgt_thread_info *tti = tgt_th_info(env);
1692 struct dt_object *obj = NULL;
1696 if (env->le_ses == NULL)
1699 tsi = tgt_ses_info(env);
1700 /* OFD may start transaction without export assigned */
1701 if (tsi->tsi_exp == NULL)
1704 echo_client = (tgt_ses_req(tsi) == NULL);
1706 if (tti->tti_has_trans && !echo_client) {
1707 if (tti->tti_mult_trans == 0) {
1708 CDEBUG(D_HA, "More than one transaction "LPU64"\n",
1712 /* we need another transno to be assigned */
1713 tti->tti_transno = 0;
1714 } else if (th->th_result == 0) {
1715 tti->tti_has_trans = 1;
1718 if (tsi->tsi_vbr_obj != NULL &&
1719 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1720 obj = tsi->tsi_vbr_obj;
1723 if (unlikely(echo_client)) /* echo client special case */
1724 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1727 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1732 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1734 struct tgt_thread_info *tti = tgt_th_info(env);
1735 struct lsd_reply_data *lrd = &tti->tti_lrd;
1736 unsigned long reply_data_size;
1738 struct lsd_reply_header *lrh = NULL;
1739 struct lsd_client_data *lcd = NULL;
1740 struct tg_reply_data *trd = NULL;
1743 struct cfs_hash *hash = NULL;
1744 struct obd_export *exp;
1745 struct obd_export *tmp;
1746 struct tg_export_data *ted;
1747 int reply_data_recovered = 0;
1749 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1752 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1756 GOTO(out, rc = -ENOMEM);
1758 if (reply_data_size == 0) {
1759 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1761 lrh->lrh_magic = LRH_MAGIC;
1762 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1763 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1764 rc = tgt_reply_header_write(env, tgt, lrh);
1766 CERROR("%s: error writing %s: rc = %d\n",
1767 tgt_name(tgt), REPLY_DATA, rc);
1771 rc = tgt_reply_header_read(env, tgt, lrh);
1773 CERROR("%s: error reading %s: rc = %d\n",
1774 tgt_name(tgt), REPLY_DATA, rc);
1777 if (lrh->lrh_magic != LRH_MAGIC ||
1778 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1779 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1780 CERROR("%s: invalid header in %s\n",
1781 tgt_name(tgt), REPLY_DATA);
1782 GOTO(out, rc = -EINVAL);
1785 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1787 GOTO(out, rc = -ENODEV);
1791 GOTO(out, rc = -ENOMEM);
1795 GOTO(out, rc = -ENOMEM);
1797 /* Load reply_data from disk */
1798 for (idx = 0, off = sizeof(struct lsd_reply_header);
1799 off < reply_data_size;
1800 idx++, off += sizeof(struct lsd_reply_data)) {
1801 rc = tgt_reply_data_read(env, tgt, lrd, off);
1803 CERROR("%s: error reading %s: rc = %d\n",
1804 tgt_name(tgt), REPLY_DATA, rc);
1808 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1810 /* old reply data from a disconnected client */
1813 ted = &exp->exp_target_data;
1814 mutex_lock(&ted->ted_lcd_lock);
1816 /* create in-memory reply_data and link it to
1817 * target export's reply list */
1818 tgt_set_reply_slot(tgt, idx);
1819 trd->trd_reply = *lrd;
1820 trd->trd_pre_versions[0] = 0;
1821 trd->trd_pre_versions[1] = 0;
1822 trd->trd_pre_versions[2] = 0;
1823 trd->trd_pre_versions[3] = 0;
1824 trd->trd_index = idx;
1826 list_add(&trd->trd_list, &ted->ted_reply_list);
1827 ted->ted_reply_cnt++;
1828 if (ted->ted_reply_cnt > ted->ted_reply_max)
1829 ted->ted_reply_max = ted->ted_reply_cnt;
1831 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1832 "transno %llu, client gen %u, slot idx %d\n",
1833 tgt_name(tgt), trd, lrd->lrd_xid,
1834 lrd->lrd_transno, lrd->lrd_client_gen,
1837 /* update export last committed transation */
1838 exp->exp_last_committed = max(exp->exp_last_committed,
1841 mutex_unlock(&ted->ted_lcd_lock);
1842 class_export_put(exp);
1844 /* update target last committed transaction */
1845 spin_lock(&tgt->lut_translock);
1846 tgt->lut_last_transno = max(tgt->lut_last_transno,
1848 spin_unlock(&tgt->lut_translock);
1850 reply_data_recovered++;
1854 GOTO(out, rc = -ENOMEM);
1856 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1857 tgt_name(tgt), reply_data_recovered);
1859 /* delete entries from client_generation<->export hash */
1860 spin_lock(&tgt->lut_obd->obd_dev_lock);
1861 list_for_each_entry_safe(exp, tmp,
1862 &tgt->lut_obd->obd_exports,
1864 struct tg_export_data *ted = &exp->exp_target_data;
1866 if (!hlist_unhashed(&exp->exp_gen_hash))
1868 &ted->ted_lcd->lcd_generation,
1869 &exp->exp_gen_hash);
1871 spin_unlock(&tgt->lut_obd->obd_dev_lock);
1874 spin_lock(&tgt->lut_translock);
1875 /* obd_last_committed is used for compatibility
1876 * with other lustre recovery code */
1877 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1878 spin_unlock(&tgt->lut_translock);
1884 cfs_hash_putref(hash);
1894 /* Look for a reply data matching specified request @req
1895 * A copy is returned in @trd if the pointer is not NULL
1897 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
1899 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1900 struct tg_reply_data *reply, *tmp;
1903 mutex_lock(&ted->ted_lcd_lock);
1904 list_for_each_entry_safe(reply, tmp, &ted->ted_reply_list, trd_list) {
1905 if (reply->trd_reply.lrd_xid == req->rq_xid) {
1910 if (found && trd != NULL)
1912 mutex_unlock(&ted->ted_lcd_lock);
1914 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
1915 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
1920 EXPORT_SYMBOL(tgt_lookup_reply);
1922 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
1924 struct tg_export_data *ted = &exp->exp_target_data;
1925 struct lu_target *lut = class_exp2tgt(exp);
1926 struct tg_reply_data *trd, *tmp;
1928 mutex_lock(&ted->ted_lcd_lock);
1929 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
1930 if (trd->trd_reply.lrd_xid > rcvd_xid)
1932 ted->ted_release_xid++;
1933 tgt_release_reply_data(lut, ted, trd);
1935 mutex_unlock(&ted->ted_lcd_lock);
1940 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
1942 struct tg_export_data *ted = &exp->exp_target_data;
1943 struct lu_target *lut = class_exp2tgt(exp);
1944 struct tg_reply_data *trd, *tmp;
1949 mutex_lock(&ted->ted_lcd_lock);
1950 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
1951 if (trd->trd_tag != tag)
1953 ted->ted_release_tag++;
1954 tgt_release_reply_data(lut, ted, trd);
1957 mutex_unlock(&ted->ted_lcd_lock);