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, 2012, 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"
47 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
49 tti->tti_buf.lb_buf = &tti->tti_lsd;
50 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
54 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
56 tti->tti_buf.lb_buf = &tti->tti_lcd;
57 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
62 * Allocate in-memory data for client slot related to export.
64 int tgt_client_alloc(struct obd_export *exp)
67 LASSERT(exp != exp->exp_obd->obd_self_export);
69 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
70 if (exp->exp_target_data.ted_lcd == NULL)
72 /* Mark that slot is not yet valid, 0 doesn't work here */
73 exp->exp_target_data.ted_lr_idx = -1;
76 EXPORT_SYMBOL(tgt_client_alloc);
79 * Free in-memory data for client slot related to export.
81 void tgt_client_free(struct obd_export *exp)
83 struct tg_export_data *ted = &exp->exp_target_data;
84 struct lu_target *lut = class_exp2tgt(exp);
86 LASSERT(exp != exp->exp_obd->obd_self_export);
88 OBD_FREE_PTR(ted->ted_lcd);
91 /* Slot may be not yet assigned */
92 if (ted->ted_lr_idx < 0)
94 /* Clear bit when lcd is freed */
95 LASSERT(lut->lut_client_bitmap);
96 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
97 CERROR("%s: client %u bit already clear in bitmap\n",
98 exp->exp_obd->obd_name, ted->ted_lr_idx);
102 EXPORT_SYMBOL(tgt_client_free);
104 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
105 struct lsd_client_data *lcd, loff_t *off, int index)
107 struct tgt_thread_info *tti = tgt_th_info(env);
111 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
113 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
114 lcd_le_to_cpu(&tti->tti_lcd, lcd);
115 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
116 lcd->lcd_last_close_result =
117 ptlrpc_status_ntoh(lcd->lcd_last_close_result);
120 CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = "LPU64
121 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
122 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
123 "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
124 *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
125 lcd->lcd_last_result, lcd->lcd_last_data,
126 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
127 lcd->lcd_last_close_result, rc);
130 EXPORT_SYMBOL(tgt_client_data_read);
132 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
133 struct lsd_client_data *lcd, loff_t *off,
136 struct tgt_thread_info *tti = tgt_th_info(env);
138 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
139 lcd->lcd_last_close_result =
140 ptlrpc_status_hton(lcd->lcd_last_close_result);
141 lcd_cpu_to_le(lcd, &tti->tti_lcd);
144 return dt_record_write(env, tgt->lut_last_rcvd, &tti->tti_buf, off, th);
146 EXPORT_SYMBOL(tgt_client_data_write);
149 * Update client data in last_rcvd
151 int tgt_client_data_update(const struct lu_env *env, struct obd_export *exp)
153 struct tg_export_data *ted = &exp->exp_target_data;
154 struct lu_target *tgt = class_exp2tgt(exp);
155 struct tgt_thread_info *tti = tgt_th_info(env);
161 th = dt_trans_create(env, tgt->lut_bottom);
165 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
166 sizeof(struct lsd_client_data),
167 ted->ted_lr_off, th);
171 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
175 * Until this operations will be committed the sync is needed
176 * for this export. This should be done _after_ starting the
177 * transaction so that many connecting clients will not bring
178 * server down with lots of sync writes.
180 rc = tgt_new_client_cb_add(th, exp);
182 /* can't add callback, do sync now */
185 spin_lock(&exp->exp_lock);
186 exp->exp_need_sync = 1;
187 spin_unlock(&exp->exp_lock);
190 tti->tti_off = ted->ted_lr_off;
191 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
194 dt_trans_stop(env, tgt->lut_bottom, th);
195 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
196 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
197 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
202 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
204 struct tgt_thread_info *tti = tgt_th_info(env);
209 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
212 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
214 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
215 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
216 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
219 EXPORT_SYMBOL(tgt_server_data_read);
221 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
224 struct tgt_thread_info *tti = tgt_th_info(env);
231 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
233 rc = dt_record_write(env, tgt->lut_last_rcvd, &tti->tti_buf,
236 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
237 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
238 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
242 EXPORT_SYMBOL(tgt_server_data_write);
245 * Update server data in last_rcvd
247 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
250 struct tgt_thread_info *tti = tgt_th_info(env);
257 "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
258 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
259 tgt->lut_last_transno);
261 /* Always save latest transno to keep it fresh */
262 spin_lock(&tgt->lut_translock);
263 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
264 spin_unlock(&tgt->lut_translock);
266 th = dt_trans_create(env, tgt->lut_bottom);
272 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
273 sizeof(struct lr_server_data),
278 rc = dt_trans_start(env, tgt->lut_bottom, th);
282 rc = tgt_server_data_write(env, tgt, th);
284 dt_trans_stop(env, tgt->lut_bottom, th);
286 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
287 "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
288 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
291 EXPORT_SYMBOL(tgt_server_data_update);
293 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
296 struct dt_object *dt = tgt->lut_last_rcvd;
304 attr.la_valid = LA_SIZE;
306 th = dt_trans_create(env, tgt->lut_bottom);
309 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
312 rc = dt_declare_attr_set(env, dt, &attr, th);
315 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
319 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th, BYPASS_CAPA);
321 rc = dt_attr_set(env, dt, &attr, th, BYPASS_CAPA);
324 dt_trans_stop(env, tgt->lut_bottom, th);
328 EXPORT_SYMBOL(tgt_truncate_last_rcvd);
330 void tgt_client_epoch_update(const struct lu_env *env, struct obd_export *exp)
332 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
333 struct lu_target *tgt = class_exp2tgt(exp);
335 LASSERT(tgt->lut_bottom);
336 /** VBR: set client last_epoch to current epoch */
337 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
339 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
340 tgt_client_data_update(env, exp);
344 * Update boot epoch when recovery ends
346 void tgt_boot_epoch_update(struct lu_target *tgt)
349 struct ptlrpc_request *req;
351 cfs_list_t client_list;
354 if (tgt->lut_obd->obd_stopping)
357 rc = lu_env_init(&env, LCT_DT_THREAD);
359 CERROR("%s: can't initialize environment: rc = %d\n",
360 tgt->lut_obd->obd_name, rc);
364 spin_lock(&tgt->lut_translock);
365 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
366 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
367 tgt->lut_lsd.lsd_start_epoch = start_epoch;
368 spin_unlock(&tgt->lut_translock);
370 CFS_INIT_LIST_HEAD(&client_list);
372 * The recovery is not yet finished and final queue can still be updated
373 * with resend requests. Move final list to separate one for processing
375 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
376 cfs_list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
377 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
380 * go through list of exports participated in recovery and
381 * set new epoch for them
383 cfs_list_for_each_entry(req, &client_list, rq_list) {
384 LASSERT(!req->rq_export->exp_delayed);
385 if (!req->rq_export->exp_vbr_failed)
386 tgt_client_epoch_update(&env, req->rq_export);
388 /** return list back at once */
389 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
390 cfs_list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
391 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
392 /** update server epoch */
393 tgt_server_data_update(&env, tgt, 1);
396 EXPORT_SYMBOL(tgt_boot_epoch_update);
399 * commit callback, need to update last_commited value
401 struct tgt_last_committed_callback {
402 struct dt_txn_commit_cb llcc_cb;
403 struct lu_target *llcc_tgt;
404 struct obd_export *llcc_exp;
408 void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
409 struct dt_txn_commit_cb *cb, int err)
411 struct tgt_last_committed_callback *ccb;
413 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
415 LASSERT(ccb->llcc_tgt != NULL);
416 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
418 spin_lock(&ccb->llcc_tgt->lut_translock);
419 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
420 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
422 LASSERT(ccb->llcc_exp);
423 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
424 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
425 spin_unlock(&ccb->llcc_tgt->lut_translock);
426 ptlrpc_commit_replies(ccb->llcc_exp);
428 spin_unlock(&ccb->llcc_tgt->lut_translock);
430 class_export_cb_put(ccb->llcc_exp);
431 if (ccb->llcc_transno)
432 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
433 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
437 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
438 struct obd_export *exp, __u64 transno)
440 struct tgt_last_committed_callback *ccb;
441 struct dt_txn_commit_cb *dcb;
449 ccb->llcc_exp = class_export_cb_get(exp);
450 ccb->llcc_transno = transno;
453 dcb->dcb_func = tgt_cb_last_committed;
454 CFS_INIT_LIST_HEAD(&dcb->dcb_linkage);
455 strncpy(dcb->dcb_name, "tgt_cb_last_committed", MAX_COMMIT_CB_STR_LEN);
456 dcb->dcb_name[MAX_COMMIT_CB_STR_LEN - 1] = '\0';
458 rc = dt_trans_cb_add(th, dcb);
460 class_export_cb_put(exp);
464 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
465 /* report failure to force synchronous operation */
470 EXPORT_SYMBOL(tgt_last_commit_cb_add);
472 struct tgt_new_client_callback {
473 struct dt_txn_commit_cb lncc_cb;
474 struct obd_export *lncc_exp;
477 void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
478 struct dt_txn_commit_cb *cb, int err)
480 struct tgt_new_client_callback *ccb;
482 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
484 LASSERT(ccb->lncc_exp->exp_obd);
486 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
487 ccb->lncc_exp->exp_obd->obd_name,
488 ccb->lncc_exp->exp_client_uuid.uuid);
490 spin_lock(&ccb->lncc_exp->exp_lock);
491 ccb->lncc_exp->exp_need_sync = 0;
492 spin_unlock(&ccb->lncc_exp->exp_lock);
493 class_export_cb_put(ccb->lncc_exp);
498 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
500 struct tgt_new_client_callback *ccb;
501 struct dt_txn_commit_cb *dcb;
508 ccb->lncc_exp = class_export_cb_get(exp);
511 dcb->dcb_func = tgt_cb_new_client;
512 CFS_INIT_LIST_HEAD(&dcb->dcb_linkage);
513 strncpy(dcb->dcb_name, "tgt_cb_new_client", MAX_COMMIT_CB_STR_LEN);
514 dcb->dcb_name[MAX_COMMIT_CB_STR_LEN - 1] = '\0';
516 rc = dt_trans_cb_add(th, dcb);
518 class_export_cb_put(exp);
525 * Add new client to the last_rcvd upon new connection.
527 * We use a bitmap to locate a free space in the last_rcvd file and initialize
530 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
532 struct tg_export_data *ted = &exp->exp_target_data;
533 struct lu_target *tgt = class_exp2tgt(exp);
538 LASSERT(tgt->lut_client_bitmap != NULL);
539 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
542 mutex_init(&ted->ted_lcd_lock);
544 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
547 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
548 * there's no need for extra complication here
550 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
552 if (idx >= LR_MAX_CLIENTS ||
553 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
554 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
555 tgt->lut_obd->obd_name, idx);
558 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
559 idx = find_next_zero_bit(tgt->lut_client_bitmap,
560 LR_MAX_CLIENTS, idx);
564 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added\n",
565 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid);
567 ted->ted_lr_idx = idx;
568 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
569 idx * tgt->lut_lsd.lsd_client_size;
571 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
573 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s'\n",
574 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
575 ted->ted_lcd->lcd_uuid);
577 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
580 rc = tgt_client_data_update(env, exp);
582 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
583 tgt->lut_obd->obd_name, idx, rc);
587 EXPORT_SYMBOL(tgt_client_new);
589 /* Add client data to the MDS. We use a bitmap to locate a free space
590 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
591 * Otherwise, we just have to read the data from the last_rcvd file and
592 * we know its offset.
594 * It should not be possible to fail adding an existing client - otherwise
595 * mdt_init_server_data() callsite needs to be fixed.
597 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
599 struct tg_export_data *ted = &exp->exp_target_data;
600 struct lu_target *tgt = class_exp2tgt(exp);
604 LASSERT(tgt->lut_client_bitmap != NULL);
605 LASSERTF(idx >= 0, "%d\n", idx);
607 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
608 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
611 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
612 CERROR("%s: client %d: bit already set in bitmap!!\n",
613 tgt->lut_obd->obd_name, idx);
617 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added\n",
618 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid);
620 ted->ted_lr_idx = idx;
621 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
622 idx * tgt->lut_lsd.lsd_client_size;
624 mutex_init(&ted->ted_lcd_lock);
626 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
630 EXPORT_SYMBOL(tgt_client_add);
632 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
634 struct tg_export_data *ted = &exp->exp_target_data;
635 struct lu_target *tgt = class_exp2tgt(exp);
640 LASSERT(ted->ted_lcd);
642 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
643 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
644 (char *)tgt->lut_obd->obd_uuid.uuid) ||
645 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
648 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
649 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
650 ted->ted_lcd->lcd_uuid);
652 /* Clear the bit _after_ zeroing out the client so we don't
653 race with filter_client_add and zero out new clients.*/
654 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
655 CERROR("%s: client %u: bit already clear in bitmap!!\n",
656 tgt->lut_obd->obd_name, ted->ted_lr_idx);
660 /* Do not erase record for recoverable client. */
661 if (exp->exp_flags & OBD_OPT_FAILOVER)
664 /* Make sure the server's last_transno is up to date.
665 * This should be done before zeroing client slot so last_transno will
666 * be in server data or in client data in case of failure */
667 rc = tgt_server_data_update(env, tgt, 0);
669 CERROR("%s: failed to update server data, skip client %s "
670 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
671 ted->ted_lcd->lcd_uuid, rc);
675 mutex_lock(&ted->ted_lcd_lock);
676 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
677 rc = tgt_client_data_update(env, exp);
678 mutex_unlock(&ted->ted_lcd_lock);
680 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
681 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
682 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
683 ted->ted_lr_idx, ted->ted_lr_off, rc);
686 EXPORT_SYMBOL(tgt_client_del);
689 * last_rcvd & last_committed update callbacks
691 int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
692 struct dt_object *obj, __u64 opdata,
693 struct thandle *th, struct ptlrpc_request *req)
695 struct tgt_thread_info *tti = tgt_th_info(env);
696 struct tg_export_data *ted;
699 bool lw_client, update = false;
703 ted = &req->rq_export->exp_target_data;
705 lw_client = exp_connect_flags(req->rq_export) & OBD_CONNECT_LIGHTWEIGHT;
706 if (ted->ted_lr_idx < 0 && !lw_client)
707 /* ofd connect may cause transaction before export has
711 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
713 spin_lock(&tgt->lut_translock);
714 if (th->th_result != 0) {
715 if (tti->tti_transno != 0) {
716 CERROR("%s: replay transno "LPU64" failed: rc = %d\n",
717 tgt_name(tgt), tti->tti_transno, th->th_result);
719 } else if (tti->tti_transno == 0) {
720 tti->tti_transno = ++tgt->lut_last_transno;
722 /* should be replay */
723 if (tti->tti_transno > tgt->lut_last_transno)
724 tgt->lut_last_transno = tti->tti_transno;
726 spin_unlock(&tgt->lut_translock);
728 /** VBR: set new versions */
729 if (th->th_result == 0 && obj != NULL)
730 dt_version_set(env, obj, tti->tti_transno, th);
732 /* filling reply data */
733 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
734 tti->tti_transno, tgt->lut_obd->obd_last_committed);
736 req->rq_transno = tti->tti_transno;
737 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
739 /* if can't add callback, do sync write */
740 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, req->rq_export,
744 /* All operations performed by LW clients are synchronous and
745 * we store the committed transno in the last_rcvd header */
746 spin_lock(&tgt->lut_translock);
747 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
748 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
751 spin_unlock(&tgt->lut_translock);
752 /* Although lightweight (LW) connections have no slot in
753 * last_rcvd, we still want to maintain the in-memory
754 * lsd_client_data structure in order to properly handle reply
756 } else if (ted->ted_lr_off == 0) {
757 CERROR("%s: client idx %d has offset %lld\n",
758 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
762 /* if the export has already been disconnected, we have no last_rcvd
763 * slot, update server data with latest transno then */
764 if (ted->ted_lcd == NULL) {
765 CWARN("commit transaction for disconnected client %s: rc %d\n",
766 req->rq_export->exp_client_uuid.uuid, rc);
767 GOTO(srv_update, rc = 0);
770 mutex_lock(&ted->ted_lcd_lock);
771 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
772 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
773 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
774 transno_p = &ted->ted_lcd->lcd_last_close_transno;
775 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
776 ted->ted_lcd->lcd_last_close_result = th->th_result;
778 /* VBR: save versions in last_rcvd for reconstruct. */
779 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
782 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
783 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
784 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
785 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
787 transno_p = &ted->ted_lcd->lcd_last_transno;
788 ted->ted_lcd->lcd_last_xid = req->rq_xid;
789 ted->ted_lcd->lcd_last_result = th->th_result;
790 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
791 * see struct ldlm_reply->lock_policy_res1; */
792 ted->ted_lcd->lcd_last_data = opdata;
795 /* Update transno in slot only if non-zero number, i.e. no errors */
796 if (likely(tti->tti_transno != 0)) {
797 if (*transno_p > tti->tti_transno &&
798 !tgt->lut_no_reconstruct) {
799 CERROR("%s: trying to overwrite bigger transno:"
800 "on-disk: "LPU64", new: "LPU64" replay: %d. "
801 "see LU-617.\n", tgt_name(tgt), *transno_p,
802 tti->tti_transno, req_is_replay(req));
803 if (req_is_replay(req)) {
804 spin_lock(&req->rq_export->exp_lock);
805 req->rq_export->exp_vbr_failed = 1;
806 spin_unlock(&req->rq_export->exp_lock);
808 mutex_unlock(&ted->ted_lcd_lock);
809 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
811 *transno_p = tti->tti_transno;
815 tti->tti_off = ted->ted_lr_off;
816 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
818 mutex_unlock(&ted->ted_lcd_lock);
822 mutex_unlock(&ted->ted_lcd_lock);
826 rc = tgt_server_data_write(env, tgt, th);
831 * last_rcvd update for echo client simulation.
832 * It updates last_rcvd client slot and version of object in
833 * simple way but with all locks to simulate all drawbacks
835 int tgt_last_rcvd_update_echo(const struct lu_env *env, struct lu_target *tgt,
836 struct dt_object *obj, struct thandle *th,
837 struct obd_export *exp)
839 struct tgt_thread_info *tti = tgt_th_info(env);
840 struct tg_export_data *ted = &exp->exp_target_data;
845 tti->tti_transno = 0;
847 spin_lock(&tgt->lut_translock);
848 if (th->th_result == 0)
849 tti->tti_transno = ++tgt->lut_last_transno;
850 spin_unlock(&tgt->lut_translock);
852 /** VBR: set new versions */
853 if (th->th_result == 0 && obj != NULL)
854 dt_version_set(env, obj, tti->tti_transno, th);
856 /* if can't add callback, do sync write */
857 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
860 LASSERT(ted->ted_lr_off > 0);
862 mutex_lock(&ted->ted_lcd_lock);
863 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
864 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
865 ted->ted_lcd->lcd_last_result = th->th_result;
867 tti->tti_off = ted->ted_lr_off;
868 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
869 mutex_unlock(&ted->ted_lcd_lock);
873 int tgt_clients_data_init(const struct lu_env *env, struct lu_target *tgt,
874 unsigned long last_size)
876 struct obd_device *obd = tgt->lut_obd;
877 struct lr_server_data *lsd = &tgt->lut_lsd;
878 struct lsd_client_data *lcd = NULL;
879 struct tg_export_data *ted;
882 loff_t off = lsd->lsd_client_start;
886 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
887 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
893 for (cl_idx = 0; off < last_size; cl_idx++) {
894 struct obd_export *exp;
897 /* Don't assume off is incremented properly by
898 * read_record(), in case sizeof(*lcd)
899 * isn't the same as fsd->lsd_client_size. */
900 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
901 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
903 CERROR("%s: error reading last_rcvd %s idx %d off "
904 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
907 break; /* read error shouldn't cause startup to fail */
910 if (lcd->lcd_uuid[0] == '\0') {
911 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
916 last_transno = lcd_last_transno(lcd);
918 /* These exports are cleaned up by disconnect, so they
919 * need to be set up like real exports as connect does.
921 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
922 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
923 last_transno, lsd->lsd_last_transno, lcd_last_xid(lcd));
925 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
927 if (PTR_ERR(exp) == -EALREADY) {
928 /* export already exists, zero out this one */
929 CERROR("%s: Duplicate export %s!\n",
930 tgt_name(tgt), lcd->lcd_uuid);
933 GOTO(err_out, rc = PTR_ERR(exp));
936 ted = &exp->exp_target_data;
937 *ted->ted_lcd = *lcd;
939 rc = tgt_client_add(env, exp, cl_idx);
940 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
941 /* VBR: set export last committed version */
942 exp->exp_last_committed = last_transno;
943 spin_lock(&exp->exp_lock);
944 exp->exp_connecting = 0;
945 exp->exp_in_recovery = 0;
946 spin_unlock(&exp->exp_lock);
947 obd->obd_max_recoverable_clients++;
948 class_export_put(exp);
950 /* Need to check last_rcvd even for duplicated exports. */
951 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
952 cl_idx, last_transno);
954 spin_lock(&tgt->lut_translock);
955 tgt->lut_last_transno = max(last_transno,
956 tgt->lut_last_transno);
957 spin_unlock(&tgt->lut_translock);
965 struct server_compat_data {
972 static struct server_compat_data tgt_scd[] = {
973 [LDD_F_SV_TYPE_MDT] = {
974 .rocompat = OBD_ROCOMPAT_LOVOBJID,
975 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
976 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
977 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI,
978 .rocinit = OBD_ROCOMPAT_LOVOBJID,
979 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
980 OBD_INCOMPAT_MULTI_OI,
982 [LDD_F_SV_TYPE_OST] = {
984 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
987 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
991 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
993 struct tgt_thread_info *tti = tgt_th_info(env);
994 struct lr_server_data *lsd = &tgt->lut_lsd;
995 unsigned long last_rcvd_size;
999 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr, BYPASS_CAPA);
1003 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1005 /* ensure padding in the struct is the correct size */
1006 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1007 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1009 rc = server_name2index(tgt_name(tgt), &index, NULL);
1011 CERROR("%s: Can not get index from name: rc = %d\n",
1015 /* server_name2index() returns type */
1017 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1018 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1022 /* last_rcvd on OST doesn't provide reconstruct support because there
1023 * may be up to 8 in-flight write requests per single slot in
1024 * last_rcvd client data
1026 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1028 if (last_rcvd_size == 0) {
1029 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1031 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1032 sizeof(lsd->lsd_uuid));
1033 lsd->lsd_last_transno = 0;
1034 lsd->lsd_mount_count = 0;
1035 lsd->lsd_server_size = LR_SERVER_SIZE;
1036 lsd->lsd_client_start = LR_CLIENT_START;
1037 lsd->lsd_client_size = LR_CLIENT_SIZE;
1038 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1039 lsd->lsd_osd_index = index;
1040 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1041 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1043 rc = tgt_server_data_read(env, tgt);
1045 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1049 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1050 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1051 "using the wrong disk %s. Were the"
1052 " /dev/ assignments rearranged?\n",
1053 tgt->lut_obd->obd_uuid.uuid,
1058 if (lsd->lsd_osd_index != index) {
1059 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1060 "is different with the index %d in"
1061 "config log, It might be disk"
1062 "corruption!\n", tgt_name(tgt),
1063 lsd->lsd_osd_index, index);
1068 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1069 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1071 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1074 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1075 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1077 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1080 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1081 if (type == LDD_F_SV_TYPE_MDT &&
1082 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1083 if (last_rcvd_size > lsd->lsd_client_start) {
1084 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1085 "remove all clients for interop needs\n",
1087 rc = tgt_truncate_last_rcvd(env, tgt,
1088 lsd->lsd_client_start);
1091 last_rcvd_size = lsd->lsd_client_start;
1093 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1094 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1097 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1099 spin_lock(&tgt->lut_translock);
1100 tgt->lut_last_transno = lsd->lsd_last_transno;
1101 spin_unlock(&tgt->lut_translock);
1103 lsd->lsd_mount_count++;
1105 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1106 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
1107 tgt_name(tgt), tgt->lut_last_transno);
1108 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
1109 tgt_name(tgt), lsd->lsd_mount_count);
1110 CDEBUG(D_INODE, "%s: server data size: %u\n",
1111 tgt_name(tgt), lsd->lsd_server_size);
1112 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1113 tgt_name(tgt), lsd->lsd_client_start);
1114 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1115 tgt_name(tgt), lsd->lsd_client_size);
1116 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1117 tgt_name(tgt), last_rcvd_size);
1118 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1119 tgt_name(tgt), lsd->lsd_subdir_count);
1120 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1121 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1122 (last_rcvd_size - lsd->lsd_client_start) /
1123 lsd->lsd_client_size);
1124 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1126 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1127 lsd->lsd_client_size == 0) {
1128 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1132 if (!tgt->lut_obd->obd_replayable)
1133 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1135 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1137 GOTO(err_client, rc);
1139 spin_lock(&tgt->lut_translock);
1140 /* obd_last_committed is used for compatibility
1141 * with other lustre recovery code */
1142 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1143 spin_unlock(&tgt->lut_translock);
1145 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1146 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1148 /* save it, so mount count and last_transno is current */
1149 rc = tgt_server_data_update(env, tgt, 0);
1151 GOTO(err_client, rc);
1156 class_disconnect_exports(tgt->lut_obd);
1160 /* add credits for last_rcvd update */
1161 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1164 struct lu_target *tgt = cookie;
1165 struct tgt_session_info *tsi;
1168 /* if there is no session, then this transaction is not result of
1169 * request processing but some local operation */
1170 if (env->le_ses == NULL)
1173 LASSERT(tgt->lut_last_rcvd);
1174 tsi = tgt_ses_info(env);
1175 /* OFD may start transaction without export assigned */
1176 if (tsi->tsi_exp == NULL)
1179 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
1180 sizeof(struct lsd_client_data),
1181 tsi->tsi_exp->exp_target_data.ted_lr_off,
1186 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
1187 sizeof(struct lr_server_data), 0, th);
1191 if (tsi->tsi_vbr_obj != NULL &&
1192 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu))
1193 rc = dt_declare_version_set(env, tsi->tsi_vbr_obj, th);
1198 /* Update last_rcvd records with latests transaction data */
1199 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1202 struct lu_target *tgt = cookie;
1203 struct tgt_session_info *tsi;
1204 struct tgt_thread_info *tti = tgt_th_info(env);
1205 struct dt_object *obj = NULL;
1209 if (env->le_ses == NULL)
1212 tsi = tgt_ses_info(env);
1213 /* OFD may start transaction without export assigned */
1214 if (tsi->tsi_exp == NULL)
1217 echo_client = (tgt_ses_req(tsi) == NULL);
1219 if (tti->tti_has_trans && !echo_client) {
1220 if (tti->tti_mult_trans == 0) {
1221 CDEBUG(D_HA, "More than one transaction "LPU64"\n",
1225 /* we need another transno to be assigned */
1226 tti->tti_transno = 0;
1227 } else if (th->th_result == 0) {
1228 tti->tti_has_trans = 1;
1231 if (tsi->tsi_vbr_obj != NULL &&
1232 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1233 obj = tsi->tsi_vbr_obj;
1236 if (unlikely(echo_client)) /* echo client special case */
1237 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1240 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,