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) 2014, 2016, 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_fid.h>
43 #include <lustre_lib.h>
44 #include <lustre_net.h>
45 #include <lustre/lustre_user.h>
46 #include <md_object.h>
47 #include <obd_class.h>
49 #include "lfsck_internal.h"
51 #define LFSCK_LAYOUT_MAGIC_V1 0xB173AE14
52 #define LFSCK_LAYOUT_MAGIC_V2 0xB1734D76
53 #define LFSCK_LAYOUT_MAGIC_V3 0xB17371B9
55 #define LFSCK_LAYOUT_MAGIC LFSCK_LAYOUT_MAGIC_V3
57 struct lfsck_layout_seq {
58 struct list_head lls_list;
61 __u64 lls_lastid_known;
62 struct dt_object *lls_lastid_obj;
63 unsigned int lls_dirty:1;
66 struct lfsck_layout_slave_target {
67 /* link into lfsck_layout_slave_data::llsd_master_list. */
68 struct list_head llst_list;
69 /* The position for next record in the rbtree for iteration. */
70 struct lu_fid llst_fid;
71 /* Dummy hash for iteration against the rbtree. */
76 /* How many times we have failed to get the master status. */
80 struct lfsck_layout_slave_data {
81 /* list for lfsck_layout_seq */
82 struct list_head llsd_seq_list;
84 /* list for the masters involve layout verification. */
85 struct list_head llsd_master_list;
88 struct dt_object *llsd_rb_obj;
89 struct rb_root llsd_rb_root;
90 rwlock_t llsd_rb_lock;
91 unsigned int llsd_rbtree_valid:1;
94 struct lfsck_layout_slave_async_args {
95 struct obd_export *llsaa_exp;
96 struct lfsck_component *llsaa_com;
97 struct lfsck_layout_slave_target *llsaa_llst;
100 static inline bool lfsck_comp_extent_aligned(__u64 size)
102 return (size & (LOV_MIN_STRIPE_SIZE - 1)) == 0;
106 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
108 if (atomic_dec_and_test(&llst->llst_ref)) {
109 LASSERT(list_empty(&llst->llst_list));
116 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
118 struct lfsck_layout_slave_target *llst;
119 struct lfsck_layout_slave_target *tmp;
126 INIT_LIST_HEAD(&llst->llst_list);
128 llst->llst_index = index;
129 atomic_set(&llst->llst_ref, 1);
131 spin_lock(&llsd->llsd_lock);
132 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
133 if (tmp->llst_index == index) {
139 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
140 spin_unlock(&llsd->llsd_lock);
149 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
150 struct lfsck_layout_slave_target *llst)
154 spin_lock(&llsd->llsd_lock);
155 if (!list_empty(&llst->llst_list)) {
156 list_del_init(&llst->llst_list);
159 spin_unlock(&llsd->llsd_lock);
162 lfsck_layout_llst_put(llst);
165 static inline struct lfsck_layout_slave_target *
166 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
167 __u32 index, bool unlink)
169 struct lfsck_layout_slave_target *llst;
171 spin_lock(&llsd->llsd_lock);
172 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
173 if (llst->llst_index == index) {
175 list_del_init(&llst->llst_list);
177 atomic_inc(&llst->llst_ref);
178 spin_unlock(&llsd->llsd_lock);
183 spin_unlock(&llsd->llsd_lock);
188 static struct lfsck_layout_req *
189 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
190 struct dt_object *child, __u32 comp_id,
191 __u32 ost_idx, __u32 lov_idx)
193 struct lfsck_layout_req *llr;
197 return ERR_PTR(-ENOMEM);
199 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
200 llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
201 llr->llr_child = child;
202 llr->llr_comp_id = comp_id;
203 llr->llr_ost_idx = ost_idx;
204 llr->llr_lov_idx = lov_idx;
209 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
210 struct lfsck_assistant_req *lar)
212 struct lfsck_layout_req *llr =
213 container_of0(lar, struct lfsck_layout_req, llr_lar);
215 lfsck_object_put(env, llr->llr_child);
216 lfsck_assistant_object_put(env, lar->lar_parent);
221 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
222 struct ptlrpc_request *req,
226 struct lfsck_async_interpret_args *laia = args;
227 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
229 ltd->ltd_synced_failures = 1;
230 atomic_dec(laia->laia_count);
237 * Notify remote LFSCK instances about former failures.
239 * The local LFSCK instance has recorded which OSTs have ever failed to respond
240 * some LFSCK verification requests (maybe because of network issues or the OST
241 * itself trouble). During the respond gap, the OST may missed some OST-objects
242 * verification, then the OST cannot know whether related OST-objects have been
243 * referenced by related MDT-objects or not, then in the second-stage scanning,
244 * these OST-objects will be regarded as orphan, if the OST-object contains bad
245 * parent FID for back reference, then it will misguide the LFSCK to make wrong
246 * fixing for the fake orphan.
248 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
249 * it will scan the bitmap for the ever failed OSTs, and notify them that they
250 * have ever missed some OST-object verification and should skip the handling
251 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
253 * \param[in] env pointer to the thread context
254 * \param[in] com pointer to the lfsck component
255 * \param[in] lr pointer to the lfsck request
257 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
258 struct lfsck_component *com,
259 struct lfsck_request *lr)
261 struct lfsck_async_interpret_args *laia =
262 &lfsck_env_info(env)->lti_laia2;
263 struct lfsck_assistant_data *lad = com->lc_data;
264 struct lfsck_layout *lo = com->lc_file_ram;
265 struct lfsck_instance *lfsck = com->lc_lfsck;
266 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
267 struct lfsck_tgt_desc *ltd;
268 struct ptlrpc_request_set *set;
274 if (!lad->lad_incomplete)
277 /* If the MDT has ever failed to verfiy some OST-objects,
278 * then sync failures with them firstly. */
279 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
281 atomic_set(&count, 0);
282 memset(laia, 0, sizeof(*laia));
283 laia->laia_count = &count;
284 set = ptlrpc_prep_set();
286 GOTO(out, rc = -ENOMEM);
288 down_read(<ds->ltd_rw_sem);
289 cfs_foreach_bit(lad->lad_bitmap, idx) {
290 ltd = lfsck_ltd2tgt(ltds, idx);
291 LASSERT(ltd != NULL);
293 laia->laia_ltd = ltd;
294 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
295 lfsck_layout_assistant_sync_failures_interpret,
298 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
299 "notify target %x for %s phase1 done: "
300 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
301 ltd->ltd_index, lad->lad_name, rc);
308 up_read(<ds->ltd_rw_sem);
310 if (rc == 0 && atomic_read(&count) > 0)
311 rc = ptlrpc_set_wait(set);
313 ptlrpc_set_destroy(set);
315 if (rc == 0 && atomic_read(&count) > 0)
322 /* If failed to sync failures with the OSTs, then have to
323 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
324 * subsequent orphan OST-object handling. */
325 lo->ll_flags |= LF_INCOMPLETE;
327 lr->lr_flags2 = lo->ll_flags;
330 static int lfsck_layout_verify_header_v1v3(struct dt_object *obj,
331 struct lov_mds_md_v1 *lmm)
336 magic = le32_to_cpu(lmm->lmm_magic);
337 /* If magic crashed, keep it there. Sometime later, during OST-object
338 * orphan handling, if some OST-object(s) back-point to it, it can be
339 * verified and repaired. */
340 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
343 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
348 CDEBUG(D_LFSCK, "%s LOV EA magic %u for the file "DFID"\n",
349 rc == -EINVAL ? "Unknown" : "Unsupported",
350 magic, PFID(lfsck_dto2fid(obj)));
355 pattern = le32_to_cpu(lmm->lmm_pattern);
356 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
357 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
358 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
359 DFID"\n", pattern, PFID(lfsck_dto2fid(obj)));
367 static int lfsck_layout_verify_header(struct dt_object *obj,
368 struct lov_mds_md_v1 *lmm)
372 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
373 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
375 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
377 if (unlikely(count == 0)) {
378 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
379 "components count 0\n",
380 PFID(lfsck_dto2fid(obj)));
385 for (i = 0; i < count; i++) {
386 struct lov_comp_md_entry_v1 *lcme =
387 &lcm->lcm_entries[i];
388 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
389 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
390 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
392 if (unlikely(comp_id == LCME_ID_INVAL ||
393 comp_id > LCME_ID_MAX)) {
394 CDEBUG(D_LFSCK, "found invalid FPL ID %u "
395 "for the file "DFID" at idx %d\n",
396 comp_id, PFID(lfsck_dto2fid(obj)), i);
401 if (unlikely(start >= end ||
402 !lfsck_comp_extent_aligned(start) ||
403 (!lfsck_comp_extent_aligned(end) &&
404 end != LUSTRE_EOF))) {
405 CDEBUG(D_LFSCK, "found invalid FPL extent "
406 "range [%llu - %llu) for the file "
408 start, end, PFID(lfsck_dto2fid(obj)), i);
413 rc = lfsck_layout_verify_header_v1v3(obj,
414 (struct lov_mds_md_v1 *)((char *)lmm +
415 le32_to_cpu(lcme->lcme_offset)));
420 rc = lfsck_layout_verify_header_v1v3(obj, lmm);
426 static int lfsck_layout_get_lovea(const struct lu_env *env,
427 struct dt_object *obj, struct lu_buf *buf)
433 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
435 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
439 lu_buf_realloc(buf, rc);
440 if (buf->lb_buf == NULL)
452 if (unlikely(buf->lb_buf == NULL)) {
453 lu_buf_alloc(buf, rc);
454 if (buf->lb_buf == NULL)
460 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf);
462 return rc1 ? rc1 : rc;
465 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
466 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
467 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
469 struct lfsck_rbtree_node {
470 struct rb_node lrn_node;
473 atomic_t lrn_known_count;
474 atomic_t lrn_accessed_count;
475 void *lrn_known_bitmap;
476 void *lrn_accessed_bitmap;
479 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
480 __u64 seq, __u32 oid)
482 if (seq < lrn->lrn_seq)
485 if (seq > lrn->lrn_seq)
488 if (oid < lrn->lrn_first_oid)
491 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
497 /* The caller should hold llsd->llsd_rb_lock. */
498 static struct lfsck_rbtree_node *
499 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
500 const struct lu_fid *fid, bool *exact)
502 struct rb_node *node = llsd->llsd_rb_root.rb_node;
503 struct rb_node *prev = NULL;
504 struct lfsck_rbtree_node *lrn = NULL;
510 while (node != NULL) {
512 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
513 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
515 node = node->rb_left;
517 node = node->rb_right;
525 /* If there is no exactly matched one, then to the next valid one. */
528 /* The rbtree is empty. */
535 node = rb_next(prev);
537 /* The end of the rbtree. */
541 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
546 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
547 const struct lu_fid *fid)
549 struct lfsck_rbtree_node *lrn;
553 return ERR_PTR(-ENOMEM);
555 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
556 if (lrn->lrn_known_bitmap == NULL) {
559 return ERR_PTR(-ENOMEM);
562 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
563 if (lrn->lrn_accessed_bitmap == NULL) {
564 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
567 return ERR_PTR(-ENOMEM);
570 RB_CLEAR_NODE(&lrn->lrn_node);
571 lrn->lrn_seq = fid_seq(fid);
572 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
573 atomic_set(&lrn->lrn_known_count, 0);
574 atomic_set(&lrn->lrn_accessed_count, 0);
579 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
581 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
582 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
586 /* The caller should hold lock. */
587 static struct lfsck_rbtree_node *
588 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
589 struct lfsck_rbtree_node *lrn)
591 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
592 struct rb_node *parent = NULL;
593 struct lfsck_rbtree_node *tmp;
596 while (*pos != NULL) {
598 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
599 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
601 pos = &(*pos)->rb_left;
603 pos = &(*pos)->rb_right;
608 rb_link_node(&lrn->lrn_node, parent, pos);
609 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
614 extern const struct dt_index_operations lfsck_orphan_index_ops;
616 static int lfsck_rbtree_setup(const struct lu_env *env,
617 struct lfsck_component *com)
619 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
620 struct lfsck_instance *lfsck = com->lc_lfsck;
621 struct dt_device *dev = lfsck->li_bottom;
622 struct lfsck_layout_slave_data *llsd = com->lc_data;
623 struct dt_object *obj;
625 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
626 fid->f_oid = lfsck_dev_idx(lfsck);
628 obj = dt_locate(env, dev, fid);
630 RETURN(PTR_ERR(obj));
632 /* Generate an in-RAM object to stand for the layout rbtree.
633 * Scanning the layout rbtree will be via the iteration over
634 * the object. In the future, the rbtree may be written onto
635 * disk with the object.
637 * Mark the object to be as exist. */
638 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
639 obj->do_index_ops = &lfsck_orphan_index_ops;
640 llsd->llsd_rb_obj = obj;
641 llsd->llsd_rbtree_valid = 1;
642 dev->dd_record_fid_accessed = 1;
644 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
645 lfsck_lfsck2name(lfsck));
650 static void lfsck_rbtree_cleanup(const struct lu_env *env,
651 struct lfsck_component *com)
653 struct lfsck_instance *lfsck = com->lc_lfsck;
654 struct lfsck_layout_slave_data *llsd = com->lc_data;
655 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
656 struct rb_node *next;
657 struct lfsck_rbtree_node *lrn;
659 lfsck->li_bottom->dd_record_fid_accessed = 0;
660 /* Invalid the rbtree, then no others will use it. */
661 write_lock(&llsd->llsd_rb_lock);
662 llsd->llsd_rbtree_valid = 0;
663 write_unlock(&llsd->llsd_rb_lock);
665 while (node != NULL) {
666 next = rb_next(node);
667 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
668 rb_erase(node, &llsd->llsd_rb_root);
669 lfsck_rbtree_free(lrn);
673 if (llsd->llsd_rb_obj != NULL) {
674 lfsck_object_put(env, llsd->llsd_rb_obj);
675 llsd->llsd_rb_obj = NULL;
678 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
679 lfsck_lfsck2name(lfsck));
682 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
683 struct lfsck_component *com,
684 const struct lu_fid *fid,
687 struct lfsck_layout_slave_data *llsd = com->lc_data;
688 struct lfsck_rbtree_node *lrn;
694 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
697 if (!fid_is_idif(fid) && !fid_is_norm(fid))
700 read_lock(&llsd->llsd_rb_lock);
701 if (!llsd->llsd_rbtree_valid)
702 GOTO(unlock, rc = 0);
704 lrn = lfsck_rbtree_search(llsd, fid, NULL);
706 struct lfsck_rbtree_node *tmp;
710 read_unlock(&llsd->llsd_rb_lock);
711 tmp = lfsck_rbtree_new(env, fid);
713 GOTO(out, rc = PTR_ERR(tmp));
716 write_lock(&llsd->llsd_rb_lock);
717 if (!llsd->llsd_rbtree_valid) {
718 lfsck_rbtree_free(tmp);
719 GOTO(unlock, rc = 0);
722 lrn = lfsck_rbtree_insert(llsd, tmp);
724 lfsck_rbtree_free(tmp);
727 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
728 /* Any accessed object must be a known object. */
729 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
730 atomic_inc(&lrn->lrn_known_count);
731 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
732 atomic_inc(&lrn->lrn_accessed_count);
734 GOTO(unlock, rc = 0);
738 write_unlock(&llsd->llsd_rb_lock);
740 read_unlock(&llsd->llsd_rb_lock);
742 if (rc != 0 && accessed) {
743 struct lfsck_layout *lo = com->lc_file_ram;
745 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
746 "bitmap, and will cause incorrect LFSCK OST-object "
747 "handling, so disable it to cancel orphan handling "
748 "for related device. rc = %d\n",
749 lfsck_lfsck2name(com->lc_lfsck), rc);
751 lo->ll_flags |= LF_INCOMPLETE;
752 lfsck_rbtree_cleanup(env, com);
756 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
757 const struct lfsck_layout_dangling_key *src)
759 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
760 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
761 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
764 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
765 const struct lfsck_layout_dangling_key *src)
767 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
768 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
769 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
772 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
773 const struct lfsck_layout_dangling_key *src)
775 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
776 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
777 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
780 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
781 const struct lfsck_layout_dangling_key *src)
783 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
784 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
785 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
788 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
789 const struct lfsck_layout *src)
793 des->ll_magic = le32_to_cpu(src->ll_magic);
794 des->ll_status = le32_to_cpu(src->ll_status);
795 des->ll_flags = le32_to_cpu(src->ll_flags);
796 des->ll_success_count = le32_to_cpu(src->ll_success_count);
797 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
798 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
799 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
800 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
801 des->ll_time_last_checkpoint =
802 le64_to_cpu(src->ll_time_last_checkpoint);
803 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
804 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
805 des->ll_pos_first_inconsistent =
806 le64_to_cpu(src->ll_pos_first_inconsistent);
807 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
808 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
809 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
810 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
811 for (i = 0; i < LLIT_MAX; i++)
812 des->ll_objs_repaired[i] =
813 le64_to_cpu(src->ll_objs_repaired[i]);
814 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
815 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
816 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
817 &src->ll_lldk_latest_scanned_phase2);
820 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
821 const struct lfsck_layout *src)
825 des->ll_magic = cpu_to_le32(src->ll_magic);
826 des->ll_status = cpu_to_le32(src->ll_status);
827 des->ll_flags = cpu_to_le32(src->ll_flags);
828 des->ll_success_count = cpu_to_le32(src->ll_success_count);
829 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
830 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
831 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
832 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
833 des->ll_time_last_checkpoint =
834 cpu_to_le64(src->ll_time_last_checkpoint);
835 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
836 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
837 des->ll_pos_first_inconsistent =
838 cpu_to_le64(src->ll_pos_first_inconsistent);
839 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
840 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
841 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
842 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
843 for (i = 0; i < LLIT_MAX; i++)
844 des->ll_objs_repaired[i] =
845 cpu_to_le64(src->ll_objs_repaired[i]);
846 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
847 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
848 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
849 &src->ll_lldk_latest_scanned_phase2);
853 * Load the OST bitmap from the lfsck_layout trace file.
855 * \param[in] env pointer to the thread context
856 * \param[in] com pointer to the lfsck component
858 * \retval 0 for success
859 * \retval negative error number on failure or data corruption
861 static int lfsck_layout_load_bitmap(const struct lu_env *env,
862 struct lfsck_component *com)
864 struct dt_object *obj = com->lc_obj;
865 struct lfsck_assistant_data *lad = com->lc_data;
866 struct lfsck_layout *lo = com->lc_file_ram;
867 struct cfs_bitmap *bitmap = lad->lad_bitmap;
868 loff_t pos = com->lc_file_size;
874 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
876 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
878 nbits = lo->ll_bitmap_size;
880 if (unlikely(nbits < BITS_PER_LONG))
881 nbits = BITS_PER_LONG;
883 if (nbits > bitmap->size) {
884 __u32 new_bits = bitmap->size;
885 struct cfs_bitmap *new_bitmap;
887 while (new_bits < nbits)
890 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
891 if (new_bitmap == NULL)
894 lad->lad_bitmap = new_bitmap;
895 CFS_FREE_BITMAP(bitmap);
899 if (lo->ll_bitmap_size == 0) {
900 lad->lad_incomplete = 0;
901 CFS_RESET_BITMAP(bitmap);
906 size = (lo->ll_bitmap_size + 7) >> 3;
907 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
909 RETURN(rc >= 0 ? -EINVAL : rc);
911 if (cfs_bitmap_check_empty(bitmap))
912 lad->lad_incomplete = 0;
914 lad->lad_incomplete = 1;
920 * Load the layout LFSCK trace file from disk.
922 * The layout LFSCK trace file records the layout LFSCK status information
923 * and other statistics, such as how many objects have been scanned, and how
924 * many objects have been repaired, and etc. It also contains the bitmap for
925 * failed OSTs during the layout LFSCK. All these information will be loaded
926 * from disk to RAM when the layout LFSCK component setup.
928 * \param[in] env pointer to the thread context
929 * \param[in] com pointer to the lfsck component
931 * \retval positive number for file data corruption, the caller
932 * should reset the layout LFSCK trace file
933 * \retval 0 for success
934 * \retval negative error number on failure
936 static int lfsck_layout_load(const struct lu_env *env,
937 struct lfsck_component *com)
939 struct lfsck_layout *lo = com->lc_file_ram;
940 ssize_t size = com->lc_file_size;
944 rc = dt_read(env, com->lc_obj,
945 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
949 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
950 lfsck_lfsck2name(com->lc_lfsck), rc);
952 } else if (rc != size) {
953 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
954 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
958 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
959 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
960 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
961 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
962 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
970 * Store the layout LFSCK trace file on disk.
972 * The layout LFSCK trace file records the layout LFSCK status information
973 * and other statistics, such as how many objects have been scanned, and how
974 * many objects have been repaired, and etc. It also contains the bitmap for
975 * failed OSTs during the layout LFSCK. All these information will be synced
976 * from RAM to disk periodically.
978 * \param[in] env pointer to the thread context
979 * \param[in] com pointer to the lfsck component
981 * \retval 0 for success
982 * \retval negative error number on failure
984 static int lfsck_layout_store(const struct lu_env *env,
985 struct lfsck_component *com)
987 struct dt_object *obj = com->lc_obj;
988 struct lfsck_instance *lfsck = com->lc_lfsck;
989 struct lfsck_layout *lo_ram = com->lc_file_ram;
990 struct lfsck_layout *lo = com->lc_file_disk;
992 struct dt_device *dev = lfsck_obj2dev(obj);
993 struct cfs_bitmap *bitmap = NULL;
995 ssize_t size = com->lc_file_size;
1000 if (lfsck->li_master) {
1001 struct lfsck_assistant_data *lad = com->lc_data;
1003 bitmap = lad->lad_bitmap;
1004 nbits = bitmap->size;
1007 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1010 lo_ram->ll_bitmap_size = nbits;
1011 lfsck_layout_cpu_to_le(lo, lo_ram);
1012 th = dt_trans_create(env, dev);
1014 GOTO(log, rc = PTR_ERR(th));
1016 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1021 if (bitmap != NULL) {
1022 rc = dt_declare_record_write(env, obj,
1023 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1029 rc = dt_trans_start_local(env, dev, th);
1034 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1038 if (bitmap != NULL) {
1040 rc = dt_record_write(env, obj,
1041 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1048 dt_trans_stop(env, dev, th);
1052 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1053 lfsck_lfsck2name(lfsck), rc);
1058 static int lfsck_layout_init(const struct lu_env *env,
1059 struct lfsck_component *com)
1061 struct lfsck_layout *lo = com->lc_file_ram;
1064 memset(lo, 0, com->lc_file_size);
1065 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1066 lo->ll_status = LS_INIT;
1067 down_write(&com->lc_sem);
1068 rc = lfsck_layout_store(env, com);
1069 if (rc == 0 && com->lc_lfsck->li_master)
1070 rc = lfsck_load_sub_trace_files(env, com,
1071 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1072 up_write(&com->lc_sem);
1077 static int fid_is_for_ostobj(const struct lu_env *env,
1078 struct lfsck_instance *lfsck,
1079 struct dt_object *obj, const struct lu_fid *fid)
1081 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1082 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1083 struct lustre_ost_attrs *loa;
1086 fld_range_set_any(range);
1087 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1089 if (fld_range_is_ost(range))
1095 loa = &lfsck_env_info(env)->lti_loa;
1096 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1098 if (rc >= sizeof(struct lustre_mdt_attrs)) {
1099 lustre_lma_swab(&loa->loa_lma);
1101 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1104 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1109 static struct lfsck_layout_seq *
1110 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1112 struct lfsck_layout_seq *lls;
1114 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1115 if (lls->lls_seq == seq)
1118 if (lls->lls_seq > seq)
1126 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1127 struct lfsck_layout_seq *lls)
1129 struct lfsck_layout_seq *tmp;
1130 struct list_head *pos = &llsd->llsd_seq_list;
1132 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1133 if (lls->lls_seq < tmp->lls_seq) {
1134 pos = &tmp->lls_list;
1138 list_add_tail(&lls->lls_list, pos);
1142 lfsck_layout_lastid_create(const struct lu_env *env,
1143 struct lfsck_instance *lfsck,
1144 struct dt_object *obj)
1146 struct lfsck_thread_info *info = lfsck_env_info(env);
1147 struct lu_attr *la = &info->lti_la;
1148 struct dt_object_format *dof = &info->lti_dof;
1149 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1150 struct dt_device *dt = lfsck_obj2dev(obj);
1157 if (bk->lb_param & LPF_DRYRUN)
1160 memset(la, 0, sizeof(*la));
1161 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1162 la->la_valid = LA_MODE | LA_UID | LA_GID;
1163 memset(dof, 0, sizeof(*dof));
1164 dof->dof_type = dt_mode_to_dft(S_IFREG);
1166 th = dt_trans_create(env, dt);
1168 GOTO(log, rc = PTR_ERR(th));
1170 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1174 rc = dt_declare_record_write(env, obj,
1175 lfsck_buf_get(env, &lastid,
1181 rc = dt_trans_start_local(env, dt, th);
1185 dt_write_lock(env, obj, 0);
1186 if (likely(dt_object_exists(obj) == 0)) {
1187 rc = dt_create(env, obj, la, NULL, dof, th);
1189 rc = dt_record_write(env, obj,
1190 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1193 dt_write_unlock(env, obj);
1198 dt_trans_stop(env, dt, th);
1201 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1203 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1209 lfsck_layout_lastid_reload(const struct lu_env *env,
1210 struct lfsck_component *com,
1211 struct lfsck_layout_seq *lls)
1217 dt_read_lock(env, lls->lls_lastid_obj, 0);
1218 rc = dt_record_read(env, lls->lls_lastid_obj,
1219 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1220 dt_read_unlock(env, lls->lls_lastid_obj);
1221 if (unlikely(rc != 0))
1224 lastid = le64_to_cpu(lastid);
1225 if (lastid < lls->lls_lastid_known) {
1226 struct lfsck_instance *lfsck = com->lc_lfsck;
1227 struct lfsck_layout *lo = com->lc_file_ram;
1229 lls->lls_lastid = lls->lls_lastid_known;
1231 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1232 LASSERT(lfsck->li_out_notify != NULL);
1234 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1235 LE_LASTID_REBUILDING);
1236 lo->ll_flags |= LF_CRASHED_LASTID;
1238 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1239 "LAST_ID file (1) for the sequence %#llx"
1240 ", old value %llu, known value %llu\n",
1241 lfsck_lfsck2name(lfsck), lls->lls_seq,
1242 lastid, lls->lls_lastid);
1244 } else if (lastid >= lls->lls_lastid) {
1245 lls->lls_lastid = lastid;
1253 lfsck_layout_lastid_store(const struct lu_env *env,
1254 struct lfsck_component *com)
1256 struct lfsck_instance *lfsck = com->lc_lfsck;
1257 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1258 struct dt_device *dt = lfsck->li_bottom;
1259 struct lfsck_layout_slave_data *llsd = com->lc_data;
1260 struct lfsck_layout_seq *lls;
1266 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1269 if (!lls->lls_dirty)
1272 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1273 "<seq> %#llx as <oid> %llu\n",
1274 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1276 if (bk->lb_param & LPF_DRYRUN) {
1281 th = dt_trans_create(env, dt);
1284 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1285 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1286 lfsck_lfsck2name(com->lc_lfsck),
1291 lastid = cpu_to_le64(lls->lls_lastid);
1292 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1293 lfsck_buf_get(env, &lastid,
1299 rc = dt_trans_start_local(env, dt, th);
1303 dt_write_lock(env, lls->lls_lastid_obj, 0);
1304 rc = dt_record_write(env, lls->lls_lastid_obj,
1305 lfsck_buf_get(env, &lastid,
1306 sizeof(lastid)), &pos, th);
1307 dt_write_unlock(env, lls->lls_lastid_obj);
1312 dt_trans_stop(env, dt, th);
1315 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1316 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1317 lfsck_lfsck2name(com->lc_lfsck),
1326 lfsck_layout_lastid_load(const struct lu_env *env,
1327 struct lfsck_component *com,
1328 struct lfsck_layout_seq *lls)
1330 struct lfsck_instance *lfsck = com->lc_lfsck;
1331 struct lfsck_layout *lo = com->lc_file_ram;
1332 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1333 struct dt_object *obj;
1338 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1339 obj = dt_locate(env, lfsck->li_bottom, fid);
1341 RETURN(PTR_ERR(obj));
1343 /* LAST_ID crashed, to be rebuilt */
1344 if (dt_object_exists(obj) == 0) {
1345 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1346 LASSERT(lfsck->li_out_notify != NULL);
1348 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1349 LE_LASTID_REBUILDING);
1350 lo->ll_flags |= LF_CRASHED_LASTID;
1352 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1353 "LAST_ID file for sequence %#llx\n",
1354 lfsck_lfsck2name(lfsck), lls->lls_seq);
1356 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1358 struct l_wait_info lwi = LWI_TIMEOUT(
1359 cfs_time_seconds(cfs_fail_val),
1362 /* Some others may changed the cfs_fail_val
1363 * as zero after above check, re-check it for
1364 * sure to avoid falling into wait for ever. */
1365 if (likely(lwi.lwi_timeout > 0)) {
1366 struct ptlrpc_thread *thread =
1369 up_write(&com->lc_sem);
1370 l_wait_event(thread->t_ctl_waitq,
1371 !thread_is_running(thread),
1373 down_write(&com->lc_sem);
1378 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1380 dt_read_lock(env, obj, 0);
1381 rc = dt_read(env, obj,
1382 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1384 dt_read_unlock(env, obj);
1385 if (rc != 0 && rc != sizeof(__u64))
1386 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1388 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1389 LASSERT(lfsck->li_out_notify != NULL);
1391 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1392 LE_LASTID_REBUILDING);
1393 lo->ll_flags |= LF_CRASHED_LASTID;
1395 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1396 "LAST_ID file for the sequence %#llx"
1398 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1401 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1409 lfsck_object_put(env, obj);
1411 lls->lls_lastid_obj = obj;
1416 static void lfsck_layout_record_failure(const struct lu_env *env,
1417 struct lfsck_instance *lfsck,
1418 struct lfsck_layout *lo)
1422 lo->ll_objs_failed_phase1++;
1423 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1425 if (lo->ll_pos_first_inconsistent == 0 ||
1426 lo->ll_pos_first_inconsistent < cookie) {
1427 lo->ll_pos_first_inconsistent = cookie;
1429 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1430 "inconsistency at the pos [%llu]\n",
1431 lfsck_lfsck2name(lfsck),
1432 lo->ll_pos_first_inconsistent);
1436 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1437 struct lfsck_component *com,
1440 struct lfsck_instance *lfsck = com->lc_lfsck;
1441 struct lfsck_layout *lo = com->lc_file_ram;
1443 down_write(&com->lc_sem);
1444 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1445 HALF_SEC - com->lc_time_last_checkpoint);
1446 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1447 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1450 if (lo->ll_flags & LF_INCOMPLETE) {
1451 lo->ll_status = LS_PARTIAL;
1453 if (lfsck->li_master) {
1454 struct lfsck_assistant_data *lad = com->lc_data;
1456 if (lad->lad_incomplete)
1457 lo->ll_status = LS_PARTIAL;
1459 lo->ll_status = LS_COMPLETED;
1461 lo->ll_status = LS_COMPLETED;
1464 lo->ll_flags &= ~LF_SCANNED_ONCE;
1465 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1466 lo->ll_flags &= ~LF_INCONSISTENT;
1467 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1468 lo->ll_success_count++;
1469 } else if (rc == 0) {
1470 if (lfsck->li_status != 0)
1471 lo->ll_status = lfsck->li_status;
1473 lo->ll_status = LS_STOPPED;
1475 lo->ll_status = LS_FAILED;
1478 rc = lfsck_layout_store(env, com);
1479 up_write(&com->lc_sem);
1484 static int lfsck_layout_trans_stop(const struct lu_env *env,
1485 struct dt_device *dev,
1486 struct thandle *handle, int result)
1490 /* XXX: If there is something worng or it needs to repair nothing,
1491 * then notify the lower to stop the modification. Currently,
1492 * we use th_result for such purpose, that may be replaced by
1493 * some rollback mechanism in the future. */
1494 handle->th_result = result;
1495 rc = dt_trans_stop(env, dev, handle);
1497 return result > 0 ? 0 : result;
1499 return rc == 0 ? 1 : rc;
1502 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1503 struct lfsck_component *com,
1504 const struct lu_fid *pfid,
1505 const struct lu_fid *cfid,
1506 __u32 comp_id, __u32 ea_off,
1509 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1510 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1511 struct dt_device *dev;
1512 struct dt_object *obj;
1513 struct thandle *th = NULL;
1518 idx = lfsck_sub_trace_file_fid2idx(pfid);
1519 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1520 dev = lfsck_obj2dev(obj);
1522 fid_cpu_to_be(&key->lldk_fid, pfid);
1523 key->lldk_comp_id = cpu_to_be32(comp_id);
1524 key->lldk_ea_off = cpu_to_be32(ea_off);
1526 fid_cpu_to_be(rec, cfid);
1527 rec->f_ver = cpu_to_be32(ost_idx);
1529 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1531 th = dt_trans_create(env, dev);
1533 GOTO(unlock, rc = PTR_ERR(th));
1535 rc = dt_declare_insert(env, obj,
1536 (const struct dt_rec *)rec,
1537 (const struct dt_key *)key, th);
1541 rc = dt_trans_start_local(env, dev, th);
1545 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1546 (const struct dt_key *)key, th, 1);
1551 if (th && !IS_ERR(th))
1552 dt_trans_stop(env, dev, th);
1554 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1556 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1557 "ea_off = %u, ost_idx = %u, into the trace file for further "
1558 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1559 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1564 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1565 struct lfsck_component *com,
1566 const struct lu_fid *fid,
1567 __u32 comp_id, __u32 ea_off)
1569 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1570 struct dt_device *dev;
1571 struct dt_object *obj;
1572 struct thandle *th = NULL;
1577 idx = lfsck_sub_trace_file_fid2idx(fid);
1578 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1579 dev = lfsck_obj2dev(obj);
1581 fid_cpu_to_be(&key->lldk_fid, fid);
1582 key->lldk_comp_id = cpu_to_be32(comp_id);
1583 key->lldk_ea_off = cpu_to_be32(ea_off);
1585 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1587 th = dt_trans_create(env, dev);
1589 GOTO(unlock, rc = PTR_ERR(th));
1591 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1595 rc = dt_trans_start_local(env, dev, th);
1599 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1604 if (th && !IS_ERR(th))
1605 dt_trans_stop(env, dev, th);
1607 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1609 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1610 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1611 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1617 * Get the system default stripe size.
1619 * \param[in] env pointer to the thread context
1620 * \param[in] lfsck pointer to the lfsck instance
1621 * \param[out] size pointer to the default stripe size
1623 * \retval 0 for success
1624 * \retval negative error number on failure
1626 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1627 struct lfsck_instance *lfsck,
1630 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1631 struct dt_object *root;
1634 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1636 return PTR_ERR(root);
1638 /* Get the default stripe size via xattr_get on the backend root. */
1639 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1642 /* The lum->lmm_stripe_size is LE mode. The *size also
1643 * should be LE mode. So it is unnecessary to convert. */
1644 *size = lum->lmm_stripe_size;
1646 } else if (unlikely(rc == 0)) {
1650 lfsck_object_put(env, root);
1656 * \retval +1: repaired
1657 * \retval 0: did nothing
1658 * \retval -ve: on error
1660 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1661 struct lfsck_instance *lfsck,
1662 struct thandle *handle,
1663 struct dt_object *parent,
1664 const struct lu_fid *cfid,
1666 struct lov_mds_md_v1 *lmm,
1667 struct lov_ost_data_v1 *slot,
1668 int fl, __u32 ost_idx, int size)
1670 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1671 struct lu_buf ea_buf;
1678 magic = le32_to_cpu(lmm->lmm_magic);
1679 pattern = le32_to_cpu(lmm->lmm_pattern);
1680 count = le16_to_cpu(lmm->lmm_stripe_count);
1682 fid_to_ostid(cfid, oi);
1683 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1684 slot->l_ost_gen = cpu_to_le32(0);
1685 slot->l_ost_idx = cpu_to_le32(ost_idx);
1687 if (pattern & LOV_PATTERN_F_HOLE) {
1688 struct lov_ost_data_v1 *objs;
1691 if (magic == LOV_MAGIC_V1)
1692 objs = &lmm->lmm_objects[0];
1694 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1695 for (i = 0; i < count; i++, objs++) {
1696 if (lovea_slot_is_dummy(objs))
1700 /* If the @slot is the last dummy slot to be refilled,
1701 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1704 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1706 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1707 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1708 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1712 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1713 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1720 static struct lov_ost_data_v1 *
1721 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1722 const struct lu_fid *pfid,
1723 __u32 stripe_size, __u32 ea_off,
1724 __u32 pattern, __u16 count)
1726 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1727 lmm->lmm_pattern = cpu_to_le32(pattern);
1728 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1729 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1730 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1731 lmm->lmm_stripe_count = cpu_to_le16(count);
1732 lmm->lmm_layout_gen = cpu_to_le16(1);
1733 memset(&lmm->lmm_objects[0], 0,
1734 sizeof(struct lov_ost_data_v1) * count);
1736 return &lmm->lmm_objects[ea_off];
1739 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1740 struct lfsck_instance *lfsck,
1741 struct ost_layout *ol,
1742 struct dt_object *parent,
1743 struct lu_buf *buf, __u32 ea_off,
1744 struct lov_mds_md_v1 **lmm,
1745 struct lov_ost_data_v1 **objs)
1748 __u32 stripe_size = ol->ol_stripe_size;
1749 __u32 pattern = LOV_PATTERN_RAID0;
1752 if (ol->ol_stripe_count != 0)
1753 count = ol->ol_stripe_count;
1757 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1758 LASSERTF(buf->lb_len >= size,
1759 "buffer len %d is less than real size %d\n",
1760 (int)buf->lb_len, size);
1762 if (stripe_size == 0) {
1765 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1771 if (ol->ol_stripe_count > 1 ||
1772 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1773 pattern |= LOV_PATTERN_F_HOLE;
1774 memset(&(*lmm)->lmm_objects[0], 0,
1775 count * sizeof(struct lov_ost_data_v1));
1778 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1779 stripe_size, ea_off, pattern, count);
1784 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1785 struct ost_layout *ol,
1786 struct dt_object *parent,
1787 struct lu_buf *buf, __u32 ea_off,
1788 struct lov_mds_md_v1 **lmm,
1789 struct lov_ost_data_v1 **objs)
1791 struct lov_comp_md_v1 *lcm;
1792 struct lov_comp_md_entry_v1 *lcme;
1793 __u32 pattern = LOV_PATTERN_RAID0;
1794 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1795 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1796 int size = offset + lcme_size;
1798 LASSERTF(buf->lb_len >= size,
1799 "buffer len %d is less than real size %d\n",
1800 (int)buf->lb_len, size);
1803 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1804 lcm->lcm_size = cpu_to_le32(size);
1805 lcm->lcm_layout_gen = cpu_to_le32(1);
1807 lcm->lcm_entry_count = cpu_to_le16(1);
1809 lcme = &lcm->lcm_entries[0];
1810 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1811 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1812 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1813 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1814 lcme->lcme_offset = cpu_to_le32(offset);
1815 lcme->lcme_size = cpu_to_le32(lcme_size);
1816 if (ol->ol_stripe_count > 1)
1817 pattern |= LOV_PATTERN_F_HOLE;
1819 *lmm = buf->lb_buf + offset;
1820 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1821 ol->ol_stripe_size, ea_off,
1822 pattern, ol->ol_stripe_count);
1827 static int lfsck_layout_add_comp_comp(const struct lu_env *env,
1828 struct lfsck_instance *lfsck,
1829 struct thandle *handle,
1830 struct ost_layout *ol,
1831 struct dt_object *parent,
1832 const struct lu_fid *cfid,
1833 struct lu_buf *buf, __u32 ost_idx,
1834 __u32 ea_off, int pos)
1836 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1837 struct lov_comp_md_entry_v1 *lcme;
1838 struct lov_mds_md_v1 *lmm;
1839 struct lov_ost_data_v1 *objs;
1840 int added = sizeof(*lcme) +
1841 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1842 int size = le32_to_cpu(lcm->lcm_size) + added;
1846 __u32 pattern = LOV_PATTERN_RAID0;
1847 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1850 lu_buf_check_and_grow(buf, size);
1851 /* set the lcm again because lu_buf_check_and_grow() may
1852 * have reallocated the buf. */
1854 lcm->lcm_size = cpu_to_le32(size);
1855 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1856 lcm->lcm_entry_count = cpu_to_le16(count + 1);
1858 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
1859 * with distance of 'added'. */
1862 for (i = pos; i < count; i++) {
1863 lcme = &lcm->lcm_entries[i];
1864 size += le32_to_cpu(lcme->lcme_size);
1867 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
1868 memmove(buf->lb_buf + offset + added,
1869 buf->lb_buf + offset, size);
1873 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
1874 * of 'sizeof(struct lov_comp_md_entry_v1)' */
1876 for (i = 0; i < pos; i++) {
1877 lcme = &lcm->lcm_entries[i];
1878 size += le32_to_cpu(lcme->lcme_size);
1881 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
1882 memmove(buf->lb_buf + offset + sizeof(*lcme),
1883 buf->lb_buf + offset, size);
1886 /* 3. Recalculate the enter offset for the component [pos, count-1] */
1887 for (i = count - 1; i >= pos; i--) {
1888 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
1889 lcm->lcm_entries[i + 1].lcme_offset =
1890 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
1891 lcme_offset) + added);
1894 /* 4. Recalculate the enter offset for the component [0, pos) */
1895 for (i = 0; i < pos; i++) {
1896 lcm->lcm_entries[i].lcme_offset =
1897 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
1898 lcme_offset) + sizeof(*lcme));
1901 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
1902 /* 4. Insert the new component header (entry) at the slot 'pos'. */
1903 lcme = &lcm->lcm_entries[pos];
1904 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1905 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1906 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1907 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1908 lcme->lcme_offset = cpu_to_le32(offset);
1909 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
1912 if (ol->ol_stripe_count > 1)
1913 pattern |= LOV_PATTERN_F_HOLE;
1915 lmm = buf->lb_buf + offset;
1916 /* 5. Insert teh new component body at the 'offset'. */
1917 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
1918 ol->ol_stripe_size, ea_off,
1919 pattern, ol->ol_stripe_count);
1921 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
1922 lmm, objs, LU_XATTR_REPLACE, ost_idx,
1923 le32_to_cpu(lcm->lcm_size));
1925 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
1926 DFID": parent "DFID", OST-index %u, stripe-index %u, "
1927 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
1928 "comp_end %llu, %s LOV EA hole: rc = %d\n",
1929 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1930 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
1931 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
1932 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
1933 "with" : "without", rc);
1938 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
1939 struct lfsck_instance *lfsck,
1940 struct thandle *handle,
1941 struct ost_layout *ol,
1942 struct dt_object *parent,
1943 const struct lu_fid *cfid,
1944 struct lu_buf *buf, __u32 ost_idx,
1947 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1948 struct lov_ost_data_v1 *objs;
1949 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
1950 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1956 /* The original LOVEA maybe re-generated via old filter_fid, at
1957 * that time, we do not know the stripe count and stripe size. */
1958 if (ol->ol_stripe_count > count)
1959 count = ol->ol_stripe_count;
1960 if (ol->ol_stripe_size != 0 &&
1961 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
1962 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
1964 if (magic == LOV_MAGIC_V1)
1965 objs = &lmm->lmm_objects[count];
1967 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
1969 gap = ea_off - count;
1973 size = lov_mds_md_size(count, magic);
1974 LASSERTF(buf->lb_len >= size,
1975 "buffer len %d is less than real size %d\n",
1976 (int)buf->lb_len, size);
1979 memset(objs, 0, gap * sizeof(*objs));
1980 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1983 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1984 lmm->lmm_stripe_count = cpu_to_le16(count);
1987 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
1988 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
1990 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1991 DFID": parent "DFID", OST-index %u, stripe-index %u, "
1992 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
1993 "comp_end %llu, %s LOV EA hole: rc = %d\n",
1994 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1995 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
1996 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
1997 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
1998 "with" : "without", rc);
2004 * \retval +1: repaired
2005 * \retval 0: did nothing
2006 * \retval -ve: on error
2008 static int lfsck_layout_update_lovea(const struct lu_env *env,
2009 struct lfsck_instance *lfsck,
2010 struct thandle *handle,
2011 struct ost_layout *ol,
2012 struct dt_object *parent,
2013 const struct lu_fid *cfid,
2014 struct lu_buf *buf, int fl,
2015 __u32 ost_idx, __u32 ea_off)
2017 struct lov_mds_md_v1 *lmm = NULL;
2018 struct lov_ost_data_v1 *objs = NULL;
2022 if (ol->ol_comp_id != 0)
2023 rc = lfsck_layout_new_comp_lovea(env, ol, parent, buf, ea_off,
2026 rc = lfsck_layout_new_v1_lovea(env, lfsck, ol, parent, buf,
2027 ea_off, &lmm, &objs);
2030 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2031 buf, lmm, objs, fl, ost_idx, rc);
2033 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2034 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2035 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2036 "comp_end %llu, fl %d, %s LOV EA hole: rc = %d\n",
2037 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2038 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2039 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end, fl,
2040 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2041 "with" : "without", rc);
2046 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2047 struct dt_object *child,
2048 const struct lu_fid *pfid,
2049 const struct ost_layout *ol, __u32 offset)
2051 struct dt_device *dev = lfsck_obj2dev(child);
2052 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2053 struct thandle *handle;
2054 struct lu_buf buf = { NULL };
2057 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2058 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2059 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2060 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2061 * parent MDT-object's layout EA. */
2062 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2063 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2064 lfsck_buf_init(&buf, ff, sizeof(*ff));
2066 handle = dt_trans_create(env, dev);
2068 RETURN(PTR_ERR(handle));
2070 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2074 rc = dt_trans_start_local(env, dev, handle);
2078 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2083 dt_trans_stop(env, dev, handle);
2089 * \retval +1: repaired
2090 * \retval 0: did nothing
2091 * \retval -ve: on error
2093 static int lfsck_layout_update_pfid(const struct lu_env *env,
2094 struct lfsck_component *com,
2095 struct dt_object *parent,
2096 struct lu_fid *cfid,
2097 struct dt_device *cdev,
2098 struct ost_layout *ol, __u32 ea_off)
2100 struct dt_object *child;
2104 child = lfsck_object_find_by_dev(env, cdev, cfid);
2106 RETURN(PTR_ERR(child));
2108 rc = __lfsck_layout_update_pfid(env, child,
2109 lu_object_fid(&parent->do_lu),
2111 lfsck_object_put(env, child);
2113 RETURN(rc == 0 ? 1 : rc);
2116 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2118 if (ol->ol_comp_id != 0)
2119 return sizeof(struct lov_comp_md_v1) +
2120 sizeof(struct lov_comp_md_entry_v1) +
2121 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2123 if (ol->ol_stripe_count != 0)
2124 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2126 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2130 * This function will create the MDT-object with the given (partial) LOV EA.
2132 * Under some data corruption cases, the MDT-object of the file may be lost,
2133 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2134 * re-create the MDT-object with the orphan OST-object(s) information.
2136 * On the other hand, the LFSCK may has created some OST-object for repairing
2137 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2138 * the old OST-object is there and should replace the former new created OST
2139 * object. Unfortunately, some others have modified such newly created object.
2140 * To keep the data (both new and old), the LFSCK will create MDT-object with
2141 * new FID to reference the original OST-object.
2143 * \param[in] env pointer to the thread context
2144 * \param[in] com pointer to the lfsck component
2145 * \param[in] ltd pointer to target device descriptor
2146 * \param[in] rec pointer to the record for the orphan OST-object
2147 * \param[in] cfid pointer to FID for the orphan OST-object
2148 * \param[in] infix additional information, such as the FID for original
2149 * MDT-object and the stripe offset in the LOV EA
2150 * \param[in] type the type for describing why the orphan MDT-object is
2151 * created. The rules are as following:
2153 * type "C": Multiple OST-objects claim the same MDT-object and the
2154 * same slot in the layout EA. Then the LFSCK will create
2155 * new MDT-object(s) to hold the conflict OST-object(s).
2157 * type "N": The orphan OST-object does not know which one was the
2158 * real parent MDT-object, so the LFSCK uses new FID for
2159 * its parent MDT-object.
2161 * type "R": The orphan OST-object knows its parent MDT-object FID,
2162 * but does not know the position (the file name) in the
2165 * type "D": The MDT-object is a directory, it may knows its parent
2166 * but because there is no valid linkEA, the LFSCK cannot
2167 * know where to put it back to the namespace.
2168 * type "O": The MDT-object has no linkEA, and there is no name
2169 * entry that references the MDT-object.
2171 * type "P": The orphan object to be created was a parent directory
2172 * of some MDT-object which linkEA shows that the @orphan
2173 * object is missing.
2175 * The orphan name will be like:
2176 * ${FID}-${infix}-${type}-${conflict_version}
2178 * \param[in] ea_off the stripe offset in the LOV EA
2180 * \retval positive on repaired something
2181 * \retval 0 if needs to repair nothing
2182 * \retval negative error number on failure
2184 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2185 struct lfsck_component *com,
2186 struct lfsck_tgt_desc *ltd,
2187 struct lu_orphan_rec_v2 *rec,
2188 struct lu_fid *cfid,
2193 struct lfsck_thread_info *info = lfsck_env_info(env);
2194 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2195 char *name = info->lti_key;
2196 struct lu_attr *la = &info->lti_la2;
2197 struct dt_object_format *dof = &info->lti_dof;
2198 struct lfsck_instance *lfsck = com->lc_lfsck;
2199 struct ost_layout *ol = &rec->lor_layout;
2200 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2201 struct lu_fid *tfid = &info->lti_fid3;
2202 struct dt_device *dev = lfsck->li_bottom;
2203 struct dt_object *lpf = lfsck->li_lpf_obj;
2204 struct dt_object *pobj = NULL;
2205 struct dt_object *cobj = NULL;
2206 struct thandle *th = NULL;
2207 struct lu_buf *ea_buf = &info->lti_big_buf;
2208 struct lu_buf lov_buf;
2209 struct lfsck_lock_handle *llh = &info->lti_llh;
2210 struct linkea_data ldata = { NULL };
2211 struct lu_buf linkea_buf;
2212 const struct lu_name *pname;
2218 if (unlikely(lpf == NULL))
2219 GOTO(log, rc = -ENXIO);
2221 /* We use two separated transactions to repair the inconsistency.
2223 * 1) create the MDT-object locally.
2224 * 2) update the OST-object's PFID EA if necessary.
2226 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2227 * updated when the layout LFSCK run next time.
2229 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2230 * when the layout LFSCK run next time. */
2232 if (fid_is_zero(pfid)) {
2233 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2237 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2239 GOTO(log, rc = PTR_ERR(cobj));
2242 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2244 GOTO(log, rc = PTR_ERR(pobj));
2246 LASSERT(infix != NULL);
2247 LASSERT(type != NULL);
2249 memset(la, 0, sizeof(*la));
2250 la->la_uid = rec->lor_rec.lor_uid;
2251 la->la_gid = rec->lor_rec.lor_gid;
2252 la->la_mode = S_IFREG | S_IRUSR;
2253 la->la_valid = LA_MODE | LA_UID | LA_GID;
2255 memset(dof, 0, sizeof(*dof));
2256 dof->dof_type = dt_mode_to_dft(S_IFREG);
2257 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2258 * the stripe(s). The LFSCK will specify the LOV EA via
2259 * lfsck_layout_update_lovea(). */
2261 size = lfsck_lovea_size(ol, ea_off);
2262 if (ea_buf->lb_len < size) {
2263 lu_buf_realloc(ea_buf, size);
2264 if (ea_buf->lb_buf == NULL)
2265 GOTO(log, rc = -ENOMEM);
2270 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2272 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2273 (const struct dt_key *)name);
2274 if (rc != 0 && rc != -ENOENT)
2278 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2279 MDS_INODELOCK_UPDATE, LCK_PW);
2283 /* Re-check whether the name conflict with othrs after taken
2285 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2286 (const struct dt_key *)name);
2287 if (unlikely(rc == 0)) {
2295 pname = lfsck_name_get_const(env, name, strlen(name));
2296 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2297 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2301 /* The 1st transaction. */
2302 th = dt_trans_create(env, dev);
2304 GOTO(unlock, rc = PTR_ERR(th));
2306 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2310 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2311 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2312 LU_XATTR_CREATE, th);
2316 dtrec->rec_fid = pfid;
2317 dtrec->rec_type = S_IFREG;
2318 rc = dt_declare_insert(env, lpf,
2319 (const struct dt_rec *)dtrec,
2320 (const struct dt_key *)name, th);
2324 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2325 ldata.ld_leh->leh_len);
2326 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2327 XATTR_NAME_LINK, 0, th);
2331 rc = dt_trans_start_local(env, dev, th);
2335 dt_write_lock(env, pobj, 0);
2336 rc = dt_create(env, pobj, la, NULL, dof, th);
2338 rc = lfsck_layout_update_lovea(env, lfsck, th, ol, pobj, cfid,
2339 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2340 dt_write_unlock(env, pobj);
2344 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2345 (const struct dt_key *)name, th, 1);
2349 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2350 if (rc == 0 && cobj != NULL) {
2351 dt_trans_stop(env, dev, th);
2354 /* The 2nd transaction. */
2355 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ol, ea_off);
2362 dt_trans_stop(env, dev, th);
2368 if (cobj != NULL && !IS_ERR(cobj))
2369 lfsck_object_put(env, cobj);
2370 if (pobj != NULL && !IS_ERR(pobj))
2371 lfsck_object_put(env, pobj);
2374 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2375 "recreate the lost MDT-object: parent "DFID
2376 ", child "DFID", OST-index %u, stripe-index %u, "
2377 "infix %s, type %s: rc = %d\n",
2378 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2379 ltd->ltd_index, ea_off, infix, type, rc);
2381 return rc >= 0 ? 1 : rc;
2384 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2385 struct lfsck_component *com,
2386 const struct lu_fid *fid,
2389 struct lfsck_thread_info *info = lfsck_env_info(env);
2390 struct lfsck_request *lr = &info->lti_lr;
2391 struct lfsck_instance *lfsck = com->lc_lfsck;
2392 struct lfsck_tgt_desc *ltd;
2393 struct ptlrpc_request *req;
2394 struct lfsck_request *tmp;
2395 struct obd_export *exp;
2399 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2400 if (unlikely(ltd == NULL))
2404 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2405 GOTO(put, rc = -EOPNOTSUPP);
2407 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2409 GOTO(put, rc = -ENOMEM);
2411 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2413 ptlrpc_request_free(req);
2418 memset(lr, 0, sizeof(*lr));
2419 lr->lr_event = LE_CONDITIONAL_DESTROY;
2420 lr->lr_active = LFSCK_TYPE_LAYOUT;
2423 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2425 ptlrpc_request_set_replen(req);
2427 rc = ptlrpc_queue_wait(req);
2428 ptlrpc_req_finished(req);
2438 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2439 struct lfsck_component *com,
2440 struct lfsck_request *lr)
2442 struct lfsck_thread_info *info = lfsck_env_info(env);
2443 struct lu_attr *la = &info->lti_la;
2444 union ldlm_policy_data *policy = &info->lti_policy;
2445 struct ldlm_res_id *resid = &info->lti_resid;
2446 struct lfsck_instance *lfsck = com->lc_lfsck;
2447 struct dt_device *dev = lfsck->li_bottom;
2448 struct lu_fid *fid = &lr->lr_fid;
2449 struct dt_object *obj;
2450 struct thandle *th = NULL;
2451 struct lustre_handle lh = { 0 };
2456 obj = lfsck_object_find_by_dev(env, dev, fid);
2458 RETURN(PTR_ERR(obj));
2460 dt_read_lock(env, obj, 0);
2461 if (dt_object_exists(obj) == 0 ||
2462 lfsck_is_dead_obj(obj)) {
2463 dt_read_unlock(env, obj);
2465 GOTO(put, rc = -ENOENT);
2468 /* Get obj's attr without lock firstly. */
2469 rc = dt_attr_get(env, obj, la);
2470 dt_read_unlock(env, obj);
2474 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2475 GOTO(put, rc = -ETXTBSY);
2477 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2478 LASSERT(lfsck->li_namespace != NULL);
2480 memset(policy, 0, sizeof(*policy));
2481 policy->l_extent.end = OBD_OBJECT_EOF;
2482 ost_fid_build_resid(fid, resid);
2483 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2484 policy, LCK_EX, &flags, ldlm_blocking_ast,
2485 ldlm_completion_ast, NULL, NULL, 0,
2486 LVB_T_NONE, NULL, &lh);
2488 GOTO(put, rc = -EIO);
2490 dt_write_lock(env, obj, 0);
2491 /* Get obj's attr within lock again. */
2492 rc = dt_attr_get(env, obj, la);
2496 if (la->la_ctime != 0)
2497 GOTO(unlock, rc = -ETXTBSY);
2499 th = dt_trans_create(env, dev);
2501 GOTO(unlock, rc = PTR_ERR(th));
2503 rc = dt_declare_ref_del(env, obj, th);
2507 rc = dt_declare_destroy(env, obj, th);
2511 rc = dt_trans_start_local(env, dev, th);
2515 rc = dt_ref_del(env, obj, th);
2519 rc = dt_destroy(env, obj, th);
2521 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2522 "OST-object "DFID" that was created for reparing "
2523 "dangling referenced case. But the original missing "
2524 "OST-object is found now.\n",
2525 lfsck_lfsck2name(lfsck), PFID(fid));
2530 dt_trans_stop(env, dev, th);
2533 dt_write_unlock(env, obj);
2534 ldlm_lock_decref(&lh, LCK_EX);
2537 lfsck_object_put(env, obj);
2543 * Some OST-object has occupied the specified layout EA slot.
2544 * Such OST-object may be generated by the LFSCK when repair
2545 * dangling referenced MDT-object, which can be indicated by
2546 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2547 * is true and such OST-object has not been modified yet, we
2548 * will replace it with the orphan OST-object; otherwise the
2549 * LFSCK will create new MDT-object to reference the orphan.
2551 * \retval +1: repaired
2552 * \retval 0: did nothing
2553 * \retval -ve: on error
2555 static int lfsck_layout_conflict_create(const struct lu_env *env,
2556 struct lfsck_component *com,
2557 struct lfsck_tgt_desc *ltd,
2558 struct lu_orphan_rec_v2 *rec,
2559 struct dt_object *parent,
2560 struct lu_fid *cfid,
2561 struct lu_buf *ea_buf,
2562 struct lov_mds_md_v1 *lmm,
2563 struct lov_ost_data_v1 *slot,
2564 __u32 ea_off, int lovea_size)
2566 struct lfsck_thread_info *info = lfsck_env_info(env);
2567 struct lu_fid *cfid2 = &info->lti_fid2;
2568 struct ost_id *oi = &info->lti_oi;
2569 struct dt_device *dev = lfsck_obj2dev(parent);
2570 struct thandle *th = NULL;
2571 struct lustre_handle lh = { 0 };
2572 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2576 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2577 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2581 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2582 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2586 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2587 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2592 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2594 /* If the conflict OST-obejct is not created for fixing dangling
2595 * referenced MDT-object in former LFSCK check/repair, or it has
2596 * been modified by others, then we cannot destroy it. Re-create
2597 * a new MDT-object for the orphan OST-object. */
2598 if (rc == -ETXTBSY) {
2599 /* No need the layout lock on the original parent. */
2600 lfsck_ibits_unlock(&lh, LCK_EX);
2602 fid_zero(&rec->lor_rec.lor_fid);
2603 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2604 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2606 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2607 info->lti_tmpbuf, "C", ea_off);
2612 if (rc != 0 && rc != -ENOENT)
2615 th = dt_trans_create(env, dev);
2617 GOTO(unlock, rc = PTR_ERR(th));
2619 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2620 LU_XATTR_REPLACE, th);
2624 rc = dt_trans_start_local(env, dev, th);
2628 dt_write_lock(env, parent, 0);
2629 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2630 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2631 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2632 ltd->ltd_index, lovea_size);
2633 dt_write_unlock(env, parent);
2638 dt_trans_stop(env, dev, th);
2641 lfsck_ibits_unlock(&lh, LCK_EX);
2644 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2645 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2646 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2647 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2648 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2651 return rc >= 0 ? 1 : rc;
2655 * \retval +1: repaired
2656 * \retval 0: did nothing
2657 * \retval -ve: on error
2659 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2660 struct lfsck_component *com,
2661 struct lfsck_tgt_desc *ltd,
2662 struct lu_orphan_rec_v2 *rec,
2663 struct dt_object *parent,
2664 struct lu_fid *cfid,
2665 __u32 ost_idx, __u32 ea_off)
2667 struct lfsck_thread_info *info = lfsck_env_info(env);
2668 struct lu_buf *buf = &info->lti_big_buf;
2669 struct lu_fid *fid = &info->lti_fid2;
2670 struct ost_id *oi = &info->lti_oi;
2671 struct lfsck_instance *lfsck = com->lc_lfsck;
2672 struct dt_device *dt = lfsck_obj2dev(parent);
2673 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2674 struct ost_layout *ol = &rec->lor_layout;
2675 struct lov_comp_md_v1 *lcm = NULL;
2676 struct lov_comp_md_entry_v1 *lcme = NULL;
2677 struct thandle *handle = NULL;
2679 struct lov_mds_md_v1 *lmm;
2680 struct lov_ost_data_v1 *objs;
2681 struct lustre_handle lh = { 0 };
2689 bool locked = false;
2692 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2693 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2696 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2697 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2698 "stripe-index %u, comp_id %u, comp_start %llu, "
2699 "comp_end %llu: rc = %d\n",
2700 lfsck_lfsck2name(lfsck), PFID(cfid),
2701 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2702 ol->ol_comp_id, ol->ol_comp_start,
2703 ol->ol_comp_end, rc);
2710 dt_write_unlock(env, parent);
2714 if (handle != NULL) {
2715 dt_trans_stop(env, dt, handle);
2720 GOTO(unlock_layout, rc);
2723 if (buf->lb_len < lovea_size) {
2724 lu_buf_realloc(buf, lovea_size);
2725 if (buf->lb_buf == NULL)
2726 GOTO(unlock_layout, rc = -ENOMEM);
2729 if (!(bk->lb_param & LPF_DRYRUN)) {
2730 handle = dt_trans_create(env, dt);
2732 GOTO(unlock_layout, rc = PTR_ERR(handle));
2734 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2739 rc = dt_trans_start_local(env, dt, handle);
2744 dt_write_lock(env, parent, 0);
2746 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2747 if (rc == -ERANGE) {
2748 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2751 } else if (rc == -ENODATA || rc == 0) {
2752 lovea_size = lfsck_lovea_size(ol, ea_off);
2753 /* If the declared is not big enough, re-try. */
2754 if (buf->lb_len < lovea_size) {
2758 fl = LU_XATTR_CREATE;
2759 } else if (rc < 0) {
2760 GOTO(unlock_parent, rc);
2761 } else if (unlikely(buf->lb_len == 0)) {
2764 fl = LU_XATTR_REPLACE;
2768 if (fl == LU_XATTR_CREATE) {
2769 if (bk->lb_param & LPF_DRYRUN)
2770 GOTO(unlock_parent, rc = 1);
2772 LASSERT(buf->lb_len >= lovea_size);
2774 rc = lfsck_layout_update_lovea(env, lfsck, handle, ol, parent,
2775 cfid, buf, fl, ost_idx, ea_off);
2777 GOTO(unlock_parent, rc);
2781 rc1 = lfsck_layout_verify_header(parent, lmm);
2783 /* If the LOV EA crashed, the rebuild it. */
2784 if (rc1 == -EINVAL) {
2785 if (bk->lb_param & LPF_DRYRUN)
2786 GOTO(unlock_parent, rc = 1);
2788 LASSERT(buf->lb_len >= lovea_size);
2790 rc = lfsck_layout_update_lovea(env, lfsck, handle, ol, parent,
2791 cfid, buf, fl, ost_idx, ea_off);
2793 GOTO(unlock_parent, rc);
2796 /* For other unknown magic/pattern, keep the current LOV EA. */
2798 GOTO(unlock_parent, rc = rc1);
2800 magic = le32_to_cpu(lmm->lmm_magic);
2801 if (magic == LOV_MAGIC_COMP_V1) {
2806 count = le16_to_cpu(lcm->lcm_entry_count);
2807 for (i = 0; i < count; i++) {
2808 lcme = &lcm->lcm_entries[i];
2809 start = le64_to_cpu(lcme->lcme_extent.e_start);
2810 end = le64_to_cpu(lcme->lcme_extent.e_end);
2812 if (end <= ol->ol_comp_start)
2815 if (start >= ol->ol_comp_end)
2818 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
2819 magic = le32_to_cpu(lmm->lmm_magic);
2820 flags = le32_to_cpu(lcme->lcme_flags);
2824 rc = lfsck_layout_add_comp_comp(env, lfsck, handle, ol, parent,
2825 cfid, buf, ost_idx, ea_off, i);
2827 GOTO(unlock_parent, rc);
2831 count = le16_to_cpu(lmm->lmm_stripe_count);
2833 GOTO(unlock_parent, rc = -EINVAL);
2836 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2837 if (count <= ea_off) {
2838 if (bk->lb_param & LPF_DRYRUN)
2839 GOTO(unlock_parent, rc = 1);
2841 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2842 /* If the declared is not big enough, re-try. */
2843 if (buf->lb_len < lovea_size) {
2848 if (lcme && !(flags & LCME_FL_INIT))
2849 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
2851 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
2852 parent, cfid, buf, ost_idx, ea_off);
2854 GOTO(unlock_parent, rc);
2857 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2859 if (magic == LOV_MAGIC_V1) {
2860 objs = &lmm->lmm_objects[0];
2862 LASSERT(magic == LOV_MAGIC_V3);
2863 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2866 for (i = 0; i < count; i++, objs++) {
2867 /* The MDT-object was created via lfsck_layout_recover_create()
2868 * by others before, and we fill the dummy layout EA. */
2869 if ((lcme && !(flags & LCME_FL_INIT)) ||
2870 lovea_slot_is_dummy(objs)) {
2874 if (bk->lb_param & LPF_DRYRUN)
2875 GOTO(unlock_parent, rc = 1);
2877 lmm->lmm_layout_gen =
2878 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2882 if (le32_to_cpu(lmm->lmm_stripe_size) !=
2883 ol->ol_stripe_size ||
2884 le16_to_cpu(lmm->lmm_stripe_count) !=
2885 ol->ol_stripe_count ||
2886 le64_to_cpu(lcme->lcme_extent.e_start) !=
2887 ol->ol_comp_start ||
2888 le64_to_cpu(lcme->lcme_extent.e_end) !=
2890 CDEBUG(D_LFSCK, "%s: found invalid "
2891 "component for "DFID ": parent "DFID
2892 ", stripe-index %u, stripe_size %u, "
2893 "stripe_count %u, comp_id %u, "
2894 "comp_start %llu, comp_end %llu, "
2895 "cur_stripe_size %u, "
2896 "cur_stripe_count %u, "
2897 "cur_comp_start %llu, "
2898 "cur_comp_end %llu\n",
2899 lfsck_lfsck2name(lfsck), PFID(cfid),
2900 PFID(lfsck_dto2fid(parent)), ea_off,
2902 ol->ol_stripe_count, ol->ol_comp_id,
2903 ol->ol_comp_start, ol->ol_comp_end,
2904 le32_to_cpu(lmm->lmm_stripe_size),
2905 le16_to_cpu(lmm->lmm_stripe_count),
2906 le64_to_cpu(lcme->lcme_extent.e_start),
2907 le64_to_cpu(lcme->lcme_extent.e_end));
2909 GOTO(unlock_parent, rc = -EINVAL);
2912 le32_add_cpu(&lcm->lcm_layout_gen, 1);
2913 lovea_size = le32_to_cpu(lcm->lcm_size);
2914 if (!(flags & LCME_FL_INIT))
2915 lcme->lcme_flags = cpu_to_le32(flags |
2919 LASSERTF(buf->lb_len >= lovea_size,
2920 "buffer len %d is less than real size %d\n",
2921 (int)buf->lb_len, (int)lovea_size);
2923 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
2924 parent, cfid, buf, lmm, objs,
2925 fl, ost_idx, lovea_size);
2927 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2928 "dummy layout slot for "DFID": parent "DFID
2929 ", OST-index %u, stripe-index %u: rc = %d\n",
2930 lfsck_lfsck2name(lfsck), PFID(cfid),
2931 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2933 GOTO(unlock_parent, rc);
2936 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2937 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2939 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2940 "invalid layout EA at the slot %d, index %u\n",
2941 lfsck_lfsck2name(lfsck),
2942 PFID(lfsck_dto2fid(parent)), i,
2943 le32_to_cpu(objs->l_ost_idx));
2945 GOTO(unlock_parent, rc);
2948 /* It should be rare case, the slot is there, but the LFSCK
2949 * does not handle it during the first-phase cycle scanning. */
2950 if (unlikely(lu_fid_eq(fid, cfid))) {
2952 GOTO(unlock_parent, rc = 0);
2954 /* Rare case that the OST-object index
2955 * does not match the parent MDT-object
2956 * layout EA. We trust the later one. */
2957 if (bk->lb_param & LPF_DRYRUN)
2958 GOTO(unlock_parent, rc = 1);
2960 dt_write_unlock(env, parent);
2962 dt_trans_stop(env, dt, handle);
2963 lfsck_ibits_unlock(&lh, LCK_EX);
2964 rc = lfsck_layout_update_pfid(env, com, parent,
2968 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2969 "updated OST-object's pfid for "DFID
2970 ": parent "DFID", OST-index %u, "
2971 "stripe-index %u: rc = %d\n",
2972 lfsck_lfsck2name(lfsck), PFID(cfid),
2973 PFID(lfsck_dto2fid(parent)),
2974 ltd->ltd_index, i, rc);
2981 /* The MDT-object exists, but related layout EA slot is occupied
2983 if (bk->lb_param & LPF_DRYRUN)
2984 GOTO(unlock_parent, rc = 1);
2986 dt_write_unlock(env, parent);
2988 dt_trans_stop(env, dt, handle);
2989 lfsck_ibits_unlock(&lh, LCK_EX);
2990 if (magic == LOV_MAGIC_V1)
2991 objs = &lmm->lmm_objects[ea_off];
2993 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2994 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2995 buf, lmm, objs, ea_off, lovea_size);
3001 dt_write_unlock(env, parent);
3005 dt_trans_stop(env, dt, handle);
3008 lfsck_ibits_unlock(&lh, LCK_EX);
3013 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3014 struct lfsck_component *com,
3015 struct lfsck_tgt_desc *ltd,
3016 struct lu_orphan_rec_v2 *rec,
3017 struct lu_fid *cfid)
3019 struct lfsck_layout *lo = com->lc_file_ram;
3020 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3021 struct dt_object *parent = NULL;
3022 __u32 ea_off = pfid->f_stripe_idx;
3026 if (!fid_is_sane(cfid))
3027 GOTO(out, rc = -EINVAL);
3030 if (fid_is_zero(pfid)) {
3031 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3036 if (!fid_is_sane(pfid))
3037 GOTO(out, rc = -EINVAL);
3039 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3041 GOTO(out, rc = PTR_ERR(parent));
3043 if (unlikely(dt_object_remote(parent) != 0))
3044 GOTO(put, rc = -EXDEV);
3046 if (dt_object_exists(parent) == 0) {
3047 lfsck_object_put(env, parent);
3048 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3053 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3054 GOTO(put, rc = -EISDIR);
3056 /* The orphan OST-object claims to be the parent's stripe, then
3057 * related dangling record in the trace file is meaningless. */
3058 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3059 rec->lor_layout.ol_comp_id, ea_off);
3060 if (rc && rc != -ENOENT)
3063 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3064 ltd->ltd_index, ea_off);
3070 lfsck_object_put(env, parent);
3072 /* The layout EA is changed, need to be reloaded next time. */
3073 dt_object_put_nocache(env, parent);
3076 down_write(&com->lc_sem);
3077 com->lc_new_scanned++;
3078 com->lc_new_checked++;
3080 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3082 } else if (rc < 0) {
3083 lo->ll_objs_failed_phase2++;
3085 up_write(&com->lc_sem);
3090 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3091 struct lfsck_component *com,
3092 struct lfsck_tgt_desc *ltd)
3094 struct lfsck_assistant_data *lad = com->lc_data;
3095 struct lfsck_instance *lfsck = com->lc_lfsck;
3096 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3097 struct lfsck_thread_info *info = lfsck_env_info(env);
3098 struct ost_id *oi = &info->lti_oi;
3099 struct lu_fid *fid = &info->lti_fid;
3100 struct dt_object *obj;
3101 const struct dt_it_ops *iops;
3106 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3107 "scanning for OST%04x\n",
3108 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3110 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
3111 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3112 "scanning for OST%04x\n",
3113 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3118 ostid_set_seq(oi, FID_SEQ_IDIF);
3119 ostid_set_id(oi, 0);
3120 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
3124 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3125 if (unlikely(IS_ERR(obj)))
3126 GOTO(log, rc = PTR_ERR(obj));
3128 rc = obj->do_ops->do_index_try(env, obj,
3129 &dt_lfsck_layout_orphan_features);
3133 iops = &obj->do_index_ops->dio_it;
3134 di = iops->init(env, obj, 0);
3136 GOTO(put, rc = PTR_ERR(di));
3138 rc = iops->load(env, di, 0);
3140 /* -ESRCH means that the orphan OST-objects rbtree has been
3141 * cleanup because of the OSS server restart or other errors. */
3142 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3147 rc = iops->next(env, di);
3159 struct lu_orphan_rec_v2 *rec = &info->lti_rec;
3161 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3162 unlikely(!thread_is_running(&lfsck->li_thread)))
3165 key = iops->key(env, di);
3166 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3167 /* Remote target OST may be runnning old LFSCK */
3168 memset(rec, 0, sizeof(*rec));
3169 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3171 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3172 &com->lc_fid_latest_scanned_phase2);
3173 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3176 lfsck_control_speed_by_self(com);
3178 rc = iops->next(env, di);
3179 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3186 iops->fini(env, di);
3188 lfsck_object_put(env, obj);
3191 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3192 "scanning for OST%04x: rc = %d\n",
3193 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3195 return rc > 0 ? 0 : rc;
3198 static int lfsck_lmm2layout(struct lov_mds_md_v1 *lmm, struct ost_layout *ol,
3201 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3205 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3206 ol->ol_stripe_size = lmm->lmm_stripe_size;
3207 ol->ol_stripe_count = lmm->lmm_stripe_count;
3208 ol->ol_comp_start = 0;
3209 ol->ol_comp_end = 0;
3211 } else if (magic == LOV_MAGIC_COMP_V1) {
3212 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3213 struct lov_comp_md_entry_v1 *lcme = NULL;
3214 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3217 for (i = 0; i < count; i++) {
3218 lcme = &lcm->lcm_entries[i];
3219 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3220 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3227 /* The comp has been removed, do nothing. */
3231 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3232 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3233 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3234 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3235 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3236 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3238 GOTO(out, rc = -EINVAL);
3248 * Repair the MDT-object with dangling LOV EA reference.
3250 * we need to repair the inconsistency according to the users' requirement:
3252 * 1) Keep the inconsistency there and report the inconsistency case,
3253 * then give the chance to the application to find related issues,
3254 * and the users can make the decision about how to handle it with
3255 * more human knownledge. (by default)
3257 * 2) Re-create the missing OST-object with the FID/owner information.
3259 * \param[in] env pointer to the thread context
3260 * \param[in] com the layout LFSCK component
3261 * \param[in] parent the MDT-object with dangling LOV EA reference
3262 * \param[in] child the OST-object to be created
3263 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3264 * \param[in] ea_off the offset of the OST-object in the LOV EA
3265 * \param[in] ost_idx the index of OST on which the OST-object resides
3267 * \retval +1 for repair successfully
3268 * \retval 0 for did nothing
3269 * \retval negative error number on failure
3271 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3272 struct lfsck_component *com,
3273 struct dt_object *parent,
3274 struct dt_object *child,
3275 __u32 comp_id, __u32 ea_off,
3276 __u32 ost_idx, bool log)
3278 struct lfsck_thread_info *info = lfsck_env_info(env);
3279 struct filter_fid *ff = &info->lti_ff;
3280 struct ost_layout *ol = &ff->ff_layout;
3281 struct dt_object_format *dof = &info->lti_dof;
3282 struct lu_attr *la = &info->lti_la;
3283 struct lfsck_instance *lfsck = com->lc_lfsck;
3284 struct dt_device *dev = lfsck_obj2dev(child);
3285 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3286 const struct lu_fid *cfid = lfsck_dto2fid(child);
3287 struct lu_buf *tbuf = &info->lti_big_buf;
3288 struct thandle *handle;
3290 struct lustre_handle lh = { 0 };
3294 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3297 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3298 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3303 rc = dt_attr_get(env, parent, la);
3307 la->la_mode = S_IFREG | 0666;
3308 la->la_atime = la->la_mtime = la->la_ctime = 0;
3309 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3310 LA_ATIME | LA_MTIME | LA_CTIME;
3311 memset(dof, 0, sizeof(*dof));
3312 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3313 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3314 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3315 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3316 * parent MDT-object's layout EA. */
3317 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3319 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3323 rc = lfsck_lmm2layout(tbuf->lb_buf, ol, comp_id);
3327 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3328 handle = dt_trans_create(env, dev);
3330 GOTO(unlock1, rc = PTR_ERR(handle));
3332 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3336 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3337 LU_XATTR_CREATE, handle);
3341 rc = dt_trans_start_local(env, dev, handle);
3345 dt_read_lock(env, parent, 0);
3346 if (unlikely(lfsck_is_dead_obj(parent)))
3347 GOTO(unlock2, rc = 0);
3349 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3350 struct ost_id *oi = &info->lti_oi;
3351 struct lu_fid *tfid = &info->lti_fid2;
3352 struct lu_buf *lovea = &info->lti_big_buf;
3353 struct lov_mds_md_v1 *lmm;
3354 struct lov_ost_data_v1 *objs;
3359 rc = lfsck_layout_get_lovea(env, parent, lovea);
3363 lmm = lovea->lb_buf;
3364 magic = le32_to_cpu(lmm->lmm_magic);
3365 if (magic == LOV_MAGIC_COMP_V1) {
3366 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3367 struct lov_comp_md_entry_v1 *lcme;
3368 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3371 for (i = 0; i < count; i++) {
3372 lcme = &lcm->lcm_entries[i];
3373 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3374 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3377 lmm = lovea->lb_buf +
3378 le32_to_cpu(lcme->lcme_offset);
3379 magic = le32_to_cpu(lmm->lmm_magic);
3384 /* Someone removed the component, do nothing. */
3385 GOTO(unlock2, rc = 0);
3389 count = le16_to_cpu(lmm->lmm_stripe_count);
3390 /* Someone changed the LOV EA, do nothing. */
3391 if (count <= ea_off)
3392 GOTO(unlock2, rc = 0);
3394 if (magic == LOV_MAGIC_V1) {
3395 objs = &lmm->lmm_objects[ea_off];
3397 LASSERT(magic == LOV_MAGIC_V3);
3399 objs = &((struct lov_mds_md_v3 *)lmm)->\
3400 lmm_objects[ea_off];
3403 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3404 idx2 = le32_to_cpu(objs->l_ost_idx);
3405 rc = ostid_to_fid(tfid, oi, idx2);
3406 /* Someone changed the LOV EA, do nothing. */
3407 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3411 rc = dt_create(env, child, la, NULL, dof, handle);
3415 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3421 dt_read_unlock(env, parent);
3424 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3427 lfsck_ibits_unlock(&lh, LCK_EX);
3431 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3432 "dangling reference for: parent "DFID", child "
3433 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3435 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3436 comp_id, ea_off, ost_idx,
3437 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3438 "Create the lost OST-object as required" :
3439 "Keep the MDT-object there by default", rc);
3445 * Repair the MDT-object with dangling LOV EA reference.
3447 * Prepare parameters and call __lfsck_layout_repair_dangling()
3448 * to repair the dangling LOV EA reference.
3450 * \param[in] env pointer to the thread context
3451 * \param[in] com the layout LFSCK component
3452 * \param[in] pfid the MDT-object's FID
3453 * \param[in] cfid the FID for the OST-object to be created
3454 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3455 * \param[in] ea_off the offset of the OST-object in the LOV EA
3456 * \param[in] ost_idx the index of OST on which the OST-object resides
3458 * \retval +1 for repair successfully
3459 * \retval 0 for did nothing
3460 * \retval negative error number on failure
3462 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3463 struct lfsck_component *com,
3464 const struct lu_fid *pfid,
3465 const struct lu_fid *cfid,
3466 __u32 comp_id, __u32 ea_off,
3469 struct lfsck_instance *lfsck = com->lc_lfsck;
3470 struct dt_object *parent = NULL;
3471 struct dt_object *child = NULL;
3472 struct lfsck_tgt_desc *ltd;
3476 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3478 GOTO(log, rc = PTR_ERR(parent));
3480 /* The MDT-object has been removed. */
3481 if (dt_object_exists(parent) == 0)
3484 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3485 if (unlikely(ltd == NULL))
3486 GOTO(log, rc = -ENODEV);
3488 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3490 GOTO(log, rc = PTR_ERR(child));
3492 /* The OST-object has been created. */
3493 if (unlikely(dt_object_exists(child) != 0))
3496 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3497 comp_id, ea_off, ost_idx, false);
3502 if (child != NULL && !IS_ERR(child))
3503 lfsck_object_put(env, child);
3505 if (parent != NULL && !IS_ERR(parent))
3506 lfsck_object_put(env, parent);
3509 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3510 "dangling reference for: parent "DFID", child "
3511 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3512 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3513 comp_id, ea_off, ost_idx,
3514 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3515 "Create the lost OST-object as required" :
3516 "Keep the MDT-object there by default", rc);
3521 /* If the OST-object does not recognize the MDT-object as its parent, and
3522 * there is no other MDT-object claims as its parent, then just trust the
3523 * given MDT-object as its parent. So update the OST-object filter_fid. */
3524 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3525 struct lfsck_component *com,
3526 struct dt_object *parent,
3527 struct lfsck_layout_req *llr,
3530 struct lfsck_thread_info *info = lfsck_env_info(env);
3531 struct filter_fid *ff = &info->lti_ff;
3532 struct ost_layout *ol = &ff->ff_layout;
3533 struct dt_object *child = llr->llr_child;
3534 struct dt_device *dev = lfsck_obj2dev(child);
3535 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3536 struct lu_buf *tbuf = &info->lti_big_buf;
3537 struct thandle *handle;
3539 struct lustre_handle lh = { 0 };
3543 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3544 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3549 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3550 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3551 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3552 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3553 * parent MDT-object's layout EA. */
3554 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3556 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3560 rc = lfsck_lmm2layout(tbuf->lb_buf, ol, llr->llr_comp_id);
3564 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3566 handle = dt_trans_create(env, dev);
3568 GOTO(unlock1, rc = PTR_ERR(handle));
3570 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3574 rc = dt_attr_get(env, parent, la);
3578 la->la_valid = LA_UID | LA_GID;
3579 rc = dt_declare_attr_set(env, child, la, handle);
3583 rc = dt_trans_start_local(env, dev, handle);
3587 dt_write_lock(env, parent, 0);
3588 if (unlikely(lfsck_is_dead_obj(parent)))
3589 GOTO(unlock2, rc = 1);
3591 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3595 /* Get the latest parent's owner. */
3596 rc = dt_attr_get(env, parent, la);
3600 la->la_valid = LA_UID | LA_GID;
3601 rc = dt_attr_set(env, child, la, handle);
3606 dt_write_unlock(env, parent);
3609 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3612 lfsck_ibits_unlock(&lh, LCK_EX);
3616 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3617 "unmatched MDT-OST pair for: parent "DFID
3618 ", child "DFID", comp_id %u, OST-index %u, "
3619 "stripe-index %u, owner %u/%u: rc = %d\n",
3620 lfsck_lfsck2name(com->lc_lfsck),
3621 PFID(lfsck_dto2fid(parent)),
3622 PFID(lfsck_dto2fid(child)),
3623 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3624 la->la_uid, la->la_gid, rc);
3629 /* If there are more than one MDT-objects claim as the OST-object's parent,
3630 * and the OST-object only recognizes one of them, then we need to generate
3631 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3632 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3633 struct lfsck_component *com,
3634 struct dt_object *parent,
3635 struct lfsck_layout_req *llr,
3638 struct lfsck_thread_info *info = lfsck_env_info(env);
3639 struct dt_allocation_hint *hint = &info->lti_hint;
3640 struct dt_object_format *dof = &info->lti_dof;
3641 struct ost_id *oi = &info->lti_oi;
3642 struct lu_buf *buf = &info->lti_big_buf;
3643 struct lfsck_instance *lfsck = com->lc_lfsck;
3644 struct dt_device *dev;
3645 struct lu_device *d =
3646 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3647 struct lu_object *o;
3648 struct lu_object *n;
3649 struct dt_object *child = NULL;
3650 struct thandle *handle = NULL;
3651 struct lov_mds_md_v1 *lmm;
3652 struct lov_ost_data_v1 *objs;
3653 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3655 struct lustre_handle lh = { 0 };
3661 /* We use two separated transactions to repair the inconsistency.
3663 * 1) create the child (OST-object).
3664 * 2) update the parent LOV EA according to the child's FID.
3666 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3667 * handled as orphan when the layout LFSCK run next time.
3669 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3670 * as dangling referened case when the layout LFSCK run next time. */
3672 /* The 1st transaction. */
3673 o = lu_object_anon(env, d, NULL);
3675 GOTO(log, rc = PTR_ERR(o));
3677 n = lu_object_locate(o->lo_header, d->ld_type);
3678 if (unlikely(n == NULL)) {
3679 lu_object_put_nocache(env, o);
3681 GOTO(log, rc = -EINVAL);
3684 child = container_of(n, struct dt_object, do_lu);
3685 memset(hint, 0, sizeof(*hint));
3686 rc = dt_attr_get(env, parent, la);
3690 la->la_valid = LA_UID | LA_GID;
3691 memset(dof, 0, sizeof(*dof));
3693 dev = lfsck_obj2dev(child);
3694 handle = dt_trans_create(env, dev);
3696 GOTO(log, rc = PTR_ERR(handle));
3698 rc = dt_declare_create(env, child, la, hint, dof, handle);
3702 rc = dt_trans_start_local(env, dev, handle);
3706 rc = dt_create(env, child, la, hint, dof, handle);
3707 dt_trans_stop(env, dev, handle);
3712 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3713 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3718 /* The 2nd transaction. */
3720 /* XXX: Generally, we should use bottom device (OSD) to update parent
3721 * LOV EA. But because the LOD-object still references the wrong
3722 * OSP-object that should be detached after the parent's LOV EA
3723 * refreshed. Unfortunately, there is no suitable API for that.
3724 * So we have to make the LOD to re-load the OSP-object(s) via
3725 * replacing the LOV EA against the LOD-object.
3727 * Once the DNE2 patches have been landed, we can replace the
3728 * LOD device with the OSD device. LU-6230. */
3730 dev = lfsck->li_next;
3731 parent = lfsck_object_locate(dev, parent);
3733 GOTO(log, rc = PTR_ERR(parent));
3735 handle = dt_trans_create(env, dev);
3737 GOTO(log, rc = PTR_ERR(handle));
3739 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3740 LU_XATTR_REPLACE, handle);
3744 rc = dt_trans_start_local(env, dev, handle);
3748 dt_write_lock(env, parent, 0);
3749 if (unlikely(lfsck_is_dead_obj(parent)))
3750 GOTO(unlock, rc = 0);
3752 rc = lfsck_layout_get_lovea(env, parent, buf);
3753 if (unlikely(!rc || rc == -ENODATA))
3754 GOTO(unlock, rc = 0);
3757 magic = le32_to_cpu(lmm->lmm_magic);
3758 if (magic == LOV_MAGIC_COMP_V1) {
3759 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3760 struct lov_comp_md_entry_v1 *lcme;
3761 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3764 LASSERT(llr->llr_comp_id != 0);
3766 for (i = 0; i < count; i++) {
3767 lcme = &lcm->lcm_entries[i];
3768 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3769 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3772 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3774 le32_to_cpu(lcme->lcme_offset);
3775 magic = le32_to_cpu(lmm->lmm_magic);
3780 GOTO(unlock, rc = 0);
3784 if (magic == LOV_MAGIC_V1) {
3785 objs = &lmm->lmm_objects[llr->llr_lov_idx];
3787 LASSERT(magic == LOV_MAGIC_V3);
3789 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3792 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3793 index = le32_to_cpu(objs->l_ost_idx);
3794 rc = ostid_to_fid(&tfid, oi, index);
3795 /* Someone changed layout during the LFSCK, no need to repair then. */
3796 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3797 GOTO(unlock, rc = 0);
3799 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3800 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3801 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3802 objs->l_ost_gen = cpu_to_le32(0);
3803 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3804 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3805 LU_XATTR_REPLACE, handle);
3807 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3810 dt_write_unlock(env, parent);
3814 dt_trans_stop(env, dev, handle);
3817 lfsck_ibits_unlock(&lh, LCK_EX);
3819 lfsck_object_put(env, child);
3822 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3823 "multiple references for: parent "DFID", comp_id %u, "
3824 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
3825 lfsck_lfsck2name(lfsck), PFID(pfid),
3826 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3827 la->la_uid, la->la_gid, rc);
3832 /* If the MDT-object and the OST-object have different owner information,
3833 * then trust the MDT-object, because the normal chown/chgrp handle order
3834 * is from MDT to OST, and it is possible that some chown/chgrp operation
3835 * is partly done. */
3836 static int lfsck_layout_repair_owner(const struct lu_env *env,
3837 struct lfsck_component *com,
3838 struct dt_object *parent,
3839 struct lfsck_layout_req *llr,
3840 struct lu_attr *pla,
3841 const struct lu_attr *cla)
3843 struct lfsck_thread_info *info = lfsck_env_info(env);
3844 struct lu_attr *tla = &info->lti_la2;
3845 struct dt_object *child = llr->llr_child;
3846 struct dt_device *dev = lfsck_obj2dev(child);
3847 struct thandle *handle;
3851 tla->la_uid = pla->la_uid;
3852 tla->la_gid = pla->la_gid;
3853 tla->la_valid = LA_UID | LA_GID;
3854 handle = dt_trans_create(env, dev);
3856 GOTO(log, rc = PTR_ERR(handle));
3858 rc = dt_declare_attr_set(env, child, tla, handle);
3862 rc = dt_trans_start_local(env, dev, handle);
3866 /* Use the dt_object lock to serialize with destroy and attr_set. */
3867 dt_read_lock(env, parent, 0);
3868 if (unlikely(lfsck_is_dead_obj(parent)))
3869 GOTO(unlock, rc = 1);
3871 /* Get the latest parent's owner. */
3872 rc = dt_attr_get(env, parent, pla);
3876 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3877 if (unlikely(tla->la_uid != pla->la_uid ||
3878 tla->la_gid != pla->la_gid))
3881 rc = dt_attr_set(env, child, tla, handle);
3886 dt_read_unlock(env, parent);
3889 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3893 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3894 "inconsistent file owner for: parent "DFID", child "DFID
3895 ", OST-index %u, stripe-index %u, old owner %u/%u, "
3896 "new owner %u/%u: rc = %d\n",
3897 lfsck_lfsck2name(com->lc_lfsck),
3898 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
3899 llr->llr_ost_idx, llr->llr_lov_idx,
3900 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
3905 /* Check whether the OST-object correctly back points to the
3906 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3907 static int lfsck_layout_check_parent(const struct lu_env *env,
3908 struct lfsck_component *com,
3909 struct lfsck_assistant_object *lso,
3910 struct filter_fid *ff,
3911 const struct lu_fid *cfid,
3912 const struct lu_attr *cla,
3913 struct lfsck_layout_req *llr)
3915 struct lfsck_thread_info *info = lfsck_env_info(env);
3916 struct lu_buf *buf = &info->lti_big_buf;
3917 struct lu_fid *pfid = &info->lti_fid;
3918 struct dt_object *tobj;
3919 struct lov_mds_md_v1 *lmm;
3920 struct lov_ost_data_v1 *objs;
3921 struct lustre_handle lh = { 0 };
3929 *pfid = ff->ff_parent;
3930 idx = pfid->f_stripe_idx;
3933 if (unlikely(!fid_is_sane(pfid)))
3934 RETURN(LLIT_UNMATCHED_PAIR);
3936 if (lu_fid_eq(pfid, &lso->lso_fid)) {
3937 if (likely(llr->llr_lov_idx == idx))
3940 RETURN(LLIT_UNMATCHED_PAIR);
3943 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3945 RETURN(PTR_ERR(tobj));
3947 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
3948 !S_ISREG(lfsck_object_type(tobj)))
3949 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3951 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3952 * remote one on another MDT. Then check whether the given OST-object
3953 * is in such layout. If yes, it is multiple referenced, otherwise it
3954 * is unmatched referenced case. */
3955 rc = lfsck_layout_get_lovea(env, tobj, buf);
3956 if (rc == 0 || rc == -ENOENT)
3957 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3963 magic = le32_to_cpu(lmm->lmm_magic);
3964 if (magic == LOV_MAGIC_COMP_V1) {
3965 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3966 struct lov_comp_md_entry_v1 *lcme;
3968 if (ff->ff_layout.ol_comp_id == 0)
3969 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3971 count = le16_to_cpu(lcm->lcm_entry_count);
3972 for (i = 0; i < count; i++) {
3973 lcme = &lcm->lcm_entries[i];
3974 if (le32_to_cpu(lcme->lcme_id) ==
3975 ff->ff_layout.ol_comp_id) {
3977 le32_to_cpu(lcme->lcme_offset);
3978 magic = le32_to_cpu(lmm->lmm_magic);
3979 if (!(le32_to_cpu(lcme->lcme_flags) &
3981 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3987 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3991 if (magic == LOV_MAGIC_V1) {
3992 objs = &lmm->lmm_objects[0];
3994 LASSERT(magic == LOV_MAGIC_V3);
3995 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3998 count = le16_to_cpu(lmm->lmm_stripe_count);
3999 for (i = 0; i < count; i++, objs++) {
4000 struct lu_fid *tfid = &info->lti_fid2;
4001 struct ost_id *oi = &info->lti_oi;
4004 if (lovea_slot_is_dummy(objs))
4007 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4008 idx2 = le32_to_cpu(objs->l_ost_idx);
4009 rc = ostid_to_fid(tfid, oi, idx2);
4011 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4012 "invalid layout EA at the slot %d, index %u\n",
4013 lfsck_lfsck2name(com->lc_lfsck),
4014 PFID(pfid), i, idx2);
4016 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4019 if (lu_fid_eq(cfid, tfid)) {
4020 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4021 MDS_INODELOCK_UPDATE |
4022 MDS_INODELOCK_LAYOUT |
4023 MDS_INODELOCK_XATTR,
4028 dt_read_lock(env, tobj, 0);
4030 /* For local MDT-object, re-check existence
4031 * after taken the lock. */
4032 if (!dt_object_remote(tobj)) {
4033 if (dt_object_exists(tobj) == 0 ||
4034 lfsck_is_dead_obj(tobj))
4035 rc = LLIT_UNMATCHED_PAIR;
4037 rc = LLIT_MULTIPLE_REFERENCED;
4042 /* For migration case, the new MDT-object and old
4043 * MDT-object may reference the same OST-object at
4044 * some migration internal time.
4046 * For remote MDT-object, the local MDT may not know
4047 * whether it has been removed or not. Try checking
4048 * for a non-existent xattr to check if this object
4049 * has been been removed or not. */
4050 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4052 if (unlikely(rc == -ENOENT || rc >= 0))
4053 rc = LLIT_UNMATCHED_PAIR;
4054 else if (rc == -ENODATA)
4055 rc = LLIT_MULTIPLE_REFERENCED;
4061 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4064 if (lustre_handle_is_used(&lh)) {
4065 dt_read_unlock(env, tobj);
4066 lfsck_ibits_unlock(&lh, LCK_EX);
4070 lfsck_object_put(env, tobj);
4075 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4076 struct lfsck_component *com,
4077 struct lfsck_assistant_req *lar)
4079 struct lfsck_layout_req *llr =
4080 container_of0(lar, struct lfsck_layout_req, llr_lar);
4081 struct lfsck_assistant_object *lso = lar->lar_parent;
4082 struct lfsck_layout *lo = com->lc_file_ram;
4083 struct lfsck_thread_info *info = lfsck_env_info(env);
4084 struct filter_fid *ff = &info->lti_ff;
4085 struct lu_buf buf = { .lb_buf = ff,
4086 .lb_len = sizeof(*ff) };
4087 struct dt_object *parent = NULL;
4088 struct dt_object *child = llr->llr_child;
4089 struct lu_attr *pla = &lso->lso_attr;
4090 struct lu_attr *cla = &info->lti_la;
4091 struct lfsck_instance *lfsck = com->lc_lfsck;
4092 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4093 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4100 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ASSISTANT_DIRECT, cfs_fail_val);
4102 rc = dt_attr_get(env, child, cla);
4103 if (rc == -ENOENT) {
4104 parent = lfsck_assistant_object_load(env, lfsck, lso);
4105 if (IS_ERR(parent)) {
4106 rc = PTR_ERR(parent);
4108 RETURN(rc == -ENOENT ? 0 : rc);
4111 type = LLIT_DANGLING;
4118 lfsck_buf_init(&buf, ff, sizeof(*ff));
4119 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4120 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4121 type = LLIT_UNMATCHED_PAIR;
4125 if (rc < 0 && rc != -ENODATA)
4128 if (rc == 0 || rc == -ENODATA)
4129 GOTO(check_owner, rc = 0);
4131 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4132 rc = lfsck_layout_check_parent(env, com, lso, ff,
4133 lu_object_fid(&child->do_lu), cla, llr);
4143 /* Someone may has changed the owner after the parent attr pre-loaded.
4144 * It can be handled later inside the lfsck_layout_repair_owner(). */
4145 if (unlikely(cla->la_uid != pla->la_uid ||
4146 cla->la_gid != pla->la_gid)) {
4147 type = LLIT_INCONSISTENT_OWNER;
4152 if (type == LLIT_NONE)
4155 if (bk->lb_param & LPF_DRYRUN)
4158 if (parent == NULL) {
4159 parent = lfsck_assistant_object_load(env, lfsck, lso);
4160 if (IS_ERR(parent)) {
4161 rc = PTR_ERR(parent);
4172 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4173 rc = lfsck_layout_ins_dangling_rec(env, com,
4174 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4175 llr->llr_comp_id, llr->llr_lov_idx,
4178 rc = __lfsck_layout_repair_dangling(env, com, parent,
4185 case LLIT_UNMATCHED_PAIR:
4186 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4189 case LLIT_MULTIPLE_REFERENCED:
4190 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4193 case LLIT_INCONSISTENT_OWNER:
4194 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4204 down_write(&com->lc_sem);
4206 struct lfsck_assistant_data *lad = com->lc_data;
4208 if (unlikely(lad->lad_exit)) {
4210 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4211 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4212 rc == -EHOSTUNREACH) {
4213 /* If cannot touch the target server,
4214 * mark the LFSCK as INCOMPLETE. */
4215 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4216 "talk with OST %x: rc = %d\n",
4217 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4218 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4219 lo->ll_objs_skipped++;
4222 lfsck_layout_record_failure(env, lfsck, lo);
4224 } else if (rc > 0 && (type != LLIT_DANGLING ||
4225 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4226 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4227 "unknown type = %d\n", type);
4229 lo->ll_objs_repaired[type - 1]++;
4230 if (bk->lb_param & LPF_DRYRUN &&
4231 unlikely(lo->ll_pos_first_inconsistent == 0))
4232 lo->ll_pos_first_inconsistent =
4233 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4236 up_write(&com->lc_sem);
4238 if (parent != NULL && !IS_ERR(parent))
4239 lfsck_object_put(env, parent);
4245 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4246 struct lfsck_component *com,
4247 struct dt_object *obj, bool first)
4249 struct lfsck_instance *lfsck = com->lc_lfsck;
4250 struct ptlrpc_thread *thread = &lfsck->li_thread;
4251 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4252 struct lfsck_layout *lo = com->lc_file_ram;
4253 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4256 struct lfsck_layout_dangling_key *parent =
4257 &lfsck_env_info(env)->lti_lldk;
4258 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4263 di = iops->init(env, obj, 0);
4265 RETURN(PTR_ERR(di));
4268 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4270 memset(parent, 0, sizeof(*parent));
4271 rc = iops->get(env, di, (const struct dt_key *)parent);
4276 /* The start one either has been processed or does not exist,
4278 rc = iops->next(env, di);
4284 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4285 unlikely(!thread_is_running(thread)))
4288 key = iops->key(env, di);
4297 lldk_be_to_cpu(parent,
4298 (const struct lfsck_layout_dangling_key *)key);
4299 if (!fid_is_sane(&parent->lldk_fid)) {
4304 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4306 fid_be_to_cpu(cfid, cfid);
4307 ost_idx = cfid->f_ver;
4309 if (!fid_is_sane(cfid)) {
4314 rc = lfsck_layout_repair_dangling(env, com,
4315 &parent->lldk_fid, cfid,
4316 parent->lldk_comp_id,
4317 parent->lldk_ea_off, ost_idx);
4321 down_write(&com->lc_sem);
4322 com->lc_new_checked++;
4323 com->lc_new_scanned++;
4325 lo->ll_lldk_latest_scanned_phase2 = *parent;
4328 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4330 lo->ll_objs_failed_phase2++;
4331 up_write(&com->lc_sem);
4333 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4336 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
4337 cfs_time_current())) &&
4338 com->lc_new_checked != 0) {
4339 down_write(&com->lc_sem);
4340 lo->ll_run_time_phase2 +=
4341 cfs_duration_sec(cfs_time_current() +
4342 HALF_SEC - com->lc_time_last_checkpoint);
4343 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4344 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4345 com->lc_new_checked = 0;
4346 lfsck_layout_store(env, com);
4347 up_write(&com->lc_sem);
4349 com->lc_time_last_checkpoint = cfs_time_current();
4350 com->lc_time_next_checkpoint =
4351 com->lc_time_last_checkpoint +
4352 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4355 lfsck_control_speed_by_self(com);
4356 if (unlikely(!thread_is_running(thread)))
4359 rc = iops->next(env, di);
4368 iops->fini(env, di);
4373 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4374 struct lfsck_component *com)
4376 struct lfsck_assistant_data *lad = com->lc_data;
4377 struct lfsck_instance *lfsck = com->lc_lfsck;
4378 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4379 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4380 struct lfsck_tgt_desc *ltd;
4384 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4385 lfsck_lfsck2name(lfsck));
4387 spin_lock(<ds->ltd_lock);
4388 while (!list_empty(&lad->lad_ost_phase2_list)) {
4389 ltd = list_entry(lad->lad_ost_phase2_list.next,
4390 struct lfsck_tgt_desc,
4391 ltd_layout_phase_list);
4392 list_del_init(<d->ltd_layout_phase_list);
4393 if (bk->lb_param & LPF_OST_ORPHAN) {
4394 spin_unlock(<ds->ltd_lock);
4395 rc = lfsck_layout_scan_orphan(env, com, ltd);
4396 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4399 if (unlikely(lad->lad_exit ||
4400 !thread_is_running(&lfsck->li_thread)))
4402 spin_lock(<ds->ltd_lock);
4406 if (list_empty(&lad->lad_ost_phase1_list))
4410 spin_unlock(<ds->ltd_lock);
4412 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4413 struct lfsck_layout *lo = com->lc_file_ram;
4416 com->lc_new_checked = 0;
4417 com->lc_new_scanned = 0;
4418 com->lc_time_last_checkpoint = cfs_time_current();
4419 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4420 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4422 i = lfsck_sub_trace_file_fid2idx(
4423 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4424 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4425 com->lc_sub_trace_objs[i].lsto_obj, true);
4426 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4427 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4428 com->lc_sub_trace_objs[i].lsto_obj, false);
4430 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4431 "at the No. %d trace file: rc = %d\n",
4432 lfsck_lfsck2name(lfsck), i, rc);
4435 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4436 lfsck_lfsck2name(lfsck), rc);
4442 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4443 struct ptlrpc_request *req,
4446 struct lfsck_layout_slave_async_args *llsaa = args;
4447 struct obd_export *exp = llsaa->llsaa_exp;
4448 struct lfsck_component *com = llsaa->llsaa_com;
4449 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4450 struct lfsck_layout_slave_data *llsd = com->lc_data;
4451 struct lfsck_reply *lr = NULL;
4455 /* It is probably caused by network trouble, or target crash,
4456 * it will try several times (depends on the obd_timeout, and
4457 * will not less than 3 times). But to make the LFSCK can go
4458 * ahead, we should not try for ever. After some try but still
4459 * hit failure, it will assume that the target exit the LFSCK
4460 * prcoessing and stop try. */
4461 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4462 int max_try = max_t(int, obd_timeout / 30, 3);
4464 if (++(llst->llst_failures) > max_try)
4470 llst->llst_failures = 0;
4471 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4472 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4473 lr->lr_status != LS_SCANNING_PHASE2)
4478 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4479 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4480 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4481 llst->llst_failures);
4483 lfsck_layout_llst_del(llsd, llst);
4486 lfsck_layout_llst_put(llst);
4487 lfsck_component_put(env, com);
4488 class_export_put(exp);
4493 static int lfsck_layout_async_query(const struct lu_env *env,
4494 struct lfsck_component *com,
4495 struct obd_export *exp,
4496 struct lfsck_layout_slave_target *llst,
4497 struct lfsck_request *lr,
4498 struct ptlrpc_request_set *set)
4500 struct lfsck_layout_slave_async_args *llsaa;
4501 struct ptlrpc_request *req;
4502 struct lfsck_request *tmp;
4506 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4510 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4512 ptlrpc_request_free(req);
4516 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4518 ptlrpc_request_set_replen(req);
4520 llsaa = ptlrpc_req_async_args(req);
4521 llsaa->llsaa_exp = exp;
4522 llsaa->llsaa_com = lfsck_component_get(com);
4523 llsaa->llsaa_llst = llst;
4524 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4525 req->rq_allow_intr = 1;
4526 ptlrpc_set_add_req(set, req);
4531 static int lfsck_layout_async_notify(const struct lu_env *env,
4532 struct obd_export *exp,
4533 struct lfsck_request *lr,
4534 struct ptlrpc_request_set *set)
4536 struct ptlrpc_request *req;
4537 struct lfsck_request *tmp;
4541 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4545 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4547 ptlrpc_request_free(req);
4551 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4553 ptlrpc_request_set_replen(req);
4554 req->rq_allow_intr = 1;
4555 ptlrpc_set_add_req(set, req);
4561 lfsck_layout_slave_query_master(const struct lu_env *env,
4562 struct lfsck_component *com)
4564 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4565 struct lfsck_instance *lfsck = com->lc_lfsck;
4566 struct lfsck_layout_slave_data *llsd = com->lc_data;
4567 struct lfsck_layout_slave_target *llst;
4568 struct obd_export *exp;
4569 struct ptlrpc_request_set *set;
4574 set = ptlrpc_prep_set();
4576 GOTO(log, rc = -ENOMEM);
4578 memset(lr, 0, sizeof(*lr));
4579 lr->lr_event = LE_QUERY;
4580 lr->lr_active = LFSCK_TYPE_LAYOUT;
4582 llsd->llsd_touch_gen++;
4583 spin_lock(&llsd->llsd_lock);
4584 while (!list_empty(&llsd->llsd_master_list)) {
4585 llst = list_entry(llsd->llsd_master_list.next,
4586 struct lfsck_layout_slave_target,
4588 if (llst->llst_gen == llsd->llsd_touch_gen)
4591 llst->llst_gen = llsd->llsd_touch_gen;
4592 list_move_tail(&llst->llst_list,
4593 &llsd->llsd_master_list);
4594 atomic_inc(&llst->llst_ref);
4595 spin_unlock(&llsd->llsd_lock);
4597 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4600 lfsck_layout_llst_del(llsd, llst);
4601 lfsck_layout_llst_put(llst);
4602 spin_lock(&llsd->llsd_lock);
4606 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4608 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4609 "query %s for layout: rc = %d\n",
4610 lfsck_lfsck2name(lfsck),
4611 exp->exp_obd->obd_name, rc);
4614 lfsck_layout_llst_put(llst);
4615 class_export_put(exp);
4617 spin_lock(&llsd->llsd_lock);
4619 spin_unlock(&llsd->llsd_lock);
4621 rc = ptlrpc_set_wait(set);
4622 ptlrpc_set_destroy(set);
4624 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
4627 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
4628 lfsck_lfsck2name(com->lc_lfsck), rc);
4634 lfsck_layout_slave_notify_master(const struct lu_env *env,
4635 struct lfsck_component *com,
4636 enum lfsck_events event, int result)
4638 struct lfsck_layout *lo = com->lc_file_ram;
4639 struct lfsck_instance *lfsck = com->lc_lfsck;
4640 struct lfsck_layout_slave_data *llsd = com->lc_data;
4641 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4642 struct lfsck_layout_slave_target *llst;
4643 struct obd_export *exp;
4644 struct ptlrpc_request_set *set;
4648 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
4649 lfsck_lfsck2name(com->lc_lfsck));
4651 set = ptlrpc_prep_set();
4655 memset(lr, 0, sizeof(*lr));
4656 lr->lr_event = event;
4657 lr->lr_flags = LEF_FROM_OST;
4658 lr->lr_status = result;
4659 lr->lr_index = lfsck_dev_idx(lfsck);
4660 lr->lr_active = LFSCK_TYPE_LAYOUT;
4661 lr->lr_flags2 = lo->ll_flags;
4662 llsd->llsd_touch_gen++;
4663 spin_lock(&llsd->llsd_lock);
4664 while (!list_empty(&llsd->llsd_master_list)) {
4665 llst = list_entry(llsd->llsd_master_list.next,
4666 struct lfsck_layout_slave_target,
4668 if (llst->llst_gen == llsd->llsd_touch_gen)
4671 llst->llst_gen = llsd->llsd_touch_gen;
4672 list_move_tail(&llst->llst_list,
4673 &llsd->llsd_master_list);
4674 atomic_inc(&llst->llst_ref);
4675 spin_unlock(&llsd->llsd_lock);
4677 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4680 lfsck_layout_llst_del(llsd, llst);
4681 lfsck_layout_llst_put(llst);
4682 spin_lock(&llsd->llsd_lock);
4686 rc = lfsck_layout_async_notify(env, exp, lr, set);
4688 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4689 "notify %s for layout: rc = %d\n",
4690 lfsck_lfsck2name(lfsck),
4691 exp->exp_obd->obd_name, rc);
4693 lfsck_layout_llst_put(llst);
4694 class_export_put(exp);
4695 spin_lock(&llsd->llsd_lock);
4697 spin_unlock(&llsd->llsd_lock);
4699 ptlrpc_set_wait(set);
4700 ptlrpc_set_destroy(set);
4706 * \ret -ENODATA: unrecognized stripe
4707 * \ret = 0 : recognized stripe
4708 * \ret < 0 : other failures
4710 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
4711 struct lfsck_component *com,
4712 struct lu_fid *cfid,
4713 struct lu_fid *pfid, __u32 comp_id)
4715 struct lfsck_thread_info *info = lfsck_env_info(env);
4716 struct lu_buf *buf = &info->lti_big_buf;
4717 struct ost_id *oi = &info->lti_oi;
4718 struct dt_object *obj;
4719 struct lov_mds_md_v1 *lmm;
4720 struct lov_ost_data_v1 *objs;
4721 __u32 idx = pfid->f_stripe_idx;
4729 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4731 RETURN(PTR_ERR(obj));
4733 dt_read_lock(env, obj, 0);
4734 if (unlikely(dt_object_exists(obj) == 0 ||
4735 lfsck_is_dead_obj(obj)))
4736 GOTO(unlock, rc = -ENOENT);
4738 if (!S_ISREG(lfsck_object_type(obj)))
4739 GOTO(unlock, rc = -ENODATA);
4741 rc = lfsck_layout_get_lovea(env, obj, buf);
4746 GOTO(unlock, rc = -ENODATA);
4749 magic = le32_to_cpu(lmm->lmm_magic);
4750 if (magic == LOV_MAGIC_COMP_V1) {
4751 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4752 struct lov_comp_md_entry_v1 *lcme;
4755 GOTO(unlock, rc = -ENODATA);
4757 count = le16_to_cpu(lcm->lcm_entry_count);
4758 for (i = 0; i < count; i++) {
4759 lcme = &lcm->lcm_entries[i];
4760 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
4762 le32_to_cpu(lcme->lcme_offset);
4763 magic = le32_to_cpu(lmm->lmm_magic);
4764 if (!(le32_to_cpu(lcme->lcme_flags) &
4766 GOTO(unlock, rc = -ENODATA);
4772 GOTO(unlock, rc = -ENODATA);
4776 if (magic == LOV_MAGIC_V1) {
4777 objs = &lmm->lmm_objects[0];
4779 LASSERT(magic == LOV_MAGIC_V3);
4780 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4783 fid_to_ostid(cfid, oi);
4784 count = le16_to_cpu(lmm->lmm_stripe_count);
4785 for (i = 0; i < count; i++, objs++) {
4788 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
4789 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
4790 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
4793 GOTO(unlock, rc = -ENODATA);
4796 dt_read_unlock(env, obj);
4797 lfsck_object_put(env, obj);
4803 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
4804 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
4805 * layout EA from MDT to OST. On one hand, the OST no need to understand
4806 * the layout EA structure; on the other hand, it may cause trouble when
4807 * transfer large layout EA from MDT to OST via normal OUT RPC.
4809 * \ret > 0: unrecognized stripe
4810 * \ret = 0: recognized stripe
4811 * \ret < 0: other failures
4813 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
4814 struct lfsck_component *com,
4815 struct lu_fid *cfid,
4816 struct lu_fid *pfid, __u32 comp_id)
4818 struct lfsck_instance *lfsck = com->lc_lfsck;
4819 struct obd_device *obd = lfsck->li_obd;
4820 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4821 struct obd_export *exp = NULL;
4822 struct ptlrpc_request *req = NULL;
4823 struct lfsck_request *lr;
4824 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
4828 if (unlikely(fid_is_idif(pfid)))
4831 fld_range_set_any(range);
4832 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
4834 RETURN(rc == -ENOENT ? 1 : rc);
4836 if (unlikely(!fld_range_is_mdt(range)))
4839 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
4840 if (unlikely(exp == NULL))
4843 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
4844 GOTO(out, rc = -EOPNOTSUPP);
4846 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4848 GOTO(out, rc = -ENOMEM);
4850 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4852 ptlrpc_request_free(req);
4857 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4858 memset(lr, 0, sizeof(*lr));
4859 lr->lr_event = LE_PAIRS_VERIFY;
4860 lr->lr_active = LFSCK_TYPE_LAYOUT;
4861 lr->lr_fid = *cfid; /* OST-object itself FID. */
4862 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
4863 lr->lr_comp_id = comp_id;
4865 ptlrpc_request_set_replen(req);
4866 rc = ptlrpc_queue_wait(req);
4867 ptlrpc_req_finished(req);
4869 if (rc == -ENOENT || rc == -ENODATA)
4876 class_export_put(exp);
4881 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
4882 struct lfsck_component *com,
4883 struct lfsck_req_local *lrl)
4885 struct dt_object *obj;
4889 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
4891 GOTO(log, rc = PTR_ERR(obj));
4893 dt_write_lock(env, obj, 0);
4894 if (unlikely(dt_object_exists(obj) == 0 ||
4895 lfsck_is_dead_obj(obj)))
4896 GOTO(unlock, rc = 0);
4898 rc = __lfsck_layout_update_pfid(env, obj, &lrl->lrl_ff_client.ff_parent,
4899 &lrl->lrl_ff_client.ff_layout,
4900 lrl->lrl_ff_client.ff_parent.f_ver);
4905 dt_write_unlock(env, obj);
4906 lfsck_object_put(env, obj);
4909 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
4910 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
4911 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
4918 static void lfsck_layout_slave_quit(const struct lu_env *env,
4919 struct lfsck_component *com);
4921 static int lfsck_layout_reset(const struct lu_env *env,
4922 struct lfsck_component *com, bool init)
4924 struct lfsck_layout *lo = com->lc_file_ram;
4927 down_write(&com->lc_sem);
4929 memset(lo, 0, com->lc_file_size);
4931 __u32 count = lo->ll_success_count;
4932 __u64 last_time = lo->ll_time_last_complete;
4934 memset(lo, 0, com->lc_file_size);
4935 lo->ll_success_count = count;
4936 lo->ll_time_last_complete = last_time;
4939 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
4940 lo->ll_status = LS_INIT;
4942 if (com->lc_lfsck->li_master) {
4943 struct lfsck_assistant_data *lad = com->lc_data;
4945 lad->lad_incomplete = 0;
4946 CFS_RESET_BITMAP(lad->lad_bitmap);
4949 rc = lfsck_layout_store(env, com);
4950 if (rc == 0 && com->lc_lfsck->li_master)
4951 rc = lfsck_load_sub_trace_files(env, com,
4952 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
4953 up_write(&com->lc_sem);
4955 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
4956 lfsck_lfsck2name(com->lc_lfsck), rc);
4961 static void lfsck_layout_fail(const struct lu_env *env,
4962 struct lfsck_component *com, bool new_checked)
4964 struct lfsck_layout *lo = com->lc_file_ram;
4966 down_write(&com->lc_sem);
4968 com->lc_new_checked++;
4969 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
4970 up_write(&com->lc_sem);
4973 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
4974 struct lfsck_component *com, bool init)
4976 struct lfsck_instance *lfsck = com->lc_lfsck;
4977 struct lfsck_layout *lo = com->lc_file_ram;
4981 rc = lfsck_checkpoint_generic(env, com);
4983 return rc > 0 ? 0 : rc;
4986 down_write(&com->lc_sem);
4988 lo->ll_pos_latest_start =
4989 lfsck->li_pos_checkpoint.lp_oit_cookie;
4991 lo->ll_pos_last_checkpoint =
4992 lfsck->li_pos_checkpoint.lp_oit_cookie;
4993 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4994 HALF_SEC - lfsck->li_time_last_checkpoint);
4995 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4996 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4997 com->lc_new_checked = 0;
5000 rc = lfsck_layout_store(env, com);
5001 up_write(&com->lc_sem);
5003 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5004 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5005 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5010 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5011 struct lfsck_component *com, bool init)
5013 struct lfsck_instance *lfsck = com->lc_lfsck;
5014 struct lfsck_layout *lo = com->lc_file_ram;
5017 if (com->lc_new_checked == 0 && !init)
5020 down_write(&com->lc_sem);
5022 lo->ll_pos_latest_start =
5023 lfsck->li_pos_checkpoint.lp_oit_cookie;
5025 lo->ll_pos_last_checkpoint =
5026 lfsck->li_pos_checkpoint.lp_oit_cookie;
5027 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
5028 HALF_SEC - lfsck->li_time_last_checkpoint);
5029 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5030 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5031 com->lc_new_checked = 0;
5034 rc = lfsck_layout_store(env, com);
5035 up_write(&com->lc_sem);
5037 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5038 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5039 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5044 static int lfsck_layout_prep(const struct lu_env *env,
5045 struct lfsck_component *com,
5046 struct lfsck_start *start)
5048 struct lfsck_instance *lfsck = com->lc_lfsck;
5049 struct lfsck_layout *lo = com->lc_file_ram;
5050 struct lfsck_position *pos = &com->lc_pos_start;
5052 fid_zero(&pos->lp_dir_parent);
5053 pos->lp_dir_cookie = 0;
5054 if (lo->ll_status == LS_COMPLETED ||
5055 lo->ll_status == LS_PARTIAL ||
5056 /* To handle orphan, must scan from the beginning. */
5057 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5060 rc = lfsck_layout_reset(env, com, false);
5062 rc = lfsck_set_param(env, lfsck, start, true);
5065 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5066 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5072 down_write(&com->lc_sem);
5073 lo->ll_time_latest_start = cfs_time_current_sec();
5074 spin_lock(&lfsck->li_lock);
5075 if (lo->ll_flags & LF_SCANNED_ONCE) {
5076 if (!lfsck->li_drop_dryrun ||
5077 lo->ll_pos_first_inconsistent == 0) {
5078 lo->ll_status = LS_SCANNING_PHASE2;
5079 list_move_tail(&com->lc_link,
5080 &lfsck->li_list_double_scan);
5081 pos->lp_oit_cookie = 0;
5085 lo->ll_status = LS_SCANNING_PHASE1;
5086 lo->ll_run_time_phase1 = 0;
5087 lo->ll_run_time_phase2 = 0;
5088 lo->ll_objs_checked_phase1 = 0;
5089 lo->ll_objs_checked_phase2 = 0;
5090 lo->ll_objs_failed_phase1 = 0;
5091 lo->ll_objs_failed_phase2 = 0;
5092 for (i = 0; i < LLIT_MAX; i++)
5093 lo->ll_objs_repaired[i] = 0;
5095 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5096 fid_zero(&com->lc_fid_latest_scanned_phase2);
5099 lo->ll_status = LS_SCANNING_PHASE1;
5100 if (!lfsck->li_drop_dryrun ||
5101 lo->ll_pos_first_inconsistent == 0)
5102 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5104 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5106 spin_unlock(&lfsck->li_lock);
5107 up_write(&com->lc_sem);
5112 static int lfsck_layout_slave_prep(const struct lu_env *env,
5113 struct lfsck_component *com,
5114 struct lfsck_start_param *lsp)
5116 struct lfsck_layout_slave_data *llsd = com->lc_data;
5117 struct lfsck_instance *lfsck = com->lc_lfsck;
5118 struct lfsck_layout *lo = com->lc_file_ram;
5119 struct lfsck_start *start = lsp->lsp_start;
5122 rc = lfsck_layout_prep(env, com, start);
5126 if (lo->ll_flags & LF_CRASHED_LASTID &&
5127 list_empty(&llsd->llsd_master_list)) {
5128 LASSERT(lfsck->li_out_notify != NULL);
5130 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5131 LE_LASTID_REBUILDING);
5134 if (!lsp->lsp_index_valid)
5137 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5138 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5139 LASSERT(!llsd->llsd_rbtree_valid);
5141 write_lock(&llsd->llsd_rb_lock);
5142 rc = lfsck_rbtree_setup(env, com);
5143 write_unlock(&llsd->llsd_rb_lock);
5146 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5147 "%llu]\n", lfsck_lfsck2name(lfsck),
5148 com->lc_pos_start.lp_oit_cookie);
5153 static int lfsck_layout_master_prep(const struct lu_env *env,
5154 struct lfsck_component *com,
5155 struct lfsck_start_param *lsp)
5160 rc = lfsck_layout_load_bitmap(env, com);
5162 rc = lfsck_layout_reset(env, com, false);
5164 rc = lfsck_set_param(env, com->lc_lfsck,
5165 lsp->lsp_start, true);
5171 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5175 rc = lfsck_start_assistant(env, com, lsp);
5180 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5181 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5182 com->lc_pos_start.lp_oit_cookie);
5187 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5188 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5189 struct lfsck_component *com,
5190 struct dt_object *parent,
5191 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5193 struct lfsck_thread_info *info = lfsck_env_info(env);
5194 struct lfsck_instance *lfsck = com->lc_lfsck;
5195 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5196 struct lfsck_layout *lo = com->lc_file_ram;
5197 struct lfsck_assistant_data *lad = com->lc_data;
5198 struct lfsck_assistant_object *lso = NULL;
5199 struct lov_ost_data_v1 *objs;
5200 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5201 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5202 struct ptlrpc_thread *athread = &lad->lad_thread;
5203 struct l_wait_info lwi = { 0 };
5211 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5212 magic = le32_to_cpu(lmm->lmm_magic);
5213 if (magic == LOV_MAGIC_V1) {
5214 objs = &lmm->lmm_objects[0];
5216 LASSERT(magic == LOV_MAGIC_V3);
5217 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5220 count = le16_to_cpu(lmm->lmm_stripe_count);
5221 for (i = 0; i < count; i++, objs++) {
5222 struct lu_fid *fid = &info->lti_fid;
5223 struct ost_id *oi = &info->lti_oi;
5224 struct lfsck_layout_req *llr;
5225 struct lfsck_tgt_desc *tgt = NULL;
5226 struct dt_object *cobj = NULL;
5228 bool wakeup = false;
5230 if (unlikely(lovea_slot_is_dummy(objs)))
5233 l_wait_event(mthread->t_ctl_waitq,
5234 lad->lad_prefetched < bk->lb_async_windows ||
5235 !thread_is_running(mthread) ||
5236 thread_is_stopped(athread),
5239 if (unlikely(!thread_is_running(mthread)) ||
5240 thread_is_stopped(athread))
5243 if (unlikely(lfsck_is_dead_obj(parent)))
5246 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5247 index = le32_to_cpu(objs->l_ost_idx);
5248 rc = ostid_to_fid(fid, oi, index);
5250 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5251 ": "DOSTID", idx %u, comp_id %u\n",
5252 lfsck_lfsck2name(lfsck),
5253 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5258 tgt = lfsck_tgt_get(ltds, index);
5259 if (unlikely(tgt == NULL)) {
5260 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5261 "did not join the layout LFSCK, comp_id %u\n",
5262 lfsck_lfsck2name(lfsck), index, comp_id);
5263 lfsck_lad_set_bitmap(env, com, index);
5267 /* There is potential deadlock race condition between object
5268 * destroy and layout LFSCK. Consider the following scenario:
5270 * 1) The LFSCK thread obtained the parent object firstly, at
5271 * that time, the parent object has not been destroyed yet.
5273 * 2) One RPC service thread destroyed the parent and all its
5274 * children objects. Because the LFSCK is referencing the
5275 * parent object, then the parent object will be marked as
5276 * dying in RAM. On the other hand, the parent object is
5277 * referencing all its children objects, then all children
5278 * objects will be marked as dying in RAM also.
5280 * 3) The LFSCK thread tries to find some child object with
5281 * the parent object referenced. Then it will find that the
5282 * child object is dying. According to the object visibility
5283 * rules: the object with dying flag cannot be returned to
5284 * others. So the LFSCK thread has to wait until the dying
5285 * object has been purged from RAM, then it can allocate a
5286 * new object (with the same FID) in RAM. Unfortunately, the
5287 * LFSCK thread itself is referencing the parent object, and
5288 * cause the parent object cannot be purged, then cause the
5289 * child object cannot be purged also. So the LFSCK thread
5290 * will fall into deadlock.
5292 * We introduce non-blocked version lu_object_find() to allow
5293 * the LFSCK thread to return failure immediately (instead of
5294 * wait) when it finds dying (child) object, then the LFSCK
5295 * thread can check whether the parent object is dying or not.
5296 * So avoid above deadlock. LU-5395 */
5297 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
5299 if (lfsck_is_dead_obj(parent)) {
5309 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_ASSISTANT_DIRECT)) {
5310 rc = dt_declare_attr_get(env, cobj);
5314 rc = dt_declare_xattr_get(env, cobj, &buf,
5321 struct lu_attr *attr = &info->lti_la;
5323 rc = dt_attr_get(env, parent, attr);
5327 lso = lfsck_assistant_object_init(env,
5328 lfsck_dto2fid(parent), attr,
5329 lfsck->li_pos_current.lp_oit_cookie, false);
5338 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5346 spin_lock(&lad->lad_lock);
5347 if (lad->lad_assistant_status < 0) {
5348 spin_unlock(&lad->lad_lock);
5349 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5351 RETURN(lad->lad_assistant_status);
5354 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5355 if (lad->lad_prefetched == 0)
5358 lad->lad_prefetched++;
5359 spin_unlock(&lad->lad_lock);
5361 wake_up_all(&athread->t_ctl_waitq);
5364 down_write(&com->lc_sem);
5365 com->lc_new_checked++;
5367 lfsck_layout_record_failure(env, lfsck, lo);
5368 up_write(&com->lc_sem);
5370 if (cobj != NULL && !IS_ERR(cobj))
5371 lfsck_object_put(env, cobj);
5373 if (likely(tgt != NULL))
5376 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5384 lfsck_assistant_object_put(env, lso);
5389 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5390 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5391 * list ::lad_req_list.
5393 * For each request on above list, the lfsck_layout_assistant thread compares
5394 * the OST side attribute with local attribute, if inconsistent, then repair it.
5396 * All above processing is async mode with pipeline. */
5397 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5398 struct lfsck_component *com,
5399 struct dt_object *obj)
5401 struct lfsck_thread_info *info = lfsck_env_info(env);
5402 struct ost_id *oi = &info->lti_oi;
5403 struct lfsck_layout *lo = com->lc_file_ram;
5404 struct lfsck_assistant_data *lad = com->lc_data;
5405 struct lfsck_instance *lfsck = com->lc_lfsck;
5406 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5407 struct thandle *handle = NULL;
5408 struct lu_buf *buf = &info->lti_big_buf;
5409 struct lov_mds_md_v1 *lmm = NULL;
5410 struct dt_device *dev = lfsck_obj2dev(obj);
5411 struct lustre_handle lh = { 0 };
5412 struct lu_buf ea_buf = { NULL };
5413 struct lov_comp_md_v1 *lcm = NULL;
5414 struct lov_comp_md_entry_v1 *lcme = NULL;
5419 bool locked = false;
5420 bool stripe = false;
5421 bool bad_oi = false;
5424 if (!S_ISREG(lfsck_object_type(obj)))
5427 if (lad->lad_assistant_status < 0)
5428 GOTO(out, rc = -ESRCH);
5430 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5431 lmm_oi_cpu_to_le(oi, oi);
5432 dt_read_lock(env, obj, 0);
5437 if (dt_object_exists(obj) == 0 ||
5438 lfsck_is_dead_obj(obj))
5441 rc = lfsck_layout_get_lovea(env, obj, buf);
5443 /* Skip bad lov EA during the 1st cycle scanning, and
5444 * try to recover it via orphan in the 2nd scanning. */
5445 GOTO(out, rc = (rc == -EINVAL ? 0 : rc));
5449 magic = le32_to_cpu(lmm->lmm_magic);
5450 if (magic == LOV_MAGIC_COMP_V1) {
5454 count = le16_to_cpu(lcm->lcm_entry_count);
5455 for (i = 0; i < count; i++) {
5456 lcme = &lcm->lcm_entries[i];
5457 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5458 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) != 0)
5462 GOTO(out, stripe = true);
5463 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5464 GOTO(out, stripe = true);
5468 /* Inconsistent lmm_oi, should be repaired. */
5471 if (bk->lb_param & LPF_DRYRUN) {
5472 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5474 GOTO(out, stripe = true);
5477 if (!lustre_handle_is_used(&lh)) {
5478 dt_read_unlock(env, obj);
5480 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5481 MDS_INODELOCK_LAYOUT |
5482 MDS_INODELOCK_XATTR, LCK_EX);
5486 handle = dt_trans_create(env, dev);
5488 GOTO(out, rc = PTR_ERR(handle));
5490 lfsck_buf_init(&ea_buf, lmm, size);
5491 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5492 LU_XATTR_REPLACE, handle);
5496 rc = dt_trans_start_local(env, dev, handle);
5500 dt_write_lock(env, obj, 0);
5506 if (magic == LOV_MAGIC_COMP_V1) {
5509 for (i = 0; i < count; i++) {
5510 lcme = &lcm->lcm_entries[i];
5511 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5518 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5519 LU_XATTR_REPLACE, handle);
5523 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5525 GOTO(out, stripe = true);
5529 if (lustre_handle_is_used(&lh))
5530 dt_write_unlock(env, obj);
5532 dt_read_unlock(env, obj);
5535 if (handle != NULL && !IS_ERR(handle))
5536 dt_trans_stop(env, dev, handle);
5538 lfsck_ibits_unlock(&lh, LCK_EX);
5541 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5542 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5543 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5544 PFID(lfsck_dto2fid(obj)), rc);
5547 if (magic == LOV_MAGIC_COMP_V1) {
5550 for (i = 0; i < count; i++) {
5551 lcme = &lcm->lcm_entries[i];
5552 if (!(le32_to_cpu(lcme->lcme_flags) &
5556 rc = lfsck_layout_scan_stripes(env, com, obj,
5557 (struct lov_mds_md_v1 *)(buf->lb_buf +
5558 le32_to_cpu(lcme->lcme_offset)),
5559 le32_to_cpu(lcme->lcme_id));
5562 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5565 down_write(&com->lc_sem);
5566 com->lc_new_checked++;
5568 lfsck_layout_record_failure(env, lfsck, lo);
5569 up_write(&com->lc_sem);
5575 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5576 struct lfsck_component *com,
5577 struct dt_object *obj)
5579 struct lfsck_instance *lfsck = com->lc_lfsck;
5580 struct lfsck_layout *lo = com->lc_file_ram;
5581 const struct lu_fid *fid = lfsck_dto2fid(obj);
5582 struct lfsck_layout_slave_data *llsd = com->lc_data;
5583 struct lfsck_layout_seq *lls;
5589 LASSERT(llsd != NULL);
5591 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5592 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5593 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
5595 struct ptlrpc_thread *thread = &lfsck->li_thread;
5597 l_wait_event(thread->t_ctl_waitq,
5598 !thread_is_running(thread),
5602 lfsck_rbtree_update_bitmap(env, com, fid, false);
5604 down_write(&com->lc_sem);
5605 if (fid_is_idif(fid))
5607 else if (!fid_is_norm(fid) ||
5608 !fid_is_for_ostobj(env, lfsck, obj, fid))
5609 GOTO(unlock, rc = 0);
5612 com->lc_new_checked++;
5614 lls = lfsck_layout_seq_lookup(llsd, seq);
5617 if (unlikely(lls == NULL))
5618 GOTO(unlock, rc = -ENOMEM);
5620 INIT_LIST_HEAD(&lls->lls_list);
5622 rc = lfsck_layout_lastid_load(env, com, lls);
5624 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5625 "load LAST_ID for %#llx: rc = %d\n",
5626 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5627 lo->ll_objs_failed_phase1++;
5632 lfsck_layout_seq_insert(llsd, lls);
5635 if (unlikely(fid_is_last_id(fid)))
5636 GOTO(unlock, rc = 0);
5638 if (fid_is_idif(fid))
5639 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5643 if (oid > lls->lls_lastid_known)
5644 lls->lls_lastid_known = oid;
5646 if (oid > lls->lls_lastid) {
5647 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
5648 /* OFD may create new objects during LFSCK scanning. */
5649 rc = lfsck_layout_lastid_reload(env, com, lls);
5650 if (unlikely(rc != 0)) {
5651 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5652 "reload LAST_ID for %#llx: rc = %d\n",
5653 lfsck_lfsck2name(com->lc_lfsck),
5659 if (oid <= lls->lls_lastid ||
5660 lo->ll_flags & LF_CRASHED_LASTID)
5661 GOTO(unlock, rc = 0);
5663 LASSERT(lfsck->li_out_notify != NULL);
5665 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5666 LE_LASTID_REBUILDING);
5667 lo->ll_flags |= LF_CRASHED_LASTID;
5669 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
5670 "LAST_ID file (2) for the sequence %#llx"
5671 ", old value %llu, known value %llu\n",
5672 lfsck_lfsck2name(lfsck), lls->lls_seq,
5673 lls->lls_lastid, oid);
5676 lls->lls_lastid = oid;
5680 GOTO(unlock, rc = 0);
5683 up_write(&com->lc_sem);
5688 static int lfsck_layout_exec_dir(const struct lu_env *env,
5689 struct lfsck_component *com,
5690 struct lfsck_assistant_object *lso,
5691 struct lu_dirent *ent, __u16 type)
5696 static int lfsck_layout_master_post(const struct lu_env *env,
5697 struct lfsck_component *com,
5698 int result, bool init)
5700 struct lfsck_instance *lfsck = com->lc_lfsck;
5701 struct lfsck_layout *lo = com->lc_file_ram;
5705 lfsck_post_generic(env, com, &result);
5707 down_write(&com->lc_sem);
5708 spin_lock(&lfsck->li_lock);
5710 lo->ll_pos_last_checkpoint =
5711 lfsck->li_pos_checkpoint.lp_oit_cookie;
5714 if (lo->ll_flags & LF_INCOMPLETE)
5715 lo->ll_status = LS_PARTIAL;
5717 lo->ll_status = LS_SCANNING_PHASE2;
5718 lo->ll_flags |= LF_SCANNED_ONCE;
5719 lo->ll_flags &= ~LF_UPGRADE;
5720 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5721 } else if (result == 0) {
5722 if (lfsck->li_status != 0)
5723 lo->ll_status = lfsck->li_status;
5725 lo->ll_status = LS_STOPPED;
5726 if (lo->ll_status != LS_PAUSED)
5727 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5729 lo->ll_status = LS_FAILED;
5730 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5732 spin_unlock(&lfsck->li_lock);
5735 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
5736 HALF_SEC - lfsck->li_time_last_checkpoint);
5737 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5738 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5739 com->lc_new_checked = 0;
5742 rc = lfsck_layout_store(env, com);
5743 up_write(&com->lc_sem);
5745 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
5746 lfsck_lfsck2name(lfsck), rc);
5751 static int lfsck_layout_slave_post(const struct lu_env *env,
5752 struct lfsck_component *com,
5753 int result, bool init)
5755 struct lfsck_instance *lfsck = com->lc_lfsck;
5756 struct lfsck_layout *lo = com->lc_file_ram;
5760 down_write(&com->lc_sem);
5761 rc = lfsck_layout_lastid_store(env, com);
5765 LASSERT(lfsck->li_out_notify != NULL);
5767 spin_lock(&lfsck->li_lock);
5769 lo->ll_pos_last_checkpoint =
5770 lfsck->li_pos_checkpoint.lp_oit_cookie;
5773 lo->ll_status = LS_SCANNING_PHASE2;
5774 lo->ll_flags |= LF_SCANNED_ONCE;
5775 if (lo->ll_flags & LF_CRASHED_LASTID) {
5777 lo->ll_flags &= ~LF_CRASHED_LASTID;
5779 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
5780 "crashed LAST_ID files successfully\n",
5781 lfsck_lfsck2name(lfsck));
5783 lo->ll_flags &= ~LF_UPGRADE;
5784 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5785 } else if (result == 0) {
5786 if (lfsck->li_status != 0)
5787 lo->ll_status = lfsck->li_status;
5789 lo->ll_status = LS_STOPPED;
5790 if (lo->ll_status != LS_PAUSED)
5791 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5793 lo->ll_status = LS_FAILED;
5794 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5796 spin_unlock(&lfsck->li_lock);
5799 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5803 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
5804 HALF_SEC - lfsck->li_time_last_checkpoint);
5805 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5806 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5807 com->lc_new_checked = 0;
5810 rc = lfsck_layout_store(env, com);
5811 up_write(&com->lc_sem);
5813 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
5815 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
5816 lfsck_lfsck2name(lfsck), rc);
5821 static void lfsck_layout_dump(const struct lu_env *env,
5822 struct lfsck_component *com, struct seq_file *m)
5824 struct lfsck_instance *lfsck = com->lc_lfsck;
5825 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5826 struct lfsck_layout *lo = com->lc_file_ram;
5828 down_read(&com->lc_sem);
5829 seq_printf(m, "name: lfsck_layout\n"
5835 lfsck_status2name(lo->ll_status));
5837 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
5839 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
5841 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
5843 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
5845 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
5847 seq_printf(m, "latest_start_position: %llu\n"
5848 "last_checkpoint_position: %llu\n"
5849 "first_failure_position: %llu\n",
5850 lo->ll_pos_latest_start,
5851 lo->ll_pos_last_checkpoint,
5852 lo->ll_pos_first_inconsistent);
5854 seq_printf(m, "success_count: %u\n"
5855 "repaired_dangling: %llu\n"
5856 "repaired_unmatched_pair: %llu\n"
5857 "repaired_multiple_referenced: %llu\n"
5858 "repaired_orphan: %llu\n"
5859 "repaired_inconsistent_owner: %llu\n"
5860 "repaired_others: %llu\n"
5862 "failed_phase1: %llu\n"
5863 "failed_phase2: %llu\n",
5864 lo->ll_success_count,
5865 lo->ll_objs_repaired[LLIT_DANGLING - 1],
5866 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
5867 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
5868 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
5869 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
5870 lo->ll_objs_repaired[LLIT_OTHERS - 1],
5871 lo->ll_objs_skipped,
5872 lo->ll_objs_failed_phase1,
5873 lo->ll_objs_failed_phase2);
5875 if (lo->ll_status == LS_SCANNING_PHASE1) {
5877 cfs_duration_t duration = cfs_time_current() -
5878 lfsck->li_time_last_checkpoint;
5879 __u64 checked = lo->ll_objs_checked_phase1 +
5880 com->lc_new_checked;
5881 __u64 speed = checked;
5882 __u64 new_checked = com->lc_new_checked *
5883 msecs_to_jiffies(MSEC_PER_SEC);
5884 __u32 rtime = lo->ll_run_time_phase1 +
5885 cfs_duration_sec(duration + HALF_SEC);
5888 do_div(new_checked, duration);
5890 do_div(speed, rtime);
5891 seq_printf(m, "checked_phase1: %llu\n"
5892 "checked_phase2: %llu\n"
5893 "run_time_phase1: %u seconds\n"
5894 "run_time_phase2: %u seconds\n"
5895 "average_speed_phase1: %llu items/sec\n"
5896 "average_speed_phase2: N/A\n"
5897 "real-time_speed_phase1: %llu items/sec\n"
5898 "real-time_speed_phase2: N/A\n",
5900 lo->ll_objs_checked_phase2,
5902 lo->ll_run_time_phase2,
5906 if (likely(lfsck->li_di_oit)) {
5907 const struct dt_it_ops *iops =
5908 &lfsck->li_obj_oit->do_index_ops->dio_it;
5910 /* The low layer otable-based iteration position may NOT
5911 * exactly match the layout-based directory traversal
5912 * cookie. Generally, it is not a serious issue. But the
5913 * caller should NOT make assumption on that. */
5914 pos = iops->store(env, lfsck->li_di_oit);
5915 if (!lfsck->li_current_oit_processed)
5918 pos = lo->ll_pos_last_checkpoint;
5921 seq_printf(m, "current_position: %llu\n", pos);
5922 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
5923 cfs_duration_t duration = cfs_time_current() -
5924 com->lc_time_last_checkpoint;
5925 __u64 checked = lo->ll_objs_checked_phase2 +
5926 com->lc_new_checked;
5927 __u64 speed1 = lo->ll_objs_checked_phase1;
5928 __u64 speed2 = checked;
5929 __u64 new_checked = com->lc_new_checked *
5930 msecs_to_jiffies(MSEC_PER_SEC);
5931 __u32 rtime = lo->ll_run_time_phase2 +
5932 cfs_duration_sec(duration + HALF_SEC);
5935 do_div(new_checked, duration);
5936 if (lo->ll_run_time_phase1 != 0)
5937 do_div(speed1, lo->ll_run_time_phase1);
5939 do_div(speed2, rtime);
5940 seq_printf(m, "checked_phase1: %llu\n"
5941 "checked_phase2: %llu\n"
5942 "run_time_phase1: %u seconds\n"
5943 "run_time_phase2: %u seconds\n"
5944 "average_speed_phase1: %llu items/sec\n"
5945 "average_speed_phase2: %llu items/sec\n"
5946 "real-time_speed_phase1: N/A\n"
5947 "real-time_speed_phase2: %llu items/sec\n"
5948 "current_position: "DFID"\n",
5949 lo->ll_objs_checked_phase1,
5951 lo->ll_run_time_phase1,
5956 PFID(&com->lc_fid_latest_scanned_phase2));
5958 __u64 speed1 = lo->ll_objs_checked_phase1;
5959 __u64 speed2 = lo->ll_objs_checked_phase2;
5961 if (lo->ll_run_time_phase1 != 0)
5962 do_div(speed1, lo->ll_run_time_phase1);
5963 if (lo->ll_run_time_phase2 != 0)
5964 do_div(speed2, lo->ll_run_time_phase2);
5965 seq_printf(m, "checked_phase1: %llu\n"
5966 "checked_phase2: %llu\n"
5967 "run_time_phase1: %u seconds\n"
5968 "run_time_phase2: %u seconds\n"
5969 "average_speed_phase1: %llu items/sec\n"
5970 "average_speed_phase2: %llu objs/sec\n"
5971 "real-time_speed_phase1: N/A\n"
5972 "real-time_speed_phase2: N/A\n"
5973 "current_position: N/A\n",
5974 lo->ll_objs_checked_phase1,
5975 lo->ll_objs_checked_phase2,
5976 lo->ll_run_time_phase1,
5977 lo->ll_run_time_phase2,
5982 up_read(&com->lc_sem);
5985 static int lfsck_layout_master_double_scan(const struct lu_env *env,
5986 struct lfsck_component *com)
5988 struct lfsck_layout *lo = com->lc_file_ram;
5989 struct lfsck_assistant_data *lad = com->lc_data;
5990 struct lfsck_instance *lfsck = com->lc_lfsck;
5991 struct lfsck_tgt_descs *ltds;
5992 struct lfsck_tgt_desc *ltd;
5993 struct lfsck_tgt_desc *next;
5996 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
5998 if (thread_is_stopped(&lad->lad_thread)) {
5999 LASSERT(list_empty(&lad->lad_req_list));
6000 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6001 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6003 ltds = &lfsck->li_ost_descs;
6004 spin_lock(<ds->ltd_lock);
6005 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6006 ltd_layout_phase_list) {
6007 list_del_init(<d->ltd_layout_phase_list);
6009 spin_unlock(<ds->ltd_lock);
6011 ltds = &lfsck->li_mdt_descs;
6012 spin_lock(<ds->ltd_lock);
6013 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6014 ltd_layout_phase_list) {
6015 list_del_init(<d->ltd_layout_phase_list);
6017 spin_unlock(<ds->ltd_lock);
6023 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6024 struct lfsck_component *com)
6026 struct lfsck_instance *lfsck = com->lc_lfsck;
6027 struct lfsck_layout_slave_data *llsd = com->lc_data;
6028 struct lfsck_layout *lo = com->lc_file_ram;
6029 struct ptlrpc_thread *thread = &lfsck->li_thread;
6033 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6034 lfsck_lfsck2name(lfsck));
6036 atomic_inc(&lfsck->li_double_scan_count);
6038 if (lo->ll_flags & LF_INCOMPLETE)
6041 com->lc_new_checked = 0;
6042 com->lc_new_scanned = 0;
6043 com->lc_time_last_checkpoint = cfs_time_current();
6044 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6045 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6048 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
6051 rc = lfsck_layout_slave_query_master(env, com);
6052 if (list_empty(&llsd->llsd_master_list)) {
6053 if (unlikely(!thread_is_running(thread)))
6064 rc = l_wait_event(thread->t_ctl_waitq,
6065 !thread_is_running(thread) ||
6066 lo->ll_flags & LF_INCOMPLETE ||
6067 list_empty(&llsd->llsd_master_list),
6069 if (unlikely(!thread_is_running(thread)))
6072 if (lo->ll_flags & LF_INCOMPLETE)
6075 if (rc == -ETIMEDOUT)
6078 GOTO(done, rc = (rc < 0 ? rc : 1));
6082 rc = lfsck_layout_double_scan_result(env, com, rc);
6083 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6084 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6085 lfsck_layout_slave_quit(env, com);
6086 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6087 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6089 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6090 "status %d: rc = %d\n",
6091 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6096 static void lfsck_layout_master_data_release(const struct lu_env *env,
6097 struct lfsck_component *com)
6099 struct lfsck_assistant_data *lad = com->lc_data;
6100 struct lfsck_instance *lfsck = com->lc_lfsck;
6101 struct lfsck_tgt_descs *ltds;
6102 struct lfsck_tgt_desc *ltd;
6103 struct lfsck_tgt_desc *next;
6105 LASSERT(lad != NULL);
6106 LASSERT(thread_is_init(&lad->lad_thread) ||
6107 thread_is_stopped(&lad->lad_thread));
6108 LASSERT(list_empty(&lad->lad_req_list));
6110 com->lc_data = NULL;
6112 ltds = &lfsck->li_ost_descs;
6113 spin_lock(<ds->ltd_lock);
6114 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6115 ltd_layout_phase_list) {
6116 list_del_init(<d->ltd_layout_phase_list);
6118 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6119 ltd_layout_phase_list) {
6120 list_del_init(<d->ltd_layout_phase_list);
6122 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6124 list_del_init(<d->ltd_layout_list);
6126 spin_unlock(<ds->ltd_lock);
6128 ltds = &lfsck->li_mdt_descs;
6129 spin_lock(<ds->ltd_lock);
6130 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6131 ltd_layout_phase_list) {
6132 list_del_init(<d->ltd_layout_phase_list);
6134 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6135 ltd_layout_phase_list) {
6136 list_del_init(<d->ltd_layout_phase_list);
6138 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6140 list_del_init(<d->ltd_layout_list);
6142 spin_unlock(<ds->ltd_lock);
6144 if (likely(lad->lad_bitmap != NULL))
6145 CFS_FREE_BITMAP(lad->lad_bitmap);
6150 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6151 struct lfsck_component *com)
6153 struct lfsck_layout_slave_data *llsd = com->lc_data;
6155 lfsck_layout_slave_quit(env, com);
6156 com->lc_data = NULL;
6160 static void lfsck_layout_master_quit(const struct lu_env *env,
6161 struct lfsck_component *com)
6163 struct lfsck_assistant_data *lad = com->lc_data;
6164 struct lfsck_instance *lfsck = com->lc_lfsck;
6165 struct lfsck_tgt_descs *ltds;
6166 struct lfsck_tgt_desc *ltd;
6167 struct lfsck_tgt_desc *next;
6169 LASSERT(lad != NULL);
6171 lfsck_quit_generic(env, com);
6173 LASSERT(thread_is_init(&lad->lad_thread) ||
6174 thread_is_stopped(&lad->lad_thread));
6175 LASSERT(list_empty(&lad->lad_req_list));
6177 ltds = &lfsck->li_ost_descs;
6178 spin_lock(<ds->ltd_lock);
6179 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6180 ltd_layout_phase_list) {
6181 list_del_init(<d->ltd_layout_phase_list);
6183 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6184 ltd_layout_phase_list) {
6185 list_del_init(<d->ltd_layout_phase_list);
6187 spin_unlock(<ds->ltd_lock);
6189 ltds = &lfsck->li_mdt_descs;
6190 spin_lock(<ds->ltd_lock);
6191 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6192 ltd_layout_phase_list) {
6193 list_del_init(<d->ltd_layout_phase_list);
6195 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6196 ltd_layout_phase_list) {
6197 list_del_init(<d->ltd_layout_phase_list);
6199 spin_unlock(<ds->ltd_lock);
6202 static void lfsck_layout_slave_quit(const struct lu_env *env,
6203 struct lfsck_component *com)
6205 struct lfsck_layout_slave_data *llsd = com->lc_data;
6206 struct lfsck_layout_seq *lls;
6207 struct lfsck_layout_seq *next;
6208 struct lfsck_layout_slave_target *llst;
6210 LASSERT(llsd != NULL);
6212 down_write(&com->lc_sem);
6213 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6215 list_del_init(&lls->lls_list);
6216 lfsck_object_put(env, lls->lls_lastid_obj);
6219 up_write(&com->lc_sem);
6221 spin_lock(&llsd->llsd_lock);
6222 while (!list_empty(&llsd->llsd_master_list)) {
6223 llst = list_entry(llsd->llsd_master_list.next,
6224 struct lfsck_layout_slave_target, llst_list);
6225 list_del_init(&llst->llst_list);
6226 spin_unlock(&llsd->llsd_lock);
6227 lfsck_layout_llst_put(llst);
6228 spin_lock(&llsd->llsd_lock);
6230 spin_unlock(&llsd->llsd_lock);
6232 lfsck_rbtree_cleanup(env, com);
6235 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6236 struct lfsck_component *com,
6237 struct lfsck_request *lr)
6239 struct lfsck_instance *lfsck = com->lc_lfsck;
6240 struct lfsck_layout *lo = com->lc_file_ram;
6241 struct lfsck_assistant_data *lad = com->lc_data;
6242 struct lfsck_tgt_descs *ltds;
6243 struct lfsck_tgt_desc *ltd;
6247 if (lr->lr_event == LE_PAIRS_VERIFY) {
6250 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6257 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6258 "from %s %x, status %d, flags %x, flags2 %x\n",
6259 lfsck_lfsck2name(lfsck), lr->lr_event,
6260 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6261 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6263 if (lr->lr_event != LE_PHASE1_DONE &&
6264 lr->lr_event != LE_PHASE2_DONE &&
6265 lr->lr_event != LE_PEER_EXIT)
6268 if (lr->lr_flags & LEF_FROM_OST)
6269 ltds = &lfsck->li_ost_descs;
6271 ltds = &lfsck->li_mdt_descs;
6272 spin_lock(<ds->ltd_lock);
6273 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6275 spin_unlock(<ds->ltd_lock);
6280 list_del_init(<d->ltd_layout_phase_list);
6281 switch (lr->lr_event) {
6282 case LE_PHASE1_DONE:
6283 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6284 if (lr->lr_flags2 & LF_INCOMPLETE) {
6285 if (lr->lr_flags & LEF_FROM_OST)
6286 lfsck_lad_set_bitmap(env, com,
6289 lo->ll_flags |= LF_INCOMPLETE;
6291 ltd->ltd_layout_done = 1;
6292 list_del_init(<d->ltd_layout_list);
6297 if (lr->lr_flags & LEF_FROM_OST) {
6298 if (list_empty(<d->ltd_layout_list))
6299 list_add_tail(<d->ltd_layout_list,
6300 &lad->lad_ost_list);
6301 list_add_tail(<d->ltd_layout_phase_list,
6302 &lad->lad_ost_phase2_list);
6304 if (list_empty(<d->ltd_layout_list))
6305 list_add_tail(<d->ltd_layout_list,
6306 &lad->lad_mdt_list);
6307 list_add_tail(<d->ltd_layout_phase_list,
6308 &lad->lad_mdt_phase2_list);
6311 case LE_PHASE2_DONE:
6312 ltd->ltd_layout_done = 1;
6313 if (!list_empty(<d->ltd_layout_list)) {
6314 list_del_init(<d->ltd_layout_list);
6315 if (lr->lr_flags2 & LF_INCOMPLETE) {
6316 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6324 ltd->ltd_layout_done = 1;
6325 list_del_init(<d->ltd_layout_list);
6326 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6327 !(lr->lr_flags & LEF_FROM_OST))
6328 lo->ll_flags |= LF_INCOMPLETE;
6333 spin_unlock(<ds->ltd_lock);
6335 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6336 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6338 memset(stop, 0, sizeof(*stop));
6339 stop->ls_status = lr->lr_status;
6340 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6341 lfsck_stop(env, lfsck->li_bottom, stop);
6342 } else if (lfsck_phase2_next_ready(lad)) {
6343 wake_up_all(&lad->lad_thread.t_ctl_waitq);
6349 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6350 struct lfsck_component *com,
6351 struct lfsck_req_local *lrl,
6356 switch (lrl->lrl_event) {
6357 case LEL_FID_ACCESSED:
6358 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6360 case LEL_PAIRS_VERIFY_LOCAL: {
6363 lrl->lrl_status = LPVS_INIT;
6364 /* Firstly, if the MDT-object which is claimed via OST-object
6365 * local stored PFID xattr recognizes the OST-object, then it
6366 * must be that the client given PFID is wrong. */
6367 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6368 &lrl->lrl_ff_local.ff_parent,
6369 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6373 lrl->lrl_status = LPVS_INCONSISTENT;
6374 /* The OST-object local stored PFID xattr is stale. We need to
6375 * check whether the MDT-object that is claimed via the client
6376 * given PFID information recognizes the OST-object or not. If
6377 * matches, then need to update the OST-object's PFID xattr. */
6378 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6379 &lrl->lrl_ff_client.ff_parent,
6380 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6382 * We are not sure whether the client given PFID information
6383 * is correct or not, do nothing to avoid improper fixing.
6386 * The client given PFID information is also invalid, we can
6387 * NOT fix the OST-object inconsistency.
6390 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6391 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6400 RETURN(-EOPNOTSUPP);
6403 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6404 struct lfsck_component *com,
6405 struct lfsck_request *lr)
6407 struct lfsck_instance *lfsck = com->lc_lfsck;
6408 struct lfsck_layout_slave_data *llsd = com->lc_data;
6409 struct lfsck_layout_slave_target *llst;
6413 switch (lr->lr_event) {
6414 case LE_CONDITIONAL_DESTROY:
6415 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6417 case LE_PHASE1_DONE: {
6418 if (lr->lr_flags2 & LF_INCOMPLETE) {
6419 struct lfsck_layout *lo = com->lc_file_ram;
6421 lo->ll_flags |= LF_INCOMPLETE;
6422 llst = lfsck_layout_llst_find_and_del(llsd,
6426 lfsck_layout_llst_put(llst);
6427 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6433 case LE_PHASE2_DONE:
6435 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6436 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6437 lr->lr_event, lr->lr_index, lr->lr_status);
6443 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6447 lfsck_layout_llst_put(llst);
6448 if (list_empty(&llsd->llsd_master_list))
6449 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6451 if (lr->lr_event == LE_PEER_EXIT &&
6452 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6453 (list_empty(&llsd->llsd_master_list) &&
6454 (lr->lr_status == LS_STOPPED ||
6455 lr->lr_status == LS_CO_STOPPED)))) {
6456 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6458 memset(stop, 0, sizeof(*stop));
6459 stop->ls_status = lr->lr_status;
6460 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6461 lfsck_stop(env, lfsck->li_bottom, stop);
6467 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6471 for (i = 0; i < LLIT_MAX; i++)
6472 *count += lo->ll_objs_repaired[i];
6475 static int lfsck_layout_query_all(const struct lu_env *env,
6476 struct lfsck_component *com,
6477 __u32 *mdts_count, __u32 *osts_count,
6480 struct lfsck_layout *lo = com->lc_file_ram;
6481 struct lfsck_tgt_descs *ltds;
6482 struct lfsck_tgt_desc *ltd;
6487 rc = lfsck_query_all(env, com);
6491 ltds = &com->lc_lfsck->li_mdt_descs;
6492 down_read(<ds->ltd_rw_sem);
6493 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6494 ltd = lfsck_ltd2tgt(ltds, idx);
6495 LASSERT(ltd != NULL);
6497 mdts_count[ltd->ltd_layout_status]++;
6498 *repaired += ltd->ltd_layout_repaired;
6500 up_read(<ds->ltd_rw_sem);
6502 ltds = &com->lc_lfsck->li_ost_descs;
6503 down_read(<ds->ltd_rw_sem);
6504 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6505 ltd = lfsck_ltd2tgt(ltds, idx);
6506 LASSERT(ltd != NULL);
6508 osts_count[ltd->ltd_layout_status]++;
6509 *repaired += ltd->ltd_layout_repaired;
6511 up_read(<ds->ltd_rw_sem);
6513 down_read(&com->lc_sem);
6514 mdts_count[lo->ll_status]++;
6515 lfsck_layout_repaired(lo, repaired);
6516 up_read(&com->lc_sem);
6521 static int lfsck_layout_query(const struct lu_env *env,
6522 struct lfsck_component *com,
6523 struct lfsck_request *req,
6524 struct lfsck_reply *rep,
6525 struct lfsck_query *que, int idx)
6527 struct lfsck_layout *lo = com->lc_file_ram;
6531 LASSERT(com->lc_lfsck->li_master);
6533 rc = lfsck_layout_query_all(env, com,
6534 que->lu_mdts_count[idx],
6535 que->lu_osts_count[idx],
6536 &que->lu_repaired[idx]);
6538 down_read(&com->lc_sem);
6539 rep->lr_status = lo->ll_status;
6540 if (req->lr_flags & LEF_QUERY_ALL)
6541 lfsck_layout_repaired(lo, &rep->lr_repaired);
6542 up_read(&com->lc_sem);
6548 /* with lfsck::li_lock held */
6549 static int lfsck_layout_slave_join(const struct lu_env *env,
6550 struct lfsck_component *com,
6551 struct lfsck_start_param *lsp)
6553 struct lfsck_instance *lfsck = com->lc_lfsck;
6554 struct lfsck_layout_slave_data *llsd = com->lc_data;
6555 struct lfsck_layout_slave_target *llst;
6556 struct lfsck_start *start = lsp->lsp_start;
6560 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6563 if (!lsp->lsp_index_valid)
6566 /* If someone is running the LFSCK without orphan handling,
6567 * it will not maintain the object accessing rbtree. So we
6568 * cannot join it for orphan handling. */
6569 if (!llsd->llsd_rbtree_valid)
6572 spin_unlock(&lfsck->li_lock);
6573 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6574 spin_lock(&lfsck->li_lock);
6575 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6576 spin_unlock(&lfsck->li_lock);
6577 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6580 lfsck_layout_llst_put(llst);
6581 spin_lock(&lfsck->li_lock);
6588 static struct lfsck_operations lfsck_layout_master_ops = {
6589 .lfsck_reset = lfsck_layout_reset,
6590 .lfsck_fail = lfsck_layout_fail,
6591 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6592 .lfsck_prep = lfsck_layout_master_prep,
6593 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6594 .lfsck_exec_dir = lfsck_layout_exec_dir,
6595 .lfsck_post = lfsck_layout_master_post,
6596 .lfsck_dump = lfsck_layout_dump,
6597 .lfsck_double_scan = lfsck_layout_master_double_scan,
6598 .lfsck_data_release = lfsck_layout_master_data_release,
6599 .lfsck_quit = lfsck_layout_master_quit,
6600 .lfsck_in_notify = lfsck_layout_master_in_notify,
6601 .lfsck_query = lfsck_layout_query,
6604 static struct lfsck_operations lfsck_layout_slave_ops = {
6605 .lfsck_reset = lfsck_layout_reset,
6606 .lfsck_fail = lfsck_layout_fail,
6607 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6608 .lfsck_prep = lfsck_layout_slave_prep,
6609 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6610 .lfsck_exec_dir = lfsck_layout_exec_dir,
6611 .lfsck_post = lfsck_layout_slave_post,
6612 .lfsck_dump = lfsck_layout_dump,
6613 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6614 .lfsck_data_release = lfsck_layout_slave_data_release,
6615 .lfsck_quit = lfsck_layout_slave_quit,
6616 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6617 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6618 .lfsck_query = lfsck_layout_query,
6619 .lfsck_join = lfsck_layout_slave_join,
6622 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6623 struct lfsck_component *com,
6624 struct lfsck_position *pos)
6626 struct lfsck_assistant_data *lad = com->lc_data;
6627 struct lfsck_layout_req *llr;
6629 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6633 if (list_empty(&lad->lad_req_list))
6636 llr = list_entry(lad->lad_req_list.next,
6637 struct lfsck_layout_req,
6639 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6642 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6643 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
6644 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
6645 .la_fill_pos = lfsck_layout_assistant_fill_pos,
6646 .la_double_scan_result = lfsck_layout_double_scan_result,
6647 .la_req_fini = lfsck_layout_assistant_req_fini,
6648 .la_sync_failures = lfsck_layout_assistant_sync_failures,
6651 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
6653 struct lfsck_component *com;
6654 struct lfsck_layout *lo;
6655 struct dt_object *root = NULL;
6656 struct dt_object *obj;
6665 INIT_LIST_HEAD(&com->lc_link);
6666 INIT_LIST_HEAD(&com->lc_link_dir);
6667 init_rwsem(&com->lc_sem);
6668 atomic_set(&com->lc_ref, 1);
6669 com->lc_lfsck = lfsck;
6670 com->lc_type = LFSCK_TYPE_LAYOUT;
6671 if (lfsck->li_master) {
6672 com->lc_ops = &lfsck_layout_master_ops;
6673 com->lc_data = lfsck_assistant_data_init(
6674 &lfsck_layout_assistant_ops,
6676 if (com->lc_data == NULL)
6677 GOTO(out, rc = -ENOMEM);
6679 for (i = 0; i < LFSCK_STF_COUNT; i++)
6680 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6682 struct lfsck_layout_slave_data *llsd;
6684 com->lc_ops = &lfsck_layout_slave_ops;
6685 OBD_ALLOC_PTR(llsd);
6687 GOTO(out, rc = -ENOMEM);
6689 INIT_LIST_HEAD(&llsd->llsd_seq_list);
6690 INIT_LIST_HEAD(&llsd->llsd_master_list);
6691 spin_lock_init(&llsd->llsd_lock);
6692 llsd->llsd_rb_root = RB_ROOT;
6693 rwlock_init(&llsd->llsd_rb_lock);
6694 com->lc_data = llsd;
6696 com->lc_file_size = sizeof(*lo);
6697 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6698 if (com->lc_file_ram == NULL)
6699 GOTO(out, rc = -ENOMEM);
6701 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6702 if (com->lc_file_disk == NULL)
6703 GOTO(out, rc = -ENOMEM);
6705 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6707 GOTO(out, rc = PTR_ERR(root));
6709 if (unlikely(!dt_try_as_dir(env, root)))
6710 GOTO(out, rc = -ENOTDIR);
6712 obj = local_file_find_or_create(env, lfsck->li_los, root,
6714 S_IFREG | S_IRUGO | S_IWUSR);
6716 GOTO(out, rc = PTR_ERR(obj));
6719 rc = lfsck_layout_load(env, com);
6721 rc = lfsck_layout_reset(env, com, true);
6722 else if (rc == -ENOENT)
6723 rc = lfsck_layout_init(env, com);
6724 else if (lfsck->li_master)
6725 rc = lfsck_load_sub_trace_files(env, com,
6726 &dt_lfsck_layout_dangling_features,
6727 LFSCK_LAYOUT, false);
6732 lo = com->lc_file_ram;
6733 switch (lo->ll_status) {
6739 spin_lock(&lfsck->li_lock);
6740 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6741 spin_unlock(&lfsck->li_lock);
6744 CERROR("%s: unknown lfsck_layout status %d\n",
6745 lfsck_lfsck2name(lfsck), lo->ll_status);
6747 case LS_SCANNING_PHASE1:
6748 case LS_SCANNING_PHASE2:
6749 /* No need to store the status to disk right now.
6750 * If the system crashed before the status stored,
6751 * it will be loaded back when next time. */
6752 lo->ll_status = LS_CRASHED;
6753 if (!lfsck->li_master)
6754 lo->ll_flags |= LF_INCOMPLETE;
6761 spin_lock(&lfsck->li_lock);
6762 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6763 spin_unlock(&lfsck->li_lock);
6767 if (lo->ll_flags & LF_CRASHED_LASTID) {
6768 LASSERT(lfsck->li_out_notify != NULL);
6770 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6771 LE_LASTID_REBUILDING);
6777 if (root != NULL && !IS_ERR(root))
6778 lfsck_object_put(env, root);
6781 lfsck_component_cleanup(env, com);
6782 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
6783 lfsck_lfsck2name(lfsck), rc);
6789 struct lfsck_orphan_it {
6790 struct lfsck_component *loi_com;
6791 struct lfsck_rbtree_node *loi_lrn;
6792 struct lfsck_layout_slave_target *loi_llst;
6793 struct lu_fid loi_key;
6794 struct lu_orphan_rec_v2 loi_rec;
6796 unsigned int loi_over:1;
6799 static int lfsck_fid_match_idx(const struct lu_env *env,
6800 struct lfsck_instance *lfsck,
6801 const struct lu_fid *fid, int idx)
6803 struct seq_server_site *ss;
6804 struct lu_server_fld *sf;
6805 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
6808 /* All abnormal cases will be returned to MDT0. */
6809 if (!fid_is_norm(fid)) {
6816 ss = lfsck_dev_site(lfsck);
6817 if (unlikely(ss == NULL))
6820 sf = ss->ss_server_fld;
6821 LASSERT(sf != NULL);
6823 fld_range_set_any(range);
6824 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
6828 if (!fld_range_is_mdt(range))
6831 if (range->lsr_index == idx)
6837 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
6838 struct dt_object *obj)
6840 struct dt_device *dev = lfsck_obj2dev(obj);
6841 struct thandle *handle;
6845 handle = dt_trans_create(env, dev);
6849 rc = dt_declare_ref_del(env, obj, handle);
6853 rc = dt_declare_destroy(env, obj, handle);
6857 rc = dt_trans_start_local(env, dev, handle);
6861 dt_write_lock(env, obj, 0);
6862 rc = dt_ref_del(env, obj, handle);
6864 rc = dt_destroy(env, obj, handle);
6865 dt_write_unlock(env, obj);
6870 dt_trans_stop(env, dev, handle);
6872 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
6873 PFID(lfsck_dto2fid(obj)), rc);
6878 static int lfsck_orphan_index_lookup(const struct lu_env *env,
6879 struct dt_object *dt,
6881 const struct dt_key *key)
6886 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
6887 struct dt_object *dt,
6888 const struct dt_rec *rec,
6889 const struct dt_key *key,
6890 struct thandle *handle)
6895 static int lfsck_orphan_index_insert(const struct lu_env *env,
6896 struct dt_object *dt,
6897 const struct dt_rec *rec,
6898 const struct dt_key *key,
6899 struct thandle *handle,
6905 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
6906 struct dt_object *dt,
6907 const struct dt_key *key,
6908 struct thandle *handle)
6913 static int lfsck_orphan_index_delete(const struct lu_env *env,
6914 struct dt_object *dt,
6915 const struct dt_key *key,
6916 struct thandle *handle)
6921 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
6922 struct dt_object *dt,
6925 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
6926 struct lfsck_instance *lfsck;
6927 struct lfsck_component *com = NULL;
6928 struct lfsck_layout_slave_data *llsd;
6929 struct lfsck_orphan_it *it = NULL;
6930 struct lfsck_layout *lo;
6934 lfsck = lfsck_instance_find(dev, true, false);
6935 if (unlikely(lfsck == NULL))
6936 RETURN(ERR_PTR(-ENXIO));
6938 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
6939 if (unlikely(com == NULL))
6940 GOTO(out, rc = -ENOENT);
6942 lo = com->lc_file_ram;
6943 if (lo->ll_flags & LF_INCOMPLETE)
6944 GOTO(out, rc = -ESRCH);
6946 llsd = com->lc_data;
6947 if (!llsd->llsd_rbtree_valid)
6948 GOTO(out, rc = -ESRCH);
6952 GOTO(out, rc = -ENOMEM);
6954 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
6955 if (it->loi_llst == NULL)
6956 GOTO(out, rc = -ENXIO);
6958 if (dev->dd_record_fid_accessed) {
6959 /* The first iteration against the rbtree, scan the whole rbtree
6960 * to remove the nodes which do NOT need to be handled. */
6961 write_lock(&llsd->llsd_rb_lock);
6962 if (dev->dd_record_fid_accessed) {
6963 struct rb_node *node;
6964 struct rb_node *next;
6965 struct lfsck_rbtree_node *lrn;
6967 /* No need to record the fid accessing anymore. */
6968 dev->dd_record_fid_accessed = 0;
6970 node = rb_first(&llsd->llsd_rb_root);
6971 while (node != NULL) {
6972 next = rb_next(node);
6973 lrn = rb_entry(node, struct lfsck_rbtree_node,
6975 if (atomic_read(&lrn->lrn_known_count) <=
6976 atomic_read(&lrn->lrn_accessed_count)) {
6977 rb_erase(node, &llsd->llsd_rb_root);
6978 lfsck_rbtree_free(lrn);
6983 write_unlock(&llsd->llsd_rb_lock);
6986 /* read lock the rbtree when init, and unlock when fini */
6987 read_lock(&llsd->llsd_rb_lock);
6995 lfsck_component_put(env, com);
6997 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
6998 lfsck_lfsck2name(lfsck), rc);
7000 lfsck_instance_put(env, lfsck);
7005 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7008 return (struct dt_it *)it;
7011 static void lfsck_orphan_it_fini(const struct lu_env *env,
7014 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7015 struct lfsck_component *com = it->loi_com;
7016 struct lfsck_layout_slave_data *llsd;
7017 struct lfsck_layout_slave_target *llst;
7020 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7021 lfsck_lfsck2name(com->lc_lfsck));
7023 llsd = com->lc_data;
7024 read_unlock(&llsd->llsd_rb_lock);
7025 llst = it->loi_llst;
7026 LASSERT(llst != NULL);
7028 /* Save the key and hash for iterate next. */
7029 llst->llst_fid = it->loi_key;
7030 llst->llst_hash = it->loi_hash;
7031 lfsck_layout_llst_put(llst);
7032 lfsck_component_put(env, com);
7038 * \retval +1: the iteration finished
7039 * \retval 0: on success, not finished
7040 * \retval -ve: on error
7042 static int lfsck_orphan_it_next(const struct lu_env *env,
7045 struct lfsck_thread_info *info = lfsck_env_info(env);
7046 struct filter_fid *ff = &info->lti_ff;
7047 struct lu_attr *la = &info->lti_la;
7048 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7049 struct lu_fid *key = &it->loi_key;
7050 struct lu_orphan_rec_v2 *rec = &it->loi_rec;
7051 struct ost_layout *ol = &rec->lor_layout;
7052 struct lfsck_component *com = it->loi_com;
7053 struct lfsck_instance *lfsck = com->lc_lfsck;
7054 struct lfsck_layout_slave_data *llsd = com->lc_data;
7055 struct dt_object *obj;
7056 struct lfsck_rbtree_node *lrn;
7060 __u32 idx = it->loi_llst->llst_index;
7070 lrn = lfsck_rbtree_search(llsd, key, &exact);
7078 key->f_seq = lrn->lrn_seq;
7079 key->f_oid = lrn->lrn_first_oid;
7084 if (unlikely(key->f_oid == 0)) {
7091 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7097 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7098 atomic_read(&lrn->lrn_accessed_count))) {
7099 struct rb_node *next = rb_next(&lrn->lrn_node);
7101 while (next != NULL) {
7102 lrn = rb_entry(next, struct lfsck_rbtree_node,
7104 if (atomic_read(&lrn->lrn_known_count) >
7105 atomic_read(&lrn->lrn_accessed_count))
7107 next = rb_next(next);
7116 key->f_seq = lrn->lrn_seq;
7117 key->f_oid = lrn->lrn_first_oid;
7121 pos = key->f_oid - lrn->lrn_first_oid;
7124 pos = find_next_bit(lrn->lrn_known_bitmap,
7125 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7126 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7127 key->f_oid = lrn->lrn_first_oid + pos;
7128 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7136 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7141 key->f_oid = lrn->lrn_first_oid + pos;
7142 obj = lfsck_object_find_bottom(env, lfsck, key);
7145 if (rc == -ENOENT) {
7152 dt_read_lock(env, obj, 0);
7153 if (dt_object_exists(obj) == 0 ||
7154 lfsck_is_dead_obj(obj)) {
7155 dt_read_unlock(env, obj);
7156 lfsck_object_put(env, obj);
7161 rc = dt_attr_get(env, obj, la);
7165 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7167 if (rc == -ENODATA) {
7168 /* For the pre-created OST-object, update the bitmap to avoid
7169 * others LFSCK (second phase) iteration to touch it again. */
7170 if (la->la_ctime == 0) {
7171 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7172 atomic_inc(&lrn->lrn_accessed_count);
7174 /* For the race between repairing dangling referenced
7175 * MDT-object and unlink the file, it may left orphan
7176 * OST-object there. Destroy it now! */
7177 if (unlikely(!(la->la_mode & S_ISUID))) {
7178 dt_read_unlock(env, obj);
7179 lfsck_layout_destroy_orphan(env, obj);
7180 lfsck_object_put(env, obj);
7184 } else if (idx == 0) {
7185 /* If the orphan OST-object has no parent information,
7186 * regard it as referenced by the MDT-object on MDT0. */
7187 fid_zero(&rec->lor_rec.lor_fid);
7188 rec->lor_rec.lor_uid = la->la_uid;
7189 rec->lor_rec.lor_gid = la->la_gid;
7190 memset(ol, 0, sizeof(*ol));
7195 dt_read_unlock(env, obj);
7196 lfsck_object_put(env, obj);
7201 if (rc < sizeof(struct lu_fid))
7202 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7204 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7205 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7206 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7207 * parent MDT-object's layout EA. */
7208 save = rec->lor_rec.lor_fid.f_stripe_idx;
7209 rec->lor_rec.lor_fid.f_ver = 0;
7210 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7211 /* If the orphan OST-object does not claim the MDT, then next.
7213 * If we do not know whether it matches or not, then return it
7214 * to the MDT for further check. */
7216 dt_read_unlock(env, obj);
7217 lfsck_object_put(env, obj);
7222 rec->lor_rec.lor_fid.f_stripe_idx = save;
7223 rec->lor_rec.lor_uid = la->la_uid;
7224 rec->lor_rec.lor_gid = la->la_gid;
7225 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7227 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7228 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7229 "COMP end %llu\n", lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7230 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7231 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7232 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end);
7237 dt_read_unlock(env, obj);
7238 lfsck_object_put(env, obj);
7246 * \retval +1: locate to the exactly position
7247 * \retval 0: cannot locate to the exactly position,
7248 * call next() to move to a valid position.
7249 * \retval -ve: on error
7251 static int lfsck_orphan_it_get(const struct lu_env *env,
7253 const struct dt_key *key)
7255 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7258 it->loi_key = *(struct lu_fid *)key;
7259 rc = lfsck_orphan_it_next(env, di);
7269 static void lfsck_orphan_it_put(const struct lu_env *env,
7274 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7275 const struct dt_it *di)
7277 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7279 return (struct dt_key *)&it->loi_key;
7282 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7283 const struct dt_it *di)
7285 return sizeof(struct lu_fid);
7288 static int lfsck_orphan_it_rec(const struct lu_env *env,
7289 const struct dt_it *di,
7293 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7295 *(struct lu_orphan_rec_v2 *)rec = it->loi_rec;
7300 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7301 const struct dt_it *di)
7303 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7305 return it->loi_hash;
7309 * \retval +1: locate to the exactly position
7310 * \retval 0: cannot locate to the exactly position,
7311 * call next() to move to a valid position.
7312 * \retval -ve: on error
7314 static int lfsck_orphan_it_load(const struct lu_env *env,
7315 const struct dt_it *di,
7318 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7319 struct lfsck_layout_slave_target *llst = it->loi_llst;
7322 LASSERT(llst != NULL);
7324 if (hash != llst->llst_hash) {
7325 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7326 "iteration does not match the one when fini "
7327 "%llu, to be reset.\n",
7328 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7330 fid_zero(&llst->llst_fid);
7331 llst->llst_hash = 0;
7334 it->loi_key = llst->llst_fid;
7335 it->loi_hash = llst->llst_hash;
7336 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7346 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7347 const struct dt_it *di,
7353 const struct dt_index_operations lfsck_orphan_index_ops = {
7354 .dio_lookup = lfsck_orphan_index_lookup,
7355 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7356 .dio_insert = lfsck_orphan_index_insert,
7357 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7358 .dio_delete = lfsck_orphan_index_delete,
7360 .init = lfsck_orphan_it_init,
7361 .fini = lfsck_orphan_it_fini,
7362 .get = lfsck_orphan_it_get,
7363 .put = lfsck_orphan_it_put,
7364 .next = lfsck_orphan_it_next,
7365 .key = lfsck_orphan_it_key,
7366 .key_size = lfsck_orphan_it_key_size,
7367 .rec = lfsck_orphan_it_rec,
7368 .store = lfsck_orphan_it_store,
7369 .load = lfsck_orphan_it_load,
7370 .key_rec = lfsck_orphan_it_key_rec,