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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, Intel Corporation.
26 * lustre/lfsck/lfsck_layout.c
28 * Author: Fan, Yong <fan.yong@intel.com>
32 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_LFSCK
36 #include <linux/bitops.h>
37 #include <linux/rbtree.h>
39 #include <lustre/lustre_idl.h>
40 #include <lu_object.h>
41 #include <dt_object.h>
42 #include <lustre_linkea.h>
43 #include <lustre_fid.h>
44 #include <lustre_lib.h>
45 #include <lustre_net.h>
46 #include <lustre/lustre_user.h>
47 #include <md_object.h>
48 #include <obd_class.h>
50 #include "lfsck_internal.h"
52 #define LFSCK_LAYOUT_MAGIC 0xB173AE14
54 static const char lfsck_layout_name[] = "lfsck_layout";
56 struct lfsck_layout_seq {
57 struct list_head lls_list;
60 __u64 lls_lastid_known;
61 struct dt_object *lls_lastid_obj;
62 unsigned int lls_dirty:1;
65 struct lfsck_layout_slave_target {
66 /* link into lfsck_layout_slave_data::llsd_master_list. */
67 struct list_head llst_list;
68 /* The position for next record in the rbtree for iteration. */
69 struct lu_fid llst_fid;
70 /* Dummy hash for iteration against the rbtree. */
77 struct lfsck_layout_slave_data {
78 /* list for lfsck_layout_seq */
79 struct list_head llsd_seq_list;
81 /* list for the masters involve layout verification. */
82 struct list_head llsd_master_list;
85 struct dt_object *llsd_rb_obj;
86 struct rb_root llsd_rb_root;
87 rwlock_t llsd_rb_lock;
88 unsigned int llsd_rbtree_valid:1;
91 struct lfsck_layout_object {
92 struct dt_object *llo_obj;
93 struct lu_attr llo_attr;
98 struct lfsck_layout_req {
99 struct list_head llr_list;
100 struct lfsck_layout_object *llr_parent;
101 struct dt_object *llr_child;
103 __u32 llr_lov_idx; /* offset in LOV EA */
106 struct lfsck_layout_master_data {
107 spinlock_t llmd_lock;
108 struct list_head llmd_req_list;
110 /* list for the ost targets involve layout verification. */
111 struct list_head llmd_ost_list;
113 /* list for the ost targets in phase1 scanning. */
114 struct list_head llmd_ost_phase1_list;
116 /* list for the ost targets in phase1 scanning. */
117 struct list_head llmd_ost_phase2_list;
119 /* list for the mdt targets involve layout verification. */
120 struct list_head llmd_mdt_list;
122 /* list for the mdt targets in phase1 scanning. */
123 struct list_head llmd_mdt_phase1_list;
125 /* list for the mdt targets in phase1 scanning. */
126 struct list_head llmd_mdt_phase2_list;
128 struct ptlrpc_thread llmd_thread;
129 __u32 llmd_touch_gen;
131 int llmd_assistant_status;
132 int llmd_post_result;
133 unsigned int llmd_to_post:1,
134 llmd_to_double_scan:1,
135 llmd_in_double_scan:1,
139 struct lfsck_layout_slave_async_args {
140 struct obd_export *llsaa_exp;
141 struct lfsck_component *llsaa_com;
142 struct lfsck_layout_slave_target *llsaa_llst;
145 static struct lfsck_layout_object *
146 lfsck_layout_object_init(const struct lu_env *env, struct dt_object *obj,
149 struct lfsck_layout_object *llo;
154 return ERR_PTR(-ENOMEM);
156 rc = dt_attr_get(env, obj, &llo->llo_attr, BYPASS_CAPA);
163 lu_object_get(&obj->do_lu);
165 /* The gen can be used to check whether some others have changed the
166 * file layout after LFSCK pre-fetching but before real verification. */
168 atomic_set(&llo->llo_ref, 1);
174 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
176 if (atomic_dec_and_test(&llst->llst_ref)) {
177 LASSERT(list_empty(&llst->llst_list));
184 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
186 struct lfsck_layout_slave_target *llst;
187 struct lfsck_layout_slave_target *tmp;
194 INIT_LIST_HEAD(&llst->llst_list);
196 llst->llst_index = index;
197 atomic_set(&llst->llst_ref, 1);
199 spin_lock(&llsd->llsd_lock);
200 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
201 if (tmp->llst_index == index) {
207 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
208 spin_unlock(&llsd->llsd_lock);
217 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
218 struct lfsck_layout_slave_target *llst)
222 spin_lock(&llsd->llsd_lock);
223 if (!list_empty(&llst->llst_list)) {
224 list_del_init(&llst->llst_list);
227 spin_unlock(&llsd->llsd_lock);
230 lfsck_layout_llst_put(llst);
233 static inline struct lfsck_layout_slave_target *
234 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
235 __u32 index, bool unlink)
237 struct lfsck_layout_slave_target *llst;
239 spin_lock(&llsd->llsd_lock);
240 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
241 if (llst->llst_index == index) {
243 list_del_init(&llst->llst_list);
245 atomic_inc(&llst->llst_ref);
246 spin_unlock(&llsd->llsd_lock);
251 spin_unlock(&llsd->llsd_lock);
256 static inline void lfsck_layout_object_put(const struct lu_env *env,
257 struct lfsck_layout_object *llo)
259 if (atomic_dec_and_test(&llo->llo_ref)) {
260 lfsck_object_put(env, llo->llo_obj);
265 static struct lfsck_layout_req *
266 lfsck_layout_req_init(struct lfsck_layout_object *parent,
267 struct dt_object *child, __u32 ost_idx, __u32 lov_idx)
269 struct lfsck_layout_req *llr;
273 return ERR_PTR(-ENOMEM);
275 INIT_LIST_HEAD(&llr->llr_list);
276 atomic_inc(&parent->llo_ref);
277 llr->llr_parent = parent;
278 llr->llr_child = child;
279 llr->llr_ost_idx = ost_idx;
280 llr->llr_lov_idx = lov_idx;
285 static inline void lfsck_layout_req_fini(const struct lu_env *env,
286 struct lfsck_layout_req *llr)
288 lu_object_put(env, &llr->llr_child->do_lu);
289 lfsck_layout_object_put(env, llr->llr_parent);
293 static inline bool lfsck_layout_req_empty(struct lfsck_layout_master_data *llmd)
297 spin_lock(&llmd->llmd_lock);
298 if (list_empty(&llmd->llmd_req_list))
300 spin_unlock(&llmd->llmd_lock);
305 static int lfsck_layout_get_lovea(const struct lu_env *env,
306 struct dt_object *obj,
307 struct lu_buf *buf, ssize_t *buflen)
312 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV, BYPASS_CAPA);
314 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
319 lu_buf_realloc(buf, rc);
321 *buflen = buf->lb_len;
323 if (buf->lb_buf == NULL)
335 if (unlikely(buf->lb_buf == NULL)) {
336 lu_buf_alloc(buf, rc);
338 *buflen = buf->lb_len;
340 if (buf->lb_buf == NULL)
349 static int lfsck_layout_verify_header(struct lov_mds_md_v1 *lmm)
354 magic = le32_to_cpu(lmm->lmm_magic);
355 /* If magic crashed, keep it there. Sometime later, during OST-object
356 * orphan handling, if some OST-object(s) back-point to it, it can be
357 * verified and repaired. */
358 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
362 lmm_oi_cpu_to_le(&oi, &lmm->lmm_oi);
363 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
368 CDEBUG(D_LFSCK, "%s LOV EA magic %u on "DOSTID"\n",
369 rc == -EINVAL ? "Unknown" : "Unsupported",
375 pattern = le32_to_cpu(lmm->lmm_pattern);
376 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
377 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
380 lmm_oi_cpu_to_le(&oi, &lmm->lmm_oi);
381 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u on "DOSTID"\n",
382 pattern, POSTID(&oi));
390 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_CACHE_SIZE
391 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
392 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
394 struct lfsck_rbtree_node {
395 struct rb_node lrn_node;
398 atomic_t lrn_known_count;
399 atomic_t lrn_accessed_count;
400 void *lrn_known_bitmap;
401 void *lrn_accessed_bitmap;
404 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
405 __u64 seq, __u32 oid)
407 if (seq < lrn->lrn_seq)
410 if (seq > lrn->lrn_seq)
413 if (oid < lrn->lrn_first_oid)
416 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
422 /* The caller should hold llsd->llsd_rb_lock. */
423 static struct lfsck_rbtree_node *
424 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
425 const struct lu_fid *fid, bool *exact)
427 struct rb_node *node = llsd->llsd_rb_root.rb_node;
428 struct rb_node *prev = NULL;
429 struct lfsck_rbtree_node *lrn = NULL;
435 while (node != NULL) {
437 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
438 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
440 node = node->rb_left;
442 node = node->rb_right;
450 /* If there is no exactly matched one, then to the next valid one. */
453 /* The rbtree is empty. */
460 node = rb_next(prev);
462 /* The end of the rbtree. */
466 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
471 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
472 const struct lu_fid *fid)
474 struct lfsck_rbtree_node *lrn;
478 return ERR_PTR(-ENOMEM);
480 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
481 if (lrn->lrn_known_bitmap == NULL) {
484 return ERR_PTR(-ENOMEM);
487 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
488 if (lrn->lrn_accessed_bitmap == NULL) {
489 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
492 return ERR_PTR(-ENOMEM);
495 rb_init_node(&lrn->lrn_node);
496 lrn->lrn_seq = fid_seq(fid);
497 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
498 atomic_set(&lrn->lrn_known_count, 0);
499 atomic_set(&lrn->lrn_accessed_count, 0);
504 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
506 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
507 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
511 /* The caller should hold lock. */
512 static struct lfsck_rbtree_node *
513 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
514 struct lfsck_rbtree_node *lrn)
516 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
517 struct rb_node *parent = NULL;
518 struct lfsck_rbtree_node *tmp;
521 while (*pos != NULL) {
523 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
524 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
526 pos = &(*pos)->rb_left;
528 pos = &(*pos)->rb_right;
533 rb_link_node(&lrn->lrn_node, parent, pos);
534 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
539 extern const struct dt_index_operations lfsck_orphan_index_ops;
541 static int lfsck_rbtree_setup(const struct lu_env *env,
542 struct lfsck_component *com)
544 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
545 struct lfsck_instance *lfsck = com->lc_lfsck;
546 struct dt_device *dev = lfsck->li_bottom;
547 struct lfsck_layout_slave_data *llsd = com->lc_data;
548 struct dt_object *obj;
550 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
551 fid->f_oid = lfsck_dev_idx(dev);
553 obj = dt_locate(env, dev, fid);
555 RETURN(PTR_ERR(obj));
557 /* Generate an in-RAM object to stand for the layout rbtree.
558 * Scanning the layout rbtree will be via the iteration over
559 * the object. In the future, the rbtree may be written onto
560 * disk with the object.
562 * Mark the object to be as exist. */
563 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
564 obj->do_index_ops = &lfsck_orphan_index_ops;
565 llsd->llsd_rb_obj = obj;
566 llsd->llsd_rbtree_valid = 1;
567 dev->dd_record_fid_accessed = 1;
572 static void lfsck_rbtree_cleanup(const struct lu_env *env,
573 struct lfsck_component *com)
575 struct lfsck_instance *lfsck = com->lc_lfsck;
576 struct lfsck_layout_slave_data *llsd = com->lc_data;
577 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
578 struct rb_node *next;
579 struct lfsck_rbtree_node *lrn;
581 lfsck->li_bottom->dd_record_fid_accessed = 0;
582 /* Invalid the rbtree, then no others will use it. */
583 write_lock(&llsd->llsd_rb_lock);
584 llsd->llsd_rbtree_valid = 0;
585 write_unlock(&llsd->llsd_rb_lock);
587 while (node != NULL) {
588 next = rb_next(node);
589 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
590 rb_erase(node, &llsd->llsd_rb_root);
591 lfsck_rbtree_free(lrn);
595 if (llsd->llsd_rb_obj != NULL) {
596 lu_object_put(env, &llsd->llsd_rb_obj->do_lu);
597 llsd->llsd_rb_obj = NULL;
601 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
602 struct lfsck_component *com,
603 const struct lu_fid *fid,
606 struct lfsck_layout_slave_data *llsd = com->lc_data;
607 struct lfsck_rbtree_node *lrn;
613 CDEBUG(D_LFSCK, "%s: update bitmap for "DFID"\n",
614 lfsck_lfsck2name(com->lc_lfsck), PFID(fid));
616 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
619 if (!fid_is_idif(fid) && !fid_is_norm(fid))
622 read_lock(&llsd->llsd_rb_lock);
623 if (!llsd->llsd_rbtree_valid)
624 GOTO(unlock, rc = 0);
626 lrn = lfsck_rbtree_search(llsd, fid, NULL);
628 struct lfsck_rbtree_node *tmp;
632 read_unlock(&llsd->llsd_rb_lock);
633 tmp = lfsck_rbtree_new(env, fid);
635 GOTO(out, rc = PTR_ERR(tmp));
638 write_lock(&llsd->llsd_rb_lock);
639 if (!llsd->llsd_rbtree_valid) {
640 lfsck_rbtree_free(tmp);
641 GOTO(unlock, rc = 0);
644 lrn = lfsck_rbtree_insert(llsd, tmp);
646 lfsck_rbtree_free(tmp);
649 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
650 /* Any accessed object must be a known object. */
651 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
652 atomic_inc(&lrn->lrn_known_count);
653 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
654 atomic_inc(&lrn->lrn_accessed_count);
656 GOTO(unlock, rc = 0);
660 write_unlock(&llsd->llsd_rb_lock);
662 read_unlock(&llsd->llsd_rb_lock);
664 if (rc != 0 && accessed) {
665 struct lfsck_layout *lo = com->lc_file_ram;
667 CERROR("%s: Fail to update object accessed bitmap, will cause "
668 "incorrect LFSCK OST-object handling, so disable it to "
669 "cancel orphan handling for related device. rc = %d.\n",
670 lfsck_lfsck2name(com->lc_lfsck), rc);
671 lo->ll_flags |= LF_INCOMPLETE;
672 lfsck_rbtree_cleanup(env, com);
676 static inline bool is_dummy_lov_ost_data(struct lov_ost_data_v1 *obj)
678 if (fid_is_zero(&obj->l_ost_oi.oi_fid) &&
679 obj->l_ost_gen == 0 && obj->l_ost_idx == 0)
685 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
686 const struct lfsck_layout *src)
690 des->ll_magic = le32_to_cpu(src->ll_magic);
691 des->ll_status = le32_to_cpu(src->ll_status);
692 des->ll_flags = le32_to_cpu(src->ll_flags);
693 des->ll_success_count = le32_to_cpu(src->ll_success_count);
694 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
695 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
696 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
697 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
698 des->ll_time_last_checkpoint =
699 le64_to_cpu(src->ll_time_last_checkpoint);
700 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
701 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
702 des->ll_pos_first_inconsistent =
703 le64_to_cpu(src->ll_pos_first_inconsistent);
704 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
705 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
706 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
707 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
708 for (i = 0; i < LLIT_MAX; i++)
709 des->ll_objs_repaired[i] =
710 le64_to_cpu(src->ll_objs_repaired[i]);
711 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
714 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
715 const struct lfsck_layout *src)
719 des->ll_magic = cpu_to_le32(src->ll_magic);
720 des->ll_status = cpu_to_le32(src->ll_status);
721 des->ll_flags = cpu_to_le32(src->ll_flags);
722 des->ll_success_count = cpu_to_le32(src->ll_success_count);
723 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
724 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
725 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
726 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
727 des->ll_time_last_checkpoint =
728 cpu_to_le64(src->ll_time_last_checkpoint);
729 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
730 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
731 des->ll_pos_first_inconsistent =
732 cpu_to_le64(src->ll_pos_first_inconsistent);
733 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
734 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
735 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
736 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
737 for (i = 0; i < LLIT_MAX; i++)
738 des->ll_objs_repaired[i] =
739 cpu_to_le64(src->ll_objs_repaired[i]);
740 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
744 * \retval +ve: the lfsck_layout is broken, the caller should reset it.
745 * \retval 0: succeed.
746 * \retval -ve: failed cases.
748 static int lfsck_layout_load(const struct lu_env *env,
749 struct lfsck_component *com)
751 struct lfsck_layout *lo = com->lc_file_ram;
752 const struct dt_body_operations *dbo = com->lc_obj->do_body_ops;
753 ssize_t size = com->lc_file_size;
757 rc = dbo->dbo_read(env, com->lc_obj,
758 lfsck_buf_get(env, com->lc_file_disk, size), &pos,
763 CWARN("%s: failed to load lfsck_layout: rc = %d\n",
764 lfsck_lfsck2name(com->lc_lfsck), rc);
766 } else if (rc != size) {
767 CWARN("%s: crashed lfsck_layout, to be reset: rc = %d\n",
768 lfsck_lfsck2name(com->lc_lfsck), rc);
772 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
773 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
774 CWARN("%s: invalid lfsck_layout magic %#x != %#x, "
775 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
776 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
783 static int lfsck_layout_store(const struct lu_env *env,
784 struct lfsck_component *com)
786 struct dt_object *obj = com->lc_obj;
787 struct lfsck_instance *lfsck = com->lc_lfsck;
788 struct lfsck_layout *lo = com->lc_file_disk;
789 struct thandle *handle;
790 ssize_t size = com->lc_file_size;
795 lfsck_layout_cpu_to_le(lo, com->lc_file_ram);
796 handle = dt_trans_create(env, lfsck->li_bottom);
797 if (IS_ERR(handle)) {
798 rc = PTR_ERR(handle);
799 CERROR("%s: fail to create trans for storing lfsck_layout: "
800 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
804 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
807 CERROR("%s: fail to declare trans for storing lfsck_layout(1): "
808 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
812 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
814 CERROR("%s: fail to start trans for storing lfsck_layout: "
815 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
819 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos,
822 CERROR("%s: fail to store lfsck_layout(1): size = %d, "
823 "rc = %d\n", lfsck_lfsck2name(lfsck), (int)size, rc);
828 dt_trans_stop(env, lfsck->li_bottom, handle);
833 static int lfsck_layout_init(const struct lu_env *env,
834 struct lfsck_component *com)
836 struct lfsck_layout *lo = com->lc_file_ram;
839 memset(lo, 0, com->lc_file_size);
840 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
841 lo->ll_status = LS_INIT;
842 down_write(&com->lc_sem);
843 rc = lfsck_layout_store(env, com);
844 up_write(&com->lc_sem);
849 static int fid_is_for_ostobj(const struct lu_env *env, struct dt_device *dt,
850 struct dt_object *obj, const struct lu_fid *fid)
852 struct seq_server_site *ss = lu_site2seq(dt->dd_lu_dev.ld_site);
853 struct lu_seq_range range = { 0 };
854 struct lustre_mdt_attrs *lma;
857 fld_range_set_any(&range);
858 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), &range);
860 if (fld_range_is_ost(&range))
866 lma = &lfsck_env_info(env)->lti_lma;
867 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
868 XATTR_NAME_LMA, BYPASS_CAPA);
869 if (rc == sizeof(*lma)) {
870 lustre_lma_swab(lma);
872 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
875 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID, BYPASS_CAPA);
880 static struct lfsck_layout_seq *
881 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
883 struct lfsck_layout_seq *lls;
885 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
886 if (lls->lls_seq == seq)
889 if (lls->lls_seq > seq)
897 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
898 struct lfsck_layout_seq *lls)
900 struct lfsck_layout_seq *tmp;
901 struct list_head *pos = &llsd->llsd_seq_list;
903 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
904 if (lls->lls_seq < tmp->lls_seq) {
905 pos = &tmp->lls_list;
909 list_add_tail(&lls->lls_list, pos);
913 lfsck_layout_lastid_create(const struct lu_env *env,
914 struct lfsck_instance *lfsck,
915 struct dt_object *obj)
917 struct lfsck_thread_info *info = lfsck_env_info(env);
918 struct lu_attr *la = &info->lti_la;
919 struct dt_object_format *dof = &info->lti_dof;
920 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
921 struct dt_device *dt = lfsck->li_bottom;
928 CDEBUG(D_LFSCK, "To create LAST_ID for <seq> "LPX64"\n",
929 fid_seq(lfsck_dto2fid(obj)));
931 if (bk->lb_param & LPF_DRYRUN)
934 memset(la, 0, sizeof(*la));
935 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
936 la->la_valid = LA_MODE | LA_UID | LA_GID;
937 dof->dof_type = dt_mode_to_dft(S_IFREG);
939 th = dt_trans_create(env, dt);
941 RETURN(rc = PTR_ERR(th));
943 rc = dt_declare_create(env, obj, la, NULL, dof, th);
947 rc = dt_declare_record_write(env, obj,
948 lfsck_buf_get(env, &lastid,
954 rc = dt_trans_start_local(env, dt, th);
958 dt_write_lock(env, obj, 0);
959 if (likely(!dt_object_exists(obj))) {
960 rc = dt_create(env, obj, la, NULL, dof, th);
962 rc = dt_record_write(env, obj,
963 lfsck_buf_get(env, &lastid, sizeof(lastid)),
966 dt_write_unlock(env, obj);
971 dt_trans_stop(env, dt, th);
977 lfsck_layout_lastid_reload(const struct lu_env *env,
978 struct lfsck_component *com,
979 struct lfsck_layout_seq *lls)
985 dt_read_lock(env, lls->lls_lastid_obj, 0);
986 rc = dt_record_read(env, lls->lls_lastid_obj,
987 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
988 dt_read_unlock(env, lls->lls_lastid_obj);
989 if (unlikely(rc != 0))
992 lastid = le64_to_cpu(lastid);
993 if (lastid < lls->lls_lastid_known) {
994 struct lfsck_instance *lfsck = com->lc_lfsck;
995 struct lfsck_layout *lo = com->lc_file_ram;
997 lls->lls_lastid = lls->lls_lastid_known;
999 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1000 LASSERT(lfsck->li_out_notify != NULL);
1002 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1003 LE_LASTID_REBUILDING);
1004 lo->ll_flags |= LF_CRASHED_LASTID;
1006 } else if (lastid >= lls->lls_lastid) {
1007 lls->lls_lastid = lastid;
1015 lfsck_layout_lastid_store(const struct lu_env *env,
1016 struct lfsck_component *com)
1018 struct lfsck_instance *lfsck = com->lc_lfsck;
1019 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1020 struct dt_device *dt = lfsck->li_bottom;
1021 struct lfsck_layout_slave_data *llsd = com->lc_data;
1022 struct lfsck_layout_seq *lls;
1028 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1031 /* XXX: Add the code back if we really found related
1032 * inconsistent cases in the future. */
1034 if (!lls->lls_dirty) {
1035 /* In OFD, before the pre-creation, the LAST_ID
1036 * file will be updated firstly, which may hide
1037 * some potential crashed cases. For example:
1039 * The old obj1's ID is higher than old LAST_ID
1040 * but lower than the new LAST_ID, but the LFSCK
1041 * have not touch the obj1 until the OFD updated
1042 * the LAST_ID. So the LFSCK does not regard it
1043 * as crashed case. But when OFD does not create
1044 * successfully, it will set the LAST_ID as the
1045 * real created objects' ID, then LFSCK needs to
1046 * found related inconsistency. */
1047 rc = lfsck_layout_lastid_reload(env, com, lls);
1048 if (likely(!lls->lls_dirty))
1053 CDEBUG(D_LFSCK, "To sync the LAST_ID for <seq> "LPX64
1054 " as <oid> "LPU64"\n", lls->lls_seq, lls->lls_lastid);
1056 if (bk->lb_param & LPF_DRYRUN) {
1061 th = dt_trans_create(env, dt);
1064 CERROR("%s: (1) failed to store "LPX64": rc = %d\n",
1065 lfsck_lfsck2name(com->lc_lfsck),
1070 lastid = cpu_to_le64(lls->lls_lastid);
1071 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1072 lfsck_buf_get(env, &lastid,
1078 rc = dt_trans_start_local(env, dt, th);
1082 dt_write_lock(env, lls->lls_lastid_obj, 0);
1083 rc = dt_record_write(env, lls->lls_lastid_obj,
1084 lfsck_buf_get(env, &lastid,
1085 sizeof(lastid)), &pos, th);
1086 dt_write_unlock(env, lls->lls_lastid_obj);
1091 dt_trans_stop(env, dt, th);
1094 CERROR("%s: (2) failed to store "LPX64": rc = %d\n",
1095 lfsck_lfsck2name(com->lc_lfsck),
1104 lfsck_layout_lastid_load(const struct lu_env *env,
1105 struct lfsck_component *com,
1106 struct lfsck_layout_seq *lls)
1108 struct lfsck_instance *lfsck = com->lc_lfsck;
1109 struct lfsck_layout *lo = com->lc_file_ram;
1110 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1111 struct dt_object *obj;
1116 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck->li_bottom));
1117 obj = dt_locate(env, lfsck->li_bottom, fid);
1119 RETURN(PTR_ERR(obj));
1121 /* LAST_ID crashed, to be rebuilt */
1122 if (!dt_object_exists(obj)) {
1123 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1124 LASSERT(lfsck->li_out_notify != NULL);
1126 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1127 LE_LASTID_REBUILDING);
1128 lo->ll_flags |= LF_CRASHED_LASTID;
1130 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1132 struct l_wait_info lwi = LWI_TIMEOUT(
1133 cfs_time_seconds(cfs_fail_val),
1136 up_write(&com->lc_sem);
1137 l_wait_event(lfsck->li_thread.t_ctl_waitq,
1138 !thread_is_running(&lfsck->li_thread),
1140 down_write(&com->lc_sem);
1144 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1146 dt_read_lock(env, obj, 0);
1147 rc = dt_read(env, obj,
1148 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1150 dt_read_unlock(env, obj);
1151 if (rc != 0 && rc != sizeof(__u64))
1152 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1154 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1155 LASSERT(lfsck->li_out_notify != NULL);
1157 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1158 LE_LASTID_REBUILDING);
1159 lo->ll_flags |= LF_CRASHED_LASTID;
1162 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1170 lfsck_object_put(env, obj);
1172 lls->lls_lastid_obj = obj;
1177 static void lfsck_layout_record_failure(const struct lu_env *env,
1178 struct lfsck_instance *lfsck,
1179 struct lfsck_layout *lo)
1181 lo->ll_objs_failed_phase1++;
1182 if (unlikely(lo->ll_pos_first_inconsistent == 0))
1183 lo->ll_pos_first_inconsistent =
1184 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1188 static int lfsck_layout_master_async_interpret(const struct lu_env *env,
1189 struct ptlrpc_request *req,
1192 struct lfsck_async_interpret_args *laia = args;
1193 struct lfsck_component *com = laia->laia_com;
1194 struct lfsck_layout_master_data *llmd = com->lc_data;
1195 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1196 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1197 struct lfsck_request *lr = laia->laia_lr;
1199 switch (lr->lr_event) {
1202 struct lfsck_layout *lo = com->lc_file_ram;
1204 CERROR("%s: fail to notify %s %x for layout start: "
1205 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1206 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1207 ltd->ltd_index, rc);
1208 lo->ll_flags |= LF_INCOMPLETE;
1212 spin_lock(<ds->ltd_lock);
1213 if (ltd->ltd_dead || ltd->ltd_layout_done) {
1214 spin_unlock(<ds->ltd_lock);
1218 if (lr->lr_flags & LEF_TO_OST) {
1219 if (list_empty(<d->ltd_layout_list))
1220 list_add_tail(<d->ltd_layout_list,
1221 &llmd->llmd_ost_list);
1222 if (list_empty(<d->ltd_layout_phase_list))
1223 list_add_tail(<d->ltd_layout_phase_list,
1224 &llmd->llmd_ost_phase1_list);
1226 if (list_empty(<d->ltd_layout_list))
1227 list_add_tail(<d->ltd_layout_list,
1228 &llmd->llmd_mdt_list);
1229 if (list_empty(<d->ltd_layout_phase_list))
1230 list_add_tail(<d->ltd_layout_phase_list,
1231 &llmd->llmd_mdt_phase1_list);
1233 spin_unlock(<ds->ltd_lock);
1236 case LE_PHASE1_DONE:
1237 case LE_PHASE2_DONE:
1239 if (rc != 0 && rc != -EALREADY)
1240 CWARN("%s: fail to notify %s %x for layout: "
1241 "event = %d, rc = %d\n",
1242 lfsck_lfsck2name(com->lc_lfsck),
1243 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1244 ltd->ltd_index, lr->lr_event, rc);
1247 struct lfsck_reply *reply;
1250 spin_lock(<ds->ltd_lock);
1251 list_del_init(<d->ltd_layout_phase_list);
1252 list_del_init(<d->ltd_layout_list);
1253 spin_unlock(<ds->ltd_lock);
1257 reply = req_capsule_server_get(&req->rq_pill,
1259 if (reply == NULL) {
1261 CERROR("%s: invalid return value: rc = %d\n",
1262 lfsck_lfsck2name(com->lc_lfsck), rc);
1263 spin_lock(<ds->ltd_lock);
1264 list_del_init(<d->ltd_layout_phase_list);
1265 list_del_init(<d->ltd_layout_list);
1266 spin_unlock(<ds->ltd_lock);
1270 switch (reply->lr_status) {
1271 case LS_SCANNING_PHASE1:
1273 case LS_SCANNING_PHASE2:
1274 spin_lock(<ds->ltd_lock);
1275 list_del_init(<d->ltd_layout_phase_list);
1276 if (ltd->ltd_dead || ltd->ltd_layout_done) {
1277 spin_unlock(<ds->ltd_lock);
1281 if (lr->lr_flags & LEF_TO_OST)
1282 list_add_tail(<d->ltd_layout_phase_list,
1283 &llmd->llmd_ost_phase2_list);
1285 list_add_tail(<d->ltd_layout_phase_list,
1286 &llmd->llmd_mdt_phase2_list);
1287 spin_unlock(<ds->ltd_lock);
1290 spin_lock(<ds->ltd_lock);
1291 list_del_init(<d->ltd_layout_phase_list);
1292 list_del_init(<d->ltd_layout_list);
1293 spin_unlock(<ds->ltd_lock);
1299 CERROR("%s: unexpected event: rc = %d\n",
1300 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
1304 if (!laia->laia_shared) {
1306 lfsck_component_put(env, com);
1312 static int lfsck_layout_master_query_others(const struct lu_env *env,
1313 struct lfsck_component *com)
1315 struct lfsck_thread_info *info = lfsck_env_info(env);
1316 struct lfsck_request *lr = &info->lti_lr;
1317 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1318 struct lfsck_instance *lfsck = com->lc_lfsck;
1319 struct lfsck_layout_master_data *llmd = com->lc_data;
1320 struct ptlrpc_request_set *set;
1321 struct lfsck_tgt_descs *ltds;
1322 struct lfsck_tgt_desc *ltd;
1323 struct list_head *head;
1328 set = ptlrpc_prep_set();
1332 llmd->llmd_touch_gen++;
1333 memset(lr, 0, sizeof(*lr));
1334 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1335 lr->lr_event = LE_QUERY;
1336 lr->lr_active = LT_LAYOUT;
1337 laia->laia_com = com;
1339 laia->laia_shared = 0;
1341 if (!list_empty(&llmd->llmd_mdt_phase1_list)) {
1342 ltds = &lfsck->li_mdt_descs;
1344 head = &llmd->llmd_mdt_phase1_list;
1348 ltds = &lfsck->li_ost_descs;
1349 lr->lr_flags = LEF_TO_OST;
1350 head = &llmd->llmd_ost_phase1_list;
1353 laia->laia_ltds = ltds;
1354 spin_lock(<ds->ltd_lock);
1355 while (!list_empty(head)) {
1356 ltd = list_entry(head->next,
1357 struct lfsck_tgt_desc,
1358 ltd_layout_phase_list);
1359 if (ltd->ltd_layout_gen == llmd->llmd_touch_gen)
1362 ltd->ltd_layout_gen = llmd->llmd_touch_gen;
1363 list_del(<d->ltd_layout_phase_list);
1364 list_add_tail(<d->ltd_layout_phase_list, head);
1365 atomic_inc(<d->ltd_ref);
1366 laia->laia_ltd = ltd;
1367 spin_unlock(<ds->ltd_lock);
1368 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1369 lfsck_layout_master_async_interpret,
1372 CERROR("%s: fail to query %s %x for layout: rc = %d\n",
1373 lfsck_lfsck2name(lfsck),
1374 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1375 ltd->ltd_index, rc);
1379 spin_lock(<ds->ltd_lock);
1381 spin_unlock(<ds->ltd_lock);
1383 rc = ptlrpc_set_wait(set);
1385 ptlrpc_set_destroy(set);
1389 if (!(lr->lr_flags & LEF_TO_OST) &&
1390 list_empty(&llmd->llmd_mdt_phase1_list))
1393 ptlrpc_set_destroy(set);
1395 RETURN(rc1 != 0 ? rc1 : rc);
1399 lfsck_layout_master_to_orphan(struct lfsck_layout_master_data *llmd)
1401 return list_empty(&llmd->llmd_mdt_phase1_list) &&
1402 (!list_empty(&llmd->llmd_ost_phase2_list) ||
1403 list_empty(&llmd->llmd_ost_phase1_list));
1406 static int lfsck_layout_master_notify_others(const struct lu_env *env,
1407 struct lfsck_component *com,
1408 struct lfsck_request *lr)
1410 struct lfsck_thread_info *info = lfsck_env_info(env);
1411 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1412 struct lfsck_instance *lfsck = com->lc_lfsck;
1413 struct lfsck_layout_master_data *llmd = com->lc_data;
1414 struct lfsck_layout *lo = com->lc_file_ram;
1415 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1416 struct ptlrpc_request_set *set;
1417 struct lfsck_tgt_descs *ltds;
1418 struct lfsck_tgt_desc *ltd;
1419 struct lfsck_tgt_desc *next;
1420 struct list_head *head;
1425 set = ptlrpc_prep_set();
1429 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1430 lr->lr_active = LT_LAYOUT;
1431 laia->laia_com = com;
1433 laia->laia_shared = 0;
1434 switch (lr->lr_event) {
1436 /* Notify OSTs firstly, then handle other MDTs if needed. */
1437 ltds = &lfsck->li_ost_descs;
1438 laia->laia_ltds = ltds;
1439 down_read(<ds->ltd_rw_sem);
1440 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1441 ltd = lfsck_tgt_get(ltds, idx);
1442 LASSERT(ltd != NULL);
1444 laia->laia_ltd = ltd;
1445 ltd->ltd_layout_done = 0;
1446 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1447 lfsck_layout_master_async_interpret,
1448 laia, LFSCK_NOTIFY);
1450 CERROR("%s: fail to notify %s %x for layout "
1452 lfsck_lfsck2name(lfsck),
1453 (lr->lr_flags & LEF_TO_OST) ? "OST" :
1456 lo->ll_flags |= LF_INCOMPLETE;
1459 up_read(<ds->ltd_rw_sem);
1462 rc = ptlrpc_set_wait(set);
1464 ptlrpc_set_destroy(set);
1468 if (!(bk->lb_param & LPF_ALL_TGT))
1471 /* link other MDT targets locallly. */
1472 spin_lock(<ds->ltd_lock);
1473 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1474 ltd = LTD_TGT(ltds, idx);
1475 LASSERT(ltd != NULL);
1477 if (!list_empty(<d->ltd_layout_list))
1480 list_add_tail(<d->ltd_layout_list,
1481 &llmd->llmd_mdt_list);
1482 list_add_tail(<d->ltd_layout_phase_list,
1483 &llmd->llmd_mdt_phase1_list);
1485 spin_unlock(<ds->ltd_lock);
1488 case LE_PHASE2_DONE:
1489 case LE_PEER_EXIT: {
1490 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1491 if (bk->lb_param & LPF_ALL_TGT) {
1492 head = &llmd->llmd_mdt_list;
1493 ltds = &lfsck->li_mdt_descs;
1494 if (lr->lr_event == LE_STOP) {
1495 /* unlink other MDT targets locallly. */
1496 spin_lock(<ds->ltd_lock);
1497 list_for_each_entry_safe(ltd, next, head,
1499 list_del_init(<d->ltd_layout_phase_list);
1500 list_del_init(<d->ltd_layout_list);
1502 spin_unlock(<ds->ltd_lock);
1504 lr->lr_flags |= LEF_TO_OST;
1505 head = &llmd->llmd_ost_list;
1506 ltds = &lfsck->li_ost_descs;
1508 lr->lr_flags &= ~LEF_TO_OST;
1511 lr->lr_flags |= LEF_TO_OST;
1512 head = &llmd->llmd_ost_list;
1513 ltds = &lfsck->li_ost_descs;
1517 laia->laia_ltds = ltds;
1518 spin_lock(<ds->ltd_lock);
1519 while (!list_empty(head)) {
1520 ltd = list_entry(head->next, struct lfsck_tgt_desc,
1522 if (!list_empty(<d->ltd_layout_phase_list))
1523 list_del_init(<d->ltd_layout_phase_list);
1524 list_del_init(<d->ltd_layout_list);
1525 atomic_inc(<d->ltd_ref);
1526 laia->laia_ltd = ltd;
1527 spin_unlock(<ds->ltd_lock);
1528 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1529 lfsck_layout_master_async_interpret,
1530 laia, LFSCK_NOTIFY);
1532 CERROR("%s: fail to notify %s %x for layout "
1533 "stop/phase2: rc = %d\n",
1534 lfsck_lfsck2name(lfsck),
1535 (lr->lr_flags & LEF_TO_OST) ? "OST" :
1536 "MDT", ltd->ltd_index, rc);
1539 spin_lock(<ds->ltd_lock);
1541 spin_unlock(<ds->ltd_lock);
1543 rc = ptlrpc_set_wait(set);
1545 ptlrpc_set_destroy(set);
1549 if (!(lr->lr_flags & LEF_TO_OST)) {
1550 lr->lr_flags |= LEF_TO_OST;
1551 head = &llmd->llmd_ost_list;
1552 ltds = &lfsck->li_ost_descs;
1557 case LE_PHASE1_DONE:
1558 llmd->llmd_touch_gen++;
1559 ltds = &lfsck->li_mdt_descs;
1560 laia->laia_ltds = ltds;
1561 spin_lock(<ds->ltd_lock);
1562 while (!list_empty(&llmd->llmd_mdt_phase1_list)) {
1563 ltd = list_entry(llmd->llmd_mdt_phase1_list.next,
1564 struct lfsck_tgt_desc,
1565 ltd_layout_phase_list);
1566 if (ltd->ltd_layout_gen == llmd->llmd_touch_gen)
1569 ltd->ltd_layout_gen = llmd->llmd_touch_gen;
1570 list_del_init(<d->ltd_layout_phase_list);
1571 list_add_tail(<d->ltd_layout_phase_list,
1572 &llmd->llmd_mdt_phase1_list);
1573 atomic_inc(<d->ltd_ref);
1574 laia->laia_ltd = ltd;
1575 spin_unlock(<ds->ltd_lock);
1576 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1577 lfsck_layout_master_async_interpret,
1578 laia, LFSCK_NOTIFY);
1580 CERROR("%s: fail to notify MDT %x for layout "
1581 "phase1 done: rc = %d\n",
1582 lfsck_lfsck2name(lfsck),
1583 ltd->ltd_index, rc);
1586 spin_lock(<ds->ltd_lock);
1588 spin_unlock(<ds->ltd_lock);
1591 CERROR("%s: unexpected LFSCK event: rc = %d\n",
1592 lfsck_lfsck2name(lfsck), lr->lr_event);
1597 rc = ptlrpc_set_wait(set);
1598 ptlrpc_set_destroy(set);
1603 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1604 struct lfsck_component *com,
1607 struct lfsck_instance *lfsck = com->lc_lfsck;
1608 struct lfsck_layout *lo = com->lc_file_ram;
1609 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1611 down_write(&com->lc_sem);
1612 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1613 HALF_SEC - lfsck->li_time_last_checkpoint);
1614 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1615 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1618 com->lc_journal = 0;
1619 if (lo->ll_flags & LF_INCOMPLETE)
1620 lo->ll_status = LS_PARTIAL;
1622 lo->ll_status = LS_COMPLETED;
1623 if (!(bk->lb_param & LPF_DRYRUN))
1624 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1625 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1626 lo->ll_success_count++;
1627 } else if (rc == 0) {
1628 lo->ll_status = lfsck->li_status;
1629 if (lo->ll_status == 0)
1630 lo->ll_status = LS_STOPPED;
1632 lo->ll_status = LS_FAILED;
1635 rc = lfsck_layout_store(env, com);
1636 up_write(&com->lc_sem);
1641 static int lfsck_layout_lock(const struct lu_env *env,
1642 struct lfsck_component *com,
1643 struct dt_object *obj,
1644 struct lustre_handle *lh, __u64 bits)
1646 struct lfsck_thread_info *info = lfsck_env_info(env);
1647 ldlm_policy_data_t *policy = &info->lti_policy;
1648 struct ldlm_res_id *resid = &info->lti_resid;
1649 struct lfsck_instance *lfsck = com->lc_lfsck;
1650 __u64 flags = LDLM_FL_ATOMIC_CB;
1653 LASSERT(lfsck->li_namespace != NULL);
1655 memset(policy, 0, sizeof(*policy));
1656 policy->l_inodebits.bits = bits;
1657 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
1658 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_IBITS,
1659 policy, LCK_EX, &flags, ldlm_blocking_ast,
1660 ldlm_completion_ast, NULL, NULL, 0,
1661 LVB_T_NONE, NULL, lh);
1662 if (rc == ELDLM_OK) {
1665 memset(lh, 0, sizeof(*lh));
1672 static void lfsck_layout_unlock(struct lustre_handle *lh)
1674 if (lustre_handle_is_used(lh)) {
1675 ldlm_lock_decref(lh, LCK_EX);
1676 memset(lh, 0, sizeof(*lh));
1680 static int lfsck_layout_trans_stop(const struct lu_env *env,
1681 struct dt_device *dev,
1682 struct thandle *handle, int result)
1686 handle->th_result = result;
1687 rc = dt_trans_stop(env, dev, handle);
1697 * \retval +1: repaired
1698 * \retval 0: did nothing
1699 * \retval -ve: on error
1701 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1702 struct thandle *handle,
1703 struct dt_object *parent,
1704 struct lu_fid *cfid,
1706 struct lov_ost_data_v1 *slot,
1707 int fl, __u32 ost_idx)
1709 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1712 fid_to_ostid(cfid, oi);
1713 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1714 slot->l_ost_gen = cpu_to_le32(0);
1715 slot->l_ost_idx = cpu_to_le32(ost_idx);
1716 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV, fl, handle,
1725 * \retval +1: repaired
1726 * \retval 0: did nothing
1727 * \retval -ve: on error
1729 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1730 struct thandle *handle,
1731 struct dt_object *parent,
1732 struct lu_fid *cfid,
1733 struct lu_buf *buf, int fl,
1734 __u32 ost_idx, __u32 ea_off, bool reset)
1736 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1737 struct lov_ost_data_v1 *objs;
1741 if (fl == LU_XATTR_CREATE || reset) {
1742 LASSERT(buf->lb_len == lov_mds_md_size(ea_off + 1,
1745 memset(lmm, 0, buf->lb_len);
1746 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1747 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
1748 lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1749 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1750 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1751 /* XXX: We cannot know the stripe size,
1752 * then use the default value (1 MB). */
1753 lmm->lmm_stripe_size =
1754 cpu_to_le32(LOV_DESC_STRIPE_SIZE_DEFAULT);
1755 objs = &(lmm->lmm_objects[ea_off]);
1757 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
1758 int gap = ea_off - count;
1759 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1761 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3
1762 * which has been verified in lfsck_layout_verify_header()
1763 * already. If some new magic introduced in the future,
1764 * then layout LFSCK needs to be updated also. */
1765 if (magic == LOV_MAGIC_V1) {
1766 objs = &(lmm->lmm_objects[count]);
1768 LASSERT(magic == LOV_MAGIC_V3);
1769 objs = &((struct lov_mds_md_v3 *)lmm)->
1774 memset(objs, 0, gap * sizeof(*objs));
1775 lmm->lmm_layout_gen =
1776 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1779 LASSERT(buf->lb_len == lov_mds_md_size(ea_off + 1, magic));
1782 lmm->lmm_stripe_count = cpu_to_le16(ea_off + 1);
1783 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1790 * \retval +1: repaired
1791 * \retval 0: did nothing
1792 * \retval -ve: on error
1794 static int lfsck_layout_update_pfid(const struct lu_env *env,
1795 struct lfsck_component *com,
1796 struct dt_object *parent,
1797 struct lu_fid *cfid,
1798 struct dt_device *cdev, __u32 ea_off)
1800 struct filter_fid *pfid = &lfsck_env_info(env)->lti_new_pfid;
1801 struct dt_object *child;
1802 struct thandle *handle;
1803 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
1808 child = lfsck_object_find_by_dev(env, cdev, cfid);
1810 RETURN(PTR_ERR(child));
1812 handle = dt_trans_create(env, cdev);
1814 GOTO(out, rc = PTR_ERR(handle));
1816 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
1817 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
1818 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1819 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1820 * parent MDT-object's layout EA. */
1821 pfid->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
1822 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
1824 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
1828 rc = dt_trans_start(env, cdev, handle);
1832 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle,
1835 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1838 dt_trans_stop(env, cdev, handle);
1841 lu_object_put(env, &child->do_lu);
1847 * \retval +1: repaired
1848 * \retval 0: did nothing
1849 * \retval -ve: on error
1851 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1852 struct lfsck_component *com,
1853 struct lfsck_tgt_desc *ltd,
1854 struct lu_orphan_rec *rec,
1855 struct lu_fid *cfid,
1857 const char *postfix,
1860 struct lfsck_thread_info *info = lfsck_env_info(env);
1861 char *name = info->lti_key;
1862 struct lu_attr *la = &info->lti_la;
1863 struct dt_object_format *dof = &info->lti_dof;
1864 struct lfsck_instance *lfsck = com->lc_lfsck;
1865 struct lu_fid *pfid = &rec->lor_fid;
1866 struct lu_fid *tfid = &info->lti_fid3;
1867 struct dt_device *next = lfsck->li_next;
1868 struct dt_object *pobj = NULL;
1869 struct dt_object *cobj = NULL;
1870 struct thandle *th = NULL;
1871 struct lu_buf *pbuf = NULL;
1872 struct lu_buf *ea_buf = &info->lti_big_buf;
1873 struct lustre_handle lh = { 0 };
1874 int buflen = ea_buf->lb_len;
1879 /* Create .lustre/lost+found/MDTxxxx when needed. */
1880 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1881 rc = lfsck_create_lpf(env, lfsck);
1886 if (fid_is_zero(pfid)) {
1887 struct filter_fid *ff = &info->lti_new_pfid;
1889 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1893 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1894 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1895 /* Currently, the filter_fid::ff_parent::f_ver is not the
1896 * real parent MDT-object's FID::f_ver, instead it is the
1897 * OST-object index in its parent MDT-object's layout EA. */
1898 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
1899 pbuf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
1900 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1902 RETURN(PTR_ERR(cobj));
1905 CDEBUG(D_LFSCK, "Re-create the lost MDT-object: parent "
1906 DFID", child "DFID", OST-index %u, stripe-index %u, "
1907 "prefix %s, postfix %s\n",
1908 PFID(pfid), PFID(cfid), ltd->ltd_index, ea_off, prefix, postfix);
1910 pobj = lfsck_object_find_by_dev(env, lfsck->li_bottom, pfid);
1912 GOTO(put, rc = PTR_ERR(pobj));
1914 LASSERT(prefix != NULL);
1915 LASSERT(postfix != NULL);
1919 * 1. Use the MDT-object's FID as the name with prefix and postfix.
1921 * 1.1 prefix "C-": More than one OST-objects claim the same
1922 * MDT-object and the same slot in the layout EA.
1923 * It may be created for dangling referenced MDT
1924 * object or may be not.
1925 * 1.2 prefix "N-": The orphan OST-object does not know which one
1926 * is the real parent, so the LFSCK assign a new
1927 * FID as its parent.
1928 * 1.3 prefix "R-": The orphan OST-object know its parent FID but
1929 * does not know the position in the namespace.
1931 * 2. If there is name conflict, append more index for new name. */
1932 sprintf(name, "%s"DFID"%s", prefix, PFID(pfid), postfix);
1934 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1935 (const struct dt_key *)name, BYPASS_CAPA);
1936 if (rc != 0 && rc != -ENOENT)
1939 if (unlikely(rc == 0)) {
1940 CWARN("%s: The name %s under lost+found has been used "
1941 "by the "DFID". Try to increase the FID version "
1942 "for the new file name.\n",
1943 lfsck_lfsck2name(lfsck), name, PFID(tfid));
1944 sprintf(name, "%s"DFID"%s-%d", prefix, PFID(pfid),
1949 memset(la, 0, sizeof(*la));
1950 la->la_uid = rec->lor_uid;
1951 la->la_gid = rec->lor_gid;
1952 la->la_mode = S_IFREG | S_IRUSR | S_IWUSR;
1953 la->la_valid = LA_MODE | LA_UID | LA_GID;
1955 memset(dof, 0, sizeof(*dof));
1956 dof->dof_type = dt_mode_to_dft(S_IFREG);
1958 rc = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1960 lu_buf_realloc(ea_buf, rc);
1961 buflen = ea_buf->lb_len;
1962 if (ea_buf->lb_buf == NULL)
1963 GOTO(put, rc = -ENOMEM);
1965 ea_buf->lb_len = rc;
1968 /* Hold update lock on the .lustre/lost+found/MDTxxxx/.
1970 * XXX: Currently, we do not grab the PDO lock as normal create cases,
1971 * because creating MDT-object for orphan OST-object is rare, we
1972 * do not much care about the performance. It can be improved in
1973 * the future when needed. */
1974 rc = lfsck_layout_lock(env, com, lfsck->li_lpf_obj, &lh,
1975 MDS_INODELOCK_UPDATE);
1979 th = dt_trans_create(env, next);
1981 GOTO(unlock, rc = PTR_ERR(th));
1983 /* 1a. Update OST-object's parent information remotely.
1985 * If other subsequent modifications failed, then next LFSCK scanning
1986 * will process the OST-object as orphan again with known parent FID. */
1988 rc = dt_declare_xattr_set(env, cobj, pbuf, XATTR_NAME_FID, 0, th);
1993 /* 2a. Create the MDT-object locally. */
1994 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1998 /* 3a. Add layout EA for the MDT-object. */
1999 rc = dt_declare_xattr_set(env, pobj, ea_buf, XATTR_NAME_LOV,
2000 LU_XATTR_CREATE, th);
2004 /* 4a. Insert the MDT-object to .lustre/lost+found/MDTxxxx/ */
2005 rc = dt_declare_insert(env, lfsck->li_lpf_obj,
2006 (const struct dt_rec *)pfid,
2007 (const struct dt_key *)name, th);
2011 rc = dt_trans_start(env, next, th);
2015 /* 1b. Update OST-object's parent information remotely. */
2017 rc = dt_xattr_set(env, cobj, pbuf, XATTR_NAME_FID, 0, th,
2023 dt_write_lock(env, pobj, 0);
2024 /* 2b. Create the MDT-object locally. */
2025 rc = dt_create(env, pobj, la, NULL, dof, th);
2027 /* 3b. Add layout EA for the MDT-object. */
2028 rc = lfsck_layout_extend_lovea(env, th, pobj, cfid, ea_buf,
2029 LU_XATTR_CREATE, ltd->ltd_index,
2031 dt_write_unlock(env, pobj);
2035 /* 4b. Insert the MDT-object to .lustre/lost+found/MDTxxxx/ */
2036 rc = dt_insert(env, lfsck->li_lpf_obj,
2037 (const struct dt_rec *)pfid,
2038 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
2043 dt_trans_stop(env, next, th);
2046 lfsck_layout_unlock(&lh);
2049 if (cobj != NULL && !IS_ERR(cobj))
2050 lu_object_put(env, &cobj->do_lu);
2051 if (pobj != NULL && !IS_ERR(pobj))
2052 lu_object_put(env, &pobj->do_lu);
2053 ea_buf->lb_len = buflen;
2055 return rc >= 0 ? 1 : rc;
2058 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2059 struct lfsck_component *com,
2060 const struct lu_fid *fid,
2063 struct lfsck_thread_info *info = lfsck_env_info(env);
2064 struct lfsck_request *lr = &info->lti_lr;
2065 struct lfsck_instance *lfsck = com->lc_lfsck;
2066 struct lfsck_tgt_desc *ltd;
2067 struct ptlrpc_request *req;
2068 struct lfsck_request *tmp;
2069 struct obd_export *exp;
2073 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2074 if (unlikely(ltd == NULL))
2078 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2079 GOTO(put, rc = -EOPNOTSUPP);
2081 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2083 GOTO(put, rc = -ENOMEM);
2085 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2087 ptlrpc_request_free(req);
2092 memset(lr, 0, sizeof(*lr));
2093 lr->lr_event = LE_CONDITIONAL_DESTROY;
2094 lr->lr_active = LT_LAYOUT;
2097 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2099 ptlrpc_request_set_replen(req);
2101 rc = ptlrpc_queue_wait(req);
2102 ptlrpc_req_finished(req);
2112 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2113 struct lfsck_component *com,
2114 struct lfsck_request *lr)
2116 struct lfsck_thread_info *info = lfsck_env_info(env);
2117 struct lu_attr *la = &info->lti_la;
2118 ldlm_policy_data_t *policy = &info->lti_policy;
2119 struct ldlm_res_id *resid = &info->lti_resid;
2120 struct lfsck_instance *lfsck = com->lc_lfsck;
2121 struct dt_device *dev = lfsck->li_bottom;
2122 struct lu_fid *fid = &lr->lr_fid;
2123 struct dt_object *obj;
2124 struct thandle *th = NULL;
2125 struct lustre_handle lh = { 0 };
2130 obj = lfsck_object_find_by_dev(env, dev, fid);
2132 RETURN(PTR_ERR(obj));
2134 dt_read_lock(env, obj, 0);
2135 if (dt_object_exists(obj) == 0) {
2136 dt_read_unlock(env, obj);
2138 GOTO(put, rc = -ENOENT);
2141 /* Get obj's attr without lock firstly. */
2142 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2143 dt_read_unlock(env, obj);
2147 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2148 GOTO(put, rc = -ETXTBSY);
2150 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2151 LASSERT(lfsck->li_namespace != NULL);
2153 memset(policy, 0, sizeof(*policy));
2154 policy->l_extent.end = OBD_OBJECT_EOF;
2155 ost_fid_build_resid(fid, resid);
2156 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2157 policy, LCK_EX, &flags, ldlm_blocking_ast,
2158 ldlm_completion_ast, NULL, NULL, 0,
2159 LVB_T_NONE, NULL, &lh);
2161 GOTO(put, rc = -EIO);
2163 dt_write_lock(env, obj, 0);
2164 /* Get obj's attr within lock again. */
2165 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2169 if (la->la_ctime != 0)
2170 GOTO(unlock, rc = -ETXTBSY);
2172 th = dt_trans_create(env, dev);
2174 GOTO(unlock, rc = PTR_ERR(th));
2176 rc = dt_declare_ref_del(env, obj, th);
2180 rc = dt_declare_destroy(env, obj, th);
2184 rc = dt_trans_start_local(env, dev, th);
2188 rc = dt_ref_del(env, obj, th);
2192 rc = dt_destroy(env, obj, th);
2194 CDEBUG(D_LFSCK, "Destroy the empty OST-object "DFID" which "
2195 "was created for reparing dangling referenced case. "
2196 "But the original missed OST-object is found now.\n",
2202 dt_trans_stop(env, dev, th);
2205 dt_write_unlock(env, obj);
2206 ldlm_lock_decref(&lh, LCK_EX);
2209 lu_object_put(env, &obj->do_lu);
2215 * Some OST-object has occupied the specified layout EA slot.
2216 * Such OST-object may be generated by the LFSCK when repair
2217 * dangling referenced MDT-object, which can be indicated by
2218 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2219 * is true and such OST-object has not been modified yet, we
2220 * will replace it with the orphan OST-object; otherwise the
2221 * LFSCK will create new MDT-object to reference the orphan.
2223 * \retval +1: repaired
2224 * \retval 0: did nothing
2225 * \retval -ve: on error
2227 static int lfsck_layout_conflict_create(const struct lu_env *env,
2228 struct lfsck_component *com,
2229 struct lfsck_tgt_desc *ltd,
2230 struct lu_orphan_rec *rec,
2231 struct dt_object *parent,
2232 struct lu_fid *cfid,
2233 struct lu_buf *ea_buf,
2234 struct lov_ost_data_v1 *slot,
2235 __u32 ea_off, __u32 ori_len)
2237 struct lfsck_thread_info *info = lfsck_env_info(env);
2238 struct lu_fid *cfid2 = &info->lti_fid2;
2239 struct ost_id *oi = &info->lti_oi;
2240 char *postfix = info->lti_tmpbuf;
2241 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2242 struct dt_device *dev = com->lc_lfsck->li_bottom;
2243 struct thandle *th = NULL;
2244 struct lustre_handle lh = { 0 };
2245 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2249 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2250 ostid_to_fid(cfid2, oi, ost_idx2);
2252 CDEBUG(D_LFSCK, "Handle layout EA conflict: parent "DFID
2253 ", cur-child "DFID" on the OST %u, orphan-child "
2254 DFID" on the OST %u, stripe-index %u\n",
2255 PFID(lfsck_dto2fid(parent)), PFID(cfid2), ost_idx2,
2256 PFID(cfid), ltd->ltd_index, ea_off);
2258 /* Hold layout lock on the parent to prevent others to access. */
2259 rc = lfsck_layout_lock(env, com, parent, &lh,
2260 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
2264 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2266 /* If the conflict OST-obejct is not created for fixing dangling
2267 * referenced MDT-object in former LFSCK check/repair, or it has
2268 * been modified by others, then we cannot destroy it. Re-create
2269 * a new MDT-object for the orphan OST-object. */
2270 if (rc == -ETXTBSY) {
2271 /* No need the layout lock on the original parent. */
2272 lfsck_layout_unlock(&lh);
2273 ea_buf->lb_len = ori_len;
2275 fid_zero(&rec->lor_fid);
2276 snprintf(postfix, LFSCK_TMPBUF_LEN, "-"DFID"-%x",
2277 PFID(lu_object_fid(&parent->do_lu)), ea_off);
2278 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2279 "C-", postfix, ea_off);
2284 if (rc != 0 && rc != -ENOENT)
2287 th = dt_trans_create(env, dev);
2289 GOTO(unlock, rc = PTR_ERR(th));
2291 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2292 LU_XATTR_REPLACE, th);
2296 rc = dt_trans_start_local(env, dev, th);
2300 dt_write_lock(env, parent, 0);
2301 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2302 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2303 LU_XATTR_REPLACE, ltd->ltd_index);
2304 dt_write_unlock(env, parent);
2309 dt_trans_stop(env, dev, th);
2312 lfsck_layout_unlock(&lh);
2315 ea_buf->lb_len = ori_len;
2317 return rc >= 0 ? 1 : rc;
2321 * \retval +1: repaired
2322 * \retval 0: did nothing
2323 * \retval -ve: on error
2325 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2326 struct lfsck_component *com,
2327 struct lfsck_tgt_desc *ltd,
2328 struct lu_orphan_rec *rec,
2329 struct dt_object *parent,
2330 struct lu_fid *cfid,
2331 __u32 ost_idx, __u32 ea_off)
2333 struct lfsck_thread_info *info = lfsck_env_info(env);
2334 struct lu_buf *buf = &info->lti_big_buf;
2335 struct lu_fid *fid = &info->lti_fid2;
2336 struct ost_id *oi = &info->lti_oi;
2337 struct lfsck_instance *lfsck = com->lc_lfsck;
2338 struct dt_device *dt = lfsck->li_bottom;
2339 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2340 struct thandle *handle = NULL;
2341 size_t buflen = buf->lb_len;
2342 struct lov_mds_md_v1 *lmm;
2343 struct lov_ost_data_v1 *objs;
2344 struct lustre_handle lh = { 0 };
2351 bool locked = false;
2354 CDEBUG(D_LFSCK, "Re-create the crashed layout EA: parent "
2355 DFID", child "DFID", OST-index %u, stripe-index %u\n",
2356 PFID(lfsck_dto2fid(parent)), PFID(cfid), ost_idx, ea_off);
2358 rc = lfsck_layout_lock(env, com, parent, &lh,
2359 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
2365 dt_write_unlock(env, parent);
2369 if (handle != NULL) {
2370 dt_trans_stop(env, dt, handle);
2375 GOTO(unlock_layout, rc);
2377 if (buf->lb_len < rc) {
2378 lu_buf_realloc(buf, rc);
2379 buflen = buf->lb_len;
2380 if (buf->lb_buf == NULL)
2381 GOTO(unlock_layout, rc = -ENOMEM);
2384 if (!(bk->lb_param & LPF_DRYRUN)) {
2385 handle = dt_trans_create(env, dt);
2387 GOTO(unlock_layout, rc = PTR_ERR(handle));
2389 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2394 rc = dt_trans_start_local(env, dt, handle);
2399 dt_write_lock(env, parent, 0);
2401 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
2402 if (rc == -ERANGE) {
2403 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV,
2407 } else if (rc == -ENODATA || rc == 0) {
2408 rc = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2409 /* If the declared is not big enough, re-try. */
2410 if (buf->lb_len < rc)
2413 fl = LU_XATTR_CREATE;
2414 } else if (rc < 0) {
2415 GOTO(unlock_parent, rc);
2416 } else if (unlikely(buf->lb_len == 0)) {
2419 fl = LU_XATTR_REPLACE;
2422 if (fl == LU_XATTR_CREATE) {
2423 if (bk->lb_param & LPF_DRYRUN)
2424 GOTO(unlock_parent, rc = 1);
2426 LASSERT(buf->lb_len >= rc);
2429 rc = lfsck_layout_extend_lovea(env, handle, parent, cfid, buf,
2430 fl, ost_idx, ea_off, false);
2432 GOTO(unlock_parent, rc);
2436 rc1 = lfsck_layout_verify_header(lmm);
2438 /* If the LOV EA crashed, the rebuild it. */
2439 if (rc1 == -EINVAL) {
2440 if (bk->lb_param & LPF_DRYRUN)
2441 GOTO(unlock_parent, rc = 1);
2443 LASSERT(buf->lb_len >= rc);
2446 memset(lmm, 0, buf->lb_len);
2447 rc = lfsck_layout_extend_lovea(env, handle, parent, cfid, buf,
2448 fl, ost_idx, ea_off, true);
2450 GOTO(unlock_parent, rc);
2453 /* For other unknown magic/pattern, keep the current LOV EA. */
2455 GOTO(unlock_parent, rc = rc1);
2457 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2458 * been verified in lfsck_layout_verify_header() already. If some
2459 * new magic introduced in the future, then layout LFSCK needs to
2460 * be updated also. */
2461 magic = le32_to_cpu(lmm->lmm_magic);
2462 if (magic == LOV_MAGIC_V1) {
2463 objs = &(lmm->lmm_objects[0]);
2465 LASSERT(magic == LOV_MAGIC_V3);
2466 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2469 count = le16_to_cpu(lmm->lmm_stripe_count);
2471 GOTO(unlock_parent, rc = -EINVAL);
2474 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2475 if (count <= ea_off) {
2476 if (bk->lb_param & LPF_DRYRUN)
2477 GOTO(unlock_parent, rc = 1);
2479 rc = lov_mds_md_size(ea_off + 1, magic);
2480 /* If the declared is not big enough, re-try. */
2481 if (buf->lb_len < rc)
2485 rc = lfsck_layout_extend_lovea(env, handle, parent, cfid, buf,
2486 fl, ost_idx, ea_off, false);
2487 GOTO(unlock_parent, rc);
2490 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2493 for (i = 0; i < count; i++, objs++) {
2494 /* The MDT-object was created via lfsck_layout_recover_create()
2495 * by others before, and we fill the dummy layout EA. */
2496 if (is_dummy_lov_ost_data(objs)) {
2500 if (bk->lb_param & LPF_DRYRUN)
2501 GOTO(unlock_parent, rc = 1);
2503 lmm->lmm_layout_gen =
2504 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2505 rc = lfsck_layout_refill_lovea(env, handle, parent,
2506 cfid, buf, objs, fl,
2508 GOTO(unlock_parent, rc);
2511 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2512 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2513 /* It should be rare case, the slot is there, but the LFSCK
2514 * does not handle it during the first-phase cycle scanning. */
2515 if (unlikely(lu_fid_eq(fid, cfid))) {
2517 GOTO(unlock_parent, rc = 0);
2519 /* Rare case that the OST-object index
2520 * does not match the parent MDT-object
2521 * layout EA. We trust the later one. */
2522 if (bk->lb_param & LPF_DRYRUN)
2523 GOTO(unlock_parent, rc = 1);
2525 dt_write_unlock(env, parent);
2527 dt_trans_stop(env, dt, handle);
2528 lfsck_layout_unlock(&lh);
2529 buf->lb_len = buflen;
2530 rc = lfsck_layout_update_pfid(env, com, parent,
2531 cfid, ltd->ltd_tgt, i);
2538 /* The MDT-object exists, but related layout EA slot is occupied
2540 if (bk->lb_param & LPF_DRYRUN)
2541 GOTO(unlock_parent, rc = 1);
2543 dt_write_unlock(env, parent);
2545 dt_trans_stop(env, dt, handle);
2546 lfsck_layout_unlock(&lh);
2547 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2548 objs = &(lmm->lmm_objects[ea_off]);
2550 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2551 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2552 buf, objs, ea_off, buflen);
2558 dt_write_unlock(env, parent);
2562 dt_trans_stop(env, dt, handle);
2565 lfsck_layout_unlock(&lh);
2566 buf->lb_len = buflen;
2571 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2572 struct lfsck_component *com,
2573 struct lfsck_tgt_desc *ltd,
2574 struct lu_orphan_rec *rec,
2575 struct lu_fid *cfid)
2577 struct lfsck_layout *lo = com->lc_file_ram;
2578 struct lu_fid *pfid = &rec->lor_fid;
2579 struct dt_object *parent = NULL;
2580 __u32 ea_off = pfid->f_stripe_idx;
2584 if (!fid_is_sane(cfid))
2585 GOTO(out, rc = -EINVAL);
2587 if (fid_is_zero(pfid)) {
2588 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2594 if (!fid_is_sane(pfid))
2595 GOTO(out, rc = -EINVAL);
2597 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2599 GOTO(out, rc = PTR_ERR(parent));
2601 if (unlikely(dt_object_remote(parent) != 0))
2602 GOTO(put, rc = -EXDEV);
2604 if (dt_object_exists(parent) == 0) {
2605 lu_object_put(env, &parent->do_lu);
2606 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2611 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2612 GOTO(put, rc = -EISDIR);
2614 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2615 ltd->ltd_index, ea_off);
2621 lu_object_put(env, &parent->do_lu);
2623 /* The layout EA is changed, need to be reloaded next time. */
2624 lu_object_put_nocache(env, &parent->do_lu);
2627 down_write(&com->lc_sem);
2628 com->lc_new_scanned++;
2629 com->lc_new_checked++;
2631 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2633 } else if (rc < 0) {
2634 lo->ll_objs_failed_phase2++;
2636 up_write(&com->lc_sem);
2641 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2642 struct lfsck_component *com,
2643 struct lfsck_tgt_desc *ltd)
2645 struct lfsck_layout *lo = com->lc_file_ram;
2646 struct lfsck_instance *lfsck = com->lc_lfsck;
2647 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2648 struct lfsck_thread_info *info = lfsck_env_info(env);
2649 struct ost_id *oi = &info->lti_oi;
2650 struct lu_fid *fid = &info->lti_fid;
2651 struct dt_object *obj;
2652 const struct dt_it_ops *iops;
2657 CDEBUG(D_LFSCK, "%s: start the orphan scanning for OST%04x\n",
2658 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2660 ostid_set_seq(oi, FID_SEQ_IDIF);
2661 ostid_set_id(oi, 0);
2662 ostid_to_fid(fid, oi, ltd->ltd_index);
2663 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2664 if (unlikely(IS_ERR(obj)))
2665 RETURN(PTR_ERR(obj));
2667 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_orphan_features);
2671 iops = &obj->do_index_ops->dio_it;
2672 di = iops->init(env, obj, 0, BYPASS_CAPA);
2674 GOTO(put, rc = PTR_ERR(di));
2676 rc = iops->load(env, di, 0);
2678 /* -ESRCH means that the orphan OST-objects rbtree has been
2679 * cleanup because of the OSS server restart or other errors. */
2680 lo->ll_flags |= LF_INCOMPLETE;
2685 rc = iops->next(env, di);
2697 struct lu_orphan_rec *rec = &info->lti_rec;
2699 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
2701 struct ptlrpc_thread *thread = &lfsck->li_thread;
2702 struct l_wait_info lwi;
2704 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2706 l_wait_event(thread->t_ctl_waitq,
2707 !thread_is_running(thread),
2711 key = iops->key(env, di);
2712 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2713 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2715 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2716 &com->lc_fid_latest_scanned_phase2);
2717 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2720 lfsck_control_speed_by_self(com);
2722 rc = iops->next(env, di);
2723 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2730 iops->fini(env, di);
2732 lu_object_put(env, &obj->do_lu);
2734 CDEBUG(D_LFSCK, "%s: finish the orphan scanning for OST%04x, rc = %d\n",
2735 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2737 return rc > 0 ? 0 : rc;
2740 /* For the MDT-object with dangling reference, we need to re-create
2741 * the missed OST-object with the known FID/owner information. */
2742 static int lfsck_layout_recreate_ostobj(const struct lu_env *env,
2743 struct lfsck_component *com,
2744 struct lfsck_layout_req *llr,
2747 struct lfsck_thread_info *info = lfsck_env_info(env);
2748 struct filter_fid *pfid = &info->lti_new_pfid;
2749 struct dt_allocation_hint *hint = &info->lti_hint;
2750 struct dt_object *parent = llr->llr_parent->llo_obj;
2751 struct dt_object *child = llr->llr_child;
2752 struct dt_device *dev = lfsck_obj2dt_dev(child);
2753 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2754 struct thandle *handle;
2756 struct lustre_handle lh = { 0 };
2760 CDEBUG(D_LFSCK, "Repair dangling reference for: parent "DFID
2761 ", child "DFID", OST-index %u, stripe-index %u, owner %u:%u\n",
2762 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2763 llr->llr_ost_idx, llr->llr_lov_idx, la->la_uid, la->la_gid);
2765 rc = lfsck_layout_lock(env, com, parent, &lh,
2766 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
2770 handle = dt_trans_create(env, dev);
2772 GOTO(unlock1, rc = PTR_ERR(handle));
2774 hint->dah_parent = NULL;
2776 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2777 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2778 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2779 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2780 * parent MDT-object's layout EA. */
2781 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2782 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2784 rc = dt_declare_create(env, child, la, hint, NULL, handle);
2788 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2789 LU_XATTR_CREATE, handle);
2793 rc = dt_trans_start(env, dev, handle);
2797 dt_read_lock(env, parent, 0);
2798 if (unlikely(lu_object_is_dying(parent->do_lu.lo_header)))
2799 GOTO(unlock2, rc = 1);
2801 rc = dt_create(env, child, la, hint, NULL, handle);
2805 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2806 handle, BYPASS_CAPA);
2811 dt_read_unlock(env, parent);
2814 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2817 lfsck_layout_unlock(&lh);
2822 /* If the OST-object does not recognize the MDT-object as its parent, and
2823 * there is no other MDT-object claims as its parent, then just trust the
2824 * given MDT-object as its parent. So update the OST-object filter_fid. */
2825 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2826 struct lfsck_component *com,
2827 struct lfsck_layout_req *llr,
2828 const struct lu_attr *pla)
2830 struct lfsck_thread_info *info = lfsck_env_info(env);
2831 struct filter_fid *pfid = &info->lti_new_pfid;
2832 struct lu_attr *tla = &info->lti_la3;
2833 struct dt_object *parent = llr->llr_parent->llo_obj;
2834 struct dt_object *child = llr->llr_child;
2835 struct dt_device *dev = lfsck_obj2dt_dev(child);
2836 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2837 struct thandle *handle;
2839 struct lustre_handle lh = { 0 };
2843 CDEBUG(D_LFSCK, "Repair unmatched MDT-OST pair for: parent "DFID
2844 ", child "DFID", OST-index %u, stripe-index %u, owner %u:%u\n",
2845 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2846 llr->llr_ost_idx, llr->llr_lov_idx, pla->la_uid, pla->la_gid);
2848 rc = lfsck_layout_lock(env, com, parent, &lh,
2849 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
2853 handle = dt_trans_create(env, dev);
2855 GOTO(unlock1, rc = PTR_ERR(handle));
2857 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2858 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2859 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2860 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2861 * parent MDT-object's layout EA. */
2862 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2863 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2865 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2869 tla->la_valid = LA_UID | LA_GID;
2870 tla->la_uid = pla->la_uid;
2871 tla->la_gid = pla->la_gid;
2872 rc = dt_declare_attr_set(env, child, tla, handle);
2876 rc = dt_trans_start(env, dev, handle);
2880 dt_write_lock(env, parent, 0);
2881 if (unlikely(lu_object_is_dying(parent->do_lu.lo_header)))
2882 GOTO(unlock2, rc = 1);
2884 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle,
2889 /* Get the latest parent's owner. */
2890 rc = dt_attr_get(env, parent, tla, BYPASS_CAPA);
2894 tla->la_valid = LA_UID | LA_GID;
2895 rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
2900 dt_write_unlock(env, parent);
2903 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2906 lfsck_layout_unlock(&lh);
2911 /* If there are more than one MDT-objects claim as the OST-object's parent,
2912 * and the OST-object only recognizes one of them, then we need to generate
2913 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
2914 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
2915 struct lfsck_component *com,
2916 struct lfsck_layout_req *llr,
2920 struct lfsck_thread_info *info = lfsck_env_info(env);
2921 struct dt_allocation_hint *hint = &info->lti_hint;
2922 struct dt_object_format *dof = &info->lti_dof;
2923 struct dt_device *pdev = com->lc_lfsck->li_next;
2924 struct ost_id *oi = &info->lti_oi;
2925 struct dt_object *parent = llr->llr_parent->llo_obj;
2926 struct dt_device *cdev = lfsck_obj2dt_dev(llr->llr_child);
2927 struct dt_object *child = NULL;
2928 struct lu_device *d = &cdev->dd_lu_dev;
2929 struct lu_object *o = NULL;
2930 struct thandle *handle;
2931 struct lov_mds_md_v1 *lmm;
2932 struct lov_ost_data_v1 *objs;
2933 struct lustre_handle lh = { 0 };
2938 CDEBUG(D_LFSCK, "Repair multiple references for: parent "DFID
2939 ", OST-index %u, stripe-index %u, owner %u:%u\n",
2940 PFID(lfsck_dto2fid(parent)), llr->llr_ost_idx,
2941 llr->llr_lov_idx, la->la_uid, la->la_gid);
2943 rc = lfsck_layout_lock(env, com, parent, &lh,
2944 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
2948 handle = dt_trans_create(env, pdev);
2950 GOTO(unlock1, rc = PTR_ERR(handle));
2952 o = lu_object_anon(env, d, NULL);
2954 GOTO(stop, rc = PTR_ERR(o));
2956 child = container_of(o, struct dt_object, do_lu);
2957 o = lu_object_locate(o->lo_header, d->ld_type);
2958 if (unlikely(o == NULL))
2959 GOTO(stop, rc = -EINVAL);
2961 child = container_of(o, struct dt_object, do_lu);
2962 la->la_valid = LA_UID | LA_GID;
2963 hint->dah_parent = NULL;
2965 dof->dof_type = DFT_REGULAR;
2966 rc = dt_declare_create(env, child, la, NULL, NULL, handle);
2970 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2971 LU_XATTR_REPLACE, handle);
2975 rc = dt_trans_start(env, pdev, handle);
2979 dt_write_lock(env, parent, 0);
2980 if (unlikely(lu_object_is_dying(parent->do_lu.lo_header)))
2981 GOTO(unlock2, rc = 0);
2983 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
2984 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2985 GOTO(unlock2, rc = 0);
2988 /* Someone change layout during the LFSCK, no need to repair then. */
2989 if (le16_to_cpu(lmm->lmm_layout_gen) != llr->llr_parent->llo_gen)
2990 GOTO(unlock2, rc = 0);
2992 rc = dt_create(env, child, la, hint, dof, handle);
2996 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2997 * been verified in lfsck_layout_verify_header() already. If some
2998 * new magic introduced in the future, then layout LFSCK needs to
2999 * be updated also. */
3000 magic = le32_to_cpu(lmm->lmm_magic);
3001 if (magic == LOV_MAGIC_V1) {
3002 objs = &(lmm->lmm_objects[0]);
3004 LASSERT(magic == LOV_MAGIC_V3);
3005 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3008 lmm->lmm_layout_gen = cpu_to_le16(llr->llr_parent->llo_gen + 1);
3009 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3010 ostid_cpu_to_le(oi, &objs[llr->llr_lov_idx].l_ost_oi);
3011 objs[llr->llr_lov_idx].l_ost_gen = cpu_to_le32(0);
3012 objs[llr->llr_lov_idx].l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3013 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3014 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
3016 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
3019 dt_write_unlock(env, parent);
3023 lu_object_put(env, &child->do_lu);
3025 dt_trans_stop(env, pdev, handle);
3028 lfsck_layout_unlock(&lh);
3033 /* If the MDT-object and the OST-object have different owner information,
3034 * then trust the MDT-object, because the normal chown/chgrp handle order
3035 * is from MDT to OST, and it is possible that some chown/chgrp operation
3036 * is partly done. */
3037 static int lfsck_layout_repair_owner(const struct lu_env *env,
3038 struct lfsck_component *com,
3039 struct lfsck_layout_req *llr,
3040 struct lu_attr *pla)
3042 struct lfsck_thread_info *info = lfsck_env_info(env);
3043 struct lu_attr *tla = &info->lti_la3;
3044 struct dt_object *parent = llr->llr_parent->llo_obj;
3045 struct dt_object *child = llr->llr_child;
3046 struct dt_device *dev = lfsck_obj2dt_dev(child);
3047 struct thandle *handle;
3051 CDEBUG(D_LFSCK, "Repair inconsistent file owner for: parent "DFID
3052 ", child "DFID", OST-index %u, stripe-index %u, owner %u:%u\n",
3053 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
3054 llr->llr_ost_idx, llr->llr_lov_idx, pla->la_uid, pla->la_gid);
3056 handle = dt_trans_create(env, dev);
3058 RETURN(PTR_ERR(handle));
3060 tla->la_uid = pla->la_uid;
3061 tla->la_gid = pla->la_gid;
3062 tla->la_valid = LA_UID | LA_GID;
3063 rc = dt_declare_attr_set(env, child, tla, handle);
3067 rc = dt_trans_start(env, dev, handle);
3071 /* Use the dt_object lock to serialize with destroy and attr_set. */
3072 dt_read_lock(env, parent, 0);
3073 if (unlikely(lu_object_is_dying(parent->do_lu.lo_header)))
3074 GOTO(unlock, rc = 1);
3076 /* Get the latest parent's owner. */
3077 rc = dt_attr_get(env, parent, tla, BYPASS_CAPA);
3079 CWARN("%s: fail to get the latest parent's ("DFID") owner, "
3080 "not sure whether some others chown/chgrp during the "
3081 "LFSCK: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
3082 PFID(lfsck_dto2fid(parent)), rc);
3087 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3088 if (unlikely(tla->la_uid != pla->la_uid ||
3089 tla->la_gid != pla->la_gid))
3090 GOTO(unlock, rc = 1);
3092 tla->la_valid = LA_UID | LA_GID;
3093 rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
3098 dt_read_unlock(env, parent);
3101 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3106 /* Check whether the OST-object correctly back points to the
3107 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3108 static int lfsck_layout_check_parent(const struct lu_env *env,
3109 struct lfsck_component *com,
3110 struct dt_object *parent,
3111 const struct lu_fid *pfid,
3112 const struct lu_fid *cfid,
3113 const struct lu_attr *pla,
3114 const struct lu_attr *cla,
3115 struct lfsck_layout_req *llr,
3116 struct lu_buf *lov_ea, __u32 idx)
3118 struct lfsck_thread_info *info = lfsck_env_info(env);
3119 struct lu_buf *buf = &info->lti_big_buf;
3120 struct dt_object *tobj;
3121 struct lov_mds_md_v1 *lmm;
3122 struct lov_ost_data_v1 *objs;
3129 if (fid_is_zero(pfid)) {
3130 /* client never wrote. */
3131 if (cla->la_size == 0 && cla->la_blocks == 0) {
3132 if (unlikely(cla->la_uid != pla->la_uid ||
3133 cla->la_gid != pla->la_gid))
3134 RETURN (LLIT_INCONSISTENT_OWNER);
3139 RETURN(LLIT_UNMATCHED_PAIR);
3142 if (unlikely(!fid_is_sane(pfid)))
3143 RETURN(LLIT_UNMATCHED_PAIR);
3145 if (lu_fid_eq(pfid, lu_object_fid(&parent->do_lu))) {
3146 if (llr->llr_lov_idx == idx)
3149 RETURN(LLIT_UNMATCHED_PAIR);
3152 tobj = lfsck_object_find(env, com->lc_lfsck, pfid);
3154 RETURN(LLIT_UNMATCHED_PAIR);
3157 RETURN(PTR_ERR(tobj));
3159 if (!dt_object_exists(tobj))
3160 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3162 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3163 * remote one on another MDT. Then check whether the given OST-object
3164 * is in such layout. If yes, it is multiple referenced, otherwise it
3165 * is unmatched referenced case. */
3166 rc = lfsck_layout_get_lovea(env, tobj, buf, NULL);
3168 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3174 magic = le32_to_cpu(lmm->lmm_magic);
3175 if (magic == LOV_MAGIC_V1) {
3176 objs = &(lmm->lmm_objects[0]);
3178 LASSERT(magic == LOV_MAGIC_V3);
3179 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3182 count = le16_to_cpu(lmm->lmm_stripe_count);
3183 for (i = 0; i < count; i++, objs++) {
3184 struct lu_fid *tfid = &info->lti_fid2;
3185 struct ost_id *oi = &info->lti_oi;
3187 if (is_dummy_lov_ost_data(objs))
3190 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3191 ostid_to_fid(tfid, oi, le32_to_cpu(objs->l_ost_idx));
3192 if (lu_fid_eq(cfid, tfid)) {
3195 GOTO(out, rc = LLIT_MULTIPLE_REFERENCED);
3199 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3202 lfsck_object_put(env, tobj);
3207 static int lfsck_layout_assistant_handle_one(const struct lu_env *env,
3208 struct lfsck_component *com,
3209 struct lfsck_layout_req *llr)
3211 struct lfsck_layout *lo = com->lc_file_ram;
3212 struct lfsck_thread_info *info = lfsck_env_info(env);
3213 struct filter_fid_old *pea = &info->lti_old_pfid;
3214 struct lu_fid *pfid = &info->lti_fid;
3215 struct lu_buf *buf = NULL;
3216 struct dt_object *parent = llr->llr_parent->llo_obj;
3217 struct dt_object *child = llr->llr_child;
3218 struct lu_attr *pla = &info->lti_la;
3219 struct lu_attr *cla = &info->lti_la2;
3220 struct lfsck_instance *lfsck = com->lc_lfsck;
3221 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3222 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
3227 rc = dt_attr_get(env, parent, pla, BYPASS_CAPA);
3229 if (lu_object_is_dying(parent->do_lu.lo_header))
3235 rc = dt_attr_get(env, child, cla, BYPASS_CAPA);
3236 if (rc == -ENOENT) {
3237 if (lu_object_is_dying(parent->do_lu.lo_header))
3240 type = LLIT_DANGLING;
3247 buf = lfsck_buf_get(env, pea, sizeof(struct filter_fid_old));
3248 rc= dt_xattr_get(env, child, buf, XATTR_NAME_FID, BYPASS_CAPA);
3249 if (unlikely(rc >= 0 && rc != sizeof(struct filter_fid_old) &&
3250 rc != sizeof(struct filter_fid))) {
3251 type = LLIT_UNMATCHED_PAIR;
3255 if (rc < 0 && rc != -ENODATA)
3258 if (rc == -ENODATA) {
3261 fid_le_to_cpu(pfid, &pea->ff_parent);
3262 /* Currently, the filter_fid::ff_parent::f_ver is not the
3263 * real parent MDT-object's FID::f_ver, instead it is the
3264 * OST-object index in its parent MDT-object's layout EA. */
3265 idx = pfid->f_stripe_idx;
3269 rc = lfsck_layout_check_parent(env, com, parent, pfid,
3270 lu_object_fid(&child->do_lu),
3271 pla, cla, llr, buf, idx);
3280 if (unlikely(cla->la_uid != pla->la_uid ||
3281 cla->la_gid != pla->la_gid)) {
3282 type = LLIT_INCONSISTENT_OWNER;
3287 if (bk->lb_param & LPF_DRYRUN) {
3288 if (type != LLIT_NONE)
3296 memset(cla, 0, sizeof(*cla));
3297 cla->la_uid = pla->la_uid;
3298 cla->la_gid = pla->la_gid;
3299 cla->la_mode = S_IFREG | 0666;
3300 cla->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3301 LA_ATIME | LA_MTIME | LA_CTIME;
3302 rc = lfsck_layout_recreate_ostobj(env, com, llr, cla);
3304 case LLIT_UNMATCHED_PAIR:
3305 rc = lfsck_layout_repair_unmatched_pair(env, com, llr, pla);
3307 case LLIT_MULTIPLE_REFERENCED:
3308 rc = lfsck_layout_repair_multiple_references(env, com, llr,
3311 case LLIT_INCONSISTENT_OWNER:
3312 rc = lfsck_layout_repair_owner(env, com, llr, pla);
3322 down_write(&com->lc_sem);
3324 struct lfsck_layout_master_data *llmd = com->lc_data;
3326 if (unlikely(llmd->llmd_exit)) {
3328 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
3329 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
3330 rc == -EHOSTUNREACH) {
3331 /* If cannot touch the target server,
3332 * mark the LFSCK as INCOMPLETE. */
3333 CERROR("%s: Fail to talk with OST %x: rc = %d.\n",
3334 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
3335 lo->ll_flags |= LF_INCOMPLETE;
3336 lo->ll_objs_skipped++;
3339 lfsck_layout_record_failure(env, lfsck, lo);
3341 } else if (rc > 0) {
3342 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
3343 "unknown type = %d\n", type);
3345 lo->ll_objs_repaired[type - 1]++;
3346 if (bk->lb_param & LPF_DRYRUN &&
3347 unlikely(lo->ll_pos_first_inconsistent == 0))
3348 lo->ll_pos_first_inconsistent =
3349 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
3352 up_write(&com->lc_sem);
3357 static int lfsck_layout_assistant(void *args)
3359 struct lfsck_thread_args *lta = args;
3360 struct lu_env *env = <a->lta_env;
3361 struct lfsck_component *com = lta->lta_com;
3362 struct lfsck_instance *lfsck = lta->lta_lfsck;
3363 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3364 struct lfsck_position *pos = &com->lc_pos_start;
3365 struct lfsck_thread_info *info = lfsck_env_info(env);
3366 struct lfsck_request *lr = &info->lti_lr;
3367 struct lfsck_layout_master_data *llmd = com->lc_data;
3368 struct ptlrpc_thread *mthread = &lfsck->li_thread;
3369 struct ptlrpc_thread *athread = &llmd->llmd_thread;
3370 struct lfsck_layout_req *llr;
3371 struct l_wait_info lwi = { 0 };
3376 memset(lr, 0, sizeof(*lr));
3377 lr->lr_event = LE_START;
3378 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
3380 lr->lr_speed = bk->lb_speed_limit;
3381 lr->lr_version = bk->lb_version;
3382 lr->lr_param = bk->lb_param;
3383 lr->lr_async_windows = bk->lb_async_windows;
3384 lr->lr_flags = LEF_TO_OST;
3385 if (pos->lp_oit_cookie <= 1)
3386 lr->lr_param |= LPF_RESET;
3388 rc = lfsck_layout_master_notify_others(env, com, lr);
3390 CERROR("%s: fail to notify others for layout start: rc = %d\n",
3391 lfsck_lfsck2name(lfsck), rc);
3395 spin_lock(&llmd->llmd_lock);
3396 thread_set_flags(athread, SVC_RUNNING);
3397 spin_unlock(&llmd->llmd_lock);
3398 wake_up_all(&mthread->t_ctl_waitq);
3401 while (!list_empty(&llmd->llmd_req_list)) {
3402 bool wakeup = false;
3404 if (unlikely(llmd->llmd_exit ||
3405 !thread_is_running(mthread)))
3406 GOTO(cleanup1, rc = llmd->llmd_post_result);
3408 llr = list_entry(llmd->llmd_req_list.next,
3409 struct lfsck_layout_req,
3411 /* Only the lfsck_layout_assistant thread itself can
3412 * remove the "llr" from the head of the list, LFSCK
3413 * engine thread only inserts other new "lld" at the
3414 * end of the list. So it is safe to handle current
3415 * "llr" without the spin_lock. */
3416 rc = lfsck_layout_assistant_handle_one(env, com, llr);
3417 spin_lock(&llmd->llmd_lock);
3418 list_del_init(&llr->llr_list);
3419 llmd->llmd_prefetched--;
3420 /* Wake up the main engine thread only when the list
3421 * is empty or half of the prefetched items have been
3422 * handled to avoid too frequent thread schedule. */
3423 if (llmd->llmd_prefetched == 0 ||
3424 (bk->lb_async_windows != 0 &&
3425 bk->lb_async_windows / 2 ==
3426 llmd->llmd_prefetched))
3428 spin_unlock(&llmd->llmd_lock);
3430 wake_up_all(&mthread->t_ctl_waitq);
3432 lfsck_layout_req_fini(env, llr);
3433 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
3437 l_wait_event(athread->t_ctl_waitq,
3438 !lfsck_layout_req_empty(llmd) ||
3440 llmd->llmd_to_post ||
3441 llmd->llmd_to_double_scan,
3444 if (unlikely(llmd->llmd_exit))
3445 GOTO(cleanup1, rc = llmd->llmd_post_result);
3447 if (!list_empty(&llmd->llmd_req_list))
3450 if (llmd->llmd_to_post) {
3451 llmd->llmd_to_post = 0;
3452 LASSERT(llmd->llmd_post_result > 0);
3454 memset(lr, 0, sizeof(*lr));
3455 lr->lr_event = LE_PHASE1_DONE;
3456 lr->lr_status = llmd->llmd_post_result;
3457 rc = lfsck_layout_master_notify_others(env, com, lr);
3459 CERROR("%s: failed to notify others "
3460 "for layout post: rc = %d\n",
3461 lfsck_lfsck2name(lfsck), rc);
3463 /* Wakeup the master engine to go ahead. */
3464 wake_up_all(&mthread->t_ctl_waitq);
3467 if (llmd->llmd_to_double_scan) {
3468 llmd->llmd_to_double_scan = 0;
3469 atomic_inc(&lfsck->li_double_scan_count);
3470 llmd->llmd_in_double_scan = 1;
3471 wake_up_all(&mthread->t_ctl_waitq);
3473 com->lc_new_checked = 0;
3474 com->lc_new_scanned = 0;
3475 com->lc_time_last_checkpoint = cfs_time_current();
3476 com->lc_time_next_checkpoint =
3477 com->lc_time_last_checkpoint +
3478 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
3480 while (llmd->llmd_in_double_scan) {
3481 struct lfsck_tgt_descs *ltds =
3482 &lfsck->li_ost_descs;
3483 struct lfsck_tgt_desc *ltd;
3485 rc = lfsck_layout_master_query_others(env, com);
3486 if (lfsck_layout_master_to_orphan(llmd))
3492 /* Pull LFSCK status on related targets once
3493 * per 30 seconds if we are not notified. */
3494 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
3495 cfs_time_seconds(1),
3497 rc = l_wait_event(athread->t_ctl_waitq,
3498 lfsck_layout_master_to_orphan(llmd) ||
3500 !thread_is_running(mthread),
3503 if (unlikely(llmd->llmd_exit ||
3504 !thread_is_running(mthread)))
3505 GOTO(cleanup2, rc = 0);
3507 if (rc == -ETIMEDOUT)
3514 spin_lock(<ds->ltd_lock);
3516 &llmd->llmd_ost_phase2_list)) {
3518 llmd->llmd_ost_phase2_list.next,
3519 struct lfsck_tgt_desc,
3520 ltd_layout_phase_list);
3522 <d->ltd_layout_phase_list);
3523 spin_unlock(<ds->ltd_lock);
3525 if (bk->lb_param & LPF_ALL_TGT) {
3526 rc = lfsck_layout_scan_orphan(
3529 bk->lb_param & LPF_FAILOUT)
3533 if (unlikely(llmd->llmd_exit ||
3534 !thread_is_running(mthread)))
3535 GOTO(cleanup2, rc = 0);
3537 spin_lock(<ds->ltd_lock);
3540 if (list_empty(&llmd->llmd_ost_phase1_list)) {
3541 spin_unlock(<ds->ltd_lock);
3542 GOTO(cleanup2, rc = 1);
3544 spin_unlock(<ds->ltd_lock);
3550 /* Cleanup the unfinished requests. */
3551 spin_lock(&llmd->llmd_lock);
3553 llmd->llmd_assistant_status = rc;
3555 while (!list_empty(&llmd->llmd_req_list)) {
3556 llr = list_entry(llmd->llmd_req_list.next,
3557 struct lfsck_layout_req,
3559 list_del_init(&llr->llr_list);
3560 llmd->llmd_prefetched--;
3561 spin_unlock(&llmd->llmd_lock);
3562 lfsck_layout_req_fini(env, llr);
3563 spin_lock(&llmd->llmd_lock);
3565 spin_unlock(&llmd->llmd_lock);
3567 LASSERTF(llmd->llmd_prefetched == 0, "unmatched prefeteched objs %d\n",
3568 llmd->llmd_prefetched);
3571 memset(lr, 0, sizeof(*lr));
3573 lr->lr_event = LE_PHASE2_DONE;
3575 } else if (rc == 0) {
3576 if (lfsck->li_flags & LPF_ALL_TGT) {
3577 lr->lr_event = LE_STOP;
3578 lr->lr_status = LS_STOPPED;
3580 lr->lr_event = LE_PEER_EXIT;
3581 switch (lfsck->li_status) {
3584 lr->lr_status = LS_CO_PAUSED;
3588 lr->lr_status = LS_CO_STOPPED;
3591 CERROR("%s: unknown status: rc = %d\n",
3592 lfsck_lfsck2name(lfsck),
3594 lr->lr_status = LS_CO_FAILED;
3599 if (lfsck->li_flags & LPF_ALL_TGT) {
3600 lr->lr_event = LE_STOP;
3601 lr->lr_status = LS_FAILED;
3603 lr->lr_event = LE_PEER_EXIT;
3604 lr->lr_status = LS_CO_FAILED;
3608 rc1 = lfsck_layout_master_notify_others(env, com, lr);
3610 CERROR("%s: failed to notify others for layout quit: rc = %d\n",
3611 lfsck_lfsck2name(lfsck), rc1);
3615 /* Under force exit case, some requests may be just freed without
3616 * verification, those objects should be re-handled when next run.
3617 * So not update the on-disk tracing file under such case. */
3618 if (llmd->llmd_in_double_scan && !llmd->llmd_exit)
3619 rc1 = lfsck_layout_double_scan_result(env, com, rc);
3622 if (llmd->llmd_in_double_scan)
3623 atomic_dec(&lfsck->li_double_scan_count);
3625 spin_lock(&llmd->llmd_lock);
3626 llmd->llmd_assistant_status = (rc1 != 0 ? rc1 : rc);
3627 thread_set_flags(athread, SVC_STOPPED);
3628 wake_up_all(&mthread->t_ctl_waitq);
3629 spin_unlock(&llmd->llmd_lock);
3630 lfsck_thread_args_fini(lta);
3636 lfsck_layout_slave_async_interpret(const struct lu_env *env,
3637 struct ptlrpc_request *req,
3640 struct lfsck_layout_slave_async_args *llsaa = args;
3641 struct obd_export *exp = llsaa->llsaa_exp;
3642 struct lfsck_component *com = llsaa->llsaa_com;
3643 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
3644 struct lfsck_layout_slave_data *llsd = com->lc_data;
3648 /* It is quite probably caused by target crash,
3649 * to make the LFSCK can go ahead, assume that
3650 * the target finished the LFSCK prcoessing. */
3653 struct lfsck_reply *lr;
3655 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
3656 if (lr->lr_status != LS_SCANNING_PHASE1 &&
3657 lr->lr_status != LS_SCANNING_PHASE2)
3661 lfsck_layout_llst_del(llsd, llst);
3662 lfsck_layout_llst_put(llst);
3663 lfsck_component_put(env, com);
3664 class_export_put(exp);
3669 static int lfsck_layout_async_query(const struct lu_env *env,
3670 struct lfsck_component *com,
3671 struct obd_export *exp,
3672 struct lfsck_layout_slave_target *llst,
3673 struct lfsck_request *lr,
3674 struct ptlrpc_request_set *set)
3676 struct lfsck_layout_slave_async_args *llsaa;
3677 struct ptlrpc_request *req;
3678 struct lfsck_request *tmp;
3682 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
3686 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
3688 ptlrpc_request_free(req);
3692 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3694 ptlrpc_request_set_replen(req);
3696 llsaa = ptlrpc_req_async_args(req);
3697 llsaa->llsaa_exp = exp;
3698 llsaa->llsaa_com = lfsck_component_get(com);
3699 llsaa->llsaa_llst = llst;
3700 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
3701 ptlrpc_set_add_req(set, req);
3706 static int lfsck_layout_async_notify(const struct lu_env *env,
3707 struct obd_export *exp,
3708 struct lfsck_request *lr,
3709 struct ptlrpc_request_set *set)
3711 struct ptlrpc_request *req;
3712 struct lfsck_request *tmp;
3716 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3720 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3722 ptlrpc_request_free(req);
3726 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3728 ptlrpc_request_set_replen(req);
3729 ptlrpc_set_add_req(set, req);
3735 lfsck_layout_slave_query_master(const struct lu_env *env,
3736 struct lfsck_component *com)
3738 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3739 struct lfsck_instance *lfsck = com->lc_lfsck;
3740 struct lfsck_layout_slave_data *llsd = com->lc_data;
3741 struct lfsck_layout_slave_target *llst;
3742 struct obd_export *exp;
3743 struct ptlrpc_request_set *set;
3748 set = ptlrpc_prep_set();
3752 memset(lr, 0, sizeof(*lr));
3753 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
3754 lr->lr_event = LE_QUERY;
3755 lr->lr_active = LT_LAYOUT;
3757 llsd->llsd_touch_gen++;
3758 spin_lock(&llsd->llsd_lock);
3759 while (!list_empty(&llsd->llsd_master_list)) {
3760 llst = list_entry(llsd->llsd_master_list.next,
3761 struct lfsck_layout_slave_target,
3763 if (llst->llst_gen == llsd->llsd_touch_gen)
3766 llst->llst_gen = llsd->llsd_touch_gen;
3767 list_del(&llst->llst_list);
3768 list_add_tail(&llst->llst_list,
3769 &llsd->llsd_master_list);
3770 atomic_inc(&llst->llst_ref);
3771 spin_unlock(&llsd->llsd_lock);
3773 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3776 lfsck_layout_llst_del(llsd, llst);
3777 lfsck_layout_llst_put(llst);
3778 spin_lock(&llsd->llsd_lock);
3782 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
3784 CERROR("%s: slave fail to query %s for layout: "
3785 "rc = %d\n", lfsck_lfsck2name(lfsck),
3786 exp->exp_obd->obd_name, rc);
3788 lfsck_layout_llst_put(llst);
3789 class_export_put(exp);
3791 spin_lock(&llsd->llsd_lock);
3793 spin_unlock(&llsd->llsd_lock);
3795 rc = ptlrpc_set_wait(set);
3796 ptlrpc_set_destroy(set);
3798 RETURN(rc1 != 0 ? rc1 : rc);
3802 lfsck_layout_slave_notify_master(const struct lu_env *env,
3803 struct lfsck_component *com,
3804 enum lfsck_events event, int result)
3806 struct lfsck_instance *lfsck = com->lc_lfsck;
3807 struct lfsck_layout_slave_data *llsd = com->lc_data;
3808 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3809 struct lfsck_layout_slave_target *llst;
3810 struct obd_export *exp;
3811 struct ptlrpc_request_set *set;
3815 set = ptlrpc_prep_set();
3819 memset(lr, 0, sizeof(*lr));
3820 lr->lr_event = event;
3821 lr->lr_flags = LEF_FROM_OST;
3822 lr->lr_status = result;
3823 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
3824 lr->lr_active = LT_LAYOUT;
3825 llsd->llsd_touch_gen++;
3826 spin_lock(&llsd->llsd_lock);
3827 while (!list_empty(&llsd->llsd_master_list)) {
3828 llst = list_entry(llsd->llsd_master_list.next,
3829 struct lfsck_layout_slave_target,
3831 if (llst->llst_gen == llsd->llsd_touch_gen)
3834 llst->llst_gen = llsd->llsd_touch_gen;
3835 list_del(&llst->llst_list);
3836 list_add_tail(&llst->llst_list,
3837 &llsd->llsd_master_list);
3838 atomic_inc(&llst->llst_ref);
3839 spin_unlock(&llsd->llsd_lock);
3841 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3844 lfsck_layout_llst_del(llsd, llst);
3845 lfsck_layout_llst_put(llst);
3846 spin_lock(&llsd->llsd_lock);
3850 rc = lfsck_layout_async_notify(env, exp, lr, set);
3852 CERROR("%s: slave fail to notify %s for layout: "
3853 "rc = %d\n", lfsck_lfsck2name(lfsck),
3854 exp->exp_obd->obd_name, rc);
3855 lfsck_layout_llst_put(llst);
3856 class_export_put(exp);
3857 spin_lock(&llsd->llsd_lock);
3859 spin_unlock(&llsd->llsd_lock);
3861 ptlrpc_set_wait(set);
3862 ptlrpc_set_destroy(set);
3868 * \ret -ENODATA: unrecognized stripe
3869 * \ret = 0 : recognized stripe
3870 * \ret < 0 : other failures
3872 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
3873 struct lfsck_component *com,
3874 struct lu_fid *cfid,
3875 struct lu_fid *pfid)
3877 struct lfsck_thread_info *info = lfsck_env_info(env);
3878 struct lu_buf *buf = &info->lti_big_buf;
3879 struct ost_id *oi = &info->lti_oi;
3880 struct dt_object *obj;
3881 struct lov_mds_md_v1 *lmm;
3882 struct lov_ost_data_v1 *objs;
3883 __u32 idx = pfid->f_stripe_idx;
3891 obj = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3893 RETURN(PTR_ERR(obj));
3895 dt_read_lock(env, obj, 0);
3896 if (unlikely(!dt_object_exists(obj)))
3897 GOTO(unlock, rc = -ENOENT);
3899 rc = lfsck_layout_get_lovea(env, obj, buf, NULL);
3904 GOTO(unlock, rc = -ENODATA);
3907 rc = lfsck_layout_verify_header(lmm);
3911 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
3912 * been verified in lfsck_layout_verify_header() already. If some
3913 * new magic introduced in the future, then layout LFSCK needs to
3914 * be updated also. */
3915 magic = le32_to_cpu(lmm->lmm_magic);
3916 if (magic == LOV_MAGIC_V1) {
3917 objs = &(lmm->lmm_objects[0]);
3919 LASSERT(magic == LOV_MAGIC_V3);
3920 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3923 fid_to_ostid(cfid, oi);
3924 count = le16_to_cpu(lmm->lmm_stripe_count);
3925 for (i = 0; i < count; i++, objs++) {
3928 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
3929 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
3930 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
3933 GOTO(unlock, rc = -ENODATA);
3936 dt_read_unlock(env, obj);
3937 lu_object_put(env, &obj->do_lu);
3943 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
3944 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
3945 * layout EA from MDT to OST. On one hand, the OST no need to understand
3946 * the layout EA structure; on the other hand, it may cause trouble when
3947 * transfer large layout EA from MDT to OST via normal OUT RPC.
3949 * \ret > 0: unrecognized stripe
3950 * \ret = 0: recognized stripe
3951 * \ret < 0: other failures
3953 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
3954 struct lfsck_component *com,
3955 struct lu_fid *cfid,
3956 struct lu_fid *pfid)
3958 struct lfsck_instance *lfsck = com->lc_lfsck;
3959 struct obd_device *obd = lfsck->li_obd;
3960 struct seq_server_site *ss =
3961 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
3962 struct obd_export *exp = NULL;
3963 struct ptlrpc_request *req = NULL;
3964 struct lfsck_request *lr;
3965 struct lu_seq_range range = { 0 };
3969 if (unlikely(fid_is_idif(pfid)))
3972 fld_range_set_any(&range);
3973 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), &range);
3975 RETURN(rc == -ENOENT ? 1 : rc);
3977 if (unlikely(!fld_range_is_mdt(&range)))
3980 exp = lustre_find_lwp_by_index(obd->obd_name, range.lsr_index);
3981 if (unlikely(exp == NULL))
3984 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
3985 GOTO(out, rc = -EOPNOTSUPP);
3987 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3989 GOTO(out, rc = -ENOMEM);
3991 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3993 ptlrpc_request_free(req);
3998 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3999 memset(lr, 0, sizeof(*lr));
4000 lr->lr_event = LE_PAIRS_VERIFY;
4001 lr->lr_active = LT_LAYOUT;
4002 lr->lr_fid = *cfid; /* OST-object itself FID. */
4003 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
4005 ptlrpc_request_set_replen(req);
4006 rc = ptlrpc_queue_wait(req);
4007 ptlrpc_req_finished(req);
4009 if (rc == -ENOENT || rc == -ENODATA)
4016 class_export_put(exp);
4021 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
4022 struct lfsck_component *com,
4023 struct lfsck_request *lr)
4025 struct lfsck_thread_info *info = lfsck_env_info(env);
4026 struct filter_fid *ff = &info->lti_new_pfid;
4028 struct dt_device *dev = com->lc_lfsck->li_bottom;
4029 struct dt_object *obj;
4030 struct thandle *th = NULL;
4034 obj = lfsck_object_find_by_dev(env, dev, &lr->lr_fid);
4036 RETURN(PTR_ERR(obj));
4038 fid_cpu_to_le(&ff->ff_parent, &lr->lr_fid2);
4039 buf = lfsck_buf_get(env, ff, sizeof(*ff));
4040 dt_write_lock(env, obj, 0);
4041 if (unlikely(!dt_object_exists(obj)))
4042 GOTO(unlock, rc = 0);
4044 th = dt_trans_create(env, dev);
4046 GOTO(unlock, rc = PTR_ERR(th));
4048 rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_FID, 0, th);
4052 rc = dt_trans_start_local(env, dev, th);
4056 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_FID, 0, th, BYPASS_CAPA);
4061 dt_trans_stop(env, dev, th);
4064 dt_write_unlock(env, obj);
4065 lu_object_put(env, &obj->do_lu);
4072 static int lfsck_layout_reset(const struct lu_env *env,
4073 struct lfsck_component *com, bool init)
4075 struct lfsck_layout *lo = com->lc_file_ram;
4078 down_write(&com->lc_sem);
4080 memset(lo, 0, com->lc_file_size);
4082 __u32 count = lo->ll_success_count;
4083 __u64 last_time = lo->ll_time_last_complete;
4085 memset(lo, 0, com->lc_file_size);
4086 lo->ll_success_count = count;
4087 lo->ll_time_last_complete = last_time;
4090 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
4091 lo->ll_status = LS_INIT;
4093 rc = lfsck_layout_store(env, com);
4094 up_write(&com->lc_sem);
4099 static void lfsck_layout_fail(const struct lu_env *env,
4100 struct lfsck_component *com, bool new_checked)
4102 struct lfsck_layout *lo = com->lc_file_ram;
4104 down_write(&com->lc_sem);
4106 com->lc_new_checked++;
4107 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
4108 up_write(&com->lc_sem);
4111 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
4112 struct lfsck_component *com, bool init)
4114 struct lfsck_instance *lfsck = com->lc_lfsck;
4115 struct lfsck_layout *lo = com->lc_file_ram;
4116 struct lfsck_layout_master_data *llmd = com->lc_data;
4117 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4118 struct ptlrpc_thread *athread = &llmd->llmd_thread;
4119 struct l_wait_info lwi = { 0 };
4122 if (com->lc_new_checked == 0 && !init)
4125 l_wait_event(mthread->t_ctl_waitq,
4126 list_empty(&llmd->llmd_req_list) ||
4127 !thread_is_running(mthread) ||
4128 thread_is_stopped(athread),
4131 if (!thread_is_running(mthread) || thread_is_stopped(athread))
4134 down_write(&com->lc_sem);
4136 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
4138 lo->ll_pos_last_checkpoint =
4139 lfsck->li_pos_current.lp_oit_cookie;
4140 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4141 HALF_SEC - lfsck->li_time_last_checkpoint);
4142 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4143 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4144 com->lc_new_checked = 0;
4147 rc = lfsck_layout_store(env, com);
4148 up_write(&com->lc_sem);
4153 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
4154 struct lfsck_component *com, bool init)
4156 struct lfsck_instance *lfsck = com->lc_lfsck;
4157 struct lfsck_layout *lo = com->lc_file_ram;
4160 if (com->lc_new_checked == 0 && !init)
4163 down_write(&com->lc_sem);
4166 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
4168 lo->ll_pos_last_checkpoint =
4169 lfsck->li_pos_current.lp_oit_cookie;
4170 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4171 HALF_SEC - lfsck->li_time_last_checkpoint);
4172 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4173 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4174 com->lc_new_checked = 0;
4177 rc = lfsck_layout_store(env, com);
4179 up_write(&com->lc_sem);
4184 static int lfsck_layout_prep(const struct lu_env *env,
4185 struct lfsck_component *com,
4186 struct lfsck_start *start)
4188 struct lfsck_instance *lfsck = com->lc_lfsck;
4189 struct lfsck_layout *lo = com->lc_file_ram;
4190 struct lfsck_position *pos = &com->lc_pos_start;
4192 fid_zero(&pos->lp_dir_parent);
4193 pos->lp_dir_cookie = 0;
4194 if (lo->ll_status == LS_COMPLETED ||
4195 lo->ll_status == LS_PARTIAL ||
4196 /* To handle orphan, must scan from the beginning. */
4197 (start != NULL && start->ls_flags & LPF_ORPHAN)) {
4200 rc = lfsck_layout_reset(env, com, false);
4205 down_write(&com->lc_sem);
4206 lo->ll_time_latest_start = cfs_time_current_sec();
4207 spin_lock(&lfsck->li_lock);
4208 if (lo->ll_flags & LF_SCANNED_ONCE) {
4209 if (!lfsck->li_drop_dryrun ||
4210 lo->ll_pos_first_inconsistent == 0) {
4211 lo->ll_status = LS_SCANNING_PHASE2;
4212 list_del_init(&com->lc_link);
4213 list_add_tail(&com->lc_link,
4214 &lfsck->li_list_double_scan);
4215 pos->lp_oit_cookie = 0;
4219 lo->ll_status = LS_SCANNING_PHASE1;
4220 lo->ll_run_time_phase1 = 0;
4221 lo->ll_run_time_phase2 = 0;
4222 lo->ll_objs_checked_phase1 = 0;
4223 lo->ll_objs_checked_phase2 = 0;
4224 lo->ll_objs_failed_phase1 = 0;
4225 lo->ll_objs_failed_phase2 = 0;
4226 for (i = 0; i < LLIT_MAX; i++)
4227 lo->ll_objs_repaired[i] = 0;
4229 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4230 fid_zero(&com->lc_fid_latest_scanned_phase2);
4233 lo->ll_status = LS_SCANNING_PHASE1;
4234 if (!lfsck->li_drop_dryrun ||
4235 lo->ll_pos_first_inconsistent == 0)
4236 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
4238 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4240 spin_unlock(&lfsck->li_lock);
4241 up_write(&com->lc_sem);
4246 static int lfsck_layout_slave_prep(const struct lu_env *env,
4247 struct lfsck_component *com,
4248 struct lfsck_start_param *lsp)
4250 struct lfsck_layout_slave_data *llsd = com->lc_data;
4251 struct lfsck_start *start = lsp->lsp_start;
4254 rc = lfsck_layout_prep(env, com, start);
4255 if (rc != 0 || !lsp->lsp_index_valid)
4258 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
4259 if (rc == 0 && start != NULL && start->ls_flags & LPF_ORPHAN) {
4260 LASSERT(!llsd->llsd_rbtree_valid);
4262 write_lock(&llsd->llsd_rb_lock);
4263 rc = lfsck_rbtree_setup(env, com);
4264 write_unlock(&llsd->llsd_rb_lock);
4270 static int lfsck_layout_master_prep(const struct lu_env *env,
4271 struct lfsck_component *com,
4272 struct lfsck_start_param *lsp)
4274 struct lfsck_instance *lfsck = com->lc_lfsck;
4275 struct lfsck_layout_master_data *llmd = com->lc_data;
4276 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4277 struct ptlrpc_thread *athread = &llmd->llmd_thread;
4278 struct lfsck_thread_args *lta;
4282 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
4286 llmd->llmd_assistant_status = 0;
4287 llmd->llmd_post_result = 0;
4288 llmd->llmd_to_post = 0;
4289 llmd->llmd_to_double_scan = 0;
4290 llmd->llmd_in_double_scan = 0;
4291 llmd->llmd_exit = 0;
4292 thread_set_flags(athread, 0);
4294 lta = lfsck_thread_args_init(lfsck, com, lsp);
4296 RETURN(PTR_ERR(lta));
4298 rc = PTR_ERR(kthread_run(lfsck_layout_assistant, lta, "lfsck_layout"));
4299 if (IS_ERR_VALUE(rc)) {
4300 CERROR("%s: Cannot start LFSCK layout assistant thread: "
4301 "rc = %ld\n", lfsck_lfsck2name(lfsck), rc);
4302 lfsck_thread_args_fini(lta);
4304 struct l_wait_info lwi = { 0 };
4306 l_wait_event(mthread->t_ctl_waitq,
4307 thread_is_running(athread) ||
4308 thread_is_stopped(athread),
4310 if (unlikely(!thread_is_running(athread)))
4311 rc = llmd->llmd_assistant_status;
4319 /* Pre-fetch the attribute for each stripe in the given layout EA. */
4320 static int lfsck_layout_scan_stripes(const struct lu_env *env,
4321 struct lfsck_component *com,
4322 struct dt_object *parent,
4323 struct lov_mds_md_v1 *lmm)
4325 struct lfsck_thread_info *info = lfsck_env_info(env);
4326 struct lfsck_instance *lfsck = com->lc_lfsck;
4327 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4328 struct lfsck_layout *lo = com->lc_file_ram;
4329 struct lfsck_layout_master_data *llmd = com->lc_data;
4330 struct lfsck_layout_object *llo = NULL;
4331 struct lov_ost_data_v1 *objs;
4332 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4333 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4334 struct ptlrpc_thread *athread = &llmd->llmd_thread;
4335 struct l_wait_info lwi = { 0 };
4344 buf = lfsck_buf_get(env, &info->lti_old_pfid,
4345 sizeof(struct filter_fid_old));
4346 count = le16_to_cpu(lmm->lmm_stripe_count);
4347 gen = le16_to_cpu(lmm->lmm_layout_gen);
4348 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
4349 * been verified in lfsck_layout_verify_header() already. If some
4350 * new magic introduced in the future, then layout LFSCK needs to
4351 * be updated also. */
4352 magic = le32_to_cpu(lmm->lmm_magic);
4353 if (magic == LOV_MAGIC_V1) {
4354 objs = &(lmm->lmm_objects[0]);
4356 LASSERT(magic == LOV_MAGIC_V3);
4357 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4360 for (i = 0; i < count; i++, objs++) {
4361 struct lu_fid *fid = &info->lti_fid;
4362 struct ost_id *oi = &info->lti_oi;
4363 struct lfsck_layout_req *llr;
4364 struct lfsck_tgt_desc *tgt = NULL;
4365 struct dt_object *cobj = NULL;
4367 le32_to_cpu(objs->l_ost_idx);
4368 bool wakeup = false;
4370 if (is_dummy_lov_ost_data(objs))
4373 l_wait_event(mthread->t_ctl_waitq,
4374 bk->lb_async_windows == 0 ||
4375 llmd->llmd_prefetched < bk->lb_async_windows ||
4376 !thread_is_running(mthread) ||
4377 thread_is_stopped(athread),
4380 if (unlikely(!thread_is_running(mthread)) ||
4381 thread_is_stopped(athread))
4384 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4385 ostid_to_fid(fid, oi, index);
4386 tgt = lfsck_tgt_get(ltds, index);
4387 if (unlikely(tgt == NULL)) {
4388 CERROR("%s: Cannot talk with OST %x which did not join "
4389 "the layout LFSCK.\n",
4390 lfsck_lfsck2name(lfsck), index);
4391 lo->ll_flags |= LF_INCOMPLETE;
4395 cobj = lfsck_object_find_by_dev(env, tgt->ltd_tgt, fid);
4401 rc = dt_declare_attr_get(env, cobj, BYPASS_CAPA);
4405 rc = dt_declare_xattr_get(env, cobj, buf, XATTR_NAME_FID,
4411 llo = lfsck_layout_object_init(env, parent, gen);
4418 llr = lfsck_layout_req_init(llo, cobj, index, i);
4425 spin_lock(&llmd->llmd_lock);
4426 if (llmd->llmd_assistant_status < 0) {
4427 spin_unlock(&llmd->llmd_lock);
4428 lfsck_layout_req_fini(env, llr);
4430 RETURN(llmd->llmd_assistant_status);
4433 list_add_tail(&llr->llr_list, &llmd->llmd_req_list);
4434 if (llmd->llmd_prefetched == 0)
4437 llmd->llmd_prefetched++;
4438 spin_unlock(&llmd->llmd_lock);
4440 wake_up_all(&athread->t_ctl_waitq);
4443 down_write(&com->lc_sem);
4444 com->lc_new_checked++;
4446 lfsck_layout_record_failure(env, lfsck, lo);
4447 up_write(&com->lc_sem);
4449 if (cobj != NULL && !IS_ERR(cobj))
4450 lu_object_put(env, &cobj->do_lu);
4452 if (likely(tgt != NULL))
4455 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4462 if (llo != NULL && !IS_ERR(llo))
4463 lfsck_layout_object_put(env, llo);
4468 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
4469 * the OST-object's attribute and generate an structure lfsck_layout_req on the
4470 * list ::llmd_req_list.
4472 * For each request on above list, the lfsck_layout_assistant thread compares
4473 * the OST side attribute with local attribute, if inconsistent, then repair it.
4475 * All above processing is async mode with pipeline. */
4476 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
4477 struct lfsck_component *com,
4478 struct dt_object *obj)
4480 struct lfsck_thread_info *info = lfsck_env_info(env);
4481 struct ost_id *oi = &info->lti_oi;
4482 struct lfsck_layout *lo = com->lc_file_ram;
4483 struct lfsck_layout_master_data *llmd = com->lc_data;
4484 struct lfsck_instance *lfsck = com->lc_lfsck;
4485 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4486 struct thandle *handle = NULL;
4487 struct lu_buf *buf = &info->lti_big_buf;
4488 struct lov_mds_md_v1 *lmm = NULL;
4489 struct dt_device *dev = lfsck->li_bottom;
4490 struct lustre_handle lh = { 0 };
4491 ssize_t buflen = buf->lb_len;
4493 bool locked = false;
4494 bool stripe = false;
4497 if (!S_ISREG(lfsck_object_type(obj)))
4500 if (llmd->llmd_assistant_status < 0)
4501 GOTO(out, rc = -ESRCH);
4503 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
4504 lmm_oi_cpu_to_le(oi, oi);
4505 dt_read_lock(env, obj, 0);
4509 rc = lfsck_layout_get_lovea(env, obj, buf, &buflen);
4515 rc = lfsck_layout_verify_header(lmm);
4516 /* If the LOV EA crashed, then it is possible to be rebuilt later
4517 * when handle orphan OST-objects. */
4521 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0)
4522 GOTO(out, stripe = true);
4524 /* Inconsistent lmm_oi, should be repaired. */
4525 CDEBUG(D_LFSCK, "Repair bad lmm_oi for "DFID"\n",
4526 PFID(lfsck_dto2fid(obj)));
4528 if (bk->lb_param & LPF_DRYRUN) {
4529 down_write(&com->lc_sem);
4530 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4531 up_write(&com->lc_sem);
4533 GOTO(out, stripe = true);
4536 if (!lustre_handle_is_used(&lh)) {
4537 dt_read_unlock(env, obj);
4539 buf->lb_len = buflen;
4540 rc = lfsck_layout_lock(env, com, obj, &lh,
4541 MDS_INODELOCK_LAYOUT |
4542 MDS_INODELOCK_XATTR);
4546 handle = dt_trans_create(env, dev);
4548 GOTO(out, rc = PTR_ERR(handle));
4550 rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_LOV,
4551 LU_XATTR_REPLACE, handle);
4555 rc = dt_trans_start_local(env, dev, handle);
4559 dt_write_lock(env, obj, 0);
4566 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LOV,
4567 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
4571 down_write(&com->lc_sem);
4572 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4573 up_write(&com->lc_sem);
4575 GOTO(out, stripe = true);
4579 if (lustre_handle_is_used(&lh))
4580 dt_write_unlock(env, obj);
4582 dt_read_unlock(env, obj);
4585 if (handle != NULL && !IS_ERR(handle))
4586 dt_trans_stop(env, dev, handle);
4588 lfsck_layout_unlock(&lh);
4590 rc = lfsck_layout_scan_stripes(env, com, obj, lmm);
4592 down_write(&com->lc_sem);
4593 com->lc_new_checked++;
4595 lfsck_layout_record_failure(env, lfsck, lo);
4596 up_write(&com->lc_sem);
4598 buf->lb_len = buflen;
4603 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
4604 struct lfsck_component *com,
4605 struct dt_object *obj)
4607 struct lfsck_instance *lfsck = com->lc_lfsck;
4608 struct lfsck_layout *lo = com->lc_file_ram;
4609 const struct lu_fid *fid = lfsck_dto2fid(obj);
4610 struct lfsck_layout_slave_data *llsd = com->lc_data;
4611 struct lfsck_layout_seq *lls;
4617 LASSERT(llsd != NULL);
4619 lfsck_rbtree_update_bitmap(env, com, fid, false);
4621 down_write(&com->lc_sem);
4622 if (fid_is_idif(fid))
4624 else if (!fid_is_norm(fid) ||
4625 !fid_is_for_ostobj(env, lfsck->li_next, obj, fid))
4626 GOTO(unlock, rc = 0);
4629 com->lc_new_checked++;
4631 lls = lfsck_layout_seq_lookup(llsd, seq);
4634 if (unlikely(lls == NULL))
4635 GOTO(unlock, rc = -ENOMEM);
4637 INIT_LIST_HEAD(&lls->lls_list);
4639 rc = lfsck_layout_lastid_load(env, com, lls);
4641 lo->ll_objs_failed_phase1++;
4646 lfsck_layout_seq_insert(llsd, lls);
4649 if (unlikely(fid_is_last_id(fid)))
4650 GOTO(unlock, rc = 0);
4653 if (oid > lls->lls_lastid_known)
4654 lls->lls_lastid_known = oid;
4656 if (oid > lls->lls_lastid) {
4657 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
4658 /* OFD may create new objects during LFSCK scanning. */
4659 rc = lfsck_layout_lastid_reload(env, com, lls);
4660 if (unlikely(rc != 0))
4661 CWARN("%s: failed to reload LAST_ID for "LPX64
4663 lfsck_lfsck2name(com->lc_lfsck),
4665 if (oid <= lls->lls_lastid)
4666 GOTO(unlock, rc = 0);
4668 LASSERT(lfsck->li_out_notify != NULL);
4670 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4671 LE_LASTID_REBUILDING);
4672 lo->ll_flags |= LF_CRASHED_LASTID;
4675 lls->lls_lastid = oid;
4679 GOTO(unlock, rc = 0);
4682 up_write(&com->lc_sem);
4687 static int lfsck_layout_exec_dir(const struct lu_env *env,
4688 struct lfsck_component *com,
4689 struct dt_object *obj,
4690 struct lu_dirent *ent)
4695 static int lfsck_layout_master_post(const struct lu_env *env,
4696 struct lfsck_component *com,
4697 int result, bool init)
4699 struct lfsck_instance *lfsck = com->lc_lfsck;
4700 struct lfsck_layout *lo = com->lc_file_ram;
4701 struct lfsck_layout_master_data *llmd = com->lc_data;
4702 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4703 struct ptlrpc_thread *athread = &llmd->llmd_thread;
4704 struct l_wait_info lwi = { 0 };
4709 llmd->llmd_post_result = result;
4710 llmd->llmd_to_post = 1;
4711 if (llmd->llmd_post_result <= 0)
4712 llmd->llmd_exit = 1;
4714 wake_up_all(&athread->t_ctl_waitq);
4715 l_wait_event(mthread->t_ctl_waitq,
4716 (result > 0 && list_empty(&llmd->llmd_req_list)) ||
4717 thread_is_stopped(athread),
4720 if (llmd->llmd_assistant_status < 0)
4721 result = llmd->llmd_assistant_status;
4723 down_write(&com->lc_sem);
4724 spin_lock(&lfsck->li_lock);
4725 /* When LFSCK failed, there may be some prefetched objects those are
4726 * not been processed yet, we do not know the exactly position, then
4727 * just restart from last check-point next time. */
4728 if (!init && !llmd->llmd_exit)
4729 lo->ll_pos_last_checkpoint =
4730 lfsck->li_pos_current.lp_oit_cookie;
4733 lo->ll_status = LS_SCANNING_PHASE2;
4734 lo->ll_flags |= LF_SCANNED_ONCE;
4735 lo->ll_flags &= ~LF_UPGRADE;
4736 list_del_init(&com->lc_link);
4737 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
4738 } else if (result == 0) {
4739 lo->ll_status = lfsck->li_status;
4740 if (lo->ll_status == 0)
4741 lo->ll_status = LS_STOPPED;
4742 if (lo->ll_status != LS_PAUSED) {
4743 list_del_init(&com->lc_link);
4744 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
4747 lo->ll_status = LS_FAILED;
4748 list_del_init(&com->lc_link);
4749 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
4751 spin_unlock(&lfsck->li_lock);
4754 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4755 HALF_SEC - lfsck->li_time_last_checkpoint);
4756 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4757 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4758 com->lc_new_checked = 0;
4761 rc = lfsck_layout_store(env, com);
4762 up_write(&com->lc_sem);
4767 static int lfsck_layout_slave_post(const struct lu_env *env,
4768 struct lfsck_component *com,
4769 int result, bool init)
4771 struct lfsck_instance *lfsck = com->lc_lfsck;
4772 struct lfsck_layout *lo = com->lc_file_ram;
4776 rc = lfsck_layout_lastid_store(env, com);
4780 LASSERT(lfsck->li_out_notify != NULL);
4782 down_write(&com->lc_sem);
4784 spin_lock(&lfsck->li_lock);
4786 lo->ll_pos_last_checkpoint =
4787 lfsck->li_pos_current.lp_oit_cookie;
4789 lo->ll_status = LS_SCANNING_PHASE2;
4790 lo->ll_flags |= LF_SCANNED_ONCE;
4791 if (lo->ll_flags & LF_CRASHED_LASTID) {
4793 lo->ll_flags &= ~LF_CRASHED_LASTID;
4795 lo->ll_flags &= ~LF_UPGRADE;
4796 list_del_init(&com->lc_link);
4797 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
4798 } else if (result == 0) {
4799 lo->ll_status = lfsck->li_status;
4800 if (lo->ll_status == 0)
4801 lo->ll_status = LS_STOPPED;
4802 if (lo->ll_status != LS_PAUSED) {
4803 list_del_init(&com->lc_link);
4804 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
4807 lo->ll_status = LS_FAILED;
4808 list_del_init(&com->lc_link);
4809 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
4811 spin_unlock(&lfsck->li_lock);
4814 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4818 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4819 HALF_SEC - lfsck->li_time_last_checkpoint);
4820 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4821 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4822 com->lc_new_checked = 0;
4825 rc = lfsck_layout_store(env, com);
4827 up_write(&com->lc_sem);
4829 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
4832 lfsck_rbtree_cleanup(env, com);
4837 static int lfsck_layout_dump(const struct lu_env *env,
4838 struct lfsck_component *com, char *buf, int len)
4840 struct lfsck_instance *lfsck = com->lc_lfsck;
4841 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4842 struct lfsck_layout *lo = com->lc_file_ram;
4847 down_read(&com->lc_sem);
4848 rc = snprintf(buf, len,
4849 "name: lfsck_layout\n"
4855 lfsck_status2names(lo->ll_status));
4861 rc = lfsck_bits_dump(&buf, &len, lo->ll_flags, lfsck_flags_names,
4866 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
4871 rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_complete,
4872 "time_since_last_completed");
4876 rc = lfsck_time_dump(&buf, &len, lo->ll_time_latest_start,
4877 "time_since_latest_start");
4881 rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_checkpoint,
4882 "time_since_last_checkpoint");
4886 rc = snprintf(buf, len,
4887 "latest_start_position: "LPU64"\n"
4888 "last_checkpoint_position: "LPU64"\n"
4889 "first_failure_position: "LPU64"\n",
4890 lo->ll_pos_latest_start,
4891 lo->ll_pos_last_checkpoint,
4892 lo->ll_pos_first_inconsistent);
4899 rc = snprintf(buf, len,
4900 "success_count: %u\n"
4901 "repaired_dangling: "LPU64"\n"
4902 "repaired_unmatched_pair: "LPU64"\n"
4903 "repaired_multiple_referenced: "LPU64"\n"
4904 "repaired_orphan: "LPU64"\n"
4905 "repaired_inconsistent_owner: "LPU64"\n"
4906 "repaired_others: "LPU64"\n"
4907 "skipped: "LPU64"\n"
4908 "failed_phase1: "LPU64"\n"
4909 "failed_phase2: "LPU64"\n",
4910 lo->ll_success_count,
4911 lo->ll_objs_repaired[LLIT_DANGLING - 1],
4912 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
4913 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
4914 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
4915 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
4916 lo->ll_objs_repaired[LLIT_OTHERS - 1],
4917 lo->ll_objs_skipped,
4918 lo->ll_objs_failed_phase1,
4919 lo->ll_objs_failed_phase2);
4926 if (lo->ll_status == LS_SCANNING_PHASE1) {
4928 const struct dt_it_ops *iops;
4929 cfs_duration_t duration = cfs_time_current() -
4930 lfsck->li_time_last_checkpoint;
4931 __u64 checked = lo->ll_objs_checked_phase1 +
4932 com->lc_new_checked;
4933 __u64 speed = checked;
4934 __u64 new_checked = com->lc_new_checked * HZ;
4935 __u32 rtime = lo->ll_run_time_phase1 +
4936 cfs_duration_sec(duration + HALF_SEC);
4939 do_div(new_checked, duration);
4941 do_div(speed, rtime);
4942 rc = snprintf(buf, len,
4943 "checked_phase1: "LPU64"\n"
4944 "checked_phase2: "LPU64"\n"
4945 "run_time_phase1: %u seconds\n"
4946 "run_time_phase2: %u seconds\n"
4947 "average_speed_phase1: "LPU64" items/sec\n"
4948 "average_speed_phase2: N/A\n"
4949 "real-time_speed_phase1: "LPU64" items/sec\n"
4950 "real-time_speed_phase2: N/A\n",
4952 lo->ll_objs_checked_phase2,
4954 lo->ll_run_time_phase2,
4963 LASSERT(lfsck->li_di_oit != NULL);
4965 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4967 /* The low layer otable-based iteration position may NOT
4968 * exactly match the layout-based directory traversal
4969 * cookie. Generally, it is not a serious issue. But the
4970 * caller should NOT make assumption on that. */
4971 pos = iops->store(env, lfsck->li_di_oit);
4972 if (!lfsck->li_current_oit_processed)
4974 rc = snprintf(buf, len, "current_position: "LPU64"\n", pos);
4980 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
4981 cfs_duration_t duration = cfs_time_current() -
4982 lfsck->li_time_last_checkpoint;
4983 __u64 checked = lo->ll_objs_checked_phase2 +
4984 com->lc_new_checked;
4985 __u64 speed1 = lo->ll_objs_checked_phase1;
4986 __u64 speed2 = checked;
4987 __u64 new_checked = com->lc_new_checked * HZ;
4988 __u32 rtime = lo->ll_run_time_phase2 +
4989 cfs_duration_sec(duration + HALF_SEC);
4992 do_div(new_checked, duration);
4993 if (lo->ll_run_time_phase1 != 0)
4994 do_div(speed1, lo->ll_run_time_phase1);
4996 do_div(speed2, rtime);
4997 rc = snprintf(buf, len,
4998 "checked_phase1: "LPU64"\n"
4999 "checked_phase2: "LPU64"\n"
5000 "run_time_phase1: %u seconds\n"
5001 "run_time_phase2: %u seconds\n"
5002 "average_speed_phase1: "LPU64" items/sec\n"
5003 "average_speed_phase2: "LPU64" items/sec\n"
5004 "real-time_speed_phase1: N/A\n"
5005 "real-time_speed_phase2: "LPU64" items/sec\n"
5006 "current_position: "DFID"\n",
5007 lo->ll_objs_checked_phase1,
5009 lo->ll_run_time_phase1,
5014 PFID(&com->lc_fid_latest_scanned_phase2));
5021 __u64 speed1 = lo->ll_objs_checked_phase1;
5022 __u64 speed2 = lo->ll_objs_checked_phase2;
5024 if (lo->ll_run_time_phase1 != 0)
5025 do_div(speed1, lo->ll_run_time_phase1);
5026 if (lo->ll_run_time_phase2 != 0)
5027 do_div(speed2, lo->ll_run_time_phase2);
5028 rc = snprintf(buf, len,
5029 "checked_phase1: "LPU64"\n"
5030 "checked_phase2: "LPU64"\n"
5031 "run_time_phase1: %u seconds\n"
5032 "run_time_phase2: %u seconds\n"
5033 "average_speed_phase1: "LPU64" items/sec\n"
5034 "average_speed_phase2: "LPU64" objs/sec\n"
5035 "real-time_speed_phase1: N/A\n"
5036 "real-time_speed_phase2: N/A\n"
5037 "current_position: N/A\n",
5038 lo->ll_objs_checked_phase1,
5039 lo->ll_objs_checked_phase2,
5040 lo->ll_run_time_phase1,
5041 lo->ll_run_time_phase2,
5053 up_read(&com->lc_sem);
5058 static int lfsck_layout_master_double_scan(const struct lu_env *env,
5059 struct lfsck_component *com)
5061 struct lfsck_layout_master_data *llmd = com->lc_data;
5062 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
5063 struct ptlrpc_thread *athread = &llmd->llmd_thread;
5064 struct lfsck_layout *lo = com->lc_file_ram;
5065 struct l_wait_info lwi = { 0 };
5067 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
5070 llmd->llmd_to_double_scan = 1;
5071 wake_up_all(&athread->t_ctl_waitq);
5072 l_wait_event(mthread->t_ctl_waitq,
5073 llmd->llmd_in_double_scan ||
5074 thread_is_stopped(athread),
5076 if (llmd->llmd_assistant_status < 0)
5077 return llmd->llmd_assistant_status;
5082 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
5083 struct lfsck_component *com)
5085 struct lfsck_instance *lfsck = com->lc_lfsck;
5086 struct lfsck_layout_slave_data *llsd = com->lc_data;
5087 struct lfsck_layout *lo = com->lc_file_ram;
5088 struct ptlrpc_thread *thread = &lfsck->li_thread;
5092 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2)) {
5093 lfsck_rbtree_cleanup(env, com);
5094 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE, 0);
5098 atomic_inc(&lfsck->li_double_scan_count);
5100 com->lc_new_checked = 0;
5101 com->lc_new_scanned = 0;
5102 com->lc_time_last_checkpoint = cfs_time_current();
5103 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5104 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5107 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
5110 rc = lfsck_layout_slave_query_master(env, com);
5111 if (list_empty(&llsd->llsd_master_list)) {
5112 if (unlikely(!thread_is_running(thread)))
5123 rc = l_wait_event(thread->t_ctl_waitq,
5124 !thread_is_running(thread) ||
5125 list_empty(&llsd->llsd_master_list),
5127 if (unlikely(!thread_is_running(thread)))
5130 if (rc == -ETIMEDOUT)
5133 GOTO(done, rc = (rc < 0 ? rc : 1));
5137 rc = lfsck_layout_double_scan_result(env, com, rc);
5139 lfsck_rbtree_cleanup(env, com);
5140 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE, rc);
5141 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
5142 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5147 static void lfsck_layout_master_data_release(const struct lu_env *env,
5148 struct lfsck_component *com)
5150 struct lfsck_layout_master_data *llmd = com->lc_data;
5151 struct lfsck_instance *lfsck = com->lc_lfsck;
5152 struct lfsck_tgt_descs *ltds;
5153 struct lfsck_tgt_desc *ltd;
5154 struct lfsck_tgt_desc *next;
5156 LASSERT(llmd != NULL);
5157 LASSERT(thread_is_init(&llmd->llmd_thread) ||
5158 thread_is_stopped(&llmd->llmd_thread));
5159 LASSERT(list_empty(&llmd->llmd_req_list));
5161 com->lc_data = NULL;
5163 ltds = &lfsck->li_ost_descs;
5164 spin_lock(<ds->ltd_lock);
5165 list_for_each_entry_safe(ltd, next, &llmd->llmd_ost_phase1_list,
5166 ltd_layout_phase_list) {
5167 list_del_init(<d->ltd_layout_phase_list);
5169 list_for_each_entry_safe(ltd, next, &llmd->llmd_ost_phase2_list,
5170 ltd_layout_phase_list) {
5171 list_del_init(<d->ltd_layout_phase_list);
5173 list_for_each_entry_safe(ltd, next, &llmd->llmd_ost_list,
5175 list_del_init(<d->ltd_layout_list);
5177 list_for_each_entry_safe(ltd, next, &llmd->llmd_mdt_phase1_list,
5178 ltd_layout_phase_list) {
5179 list_del_init(<d->ltd_layout_phase_list);
5181 list_for_each_entry_safe(ltd, next, &llmd->llmd_mdt_phase2_list,
5182 ltd_layout_phase_list) {
5183 list_del_init(<d->ltd_layout_phase_list);
5185 list_for_each_entry_safe(ltd, next, &llmd->llmd_mdt_list,
5187 list_del_init(<d->ltd_layout_list);
5189 spin_unlock(<ds->ltd_lock);
5194 static void lfsck_layout_slave_data_release(const struct lu_env *env,
5195 struct lfsck_component *com)
5197 struct lfsck_layout_slave_data *llsd = com->lc_data;
5198 struct lfsck_layout_seq *lls;
5199 struct lfsck_layout_seq *next;
5200 struct lfsck_layout_slave_target *llst;
5201 struct lfsck_layout_slave_target *tmp;
5203 LASSERT(llsd != NULL);
5205 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
5207 list_del_init(&lls->lls_list);
5208 lfsck_object_put(env, lls->lls_lastid_obj);
5212 list_for_each_entry_safe(llst, tmp, &llsd->llsd_master_list,
5214 list_del_init(&llst->llst_list);
5218 lfsck_rbtree_cleanup(env, com);
5219 com->lc_data = NULL;
5223 static void lfsck_layout_master_quit(const struct lu_env *env,
5224 struct lfsck_component *com)
5226 struct lfsck_layout_master_data *llmd = com->lc_data;
5227 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
5228 struct ptlrpc_thread *athread = &llmd->llmd_thread;
5229 struct l_wait_info lwi = { 0 };
5231 llmd->llmd_exit = 1;
5232 wake_up_all(&athread->t_ctl_waitq);
5233 l_wait_event(mthread->t_ctl_waitq,
5234 thread_is_init(athread) ||
5235 thread_is_stopped(athread),
5239 static void lfsck_layout_slave_quit(const struct lu_env *env,
5240 struct lfsck_component *com)
5242 lfsck_rbtree_cleanup(env, com);
5245 static int lfsck_layout_master_in_notify(const struct lu_env *env,
5246 struct lfsck_component *com,
5247 struct lfsck_request *lr)
5249 struct lfsck_instance *lfsck = com->lc_lfsck;
5250 struct lfsck_layout *lo = com->lc_file_ram;
5251 struct lfsck_layout_master_data *llmd = com->lc_data;
5252 struct lfsck_tgt_descs *ltds;
5253 struct lfsck_tgt_desc *ltd;
5257 if (lr->lr_event == LE_PAIRS_VERIFY) {
5260 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
5266 if (lr->lr_event != LE_PHASE1_DONE &&
5267 lr->lr_event != LE_PHASE2_DONE &&
5268 lr->lr_event != LE_PEER_EXIT)
5271 if (lr->lr_flags & LEF_FROM_OST)
5272 ltds = &lfsck->li_ost_descs;
5274 ltds = &lfsck->li_mdt_descs;
5275 spin_lock(<ds->ltd_lock);
5276 ltd = LTD_TGT(ltds, lr->lr_index);
5278 spin_unlock(<ds->ltd_lock);
5283 list_del_init(<d->ltd_layout_phase_list);
5284 switch (lr->lr_event) {
5285 case LE_PHASE1_DONE:
5286 if (lr->lr_status <= 0) {
5287 ltd->ltd_layout_done = 1;
5288 list_del_init(<d->ltd_layout_list);
5289 CWARN("%s: %s %x failed/stopped at phase1: rc = %d.\n",
5290 lfsck_lfsck2name(lfsck),
5291 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
5292 ltd->ltd_index, lr->lr_status);
5293 lo->ll_flags |= LF_INCOMPLETE;
5298 if (lr->lr_flags & LEF_FROM_OST) {
5299 if (list_empty(<d->ltd_layout_list))
5300 list_add_tail(<d->ltd_layout_list,
5301 &llmd->llmd_ost_list);
5302 list_add_tail(<d->ltd_layout_phase_list,
5303 &llmd->llmd_ost_phase2_list);
5305 if (list_empty(<d->ltd_layout_list))
5306 list_add_tail(<d->ltd_layout_list,
5307 &llmd->llmd_mdt_list);
5308 list_add_tail(<d->ltd_layout_phase_list,
5309 &llmd->llmd_mdt_phase2_list);
5312 case LE_PHASE2_DONE:
5313 ltd->ltd_layout_done = 1;
5314 list_del_init(<d->ltd_layout_list);
5318 ltd->ltd_layout_done = 1;
5319 list_del_init(<d->ltd_layout_list);
5320 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
5321 CWARN("%s: the peer %s %x exit layout LFSCK.\n",
5322 lfsck_lfsck2name(lfsck),
5323 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
5325 lo->ll_flags |= LF_INCOMPLETE;
5331 spin_unlock(<ds->ltd_lock);
5333 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5334 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5336 memset(stop, 0, sizeof(*stop));
5337 stop->ls_status = lr->lr_status;
5338 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5339 lfsck_stop(env, lfsck->li_bottom, stop);
5340 } else if (lfsck_layout_master_to_orphan(llmd)) {
5341 wake_up_all(&llmd->llmd_thread.t_ctl_waitq);
5347 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
5348 struct lfsck_component *com,
5349 struct lfsck_request *lr)
5351 struct lfsck_instance *lfsck = com->lc_lfsck;
5352 struct lfsck_layout_slave_data *llsd = com->lc_data;
5353 struct lfsck_layout_slave_target *llst;
5357 switch (lr->lr_event) {
5358 case LE_FID_ACCESSED:
5359 lfsck_rbtree_update_bitmap(env, com, &lr->lr_fid, true);
5361 case LE_CONDITIONAL_DESTROY:
5362 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
5364 case LE_PAIRS_VERIFY: {
5365 lr->lr_status = LPVS_INIT;
5366 /* Firstly, if the MDT-object which is claimed via OST-object
5367 * local stored PFID xattr recognizes the OST-object, then it
5368 * must be that the client given PFID is wrong. */
5369 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5374 lr->lr_status = LPVS_INCONSISTENT;
5375 /* The OST-object local stored PFID xattr is stale. We need to
5376 * check whether the MDT-object that is claimed via the client
5377 * given PFID information recognizes the OST-object or not. If
5378 * matches, then need to update the OST-object's PFID xattr. */
5379 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5382 * We are not sure whether the client given PFID information
5383 * is correct or not, do nothing to avoid improper fixing.
5386 * The client given PFID information is also invalid, we can
5387 * NOT fix the OST-object inconsistency.
5392 lr->lr_status = LPVS_INCONSISTENT_TOFIX;
5393 rc = lfsck_layout_slave_repair_pfid(env, com, lr);
5397 case LE_PHASE2_DONE:
5404 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
5408 lfsck_layout_llst_put(llst);
5409 if (list_empty(&llsd->llsd_master_list))
5410 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5412 if (lr->lr_event == LE_PEER_EXIT &&
5413 lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5414 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5416 memset(stop, 0, sizeof(*stop));
5417 stop->ls_status = lr->lr_status;
5418 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5419 lfsck_stop(env, lfsck->li_bottom, stop);
5425 static int lfsck_layout_query(const struct lu_env *env,
5426 struct lfsck_component *com)
5428 struct lfsck_layout *lo = com->lc_file_ram;
5430 return lo->ll_status;
5433 static int lfsck_layout_master_stop_notify(const struct lu_env *env,
5434 struct lfsck_component *com,
5435 struct lfsck_tgt_descs *ltds,
5436 struct lfsck_tgt_desc *ltd,
5437 struct ptlrpc_request_set *set)
5439 struct lfsck_thread_info *info = lfsck_env_info(env);
5440 struct lfsck_async_interpret_args *laia = &info->lti_laia;
5441 struct lfsck_request *lr = &info->lti_lr;
5442 struct lfsck_instance *lfsck = com->lc_lfsck;
5445 spin_lock(<ds->ltd_lock);
5446 if (list_empty(<d->ltd_layout_list)) {
5447 LASSERT(list_empty(<d->ltd_layout_phase_list));
5448 spin_unlock(<ds->ltd_lock);
5453 list_del_init(<d->ltd_layout_phase_list);
5454 list_del_init(<d->ltd_layout_list);
5455 spin_unlock(<ds->ltd_lock);
5457 memset(lr, 0, sizeof(*lr));
5458 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
5459 lr->lr_event = LE_PEER_EXIT;
5460 lr->lr_active = LT_LAYOUT;
5461 lr->lr_status = LS_CO_PAUSED;
5462 if (ltds == &lfsck->li_ost_descs)
5463 lr->lr_flags = LEF_TO_OST;
5465 laia->laia_com = com;
5466 laia->laia_ltds = ltds;
5467 atomic_inc(<d->ltd_ref);
5468 laia->laia_ltd = ltd;
5470 laia->laia_shared = 0;
5472 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
5473 lfsck_layout_master_async_interpret,
5474 laia, LFSCK_NOTIFY);
5476 CERROR("%s: Fail to notify %s %x for co-stop: rc = %d\n",
5477 lfsck_lfsck2name(lfsck),
5478 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
5479 ltd->ltd_index, rc);
5486 /* with lfsck::li_lock held */
5487 static int lfsck_layout_slave_join(const struct lu_env *env,
5488 struct lfsck_component *com,
5489 struct lfsck_start_param *lsp)
5491 struct lfsck_instance *lfsck = com->lc_lfsck;
5492 struct lfsck_layout_slave_data *llsd = com->lc_data;
5493 struct lfsck_layout_slave_target *llst;
5494 struct lfsck_start *start = lsp->lsp_start;
5498 if (!lsp->lsp_index_valid || start == NULL ||
5499 !(start->ls_flags & LPF_ALL_TGT) ||
5500 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT))
5503 spin_unlock(&lfsck->li_lock);
5504 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5505 spin_lock(&lfsck->li_lock);
5506 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
5507 spin_unlock(&lfsck->li_lock);
5508 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
5511 lfsck_layout_llst_put(llst);
5512 spin_lock(&lfsck->li_lock);
5519 static struct lfsck_operations lfsck_layout_master_ops = {
5520 .lfsck_reset = lfsck_layout_reset,
5521 .lfsck_fail = lfsck_layout_fail,
5522 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
5523 .lfsck_prep = lfsck_layout_master_prep,
5524 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
5525 .lfsck_exec_dir = lfsck_layout_exec_dir,
5526 .lfsck_post = lfsck_layout_master_post,
5527 .lfsck_interpret = lfsck_layout_master_async_interpret,
5528 .lfsck_dump = lfsck_layout_dump,
5529 .lfsck_double_scan = lfsck_layout_master_double_scan,
5530 .lfsck_data_release = lfsck_layout_master_data_release,
5531 .lfsck_quit = lfsck_layout_master_quit,
5532 .lfsck_in_notify = lfsck_layout_master_in_notify,
5533 .lfsck_query = lfsck_layout_query,
5534 .lfsck_stop_notify = lfsck_layout_master_stop_notify,
5537 static struct lfsck_operations lfsck_layout_slave_ops = {
5538 .lfsck_reset = lfsck_layout_reset,
5539 .lfsck_fail = lfsck_layout_fail,
5540 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
5541 .lfsck_prep = lfsck_layout_slave_prep,
5542 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
5543 .lfsck_exec_dir = lfsck_layout_exec_dir,
5544 .lfsck_post = lfsck_layout_slave_post,
5545 .lfsck_dump = lfsck_layout_dump,
5546 .lfsck_double_scan = lfsck_layout_slave_double_scan,
5547 .lfsck_data_release = lfsck_layout_slave_data_release,
5548 .lfsck_quit = lfsck_layout_slave_quit,
5549 .lfsck_in_notify = lfsck_layout_slave_in_notify,
5550 .lfsck_query = lfsck_layout_query,
5551 .lfsck_join = lfsck_layout_slave_join,
5554 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
5556 struct lfsck_component *com;
5557 struct lfsck_layout *lo;
5558 struct dt_object *root = NULL;
5559 struct dt_object *obj;
5567 INIT_LIST_HEAD(&com->lc_link);
5568 INIT_LIST_HEAD(&com->lc_link_dir);
5569 init_rwsem(&com->lc_sem);
5570 atomic_set(&com->lc_ref, 1);
5571 com->lc_lfsck = lfsck;
5572 com->lc_type = LT_LAYOUT;
5573 if (lfsck->li_master) {
5574 struct lfsck_layout_master_data *llmd;
5576 com->lc_ops = &lfsck_layout_master_ops;
5577 OBD_ALLOC_PTR(llmd);
5579 GOTO(out, rc = -ENOMEM);
5581 INIT_LIST_HEAD(&llmd->llmd_req_list);
5582 spin_lock_init(&llmd->llmd_lock);
5583 INIT_LIST_HEAD(&llmd->llmd_ost_list);
5584 INIT_LIST_HEAD(&llmd->llmd_ost_phase1_list);
5585 INIT_LIST_HEAD(&llmd->llmd_ost_phase2_list);
5586 INIT_LIST_HEAD(&llmd->llmd_mdt_list);
5587 INIT_LIST_HEAD(&llmd->llmd_mdt_phase1_list);
5588 INIT_LIST_HEAD(&llmd->llmd_mdt_phase2_list);
5589 init_waitqueue_head(&llmd->llmd_thread.t_ctl_waitq);
5590 com->lc_data = llmd;
5592 struct lfsck_layout_slave_data *llsd;
5594 com->lc_ops = &lfsck_layout_slave_ops;
5595 OBD_ALLOC_PTR(llsd);
5597 GOTO(out, rc = -ENOMEM);
5599 INIT_LIST_HEAD(&llsd->llsd_seq_list);
5600 INIT_LIST_HEAD(&llsd->llsd_master_list);
5601 spin_lock_init(&llsd->llsd_lock);
5602 llsd->llsd_rb_root = RB_ROOT;
5603 rwlock_init(&llsd->llsd_rb_lock);
5604 com->lc_data = llsd;
5606 com->lc_file_size = sizeof(*lo);
5607 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
5608 if (com->lc_file_ram == NULL)
5609 GOTO(out, rc = -ENOMEM);
5611 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
5612 if (com->lc_file_disk == NULL)
5613 GOTO(out, rc = -ENOMEM);
5615 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
5617 GOTO(out, rc = PTR_ERR(root));
5619 if (unlikely(!dt_try_as_dir(env, root)))
5620 GOTO(out, rc = -ENOTDIR);
5622 obj = local_file_find_or_create(env, lfsck->li_los, root,
5624 S_IFREG | S_IRUGO | S_IWUSR);
5626 GOTO(out, rc = PTR_ERR(obj));
5629 rc = lfsck_layout_load(env, com);
5631 rc = lfsck_layout_reset(env, com, true);
5632 else if (rc == -ENOENT)
5633 rc = lfsck_layout_init(env, com);
5638 lo = com->lc_file_ram;
5639 switch (lo->ll_status) {
5645 spin_lock(&lfsck->li_lock);
5646 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
5647 spin_unlock(&lfsck->li_lock);
5650 CERROR("%s: unknown lfsck_layout status: rc = %u\n",
5651 lfsck_lfsck2name(lfsck), lo->ll_status);
5653 case LS_SCANNING_PHASE1:
5654 case LS_SCANNING_PHASE2:
5655 /* No need to store the status to disk right now.
5656 * If the system crashed before the status stored,
5657 * it will be loaded back when next time. */
5658 lo->ll_status = LS_CRASHED;
5659 lo->ll_flags |= LF_INCOMPLETE;
5666 spin_lock(&lfsck->li_lock);
5667 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
5668 spin_unlock(&lfsck->li_lock);
5672 if (lo->ll_flags & LF_CRASHED_LASTID) {
5673 LASSERT(lfsck->li_out_notify != NULL);
5675 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5676 LE_LASTID_REBUILDING);
5682 if (root != NULL && !IS_ERR(root))
5683 lu_object_put(env, &root->do_lu);
5686 lfsck_component_cleanup(env, com);
5691 struct lfsck_orphan_it {
5692 struct lfsck_component *loi_com;
5693 struct lfsck_rbtree_node *loi_lrn;
5694 struct lfsck_layout_slave_target *loi_llst;
5695 struct lu_fid loi_key;
5696 struct lu_orphan_rec loi_rec;
5698 unsigned int loi_over:1;
5701 static int lfsck_fid_match_idx(const struct lu_env *env,
5702 struct lfsck_instance *lfsck,
5703 const struct lu_fid *fid, int idx)
5705 struct seq_server_site *ss;
5706 struct lu_server_fld *sf;
5707 struct lu_seq_range range = { 0 };
5710 /* All abnormal cases will be returned to MDT0. */
5711 if (!fid_is_norm(fid)) {
5718 ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
5719 if (unlikely(ss == NULL))
5722 sf = ss->ss_server_fld;
5723 LASSERT(sf != NULL);
5725 fld_range_set_any(&range);
5726 rc = fld_server_lookup(env, sf, fid_seq(fid), &range);
5730 if (!fld_range_is_mdt(&range))
5733 if (range.lsr_index == idx)
5739 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
5740 struct dt_device *dev,
5741 struct dt_object *obj)
5743 struct thandle *handle;
5747 handle = dt_trans_create(env, dev);
5751 rc = dt_declare_ref_del(env, obj, handle);
5755 rc = dt_declare_destroy(env, obj, handle);
5759 rc = dt_trans_start_local(env, dev, handle);
5763 dt_write_lock(env, obj, 0);
5764 rc = dt_ref_del(env, obj, handle);
5766 rc = dt_destroy(env, obj, handle);
5767 dt_write_unlock(env, obj);
5772 dt_trans_stop(env, dev, handle);
5777 static int lfsck_orphan_index_lookup(const struct lu_env *env,
5778 struct dt_object *dt,
5780 const struct dt_key *key,
5781 struct lustre_capa *capa)
5786 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
5787 struct dt_object *dt,
5788 const struct dt_rec *rec,
5789 const struct dt_key *key,
5790 struct thandle *handle)
5795 static int lfsck_orphan_index_insert(const struct lu_env *env,
5796 struct dt_object *dt,
5797 const struct dt_rec *rec,
5798 const struct dt_key *key,
5799 struct thandle *handle,
5800 struct lustre_capa *capa,
5806 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
5807 struct dt_object *dt,
5808 const struct dt_key *key,
5809 struct thandle *handle)
5814 static int lfsck_orphan_index_delete(const struct lu_env *env,
5815 struct dt_object *dt,
5816 const struct dt_key *key,
5817 struct thandle *handle,
5818 struct lustre_capa *capa)
5823 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
5824 struct dt_object *dt,
5826 struct lustre_capa *capa)
5828 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
5829 struct lfsck_instance *lfsck;
5830 struct lfsck_component *com = NULL;
5831 struct lfsck_layout_slave_data *llsd;
5832 struct lfsck_orphan_it *it = NULL;
5836 lfsck = lfsck_instance_find(dev, true, false);
5837 if (unlikely(lfsck == NULL))
5838 RETURN(ERR_PTR(-ENXIO));
5840 com = lfsck_component_find(lfsck, LT_LAYOUT);
5841 if (unlikely(com == NULL))
5842 GOTO(out, rc = -ENOENT);
5844 llsd = com->lc_data;
5845 if (!llsd->llsd_rbtree_valid)
5846 GOTO(out, rc = -ESRCH);
5850 GOTO(out, rc = -ENOMEM);
5852 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
5853 if (it->loi_llst == NULL)
5854 GOTO(out, rc = -ENXIO);
5856 if (dev->dd_record_fid_accessed) {
5857 /* The first iteration against the rbtree, scan the whole rbtree
5858 * to remove the nodes which do NOT need to be handled. */
5859 write_lock(&llsd->llsd_rb_lock);
5860 if (dev->dd_record_fid_accessed) {
5861 struct rb_node *node;
5862 struct rb_node *next;
5863 struct lfsck_rbtree_node *lrn;
5865 /* No need to record the fid accessing anymore. */
5866 dev->dd_record_fid_accessed = 0;
5868 node = rb_first(&llsd->llsd_rb_root);
5869 while (node != NULL) {
5870 next = rb_next(node);
5871 lrn = rb_entry(node, struct lfsck_rbtree_node,
5873 if (atomic_read(&lrn->lrn_known_count) <=
5874 atomic_read(&lrn->lrn_accessed_count)) {
5875 rb_erase(node, &llsd->llsd_rb_root);
5876 lfsck_rbtree_free(lrn);
5881 write_unlock(&llsd->llsd_rb_lock);
5884 /* read lock the rbtree when init, and unlock when fini */
5885 read_lock(&llsd->llsd_rb_lock);
5893 lfsck_component_put(env, com);
5894 lfsck_instance_put(env, lfsck);
5899 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
5902 return (struct dt_it *)it;
5905 static void lfsck_orphan_it_fini(const struct lu_env *env,
5908 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5909 struct lfsck_component *com = it->loi_com;
5910 struct lfsck_layout_slave_data *llsd;
5911 struct lfsck_layout_slave_target *llst;
5914 llsd = com->lc_data;
5915 read_unlock(&llsd->llsd_rb_lock);
5916 llst = it->loi_llst;
5917 LASSERT(llst != NULL);
5919 /* Save the key and hash for iterate next. */
5920 llst->llst_fid = it->loi_key;
5921 llst->llst_hash = it->loi_hash;
5922 lfsck_layout_llst_put(llst);
5923 lfsck_component_put(env, com);
5929 * \retval +1: the iteration finished
5930 * \retval 0: on success, not finished
5931 * \retval -ve: on error
5933 static int lfsck_orphan_it_next(const struct lu_env *env,
5936 struct lfsck_thread_info *info = lfsck_env_info(env);
5937 struct filter_fid_old *pfid = &info->lti_old_pfid;
5938 struct lu_attr *la = &info->lti_la;
5939 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5940 struct lu_fid *key = &it->loi_key;
5941 struct lu_orphan_rec *rec = &it->loi_rec;
5942 struct lfsck_component *com = it->loi_com;
5943 struct lfsck_instance *lfsck = com->lc_lfsck;
5944 struct lfsck_layout_slave_data *llsd = com->lc_data;
5945 struct dt_object *obj;
5946 struct lfsck_rbtree_node *lrn;
5950 __u32 idx = it->loi_llst->llst_index;
5960 lrn = lfsck_rbtree_search(llsd, key, &exact);
5968 key->f_seq = lrn->lrn_seq;
5969 key->f_oid = lrn->lrn_first_oid;
5974 if (unlikely(key->f_oid == 0)) {
5981 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
5987 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
5988 atomic_read(&lrn->lrn_accessed_count))) {
5989 struct rb_node *next = rb_next(&lrn->lrn_node);
5991 while (next != NULL) {
5992 lrn = rb_entry(next, struct lfsck_rbtree_node,
5994 if (atomic_read(&lrn->lrn_known_count) >
5995 atomic_read(&lrn->lrn_accessed_count))
5997 next = rb_next(next);
6006 key->f_seq = lrn->lrn_seq;
6007 key->f_oid = lrn->lrn_first_oid;
6011 pos = key->f_oid - lrn->lrn_first_oid;
6014 pos = find_next_bit(lrn->lrn_known_bitmap,
6015 LFSCK_RBTREE_BITMAP_WIDTH, pos);
6016 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
6017 key->f_oid = lrn->lrn_first_oid + pos;
6018 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
6026 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
6031 key->f_oid = lrn->lrn_first_oid + pos;
6032 obj = lfsck_object_find(env, lfsck, key);
6035 if (rc == -ENOENT) {
6042 dt_read_lock(env, obj, 0);
6043 if (!dt_object_exists(obj)) {
6044 dt_read_unlock(env, obj);
6045 lfsck_object_put(env, obj);
6050 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
6054 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, pfid, sizeof(*pfid)),
6055 XATTR_NAME_FID, BYPASS_CAPA);
6056 if (rc == -ENODATA) {
6057 /* For the pre-created OST-object, update the bitmap to avoid
6058 * others LFSCK (second phase) iteration to touch it again. */
6059 if (la->la_ctime == 0) {
6060 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
6061 atomic_inc(&lrn->lrn_accessed_count);
6063 /* For the race between repairing dangling referenced
6064 * MDT-object and unlink the file, it may left orphan
6065 * OST-object there. Destroy it now! */
6066 if (unlikely(!(la->la_mode & S_ISUID))) {
6067 dt_read_unlock(env, obj);
6068 lfsck_layout_destroy_orphan(env,
6071 lfsck_object_put(env, obj);
6075 } else if (idx == 0) {
6076 /* If the orphan OST-object has no parent information,
6077 * regard it as referenced by the MDT-object on MDT0. */
6078 fid_zero(&rec->lor_fid);
6079 rec->lor_uid = la->la_uid;
6080 rec->lor_gid = la->la_gid;
6084 dt_read_unlock(env, obj);
6085 lfsck_object_put(env, obj);
6093 if (rc != sizeof(struct filter_fid) &&
6094 rc != sizeof(struct filter_fid_old))
6095 GOTO(out, rc = -EINVAL);
6097 fid_le_to_cpu(&rec->lor_fid, &pfid->ff_parent);
6098 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
6099 * MDT-object's FID::f_ver, instead it is the OST-object index in its
6100 * parent MDT-object's layout EA. */
6101 save = rec->lor_fid.f_stripe_idx;
6102 rec->lor_fid.f_ver = 0;
6103 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_fid, idx);
6104 /* If the orphan OST-object does not claim the MDT, then next.
6106 * If we do not know whether it matches or not, then return it
6107 * to the MDT for further check. */
6109 dt_read_unlock(env, obj);
6110 lfsck_object_put(env, obj);
6115 rec->lor_fid.f_stripe_idx = save;
6116 rec->lor_uid = la->la_uid;
6117 rec->lor_gid = la->la_gid;
6119 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u\n",
6120 lfsck_lfsck2name(com->lc_lfsck), PFID(key), PFID(&rec->lor_fid),
6121 rec->lor_uid, rec->lor_gid);
6126 dt_read_unlock(env, obj);
6127 lfsck_object_put(env, obj);
6135 * \retval +1: locate to the exactly position
6136 * \retval 0: cannot locate to the exactly position,
6137 * call next() to move to a valid position.
6138 * \retval -ve: on error
6140 static int lfsck_orphan_it_get(const struct lu_env *env,
6142 const struct dt_key *key)
6144 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6147 it->loi_key = *(struct lu_fid *)key;
6148 rc = lfsck_orphan_it_next(env, di);
6158 static void lfsck_orphan_it_put(const struct lu_env *env,
6163 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
6164 const struct dt_it *di)
6166 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6168 return (struct dt_key *)&it->loi_key;
6171 static int lfsck_orphan_it_key_size(const struct lu_env *env,
6172 const struct dt_it *di)
6174 return sizeof(struct lu_fid);
6177 static int lfsck_orphan_it_rec(const struct lu_env *env,
6178 const struct dt_it *di,
6182 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6184 *(struct lu_orphan_rec *)rec = it->loi_rec;
6189 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
6190 const struct dt_it *di)
6192 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6194 return it->loi_hash;
6198 * \retval +1: locate to the exactly position
6199 * \retval 0: cannot locate to the exactly position,
6200 * call next() to move to a valid position.
6201 * \retval -ve: on error
6203 static int lfsck_orphan_it_load(const struct lu_env *env,
6204 const struct dt_it *di,
6207 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6208 struct lfsck_layout_slave_target *llst = it->loi_llst;
6211 LASSERT(llst != NULL);
6213 if (hash != llst->llst_hash) {
6214 CWARN("%s: the given hash "LPU64" for orphan iteration does "
6215 "not match the one when fini "LPU64", to be reset.\n",
6216 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
6218 fid_zero(&llst->llst_fid);
6219 llst->llst_hash = 0;
6222 it->loi_key = llst->llst_fid;
6223 it->loi_hash = llst->llst_hash;
6224 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
6234 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
6235 const struct dt_it *di,
6241 const struct dt_index_operations lfsck_orphan_index_ops = {
6242 .dio_lookup = lfsck_orphan_index_lookup,
6243 .dio_declare_insert = lfsck_orphan_index_declare_insert,
6244 .dio_insert = lfsck_orphan_index_insert,
6245 .dio_declare_delete = lfsck_orphan_index_declare_delete,
6246 .dio_delete = lfsck_orphan_index_delete,
6248 .init = lfsck_orphan_it_init,
6249 .fini = lfsck_orphan_it_fini,
6250 .get = lfsck_orphan_it_get,
6251 .put = lfsck_orphan_it_put,
6252 .next = lfsck_orphan_it_next,
6253 .key = lfsck_orphan_it_key,
6254 .key_size = lfsck_orphan_it_key_size,
6255 .rec = lfsck_orphan_it_rec,
6256 .store = lfsck_orphan_it_store,
6257 .load = lfsck_orphan_it_load,
6258 .key_rec = lfsck_orphan_it_key_rec,