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(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) * sizeof(long));
57 spin_lock(&lut->lut_client_bitmap_lock);
59 if (lut->lut_reply_bitmap[chunk] != NULL) {
60 /* someone else already allocated the bitmap for this chunk */
61 spin_unlock(&lut->lut_client_bitmap_lock);
62 OBD_FREE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
67 lut->lut_reply_bitmap[chunk] = bm;
69 spin_unlock(&lut->lut_client_bitmap_lock);
74 /* Look for an available reply data slot in the bitmap
76 * Allocate bitmap chunk when first used
77 * XXX algo could be improved if this routine limits performance
79 static int tgt_find_free_reply_slot(struct lu_target *lut)
86 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
87 /* allocate the bitmap chunk if necessary */
88 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
89 rc = tgt_bitmap_chunk_alloc(lut, chunk);
93 bmp = lut->lut_reply_bitmap[chunk];
95 /* look for an available slot in this chunk */
97 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
98 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
102 if (test_and_set_bit(b, bmp) == 0)
103 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
110 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
112 * Allocate the bitmap chunk if necessary
114 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
120 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
121 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
123 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
124 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
126 /* allocate the bitmap chunk if necessary */
127 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
128 rc = tgt_bitmap_chunk_alloc(lut, chunk);
133 /* mark the slot 'used' in this chunk */
134 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
135 CERROR("%s: slot %d already set in bitmap\n",
144 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
147 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
152 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
153 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
155 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
156 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
158 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
159 CERROR("%s: slot %d already clear in bitmap\n",
168 /* Read header of reply_data file of target @tgt into structure @lrh */
169 static int tgt_reply_header_read(const struct lu_env *env,
170 struct lu_target *tgt,
171 struct lsd_reply_header *lrh)
174 struct lsd_reply_header buf;
175 struct tgt_thread_info *tti = tgt_th_info(env);
178 tti->tti_buf.lb_buf = &buf;
179 tti->tti_buf.lb_len = sizeof(buf);
181 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
186 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
187 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
188 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
190 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
191 "header_size=%d reply_size=%d\n",
192 tgt->lut_obd->obd_name, REPLY_DATA,
193 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
198 /* Write header into replay_data file of target @tgt from structure @lrh */
199 static int tgt_reply_header_write(const struct lu_env *env,
200 struct lu_target *tgt,
201 struct lsd_reply_header *lrh)
204 struct lsd_reply_header buf;
205 struct tgt_thread_info *tti = tgt_th_info(env);
207 struct dt_object *dto;
209 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
210 "header_size=%d reply_size=%d\n",
211 tgt->lut_obd->obd_name, REPLY_DATA,
212 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
214 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
215 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
216 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
218 th = dt_trans_create(env, tgt->lut_bottom);
224 tti->tti_buf.lb_buf = &buf;
225 tti->tti_buf.lb_len = sizeof(buf);
227 rc = dt_declare_record_write(env, tgt->lut_reply_data,
228 &tti->tti_buf, tti->tti_off, th);
232 rc = dt_trans_start(env, tgt->lut_bottom, th);
236 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
237 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
239 dt_trans_stop(env, tgt->lut_bottom, th);
243 /* Write the reply data @lrd into reply_data file of target @tgt
246 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
247 struct lsd_reply_data *lrd, loff_t off,
250 struct tgt_thread_info *tti = tgt_th_info(env);
251 struct dt_object *dto;
252 struct lsd_reply_data *buf = &tti->tti_lrd;
254 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
256 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
257 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
258 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
259 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
260 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
262 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
265 tti->tti_buf.lb_buf = buf;
266 tti->tti_buf.lb_len = sizeof(*buf);
268 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
269 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
272 /* Read the reply data from reply_data file of target @tgt at offset @off
273 * into structure @lrd
275 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
276 struct lsd_reply_data *lrd, loff_t off)
279 struct tgt_thread_info *tti = tgt_th_info(env);
280 struct lsd_reply_data *buf = &tti->tti_lrd;
283 tti->tti_buf.lb_buf = buf;
284 tti->tti_buf.lb_len = sizeof(*buf);
286 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
291 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
292 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
293 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
294 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
295 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
301 /* Free the in-memory reply data structure @trd and release
302 * the corresponding slot in the reply_data file of target @lut
303 * Called with ted_lcd_lock held
305 static void tgt_free_reply_data(struct lu_target *lut,
306 struct tg_export_data *ted,
307 struct tg_reply_data *trd)
309 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
310 "client gen %u, slot idx %d\n",
311 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
312 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
315 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
317 list_del(&trd->trd_list);
318 ted->ted_reply_cnt--;
320 tgt_clear_reply_slot(lut, trd->trd_index);
324 /* Release the reply data @trd from target @lut
325 * The reply data with the highest transno for this export
326 * is retained to ensure correctness of target recovery
327 * Called with ted_lcd_lock held
329 static void tgt_release_reply_data(struct lu_target *lut,
330 struct tg_export_data *ted,
331 struct tg_reply_data *trd)
333 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
334 "client gen %u, slot idx %d\n",
335 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
336 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
339 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
341 /* Do not free the reply data corresponding to the
342 * highest transno of this export.
343 * This ensures on-disk reply data is kept and
344 * last committed transno can be restored from disk in case
347 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
348 /* free previous retained reply */
349 if (ted->ted_reply_last != NULL)
350 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
351 /* retain the reply */
352 list_del_init(&trd->trd_list);
353 ted->ted_reply_last = trd;
355 tgt_free_reply_data(lut, ted, trd);
359 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
361 tti->tti_buf.lb_buf = &tti->tti_lsd;
362 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
363 return &tti->tti_buf;
366 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
368 tti->tti_buf.lb_buf = &tti->tti_lcd;
369 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
370 return &tti->tti_buf;
374 * Allocate in-memory data for client slot related to export.
376 int tgt_client_alloc(struct obd_export *exp)
379 LASSERT(exp != exp->exp_obd->obd_self_export);
381 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
382 if (exp->exp_target_data.ted_lcd == NULL)
384 /* Mark that slot is not yet valid, 0 doesn't work here */
385 exp->exp_target_data.ted_lr_idx = -1;
386 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
387 mutex_init(&exp->exp_target_data.ted_lcd_lock);
390 EXPORT_SYMBOL(tgt_client_alloc);
393 * Free in-memory data for client slot related to export.
395 void tgt_client_free(struct obd_export *exp)
397 struct tg_export_data *ted = &exp->exp_target_data;
398 struct lu_target *lut = class_exp2tgt(exp);
399 struct tg_reply_data *trd, *tmp;
401 LASSERT(exp != exp->exp_obd->obd_self_export);
403 /* free reply data */
404 mutex_lock(&ted->ted_lcd_lock);
405 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
406 tgt_release_reply_data(lut, ted, trd);
408 if (ted->ted_reply_last != NULL) {
409 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
410 ted->ted_reply_last = NULL;
412 mutex_unlock(&ted->ted_lcd_lock);
414 if (!hlist_unhashed(&exp->exp_gen_hash))
415 cfs_hash_del(exp->exp_obd->obd_gen_hash,
416 &ted->ted_lcd->lcd_generation,
419 OBD_FREE_PTR(ted->ted_lcd);
422 /* Target may have been freed (see LU-7430)
423 * Slot may be not yet assigned */
424 if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
428 /* Clear bit when lcd is freed */
429 LASSERT(lut && lut->lut_client_bitmap);
430 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
431 CERROR("%s: client %u bit already clear in bitmap\n",
432 exp->exp_obd->obd_name, ted->ted_lr_idx);
436 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
437 atomic_dec(&lut->lut_num_clients);
439 EXPORT_SYMBOL(tgt_client_free);
441 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
442 struct lsd_client_data *lcd, loff_t *off, int index)
444 struct tgt_thread_info *tti = tgt_th_info(env);
448 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
450 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
451 lcd_le_to_cpu(&tti->tti_lcd, lcd);
452 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
453 lcd->lcd_last_close_result =
454 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
457 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = "LPU64
458 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
459 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
460 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
461 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
462 lcd->lcd_last_result, lcd->lcd_last_data,
463 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
464 lcd->lcd_last_close_result, rc);
468 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
469 struct lsd_client_data *lcd, loff_t *off,
472 struct tgt_thread_info *tti = tgt_th_info(env);
473 struct dt_object *dto;
475 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
476 lcd->lcd_last_close_result =
477 ptlrpc_status_hton(lcd->lcd_last_close_result);
478 lcd_cpu_to_le(lcd, &tti->tti_lcd);
481 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
482 return dt_record_write(env, dto, &tti->tti_buf, off, th);
486 * Update client data in last_rcvd
488 static int tgt_client_data_update(const struct lu_env *env,
489 struct obd_export *exp)
491 struct tg_export_data *ted = &exp->exp_target_data;
492 struct lu_target *tgt = class_exp2tgt(exp);
493 struct tgt_thread_info *tti = tgt_th_info(env);
499 if (unlikely(tgt == NULL)) {
500 CDEBUG(D_ERROR, "%s: No target for connected export\n",
501 class_exp2obd(exp)->obd_name);
505 th = dt_trans_create(env, tgt->lut_bottom);
510 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
512 ted->ted_lr_off, th);
516 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
520 * Until this operations will be committed the sync is needed
521 * for this export. This should be done _after_ starting the
522 * transaction so that many connecting clients will not bring
523 * server down with lots of sync writes.
525 rc = tgt_new_client_cb_add(th, exp);
527 /* can't add callback, do sync now */
530 spin_lock(&exp->exp_lock);
531 exp->exp_need_sync = 1;
532 spin_unlock(&exp->exp_lock);
535 tti->tti_off = ted->ted_lr_off;
536 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
539 dt_trans_stop(env, tgt->lut_bottom, th);
540 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
541 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
542 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
547 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
549 struct tgt_thread_info *tti = tgt_th_info(env);
554 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
557 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
559 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
560 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
561 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
565 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
568 struct tgt_thread_info *tti = tgt_th_info(env);
569 struct dt_object *dto;
576 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
578 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
579 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
581 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
582 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
583 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
589 * Update server data in last_rcvd
591 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
594 struct tgt_thread_info *tti = tgt_th_info(env);
601 "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
602 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
603 tgt->lut_last_transno);
605 /* Always save latest transno to keep it fresh */
606 spin_lock(&tgt->lut_translock);
607 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
608 spin_unlock(&tgt->lut_translock);
610 th = dt_trans_create(env, tgt->lut_bottom);
617 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
618 &tti->tti_buf, tti->tti_off, th);
622 rc = dt_trans_start(env, tgt->lut_bottom, th);
626 rc = tgt_server_data_write(env, tgt, th);
628 dt_trans_stop(env, tgt->lut_bottom, th);
630 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
631 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
632 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
635 EXPORT_SYMBOL(tgt_server_data_update);
637 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
640 struct dt_object *dt = tgt->lut_last_rcvd;
648 attr.la_valid = LA_SIZE;
650 th = dt_trans_create(env, tgt->lut_bottom);
653 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
656 rc = dt_declare_attr_set(env, dt, &attr, th);
659 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
663 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
665 rc = dt_attr_set(env, dt, &attr, th);
668 dt_trans_stop(env, tgt->lut_bottom, th);
673 static void tgt_client_epoch_update(const struct lu_env *env,
674 struct obd_export *exp)
676 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
677 struct lu_target *tgt = class_exp2tgt(exp);
679 LASSERT(tgt && tgt->lut_bottom);
680 /** VBR: set client last_epoch to current epoch */
681 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
683 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
684 tgt_client_data_update(env, exp);
688 * Update boot epoch when recovery ends
690 void tgt_boot_epoch_update(struct lu_target *tgt)
693 struct ptlrpc_request *req;
695 struct list_head client_list;
698 if (tgt->lut_obd->obd_stopping)
701 rc = lu_env_init(&env, LCT_DT_THREAD);
703 CERROR("%s: can't initialize environment: rc = %d\n",
704 tgt->lut_obd->obd_name, rc);
708 spin_lock(&tgt->lut_translock);
709 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
710 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
711 tgt->lut_lsd.lsd_start_epoch = start_epoch;
712 spin_unlock(&tgt->lut_translock);
714 INIT_LIST_HEAD(&client_list);
716 * The recovery is not yet finished and final queue can still be updated
717 * with resend requests. Move final list to separate one for processing
719 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
720 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
721 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
724 * go through list of exports participated in recovery and
725 * set new epoch for them
727 list_for_each_entry(req, &client_list, rq_list) {
728 LASSERT(!req->rq_export->exp_delayed);
729 if (!req->rq_export->exp_vbr_failed)
730 tgt_client_epoch_update(&env, req->rq_export);
732 /** return list back at once */
733 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
734 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
735 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
737 /** Clear MULTI RPCS incompatibility flag if
738 * - target is MDT and
739 * - there is no client to recover or the recovery was aborted
741 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
742 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
743 tgt->lut_obd->obd_abort_recovery))
744 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
746 /** update server epoch */
747 tgt_server_data_update(&env, tgt, 1);
752 * commit callback, need to update last_committed value
754 struct tgt_last_committed_callback {
755 struct dt_txn_commit_cb llcc_cb;
756 struct lu_target *llcc_tgt;
757 struct obd_export *llcc_exp;
761 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
762 struct dt_txn_commit_cb *cb, int err)
764 struct tgt_last_committed_callback *ccb;
766 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
768 LASSERT(ccb->llcc_exp);
769 LASSERT(ccb->llcc_tgt != NULL);
770 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
772 /* Fast path w/o spinlock, if exp_last_committed was updated
773 * with higher transno, no need to take spinlock and check,
774 * also no need to update obd_last_committed. */
775 if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
777 spin_lock(&ccb->llcc_tgt->lut_translock);
778 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
779 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
781 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
782 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
783 spin_unlock(&ccb->llcc_tgt->lut_translock);
784 ptlrpc_commit_replies(ccb->llcc_exp);
786 spin_unlock(&ccb->llcc_tgt->lut_translock);
789 class_export_cb_put(ccb->llcc_exp);
790 if (ccb->llcc_transno)
791 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
792 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
796 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
797 struct obd_export *exp, __u64 transno)
799 struct tgt_last_committed_callback *ccb;
800 struct dt_txn_commit_cb *dcb;
808 ccb->llcc_exp = class_export_cb_get(exp);
809 ccb->llcc_transno = transno;
812 dcb->dcb_func = tgt_cb_last_committed;
813 INIT_LIST_HEAD(&dcb->dcb_linkage);
814 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
816 rc = dt_trans_cb_add(th, dcb);
818 class_export_cb_put(exp);
822 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
823 /* report failure to force synchronous operation */
829 struct tgt_new_client_callback {
830 struct dt_txn_commit_cb lncc_cb;
831 struct obd_export *lncc_exp;
834 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
835 struct dt_txn_commit_cb *cb, int err)
837 struct tgt_new_client_callback *ccb;
839 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
841 LASSERT(ccb->lncc_exp->exp_obd);
843 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
844 ccb->lncc_exp->exp_obd->obd_name,
845 ccb->lncc_exp->exp_client_uuid.uuid);
847 spin_lock(&ccb->lncc_exp->exp_lock);
849 ccb->lncc_exp->exp_need_sync = 0;
851 spin_unlock(&ccb->lncc_exp->exp_lock);
852 class_export_cb_put(ccb->lncc_exp);
857 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
859 struct tgt_new_client_callback *ccb;
860 struct dt_txn_commit_cb *dcb;
867 ccb->lncc_exp = class_export_cb_get(exp);
870 dcb->dcb_func = tgt_cb_new_client;
871 INIT_LIST_HEAD(&dcb->dcb_linkage);
872 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
874 rc = dt_trans_cb_add(th, dcb);
876 class_export_cb_put(exp);
883 * Add new client to the last_rcvd upon new connection.
885 * We use a bitmap to locate a free space in the last_rcvd file and initialize
888 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
890 struct tg_export_data *ted = &exp->exp_target_data;
891 struct lu_target *tgt = class_exp2tgt(exp);
896 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
897 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
900 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
903 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
904 * there's no need for extra complication here
906 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
908 if (idx >= LR_MAX_CLIENTS ||
909 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
910 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
911 tgt->lut_obd->obd_name, idx);
914 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
915 idx = find_next_zero_bit(tgt->lut_client_bitmap,
916 LR_MAX_CLIENTS, idx);
920 ted->ted_lr_idx = idx;
921 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
922 idx * tgt->lut_lsd.lsd_client_size;
924 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
926 if (tgt_is_multimodrpcs_client(exp)) {
927 /* Set MULTI RPCS incompatibility flag to prevent previous
928 * Lustre versions to mount a target with reply_data file */
929 atomic_inc(&tgt->lut_num_clients);
930 if (!(tgt->lut_lsd.lsd_feature_incompat &
931 OBD_INCOMPAT_MULTI_RPCS)) {
932 tgt->lut_lsd.lsd_feature_incompat |=
933 OBD_INCOMPAT_MULTI_RPCS;
934 rc = tgt_server_data_update(env, tgt, 1);
936 CERROR("%s: unable to set MULTI RPCS "
937 "incompatibility flag\n",
938 exp->exp_obd->obd_name);
943 /* assign client slot generation */
944 ted->ted_lcd->lcd_generation =
945 atomic_inc_return(&tgt->lut_client_generation);
947 ted->ted_lcd->lcd_generation = 0;
950 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
952 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
953 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
955 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
958 rc = tgt_client_data_update(env, exp);
960 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
961 tgt->lut_obd->obd_name, idx, rc);
965 EXPORT_SYMBOL(tgt_client_new);
967 /* Add an existing client to the MDS in-memory state based on
968 * a client that was previously found in the last_rcvd file and
969 * already has an assigned slot (idx >= 0).
971 * It should not be possible to fail adding an existing client - otherwise
972 * mdt_init_server_data() callsite needs to be fixed.
974 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
976 struct tg_export_data *ted = &exp->exp_target_data;
977 struct lu_target *tgt = class_exp2tgt(exp);
981 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
982 LASSERTF(idx >= 0, "%d\n", idx);
984 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
985 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
988 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
989 CERROR("%s: client %d: bit already set in bitmap!!\n",
990 tgt->lut_obd->obd_name, idx);
993 atomic_inc(&tgt->lut_num_clients);
995 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
997 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
998 ted->ted_lcd->lcd_generation);
1000 ted->ted_lr_idx = idx;
1001 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1002 idx * tgt->lut_lsd.lsd_client_size;
1004 mutex_init(&ted->ted_lcd_lock);
1006 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1011 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1013 struct tg_export_data *ted = &exp->exp_target_data;
1014 struct lu_target *tgt = class_exp2tgt(exp);
1019 LASSERT(ted->ted_lcd);
1021 if (unlikely(tgt == NULL)) {
1022 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1023 class_exp2obd(exp)->obd_name);
1027 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1028 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1029 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1030 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1033 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1034 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1035 ted->ted_lcd->lcd_uuid);
1037 /* Clear the bit _after_ zeroing out the client so we don't
1038 race with filter_client_add and zero out new clients.*/
1039 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1040 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1041 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1045 /* Do not erase record for recoverable client. */
1046 if (exp->exp_flags & OBD_OPT_FAILOVER)
1049 /* Make sure the server's last_transno is up to date.
1050 * This should be done before zeroing client slot so last_transno will
1051 * be in server data or in client data in case of failure */
1052 rc = tgt_server_data_update(env, tgt, 0);
1054 CERROR("%s: failed to update server data, skip client %s "
1055 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1056 ted->ted_lcd->lcd_uuid, rc);
1060 mutex_lock(&ted->ted_lcd_lock);
1061 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1062 rc = tgt_client_data_update(env, exp);
1063 mutex_unlock(&ted->ted_lcd_lock);
1065 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1066 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1067 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1068 ted->ted_lr_idx, ted->ted_lr_off, rc);
1071 EXPORT_SYMBOL(tgt_client_del);
1073 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1074 struct tg_export_data *ted, struct tg_reply_data *trd,
1075 struct thandle *th, bool update_lrd_file)
1077 struct lsd_reply_data *lrd;
1080 lrd = &trd->trd_reply;
1081 /* update export last transno */
1082 mutex_lock(&ted->ted_lcd_lock);
1083 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1084 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1085 mutex_unlock(&ted->ted_lcd_lock);
1087 /* find a empty slot */
1088 i = tgt_find_free_reply_slot(tgt);
1089 if (unlikely(i < 0)) {
1090 CERROR("%s: couldn't find a slot for reply data: "
1091 "rc = %d\n", tgt_name(tgt), i);
1096 if (update_lrd_file) {
1100 /* write reply data to disk */
1101 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1102 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1103 if (unlikely(rc != 0)) {
1104 CERROR("%s: can't update %s file: rc = %d\n",
1105 tgt_name(tgt), REPLY_DATA, rc);
1109 /* add reply data to target export's reply list */
1110 mutex_lock(&ted->ted_lcd_lock);
1111 list_add(&trd->trd_list, &ted->ted_reply_list);
1112 ted->ted_reply_cnt++;
1113 if (ted->ted_reply_cnt > ted->ted_reply_max)
1114 ted->ted_reply_max = ted->ted_reply_cnt;
1115 mutex_unlock(&ted->ted_lcd_lock);
1117 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1118 "tag %hu, client gen %u, slot idx %d\n",
1119 trd, lrd->lrd_xid, lrd->lrd_transno,
1120 trd->trd_tag, lrd->lrd_client_gen, i);
1123 EXPORT_SYMBOL(tgt_add_reply_data);
1126 * last_rcvd & last_committed update callbacks
1128 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1129 struct dt_object *obj, __u64 opdata,
1130 struct thandle *th, struct ptlrpc_request *req)
1132 struct tgt_thread_info *tti = tgt_th_info(env);
1133 struct tgt_session_info *tsi = tgt_ses_info(env);
1134 struct obd_export *exp = tsi->tsi_exp;
1135 struct tg_export_data *ted;
1143 LASSERT(exp != NULL);
1144 ted = &exp->exp_target_data;
1146 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1147 if (ted->ted_lr_idx < 0 && !lw_client)
1148 /* ofd connect may cause transaction before export has
1153 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1155 /* From update replay, tti_transno should be set already */
1156 LASSERT(tti->tti_transno != 0);
1158 spin_lock(&tgt->lut_translock);
1159 if (th->th_result != 0) {
1160 if (tti->tti_transno != 0) {
1161 CERROR("%s: replay transno "LPU64" failed: rc = %d\n",
1162 tgt_name(tgt), tti->tti_transno, th->th_result);
1164 } else if (tti->tti_transno == 0) {
1165 tti->tti_transno = ++tgt->lut_last_transno;
1167 /* should be replay */
1168 if (tti->tti_transno > tgt->lut_last_transno)
1169 tgt->lut_last_transno = tti->tti_transno;
1171 spin_unlock(&tgt->lut_translock);
1173 /** VBR: set new versions */
1174 if (th->th_result == 0 && obj != NULL) {
1175 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1176 dt_version_set(env, dto, tti->tti_transno, th);
1179 /* filling reply data */
1180 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
1181 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1184 req->rq_transno = tti->tti_transno;
1185 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1188 /* if can't add callback, do sync write */
1189 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1192 /* All operations performed by LW clients are synchronous and
1193 * we store the committed transno in the last_rcvd header */
1194 spin_lock(&tgt->lut_translock);
1195 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1196 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1197 spin_unlock(&tgt->lut_translock);
1198 /* Although lightweight (LW) connections have no slot
1199 * in the last_rcvd, we still want to maintain
1200 * the in-memory lsd_client_data structure in order to
1201 * properly handle reply reconstruction. */
1202 rc = tgt_server_data_write(env, tgt, th);
1204 spin_unlock(&tgt->lut_translock);
1206 } else if (ted->ted_lr_off == 0) {
1207 CERROR("%s: client idx %d has offset %lld\n",
1208 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1212 /* Target that supports multiple reply data */
1213 if (tgt_is_multimodrpcs_client(exp)) {
1214 struct tg_reply_data *trd;
1215 struct lsd_reply_data *lrd;
1216 __u64 *pre_versions;
1220 if (unlikely(trd == NULL))
1223 /* fill reply data information */
1224 lrd = &trd->trd_reply;
1225 lrd->lrd_transno = tti->tti_transno;
1227 lrd->lrd_xid = req->rq_xid;
1228 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1229 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1230 lrd->lrd_result = th->th_result;
1231 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1232 write_update = true;
1234 LASSERT(tsi->tsi_xid != 0);
1235 lrd->lrd_xid = tsi->tsi_xid;
1236 lrd->lrd_result = tsi->tsi_result;
1237 lrd->lrd_client_gen = tsi->tsi_client_gen;
1239 pre_versions = NULL;
1240 write_update = false;
1243 lrd->lrd_data = opdata;
1245 trd->trd_pre_versions[0] = pre_versions[0];
1246 trd->trd_pre_versions[1] = pre_versions[1];
1247 trd->trd_pre_versions[2] = pre_versions[2];
1248 trd->trd_pre_versions[3] = pre_versions[3];
1251 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1257 /* Enough for update replay, let's return */
1261 mutex_lock(&ted->ted_lcd_lock);
1262 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1263 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1264 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1265 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1266 ted->ted_lcd->lcd_last_close_result = th->th_result;
1268 /* VBR: save versions in last_rcvd for reconstruct. */
1269 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1272 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1273 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1274 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1275 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1277 transno_p = &ted->ted_lcd->lcd_last_transno;
1278 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1279 ted->ted_lcd->lcd_last_result = th->th_result;
1280 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1281 * see struct ldlm_reply->lock_policy_res1; */
1282 ted->ted_lcd->lcd_last_data = opdata;
1285 /* Update transno in slot only if non-zero number, i.e. no errors */
1286 if (likely(tti->tti_transno != 0)) {
1287 /* Don't overwrite bigger transaction number with lower one.
1288 * That is not sign of problem in all cases, but in any case
1289 * this value should be monotonically increased only. */
1290 if (*transno_p > tti->tti_transno) {
1291 if (!tgt->lut_no_reconstruct) {
1292 CERROR("%s: trying to overwrite bigger transno:"
1293 "on-disk: "LPU64", new: "LPU64" replay: "
1294 "%d. See LU-617.\n", tgt_name(tgt),
1295 *transno_p, tti->tti_transno,
1296 req_is_replay(req));
1297 if (req_is_replay(req)) {
1298 spin_lock(&req->rq_export->exp_lock);
1299 req->rq_export->exp_vbr_failed = 1;
1300 spin_unlock(&req->rq_export->exp_lock);
1302 mutex_unlock(&ted->ted_lcd_lock);
1303 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1306 *transno_p = tti->tti_transno;
1311 tti->tti_off = ted->ted_lr_off;
1312 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1314 mutex_unlock(&ted->ted_lcd_lock);
1318 mutex_unlock(&ted->ted_lcd_lock);
1323 * last_rcvd update for echo client simulation.
1324 * It updates last_rcvd client slot and version of object in
1325 * simple way but with all locks to simulate all drawbacks
1327 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1328 struct lu_target *tgt,
1329 struct dt_object *obj,
1331 struct obd_export *exp)
1333 struct tgt_thread_info *tti = tgt_th_info(env);
1334 struct tg_export_data *ted = &exp->exp_target_data;
1339 tti->tti_transno = 0;
1341 spin_lock(&tgt->lut_translock);
1342 if (th->th_result == 0)
1343 tti->tti_transno = ++tgt->lut_last_transno;
1344 spin_unlock(&tgt->lut_translock);
1346 /** VBR: set new versions */
1347 if (th->th_result == 0 && obj != NULL)
1348 dt_version_set(env, obj, tti->tti_transno, th);
1350 /* if can't add callback, do sync write */
1351 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1354 LASSERT(ted->ted_lr_off > 0);
1356 mutex_lock(&ted->ted_lcd_lock);
1357 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1358 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1359 ted->ted_lcd->lcd_last_result = th->th_result;
1361 tti->tti_off = ted->ted_lr_off;
1362 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1363 mutex_unlock(&ted->ted_lcd_lock);
1367 static int tgt_clients_data_init(const struct lu_env *env,
1368 struct lu_target *tgt,
1369 unsigned long last_size)
1371 struct obd_device *obd = tgt->lut_obd;
1372 struct lr_server_data *lsd = &tgt->lut_lsd;
1373 struct lsd_client_data *lcd = NULL;
1374 struct tg_export_data *ted;
1377 loff_t off = lsd->lsd_client_start;
1378 __u32 generation = 0;
1379 struct cfs_hash *hash = NULL;
1383 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1384 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1390 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1392 GOTO(err_out, rc = -ENODEV);
1394 for (cl_idx = 0; off < last_size; cl_idx++) {
1395 struct obd_export *exp;
1398 /* Don't assume off is incremented properly by
1399 * read_record(), in case sizeof(*lcd)
1400 * isn't the same as fsd->lsd_client_size. */
1401 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1402 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1404 CERROR("%s: error reading last_rcvd %s idx %d off "
1405 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1408 break; /* read error shouldn't cause startup to fail */
1411 if (lcd->lcd_uuid[0] == '\0') {
1412 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1417 last_transno = lcd_last_transno(lcd);
1419 /* These exports are cleaned up by disconnect, so they
1420 * need to be set up like real exports as connect does.
1422 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
1423 " srv lr: "LPU64" lx: "LPU64" gen %u\n", lcd->lcd_uuid,
1424 cl_idx, last_transno, lsd->lsd_last_transno,
1425 lcd_last_xid(lcd), lcd->lcd_generation);
1427 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1429 if (PTR_ERR(exp) == -EALREADY) {
1430 /* export already exists, zero out this one */
1431 CERROR("%s: Duplicate export %s!\n",
1432 tgt_name(tgt), lcd->lcd_uuid);
1435 GOTO(err_out, rc = PTR_ERR(exp));
1438 ted = &exp->exp_target_data;
1439 *ted->ted_lcd = *lcd;
1441 rc = tgt_client_add(env, exp, cl_idx);
1442 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1443 /* VBR: set export last committed version */
1444 exp->exp_last_committed = last_transno;
1445 spin_lock(&exp->exp_lock);
1446 exp->exp_connecting = 0;
1447 exp->exp_in_recovery = 0;
1448 spin_unlock(&exp->exp_lock);
1449 obd->obd_max_recoverable_clients++;
1451 if (tgt->lut_lsd.lsd_feature_incompat &
1452 OBD_INCOMPAT_MULTI_RPCS &&
1453 lcd->lcd_generation != 0) {
1454 /* compute the highest valid client generation */
1455 generation = max(generation, lcd->lcd_generation);
1456 /* fill client_generation <-> export hash table */
1457 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1458 &exp->exp_gen_hash);
1460 CERROR("%s: duplicate export for client "
1462 tgt_name(tgt), lcd->lcd_generation);
1463 class_export_put(exp);
1468 class_export_put(exp);
1470 rc = rev_import_init(exp);
1472 class_unlink_export(exp);
1476 /* Need to check last_rcvd even for duplicated exports. */
1477 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
1478 cl_idx, last_transno);
1480 spin_lock(&tgt->lut_translock);
1481 tgt->lut_last_transno = max(last_transno,
1482 tgt->lut_last_transno);
1483 spin_unlock(&tgt->lut_translock);
1486 /* record highest valid client generation */
1487 atomic_set(&tgt->lut_client_generation, generation);
1491 cfs_hash_putref(hash);
1496 struct server_compat_data {
1503 static struct server_compat_data tgt_scd[] = {
1504 [LDD_F_SV_TYPE_MDT] = {
1505 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1506 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1507 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1508 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1509 OBD_INCOMPAT_MULTI_RPCS,
1510 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1511 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1512 OBD_INCOMPAT_MULTI_OI,
1514 [LDD_F_SV_TYPE_OST] = {
1515 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1516 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1518 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1519 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1523 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1525 struct tgt_thread_info *tti = tgt_th_info(env);
1526 struct lr_server_data *lsd = &tgt->lut_lsd;
1527 unsigned long last_rcvd_size;
1531 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1535 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1537 /* ensure padding in the struct is the correct size */
1538 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1539 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1541 rc = server_name2index(tgt_name(tgt), &index, NULL);
1543 CERROR("%s: Can not get index from name: rc = %d\n",
1547 /* server_name2index() returns type */
1549 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1550 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1554 /* last_rcvd on OST doesn't provide reconstruct support because there
1555 * may be up to 8 in-flight write requests per single slot in
1556 * last_rcvd client data
1558 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1560 if (last_rcvd_size == 0) {
1561 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1563 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1564 sizeof(lsd->lsd_uuid));
1565 lsd->lsd_last_transno = 0;
1566 lsd->lsd_mount_count = 0;
1567 lsd->lsd_server_size = LR_SERVER_SIZE;
1568 lsd->lsd_client_start = LR_CLIENT_START;
1569 lsd->lsd_client_size = LR_CLIENT_SIZE;
1570 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1571 lsd->lsd_osd_index = index;
1572 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1573 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1575 rc = tgt_server_data_read(env, tgt);
1577 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1581 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1582 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1583 "using the wrong disk %s. Were the"
1584 " /dev/ assignments rearranged?\n",
1585 tgt->lut_obd->obd_uuid.uuid,
1590 if (lsd->lsd_osd_index != index) {
1591 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1592 "is different with the index %d in"
1593 "config log, It might be disk"
1594 "corruption!\n", tgt_name(tgt),
1595 lsd->lsd_osd_index, index);
1600 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1601 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1603 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1607 if (type == LDD_F_SV_TYPE_MDT)
1608 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1610 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1611 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1613 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1616 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1617 if (type == LDD_F_SV_TYPE_MDT &&
1618 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1619 if (last_rcvd_size > lsd->lsd_client_start) {
1620 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1621 "remove all clients for interop needs\n",
1623 rc = tgt_truncate_last_rcvd(env, tgt,
1624 lsd->lsd_client_start);
1627 last_rcvd_size = lsd->lsd_client_start;
1629 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1630 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1633 spin_lock(&tgt->lut_translock);
1634 tgt->lut_last_transno = lsd->lsd_last_transno;
1635 spin_unlock(&tgt->lut_translock);
1637 lsd->lsd_mount_count++;
1639 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1640 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
1641 tgt_name(tgt), tgt->lut_last_transno);
1642 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
1643 tgt_name(tgt), lsd->lsd_mount_count);
1644 CDEBUG(D_INODE, "%s: server data size: %u\n",
1645 tgt_name(tgt), lsd->lsd_server_size);
1646 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1647 tgt_name(tgt), lsd->lsd_client_start);
1648 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1649 tgt_name(tgt), lsd->lsd_client_size);
1650 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1651 tgt_name(tgt), last_rcvd_size);
1652 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1653 tgt_name(tgt), lsd->lsd_subdir_count);
1654 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1655 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1656 (last_rcvd_size - lsd->lsd_client_start) /
1657 lsd->lsd_client_size);
1658 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1660 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1661 lsd->lsd_client_size == 0) {
1662 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1666 if (!tgt->lut_obd->obd_replayable)
1667 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1669 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1671 GOTO(err_client, rc);
1673 spin_lock(&tgt->lut_translock);
1674 /* obd_last_committed is used for compatibility
1675 * with other lustre recovery code */
1676 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1677 spin_unlock(&tgt->lut_translock);
1679 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1680 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1682 /* save it, so mount count and last_transno is current */
1683 rc = tgt_server_data_update(env, tgt, 0);
1685 GOTO(err_client, rc);
1690 class_disconnect_exports(tgt->lut_obd);
1694 /* add credits for last_rcvd update */
1695 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1698 struct lu_target *tgt = cookie;
1699 struct tgt_session_info *tsi;
1700 struct tgt_thread_info *tti = tgt_th_info(env);
1701 struct dt_object *dto;
1704 /* if there is no session, then this transaction is not result of
1705 * request processing but some local operation */
1706 if (env->le_ses == NULL)
1709 LASSERT(tgt->lut_last_rcvd);
1710 tsi = tgt_ses_info(env);
1711 /* OFD may start transaction without export assigned */
1712 if (tsi->tsi_exp == NULL)
1715 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1718 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1719 tsi->tsi_exp->exp_target_data.ted_lr_off,
1725 rc = dt_declare_record_write(env, dto, &tti->tti_buf, 0, th);
1729 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1730 tti->tti_off = atomic_read(&tgt->lut_num_clients) * 8
1731 * sizeof(struct lsd_reply_data);
1732 tti->tti_buf.lb_buf = NULL;
1733 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1734 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1735 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1741 if (tsi->tsi_vbr_obj != NULL &&
1742 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1743 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1744 rc = dt_declare_version_set(env, dto, th);
1750 /* Update last_rcvd records with latests transaction data */
1751 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1754 struct lu_target *tgt = cookie;
1755 struct tgt_session_info *tsi;
1756 struct tgt_thread_info *tti = tgt_th_info(env);
1757 struct dt_object *obj = NULL;
1761 if (env->le_ses == NULL)
1764 tsi = tgt_ses_info(env);
1765 /* OFD may start transaction without export assigned */
1766 if (tsi->tsi_exp == NULL)
1769 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1771 if (tti->tti_has_trans && !echo_client) {
1772 if (tti->tti_mult_trans == 0) {
1773 CDEBUG(D_HA, "More than one transaction "LPU64"\n",
1777 /* we need another transno to be assigned */
1778 tti->tti_transno = 0;
1779 } else if (th->th_result == 0) {
1780 tti->tti_has_trans = 1;
1783 if (tsi->tsi_vbr_obj != NULL &&
1784 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1785 obj = tsi->tsi_vbr_obj;
1788 if (unlikely(echo_client)) /* echo client special case */
1789 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1792 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1797 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1799 struct tgt_thread_info *tti = tgt_th_info(env);
1800 struct lsd_reply_data *lrd = &tti->tti_lrd;
1801 unsigned long reply_data_size;
1803 struct lsd_reply_header *lrh = NULL;
1804 struct lsd_client_data *lcd = NULL;
1805 struct tg_reply_data *trd = NULL;
1808 struct cfs_hash *hash = NULL;
1809 struct obd_export *exp;
1810 struct tg_export_data *ted;
1811 int reply_data_recovered = 0;
1813 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1816 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1820 GOTO(out, rc = -ENOMEM);
1822 if (reply_data_size == 0) {
1823 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1825 lrh->lrh_magic = LRH_MAGIC;
1826 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1827 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1828 rc = tgt_reply_header_write(env, tgt, lrh);
1830 CERROR("%s: error writing %s: rc = %d\n",
1831 tgt_name(tgt), REPLY_DATA, rc);
1835 rc = tgt_reply_header_read(env, tgt, lrh);
1837 CERROR("%s: error reading %s: rc = %d\n",
1838 tgt_name(tgt), REPLY_DATA, rc);
1841 if (lrh->lrh_magic != LRH_MAGIC ||
1842 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1843 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1844 CERROR("%s: invalid header in %s\n",
1845 tgt_name(tgt), REPLY_DATA);
1846 GOTO(out, rc = -EINVAL);
1849 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1851 GOTO(out, rc = -ENODEV);
1855 GOTO(out, rc = -ENOMEM);
1859 GOTO(out, rc = -ENOMEM);
1861 /* Load reply_data from disk */
1862 for (idx = 0, off = sizeof(struct lsd_reply_header);
1863 off < reply_data_size;
1864 idx++, off += sizeof(struct lsd_reply_data)) {
1865 rc = tgt_reply_data_read(env, tgt, lrd, off);
1867 CERROR("%s: error reading %s: rc = %d\n",
1868 tgt_name(tgt), REPLY_DATA, rc);
1872 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1874 /* old reply data from a disconnected client */
1877 ted = &exp->exp_target_data;
1878 mutex_lock(&ted->ted_lcd_lock);
1880 /* create in-memory reply_data and link it to
1881 * target export's reply list */
1882 tgt_set_reply_slot(tgt, idx);
1883 trd->trd_reply = *lrd;
1884 trd->trd_pre_versions[0] = 0;
1885 trd->trd_pre_versions[1] = 0;
1886 trd->trd_pre_versions[2] = 0;
1887 trd->trd_pre_versions[3] = 0;
1888 trd->trd_index = idx;
1890 list_add(&trd->trd_list, &ted->ted_reply_list);
1891 ted->ted_reply_cnt++;
1892 if (ted->ted_reply_cnt > ted->ted_reply_max)
1893 ted->ted_reply_max = ted->ted_reply_cnt;
1895 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1896 "transno %llu, client gen %u, slot idx %d\n",
1897 tgt_name(tgt), trd, lrd->lrd_xid,
1898 lrd->lrd_transno, lrd->lrd_client_gen,
1901 /* update export last committed transation */
1902 exp->exp_last_committed = max(exp->exp_last_committed,
1905 mutex_unlock(&ted->ted_lcd_lock);
1906 class_export_put(exp);
1908 /* update target last committed transaction */
1909 spin_lock(&tgt->lut_translock);
1910 tgt->lut_last_transno = max(tgt->lut_last_transno,
1912 spin_unlock(&tgt->lut_translock);
1914 reply_data_recovered++;
1918 GOTO(out, rc = -ENOMEM);
1920 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1921 tgt_name(tgt), reply_data_recovered);
1924 spin_lock(&tgt->lut_translock);
1925 /* obd_last_committed is used for compatibility
1926 * with other lustre recovery code */
1927 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1928 spin_unlock(&tgt->lut_translock);
1934 cfs_hash_putref(hash);
1944 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
1947 struct tg_reply_data *found = NULL;
1948 struct tg_reply_data *reply;
1950 mutex_lock(&ted->ted_lcd_lock);
1951 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
1952 if (reply->trd_reply.lrd_xid == xid) {
1957 mutex_unlock(&ted->ted_lcd_lock);
1960 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
1962 /* Look for a reply data matching specified request @req
1963 * A copy is returned in @trd if the pointer is not NULL
1965 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
1967 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1968 struct tg_reply_data *reply;
1971 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
1972 if (reply != NULL) {
1978 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
1979 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
1984 EXPORT_SYMBOL(tgt_lookup_reply);
1986 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
1988 struct tg_export_data *ted = &exp->exp_target_data;
1989 struct lu_target *lut = class_exp2tgt(exp);
1990 struct tg_reply_data *trd, *tmp;
1992 mutex_lock(&ted->ted_lcd_lock);
1993 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
1994 if (trd->trd_reply.lrd_xid > rcvd_xid)
1996 ted->ted_release_xid++;
1997 tgt_release_reply_data(lut, ted, trd);
1999 mutex_unlock(&ted->ted_lcd_lock);
2004 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2006 struct tg_export_data *ted = &exp->exp_target_data;
2007 struct lu_target *lut = class_exp2tgt(exp);
2008 struct tg_reply_data *trd, *tmp;
2013 mutex_lock(&ted->ted_lcd_lock);
2014 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2015 if (trd->trd_tag != tag)
2017 ted->ted_release_tag++;
2018 tgt_release_reply_data(lut, ted, trd);
2021 mutex_unlock(&ted->ted_lcd_lock);