4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Lustre Unified Target
33 * These are common function to work with last_received file
35 * Author: Mikhail Pershin <mike.pershin@intel.com>
38 #include <obd_class.h>
39 #include <lustre_fid.h>
41 #include "tgt_internal.h"
44 /* Allocate a bitmap for a chunk of reply data slots */
45 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
49 OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
54 spin_lock(&lut->lut_client_bitmap_lock);
56 if (lut->lut_reply_bitmap[chunk] != NULL) {
57 /* someone else already allocated the bitmap for this chunk */
58 spin_unlock(&lut->lut_client_bitmap_lock);
59 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
64 lut->lut_reply_bitmap[chunk] = bm;
66 spin_unlock(&lut->lut_client_bitmap_lock);
71 /* Look for an available reply data slot in the bitmap
73 * Allocate bitmap chunk when first used
74 * XXX algo could be improved if this routine limits performance
76 static int tgt_find_free_reply_slot(struct lu_target *lut)
83 for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
84 /* allocate the bitmap chunk if necessary */
85 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
86 rc = tgt_bitmap_chunk_alloc(lut, chunk);
90 bmp = lut->lut_reply_bitmap[chunk];
92 /* look for an available slot in this chunk */
94 b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
95 if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
99 if (test_and_set_bit(b, bmp) == 0)
100 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
107 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
109 * Allocate the bitmap chunk if necessary
111 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
117 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
118 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
120 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
121 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
123 /* allocate the bitmap chunk if necessary */
124 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
125 rc = tgt_bitmap_chunk_alloc(lut, chunk);
130 /* mark the slot 'used' in this chunk */
131 if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
132 CERROR("%s: slot %d already set in bitmap\n",
141 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
144 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
149 chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
150 b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
152 LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
153 LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
155 if (lut->lut_reply_bitmap[chunk] == NULL) {
156 CERROR("%s: slot %d not allocated\n",
161 if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
162 CERROR("%s: slot %d already clear in bitmap\n",
171 /* Read header of reply_data file of target @tgt into structure @lrh */
172 static int tgt_reply_header_read(const struct lu_env *env,
173 struct lu_target *tgt,
174 struct lsd_reply_header *lrh)
177 struct lsd_reply_header buf;
178 struct tgt_thread_info *tti = tgt_th_info(env);
181 tti->tti_buf.lb_buf = &buf;
182 tti->tti_buf.lb_len = sizeof(buf);
184 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
189 lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
190 lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
191 lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
193 CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
194 "header_size=%d reply_size=%d\n",
195 tgt->lut_obd->obd_name, REPLY_DATA,
196 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
201 /* Write header into replay_data file of target @tgt from structure @lrh */
202 static int tgt_reply_header_write(const struct lu_env *env,
203 struct lu_target *tgt,
204 struct lsd_reply_header *lrh)
207 struct lsd_reply_header buf;
208 struct tgt_thread_info *tti = tgt_th_info(env);
210 struct dt_object *dto;
212 CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
213 "header_size=%d reply_size=%d\n",
214 tgt->lut_obd->obd_name, REPLY_DATA,
215 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
217 if (tgt->lut_bottom->dd_rdonly)
220 buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
221 buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
222 buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
224 th = dt_trans_create(env, tgt->lut_bottom);
230 tti->tti_buf.lb_buf = &buf;
231 tti->tti_buf.lb_len = sizeof(buf);
233 rc = dt_declare_record_write(env, tgt->lut_reply_data,
234 &tti->tti_buf, tti->tti_off, th);
238 rc = dt_trans_start(env, tgt->lut_bottom, th);
242 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
243 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
245 dt_trans_stop(env, tgt->lut_bottom, th);
249 /* Write the reply data @lrd into reply_data file of target @tgt
252 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
253 struct lsd_reply_data *lrd, loff_t off,
256 struct tgt_thread_info *tti = tgt_th_info(env);
257 struct dt_object *dto;
258 struct lsd_reply_data *buf = &tti->tti_lrd;
260 lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
262 buf->lrd_transno = cpu_to_le64(lrd->lrd_transno);
263 buf->lrd_xid = cpu_to_le64(lrd->lrd_xid);
264 buf->lrd_data = cpu_to_le64(lrd->lrd_data);
265 buf->lrd_result = cpu_to_le32(lrd->lrd_result);
266 buf->lrd_client_gen = cpu_to_le32(lrd->lrd_client_gen);
268 lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
271 tti->tti_buf.lb_buf = buf;
272 tti->tti_buf.lb_len = sizeof(*buf);
274 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
275 return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
278 /* Read the reply data from reply_data file of target @tgt at offset @off
279 * into structure @lrd
281 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
282 struct lsd_reply_data *lrd, loff_t off)
285 struct tgt_thread_info *tti = tgt_th_info(env);
286 struct lsd_reply_data *buf = &tti->tti_lrd;
289 tti->tti_buf.lb_buf = buf;
290 tti->tti_buf.lb_len = sizeof(*buf);
292 rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
297 lrd->lrd_transno = le64_to_cpu(buf->lrd_transno);
298 lrd->lrd_xid = le64_to_cpu(buf->lrd_xid);
299 lrd->lrd_data = le64_to_cpu(buf->lrd_data);
300 lrd->lrd_result = le32_to_cpu(buf->lrd_result);
301 lrd->lrd_client_gen = le32_to_cpu(buf->lrd_client_gen);
307 /* Free the in-memory reply data structure @trd and release
308 * the corresponding slot in the reply_data file of target @lut
309 * Called with ted_lcd_lock held
311 static void tgt_free_reply_data(struct lu_target *lut,
312 struct tg_export_data *ted,
313 struct tg_reply_data *trd)
315 CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
316 "client gen %u, slot idx %d\n",
317 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
318 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
321 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
323 list_del(&trd->trd_list);
324 ted->ted_reply_cnt--;
326 tgt_clear_reply_slot(lut, trd->trd_index);
330 /* Release the reply data @trd from target @lut
331 * The reply data with the highest transno for this export
332 * is retained to ensure correctness of target recovery
333 * Called with ted_lcd_lock held
335 static void tgt_release_reply_data(struct lu_target *lut,
336 struct tg_export_data *ted,
337 struct tg_reply_data *trd)
339 CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
340 "client gen %u, slot idx %d\n",
341 lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
342 trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
345 LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
347 /* Do not free the reply data corresponding to the
348 * highest transno of this export.
349 * This ensures on-disk reply data is kept and
350 * last committed transno can be restored from disk in case
353 if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
354 /* free previous retained reply */
355 if (ted->ted_reply_last != NULL)
356 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
357 /* retain the reply */
358 list_del_init(&trd->trd_list);
359 ted->ted_reply_last = trd;
361 tgt_free_reply_data(lut, ted, trd);
365 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
367 tti->tti_buf.lb_buf = &tti->tti_lsd;
368 tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
369 return &tti->tti_buf;
372 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
374 tti->tti_buf.lb_buf = &tti->tti_lcd;
375 tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
376 return &tti->tti_buf;
380 * Allocate in-memory data for client slot related to export.
382 int tgt_client_alloc(struct obd_export *exp)
385 LASSERT(exp != exp->exp_obd->obd_self_export);
387 spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
388 INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
390 OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
391 if (exp->exp_target_data.ted_lcd == NULL)
393 /* Mark that slot is not yet valid, 0 doesn't work here */
394 exp->exp_target_data.ted_lr_idx = -1;
395 INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
396 mutex_init(&exp->exp_target_data.ted_lcd_lock);
399 EXPORT_SYMBOL(tgt_client_alloc);
402 * Free in-memory data for client slot related to export.
404 void tgt_client_free(struct obd_export *exp)
406 struct tg_export_data *ted = &exp->exp_target_data;
407 struct lu_target *lut = class_exp2tgt(exp);
408 struct tg_reply_data *trd, *tmp;
410 LASSERT(exp != exp->exp_obd->obd_self_export);
412 /* free reply data */
413 mutex_lock(&ted->ted_lcd_lock);
414 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
415 tgt_release_reply_data(lut, ted, trd);
417 if (ted->ted_reply_last != NULL) {
418 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
419 ted->ted_reply_last = NULL;
421 mutex_unlock(&ted->ted_lcd_lock);
423 if (!hlist_unhashed(&exp->exp_gen_hash))
424 cfs_hash_del(exp->exp_obd->obd_gen_hash,
425 &ted->ted_lcd->lcd_generation,
428 OBD_FREE_PTR(ted->ted_lcd);
431 /* Target may have been freed (see LU-7430)
432 * Slot may be not yet assigned */
433 if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
437 /* Clear bit when lcd is freed */
438 LASSERT(lut && lut->lut_client_bitmap);
439 if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
440 CERROR("%s: client %u bit already clear in bitmap\n",
441 exp->exp_obd->obd_name, ted->ted_lr_idx);
445 if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
446 atomic_dec(&lut->lut_num_clients);
448 EXPORT_SYMBOL(tgt_client_free);
450 static int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
451 struct lsd_client_data *lcd,
452 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 = %llu"
468 ", last_xid = %llu, last_result = %u, last_data = %u, "
469 "last_close_transno = %llu, last_close_xid = %llu, "
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 static int tgt_client_data_write(const struct lu_env *env,
479 struct lu_target *tgt,
480 struct lsd_client_data *lcd,
481 loff_t *off, struct thandle *th)
483 struct tgt_thread_info *tti = tgt_th_info(env);
484 struct dt_object *dto;
486 lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
487 lcd->lcd_last_close_result =
488 ptlrpc_status_hton(lcd->lcd_last_close_result);
489 lcd_cpu_to_le(lcd, &tti->tti_lcd);
492 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
493 return dt_record_write(env, dto, &tti->tti_buf, off, th);
496 struct tgt_new_client_callback {
497 struct dt_txn_commit_cb lncc_cb;
498 struct obd_export *lncc_exp;
501 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
502 struct dt_txn_commit_cb *cb, int err)
504 struct tgt_new_client_callback *ccb;
506 ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
508 LASSERT(ccb->lncc_exp->exp_obd);
510 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
511 ccb->lncc_exp->exp_obd->obd_name,
512 ccb->lncc_exp->exp_client_uuid.uuid);
514 spin_lock(&ccb->lncc_exp->exp_lock);
516 ccb->lncc_exp->exp_need_sync = 0;
518 spin_unlock(&ccb->lncc_exp->exp_lock);
519 class_export_cb_put(ccb->lncc_exp);
524 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
526 struct tgt_new_client_callback *ccb;
527 struct dt_txn_commit_cb *dcb;
534 ccb->lncc_exp = class_export_cb_get(exp);
537 dcb->dcb_func = tgt_cb_new_client;
538 INIT_LIST_HEAD(&dcb->dcb_linkage);
539 strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
541 rc = dt_trans_cb_add(th, dcb);
543 class_export_cb_put(exp);
550 * Update client data in last_rcvd
552 static int tgt_client_data_update(const struct lu_env *env,
553 struct obd_export *exp)
555 struct tg_export_data *ted = &exp->exp_target_data;
556 struct lu_target *tgt = class_exp2tgt(exp);
557 struct tgt_thread_info *tti = tgt_th_info(env);
563 if (unlikely(tgt == NULL)) {
564 CDEBUG(D_ERROR, "%s: No target for connected export\n",
565 class_exp2obd(exp)->obd_name);
569 if (tgt->lut_bottom->dd_rdonly)
572 th = dt_trans_create(env, tgt->lut_bottom);
577 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
579 ted->ted_lr_off, th);
583 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
587 mutex_lock(&ted->ted_lcd_lock);
590 * Until this operations will be committed the sync is needed
591 * for this export. This should be done _after_ starting the
592 * transaction so that many connecting clients will not bring
593 * server down with lots of sync writes.
595 rc = tgt_new_client_cb_add(th, exp);
597 /* can't add callback, do sync now */
600 spin_lock(&exp->exp_lock);
601 exp->exp_need_sync = 1;
602 spin_unlock(&exp->exp_lock);
605 tti->tti_off = ted->ted_lr_off;
606 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
608 mutex_unlock(&ted->ted_lcd_lock);
612 dt_trans_stop(env, tgt->lut_bottom, th);
613 CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
614 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
615 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
620 static int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
622 struct tgt_thread_info *tti = tgt_th_info(env);
627 rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
630 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
632 CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
633 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
634 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
638 static int tgt_server_data_write(const struct lu_env *env,
639 struct lu_target *tgt, struct thandle *th)
641 struct tgt_thread_info *tti = tgt_th_info(env);
642 struct dt_object *dto;
649 lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
651 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
652 rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
654 CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
655 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
656 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
662 * Update server data in last_rcvd
664 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
667 struct tgt_thread_info *tti = tgt_th_info(env);
674 "%s: mount_count is %llu, last_transno is %llu\n",
675 tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
676 tgt->lut_last_transno);
678 /* Always save latest transno to keep it fresh */
679 spin_lock(&tgt->lut_translock);
680 tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
681 spin_unlock(&tgt->lut_translock);
683 if (tgt->lut_bottom->dd_rdonly)
686 th = dt_trans_create(env, tgt->lut_bottom);
693 rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
694 &tti->tti_buf, tti->tti_off, th);
698 rc = dt_trans_start(env, tgt->lut_bottom, th);
702 rc = tgt_server_data_write(env, tgt, th);
704 dt_trans_stop(env, tgt->lut_bottom, th);
706 CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
707 "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
708 tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
711 EXPORT_SYMBOL(tgt_server_data_update);
713 static int tgt_truncate_last_rcvd(const struct lu_env *env,
714 struct lu_target *tgt, loff_t size)
716 struct dt_object *dt = tgt->lut_last_rcvd;
723 if (tgt->lut_bottom->dd_rdonly)
727 attr.la_valid = LA_SIZE;
729 th = dt_trans_create(env, tgt->lut_bottom);
732 rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
735 rc = dt_declare_attr_set(env, dt, &attr, th);
738 rc = dt_trans_start_local(env, tgt->lut_bottom, th);
742 rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
744 rc = dt_attr_set(env, dt, &attr, th);
747 dt_trans_stop(env, tgt->lut_bottom, th);
752 static void tgt_client_epoch_update(const struct lu_env *env,
753 struct obd_export *exp)
755 struct lsd_client_data *lcd = exp->exp_target_data.ted_lcd;
756 struct lu_target *tgt = class_exp2tgt(exp);
758 LASSERT(tgt && tgt->lut_bottom);
759 /** VBR: set client last_epoch to current epoch */
760 if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
762 lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
763 tgt_client_data_update(env, exp);
767 * Update boot epoch when recovery ends
769 void tgt_boot_epoch_update(struct lu_target *tgt)
772 struct ptlrpc_request *req;
774 struct list_head client_list;
777 if (tgt->lut_obd->obd_stopping)
780 rc = lu_env_init(&env, LCT_DT_THREAD);
782 CERROR("%s: can't initialize environment: rc = %d\n",
783 tgt->lut_obd->obd_name, rc);
787 spin_lock(&tgt->lut_translock);
788 start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
789 tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
790 tgt->lut_lsd.lsd_start_epoch = start_epoch;
791 spin_unlock(&tgt->lut_translock);
793 INIT_LIST_HEAD(&client_list);
795 * The recovery is not yet finished and final queue can still be updated
796 * with resend requests. Move final list to separate one for processing
798 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
799 list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
800 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
803 * go through list of exports participated in recovery and
804 * set new epoch for them
806 list_for_each_entry(req, &client_list, rq_list) {
807 LASSERT(!req->rq_export->exp_delayed);
808 if (!req->rq_export->exp_vbr_failed)
809 tgt_client_epoch_update(&env, req->rq_export);
811 /** return list back at once */
812 spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
813 list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
814 spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
816 /** Clear MULTI RPCS incompatibility flag if
817 * - target is MDT and
818 * - there is no client to recover or the recovery was aborted
820 if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
821 (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
822 tgt->lut_obd->obd_abort_recovery))
823 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
825 /** update server epoch */
826 tgt_server_data_update(&env, tgt, 1);
831 * commit callback, need to update last_committed value
833 struct tgt_last_committed_callback {
834 struct dt_txn_commit_cb llcc_cb;
835 struct lu_target *llcc_tgt;
836 struct obd_export *llcc_exp;
840 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
841 struct dt_txn_commit_cb *cb, int err)
843 struct tgt_last_committed_callback *ccb;
845 ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
847 LASSERT(ccb->llcc_exp);
848 LASSERT(ccb->llcc_tgt != NULL);
849 LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
851 /* error hit, don't update last committed to provide chance to
852 * replay data after fail */
856 /* Fast path w/o spinlock, if exp_last_committed was updated
857 * with higher transno, no need to take spinlock and check,
858 * also no need to update obd_last_committed. */
859 if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
861 spin_lock(&ccb->llcc_tgt->lut_translock);
862 if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
863 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
865 if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
866 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
867 spin_unlock(&ccb->llcc_tgt->lut_translock);
869 ptlrpc_commit_replies(ccb->llcc_exp);
870 tgt_cancel_slc_locks(ccb->llcc_tgt, ccb->llcc_transno);
872 spin_unlock(&ccb->llcc_tgt->lut_translock);
875 CDEBUG(D_HA, "%s: transno %lld is committed\n",
876 ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
879 class_export_cb_put(ccb->llcc_exp);
884 * Add commit callback function, it returns a non-zero value to inform
885 * caller to use sync transaction if necessary.
887 static int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
888 struct obd_export *exp, __u64 transno)
890 struct tgt_last_committed_callback *ccb;
891 struct dt_txn_commit_cb *dcb;
899 ccb->llcc_exp = class_export_cb_get(exp);
900 ccb->llcc_transno = transno;
903 dcb->dcb_func = tgt_cb_last_committed;
904 INIT_LIST_HEAD(&dcb->dcb_linkage);
905 strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
907 rc = dt_trans_cb_add(th, dcb);
909 class_export_cb_put(exp);
913 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
914 /* report failure to force synchronous operation */
917 /* if exp_need_sync is set, return non-zero value to force
918 * a sync transaction. */
919 return rc ? rc : exp->exp_need_sync;
923 * Add new client to the last_rcvd upon new connection.
925 * We use a bitmap to locate a free space in the last_rcvd file and initialize
928 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
930 struct tg_export_data *ted = &exp->exp_target_data;
931 struct lu_target *tgt = class_exp2tgt(exp);
936 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
937 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
940 if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
943 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
944 * there's no need for extra complication here
946 idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
948 if (idx >= LR_MAX_CLIENTS ||
949 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
950 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
951 tgt->lut_obd->obd_name, idx);
954 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
955 idx = find_next_zero_bit(tgt->lut_client_bitmap,
956 LR_MAX_CLIENTS, idx);
960 ted->ted_lr_idx = idx;
961 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
962 idx * tgt->lut_lsd.lsd_client_size;
964 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
966 if (tgt_is_multimodrpcs_client(exp)) {
967 /* Set MULTI RPCS incompatibility flag to prevent previous
968 * Lustre versions to mount a target with reply_data file */
969 atomic_inc(&tgt->lut_num_clients);
970 if (!(tgt->lut_lsd.lsd_feature_incompat &
971 OBD_INCOMPAT_MULTI_RPCS)) {
972 tgt->lut_lsd.lsd_feature_incompat |=
973 OBD_INCOMPAT_MULTI_RPCS;
974 rc = tgt_server_data_update(env, tgt, 1);
976 CERROR("%s: unable to set MULTI RPCS "
977 "incompatibility flag\n",
978 exp->exp_obd->obd_name);
983 /* assign client slot generation */
984 ted->ted_lcd->lcd_generation =
985 atomic_inc_return(&tgt->lut_client_generation);
987 ted->ted_lcd->lcd_generation = 0;
990 CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
992 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
993 ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
995 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
998 rc = tgt_client_data_update(env, exp);
1000 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
1001 tgt->lut_obd->obd_name, idx, rc);
1005 EXPORT_SYMBOL(tgt_client_new);
1007 /* Add an existing client to the MDS in-memory state based on
1008 * a client that was previously found in the last_rcvd file and
1009 * already has an assigned slot (idx >= 0).
1011 * It should not be possible to fail adding an existing client - otherwise
1012 * mdt_init_server_data() callsite needs to be fixed.
1014 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int idx)
1016 struct tg_export_data *ted = &exp->exp_target_data;
1017 struct lu_target *tgt = class_exp2tgt(exp);
1021 LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1022 LASSERTF(idx >= 0, "%d\n", idx);
1024 if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1025 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1028 if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1029 CERROR("%s: client %d: bit already set in bitmap!!\n",
1030 tgt->lut_obd->obd_name, idx);
1033 atomic_inc(&tgt->lut_num_clients);
1035 CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1037 tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1038 ted->ted_lcd->lcd_generation);
1040 ted->ted_lr_idx = idx;
1041 ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1042 idx * tgt->lut_lsd.lsd_client_size;
1044 mutex_init(&ted->ted_lcd_lock);
1046 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1051 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1053 struct tg_export_data *ted = &exp->exp_target_data;
1054 struct lu_target *tgt = class_exp2tgt(exp);
1059 LASSERT(ted->ted_lcd);
1061 if (unlikely(tgt == NULL)) {
1062 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1063 class_exp2obd(exp)->obd_name);
1067 /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1068 if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1069 (char *)tgt->lut_obd->obd_uuid.uuid) ||
1070 exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1073 /* Slot may be not yet assigned, use case is race between Client
1074 * reconnect and forced eviction */
1075 if (ted->ted_lr_idx < 0) {
1076 CWARN("%s: client with UUID '%s' not in bitmap\n",
1077 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1081 CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1082 tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1083 ted->ted_lcd->lcd_uuid);
1085 /* Clear the bit _after_ zeroing out the client so we don't
1086 race with filter_client_add and zero out new clients.*/
1087 if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1088 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1089 tgt->lut_obd->obd_name, ted->ted_lr_idx);
1093 /* Do not erase record for recoverable client. */
1094 if (exp->exp_flags & OBD_OPT_FAILOVER)
1097 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1100 /* Make sure the server's last_transno is up to date.
1101 * This should be done before zeroing client slot so last_transno will
1102 * be in server data or in client data in case of failure */
1103 rc = tgt_server_data_update(env, tgt, 0);
1105 CERROR("%s: failed to update server data, skip client %s "
1106 "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1107 ted->ted_lcd->lcd_uuid, rc);
1111 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1112 rc = tgt_client_data_update(env, exp);
1114 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1115 "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1116 tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1117 ted->ted_lr_idx, ted->ted_lr_off, rc);
1120 EXPORT_SYMBOL(tgt_client_del);
1122 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1123 struct tg_export_data *ted, struct tg_reply_data *trd,
1124 struct thandle *th, bool update_lrd_file)
1126 struct lsd_reply_data *lrd;
1129 lrd = &trd->trd_reply;
1130 /* update export last transno */
1131 mutex_lock(&ted->ted_lcd_lock);
1132 if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1133 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1134 mutex_unlock(&ted->ted_lcd_lock);
1136 /* find a empty slot */
1137 i = tgt_find_free_reply_slot(tgt);
1138 if (unlikely(i < 0)) {
1139 CERROR("%s: couldn't find a slot for reply data: "
1140 "rc = %d\n", tgt_name(tgt), i);
1145 if (update_lrd_file) {
1149 /* write reply data to disk */
1150 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1151 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1152 if (unlikely(rc != 0)) {
1153 CERROR("%s: can't update %s file: rc = %d\n",
1154 tgt_name(tgt), REPLY_DATA, rc);
1158 /* add reply data to target export's reply list */
1159 mutex_lock(&ted->ted_lcd_lock);
1160 list_add(&trd->trd_list, &ted->ted_reply_list);
1161 ted->ted_reply_cnt++;
1162 if (ted->ted_reply_cnt > ted->ted_reply_max)
1163 ted->ted_reply_max = ted->ted_reply_cnt;
1164 mutex_unlock(&ted->ted_lcd_lock);
1166 CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1167 "tag %hu, client gen %u, slot idx %d\n",
1168 trd, lrd->lrd_xid, lrd->lrd_transno,
1169 trd->trd_tag, lrd->lrd_client_gen, i);
1172 EXPORT_SYMBOL(tgt_add_reply_data);
1175 * last_rcvd & last_committed update callbacks
1177 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1178 struct dt_object *obj, __u64 opdata,
1179 struct thandle *th, struct ptlrpc_request *req)
1181 struct tgt_thread_info *tti = tgt_th_info(env);
1182 struct tgt_session_info *tsi = tgt_ses_info(env);
1183 struct obd_export *exp = tsi->tsi_exp;
1184 struct tg_export_data *ted;
1192 LASSERT(exp != NULL);
1193 ted = &exp->exp_target_data;
1195 lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1196 if (ted->ted_lr_idx < 0 && !lw_client)
1197 /* ofd connect may cause transaction before export has
1202 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1204 /* From update replay, tti_transno should be set already */
1205 LASSERT(tti->tti_transno != 0);
1207 spin_lock(&tgt->lut_translock);
1208 if (th->th_result != 0) {
1209 if (tti->tti_transno != 0) {
1210 CERROR("%s: replay transno %llu failed: rc = %d\n",
1211 tgt_name(tgt), tti->tti_transno, th->th_result);
1213 } else if (tti->tti_transno == 0) {
1214 tti->tti_transno = ++tgt->lut_last_transno;
1216 /* should be replay */
1217 if (tti->tti_transno > tgt->lut_last_transno)
1218 tgt->lut_last_transno = tti->tti_transno;
1220 spin_unlock(&tgt->lut_translock);
1222 /** VBR: set new versions */
1223 if (th->th_result == 0 && obj != NULL) {
1224 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1225 dt_version_set(env, dto, tti->tti_transno, th);
1228 /* filling reply data */
1229 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1230 tti->tti_transno, tgt->lut_obd->obd_last_committed);
1233 req->rq_transno = tti->tti_transno;
1234 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1237 /* if can't add callback, do sync write */
1238 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1241 /* All operations performed by LW clients are synchronous and
1242 * we store the committed transno in the last_rcvd header */
1243 spin_lock(&tgt->lut_translock);
1244 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1245 tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1246 spin_unlock(&tgt->lut_translock);
1247 /* Although lightweight (LW) connections have no slot
1248 * in the last_rcvd, we still want to maintain
1249 * the in-memory lsd_client_data structure in order to
1250 * properly handle reply reconstruction. */
1251 rc = tgt_server_data_write(env, tgt, th);
1253 spin_unlock(&tgt->lut_translock);
1255 } else if (ted->ted_lr_off == 0) {
1256 CERROR("%s: client idx %d has offset %lld\n",
1257 tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1261 /* Target that supports multiple reply data */
1262 if (tgt_is_multimodrpcs_client(exp)) {
1263 struct tg_reply_data *trd;
1264 struct lsd_reply_data *lrd;
1265 __u64 *pre_versions;
1269 if (unlikely(trd == NULL))
1272 /* fill reply data information */
1273 lrd = &trd->trd_reply;
1274 lrd->lrd_transno = tti->tti_transno;
1276 lrd->lrd_xid = req->rq_xid;
1277 trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1278 pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1279 lrd->lrd_result = th->th_result;
1280 lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1281 write_update = true;
1283 LASSERT(tsi->tsi_xid != 0);
1284 lrd->lrd_xid = tsi->tsi_xid;
1285 lrd->lrd_result = tsi->tsi_result;
1286 lrd->lrd_client_gen = tsi->tsi_client_gen;
1288 pre_versions = NULL;
1289 write_update = false;
1292 lrd->lrd_data = opdata;
1294 trd->trd_pre_versions[0] = pre_versions[0];
1295 trd->trd_pre_versions[1] = pre_versions[1];
1296 trd->trd_pre_versions[2] = pre_versions[2];
1297 trd->trd_pre_versions[3] = pre_versions[3];
1300 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1306 /* Enough for update replay, let's return */
1310 mutex_lock(&ted->ted_lcd_lock);
1311 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1312 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1313 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1314 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1315 ted->ted_lcd->lcd_last_close_result = th->th_result;
1317 /* VBR: save versions in last_rcvd for reconstruct. */
1318 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1321 ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1322 ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1323 ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1324 ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1326 transno_p = &ted->ted_lcd->lcd_last_transno;
1327 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1328 ted->ted_lcd->lcd_last_result = th->th_result;
1329 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1330 * see struct ldlm_reply->lock_policy_res1; */
1331 ted->ted_lcd->lcd_last_data = opdata;
1334 /* Update transno in slot only if non-zero number, i.e. no errors */
1335 if (likely(tti->tti_transno != 0)) {
1336 /* Don't overwrite bigger transaction number with lower one.
1337 * That is not sign of problem in all cases, but in any case
1338 * this value should be monotonically increased only. */
1339 if (*transno_p > tti->tti_transno) {
1340 if (!tgt->lut_no_reconstruct) {
1341 CERROR("%s: trying to overwrite bigger transno:"
1342 "on-disk: %llu, new: %llu replay: "
1343 "%d. See LU-617.\n", tgt_name(tgt),
1344 *transno_p, tti->tti_transno,
1345 req_is_replay(req));
1346 if (req_is_replay(req)) {
1347 spin_lock(&req->rq_export->exp_lock);
1348 req->rq_export->exp_vbr_failed = 1;
1349 spin_unlock(&req->rq_export->exp_lock);
1351 mutex_unlock(&ted->ted_lcd_lock);
1352 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1355 *transno_p = tti->tti_transno;
1360 tti->tti_off = ted->ted_lr_off;
1361 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1364 rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1367 mutex_unlock(&ted->ted_lcd_lock);
1371 mutex_unlock(&ted->ted_lcd_lock);
1376 * last_rcvd update for echo client simulation.
1377 * It updates last_rcvd client slot and version of object in
1378 * simple way but with all locks to simulate all drawbacks
1380 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1381 struct lu_target *tgt,
1382 struct dt_object *obj,
1384 struct obd_export *exp)
1386 struct tgt_thread_info *tti = tgt_th_info(env);
1387 struct tg_export_data *ted = &exp->exp_target_data;
1392 tti->tti_transno = 0;
1394 spin_lock(&tgt->lut_translock);
1395 if (th->th_result == 0)
1396 tti->tti_transno = ++tgt->lut_last_transno;
1397 spin_unlock(&tgt->lut_translock);
1399 /** VBR: set new versions */
1400 if (th->th_result == 0 && obj != NULL)
1401 dt_version_set(env, obj, tti->tti_transno, th);
1403 /* if can't add callback, do sync write */
1404 th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1407 LASSERT(ted->ted_lr_off > 0);
1409 mutex_lock(&ted->ted_lcd_lock);
1410 LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1411 ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1412 ted->ted_lcd->lcd_last_result = th->th_result;
1414 tti->tti_off = ted->ted_lr_off;
1415 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1416 mutex_unlock(&ted->ted_lcd_lock);
1420 static int tgt_clients_data_init(const struct lu_env *env,
1421 struct lu_target *tgt,
1422 unsigned long last_size)
1424 struct obd_device *obd = tgt->lut_obd;
1425 struct lr_server_data *lsd = &tgt->lut_lsd;
1426 struct lsd_client_data *lcd = NULL;
1427 struct tg_export_data *ted;
1430 loff_t off = lsd->lsd_client_start;
1431 __u32 generation = 0;
1432 struct cfs_hash *hash = NULL;
1436 if (tgt->lut_bottom->dd_rdonly)
1439 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1440 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1446 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1448 GOTO(err_out, rc = -ENODEV);
1450 for (cl_idx = 0; off < last_size; cl_idx++) {
1451 struct obd_export *exp;
1454 /* Don't assume off is incremented properly by
1455 * read_record(), in case sizeof(*lcd)
1456 * isn't the same as fsd->lsd_client_size. */
1457 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1458 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1460 CERROR("%s: error reading last_rcvd %s idx %d off "
1461 "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1464 break; /* read error shouldn't cause startup to fail */
1467 if (lcd->lcd_uuid[0] == '\0') {
1468 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1473 last_transno = lcd_last_transno(lcd);
1475 /* These exports are cleaned up by disconnect, so they
1476 * need to be set up like real exports as connect does.
1478 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1479 " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1480 cl_idx, last_transno, lsd->lsd_last_transno,
1481 lcd_last_xid(lcd), lcd->lcd_generation);
1483 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1485 if (PTR_ERR(exp) == -EALREADY) {
1486 /* export already exists, zero out this one */
1487 CERROR("%s: Duplicate export %s!\n",
1488 tgt_name(tgt), lcd->lcd_uuid);
1491 GOTO(err_out, rc = PTR_ERR(exp));
1494 ted = &exp->exp_target_data;
1495 *ted->ted_lcd = *lcd;
1497 rc = tgt_client_add(env, exp, cl_idx);
1498 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1499 /* VBR: set export last committed version */
1500 exp->exp_last_committed = last_transno;
1501 spin_lock(&exp->exp_lock);
1502 exp->exp_connecting = 0;
1503 exp->exp_in_recovery = 0;
1504 spin_unlock(&exp->exp_lock);
1505 obd->obd_max_recoverable_clients++;
1507 if (tgt->lut_lsd.lsd_feature_incompat &
1508 OBD_INCOMPAT_MULTI_RPCS &&
1509 lcd->lcd_generation != 0) {
1510 /* compute the highest valid client generation */
1511 generation = max(generation, lcd->lcd_generation);
1512 /* fill client_generation <-> export hash table */
1513 rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1514 &exp->exp_gen_hash);
1516 CERROR("%s: duplicate export for client "
1518 tgt_name(tgt), lcd->lcd_generation);
1519 class_export_put(exp);
1524 class_export_put(exp);
1526 rc = rev_import_init(exp);
1528 class_unlink_export(exp);
1532 /* Need to check last_rcvd even for duplicated exports. */
1533 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1534 cl_idx, last_transno);
1536 spin_lock(&tgt->lut_translock);
1537 tgt->lut_last_transno = max(last_transno,
1538 tgt->lut_last_transno);
1539 spin_unlock(&tgt->lut_translock);
1542 /* record highest valid client generation */
1543 atomic_set(&tgt->lut_client_generation, generation);
1547 cfs_hash_putref(hash);
1552 struct server_compat_data {
1559 static struct server_compat_data tgt_scd[] = {
1560 [LDD_F_SV_TYPE_MDT] = {
1561 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1562 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1563 OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1564 OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1565 OBD_INCOMPAT_MULTI_RPCS,
1566 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1567 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1568 OBD_INCOMPAT_MULTI_OI,
1570 [LDD_F_SV_TYPE_OST] = {
1571 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1572 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1574 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1575 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1579 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1581 struct tgt_thread_info *tti = tgt_th_info(env);
1582 struct lr_server_data *lsd = &tgt->lut_lsd;
1583 unsigned long last_rcvd_size;
1587 rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1591 last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1593 /* ensure padding in the struct is the correct size */
1594 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1595 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1597 rc = server_name2index(tgt_name(tgt), &index, NULL);
1599 CERROR("%s: Can not get index from name: rc = %d\n",
1603 /* server_name2index() returns type */
1605 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1606 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1610 /* last_rcvd on OST doesn't provide reconstruct support because there
1611 * may be up to 8 in-flight write requests per single slot in
1612 * last_rcvd client data
1614 tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1616 if (last_rcvd_size == 0) {
1617 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1619 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1620 sizeof(lsd->lsd_uuid));
1621 lsd->lsd_last_transno = 0;
1622 lsd->lsd_mount_count = 0;
1623 lsd->lsd_server_size = LR_SERVER_SIZE;
1624 lsd->lsd_client_start = LR_CLIENT_START;
1625 lsd->lsd_client_size = LR_CLIENT_SIZE;
1626 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1627 lsd->lsd_osd_index = index;
1628 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1629 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1631 rc = tgt_server_data_read(env, tgt);
1633 CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1637 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1638 if (tgt->lut_bottom->dd_rdonly) {
1639 /* Such difference may be caused by mounting
1640 * up snapshot with new fsname under rd_only
1641 * mode. But even if it was NOT, it will not
1642 * damage the system because of "rd_only". */
1643 memcpy(lsd->lsd_uuid,
1644 tgt->lut_obd->obd_uuid.uuid,
1645 sizeof(lsd->lsd_uuid));
1647 LCONSOLE_ERROR_MSG(0x157, "Trying to start "
1648 "OBD %s using the wrong "
1649 "disk %s. Were the /dev/ "
1650 "assignments rearranged?\n",
1651 tgt->lut_obd->obd_uuid.uuid,
1657 if (lsd->lsd_osd_index != index) {
1658 LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1659 "is different with the index %d in"
1660 "config log, It might be disk"
1661 "corruption!\n", tgt_name(tgt),
1662 lsd->lsd_osd_index, index);
1667 if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1668 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1670 lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1674 if (type == LDD_F_SV_TYPE_MDT)
1675 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1677 if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1678 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1680 lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1683 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1684 if (type == LDD_F_SV_TYPE_MDT &&
1685 !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1686 if (last_rcvd_size > lsd->lsd_client_start) {
1687 LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1688 "remove all clients for interop needs\n",
1690 rc = tgt_truncate_last_rcvd(env, tgt,
1691 lsd->lsd_client_start);
1694 last_rcvd_size = lsd->lsd_client_start;
1696 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1697 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1700 spin_lock(&tgt->lut_translock);
1701 tgt->lut_last_transno = lsd->lsd_last_transno;
1702 spin_unlock(&tgt->lut_translock);
1704 lsd->lsd_mount_count++;
1706 CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1707 CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1708 tgt_name(tgt), tgt->lut_last_transno);
1709 CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1710 tgt_name(tgt), lsd->lsd_mount_count);
1711 CDEBUG(D_INODE, "%s: server data size: %u\n",
1712 tgt_name(tgt), lsd->lsd_server_size);
1713 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1714 tgt_name(tgt), lsd->lsd_client_start);
1715 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1716 tgt_name(tgt), lsd->lsd_client_size);
1717 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1718 tgt_name(tgt), last_rcvd_size);
1719 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1720 tgt_name(tgt), lsd->lsd_subdir_count);
1721 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1722 last_rcvd_size <= lsd->lsd_client_start ? 0 :
1723 (last_rcvd_size - lsd->lsd_client_start) /
1724 lsd->lsd_client_size);
1725 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1727 if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1728 lsd->lsd_client_size == 0) {
1729 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1733 if (!tgt->lut_obd->obd_replayable)
1734 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1736 rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1738 GOTO(err_client, rc);
1740 spin_lock(&tgt->lut_translock);
1741 /* obd_last_committed is used for compatibility
1742 * with other lustre recovery code */
1743 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1744 spin_unlock(&tgt->lut_translock);
1746 tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1747 tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1749 /* save it, so mount count and last_transno is current */
1750 rc = tgt_server_data_update(env, tgt, 0);
1752 GOTO(err_client, rc);
1757 class_disconnect_exports(tgt->lut_obd);
1761 /* add credits for last_rcvd update */
1762 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1765 struct lu_target *tgt = cookie;
1766 struct tgt_session_info *tsi;
1767 struct tgt_thread_info *tti = tgt_th_info(env);
1768 struct dt_object *dto;
1771 /* For readonly case, the caller should have got failure
1772 * when start the transaction. If the logic comes here,
1773 * there must be something wrong. */
1774 if (unlikely(tgt->lut_bottom->dd_rdonly)) {
1779 /* if there is no session, then this transaction is not result of
1780 * request processing but some local operation */
1781 if (env->le_ses == NULL)
1784 LASSERT(tgt->lut_last_rcvd);
1785 tsi = tgt_ses_info(env);
1786 /* OFD may start transaction without export assigned */
1787 if (tsi->tsi_exp == NULL)
1790 if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1792 * Use maximum possible file offset for declaration to ensure
1793 * ZFS will reserve enough credits for a write anywhere in this
1794 * file, since we don't know where in the file the write will be
1795 * because a replay slot has not been assigned. This should be
1796 * replaced by dmu_tx_hold_append() when available.
1798 tti->tti_buf.lb_buf = NULL;
1799 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1800 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1801 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1805 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1807 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1808 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1814 if (tsi->tsi_vbr_obj != NULL &&
1815 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1816 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1817 rc = dt_declare_version_set(env, dto, th);
1823 /* Update last_rcvd records with latests transaction data */
1824 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1827 struct lu_target *tgt = cookie;
1828 struct tgt_session_info *tsi;
1829 struct tgt_thread_info *tti = tgt_th_info(env);
1830 struct dt_object *obj = NULL;
1834 if (env->le_ses == NULL)
1837 tsi = tgt_ses_info(env);
1838 /* OFD may start transaction without export assigned */
1839 if (tsi->tsi_exp == NULL)
1842 echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1844 if (tti->tti_has_trans && !echo_client) {
1845 if (tti->tti_mult_trans == 0) {
1846 CDEBUG(D_HA, "More than one transaction %llu\n",
1850 /* we need another transno to be assigned */
1851 tti->tti_transno = 0;
1852 } else if (th->th_result == 0) {
1853 tti->tti_has_trans = 1;
1856 if (tsi->tsi_vbr_obj != NULL &&
1857 !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1858 obj = tsi->tsi_vbr_obj;
1861 if (unlikely(echo_client)) /* echo client special case */
1862 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1865 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1870 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1872 struct tgt_thread_info *tti = tgt_th_info(env);
1873 struct lsd_reply_data *lrd = &tti->tti_lrd;
1874 unsigned long reply_data_size;
1876 struct lsd_reply_header *lrh = NULL;
1877 struct lsd_client_data *lcd = NULL;
1878 struct tg_reply_data *trd = NULL;
1881 struct cfs_hash *hash = NULL;
1882 struct obd_export *exp;
1883 struct tg_export_data *ted;
1884 int reply_data_recovered = 0;
1886 rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1889 reply_data_size = (unsigned long)tti->tti_attr.la_size;
1893 GOTO(out, rc = -ENOMEM);
1895 if (reply_data_size == 0) {
1896 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1898 lrh->lrh_magic = LRH_MAGIC;
1899 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1900 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1901 rc = tgt_reply_header_write(env, tgt, lrh);
1903 CERROR("%s: error writing %s: rc = %d\n",
1904 tgt_name(tgt), REPLY_DATA, rc);
1908 rc = tgt_reply_header_read(env, tgt, lrh);
1910 CERROR("%s: error reading %s: rc = %d\n",
1911 tgt_name(tgt), REPLY_DATA, rc);
1914 if (lrh->lrh_magic != LRH_MAGIC ||
1915 lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1916 lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1917 CERROR("%s: invalid header in %s\n",
1918 tgt_name(tgt), REPLY_DATA);
1919 GOTO(out, rc = -EINVAL);
1922 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1924 GOTO(out, rc = -ENODEV);
1928 GOTO(out, rc = -ENOMEM);
1932 GOTO(out, rc = -ENOMEM);
1934 /* Load reply_data from disk */
1935 for (idx = 0, off = sizeof(struct lsd_reply_header);
1936 off < reply_data_size;
1937 idx++, off += sizeof(struct lsd_reply_data)) {
1938 rc = tgt_reply_data_read(env, tgt, lrd, off);
1940 CERROR("%s: error reading %s: rc = %d\n",
1941 tgt_name(tgt), REPLY_DATA, rc);
1945 exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1947 /* old reply data from a disconnected client */
1950 ted = &exp->exp_target_data;
1951 mutex_lock(&ted->ted_lcd_lock);
1953 /* create in-memory reply_data and link it to
1954 * target export's reply list */
1955 rc = tgt_set_reply_slot(tgt, idx);
1957 mutex_unlock(&ted->ted_lcd_lock);
1960 trd->trd_reply = *lrd;
1961 trd->trd_pre_versions[0] = 0;
1962 trd->trd_pre_versions[1] = 0;
1963 trd->trd_pre_versions[2] = 0;
1964 trd->trd_pre_versions[3] = 0;
1965 trd->trd_index = idx;
1967 list_add(&trd->trd_list, &ted->ted_reply_list);
1968 ted->ted_reply_cnt++;
1969 if (ted->ted_reply_cnt > ted->ted_reply_max)
1970 ted->ted_reply_max = ted->ted_reply_cnt;
1972 CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1973 "transno %llu, client gen %u, slot idx %d\n",
1974 tgt_name(tgt), trd, lrd->lrd_xid,
1975 lrd->lrd_transno, lrd->lrd_client_gen,
1978 /* update export last committed transation */
1979 exp->exp_last_committed = max(exp->exp_last_committed,
1982 mutex_unlock(&ted->ted_lcd_lock);
1983 class_export_put(exp);
1985 /* update target last committed transaction */
1986 spin_lock(&tgt->lut_translock);
1987 tgt->lut_last_transno = max(tgt->lut_last_transno,
1989 spin_unlock(&tgt->lut_translock);
1991 reply_data_recovered++;
1995 GOTO(out, rc = -ENOMEM);
1997 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
1998 tgt_name(tgt), reply_data_recovered);
2001 spin_lock(&tgt->lut_translock);
2002 /* obd_last_committed is used for compatibility
2003 * with other lustre recovery code */
2004 tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
2005 spin_unlock(&tgt->lut_translock);
2011 cfs_hash_putref(hash);
2021 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
2024 struct tg_reply_data *found = NULL;
2025 struct tg_reply_data *reply;
2027 mutex_lock(&ted->ted_lcd_lock);
2028 list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
2029 if (reply->trd_reply.lrd_xid == xid) {
2034 mutex_unlock(&ted->ted_lcd_lock);
2037 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
2039 /* Look for a reply data matching specified request @req
2040 * A copy is returned in @trd if the pointer is not NULL
2042 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2044 struct tg_export_data *ted = &req->rq_export->exp_target_data;
2045 struct tg_reply_data *reply;
2048 reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2049 if (reply != NULL) {
2055 CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2056 tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2061 EXPORT_SYMBOL(tgt_lookup_reply);
2063 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2065 struct tg_export_data *ted = &exp->exp_target_data;
2066 struct lu_target *lut = class_exp2tgt(exp);
2067 struct tg_reply_data *trd, *tmp;
2069 mutex_lock(&ted->ted_lcd_lock);
2070 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2071 if (trd->trd_reply.lrd_xid > rcvd_xid)
2073 ted->ted_release_xid++;
2074 tgt_release_reply_data(lut, ted, trd);
2076 mutex_unlock(&ted->ted_lcd_lock);
2081 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2083 struct tg_export_data *ted = &exp->exp_target_data;
2084 struct lu_target *lut = class_exp2tgt(exp);
2085 struct tg_reply_data *trd, *tmp;
2090 mutex_lock(&ted->ted_lcd_lock);
2091 list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2092 if (trd->trd_tag != tag)
2094 ted->ted_release_tag++;
2095 tgt_release_reply_data(lut, ted, trd);
2098 mutex_unlock(&ted->ted_lcd_lock);