4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Lustre Unified Target
37 * These are common function to work with last_received file
39 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 #include <obd_class.h>
43 #include <lustre_fid.h>
45 #include "tgt_internal.h"
48 /* Allocate a bitmap for a chunk of reply data slots */
49 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
53 OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
58 spin_lock(&lut->lut_client_bitmap_lock);
60 if (lut->lut_reply_bitmap[chunk] != NULL) {
61 /* someone else already allocated the bitmap for this chunk */
62 spin_unlock(&lut->lut_client_bitmap_lock);
63 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
68 lut->lut_reply_bitmap[chunk] = bm;
70 spin_unlock(&lut->lut_client_bitmap_lock);
75 /* Look for an available reply data slot in the bitmap
77 * Allocate bitmap chunk when first used
78 * XXX algo could be improved if this routine limits performance
80 static int tgt_find_free_reply_slot(struct lu_target *lut)
87 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
88 /* allocate the bitmap chunk if necessary */
89 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
90 rc = tgt_bitmap_chunk_alloc(lut, chunk);
94 bmp = lut->lut_reply_bitmap[chunk];
96 /* look for an available slot in this chunk */
98 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
99 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
103 if (test_and_set_bit(b, bmp) == 0)
104 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
111 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
113 * Allocate the bitmap chunk if necessary
115 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
121 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
122 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
124 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
125 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
127 /* allocate the bitmap chunk if necessary */
128 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
129 rc = tgt_bitmap_chunk_alloc(lut, chunk);
134 /* mark the slot 'used' in this chunk */
135 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
136 CERROR("%s: slot %d already set in bitmap\n",
145 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
148 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
153 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
154 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
156 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
157 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
159 if (lut->lut_reply_bitmap[chunk] == NULL) {
160 CERROR("%s: slot %d not allocated\n",
165 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
166 CERROR("%s: slot %d already clear in bitmap\n",
175 /* Read header of reply_data file of target @tgt into structure @lrh */
176 static int tgt_reply_header_read(const struct lu_env *env,
177 struct lu_target *tgt,
178 struct lsd_reply_header *lrh)
181 struct lsd_reply_header buf;
182 struct tgt_thread_info *tti = tgt_th_info(env);
185 tti->tti_buf.lb_buf = &buf;
186 tti->tti_buf.lb_len = sizeof(buf);
188 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
193 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
194 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
195 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
197 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
198 "header_size=%d reply_size=%d\n",
199 tgt->lut_obd->obd_name, REPLY_DATA,
200 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
205 /* Write header into replay_data file of target @tgt from structure @lrh */
206 static int tgt_reply_header_write(const struct lu_env *env,
207 struct lu_target *tgt,
208 struct lsd_reply_header *lrh)
211 struct lsd_reply_header buf;
212 struct tgt_thread_info *tti = tgt_th_info(env);
214 struct dt_object *dto;
216 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
217 "header_size=%d reply_size=%d\n",
218 tgt->lut_obd->obd_name, REPLY_DATA,
219 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
221 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
222 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
223 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
225 th = dt_trans_create(env, tgt->lut_bottom);
231 tti->tti_buf.lb_buf = &buf;
232 tti->tti_buf.lb_len = sizeof(buf);
234 rc = dt_declare_record_write(env, tgt->lut_reply_data,
235 &tti->tti_buf, tti->tti_off, th);
239 rc = dt_trans_start(env, tgt->lut_bottom, th);
243 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
244 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
246 dt_trans_stop(env, tgt->lut_bottom, th);
250 /* Write the reply data @lrd into reply_data file of target @tgt
253 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
254 struct lsd_reply_data *lrd, loff_t off,
257 struct tgt_thread_info *tti = tgt_th_info(env);
258 struct dt_object *dto;
259 struct lsd_reply_data *buf = &tti->tti_lrd;
261 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
263 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
264 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
265 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
266 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
267 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
269 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
272 tti->tti_buf.lb_buf = buf;
273 tti->tti_buf.lb_len = sizeof(*buf);
275 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
276 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
279 /* Read the reply data from reply_data file of target @tgt at offset @off
280 * into structure @lrd
282 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
283 struct lsd_reply_data *lrd, loff_t off)
286 struct tgt_thread_info *tti = tgt_th_info(env);
287 struct lsd_reply_data *buf = &tti->tti_lrd;
290 tti->tti_buf.lb_buf = buf;
291 tti->tti_buf.lb_len = sizeof(*buf);
293 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
298 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
299 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
300 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
301 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
302 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
308 /* Free the in-memory reply data structure @trd and release
309 * the corresponding slot in the reply_data file of target @lut
310 * Called with ted_lcd_lock held
312 static void tgt_free_reply_data(struct lu_target *lut,
313 struct tg_export_data *ted,
314 struct tg_reply_data *trd)
316 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
317 "client gen %u, slot idx %d\n",
318 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
319 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
322 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
324 list_del(&trd->trd_list);
325 ted->ted_reply_cnt--;
327 tgt_clear_reply_slot(lut, trd->trd_index);
331 /* Release the reply data @trd from target @lut
332 * The reply data with the highest transno for this export
333 * is retained to ensure correctness of target recovery
334 * Called with ted_lcd_lock held
336 static void tgt_release_reply_data(struct lu_target *lut,
337 struct tg_export_data *ted,
338 struct tg_reply_data *trd)
340 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
341 "client gen %u, slot idx %d\n",
342 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
343 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
346 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
348 /* Do not free the reply data corresponding to the
349 * highest transno of this export.
350 * This ensures on-disk reply data is kept and
351 * last committed transno can be restored from disk in case
354 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
355 /* free previous retained reply */
356 if (ted->ted_reply_last != NULL)
357 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
358 /* retain the reply */
359 list_del_init(&trd->trd_list);
360 ted->ted_reply_last = trd;
362 tgt_free_reply_data(lut, ted, trd);
366 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
368 tti->tti_buf.lb_buf = &tti->tti_lsd;
369 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
370 return &tti->tti_buf;
373 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
375 tti->tti_buf.lb_buf = &tti->tti_lcd;
376 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
377 return &tti->tti_buf;
381 * Allocate in-memory data for client slot related to export.
383 int tgt_client_alloc(struct obd_export *exp)
386 LASSERT(exp != exp->exp_obd->obd_self_export);
388 spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
389 INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
391 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
392 if (exp->exp_target_data.ted_lcd == NULL)
394 /* Mark that slot is not yet valid, 0 doesn't work here */
395 exp->exp_target_data.ted_lr_idx = -1;
396 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
397 mutex_init(&exp->exp_target_data.ted_lcd_lock);
400 EXPORT_SYMBOL(tgt_client_alloc);
403 * Free in-memory data for client slot related to export.
405 void tgt_client_free(struct obd_export *exp)
407 struct tg_export_data *ted = &exp->exp_target_data;
408 struct lu_target *lut = class_exp2tgt(exp);
409 struct tg_reply_data *trd, *tmp;
411 LASSERT(exp != exp->exp_obd->obd_self_export);
413 /* free reply data */
414 mutex_lock(&ted->ted_lcd_lock);
415 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
416 tgt_release_reply_data(lut, ted, trd);
418 if (ted->ted_reply_last != NULL) {
419 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
420 ted->ted_reply_last = NULL;
422 mutex_unlock(&ted->ted_lcd_lock);
424 if (!hlist_unhashed(&exp->exp_gen_hash))
425 cfs_hash_del(exp->exp_obd->obd_gen_hash,
426 &ted->ted_lcd->lcd_generation,
429 OBD_FREE_PTR(ted->ted_lcd);
432 /* Target may have been freed (see LU-7430)
433 * Slot may be not yet assigned */
434 if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
438 /* Clear bit when lcd is freed */
439 LASSERT(lut && lut->lut_client_bitmap);
440 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
441 CERROR("%s: client %u bit already clear in bitmap\n",
442 exp->exp_obd->obd_name, ted->ted_lr_idx);
446 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
447 atomic_dec(&lut->lut_num_clients);
449 EXPORT_SYMBOL(tgt_client_free);
451 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
452 struct lsd_client_data *lcd, loff_t *off, int index)
454 struct tgt_thread_info *tti = tgt_th_info(env);
458 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
460 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
461 lcd_le_to_cpu(&tti->tti_lcd, lcd);
462 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
463 lcd->lcd_last_close_result =
464 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
467 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = "LPU64
468 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
469 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
470 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
471 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
472 lcd->lcd_last_result, lcd->lcd_last_data,
473 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
474 lcd->lcd_last_close_result, rc);
478 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
479 struct lsd_client_data *lcd, loff_t *off,
482 struct tgt_thread_info *tti = tgt_th_info(env);
483 struct dt_object *dto;
485 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
486 lcd->lcd_last_close_result =
487 ptlrpc_status_hton(lcd->lcd_last_close_result);
488 lcd_cpu_to_le(lcd, &tti->tti_lcd);
491 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
492 return dt_record_write(env, dto, &tti->tti_buf, off, th);
496 * Update client data in last_rcvd
498 static int tgt_client_data_update(const struct lu_env *env,
499 struct obd_export *exp)
501 struct tg_export_data *ted = &exp->exp_target_data;
502 struct lu_target *tgt = class_exp2tgt(exp);
503 struct tgt_thread_info *tti = tgt_th_info(env);
509 if (unlikely(tgt == NULL)) {
510 CDEBUG(D_ERROR, "%s: No target for connected export\n",
511 class_exp2obd(exp)->obd_name);
515 th = dt_trans_create(env, tgt->lut_bottom);
520 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
522 ted->ted_lr_off, th);
526 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
530 * Until this operations will be committed the sync is needed
531 * for this export. This should be done _after_ starting the
532 * transaction so that many connecting clients will not bring
533 * server down with lots of sync writes.
535 rc = tgt_new_client_cb_add(th, exp);
537 /* can't add callback, do sync now */
540 spin_lock(&exp->exp_lock);
541 exp->exp_need_sync = 1;
542 spin_unlock(&exp->exp_lock);
545 tti->tti_off = ted->ted_lr_off;
546 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
549 dt_trans_stop(env, tgt->lut_bottom, th);
550 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
551 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
552 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
557 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
559 struct tgt_thread_info *tti = tgt_th_info(env);
564 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
567 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
569 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
570 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
571 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
575 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
578 struct tgt_thread_info *tti = tgt_th_info(env);
579 struct dt_object *dto;
586 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
588 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
589 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
591 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
592 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
593 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
599 * Update server data in last_rcvd
601 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
604 struct tgt_thread_info *tti = tgt_th_info(env);
611 "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
612 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
613 tgt->lut_last_transno);
615 /* Always save latest transno to keep it fresh */
616 spin_lock(&tgt->lut_translock);
617 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
618 spin_unlock(&tgt->lut_translock);
620 th = dt_trans_create(env, tgt->lut_bottom);
627 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
628 &tti->tti_buf, tti->tti_off, th);
632 rc = dt_trans_start(env, tgt->lut_bottom, th);
636 rc = tgt_server_data_write(env, tgt, th);
638 dt_trans_stop(env, tgt->lut_bottom, th);
640 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
641 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
642 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
645 EXPORT_SYMBOL(tgt_server_data_update);
647 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
650 struct dt_object *dt = tgt->lut_last_rcvd;
658 attr.la_valid = LA_SIZE;
660 th = dt_trans_create(env, tgt->lut_bottom);
663 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
666 rc = dt_declare_attr_set(env, dt, &attr, th);
669 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
673 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
675 rc = dt_attr_set(env, dt, &attr, th);
678 dt_trans_stop(env, tgt->lut_bottom, th);
683 static void tgt_client_epoch_update(const struct lu_env *env,
684 struct obd_export *exp)
686 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
687 struct lu_target *tgt = class_exp2tgt(exp);
689 LASSERT(tgt && tgt->lut_bottom);
690 /** VBR: set client last_epoch to current epoch */
691 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
693 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
694 tgt_client_data_update(env, exp);
698 * Update boot epoch when recovery ends
700 void tgt_boot_epoch_update(struct lu_target *tgt)
703 struct ptlrpc_request *req;
705 struct list_head client_list;
708 if (tgt->lut_obd->obd_stopping)
711 rc = lu_env_init(&env, LCT_DT_THREAD);
713 CERROR("%s: can't initialize environment: rc = %d\n",
714 tgt->lut_obd->obd_name, rc);
718 spin_lock(&tgt->lut_translock);
719 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
720 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
721 tgt->lut_lsd.lsd_start_epoch = start_epoch;
722 spin_unlock(&tgt->lut_translock);
724 INIT_LIST_HEAD(&client_list);
726 * The recovery is not yet finished and final queue can still be updated
727 * with resend requests. Move final list to separate one for processing
729 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
730 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
731 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
734 * go through list of exports participated in recovery and
735 * set new epoch for them
737 list_for_each_entry(req, &client_list, rq_list) {
738 LASSERT(!req->rq_export->exp_delayed);
739 if (!req->rq_export->exp_vbr_failed)
740 tgt_client_epoch_update(&env, req->rq_export);
742 /** return list back at once */
743 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
744 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
745 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
747 /** Clear MULTI RPCS incompatibility flag if
748 * - target is MDT and
749 * - there is no client to recover or the recovery was aborted
751 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
752 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
753 tgt->lut_obd->obd_abort_recovery))
754 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
756 /** update server epoch */
757 tgt_server_data_update(&env, tgt, 1);
762 * commit callback, need to update last_committed value
764 struct tgt_last_committed_callback {
765 struct dt_txn_commit_cb llcc_cb;
766 struct lu_target *llcc_tgt;
767 struct obd_export *llcc_exp;
771 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
772 struct dt_txn_commit_cb *cb, int err)
774 struct tgt_last_committed_callback *ccb;
776 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
778 LASSERT(ccb->llcc_exp);
779 LASSERT(ccb->llcc_tgt != NULL);
780 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
782 /* Fast path w/o spinlock, if exp_last_committed was updated
783 * with higher transno, no need to take spinlock and check,
784 * also no need to update obd_last_committed. */
785 if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
787 spin_lock(&ccb->llcc_tgt->lut_translock);
788 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
789 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
791 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
792 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
793 spin_unlock(&ccb->llcc_tgt->lut_translock);
795 ptlrpc_commit_replies(ccb->llcc_exp);
796 tgt_cancel_slc_locks(ccb->llcc_transno);
798 spin_unlock(&ccb->llcc_tgt->lut_translock);
801 class_export_cb_put(ccb->llcc_exp);
802 if (ccb->llcc_transno)
803 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
804 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
808 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
809 struct obd_export *exp, __u64 transno)
811 struct tgt_last_committed_callback *ccb;
812 struct dt_txn_commit_cb *dcb;
820 ccb->llcc_exp = class_export_cb_get(exp);
821 ccb->llcc_transno = transno;
824 dcb->dcb_func = tgt_cb_last_committed;
825 INIT_LIST_HEAD(&dcb->dcb_linkage);
826 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
828 rc = dt_trans_cb_add(th, dcb);
830 class_export_cb_put(exp);
834 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
835 /* report failure to force synchronous operation */
841 struct tgt_new_client_callback {
842 struct dt_txn_commit_cb lncc_cb;
843 struct obd_export *lncc_exp;
846 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
847 struct dt_txn_commit_cb *cb, int err)
849 struct tgt_new_client_callback *ccb;
851 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
853 LASSERT(ccb->lncc_exp->exp_obd);
855 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
856 ccb->lncc_exp->exp_obd->obd_name,
857 ccb->lncc_exp->exp_client_uuid.uuid);
859 spin_lock(&ccb->lncc_exp->exp_lock);
861 ccb->lncc_exp->exp_need_sync = 0;
863 spin_unlock(&ccb->lncc_exp->exp_lock);
864 class_export_cb_put(ccb->lncc_exp);
869 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
871 struct tgt_new_client_callback *ccb;
872 struct dt_txn_commit_cb *dcb;
879 ccb->lncc_exp = class_export_cb_get(exp);
882 dcb->dcb_func = tgt_cb_new_client;
883 INIT_LIST_HEAD(&dcb->dcb_linkage);
884 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
886 rc = dt_trans_cb_add(th, dcb);
888 class_export_cb_put(exp);
895 * Add new client to the last_rcvd upon new connection.
897 * We use a bitmap to locate a free space in the last_rcvd file and initialize
900 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
902 struct tg_export_data *ted = &exp->exp_target_data;
903 struct lu_target *tgt = class_exp2tgt(exp);
908 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
909 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
912 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
915 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
916 * there's no need for extra complication here
918 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
920 if (idx >= LR_MAX_CLIENTS ||
921 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
922 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
923 tgt->lut_obd->obd_name, idx);
926 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
927 idx = find_next_zero_bit(tgt->lut_client_bitmap,
928 LR_MAX_CLIENTS, idx);
932 ted->ted_lr_idx = idx;
933 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
934 idx * tgt->lut_lsd.lsd_client_size;
936 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
938 if (tgt_is_multimodrpcs_client(exp)) {
939 /* Set MULTI RPCS incompatibility flag to prevent previous
940 * Lustre versions to mount a target with reply_data file */
941 atomic_inc(&tgt->lut_num_clients);
942 if (!(tgt->lut_lsd.lsd_feature_incompat &
943 OBD_INCOMPAT_MULTI_RPCS)) {
944 tgt->lut_lsd.lsd_feature_incompat |=
945 OBD_INCOMPAT_MULTI_RPCS;
946 rc = tgt_server_data_update(env, tgt, 1);
948 CERROR("%s: unable to set MULTI RPCS "
949 "incompatibility flag\n",
950 exp->exp_obd->obd_name);
955 /* assign client slot generation */
956 ted->ted_lcd->lcd_generation =
957 atomic_inc_return(&tgt->lut_client_generation);
959 ted->ted_lcd->lcd_generation = 0;
962 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
964 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
965 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
967 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
970 rc = tgt_client_data_update(env, exp);
972 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
973 tgt->lut_obd->obd_name, idx, rc);
977 EXPORT_SYMBOL(tgt_client_new);
979 /* Add an existing client to the MDS in-memory state based on
980 * a client that was previously found in the last_rcvd file and
981 * already has an assigned slot (idx >= 0).
983 * It should not be possible to fail adding an existing client - otherwise
984 * mdt_init_server_data() callsite needs to be fixed.
986 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
988 struct tg_export_data *ted = &exp->exp_target_data;
989 struct lu_target *tgt = class_exp2tgt(exp);
993 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
994 LASSERTF(idx >= 0, "%d\n", idx);
996 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
997 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1000 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1001 CERROR("%s: client %d: bit already set in bitmap!!\n",
1002 tgt->lut_obd->obd_name, idx);
1005 atomic_inc(&tgt->lut_num_clients);
1007 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1009 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1010 ted->ted_lcd->lcd_generation);
1012 ted->ted_lr_idx = idx;
1013 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1014 idx * tgt->lut_lsd.lsd_client_size;
1016 mutex_init(&ted->ted_lcd_lock);
1018 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1023 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1025 struct tg_export_data *ted = &exp->exp_target_data;
1026 struct lu_target *tgt = class_exp2tgt(exp);
1031 LASSERT(ted->ted_lcd);
1033 if (unlikely(tgt == NULL)) {
1034 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1035 class_exp2obd(exp)->obd_name);
1039 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1040 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1041 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1042 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1045 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1046 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1047 ted->ted_lcd->lcd_uuid);
1049 /* Clear the bit _after_ zeroing out the client so we don't
1050 race with filter_client_add and zero out new clients.*/
1051 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1052 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1053 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1057 /* Do not erase record for recoverable client. */
1058 if (exp->exp_flags & OBD_OPT_FAILOVER)
1061 /* Make sure the server's last_transno is up to date.
1062 * This should be done before zeroing client slot so last_transno will
1063 * be in server data or in client data in case of failure */
1064 rc = tgt_server_data_update(env, tgt, 0);
1066 CERROR("%s: failed to update server data, skip client %s "
1067 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1068 ted->ted_lcd->lcd_uuid, rc);
1072 mutex_lock(&ted->ted_lcd_lock);
1073 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1074 rc = tgt_client_data_update(env, exp);
1075 mutex_unlock(&ted->ted_lcd_lock);
1077 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1078 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1079 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1080 ted->ted_lr_idx, ted->ted_lr_off, rc);
1083 EXPORT_SYMBOL(tgt_client_del);
1085 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1086 struct tg_export_data *ted, struct tg_reply_data *trd,
1087 struct thandle *th, bool update_lrd_file)
1089 struct lsd_reply_data *lrd;
1092 lrd = &trd->trd_reply;
1093 /* update export last transno */
1094 mutex_lock(&ted->ted_lcd_lock);
1095 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1096 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1097 mutex_unlock(&ted->ted_lcd_lock);
1099 /* find a empty slot */
1100 i = tgt_find_free_reply_slot(tgt);
1101 if (unlikely(i < 0)) {
1102 CERROR("%s: couldn't find a slot for reply data: "
1103 "rc = %d\n", tgt_name(tgt), i);
1108 if (update_lrd_file) {
1112 /* write reply data to disk */
1113 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1114 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1115 if (unlikely(rc != 0)) {
1116 CERROR("%s: can't update %s file: rc = %d\n",
1117 tgt_name(tgt), REPLY_DATA, rc);
1121 /* add reply data to target export's reply list */
1122 mutex_lock(&ted->ted_lcd_lock);
1123 list_add(&trd->trd_list, &ted->ted_reply_list);
1124 ted->ted_reply_cnt++;
1125 if (ted->ted_reply_cnt > ted->ted_reply_max)
1126 ted->ted_reply_max = ted->ted_reply_cnt;
1127 mutex_unlock(&ted->ted_lcd_lock);
1129 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1130 "tag %hu, client gen %u, slot idx %d\n",
1131 trd, lrd->lrd_xid, lrd->lrd_transno,
1132 trd->trd_tag, lrd->lrd_client_gen, i);
1135 EXPORT_SYMBOL(tgt_add_reply_data);
1138 * last_rcvd & last_committed update callbacks
1140 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1141 struct dt_object *obj, __u64 opdata,
1142 struct thandle *th, struct ptlrpc_request *req)
1144 struct tgt_thread_info *tti = tgt_th_info(env);
1145 struct tgt_session_info *tsi = tgt_ses_info(env);
1146 struct obd_export *exp = tsi->tsi_exp;
1147 struct tg_export_data *ted;
1155 LASSERT(exp != NULL);
1156 ted = &exp->exp_target_data;
1158 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1159 if (ted->ted_lr_idx < 0 && !lw_client)
1160 /* ofd connect may cause transaction before export has
1165 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1167 /* From update replay, tti_transno should be set already */
1168 LASSERT(tti->tti_transno != 0);
1170 spin_lock(&tgt->lut_translock);
1171 if (th->th_result != 0) {
1172 if (tti->tti_transno != 0) {
1173 CERROR("%s: replay transno "LPU64" failed: rc = %d\n",
1174 tgt_name(tgt), tti->tti_transno, th->th_result);
1176 } else if (tti->tti_transno == 0) {
1177 tti->tti_transno = ++tgt->lut_last_transno;
1179 /* should be replay */
1180 if (tti->tti_transno > tgt->lut_last_transno)
1181 tgt->lut_last_transno = tti->tti_transno;
1183 spin_unlock(&tgt->lut_translock);
1185 /** VBR: set new versions */
1186 if (th->th_result == 0 && obj != NULL) {
1187 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1188 dt_version_set(env, dto, tti->tti_transno, th);
1191 /* filling reply data */
1192 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
1193 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1196 req->rq_transno = tti->tti_transno;
1197 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1200 /* if can't add callback, do sync write */
1201 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1204 /* All operations performed by LW clients are synchronous and
1205 * we store the committed transno in the last_rcvd header */
1206 spin_lock(&tgt->lut_translock);
1207 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1208 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1209 spin_unlock(&tgt->lut_translock);
1210 /* Although lightweight (LW) connections have no slot
1211 * in the last_rcvd, we still want to maintain
1212 * the in-memory lsd_client_data structure in order to
1213 * properly handle reply reconstruction. */
1214 rc = tgt_server_data_write(env, tgt, th);
1216 spin_unlock(&tgt->lut_translock);
1218 } else if (ted->ted_lr_off == 0) {
1219 CERROR("%s: client idx %d has offset %lld\n",
1220 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1224 /* Target that supports multiple reply data */
1225 if (tgt_is_multimodrpcs_client(exp)) {
1226 struct tg_reply_data *trd;
1227 struct lsd_reply_data *lrd;
1228 __u64 *pre_versions;
1232 if (unlikely(trd == NULL))
1235 /* fill reply data information */
1236 lrd = &trd->trd_reply;
1237 lrd->lrd_transno = tti->tti_transno;
1239 lrd->lrd_xid = req->rq_xid;
1240 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1241 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1242 lrd->lrd_result = th->th_result;
1243 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1244 write_update = true;
1246 LASSERT(tsi->tsi_xid != 0);
1247 lrd->lrd_xid = tsi->tsi_xid;
1248 lrd->lrd_result = tsi->tsi_result;
1249 lrd->lrd_client_gen = tsi->tsi_client_gen;
1251 pre_versions = NULL;
1252 write_update = false;
1255 lrd->lrd_data = opdata;
1257 trd->trd_pre_versions[0] = pre_versions[0];
1258 trd->trd_pre_versions[1] = pre_versions[1];
1259 trd->trd_pre_versions[2] = pre_versions[2];
1260 trd->trd_pre_versions[3] = pre_versions[3];
1263 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1269 /* Enough for update replay, let's return */
1273 mutex_lock(&ted->ted_lcd_lock);
1274 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1275 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1276 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1277 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1278 ted->ted_lcd->lcd_last_close_result = th->th_result;
1280 /* VBR: save versions in last_rcvd for reconstruct. */
1281 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1284 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1285 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1286 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1287 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1289 transno_p = &ted->ted_lcd->lcd_last_transno;
1290 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1291 ted->ted_lcd->lcd_last_result = th->th_result;
1292 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1293 * see struct ldlm_reply->lock_policy_res1; */
1294 ted->ted_lcd->lcd_last_data = opdata;
1297 /* Update transno in slot only if non-zero number, i.e. no errors */
1298 if (likely(tti->tti_transno != 0)) {
1299 /* Don't overwrite bigger transaction number with lower one.
1300 * That is not sign of problem in all cases, but in any case
1301 * this value should be monotonically increased only. */
1302 if (*transno_p > tti->tti_transno) {
1303 if (!tgt->lut_no_reconstruct) {
1304 CERROR("%s: trying to overwrite bigger transno:"
1305 "on-disk: "LPU64", new: "LPU64" replay: "
1306 "%d. See LU-617.\n", tgt_name(tgt),
1307 *transno_p, tti->tti_transno,
1308 req_is_replay(req));
1309 if (req_is_replay(req)) {
1310 spin_lock(&req->rq_export->exp_lock);
1311 req->rq_export->exp_vbr_failed = 1;
1312 spin_unlock(&req->rq_export->exp_lock);
1314 mutex_unlock(&ted->ted_lcd_lock);
1315 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1318 *transno_p = tti->tti_transno;
1323 tti->tti_off = ted->ted_lr_off;
1324 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1326 mutex_unlock(&ted->ted_lcd_lock);
1330 mutex_unlock(&ted->ted_lcd_lock);
1335 * last_rcvd update for echo client simulation.
1336 * It updates last_rcvd client slot and version of object in
1337 * simple way but with all locks to simulate all drawbacks
1339 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1340 struct lu_target *tgt,
1341 struct dt_object *obj,
1343 struct obd_export *exp)
1345 struct tgt_thread_info *tti = tgt_th_info(env);
1346 struct tg_export_data *ted = &exp->exp_target_data;
1351 tti->tti_transno = 0;
1353 spin_lock(&tgt->lut_translock);
1354 if (th->th_result == 0)
1355 tti->tti_transno = ++tgt->lut_last_transno;
1356 spin_unlock(&tgt->lut_translock);
1358 /** VBR: set new versions */
1359 if (th->th_result == 0 && obj != NULL)
1360 dt_version_set(env, obj, tti->tti_transno, th);
1362 /* if can't add callback, do sync write */
1363 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1366 LASSERT(ted->ted_lr_off > 0);
1368 mutex_lock(&ted->ted_lcd_lock);
1369 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1370 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1371 ted->ted_lcd->lcd_last_result = th->th_result;
1373 tti->tti_off = ted->ted_lr_off;
1374 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1375 mutex_unlock(&ted->ted_lcd_lock);
1379 static int tgt_clients_data_init(const struct lu_env *env,
1380 struct lu_target *tgt,
1381 unsigned long last_size)
1383 struct obd_device *obd = tgt->lut_obd;
1384 struct lr_server_data *lsd = &tgt->lut_lsd;
1385 struct lsd_client_data *lcd = NULL;
1386 struct tg_export_data *ted;
1389 loff_t off = lsd->lsd_client_start;
1390 __u32 generation = 0;
1391 struct cfs_hash *hash = NULL;
1395 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1396 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1402 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1404 GOTO(err_out, rc = -ENODEV);
1406 for (cl_idx = 0; off < last_size; cl_idx++) {
1407 struct obd_export *exp;
1410 /* Don't assume off is incremented properly by
1411 * read_record(), in case sizeof(*lcd)
1412 * isn't the same as fsd->lsd_client_size. */
1413 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1414 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1416 CERROR("%s: error reading last_rcvd %s idx %d off "
1417 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1420 break; /* read error shouldn't cause startup to fail */
1423 if (lcd->lcd_uuid[0] == '\0') {
1424 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1429 last_transno = lcd_last_transno(lcd);
1431 /* These exports are cleaned up by disconnect, so they
1432 * need to be set up like real exports as connect does.
1434 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
1435 " srv lr: "LPU64" lx: "LPU64" gen %u\n", lcd->lcd_uuid,
1436 cl_idx, last_transno, lsd->lsd_last_transno,
1437 lcd_last_xid(lcd), lcd->lcd_generation);
1439 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1441 if (PTR_ERR(exp) == -EALREADY) {
1442 /* export already exists, zero out this one */
1443 CERROR("%s: Duplicate export %s!\n",
1444 tgt_name(tgt), lcd->lcd_uuid);
1447 GOTO(err_out, rc = PTR_ERR(exp));
1450 ted = &exp->exp_target_data;
1451 *ted->ted_lcd = *lcd;
1453 rc = tgt_client_add(env, exp, cl_idx);
1454 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1455 /* VBR: set export last committed version */
1456 exp->exp_last_committed = last_transno;
1457 spin_lock(&exp->exp_lock);
1458 exp->exp_connecting = 0;
1459 exp->exp_in_recovery = 0;
1460 spin_unlock(&exp->exp_lock);
1461 obd->obd_max_recoverable_clients++;
1463 if (tgt->lut_lsd.lsd_feature_incompat &
1464 OBD_INCOMPAT_MULTI_RPCS &&
1465 lcd->lcd_generation != 0) {
1466 /* compute the highest valid client generation */
1467 generation = max(generation, lcd->lcd_generation);
1468 /* fill client_generation <-> export hash table */
1469 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1470 &exp->exp_gen_hash);
1472 CERROR("%s: duplicate export for client "
1474 tgt_name(tgt), lcd->lcd_generation);
1475 class_export_put(exp);
1480 class_export_put(exp);
1482 rc = rev_import_init(exp);
1484 class_unlink_export(exp);
1488 /* Need to check last_rcvd even for duplicated exports. */
1489 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
1490 cl_idx, last_transno);
1492 spin_lock(&tgt->lut_translock);
1493 tgt->lut_last_transno = max(last_transno,
1494 tgt->lut_last_transno);
1495 spin_unlock(&tgt->lut_translock);
1498 /* record highest valid client generation */
1499 atomic_set(&tgt->lut_client_generation, generation);
1503 cfs_hash_putref(hash);
1508 struct server_compat_data {
1515 static struct server_compat_data tgt_scd[] = {
1516 [LDD_F_SV_TYPE_MDT] = {
1517 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1518 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1519 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1520 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1521 OBD_INCOMPAT_MULTI_RPCS,
1522 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1523 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1524 OBD_INCOMPAT_MULTI_OI,
1526 [LDD_F_SV_TYPE_OST] = {
1527 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1528 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1530 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1531 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1535 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1537 struct tgt_thread_info *tti = tgt_th_info(env);
1538 struct lr_server_data *lsd = &tgt->lut_lsd;
1539 unsigned long last_rcvd_size;
1543 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1547 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1549 /* ensure padding in the struct is the correct size */
1550 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1551 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1553 rc = server_name2index(tgt_name(tgt), &index, NULL);
1555 CERROR("%s: Can not get index from name: rc = %d\n",
1559 /* server_name2index() returns type */
1561 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1562 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1566 /* last_rcvd on OST doesn't provide reconstruct support because there
1567 * may be up to 8 in-flight write requests per single slot in
1568 * last_rcvd client data
1570 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1572 if (last_rcvd_size == 0) {
1573 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1575 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1576 sizeof(lsd->lsd_uuid));
1577 lsd->lsd_last_transno = 0;
1578 lsd->lsd_mount_count = 0;
1579 lsd->lsd_server_size = LR_SERVER_SIZE;
1580 lsd->lsd_client_start = LR_CLIENT_START;
1581 lsd->lsd_client_size = LR_CLIENT_SIZE;
1582 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1583 lsd->lsd_osd_index = index;
1584 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1585 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1587 rc = tgt_server_data_read(env, tgt);
1589 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1593 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1594 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1595 "using the wrong disk %s. Were the"
1596 " /dev/ assignments rearranged?\n",
1597 tgt->lut_obd->obd_uuid.uuid,
1602 if (lsd->lsd_osd_index != index) {
1603 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1604 "is different with the index %d in"
1605 "config log, It might be disk"
1606 "corruption!\n", tgt_name(tgt),
1607 lsd->lsd_osd_index, index);
1612 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1613 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1615 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1619 if (type == LDD_F_SV_TYPE_MDT)
1620 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1622 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1623 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1625 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1628 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1629 if (type == LDD_F_SV_TYPE_MDT &&
1630 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1631 if (last_rcvd_size > lsd->lsd_client_start) {
1632 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1633 "remove all clients for interop needs\n",
1635 rc = tgt_truncate_last_rcvd(env, tgt,
1636 lsd->lsd_client_start);
1639 last_rcvd_size = lsd->lsd_client_start;
1641 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1642 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1645 spin_lock(&tgt->lut_translock);
1646 tgt->lut_last_transno = lsd->lsd_last_transno;
1647 spin_unlock(&tgt->lut_translock);
1649 lsd->lsd_mount_count++;
1651 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1652 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
1653 tgt_name(tgt), tgt->lut_last_transno);
1654 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
1655 tgt_name(tgt), lsd->lsd_mount_count);
1656 CDEBUG(D_INODE, "%s: server data size: %u\n",
1657 tgt_name(tgt), lsd->lsd_server_size);
1658 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1659 tgt_name(tgt), lsd->lsd_client_start);
1660 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1661 tgt_name(tgt), lsd->lsd_client_size);
1662 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1663 tgt_name(tgt), last_rcvd_size);
1664 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1665 tgt_name(tgt), lsd->lsd_subdir_count);
1666 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1667 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1668 (last_rcvd_size - lsd->lsd_client_start) /
1669 lsd->lsd_client_size);
1670 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1672 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1673 lsd->lsd_client_size == 0) {
1674 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1678 if (!tgt->lut_obd->obd_replayable)
1679 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1681 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1683 GOTO(err_client, rc);
1685 spin_lock(&tgt->lut_translock);
1686 /* obd_last_committed is used for compatibility
1687 * with other lustre recovery code */
1688 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1689 spin_unlock(&tgt->lut_translock);
1691 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1692 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1694 /* save it, so mount count and last_transno is current */
1695 rc = tgt_server_data_update(env, tgt, 0);
1697 GOTO(err_client, rc);
1702 class_disconnect_exports(tgt->lut_obd);
1706 /* add credits for last_rcvd update */
1707 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1710 struct lu_target *tgt = cookie;
1711 struct tgt_session_info *tsi;
1712 struct tgt_thread_info *tti = tgt_th_info(env);
1713 struct dt_object *dto;
1716 /* if there is no session, then this transaction is not result of
1717 * request processing but some local operation */
1718 if (env->le_ses == NULL)
1721 LASSERT(tgt->lut_last_rcvd);
1722 tsi = tgt_ses_info(env);
1723 /* OFD may start transaction without export assigned */
1724 if (tsi->tsi_exp == NULL)
1727 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1729 * Use maximum possible file offset for declaration to ensure
1730 * ZFS will reserve enough credits for a write anywhere in this
1731 * file, since we don't know where in the file the write will be
1732 * because a replay slot has not been assigned. This should be
1733 * replaced by dmu_tx_hold_append() when available.
1735 tti->tti_off = atomic_read(&tgt->lut_num_clients) * 8 *
1736 sizeof(struct lsd_reply_data);
1737 tti->tti_buf.lb_buf = NULL;
1738 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1739 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1740 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1745 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1747 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1748 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1754 if (tsi->tsi_vbr_obj != NULL &&
1755 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1756 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1757 rc = dt_declare_version_set(env, dto, th);
1763 /* Update last_rcvd records with latests transaction data */
1764 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1767 struct lu_target *tgt = cookie;
1768 struct tgt_session_info *tsi;
1769 struct tgt_thread_info *tti = tgt_th_info(env);
1770 struct dt_object *obj = NULL;
1774 if (env->le_ses == NULL)
1777 tsi = tgt_ses_info(env);
1778 /* OFD may start transaction without export assigned */
1779 if (tsi->tsi_exp == NULL)
1782 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1784 if (tti->tti_has_trans && !echo_client) {
1785 if (tti->tti_mult_trans == 0) {
1786 CDEBUG(D_HA, "More than one transaction "LPU64"\n",
1790 /* we need another transno to be assigned */
1791 tti->tti_transno = 0;
1792 } else if (th->th_result == 0) {
1793 tti->tti_has_trans = 1;
1796 if (tsi->tsi_vbr_obj != NULL &&
1797 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1798 obj = tsi->tsi_vbr_obj;
1801 if (unlikely(echo_client)) /* echo client special case */
1802 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1805 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1810 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1812 struct tgt_thread_info *tti = tgt_th_info(env);
1813 struct lsd_reply_data *lrd = &tti->tti_lrd;
1814 unsigned long reply_data_size;
1816 struct lsd_reply_header *lrh = NULL;
1817 struct lsd_client_data *lcd = NULL;
1818 struct tg_reply_data *trd = NULL;
1821 struct cfs_hash *hash = NULL;
1822 struct obd_export *exp;
1823 struct tg_export_data *ted;
1824 int reply_data_recovered = 0;
1826 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1829 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1833 GOTO(out, rc = -ENOMEM);
1835 if (reply_data_size == 0) {
1836 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1838 lrh->lrh_magic = LRH_MAGIC;
1839 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1840 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1841 rc = tgt_reply_header_write(env, tgt, lrh);
1843 CERROR("%s: error writing %s: rc = %d\n",
1844 tgt_name(tgt), REPLY_DATA, rc);
1848 rc = tgt_reply_header_read(env, tgt, lrh);
1850 CERROR("%s: error reading %s: rc = %d\n",
1851 tgt_name(tgt), REPLY_DATA, rc);
1854 if (lrh->lrh_magic != LRH_MAGIC ||
1855 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1856 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1857 CERROR("%s: invalid header in %s\n",
1858 tgt_name(tgt), REPLY_DATA);
1859 GOTO(out, rc = -EINVAL);
1862 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1864 GOTO(out, rc = -ENODEV);
1868 GOTO(out, rc = -ENOMEM);
1872 GOTO(out, rc = -ENOMEM);
1874 /* Load reply_data from disk */
1875 for (idx = 0, off = sizeof(struct lsd_reply_header);
1876 off < reply_data_size;
1877 idx++, off += sizeof(struct lsd_reply_data)) {
1878 rc = tgt_reply_data_read(env, tgt, lrd, off);
1880 CERROR("%s: error reading %s: rc = %d\n",
1881 tgt_name(tgt), REPLY_DATA, rc);
1885 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1887 /* old reply data from a disconnected client */
1890 ted = &exp->exp_target_data;
1891 mutex_lock(&ted->ted_lcd_lock);
1893 /* create in-memory reply_data and link it to
1894 * target export's reply list */
1895 rc = tgt_set_reply_slot(tgt, idx);
1897 mutex_unlock(&ted->ted_lcd_lock);
1900 trd->trd_reply = *lrd;
1901 trd->trd_pre_versions[0] = 0;
1902 trd->trd_pre_versions[1] = 0;
1903 trd->trd_pre_versions[2] = 0;
1904 trd->trd_pre_versions[3] = 0;
1905 trd->trd_index = idx;
1907 list_add(&trd->trd_list, &ted->ted_reply_list);
1908 ted->ted_reply_cnt++;
1909 if (ted->ted_reply_cnt > ted->ted_reply_max)
1910 ted->ted_reply_max = ted->ted_reply_cnt;
1912 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1913 "transno %llu, client gen %u, slot idx %d\n",
1914 tgt_name(tgt), trd, lrd->lrd_xid,
1915 lrd->lrd_transno, lrd->lrd_client_gen,
1918 /* update export last committed transation */
1919 exp->exp_last_committed = max(exp->exp_last_committed,
1922 mutex_unlock(&ted->ted_lcd_lock);
1923 class_export_put(exp);
1925 /* update target last committed transaction */
1926 spin_lock(&tgt->lut_translock);
1927 tgt->lut_last_transno = max(tgt->lut_last_transno,
1929 spin_unlock(&tgt->lut_translock);
1931 reply_data_recovered++;
1935 GOTO(out, rc = -ENOMEM);
1937 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1938 tgt_name(tgt), reply_data_recovered);
1941 spin_lock(&tgt->lut_translock);
1942 /* obd_last_committed is used for compatibility
1943 * with other lustre recovery code */
1944 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1945 spin_unlock(&tgt->lut_translock);
1951 cfs_hash_putref(hash);
1961 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
1964 struct tg_reply_data *found = NULL;
1965 struct tg_reply_data *reply;
1967 mutex_lock(&ted->ted_lcd_lock);
1968 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
1969 if (reply->trd_reply.lrd_xid == xid) {
1974 mutex_unlock(&ted->ted_lcd_lock);
1977 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
1979 /* Look for a reply data matching specified request @req
1980 * A copy is returned in @trd if the pointer is not NULL
1982 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
1984 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1985 struct tg_reply_data *reply;
1988 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
1989 if (reply != NULL) {
1995 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
1996 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2001 EXPORT_SYMBOL(tgt_lookup_reply);
2003 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2005 struct tg_export_data *ted = &exp->exp_target_data;
2006 struct lu_target *lut = class_exp2tgt(exp);
2007 struct tg_reply_data *trd, *tmp;
2009 mutex_lock(&ted->ted_lcd_lock);
2010 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2011 if (trd->trd_reply.lrd_xid > rcvd_xid)
2013 ted->ted_release_xid++;
2014 tgt_release_reply_data(lut, ted, trd);
2016 mutex_unlock(&ted->ted_lcd_lock);
2021 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2023 struct tg_export_data *ted = &exp->exp_target_data;
2024 struct lu_target *lut = class_exp2tgt(exp);
2025 struct tg_reply_data *trd, *tmp;
2030 mutex_lock(&ted->ted_lcd_lock);
2031 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2032 if (trd->trd_tag != tag)
2034 ted->ted_release_tag++;
2035 tgt_release_reply_data(lut, ted, trd);
2038 mutex_unlock(&ted->ted_lcd_lock);