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);
294 laia->laia_ltd = ltd;
295 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
296 lfsck_layout_assistant_sync_failures_interpret,
299 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
300 "notify target %x for %s phase1 done: "
301 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
302 ltd->ltd_index, lad->lad_name, rc);
309 up_read(<ds->ltd_rw_sem);
311 if (rc == 0 && atomic_read(&count) > 0)
312 rc = ptlrpc_set_wait(set);
314 ptlrpc_set_destroy(set);
316 if (rc == 0 && atomic_read(&count) > 0)
323 /* If failed to sync failures with the OSTs, then have to
324 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
325 * subsequent orphan OST-object handling. */
326 lo->ll_flags |= LF_INCOMPLETE;
328 lr->lr_flags2 = lo->ll_flags;
331 static int lfsck_layout_verify_header_v1v3(struct dt_object *obj,
332 struct lov_mds_md_v1 *lmm)
337 magic = le32_to_cpu(lmm->lmm_magic);
338 /* If magic crashed, keep it there. Sometime later, during OST-object
339 * orphan handling, if some OST-object(s) back-point to it, it can be
340 * verified and repaired. */
341 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
344 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
349 CDEBUG(D_LFSCK, "%s LOV EA magic %u for the file "DFID"\n",
350 rc == -EINVAL ? "Unknown" : "Unsupported",
351 magic, PFID(lfsck_dto2fid(obj)));
356 pattern = le32_to_cpu(lmm->lmm_pattern);
357 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
358 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
359 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
360 DFID"\n", pattern, PFID(lfsck_dto2fid(obj)));
368 static int lfsck_layout_verify_header(struct dt_object *obj,
369 struct lov_mds_md_v1 *lmm)
373 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
374 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
376 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
378 if (unlikely(count == 0)) {
379 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
380 "components count 0\n",
381 PFID(lfsck_dto2fid(obj)));
386 for (i = 0; i < count; i++) {
387 struct lov_comp_md_entry_v1 *lcme =
388 &lcm->lcm_entries[i];
389 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
390 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
391 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
393 if (unlikely(comp_id == LCME_ID_INVAL ||
394 comp_id > LCME_ID_MAX)) {
395 CDEBUG(D_LFSCK, "found invalid FPL ID %u "
396 "for the file "DFID" at idx %d\n",
397 comp_id, PFID(lfsck_dto2fid(obj)), i);
402 if (unlikely(start >= end ||
403 !lfsck_comp_extent_aligned(start) ||
404 (!lfsck_comp_extent_aligned(end) &&
405 end != LUSTRE_EOF))) {
406 CDEBUG(D_LFSCK, "found invalid FPL extent "
407 "range [%llu - %llu) for the file "
409 start, end, PFID(lfsck_dto2fid(obj)), i);
414 rc = lfsck_layout_verify_header_v1v3(obj,
415 (struct lov_mds_md_v1 *)((char *)lmm +
416 le32_to_cpu(lcme->lcme_offset)));
421 rc = lfsck_layout_verify_header_v1v3(obj, lmm);
427 static int lfsck_layout_get_lovea(const struct lu_env *env,
428 struct dt_object *obj, struct lu_buf *buf)
434 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
436 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
440 lu_buf_realloc(buf, rc);
441 if (buf->lb_buf == NULL)
453 if (unlikely(buf->lb_buf == NULL)) {
454 lu_buf_alloc(buf, rc);
455 if (buf->lb_buf == NULL)
461 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf);
463 return rc1 ? rc1 : rc;
466 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
467 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
468 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
470 struct lfsck_rbtree_node {
471 struct rb_node lrn_node;
474 atomic_t lrn_known_count;
475 atomic_t lrn_accessed_count;
476 void *lrn_known_bitmap;
477 void *lrn_accessed_bitmap;
480 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
481 __u64 seq, __u32 oid)
483 if (seq < lrn->lrn_seq)
486 if (seq > lrn->lrn_seq)
489 if (oid < lrn->lrn_first_oid)
492 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
498 /* The caller should hold llsd->llsd_rb_lock. */
499 static struct lfsck_rbtree_node *
500 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
501 const struct lu_fid *fid, bool *exact)
503 struct rb_node *node = llsd->llsd_rb_root.rb_node;
504 struct rb_node *prev = NULL;
505 struct lfsck_rbtree_node *lrn = NULL;
511 while (node != NULL) {
513 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
514 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
516 node = node->rb_left;
518 node = node->rb_right;
526 /* If there is no exactly matched one, then to the next valid one. */
529 /* The rbtree is empty. */
536 node = rb_next(prev);
538 /* The end of the rbtree. */
542 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
547 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
548 const struct lu_fid *fid)
550 struct lfsck_rbtree_node *lrn;
554 return ERR_PTR(-ENOMEM);
556 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
557 if (lrn->lrn_known_bitmap == NULL) {
560 return ERR_PTR(-ENOMEM);
563 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
564 if (lrn->lrn_accessed_bitmap == NULL) {
565 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
568 return ERR_PTR(-ENOMEM);
571 RB_CLEAR_NODE(&lrn->lrn_node);
572 lrn->lrn_seq = fid_seq(fid);
573 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
574 atomic_set(&lrn->lrn_known_count, 0);
575 atomic_set(&lrn->lrn_accessed_count, 0);
580 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
582 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
583 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
587 /* The caller should hold lock. */
588 static struct lfsck_rbtree_node *
589 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
590 struct lfsck_rbtree_node *lrn)
592 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
593 struct rb_node *parent = NULL;
594 struct lfsck_rbtree_node *tmp;
597 while (*pos != NULL) {
599 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
600 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
602 pos = &(*pos)->rb_left;
604 pos = &(*pos)->rb_right;
609 rb_link_node(&lrn->lrn_node, parent, pos);
610 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
615 extern const struct dt_index_operations lfsck_orphan_index_ops;
617 static int lfsck_rbtree_setup(const struct lu_env *env,
618 struct lfsck_component *com)
620 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
621 struct lfsck_instance *lfsck = com->lc_lfsck;
622 struct dt_device *dev = lfsck->li_bottom;
623 struct lfsck_layout_slave_data *llsd = com->lc_data;
624 struct dt_object *obj;
626 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
627 fid->f_oid = lfsck_dev_idx(lfsck);
629 obj = dt_locate(env, dev, fid);
631 RETURN(PTR_ERR(obj));
633 /* Generate an in-RAM object to stand for the layout rbtree.
634 * Scanning the layout rbtree will be via the iteration over
635 * the object. In the future, the rbtree may be written onto
636 * disk with the object.
638 * Mark the object to be as exist. */
639 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
640 obj->do_index_ops = &lfsck_orphan_index_ops;
641 llsd->llsd_rb_obj = obj;
642 llsd->llsd_rbtree_valid = 1;
643 dev->dd_record_fid_accessed = 1;
645 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
646 lfsck_lfsck2name(lfsck));
651 static void lfsck_rbtree_cleanup(const struct lu_env *env,
652 struct lfsck_component *com)
654 struct lfsck_instance *lfsck = com->lc_lfsck;
655 struct lfsck_layout_slave_data *llsd = com->lc_data;
656 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
657 struct rb_node *next;
658 struct lfsck_rbtree_node *lrn;
660 lfsck->li_bottom->dd_record_fid_accessed = 0;
661 /* Invalid the rbtree, then no others will use it. */
662 write_lock(&llsd->llsd_rb_lock);
663 llsd->llsd_rbtree_valid = 0;
664 write_unlock(&llsd->llsd_rb_lock);
666 while (node != NULL) {
667 next = rb_next(node);
668 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
669 rb_erase(node, &llsd->llsd_rb_root);
670 lfsck_rbtree_free(lrn);
674 if (llsd->llsd_rb_obj != NULL) {
675 lfsck_object_put(env, llsd->llsd_rb_obj);
676 llsd->llsd_rb_obj = NULL;
679 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
680 lfsck_lfsck2name(lfsck));
683 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
684 struct lfsck_component *com,
685 const struct lu_fid *fid,
688 struct lfsck_layout_slave_data *llsd = com->lc_data;
689 struct lfsck_rbtree_node *lrn;
695 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
698 if (!fid_is_idif(fid) && !fid_is_norm(fid))
701 read_lock(&llsd->llsd_rb_lock);
702 if (!llsd->llsd_rbtree_valid)
703 GOTO(unlock, rc = 0);
705 lrn = lfsck_rbtree_search(llsd, fid, NULL);
707 struct lfsck_rbtree_node *tmp;
711 read_unlock(&llsd->llsd_rb_lock);
712 tmp = lfsck_rbtree_new(env, fid);
714 GOTO(out, rc = PTR_ERR(tmp));
717 write_lock(&llsd->llsd_rb_lock);
718 if (!llsd->llsd_rbtree_valid) {
719 lfsck_rbtree_free(tmp);
720 GOTO(unlock, rc = 0);
723 lrn = lfsck_rbtree_insert(llsd, tmp);
725 lfsck_rbtree_free(tmp);
728 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
729 /* Any accessed object must be a known object. */
730 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
731 atomic_inc(&lrn->lrn_known_count);
732 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
733 atomic_inc(&lrn->lrn_accessed_count);
735 GOTO(unlock, rc = 0);
739 write_unlock(&llsd->llsd_rb_lock);
741 read_unlock(&llsd->llsd_rb_lock);
743 if (rc != 0 && accessed) {
744 struct lfsck_layout *lo = com->lc_file_ram;
746 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
747 "bitmap, and will cause incorrect LFSCK OST-object "
748 "handling, so disable it to cancel orphan handling "
749 "for related device. rc = %d\n",
750 lfsck_lfsck2name(com->lc_lfsck), rc);
752 lo->ll_flags |= LF_INCOMPLETE;
753 lfsck_rbtree_cleanup(env, com);
757 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
758 const struct lfsck_layout_dangling_key *src)
760 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
761 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
762 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
765 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
766 const struct lfsck_layout_dangling_key *src)
768 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
769 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
770 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
773 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
774 const struct lfsck_layout_dangling_key *src)
776 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
777 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
778 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
781 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
782 const struct lfsck_layout_dangling_key *src)
784 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
785 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
786 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
789 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
790 const struct lfsck_layout *src)
794 des->ll_magic = le32_to_cpu(src->ll_magic);
795 des->ll_status = le32_to_cpu(src->ll_status);
796 des->ll_flags = le32_to_cpu(src->ll_flags);
797 des->ll_success_count = le32_to_cpu(src->ll_success_count);
798 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
799 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
800 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
801 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
802 des->ll_time_last_checkpoint =
803 le64_to_cpu(src->ll_time_last_checkpoint);
804 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
805 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
806 des->ll_pos_first_inconsistent =
807 le64_to_cpu(src->ll_pos_first_inconsistent);
808 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
809 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
810 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
811 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
812 for (i = 0; i < LLIT_MAX; i++)
813 des->ll_objs_repaired[i] =
814 le64_to_cpu(src->ll_objs_repaired[i]);
815 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
816 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
817 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
818 &src->ll_lldk_latest_scanned_phase2);
821 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
822 const struct lfsck_layout *src)
826 des->ll_magic = cpu_to_le32(src->ll_magic);
827 des->ll_status = cpu_to_le32(src->ll_status);
828 des->ll_flags = cpu_to_le32(src->ll_flags);
829 des->ll_success_count = cpu_to_le32(src->ll_success_count);
830 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
831 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
832 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
833 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
834 des->ll_time_last_checkpoint =
835 cpu_to_le64(src->ll_time_last_checkpoint);
836 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
837 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
838 des->ll_pos_first_inconsistent =
839 cpu_to_le64(src->ll_pos_first_inconsistent);
840 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
841 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
842 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
843 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
844 for (i = 0; i < LLIT_MAX; i++)
845 des->ll_objs_repaired[i] =
846 cpu_to_le64(src->ll_objs_repaired[i]);
847 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
848 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
849 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
850 &src->ll_lldk_latest_scanned_phase2);
854 * Load the OST bitmap from the lfsck_layout trace file.
856 * \param[in] env pointer to the thread context
857 * \param[in] com pointer to the lfsck component
859 * \retval 0 for success
860 * \retval negative error number on failure or data corruption
862 static int lfsck_layout_load_bitmap(const struct lu_env *env,
863 struct lfsck_component *com)
865 struct dt_object *obj = com->lc_obj;
866 struct lfsck_assistant_data *lad = com->lc_data;
867 struct lfsck_layout *lo = com->lc_file_ram;
868 struct cfs_bitmap *bitmap = lad->lad_bitmap;
869 loff_t pos = com->lc_file_size;
875 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
877 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
879 nbits = lo->ll_bitmap_size;
881 if (unlikely(nbits < BITS_PER_LONG))
882 nbits = BITS_PER_LONG;
884 if (nbits > bitmap->size) {
885 __u32 new_bits = bitmap->size;
886 struct cfs_bitmap *new_bitmap;
888 while (new_bits < nbits)
891 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
892 if (new_bitmap == NULL)
895 lad->lad_bitmap = new_bitmap;
896 CFS_FREE_BITMAP(bitmap);
900 if (lo->ll_bitmap_size == 0) {
901 lad->lad_incomplete = 0;
902 CFS_RESET_BITMAP(bitmap);
907 size = (lo->ll_bitmap_size + 7) >> 3;
908 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
910 RETURN(rc >= 0 ? -EINVAL : rc);
912 if (cfs_bitmap_check_empty(bitmap))
913 lad->lad_incomplete = 0;
915 lad->lad_incomplete = 1;
921 * Load the layout LFSCK trace file from disk.
923 * The layout LFSCK trace file records the layout LFSCK status information
924 * and other statistics, such as how many objects have been scanned, and how
925 * many objects have been repaired, and etc. It also contains the bitmap for
926 * failed OSTs during the layout LFSCK. All these information will be loaded
927 * from disk to RAM when the layout LFSCK component setup.
929 * \param[in] env pointer to the thread context
930 * \param[in] com pointer to the lfsck component
932 * \retval positive number for file data corruption, the caller
933 * should reset the layout LFSCK trace file
934 * \retval 0 for success
935 * \retval negative error number on failure
937 static int lfsck_layout_load(const struct lu_env *env,
938 struct lfsck_component *com)
940 struct lfsck_layout *lo = com->lc_file_ram;
941 ssize_t size = com->lc_file_size;
945 rc = dt_read(env, com->lc_obj,
946 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
950 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
951 lfsck_lfsck2name(com->lc_lfsck), rc);
953 } else if (rc != size) {
954 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
955 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
959 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
960 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
961 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
962 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
963 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
971 * Store the layout LFSCK trace file on disk.
973 * The layout LFSCK trace file records the layout LFSCK status information
974 * and other statistics, such as how many objects have been scanned, and how
975 * many objects have been repaired, and etc. It also contains the bitmap for
976 * failed OSTs during the layout LFSCK. All these information will be synced
977 * from RAM to disk periodically.
979 * \param[in] env pointer to the thread context
980 * \param[in] com pointer to the lfsck component
982 * \retval 0 for success
983 * \retval negative error number on failure
985 static int lfsck_layout_store(const struct lu_env *env,
986 struct lfsck_component *com)
988 struct dt_object *obj = com->lc_obj;
989 struct lfsck_instance *lfsck = com->lc_lfsck;
990 struct lfsck_layout *lo_ram = com->lc_file_ram;
991 struct lfsck_layout *lo = com->lc_file_disk;
993 struct dt_device *dev = lfsck_obj2dev(obj);
994 struct cfs_bitmap *bitmap = NULL;
996 ssize_t size = com->lc_file_size;
1001 if (lfsck->li_master) {
1002 struct lfsck_assistant_data *lad = com->lc_data;
1004 bitmap = lad->lad_bitmap;
1005 nbits = bitmap->size;
1008 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1011 lo_ram->ll_bitmap_size = nbits;
1012 lfsck_layout_cpu_to_le(lo, lo_ram);
1013 th = dt_trans_create(env, dev);
1015 GOTO(log, rc = PTR_ERR(th));
1017 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1022 if (bitmap != NULL) {
1023 rc = dt_declare_record_write(env, obj,
1024 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1030 rc = dt_trans_start_local(env, dev, th);
1035 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1039 if (bitmap != NULL) {
1041 rc = dt_record_write(env, obj,
1042 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1049 dt_trans_stop(env, dev, th);
1053 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1054 lfsck_lfsck2name(lfsck), rc);
1059 static int lfsck_layout_init(const struct lu_env *env,
1060 struct lfsck_component *com)
1062 struct lfsck_layout *lo = com->lc_file_ram;
1065 memset(lo, 0, com->lc_file_size);
1066 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1067 lo->ll_status = LS_INIT;
1068 down_write(&com->lc_sem);
1069 rc = lfsck_layout_store(env, com);
1070 if (rc == 0 && com->lc_lfsck->li_master)
1071 rc = lfsck_load_sub_trace_files(env, com,
1072 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1073 up_write(&com->lc_sem);
1078 static int fid_is_for_ostobj(const struct lu_env *env,
1079 struct lfsck_instance *lfsck,
1080 struct dt_object *obj, const struct lu_fid *fid)
1082 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1083 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1084 struct lustre_ost_attrs *loa;
1087 fld_range_set_any(range);
1088 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1090 if (fld_range_is_ost(range))
1096 loa = &lfsck_env_info(env)->lti_loa;
1097 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1099 if (rc >= sizeof(struct lustre_mdt_attrs)) {
1100 lustre_lma_swab(&loa->loa_lma);
1102 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1105 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1110 static struct lfsck_layout_seq *
1111 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1113 struct lfsck_layout_seq *lls;
1115 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1116 if (lls->lls_seq == seq)
1119 if (lls->lls_seq > seq)
1127 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1128 struct lfsck_layout_seq *lls)
1130 struct lfsck_layout_seq *tmp;
1131 struct list_head *pos = &llsd->llsd_seq_list;
1133 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1134 if (lls->lls_seq < tmp->lls_seq) {
1135 pos = &tmp->lls_list;
1139 list_add_tail(&lls->lls_list, pos);
1143 lfsck_layout_lastid_create(const struct lu_env *env,
1144 struct lfsck_instance *lfsck,
1145 struct dt_object *obj)
1147 struct lfsck_thread_info *info = lfsck_env_info(env);
1148 struct lu_attr *la = &info->lti_la;
1149 struct dt_object_format *dof = &info->lti_dof;
1150 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1151 struct dt_device *dt = lfsck_obj2dev(obj);
1158 if (bk->lb_param & LPF_DRYRUN)
1161 memset(la, 0, sizeof(*la));
1162 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1163 la->la_valid = LA_MODE | LA_UID | LA_GID;
1164 memset(dof, 0, sizeof(*dof));
1165 dof->dof_type = dt_mode_to_dft(S_IFREG);
1167 th = dt_trans_create(env, dt);
1169 GOTO(log, rc = PTR_ERR(th));
1171 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1175 rc = dt_declare_record_write(env, obj,
1176 lfsck_buf_get(env, &lastid,
1182 rc = dt_trans_start_local(env, dt, th);
1186 dt_write_lock(env, obj, 0);
1187 if (likely(dt_object_exists(obj) == 0)) {
1188 rc = dt_create(env, obj, la, NULL, dof, th);
1190 rc = dt_record_write(env, obj,
1191 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1194 dt_write_unlock(env, obj);
1199 dt_trans_stop(env, dt, th);
1202 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1204 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1210 lfsck_layout_lastid_reload(const struct lu_env *env,
1211 struct lfsck_component *com,
1212 struct lfsck_layout_seq *lls)
1218 dt_read_lock(env, lls->lls_lastid_obj, 0);
1219 rc = dt_record_read(env, lls->lls_lastid_obj,
1220 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1221 dt_read_unlock(env, lls->lls_lastid_obj);
1222 if (unlikely(rc != 0))
1225 lastid = le64_to_cpu(lastid);
1226 if (lastid < lls->lls_lastid_known) {
1227 struct lfsck_instance *lfsck = com->lc_lfsck;
1228 struct lfsck_layout *lo = com->lc_file_ram;
1230 lls->lls_lastid = lls->lls_lastid_known;
1232 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1233 LASSERT(lfsck->li_out_notify != NULL);
1235 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1236 LE_LASTID_REBUILDING);
1237 lo->ll_flags |= LF_CRASHED_LASTID;
1239 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1240 "LAST_ID file (1) for the sequence %#llx"
1241 ", old value %llu, known value %llu\n",
1242 lfsck_lfsck2name(lfsck), lls->lls_seq,
1243 lastid, lls->lls_lastid);
1245 } else if (lastid >= lls->lls_lastid) {
1246 lls->lls_lastid = lastid;
1254 lfsck_layout_lastid_store(const struct lu_env *env,
1255 struct lfsck_component *com)
1257 struct lfsck_instance *lfsck = com->lc_lfsck;
1258 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1259 struct dt_device *dt = lfsck->li_bottom;
1260 struct lfsck_layout_slave_data *llsd = com->lc_data;
1261 struct lfsck_layout_seq *lls;
1267 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1270 if (!lls->lls_dirty)
1273 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1274 "<seq> %#llx as <oid> %llu\n",
1275 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1277 if (bk->lb_param & LPF_DRYRUN) {
1282 th = dt_trans_create(env, dt);
1285 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1286 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1287 lfsck_lfsck2name(com->lc_lfsck),
1292 lastid = cpu_to_le64(lls->lls_lastid);
1293 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1294 lfsck_buf_get(env, &lastid,
1300 rc = dt_trans_start_local(env, dt, th);
1304 dt_write_lock(env, lls->lls_lastid_obj, 0);
1305 rc = dt_record_write(env, lls->lls_lastid_obj,
1306 lfsck_buf_get(env, &lastid,
1307 sizeof(lastid)), &pos, th);
1308 dt_write_unlock(env, lls->lls_lastid_obj);
1313 dt_trans_stop(env, dt, th);
1316 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1317 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1318 lfsck_lfsck2name(com->lc_lfsck),
1327 lfsck_layout_lastid_load(const struct lu_env *env,
1328 struct lfsck_component *com,
1329 struct lfsck_layout_seq *lls)
1331 struct lfsck_instance *lfsck = com->lc_lfsck;
1332 struct lfsck_layout *lo = com->lc_file_ram;
1333 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1334 struct dt_object *obj;
1339 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1340 obj = dt_locate(env, lfsck->li_bottom, fid);
1342 RETURN(PTR_ERR(obj));
1344 /* LAST_ID crashed, to be rebuilt */
1345 if (dt_object_exists(obj) == 0) {
1346 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1347 LASSERT(lfsck->li_out_notify != NULL);
1349 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1350 LE_LASTID_REBUILDING);
1351 lo->ll_flags |= LF_CRASHED_LASTID;
1353 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1354 "LAST_ID file for sequence %#llx\n",
1355 lfsck_lfsck2name(lfsck), lls->lls_seq);
1357 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1359 struct l_wait_info lwi = LWI_TIMEOUT(
1360 cfs_time_seconds(cfs_fail_val),
1363 /* Some others may changed the cfs_fail_val
1364 * as zero after above check, re-check it for
1365 * sure to avoid falling into wait for ever. */
1366 if (likely(lwi.lwi_timeout > 0)) {
1367 struct ptlrpc_thread *thread =
1370 up_write(&com->lc_sem);
1371 l_wait_event(thread->t_ctl_waitq,
1372 !thread_is_running(thread),
1374 down_write(&com->lc_sem);
1379 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1381 dt_read_lock(env, obj, 0);
1382 rc = dt_read(env, obj,
1383 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1385 dt_read_unlock(env, obj);
1386 if (rc != 0 && rc != sizeof(__u64))
1387 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1389 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1390 LASSERT(lfsck->li_out_notify != NULL);
1392 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1393 LE_LASTID_REBUILDING);
1394 lo->ll_flags |= LF_CRASHED_LASTID;
1396 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1397 "LAST_ID file for the sequence %#llx"
1399 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1402 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1410 lfsck_object_put(env, obj);
1412 lls->lls_lastid_obj = obj;
1417 static void lfsck_layout_record_failure(const struct lu_env *env,
1418 struct lfsck_instance *lfsck,
1419 struct lfsck_layout *lo)
1423 lo->ll_objs_failed_phase1++;
1424 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1426 if (lo->ll_pos_first_inconsistent == 0 ||
1427 lo->ll_pos_first_inconsistent < cookie) {
1428 lo->ll_pos_first_inconsistent = cookie;
1430 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1431 "inconsistency at the pos [%llu]\n",
1432 lfsck_lfsck2name(lfsck),
1433 lo->ll_pos_first_inconsistent);
1437 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1438 struct lfsck_component *com,
1441 struct lfsck_instance *lfsck = com->lc_lfsck;
1442 struct lfsck_layout *lo = com->lc_file_ram;
1444 down_write(&com->lc_sem);
1445 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1446 HALF_SEC - com->lc_time_last_checkpoint);
1447 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1448 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1451 if (lo->ll_flags & LF_INCOMPLETE) {
1452 lo->ll_status = LS_PARTIAL;
1454 if (lfsck->li_master) {
1455 struct lfsck_assistant_data *lad = com->lc_data;
1457 if (lad->lad_incomplete)
1458 lo->ll_status = LS_PARTIAL;
1460 lo->ll_status = LS_COMPLETED;
1462 lo->ll_status = LS_COMPLETED;
1465 lo->ll_flags &= ~LF_SCANNED_ONCE;
1466 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1467 lo->ll_flags &= ~LF_INCONSISTENT;
1468 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1469 lo->ll_success_count++;
1470 } else if (rc == 0) {
1471 if (lfsck->li_status != 0)
1472 lo->ll_status = lfsck->li_status;
1474 lo->ll_status = LS_STOPPED;
1476 lo->ll_status = LS_FAILED;
1479 rc = lfsck_layout_store(env, com);
1480 up_write(&com->lc_sem);
1485 static int lfsck_layout_trans_stop(const struct lu_env *env,
1486 struct dt_device *dev,
1487 struct thandle *handle, int result)
1491 /* XXX: If there is something worng or it needs to repair nothing,
1492 * then notify the lower to stop the modification. Currently,
1493 * we use th_result for such purpose, that may be replaced by
1494 * some rollback mechanism in the future. */
1495 handle->th_result = result;
1496 rc = dt_trans_stop(env, dev, handle);
1498 return result > 0 ? 0 : result;
1500 return rc == 0 ? 1 : rc;
1503 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1504 struct lfsck_component *com,
1505 const struct lu_fid *pfid,
1506 const struct lu_fid *cfid,
1507 __u32 comp_id, __u32 ea_off,
1510 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1511 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1512 struct dt_device *dev;
1513 struct dt_object *obj;
1514 struct thandle *th = NULL;
1519 idx = lfsck_sub_trace_file_fid2idx(pfid);
1520 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1521 dev = lfsck_obj2dev(obj);
1523 fid_cpu_to_be(&key->lldk_fid, pfid);
1524 key->lldk_comp_id = cpu_to_be32(comp_id);
1525 key->lldk_ea_off = cpu_to_be32(ea_off);
1527 fid_cpu_to_be(rec, cfid);
1528 rec->f_ver = cpu_to_be32(ost_idx);
1530 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1532 th = dt_trans_create(env, dev);
1534 GOTO(unlock, rc = PTR_ERR(th));
1536 rc = dt_declare_insert(env, obj,
1537 (const struct dt_rec *)rec,
1538 (const struct dt_key *)key, th);
1542 rc = dt_trans_start_local(env, dev, th);
1546 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1547 (const struct dt_key *)key, th, 1);
1552 if (th && !IS_ERR(th))
1553 dt_trans_stop(env, dev, th);
1555 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1557 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1558 "ea_off = %u, ost_idx = %u, into the trace file for further "
1559 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1560 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1565 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1566 struct lfsck_component *com,
1567 const struct lu_fid *fid,
1568 __u32 comp_id, __u32 ea_off)
1570 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1571 struct dt_device *dev;
1572 struct dt_object *obj;
1573 struct thandle *th = NULL;
1578 idx = lfsck_sub_trace_file_fid2idx(fid);
1579 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1580 dev = lfsck_obj2dev(obj);
1582 fid_cpu_to_be(&key->lldk_fid, fid);
1583 key->lldk_comp_id = cpu_to_be32(comp_id);
1584 key->lldk_ea_off = cpu_to_be32(ea_off);
1586 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1588 th = dt_trans_create(env, dev);
1590 GOTO(unlock, rc = PTR_ERR(th));
1592 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1596 rc = dt_trans_start_local(env, dev, th);
1600 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1605 if (th && !IS_ERR(th))
1606 dt_trans_stop(env, dev, th);
1608 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1610 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1611 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1612 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1618 * Get the system default stripe size.
1620 * \param[in] env pointer to the thread context
1621 * \param[in] lfsck pointer to the lfsck instance
1622 * \param[out] size pointer to the default stripe size
1624 * \retval 0 for success
1625 * \retval negative error number on failure
1627 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1628 struct lfsck_instance *lfsck,
1631 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1632 struct dt_object *root;
1635 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1637 return PTR_ERR(root);
1639 /* Get the default stripe size via xattr_get on the backend root. */
1640 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1643 /* The lum->lmm_stripe_size is LE mode. The *size also
1644 * should be LE mode. So it is unnecessary to convert. */
1645 *size = lum->lmm_stripe_size;
1647 } else if (unlikely(rc == 0)) {
1651 lfsck_object_put(env, root);
1657 * \retval +1: repaired
1658 * \retval 0: did nothing
1659 * \retval -ve: on error
1661 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1662 struct lfsck_instance *lfsck,
1663 struct thandle *handle,
1664 struct dt_object *parent,
1665 const struct lu_fid *cfid,
1667 struct lov_mds_md_v1 *lmm,
1668 struct lov_ost_data_v1 *slot,
1669 int fl, __u32 ost_idx, int size)
1671 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1672 struct lu_buf ea_buf;
1679 magic = le32_to_cpu(lmm->lmm_magic);
1680 pattern = le32_to_cpu(lmm->lmm_pattern);
1681 count = le16_to_cpu(lmm->lmm_stripe_count);
1683 fid_to_ostid(cfid, oi);
1684 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1685 slot->l_ost_gen = cpu_to_le32(0);
1686 slot->l_ost_idx = cpu_to_le32(ost_idx);
1688 if (pattern & LOV_PATTERN_F_HOLE) {
1689 struct lov_ost_data_v1 *objs;
1692 if (magic == LOV_MAGIC_V1)
1693 objs = &lmm->lmm_objects[0];
1695 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1696 for (i = 0; i < count; i++, objs++) {
1697 if (lovea_slot_is_dummy(objs))
1701 /* If the @slot is the last dummy slot to be refilled,
1702 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1705 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1707 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1708 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1709 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1713 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1714 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1721 static struct lov_ost_data_v1 *
1722 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1723 const struct lu_fid *pfid,
1724 __u32 stripe_size, __u32 ea_off,
1725 __u32 pattern, __u16 count)
1727 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1728 lmm->lmm_pattern = cpu_to_le32(pattern);
1729 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1730 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1731 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1732 lmm->lmm_stripe_count = cpu_to_le16(count);
1733 lmm->lmm_layout_gen = cpu_to_le16(1);
1734 memset(&lmm->lmm_objects[0], 0,
1735 sizeof(struct lov_ost_data_v1) * count);
1737 return &lmm->lmm_objects[ea_off];
1740 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1741 struct lfsck_instance *lfsck,
1742 struct ost_layout *ol,
1743 struct dt_object *parent,
1744 struct lu_buf *buf, __u32 ea_off,
1745 struct lov_mds_md_v1 **lmm,
1746 struct lov_ost_data_v1 **objs)
1749 __u32 stripe_size = ol->ol_stripe_size;
1750 __u32 pattern = LOV_PATTERN_RAID0;
1753 if (ol->ol_stripe_count != 0)
1754 count = ol->ol_stripe_count;
1758 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1759 LASSERTF(buf->lb_len >= size,
1760 "buffer len %d is less than real size %d\n",
1761 (int)buf->lb_len, size);
1763 if (stripe_size == 0) {
1766 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1772 if (ol->ol_stripe_count > 1 ||
1773 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1774 pattern |= LOV_PATTERN_F_HOLE;
1775 memset(&(*lmm)->lmm_objects[0], 0,
1776 count * sizeof(struct lov_ost_data_v1));
1779 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1780 stripe_size, ea_off, pattern, count);
1785 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1786 struct ost_layout *ol,
1787 struct dt_object *parent,
1788 struct lu_buf *buf, __u32 ea_off,
1789 struct lov_mds_md_v1 **lmm,
1790 struct lov_ost_data_v1 **objs)
1792 struct lov_comp_md_v1 *lcm;
1793 struct lov_comp_md_entry_v1 *lcme;
1794 __u32 pattern = LOV_PATTERN_RAID0;
1795 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1796 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1797 int size = offset + lcme_size;
1799 LASSERTF(buf->lb_len >= size,
1800 "buffer len %d is less than real size %d\n",
1801 (int)buf->lb_len, size);
1804 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1805 lcm->lcm_size = cpu_to_le32(size);
1806 lcm->lcm_layout_gen = cpu_to_le32(1);
1808 lcm->lcm_entry_count = cpu_to_le16(1);
1810 lcme = &lcm->lcm_entries[0];
1811 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1812 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1813 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1814 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1815 lcme->lcme_offset = cpu_to_le32(offset);
1816 lcme->lcme_size = cpu_to_le32(lcme_size);
1817 if (ol->ol_stripe_count > 1)
1818 pattern |= LOV_PATTERN_F_HOLE;
1820 *lmm = buf->lb_buf + offset;
1821 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1822 ol->ol_stripe_size, ea_off,
1823 pattern, ol->ol_stripe_count);
1828 static int lfsck_layout_add_comp_comp(const struct lu_env *env,
1829 struct lfsck_instance *lfsck,
1830 struct thandle *handle,
1831 struct ost_layout *ol,
1832 struct dt_object *parent,
1833 const struct lu_fid *cfid,
1834 struct lu_buf *buf, __u32 ost_idx,
1835 __u32 ea_off, int pos)
1837 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1838 struct lov_comp_md_entry_v1 *lcme;
1839 struct lov_mds_md_v1 *lmm;
1840 struct lov_ost_data_v1 *objs;
1841 int added = sizeof(*lcme) +
1842 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1843 int size = le32_to_cpu(lcm->lcm_size) + added;
1847 __u32 pattern = LOV_PATTERN_RAID0;
1848 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1851 lu_buf_check_and_grow(buf, size);
1852 /* set the lcm again because lu_buf_check_and_grow() may
1853 * have reallocated the buf. */
1855 lcm->lcm_size = cpu_to_le32(size);
1856 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1857 lcm->lcm_entry_count = cpu_to_le16(count + 1);
1859 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
1860 * with distance of 'added'. */
1863 for (i = pos; i < count; i++) {
1864 lcme = &lcm->lcm_entries[i];
1865 size += le32_to_cpu(lcme->lcme_size);
1868 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
1869 memmove(buf->lb_buf + offset + added,
1870 buf->lb_buf + offset, size);
1874 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
1875 * of 'sizeof(struct lov_comp_md_entry_v1)' */
1877 for (i = 0; i < pos; i++) {
1878 lcme = &lcm->lcm_entries[i];
1879 size += le32_to_cpu(lcme->lcme_size);
1882 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
1883 memmove(buf->lb_buf + offset + sizeof(*lcme),
1884 buf->lb_buf + offset, size);
1887 /* 3. Recalculate the enter offset for the component [pos, count-1] */
1888 for (i = count - 1; i >= pos; i--) {
1889 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
1890 lcm->lcm_entries[i + 1].lcme_offset =
1891 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
1892 lcme_offset) + added);
1895 /* 4. Recalculate the enter offset for the component [0, pos) */
1896 for (i = 0; i < pos; i++) {
1897 lcm->lcm_entries[i].lcme_offset =
1898 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
1899 lcme_offset) + sizeof(*lcme));
1902 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
1903 /* 4. Insert the new component header (entry) at the slot 'pos'. */
1904 lcme = &lcm->lcm_entries[pos];
1905 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1906 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1907 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1908 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1909 lcme->lcme_offset = cpu_to_le32(offset);
1910 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
1913 if (ol->ol_stripe_count > 1)
1914 pattern |= LOV_PATTERN_F_HOLE;
1916 lmm = buf->lb_buf + offset;
1917 /* 5. Insert teh new component body at the 'offset'. */
1918 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
1919 ol->ol_stripe_size, ea_off,
1920 pattern, ol->ol_stripe_count);
1922 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
1923 lmm, objs, LU_XATTR_REPLACE, ost_idx,
1924 le32_to_cpu(lcm->lcm_size));
1926 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
1927 DFID": parent "DFID", OST-index %u, stripe-index %u, "
1928 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
1929 "comp_end %llu, %s LOV EA hole: rc = %d\n",
1930 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1931 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
1932 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
1933 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
1934 "with" : "without", rc);
1939 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
1940 struct lfsck_instance *lfsck,
1941 struct thandle *handle,
1942 struct ost_layout *ol,
1943 struct dt_object *parent,
1944 const struct lu_fid *cfid,
1945 struct lu_buf *buf, __u32 ost_idx,
1948 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1949 struct lov_ost_data_v1 *objs;
1950 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
1951 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1957 /* The original LOVEA maybe re-generated via old filter_fid, at
1958 * that time, we do not know the stripe count and stripe size. */
1959 if (ol->ol_stripe_count > count)
1960 count = ol->ol_stripe_count;
1961 if (ol->ol_stripe_size != 0 &&
1962 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
1963 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
1965 if (magic == LOV_MAGIC_V1)
1966 objs = &lmm->lmm_objects[count];
1968 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
1970 gap = ea_off - count;
1974 size = lov_mds_md_size(count, magic);
1975 LASSERTF(buf->lb_len >= size,
1976 "buffer len %d is less than real size %d\n",
1977 (int)buf->lb_len, size);
1980 memset(objs, 0, gap * sizeof(*objs));
1981 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1984 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1985 lmm->lmm_stripe_count = cpu_to_le16(count);
1988 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
1989 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
1991 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1992 DFID": parent "DFID", OST-index %u, stripe-index %u, "
1993 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
1994 "comp_end %llu, %s LOV EA hole: rc = %d\n",
1995 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1996 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
1997 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
1998 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
1999 "with" : "without", rc);
2005 * \retval +1: repaired
2006 * \retval 0: did nothing
2007 * \retval -ve: on error
2009 static int lfsck_layout_update_lovea(const struct lu_env *env,
2010 struct lfsck_instance *lfsck,
2011 struct thandle *handle,
2012 struct ost_layout *ol,
2013 struct dt_object *parent,
2014 const struct lu_fid *cfid,
2015 struct lu_buf *buf, int fl,
2016 __u32 ost_idx, __u32 ea_off)
2018 struct lov_mds_md_v1 *lmm = NULL;
2019 struct lov_ost_data_v1 *objs = NULL;
2023 if (ol->ol_comp_id != 0)
2024 rc = lfsck_layout_new_comp_lovea(env, ol, parent, buf, ea_off,
2027 rc = lfsck_layout_new_v1_lovea(env, lfsck, ol, parent, buf,
2028 ea_off, &lmm, &objs);
2031 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2032 buf, lmm, objs, fl, ost_idx, rc);
2034 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2035 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2036 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2037 "comp_end %llu, fl %d, %s LOV EA hole: rc = %d\n",
2038 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2039 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2040 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end, fl,
2041 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2042 "with" : "without", rc);
2047 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2048 struct dt_object *child,
2049 const struct lu_fid *pfid,
2050 const struct ost_layout *ol, __u32 offset)
2052 struct dt_device *dev = lfsck_obj2dev(child);
2053 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2054 struct thandle *handle;
2055 struct lu_buf buf = { NULL };
2058 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2059 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2060 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2061 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2062 * parent MDT-object's layout EA. */
2063 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2064 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2065 lfsck_buf_init(&buf, ff, sizeof(*ff));
2067 handle = dt_trans_create(env, dev);
2069 RETURN(PTR_ERR(handle));
2071 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2075 rc = dt_trans_start_local(env, dev, handle);
2079 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2084 dt_trans_stop(env, dev, handle);
2090 * \retval +1: repaired
2091 * \retval 0: did nothing
2092 * \retval -ve: on error
2094 static int lfsck_layout_update_pfid(const struct lu_env *env,
2095 struct lfsck_component *com,
2096 struct dt_object *parent,
2097 struct lu_fid *cfid,
2098 struct dt_device *cdev,
2099 struct ost_layout *ol, __u32 ea_off)
2101 struct dt_object *child;
2105 child = lfsck_object_find_by_dev(env, cdev, cfid);
2107 RETURN(PTR_ERR(child));
2109 rc = __lfsck_layout_update_pfid(env, child,
2110 lu_object_fid(&parent->do_lu),
2112 lfsck_object_put(env, child);
2114 RETURN(rc == 0 ? 1 : rc);
2117 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2119 if (ol->ol_comp_id != 0)
2120 return sizeof(struct lov_comp_md_v1) +
2121 sizeof(struct lov_comp_md_entry_v1) +
2122 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2124 if (ol->ol_stripe_count != 0)
2125 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2127 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2131 * This function will create the MDT-object with the given (partial) LOV EA.
2133 * Under some data corruption cases, the MDT-object of the file may be lost,
2134 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2135 * re-create the MDT-object with the orphan OST-object(s) information.
2137 * On the other hand, the LFSCK may has created some OST-object for repairing
2138 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2139 * the old OST-object is there and should replace the former new created OST
2140 * object. Unfortunately, some others have modified such newly created object.
2141 * To keep the data (both new and old), the LFSCK will create MDT-object with
2142 * new FID to reference the original OST-object.
2144 * \param[in] env pointer to the thread context
2145 * \param[in] com pointer to the lfsck component
2146 * \param[in] ltd pointer to target device descriptor
2147 * \param[in] rec pointer to the record for the orphan OST-object
2148 * \param[in] cfid pointer to FID for the orphan OST-object
2149 * \param[in] infix additional information, such as the FID for original
2150 * MDT-object and the stripe offset in the LOV EA
2151 * \param[in] type the type for describing why the orphan MDT-object is
2152 * created. The rules are as following:
2154 * type "C": Multiple OST-objects claim the same MDT-object and the
2155 * same slot in the layout EA. Then the LFSCK will create
2156 * new MDT-object(s) to hold the conflict OST-object(s).
2158 * type "N": The orphan OST-object does not know which one was the
2159 * real parent MDT-object, so the LFSCK uses new FID for
2160 * its parent MDT-object.
2162 * type "R": The orphan OST-object knows its parent MDT-object FID,
2163 * but does not know the position (the file name) in the
2166 * type "D": The MDT-object is a directory, it may knows its parent
2167 * but because there is no valid linkEA, the LFSCK cannot
2168 * know where to put it back to the namespace.
2169 * type "O": The MDT-object has no linkEA, and there is no name
2170 * entry that references the MDT-object.
2172 * type "P": The orphan object to be created was a parent directory
2173 * of some MDT-object which linkEA shows that the @orphan
2174 * object is missing.
2176 * The orphan name will be like:
2177 * ${FID}-${infix}-${type}-${conflict_version}
2179 * \param[in] ea_off the stripe offset in the LOV EA
2181 * \retval positive on repaired something
2182 * \retval 0 if needs to repair nothing
2183 * \retval negative error number on failure
2185 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2186 struct lfsck_component *com,
2187 struct lfsck_tgt_desc *ltd,
2188 struct lu_orphan_rec_v2 *rec,
2189 struct lu_fid *cfid,
2194 struct lfsck_thread_info *info = lfsck_env_info(env);
2195 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2196 char *name = info->lti_key;
2197 struct lu_attr *la = &info->lti_la2;
2198 struct dt_object_format *dof = &info->lti_dof;
2199 struct lfsck_instance *lfsck = com->lc_lfsck;
2200 struct ost_layout *ol = &rec->lor_layout;
2201 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2202 struct lu_fid *tfid = &info->lti_fid3;
2203 struct dt_device *dev = lfsck->li_bottom;
2204 struct dt_object *lpf = lfsck->li_lpf_obj;
2205 struct dt_object *pobj = NULL;
2206 struct dt_object *cobj = NULL;
2207 struct thandle *th = NULL;
2208 struct lu_buf *ea_buf = &info->lti_big_buf;
2209 struct lu_buf lov_buf;
2210 struct lfsck_lock_handle *llh = &info->lti_llh;
2211 struct linkea_data ldata = { NULL };
2212 struct lu_buf linkea_buf;
2213 const struct lu_name *pname;
2219 if (unlikely(lpf == NULL))
2220 GOTO(log, rc = -ENXIO);
2222 /* We use two separated transactions to repair the inconsistency.
2224 * 1) create the MDT-object locally.
2225 * 2) update the OST-object's PFID EA if necessary.
2227 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2228 * updated when the layout LFSCK run next time.
2230 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2231 * when the layout LFSCK run next time. */
2233 if (fid_is_zero(pfid)) {
2234 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2238 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2240 GOTO(log, rc = PTR_ERR(cobj));
2243 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2245 GOTO(log, rc = PTR_ERR(pobj));
2247 LASSERT(infix != NULL);
2248 LASSERT(type != NULL);
2250 memset(la, 0, sizeof(*la));
2251 la->la_uid = rec->lor_rec.lor_uid;
2252 la->la_gid = rec->lor_rec.lor_gid;
2253 la->la_mode = S_IFREG | S_IRUSR;
2254 la->la_valid = LA_MODE | LA_UID | LA_GID;
2256 memset(dof, 0, sizeof(*dof));
2257 dof->dof_type = dt_mode_to_dft(S_IFREG);
2258 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2259 * the stripe(s). The LFSCK will specify the LOV EA via
2260 * lfsck_layout_update_lovea(). */
2262 size = lfsck_lovea_size(ol, ea_off);
2263 if (ea_buf->lb_len < size) {
2264 lu_buf_realloc(ea_buf, size);
2265 if (ea_buf->lb_buf == NULL)
2266 GOTO(log, rc = -ENOMEM);
2271 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2273 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2274 (const struct dt_key *)name);
2275 if (rc != 0 && rc != -ENOENT)
2279 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2280 MDS_INODELOCK_UPDATE, LCK_PW);
2284 /* Re-check whether the name conflict with othrs after taken
2286 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2287 (const struct dt_key *)name);
2288 if (unlikely(rc == 0)) {
2296 pname = lfsck_name_get_const(env, name, strlen(name));
2297 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2298 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2302 /* The 1st transaction. */
2303 th = dt_trans_create(env, dev);
2305 GOTO(unlock, rc = PTR_ERR(th));
2307 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2311 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2312 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2313 LU_XATTR_CREATE, th);
2317 dtrec->rec_fid = pfid;
2318 dtrec->rec_type = S_IFREG;
2319 rc = dt_declare_insert(env, lpf,
2320 (const struct dt_rec *)dtrec,
2321 (const struct dt_key *)name, th);
2325 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2326 ldata.ld_leh->leh_len);
2327 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2328 XATTR_NAME_LINK, 0, th);
2332 rc = dt_trans_start_local(env, dev, th);
2336 dt_write_lock(env, pobj, 0);
2337 rc = dt_create(env, pobj, la, NULL, dof, th);
2339 rc = lfsck_layout_update_lovea(env, lfsck, th, ol, pobj, cfid,
2340 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2341 dt_write_unlock(env, pobj);
2345 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2346 (const struct dt_key *)name, th, 1);
2350 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2351 if (rc == 0 && cobj != NULL) {
2352 dt_trans_stop(env, dev, th);
2355 /* The 2nd transaction. */
2356 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ol, ea_off);
2363 dt_trans_stop(env, dev, th);
2369 if (cobj != NULL && !IS_ERR(cobj))
2370 lfsck_object_put(env, cobj);
2371 if (pobj != NULL && !IS_ERR(pobj))
2372 lfsck_object_put(env, pobj);
2375 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2376 "recreate the lost MDT-object: parent "DFID
2377 ", child "DFID", OST-index %u, stripe-index %u, "
2378 "infix %s, type %s: rc = %d\n",
2379 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2380 ltd->ltd_index, ea_off, infix, type, rc);
2382 return rc >= 0 ? 1 : rc;
2385 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2386 struct lfsck_component *com,
2387 const struct lu_fid *fid,
2390 struct lfsck_thread_info *info = lfsck_env_info(env);
2391 struct lfsck_request *lr = &info->lti_lr;
2392 struct lfsck_instance *lfsck = com->lc_lfsck;
2393 struct lfsck_tgt_desc *ltd;
2394 struct ptlrpc_request *req;
2395 struct lfsck_request *tmp;
2396 struct obd_export *exp;
2400 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2401 if (unlikely(ltd == NULL))
2405 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2406 GOTO(put, rc = -EOPNOTSUPP);
2408 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2410 GOTO(put, rc = -ENOMEM);
2412 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2414 ptlrpc_request_free(req);
2419 memset(lr, 0, sizeof(*lr));
2420 lr->lr_event = LE_CONDITIONAL_DESTROY;
2421 lr->lr_active = LFSCK_TYPE_LAYOUT;
2424 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2426 ptlrpc_request_set_replen(req);
2428 rc = ptlrpc_queue_wait(req);
2429 ptlrpc_req_finished(req);
2439 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2440 struct lfsck_component *com,
2441 struct lfsck_request *lr)
2443 struct lfsck_thread_info *info = lfsck_env_info(env);
2444 struct lu_attr *la = &info->lti_la;
2445 union ldlm_policy_data *policy = &info->lti_policy;
2446 struct ldlm_res_id *resid = &info->lti_resid;
2447 struct lfsck_instance *lfsck = com->lc_lfsck;
2448 struct dt_device *dev = lfsck->li_bottom;
2449 struct lu_fid *fid = &lr->lr_fid;
2450 struct dt_object *obj;
2451 struct thandle *th = NULL;
2452 struct lustre_handle lh = { 0 };
2457 obj = lfsck_object_find_by_dev(env, dev, fid);
2459 RETURN(PTR_ERR(obj));
2461 dt_read_lock(env, obj, 0);
2462 if (dt_object_exists(obj) == 0 ||
2463 lfsck_is_dead_obj(obj)) {
2464 dt_read_unlock(env, obj);
2466 GOTO(put, rc = -ENOENT);
2469 /* Get obj's attr without lock firstly. */
2470 rc = dt_attr_get(env, obj, la);
2471 dt_read_unlock(env, obj);
2475 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2476 GOTO(put, rc = -ETXTBSY);
2478 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2479 LASSERT(lfsck->li_namespace != NULL);
2481 memset(policy, 0, sizeof(*policy));
2482 policy->l_extent.end = OBD_OBJECT_EOF;
2483 ost_fid_build_resid(fid, resid);
2484 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2485 policy, LCK_EX, &flags, ldlm_blocking_ast,
2486 ldlm_completion_ast, NULL, NULL, 0,
2487 LVB_T_NONE, NULL, &lh);
2489 GOTO(put, rc = -EIO);
2491 dt_write_lock(env, obj, 0);
2492 /* Get obj's attr within lock again. */
2493 rc = dt_attr_get(env, obj, la);
2497 if (la->la_ctime != 0)
2498 GOTO(unlock, rc = -ETXTBSY);
2500 th = dt_trans_create(env, dev);
2502 GOTO(unlock, rc = PTR_ERR(th));
2504 rc = dt_declare_ref_del(env, obj, th);
2508 rc = dt_declare_destroy(env, obj, th);
2512 rc = dt_trans_start_local(env, dev, th);
2516 rc = dt_ref_del(env, obj, th);
2520 rc = dt_destroy(env, obj, th);
2522 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2523 "OST-object "DFID" that was created for reparing "
2524 "dangling referenced case. But the original missing "
2525 "OST-object is found now.\n",
2526 lfsck_lfsck2name(lfsck), PFID(fid));
2531 dt_trans_stop(env, dev, th);
2534 dt_write_unlock(env, obj);
2535 ldlm_lock_decref(&lh, LCK_EX);
2538 lfsck_object_put(env, obj);
2544 * Some OST-object has occupied the specified layout EA slot.
2545 * Such OST-object may be generated by the LFSCK when repair
2546 * dangling referenced MDT-object, which can be indicated by
2547 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2548 * is true and such OST-object has not been modified yet, we
2549 * will replace it with the orphan OST-object; otherwise the
2550 * LFSCK will create new MDT-object to reference the orphan.
2552 * \retval +1: repaired
2553 * \retval 0: did nothing
2554 * \retval -ve: on error
2556 static int lfsck_layout_conflict_create(const struct lu_env *env,
2557 struct lfsck_component *com,
2558 struct lfsck_tgt_desc *ltd,
2559 struct lu_orphan_rec_v2 *rec,
2560 struct dt_object *parent,
2561 struct lu_fid *cfid,
2562 struct lu_buf *ea_buf,
2563 struct lov_mds_md_v1 *lmm,
2564 struct lov_ost_data_v1 *slot,
2565 __u32 ea_off, int lovea_size)
2567 struct lfsck_thread_info *info = lfsck_env_info(env);
2568 struct lu_fid *cfid2 = &info->lti_fid2;
2569 struct ost_id *oi = &info->lti_oi;
2570 struct dt_device *dev = lfsck_obj2dev(parent);
2571 struct thandle *th = NULL;
2572 struct lustre_handle lh = { 0 };
2573 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2577 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2578 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2582 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2583 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2587 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2588 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2593 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2595 /* If the conflict OST-obejct is not created for fixing dangling
2596 * referenced MDT-object in former LFSCK check/repair, or it has
2597 * been modified by others, then we cannot destroy it. Re-create
2598 * a new MDT-object for the orphan OST-object. */
2599 if (rc == -ETXTBSY) {
2600 /* No need the layout lock on the original parent. */
2601 lfsck_ibits_unlock(&lh, LCK_EX);
2603 fid_zero(&rec->lor_rec.lor_fid);
2604 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2605 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2607 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2608 info->lti_tmpbuf, "C", ea_off);
2613 if (rc != 0 && rc != -ENOENT)
2616 th = dt_trans_create(env, dev);
2618 GOTO(unlock, rc = PTR_ERR(th));
2620 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2621 LU_XATTR_REPLACE, th);
2625 rc = dt_trans_start_local(env, dev, th);
2629 dt_write_lock(env, parent, 0);
2630 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2631 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2632 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2633 ltd->ltd_index, lovea_size);
2634 dt_write_unlock(env, parent);
2639 dt_trans_stop(env, dev, th);
2642 lfsck_ibits_unlock(&lh, LCK_EX);
2645 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2646 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2647 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2648 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2649 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2652 return rc >= 0 ? 1 : rc;
2656 * \retval +1: repaired
2657 * \retval 0: did nothing
2658 * \retval -ve: on error
2660 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2661 struct lfsck_component *com,
2662 struct lfsck_tgt_desc *ltd,
2663 struct lu_orphan_rec_v2 *rec,
2664 struct dt_object *parent,
2665 struct lu_fid *cfid,
2666 __u32 ost_idx, __u32 ea_off)
2668 struct lfsck_thread_info *info = lfsck_env_info(env);
2669 struct lu_buf *buf = &info->lti_big_buf;
2670 struct lu_fid *fid = &info->lti_fid2;
2671 struct ost_id *oi = &info->lti_oi;
2672 struct lfsck_instance *lfsck = com->lc_lfsck;
2673 struct dt_device *dt = lfsck_obj2dev(parent);
2674 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2675 struct ost_layout *ol = &rec->lor_layout;
2676 struct lov_comp_md_v1 *lcm = NULL;
2677 struct lov_comp_md_entry_v1 *lcme = NULL;
2678 struct thandle *handle = NULL;
2680 struct lov_mds_md_v1 *lmm;
2681 struct lov_ost_data_v1 *objs;
2682 struct lustre_handle lh = { 0 };
2690 bool locked = false;
2693 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2694 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2697 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2698 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2699 "stripe-index %u, comp_id %u, comp_start %llu, "
2700 "comp_end %llu: rc = %d\n",
2701 lfsck_lfsck2name(lfsck), PFID(cfid),
2702 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2703 ol->ol_comp_id, ol->ol_comp_start,
2704 ol->ol_comp_end, rc);
2711 dt_write_unlock(env, parent);
2715 if (handle != NULL) {
2716 dt_trans_stop(env, dt, handle);
2721 GOTO(unlock_layout, rc);
2724 if (buf->lb_len < lovea_size) {
2725 lu_buf_realloc(buf, lovea_size);
2726 if (buf->lb_buf == NULL)
2727 GOTO(unlock_layout, rc = -ENOMEM);
2730 if (!(bk->lb_param & LPF_DRYRUN)) {
2731 handle = dt_trans_create(env, dt);
2733 GOTO(unlock_layout, rc = PTR_ERR(handle));
2735 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2740 rc = dt_trans_start_local(env, dt, handle);
2745 dt_write_lock(env, parent, 0);
2747 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2748 if (rc == -ERANGE) {
2749 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2752 } else if (rc == -ENODATA || rc == 0) {
2753 lovea_size = lfsck_lovea_size(ol, ea_off);
2754 /* If the declared is not big enough, re-try. */
2755 if (buf->lb_len < lovea_size) {
2759 fl = LU_XATTR_CREATE;
2760 } else if (rc < 0) {
2761 GOTO(unlock_parent, rc);
2762 } else if (unlikely(buf->lb_len == 0)) {
2765 fl = LU_XATTR_REPLACE;
2769 if (fl == LU_XATTR_CREATE) {
2770 if (bk->lb_param & LPF_DRYRUN)
2771 GOTO(unlock_parent, rc = 1);
2773 LASSERT(buf->lb_len >= lovea_size);
2775 rc = lfsck_layout_update_lovea(env, lfsck, handle, ol, parent,
2776 cfid, buf, fl, ost_idx, ea_off);
2778 GOTO(unlock_parent, rc);
2782 rc1 = lfsck_layout_verify_header(parent, lmm);
2784 /* If the LOV EA crashed, the rebuild it. */
2785 if (rc1 == -EINVAL) {
2786 if (bk->lb_param & LPF_DRYRUN)
2787 GOTO(unlock_parent, rc = 1);
2789 LASSERT(buf->lb_len >= lovea_size);
2791 rc = lfsck_layout_update_lovea(env, lfsck, handle, ol, parent,
2792 cfid, buf, fl, ost_idx, ea_off);
2794 GOTO(unlock_parent, rc);
2797 /* For other unknown magic/pattern, keep the current LOV EA. */
2799 GOTO(unlock_parent, rc = rc1);
2801 magic = le32_to_cpu(lmm->lmm_magic);
2802 if (magic == LOV_MAGIC_COMP_V1) {
2807 count = le16_to_cpu(lcm->lcm_entry_count);
2808 for (i = 0; i < count; i++) {
2809 lcme = &lcm->lcm_entries[i];
2810 start = le64_to_cpu(lcme->lcme_extent.e_start);
2811 end = le64_to_cpu(lcme->lcme_extent.e_end);
2813 if (end <= ol->ol_comp_start)
2816 if (start >= ol->ol_comp_end)
2819 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
2820 magic = le32_to_cpu(lmm->lmm_magic);
2821 flags = le32_to_cpu(lcme->lcme_flags);
2825 rc = lfsck_layout_add_comp_comp(env, lfsck, handle, ol, parent,
2826 cfid, buf, ost_idx, ea_off, i);
2828 GOTO(unlock_parent, rc);
2832 count = le16_to_cpu(lmm->lmm_stripe_count);
2834 GOTO(unlock_parent, rc = -EINVAL);
2837 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2838 if (count <= ea_off) {
2839 if (bk->lb_param & LPF_DRYRUN)
2840 GOTO(unlock_parent, rc = 1);
2842 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2843 /* If the declared is not big enough, re-try. */
2844 if (buf->lb_len < lovea_size) {
2849 if (lcme && !(flags & LCME_FL_INIT))
2850 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
2852 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
2853 parent, cfid, buf, ost_idx, ea_off);
2855 GOTO(unlock_parent, rc);
2858 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2860 if (magic == LOV_MAGIC_V1) {
2861 objs = &lmm->lmm_objects[0];
2863 LASSERT(magic == LOV_MAGIC_V3);
2864 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2867 for (i = 0; i < count; i++, objs++) {
2868 /* The MDT-object was created via lfsck_layout_recover_create()
2869 * by others before, and we fill the dummy layout EA. */
2870 if ((lcme && !(flags & LCME_FL_INIT)) ||
2871 lovea_slot_is_dummy(objs)) {
2875 if (bk->lb_param & LPF_DRYRUN)
2876 GOTO(unlock_parent, rc = 1);
2878 lmm->lmm_layout_gen =
2879 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2883 if (le32_to_cpu(lmm->lmm_stripe_size) !=
2884 ol->ol_stripe_size ||
2885 le16_to_cpu(lmm->lmm_stripe_count) !=
2886 ol->ol_stripe_count ||
2887 le64_to_cpu(lcme->lcme_extent.e_start) !=
2888 ol->ol_comp_start ||
2889 le64_to_cpu(lcme->lcme_extent.e_end) !=
2891 CDEBUG(D_LFSCK, "%s: found invalid "
2892 "component for "DFID ": parent "DFID
2893 ", stripe-index %u, stripe_size %u, "
2894 "stripe_count %u, comp_id %u, "
2895 "comp_start %llu, comp_end %llu, "
2896 "cur_stripe_size %u, "
2897 "cur_stripe_count %u, "
2898 "cur_comp_start %llu, "
2899 "cur_comp_end %llu\n",
2900 lfsck_lfsck2name(lfsck), PFID(cfid),
2901 PFID(lfsck_dto2fid(parent)), ea_off,
2903 ol->ol_stripe_count, ol->ol_comp_id,
2904 ol->ol_comp_start, ol->ol_comp_end,
2905 le32_to_cpu(lmm->lmm_stripe_size),
2906 le16_to_cpu(lmm->lmm_stripe_count),
2907 le64_to_cpu(lcme->lcme_extent.e_start),
2908 le64_to_cpu(lcme->lcme_extent.e_end));
2910 GOTO(unlock_parent, rc = -EINVAL);
2913 le32_add_cpu(&lcm->lcm_layout_gen, 1);
2914 lovea_size = le32_to_cpu(lcm->lcm_size);
2915 if (!(flags & LCME_FL_INIT))
2916 lcme->lcme_flags = cpu_to_le32(flags |
2920 LASSERTF(buf->lb_len >= lovea_size,
2921 "buffer len %d is less than real size %d\n",
2922 (int)buf->lb_len, (int)lovea_size);
2924 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
2925 parent, cfid, buf, lmm, objs,
2926 fl, ost_idx, lovea_size);
2928 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2929 "dummy layout slot for "DFID": parent "DFID
2930 ", OST-index %u, stripe-index %u: rc = %d\n",
2931 lfsck_lfsck2name(lfsck), PFID(cfid),
2932 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2934 GOTO(unlock_parent, rc);
2937 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2938 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2940 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2941 "invalid layout EA at the slot %d, index %u\n",
2942 lfsck_lfsck2name(lfsck),
2943 PFID(lfsck_dto2fid(parent)), i,
2944 le32_to_cpu(objs->l_ost_idx));
2946 GOTO(unlock_parent, rc);
2949 /* It should be rare case, the slot is there, but the LFSCK
2950 * does not handle it during the first-phase cycle scanning. */
2951 if (unlikely(lu_fid_eq(fid, cfid))) {
2953 GOTO(unlock_parent, rc = 0);
2955 /* Rare case that the OST-object index
2956 * does not match the parent MDT-object
2957 * layout EA. We trust the later one. */
2958 if (bk->lb_param & LPF_DRYRUN)
2959 GOTO(unlock_parent, rc = 1);
2961 dt_write_unlock(env, parent);
2963 dt_trans_stop(env, dt, handle);
2964 lfsck_ibits_unlock(&lh, LCK_EX);
2965 rc = lfsck_layout_update_pfid(env, com, parent,
2969 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2970 "updated OST-object's pfid for "DFID
2971 ": parent "DFID", OST-index %u, "
2972 "stripe-index %u: rc = %d\n",
2973 lfsck_lfsck2name(lfsck), PFID(cfid),
2974 PFID(lfsck_dto2fid(parent)),
2975 ltd->ltd_index, i, rc);
2982 /* The MDT-object exists, but related layout EA slot is occupied
2984 if (bk->lb_param & LPF_DRYRUN)
2985 GOTO(unlock_parent, rc = 1);
2987 dt_write_unlock(env, parent);
2989 dt_trans_stop(env, dt, handle);
2990 lfsck_ibits_unlock(&lh, LCK_EX);
2991 if (magic == LOV_MAGIC_V1)
2992 objs = &lmm->lmm_objects[ea_off];
2994 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2995 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2996 buf, lmm, objs, ea_off, lovea_size);
3002 dt_write_unlock(env, parent);
3006 dt_trans_stop(env, dt, handle);
3009 lfsck_ibits_unlock(&lh, LCK_EX);
3014 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3015 struct lfsck_component *com,
3016 struct lfsck_tgt_desc *ltd,
3017 struct lu_orphan_rec_v2 *rec,
3018 struct lu_fid *cfid)
3020 struct lfsck_layout *lo = com->lc_file_ram;
3021 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3022 struct dt_object *parent = NULL;
3023 __u32 ea_off = pfid->f_stripe_idx;
3027 if (!fid_is_sane(cfid))
3028 GOTO(out, rc = -EINVAL);
3031 if (fid_is_zero(pfid)) {
3032 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3037 if (!fid_is_sane(pfid))
3038 GOTO(out, rc = -EINVAL);
3040 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3042 GOTO(out, rc = PTR_ERR(parent));
3044 if (unlikely(dt_object_remote(parent) != 0))
3045 GOTO(put, rc = -EXDEV);
3047 if (dt_object_exists(parent) == 0) {
3048 lfsck_object_put(env, parent);
3049 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3054 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3055 GOTO(put, rc = -EISDIR);
3057 /* The orphan OST-object claims to be the parent's stripe, then
3058 * related dangling record in the trace file is meaningless. */
3059 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3060 rec->lor_layout.ol_comp_id, ea_off);
3061 if (rc && rc != -ENOENT)
3064 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3065 ltd->ltd_index, ea_off);
3071 lfsck_object_put(env, parent);
3073 /* The layout EA is changed, need to be reloaded next time. */
3074 dt_object_put_nocache(env, parent);
3077 down_write(&com->lc_sem);
3078 com->lc_new_scanned++;
3079 com->lc_new_checked++;
3081 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3083 } else if (rc < 0) {
3084 lo->ll_objs_failed_phase2++;
3086 up_write(&com->lc_sem);
3091 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3092 struct lfsck_component *com,
3093 struct lfsck_tgt_desc *ltd)
3095 struct lfsck_assistant_data *lad = com->lc_data;
3096 struct lfsck_instance *lfsck = com->lc_lfsck;
3097 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3098 struct lfsck_thread_info *info = lfsck_env_info(env);
3099 struct ost_id *oi = &info->lti_oi;
3100 struct lu_fid *fid = &info->lti_fid;
3101 struct dt_object *obj;
3102 const struct dt_it_ops *iops;
3107 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3108 "scanning for OST%04x\n",
3109 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3111 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
3112 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3113 "scanning for OST%04x\n",
3114 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3119 ostid_set_seq(oi, FID_SEQ_IDIF);
3120 rc = ostid_set_id(oi, 0);
3124 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
3128 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3129 if (unlikely(IS_ERR(obj)))
3130 GOTO(log, rc = PTR_ERR(obj));
3132 rc = obj->do_ops->do_index_try(env, obj,
3133 &dt_lfsck_layout_orphan_features);
3137 iops = &obj->do_index_ops->dio_it;
3138 di = iops->init(env, obj, 0);
3140 GOTO(put, rc = PTR_ERR(di));
3142 rc = iops->load(env, di, 0);
3144 /* -ESRCH means that the orphan OST-objects rbtree has been
3145 * cleanup because of the OSS server restart or other errors. */
3146 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3151 rc = iops->next(env, di);
3163 struct lu_orphan_rec_v2 *rec = &info->lti_rec;
3165 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3166 unlikely(!thread_is_running(&lfsck->li_thread)))
3169 key = iops->key(env, di);
3170 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3171 /* Remote target OST may be runnning old LFSCK */
3172 memset(rec, 0, sizeof(*rec));
3173 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3175 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3176 &com->lc_fid_latest_scanned_phase2);
3177 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3180 lfsck_control_speed_by_self(com);
3182 rc = iops->next(env, di);
3183 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3190 iops->fini(env, di);
3192 lfsck_object_put(env, obj);
3195 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3196 "scanning for OST%04x: rc = %d\n",
3197 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3199 return rc > 0 ? 0 : rc;
3202 static int lfsck_lmm2layout(struct lov_mds_md_v1 *lmm, struct ost_layout *ol,
3205 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3209 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3210 ol->ol_stripe_size = lmm->lmm_stripe_size;
3211 ol->ol_stripe_count = lmm->lmm_stripe_count;
3212 ol->ol_comp_start = 0;
3213 ol->ol_comp_end = 0;
3215 } else if (magic == LOV_MAGIC_COMP_V1) {
3216 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3217 struct lov_comp_md_entry_v1 *lcme = NULL;
3218 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3221 for (i = 0; i < count; i++) {
3222 lcme = &lcm->lcm_entries[i];
3223 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3224 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3231 /* The comp has been removed, do nothing. */
3235 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3236 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3237 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3238 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3239 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3240 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3242 GOTO(out, rc = -EINVAL);
3252 * Repair the MDT-object with dangling LOV EA reference.
3254 * we need to repair the inconsistency according to the users' requirement:
3256 * 1) Keep the inconsistency there and report the inconsistency case,
3257 * then give the chance to the application to find related issues,
3258 * and the users can make the decision about how to handle it with
3259 * more human knownledge. (by default)
3261 * 2) Re-create the missing OST-object with the FID/owner information.
3263 * \param[in] env pointer to the thread context
3264 * \param[in] com the layout LFSCK component
3265 * \param[in] parent the MDT-object with dangling LOV EA reference
3266 * \param[in] child the OST-object to be created
3267 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3268 * \param[in] ea_off the offset of the OST-object in the LOV EA
3269 * \param[in] ost_idx the index of OST on which the OST-object resides
3271 * \retval +1 for repair successfully
3272 * \retval 0 for did nothing
3273 * \retval negative error number on failure
3275 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3276 struct lfsck_component *com,
3277 struct dt_object *parent,
3278 struct dt_object *child,
3279 __u32 comp_id, __u32 ea_off,
3280 __u32 ost_idx, bool log)
3282 struct lfsck_thread_info *info = lfsck_env_info(env);
3283 struct filter_fid *ff = &info->lti_ff;
3284 struct ost_layout *ol = &ff->ff_layout;
3285 struct dt_object_format *dof = &info->lti_dof;
3286 struct lu_attr *la = &info->lti_la;
3287 struct lfsck_instance *lfsck = com->lc_lfsck;
3288 struct dt_device *dev = lfsck_obj2dev(child);
3289 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3290 const struct lu_fid *cfid = lfsck_dto2fid(child);
3291 struct lu_buf *tbuf = &info->lti_big_buf;
3292 struct thandle *handle;
3294 struct lustre_handle lh = { 0 };
3298 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3301 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3302 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3307 rc = dt_attr_get(env, parent, la);
3311 la->la_mode = S_IFREG | 0666;
3312 la->la_atime = la->la_mtime = la->la_ctime = 0;
3313 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3314 LA_ATIME | LA_MTIME | LA_CTIME;
3315 memset(dof, 0, sizeof(*dof));
3316 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3317 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3318 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3319 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3320 * parent MDT-object's layout EA. */
3321 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3323 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3327 rc = lfsck_lmm2layout(tbuf->lb_buf, ol, comp_id);
3331 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3332 handle = dt_trans_create(env, dev);
3334 GOTO(unlock1, rc = PTR_ERR(handle));
3336 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3340 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3341 LU_XATTR_CREATE, handle);
3345 rc = dt_trans_start_local(env, dev, handle);
3349 dt_read_lock(env, parent, 0);
3350 if (unlikely(lfsck_is_dead_obj(parent)))
3351 GOTO(unlock2, rc = 0);
3353 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3354 struct ost_id *oi = &info->lti_oi;
3355 struct lu_fid *tfid = &info->lti_fid2;
3356 struct lu_buf *lovea = &info->lti_big_buf;
3357 struct lov_mds_md_v1 *lmm;
3358 struct lov_ost_data_v1 *objs;
3363 rc = lfsck_layout_get_lovea(env, parent, lovea);
3367 lmm = lovea->lb_buf;
3368 magic = le32_to_cpu(lmm->lmm_magic);
3369 if (magic == LOV_MAGIC_COMP_V1) {
3370 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3371 struct lov_comp_md_entry_v1 *lcme;
3372 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3375 for (i = 0; i < count; i++) {
3376 lcme = &lcm->lcm_entries[i];
3377 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3378 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3381 lmm = lovea->lb_buf +
3382 le32_to_cpu(lcme->lcme_offset);
3383 magic = le32_to_cpu(lmm->lmm_magic);
3388 /* Someone removed the component, do nothing. */
3389 GOTO(unlock2, rc = 0);
3393 count = le16_to_cpu(lmm->lmm_stripe_count);
3394 /* Someone changed the LOV EA, do nothing. */
3395 if (count <= ea_off)
3396 GOTO(unlock2, rc = 0);
3398 if (magic == LOV_MAGIC_V1) {
3399 objs = &lmm->lmm_objects[ea_off];
3401 LASSERT(magic == LOV_MAGIC_V3);
3403 objs = &((struct lov_mds_md_v3 *)lmm)->\
3404 lmm_objects[ea_off];
3407 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3408 idx2 = le32_to_cpu(objs->l_ost_idx);
3409 rc = ostid_to_fid(tfid, oi, idx2);
3410 /* Someone changed the LOV EA, do nothing. */
3411 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3415 rc = dt_create(env, child, la, NULL, dof, handle);
3419 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3425 dt_read_unlock(env, parent);
3428 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3431 lfsck_ibits_unlock(&lh, LCK_EX);
3435 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3436 "dangling reference for: parent "DFID", child "
3437 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3439 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3440 comp_id, ea_off, ost_idx,
3441 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3442 "Create the lost OST-object as required" :
3443 "Keep the MDT-object there by default", rc);
3449 * Repair the MDT-object with dangling LOV EA reference.
3451 * Prepare parameters and call __lfsck_layout_repair_dangling()
3452 * to repair the dangling LOV EA reference.
3454 * \param[in] env pointer to the thread context
3455 * \param[in] com the layout LFSCK component
3456 * \param[in] pfid the MDT-object's FID
3457 * \param[in] cfid the FID for the OST-object to be created
3458 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3459 * \param[in] ea_off the offset of the OST-object in the LOV EA
3460 * \param[in] ost_idx the index of OST on which the OST-object resides
3462 * \retval +1 for repair successfully
3463 * \retval 0 for did nothing
3464 * \retval negative error number on failure
3466 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3467 struct lfsck_component *com,
3468 const struct lu_fid *pfid,
3469 const struct lu_fid *cfid,
3470 __u32 comp_id, __u32 ea_off,
3473 struct lfsck_instance *lfsck = com->lc_lfsck;
3474 struct dt_object *parent = NULL;
3475 struct dt_object *child = NULL;
3476 struct lfsck_tgt_desc *ltd;
3480 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3482 GOTO(log, rc = PTR_ERR(parent));
3484 /* The MDT-object has been removed. */
3485 if (dt_object_exists(parent) == 0)
3488 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3489 if (unlikely(ltd == NULL))
3490 GOTO(log, rc = -ENODEV);
3492 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3494 GOTO(log, rc = PTR_ERR(child));
3496 /* The OST-object has been created. */
3497 if (unlikely(dt_object_exists(child) != 0))
3500 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3501 comp_id, ea_off, ost_idx, false);
3506 if (child != NULL && !IS_ERR(child))
3507 lfsck_object_put(env, child);
3509 if (parent != NULL && !IS_ERR(parent))
3510 lfsck_object_put(env, parent);
3513 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3514 "dangling reference for: parent "DFID", child "
3515 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3516 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3517 comp_id, ea_off, ost_idx,
3518 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3519 "Create the lost OST-object as required" :
3520 "Keep the MDT-object there by default", rc);
3525 /* If the OST-object does not recognize the MDT-object as its parent, and
3526 * there is no other MDT-object claims as its parent, then just trust the
3527 * given MDT-object as its parent. So update the OST-object filter_fid. */
3528 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3529 struct lfsck_component *com,
3530 struct dt_object *parent,
3531 struct lfsck_layout_req *llr,
3534 struct lfsck_thread_info *info = lfsck_env_info(env);
3535 struct filter_fid *ff = &info->lti_ff;
3536 struct ost_layout *ol = &ff->ff_layout;
3537 struct dt_object *child = llr->llr_child;
3538 struct dt_device *dev = lfsck_obj2dev(child);
3539 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3540 struct lu_buf *tbuf = &info->lti_big_buf;
3541 struct thandle *handle;
3543 struct lustre_handle lh = { 0 };
3547 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3548 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3553 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3554 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3555 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3556 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3557 * parent MDT-object's layout EA. */
3558 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3560 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3564 rc = lfsck_lmm2layout(tbuf->lb_buf, ol, llr->llr_comp_id);
3568 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3570 handle = dt_trans_create(env, dev);
3572 GOTO(unlock1, rc = PTR_ERR(handle));
3574 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3578 rc = dt_attr_get(env, parent, la);
3582 la->la_valid = LA_UID | LA_GID;
3583 rc = dt_declare_attr_set(env, child, la, handle);
3587 rc = dt_trans_start_local(env, dev, handle);
3591 dt_write_lock(env, parent, 0);
3592 if (unlikely(lfsck_is_dead_obj(parent)))
3593 GOTO(unlock2, rc = 1);
3595 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3599 /* Get the latest parent's owner. */
3600 rc = dt_attr_get(env, parent, la);
3604 la->la_valid = LA_UID | LA_GID;
3605 rc = dt_attr_set(env, child, la, handle);
3610 dt_write_unlock(env, parent);
3613 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3616 lfsck_ibits_unlock(&lh, LCK_EX);
3620 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3621 "unmatched MDT-OST pair for: parent "DFID
3622 ", child "DFID", comp_id %u, OST-index %u, "
3623 "stripe-index %u, owner %u/%u: rc = %d\n",
3624 lfsck_lfsck2name(com->lc_lfsck),
3625 PFID(lfsck_dto2fid(parent)),
3626 PFID(lfsck_dto2fid(child)),
3627 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3628 la->la_uid, la->la_gid, rc);
3633 /* If there are more than one MDT-objects claim as the OST-object's parent,
3634 * and the OST-object only recognizes one of them, then we need to generate
3635 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3636 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3637 struct lfsck_component *com,
3638 struct dt_object *parent,
3639 struct lfsck_layout_req *llr,
3642 struct lfsck_thread_info *info = lfsck_env_info(env);
3643 struct dt_allocation_hint *hint = &info->lti_hint;
3644 struct dt_object_format *dof = &info->lti_dof;
3645 struct ost_id *oi = &info->lti_oi;
3646 struct lu_buf *buf = &info->lti_big_buf;
3647 struct lfsck_instance *lfsck = com->lc_lfsck;
3648 struct dt_device *dev;
3649 struct lu_device *d =
3650 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3651 struct lu_object *o;
3652 struct lu_object *n;
3653 struct dt_object *child = NULL;
3654 struct thandle *handle = NULL;
3655 struct lov_mds_md_v1 *lmm;
3656 struct lov_ost_data_v1 *objs;
3657 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3659 struct lustre_handle lh = { 0 };
3665 /* We use two separated transactions to repair the inconsistency.
3667 * 1) create the child (OST-object).
3668 * 2) update the parent LOV EA according to the child's FID.
3670 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3671 * handled as orphan when the layout LFSCK run next time.
3673 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3674 * as dangling referened case when the layout LFSCK run next time. */
3676 /* The 1st transaction. */
3677 o = lu_object_anon(env, d, NULL);
3679 GOTO(log, rc = PTR_ERR(o));
3681 n = lu_object_locate(o->lo_header, d->ld_type);
3682 if (unlikely(n == NULL)) {
3683 lu_object_put_nocache(env, o);
3685 GOTO(log, rc = -EINVAL);
3688 child = container_of(n, struct dt_object, do_lu);
3689 memset(hint, 0, sizeof(*hint));
3690 rc = dt_attr_get(env, parent, la);
3694 la->la_valid = LA_UID | LA_GID;
3695 memset(dof, 0, sizeof(*dof));
3697 dev = lfsck_obj2dev(child);
3698 handle = dt_trans_create(env, dev);
3700 GOTO(log, rc = PTR_ERR(handle));
3702 rc = dt_declare_create(env, child, la, hint, dof, handle);
3706 rc = dt_trans_start_local(env, dev, handle);
3710 rc = dt_create(env, child, la, hint, dof, handle);
3711 dt_trans_stop(env, dev, handle);
3716 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3717 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3722 /* The 2nd transaction. */
3724 /* XXX: Generally, we should use bottom device (OSD) to update parent
3725 * LOV EA. But because the LOD-object still references the wrong
3726 * OSP-object that should be detached after the parent's LOV EA
3727 * refreshed. Unfortunately, there is no suitable API for that.
3728 * So we have to make the LOD to re-load the OSP-object(s) via
3729 * replacing the LOV EA against the LOD-object.
3731 * Once the DNE2 patches have been landed, we can replace the
3732 * LOD device with the OSD device. LU-6230. */
3734 dev = lfsck->li_next;
3735 parent = lfsck_object_locate(dev, parent);
3737 GOTO(log, rc = PTR_ERR(parent));
3739 handle = dt_trans_create(env, dev);
3741 GOTO(log, rc = PTR_ERR(handle));
3743 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3744 LU_XATTR_REPLACE, handle);
3748 rc = dt_trans_start_local(env, dev, handle);
3752 dt_write_lock(env, parent, 0);
3753 if (unlikely(lfsck_is_dead_obj(parent)))
3754 GOTO(unlock, rc = 0);
3756 rc = lfsck_layout_get_lovea(env, parent, buf);
3757 if (unlikely(!rc || rc == -ENODATA))
3758 GOTO(unlock, rc = 0);
3761 magic = le32_to_cpu(lmm->lmm_magic);
3762 if (magic == LOV_MAGIC_COMP_V1) {
3763 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3764 struct lov_comp_md_entry_v1 *lcme;
3765 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3768 LASSERT(llr->llr_comp_id != 0);
3770 for (i = 0; i < count; i++) {
3771 lcme = &lcm->lcm_entries[i];
3772 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3773 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3776 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3778 le32_to_cpu(lcme->lcme_offset);
3779 magic = le32_to_cpu(lmm->lmm_magic);
3784 GOTO(unlock, rc = 0);
3788 if (magic == LOV_MAGIC_V1) {
3789 objs = &lmm->lmm_objects[llr->llr_lov_idx];
3791 LASSERT(magic == LOV_MAGIC_V3);
3793 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3796 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3797 index = le32_to_cpu(objs->l_ost_idx);
3798 rc = ostid_to_fid(&tfid, oi, index);
3799 /* Someone changed layout during the LFSCK, no need to repair then. */
3800 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3801 GOTO(unlock, rc = 0);
3803 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3804 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3805 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3806 objs->l_ost_gen = cpu_to_le32(0);
3807 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3808 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3809 LU_XATTR_REPLACE, handle);
3811 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3814 dt_write_unlock(env, parent);
3818 dt_trans_stop(env, dev, handle);
3821 lfsck_ibits_unlock(&lh, LCK_EX);
3823 lfsck_object_put(env, child);
3826 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3827 "multiple references for: parent "DFID", comp_id %u, "
3828 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
3829 lfsck_lfsck2name(lfsck), PFID(pfid),
3830 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3831 la->la_uid, la->la_gid, rc);
3836 /* If the MDT-object and the OST-object have different owner information,
3837 * then trust the MDT-object, because the normal chown/chgrp handle order
3838 * is from MDT to OST, and it is possible that some chown/chgrp operation
3839 * is partly done. */
3840 static int lfsck_layout_repair_owner(const struct lu_env *env,
3841 struct lfsck_component *com,
3842 struct dt_object *parent,
3843 struct lfsck_layout_req *llr,
3844 struct lu_attr *pla,
3845 const struct lu_attr *cla)
3847 struct lfsck_thread_info *info = lfsck_env_info(env);
3848 struct lu_attr *tla = &info->lti_la2;
3849 struct dt_object *child = llr->llr_child;
3850 struct dt_device *dev = lfsck_obj2dev(child);
3851 struct thandle *handle;
3855 tla->la_uid = pla->la_uid;
3856 tla->la_gid = pla->la_gid;
3857 tla->la_valid = LA_UID | LA_GID;
3858 handle = dt_trans_create(env, dev);
3860 GOTO(log, rc = PTR_ERR(handle));
3862 rc = dt_declare_attr_set(env, child, tla, handle);
3866 rc = dt_trans_start_local(env, dev, handle);
3870 /* Use the dt_object lock to serialize with destroy and attr_set. */
3871 dt_read_lock(env, parent, 0);
3872 if (unlikely(lfsck_is_dead_obj(parent)))
3873 GOTO(unlock, rc = 1);
3875 /* Get the latest parent's owner. */
3876 rc = dt_attr_get(env, parent, pla);
3880 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3881 if (unlikely(tla->la_uid != pla->la_uid ||
3882 tla->la_gid != pla->la_gid))
3885 rc = dt_attr_set(env, child, tla, handle);
3890 dt_read_unlock(env, parent);
3893 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3897 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3898 "inconsistent file owner for: parent "DFID", child "DFID
3899 ", OST-index %u, stripe-index %u, old owner %u/%u, "
3900 "new owner %u/%u: rc = %d\n",
3901 lfsck_lfsck2name(com->lc_lfsck),
3902 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
3903 llr->llr_ost_idx, llr->llr_lov_idx,
3904 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
3909 /* Check whether the OST-object correctly back points to the
3910 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3911 static int lfsck_layout_check_parent(const struct lu_env *env,
3912 struct lfsck_component *com,
3913 struct lfsck_assistant_object *lso,
3914 struct filter_fid *ff,
3915 const struct lu_fid *cfid,
3916 const struct lu_attr *cla,
3917 struct lfsck_layout_req *llr)
3919 struct lfsck_thread_info *info = lfsck_env_info(env);
3920 struct lu_buf *buf = &info->lti_big_buf;
3921 struct lu_fid *pfid = &info->lti_fid;
3922 struct dt_object *tobj;
3923 struct lov_mds_md_v1 *lmm;
3924 struct lov_ost_data_v1 *objs;
3925 struct lustre_handle lh = { 0 };
3933 *pfid = ff->ff_parent;
3934 idx = pfid->f_stripe_idx;
3937 if (unlikely(!fid_is_sane(pfid)))
3938 RETURN(LLIT_UNMATCHED_PAIR);
3940 if (lu_fid_eq(pfid, &lso->lso_fid)) {
3941 if (likely(llr->llr_lov_idx == idx))
3944 RETURN(LLIT_UNMATCHED_PAIR);
3947 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3949 RETURN(PTR_ERR(tobj));
3951 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
3952 !S_ISREG(lfsck_object_type(tobj)))
3953 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3955 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3956 * remote one on another MDT. Then check whether the given OST-object
3957 * is in such layout. If yes, it is multiple referenced, otherwise it
3958 * is unmatched referenced case. */
3959 rc = lfsck_layout_get_lovea(env, tobj, buf);
3960 if (rc == 0 || rc == -ENOENT)
3961 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3967 magic = le32_to_cpu(lmm->lmm_magic);
3968 if (magic == LOV_MAGIC_COMP_V1) {
3969 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3970 struct lov_comp_md_entry_v1 *lcme;
3972 if (ff->ff_layout.ol_comp_id == 0)
3973 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3975 count = le16_to_cpu(lcm->lcm_entry_count);
3976 for (i = 0; i < count; i++) {
3977 lcme = &lcm->lcm_entries[i];
3978 if (le32_to_cpu(lcme->lcme_id) ==
3979 ff->ff_layout.ol_comp_id) {
3981 le32_to_cpu(lcme->lcme_offset);
3982 magic = le32_to_cpu(lmm->lmm_magic);
3983 if (!(le32_to_cpu(lcme->lcme_flags) &
3985 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3991 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3995 if (magic == LOV_MAGIC_V1) {
3996 objs = &lmm->lmm_objects[0];
3998 LASSERT(magic == LOV_MAGIC_V3);
3999 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4002 count = le16_to_cpu(lmm->lmm_stripe_count);
4003 for (i = 0; i < count; i++, objs++) {
4004 struct lu_fid *tfid = &info->lti_fid2;
4005 struct ost_id *oi = &info->lti_oi;
4008 if (lovea_slot_is_dummy(objs))
4011 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4012 idx2 = le32_to_cpu(objs->l_ost_idx);
4013 rc = ostid_to_fid(tfid, oi, idx2);
4015 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4016 "invalid layout EA at the slot %d, index %u\n",
4017 lfsck_lfsck2name(com->lc_lfsck),
4018 PFID(pfid), i, idx2);
4020 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4023 if (lu_fid_eq(cfid, tfid)) {
4024 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4025 MDS_INODELOCK_UPDATE |
4026 MDS_INODELOCK_LAYOUT |
4027 MDS_INODELOCK_XATTR,
4032 dt_read_lock(env, tobj, 0);
4034 /* For local MDT-object, re-check existence
4035 * after taken the lock. */
4036 if (!dt_object_remote(tobj)) {
4037 if (dt_object_exists(tobj) == 0 ||
4038 lfsck_is_dead_obj(tobj))
4039 rc = LLIT_UNMATCHED_PAIR;
4041 rc = LLIT_MULTIPLE_REFERENCED;
4046 /* For migration case, the new MDT-object and old
4047 * MDT-object may reference the same OST-object at
4048 * some migration internal time.
4050 * For remote MDT-object, the local MDT may not know
4051 * whether it has been removed or not. Try checking
4052 * for a non-existent xattr to check if this object
4053 * has been been removed or not. */
4054 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4056 if (unlikely(rc == -ENOENT || rc >= 0))
4057 rc = LLIT_UNMATCHED_PAIR;
4058 else if (rc == -ENODATA)
4059 rc = LLIT_MULTIPLE_REFERENCED;
4065 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4068 if (lustre_handle_is_used(&lh)) {
4069 dt_read_unlock(env, tobj);
4070 lfsck_ibits_unlock(&lh, LCK_EX);
4074 lfsck_object_put(env, tobj);
4079 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4080 struct lfsck_component *com,
4081 struct lfsck_assistant_req *lar)
4083 struct lfsck_layout_req *llr =
4084 container_of0(lar, struct lfsck_layout_req, llr_lar);
4085 struct lfsck_assistant_object *lso = lar->lar_parent;
4086 struct lfsck_layout *lo = com->lc_file_ram;
4087 struct lfsck_thread_info *info = lfsck_env_info(env);
4088 struct filter_fid *ff = &info->lti_ff;
4089 struct lu_buf buf = { .lb_buf = ff,
4090 .lb_len = sizeof(*ff) };
4091 struct dt_object *parent = NULL;
4092 struct dt_object *child = llr->llr_child;
4093 struct lu_attr *pla = &lso->lso_attr;
4094 struct lu_attr *cla = &info->lti_la;
4095 struct lfsck_instance *lfsck = com->lc_lfsck;
4096 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4097 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4104 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ASSISTANT_DIRECT, cfs_fail_val);
4106 rc = dt_attr_get(env, child, cla);
4107 if (rc == -ENOENT) {
4108 parent = lfsck_assistant_object_load(env, lfsck, lso);
4109 if (IS_ERR(parent)) {
4110 rc = PTR_ERR(parent);
4112 RETURN(rc == -ENOENT ? 0 : rc);
4115 type = LLIT_DANGLING;
4122 lfsck_buf_init(&buf, ff, sizeof(*ff));
4123 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4124 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4125 type = LLIT_UNMATCHED_PAIR;
4129 if (rc < 0 && rc != -ENODATA)
4132 if (rc == 0 || rc == -ENODATA)
4133 GOTO(check_owner, rc = 0);
4135 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4136 rc = lfsck_layout_check_parent(env, com, lso, ff,
4137 lu_object_fid(&child->do_lu), cla, llr);
4147 /* Someone may has changed the owner after the parent attr pre-loaded.
4148 * It can be handled later inside the lfsck_layout_repair_owner(). */
4149 if (unlikely(cla->la_uid != pla->la_uid ||
4150 cla->la_gid != pla->la_gid)) {
4151 type = LLIT_INCONSISTENT_OWNER;
4156 if (type == LLIT_NONE)
4159 if (bk->lb_param & LPF_DRYRUN)
4162 if (parent == NULL) {
4163 parent = lfsck_assistant_object_load(env, lfsck, lso);
4164 if (IS_ERR(parent)) {
4165 rc = PTR_ERR(parent);
4176 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4177 rc = lfsck_layout_ins_dangling_rec(env, com,
4178 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4179 llr->llr_comp_id, llr->llr_lov_idx,
4182 rc = __lfsck_layout_repair_dangling(env, com, parent,
4189 case LLIT_UNMATCHED_PAIR:
4190 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4193 case LLIT_MULTIPLE_REFERENCED:
4194 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4197 case LLIT_INCONSISTENT_OWNER:
4198 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4208 down_write(&com->lc_sem);
4210 struct lfsck_assistant_data *lad = com->lc_data;
4212 if (unlikely(lad->lad_exit)) {
4214 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4215 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4216 rc == -EHOSTUNREACH) {
4217 /* If cannot touch the target server,
4218 * mark the LFSCK as INCOMPLETE. */
4219 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4220 "talk with OST %x: rc = %d\n",
4221 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4222 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4223 lo->ll_objs_skipped++;
4226 lfsck_layout_record_failure(env, lfsck, lo);
4228 } else if (rc > 0 && (type != LLIT_DANGLING ||
4229 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4230 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4231 "unknown type = %d\n", type);
4233 lo->ll_objs_repaired[type - 1]++;
4234 if (bk->lb_param & LPF_DRYRUN &&
4235 unlikely(lo->ll_pos_first_inconsistent == 0))
4236 lo->ll_pos_first_inconsistent =
4237 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4240 up_write(&com->lc_sem);
4242 if (parent != NULL && !IS_ERR(parent))
4243 lfsck_object_put(env, parent);
4249 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4250 struct lfsck_component *com,
4251 struct dt_object *obj, bool first)
4253 struct lfsck_instance *lfsck = com->lc_lfsck;
4254 struct ptlrpc_thread *thread = &lfsck->li_thread;
4255 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4256 struct lfsck_layout *lo = com->lc_file_ram;
4257 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4260 struct lfsck_layout_dangling_key *parent =
4261 &lfsck_env_info(env)->lti_lldk;
4262 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4267 di = iops->init(env, obj, 0);
4269 RETURN(PTR_ERR(di));
4272 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4274 memset(parent, 0, sizeof(*parent));
4275 rc = iops->get(env, di, (const struct dt_key *)parent);
4280 /* The start one either has been processed or does not exist,
4282 rc = iops->next(env, di);
4288 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4289 unlikely(!thread_is_running(thread)))
4292 key = iops->key(env, di);
4301 lldk_be_to_cpu(parent,
4302 (const struct lfsck_layout_dangling_key *)key);
4303 if (!fid_is_sane(&parent->lldk_fid)) {
4308 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4310 fid_be_to_cpu(cfid, cfid);
4311 ost_idx = cfid->f_ver;
4313 if (!fid_is_sane(cfid)) {
4318 rc = lfsck_layout_repair_dangling(env, com,
4319 &parent->lldk_fid, cfid,
4320 parent->lldk_comp_id,
4321 parent->lldk_ea_off, ost_idx);
4325 down_write(&com->lc_sem);
4326 com->lc_new_checked++;
4327 com->lc_new_scanned++;
4329 lo->ll_lldk_latest_scanned_phase2 = *parent;
4332 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4334 lo->ll_objs_failed_phase2++;
4335 up_write(&com->lc_sem);
4337 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4340 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
4341 cfs_time_current())) &&
4342 com->lc_new_checked != 0) {
4343 down_write(&com->lc_sem);
4344 lo->ll_run_time_phase2 +=
4345 cfs_duration_sec(cfs_time_current() +
4346 HALF_SEC - com->lc_time_last_checkpoint);
4347 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4348 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4349 com->lc_new_checked = 0;
4350 lfsck_layout_store(env, com);
4351 up_write(&com->lc_sem);
4353 com->lc_time_last_checkpoint = cfs_time_current();
4354 com->lc_time_next_checkpoint =
4355 com->lc_time_last_checkpoint +
4356 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4359 lfsck_control_speed_by_self(com);
4360 if (unlikely(!thread_is_running(thread)))
4363 rc = iops->next(env, di);
4372 iops->fini(env, di);
4377 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4378 struct lfsck_component *com)
4380 struct lfsck_assistant_data *lad = com->lc_data;
4381 struct lfsck_instance *lfsck = com->lc_lfsck;
4382 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4383 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4384 struct lfsck_tgt_desc *ltd;
4388 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4389 lfsck_lfsck2name(lfsck));
4391 spin_lock(<ds->ltd_lock);
4392 while (!list_empty(&lad->lad_ost_phase2_list)) {
4393 ltd = list_entry(lad->lad_ost_phase2_list.next,
4394 struct lfsck_tgt_desc,
4395 ltd_layout_phase_list);
4396 list_del_init(<d->ltd_layout_phase_list);
4397 if (bk->lb_param & LPF_OST_ORPHAN) {
4398 spin_unlock(<ds->ltd_lock);
4399 rc = lfsck_layout_scan_orphan(env, com, ltd);
4400 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4403 if (unlikely(lad->lad_exit ||
4404 !thread_is_running(&lfsck->li_thread)))
4406 spin_lock(<ds->ltd_lock);
4410 if (list_empty(&lad->lad_ost_phase1_list))
4414 spin_unlock(<ds->ltd_lock);
4416 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4417 struct lfsck_layout *lo = com->lc_file_ram;
4420 com->lc_new_checked = 0;
4421 com->lc_new_scanned = 0;
4422 com->lc_time_last_checkpoint = cfs_time_current();
4423 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4424 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4426 i = lfsck_sub_trace_file_fid2idx(
4427 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4428 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4429 com->lc_sub_trace_objs[i].lsto_obj, true);
4430 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4431 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4432 com->lc_sub_trace_objs[i].lsto_obj, false);
4434 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4435 "at the No. %d trace file: rc = %d\n",
4436 lfsck_lfsck2name(lfsck), i, rc);
4439 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4440 lfsck_lfsck2name(lfsck), rc);
4446 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4447 struct ptlrpc_request *req,
4450 struct lfsck_layout_slave_async_args *llsaa = args;
4451 struct obd_export *exp = llsaa->llsaa_exp;
4452 struct lfsck_component *com = llsaa->llsaa_com;
4453 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4454 struct lfsck_layout_slave_data *llsd = com->lc_data;
4455 struct lfsck_reply *lr = NULL;
4459 /* It is probably caused by network trouble, or target crash,
4460 * it will try several times (depends on the obd_timeout, and
4461 * will not less than 3 times). But to make the LFSCK can go
4462 * ahead, we should not try for ever. After some try but still
4463 * hit failure, it will assume that the target exit the LFSCK
4464 * prcoessing and stop try. */
4465 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4466 int max_try = max_t(int, obd_timeout / 30, 3);
4468 if (++(llst->llst_failures) > max_try)
4474 llst->llst_failures = 0;
4475 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4476 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4477 lr->lr_status != LS_SCANNING_PHASE2)
4482 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4483 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4484 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4485 llst->llst_failures);
4487 lfsck_layout_llst_del(llsd, llst);
4490 lfsck_layout_llst_put(llst);
4491 lfsck_component_put(env, com);
4492 class_export_put(exp);
4497 static int lfsck_layout_async_query(const struct lu_env *env,
4498 struct lfsck_component *com,
4499 struct obd_export *exp,
4500 struct lfsck_layout_slave_target *llst,
4501 struct lfsck_request *lr,
4502 struct ptlrpc_request_set *set)
4504 struct lfsck_layout_slave_async_args *llsaa;
4505 struct ptlrpc_request *req;
4506 struct lfsck_request *tmp;
4510 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4514 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4516 ptlrpc_request_free(req);
4520 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4522 ptlrpc_request_set_replen(req);
4524 llsaa = ptlrpc_req_async_args(req);
4525 llsaa->llsaa_exp = exp;
4526 llsaa->llsaa_com = lfsck_component_get(com);
4527 llsaa->llsaa_llst = llst;
4528 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4529 req->rq_allow_intr = 1;
4530 ptlrpc_set_add_req(set, req);
4535 static int lfsck_layout_async_notify(const struct lu_env *env,
4536 struct obd_export *exp,
4537 struct lfsck_request *lr,
4538 struct ptlrpc_request_set *set)
4540 struct ptlrpc_request *req;
4541 struct lfsck_request *tmp;
4545 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4549 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4551 ptlrpc_request_free(req);
4555 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4557 ptlrpc_request_set_replen(req);
4558 req->rq_allow_intr = 1;
4559 ptlrpc_set_add_req(set, req);
4565 lfsck_layout_slave_query_master(const struct lu_env *env,
4566 struct lfsck_component *com)
4568 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4569 struct lfsck_instance *lfsck = com->lc_lfsck;
4570 struct lfsck_layout_slave_data *llsd = com->lc_data;
4571 struct lfsck_layout_slave_target *llst;
4572 struct obd_export *exp;
4573 struct ptlrpc_request_set *set;
4578 set = ptlrpc_prep_set();
4580 GOTO(log, rc = -ENOMEM);
4582 memset(lr, 0, sizeof(*lr));
4583 lr->lr_event = LE_QUERY;
4584 lr->lr_active = LFSCK_TYPE_LAYOUT;
4586 llsd->llsd_touch_gen++;
4587 spin_lock(&llsd->llsd_lock);
4588 while (!list_empty(&llsd->llsd_master_list)) {
4589 llst = list_entry(llsd->llsd_master_list.next,
4590 struct lfsck_layout_slave_target,
4592 if (llst->llst_gen == llsd->llsd_touch_gen)
4595 llst->llst_gen = llsd->llsd_touch_gen;
4596 list_move_tail(&llst->llst_list,
4597 &llsd->llsd_master_list);
4598 atomic_inc(&llst->llst_ref);
4599 spin_unlock(&llsd->llsd_lock);
4601 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4604 lfsck_layout_llst_del(llsd, llst);
4605 lfsck_layout_llst_put(llst);
4606 spin_lock(&llsd->llsd_lock);
4610 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4612 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4613 "query %s for layout: rc = %d\n",
4614 lfsck_lfsck2name(lfsck),
4615 exp->exp_obd->obd_name, rc);
4618 lfsck_layout_llst_put(llst);
4619 class_export_put(exp);
4621 spin_lock(&llsd->llsd_lock);
4623 spin_unlock(&llsd->llsd_lock);
4625 rc = ptlrpc_set_wait(set);
4626 ptlrpc_set_destroy(set);
4628 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
4631 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
4632 lfsck_lfsck2name(com->lc_lfsck), rc);
4638 lfsck_layout_slave_notify_master(const struct lu_env *env,
4639 struct lfsck_component *com,
4640 enum lfsck_events event, int result)
4642 struct lfsck_layout *lo = com->lc_file_ram;
4643 struct lfsck_instance *lfsck = com->lc_lfsck;
4644 struct lfsck_layout_slave_data *llsd = com->lc_data;
4645 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4646 struct lfsck_layout_slave_target *llst;
4647 struct obd_export *exp;
4648 struct ptlrpc_request_set *set;
4652 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
4653 lfsck_lfsck2name(com->lc_lfsck));
4655 set = ptlrpc_prep_set();
4659 memset(lr, 0, sizeof(*lr));
4660 lr->lr_event = event;
4661 lr->lr_flags = LEF_FROM_OST;
4662 lr->lr_status = result;
4663 lr->lr_index = lfsck_dev_idx(lfsck);
4664 lr->lr_active = LFSCK_TYPE_LAYOUT;
4665 lr->lr_flags2 = lo->ll_flags;
4666 llsd->llsd_touch_gen++;
4667 spin_lock(&llsd->llsd_lock);
4668 while (!list_empty(&llsd->llsd_master_list)) {
4669 llst = list_entry(llsd->llsd_master_list.next,
4670 struct lfsck_layout_slave_target,
4672 if (llst->llst_gen == llsd->llsd_touch_gen)
4675 llst->llst_gen = llsd->llsd_touch_gen;
4676 list_move_tail(&llst->llst_list,
4677 &llsd->llsd_master_list);
4678 atomic_inc(&llst->llst_ref);
4679 spin_unlock(&llsd->llsd_lock);
4681 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4684 lfsck_layout_llst_del(llsd, llst);
4685 lfsck_layout_llst_put(llst);
4686 spin_lock(&llsd->llsd_lock);
4690 rc = lfsck_layout_async_notify(env, exp, lr, set);
4692 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4693 "notify %s for layout: rc = %d\n",
4694 lfsck_lfsck2name(lfsck),
4695 exp->exp_obd->obd_name, rc);
4697 lfsck_layout_llst_put(llst);
4698 class_export_put(exp);
4699 spin_lock(&llsd->llsd_lock);
4701 spin_unlock(&llsd->llsd_lock);
4703 ptlrpc_set_wait(set);
4704 ptlrpc_set_destroy(set);
4710 * \ret -ENODATA: unrecognized stripe
4711 * \ret = 0 : recognized stripe
4712 * \ret < 0 : other failures
4714 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
4715 struct lfsck_component *com,
4716 struct lu_fid *cfid,
4717 struct lu_fid *pfid, __u32 comp_id)
4719 struct lfsck_thread_info *info = lfsck_env_info(env);
4720 struct lu_buf *buf = &info->lti_big_buf;
4721 struct ost_id *oi = &info->lti_oi;
4722 struct dt_object *obj;
4723 struct lov_mds_md_v1 *lmm;
4724 struct lov_ost_data_v1 *objs;
4725 __u32 idx = pfid->f_stripe_idx;
4733 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4735 RETURN(PTR_ERR(obj));
4737 dt_read_lock(env, obj, 0);
4738 if (unlikely(dt_object_exists(obj) == 0 ||
4739 lfsck_is_dead_obj(obj)))
4740 GOTO(unlock, rc = -ENOENT);
4742 if (!S_ISREG(lfsck_object_type(obj)))
4743 GOTO(unlock, rc = -ENODATA);
4745 rc = lfsck_layout_get_lovea(env, obj, buf);
4750 GOTO(unlock, rc = -ENODATA);
4753 magic = le32_to_cpu(lmm->lmm_magic);
4754 if (magic == LOV_MAGIC_COMP_V1) {
4755 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4756 struct lov_comp_md_entry_v1 *lcme;
4759 GOTO(unlock, rc = -ENODATA);
4761 count = le16_to_cpu(lcm->lcm_entry_count);
4762 for (i = 0; i < count; i++) {
4763 lcme = &lcm->lcm_entries[i];
4764 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
4766 le32_to_cpu(lcme->lcme_offset);
4767 magic = le32_to_cpu(lmm->lmm_magic);
4768 if (!(le32_to_cpu(lcme->lcme_flags) &
4770 GOTO(unlock, rc = -ENODATA);
4776 GOTO(unlock, rc = -ENODATA);
4780 if (magic == LOV_MAGIC_V1) {
4781 objs = &lmm->lmm_objects[0];
4783 LASSERT(magic == LOV_MAGIC_V3);
4784 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4787 fid_to_ostid(cfid, oi);
4788 count = le16_to_cpu(lmm->lmm_stripe_count);
4789 for (i = 0; i < count; i++, objs++) {
4792 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
4793 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
4794 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
4797 GOTO(unlock, rc = -ENODATA);
4800 dt_read_unlock(env, obj);
4801 lfsck_object_put(env, obj);
4807 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
4808 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
4809 * layout EA from MDT to OST. On one hand, the OST no need to understand
4810 * the layout EA structure; on the other hand, it may cause trouble when
4811 * transfer large layout EA from MDT to OST via normal OUT RPC.
4813 * \ret > 0: unrecognized stripe
4814 * \ret = 0: recognized stripe
4815 * \ret < 0: other failures
4817 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
4818 struct lfsck_component *com,
4819 struct lu_fid *cfid,
4820 struct lu_fid *pfid, __u32 comp_id)
4822 struct lfsck_instance *lfsck = com->lc_lfsck;
4823 struct obd_device *obd = lfsck->li_obd;
4824 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4825 struct obd_export *exp = NULL;
4826 struct ptlrpc_request *req = NULL;
4827 struct lfsck_request *lr;
4828 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
4832 if (unlikely(fid_is_idif(pfid)))
4835 fld_range_set_any(range);
4836 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
4838 RETURN(rc == -ENOENT ? 1 : rc);
4840 if (unlikely(!fld_range_is_mdt(range)))
4843 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
4844 if (unlikely(exp == NULL))
4847 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
4848 GOTO(out, rc = -EOPNOTSUPP);
4850 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4852 GOTO(out, rc = -ENOMEM);
4854 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4856 ptlrpc_request_free(req);
4861 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4862 memset(lr, 0, sizeof(*lr));
4863 lr->lr_event = LE_PAIRS_VERIFY;
4864 lr->lr_active = LFSCK_TYPE_LAYOUT;
4865 lr->lr_fid = *cfid; /* OST-object itself FID. */
4866 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
4867 lr->lr_comp_id = comp_id;
4869 ptlrpc_request_set_replen(req);
4870 rc = ptlrpc_queue_wait(req);
4871 ptlrpc_req_finished(req);
4873 if (rc == -ENOENT || rc == -ENODATA)
4880 class_export_put(exp);
4885 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
4886 struct lfsck_component *com,
4887 struct lfsck_req_local *lrl)
4889 struct dt_object *obj;
4893 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
4895 GOTO(log, rc = PTR_ERR(obj));
4897 dt_write_lock(env, obj, 0);
4898 if (unlikely(dt_object_exists(obj) == 0 ||
4899 lfsck_is_dead_obj(obj)))
4900 GOTO(unlock, rc = 0);
4902 rc = __lfsck_layout_update_pfid(env, obj, &lrl->lrl_ff_client.ff_parent,
4903 &lrl->lrl_ff_client.ff_layout,
4904 lrl->lrl_ff_client.ff_parent.f_ver);
4909 dt_write_unlock(env, obj);
4910 lfsck_object_put(env, obj);
4913 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
4914 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
4915 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
4922 static void lfsck_layout_slave_quit(const struct lu_env *env,
4923 struct lfsck_component *com);
4925 static int lfsck_layout_reset(const struct lu_env *env,
4926 struct lfsck_component *com, bool init)
4928 struct lfsck_layout *lo = com->lc_file_ram;
4931 down_write(&com->lc_sem);
4933 memset(lo, 0, com->lc_file_size);
4935 __u32 count = lo->ll_success_count;
4936 __u64 last_time = lo->ll_time_last_complete;
4938 memset(lo, 0, com->lc_file_size);
4939 lo->ll_success_count = count;
4940 lo->ll_time_last_complete = last_time;
4943 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
4944 lo->ll_status = LS_INIT;
4946 if (com->lc_lfsck->li_master) {
4947 struct lfsck_assistant_data *lad = com->lc_data;
4949 lad->lad_incomplete = 0;
4950 CFS_RESET_BITMAP(lad->lad_bitmap);
4953 rc = lfsck_layout_store(env, com);
4954 if (rc == 0 && com->lc_lfsck->li_master)
4955 rc = lfsck_load_sub_trace_files(env, com,
4956 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
4957 up_write(&com->lc_sem);
4959 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
4960 lfsck_lfsck2name(com->lc_lfsck), rc);
4965 static void lfsck_layout_fail(const struct lu_env *env,
4966 struct lfsck_component *com, bool new_checked)
4968 struct lfsck_layout *lo = com->lc_file_ram;
4970 down_write(&com->lc_sem);
4972 com->lc_new_checked++;
4973 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
4974 up_write(&com->lc_sem);
4977 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
4978 struct lfsck_component *com, bool init)
4980 struct lfsck_instance *lfsck = com->lc_lfsck;
4981 struct lfsck_layout *lo = com->lc_file_ram;
4985 rc = lfsck_checkpoint_generic(env, com);
4987 return rc > 0 ? 0 : rc;
4990 down_write(&com->lc_sem);
4992 lo->ll_pos_latest_start =
4993 lfsck->li_pos_checkpoint.lp_oit_cookie;
4995 lo->ll_pos_last_checkpoint =
4996 lfsck->li_pos_checkpoint.lp_oit_cookie;
4997 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4998 HALF_SEC - lfsck->li_time_last_checkpoint);
4999 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5000 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5001 com->lc_new_checked = 0;
5004 rc = lfsck_layout_store(env, com);
5005 up_write(&com->lc_sem);
5007 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5008 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5009 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5014 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5015 struct lfsck_component *com, bool init)
5017 struct lfsck_instance *lfsck = com->lc_lfsck;
5018 struct lfsck_layout *lo = com->lc_file_ram;
5021 if (com->lc_new_checked == 0 && !init)
5024 down_write(&com->lc_sem);
5026 lo->ll_pos_latest_start =
5027 lfsck->li_pos_checkpoint.lp_oit_cookie;
5029 lo->ll_pos_last_checkpoint =
5030 lfsck->li_pos_checkpoint.lp_oit_cookie;
5031 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
5032 HALF_SEC - lfsck->li_time_last_checkpoint);
5033 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5034 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5035 com->lc_new_checked = 0;
5038 rc = lfsck_layout_store(env, com);
5039 up_write(&com->lc_sem);
5041 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5042 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5043 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5048 static int lfsck_layout_prep(const struct lu_env *env,
5049 struct lfsck_component *com,
5050 struct lfsck_start *start)
5052 struct lfsck_instance *lfsck = com->lc_lfsck;
5053 struct lfsck_layout *lo = com->lc_file_ram;
5054 struct lfsck_position *pos = &com->lc_pos_start;
5056 fid_zero(&pos->lp_dir_parent);
5057 pos->lp_dir_cookie = 0;
5058 if (lo->ll_status == LS_COMPLETED ||
5059 lo->ll_status == LS_PARTIAL ||
5060 /* To handle orphan, must scan from the beginning. */
5061 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5064 rc = lfsck_layout_reset(env, com, false);
5066 rc = lfsck_set_param(env, lfsck, start, true);
5069 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5070 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5076 down_write(&com->lc_sem);
5077 lo->ll_time_latest_start = cfs_time_current_sec();
5078 spin_lock(&lfsck->li_lock);
5079 if (lo->ll_flags & LF_SCANNED_ONCE) {
5080 if (!lfsck->li_drop_dryrun ||
5081 lo->ll_pos_first_inconsistent == 0) {
5082 lo->ll_status = LS_SCANNING_PHASE2;
5083 list_move_tail(&com->lc_link,
5084 &lfsck->li_list_double_scan);
5085 pos->lp_oit_cookie = 0;
5089 lo->ll_status = LS_SCANNING_PHASE1;
5090 lo->ll_run_time_phase1 = 0;
5091 lo->ll_run_time_phase2 = 0;
5092 lo->ll_objs_checked_phase1 = 0;
5093 lo->ll_objs_checked_phase2 = 0;
5094 lo->ll_objs_failed_phase1 = 0;
5095 lo->ll_objs_failed_phase2 = 0;
5096 for (i = 0; i < LLIT_MAX; i++)
5097 lo->ll_objs_repaired[i] = 0;
5099 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5100 fid_zero(&com->lc_fid_latest_scanned_phase2);
5103 lo->ll_status = LS_SCANNING_PHASE1;
5104 if (!lfsck->li_drop_dryrun ||
5105 lo->ll_pos_first_inconsistent == 0)
5106 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5108 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5110 spin_unlock(&lfsck->li_lock);
5111 up_write(&com->lc_sem);
5116 static int lfsck_layout_slave_prep(const struct lu_env *env,
5117 struct lfsck_component *com,
5118 struct lfsck_start_param *lsp)
5120 struct lfsck_layout_slave_data *llsd = com->lc_data;
5121 struct lfsck_instance *lfsck = com->lc_lfsck;
5122 struct lfsck_layout *lo = com->lc_file_ram;
5123 struct lfsck_start *start = lsp->lsp_start;
5126 rc = lfsck_layout_prep(env, com, start);
5130 if (lo->ll_flags & LF_CRASHED_LASTID &&
5131 list_empty(&llsd->llsd_master_list)) {
5132 LASSERT(lfsck->li_out_notify != NULL);
5134 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5135 LE_LASTID_REBUILDING);
5138 if (!lsp->lsp_index_valid)
5141 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5142 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5143 LASSERT(!llsd->llsd_rbtree_valid);
5145 write_lock(&llsd->llsd_rb_lock);
5146 rc = lfsck_rbtree_setup(env, com);
5147 write_unlock(&llsd->llsd_rb_lock);
5150 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5151 "%llu]\n", lfsck_lfsck2name(lfsck),
5152 com->lc_pos_start.lp_oit_cookie);
5157 static int lfsck_layout_master_prep(const struct lu_env *env,
5158 struct lfsck_component *com,
5159 struct lfsck_start_param *lsp)
5164 rc = lfsck_layout_load_bitmap(env, com);
5166 rc = lfsck_layout_reset(env, com, false);
5168 rc = lfsck_set_param(env, com->lc_lfsck,
5169 lsp->lsp_start, true);
5175 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5179 rc = lfsck_start_assistant(env, com, lsp);
5184 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5185 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5186 com->lc_pos_start.lp_oit_cookie);
5191 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5192 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5193 struct lfsck_component *com,
5194 struct dt_object *parent,
5195 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5197 struct lfsck_thread_info *info = lfsck_env_info(env);
5198 struct lfsck_instance *lfsck = com->lc_lfsck;
5199 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5200 struct lfsck_layout *lo = com->lc_file_ram;
5201 struct lfsck_assistant_data *lad = com->lc_data;
5202 struct lfsck_assistant_object *lso = NULL;
5203 struct lov_ost_data_v1 *objs;
5204 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5205 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5206 struct ptlrpc_thread *athread = &lad->lad_thread;
5207 struct l_wait_info lwi = { 0 };
5215 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5216 magic = le32_to_cpu(lmm->lmm_magic);
5217 if (magic == LOV_MAGIC_V1) {
5218 objs = &lmm->lmm_objects[0];
5220 LASSERT(magic == LOV_MAGIC_V3);
5221 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5224 count = le16_to_cpu(lmm->lmm_stripe_count);
5225 for (i = 0; i < count; i++, objs++) {
5226 struct lu_fid *fid = &info->lti_fid;
5227 struct ost_id *oi = &info->lti_oi;
5228 struct lfsck_layout_req *llr;
5229 struct lfsck_tgt_desc *tgt = NULL;
5230 struct dt_object *cobj = NULL;
5232 bool wakeup = false;
5234 if (unlikely(lovea_slot_is_dummy(objs)))
5237 l_wait_event(mthread->t_ctl_waitq,
5238 lad->lad_prefetched < bk->lb_async_windows ||
5239 !thread_is_running(mthread) ||
5240 thread_is_stopped(athread),
5243 if (unlikely(!thread_is_running(mthread)) ||
5244 thread_is_stopped(athread))
5247 if (unlikely(lfsck_is_dead_obj(parent)))
5250 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5251 index = le32_to_cpu(objs->l_ost_idx);
5252 rc = ostid_to_fid(fid, oi, index);
5254 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5255 ": "DOSTID", idx %u, comp_id %u\n",
5256 lfsck_lfsck2name(lfsck),
5257 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5262 tgt = lfsck_tgt_get(ltds, index);
5263 if (unlikely(tgt == NULL)) {
5264 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5265 "did not join the layout LFSCK, comp_id %u\n",
5266 lfsck_lfsck2name(lfsck), index, comp_id);
5267 lfsck_lad_set_bitmap(env, com, index);
5271 /* There is potential deadlock race condition between object
5272 * destroy and layout LFSCK. Consider the following scenario:
5274 * 1) The LFSCK thread obtained the parent object firstly, at
5275 * that time, the parent object has not been destroyed yet.
5277 * 2) One RPC service thread destroyed the parent and all its
5278 * children objects. Because the LFSCK is referencing the
5279 * parent object, then the parent object will be marked as
5280 * dying in RAM. On the other hand, the parent object is
5281 * referencing all its children objects, then all children
5282 * objects will be marked as dying in RAM also.
5284 * 3) The LFSCK thread tries to find some child object with
5285 * the parent object referenced. Then it will find that the
5286 * child object is dying. According to the object visibility
5287 * rules: the object with dying flag cannot be returned to
5288 * others. So the LFSCK thread has to wait until the dying
5289 * object has been purged from RAM, then it can allocate a
5290 * new object (with the same FID) in RAM. Unfortunately, the
5291 * LFSCK thread itself is referencing the parent object, and
5292 * cause the parent object cannot be purged, then cause the
5293 * child object cannot be purged also. So the LFSCK thread
5294 * will fall into deadlock.
5296 * We introduce non-blocked version lu_object_find() to allow
5297 * the LFSCK thread to return failure immediately (instead of
5298 * wait) when it finds dying (child) object, then the LFSCK
5299 * thread can check whether the parent object is dying or not.
5300 * So avoid above deadlock. LU-5395 */
5301 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
5303 if (lfsck_is_dead_obj(parent)) {
5313 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_ASSISTANT_DIRECT)) {
5314 rc = dt_declare_attr_get(env, cobj);
5318 rc = dt_declare_xattr_get(env, cobj, &buf,
5325 struct lu_attr *attr = &info->lti_la;
5327 rc = dt_attr_get(env, parent, attr);
5331 lso = lfsck_assistant_object_init(env,
5332 lfsck_dto2fid(parent), attr,
5333 lfsck->li_pos_current.lp_oit_cookie, false);
5342 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5350 spin_lock(&lad->lad_lock);
5351 if (lad->lad_assistant_status < 0) {
5352 spin_unlock(&lad->lad_lock);
5353 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5355 RETURN(lad->lad_assistant_status);
5358 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5359 if (lad->lad_prefetched == 0)
5362 lad->lad_prefetched++;
5363 spin_unlock(&lad->lad_lock);
5365 wake_up_all(&athread->t_ctl_waitq);
5368 down_write(&com->lc_sem);
5369 com->lc_new_checked++;
5371 lfsck_layout_record_failure(env, lfsck, lo);
5372 up_write(&com->lc_sem);
5374 if (cobj != NULL && !IS_ERR(cobj))
5375 lfsck_object_put(env, cobj);
5377 if (likely(tgt != NULL))
5380 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5388 lfsck_assistant_object_put(env, lso);
5393 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5394 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5395 * list ::lad_req_list.
5397 * For each request on above list, the lfsck_layout_assistant thread compares
5398 * the OST side attribute with local attribute, if inconsistent, then repair it.
5400 * All above processing is async mode with pipeline. */
5401 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5402 struct lfsck_component *com,
5403 struct dt_object *obj)
5405 struct lfsck_thread_info *info = lfsck_env_info(env);
5406 struct ost_id *oi = &info->lti_oi;
5407 struct lfsck_layout *lo = com->lc_file_ram;
5408 struct lfsck_assistant_data *lad = com->lc_data;
5409 struct lfsck_instance *lfsck = com->lc_lfsck;
5410 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5411 struct thandle *handle = NULL;
5412 struct lu_buf *buf = &info->lti_big_buf;
5413 struct lov_mds_md_v1 *lmm = NULL;
5414 struct dt_device *dev = lfsck_obj2dev(obj);
5415 struct lustre_handle lh = { 0 };
5416 struct lu_buf ea_buf = { NULL };
5417 struct lov_comp_md_v1 *lcm = NULL;
5418 struct lov_comp_md_entry_v1 *lcme = NULL;
5423 bool locked = false;
5424 bool stripe = false;
5425 bool bad_oi = false;
5428 if (!S_ISREG(lfsck_object_type(obj)))
5431 if (lad->lad_assistant_status < 0)
5432 GOTO(out, rc = -ESRCH);
5434 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5435 lmm_oi_cpu_to_le(oi, oi);
5436 dt_read_lock(env, obj, 0);
5441 if (dt_object_exists(obj) == 0 ||
5442 lfsck_is_dead_obj(obj))
5445 rc = lfsck_layout_get_lovea(env, obj, buf);
5447 /* Skip bad lov EA during the 1st cycle scanning, and
5448 * try to recover it via orphan in the 2nd scanning. */
5449 GOTO(out, rc = (rc == -EINVAL ? 0 : rc));
5453 magic = le32_to_cpu(lmm->lmm_magic);
5454 if (magic == LOV_MAGIC_COMP_V1) {
5458 count = le16_to_cpu(lcm->lcm_entry_count);
5459 for (i = 0; i < count; i++) {
5460 lcme = &lcm->lcm_entries[i];
5461 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5462 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) != 0)
5466 GOTO(out, stripe = true);
5467 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5468 GOTO(out, stripe = true);
5472 /* Inconsistent lmm_oi, should be repaired. */
5475 if (bk->lb_param & LPF_DRYRUN) {
5476 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5478 GOTO(out, stripe = true);
5481 if (!lustre_handle_is_used(&lh)) {
5482 dt_read_unlock(env, obj);
5484 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5485 MDS_INODELOCK_LAYOUT |
5486 MDS_INODELOCK_XATTR, LCK_EX);
5490 handle = dt_trans_create(env, dev);
5492 GOTO(out, rc = PTR_ERR(handle));
5494 lfsck_buf_init(&ea_buf, lmm, size);
5495 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5496 LU_XATTR_REPLACE, handle);
5500 rc = dt_trans_start_local(env, dev, handle);
5504 dt_write_lock(env, obj, 0);
5510 if (magic == LOV_MAGIC_COMP_V1) {
5513 for (i = 0; i < count; i++) {
5514 lcme = &lcm->lcm_entries[i];
5515 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5522 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5523 LU_XATTR_REPLACE, handle);
5527 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5529 GOTO(out, stripe = true);
5533 if (lustre_handle_is_used(&lh))
5534 dt_write_unlock(env, obj);
5536 dt_read_unlock(env, obj);
5539 if (handle != NULL && !IS_ERR(handle))
5540 dt_trans_stop(env, dev, handle);
5542 lfsck_ibits_unlock(&lh, LCK_EX);
5545 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5546 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5547 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5548 PFID(lfsck_dto2fid(obj)), rc);
5551 if (magic == LOV_MAGIC_COMP_V1) {
5554 for (i = 0; i < count; i++) {
5555 lcme = &lcm->lcm_entries[i];
5556 if (!(le32_to_cpu(lcme->lcme_flags) &
5560 rc = lfsck_layout_scan_stripes(env, com, obj,
5561 (struct lov_mds_md_v1 *)(buf->lb_buf +
5562 le32_to_cpu(lcme->lcme_offset)),
5563 le32_to_cpu(lcme->lcme_id));
5566 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5569 down_write(&com->lc_sem);
5570 com->lc_new_checked++;
5572 lfsck_layout_record_failure(env, lfsck, lo);
5573 up_write(&com->lc_sem);
5579 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5580 struct lfsck_component *com,
5581 struct dt_object *obj)
5583 struct lfsck_instance *lfsck = com->lc_lfsck;
5584 struct lfsck_layout *lo = com->lc_file_ram;
5585 const struct lu_fid *fid = lfsck_dto2fid(obj);
5586 struct lfsck_layout_slave_data *llsd = com->lc_data;
5587 struct lfsck_layout_seq *lls;
5593 LASSERT(llsd != NULL);
5595 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5596 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5597 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
5599 struct ptlrpc_thread *thread = &lfsck->li_thread;
5601 l_wait_event(thread->t_ctl_waitq,
5602 !thread_is_running(thread),
5606 lfsck_rbtree_update_bitmap(env, com, fid, false);
5608 down_write(&com->lc_sem);
5609 if (fid_is_idif(fid))
5611 else if (!fid_is_norm(fid) ||
5612 !fid_is_for_ostobj(env, lfsck, obj, fid))
5613 GOTO(unlock, rc = 0);
5616 com->lc_new_checked++;
5618 lls = lfsck_layout_seq_lookup(llsd, seq);
5621 if (unlikely(lls == NULL))
5622 GOTO(unlock, rc = -ENOMEM);
5624 INIT_LIST_HEAD(&lls->lls_list);
5626 rc = lfsck_layout_lastid_load(env, com, lls);
5628 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5629 "load LAST_ID for %#llx: rc = %d\n",
5630 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5631 lo->ll_objs_failed_phase1++;
5636 lfsck_layout_seq_insert(llsd, lls);
5639 if (unlikely(fid_is_last_id(fid)))
5640 GOTO(unlock, rc = 0);
5642 if (fid_is_idif(fid))
5643 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5647 if (oid > lls->lls_lastid_known)
5648 lls->lls_lastid_known = oid;
5650 if (oid > lls->lls_lastid) {
5651 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
5652 /* OFD may create new objects during LFSCK scanning. */
5653 rc = lfsck_layout_lastid_reload(env, com, lls);
5654 if (unlikely(rc != 0)) {
5655 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5656 "reload LAST_ID for %#llx: rc = %d\n",
5657 lfsck_lfsck2name(com->lc_lfsck),
5663 if (oid <= lls->lls_lastid ||
5664 lo->ll_flags & LF_CRASHED_LASTID)
5665 GOTO(unlock, rc = 0);
5667 LASSERT(lfsck->li_out_notify != NULL);
5669 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5670 LE_LASTID_REBUILDING);
5671 lo->ll_flags |= LF_CRASHED_LASTID;
5673 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
5674 "LAST_ID file (2) for the sequence %#llx"
5675 ", old value %llu, known value %llu\n",
5676 lfsck_lfsck2name(lfsck), lls->lls_seq,
5677 lls->lls_lastid, oid);
5680 lls->lls_lastid = oid;
5684 GOTO(unlock, rc = 0);
5687 up_write(&com->lc_sem);
5692 static int lfsck_layout_exec_dir(const struct lu_env *env,
5693 struct lfsck_component *com,
5694 struct lfsck_assistant_object *lso,
5695 struct lu_dirent *ent, __u16 type)
5700 static int lfsck_layout_master_post(const struct lu_env *env,
5701 struct lfsck_component *com,
5702 int result, bool init)
5704 struct lfsck_instance *lfsck = com->lc_lfsck;
5705 struct lfsck_layout *lo = com->lc_file_ram;
5709 lfsck_post_generic(env, com, &result);
5711 down_write(&com->lc_sem);
5712 spin_lock(&lfsck->li_lock);
5714 lo->ll_pos_last_checkpoint =
5715 lfsck->li_pos_checkpoint.lp_oit_cookie;
5718 if (lo->ll_flags & LF_INCOMPLETE)
5719 lo->ll_status = LS_PARTIAL;
5721 lo->ll_status = LS_SCANNING_PHASE2;
5722 lo->ll_flags |= LF_SCANNED_ONCE;
5723 lo->ll_flags &= ~LF_UPGRADE;
5724 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5725 } else if (result == 0) {
5726 if (lfsck->li_status != 0)
5727 lo->ll_status = lfsck->li_status;
5729 lo->ll_status = LS_STOPPED;
5730 if (lo->ll_status != LS_PAUSED)
5731 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5733 lo->ll_status = LS_FAILED;
5734 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5736 spin_unlock(&lfsck->li_lock);
5739 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
5740 HALF_SEC - lfsck->li_time_last_checkpoint);
5741 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5742 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5743 com->lc_new_checked = 0;
5746 rc = lfsck_layout_store(env, com);
5747 up_write(&com->lc_sem);
5749 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
5750 lfsck_lfsck2name(lfsck), rc);
5755 static int lfsck_layout_slave_post(const struct lu_env *env,
5756 struct lfsck_component *com,
5757 int result, bool init)
5759 struct lfsck_instance *lfsck = com->lc_lfsck;
5760 struct lfsck_layout *lo = com->lc_file_ram;
5764 down_write(&com->lc_sem);
5765 rc = lfsck_layout_lastid_store(env, com);
5769 LASSERT(lfsck->li_out_notify != NULL);
5771 spin_lock(&lfsck->li_lock);
5773 lo->ll_pos_last_checkpoint =
5774 lfsck->li_pos_checkpoint.lp_oit_cookie;
5777 lo->ll_status = LS_SCANNING_PHASE2;
5778 lo->ll_flags |= LF_SCANNED_ONCE;
5779 if (lo->ll_flags & LF_CRASHED_LASTID) {
5781 lo->ll_flags &= ~LF_CRASHED_LASTID;
5783 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
5784 "crashed LAST_ID files successfully\n",
5785 lfsck_lfsck2name(lfsck));
5787 lo->ll_flags &= ~LF_UPGRADE;
5788 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5789 } else if (result == 0) {
5790 if (lfsck->li_status != 0)
5791 lo->ll_status = lfsck->li_status;
5793 lo->ll_status = LS_STOPPED;
5794 if (lo->ll_status != LS_PAUSED)
5795 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5797 lo->ll_status = LS_FAILED;
5798 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5800 spin_unlock(&lfsck->li_lock);
5803 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5807 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
5808 HALF_SEC - lfsck->li_time_last_checkpoint);
5809 lo->ll_time_last_checkpoint = cfs_time_current_sec();
5810 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5811 com->lc_new_checked = 0;
5814 rc = lfsck_layout_store(env, com);
5815 up_write(&com->lc_sem);
5817 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
5819 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
5820 lfsck_lfsck2name(lfsck), rc);
5825 static void lfsck_layout_dump(const struct lu_env *env,
5826 struct lfsck_component *com, struct seq_file *m)
5828 struct lfsck_instance *lfsck = com->lc_lfsck;
5829 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5830 struct lfsck_layout *lo = com->lc_file_ram;
5832 down_read(&com->lc_sem);
5833 seq_printf(m, "name: lfsck_layout\n"
5839 lfsck_status2name(lo->ll_status));
5841 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
5843 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
5845 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
5847 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
5849 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
5851 seq_printf(m, "latest_start_position: %llu\n"
5852 "last_checkpoint_position: %llu\n"
5853 "first_failure_position: %llu\n",
5854 lo->ll_pos_latest_start,
5855 lo->ll_pos_last_checkpoint,
5856 lo->ll_pos_first_inconsistent);
5858 seq_printf(m, "success_count: %u\n"
5859 "repaired_dangling: %llu\n"
5860 "repaired_unmatched_pair: %llu\n"
5861 "repaired_multiple_referenced: %llu\n"
5862 "repaired_orphan: %llu\n"
5863 "repaired_inconsistent_owner: %llu\n"
5864 "repaired_others: %llu\n"
5866 "failed_phase1: %llu\n"
5867 "failed_phase2: %llu\n",
5868 lo->ll_success_count,
5869 lo->ll_objs_repaired[LLIT_DANGLING - 1],
5870 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
5871 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
5872 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
5873 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
5874 lo->ll_objs_repaired[LLIT_OTHERS - 1],
5875 lo->ll_objs_skipped,
5876 lo->ll_objs_failed_phase1,
5877 lo->ll_objs_failed_phase2);
5879 if (lo->ll_status == LS_SCANNING_PHASE1) {
5881 cfs_duration_t duration = cfs_time_current() -
5882 lfsck->li_time_last_checkpoint;
5883 __u64 checked = lo->ll_objs_checked_phase1 +
5884 com->lc_new_checked;
5885 __u64 speed = checked;
5886 __u64 new_checked = com->lc_new_checked *
5887 msecs_to_jiffies(MSEC_PER_SEC);
5888 __u32 rtime = lo->ll_run_time_phase1 +
5889 cfs_duration_sec(duration + HALF_SEC);
5892 do_div(new_checked, duration);
5894 do_div(speed, rtime);
5895 seq_printf(m, "checked_phase1: %llu\n"
5896 "checked_phase2: %llu\n"
5897 "run_time_phase1: %u seconds\n"
5898 "run_time_phase2: %u seconds\n"
5899 "average_speed_phase1: %llu items/sec\n"
5900 "average_speed_phase2: N/A\n"
5901 "real-time_speed_phase1: %llu items/sec\n"
5902 "real-time_speed_phase2: N/A\n",
5904 lo->ll_objs_checked_phase2,
5906 lo->ll_run_time_phase2,
5910 if (likely(lfsck->li_di_oit)) {
5911 const struct dt_it_ops *iops =
5912 &lfsck->li_obj_oit->do_index_ops->dio_it;
5914 /* The low layer otable-based iteration position may NOT
5915 * exactly match the layout-based directory traversal
5916 * cookie. Generally, it is not a serious issue. But the
5917 * caller should NOT make assumption on that. */
5918 pos = iops->store(env, lfsck->li_di_oit);
5919 if (!lfsck->li_current_oit_processed)
5922 pos = lo->ll_pos_last_checkpoint;
5925 seq_printf(m, "current_position: %llu\n", pos);
5926 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
5927 cfs_duration_t duration = cfs_time_current() -
5928 com->lc_time_last_checkpoint;
5929 __u64 checked = lo->ll_objs_checked_phase2 +
5930 com->lc_new_checked;
5931 __u64 speed1 = lo->ll_objs_checked_phase1;
5932 __u64 speed2 = checked;
5933 __u64 new_checked = com->lc_new_checked *
5934 msecs_to_jiffies(MSEC_PER_SEC);
5935 __u32 rtime = lo->ll_run_time_phase2 +
5936 cfs_duration_sec(duration + HALF_SEC);
5939 do_div(new_checked, duration);
5940 if (lo->ll_run_time_phase1 != 0)
5941 do_div(speed1, lo->ll_run_time_phase1);
5943 do_div(speed2, rtime);
5944 seq_printf(m, "checked_phase1: %llu\n"
5945 "checked_phase2: %llu\n"
5946 "run_time_phase1: %u seconds\n"
5947 "run_time_phase2: %u seconds\n"
5948 "average_speed_phase1: %llu items/sec\n"
5949 "average_speed_phase2: %llu items/sec\n"
5950 "real-time_speed_phase1: N/A\n"
5951 "real-time_speed_phase2: %llu items/sec\n"
5952 "current_position: "DFID"\n",
5953 lo->ll_objs_checked_phase1,
5955 lo->ll_run_time_phase1,
5960 PFID(&com->lc_fid_latest_scanned_phase2));
5962 __u64 speed1 = lo->ll_objs_checked_phase1;
5963 __u64 speed2 = lo->ll_objs_checked_phase2;
5965 if (lo->ll_run_time_phase1 != 0)
5966 do_div(speed1, lo->ll_run_time_phase1);
5967 if (lo->ll_run_time_phase2 != 0)
5968 do_div(speed2, lo->ll_run_time_phase2);
5969 seq_printf(m, "checked_phase1: %llu\n"
5970 "checked_phase2: %llu\n"
5971 "run_time_phase1: %u seconds\n"
5972 "run_time_phase2: %u seconds\n"
5973 "average_speed_phase1: %llu items/sec\n"
5974 "average_speed_phase2: %llu objs/sec\n"
5975 "real-time_speed_phase1: N/A\n"
5976 "real-time_speed_phase2: N/A\n"
5977 "current_position: N/A\n",
5978 lo->ll_objs_checked_phase1,
5979 lo->ll_objs_checked_phase2,
5980 lo->ll_run_time_phase1,
5981 lo->ll_run_time_phase2,
5986 up_read(&com->lc_sem);
5989 static int lfsck_layout_master_double_scan(const struct lu_env *env,
5990 struct lfsck_component *com)
5992 struct lfsck_layout *lo = com->lc_file_ram;
5993 struct lfsck_assistant_data *lad = com->lc_data;
5994 struct lfsck_instance *lfsck = com->lc_lfsck;
5995 struct lfsck_tgt_descs *ltds;
5996 struct lfsck_tgt_desc *ltd;
5997 struct lfsck_tgt_desc *next;
6000 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
6002 if (thread_is_stopped(&lad->lad_thread)) {
6003 LASSERT(list_empty(&lad->lad_req_list));
6004 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6005 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6007 ltds = &lfsck->li_ost_descs;
6008 spin_lock(<ds->ltd_lock);
6009 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6010 ltd_layout_phase_list) {
6011 list_del_init(<d->ltd_layout_phase_list);
6013 spin_unlock(<ds->ltd_lock);
6015 ltds = &lfsck->li_mdt_descs;
6016 spin_lock(<ds->ltd_lock);
6017 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6018 ltd_layout_phase_list) {
6019 list_del_init(<d->ltd_layout_phase_list);
6021 spin_unlock(<ds->ltd_lock);
6027 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6028 struct lfsck_component *com)
6030 struct lfsck_instance *lfsck = com->lc_lfsck;
6031 struct lfsck_layout_slave_data *llsd = com->lc_data;
6032 struct lfsck_layout *lo = com->lc_file_ram;
6033 struct ptlrpc_thread *thread = &lfsck->li_thread;
6037 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6038 lfsck_lfsck2name(lfsck));
6040 atomic_inc(&lfsck->li_double_scan_count);
6042 if (lo->ll_flags & LF_INCOMPLETE)
6045 com->lc_new_checked = 0;
6046 com->lc_new_scanned = 0;
6047 com->lc_time_last_checkpoint = cfs_time_current();
6048 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6049 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6052 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
6055 rc = lfsck_layout_slave_query_master(env, com);
6056 if (list_empty(&llsd->llsd_master_list)) {
6057 if (unlikely(!thread_is_running(thread)))
6068 rc = l_wait_event(thread->t_ctl_waitq,
6069 !thread_is_running(thread) ||
6070 lo->ll_flags & LF_INCOMPLETE ||
6071 list_empty(&llsd->llsd_master_list),
6073 if (unlikely(!thread_is_running(thread)))
6076 if (lo->ll_flags & LF_INCOMPLETE)
6079 if (rc == -ETIMEDOUT)
6082 GOTO(done, rc = (rc < 0 ? rc : 1));
6086 rc = lfsck_layout_double_scan_result(env, com, rc);
6087 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6088 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6089 lfsck_layout_slave_quit(env, com);
6090 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6091 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6093 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6094 "status %d: rc = %d\n",
6095 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6100 static void lfsck_layout_master_data_release(const struct lu_env *env,
6101 struct lfsck_component *com)
6103 struct lfsck_assistant_data *lad = com->lc_data;
6104 struct lfsck_instance *lfsck = com->lc_lfsck;
6105 struct lfsck_tgt_descs *ltds;
6106 struct lfsck_tgt_desc *ltd;
6107 struct lfsck_tgt_desc *next;
6109 LASSERT(lad != NULL);
6110 LASSERT(thread_is_init(&lad->lad_thread) ||
6111 thread_is_stopped(&lad->lad_thread));
6112 LASSERT(list_empty(&lad->lad_req_list));
6114 com->lc_data = NULL;
6116 ltds = &lfsck->li_ost_descs;
6117 spin_lock(<ds->ltd_lock);
6118 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_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_phase2_list,
6123 ltd_layout_phase_list) {
6124 list_del_init(<d->ltd_layout_phase_list);
6126 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6128 list_del_init(<d->ltd_layout_list);
6130 spin_unlock(<ds->ltd_lock);
6132 ltds = &lfsck->li_mdt_descs;
6133 spin_lock(<ds->ltd_lock);
6134 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_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_phase2_list,
6139 ltd_layout_phase_list) {
6140 list_del_init(<d->ltd_layout_phase_list);
6142 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6144 list_del_init(<d->ltd_layout_list);
6146 spin_unlock(<ds->ltd_lock);
6148 if (likely(lad->lad_bitmap != NULL))
6149 CFS_FREE_BITMAP(lad->lad_bitmap);
6154 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6155 struct lfsck_component *com)
6157 struct lfsck_layout_slave_data *llsd = com->lc_data;
6159 lfsck_layout_slave_quit(env, com);
6160 com->lc_data = NULL;
6164 static void lfsck_layout_master_quit(const struct lu_env *env,
6165 struct lfsck_component *com)
6167 struct lfsck_assistant_data *lad = com->lc_data;
6168 struct lfsck_instance *lfsck = com->lc_lfsck;
6169 struct lfsck_tgt_descs *ltds;
6170 struct lfsck_tgt_desc *ltd;
6171 struct lfsck_tgt_desc *next;
6173 LASSERT(lad != NULL);
6175 lfsck_quit_generic(env, com);
6177 LASSERT(thread_is_init(&lad->lad_thread) ||
6178 thread_is_stopped(&lad->lad_thread));
6179 LASSERT(list_empty(&lad->lad_req_list));
6181 ltds = &lfsck->li_ost_descs;
6182 spin_lock(<ds->ltd_lock);
6183 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6184 ltd_layout_phase_list) {
6185 list_del_init(<d->ltd_layout_phase_list);
6187 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6188 ltd_layout_phase_list) {
6189 list_del_init(<d->ltd_layout_phase_list);
6191 spin_unlock(<ds->ltd_lock);
6193 ltds = &lfsck->li_mdt_descs;
6194 spin_lock(<ds->ltd_lock);
6195 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6196 ltd_layout_phase_list) {
6197 list_del_init(<d->ltd_layout_phase_list);
6199 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6200 ltd_layout_phase_list) {
6201 list_del_init(<d->ltd_layout_phase_list);
6203 spin_unlock(<ds->ltd_lock);
6206 static void lfsck_layout_slave_quit(const struct lu_env *env,
6207 struct lfsck_component *com)
6209 struct lfsck_layout_slave_data *llsd = com->lc_data;
6210 struct lfsck_layout_seq *lls;
6211 struct lfsck_layout_seq *next;
6212 struct lfsck_layout_slave_target *llst;
6214 LASSERT(llsd != NULL);
6216 down_write(&com->lc_sem);
6217 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6219 list_del_init(&lls->lls_list);
6220 lfsck_object_put(env, lls->lls_lastid_obj);
6223 up_write(&com->lc_sem);
6225 spin_lock(&llsd->llsd_lock);
6226 while (!list_empty(&llsd->llsd_master_list)) {
6227 llst = list_entry(llsd->llsd_master_list.next,
6228 struct lfsck_layout_slave_target, llst_list);
6229 list_del_init(&llst->llst_list);
6230 spin_unlock(&llsd->llsd_lock);
6231 lfsck_layout_llst_put(llst);
6232 spin_lock(&llsd->llsd_lock);
6234 spin_unlock(&llsd->llsd_lock);
6236 lfsck_rbtree_cleanup(env, com);
6239 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6240 struct lfsck_component *com,
6241 struct lfsck_request *lr)
6243 struct lfsck_instance *lfsck = com->lc_lfsck;
6244 struct lfsck_layout *lo = com->lc_file_ram;
6245 struct lfsck_assistant_data *lad = com->lc_data;
6246 struct lfsck_tgt_descs *ltds;
6247 struct lfsck_tgt_desc *ltd;
6251 if (lr->lr_event == LE_PAIRS_VERIFY) {
6254 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6261 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6262 "from %s %x, status %d, flags %x, flags2 %x\n",
6263 lfsck_lfsck2name(lfsck), lr->lr_event,
6264 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6265 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6267 if (lr->lr_event != LE_PHASE1_DONE &&
6268 lr->lr_event != LE_PHASE2_DONE &&
6269 lr->lr_event != LE_PEER_EXIT)
6272 if (lr->lr_flags & LEF_FROM_OST)
6273 ltds = &lfsck->li_ost_descs;
6275 ltds = &lfsck->li_mdt_descs;
6276 spin_lock(<ds->ltd_lock);
6277 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6279 spin_unlock(<ds->ltd_lock);
6284 list_del_init(<d->ltd_layout_phase_list);
6285 switch (lr->lr_event) {
6286 case LE_PHASE1_DONE:
6287 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6288 if (lr->lr_flags2 & LF_INCOMPLETE) {
6289 if (lr->lr_flags & LEF_FROM_OST)
6290 lfsck_lad_set_bitmap(env, com,
6293 lo->ll_flags |= LF_INCOMPLETE;
6295 ltd->ltd_layout_done = 1;
6296 list_del_init(<d->ltd_layout_list);
6301 if (lr->lr_flags & LEF_FROM_OST) {
6302 if (list_empty(<d->ltd_layout_list))
6303 list_add_tail(<d->ltd_layout_list,
6304 &lad->lad_ost_list);
6305 list_add_tail(<d->ltd_layout_phase_list,
6306 &lad->lad_ost_phase2_list);
6308 if (list_empty(<d->ltd_layout_list))
6309 list_add_tail(<d->ltd_layout_list,
6310 &lad->lad_mdt_list);
6311 list_add_tail(<d->ltd_layout_phase_list,
6312 &lad->lad_mdt_phase2_list);
6315 case LE_PHASE2_DONE:
6316 ltd->ltd_layout_done = 1;
6317 if (!list_empty(<d->ltd_layout_list)) {
6318 list_del_init(<d->ltd_layout_list);
6319 if (lr->lr_flags2 & LF_INCOMPLETE) {
6320 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6328 ltd->ltd_layout_done = 1;
6329 list_del_init(<d->ltd_layout_list);
6330 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6331 !(lr->lr_flags & LEF_FROM_OST))
6332 lo->ll_flags |= LF_INCOMPLETE;
6337 spin_unlock(<ds->ltd_lock);
6339 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6340 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6342 memset(stop, 0, sizeof(*stop));
6343 stop->ls_status = lr->lr_status;
6344 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6345 lfsck_stop(env, lfsck->li_bottom, stop);
6346 } else if (lfsck_phase2_next_ready(lad)) {
6347 wake_up_all(&lad->lad_thread.t_ctl_waitq);
6353 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6354 struct lfsck_component *com,
6355 struct lfsck_req_local *lrl,
6360 switch (lrl->lrl_event) {
6361 case LEL_FID_ACCESSED:
6362 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6364 case LEL_PAIRS_VERIFY_LOCAL: {
6367 lrl->lrl_status = LPVS_INIT;
6368 /* Firstly, if the MDT-object which is claimed via OST-object
6369 * local stored PFID xattr recognizes the OST-object, then it
6370 * must be that the client given PFID is wrong. */
6371 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6372 &lrl->lrl_ff_local.ff_parent,
6373 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6377 lrl->lrl_status = LPVS_INCONSISTENT;
6378 /* The OST-object local stored PFID xattr is stale. We need to
6379 * check whether the MDT-object that is claimed via the client
6380 * given PFID information recognizes the OST-object or not. If
6381 * matches, then need to update the OST-object's PFID xattr. */
6382 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6383 &lrl->lrl_ff_client.ff_parent,
6384 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6386 * We are not sure whether the client given PFID information
6387 * is correct or not, do nothing to avoid improper fixing.
6390 * The client given PFID information is also invalid, we can
6391 * NOT fix the OST-object inconsistency.
6394 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6395 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6404 RETURN(-EOPNOTSUPP);
6407 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6408 struct lfsck_component *com,
6409 struct lfsck_request *lr)
6411 struct lfsck_instance *lfsck = com->lc_lfsck;
6412 struct lfsck_layout_slave_data *llsd = com->lc_data;
6413 struct lfsck_layout_slave_target *llst;
6417 switch (lr->lr_event) {
6418 case LE_CONDITIONAL_DESTROY:
6419 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6421 case LE_PHASE1_DONE: {
6422 if (lr->lr_flags2 & LF_INCOMPLETE) {
6423 struct lfsck_layout *lo = com->lc_file_ram;
6425 lo->ll_flags |= LF_INCOMPLETE;
6426 llst = lfsck_layout_llst_find_and_del(llsd,
6430 lfsck_layout_llst_put(llst);
6431 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6437 case LE_PHASE2_DONE:
6439 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6440 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6441 lr->lr_event, lr->lr_index, lr->lr_status);
6447 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6451 lfsck_layout_llst_put(llst);
6452 if (list_empty(&llsd->llsd_master_list))
6453 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6455 if (lr->lr_event == LE_PEER_EXIT &&
6456 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6457 (list_empty(&llsd->llsd_master_list) &&
6458 (lr->lr_status == LS_STOPPED ||
6459 lr->lr_status == LS_CO_STOPPED)))) {
6460 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6462 memset(stop, 0, sizeof(*stop));
6463 stop->ls_status = lr->lr_status;
6464 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6465 lfsck_stop(env, lfsck->li_bottom, stop);
6471 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6475 for (i = 0; i < LLIT_MAX; i++)
6476 *count += lo->ll_objs_repaired[i];
6479 static int lfsck_layout_query_all(const struct lu_env *env,
6480 struct lfsck_component *com,
6481 __u32 *mdts_count, __u32 *osts_count,
6484 struct lfsck_layout *lo = com->lc_file_ram;
6485 struct lfsck_tgt_descs *ltds;
6486 struct lfsck_tgt_desc *ltd;
6491 rc = lfsck_query_all(env, com);
6495 ltds = &com->lc_lfsck->li_mdt_descs;
6496 down_read(<ds->ltd_rw_sem);
6497 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6498 ltd = lfsck_ltd2tgt(ltds, idx);
6499 LASSERT(ltd != NULL);
6501 mdts_count[ltd->ltd_layout_status]++;
6502 *repaired += ltd->ltd_layout_repaired;
6504 up_read(<ds->ltd_rw_sem);
6506 ltds = &com->lc_lfsck->li_ost_descs;
6507 down_read(<ds->ltd_rw_sem);
6508 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6509 ltd = lfsck_ltd2tgt(ltds, idx);
6510 LASSERT(ltd != NULL);
6512 osts_count[ltd->ltd_layout_status]++;
6513 *repaired += ltd->ltd_layout_repaired;
6515 up_read(<ds->ltd_rw_sem);
6517 down_read(&com->lc_sem);
6518 mdts_count[lo->ll_status]++;
6519 lfsck_layout_repaired(lo, repaired);
6520 up_read(&com->lc_sem);
6525 static int lfsck_layout_query(const struct lu_env *env,
6526 struct lfsck_component *com,
6527 struct lfsck_request *req,
6528 struct lfsck_reply *rep,
6529 struct lfsck_query *que, int idx)
6531 struct lfsck_layout *lo = com->lc_file_ram;
6535 LASSERT(com->lc_lfsck->li_master);
6537 rc = lfsck_layout_query_all(env, com,
6538 que->lu_mdts_count[idx],
6539 que->lu_osts_count[idx],
6540 &que->lu_repaired[idx]);
6542 down_read(&com->lc_sem);
6543 rep->lr_status = lo->ll_status;
6544 if (req->lr_flags & LEF_QUERY_ALL)
6545 lfsck_layout_repaired(lo, &rep->lr_repaired);
6546 up_read(&com->lc_sem);
6552 /* with lfsck::li_lock held */
6553 static int lfsck_layout_slave_join(const struct lu_env *env,
6554 struct lfsck_component *com,
6555 struct lfsck_start_param *lsp)
6557 struct lfsck_instance *lfsck = com->lc_lfsck;
6558 struct lfsck_layout_slave_data *llsd = com->lc_data;
6559 struct lfsck_layout_slave_target *llst;
6560 struct lfsck_start *start = lsp->lsp_start;
6564 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6567 if (!lsp->lsp_index_valid)
6570 /* If someone is running the LFSCK without orphan handling,
6571 * it will not maintain the object accessing rbtree. So we
6572 * cannot join it for orphan handling. */
6573 if (!llsd->llsd_rbtree_valid)
6576 spin_unlock(&lfsck->li_lock);
6577 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6578 spin_lock(&lfsck->li_lock);
6579 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6580 spin_unlock(&lfsck->li_lock);
6581 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6584 lfsck_layout_llst_put(llst);
6585 spin_lock(&lfsck->li_lock);
6592 static struct lfsck_operations lfsck_layout_master_ops = {
6593 .lfsck_reset = lfsck_layout_reset,
6594 .lfsck_fail = lfsck_layout_fail,
6595 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6596 .lfsck_prep = lfsck_layout_master_prep,
6597 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6598 .lfsck_exec_dir = lfsck_layout_exec_dir,
6599 .lfsck_post = lfsck_layout_master_post,
6600 .lfsck_dump = lfsck_layout_dump,
6601 .lfsck_double_scan = lfsck_layout_master_double_scan,
6602 .lfsck_data_release = lfsck_layout_master_data_release,
6603 .lfsck_quit = lfsck_layout_master_quit,
6604 .lfsck_in_notify = lfsck_layout_master_in_notify,
6605 .lfsck_query = lfsck_layout_query,
6608 static struct lfsck_operations lfsck_layout_slave_ops = {
6609 .lfsck_reset = lfsck_layout_reset,
6610 .lfsck_fail = lfsck_layout_fail,
6611 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6612 .lfsck_prep = lfsck_layout_slave_prep,
6613 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6614 .lfsck_exec_dir = lfsck_layout_exec_dir,
6615 .lfsck_post = lfsck_layout_slave_post,
6616 .lfsck_dump = lfsck_layout_dump,
6617 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6618 .lfsck_data_release = lfsck_layout_slave_data_release,
6619 .lfsck_quit = lfsck_layout_slave_quit,
6620 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6621 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6622 .lfsck_query = lfsck_layout_query,
6623 .lfsck_join = lfsck_layout_slave_join,
6626 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6627 struct lfsck_component *com,
6628 struct lfsck_position *pos)
6630 struct lfsck_assistant_data *lad = com->lc_data;
6631 struct lfsck_layout_req *llr;
6633 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6637 if (list_empty(&lad->lad_req_list))
6640 llr = list_entry(lad->lad_req_list.next,
6641 struct lfsck_layout_req,
6643 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6646 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6647 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
6648 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
6649 .la_fill_pos = lfsck_layout_assistant_fill_pos,
6650 .la_double_scan_result = lfsck_layout_double_scan_result,
6651 .la_req_fini = lfsck_layout_assistant_req_fini,
6652 .la_sync_failures = lfsck_layout_assistant_sync_failures,
6655 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
6657 struct lfsck_component *com;
6658 struct lfsck_layout *lo;
6659 struct dt_object *root = NULL;
6660 struct dt_object *obj;
6669 INIT_LIST_HEAD(&com->lc_link);
6670 INIT_LIST_HEAD(&com->lc_link_dir);
6671 init_rwsem(&com->lc_sem);
6672 atomic_set(&com->lc_ref, 1);
6673 com->lc_lfsck = lfsck;
6674 com->lc_type = LFSCK_TYPE_LAYOUT;
6675 if (lfsck->li_master) {
6676 com->lc_ops = &lfsck_layout_master_ops;
6677 com->lc_data = lfsck_assistant_data_init(
6678 &lfsck_layout_assistant_ops,
6680 if (com->lc_data == NULL)
6681 GOTO(out, rc = -ENOMEM);
6683 for (i = 0; i < LFSCK_STF_COUNT; i++)
6684 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6686 struct lfsck_layout_slave_data *llsd;
6688 com->lc_ops = &lfsck_layout_slave_ops;
6689 OBD_ALLOC_PTR(llsd);
6691 GOTO(out, rc = -ENOMEM);
6693 INIT_LIST_HEAD(&llsd->llsd_seq_list);
6694 INIT_LIST_HEAD(&llsd->llsd_master_list);
6695 spin_lock_init(&llsd->llsd_lock);
6696 llsd->llsd_rb_root = RB_ROOT;
6697 rwlock_init(&llsd->llsd_rb_lock);
6698 com->lc_data = llsd;
6700 com->lc_file_size = sizeof(*lo);
6701 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6702 if (com->lc_file_ram == NULL)
6703 GOTO(out, rc = -ENOMEM);
6705 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6706 if (com->lc_file_disk == NULL)
6707 GOTO(out, rc = -ENOMEM);
6709 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6711 GOTO(out, rc = PTR_ERR(root));
6713 if (unlikely(!dt_try_as_dir(env, root)))
6714 GOTO(out, rc = -ENOTDIR);
6716 obj = local_file_find_or_create(env, lfsck->li_los, root,
6718 S_IFREG | S_IRUGO | S_IWUSR);
6720 GOTO(out, rc = PTR_ERR(obj));
6723 rc = lfsck_layout_load(env, com);
6725 rc = lfsck_layout_reset(env, com, true);
6726 else if (rc == -ENOENT)
6727 rc = lfsck_layout_init(env, com);
6728 else if (lfsck->li_master)
6729 rc = lfsck_load_sub_trace_files(env, com,
6730 &dt_lfsck_layout_dangling_features,
6731 LFSCK_LAYOUT, false);
6736 lo = com->lc_file_ram;
6737 switch (lo->ll_status) {
6743 spin_lock(&lfsck->li_lock);
6744 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6745 spin_unlock(&lfsck->li_lock);
6748 CERROR("%s: unknown lfsck_layout status %d\n",
6749 lfsck_lfsck2name(lfsck), lo->ll_status);
6751 case LS_SCANNING_PHASE1:
6752 case LS_SCANNING_PHASE2:
6753 /* No need to store the status to disk right now.
6754 * If the system crashed before the status stored,
6755 * it will be loaded back when next time. */
6756 lo->ll_status = LS_CRASHED;
6757 if (!lfsck->li_master)
6758 lo->ll_flags |= LF_INCOMPLETE;
6765 spin_lock(&lfsck->li_lock);
6766 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6767 spin_unlock(&lfsck->li_lock);
6771 if (lo->ll_flags & LF_CRASHED_LASTID) {
6772 LASSERT(lfsck->li_out_notify != NULL);
6774 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6775 LE_LASTID_REBUILDING);
6781 if (root != NULL && !IS_ERR(root))
6782 lfsck_object_put(env, root);
6785 lfsck_component_cleanup(env, com);
6786 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
6787 lfsck_lfsck2name(lfsck), rc);
6793 struct lfsck_orphan_it {
6794 struct lfsck_component *loi_com;
6795 struct lfsck_rbtree_node *loi_lrn;
6796 struct lfsck_layout_slave_target *loi_llst;
6797 struct lu_fid loi_key;
6798 struct lu_orphan_rec_v2 loi_rec;
6800 unsigned int loi_over:1;
6803 static int lfsck_fid_match_idx(const struct lu_env *env,
6804 struct lfsck_instance *lfsck,
6805 const struct lu_fid *fid, int idx)
6807 struct seq_server_site *ss;
6808 struct lu_server_fld *sf;
6809 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
6812 /* All abnormal cases will be returned to MDT0. */
6813 if (!fid_is_norm(fid)) {
6820 ss = lfsck_dev_site(lfsck);
6821 if (unlikely(ss == NULL))
6824 sf = ss->ss_server_fld;
6825 LASSERT(sf != NULL);
6827 fld_range_set_any(range);
6828 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
6832 if (!fld_range_is_mdt(range))
6835 if (range->lsr_index == idx)
6841 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
6842 struct dt_object *obj)
6844 struct dt_device *dev = lfsck_obj2dev(obj);
6845 struct thandle *handle;
6849 handle = dt_trans_create(env, dev);
6853 rc = dt_declare_ref_del(env, obj, handle);
6857 rc = dt_declare_destroy(env, obj, handle);
6861 rc = dt_trans_start_local(env, dev, handle);
6865 dt_write_lock(env, obj, 0);
6866 rc = dt_ref_del(env, obj, handle);
6868 rc = dt_destroy(env, obj, handle);
6869 dt_write_unlock(env, obj);
6874 dt_trans_stop(env, dev, handle);
6876 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
6877 PFID(lfsck_dto2fid(obj)), rc);
6882 static int lfsck_orphan_index_lookup(const struct lu_env *env,
6883 struct dt_object *dt,
6885 const struct dt_key *key)
6890 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
6891 struct dt_object *dt,
6892 const struct dt_rec *rec,
6893 const struct dt_key *key,
6894 struct thandle *handle)
6899 static int lfsck_orphan_index_insert(const struct lu_env *env,
6900 struct dt_object *dt,
6901 const struct dt_rec *rec,
6902 const struct dt_key *key,
6903 struct thandle *handle,
6909 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
6910 struct dt_object *dt,
6911 const struct dt_key *key,
6912 struct thandle *handle)
6917 static int lfsck_orphan_index_delete(const struct lu_env *env,
6918 struct dt_object *dt,
6919 const struct dt_key *key,
6920 struct thandle *handle)
6925 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
6926 struct dt_object *dt,
6929 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
6930 struct lfsck_instance *lfsck;
6931 struct lfsck_component *com = NULL;
6932 struct lfsck_layout_slave_data *llsd;
6933 struct lfsck_orphan_it *it = NULL;
6934 struct lfsck_layout *lo;
6938 lfsck = lfsck_instance_find(dev, true, false);
6939 if (unlikely(lfsck == NULL))
6940 RETURN(ERR_PTR(-ENXIO));
6942 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
6943 if (unlikely(com == NULL))
6944 GOTO(out, rc = -ENOENT);
6946 lo = com->lc_file_ram;
6947 if (lo->ll_flags & LF_INCOMPLETE)
6948 GOTO(out, rc = -ESRCH);
6950 llsd = com->lc_data;
6951 if (!llsd->llsd_rbtree_valid)
6952 GOTO(out, rc = -ESRCH);
6956 GOTO(out, rc = -ENOMEM);
6958 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
6959 if (it->loi_llst == NULL)
6960 GOTO(out, rc = -ENXIO);
6962 if (dev->dd_record_fid_accessed) {
6963 /* The first iteration against the rbtree, scan the whole rbtree
6964 * to remove the nodes which do NOT need to be handled. */
6965 write_lock(&llsd->llsd_rb_lock);
6966 if (dev->dd_record_fid_accessed) {
6967 struct rb_node *node;
6968 struct rb_node *next;
6969 struct lfsck_rbtree_node *lrn;
6971 /* No need to record the fid accessing anymore. */
6972 dev->dd_record_fid_accessed = 0;
6974 node = rb_first(&llsd->llsd_rb_root);
6975 while (node != NULL) {
6976 next = rb_next(node);
6977 lrn = rb_entry(node, struct lfsck_rbtree_node,
6979 if (atomic_read(&lrn->lrn_known_count) <=
6980 atomic_read(&lrn->lrn_accessed_count)) {
6981 rb_erase(node, &llsd->llsd_rb_root);
6982 lfsck_rbtree_free(lrn);
6987 write_unlock(&llsd->llsd_rb_lock);
6990 /* read lock the rbtree when init, and unlock when fini */
6991 read_lock(&llsd->llsd_rb_lock);
6999 lfsck_component_put(env, com);
7001 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
7002 lfsck_lfsck2name(lfsck), rc);
7004 lfsck_instance_put(env, lfsck);
7009 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7012 return (struct dt_it *)it;
7015 static void lfsck_orphan_it_fini(const struct lu_env *env,
7018 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7019 struct lfsck_component *com = it->loi_com;
7020 struct lfsck_layout_slave_data *llsd;
7021 struct lfsck_layout_slave_target *llst;
7024 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7025 lfsck_lfsck2name(com->lc_lfsck));
7027 llsd = com->lc_data;
7028 read_unlock(&llsd->llsd_rb_lock);
7029 llst = it->loi_llst;
7030 LASSERT(llst != NULL);
7032 /* Save the key and hash for iterate next. */
7033 llst->llst_fid = it->loi_key;
7034 llst->llst_hash = it->loi_hash;
7035 lfsck_layout_llst_put(llst);
7036 lfsck_component_put(env, com);
7042 * \retval +1: the iteration finished
7043 * \retval 0: on success, not finished
7044 * \retval -ve: on error
7046 static int lfsck_orphan_it_next(const struct lu_env *env,
7049 struct lfsck_thread_info *info = lfsck_env_info(env);
7050 struct filter_fid *ff = &info->lti_ff;
7051 struct lu_attr *la = &info->lti_la;
7052 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7053 struct lu_fid *key = &it->loi_key;
7054 struct lu_orphan_rec_v2 *rec = &it->loi_rec;
7055 struct ost_layout *ol = &rec->lor_layout;
7056 struct lfsck_component *com = it->loi_com;
7057 struct lfsck_instance *lfsck = com->lc_lfsck;
7058 struct lfsck_layout_slave_data *llsd = com->lc_data;
7059 struct dt_object *obj;
7060 struct lfsck_rbtree_node *lrn;
7064 __u32 idx = it->loi_llst->llst_index;
7074 lrn = lfsck_rbtree_search(llsd, key, &exact);
7082 key->f_seq = lrn->lrn_seq;
7083 key->f_oid = lrn->lrn_first_oid;
7088 if (unlikely(key->f_oid == 0)) {
7095 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7101 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7102 atomic_read(&lrn->lrn_accessed_count))) {
7103 struct rb_node *next = rb_next(&lrn->lrn_node);
7105 while (next != NULL) {
7106 lrn = rb_entry(next, struct lfsck_rbtree_node,
7108 if (atomic_read(&lrn->lrn_known_count) >
7109 atomic_read(&lrn->lrn_accessed_count))
7111 next = rb_next(next);
7120 key->f_seq = lrn->lrn_seq;
7121 key->f_oid = lrn->lrn_first_oid;
7125 pos = key->f_oid - lrn->lrn_first_oid;
7128 pos = find_next_bit(lrn->lrn_known_bitmap,
7129 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7130 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7131 key->f_oid = lrn->lrn_first_oid + pos;
7132 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7140 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7145 key->f_oid = lrn->lrn_first_oid + pos;
7146 obj = lfsck_object_find_bottom(env, lfsck, key);
7149 if (rc == -ENOENT) {
7156 dt_read_lock(env, obj, 0);
7157 if (dt_object_exists(obj) == 0 ||
7158 lfsck_is_dead_obj(obj)) {
7159 dt_read_unlock(env, obj);
7160 lfsck_object_put(env, obj);
7165 rc = dt_attr_get(env, obj, la);
7169 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7171 if (rc == -ENODATA) {
7172 /* For the pre-created OST-object, update the bitmap to avoid
7173 * others LFSCK (second phase) iteration to touch it again. */
7174 if (la->la_ctime == 0) {
7175 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7176 atomic_inc(&lrn->lrn_accessed_count);
7178 /* For the race between repairing dangling referenced
7179 * MDT-object and unlink the file, it may left orphan
7180 * OST-object there. Destroy it now! */
7181 if (unlikely(!(la->la_mode & S_ISUID))) {
7182 dt_read_unlock(env, obj);
7183 lfsck_layout_destroy_orphan(env, obj);
7184 lfsck_object_put(env, obj);
7188 } else if (idx == 0) {
7189 /* If the orphan OST-object has no parent information,
7190 * regard it as referenced by the MDT-object on MDT0. */
7191 fid_zero(&rec->lor_rec.lor_fid);
7192 rec->lor_rec.lor_uid = la->la_uid;
7193 rec->lor_rec.lor_gid = la->la_gid;
7194 memset(ol, 0, sizeof(*ol));
7199 dt_read_unlock(env, obj);
7200 lfsck_object_put(env, obj);
7205 if (rc < sizeof(struct lu_fid))
7206 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7208 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7209 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7210 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7211 * parent MDT-object's layout EA. */
7212 save = rec->lor_rec.lor_fid.f_stripe_idx;
7213 rec->lor_rec.lor_fid.f_ver = 0;
7214 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7215 /* If the orphan OST-object does not claim the MDT, then next.
7217 * If we do not know whether it matches or not, then return it
7218 * to the MDT for further check. */
7220 dt_read_unlock(env, obj);
7221 lfsck_object_put(env, obj);
7226 rec->lor_rec.lor_fid.f_stripe_idx = save;
7227 rec->lor_rec.lor_uid = la->la_uid;
7228 rec->lor_rec.lor_gid = la->la_gid;
7229 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7231 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7232 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7233 "COMP end %llu\n", lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7234 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7235 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7236 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end);
7241 dt_read_unlock(env, obj);
7242 lfsck_object_put(env, obj);
7250 * \retval +1: locate to the exactly position
7251 * \retval 0: cannot locate to the exactly position,
7252 * call next() to move to a valid position.
7253 * \retval -ve: on error
7255 static int lfsck_orphan_it_get(const struct lu_env *env,
7257 const struct dt_key *key)
7259 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7262 it->loi_key = *(struct lu_fid *)key;
7263 rc = lfsck_orphan_it_next(env, di);
7273 static void lfsck_orphan_it_put(const struct lu_env *env,
7278 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7279 const struct dt_it *di)
7281 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7283 return (struct dt_key *)&it->loi_key;
7286 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7287 const struct dt_it *di)
7289 return sizeof(struct lu_fid);
7292 static int lfsck_orphan_it_rec(const struct lu_env *env,
7293 const struct dt_it *di,
7297 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7299 *(struct lu_orphan_rec_v2 *)rec = it->loi_rec;
7304 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7305 const struct dt_it *di)
7307 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7309 return it->loi_hash;
7313 * \retval +1: locate to the exactly position
7314 * \retval 0: cannot locate to the exactly position,
7315 * call next() to move to a valid position.
7316 * \retval -ve: on error
7318 static int lfsck_orphan_it_load(const struct lu_env *env,
7319 const struct dt_it *di,
7322 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7323 struct lfsck_layout_slave_target *llst = it->loi_llst;
7326 LASSERT(llst != NULL);
7328 if (hash != llst->llst_hash) {
7329 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7330 "iteration does not match the one when fini "
7331 "%llu, to be reset.\n",
7332 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7334 fid_zero(&llst->llst_fid);
7335 llst->llst_hash = 0;
7338 it->loi_key = llst->llst_fid;
7339 it->loi_hash = llst->llst_hash;
7340 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7350 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7351 const struct dt_it *di,
7357 const struct dt_index_operations lfsck_orphan_index_ops = {
7358 .dio_lookup = lfsck_orphan_index_lookup,
7359 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7360 .dio_insert = lfsck_orphan_index_insert,
7361 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7362 .dio_delete = lfsck_orphan_index_delete,
7364 .init = lfsck_orphan_it_init,
7365 .fini = lfsck_orphan_it_fini,
7366 .get = lfsck_orphan_it_get,
7367 .put = lfsck_orphan_it_put,
7368 .next = lfsck_orphan_it_next,
7369 .key = lfsck_orphan_it_key,
7370 .key_size = lfsck_orphan_it_key_size,
7371 .rec = lfsck_orphan_it_rec,
7372 .store = lfsck_orphan_it_store,
7373 .load = lfsck_orphan_it_load,
7374 .key_rec = lfsck_orphan_it_key_rec,