4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Lustre Unified Target
33 * These are common function to work with last_received file
35 * Author: Mikhail Pershin <mike.pershin@intel.com>
38 #include <obd_class.h>
39 #include <lustre_fid.h>
41 #include "tgt_internal.h"
43 /** version recovery epoch */
44 #define LR_EPOCH_BITS 32
46 /* Allocate a bitmap for a chunk of reply data slots */
47 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
51 OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
56 spin_lock(&lut->lut_client_bitmap_lock);
58 if (lut->lut_reply_bitmap[chunk] != NULL) {
59 /* someone else already allocated the bitmap for this chunk */
60 spin_unlock(&lut->lut_client_bitmap_lock);
61 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
66 lut->lut_reply_bitmap[chunk] = bm;
68 spin_unlock(&lut->lut_client_bitmap_lock);
73 /* Look for an available reply data slot in the bitmap
75 * Allocate bitmap chunk when first used
76 * XXX algo could be improved if this routine limits performance
78 static int tgt_find_free_reply_slot(struct lu_target *lut)
85 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
86 /* allocate the bitmap chunk if necessary */
87 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
88 rc = tgt_bitmap_chunk_alloc(lut, chunk);
92 bmp = lut->lut_reply_bitmap[chunk];
94 /* look for an available slot in this chunk */
96 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
97 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
101 if (test_and_set_bit(b, bmp) == 0)
102 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
109 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
111 * Allocate the bitmap chunk if necessary
113 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
119 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
120 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
122 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
123 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
125 /* allocate the bitmap chunk if necessary */
126 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
127 rc = tgt_bitmap_chunk_alloc(lut, chunk);
132 /* mark the slot 'used' in this chunk */
133 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
134 CERROR("%s: slot %d already set in bitmap\n",
143 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
146 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
151 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
152 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
154 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
155 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
157 if (lut->lut_reply_bitmap[chunk] == NULL) {
158 CERROR("%s: slot %d not allocated\n",
163 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
164 CERROR("%s: slot %d already clear in bitmap\n",
173 /* Read header of reply_data file of target @tgt into structure @lrh */
174 static int tgt_reply_header_read(const struct lu_env *env,
175 struct lu_target *tgt,
176 struct lsd_reply_header *lrh)
179 struct lsd_reply_header buf;
180 struct tgt_thread_info *tti = tgt_th_info(env);
183 tti->tti_buf.lb_buf = &buf;
184 tti->tti_buf.lb_len = sizeof(buf);
186 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
191 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
192 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
193 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
195 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
196 "header_size=%d reply_size=%d\n",
197 tgt->lut_obd->obd_name, REPLY_DATA,
198 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
203 /* Write header into replay_data file of target @tgt from structure @lrh */
204 static int tgt_reply_header_write(const struct lu_env *env,
205 struct lu_target *tgt,
206 struct lsd_reply_header *lrh)
209 struct lsd_reply_header buf;
210 struct tgt_thread_info *tti = tgt_th_info(env);
212 struct dt_object *dto;
214 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
215 "header_size=%d reply_size=%d\n",
216 tgt->lut_obd->obd_name, REPLY_DATA,
217 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
219 if (tgt->lut_bottom->dd_rdonly)
222 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
223 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
224 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
226 th = dt_trans_create(env, tgt->lut_bottom);
232 tti->tti_buf.lb_buf = &buf;
233 tti->tti_buf.lb_len = sizeof(buf);
235 rc = dt_declare_record_write(env, tgt->lut_reply_data,
236 &tti->tti_buf, tti->tti_off, th);
240 rc = dt_trans_start(env, tgt->lut_bottom, th);
244 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
245 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
247 dt_trans_stop(env, tgt->lut_bottom, th);
251 /* Write the reply data @lrd into reply_data file of target @tgt
254 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
255 struct lsd_reply_data *lrd, loff_t off,
258 struct tgt_thread_info *tti = tgt_th_info(env);
259 struct dt_object *dto;
260 struct lsd_reply_data *buf = &tti->tti_lrd;
262 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
264 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
265 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
266 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
267 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
268 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
270 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
273 tti->tti_buf.lb_buf = buf;
274 tti->tti_buf.lb_len = sizeof(*buf);
276 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
277 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
280 /* Read the reply data from reply_data file of target @tgt at offset @off
281 * into structure @lrd
283 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
284 struct lsd_reply_data *lrd, loff_t off)
287 struct tgt_thread_info *tti = tgt_th_info(env);
288 struct lsd_reply_data *buf = &tti->tti_lrd;
291 tti->tti_buf.lb_buf = buf;
292 tti->tti_buf.lb_len = sizeof(*buf);
294 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
299 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
300 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
301 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
302 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
303 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
309 /* Free the in-memory reply data structure @trd and release
310 * the corresponding slot in the reply_data file of target @lut
311 * Called with ted_lcd_lock held
313 static void tgt_free_reply_data(struct lu_target *lut,
314 struct tg_export_data *ted,
315 struct tg_reply_data *trd)
317 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
318 "client gen %u, slot idx %d\n",
319 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
320 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
323 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
325 list_del(&trd->trd_list);
326 ted->ted_reply_cnt--;
328 tgt_clear_reply_slot(lut, trd->trd_index);
332 /* Release the reply data @trd from target @lut
333 * The reply data with the highest transno for this export
334 * is retained to ensure correctness of target recovery
335 * Called with ted_lcd_lock held
337 static void tgt_release_reply_data(struct lu_target *lut,
338 struct tg_export_data *ted,
339 struct tg_reply_data *trd)
341 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
342 "client gen %u, slot idx %d\n",
343 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
344 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
347 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
349 /* Do not free the reply data corresponding to the
350 * highest transno of this export.
351 * This ensures on-disk reply data is kept and
352 * last committed transno can be restored from disk in case
355 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
356 /* free previous retained reply */
357 if (ted->ted_reply_last != NULL)
358 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
359 /* retain the reply */
360 list_del_init(&trd->trd_list);
361 ted->ted_reply_last = trd;
363 tgt_free_reply_data(lut, ted, trd);
367 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
369 tti->tti_buf.lb_buf = &tti->tti_lsd;
370 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
371 return &tti->tti_buf;
374 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
376 tti->tti_buf.lb_buf = &tti->tti_lcd;
377 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
378 return &tti->tti_buf;
382 * Allocate in-memory data for client slot related to export.
384 int tgt_client_alloc(struct obd_export *exp)
387 LASSERT(exp != exp->exp_obd->obd_self_export);
389 spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
390 INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
392 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
393 if (exp->exp_target_data.ted_lcd == NULL)
395 /* Mark that slot is not yet valid, 0 doesn't work here */
396 exp->exp_target_data.ted_lr_idx = -1;
397 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
398 mutex_init(&exp->exp_target_data.ted_lcd_lock);
401 EXPORT_SYMBOL(tgt_client_alloc);
404 * Free in-memory data for client slot related to export.
406 void tgt_client_free(struct obd_export *exp)
408 struct tg_export_data *ted = &exp->exp_target_data;
409 struct lu_target *lut = class_exp2tgt(exp);
410 struct tg_reply_data *trd, *tmp;
412 LASSERT(exp != exp->exp_obd->obd_self_export);
414 /* free reply data */
415 mutex_lock(&ted->ted_lcd_lock);
416 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
417 tgt_release_reply_data(lut, ted, trd);
419 if (ted->ted_reply_last != NULL) {
420 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
421 ted->ted_reply_last = NULL;
423 mutex_unlock(&ted->ted_lcd_lock);
425 if (!hlist_unhashed(&exp->exp_gen_hash))
426 cfs_hash_del(exp->exp_obd->obd_gen_hash,
427 &ted->ted_lcd->lcd_generation,
430 OBD_FREE_PTR(ted->ted_lcd);
433 /* Target may have been freed (see LU-7430)
434 * Slot may be not yet assigned */
435 if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
439 /* Clear bit when lcd is freed */
440 LASSERT(lut && lut->lut_client_bitmap);
441 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
442 CERROR("%s: client %u bit already clear in bitmap\n",
443 exp->exp_obd->obd_name, ted->ted_lr_idx);
447 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
448 atomic_dec(&lut->lut_num_clients);
450 EXPORT_SYMBOL(tgt_client_free);
452 static inline void tgt_check_lcd(const char *obd_name, int index,
453 struct lsd_client_data *lcd)
455 size_t uuid_size = sizeof(lcd->lcd_uuid);
457 if (strnlen((char*)lcd->lcd_uuid, uuid_size) == uuid_size) {
458 lcd->lcd_uuid[uuid_size - 1] = '\0';
460 LCONSOLE_ERROR("the client UUID (%s) on %s for exports stored in last_rcvd(index = %d) is bad!\n",
461 lcd->lcd_uuid, obd_name, index);
465 static int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
466 struct lsd_client_data *lcd,
467 loff_t *off, int index)
469 struct tgt_thread_info *tti = tgt_th_info(env);
473 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
475 tgt_check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
476 lcd_le_to_cpu(&tti->tti_lcd, lcd);
477 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
478 lcd->lcd_last_close_result =
479 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
482 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = %llu"
483 ", last_xid = %llu, last_result = %u, last_data = %u, "
484 "last_close_transno = %llu, last_close_xid = %llu, "
485 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
486 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
487 lcd->lcd_last_result, lcd->lcd_last_data,
488 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
489 lcd->lcd_last_close_result, rc);
493 static int tgt_client_data_write(const struct lu_env *env,
494 struct lu_target *tgt,
495 struct lsd_client_data *lcd,
496 loff_t *off, struct thandle *th)
498 struct tgt_thread_info *tti = tgt_th_info(env);
499 struct dt_object *dto;
501 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
502 lcd->lcd_last_close_result =
503 ptlrpc_status_hton(lcd->lcd_last_close_result);
504 lcd_cpu_to_le(lcd, &tti->tti_lcd);
507 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
508 return dt_record_write(env, dto, &tti->tti_buf, off, th);
511 struct tgt_new_client_callback {
512 struct dt_txn_commit_cb lncc_cb;
513 struct obd_export *lncc_exp;
516 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
517 struct dt_txn_commit_cb *cb, int err)
519 struct tgt_new_client_callback *ccb;
521 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
523 LASSERT(ccb->lncc_exp->exp_obd);
525 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
526 ccb->lncc_exp->exp_obd->obd_name,
527 ccb->lncc_exp->exp_client_uuid.uuid);
529 spin_lock(&ccb->lncc_exp->exp_lock);
531 ccb->lncc_exp->exp_need_sync = 0;
533 spin_unlock(&ccb->lncc_exp->exp_lock);
534 class_export_cb_put(ccb->lncc_exp);
539 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
541 struct tgt_new_client_callback *ccb;
542 struct dt_txn_commit_cb *dcb;
549 ccb->lncc_exp = class_export_cb_get(exp);
552 dcb->dcb_func = tgt_cb_new_client;
553 INIT_LIST_HEAD(&dcb->dcb_linkage);
554 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
556 rc = dt_trans_cb_add(th, dcb);
558 class_export_cb_put(exp);
565 * Update client data in last_rcvd
567 static int tgt_client_data_update(const struct lu_env *env,
568 struct obd_export *exp)
570 struct tg_export_data *ted = &exp->exp_target_data;
571 struct lu_target *tgt = class_exp2tgt(exp);
572 struct tgt_thread_info *tti = tgt_th_info(env);
578 if (unlikely(tgt == NULL)) {
579 CDEBUG(D_ERROR, "%s: No target for connected export\n",
580 class_exp2obd(exp)->obd_name);
584 if (tgt->lut_bottom->dd_rdonly)
587 th = dt_trans_create(env, tgt->lut_bottom);
592 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
594 ted->ted_lr_off, th);
598 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
602 mutex_lock(&ted->ted_lcd_lock);
605 * Until this operations will be committed the sync is needed
606 * for this export. This should be done _after_ starting the
607 * transaction so that many connecting clients will not bring
608 * server down with lots of sync writes.
610 rc = tgt_new_client_cb_add(th, exp);
612 /* can't add callback, do sync now */
615 spin_lock(&exp->exp_lock);
616 exp->exp_need_sync = 1;
617 spin_unlock(&exp->exp_lock);
620 tti->tti_off = ted->ted_lr_off;
621 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
623 mutex_unlock(&ted->ted_lcd_lock);
627 dt_trans_stop(env, tgt->lut_bottom, th);
628 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
629 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
630 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
635 static int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
637 struct tgt_thread_info *tti = tgt_th_info(env);
642 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
645 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
647 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
648 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
649 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
653 static int tgt_server_data_write(const struct lu_env *env,
654 struct lu_target *tgt, struct thandle *th)
656 struct tgt_thread_info *tti = tgt_th_info(env);
657 struct dt_object *dto;
664 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
666 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
667 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
669 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
670 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
671 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
677 * Update server data in last_rcvd
679 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
682 struct tgt_thread_info *tti = tgt_th_info(env);
689 "%s: mount_count is %llu, last_transno is %llu\n",
690 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
691 tgt->lut_last_transno);
693 /* Always save latest transno to keep it fresh */
694 spin_lock(&tgt->lut_translock);
695 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
696 spin_unlock(&tgt->lut_translock);
698 if (tgt->lut_bottom->dd_rdonly)
701 th = dt_trans_create(env, tgt->lut_bottom);
708 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
709 &tti->tti_buf, tti->tti_off, th);
713 rc = dt_trans_start(env, tgt->lut_bottom, th);
717 rc = tgt_server_data_write(env, tgt, th);
719 dt_trans_stop(env, tgt->lut_bottom, th);
721 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
722 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
723 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
726 EXPORT_SYMBOL(tgt_server_data_update);
728 static int tgt_truncate_last_rcvd(const struct lu_env *env,
729 struct lu_target *tgt, loff_t size)
731 struct dt_object *dt = tgt->lut_last_rcvd;
738 if (tgt->lut_bottom->dd_rdonly)
742 attr.la_valid = LA_SIZE;
744 th = dt_trans_create(env, tgt->lut_bottom);
747 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
750 rc = dt_declare_attr_set(env, dt, &attr, th);
753 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
757 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
759 rc = dt_attr_set(env, dt, &attr, th);
762 dt_trans_stop(env, tgt->lut_bottom, th);
767 static void tgt_client_epoch_update(const struct lu_env *env,
768 struct obd_export *exp)
770 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
771 struct lu_target *tgt = class_exp2tgt(exp);
773 LASSERT(tgt && tgt->lut_bottom);
774 /** VBR: set client last_epoch to current epoch */
775 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
777 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
778 tgt_client_data_update(env, exp);
782 * Update boot epoch when recovery ends
784 void tgt_boot_epoch_update(struct lu_target *tgt)
787 struct ptlrpc_request *req;
789 struct list_head client_list;
792 if (tgt->lut_obd->obd_stopping)
795 rc = lu_env_init(&env, LCT_DT_THREAD);
797 CERROR("%s: can't initialize environment: rc = %d\n",
798 tgt->lut_obd->obd_name, rc);
802 spin_lock(&tgt->lut_translock);
803 start_epoch = (tgt->lut_last_transno >> LR_EPOCH_BITS) + 1;
804 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
805 tgt->lut_lsd.lsd_start_epoch = start_epoch;
806 spin_unlock(&tgt->lut_translock);
808 INIT_LIST_HEAD(&client_list);
810 * The recovery is not yet finished and final queue can still be updated
811 * with resend requests. Move final list to separate one for processing
813 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
814 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
815 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
818 * go through list of exports participated in recovery and
819 * set new epoch for them
821 list_for_each_entry(req, &client_list, rq_list) {
822 LASSERT(!req->rq_export->exp_delayed);
823 if (!req->rq_export->exp_vbr_failed)
824 tgt_client_epoch_update(&env, req->rq_export);
826 /** return list back at once */
827 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
828 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
829 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
831 /** Clear MULTI RPCS incompatibility flag if
832 * - target is MDT and
833 * - there is no client to recover or the recovery was aborted
835 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
836 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
837 tgt->lut_obd->obd_abort_recovery))
838 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
840 /** update server epoch */
841 tgt_server_data_update(&env, tgt, 1);
846 * commit callback, need to update last_committed value
848 struct tgt_last_committed_callback {
849 struct dt_txn_commit_cb llcc_cb;
850 struct lu_target *llcc_tgt;
851 struct obd_export *llcc_exp;
855 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
856 struct dt_txn_commit_cb *cb, int err)
858 struct tgt_last_committed_callback *ccb;
860 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
862 LASSERT(ccb->llcc_exp);
863 LASSERT(ccb->llcc_tgt != NULL);
864 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
866 /* error hit, don't update last committed to provide chance to
867 * replay data after fail */
871 /* Fast path w/o spinlock, if exp_last_committed was updated
872 * with higher transno, no need to take spinlock and check,
873 * also no need to update obd_last_committed. */
874 if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
876 spin_lock(&ccb->llcc_tgt->lut_translock);
877 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
878 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
880 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
881 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
882 spin_unlock(&ccb->llcc_tgt->lut_translock);
884 ptlrpc_commit_replies(ccb->llcc_exp);
885 tgt_cancel_slc_locks(ccb->llcc_tgt, ccb->llcc_transno);
887 spin_unlock(&ccb->llcc_tgt->lut_translock);
890 CDEBUG(D_HA, "%s: transno %lld is committed\n",
891 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
894 class_export_cb_put(ccb->llcc_exp);
899 * Add commit callback function, it returns a non-zero value to inform
900 * caller to use sync transaction if necessary.
902 static int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
903 struct obd_export *exp, __u64 transno)
905 struct tgt_last_committed_callback *ccb;
906 struct dt_txn_commit_cb *dcb;
914 ccb->llcc_exp = class_export_cb_get(exp);
915 ccb->llcc_transno = transno;
918 dcb->dcb_func = tgt_cb_last_committed;
919 INIT_LIST_HEAD(&dcb->dcb_linkage);
920 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
922 rc = dt_trans_cb_add(th, dcb);
924 class_export_cb_put(exp);
928 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
929 /* report failure to force synchronous operation */
932 /* if exp_need_sync is set, return non-zero value to force
933 * a sync transaction. */
934 return rc ? rc : exp->exp_need_sync;
938 * Add new client to the last_rcvd upon new connection.
940 * We use a bitmap to locate a free space in the last_rcvd file and initialize
943 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
945 struct tg_export_data *ted = &exp->exp_target_data;
946 struct lu_target *tgt = class_exp2tgt(exp);
951 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
952 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
955 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
958 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
959 * there's no need for extra complication here
961 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
963 if (idx >= LR_MAX_CLIENTS ||
964 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
965 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
966 tgt->lut_obd->obd_name, idx);
969 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
970 idx = find_next_zero_bit(tgt->lut_client_bitmap,
971 LR_MAX_CLIENTS, idx);
975 ted->ted_lr_idx = idx;
976 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
977 idx * tgt->lut_lsd.lsd_client_size;
979 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
981 if (tgt_is_multimodrpcs_client(exp)) {
982 /* Set MULTI RPCS incompatibility flag to prevent previous
983 * Lustre versions to mount a target with reply_data file */
984 atomic_inc(&tgt->lut_num_clients);
985 if (!(tgt->lut_lsd.lsd_feature_incompat &
986 OBD_INCOMPAT_MULTI_RPCS)) {
987 tgt->lut_lsd.lsd_feature_incompat |=
988 OBD_INCOMPAT_MULTI_RPCS;
989 rc = tgt_server_data_update(env, tgt, 1);
991 CERROR("%s: unable to set MULTI RPCS "
992 "incompatibility flag\n",
993 exp->exp_obd->obd_name);
998 /* assign client slot generation */
999 ted->ted_lcd->lcd_generation =
1000 atomic_inc_return(&tgt->lut_client_generation);
1002 ted->ted_lcd->lcd_generation = 0;
1005 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
1007 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1008 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
1010 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
1013 rc = tgt_client_data_update(env, exp);
1015 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
1016 tgt->lut_obd->obd_name, idx, rc);
1020 EXPORT_SYMBOL(tgt_client_new);
1022 /* Add an existing client to the MDS in-memory state based on
1023 * a client that was previously found in the last_rcvd file and
1024 * already has an assigned slot (idx >= 0).
1026 * It should not be possible to fail adding an existing client - otherwise
1027 * mdt_init_server_data() callsite needs to be fixed.
1029 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
1031 struct tg_export_data *ted = &exp->exp_target_data;
1032 struct lu_target *tgt = class_exp2tgt(exp);
1036 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1037 LASSERTF(idx >= 0, "%d\n", idx);
1039 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1040 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1043 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1044 CERROR("%s: client %d: bit already set in bitmap!!\n",
1045 tgt->lut_obd->obd_name, idx);
1048 atomic_inc(&tgt->lut_num_clients);
1050 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1052 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1053 ted->ted_lcd->lcd_generation);
1055 ted->ted_lr_idx = idx;
1056 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1057 idx * tgt->lut_lsd.lsd_client_size;
1059 mutex_init(&ted->ted_lcd_lock);
1061 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1066 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1068 struct tg_export_data *ted = &exp->exp_target_data;
1069 struct lu_target *tgt = class_exp2tgt(exp);
1074 LASSERT(ted->ted_lcd);
1076 if (unlikely(tgt == NULL)) {
1077 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1078 class_exp2obd(exp)->obd_name);
1082 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1083 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1084 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1085 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1088 /* Slot may be not yet assigned, use case is race between Client
1089 * reconnect and forced eviction */
1090 if (ted->ted_lr_idx < 0) {
1091 CWARN("%s: client with UUID '%s' not in bitmap\n",
1092 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1096 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1097 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1098 ted->ted_lcd->lcd_uuid);
1100 /* Clear the bit _after_ zeroing out the client so we don't
1101 race with filter_client_add and zero out new clients.*/
1102 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1103 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1104 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1108 /* Do not erase record for recoverable client. */
1109 if (exp->exp_flags & OBD_OPT_FAILOVER)
1112 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1115 /* Make sure the server's last_transno is up to date.
1116 * This should be done before zeroing client slot so last_transno will
1117 * be in server data or in client data in case of failure */
1118 rc = tgt_server_data_update(env, tgt, 0);
1120 CERROR("%s: failed to update server data, skip client %s "
1121 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1122 ted->ted_lcd->lcd_uuid, rc);
1126 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1127 rc = tgt_client_data_update(env, exp);
1129 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1130 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1131 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1132 ted->ted_lr_idx, ted->ted_lr_off, rc);
1135 EXPORT_SYMBOL(tgt_client_del);
1137 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1138 struct tg_export_data *ted, struct tg_reply_data *trd,
1139 struct thandle *th, bool update_lrd_file)
1141 struct lsd_reply_data *lrd;
1144 lrd = &trd->trd_reply;
1145 /* update export last transno */
1146 mutex_lock(&ted->ted_lcd_lock);
1147 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1148 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1149 mutex_unlock(&ted->ted_lcd_lock);
1151 /* find a empty slot */
1152 i = tgt_find_free_reply_slot(tgt);
1153 if (unlikely(i < 0)) {
1154 CERROR("%s: couldn't find a slot for reply data: "
1155 "rc = %d\n", tgt_name(tgt), i);
1160 if (update_lrd_file) {
1164 /* write reply data to disk */
1165 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1166 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1167 if (unlikely(rc != 0)) {
1168 CERROR("%s: can't update %s file: rc = %d\n",
1169 tgt_name(tgt), REPLY_DATA, rc);
1173 /* add reply data to target export's reply list */
1174 mutex_lock(&ted->ted_lcd_lock);
1175 list_add(&trd->trd_list, &ted->ted_reply_list);
1176 ted->ted_reply_cnt++;
1177 if (ted->ted_reply_cnt > ted->ted_reply_max)
1178 ted->ted_reply_max = ted->ted_reply_cnt;
1179 mutex_unlock(&ted->ted_lcd_lock);
1181 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1182 "tag %hu, client gen %u, slot idx %d\n",
1183 trd, lrd->lrd_xid, lrd->lrd_transno,
1184 trd->trd_tag, lrd->lrd_client_gen, i);
1187 EXPORT_SYMBOL(tgt_add_reply_data);
1190 * last_rcvd & last_committed update callbacks
1192 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1193 struct dt_object *obj, __u64 opdata,
1194 struct thandle *th, struct ptlrpc_request *req)
1196 struct tgt_thread_info *tti = tgt_th_info(env);
1197 struct tgt_session_info *tsi = tgt_ses_info(env);
1198 struct obd_export *exp = tsi->tsi_exp;
1199 struct tg_export_data *ted;
1207 LASSERT(exp != NULL);
1208 ted = &exp->exp_target_data;
1210 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1211 if (ted->ted_lr_idx < 0 && !lw_client)
1212 /* ofd connect may cause transaction before export has
1217 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1219 /* From update replay, tti_transno should be set already */
1220 LASSERT(tti->tti_transno != 0);
1222 spin_lock(&tgt->lut_translock);
1223 if (th->th_result != 0) {
1224 if (tti->tti_transno != 0) {
1225 CERROR("%s: replay transno %llu failed: rc = %d\n",
1226 tgt_name(tgt), tti->tti_transno, th->th_result);
1228 } else if (tti->tti_transno == 0) {
1229 tti->tti_transno = ++tgt->lut_last_transno;
1231 /* should be replay */
1232 if (tti->tti_transno > tgt->lut_last_transno)
1233 tgt->lut_last_transno = tti->tti_transno;
1235 spin_unlock(&tgt->lut_translock);
1237 /** VBR: set new versions */
1238 if (th->th_result == 0 && obj != NULL) {
1239 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1240 dt_version_set(env, dto, tti->tti_transno, th);
1243 /* filling reply data */
1244 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1245 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1248 req->rq_transno = tti->tti_transno;
1249 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1252 /* if can't add callback, do sync write */
1253 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1256 /* All operations performed by LW clients are synchronous and
1257 * we store the committed transno in the last_rcvd header */
1258 spin_lock(&tgt->lut_translock);
1259 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1260 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1261 spin_unlock(&tgt->lut_translock);
1262 /* Although lightweight (LW) connections have no slot
1263 * in the last_rcvd, we still want to maintain
1264 * the in-memory lsd_client_data structure in order to
1265 * properly handle reply reconstruction. */
1266 rc = tgt_server_data_write(env, tgt, th);
1268 spin_unlock(&tgt->lut_translock);
1270 } else if (ted->ted_lr_off == 0) {
1271 CERROR("%s: client idx %d has offset %lld\n",
1272 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1276 /* Target that supports multiple reply data */
1277 if (tgt_is_multimodrpcs_client(exp)) {
1278 struct tg_reply_data *trd;
1279 struct lsd_reply_data *lrd;
1280 __u64 *pre_versions;
1284 if (unlikely(trd == NULL))
1287 /* fill reply data information */
1288 lrd = &trd->trd_reply;
1289 lrd->lrd_transno = tti->tti_transno;
1291 lrd->lrd_xid = req->rq_xid;
1292 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1293 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1294 lrd->lrd_result = th->th_result;
1295 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1296 write_update = true;
1298 LASSERT(tsi->tsi_xid != 0);
1299 lrd->lrd_xid = tsi->tsi_xid;
1300 lrd->lrd_result = tsi->tsi_result;
1301 lrd->lrd_client_gen = tsi->tsi_client_gen;
1303 pre_versions = NULL;
1304 write_update = false;
1307 lrd->lrd_data = opdata;
1309 trd->trd_pre_versions[0] = pre_versions[0];
1310 trd->trd_pre_versions[1] = pre_versions[1];
1311 trd->trd_pre_versions[2] = pre_versions[2];
1312 trd->trd_pre_versions[3] = pre_versions[3];
1315 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1321 /* Enough for update replay, let's return */
1325 mutex_lock(&ted->ted_lcd_lock);
1326 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1327 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1328 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1329 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1330 ted->ted_lcd->lcd_last_close_result = th->th_result;
1332 /* VBR: save versions in last_rcvd for reconstruct. */
1333 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1336 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1337 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1338 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1339 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1341 transno_p = &ted->ted_lcd->lcd_last_transno;
1342 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1343 ted->ted_lcd->lcd_last_result = th->th_result;
1344 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1345 * see struct ldlm_reply->lock_policy_res1; */
1346 ted->ted_lcd->lcd_last_data = opdata;
1349 /* Update transno in slot only if non-zero number, i.e. no errors */
1350 if (likely(tti->tti_transno != 0)) {
1351 /* Don't overwrite bigger transaction number with lower one.
1352 * That is not sign of problem in all cases, but in any case
1353 * this value should be monotonically increased only. */
1354 if (*transno_p > tti->tti_transno) {
1355 if (!tgt->lut_no_reconstruct) {
1356 CERROR("%s: trying to overwrite bigger transno:"
1357 "on-disk: %llu, new: %llu replay: "
1358 "%d. See LU-617.\n", tgt_name(tgt),
1359 *transno_p, tti->tti_transno,
1360 req_is_replay(req));
1361 if (req_is_replay(req)) {
1362 spin_lock(&req->rq_export->exp_lock);
1363 req->rq_export->exp_vbr_failed = 1;
1364 spin_unlock(&req->rq_export->exp_lock);
1366 mutex_unlock(&ted->ted_lcd_lock);
1367 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1370 *transno_p = tti->tti_transno;
1375 tti->tti_off = ted->ted_lr_off;
1376 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1379 rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1382 mutex_unlock(&ted->ted_lcd_lock);
1386 mutex_unlock(&ted->ted_lcd_lock);
1391 * last_rcvd update for echo client simulation.
1392 * It updates last_rcvd client slot and version of object in
1393 * simple way but with all locks to simulate all drawbacks
1395 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1396 struct lu_target *tgt,
1397 struct dt_object *obj,
1399 struct obd_export *exp)
1401 struct tgt_thread_info *tti = tgt_th_info(env);
1402 struct tg_export_data *ted = &exp->exp_target_data;
1407 tti->tti_transno = 0;
1409 spin_lock(&tgt->lut_translock);
1410 if (th->th_result == 0)
1411 tti->tti_transno = ++tgt->lut_last_transno;
1412 spin_unlock(&tgt->lut_translock);
1414 /** VBR: set new versions */
1415 if (th->th_result == 0 && obj != NULL)
1416 dt_version_set(env, obj, tti->tti_transno, th);
1418 /* if can't add callback, do sync write */
1419 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1422 LASSERT(ted->ted_lr_off > 0);
1424 mutex_lock(&ted->ted_lcd_lock);
1425 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1426 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1427 ted->ted_lcd->lcd_last_result = th->th_result;
1429 tti->tti_off = ted->ted_lr_off;
1430 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1431 mutex_unlock(&ted->ted_lcd_lock);
1435 static int tgt_clients_data_init(const struct lu_env *env,
1436 struct lu_target *tgt,
1437 unsigned long last_size)
1439 struct obd_device *obd = tgt->lut_obd;
1440 struct lr_server_data *lsd = &tgt->lut_lsd;
1441 struct lsd_client_data *lcd = NULL;
1442 struct tg_export_data *ted;
1445 loff_t off = lsd->lsd_client_start;
1446 __u32 generation = 0;
1447 struct cfs_hash *hash = NULL;
1451 if (tgt->lut_bottom->dd_rdonly)
1454 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1455 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1461 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1463 GOTO(err_out, rc = -ENODEV);
1465 for (cl_idx = 0; off < last_size; cl_idx++) {
1466 struct obd_export *exp;
1469 /* Don't assume off is incremented properly by
1470 * read_record(), in case sizeof(*lcd)
1471 * isn't the same as fsd->lsd_client_size. */
1472 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1473 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1475 CERROR("%s: error reading last_rcvd %s idx %d off "
1476 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1479 break; /* read error shouldn't cause startup to fail */
1482 if (lcd->lcd_uuid[0] == '\0') {
1483 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1488 last_transno = lcd_last_transno(lcd);
1490 /* These exports are cleaned up by disconnect, so they
1491 * need to be set up like real exports as connect does.
1493 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1494 " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1495 cl_idx, last_transno, lsd->lsd_last_transno,
1496 lcd_last_xid(lcd), lcd->lcd_generation);
1498 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1500 if (PTR_ERR(exp) == -EALREADY) {
1501 /* export already exists, zero out this one */
1502 CERROR("%s: Duplicate export %s!\n",
1503 tgt_name(tgt), lcd->lcd_uuid);
1506 GOTO(err_out, rc = PTR_ERR(exp));
1509 ted = &exp->exp_target_data;
1510 *ted->ted_lcd = *lcd;
1512 rc = tgt_client_add(env, exp, cl_idx);
1513 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1514 /* VBR: set export last committed version */
1515 exp->exp_last_committed = last_transno;
1516 spin_lock(&exp->exp_lock);
1517 exp->exp_connecting = 0;
1518 exp->exp_in_recovery = 0;
1519 spin_unlock(&exp->exp_lock);
1520 obd->obd_max_recoverable_clients++;
1522 if (tgt->lut_lsd.lsd_feature_incompat &
1523 OBD_INCOMPAT_MULTI_RPCS &&
1524 lcd->lcd_generation != 0) {
1525 /* compute the highest valid client generation */
1526 generation = max(generation, lcd->lcd_generation);
1527 /* fill client_generation <-> export hash table */
1528 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1529 &exp->exp_gen_hash);
1531 CERROR("%s: duplicate export for client "
1533 tgt_name(tgt), lcd->lcd_generation);
1534 class_export_put(exp);
1539 class_export_put(exp);
1541 rc = rev_import_init(exp);
1543 class_unlink_export(exp);
1547 /* Need to check last_rcvd even for duplicated exports. */
1548 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1549 cl_idx, last_transno);
1551 spin_lock(&tgt->lut_translock);
1552 tgt->lut_last_transno = max(last_transno,
1553 tgt->lut_last_transno);
1554 spin_unlock(&tgt->lut_translock);
1557 /* record highest valid client generation */
1558 atomic_set(&tgt->lut_client_generation, generation);
1562 cfs_hash_putref(hash);
1567 struct server_compat_data {
1574 static struct server_compat_data tgt_scd[] = {
1575 [LDD_F_SV_TYPE_MDT] = {
1576 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1577 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1578 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1579 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1580 OBD_INCOMPAT_MULTI_RPCS,
1581 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1582 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1583 OBD_INCOMPAT_MULTI_OI,
1585 [LDD_F_SV_TYPE_OST] = {
1586 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1587 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1589 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1590 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1594 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1596 struct tgt_thread_info *tti = tgt_th_info(env);
1597 struct lr_server_data *lsd = &tgt->lut_lsd;
1598 unsigned long last_rcvd_size;
1602 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1606 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1608 /* ensure padding in the struct is the correct size */
1609 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1610 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1612 rc = server_name2index(tgt_name(tgt), &index, NULL);
1614 CERROR("%s: Can not get index from name: rc = %d\n",
1618 /* server_name2index() returns type */
1620 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1621 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1625 /* last_rcvd on OST doesn't provide reconstruct support because there
1626 * may be up to 8 in-flight write requests per single slot in
1627 * last_rcvd client data
1629 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1631 if (last_rcvd_size == 0) {
1632 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1634 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1635 sizeof(lsd->lsd_uuid));
1636 lsd->lsd_last_transno = 0;
1637 lsd->lsd_mount_count = 0;
1638 lsd->lsd_server_size = LR_SERVER_SIZE;
1639 lsd->lsd_client_start = LR_CLIENT_START;
1640 lsd->lsd_client_size = LR_CLIENT_SIZE;
1641 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1642 lsd->lsd_osd_index = index;
1643 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1644 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1646 rc = tgt_server_data_read(env, tgt);
1648 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1652 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1653 if (tgt->lut_bottom->dd_rdonly) {
1654 /* Such difference may be caused by mounting
1655 * up snapshot with new fsname under rd_only
1656 * mode. But even if it was NOT, it will not
1657 * damage the system because of "rd_only". */
1658 memcpy(lsd->lsd_uuid,
1659 tgt->lut_obd->obd_uuid.uuid,
1660 sizeof(lsd->lsd_uuid));
1662 LCONSOLE_ERROR_MSG(0x157, "Trying to start "
1663 "OBD %s using the wrong "
1664 "disk %s. Were the /dev/ "
1665 "assignments rearranged?\n",
1666 tgt->lut_obd->obd_uuid.uuid,
1672 if (lsd->lsd_osd_index != index) {
1673 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1674 "is different with the index %d in"
1675 "config log, It might be disk"
1676 "corruption!\n", tgt_name(tgt),
1677 lsd->lsd_osd_index, index);
1682 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1683 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1685 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1689 if (type == LDD_F_SV_TYPE_MDT)
1690 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1692 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1693 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1695 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1698 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1699 if (type == LDD_F_SV_TYPE_MDT &&
1700 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1701 if (last_rcvd_size > lsd->lsd_client_start) {
1702 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1703 "remove all clients for interop needs\n",
1705 rc = tgt_truncate_last_rcvd(env, tgt,
1706 lsd->lsd_client_start);
1709 last_rcvd_size = lsd->lsd_client_start;
1711 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1712 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1715 spin_lock(&tgt->lut_translock);
1716 tgt->lut_last_transno = lsd->lsd_last_transno;
1717 spin_unlock(&tgt->lut_translock);
1719 lsd->lsd_mount_count++;
1721 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1722 CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1723 tgt_name(tgt), tgt->lut_last_transno);
1724 CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1725 tgt_name(tgt), lsd->lsd_mount_count);
1726 CDEBUG(D_INODE, "%s: server data size: %u\n",
1727 tgt_name(tgt), lsd->lsd_server_size);
1728 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1729 tgt_name(tgt), lsd->lsd_client_start);
1730 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1731 tgt_name(tgt), lsd->lsd_client_size);
1732 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1733 tgt_name(tgt), last_rcvd_size);
1734 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1735 tgt_name(tgt), lsd->lsd_subdir_count);
1736 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1737 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1738 (last_rcvd_size - lsd->lsd_client_start) /
1739 lsd->lsd_client_size);
1740 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1742 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1743 lsd->lsd_client_size == 0) {
1744 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1748 if (!tgt->lut_obd->obd_replayable)
1749 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1751 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1753 GOTO(err_client, rc);
1755 spin_lock(&tgt->lut_translock);
1756 /* obd_last_committed is used for compatibility
1757 * with other lustre recovery code */
1758 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1759 spin_unlock(&tgt->lut_translock);
1761 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1762 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1764 /* save it, so mount count and last_transno is current */
1765 rc = tgt_server_data_update(env, tgt, 0);
1767 GOTO(err_client, rc);
1772 class_disconnect_exports(tgt->lut_obd);
1776 /* add credits for last_rcvd update */
1777 int tgt_txn_start_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 *dto;
1786 /* For readonly case, the caller should have got failure
1787 * when start the transaction. If the logic comes here,
1788 * there must be something wrong. */
1789 if (unlikely(tgt->lut_bottom->dd_rdonly)) {
1794 /* if there is no session, then this transaction is not result of
1795 * request processing but some local operation */
1796 if (env->le_ses == NULL)
1799 LASSERT(tgt->lut_last_rcvd);
1800 tsi = tgt_ses_info(env);
1801 /* OFD may start transaction without export assigned */
1802 if (tsi->tsi_exp == NULL)
1805 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1807 * Use maximum possible file offset for declaration to ensure
1808 * ZFS will reserve enough credits for a write anywhere in this
1809 * file, since we don't know where in the file the write will be
1810 * because a replay slot has not been assigned. This should be
1811 * replaced by dmu_tx_hold_append() when available.
1813 tti->tti_buf.lb_buf = NULL;
1814 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1815 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1816 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1820 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1822 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1823 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1829 if (tsi->tsi_vbr_obj != NULL &&
1830 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1831 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1832 rc = dt_declare_version_set(env, dto, th);
1838 /* Update last_rcvd records with latests transaction data */
1839 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1842 struct lu_target *tgt = cookie;
1843 struct tgt_session_info *tsi;
1844 struct tgt_thread_info *tti = tgt_th_info(env);
1845 struct dt_object *obj = NULL;
1849 if (env->le_ses == NULL)
1852 tsi = tgt_ses_info(env);
1853 /* OFD may start transaction without export assigned */
1854 if (tsi->tsi_exp == NULL)
1857 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1859 if (tti->tti_has_trans && !echo_client) {
1860 if (tti->tti_mult_trans == 0) {
1861 CDEBUG(D_HA, "More than one transaction %llu\n",
1865 /* we need another transno to be assigned */
1866 tti->tti_transno = 0;
1867 } else if (th->th_result == 0) {
1868 tti->tti_has_trans = 1;
1871 if (tsi->tsi_vbr_obj != NULL &&
1872 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1873 obj = tsi->tsi_vbr_obj;
1876 if (unlikely(echo_client)) /* echo client special case */
1877 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1880 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1885 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1887 struct tgt_thread_info *tti = tgt_th_info(env);
1888 struct lsd_reply_data *lrd = &tti->tti_lrd;
1889 unsigned long reply_data_size;
1891 struct lsd_reply_header *lrh = NULL;
1892 struct lsd_client_data *lcd = NULL;
1893 struct tg_reply_data *trd = NULL;
1896 struct cfs_hash *hash = NULL;
1897 struct obd_export *exp;
1898 struct tg_export_data *ted;
1899 int reply_data_recovered = 0;
1901 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1904 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1908 GOTO(out, rc = -ENOMEM);
1910 if (reply_data_size == 0) {
1911 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1913 lrh->lrh_magic = LRH_MAGIC;
1914 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1915 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1916 rc = tgt_reply_header_write(env, tgt, lrh);
1918 CERROR("%s: error writing %s: rc = %d\n",
1919 tgt_name(tgt), REPLY_DATA, rc);
1923 rc = tgt_reply_header_read(env, tgt, lrh);
1925 CERROR("%s: error reading %s: rc = %d\n",
1926 tgt_name(tgt), REPLY_DATA, rc);
1929 if (lrh->lrh_magic != LRH_MAGIC ||
1930 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1931 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1932 CERROR("%s: invalid header in %s\n",
1933 tgt_name(tgt), REPLY_DATA);
1934 GOTO(out, rc = -EINVAL);
1937 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1939 GOTO(out, rc = -ENODEV);
1943 GOTO(out, rc = -ENOMEM);
1947 GOTO(out, rc = -ENOMEM);
1949 /* Load reply_data from disk */
1950 for (idx = 0, off = sizeof(struct lsd_reply_header);
1951 off < reply_data_size;
1952 idx++, off += sizeof(struct lsd_reply_data)) {
1953 rc = tgt_reply_data_read(env, tgt, lrd, off);
1955 CERROR("%s: error reading %s: rc = %d\n",
1956 tgt_name(tgt), REPLY_DATA, rc);
1960 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1962 /* old reply data from a disconnected client */
1965 ted = &exp->exp_target_data;
1966 mutex_lock(&ted->ted_lcd_lock);
1968 /* create in-memory reply_data and link it to
1969 * target export's reply list */
1970 rc = tgt_set_reply_slot(tgt, idx);
1972 mutex_unlock(&ted->ted_lcd_lock);
1975 trd->trd_reply = *lrd;
1976 trd->trd_pre_versions[0] = 0;
1977 trd->trd_pre_versions[1] = 0;
1978 trd->trd_pre_versions[2] = 0;
1979 trd->trd_pre_versions[3] = 0;
1980 trd->trd_index = idx;
1982 list_add(&trd->trd_list, &ted->ted_reply_list);
1983 ted->ted_reply_cnt++;
1984 if (ted->ted_reply_cnt > ted->ted_reply_max)
1985 ted->ted_reply_max = ted->ted_reply_cnt;
1987 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1988 "transno %llu, client gen %u, slot idx %d\n",
1989 tgt_name(tgt), trd, lrd->lrd_xid,
1990 lrd->lrd_transno, lrd->lrd_client_gen,
1993 /* update export last committed transation */
1994 exp->exp_last_committed = max(exp->exp_last_committed,
1997 mutex_unlock(&ted->ted_lcd_lock);
1998 class_export_put(exp);
2000 /* update target last committed transaction */
2001 spin_lock(&tgt->lut_translock);
2002 tgt->lut_last_transno = max(tgt->lut_last_transno,
2004 spin_unlock(&tgt->lut_translock);
2006 reply_data_recovered++;
2010 GOTO(out, rc = -ENOMEM);
2012 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
2013 tgt_name(tgt), reply_data_recovered);
2016 spin_lock(&tgt->lut_translock);
2017 /* obd_last_committed is used for compatibility
2018 * with other lustre recovery code */
2019 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
2020 spin_unlock(&tgt->lut_translock);
2026 cfs_hash_putref(hash);
2036 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
2039 struct tg_reply_data *found = NULL;
2040 struct tg_reply_data *reply;
2042 mutex_lock(&ted->ted_lcd_lock);
2043 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
2044 if (reply->trd_reply.lrd_xid == xid) {
2049 mutex_unlock(&ted->ted_lcd_lock);
2052 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
2054 /* Look for a reply data matching specified request @req
2055 * A copy is returned in @trd if the pointer is not NULL
2057 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2059 struct tg_export_data *ted = &req->rq_export->exp_target_data;
2060 struct tg_reply_data *reply;
2063 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2064 if (reply != NULL) {
2070 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2071 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2076 EXPORT_SYMBOL(tgt_lookup_reply);
2078 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2080 struct tg_export_data *ted = &exp->exp_target_data;
2081 struct lu_target *lut = class_exp2tgt(exp);
2082 struct tg_reply_data *trd, *tmp;
2084 mutex_lock(&ted->ted_lcd_lock);
2085 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2086 if (trd->trd_reply.lrd_xid > rcvd_xid)
2088 ted->ted_release_xid++;
2089 tgt_release_reply_data(lut, ted, trd);
2091 mutex_unlock(&ted->ted_lcd_lock);
2096 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2098 struct tg_export_data *ted = &exp->exp_target_data;
2099 struct lu_target *lut = class_exp2tgt(exp);
2100 struct tg_reply_data *trd, *tmp;
2105 mutex_lock(&ted->ted_lcd_lock);
2106 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2107 if (trd->trd_tag != tag)
2109 ted->ted_release_tag++;
2110 tgt_release_reply_data(lut, ted, trd);
2113 mutex_unlock(&ted->ted_lcd_lock);