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 = %llu"
468 ", last_xid = %llu, last_result = %u, last_data = %u, "
469 "last_close_transno = %llu, last_close_xid = %llu, "
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 = %llu: rc = %d\n", tgt->lut_obd->obd_name,
554 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
559 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
561 struct tgt_thread_info *tti = tgt_th_info(env);
566 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
569 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
571 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
572 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
573 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
577 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
580 struct tgt_thread_info *tti = tgt_th_info(env);
581 struct dt_object *dto;
588 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
590 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
591 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
593 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
594 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
595 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
601 * Update server data in last_rcvd
603 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
606 struct tgt_thread_info *tti = tgt_th_info(env);
613 "%s: mount_count is %llu, last_transno is %llu\n",
614 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
615 tgt->lut_last_transno);
617 /* Always save latest transno to keep it fresh */
618 spin_lock(&tgt->lut_translock);
619 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
620 spin_unlock(&tgt->lut_translock);
622 th = dt_trans_create(env, tgt->lut_bottom);
629 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
630 &tti->tti_buf, tti->tti_off, th);
634 rc = dt_trans_start(env, tgt->lut_bottom, th);
638 rc = tgt_server_data_write(env, tgt, th);
640 dt_trans_stop(env, tgt->lut_bottom, th);
642 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
643 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
644 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
647 EXPORT_SYMBOL(tgt_server_data_update);
649 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
652 struct dt_object *dt = tgt->lut_last_rcvd;
660 attr.la_valid = LA_SIZE;
662 th = dt_trans_create(env, tgt->lut_bottom);
665 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
668 rc = dt_declare_attr_set(env, dt, &attr, th);
671 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
675 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
677 rc = dt_attr_set(env, dt, &attr, th);
680 dt_trans_stop(env, tgt->lut_bottom, th);
685 static void tgt_client_epoch_update(const struct lu_env *env,
686 struct obd_export *exp)
688 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
689 struct lu_target *tgt = class_exp2tgt(exp);
691 LASSERT(tgt && tgt->lut_bottom);
692 /** VBR: set client last_epoch to current epoch */
693 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
695 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
696 tgt_client_data_update(env, exp);
700 * Update boot epoch when recovery ends
702 void tgt_boot_epoch_update(struct lu_target *tgt)
705 struct ptlrpc_request *req;
707 struct list_head client_list;
710 if (tgt->lut_obd->obd_stopping)
713 rc = lu_env_init(&env, LCT_DT_THREAD);
715 CERROR("%s: can't initialize environment: rc = %d\n",
716 tgt->lut_obd->obd_name, rc);
720 spin_lock(&tgt->lut_translock);
721 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
722 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
723 tgt->lut_lsd.lsd_start_epoch = start_epoch;
724 spin_unlock(&tgt->lut_translock);
726 INIT_LIST_HEAD(&client_list);
728 * The recovery is not yet finished and final queue can still be updated
729 * with resend requests. Move final list to separate one for processing
731 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
732 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
733 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
736 * go through list of exports participated in recovery and
737 * set new epoch for them
739 list_for_each_entry(req, &client_list, rq_list) {
740 LASSERT(!req->rq_export->exp_delayed);
741 if (!req->rq_export->exp_vbr_failed)
742 tgt_client_epoch_update(&env, req->rq_export);
744 /** return list back at once */
745 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
746 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
747 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
749 /** Clear MULTI RPCS incompatibility flag if
750 * - target is MDT and
751 * - there is no client to recover or the recovery was aborted
753 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
754 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
755 tgt->lut_obd->obd_abort_recovery))
756 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
758 /** update server epoch */
759 tgt_server_data_update(&env, tgt, 1);
764 * commit callback, need to update last_committed value
766 struct tgt_last_committed_callback {
767 struct dt_txn_commit_cb llcc_cb;
768 struct lu_target *llcc_tgt;
769 struct obd_export *llcc_exp;
773 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
774 struct dt_txn_commit_cb *cb, int err)
776 struct tgt_last_committed_callback *ccb;
778 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
780 LASSERT(ccb->llcc_exp);
781 LASSERT(ccb->llcc_tgt != NULL);
782 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
784 /* error hit, don't update last committed to provide chance to
785 * replay data after fail */
789 /* Fast path w/o spinlock, if exp_last_committed was updated
790 * with higher transno, no need to take spinlock and check,
791 * also no need to update obd_last_committed. */
792 if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
794 spin_lock(&ccb->llcc_tgt->lut_translock);
795 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
796 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
798 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
799 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
800 spin_unlock(&ccb->llcc_tgt->lut_translock);
802 ptlrpc_commit_replies(ccb->llcc_exp);
803 tgt_cancel_slc_locks(ccb->llcc_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 %lld 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 /* Slot may be not yet assigned, use case is race between Client
1053 * reconnect and forced eviction */
1054 if (ted->ted_lr_idx < 0) {
1055 CWARN("%s: client with UUID '%s' not in bitmap\n",
1056 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1060 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1061 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1062 ted->ted_lcd->lcd_uuid);
1064 /* Clear the bit _after_ zeroing out the client so we don't
1065 race with filter_client_add and zero out new clients.*/
1066 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1067 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1068 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1072 /* Do not erase record for recoverable client. */
1073 if (exp->exp_flags & OBD_OPT_FAILOVER)
1076 /* Make sure the server's last_transno is up to date.
1077 * This should be done before zeroing client slot so last_transno will
1078 * be in server data or in client data in case of failure */
1079 rc = tgt_server_data_update(env, tgt, 0);
1081 CERROR("%s: failed to update server data, skip client %s "
1082 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1083 ted->ted_lcd->lcd_uuid, rc);
1087 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1088 rc = tgt_client_data_update(env, exp);
1090 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1091 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1092 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1093 ted->ted_lr_idx, ted->ted_lr_off, rc);
1096 EXPORT_SYMBOL(tgt_client_del);
1098 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1099 struct tg_export_data *ted, struct tg_reply_data *trd,
1100 struct thandle *th, bool update_lrd_file)
1102 struct lsd_reply_data *lrd;
1105 lrd = &trd->trd_reply;
1106 /* update export last transno */
1107 mutex_lock(&ted->ted_lcd_lock);
1108 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1109 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1110 mutex_unlock(&ted->ted_lcd_lock);
1112 /* find a empty slot */
1113 i = tgt_find_free_reply_slot(tgt);
1114 if (unlikely(i < 0)) {
1115 CERROR("%s: couldn't find a slot for reply data: "
1116 "rc = %d\n", tgt_name(tgt), i);
1121 if (update_lrd_file) {
1125 /* write reply data to disk */
1126 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1127 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1128 if (unlikely(rc != 0)) {
1129 CERROR("%s: can't update %s file: rc = %d\n",
1130 tgt_name(tgt), REPLY_DATA, rc);
1134 /* add reply data to target export's reply list */
1135 mutex_lock(&ted->ted_lcd_lock);
1136 list_add(&trd->trd_list, &ted->ted_reply_list);
1137 ted->ted_reply_cnt++;
1138 if (ted->ted_reply_cnt > ted->ted_reply_max)
1139 ted->ted_reply_max = ted->ted_reply_cnt;
1140 mutex_unlock(&ted->ted_lcd_lock);
1142 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1143 "tag %hu, client gen %u, slot idx %d\n",
1144 trd, lrd->lrd_xid, lrd->lrd_transno,
1145 trd->trd_tag, lrd->lrd_client_gen, i);
1148 EXPORT_SYMBOL(tgt_add_reply_data);
1151 * last_rcvd & last_committed update callbacks
1153 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1154 struct dt_object *obj, __u64 opdata,
1155 struct thandle *th, struct ptlrpc_request *req)
1157 struct tgt_thread_info *tti = tgt_th_info(env);
1158 struct tgt_session_info *tsi = tgt_ses_info(env);
1159 struct obd_export *exp = tsi->tsi_exp;
1160 struct tg_export_data *ted;
1168 LASSERT(exp != NULL);
1169 ted = &exp->exp_target_data;
1171 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1172 if (ted->ted_lr_idx < 0 && !lw_client)
1173 /* ofd connect may cause transaction before export has
1178 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1180 /* From update replay, tti_transno should be set already */
1181 LASSERT(tti->tti_transno != 0);
1183 spin_lock(&tgt->lut_translock);
1184 if (th->th_result != 0) {
1185 if (tti->tti_transno != 0) {
1186 CERROR("%s: replay transno %llu failed: rc = %d\n",
1187 tgt_name(tgt), tti->tti_transno, th->th_result);
1189 } else if (tti->tti_transno == 0) {
1190 tti->tti_transno = ++tgt->lut_last_transno;
1192 /* should be replay */
1193 if (tti->tti_transno > tgt->lut_last_transno)
1194 tgt->lut_last_transno = tti->tti_transno;
1196 spin_unlock(&tgt->lut_translock);
1198 /** VBR: set new versions */
1199 if (th->th_result == 0 && obj != NULL) {
1200 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1201 dt_version_set(env, dto, tti->tti_transno, th);
1204 /* filling reply data */
1205 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1206 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1209 req->rq_transno = tti->tti_transno;
1210 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1213 /* if can't add callback, do sync write */
1214 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1217 /* All operations performed by LW clients are synchronous and
1218 * we store the committed transno in the last_rcvd header */
1219 spin_lock(&tgt->lut_translock);
1220 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1221 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1222 spin_unlock(&tgt->lut_translock);
1223 /* Although lightweight (LW) connections have no slot
1224 * in the last_rcvd, we still want to maintain
1225 * the in-memory lsd_client_data structure in order to
1226 * properly handle reply reconstruction. */
1227 rc = tgt_server_data_write(env, tgt, th);
1229 spin_unlock(&tgt->lut_translock);
1231 } else if (ted->ted_lr_off == 0) {
1232 CERROR("%s: client idx %d has offset %lld\n",
1233 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1237 /* Target that supports multiple reply data */
1238 if (tgt_is_multimodrpcs_client(exp)) {
1239 struct tg_reply_data *trd;
1240 struct lsd_reply_data *lrd;
1241 __u64 *pre_versions;
1245 if (unlikely(trd == NULL))
1248 /* fill reply data information */
1249 lrd = &trd->trd_reply;
1250 lrd->lrd_transno = tti->tti_transno;
1252 lrd->lrd_xid = req->rq_xid;
1253 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1254 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1255 lrd->lrd_result = th->th_result;
1256 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1257 write_update = true;
1259 LASSERT(tsi->tsi_xid != 0);
1260 lrd->lrd_xid = tsi->tsi_xid;
1261 lrd->lrd_result = tsi->tsi_result;
1262 lrd->lrd_client_gen = tsi->tsi_client_gen;
1264 pre_versions = NULL;
1265 write_update = false;
1268 lrd->lrd_data = opdata;
1270 trd->trd_pre_versions[0] = pre_versions[0];
1271 trd->trd_pre_versions[1] = pre_versions[1];
1272 trd->trd_pre_versions[2] = pre_versions[2];
1273 trd->trd_pre_versions[3] = pre_versions[3];
1276 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1282 /* Enough for update replay, let's return */
1286 mutex_lock(&ted->ted_lcd_lock);
1287 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1288 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1289 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1290 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1291 ted->ted_lcd->lcd_last_close_result = th->th_result;
1293 /* VBR: save versions in last_rcvd for reconstruct. */
1294 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1297 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1298 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1299 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1300 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1302 transno_p = &ted->ted_lcd->lcd_last_transno;
1303 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1304 ted->ted_lcd->lcd_last_result = th->th_result;
1305 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1306 * see struct ldlm_reply->lock_policy_res1; */
1307 ted->ted_lcd->lcd_last_data = opdata;
1310 /* Update transno in slot only if non-zero number, i.e. no errors */
1311 if (likely(tti->tti_transno != 0)) {
1312 /* Don't overwrite bigger transaction number with lower one.
1313 * That is not sign of problem in all cases, but in any case
1314 * this value should be monotonically increased only. */
1315 if (*transno_p > tti->tti_transno) {
1316 if (!tgt->lut_no_reconstruct) {
1317 CERROR("%s: trying to overwrite bigger transno:"
1318 "on-disk: %llu, new: %llu replay: "
1319 "%d. See LU-617.\n", tgt_name(tgt),
1320 *transno_p, tti->tti_transno,
1321 req_is_replay(req));
1322 if (req_is_replay(req)) {
1323 spin_lock(&req->rq_export->exp_lock);
1324 req->rq_export->exp_vbr_failed = 1;
1325 spin_unlock(&req->rq_export->exp_lock);
1327 mutex_unlock(&ted->ted_lcd_lock);
1328 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1331 *transno_p = tti->tti_transno;
1336 tti->tti_off = ted->ted_lr_off;
1337 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1339 mutex_unlock(&ted->ted_lcd_lock);
1343 mutex_unlock(&ted->ted_lcd_lock);
1348 * last_rcvd update for echo client simulation.
1349 * It updates last_rcvd client slot and version of object in
1350 * simple way but with all locks to simulate all drawbacks
1352 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1353 struct lu_target *tgt,
1354 struct dt_object *obj,
1356 struct obd_export *exp)
1358 struct tgt_thread_info *tti = tgt_th_info(env);
1359 struct tg_export_data *ted = &exp->exp_target_data;
1364 tti->tti_transno = 0;
1366 spin_lock(&tgt->lut_translock);
1367 if (th->th_result == 0)
1368 tti->tti_transno = ++tgt->lut_last_transno;
1369 spin_unlock(&tgt->lut_translock);
1371 /** VBR: set new versions */
1372 if (th->th_result == 0 && obj != NULL)
1373 dt_version_set(env, obj, tti->tti_transno, th);
1375 /* if can't add callback, do sync write */
1376 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1379 LASSERT(ted->ted_lr_off > 0);
1381 mutex_lock(&ted->ted_lcd_lock);
1382 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1383 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1384 ted->ted_lcd->lcd_last_result = th->th_result;
1386 tti->tti_off = ted->ted_lr_off;
1387 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1388 mutex_unlock(&ted->ted_lcd_lock);
1392 static int tgt_clients_data_init(const struct lu_env *env,
1393 struct lu_target *tgt,
1394 unsigned long last_size)
1396 struct obd_device *obd = tgt->lut_obd;
1397 struct lr_server_data *lsd = &tgt->lut_lsd;
1398 struct lsd_client_data *lcd = NULL;
1399 struct tg_export_data *ted;
1402 loff_t off = lsd->lsd_client_start;
1403 __u32 generation = 0;
1404 struct cfs_hash *hash = NULL;
1408 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1409 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1415 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1417 GOTO(err_out, rc = -ENODEV);
1419 for (cl_idx = 0; off < last_size; cl_idx++) {
1420 struct obd_export *exp;
1423 /* Don't assume off is incremented properly by
1424 * read_record(), in case sizeof(*lcd)
1425 * isn't the same as fsd->lsd_client_size. */
1426 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1427 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1429 CERROR("%s: error reading last_rcvd %s idx %d off "
1430 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1433 break; /* read error shouldn't cause startup to fail */
1436 if (lcd->lcd_uuid[0] == '\0') {
1437 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1442 last_transno = lcd_last_transno(lcd);
1444 /* These exports are cleaned up by disconnect, so they
1445 * need to be set up like real exports as connect does.
1447 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1448 " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1449 cl_idx, last_transno, lsd->lsd_last_transno,
1450 lcd_last_xid(lcd), lcd->lcd_generation);
1452 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1454 if (PTR_ERR(exp) == -EALREADY) {
1455 /* export already exists, zero out this one */
1456 CERROR("%s: Duplicate export %s!\n",
1457 tgt_name(tgt), lcd->lcd_uuid);
1460 GOTO(err_out, rc = PTR_ERR(exp));
1463 ted = &exp->exp_target_data;
1464 *ted->ted_lcd = *lcd;
1466 rc = tgt_client_add(env, exp, cl_idx);
1467 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1468 /* VBR: set export last committed version */
1469 exp->exp_last_committed = last_transno;
1470 spin_lock(&exp->exp_lock);
1471 exp->exp_connecting = 0;
1472 exp->exp_in_recovery = 0;
1473 spin_unlock(&exp->exp_lock);
1474 obd->obd_max_recoverable_clients++;
1476 if (tgt->lut_lsd.lsd_feature_incompat &
1477 OBD_INCOMPAT_MULTI_RPCS &&
1478 lcd->lcd_generation != 0) {
1479 /* compute the highest valid client generation */
1480 generation = max(generation, lcd->lcd_generation);
1481 /* fill client_generation <-> export hash table */
1482 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1483 &exp->exp_gen_hash);
1485 CERROR("%s: duplicate export for client "
1487 tgt_name(tgt), lcd->lcd_generation);
1488 class_export_put(exp);
1493 class_export_put(exp);
1495 rc = rev_import_init(exp);
1497 class_unlink_export(exp);
1501 /* Need to check last_rcvd even for duplicated exports. */
1502 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1503 cl_idx, last_transno);
1505 spin_lock(&tgt->lut_translock);
1506 tgt->lut_last_transno = max(last_transno,
1507 tgt->lut_last_transno);
1508 spin_unlock(&tgt->lut_translock);
1511 /* record highest valid client generation */
1512 atomic_set(&tgt->lut_client_generation, generation);
1516 cfs_hash_putref(hash);
1521 struct server_compat_data {
1528 static struct server_compat_data tgt_scd[] = {
1529 [LDD_F_SV_TYPE_MDT] = {
1530 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1531 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1532 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1533 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1534 OBD_INCOMPAT_MULTI_RPCS,
1535 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1536 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1537 OBD_INCOMPAT_MULTI_OI,
1539 [LDD_F_SV_TYPE_OST] = {
1540 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1541 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1543 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1544 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1548 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1550 struct tgt_thread_info *tti = tgt_th_info(env);
1551 struct lr_server_data *lsd = &tgt->lut_lsd;
1552 unsigned long last_rcvd_size;
1556 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1560 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1562 /* ensure padding in the struct is the correct size */
1563 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1564 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1566 rc = server_name2index(tgt_name(tgt), &index, NULL);
1568 CERROR("%s: Can not get index from name: rc = %d\n",
1572 /* server_name2index() returns type */
1574 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1575 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1579 /* last_rcvd on OST doesn't provide reconstruct support because there
1580 * may be up to 8 in-flight write requests per single slot in
1581 * last_rcvd client data
1583 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1585 if (last_rcvd_size == 0) {
1586 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1588 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1589 sizeof(lsd->lsd_uuid));
1590 lsd->lsd_last_transno = 0;
1591 lsd->lsd_mount_count = 0;
1592 lsd->lsd_server_size = LR_SERVER_SIZE;
1593 lsd->lsd_client_start = LR_CLIENT_START;
1594 lsd->lsd_client_size = LR_CLIENT_SIZE;
1595 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1596 lsd->lsd_osd_index = index;
1597 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1598 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1600 rc = tgt_server_data_read(env, tgt);
1602 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1606 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1607 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1608 "using the wrong disk %s. Were the"
1609 " /dev/ assignments rearranged?\n",
1610 tgt->lut_obd->obd_uuid.uuid,
1615 if (lsd->lsd_osd_index != index) {
1616 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1617 "is different with the index %d in"
1618 "config log, It might be disk"
1619 "corruption!\n", tgt_name(tgt),
1620 lsd->lsd_osd_index, index);
1625 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1626 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1628 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1632 if (type == LDD_F_SV_TYPE_MDT)
1633 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1635 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1636 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1638 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1641 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1642 if (type == LDD_F_SV_TYPE_MDT &&
1643 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1644 if (last_rcvd_size > lsd->lsd_client_start) {
1645 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1646 "remove all clients for interop needs\n",
1648 rc = tgt_truncate_last_rcvd(env, tgt,
1649 lsd->lsd_client_start);
1652 last_rcvd_size = lsd->lsd_client_start;
1654 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1655 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1658 spin_lock(&tgt->lut_translock);
1659 tgt->lut_last_transno = lsd->lsd_last_transno;
1660 spin_unlock(&tgt->lut_translock);
1662 lsd->lsd_mount_count++;
1664 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1665 CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1666 tgt_name(tgt), tgt->lut_last_transno);
1667 CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1668 tgt_name(tgt), lsd->lsd_mount_count);
1669 CDEBUG(D_INODE, "%s: server data size: %u\n",
1670 tgt_name(tgt), lsd->lsd_server_size);
1671 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1672 tgt_name(tgt), lsd->lsd_client_start);
1673 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1674 tgt_name(tgt), lsd->lsd_client_size);
1675 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1676 tgt_name(tgt), last_rcvd_size);
1677 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1678 tgt_name(tgt), lsd->lsd_subdir_count);
1679 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1680 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1681 (last_rcvd_size - lsd->lsd_client_start) /
1682 lsd->lsd_client_size);
1683 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1685 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1686 lsd->lsd_client_size == 0) {
1687 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1691 if (!tgt->lut_obd->obd_replayable)
1692 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1694 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1696 GOTO(err_client, rc);
1698 spin_lock(&tgt->lut_translock);
1699 /* obd_last_committed is used for compatibility
1700 * with other lustre recovery code */
1701 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1702 spin_unlock(&tgt->lut_translock);
1704 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1705 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1707 /* save it, so mount count and last_transno is current */
1708 rc = tgt_server_data_update(env, tgt, 0);
1710 GOTO(err_client, rc);
1715 class_disconnect_exports(tgt->lut_obd);
1719 /* add credits for last_rcvd update */
1720 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1723 struct lu_target *tgt = cookie;
1724 struct tgt_session_info *tsi;
1725 struct tgt_thread_info *tti = tgt_th_info(env);
1726 struct dt_object *dto;
1729 /* if there is no session, then this transaction is not result of
1730 * request processing but some local operation */
1731 if (env->le_ses == NULL)
1734 LASSERT(tgt->lut_last_rcvd);
1735 tsi = tgt_ses_info(env);
1736 /* OFD may start transaction without export assigned */
1737 if (tsi->tsi_exp == NULL)
1740 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1742 * Use maximum possible file offset for declaration to ensure
1743 * ZFS will reserve enough credits for a write anywhere in this
1744 * file, since we don't know where in the file the write will be
1745 * because a replay slot has not been assigned. This should be
1746 * replaced by dmu_tx_hold_append() when available.
1748 tti->tti_off = atomic_read(&tgt->lut_num_clients) * 8 *
1749 sizeof(struct lsd_reply_data);
1750 tti->tti_buf.lb_buf = NULL;
1751 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1752 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1753 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1758 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1760 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1761 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1767 if (tsi->tsi_vbr_obj != NULL &&
1768 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1769 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1770 rc = dt_declare_version_set(env, dto, th);
1776 /* Update last_rcvd records with latests transaction data */
1777 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1780 struct lu_target *tgt = cookie;
1781 struct tgt_session_info *tsi;
1782 struct tgt_thread_info *tti = tgt_th_info(env);
1783 struct dt_object *obj = NULL;
1787 if (env->le_ses == NULL)
1790 tsi = tgt_ses_info(env);
1791 /* OFD may start transaction without export assigned */
1792 if (tsi->tsi_exp == NULL)
1795 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1797 if (tti->tti_has_trans && !echo_client) {
1798 if (tti->tti_mult_trans == 0) {
1799 CDEBUG(D_HA, "More than one transaction %llu\n",
1803 /* we need another transno to be assigned */
1804 tti->tti_transno = 0;
1805 } else if (th->th_result == 0) {
1806 tti->tti_has_trans = 1;
1809 if (tsi->tsi_vbr_obj != NULL &&
1810 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1811 obj = tsi->tsi_vbr_obj;
1814 if (unlikely(echo_client)) /* echo client special case */
1815 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1818 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1823 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1825 struct tgt_thread_info *tti = tgt_th_info(env);
1826 struct lsd_reply_data *lrd = &tti->tti_lrd;
1827 unsigned long reply_data_size;
1829 struct lsd_reply_header *lrh = NULL;
1830 struct lsd_client_data *lcd = NULL;
1831 struct tg_reply_data *trd = NULL;
1834 struct cfs_hash *hash = NULL;
1835 struct obd_export *exp;
1836 struct tg_export_data *ted;
1837 int reply_data_recovered = 0;
1839 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1842 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1846 GOTO(out, rc = -ENOMEM);
1848 if (reply_data_size == 0) {
1849 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1851 lrh->lrh_magic = LRH_MAGIC;
1852 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1853 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1854 rc = tgt_reply_header_write(env, tgt, lrh);
1856 CERROR("%s: error writing %s: rc = %d\n",
1857 tgt_name(tgt), REPLY_DATA, rc);
1861 rc = tgt_reply_header_read(env, tgt, lrh);
1863 CERROR("%s: error reading %s: rc = %d\n",
1864 tgt_name(tgt), REPLY_DATA, rc);
1867 if (lrh->lrh_magic != LRH_MAGIC ||
1868 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1869 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1870 CERROR("%s: invalid header in %s\n",
1871 tgt_name(tgt), REPLY_DATA);
1872 GOTO(out, rc = -EINVAL);
1875 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1877 GOTO(out, rc = -ENODEV);
1881 GOTO(out, rc = -ENOMEM);
1885 GOTO(out, rc = -ENOMEM);
1887 /* Load reply_data from disk */
1888 for (idx = 0, off = sizeof(struct lsd_reply_header);
1889 off < reply_data_size;
1890 idx++, off += sizeof(struct lsd_reply_data)) {
1891 rc = tgt_reply_data_read(env, tgt, lrd, off);
1893 CERROR("%s: error reading %s: rc = %d\n",
1894 tgt_name(tgt), REPLY_DATA, rc);
1898 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1900 /* old reply data from a disconnected client */
1903 ted = &exp->exp_target_data;
1904 mutex_lock(&ted->ted_lcd_lock);
1906 /* create in-memory reply_data and link it to
1907 * target export's reply list */
1908 rc = tgt_set_reply_slot(tgt, idx);
1910 mutex_unlock(&ted->ted_lcd_lock);
1913 trd->trd_reply = *lrd;
1914 trd->trd_pre_versions[0] = 0;
1915 trd->trd_pre_versions[1] = 0;
1916 trd->trd_pre_versions[2] = 0;
1917 trd->trd_pre_versions[3] = 0;
1918 trd->trd_index = idx;
1920 list_add(&trd->trd_list, &ted->ted_reply_list);
1921 ted->ted_reply_cnt++;
1922 if (ted->ted_reply_cnt > ted->ted_reply_max)
1923 ted->ted_reply_max = ted->ted_reply_cnt;
1925 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1926 "transno %llu, client gen %u, slot idx %d\n",
1927 tgt_name(tgt), trd, lrd->lrd_xid,
1928 lrd->lrd_transno, lrd->lrd_client_gen,
1931 /* update export last committed transation */
1932 exp->exp_last_committed = max(exp->exp_last_committed,
1935 mutex_unlock(&ted->ted_lcd_lock);
1936 class_export_put(exp);
1938 /* update target last committed transaction */
1939 spin_lock(&tgt->lut_translock);
1940 tgt->lut_last_transno = max(tgt->lut_last_transno,
1942 spin_unlock(&tgt->lut_translock);
1944 reply_data_recovered++;
1948 GOTO(out, rc = -ENOMEM);
1950 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1951 tgt_name(tgt), reply_data_recovered);
1954 spin_lock(&tgt->lut_translock);
1955 /* obd_last_committed is used for compatibility
1956 * with other lustre recovery code */
1957 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1958 spin_unlock(&tgt->lut_translock);
1964 cfs_hash_putref(hash);
1974 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
1977 struct tg_reply_data *found = NULL;
1978 struct tg_reply_data *reply;
1980 mutex_lock(&ted->ted_lcd_lock);
1981 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
1982 if (reply->trd_reply.lrd_xid == xid) {
1987 mutex_unlock(&ted->ted_lcd_lock);
1990 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
1992 /* Look for a reply data matching specified request @req
1993 * A copy is returned in @trd if the pointer is not NULL
1995 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
1997 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1998 struct tg_reply_data *reply;
2001 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2002 if (reply != NULL) {
2008 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2009 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2014 EXPORT_SYMBOL(tgt_lookup_reply);
2016 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2018 struct tg_export_data *ted = &exp->exp_target_data;
2019 struct lu_target *lut = class_exp2tgt(exp);
2020 struct tg_reply_data *trd, *tmp;
2022 mutex_lock(&ted->ted_lcd_lock);
2023 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2024 if (trd->trd_reply.lrd_xid > rcvd_xid)
2026 ted->ted_release_xid++;
2027 tgt_release_reply_data(lut, ted, trd);
2029 mutex_unlock(&ted->ted_lcd_lock);
2034 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2036 struct tg_export_data *ted = &exp->exp_target_data;
2037 struct lu_target *lut = class_exp2tgt(exp);
2038 struct tg_reply_data *trd, *tmp;
2043 mutex_lock(&ted->ted_lcd_lock);
2044 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2045 if (trd->trd_tag != tag)
2047 ted->ted_release_tag++;
2048 tgt_release_reply_data(lut, ted, trd);
2051 mutex_unlock(&ted->ted_lcd_lock);