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, 2015, 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_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
58 spin_lock(&lut->lut_client_bitmap_lock);
60 if (lut->lut_reply_bitmap[chunk] != NULL) {
61 /* someone else already allocated the bitmap for this chunk */
62 spin_unlock(&lut->lut_client_bitmap_lock);
63 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
68 lut->lut_reply_bitmap[chunk] = bm;
70 spin_unlock(&lut->lut_client_bitmap_lock);
75 /* Look for an available reply data slot in the bitmap
77 * Allocate bitmap chunk when first used
78 * XXX algo could be improved if this routine limits performance
80 static int tgt_find_free_reply_slot(struct lu_target *lut)
87 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
88 /* allocate the bitmap chunk if necessary */
89 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
90 rc = tgt_bitmap_chunk_alloc(lut, chunk);
94 bmp = lut->lut_reply_bitmap[chunk];
96 /* look for an available slot in this chunk */
98 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
99 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
103 if (test_and_set_bit(b, bmp) == 0)
104 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
111 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
113 * Allocate the bitmap chunk if necessary
115 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
121 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
122 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
124 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
125 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
127 /* allocate the bitmap chunk if necessary */
128 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
129 rc = tgt_bitmap_chunk_alloc(lut, chunk);
134 /* mark the slot 'used' in this chunk */
135 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
136 CERROR("%s: slot %d already set in bitmap\n",
145 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
148 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
153 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
154 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
156 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
157 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
159 if (lut->lut_reply_bitmap[chunk] == NULL) {
160 CERROR("%s: slot %d not allocated\n",
165 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
166 CERROR("%s: slot %d already clear in bitmap\n",
175 /* Read header of reply_data file of target @tgt into structure @lrh */
176 static int tgt_reply_header_read(const struct lu_env *env,
177 struct lu_target *tgt,
178 struct lsd_reply_header *lrh)
181 struct lsd_reply_header buf;
182 struct tgt_thread_info *tti = tgt_th_info(env);
185 tti->tti_buf.lb_buf = &buf;
186 tti->tti_buf.lb_len = sizeof(buf);
188 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
193 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
194 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
195 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
197 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
198 "header_size=%d reply_size=%d\n",
199 tgt->lut_obd->obd_name, REPLY_DATA,
200 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
205 /* Write header into replay_data file of target @tgt from structure @lrh */
206 static int tgt_reply_header_write(const struct lu_env *env,
207 struct lu_target *tgt,
208 struct lsd_reply_header *lrh)
211 struct lsd_reply_header buf;
212 struct tgt_thread_info *tti = tgt_th_info(env);
214 struct dt_object *dto;
216 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
217 "header_size=%d reply_size=%d\n",
218 tgt->lut_obd->obd_name, REPLY_DATA,
219 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
221 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
222 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
223 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
225 th = dt_trans_create(env, tgt->lut_bottom);
231 tti->tti_buf.lb_buf = &buf;
232 tti->tti_buf.lb_len = sizeof(buf);
234 rc = dt_declare_record_write(env, tgt->lut_reply_data,
235 &tti->tti_buf, tti->tti_off, th);
239 rc = dt_trans_start(env, tgt->lut_bottom, th);
243 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
244 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
246 dt_trans_stop(env, tgt->lut_bottom, th);
250 /* Write the reply data @lrd into reply_data file of target @tgt
253 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
254 struct lsd_reply_data *lrd, loff_t off,
257 struct tgt_thread_info *tti = tgt_th_info(env);
258 struct dt_object *dto;
259 struct lsd_reply_data *buf = &tti->tti_lrd;
261 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
263 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
264 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
265 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
266 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
267 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
269 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
272 tti->tti_buf.lb_buf = buf;
273 tti->tti_buf.lb_len = sizeof(*buf);
275 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
276 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
279 /* Read the reply data from reply_data file of target @tgt at offset @off
280 * into structure @lrd
282 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
283 struct lsd_reply_data *lrd, loff_t off)
286 struct tgt_thread_info *tti = tgt_th_info(env);
287 struct lsd_reply_data *buf = &tti->tti_lrd;
290 tti->tti_buf.lb_buf = buf;
291 tti->tti_buf.lb_len = sizeof(*buf);
293 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
298 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
299 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
300 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
301 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
302 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
308 /* Free the in-memory reply data structure @trd and release
309 * the corresponding slot in the reply_data file of target @lut
310 * Called with ted_lcd_lock held
312 static void tgt_free_reply_data(struct lu_target *lut,
313 struct tg_export_data *ted,
314 struct tg_reply_data *trd)
316 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
317 "client gen %u, slot idx %d\n",
318 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
319 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
322 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
324 list_del(&trd->trd_list);
325 ted->ted_reply_cnt--;
327 tgt_clear_reply_slot(lut, trd->trd_index);
331 /* Release the reply data @trd from target @lut
332 * The reply data with the highest transno for this export
333 * is retained to ensure correctness of target recovery
334 * Called with ted_lcd_lock held
336 static void tgt_release_reply_data(struct lu_target *lut,
337 struct tg_export_data *ted,
338 struct tg_reply_data *trd)
340 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
341 "client gen %u, slot idx %d\n",
342 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
343 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
346 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
348 /* Do not free the reply data corresponding to the
349 * highest transno of this export.
350 * This ensures on-disk reply data is kept and
351 * last committed transno can be restored from disk in case
354 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
355 /* free previous retained reply */
356 if (ted->ted_reply_last != NULL)
357 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
358 /* retain the reply */
359 list_del_init(&trd->trd_list);
360 ted->ted_reply_last = trd;
362 tgt_free_reply_data(lut, ted, trd);
366 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
368 tti->tti_buf.lb_buf = &tti->tti_lsd;
369 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
370 return &tti->tti_buf;
373 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
375 tti->tti_buf.lb_buf = &tti->tti_lcd;
376 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
377 return &tti->tti_buf;
381 * Allocate in-memory data for client slot related to export.
383 int tgt_client_alloc(struct obd_export *exp)
386 LASSERT(exp != exp->exp_obd->obd_self_export);
388 spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
389 INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
391 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
392 if (exp->exp_target_data.ted_lcd == NULL)
394 /* Mark that slot is not yet valid, 0 doesn't work here */
395 exp->exp_target_data.ted_lr_idx = -1;
396 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
397 mutex_init(&exp->exp_target_data.ted_lcd_lock);
400 EXPORT_SYMBOL(tgt_client_alloc);
403 * Free in-memory data for client slot related to export.
405 void tgt_client_free(struct obd_export *exp)
407 struct tg_export_data *ted = &exp->exp_target_data;
408 struct lu_target *lut = class_exp2tgt(exp);
409 struct tg_reply_data *trd, *tmp;
411 LASSERT(exp != exp->exp_obd->obd_self_export);
413 /* free reply data */
414 mutex_lock(&ted->ted_lcd_lock);
415 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
416 tgt_release_reply_data(lut, ted, trd);
418 if (ted->ted_reply_last != NULL) {
419 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
420 ted->ted_reply_last = NULL;
422 mutex_unlock(&ted->ted_lcd_lock);
424 if (!hlist_unhashed(&exp->exp_gen_hash))
425 cfs_hash_del(exp->exp_obd->obd_gen_hash,
426 &ted->ted_lcd->lcd_generation,
429 OBD_FREE_PTR(ted->ted_lcd);
432 /* Target may have been freed (see LU-7430)
433 * Slot may be not yet assigned */
434 if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
438 /* Clear bit when lcd is freed */
439 LASSERT(lut && lut->lut_client_bitmap);
440 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
441 CERROR("%s: client %u bit already clear in bitmap\n",
442 exp->exp_obd->obd_name, ted->ted_lr_idx);
446 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
447 atomic_dec(&lut->lut_num_clients);
449 EXPORT_SYMBOL(tgt_client_free);
451 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
452 struct lsd_client_data *lcd, loff_t *off, int index)
454 struct tgt_thread_info *tti = tgt_th_info(env);
458 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
460 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
461 lcd_le_to_cpu(&tti->tti_lcd, lcd);
462 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
463 lcd->lcd_last_close_result =
464 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
467 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = "LPU64
468 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
469 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
470 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
471 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
472 lcd->lcd_last_result, lcd->lcd_last_data,
473 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
474 lcd->lcd_last_close_result, rc);
478 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
479 struct lsd_client_data *lcd, loff_t *off,
482 struct tgt_thread_info *tti = tgt_th_info(env);
483 struct dt_object *dto;
485 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
486 lcd->lcd_last_close_result =
487 ptlrpc_status_hton(lcd->lcd_last_close_result);
488 lcd_cpu_to_le(lcd, &tti->tti_lcd);
491 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
492 return dt_record_write(env, dto, &tti->tti_buf, off, th);
496 * Update client data in last_rcvd
498 static int tgt_client_data_update(const struct lu_env *env,
499 struct obd_export *exp)
501 struct tg_export_data *ted = &exp->exp_target_data;
502 struct lu_target *tgt = class_exp2tgt(exp);
503 struct tgt_thread_info *tti = tgt_th_info(env);
509 if (unlikely(tgt == NULL)) {
510 CDEBUG(D_ERROR, "%s: No target for connected export\n",
511 class_exp2obd(exp)->obd_name);
515 th = dt_trans_create(env, tgt->lut_bottom);
520 mutex_lock(&ted->ted_lcd_lock);
521 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
523 ted->ted_lr_off, th);
527 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
531 * Until this operations will be committed the sync is needed
532 * for this export. This should be done _after_ starting the
533 * transaction so that many connecting clients will not bring
534 * server down with lots of sync writes.
536 rc = tgt_new_client_cb_add(th, exp);
538 /* can't add callback, do sync now */
541 spin_lock(&exp->exp_lock);
542 exp->exp_need_sync = 1;
543 spin_unlock(&exp->exp_lock);
546 tti->tti_off = ted->ted_lr_off;
547 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
550 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 = "LPU64": 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 = "LPU64": 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 = "LPU64": 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 "LPU64", last_transno is "LPU64"\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 = "LPU64": 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_transno);
805 spin_unlock(&ccb->llcc_tgt->lut_translock);
808 class_export_cb_put(ccb->llcc_exp);
809 if (ccb->llcc_transno)
810 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
811 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
815 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
816 struct obd_export *exp, __u64 transno)
818 struct tgt_last_committed_callback *ccb;
819 struct dt_txn_commit_cb *dcb;
827 ccb->llcc_exp = class_export_cb_get(exp);
828 ccb->llcc_transno = transno;
831 dcb->dcb_func = tgt_cb_last_committed;
832 INIT_LIST_HEAD(&dcb->dcb_linkage);
833 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
835 rc = dt_trans_cb_add(th, dcb);
837 class_export_cb_put(exp);
841 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
842 /* report failure to force synchronous operation */
848 struct tgt_new_client_callback {
849 struct dt_txn_commit_cb lncc_cb;
850 struct obd_export *lncc_exp;
853 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
854 struct dt_txn_commit_cb *cb, int err)
856 struct tgt_new_client_callback *ccb;
858 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
860 LASSERT(ccb->lncc_exp->exp_obd);
862 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
863 ccb->lncc_exp->exp_obd->obd_name,
864 ccb->lncc_exp->exp_client_uuid.uuid);
866 spin_lock(&ccb->lncc_exp->exp_lock);
868 ccb->lncc_exp->exp_need_sync = 0;
870 spin_unlock(&ccb->lncc_exp->exp_lock);
871 class_export_cb_put(ccb->lncc_exp);
876 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
878 struct tgt_new_client_callback *ccb;
879 struct dt_txn_commit_cb *dcb;
886 ccb->lncc_exp = class_export_cb_get(exp);
889 dcb->dcb_func = tgt_cb_new_client;
890 INIT_LIST_HEAD(&dcb->dcb_linkage);
891 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
893 rc = dt_trans_cb_add(th, dcb);
895 class_export_cb_put(exp);
902 * Add new client to the last_rcvd upon new connection.
904 * We use a bitmap to locate a free space in the last_rcvd file and initialize
907 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
909 struct tg_export_data *ted = &exp->exp_target_data;
910 struct lu_target *tgt = class_exp2tgt(exp);
915 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
916 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
919 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
922 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
923 * there's no need for extra complication here
925 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
927 if (idx >= LR_MAX_CLIENTS ||
928 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
929 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
930 tgt->lut_obd->obd_name, idx);
933 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
934 idx = find_next_zero_bit(tgt->lut_client_bitmap,
935 LR_MAX_CLIENTS, idx);
939 ted->ted_lr_idx = idx;
940 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
941 idx * tgt->lut_lsd.lsd_client_size;
943 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
945 if (tgt_is_multimodrpcs_client(exp)) {
946 /* Set MULTI RPCS incompatibility flag to prevent previous
947 * Lustre versions to mount a target with reply_data file */
948 atomic_inc(&tgt->lut_num_clients);
949 if (!(tgt->lut_lsd.lsd_feature_incompat &
950 OBD_INCOMPAT_MULTI_RPCS)) {
951 tgt->lut_lsd.lsd_feature_incompat |=
952 OBD_INCOMPAT_MULTI_RPCS;
953 rc = tgt_server_data_update(env, tgt, 1);
955 CERROR("%s: unable to set MULTI RPCS "
956 "incompatibility flag\n",
957 exp->exp_obd->obd_name);
962 /* assign client slot generation */
963 ted->ted_lcd->lcd_generation =
964 atomic_inc_return(&tgt->lut_client_generation);
966 ted->ted_lcd->lcd_generation = 0;
969 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
971 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
972 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
974 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
977 rc = tgt_client_data_update(env, exp);
979 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
980 tgt->lut_obd->obd_name, idx, rc);
984 EXPORT_SYMBOL(tgt_client_new);
986 /* Add an existing client to the MDS in-memory state based on
987 * a client that was previously found in the last_rcvd file and
988 * already has an assigned slot (idx >= 0).
990 * It should not be possible to fail adding an existing client - otherwise
991 * mdt_init_server_data() callsite needs to be fixed.
993 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
995 struct tg_export_data *ted = &exp->exp_target_data;
996 struct lu_target *tgt = class_exp2tgt(exp);
1000 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1001 LASSERTF(idx >= 0, "%d\n", idx);
1003 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1004 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1007 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1008 CERROR("%s: client %d: bit already set in bitmap!!\n",
1009 tgt->lut_obd->obd_name, idx);
1012 atomic_inc(&tgt->lut_num_clients);
1014 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1016 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1017 ted->ted_lcd->lcd_generation);
1019 ted->ted_lr_idx = idx;
1020 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1021 idx * tgt->lut_lsd.lsd_client_size;
1023 mutex_init(&ted->ted_lcd_lock);
1025 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1030 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1032 struct tg_export_data *ted = &exp->exp_target_data;
1033 struct lu_target *tgt = class_exp2tgt(exp);
1038 LASSERT(ted->ted_lcd);
1040 if (unlikely(tgt == NULL)) {
1041 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1042 class_exp2obd(exp)->obd_name);
1046 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1047 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1048 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1049 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1052 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1053 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1054 ted->ted_lcd->lcd_uuid);
1056 /* Clear the bit _after_ zeroing out the client so we don't
1057 race with filter_client_add and zero out new clients.*/
1058 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1059 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1060 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1064 /* Do not erase record for recoverable client. */
1065 if (exp->exp_flags & OBD_OPT_FAILOVER)
1068 /* Make sure the server's last_transno is up to date.
1069 * This should be done before zeroing client slot so last_transno will
1070 * be in server data or in client data in case of failure */
1071 rc = tgt_server_data_update(env, tgt, 0);
1073 CERROR("%s: failed to update server data, skip client %s "
1074 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1075 ted->ted_lcd->lcd_uuid, rc);
1079 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1080 rc = tgt_client_data_update(env, exp);
1082 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1083 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1084 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1085 ted->ted_lr_idx, ted->ted_lr_off, rc);
1088 EXPORT_SYMBOL(tgt_client_del);
1090 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1091 struct tg_export_data *ted, struct tg_reply_data *trd,
1092 struct thandle *th, bool update_lrd_file)
1094 struct lsd_reply_data *lrd;
1097 lrd = &trd->trd_reply;
1098 /* update export last transno */
1099 mutex_lock(&ted->ted_lcd_lock);
1100 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1101 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1102 mutex_unlock(&ted->ted_lcd_lock);
1104 /* find a empty slot */
1105 i = tgt_find_free_reply_slot(tgt);
1106 if (unlikely(i < 0)) {
1107 CERROR("%s: couldn't find a slot for reply data: "
1108 "rc = %d\n", tgt_name(tgt), i);
1113 if (update_lrd_file) {
1117 /* write reply data to disk */
1118 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1119 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1120 if (unlikely(rc != 0)) {
1121 CERROR("%s: can't update %s file: rc = %d\n",
1122 tgt_name(tgt), REPLY_DATA, rc);
1126 /* add reply data to target export's reply list */
1127 mutex_lock(&ted->ted_lcd_lock);
1128 list_add(&trd->trd_list, &ted->ted_reply_list);
1129 ted->ted_reply_cnt++;
1130 if (ted->ted_reply_cnt > ted->ted_reply_max)
1131 ted->ted_reply_max = ted->ted_reply_cnt;
1132 mutex_unlock(&ted->ted_lcd_lock);
1134 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1135 "tag %hu, client gen %u, slot idx %d\n",
1136 trd, lrd->lrd_xid, lrd->lrd_transno,
1137 trd->trd_tag, lrd->lrd_client_gen, i);
1140 EXPORT_SYMBOL(tgt_add_reply_data);
1143 * last_rcvd & last_committed update callbacks
1145 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1146 struct dt_object *obj, __u64 opdata,
1147 struct thandle *th, struct ptlrpc_request *req)
1149 struct tgt_thread_info *tti = tgt_th_info(env);
1150 struct tgt_session_info *tsi = tgt_ses_info(env);
1151 struct obd_export *exp = tsi->tsi_exp;
1152 struct tg_export_data *ted;
1160 LASSERT(exp != NULL);
1161 ted = &exp->exp_target_data;
1163 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1164 if (ted->ted_lr_idx < 0 && !lw_client)
1165 /* ofd connect may cause transaction before export has
1170 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1172 /* From update replay, tti_transno should be set already */
1173 LASSERT(tti->tti_transno != 0);
1175 spin_lock(&tgt->lut_translock);
1176 if (th->th_result != 0) {
1177 if (tti->tti_transno != 0) {
1178 CERROR("%s: replay transno "LPU64" failed: rc = %d\n",
1179 tgt_name(tgt), tti->tti_transno, th->th_result);
1181 } else if (tti->tti_transno == 0) {
1182 tti->tti_transno = ++tgt->lut_last_transno;
1184 /* should be replay */
1185 if (tti->tti_transno > tgt->lut_last_transno)
1186 tgt->lut_last_transno = tti->tti_transno;
1188 spin_unlock(&tgt->lut_translock);
1190 /** VBR: set new versions */
1191 if (th->th_result == 0 && obj != NULL) {
1192 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1193 dt_version_set(env, dto, tti->tti_transno, th);
1196 /* filling reply data */
1197 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
1198 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1201 req->rq_transno = tti->tti_transno;
1202 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1205 /* if can't add callback, do sync write */
1206 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1209 /* All operations performed by LW clients are synchronous and
1210 * we store the committed transno in the last_rcvd header */
1211 spin_lock(&tgt->lut_translock);
1212 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1213 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1214 spin_unlock(&tgt->lut_translock);
1215 /* Although lightweight (LW) connections have no slot
1216 * in the last_rcvd, we still want to maintain
1217 * the in-memory lsd_client_data structure in order to
1218 * properly handle reply reconstruction. */
1219 rc = tgt_server_data_write(env, tgt, th);
1221 spin_unlock(&tgt->lut_translock);
1223 } else if (ted->ted_lr_off == 0) {
1224 CERROR("%s: client idx %d has offset %lld\n",
1225 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1229 /* Target that supports multiple reply data */
1230 if (tgt_is_multimodrpcs_client(exp)) {
1231 struct tg_reply_data *trd;
1232 struct lsd_reply_data *lrd;
1233 __u64 *pre_versions;
1237 if (unlikely(trd == NULL))
1240 /* fill reply data information */
1241 lrd = &trd->trd_reply;
1242 lrd->lrd_transno = tti->tti_transno;
1244 lrd->lrd_xid = req->rq_xid;
1245 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1246 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1247 lrd->lrd_result = th->th_result;
1248 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1249 write_update = true;
1251 LASSERT(tsi->tsi_xid != 0);
1252 lrd->lrd_xid = tsi->tsi_xid;
1253 lrd->lrd_result = tsi->tsi_result;
1254 lrd->lrd_client_gen = tsi->tsi_client_gen;
1256 pre_versions = NULL;
1257 write_update = false;
1260 lrd->lrd_data = opdata;
1262 trd->trd_pre_versions[0] = pre_versions[0];
1263 trd->trd_pre_versions[1] = pre_versions[1];
1264 trd->trd_pre_versions[2] = pre_versions[2];
1265 trd->trd_pre_versions[3] = pre_versions[3];
1268 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1274 /* Enough for update replay, let's return */
1278 mutex_lock(&ted->ted_lcd_lock);
1279 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1280 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1281 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1282 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1283 ted->ted_lcd->lcd_last_close_result = th->th_result;
1285 /* VBR: save versions in last_rcvd for reconstruct. */
1286 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1289 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1290 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1291 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1292 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1294 transno_p = &ted->ted_lcd->lcd_last_transno;
1295 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1296 ted->ted_lcd->lcd_last_result = th->th_result;
1297 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1298 * see struct ldlm_reply->lock_policy_res1; */
1299 ted->ted_lcd->lcd_last_data = opdata;
1302 /* Update transno in slot only if non-zero number, i.e. no errors */
1303 if (likely(tti->tti_transno != 0)) {
1304 /* Don't overwrite bigger transaction number with lower one.
1305 * That is not sign of problem in all cases, but in any case
1306 * this value should be monotonically increased only. */
1307 if (*transno_p > tti->tti_transno) {
1308 if (!tgt->lut_no_reconstruct) {
1309 CERROR("%s: trying to overwrite bigger transno:"
1310 "on-disk: "LPU64", new: "LPU64" replay: "
1311 "%d. See LU-617.\n", tgt_name(tgt),
1312 *transno_p, tti->tti_transno,
1313 req_is_replay(req));
1314 if (req_is_replay(req)) {
1315 spin_lock(&req->rq_export->exp_lock);
1316 req->rq_export->exp_vbr_failed = 1;
1317 spin_unlock(&req->rq_export->exp_lock);
1319 mutex_unlock(&ted->ted_lcd_lock);
1320 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1323 *transno_p = tti->tti_transno;
1328 tti->tti_off = ted->ted_lr_off;
1329 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1331 mutex_unlock(&ted->ted_lcd_lock);
1335 mutex_unlock(&ted->ted_lcd_lock);
1340 * last_rcvd update for echo client simulation.
1341 * It updates last_rcvd client slot and version of object in
1342 * simple way but with all locks to simulate all drawbacks
1344 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1345 struct lu_target *tgt,
1346 struct dt_object *obj,
1348 struct obd_export *exp)
1350 struct tgt_thread_info *tti = tgt_th_info(env);
1351 struct tg_export_data *ted = &exp->exp_target_data;
1356 tti->tti_transno = 0;
1358 spin_lock(&tgt->lut_translock);
1359 if (th->th_result == 0)
1360 tti->tti_transno = ++tgt->lut_last_transno;
1361 spin_unlock(&tgt->lut_translock);
1363 /** VBR: set new versions */
1364 if (th->th_result == 0 && obj != NULL)
1365 dt_version_set(env, obj, tti->tti_transno, th);
1367 /* if can't add callback, do sync write */
1368 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1371 LASSERT(ted->ted_lr_off > 0);
1373 mutex_lock(&ted->ted_lcd_lock);
1374 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1375 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1376 ted->ted_lcd->lcd_last_result = th->th_result;
1378 tti->tti_off = ted->ted_lr_off;
1379 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1380 mutex_unlock(&ted->ted_lcd_lock);
1384 static int tgt_clients_data_init(const struct lu_env *env,
1385 struct lu_target *tgt,
1386 unsigned long last_size)
1388 struct obd_device *obd = tgt->lut_obd;
1389 struct lr_server_data *lsd = &tgt->lut_lsd;
1390 struct lsd_client_data *lcd = NULL;
1391 struct tg_export_data *ted;
1394 loff_t off = lsd->lsd_client_start;
1395 __u32 generation = 0;
1396 struct cfs_hash *hash = NULL;
1400 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1401 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1407 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1409 GOTO(err_out, rc = -ENODEV);
1411 for (cl_idx = 0; off < last_size; cl_idx++) {
1412 struct obd_export *exp;
1415 /* Don't assume off is incremented properly by
1416 * read_record(), in case sizeof(*lcd)
1417 * isn't the same as fsd->lsd_client_size. */
1418 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1419 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1421 CERROR("%s: error reading last_rcvd %s idx %d off "
1422 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1425 break; /* read error shouldn't cause startup to fail */
1428 if (lcd->lcd_uuid[0] == '\0') {
1429 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1434 last_transno = lcd_last_transno(lcd);
1436 /* These exports are cleaned up by disconnect, so they
1437 * need to be set up like real exports as connect does.
1439 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
1440 " srv lr: "LPU64" lx: "LPU64" gen %u\n", lcd->lcd_uuid,
1441 cl_idx, last_transno, lsd->lsd_last_transno,
1442 lcd_last_xid(lcd), lcd->lcd_generation);
1444 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1446 if (PTR_ERR(exp) == -EALREADY) {
1447 /* export already exists, zero out this one */
1448 CERROR("%s: Duplicate export %s!\n",
1449 tgt_name(tgt), lcd->lcd_uuid);
1452 GOTO(err_out, rc = PTR_ERR(exp));
1455 ted = &exp->exp_target_data;
1456 *ted->ted_lcd = *lcd;
1458 rc = tgt_client_add(env, exp, cl_idx);
1459 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1460 /* VBR: set export last committed version */
1461 exp->exp_last_committed = last_transno;
1462 spin_lock(&exp->exp_lock);
1463 exp->exp_connecting = 0;
1464 exp->exp_in_recovery = 0;
1465 spin_unlock(&exp->exp_lock);
1466 obd->obd_max_recoverable_clients++;
1468 if (tgt->lut_lsd.lsd_feature_incompat &
1469 OBD_INCOMPAT_MULTI_RPCS &&
1470 lcd->lcd_generation != 0) {
1471 /* compute the highest valid client generation */
1472 generation = max(generation, lcd->lcd_generation);
1473 /* fill client_generation <-> export hash table */
1474 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1475 &exp->exp_gen_hash);
1477 CERROR("%s: duplicate export for client "
1479 tgt_name(tgt), lcd->lcd_generation);
1480 class_export_put(exp);
1485 class_export_put(exp);
1487 rc = rev_import_init(exp);
1489 class_unlink_export(exp);
1493 /* Need to check last_rcvd even for duplicated exports. */
1494 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
1495 cl_idx, last_transno);
1497 spin_lock(&tgt->lut_translock);
1498 tgt->lut_last_transno = max(last_transno,
1499 tgt->lut_last_transno);
1500 spin_unlock(&tgt->lut_translock);
1503 /* record highest valid client generation */
1504 atomic_set(&tgt->lut_client_generation, generation);
1508 cfs_hash_putref(hash);
1513 struct server_compat_data {
1520 static struct server_compat_data tgt_scd[] = {
1521 [LDD_F_SV_TYPE_MDT] = {
1522 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1523 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1524 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1525 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1526 OBD_INCOMPAT_MULTI_RPCS,
1527 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1528 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1529 OBD_INCOMPAT_MULTI_OI,
1531 [LDD_F_SV_TYPE_OST] = {
1532 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1533 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1535 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1536 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1540 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1542 struct tgt_thread_info *tti = tgt_th_info(env);
1543 struct lr_server_data *lsd = &tgt->lut_lsd;
1544 unsigned long last_rcvd_size;
1548 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1552 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1554 /* ensure padding in the struct is the correct size */
1555 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1556 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1558 rc = server_name2index(tgt_name(tgt), &index, NULL);
1560 CERROR("%s: Can not get index from name: rc = %d\n",
1564 /* server_name2index() returns type */
1566 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1567 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1571 /* last_rcvd on OST doesn't provide reconstruct support because there
1572 * may be up to 8 in-flight write requests per single slot in
1573 * last_rcvd client data
1575 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1577 if (last_rcvd_size == 0) {
1578 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1580 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1581 sizeof(lsd->lsd_uuid));
1582 lsd->lsd_last_transno = 0;
1583 lsd->lsd_mount_count = 0;
1584 lsd->lsd_server_size = LR_SERVER_SIZE;
1585 lsd->lsd_client_start = LR_CLIENT_START;
1586 lsd->lsd_client_size = LR_CLIENT_SIZE;
1587 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1588 lsd->lsd_osd_index = index;
1589 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1590 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1592 rc = tgt_server_data_read(env, tgt);
1594 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1598 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1599 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1600 "using the wrong disk %s. Were the"
1601 " /dev/ assignments rearranged?\n",
1602 tgt->lut_obd->obd_uuid.uuid,
1607 if (lsd->lsd_osd_index != index) {
1608 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1609 "is different with the index %d in"
1610 "config log, It might be disk"
1611 "corruption!\n", tgt_name(tgt),
1612 lsd->lsd_osd_index, index);
1617 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1618 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1620 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1624 if (type == LDD_F_SV_TYPE_MDT)
1625 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1627 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1628 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1630 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1633 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1634 if (type == LDD_F_SV_TYPE_MDT &&
1635 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1636 if (last_rcvd_size > lsd->lsd_client_start) {
1637 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1638 "remove all clients for interop needs\n",
1640 rc = tgt_truncate_last_rcvd(env, tgt,
1641 lsd->lsd_client_start);
1644 last_rcvd_size = lsd->lsd_client_start;
1646 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1647 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1650 spin_lock(&tgt->lut_translock);
1651 tgt->lut_last_transno = lsd->lsd_last_transno;
1652 spin_unlock(&tgt->lut_translock);
1654 lsd->lsd_mount_count++;
1656 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1657 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
1658 tgt_name(tgt), tgt->lut_last_transno);
1659 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
1660 tgt_name(tgt), lsd->lsd_mount_count);
1661 CDEBUG(D_INODE, "%s: server data size: %u\n",
1662 tgt_name(tgt), lsd->lsd_server_size);
1663 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1664 tgt_name(tgt), lsd->lsd_client_start);
1665 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1666 tgt_name(tgt), lsd->lsd_client_size);
1667 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1668 tgt_name(tgt), last_rcvd_size);
1669 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1670 tgt_name(tgt), lsd->lsd_subdir_count);
1671 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1672 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1673 (last_rcvd_size - lsd->lsd_client_start) /
1674 lsd->lsd_client_size);
1675 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1677 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1678 lsd->lsd_client_size == 0) {
1679 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1683 if (!tgt->lut_obd->obd_replayable)
1684 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1686 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1688 GOTO(err_client, rc);
1690 spin_lock(&tgt->lut_translock);
1691 /* obd_last_committed is used for compatibility
1692 * with other lustre recovery code */
1693 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1694 spin_unlock(&tgt->lut_translock);
1696 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1697 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1699 /* save it, so mount count and last_transno is current */
1700 rc = tgt_server_data_update(env, tgt, 0);
1702 GOTO(err_client, rc);
1707 class_disconnect_exports(tgt->lut_obd);
1711 /* add credits for last_rcvd update */
1712 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1715 struct lu_target *tgt = cookie;
1716 struct tgt_session_info *tsi;
1717 struct tgt_thread_info *tti = tgt_th_info(env);
1718 struct dt_object *dto;
1721 /* if there is no session, then this transaction is not result of
1722 * request processing but some local operation */
1723 if (env->le_ses == NULL)
1726 LASSERT(tgt->lut_last_rcvd);
1727 tsi = tgt_ses_info(env);
1728 /* OFD may start transaction without export assigned */
1729 if (tsi->tsi_exp == NULL)
1732 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1734 * Use maximum possible file offset for declaration to ensure
1735 * ZFS will reserve enough credits for a write anywhere in this
1736 * file, since we don't know where in the file the write will be
1737 * because a replay slot has not been assigned. This should be
1738 * replaced by dmu_tx_hold_append() when available.
1740 tti->tti_off = atomic_read(&tgt->lut_num_clients) * 8 *
1741 sizeof(struct lsd_reply_data);
1742 tti->tti_buf.lb_buf = NULL;
1743 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1744 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1745 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1750 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1752 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1753 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1759 if (tsi->tsi_vbr_obj != NULL &&
1760 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1761 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1762 rc = dt_declare_version_set(env, dto, th);
1768 /* Update last_rcvd records with latests transaction data */
1769 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1772 struct lu_target *tgt = cookie;
1773 struct tgt_session_info *tsi;
1774 struct tgt_thread_info *tti = tgt_th_info(env);
1775 struct dt_object *obj = NULL;
1779 if (env->le_ses == NULL)
1782 tsi = tgt_ses_info(env);
1783 /* OFD may start transaction without export assigned */
1784 if (tsi->tsi_exp == NULL)
1787 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1789 if (tti->tti_has_trans && !echo_client) {
1790 if (tti->tti_mult_trans == 0) {
1791 CDEBUG(D_HA, "More than one transaction "LPU64"\n",
1795 /* we need another transno to be assigned */
1796 tti->tti_transno = 0;
1797 } else if (th->th_result == 0) {
1798 tti->tti_has_trans = 1;
1801 if (tsi->tsi_vbr_obj != NULL &&
1802 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1803 obj = tsi->tsi_vbr_obj;
1806 if (unlikely(echo_client)) /* echo client special case */
1807 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1810 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1815 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1817 struct tgt_thread_info *tti = tgt_th_info(env);
1818 struct lsd_reply_data *lrd = &tti->tti_lrd;
1819 unsigned long reply_data_size;
1821 struct lsd_reply_header *lrh = NULL;
1822 struct lsd_client_data *lcd = NULL;
1823 struct tg_reply_data *trd = NULL;
1826 struct cfs_hash *hash = NULL;
1827 struct obd_export *exp;
1828 struct tg_export_data *ted;
1829 int reply_data_recovered = 0;
1831 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1834 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1838 GOTO(out, rc = -ENOMEM);
1840 if (reply_data_size == 0) {
1841 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1843 lrh->lrh_magic = LRH_MAGIC;
1844 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1845 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1846 rc = tgt_reply_header_write(env, tgt, lrh);
1848 CERROR("%s: error writing %s: rc = %d\n",
1849 tgt_name(tgt), REPLY_DATA, rc);
1853 rc = tgt_reply_header_read(env, tgt, lrh);
1855 CERROR("%s: error reading %s: rc = %d\n",
1856 tgt_name(tgt), REPLY_DATA, rc);
1859 if (lrh->lrh_magic != LRH_MAGIC ||
1860 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1861 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1862 CERROR("%s: invalid header in %s\n",
1863 tgt_name(tgt), REPLY_DATA);
1864 GOTO(out, rc = -EINVAL);
1867 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1869 GOTO(out, rc = -ENODEV);
1873 GOTO(out, rc = -ENOMEM);
1877 GOTO(out, rc = -ENOMEM);
1879 /* Load reply_data from disk */
1880 for (idx = 0, off = sizeof(struct lsd_reply_header);
1881 off < reply_data_size;
1882 idx++, off += sizeof(struct lsd_reply_data)) {
1883 rc = tgt_reply_data_read(env, tgt, lrd, off);
1885 CERROR("%s: error reading %s: rc = %d\n",
1886 tgt_name(tgt), REPLY_DATA, rc);
1890 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1892 /* old reply data from a disconnected client */
1895 ted = &exp->exp_target_data;
1896 mutex_lock(&ted->ted_lcd_lock);
1898 /* create in-memory reply_data and link it to
1899 * target export's reply list */
1900 rc = tgt_set_reply_slot(tgt, idx);
1902 mutex_unlock(&ted->ted_lcd_lock);
1905 trd->trd_reply = *lrd;
1906 trd->trd_pre_versions[0] = 0;
1907 trd->trd_pre_versions[1] = 0;
1908 trd->trd_pre_versions[2] = 0;
1909 trd->trd_pre_versions[3] = 0;
1910 trd->trd_index = idx;
1912 list_add(&trd->trd_list, &ted->ted_reply_list);
1913 ted->ted_reply_cnt++;
1914 if (ted->ted_reply_cnt > ted->ted_reply_max)
1915 ted->ted_reply_max = ted->ted_reply_cnt;
1917 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1918 "transno %llu, client gen %u, slot idx %d\n",
1919 tgt_name(tgt), trd, lrd->lrd_xid,
1920 lrd->lrd_transno, lrd->lrd_client_gen,
1923 /* update export last committed transation */
1924 exp->exp_last_committed = max(exp->exp_last_committed,
1927 mutex_unlock(&ted->ted_lcd_lock);
1928 class_export_put(exp);
1930 /* update target last committed transaction */
1931 spin_lock(&tgt->lut_translock);
1932 tgt->lut_last_transno = max(tgt->lut_last_transno,
1934 spin_unlock(&tgt->lut_translock);
1936 reply_data_recovered++;
1940 GOTO(out, rc = -ENOMEM);
1942 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1943 tgt_name(tgt), reply_data_recovered);
1946 spin_lock(&tgt->lut_translock);
1947 /* obd_last_committed is used for compatibility
1948 * with other lustre recovery code */
1949 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1950 spin_unlock(&tgt->lut_translock);
1956 cfs_hash_putref(hash);
1966 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
1969 struct tg_reply_data *found = NULL;
1970 struct tg_reply_data *reply;
1972 mutex_lock(&ted->ted_lcd_lock);
1973 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
1974 if (reply->trd_reply.lrd_xid == xid) {
1979 mutex_unlock(&ted->ted_lcd_lock);
1982 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
1984 /* Look for a reply data matching specified request @req
1985 * A copy is returned in @trd if the pointer is not NULL
1987 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
1989 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1990 struct tg_reply_data *reply;
1993 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
1994 if (reply != NULL) {
2000 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2001 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2006 EXPORT_SYMBOL(tgt_lookup_reply);
2008 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2010 struct tg_export_data *ted = &exp->exp_target_data;
2011 struct lu_target *lut = class_exp2tgt(exp);
2012 struct tg_reply_data *trd, *tmp;
2014 mutex_lock(&ted->ted_lcd_lock);
2015 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2016 if (trd->trd_reply.lrd_xid > rcvd_xid)
2018 ted->ted_release_xid++;
2019 tgt_release_reply_data(lut, ted, trd);
2021 mutex_unlock(&ted->ted_lcd_lock);
2026 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2028 struct tg_export_data *ted = &exp->exp_target_data;
2029 struct lu_target *lut = class_exp2tgt(exp);
2030 struct tg_reply_data *trd, *tmp;
2035 mutex_lock(&ted->ted_lcd_lock);
2036 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2037 if (trd->trd_tag != tag)
2039 ted->ted_release_tag++;
2040 tgt_release_reply_data(lut, ted, trd);
2043 mutex_unlock(&ted->ted_lcd_lock);