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 /* Do not erase record for recoverable client. */
1083 if (exp->exp_obd->obd_fail)
1086 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1087 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1088 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1089 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1092 /* Slot may be not yet assigned, use case is race between Client
1093 * reconnect and forced eviction */
1094 if (ted->ted_lr_idx < 0) {
1095 CWARN("%s: client with UUID '%s' not in bitmap\n",
1096 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1100 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1101 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1102 ted->ted_lcd->lcd_uuid);
1104 /* Clear the bit _after_ zeroing out the client so we don't
1105 race with filter_client_add and zero out new clients.*/
1106 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1107 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1108 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1113 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1116 /* Make sure the server's last_transno is up to date.
1117 * This should be done before zeroing client slot so last_transno will
1118 * be in server data or in client data in case of failure */
1119 rc = tgt_server_data_update(env, tgt, 0);
1121 CERROR("%s: failed to update server data, skip client %s "
1122 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1123 ted->ted_lcd->lcd_uuid, rc);
1127 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1128 rc = tgt_client_data_update(env, exp);
1130 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1131 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1132 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1133 ted->ted_lr_idx, ted->ted_lr_off, rc);
1136 EXPORT_SYMBOL(tgt_client_del);
1138 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1139 struct tg_export_data *ted, struct tg_reply_data *trd,
1140 struct thandle *th, bool update_lrd_file)
1142 struct lsd_reply_data *lrd;
1145 lrd = &trd->trd_reply;
1146 /* update export last transno */
1147 mutex_lock(&ted->ted_lcd_lock);
1148 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1149 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1150 mutex_unlock(&ted->ted_lcd_lock);
1152 /* find a empty slot */
1153 i = tgt_find_free_reply_slot(tgt);
1154 if (unlikely(i < 0)) {
1155 CERROR("%s: couldn't find a slot for reply data: "
1156 "rc = %d\n", tgt_name(tgt), i);
1161 if (update_lrd_file) {
1165 /* write reply data to disk */
1166 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1167 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1168 if (unlikely(rc != 0)) {
1169 CERROR("%s: can't update %s file: rc = %d\n",
1170 tgt_name(tgt), REPLY_DATA, rc);
1174 /* add reply data to target export's reply list */
1175 mutex_lock(&ted->ted_lcd_lock);
1176 list_add(&trd->trd_list, &ted->ted_reply_list);
1177 ted->ted_reply_cnt++;
1178 if (ted->ted_reply_cnt > ted->ted_reply_max)
1179 ted->ted_reply_max = ted->ted_reply_cnt;
1180 mutex_unlock(&ted->ted_lcd_lock);
1182 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1183 "tag %hu, client gen %u, slot idx %d\n",
1184 trd, lrd->lrd_xid, lrd->lrd_transno,
1185 trd->trd_tag, lrd->lrd_client_gen, i);
1188 EXPORT_SYMBOL(tgt_add_reply_data);
1191 * last_rcvd & last_committed update callbacks
1193 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1194 struct dt_object *obj, __u64 opdata,
1195 struct thandle *th, struct ptlrpc_request *req)
1197 struct tgt_thread_info *tti = tgt_th_info(env);
1198 struct tgt_session_info *tsi = tgt_ses_info(env);
1199 struct obd_export *exp = tsi->tsi_exp;
1200 struct tg_export_data *ted;
1208 LASSERT(exp != NULL);
1209 ted = &exp->exp_target_data;
1211 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1212 if (ted->ted_lr_idx < 0 && !lw_client)
1213 /* ofd connect may cause transaction before export has
1218 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1220 /* From update replay, tti_transno should be set already */
1221 LASSERT(tti->tti_transno != 0);
1223 spin_lock(&tgt->lut_translock);
1224 if (th->th_result != 0) {
1225 if (tti->tti_transno != 0) {
1226 CERROR("%s: replay transno %llu failed: rc = %d\n",
1227 tgt_name(tgt), tti->tti_transno, th->th_result);
1229 } else if (tti->tti_transno == 0) {
1230 tti->tti_transno = ++tgt->lut_last_transno;
1232 /* should be replay */
1233 if (tti->tti_transno > tgt->lut_last_transno)
1234 tgt->lut_last_transno = tti->tti_transno;
1236 spin_unlock(&tgt->lut_translock);
1238 /** VBR: set new versions */
1239 if (th->th_result == 0 && obj != NULL) {
1240 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1241 dt_version_set(env, dto, tti->tti_transno, th);
1244 /* filling reply data */
1245 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1246 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1249 req->rq_transno = tti->tti_transno;
1250 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1253 /* if can't add callback, do sync write */
1254 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1257 /* All operations performed by LW clients are synchronous and
1258 * we store the committed transno in the last_rcvd header */
1259 spin_lock(&tgt->lut_translock);
1260 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1261 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1262 spin_unlock(&tgt->lut_translock);
1263 /* Although lightweight (LW) connections have no slot
1264 * in the last_rcvd, we still want to maintain
1265 * the in-memory lsd_client_data structure in order to
1266 * properly handle reply reconstruction. */
1267 rc = tgt_server_data_write(env, tgt, th);
1269 spin_unlock(&tgt->lut_translock);
1271 } else if (ted->ted_lr_off == 0) {
1272 CERROR("%s: client idx %d has offset %lld\n",
1273 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1277 /* Target that supports multiple reply data */
1278 if (tgt_is_multimodrpcs_client(exp)) {
1279 struct tg_reply_data *trd;
1280 struct lsd_reply_data *lrd;
1281 __u64 *pre_versions;
1285 if (unlikely(trd == NULL))
1288 /* fill reply data information */
1289 lrd = &trd->trd_reply;
1290 lrd->lrd_transno = tti->tti_transno;
1292 lrd->lrd_xid = req->rq_xid;
1293 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1294 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1295 lrd->lrd_result = th->th_result;
1296 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1297 write_update = true;
1299 LASSERT(tsi->tsi_xid != 0);
1300 lrd->lrd_xid = tsi->tsi_xid;
1301 lrd->lrd_result = tsi->tsi_result;
1302 lrd->lrd_client_gen = tsi->tsi_client_gen;
1304 pre_versions = NULL;
1305 write_update = false;
1308 lrd->lrd_data = opdata;
1310 trd->trd_pre_versions[0] = pre_versions[0];
1311 trd->trd_pre_versions[1] = pre_versions[1];
1312 trd->trd_pre_versions[2] = pre_versions[2];
1313 trd->trd_pre_versions[3] = pre_versions[3];
1316 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1322 /* Enough for update replay, let's return */
1326 mutex_lock(&ted->ted_lcd_lock);
1327 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1328 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1329 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1330 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1331 ted->ted_lcd->lcd_last_close_result = th->th_result;
1333 /* VBR: save versions in last_rcvd for reconstruct. */
1334 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1337 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1338 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1339 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1340 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1342 transno_p = &ted->ted_lcd->lcd_last_transno;
1343 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1344 ted->ted_lcd->lcd_last_result = th->th_result;
1345 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1346 * see struct ldlm_reply->lock_policy_res1; */
1347 ted->ted_lcd->lcd_last_data = opdata;
1350 /* Update transno in slot only if non-zero number, i.e. no errors */
1351 if (likely(tti->tti_transno != 0)) {
1352 /* Don't overwrite bigger transaction number with lower one.
1353 * That is not sign of problem in all cases, but in any case
1354 * this value should be monotonically increased only. */
1355 if (*transno_p > tti->tti_transno) {
1356 if (!tgt->lut_no_reconstruct) {
1357 CERROR("%s: trying to overwrite bigger transno:"
1358 "on-disk: %llu, new: %llu replay: "
1359 "%d. See LU-617.\n", tgt_name(tgt),
1360 *transno_p, tti->tti_transno,
1361 req_is_replay(req));
1362 if (req_is_replay(req)) {
1363 spin_lock(&req->rq_export->exp_lock);
1364 req->rq_export->exp_vbr_failed = 1;
1365 spin_unlock(&req->rq_export->exp_lock);
1367 mutex_unlock(&ted->ted_lcd_lock);
1368 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1371 *transno_p = tti->tti_transno;
1376 tti->tti_off = ted->ted_lr_off;
1377 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1380 rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1383 mutex_unlock(&ted->ted_lcd_lock);
1387 mutex_unlock(&ted->ted_lcd_lock);
1392 * last_rcvd update for echo client simulation.
1393 * It updates last_rcvd client slot and version of object in
1394 * simple way but with all locks to simulate all drawbacks
1396 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1397 struct lu_target *tgt,
1398 struct dt_object *obj,
1400 struct obd_export *exp)
1402 struct tgt_thread_info *tti = tgt_th_info(env);
1403 struct tg_export_data *ted = &exp->exp_target_data;
1408 tti->tti_transno = 0;
1410 spin_lock(&tgt->lut_translock);
1411 if (th->th_result == 0)
1412 tti->tti_transno = ++tgt->lut_last_transno;
1413 spin_unlock(&tgt->lut_translock);
1415 /** VBR: set new versions */
1416 if (th->th_result == 0 && obj != NULL)
1417 dt_version_set(env, obj, tti->tti_transno, th);
1419 /* if can't add callback, do sync write */
1420 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1423 LASSERT(ted->ted_lr_off > 0);
1425 mutex_lock(&ted->ted_lcd_lock);
1426 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1427 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1428 ted->ted_lcd->lcd_last_result = th->th_result;
1430 tti->tti_off = ted->ted_lr_off;
1431 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1432 mutex_unlock(&ted->ted_lcd_lock);
1436 static int tgt_clients_data_init(const struct lu_env *env,
1437 struct lu_target *tgt,
1438 unsigned long last_size)
1440 struct obd_device *obd = tgt->lut_obd;
1441 struct lr_server_data *lsd = &tgt->lut_lsd;
1442 struct lsd_client_data *lcd = NULL;
1443 struct tg_export_data *ted;
1446 loff_t off = lsd->lsd_client_start;
1447 __u32 generation = 0;
1448 struct cfs_hash *hash = NULL;
1452 if (tgt->lut_bottom->dd_rdonly)
1455 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1456 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1462 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1464 GOTO(err_out, rc = -ENODEV);
1466 for (cl_idx = 0; off < last_size; cl_idx++) {
1467 struct obd_export *exp;
1470 /* Don't assume off is incremented properly by
1471 * read_record(), in case sizeof(*lcd)
1472 * isn't the same as fsd->lsd_client_size. */
1473 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1474 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1476 CERROR("%s: error reading last_rcvd %s idx %d off "
1477 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1480 break; /* read error shouldn't cause startup to fail */
1483 if (lcd->lcd_uuid[0] == '\0') {
1484 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1489 last_transno = lcd_last_transno(lcd);
1491 /* These exports are cleaned up by disconnect, so they
1492 * need to be set up like real exports as connect does.
1494 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1495 " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1496 cl_idx, last_transno, lsd->lsd_last_transno,
1497 lcd_last_xid(lcd), lcd->lcd_generation);
1499 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1501 if (PTR_ERR(exp) == -EALREADY) {
1502 /* export already exists, zero out this one */
1503 CERROR("%s: Duplicate export %s!\n",
1504 tgt_name(tgt), lcd->lcd_uuid);
1507 GOTO(err_out, rc = PTR_ERR(exp));
1510 ted = &exp->exp_target_data;
1511 *ted->ted_lcd = *lcd;
1513 rc = tgt_client_add(env, exp, cl_idx);
1514 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1515 /* VBR: set export last committed version */
1516 exp->exp_last_committed = last_transno;
1517 spin_lock(&exp->exp_lock);
1518 exp->exp_connecting = 0;
1519 exp->exp_in_recovery = 0;
1520 spin_unlock(&exp->exp_lock);
1521 obd->obd_max_recoverable_clients++;
1523 if (tgt->lut_lsd.lsd_feature_incompat &
1524 OBD_INCOMPAT_MULTI_RPCS &&
1525 lcd->lcd_generation != 0) {
1526 /* compute the highest valid client generation */
1527 generation = max(generation, lcd->lcd_generation);
1528 /* fill client_generation <-> export hash table */
1529 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1530 &exp->exp_gen_hash);
1532 CERROR("%s: duplicate export for client "
1534 tgt_name(tgt), lcd->lcd_generation);
1535 class_export_put(exp);
1540 class_export_put(exp);
1542 rc = rev_import_init(exp);
1544 class_unlink_export(exp);
1548 /* Need to check last_rcvd even for duplicated exports. */
1549 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1550 cl_idx, last_transno);
1552 spin_lock(&tgt->lut_translock);
1553 tgt->lut_last_transno = max(last_transno,
1554 tgt->lut_last_transno);
1555 spin_unlock(&tgt->lut_translock);
1558 /* record highest valid client generation */
1559 atomic_set(&tgt->lut_client_generation, generation);
1563 cfs_hash_putref(hash);
1568 struct server_compat_data {
1575 static struct server_compat_data tgt_scd[] = {
1576 [LDD_F_SV_TYPE_MDT] = {
1577 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1578 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1579 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1580 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1581 OBD_INCOMPAT_MULTI_RPCS,
1582 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1583 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1584 OBD_INCOMPAT_MULTI_OI,
1586 [LDD_F_SV_TYPE_OST] = {
1587 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1588 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1590 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1591 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1595 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1597 struct tgt_thread_info *tti = tgt_th_info(env);
1598 struct lr_server_data *lsd = &tgt->lut_lsd;
1599 unsigned long last_rcvd_size;
1603 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1607 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1609 /* ensure padding in the struct is the correct size */
1610 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1611 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1613 rc = server_name2index(tgt_name(tgt), &index, NULL);
1615 CERROR("%s: Can not get index from name: rc = %d\n",
1619 /* server_name2index() returns type */
1621 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1622 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1626 /* last_rcvd on OST doesn't provide reconstruct support because there
1627 * may be up to 8 in-flight write requests per single slot in
1628 * last_rcvd client data
1630 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1632 if (last_rcvd_size == 0) {
1633 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1635 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1636 sizeof(lsd->lsd_uuid));
1637 lsd->lsd_last_transno = 0;
1638 lsd->lsd_mount_count = 0;
1639 lsd->lsd_server_size = LR_SERVER_SIZE;
1640 lsd->lsd_client_start = LR_CLIENT_START;
1641 lsd->lsd_client_size = LR_CLIENT_SIZE;
1642 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1643 lsd->lsd_osd_index = index;
1644 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1645 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1647 rc = tgt_server_data_read(env, tgt);
1649 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1653 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1654 if (tgt->lut_bottom->dd_rdonly) {
1655 /* Such difference may be caused by mounting
1656 * up snapshot with new fsname under rd_only
1657 * mode. But even if it was NOT, it will not
1658 * damage the system because of "rd_only". */
1659 memcpy(lsd->lsd_uuid,
1660 tgt->lut_obd->obd_uuid.uuid,
1661 sizeof(lsd->lsd_uuid));
1663 LCONSOLE_ERROR_MSG(0x157, "Trying to start "
1664 "OBD %s using the wrong "
1665 "disk %s. Were the /dev/ "
1666 "assignments rearranged?\n",
1667 tgt->lut_obd->obd_uuid.uuid,
1673 if (lsd->lsd_osd_index != index) {
1674 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1675 "is different with the index %d in"
1676 "config log, It might be disk"
1677 "corruption!\n", tgt_name(tgt),
1678 lsd->lsd_osd_index, index);
1683 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1684 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1686 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1690 if (type == LDD_F_SV_TYPE_MDT)
1691 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1693 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1694 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1696 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1699 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1700 if (type == LDD_F_SV_TYPE_MDT &&
1701 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1702 if (last_rcvd_size > lsd->lsd_client_start) {
1703 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1704 "remove all clients for interop needs\n",
1706 rc = tgt_truncate_last_rcvd(env, tgt,
1707 lsd->lsd_client_start);
1710 last_rcvd_size = lsd->lsd_client_start;
1712 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1713 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1716 spin_lock(&tgt->lut_translock);
1717 tgt->lut_last_transno = lsd->lsd_last_transno;
1718 spin_unlock(&tgt->lut_translock);
1720 lsd->lsd_mount_count++;
1722 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1723 CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1724 tgt_name(tgt), tgt->lut_last_transno);
1725 CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1726 tgt_name(tgt), lsd->lsd_mount_count);
1727 CDEBUG(D_INODE, "%s: server data size: %u\n",
1728 tgt_name(tgt), lsd->lsd_server_size);
1729 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1730 tgt_name(tgt), lsd->lsd_client_start);
1731 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1732 tgt_name(tgt), lsd->lsd_client_size);
1733 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1734 tgt_name(tgt), last_rcvd_size);
1735 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1736 tgt_name(tgt), lsd->lsd_subdir_count);
1737 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1738 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1739 (last_rcvd_size - lsd->lsd_client_start) /
1740 lsd->lsd_client_size);
1741 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1743 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1744 lsd->lsd_client_size == 0) {
1745 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1749 if (!tgt->lut_obd->obd_replayable)
1750 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1752 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1754 GOTO(err_client, rc);
1756 spin_lock(&tgt->lut_translock);
1757 /* obd_last_committed is used for compatibility
1758 * with other lustre recovery code */
1759 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1760 spin_unlock(&tgt->lut_translock);
1762 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1763 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1765 /* save it, so mount count and last_transno is current */
1766 rc = tgt_server_data_update(env, tgt, 0);
1768 GOTO(err_client, rc);
1773 class_disconnect_exports(tgt->lut_obd);
1777 /* add credits for last_rcvd update */
1778 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1781 struct lu_target *tgt = cookie;
1782 struct tgt_session_info *tsi;
1783 struct tgt_thread_info *tti = tgt_th_info(env);
1784 struct dt_object *dto;
1787 /* For readonly case, the caller should have got failure
1788 * when start the transaction. If the logic comes here,
1789 * there must be something wrong. */
1790 if (unlikely(tgt->lut_bottom->dd_rdonly)) {
1795 /* if there is no session, then this transaction is not result of
1796 * request processing but some local operation */
1797 if (env->le_ses == NULL)
1800 LASSERT(tgt->lut_last_rcvd);
1801 tsi = tgt_ses_info(env);
1802 /* OFD may start transaction without export assigned */
1803 if (tsi->tsi_exp == NULL)
1806 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1808 * Use maximum possible file offset for declaration to ensure
1809 * ZFS will reserve enough credits for a write anywhere in this
1810 * file, since we don't know where in the file the write will be
1811 * because a replay slot has not been assigned. This should be
1812 * replaced by dmu_tx_hold_append() when available.
1814 tti->tti_buf.lb_buf = NULL;
1815 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1816 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1817 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1821 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1823 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1824 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1830 if (tsi->tsi_vbr_obj != NULL &&
1831 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1832 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1833 rc = dt_declare_version_set(env, dto, th);
1839 /* Update last_rcvd records with latests transaction data */
1840 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1843 struct lu_target *tgt = cookie;
1844 struct tgt_session_info *tsi;
1845 struct tgt_thread_info *tti = tgt_th_info(env);
1846 struct dt_object *obj = NULL;
1850 if (env->le_ses == NULL)
1853 tsi = tgt_ses_info(env);
1854 /* OFD may start transaction without export assigned */
1855 if (tsi->tsi_exp == NULL)
1858 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1860 if (tti->tti_has_trans && !echo_client) {
1861 if (tti->tti_mult_trans == 0) {
1862 CDEBUG(D_HA, "More than one transaction %llu\n",
1866 /* we need another transno to be assigned */
1867 tti->tti_transno = 0;
1868 } else if (th->th_result == 0) {
1869 tti->tti_has_trans = 1;
1872 if (tsi->tsi_vbr_obj != NULL &&
1873 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1874 obj = tsi->tsi_vbr_obj;
1877 if (unlikely(echo_client)) /* echo client special case */
1878 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1881 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1886 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1888 struct tgt_thread_info *tti = tgt_th_info(env);
1889 struct lsd_reply_data *lrd = &tti->tti_lrd;
1890 unsigned long reply_data_size;
1892 struct lsd_reply_header *lrh = NULL;
1893 struct lsd_client_data *lcd = NULL;
1894 struct tg_reply_data *trd = NULL;
1897 struct cfs_hash *hash = NULL;
1898 struct obd_export *exp;
1899 struct tg_export_data *ted;
1900 int reply_data_recovered = 0;
1902 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1905 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1909 GOTO(out, rc = -ENOMEM);
1911 if (reply_data_size == 0) {
1912 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1914 lrh->lrh_magic = LRH_MAGIC;
1915 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1916 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1917 rc = tgt_reply_header_write(env, tgt, lrh);
1919 CERROR("%s: error writing %s: rc = %d\n",
1920 tgt_name(tgt), REPLY_DATA, rc);
1924 rc = tgt_reply_header_read(env, tgt, lrh);
1926 CERROR("%s: error reading %s: rc = %d\n",
1927 tgt_name(tgt), REPLY_DATA, rc);
1930 if (lrh->lrh_magic != LRH_MAGIC ||
1931 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1932 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1933 CERROR("%s: invalid header in %s\n",
1934 tgt_name(tgt), REPLY_DATA);
1935 GOTO(out, rc = -EINVAL);
1938 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1940 GOTO(out, rc = -ENODEV);
1944 GOTO(out, rc = -ENOMEM);
1948 GOTO(out, rc = -ENOMEM);
1950 /* Load reply_data from disk */
1951 for (idx = 0, off = sizeof(struct lsd_reply_header);
1952 off < reply_data_size;
1953 idx++, off += sizeof(struct lsd_reply_data)) {
1954 rc = tgt_reply_data_read(env, tgt, lrd, off);
1956 CERROR("%s: error reading %s: rc = %d\n",
1957 tgt_name(tgt), REPLY_DATA, rc);
1961 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1963 /* old reply data from a disconnected client */
1966 ted = &exp->exp_target_data;
1967 mutex_lock(&ted->ted_lcd_lock);
1969 /* create in-memory reply_data and link it to
1970 * target export's reply list */
1971 rc = tgt_set_reply_slot(tgt, idx);
1973 mutex_unlock(&ted->ted_lcd_lock);
1976 trd->trd_reply = *lrd;
1977 trd->trd_pre_versions[0] = 0;
1978 trd->trd_pre_versions[1] = 0;
1979 trd->trd_pre_versions[2] = 0;
1980 trd->trd_pre_versions[3] = 0;
1981 trd->trd_index = idx;
1983 list_add(&trd->trd_list, &ted->ted_reply_list);
1984 ted->ted_reply_cnt++;
1985 if (ted->ted_reply_cnt > ted->ted_reply_max)
1986 ted->ted_reply_max = ted->ted_reply_cnt;
1988 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1989 "transno %llu, client gen %u, slot idx %d\n",
1990 tgt_name(tgt), trd, lrd->lrd_xid,
1991 lrd->lrd_transno, lrd->lrd_client_gen,
1994 /* update export last committed transation */
1995 exp->exp_last_committed = max(exp->exp_last_committed,
1998 mutex_unlock(&ted->ted_lcd_lock);
1999 class_export_put(exp);
2001 /* update target last committed transaction */
2002 spin_lock(&tgt->lut_translock);
2003 tgt->lut_last_transno = max(tgt->lut_last_transno,
2005 spin_unlock(&tgt->lut_translock);
2007 reply_data_recovered++;
2011 GOTO(out, rc = -ENOMEM);
2013 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
2014 tgt_name(tgt), reply_data_recovered);
2017 spin_lock(&tgt->lut_translock);
2018 /* obd_last_committed is used for compatibility
2019 * with other lustre recovery code */
2020 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
2021 spin_unlock(&tgt->lut_translock);
2027 cfs_hash_putref(hash);
2037 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
2040 struct tg_reply_data *found = NULL;
2041 struct tg_reply_data *reply;
2043 mutex_lock(&ted->ted_lcd_lock);
2044 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
2045 if (reply->trd_reply.lrd_xid == xid) {
2050 mutex_unlock(&ted->ted_lcd_lock);
2053 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
2055 /* Look for a reply data matching specified request @req
2056 * A copy is returned in @trd if the pointer is not NULL
2058 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2060 struct tg_export_data *ted = &req->rq_export->exp_target_data;
2061 struct tg_reply_data *reply;
2064 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2065 if (reply != NULL) {
2071 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2072 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2077 EXPORT_SYMBOL(tgt_lookup_reply);
2079 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2081 struct tg_export_data *ted = &exp->exp_target_data;
2082 struct lu_target *lut = class_exp2tgt(exp);
2083 struct tg_reply_data *trd, *tmp;
2085 mutex_lock(&ted->ted_lcd_lock);
2086 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2087 if (trd->trd_reply.lrd_xid > rcvd_xid)
2089 ted->ted_release_xid++;
2090 tgt_release_reply_data(lut, ted, trd);
2092 mutex_unlock(&ted->ted_lcd_lock);
2097 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2099 struct tg_export_data *ted = &exp->exp_target_data;
2100 struct lu_target *lut = class_exp2tgt(exp);
2101 struct tg_reply_data *trd, *tmp;
2106 mutex_lock(&ted->ted_lcd_lock);
2107 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2108 if (trd->trd_tag != tag)
2110 ted->ted_release_tag++;
2111 tgt_release_reply_data(lut, ted, trd);
2114 mutex_unlock(&ted->ted_lcd_lock);