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, 2017, 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 <lu_object.h>
40 #include <dt_object.h>
41 #include <lustre_fid.h>
42 #include <lustre_lib.h>
43 #include <lustre_net.h>
44 #include <md_object.h>
45 #include <obd_class.h>
47 #include "lfsck_internal.h"
49 #define LFSCK_LAYOUT_MAGIC_V1 0xB173AE14
50 #define LFSCK_LAYOUT_MAGIC_V2 0xB1734D76
51 #define LFSCK_LAYOUT_MAGIC_V3 0xB17371B9
52 #define LFSCK_LAYOUT_MAGIC_V4 0xB1732FED
54 #define LFSCK_LAYOUT_MAGIC LFSCK_LAYOUT_MAGIC_V4
56 struct lfsck_layout_seq {
57 struct list_head lls_list;
60 __u64 lls_lastid_known;
61 struct dt_object *lls_lastid_obj;
62 unsigned int lls_dirty:1;
65 struct lfsck_layout_slave_target {
66 /* link into lfsck_layout_slave_data::llsd_master_list. */
67 struct list_head llst_list;
68 /* The position for next record in the rbtree for iteration. */
69 struct lu_fid llst_fid;
70 /* Dummy hash for iteration against the rbtree. */
75 /* How many times we have failed to get the master status. */
79 struct lfsck_layout_slave_data {
80 /* list for lfsck_layout_seq */
81 struct list_head llsd_seq_list;
83 /* list for the masters involve layout verification. */
84 struct list_head llsd_master_list;
87 struct dt_object *llsd_rb_obj;
88 struct rb_root llsd_rb_root;
89 struct rw_semaphore llsd_rb_rwsem;
90 unsigned int llsd_rbtree_valid:1;
93 struct lfsck_layout_slave_async_args {
94 struct obd_export *llsaa_exp;
95 struct lfsck_component *llsaa_com;
96 struct lfsck_layout_slave_target *llsaa_llst;
99 static inline bool lfsck_comp_extent_aligned(__u64 size)
101 return (size & (LOV_MIN_STRIPE_SIZE - 1)) == 0;
105 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
107 if (atomic_dec_and_test(&llst->llst_ref)) {
108 LASSERT(list_empty(&llst->llst_list));
115 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
117 struct lfsck_layout_slave_target *llst;
118 struct lfsck_layout_slave_target *tmp;
125 INIT_LIST_HEAD(&llst->llst_list);
127 llst->llst_index = index;
128 atomic_set(&llst->llst_ref, 1);
130 spin_lock(&llsd->llsd_lock);
131 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
132 if (tmp->llst_index == index) {
138 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
139 spin_unlock(&llsd->llsd_lock);
148 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
149 struct lfsck_layout_slave_target *llst)
153 spin_lock(&llsd->llsd_lock);
154 if (!list_empty(&llst->llst_list)) {
155 list_del_init(&llst->llst_list);
158 spin_unlock(&llsd->llsd_lock);
161 lfsck_layout_llst_put(llst);
164 static inline struct lfsck_layout_slave_target *
165 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
166 __u32 index, bool unlink)
168 struct lfsck_layout_slave_target *llst;
170 spin_lock(&llsd->llsd_lock);
171 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
172 if (llst->llst_index == index) {
174 list_del_init(&llst->llst_list);
176 atomic_inc(&llst->llst_ref);
177 spin_unlock(&llsd->llsd_lock);
182 spin_unlock(&llsd->llsd_lock);
187 static struct lfsck_layout_req *
188 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
189 struct dt_object *child, __u32 comp_id,
190 __u32 ost_idx, __u32 lov_idx)
192 struct lfsck_layout_req *llr;
196 return ERR_PTR(-ENOMEM);
198 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
199 llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
200 llr->llr_child = child;
201 llr->llr_comp_id = comp_id;
202 llr->llr_ost_idx = ost_idx;
203 llr->llr_lov_idx = lov_idx;
208 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
209 struct lfsck_assistant_req *lar)
211 struct lfsck_layout_req *llr =
212 container_of0(lar, struct lfsck_layout_req, llr_lar);
214 lfsck_object_put(env, llr->llr_child);
215 lfsck_assistant_object_put(env, lar->lar_parent);
220 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
221 struct ptlrpc_request *req,
225 struct lfsck_async_interpret_args *laia = args;
226 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
228 ltd->ltd_synced_failures = 1;
229 atomic_dec(laia->laia_count);
236 * Notify remote LFSCK instances about former failures.
238 * The local LFSCK instance has recorded which OSTs have ever failed to respond
239 * some LFSCK verification requests (maybe because of network issues or the OST
240 * itself trouble). During the respond gap, the OST may missed some OST-objects
241 * verification, then the OST cannot know whether related OST-objects have been
242 * referenced by related MDT-objects or not, then in the second-stage scanning,
243 * these OST-objects will be regarded as orphan, if the OST-object contains bad
244 * parent FID for back reference, then it will misguide the LFSCK to make wrong
245 * fixing for the fake orphan.
247 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
248 * it will scan the bitmap for the ever failed OSTs, and notify them that they
249 * have ever missed some OST-object verification and should skip the handling
250 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
252 * \param[in] env pointer to the thread context
253 * \param[in] com pointer to the lfsck component
254 * \param[in] lr pointer to the lfsck request
256 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
257 struct lfsck_component *com,
258 struct lfsck_request *lr)
260 struct lfsck_async_interpret_args *laia =
261 &lfsck_env_info(env)->lti_laia2;
262 struct lfsck_assistant_data *lad = com->lc_data;
263 struct lfsck_layout *lo = com->lc_file_ram;
264 struct lfsck_instance *lfsck = com->lc_lfsck;
265 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
266 struct lfsck_tgt_desc *ltd;
267 struct ptlrpc_request_set *set;
273 if (!test_bit(LAD_INCOMPLETE, &lad->lad_flags))
276 /* If the MDT has ever failed to verfiy some OST-objects,
277 * then sync failures with them firstly. */
278 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
280 atomic_set(&count, 0);
281 memset(laia, 0, sizeof(*laia));
282 laia->laia_count = &count;
283 set = ptlrpc_prep_set();
285 GOTO(out, rc = -ENOMEM);
287 down_read(<ds->ltd_rw_sem);
288 cfs_foreach_bit(lad->lad_bitmap, idx) {
289 ltd = lfsck_ltd2tgt(ltds, idx);
293 laia->laia_ltd = ltd;
294 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
295 lfsck_layout_assistant_sync_failures_interpret,
298 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
299 "notify target %x for %s phase1 done: "
300 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
301 ltd->ltd_index, lad->lad_name, rc);
308 up_read(<ds->ltd_rw_sem);
310 if (rc == 0 && atomic_read(&count) > 0)
311 rc = ptlrpc_set_wait(env, set);
313 ptlrpc_set_destroy(set);
315 if (rc == 0 && atomic_read(&count) > 0)
322 /* If failed to sync failures with the OSTs, then have to
323 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
324 * subsequent orphan OST-object handling. */
325 lo->ll_flags |= LF_INCOMPLETE;
327 lr->lr_flags2 = lo->ll_flags;
330 static int lfsck_layout_verify_header_v1v3(struct dt_object *obj,
331 struct lov_mds_md_v1 *lmm,
332 __u64 start, __u32 comp_id)
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);
359 /* XXX: DoM file verification will be supportted via LU-11081. */
360 if (lov_pattern(pattern) == LOV_PATTERN_MDT) {
362 CDEBUG(D_LFSCK, "The DoM entry for "DFID" is not "
363 "the first component in the mirror %x/%llu\n",
364 PFID(lfsck_dto2fid(obj)), comp_id, start);
371 if (!lov_pattern_supported_normal_comp(lov_pattern(pattern))) {
372 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
373 DFID" in the component %x\n",
374 pattern, PFID(lfsck_dto2fid(obj)), comp_id);
382 static int lfsck_layout_verify_header_foreign(struct dt_object *obj,
383 struct lov_foreign_md *lfm,
386 /* magic has been verified already */
387 __u32 value_len = le32_to_cpu(lfm->lfm_length);
388 /* type and flags are not checked for instance */
390 CDEBUG(D_INFO, "foreign LOV EA, magic %x, len %u, type %x, flags %x, for file "DFID"\n",
391 le32_to_cpu(lfm->lfm_magic), value_len,
392 le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
393 PFID(lfsck_dto2fid(obj)));
395 if (len != value_len + offsetof(typeof(*lfm), lfm_value))
396 CDEBUG(D_LFSCK, "foreign LOV EA internal size %u does not match EA full size %zu for file "DFID"\n",
397 value_len, len, PFID(lfsck_dto2fid(obj)));
399 /* nothing to repair */
403 static int lfsck_layout_verify_header(struct dt_object *obj,
404 struct lov_mds_md_v1 *lmm, size_t len)
408 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
409 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
411 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
413 if (unlikely(count == 0)) {
414 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
415 "components count 0\n",
416 PFID(lfsck_dto2fid(obj)));
421 for (i = 0; i < count && !rc; i++) {
422 struct lov_comp_md_entry_v1 *lcme =
423 &lcm->lcm_entries[i];
424 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
425 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
426 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
428 if (unlikely(comp_id == LCME_ID_INVAL ||
429 comp_id > LCME_ID_MAX)) {
430 CDEBUG(D_LFSCK, "found invalid FPL ID %u "
431 "for the file "DFID" at idx %d\n",
432 comp_id, PFID(lfsck_dto2fid(obj)), i);
437 if (unlikely(start >= end ||
438 !lfsck_comp_extent_aligned(start) ||
439 (!lfsck_comp_extent_aligned(end) &&
440 end != LUSTRE_EOF))) {
441 CDEBUG(D_LFSCK, "found invalid FPL extent "
442 "range [%llu - %llu) for the file "
444 start, end, PFID(lfsck_dto2fid(obj)), i);
449 rc = lfsck_layout_verify_header_v1v3(obj,
450 (struct lov_mds_md_v1 *)((char *)lmm +
451 le32_to_cpu(lcme->lcme_offset)), start,
454 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_FOREIGN) {
455 rc = lfsck_layout_verify_header_foreign(obj,
456 (struct lov_foreign_md *)lmm,
459 rc = lfsck_layout_verify_header_v1v3(obj, lmm, 1, 0);
465 static int lfsck_layout_get_lovea(const struct lu_env *env,
466 struct dt_object *obj, struct lu_buf *buf)
472 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
474 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
476 return !rc ? -ENODATA : rc;
478 lu_buf_realloc(buf, rc);
479 if (buf->lb_buf == NULL)
486 return !rc ? -ENODATA : rc;
488 if (unlikely(buf->lb_buf == NULL)) {
489 lu_buf_alloc(buf, rc);
490 if (buf->lb_buf == NULL)
496 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf, rc);
498 return rc1 ? rc1 : rc;
501 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
502 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
503 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
505 struct lfsck_rbtree_node {
506 struct rb_node lrn_node;
509 atomic_t lrn_known_count;
510 atomic_t lrn_accessed_count;
511 void *lrn_known_bitmap;
512 void *lrn_accessed_bitmap;
515 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
516 __u64 seq, __u32 oid)
518 if (seq < lrn->lrn_seq)
521 if (seq > lrn->lrn_seq)
524 if (oid < lrn->lrn_first_oid)
527 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
533 /* The caller should hold llsd->llsd_rb_lock. */
534 static struct lfsck_rbtree_node *
535 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
536 const struct lu_fid *fid, bool *exact)
538 struct rb_node *node = llsd->llsd_rb_root.rb_node;
539 struct rb_node *prev = NULL;
540 struct lfsck_rbtree_node *lrn = NULL;
546 while (node != NULL) {
548 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
549 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
551 node = node->rb_left;
553 node = node->rb_right;
561 /* If there is no exactly matched one, then to the next valid one. */
564 /* The rbtree is empty. */
571 node = rb_next(prev);
573 /* The end of the rbtree. */
577 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
582 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
583 const struct lu_fid *fid)
585 struct lfsck_rbtree_node *lrn;
589 return ERR_PTR(-ENOMEM);
591 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
592 if (lrn->lrn_known_bitmap == NULL) {
595 return ERR_PTR(-ENOMEM);
598 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
599 if (lrn->lrn_accessed_bitmap == NULL) {
600 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
603 return ERR_PTR(-ENOMEM);
606 RB_CLEAR_NODE(&lrn->lrn_node);
607 lrn->lrn_seq = fid_seq(fid);
608 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
609 atomic_set(&lrn->lrn_known_count, 0);
610 atomic_set(&lrn->lrn_accessed_count, 0);
615 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
617 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
618 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
622 /* The caller should hold lock. */
623 static struct lfsck_rbtree_node *
624 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
625 struct lfsck_rbtree_node *lrn)
627 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
628 struct rb_node *parent = NULL;
629 struct lfsck_rbtree_node *tmp;
632 while (*pos != NULL) {
634 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
635 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
637 pos = &(*pos)->rb_left;
639 pos = &(*pos)->rb_right;
644 rb_link_node(&lrn->lrn_node, parent, pos);
645 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
650 static const struct dt_index_operations lfsck_orphan_index_ops;
652 static int lfsck_rbtree_setup(const struct lu_env *env,
653 struct lfsck_component *com)
655 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
656 struct lfsck_instance *lfsck = com->lc_lfsck;
657 struct dt_device *dev = lfsck->li_bottom;
658 struct lfsck_layout_slave_data *llsd = com->lc_data;
659 struct dt_object *obj;
661 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
662 fid->f_oid = lfsck_dev_idx(lfsck);
664 obj = dt_locate(env, dev, fid);
666 RETURN(PTR_ERR(obj));
668 /* Generate an in-RAM object to stand for the layout rbtree.
669 * Scanning the layout rbtree will be via the iteration over
670 * the object. In the future, the rbtree may be written onto
671 * disk with the object.
673 * Mark the object to be as exist. */
674 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
675 obj->do_index_ops = &lfsck_orphan_index_ops;
676 llsd->llsd_rb_obj = obj;
677 llsd->llsd_rbtree_valid = 1;
678 dev->dd_record_fid_accessed = 1;
680 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
681 lfsck_lfsck2name(lfsck));
686 static void lfsck_rbtree_cleanup(const struct lu_env *env,
687 struct lfsck_component *com)
689 struct lfsck_instance *lfsck = com->lc_lfsck;
690 struct lfsck_layout_slave_data *llsd = com->lc_data;
691 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
692 struct rb_node *next;
693 struct lfsck_rbtree_node *lrn;
695 lfsck->li_bottom->dd_record_fid_accessed = 0;
696 /* Invalid the rbtree, then no others will use it. */
697 down_write(&llsd->llsd_rb_rwsem);
698 llsd->llsd_rbtree_valid = 0;
699 up_write(&llsd->llsd_rb_rwsem);
701 while (node != NULL) {
702 next = rb_next(node);
703 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
704 rb_erase(node, &llsd->llsd_rb_root);
705 lfsck_rbtree_free(lrn);
709 if (llsd->llsd_rb_obj != NULL) {
710 lfsck_object_put(env, llsd->llsd_rb_obj);
711 llsd->llsd_rb_obj = NULL;
714 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
715 lfsck_lfsck2name(lfsck));
718 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
719 struct lfsck_component *com,
720 const struct lu_fid *fid,
723 struct lfsck_layout_slave_data *llsd = com->lc_data;
724 struct lfsck_rbtree_node *lrn;
730 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
733 if (!fid_is_idif(fid) && !fid_is_norm(fid))
736 down_read(&llsd->llsd_rb_rwsem);
737 if (!llsd->llsd_rbtree_valid)
738 GOTO(unlock, rc = 0);
740 lrn = lfsck_rbtree_search(llsd, fid, NULL);
742 struct lfsck_rbtree_node *tmp;
746 up_read(&llsd->llsd_rb_rwsem);
747 tmp = lfsck_rbtree_new(env, fid);
749 GOTO(out, rc = PTR_ERR(tmp));
752 down_write(&llsd->llsd_rb_rwsem);
753 if (!llsd->llsd_rbtree_valid) {
754 lfsck_rbtree_free(tmp);
755 GOTO(unlock, rc = 0);
758 lrn = lfsck_rbtree_insert(llsd, tmp);
760 lfsck_rbtree_free(tmp);
763 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
764 /* Any accessed object must be a known object. */
765 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
766 atomic_inc(&lrn->lrn_known_count);
767 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
768 atomic_inc(&lrn->lrn_accessed_count);
770 GOTO(unlock, rc = 0);
774 up_write(&llsd->llsd_rb_rwsem);
776 up_read(&llsd->llsd_rb_rwsem);
778 if (rc != 0 && accessed) {
779 struct lfsck_layout *lo = com->lc_file_ram;
781 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
782 "bitmap, and will cause incorrect LFSCK OST-object "
783 "handling, so disable it to cancel orphan handling "
784 "for related device. rc = %d\n",
785 lfsck_lfsck2name(com->lc_lfsck), rc);
787 lo->ll_flags |= LF_INCOMPLETE;
788 lfsck_rbtree_cleanup(env, com);
792 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
793 const struct lfsck_layout_dangling_key *src)
795 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
796 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
797 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
800 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
801 const struct lfsck_layout_dangling_key *src)
803 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
804 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
805 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
808 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
809 const struct lfsck_layout_dangling_key *src)
811 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
812 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
813 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
816 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
817 const struct lfsck_layout_dangling_key *src)
819 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
820 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
821 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
824 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
825 const struct lfsck_layout *src)
829 des->ll_magic = le32_to_cpu(src->ll_magic);
830 des->ll_status = le32_to_cpu(src->ll_status);
831 des->ll_flags = le32_to_cpu(src->ll_flags);
832 des->ll_success_count = le32_to_cpu(src->ll_success_count);
833 des->ll_run_time_phase1 = le64_to_cpu(src->ll_run_time_phase1);
834 des->ll_run_time_phase2 = le64_to_cpu(src->ll_run_time_phase2);
835 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
836 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
837 des->ll_time_last_checkpoint =
838 le64_to_cpu(src->ll_time_last_checkpoint);
839 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
840 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
841 des->ll_pos_first_inconsistent =
842 le64_to_cpu(src->ll_pos_first_inconsistent);
843 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
844 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
845 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
846 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
847 for (i = 0; i < LLIT_MAX; i++)
848 des->ll_objs_repaired[i] =
849 le64_to_cpu(src->ll_objs_repaired[i]);
850 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
851 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
852 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
853 &src->ll_lldk_latest_scanned_phase2);
856 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
857 const struct lfsck_layout *src)
861 des->ll_magic = cpu_to_le32(src->ll_magic);
862 des->ll_status = cpu_to_le32(src->ll_status);
863 des->ll_flags = cpu_to_le32(src->ll_flags);
864 des->ll_success_count = cpu_to_le32(src->ll_success_count);
865 des->ll_run_time_phase1 = cpu_to_le64(src->ll_run_time_phase1);
866 des->ll_run_time_phase2 = cpu_to_le64(src->ll_run_time_phase2);
867 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
868 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
869 des->ll_time_last_checkpoint =
870 cpu_to_le64(src->ll_time_last_checkpoint);
871 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
872 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
873 des->ll_pos_first_inconsistent =
874 cpu_to_le64(src->ll_pos_first_inconsistent);
875 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
876 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
877 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
878 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
879 for (i = 0; i < LLIT_MAX; i++)
880 des->ll_objs_repaired[i] =
881 cpu_to_le64(src->ll_objs_repaired[i]);
882 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
883 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
884 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
885 &src->ll_lldk_latest_scanned_phase2);
889 * Load the OST bitmap from the lfsck_layout trace file.
891 * \param[in] env pointer to the thread context
892 * \param[in] com pointer to the lfsck component
894 * \retval 0 for success
895 * \retval negative error number on failure or data corruption
897 static int lfsck_layout_load_bitmap(const struct lu_env *env,
898 struct lfsck_component *com)
900 struct dt_object *obj = com->lc_obj;
901 struct lfsck_assistant_data *lad = com->lc_data;
902 struct lfsck_layout *lo = com->lc_file_ram;
903 struct cfs_bitmap *bitmap = lad->lad_bitmap;
904 loff_t pos = com->lc_file_size;
910 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
912 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
914 nbits = lo->ll_bitmap_size;
916 if (unlikely(nbits < BITS_PER_LONG))
917 nbits = BITS_PER_LONG;
919 if (nbits > bitmap->size) {
920 __u32 new_bits = bitmap->size;
921 struct cfs_bitmap *new_bitmap;
923 while (new_bits < nbits)
926 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
927 if (new_bitmap == NULL)
930 lad->lad_bitmap = new_bitmap;
931 CFS_FREE_BITMAP(bitmap);
935 if (lo->ll_bitmap_size == 0) {
936 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
937 CFS_RESET_BITMAP(bitmap);
942 size = (lo->ll_bitmap_size + 7) >> 3;
943 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
945 RETURN(rc >= 0 ? -EINVAL : rc);
947 if (cfs_bitmap_check_empty(bitmap))
948 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
950 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
956 * Load the layout LFSCK trace file from disk.
958 * The layout LFSCK trace file records the layout LFSCK status information
959 * and other statistics, such as how many objects have been scanned, and how
960 * many objects have been repaired, and etc. It also contains the bitmap for
961 * failed OSTs during the layout LFSCK. All these information will be loaded
962 * from disk to RAM when the layout LFSCK component setup.
964 * \param[in] env pointer to the thread context
965 * \param[in] com pointer to the lfsck component
967 * \retval positive number for file data corruption, the caller
968 * should reset the layout LFSCK trace file
969 * \retval 0 for success
970 * \retval negative error number on failure
972 static int lfsck_layout_load(const struct lu_env *env,
973 struct lfsck_component *com)
975 struct lfsck_layout *lo = com->lc_file_ram;
976 ssize_t size = com->lc_file_size;
980 rc = dt_read(env, com->lc_obj,
981 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
985 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
986 lfsck_lfsck2name(com->lc_lfsck), rc);
988 } else if (rc != size) {
989 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
990 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
994 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
995 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
996 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
997 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
998 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
1006 * Store the layout LFSCK trace file on disk.
1008 * The layout LFSCK trace file records the layout LFSCK status information
1009 * and other statistics, such as how many objects have been scanned, and how
1010 * many objects have been repaired, and etc. It also contains the bitmap for
1011 * failed OSTs during the layout LFSCK. All these information will be synced
1012 * from RAM to disk periodically.
1014 * \param[in] env pointer to the thread context
1015 * \param[in] com pointer to the lfsck component
1017 * \retval 0 for success
1018 * \retval negative error number on failure
1020 static int lfsck_layout_store(const struct lu_env *env,
1021 struct lfsck_component *com)
1023 struct dt_object *obj = com->lc_obj;
1024 struct lfsck_instance *lfsck = com->lc_lfsck;
1025 struct lfsck_layout *lo_ram = com->lc_file_ram;
1026 struct lfsck_layout *lo = com->lc_file_disk;
1028 struct dt_device *dev = lfsck_obj2dev(obj);
1029 struct cfs_bitmap *bitmap = NULL;
1031 ssize_t size = com->lc_file_size;
1036 if (lfsck->li_master) {
1037 struct lfsck_assistant_data *lad = com->lc_data;
1039 bitmap = lad->lad_bitmap;
1040 nbits = bitmap->size;
1043 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1046 lo_ram->ll_bitmap_size = nbits;
1047 lfsck_layout_cpu_to_le(lo, lo_ram);
1048 th = dt_trans_create(env, dev);
1050 GOTO(log, rc = PTR_ERR(th));
1052 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1057 if (bitmap != NULL) {
1058 rc = dt_declare_record_write(env, obj,
1059 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1065 rc = dt_trans_start_local(env, dev, th);
1070 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1074 if (bitmap != NULL) {
1076 rc = dt_record_write(env, obj,
1077 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1084 dt_trans_stop(env, dev, th);
1088 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1089 lfsck_lfsck2name(lfsck), rc);
1094 static int lfsck_layout_init(const struct lu_env *env,
1095 struct lfsck_component *com)
1097 struct lfsck_layout *lo = com->lc_file_ram;
1100 memset(lo, 0, com->lc_file_size);
1101 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1102 lo->ll_status = LS_INIT;
1103 down_write(&com->lc_sem);
1104 rc = lfsck_layout_store(env, com);
1105 if (rc == 0 && com->lc_lfsck->li_master)
1106 rc = lfsck_load_sub_trace_files(env, com,
1107 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1108 up_write(&com->lc_sem);
1113 static int fid_is_for_ostobj(const struct lu_env *env,
1114 struct lfsck_instance *lfsck,
1115 struct dt_object *obj, const struct lu_fid *fid)
1117 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1118 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1119 struct lustre_ost_attrs *loa;
1122 fld_range_set_any(range);
1123 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1125 if (fld_range_is_ost(range))
1131 loa = &lfsck_env_info(env)->lti_loa;
1132 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1134 if (rc >= (int)sizeof(struct lustre_mdt_attrs)) {
1135 lustre_lma_swab(&loa->loa_lma);
1137 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1140 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1145 static struct lfsck_layout_seq *
1146 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1148 struct lfsck_layout_seq *lls;
1150 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1151 if (lls->lls_seq == seq)
1154 if (lls->lls_seq > seq)
1162 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1163 struct lfsck_layout_seq *lls)
1165 struct lfsck_layout_seq *tmp;
1166 struct list_head *pos = &llsd->llsd_seq_list;
1168 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1169 if (lls->lls_seq < tmp->lls_seq) {
1170 pos = &tmp->lls_list;
1174 list_add_tail(&lls->lls_list, pos);
1178 lfsck_layout_lastid_create(const struct lu_env *env,
1179 struct lfsck_instance *lfsck,
1180 struct dt_object *obj)
1182 struct lfsck_thread_info *info = lfsck_env_info(env);
1183 struct lu_attr *la = &info->lti_la;
1184 struct dt_object_format *dof = &info->lti_dof;
1185 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1186 struct dt_device *dt = lfsck_obj2dev(obj);
1193 if (bk->lb_param & LPF_DRYRUN)
1196 memset(la, 0, sizeof(*la));
1197 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1198 la->la_valid = LA_MODE | LA_UID | LA_GID;
1199 memset(dof, 0, sizeof(*dof));
1200 dof->dof_type = dt_mode_to_dft(S_IFREG);
1202 th = dt_trans_create(env, dt);
1204 GOTO(log, rc = PTR_ERR(th));
1206 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1210 rc = dt_declare_record_write(env, obj,
1211 lfsck_buf_get(env, &lastid,
1217 rc = dt_trans_start_local(env, dt, th);
1221 dt_write_lock(env, obj, 0);
1222 if (likely(dt_object_exists(obj) == 0)) {
1223 rc = dt_create(env, obj, la, NULL, dof, th);
1225 rc = dt_record_write(env, obj,
1226 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1229 dt_write_unlock(env, obj);
1234 dt_trans_stop(env, dt, th);
1237 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1239 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1245 lfsck_layout_lastid_reload(const struct lu_env *env,
1246 struct lfsck_component *com,
1247 struct lfsck_layout_seq *lls)
1253 dt_read_lock(env, lls->lls_lastid_obj, 0);
1254 rc = dt_record_read(env, lls->lls_lastid_obj,
1255 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1256 dt_read_unlock(env, lls->lls_lastid_obj);
1257 if (unlikely(rc != 0))
1260 lastid = le64_to_cpu(lastid);
1261 if (lastid < lls->lls_lastid_known) {
1262 struct lfsck_instance *lfsck = com->lc_lfsck;
1263 struct lfsck_layout *lo = com->lc_file_ram;
1265 lls->lls_lastid = lls->lls_lastid_known;
1267 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1268 LASSERT(lfsck->li_out_notify != NULL);
1270 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1271 LE_LASTID_REBUILDING);
1272 lo->ll_flags |= LF_CRASHED_LASTID;
1274 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1275 "LAST_ID file (1) for the sequence %#llx"
1276 ", old value %llu, known value %llu\n",
1277 lfsck_lfsck2name(lfsck), lls->lls_seq,
1278 lastid, lls->lls_lastid);
1280 } else if (lastid >= lls->lls_lastid) {
1281 lls->lls_lastid = lastid;
1289 lfsck_layout_lastid_store(const struct lu_env *env,
1290 struct lfsck_component *com)
1292 struct lfsck_instance *lfsck = com->lc_lfsck;
1293 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1294 struct dt_device *dt = lfsck->li_bottom;
1295 struct lfsck_layout_slave_data *llsd = com->lc_data;
1296 struct lfsck_layout_seq *lls;
1302 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1305 if (!lls->lls_dirty)
1308 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1309 "<seq> %#llx as <oid> %llu\n",
1310 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1312 if (bk->lb_param & LPF_DRYRUN) {
1317 th = dt_trans_create(env, dt);
1320 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1321 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1322 lfsck_lfsck2name(com->lc_lfsck),
1327 lastid = cpu_to_le64(lls->lls_lastid);
1328 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1329 lfsck_buf_get(env, &lastid,
1335 rc = dt_trans_start_local(env, dt, th);
1339 dt_write_lock(env, lls->lls_lastid_obj, 0);
1340 rc = dt_record_write(env, lls->lls_lastid_obj,
1341 lfsck_buf_get(env, &lastid,
1342 sizeof(lastid)), &pos, th);
1343 dt_write_unlock(env, lls->lls_lastid_obj);
1348 dt_trans_stop(env, dt, th);
1351 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1352 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1353 lfsck_lfsck2name(com->lc_lfsck),
1362 lfsck_layout_lastid_load(const struct lu_env *env,
1363 struct lfsck_component *com,
1364 struct lfsck_layout_seq *lls)
1366 struct lfsck_instance *lfsck = com->lc_lfsck;
1367 struct lfsck_layout *lo = com->lc_file_ram;
1368 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1369 struct dt_object *obj;
1374 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1375 obj = dt_locate(env, lfsck->li_bottom, fid);
1377 RETURN(PTR_ERR(obj));
1379 /* LAST_ID crashed, to be rebuilt */
1380 if (dt_object_exists(obj) == 0) {
1381 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1382 LASSERT(lfsck->li_out_notify != NULL);
1384 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1385 LE_LASTID_REBUILDING);
1386 lo->ll_flags |= LF_CRASHED_LASTID;
1388 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1389 "LAST_ID file for sequence %#llx\n",
1390 lfsck_lfsck2name(lfsck), lls->lls_seq);
1392 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1394 struct ptlrpc_thread *thread =
1397 up_write(&com->lc_sem);
1398 wait_event_idle_timeout(
1399 thread->t_ctl_waitq,
1400 !thread_is_running(thread),
1401 cfs_time_seconds(cfs_fail_val));
1402 down_write(&com->lc_sem);
1406 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1408 dt_read_lock(env, obj, 0);
1409 rc = dt_read(env, obj,
1410 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1412 dt_read_unlock(env, obj);
1413 if (rc != 0 && rc != sizeof(__u64))
1414 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1416 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1417 LASSERT(lfsck->li_out_notify != NULL);
1419 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1420 LE_LASTID_REBUILDING);
1421 lo->ll_flags |= LF_CRASHED_LASTID;
1423 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1424 "LAST_ID file for the sequence %#llx"
1426 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1429 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1437 lfsck_object_put(env, obj);
1439 lls->lls_lastid_obj = obj;
1444 static void lfsck_layout_record_failure(const struct lu_env *env,
1445 struct lfsck_instance *lfsck,
1446 struct lfsck_layout *lo)
1450 lo->ll_objs_failed_phase1++;
1451 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1453 if (lo->ll_pos_first_inconsistent == 0 ||
1454 lo->ll_pos_first_inconsistent < cookie) {
1455 lo->ll_pos_first_inconsistent = cookie;
1457 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1458 "inconsistency at the pos [%llu]\n",
1459 lfsck_lfsck2name(lfsck),
1460 lo->ll_pos_first_inconsistent);
1464 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1465 struct lfsck_component *com,
1468 struct lfsck_instance *lfsck = com->lc_lfsck;
1469 struct lfsck_layout *lo = com->lc_file_ram;
1471 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan: rc = %d\n",
1472 lfsck_lfsck2name(lfsck), rc);
1474 down_write(&com->lc_sem);
1475 lo->ll_run_time_phase2 += ktime_get_seconds() -
1476 com->lc_time_last_checkpoint;
1477 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
1478 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1481 if (lo->ll_flags & LF_INCOMPLETE) {
1482 lo->ll_status = LS_PARTIAL;
1484 if (lfsck->li_master) {
1485 struct lfsck_assistant_data *lad = com->lc_data;
1487 if (test_bit(LAD_INCOMPLETE, &lad->lad_flags))
1488 lo->ll_status = LS_PARTIAL;
1490 lo->ll_status = LS_COMPLETED;
1492 lo->ll_status = LS_COMPLETED;
1495 lo->ll_flags &= ~LF_SCANNED_ONCE;
1496 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1497 lo->ll_flags &= ~LF_INCONSISTENT;
1498 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1499 lo->ll_success_count++;
1500 } else if (rc == 0) {
1501 if (lfsck->li_status != 0)
1502 lo->ll_status = lfsck->li_status;
1504 lo->ll_status = LS_STOPPED;
1506 lo->ll_status = LS_FAILED;
1509 rc = lfsck_layout_store(env, com);
1510 up_write(&com->lc_sem);
1512 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan result %u: rc = %d\n",
1513 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
1518 static int lfsck_layout_trans_stop(const struct lu_env *env,
1519 struct dt_device *dev,
1520 struct thandle *handle, int result)
1524 /* XXX: If there is something worng or it needs to repair nothing,
1525 * then notify the lower to stop the modification. Currently,
1526 * we use th_result for such purpose, that may be replaced by
1527 * some rollback mechanism in the future. */
1528 handle->th_result = result;
1529 rc = dt_trans_stop(env, dev, handle);
1531 return result > 0 ? 0 : result;
1533 return rc == 0 ? 1 : rc;
1536 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1537 struct lfsck_component *com,
1538 const struct lu_fid *pfid,
1539 const struct lu_fid *cfid,
1540 __u32 comp_id, __u32 ea_off,
1543 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1544 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1545 struct dt_device *dev;
1546 struct dt_object *obj;
1547 struct thandle *th = NULL;
1552 idx = lfsck_sub_trace_file_fid2idx(pfid);
1553 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1554 dev = lfsck_obj2dev(obj);
1556 fid_cpu_to_be(&key->lldk_fid, pfid);
1557 key->lldk_comp_id = cpu_to_be32(comp_id);
1558 key->lldk_ea_off = cpu_to_be32(ea_off);
1560 fid_cpu_to_be(rec, cfid);
1561 rec->f_ver = cpu_to_be32(ost_idx);
1563 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1565 th = dt_trans_create(env, dev);
1567 GOTO(unlock, rc = PTR_ERR(th));
1569 rc = dt_declare_insert(env, obj,
1570 (const struct dt_rec *)rec,
1571 (const struct dt_key *)key, th);
1575 rc = dt_trans_start_local(env, dev, th);
1579 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1580 (const struct dt_key *)key, th);
1585 if (th && !IS_ERR(th))
1586 dt_trans_stop(env, dev, th);
1588 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1590 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1591 "ea_off = %u, ost_idx = %u, into the trace file for further "
1592 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1593 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1598 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1599 struct lfsck_component *com,
1600 const struct lu_fid *fid,
1601 __u32 comp_id, __u32 ea_off)
1603 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1604 struct dt_device *dev;
1605 struct dt_object *obj;
1606 struct thandle *th = NULL;
1611 idx = lfsck_sub_trace_file_fid2idx(fid);
1612 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1613 dev = lfsck_obj2dev(obj);
1615 fid_cpu_to_be(&key->lldk_fid, fid);
1616 key->lldk_comp_id = cpu_to_be32(comp_id);
1617 key->lldk_ea_off = cpu_to_be32(ea_off);
1619 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1621 th = dt_trans_create(env, dev);
1623 GOTO(unlock, rc = PTR_ERR(th));
1625 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1629 rc = dt_trans_start_local(env, dev, th);
1633 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1638 if (th && !IS_ERR(th))
1639 dt_trans_stop(env, dev, th);
1641 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1643 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1644 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1645 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1651 * Get the system default stripe size.
1653 * \param[in] env pointer to the thread context
1654 * \param[in] lfsck pointer to the lfsck instance
1655 * \param[out] size pointer to the default stripe size
1657 * \retval 0 for success
1658 * \retval negative error number on failure
1660 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1661 struct lfsck_instance *lfsck,
1664 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1665 struct dt_object *root;
1668 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1670 return PTR_ERR(root);
1672 /* Get the default stripe size via xattr_get on the backend root. */
1673 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1676 /* The lum->lmm_stripe_size is LE mode. The *size also
1677 * should be LE mode. So it is unnecessary to convert. */
1678 *size = lum->lmm_stripe_size;
1680 } else if (unlikely(rc == 0)) {
1684 lfsck_object_put(env, root);
1690 * \retval +1: repaired
1691 * \retval 0: did nothing
1692 * \retval -ve: on error
1694 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1695 struct lfsck_instance *lfsck,
1696 struct thandle *handle,
1697 struct dt_object *parent,
1698 const struct lu_fid *cfid,
1700 struct lov_mds_md_v1 *lmm,
1701 struct lov_ost_data_v1 *slot,
1702 int fl, __u32 ost_idx, int size)
1704 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1705 struct lu_buf ea_buf;
1712 magic = le32_to_cpu(lmm->lmm_magic);
1713 pattern = le32_to_cpu(lmm->lmm_pattern);
1714 count = le16_to_cpu(lmm->lmm_stripe_count);
1716 fid_to_ostid(cfid, oi);
1717 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1718 slot->l_ost_gen = cpu_to_le32(0);
1719 slot->l_ost_idx = cpu_to_le32(ost_idx);
1721 if (pattern & LOV_PATTERN_F_HOLE) {
1722 struct lov_ost_data_v1 *objs;
1725 if (magic == LOV_MAGIC_V1)
1726 objs = &lmm->lmm_objects[0];
1728 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1729 for (i = 0; i < count; i++, objs++) {
1730 if (lovea_slot_is_dummy(objs))
1734 /* If the @slot is the last dummy slot to be refilled,
1735 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1738 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1740 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1741 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1742 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1746 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1747 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1754 static struct lov_ost_data_v1 *
1755 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1756 const struct lu_fid *pfid,
1757 __u32 stripe_size, __u32 ea_off,
1758 __u32 pattern, __u16 count)
1760 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1761 lmm->lmm_pattern = cpu_to_le32(pattern);
1762 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1763 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1764 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1765 lmm->lmm_stripe_count = cpu_to_le16(count);
1766 lmm->lmm_layout_gen = cpu_to_le16(1);
1767 memset(&lmm->lmm_objects[0], 0,
1768 sizeof(struct lov_ost_data_v1) * count);
1770 return &lmm->lmm_objects[ea_off];
1773 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1774 struct lfsck_instance *lfsck,
1775 struct ost_layout *ol,
1776 struct dt_object *parent,
1777 struct lu_buf *buf, __u32 ea_off,
1778 struct lov_mds_md_v1 **lmm,
1779 struct lov_ost_data_v1 **objs)
1782 __u32 stripe_size = ol->ol_stripe_size;
1783 __u32 pattern = LOV_PATTERN_RAID0;
1786 if (ol->ol_stripe_count != 0)
1787 count = ol->ol_stripe_count;
1791 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1792 LASSERTF(buf->lb_len >= size,
1793 "buffer len %d is less than real size %d\n",
1794 (int)buf->lb_len, size);
1796 if (stripe_size == 0) {
1799 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1805 if (ol->ol_stripe_count > 1 ||
1806 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1807 pattern |= LOV_PATTERN_F_HOLE;
1808 memset(&(*lmm)->lmm_objects[0], 0,
1809 count * sizeof(struct lov_ost_data_v1));
1812 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1813 stripe_size, ea_off, pattern, count);
1818 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1819 struct lu_orphan_rec_v3 *rec,
1820 struct dt_object *parent,
1821 struct lu_buf *buf, __u32 ea_off,
1822 struct lov_mds_md_v1 **lmm,
1823 struct lov_ost_data_v1 **objs)
1825 struct ost_layout *ol = &rec->lor_layout;
1826 struct lov_comp_md_v1 *lcm;
1827 struct lov_comp_md_entry_v1 *lcme;
1828 __u32 pattern = LOV_PATTERN_RAID0;
1829 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1830 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1831 int size = offset + lcme_size;
1833 LASSERTF(buf->lb_len >= size,
1834 "buffer len %d is less than real size %d\n",
1835 (int)buf->lb_len, size);
1838 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1839 lcm->lcm_size = cpu_to_le32(size);
1840 if (rec->lor_range) {
1841 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1843 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1844 } else if (rec->lor_layout_version) {
1845 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1847 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1849 lcm->lcm_layout_gen = cpu_to_le32(1);
1850 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1852 lcm->lcm_entry_count = cpu_to_le16(1);
1853 /* Currently, we do not know how many mirrors will be, set it as zero
1854 * at the beginning. It will be updated when more mirrors are found. */
1855 lcm->lcm_mirror_count = 0;
1857 lcme = &lcm->lcm_entries[0];
1858 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1859 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1860 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1861 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1862 lcme->lcme_offset = cpu_to_le32(offset);
1863 lcme->lcme_size = cpu_to_le32(lcme_size);
1864 lcme->lcme_layout_gen = lcm->lcm_layout_gen;
1865 if (ol->ol_stripe_count > 1)
1866 pattern |= LOV_PATTERN_F_HOLE;
1868 *lmm = buf->lb_buf + offset;
1869 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1870 ol->ol_stripe_size, ea_off,
1871 pattern, ol->ol_stripe_count);
1876 static void lfsck_layout_update_lcm(struct lov_comp_md_v1 *lcm,
1877 struct lov_comp_md_entry_v1 *lcme,
1878 __u32 version, __u32 range)
1880 struct lov_comp_md_entry_v1 *tmp;
1881 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
1882 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
1883 __u32 gen = version + range;
1886 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1887 __u16 flags = le16_to_cpu(lcm->lcm_flags);
1891 lcme->lcme_layout_gen = cpu_to_le32(gen);
1892 if (le32_to_cpu(lcm->lcm_layout_gen) < gen)
1893 lcm->lcm_layout_gen = cpu_to_le32(gen);
1896 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1897 else if (flags == LCM_FL_NONE && le16_to_cpu(lcm->lcm_mirror_count) > 0)
1898 lcm->lcm_flags = cpu_to_le16(LCM_FL_RDONLY);
1900 for (i = 0; i < count; i++) {
1901 tmp = &lcm->lcm_entries[i];
1902 if (le64_to_cpu(tmp->lcme_extent.e_end) <= start)
1905 if (le64_to_cpu(tmp->lcme_extent.e_start) >= end)
1908 if (le32_to_cpu(tmp->lcme_flags) & LCME_FL_STALE)
1911 tmp_gen = le32_to_cpu(tmp->lcme_layout_gen);
1912 /* "lcme_layout_gen == 0" but without LCME_FL_STALE flag,
1913 * then it should be the latest version of all mirrors. */
1914 if (tmp_gen == 0 || tmp_gen > gen) {
1915 lcme->lcme_flags = cpu_to_le32(
1916 le32_to_cpu(lcme->lcme_flags) | LCME_FL_STALE);
1921 tmp->lcme_flags = cpu_to_le32(
1922 le32_to_cpu(tmp->lcme_flags) | LCME_FL_STALE);
1926 static int lfsck_layout_add_comp(const struct lu_env *env,
1927 struct lfsck_instance *lfsck,
1928 struct thandle *handle,
1929 struct lu_orphan_rec_v3 *rec,
1930 struct dt_object *parent,
1931 const struct lu_fid *cfid,
1932 struct lu_buf *buf, __u32 ost_idx,
1933 __u32 ea_off, int pos, bool new_mirror)
1935 struct ost_layout *ol = &rec->lor_layout;
1936 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1937 struct lov_comp_md_entry_v1 *lcme;
1938 struct lov_mds_md_v1 *lmm;
1939 struct lov_ost_data_v1 *objs;
1940 int added = sizeof(*lcme) +
1941 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1942 int size = le32_to_cpu(lcm->lcm_size) + added;
1946 __u32 pattern = LOV_PATTERN_RAID0;
1947 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1950 lu_buf_check_and_grow(buf, size);
1951 /* set the lcm again because lu_buf_check_and_grow() may
1952 * have reallocated the buf. */
1954 lcm->lcm_size = cpu_to_le32(size);
1955 lcm->lcm_entry_count = cpu_to_le16(count + 1);
1957 le16_add_cpu(&lcm->lcm_mirror_count, 1);
1959 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
1960 * with distance of 'added'. */
1963 for (i = pos; i < count; i++) {
1964 lcme = &lcm->lcm_entries[i];
1965 size += le32_to_cpu(lcme->lcme_size);
1968 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
1969 memmove(buf->lb_buf + offset + added,
1970 buf->lb_buf + offset, size);
1974 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
1975 * of 'sizeof(struct lov_comp_md_entry_v1)' */
1977 for (i = 0; i < pos; i++) {
1978 lcme = &lcm->lcm_entries[i];
1979 size += le32_to_cpu(lcme->lcme_size);
1982 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
1983 memmove(buf->lb_buf + offset + sizeof(*lcme),
1984 buf->lb_buf + offset, size);
1987 /* 3. Recalculate the enter offset for the component [pos, count-1] */
1988 for (i = count - 1; i >= pos; i--) {
1989 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
1990 lcm->lcm_entries[i + 1].lcme_offset =
1991 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
1992 lcme_offset) + added);
1995 /* 4. Recalculate the enter offset for the component [0, pos) */
1996 for (i = 0; i < pos; i++) {
1997 lcm->lcm_entries[i].lcme_offset =
1998 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
1999 lcme_offset) + sizeof(*lcme));
2002 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
2003 /* 4. Insert the new component header (entry) at the slot 'pos'. */
2004 lcme = &lcm->lcm_entries[pos];
2005 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
2006 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2007 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
2008 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
2009 lcme->lcme_offset = cpu_to_le32(offset);
2010 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
2013 if (ol->ol_stripe_count > 1)
2014 pattern |= LOV_PATTERN_F_HOLE;
2016 lmm = buf->lb_buf + offset;
2017 /* 5. Insert teh new component body at the 'offset'. */
2018 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
2019 ol->ol_stripe_size, ea_off,
2020 pattern, ol->ol_stripe_count);
2022 /* 6. Update mirror related flags and version. */
2023 lfsck_layout_update_lcm(lcm, lcme, rec->lor_layout_version,
2026 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2027 lmm, objs, LU_XATTR_REPLACE, ost_idx,
2028 le32_to_cpu(lcm->lcm_size));
2030 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
2031 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2032 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2033 "comp_end %llu, layout version %u, range %u, "
2034 "%s LOV EA hole: rc = %d\n",
2035 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2036 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2037 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2038 rec->lor_layout_version, rec->lor_range,
2039 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2040 "with" : "without", rc);
2045 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
2046 struct lfsck_instance *lfsck,
2047 struct thandle *handle,
2048 struct ost_layout *ol,
2049 struct dt_object *parent,
2050 const struct lu_fid *cfid,
2051 struct lu_buf *buf, __u32 ost_idx,
2054 struct lov_mds_md_v1 *lmm = buf->lb_buf;
2055 struct lov_ost_data_v1 *objs;
2056 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
2057 __u32 magic = le32_to_cpu(lmm->lmm_magic);
2063 /* The original LOVEA maybe re-generated via old filter_fid, at
2064 * that time, we do not know the stripe count and stripe size. */
2065 if (ol->ol_stripe_count > count)
2066 count = ol->ol_stripe_count;
2067 if (ol->ol_stripe_size != 0 &&
2068 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
2069 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
2071 if (magic == LOV_MAGIC_V1)
2072 objs = &lmm->lmm_objects[count];
2074 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
2076 gap = ea_off - count;
2080 size = lov_mds_md_size(count, magic);
2081 LASSERTF(buf->lb_len >= size,
2082 "buffer len %d is less than real size %d\n",
2083 (int)buf->lb_len, size);
2086 memset(objs, 0, gap * sizeof(*objs));
2087 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
2090 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2091 lmm->lmm_stripe_count = cpu_to_le16(count);
2094 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2095 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
2097 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
2098 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2099 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2100 "comp_end %llu, %s LOV EA hole: rc = %d\n",
2101 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2102 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2103 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2104 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2105 "with" : "without", rc);
2111 * \retval +1: repaired
2112 * \retval 0: did nothing
2113 * \retval -ve: on error
2115 static int lfsck_layout_update_lovea(const struct lu_env *env,
2116 struct lfsck_instance *lfsck,
2117 struct thandle *handle,
2118 struct lu_orphan_rec_v3 *rec,
2119 struct dt_object *parent,
2120 const struct lu_fid *cfid,
2121 struct lu_buf *buf, int fl,
2122 __u32 ost_idx, __u32 ea_off)
2124 struct ost_layout *ol = &rec->lor_layout;
2125 struct lov_mds_md_v1 *lmm = NULL;
2126 struct lov_ost_data_v1 *objs = NULL;
2130 if (ol->ol_comp_id != 0)
2131 rc = lfsck_layout_new_comp_lovea(env, rec, parent, buf, ea_off,
2134 rc = lfsck_layout_new_v1_lovea(env, lfsck, &rec->lor_layout,
2135 parent, buf, ea_off, &lmm,
2138 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2139 buf, lmm, objs, fl, ost_idx, rc);
2141 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2142 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2143 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2144 "comp_end %llu, layout version %u, range %u, fl %d, "
2145 "%s LOV EA hole: rc = %d\n",
2146 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2147 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2148 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2149 rec->lor_layout_version, rec->lor_range, fl,
2150 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2151 "with" : "without", rc);
2156 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2157 struct dt_object *child,
2158 const struct lu_fid *pfid,
2159 const struct ost_layout *ol, __u32 offset,
2160 __u32 version, __u32 range)
2162 struct dt_device *dev = lfsck_obj2dev(child);
2163 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2164 struct thandle *handle;
2165 struct lu_buf buf = { NULL };
2168 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2169 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2170 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2171 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2172 * parent MDT-object's layout EA. */
2173 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2174 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2175 ff->ff_layout_version = cpu_to_le32(version);
2176 ff->ff_range = cpu_to_le32(range);
2177 lfsck_buf_init(&buf, ff, sizeof(*ff));
2179 handle = dt_trans_create(env, dev);
2181 RETURN(PTR_ERR(handle));
2183 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2187 rc = dt_trans_start_local(env, dev, handle);
2191 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2196 dt_trans_stop(env, dev, handle);
2202 * \retval +1: repaired
2203 * \retval 0: did nothing
2204 * \retval -ve: on error
2206 static int lfsck_layout_update_pfid(const struct lu_env *env,
2207 struct lfsck_component *com,
2208 struct dt_object *parent,
2209 struct lu_fid *cfid,
2210 struct dt_device *cdev,
2211 struct lu_orphan_rec_v3 *rec, __u32 ea_off)
2213 struct dt_object *child;
2217 child = lfsck_object_find_by_dev(env, cdev, cfid);
2219 RETURN(PTR_ERR(child));
2221 rc = __lfsck_layout_update_pfid(env, child,
2222 lu_object_fid(&parent->do_lu),
2223 &rec->lor_layout, ea_off,
2224 rec->lor_layout_version,
2226 lfsck_object_put(env, child);
2228 RETURN(rc == 0 ? 1 : rc);
2231 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2233 if (ol->ol_comp_id != 0)
2234 return sizeof(struct lov_comp_md_v1) +
2235 sizeof(struct lov_comp_md_entry_v1) +
2236 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2238 if (ol->ol_stripe_count != 0)
2239 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2241 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2245 * This function will create the MDT-object with the given (partial) LOV EA.
2247 * Under some data corruption cases, the MDT-object of the file may be lost,
2248 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2249 * re-create the MDT-object with the orphan OST-object(s) information.
2251 * On the other hand, the LFSCK may has created some OST-object for repairing
2252 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2253 * the old OST-object is there and should replace the former new created OST
2254 * object. Unfortunately, some others have modified such newly created object.
2255 * To keep the data (both new and old), the LFSCK will create MDT-object with
2256 * new FID to reference the original OST-object.
2258 * \param[in] env pointer to the thread context
2259 * \param[in] com pointer to the lfsck component
2260 * \param[in] ltd pointer to target device descriptor
2261 * \param[in] rec pointer to the record for the orphan OST-object
2262 * \param[in] cfid pointer to FID for the orphan OST-object
2263 * \param[in] infix additional information, such as the FID for original
2264 * MDT-object and the stripe offset in the LOV EA
2265 * \param[in] type the type for describing why the orphan MDT-object is
2266 * created. The rules are as following:
2268 * type "C": Multiple OST-objects claim the same MDT-object and the
2269 * same slot in the layout EA. Then the LFSCK will create
2270 * new MDT-object(s) to hold the conflict OST-object(s).
2272 * type "N": The orphan OST-object does not know which one was the
2273 * real parent MDT-object, so the LFSCK uses new FID for
2274 * its parent MDT-object.
2276 * type "R": The orphan OST-object knows its parent MDT-object FID,
2277 * but does not know the position (the file name) in the
2280 * type "D": The MDT-object is a directory, it may knows its parent
2281 * but because there is no valid linkEA, the LFSCK cannot
2282 * know where to put it back to the namespace.
2283 * type "O": The MDT-object has no linkEA, and there is no name
2284 * entry that references the MDT-object.
2286 * type "P": The orphan object to be created was a parent directory
2287 * of some MDT-object which linkEA shows that the @orphan
2288 * object is missing.
2290 * The orphan name will be like:
2291 * ${FID}-${infix}-${type}-${conflict_version}
2293 * \param[in] ea_off the stripe offset in the LOV EA
2295 * \retval positive on repaired something
2296 * \retval 0 if needs to repair nothing
2297 * \retval negative error number on failure
2299 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2300 struct lfsck_component *com,
2301 struct lfsck_tgt_desc *ltd,
2302 struct lu_orphan_rec_v3 *rec,
2303 struct lu_fid *cfid,
2308 struct lfsck_thread_info *info = lfsck_env_info(env);
2309 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2310 char *name = info->lti_key;
2311 struct lu_attr *la = &info->lti_la2;
2312 struct dt_object_format *dof = &info->lti_dof;
2313 struct lfsck_instance *lfsck = com->lc_lfsck;
2314 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2315 struct lu_fid *tfid = &info->lti_fid3;
2316 struct dt_device *dev = lfsck->li_bottom;
2317 struct dt_object *lpf = lfsck->li_lpf_obj;
2318 struct dt_object *pobj = NULL;
2319 struct dt_object *cobj = NULL;
2320 struct thandle *th = NULL;
2321 struct lu_buf *ea_buf = &info->lti_big_buf;
2322 struct lu_buf lov_buf;
2323 struct lfsck_lock_handle *llh = &info->lti_llh;
2324 struct linkea_data ldata = { NULL };
2325 struct lu_buf linkea_buf;
2326 const struct lu_name *pname;
2332 if (unlikely(lpf == NULL))
2333 GOTO(log, rc = -ENXIO);
2335 /* We use two separated transactions to repair the inconsistency.
2337 * 1) create the MDT-object locally.
2338 * 2) update the OST-object's PFID EA if necessary.
2340 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2341 * updated when the layout LFSCK run next time.
2343 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2344 * when the layout LFSCK run next time. */
2346 if (fid_is_zero(pfid)) {
2347 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2351 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2353 GOTO(log, rc = PTR_ERR(cobj));
2356 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2358 GOTO(log, rc = PTR_ERR(pobj));
2360 LASSERT(infix != NULL);
2361 LASSERT(type != NULL);
2363 memset(la, 0, sizeof(*la));
2364 la->la_uid = rec->lor_rec.lor_uid;
2365 la->la_gid = rec->lor_rec.lor_gid;
2366 la->la_mode = S_IFREG | S_IRUSR;
2367 la->la_valid = LA_MODE | LA_UID | LA_GID;
2369 memset(dof, 0, sizeof(*dof));
2370 dof->dof_type = dt_mode_to_dft(S_IFREG);
2371 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2372 * the stripe(s). The LFSCK will specify the LOV EA via
2373 * lfsck_layout_update_lovea(). */
2375 size = lfsck_lovea_size(&rec->lor_layout, ea_off);
2376 if (ea_buf->lb_len < size) {
2377 lu_buf_realloc(ea_buf, size);
2378 if (ea_buf->lb_buf == NULL)
2379 GOTO(log, rc = -ENOMEM);
2384 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2386 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2387 (const struct dt_key *)name);
2388 if (rc != 0 && rc != -ENOENT)
2392 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2393 MDS_INODELOCK_UPDATE, LCK_PW);
2397 /* Re-check whether the name conflict with othrs after taken
2399 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2400 (const struct dt_key *)name);
2401 if (unlikely(rc == 0)) {
2409 pname = lfsck_name_get_const(env, name, strlen(name));
2410 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2411 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2415 /* The 1st transaction. */
2416 th = dt_trans_create(env, dev);
2418 GOTO(unlock, rc = PTR_ERR(th));
2420 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2424 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2425 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2426 LU_XATTR_CREATE, th);
2430 dtrec->rec_fid = pfid;
2431 dtrec->rec_type = S_IFREG;
2432 rc = dt_declare_insert(env, lpf,
2433 (const struct dt_rec *)dtrec,
2434 (const struct dt_key *)name, th);
2438 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2439 ldata.ld_leh->leh_len);
2440 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2441 XATTR_NAME_LINK, 0, th);
2445 rc = dt_trans_start_local(env, dev, th);
2449 dt_write_lock(env, pobj, 0);
2450 rc = dt_create(env, pobj, la, NULL, dof, th);
2452 rc = lfsck_layout_update_lovea(env, lfsck, th, rec, pobj, cfid,
2453 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2454 dt_write_unlock(env, pobj);
2458 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2459 (const struct dt_key *)name, th);
2463 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2464 if (rc == 0 && cobj != NULL) {
2465 dt_trans_stop(env, dev, th);
2468 /* The 2nd transaction. */
2469 rc = __lfsck_layout_update_pfid(env, cobj, pfid,
2470 &rec->lor_layout, ea_off,
2471 rec->lor_layout_version,
2479 dt_trans_stop(env, dev, th);
2485 if (cobj != NULL && !IS_ERR(cobj))
2486 lfsck_object_put(env, cobj);
2487 if (pobj != NULL && !IS_ERR(pobj))
2488 lfsck_object_put(env, pobj);
2491 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2492 "recreate the lost MDT-object: parent "DFID
2493 ", child "DFID", OST-index %u, stripe-index %u, "
2494 "infix %s, type %s: rc = %d\n",
2495 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2496 ltd->ltd_index, ea_off, infix, type, rc);
2498 return rc >= 0 ? 1 : rc;
2501 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2502 struct lfsck_component *com,
2503 const struct lu_fid *fid,
2506 struct lfsck_thread_info *info = lfsck_env_info(env);
2507 struct lfsck_request *lr = &info->lti_lr;
2508 struct lfsck_instance *lfsck = com->lc_lfsck;
2509 struct lfsck_tgt_desc *ltd;
2510 struct ptlrpc_request *req;
2511 struct lfsck_request *tmp;
2512 struct obd_export *exp;
2516 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2517 if (unlikely(ltd == NULL))
2521 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2522 GOTO(put, rc = -EOPNOTSUPP);
2524 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2526 GOTO(put, rc = -ENOMEM);
2528 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2530 ptlrpc_request_free(req);
2535 memset(lr, 0, sizeof(*lr));
2536 lr->lr_event = LE_CONDITIONAL_DESTROY;
2537 lr->lr_active = LFSCK_TYPE_LAYOUT;
2540 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2542 ptlrpc_request_set_replen(req);
2544 rc = ptlrpc_queue_wait(req);
2545 ptlrpc_req_finished(req);
2555 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2556 struct lfsck_component *com,
2557 struct lfsck_request *lr)
2559 struct lfsck_thread_info *info = lfsck_env_info(env);
2560 struct lu_attr *la = &info->lti_la;
2561 union ldlm_policy_data *policy = &info->lti_policy;
2562 struct ldlm_res_id *resid = &info->lti_resid;
2563 struct lfsck_instance *lfsck = com->lc_lfsck;
2564 struct dt_device *dev = lfsck->li_bottom;
2565 struct lu_fid *fid = &lr->lr_fid;
2566 struct dt_object *obj;
2567 struct thandle *th = NULL;
2568 struct lustre_handle lh = { 0 };
2573 obj = lfsck_object_find_by_dev(env, dev, fid);
2575 RETURN(PTR_ERR(obj));
2577 dt_read_lock(env, obj, 0);
2578 if (dt_object_exists(obj) == 0 ||
2579 lfsck_is_dead_obj(obj)) {
2580 dt_read_unlock(env, obj);
2582 GOTO(put, rc = -ENOENT);
2585 /* Get obj's attr without lock firstly. */
2586 rc = dt_attr_get(env, obj, la);
2587 dt_read_unlock(env, obj);
2591 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2592 GOTO(put, rc = -ETXTBSY);
2594 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2595 LASSERT(lfsck->li_namespace != NULL);
2597 memset(policy, 0, sizeof(*policy));
2598 policy->l_extent.end = OBD_OBJECT_EOF;
2599 ost_fid_build_resid(fid, resid);
2600 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
2601 LDLM_EXTENT, policy, LCK_EX, &flags,
2602 ldlm_blocking_ast, ldlm_completion_ast,
2603 NULL, NULL, 0, LVB_T_NONE, NULL, &lh);
2605 GOTO(put, rc = -EIO);
2607 dt_write_lock(env, obj, 0);
2608 /* Get obj's attr within lock again. */
2609 rc = dt_attr_get(env, obj, la);
2613 if (la->la_ctime != 0)
2614 GOTO(unlock, rc = -ETXTBSY);
2616 th = dt_trans_create(env, dev);
2618 GOTO(unlock, rc = PTR_ERR(th));
2620 rc = dt_declare_ref_del(env, obj, th);
2624 rc = dt_declare_destroy(env, obj, th);
2628 rc = dt_trans_start_local(env, dev, th);
2632 rc = dt_ref_del(env, obj, th);
2636 rc = dt_destroy(env, obj, th);
2638 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2639 "OST-object "DFID" that was created for reparing "
2640 "dangling referenced case. But the original missing "
2641 "OST-object is found now.\n",
2642 lfsck_lfsck2name(lfsck), PFID(fid));
2647 dt_trans_stop(env, dev, th);
2650 dt_write_unlock(env, obj);
2651 ldlm_lock_decref(&lh, LCK_EX);
2654 lfsck_object_put(env, obj);
2660 * Some OST-object has occupied the specified layout EA slot.
2661 * Such OST-object may be generated by the LFSCK when repair
2662 * dangling referenced MDT-object, which can be indicated by
2663 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2664 * is true and such OST-object has not been modified yet, we
2665 * will replace it with the orphan OST-object; otherwise the
2666 * LFSCK will create new MDT-object to reference the orphan.
2668 * \retval +1: repaired
2669 * \retval 0: did nothing
2670 * \retval -ve: on error
2672 static int lfsck_layout_conflict_create(const struct lu_env *env,
2673 struct lfsck_component *com,
2674 struct lfsck_tgt_desc *ltd,
2675 struct lu_orphan_rec_v3 *rec,
2676 struct dt_object *parent,
2677 struct lu_fid *cfid,
2678 struct lu_buf *ea_buf,
2679 struct lov_mds_md_v1 *lmm,
2680 struct lov_ost_data_v1 *slot,
2681 __u32 ea_off, int lovea_size)
2683 struct lfsck_thread_info *info = lfsck_env_info(env);
2684 struct lu_fid *cfid2 = &info->lti_fid2;
2685 struct ost_id *oi = &info->lti_oi;
2686 struct dt_device *dev = lfsck_obj2dev(parent);
2687 struct thandle *th = NULL;
2688 struct lustre_handle lh = { 0 };
2689 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2693 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2694 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2698 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2699 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2703 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2704 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2709 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2711 /* If the conflict OST-obejct is not created for fixing dangling
2712 * referenced MDT-object in former LFSCK check/repair, or it has
2713 * been modified by others, then we cannot destroy it. Re-create
2714 * a new MDT-object for the orphan OST-object. */
2715 if (rc == -ETXTBSY) {
2716 /* No need the layout lock on the original parent. */
2717 lfsck_ibits_unlock(&lh, LCK_EX);
2719 fid_zero(&rec->lor_rec.lor_fid);
2720 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2721 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2723 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2724 info->lti_tmpbuf, "C", ea_off);
2729 if (rc != 0 && rc != -ENOENT)
2732 th = dt_trans_create(env, dev);
2734 GOTO(unlock, rc = PTR_ERR(th));
2736 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2737 LU_XATTR_REPLACE, th);
2741 rc = dt_trans_start_local(env, dev, th);
2745 dt_write_lock(env, parent, 0);
2746 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2747 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2748 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2749 ltd->ltd_index, lovea_size);
2750 dt_write_unlock(env, parent);
2755 dt_trans_stop(env, dev, th);
2758 lfsck_ibits_unlock(&lh, LCK_EX);
2761 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2762 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2763 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2764 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2765 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2768 return rc >= 0 ? 1 : rc;
2772 * \retval +1: repaired
2773 * \retval 0: did nothing
2774 * \retval -ve: on error
2776 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2777 struct lfsck_component *com,
2778 struct lfsck_tgt_desc *ltd,
2779 struct lu_orphan_rec_v3 *rec,
2780 struct dt_object *parent,
2781 struct lu_fid *cfid,
2782 __u32 ost_idx, __u32 ea_off)
2784 struct lfsck_thread_info *info = lfsck_env_info(env);
2785 struct lu_buf *buf = &info->lti_big_buf;
2786 struct lu_fid *fid = &info->lti_fid2;
2787 struct ost_id *oi = &info->lti_oi;
2788 struct lfsck_instance *lfsck = com->lc_lfsck;
2789 struct dt_device *dt = lfsck_obj2dev(parent);
2790 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2791 struct ost_layout *ol = &rec->lor_layout;
2792 struct lov_comp_md_v1 *lcm = NULL;
2793 struct lov_comp_md_entry_v1 *lcme = NULL;
2794 struct thandle *handle = NULL;
2796 struct lov_mds_md_v1 *lmm;
2797 struct lov_ost_data_v1 *objs;
2798 struct lustre_handle lh = { 0 };
2807 bool locked = false;
2808 bool new_mirror = true;
2811 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2812 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2815 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2816 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2817 "stripe-index %u, comp_id %u, comp_start %llu, "
2818 "comp_end %llu, layout version %u, range %u: rc = %d\n",
2819 lfsck_lfsck2name(lfsck), PFID(cfid),
2820 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2821 ol->ol_comp_id, ol->ol_comp_start,
2822 ol->ol_comp_end, rec->lor_layout_version,
2823 rec->lor_range, rc);
2830 dt_write_unlock(env, parent);
2834 if (handle != NULL) {
2835 dt_trans_stop(env, dt, handle);
2840 GOTO(unlock_layout, rc);
2843 if (buf->lb_len < lovea_size) {
2844 lu_buf_realloc(buf, lovea_size);
2845 if (buf->lb_buf == NULL)
2846 GOTO(unlock_layout, rc = -ENOMEM);
2849 if (!(bk->lb_param & LPF_DRYRUN)) {
2850 handle = dt_trans_create(env, dt);
2852 GOTO(unlock_layout, rc = PTR_ERR(handle));
2854 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2859 rc = dt_trans_start_local(env, dt, handle);
2864 dt_write_lock(env, parent, 0);
2866 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2867 if (rc == -ERANGE) {
2868 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2871 } else if (rc == -ENODATA || rc == 0) {
2872 lovea_size = lfsck_lovea_size(ol, ea_off);
2873 /* If the declared is not big enough, re-try. */
2874 if (buf->lb_len < lovea_size) {
2878 fl = LU_XATTR_CREATE;
2879 } else if (rc < 0) {
2880 GOTO(unlock_parent, rc);
2881 } else if (unlikely(buf->lb_len == 0)) {
2884 fl = LU_XATTR_REPLACE;
2888 if (fl == LU_XATTR_CREATE) {
2889 if (bk->lb_param & LPF_DRYRUN)
2890 GOTO(unlock_parent, rc = 1);
2892 LASSERT(buf->lb_len >= lovea_size);
2894 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2895 cfid, buf, fl, ost_idx, ea_off);
2897 GOTO(unlock_parent, rc);
2901 rc1 = lfsck_layout_verify_header(parent, lmm, lovea_size);
2903 /* If the LOV EA crashed, the rebuild it. */
2904 if (rc1 == -EINVAL) {
2905 if (bk->lb_param & LPF_DRYRUN)
2906 GOTO(unlock_parent, rc = 1);
2908 LASSERT(buf->lb_len >= lovea_size);
2910 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2911 cfid, buf, fl, ost_idx, ea_off);
2913 GOTO(unlock_parent, rc);
2916 /* For other unknown magic/pattern, keep the current LOV EA. */
2917 if (rc1 == -EOPNOTSUPP)
2918 GOTO(unlock_parent, rc1 = 0);
2921 GOTO(unlock_parent, rc = rc1);
2923 magic = le32_to_cpu(lmm->lmm_magic);
2924 if (magic == LOV_MAGIC_COMP_V1) {
2927 __u16 mirror_id0 = mirror_id_of(ol->ol_comp_id);
2930 if (bk->lb_param & LPF_DRYRUN)
2931 GOTO(unlock_parent, rc = 1);
2934 count = le16_to_cpu(lcm->lcm_entry_count);
2935 for (i = 0; i < count; pos = ++i) {
2936 lcme = &lcm->lcm_entries[i];
2937 start = le64_to_cpu(lcme->lcme_extent.e_start);
2938 end = le64_to_cpu(lcme->lcme_extent.e_end);
2939 mirror_id1 = mirror_id_of(le32_to_cpu(lcme->lcme_id));
2941 if (mirror_id0 > mirror_id1)
2944 if (mirror_id0 < mirror_id1)
2948 if (end <= ol->ol_comp_start)
2951 if (start >= ol->ol_comp_end)
2954 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
2955 magic = le32_to_cpu(lmm->lmm_magic);
2956 flags = le32_to_cpu(lcme->lcme_flags);
2960 rc = lfsck_layout_add_comp(env, lfsck, handle, rec, parent,
2961 cfid, buf, ost_idx, ea_off, pos, new_mirror);
2963 GOTO(unlock_parent, rc);
2967 count = le16_to_cpu(lmm->lmm_stripe_count);
2969 GOTO(unlock_parent, rc = -EINVAL);
2972 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2973 if (count <= ea_off) {
2974 if (bk->lb_param & LPF_DRYRUN)
2975 GOTO(unlock_parent, rc = 1);
2977 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2978 /* If the declared is not big enough, re-try. */
2979 if (buf->lb_len < lovea_size) {
2987 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
2988 lfsck_layout_update_lcm(lcm, lcme,
2989 rec->lor_layout_version,
2993 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
2994 parent, cfid, buf, ost_idx, ea_off);
2996 GOTO(unlock_parent, rc);
2999 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
3001 if (magic == LOV_MAGIC_V1) {
3002 objs = &lmm->lmm_objects[0];
3004 LASSERT(magic == LOV_MAGIC_V3);
3005 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3008 for (i = 0; i < count; i++, objs++) {
3009 /* The MDT-object was created via lfsck_layout_recover_create()
3010 * by others before, and we fill the dummy layout EA. */
3011 if ((lcme && !(flags & LCME_FL_INIT)) ||
3012 lovea_slot_is_dummy(objs)) {
3016 if (bk->lb_param & LPF_DRYRUN)
3017 GOTO(unlock_parent, rc = 1);
3019 lmm->lmm_layout_gen =
3020 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3024 if (le32_to_cpu(lmm->lmm_stripe_size) !=
3025 ol->ol_stripe_size ||
3026 le16_to_cpu(lmm->lmm_stripe_count) !=
3027 ol->ol_stripe_count ||
3028 le64_to_cpu(lcme->lcme_extent.e_start) !=
3029 ol->ol_comp_start ||
3030 le64_to_cpu(lcme->lcme_extent.e_end) !=
3032 CDEBUG(D_LFSCK, "%s: found invalid "
3033 "component for "DFID ": parent "DFID
3034 ", stripe-index %u, stripe_size %u, "
3035 "stripe_count %u, comp_id %u, "
3036 "comp_start %llu, comp_end %llu, "
3037 "cur_stripe_size %u, "
3038 "cur_stripe_count %u, "
3039 "cur_comp_start %llu, "
3040 "cur_comp_end %llu\n",
3041 lfsck_lfsck2name(lfsck), PFID(cfid),
3042 PFID(lfsck_dto2fid(parent)), ea_off,
3044 ol->ol_stripe_count, ol->ol_comp_id,
3045 ol->ol_comp_start, ol->ol_comp_end,
3046 le32_to_cpu(lmm->lmm_stripe_size),
3047 le16_to_cpu(lmm->lmm_stripe_count),
3048 le64_to_cpu(lcme->lcme_extent.e_start),
3049 le64_to_cpu(lcme->lcme_extent.e_end));
3051 GOTO(unlock_parent, rc = -EINVAL);
3054 lovea_size = le32_to_cpu(lcm->lcm_size);
3055 lcme->lcme_flags = cpu_to_le32(flags |
3057 lfsck_layout_update_lcm(lcm, lcme,
3058 rec->lor_layout_version,
3062 LASSERTF(buf->lb_len >= lovea_size,
3063 "buffer len %d is less than real size %d\n",
3064 (int)buf->lb_len, (int)lovea_size);
3066 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
3067 parent, cfid, buf, lmm, objs,
3068 fl, ost_idx, lovea_size);
3070 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
3071 "dummy layout slot for "DFID": parent "DFID
3072 ", OST-index %u, stripe-index %u: rc = %d\n",
3073 lfsck_lfsck2name(lfsck), PFID(cfid),
3074 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
3076 GOTO(unlock_parent, rc);
3079 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3080 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
3082 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3083 "invalid layout EA at the slot %d, index %u\n",
3084 lfsck_lfsck2name(lfsck),
3085 PFID(lfsck_dto2fid(parent)), i,
3086 le32_to_cpu(objs->l_ost_idx));
3088 GOTO(unlock_parent, rc);
3091 /* It should be rare case, the slot is there, but the LFSCK
3092 * does not handle it during the first-phase cycle scanning. */
3093 if (unlikely(lu_fid_eq(fid, cfid))) {
3095 GOTO(unlock_parent, rc = 0);
3097 /* Rare case that the OST-object index
3098 * does not match the parent MDT-object
3099 * layout EA. We trust the later one. */
3100 if (bk->lb_param & LPF_DRYRUN)
3101 GOTO(unlock_parent, rc = 1);
3103 dt_write_unlock(env, parent);
3105 dt_trans_stop(env, dt, handle);
3106 lfsck_ibits_unlock(&lh, LCK_EX);
3107 rc = lfsck_layout_update_pfid(env, com, parent,
3111 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
3112 "updated OST-object's pfid for "DFID
3113 ": parent "DFID", OST-index %u, "
3114 "stripe-index %u: rc = %d\n",
3115 lfsck_lfsck2name(lfsck), PFID(cfid),
3116 PFID(lfsck_dto2fid(parent)),
3117 ltd->ltd_index, i, rc);
3124 /* The MDT-object exists, but related layout EA slot is occupied
3126 if (bk->lb_param & LPF_DRYRUN)
3127 GOTO(unlock_parent, rc = 1);
3129 dt_write_unlock(env, parent);
3131 dt_trans_stop(env, dt, handle);
3132 lfsck_ibits_unlock(&lh, LCK_EX);
3133 if (magic == LOV_MAGIC_V1)
3134 objs = &lmm->lmm_objects[ea_off];
3136 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
3137 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
3138 buf, lmm, objs, ea_off, lovea_size);
3144 dt_write_unlock(env, parent);
3148 dt_trans_stop(env, dt, handle);
3151 lfsck_ibits_unlock(&lh, LCK_EX);
3156 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3157 struct lfsck_component *com,
3158 struct lfsck_tgt_desc *ltd,
3159 struct lu_orphan_rec_v3 *rec,
3160 struct lu_fid *cfid)
3162 struct lfsck_layout *lo = com->lc_file_ram;
3163 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3164 struct dt_object *parent = NULL;
3165 __u32 ea_off = pfid->f_stripe_idx;
3169 if (!fid_is_sane(cfid))
3170 GOTO(out, rc = -EINVAL);
3173 if (fid_is_zero(pfid)) {
3174 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3179 if (!fid_is_sane(pfid))
3180 GOTO(out, rc = -EINVAL);
3182 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3184 GOTO(out, rc = PTR_ERR(parent));
3186 if (unlikely(dt_object_remote(parent) != 0))
3187 GOTO(put, rc = -EXDEV);
3189 if (dt_object_exists(parent) == 0) {
3190 lfsck_object_put(env, parent);
3191 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3196 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3197 GOTO(put, rc = -EISDIR);
3199 /* The orphan OST-object claims to be the parent's stripe, then
3200 * related dangling record in the trace file is meaningless. */
3201 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3202 rec->lor_layout.ol_comp_id, ea_off);
3203 if (rc && rc != -ENOENT)
3206 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3207 ltd->ltd_index, ea_off);
3213 lfsck_object_put(env, parent);
3215 /* The layout EA is changed, need to be reloaded next time. */
3216 dt_object_put_nocache(env, parent);
3219 down_write(&com->lc_sem);
3220 com->lc_new_scanned++;
3221 com->lc_new_checked++;
3223 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3225 } else if (rc < 0) {
3226 lo->ll_objs_failed_phase2++;
3228 up_write(&com->lc_sem);
3233 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3234 struct lfsck_component *com,
3235 struct lfsck_tgt_desc *ltd)
3237 struct lfsck_assistant_data *lad = com->lc_data;
3238 struct lfsck_instance *lfsck = com->lc_lfsck;
3239 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3240 struct lfsck_thread_info *info = lfsck_env_info(env);
3241 struct lu_fid *fid = &info->lti_fid;
3242 struct dt_object *obj;
3243 const struct dt_it_ops *iops;
3248 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3249 "scanning for OST%04x\n",
3250 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3252 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
3253 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3254 "scanning for OST%04x\n",
3255 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3260 fid->f_seq = fid_idif_seq(0, ltd->ltd_index);
3261 fid->f_oid = fid->f_ver = 0;
3263 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3264 if (unlikely(IS_ERR(obj)))
3265 GOTO(log, rc = PTR_ERR(obj));
3267 rc = obj->do_ops->do_index_try(env, obj,
3268 &dt_lfsck_layout_orphan_features);
3272 iops = &obj->do_index_ops->dio_it;
3273 di = iops->init(env, obj, 0);
3275 GOTO(put, rc = PTR_ERR(di));
3277 rc = iops->load(env, di, 0);
3279 /* -ESRCH means that the orphan OST-objects rbtree has been
3280 * cleanup because of the OSS server restart or other errors. */
3281 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3286 rc = iops->next(env, di);
3298 struct lu_orphan_rec_v3 *rec = &info->lti_rec;
3300 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3301 unlikely(!thread_is_running(&lfsck->li_thread)))
3304 key = iops->key(env, di);
3305 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3306 /* Remote target OST may be runnning old LFSCK */
3307 memset(rec, 0, sizeof(*rec));
3308 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3310 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3311 &com->lc_fid_latest_scanned_phase2);
3312 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3315 lfsck_control_speed_by_self(com);
3317 rc = iops->next(env, di);
3318 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3325 iops->fini(env, di);
3327 lfsck_object_put(env, obj);
3330 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3331 "scanning for OST%04x: rc = %d\n",
3332 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3334 return rc > 0 ? 0 : rc;
3337 static int lfsck_lov2layout(struct lov_mds_md_v1 *lmm, struct filter_fid *ff,
3340 struct ost_layout *ol = &ff->ff_layout;
3341 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3345 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3346 ol->ol_stripe_size = lmm->lmm_stripe_size;
3347 ol->ol_stripe_count = lmm->lmm_stripe_count;
3348 ol->ol_comp_start = 0;
3349 ol->ol_comp_end = 0;
3351 ff->ff_layout_version = 0;
3353 } else if (magic == LOV_MAGIC_COMP_V1) {
3354 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3355 struct lov_comp_md_entry_v1 *lcme = NULL;
3356 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3359 for (i = 0; i < count; i++) {
3360 lcme = &lcm->lcm_entries[i];
3361 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3362 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3369 /* The comp has been removed, do nothing. */
3373 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3374 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3375 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3376 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3377 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3378 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3379 ff->ff_layout_version = le32_to_cpu(lcme->lcme_layout_gen);
3382 GOTO(out, rc = -EINVAL);
3392 * Repair the MDT-object with dangling LOV EA reference.
3394 * we need to repair the inconsistency according to the users' requirement:
3396 * 1) Keep the inconsistency there and report the inconsistency case,
3397 * then give the chance to the application to find related issues,
3398 * and the users can make the decision about how to handle it with
3399 * more human knownledge. (by default)
3401 * 2) Re-create the missing OST-object with the FID/owner information.
3403 * \param[in] env pointer to the thread context
3404 * \param[in] com the layout LFSCK component
3405 * \param[in] parent the MDT-object with dangling LOV EA reference
3406 * \param[in] child the OST-object to be created
3407 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3408 * \param[in] ea_off the offset of the OST-object in the LOV EA
3409 * \param[in] ost_idx the index of OST on which the OST-object resides
3411 * \retval +1 for repair successfully
3412 * \retval 0 for did nothing
3413 * \retval negative error number on failure
3415 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3416 struct lfsck_component *com,
3417 struct dt_object *parent,
3418 struct dt_object *child,
3419 __u32 comp_id, __u32 ea_off,
3420 __u32 ost_idx, bool log)
3422 struct lfsck_thread_info *info = lfsck_env_info(env);
3423 struct filter_fid *ff = &info->lti_ff;
3424 struct dt_object_format *dof = &info->lti_dof;
3425 struct lu_attr *la = &info->lti_la;
3426 struct lfsck_instance *lfsck = com->lc_lfsck;
3427 struct dt_device *dev = lfsck_obj2dev(child);
3428 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3429 const struct lu_fid *cfid = lfsck_dto2fid(child);
3430 struct lu_buf *tbuf = &info->lti_big_buf;
3431 struct thandle *handle;
3433 struct lustre_handle lh = { 0 };
3437 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3440 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3441 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3446 rc = dt_attr_get(env, parent, la);
3450 la->la_mode = S_IFREG | 0666;
3451 la->la_atime = la->la_mtime = la->la_ctime = 0;
3452 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3453 LA_ATIME | LA_MTIME | LA_CTIME;
3454 memset(dof, 0, sizeof(*dof));
3455 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3456 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3457 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3458 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3459 * parent MDT-object's layout EA. */
3460 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3462 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3463 if (unlikely(rc == -ENODATA))
3468 rc = lfsck_lov2layout(tbuf->lb_buf, ff, comp_id);
3472 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3473 handle = dt_trans_create(env, dev);
3475 GOTO(unlock1, rc = PTR_ERR(handle));
3477 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3481 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3482 LU_XATTR_CREATE, handle);
3486 rc = dt_trans_start_local(env, dev, handle);
3490 dt_read_lock(env, parent, 0);
3491 if (unlikely(lfsck_is_dead_obj(parent)))
3492 GOTO(unlock2, rc = 0);
3494 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3495 struct ost_id *oi = &info->lti_oi;
3496 struct lu_fid *tfid = &info->lti_fid2;
3497 struct lu_buf *lovea = &info->lti_big_buf;
3498 struct lov_mds_md_v1 *lmm;
3499 struct lov_ost_data_v1 *objs;
3504 rc = lfsck_layout_get_lovea(env, parent, lovea);
3505 if (unlikely(rc == -ENODATA))
3510 lmm = lovea->lb_buf;
3511 magic = le32_to_cpu(lmm->lmm_magic);
3512 if (magic == LOV_MAGIC_COMP_V1) {
3513 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3514 struct lov_comp_md_entry_v1 *lcme;
3515 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3518 for (i = 0; i < count; i++) {
3519 lcme = &lcm->lcm_entries[i];
3520 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3521 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3524 lmm = lovea->lb_buf +
3525 le32_to_cpu(lcme->lcme_offset);
3526 magic = le32_to_cpu(lmm->lmm_magic);
3531 /* Someone removed the component, do nothing. */
3532 GOTO(unlock2, rc = 0);
3536 count = le16_to_cpu(lmm->lmm_stripe_count);
3537 /* Someone changed the LOV EA, do nothing. */
3538 if (count <= ea_off)
3539 GOTO(unlock2, rc = 0);
3541 if (magic == LOV_MAGIC_V1) {
3542 objs = &lmm->lmm_objects[ea_off];
3544 LASSERT(magic == LOV_MAGIC_V3);
3546 objs = &((struct lov_mds_md_v3 *)lmm)->\
3547 lmm_objects[ea_off];
3550 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3551 idx2 = le32_to_cpu(objs->l_ost_idx);
3552 rc = ostid_to_fid(tfid, oi, idx2);
3553 /* Someone changed the LOV EA, do nothing. */
3554 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3558 rc = dt_create(env, child, la, NULL, dof, handle);
3562 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3568 dt_read_unlock(env, parent);
3571 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3574 lfsck_ibits_unlock(&lh, LCK_EX);
3578 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3579 "dangling reference for: parent "DFID", child "
3580 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3582 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3583 comp_id, ea_off, ost_idx,
3584 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3585 "Create the lost OST-object as required" :
3586 "Keep the MDT-object there by default", rc);
3592 * Repair the MDT-object with dangling LOV EA reference.
3594 * Prepare parameters and call __lfsck_layout_repair_dangling()
3595 * to repair the dangling LOV EA reference.
3597 * \param[in] env pointer to the thread context
3598 * \param[in] com the layout LFSCK component
3599 * \param[in] pfid the MDT-object's FID
3600 * \param[in] cfid the FID for the OST-object to be created
3601 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3602 * \param[in] ea_off the offset of the OST-object in the LOV EA
3603 * \param[in] ost_idx the index of OST on which the OST-object resides
3605 * \retval +1 for repair successfully
3606 * \retval 0 for did nothing
3607 * \retval negative error number on failure
3609 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3610 struct lfsck_component *com,
3611 const struct lu_fid *pfid,
3612 const struct lu_fid *cfid,
3613 __u32 comp_id, __u32 ea_off,
3616 struct lfsck_instance *lfsck = com->lc_lfsck;
3617 struct dt_object *parent = NULL;
3618 struct dt_object *child = NULL;
3619 struct lfsck_tgt_desc *ltd;
3623 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3625 GOTO(log, rc = PTR_ERR(parent));
3627 /* The MDT-object has been removed. */
3628 if (dt_object_exists(parent) == 0)
3631 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3632 if (unlikely(ltd == NULL))
3633 GOTO(log, rc = -ENODEV);
3635 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3637 GOTO(log, rc = PTR_ERR(child));
3639 /* The OST-object has been created. */
3640 if (unlikely(dt_object_exists(child) != 0))
3643 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3644 comp_id, ea_off, ost_idx, false);
3649 if (child != NULL && !IS_ERR(child))
3650 lfsck_object_put(env, child);
3652 if (parent != NULL && !IS_ERR(parent))
3653 lfsck_object_put(env, parent);
3656 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3657 "dangling reference for: parent "DFID", child "
3658 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3659 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3660 comp_id, ea_off, ost_idx,
3661 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3662 "Create the lost OST-object as required" :
3663 "Keep the MDT-object there by default", rc);
3668 /* If the OST-object does not recognize the MDT-object as its parent, and
3669 * there is no other MDT-object claims as its parent, then just trust the
3670 * given MDT-object as its parent. So update the OST-object filter_fid. */
3671 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3672 struct lfsck_component *com,
3673 struct dt_object *parent,
3674 struct lfsck_layout_req *llr,
3677 struct lfsck_thread_info *info = lfsck_env_info(env);
3678 struct filter_fid *ff = &info->lti_ff;
3679 struct dt_object *child = llr->llr_child;
3680 struct dt_device *dev = lfsck_obj2dev(child);
3681 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3682 struct lu_buf *tbuf = &info->lti_big_buf;
3683 struct thandle *handle;
3685 struct lustre_handle lh = { 0 };
3689 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3690 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3695 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3696 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3697 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3698 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3699 * parent MDT-object's layout EA. */
3700 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3702 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3703 if (unlikely(rc == -ENODATA))
3708 rc = lfsck_lov2layout(tbuf->lb_buf, ff, llr->llr_comp_id);
3712 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3714 handle = dt_trans_create(env, dev);
3716 GOTO(unlock1, rc = PTR_ERR(handle));
3718 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3722 rc = dt_attr_get(env, parent, la);
3726 la->la_valid = LA_UID | LA_GID;
3727 rc = dt_declare_attr_set(env, child, la, handle);
3731 rc = dt_trans_start_local(env, dev, handle);
3735 dt_write_lock(env, parent, 0);
3736 if (unlikely(lfsck_is_dead_obj(parent)))
3737 GOTO(unlock2, rc = 1);
3739 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3743 /* Get the latest parent's owner. */
3744 rc = dt_attr_get(env, parent, la);
3748 la->la_valid = LA_UID | LA_GID;
3749 rc = dt_attr_set(env, child, la, handle);
3754 dt_write_unlock(env, parent);
3757 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3760 lfsck_ibits_unlock(&lh, LCK_EX);
3764 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3765 "unmatched MDT-OST pair for: parent "DFID
3766 ", child "DFID", comp_id %u, OST-index %u, "
3767 "stripe-index %u, owner %u/%u: rc = %d\n",
3768 lfsck_lfsck2name(com->lc_lfsck),
3769 PFID(lfsck_dto2fid(parent)),
3770 PFID(lfsck_dto2fid(child)),
3771 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3772 la->la_uid, la->la_gid, rc);
3777 /* If there are more than one MDT-objects claim as the OST-object's parent,
3778 * and the OST-object only recognizes one of them, then we need to generate
3779 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3780 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3781 struct lfsck_component *com,
3782 struct dt_object *parent,
3783 struct lfsck_layout_req *llr,
3786 struct lfsck_thread_info *info = lfsck_env_info(env);
3787 struct dt_allocation_hint *hint = &info->lti_hint;
3788 struct dt_object_format *dof = &info->lti_dof;
3789 struct ost_id *oi = &info->lti_oi;
3790 struct lu_buf *buf = &info->lti_big_buf;
3791 struct lfsck_instance *lfsck = com->lc_lfsck;
3792 struct dt_device *dev;
3793 struct lu_device *d =
3794 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3795 struct lu_object *o;
3796 struct lu_object *n;
3797 struct dt_object *child = NULL;
3798 struct thandle *handle = NULL;
3799 struct lov_mds_md_v1 *lmm;
3800 struct lov_ost_data_v1 *objs;
3801 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3803 struct lustre_handle lh = { 0 };
3809 /* We use two separated transactions to repair the inconsistency.
3811 * 1) create the child (OST-object).
3812 * 2) update the parent LOV EA according to the child's FID.
3814 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3815 * handled as orphan when the layout LFSCK run next time.
3817 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3818 * as dangling referened case when the layout LFSCK run next time. */
3820 /* The 1st transaction. */
3821 o = lu_object_anon(env, d, NULL);
3823 GOTO(log, rc = PTR_ERR(o));
3825 n = lu_object_locate(o->lo_header, d->ld_type);
3826 if (unlikely(n == NULL)) {
3827 lu_object_put_nocache(env, o);
3829 GOTO(log, rc = -EINVAL);
3832 child = container_of(n, struct dt_object, do_lu);
3833 memset(hint, 0, sizeof(*hint));
3834 rc = dt_attr_get(env, parent, la);
3838 la->la_valid = LA_UID | LA_GID;
3839 memset(dof, 0, sizeof(*dof));
3841 dev = lfsck_obj2dev(child);
3842 handle = dt_trans_create(env, dev);
3844 GOTO(log, rc = PTR_ERR(handle));
3846 rc = dt_declare_create(env, child, la, hint, dof, handle);
3850 rc = dt_trans_start_local(env, dev, handle);
3854 rc = dt_create(env, child, la, hint, dof, handle);
3855 dt_trans_stop(env, dev, handle);
3860 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3861 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3866 /* The 2nd transaction. */
3868 /* XXX: Generally, we should use bottom device (OSD) to update parent
3869 * LOV EA. But because the LOD-object still references the wrong
3870 * OSP-object that should be detached after the parent's LOV EA
3871 * refreshed. Unfortunately, there is no suitable API for that.
3872 * So we have to make the LOD to re-load the OSP-object(s) via
3873 * replacing the LOV EA against the LOD-object.
3875 * Once the DNE2 patches have been landed, we can replace the
3876 * LOD device with the OSD device. LU-6230. */
3878 dev = lfsck->li_next;
3879 parent = lfsck_object_locate(dev, parent);
3881 GOTO(log, rc = PTR_ERR(parent));
3883 handle = dt_trans_create(env, dev);
3885 GOTO(log, rc = PTR_ERR(handle));
3887 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3888 LU_XATTR_REPLACE, handle);
3892 rc = dt_trans_start_local(env, dev, handle);
3896 dt_write_lock(env, parent, 0);
3897 if (unlikely(lfsck_is_dead_obj(parent)))
3898 GOTO(unlock, rc = 0);
3900 rc = lfsck_layout_get_lovea(env, parent, buf);
3901 if (unlikely(rc == -ENODATA))
3907 magic = le32_to_cpu(lmm->lmm_magic);
3908 if (magic == LOV_MAGIC_COMP_V1) {
3909 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3910 struct lov_comp_md_entry_v1 *lcme;
3911 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3914 LASSERT(llr->llr_comp_id != 0);
3916 for (i = 0; i < count; i++) {
3917 lcme = &lcm->lcm_entries[i];
3918 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3919 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3922 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3924 le32_to_cpu(lcme->lcme_offset);
3925 magic = le32_to_cpu(lmm->lmm_magic);
3930 GOTO(unlock, rc = 0);
3934 if (magic == LOV_MAGIC_V1) {
3935 objs = &lmm->lmm_objects[llr->llr_lov_idx];
3937 LASSERT(magic == LOV_MAGIC_V3);
3939 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3942 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3943 index = le32_to_cpu(objs->l_ost_idx);
3944 rc = ostid_to_fid(&tfid, oi, index);
3945 /* Someone changed layout during the LFSCK, no need to repair then. */
3946 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3947 GOTO(unlock, rc = 0);
3949 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3950 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3951 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3952 objs->l_ost_gen = cpu_to_le32(0);
3953 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3954 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3955 LU_XATTR_REPLACE, handle);
3957 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3960 dt_write_unlock(env, parent);
3964 dt_trans_stop(env, dev, handle);
3967 lfsck_ibits_unlock(&lh, LCK_EX);
3969 lfsck_object_put(env, child);
3972 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3973 "multiple references for: parent "DFID", comp_id %u, "
3974 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
3975 lfsck_lfsck2name(lfsck), PFID(pfid),
3976 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3977 la->la_uid, la->la_gid, rc);
3982 /* If the MDT-object and the OST-object have different owner information,
3983 * then trust the MDT-object, because the normal chown/chgrp handle order
3984 * is from MDT to OST, and it is possible that some chown/chgrp operation
3985 * is partly done. */
3986 static int lfsck_layout_repair_owner(const struct lu_env *env,
3987 struct lfsck_component *com,
3988 struct dt_object *parent,
3989 struct lfsck_layout_req *llr,
3990 struct lu_attr *pla,
3991 const struct lu_attr *cla)
3993 struct lfsck_thread_info *info = lfsck_env_info(env);
3994 struct lu_attr *tla = &info->lti_la2;
3995 struct dt_object *child = llr->llr_child;
3996 struct dt_device *dev = lfsck_obj2dev(child);
3997 struct thandle *handle;
3999 dt_obj_version_t version;
4002 tla->la_uid = pla->la_uid;
4003 tla->la_gid = pla->la_gid;
4004 tla->la_valid = LA_UID | LA_GID;
4005 handle = dt_trans_create(env, dev);
4007 GOTO(log, rc = PTR_ERR(handle));
4009 rc = dt_declare_attr_set(env, child, tla, handle);
4013 rc = dt_trans_start_local(env, dev, handle);
4017 /* Use the dt_object lock to serialize with destroy and attr_set. */
4018 dt_read_lock(env, parent, 0);
4019 if (unlikely(lfsck_is_dead_obj(parent)))
4020 GOTO(unlock, rc = 1);
4022 version = dt_version_get(env, child);
4023 if (version == -EOPNOTSUPP)
4026 /* Get the latest parent's owner. */
4027 rc = dt_attr_get(env, parent, pla);
4031 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
4032 if (unlikely((!version && tla->la_ctime == 0) ||
4033 tla->la_uid != pla->la_uid || tla->la_gid != pla->la_gid))
4036 rc = dt_attr_set(env, child, tla, handle);
4041 dt_read_unlock(env, parent);
4044 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
4048 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4049 "inconsistent file owner for: parent "DFID", child "DFID
4050 ", OST-index %u, stripe-index %u, old owner %u/%u, "
4051 "new owner %u/%u: rc = %d\n",
4052 lfsck_lfsck2name(com->lc_lfsck),
4053 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4054 llr->llr_ost_idx, llr->llr_lov_idx,
4055 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
4060 /* Check whether the OST-object correctly back points to the
4061 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
4062 static int lfsck_layout_check_parent(const struct lu_env *env,
4063 struct lfsck_component *com,
4064 struct lfsck_assistant_object *lso,
4065 struct filter_fid *ff,
4066 const struct lu_fid *cfid,
4067 const struct lu_attr *cla,
4068 struct lfsck_layout_req *llr)
4070 struct lfsck_thread_info *info = lfsck_env_info(env);
4071 struct lu_buf *buf = &info->lti_big_buf;
4072 struct lu_fid *pfid = &info->lti_fid;
4073 struct dt_object *tobj;
4074 struct lov_mds_md_v1 *lmm;
4075 struct lov_ost_data_v1 *objs;
4076 struct lustre_handle lh = { 0 };
4084 *pfid = ff->ff_parent;
4085 idx = pfid->f_stripe_idx;
4088 if (unlikely(!fid_is_sane(pfid)))
4089 RETURN(LLIT_UNMATCHED_PAIR);
4091 if (lu_fid_eq(pfid, &lso->lso_fid)) {
4092 if (likely(llr->llr_lov_idx == idx))
4095 RETURN(LLIT_UNMATCHED_PAIR);
4098 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4100 RETURN(PTR_ERR(tobj));
4102 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
4103 !S_ISREG(lfsck_object_type(tobj)))
4104 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4106 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
4107 * remote one on another MDT. Then check whether the given OST-object
4108 * is in such layout. If yes, it is multiple referenced, otherwise it
4109 * is unmatched referenced case. */
4110 rc = lfsck_layout_get_lovea(env, tobj, buf);
4111 if (rc == 0 || rc == -ENODATA || rc == -ENOENT)
4112 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4114 if (unlikely(rc == -EOPNOTSUPP))
4115 GOTO(out, rc = LLIT_NONE);
4121 magic = le32_to_cpu(lmm->lmm_magic);
4122 if (magic == LOV_MAGIC_COMP_V1) {
4123 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4124 struct lov_comp_md_entry_v1 *lcme;
4126 if (ff->ff_layout.ol_comp_id == 0)
4127 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4129 count = le16_to_cpu(lcm->lcm_entry_count);
4130 for (i = 0; i < count; i++) {
4131 lcme = &lcm->lcm_entries[i];
4132 if (le32_to_cpu(lcme->lcme_id) ==
4133 ff->ff_layout.ol_comp_id) {
4135 le32_to_cpu(lcme->lcme_offset);
4136 magic = le32_to_cpu(lmm->lmm_magic);
4137 if (!(le32_to_cpu(lcme->lcme_flags) &
4139 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4145 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4149 if (magic == LOV_MAGIC_V1) {
4150 objs = &lmm->lmm_objects[0];
4152 LASSERT(magic == LOV_MAGIC_V3);
4153 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4156 count = le16_to_cpu(lmm->lmm_stripe_count);
4157 for (i = 0; i < count; i++, objs++) {
4158 struct lu_fid *tfid = &info->lti_fid2;
4159 struct ost_id *oi = &info->lti_oi;
4162 if (lovea_slot_is_dummy(objs))
4165 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4166 idx2 = le32_to_cpu(objs->l_ost_idx);
4167 rc = ostid_to_fid(tfid, oi, idx2);
4169 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4170 "invalid layout EA at the slot %d, index %u\n",
4171 lfsck_lfsck2name(com->lc_lfsck),
4172 PFID(pfid), i, idx2);
4174 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4177 if (lu_fid_eq(cfid, tfid)) {
4178 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4179 MDS_INODELOCK_UPDATE |
4180 MDS_INODELOCK_LAYOUT |
4181 MDS_INODELOCK_XATTR,
4186 dt_read_lock(env, tobj, 0);
4188 /* For local MDT-object, re-check existence
4189 * after taken the lock. */
4190 if (!dt_object_remote(tobj)) {
4191 if (dt_object_exists(tobj) == 0 ||
4192 lfsck_is_dead_obj(tobj))
4193 rc = LLIT_UNMATCHED_PAIR;
4195 rc = LLIT_MULTIPLE_REFERENCED;
4200 /* For migration case, the new MDT-object and old
4201 * MDT-object may reference the same OST-object at
4202 * some migration internal time.
4204 * For remote MDT-object, the local MDT may not know
4205 * whether it has been removed or not. Try checking
4206 * for a non-existent xattr to check if this object
4207 * has been been removed or not. */
4208 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4210 if (unlikely(rc == -ENOENT || rc >= 0))
4211 rc = LLIT_UNMATCHED_PAIR;
4212 else if (rc == -ENODATA)
4213 rc = LLIT_MULTIPLE_REFERENCED;
4219 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4222 if (lustre_handle_is_used(&lh)) {
4223 dt_read_unlock(env, tobj);
4224 lfsck_ibits_unlock(&lh, LCK_EX);
4228 lfsck_object_put(env, tobj);
4233 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4234 struct lfsck_component *com,
4235 struct lfsck_assistant_req *lar)
4237 struct lfsck_layout_req *llr =
4238 container_of0(lar, struct lfsck_layout_req, llr_lar);
4239 struct lfsck_assistant_object *lso = lar->lar_parent;
4240 struct lfsck_layout *lo = com->lc_file_ram;
4241 struct lfsck_thread_info *info = lfsck_env_info(env);
4242 struct filter_fid *ff = &info->lti_ff;
4243 struct lu_buf buf = { .lb_buf = ff,
4244 .lb_len = sizeof(*ff) };
4245 struct dt_object *parent = NULL;
4246 struct dt_object *child = llr->llr_child;
4247 struct lu_attr *pla = &lso->lso_attr;
4248 struct lu_attr *cla = &info->lti_la;
4249 struct lfsck_instance *lfsck = com->lc_lfsck;
4250 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4251 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4258 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
4260 rc = dt_attr_get(env, child, cla);
4261 if (rc == -ENOENT) {
4262 parent = lfsck_assistant_object_load(env, lfsck, lso);
4263 if (IS_ERR(parent)) {
4264 rc = PTR_ERR(parent);
4266 RETURN(rc == -ENOENT ? 0 : rc);
4269 type = LLIT_DANGLING;
4276 lfsck_buf_init(&buf, ff, sizeof(*ff));
4277 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4278 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4279 type = LLIT_UNMATCHED_PAIR;
4283 if (rc < 0 && rc != -ENODATA)
4286 if (rc == 0 || rc == -ENODATA)
4287 GOTO(check_owner, rc = 0);
4289 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4290 rc = lfsck_layout_check_parent(env, com, lso, ff,
4291 lu_object_fid(&child->do_lu), cla, llr);
4301 /* Someone may has changed the owner after the parent attr pre-loaded.
4302 * It can be handled later inside the lfsck_layout_repair_owner(). */
4303 if (unlikely(cla->la_uid != pla->la_uid ||
4304 cla->la_gid != pla->la_gid)) {
4305 type = LLIT_INCONSISTENT_OWNER;
4310 if (type == LLIT_NONE)
4313 if (bk->lb_param & LPF_DRYRUN)
4316 if (parent == NULL) {
4317 parent = lfsck_assistant_object_load(env, lfsck, lso);
4318 if (IS_ERR(parent)) {
4319 rc = PTR_ERR(parent);
4330 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4331 rc = lfsck_layout_ins_dangling_rec(env, com,
4332 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4333 llr->llr_comp_id, llr->llr_lov_idx,
4336 rc = __lfsck_layout_repair_dangling(env, com, parent,
4343 case LLIT_UNMATCHED_PAIR:
4344 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4347 case LLIT_MULTIPLE_REFERENCED:
4348 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4351 case LLIT_INCONSISTENT_OWNER:
4352 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4362 down_write(&com->lc_sem);
4364 struct lfsck_assistant_data *lad = com->lc_data;
4366 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags))) {
4368 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4369 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4370 rc == -EHOSTUNREACH) {
4371 /* If cannot touch the target server,
4372 * mark the LFSCK as INCOMPLETE. */
4373 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4374 "talk with OST %x: rc = %d\n",
4375 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4376 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4377 lo->ll_objs_skipped++;
4380 lfsck_layout_record_failure(env, lfsck, lo);
4382 } else if (rc > 0 && (type != LLIT_DANGLING ||
4383 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4384 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4385 "unknown type = %d\n", type);
4387 lo->ll_objs_repaired[type - 1]++;
4388 if (bk->lb_param & LPF_DRYRUN &&
4389 unlikely(lo->ll_pos_first_inconsistent == 0))
4390 lo->ll_pos_first_inconsistent =
4391 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4394 up_write(&com->lc_sem);
4396 if (parent != NULL && !IS_ERR(parent))
4397 lfsck_object_put(env, parent);
4403 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4404 struct lfsck_component *com,
4405 struct dt_object *obj, bool first)
4407 struct lfsck_instance *lfsck = com->lc_lfsck;
4408 struct ptlrpc_thread *thread = &lfsck->li_thread;
4409 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4410 struct lfsck_layout *lo = com->lc_file_ram;
4411 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4414 struct lfsck_layout_dangling_key *parent =
4415 &lfsck_env_info(env)->lti_lldk;
4416 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4421 di = iops->init(env, obj, 0);
4423 RETURN(PTR_ERR(di));
4426 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4428 memset(parent, 0, sizeof(*parent));
4429 rc = iops->get(env, di, (const struct dt_key *)parent);
4434 /* The start one either has been processed or does not exist,
4436 rc = iops->next(env, di);
4442 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4443 unlikely(!thread_is_running(thread)))
4446 key = iops->key(env, di);
4455 lldk_be_to_cpu(parent,
4456 (const struct lfsck_layout_dangling_key *)key);
4457 if (!fid_is_sane(&parent->lldk_fid)) {
4462 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4464 fid_be_to_cpu(cfid, cfid);
4465 ost_idx = cfid->f_ver;
4467 if (!fid_is_sane(cfid)) {
4472 rc = lfsck_layout_repair_dangling(env, com,
4473 &parent->lldk_fid, cfid,
4474 parent->lldk_comp_id,
4475 parent->lldk_ea_off, ost_idx);
4479 down_write(&com->lc_sem);
4480 com->lc_new_checked++;
4481 com->lc_new_scanned++;
4483 lo->ll_lldk_latest_scanned_phase2 = *parent;
4486 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4488 lo->ll_objs_failed_phase2++;
4489 up_write(&com->lc_sem);
4491 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4494 if (unlikely(com->lc_time_next_checkpoint <=
4495 ktime_get_seconds()) &&
4496 com->lc_new_checked != 0) {
4497 down_write(&com->lc_sem);
4498 lo->ll_run_time_phase2 += ktime_get_seconds() -
4499 com->lc_time_last_checkpoint;
4500 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
4501 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4502 com->lc_new_checked = 0;
4503 lfsck_layout_store(env, com);
4504 up_write(&com->lc_sem);
4506 com->lc_time_last_checkpoint = ktime_get_seconds();
4507 com->lc_time_next_checkpoint =
4508 com->lc_time_last_checkpoint +
4509 LFSCK_CHECKPOINT_INTERVAL;
4512 lfsck_control_speed_by_self(com);
4513 if (unlikely(!thread_is_running(thread)))
4516 rc = iops->next(env, di);
4525 iops->fini(env, di);
4530 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4531 struct lfsck_component *com)
4533 struct lfsck_assistant_data *lad = com->lc_data;
4534 struct lfsck_instance *lfsck = com->lc_lfsck;
4535 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4536 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4537 struct lfsck_tgt_desc *ltd;
4541 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4542 lfsck_lfsck2name(lfsck));
4544 spin_lock(<ds->ltd_lock);
4545 while (!list_empty(&lad->lad_ost_phase2_list)) {
4546 ltd = list_entry(lad->lad_ost_phase2_list.next,
4547 struct lfsck_tgt_desc,
4548 ltd_layout_phase_list);
4549 list_del_init(<d->ltd_layout_phase_list);
4550 if (bk->lb_param & LPF_OST_ORPHAN) {
4551 spin_unlock(<ds->ltd_lock);
4552 rc = lfsck_layout_scan_orphan(env, com, ltd);
4553 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4556 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags) ||
4557 !thread_is_running(&lfsck->li_thread)))
4559 spin_lock(<ds->ltd_lock);
4563 if (list_empty(&lad->lad_ost_phase1_list))
4567 spin_unlock(<ds->ltd_lock);
4569 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4570 struct lfsck_layout *lo = com->lc_file_ram;
4573 com->lc_new_checked = 0;
4574 com->lc_new_scanned = 0;
4575 com->lc_time_last_checkpoint = ktime_get_seconds();
4576 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4577 LFSCK_CHECKPOINT_INTERVAL;
4579 i = lfsck_sub_trace_file_fid2idx(
4580 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4581 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4582 com->lc_sub_trace_objs[i].lsto_obj, true);
4583 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4584 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4585 com->lc_sub_trace_objs[i].lsto_obj, false);
4587 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4588 "at the No. %d trace file: rc = %d\n",
4589 lfsck_lfsck2name(lfsck), i, rc);
4592 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4593 lfsck_lfsck2name(lfsck), rc);
4599 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4600 struct ptlrpc_request *req,
4603 struct lfsck_layout_slave_async_args *llsaa = args;
4604 struct obd_export *exp = llsaa->llsaa_exp;
4605 struct lfsck_component *com = llsaa->llsaa_com;
4606 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4607 struct lfsck_layout_slave_data *llsd = com->lc_data;
4608 struct lfsck_reply *lr = NULL;
4612 /* It is probably caused by network trouble, or target crash,
4613 * it will try several times (depends on the obd_timeout, and
4614 * will not less than 3 times). But to make the LFSCK can go
4615 * ahead, we should not try for ever. After some try but still
4616 * hit failure, it will assume that the target exit the LFSCK
4617 * prcoessing and stop try. */
4618 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4619 int max_try = max_t(int, obd_timeout / 30, 3);
4621 if (++(llst->llst_failures) > max_try)
4627 llst->llst_failures = 0;
4628 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4629 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4630 lr->lr_status != LS_SCANNING_PHASE2)
4635 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4636 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4637 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4638 llst->llst_failures);
4640 lfsck_layout_llst_del(llsd, llst);
4643 lfsck_layout_llst_put(llst);
4644 lfsck_component_put(env, com);
4645 class_export_put(exp);
4650 static int lfsck_layout_async_query(const struct lu_env *env,
4651 struct lfsck_component *com,
4652 struct obd_export *exp,
4653 struct lfsck_layout_slave_target *llst,
4654 struct lfsck_request *lr,
4655 struct ptlrpc_request_set *set)
4657 struct lfsck_layout_slave_async_args *llsaa;
4658 struct ptlrpc_request *req;
4659 struct lfsck_request *tmp;
4663 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4667 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4669 ptlrpc_request_free(req);
4673 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4675 ptlrpc_request_set_replen(req);
4677 llsaa = ptlrpc_req_async_args(llsaa, req);
4678 llsaa->llsaa_exp = exp;
4679 llsaa->llsaa_com = lfsck_component_get(com);
4680 llsaa->llsaa_llst = llst;
4681 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4682 req->rq_allow_intr = 1;
4683 req->rq_no_delay = 1;
4684 ptlrpc_set_add_req(set, req);
4689 static int lfsck_layout_async_notify(const struct lu_env *env,
4690 struct obd_export *exp,
4691 struct lfsck_request *lr,
4692 struct ptlrpc_request_set *set)
4694 struct ptlrpc_request *req;
4695 struct lfsck_request *tmp;
4699 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4703 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4705 ptlrpc_request_free(req);
4709 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4711 ptlrpc_request_set_replen(req);
4712 req->rq_allow_intr = 1;
4713 req->rq_no_delay = 1;
4714 ptlrpc_set_add_req(set, req);
4720 lfsck_layout_slave_query_master(const struct lu_env *env,
4721 struct lfsck_component *com)
4723 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4724 struct lfsck_instance *lfsck = com->lc_lfsck;
4725 struct lfsck_layout_slave_data *llsd = com->lc_data;
4726 struct lfsck_layout_slave_target *llst;
4727 struct obd_export *exp;
4728 struct ptlrpc_request_set *set;
4733 set = ptlrpc_prep_set();
4735 GOTO(log, rc = -ENOMEM);
4737 memset(lr, 0, sizeof(*lr));
4738 lr->lr_event = LE_QUERY;
4739 lr->lr_active = LFSCK_TYPE_LAYOUT;
4741 llsd->llsd_touch_gen++;
4742 spin_lock(&llsd->llsd_lock);
4743 while (!list_empty(&llsd->llsd_master_list)) {
4744 llst = list_entry(llsd->llsd_master_list.next,
4745 struct lfsck_layout_slave_target,
4747 if (llst->llst_gen == llsd->llsd_touch_gen)
4750 llst->llst_gen = llsd->llsd_touch_gen;
4751 list_move_tail(&llst->llst_list,
4752 &llsd->llsd_master_list);
4753 atomic_inc(&llst->llst_ref);
4754 spin_unlock(&llsd->llsd_lock);
4756 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4759 lfsck_layout_llst_del(llsd, llst);
4760 lfsck_layout_llst_put(llst);
4761 spin_lock(&llsd->llsd_lock);
4765 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4767 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4768 "query %s for layout: rc = %d\n",
4769 lfsck_lfsck2name(lfsck),
4770 exp->exp_obd->obd_name, rc);
4773 lfsck_layout_llst_put(llst);
4774 class_export_put(exp);
4776 spin_lock(&llsd->llsd_lock);
4778 spin_unlock(&llsd->llsd_lock);
4780 rc = ptlrpc_set_wait(env, set);
4781 ptlrpc_set_destroy(set);
4783 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
4786 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
4787 lfsck_lfsck2name(com->lc_lfsck), rc);
4793 lfsck_layout_slave_notify_master(const struct lu_env *env,
4794 struct lfsck_component *com,
4795 enum lfsck_events event, int result)
4797 struct lfsck_layout *lo = com->lc_file_ram;
4798 struct lfsck_instance *lfsck = com->lc_lfsck;
4799 struct lfsck_layout_slave_data *llsd = com->lc_data;
4800 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4801 struct lfsck_layout_slave_target *llst;
4802 struct obd_export *exp;
4803 struct ptlrpc_request_set *set;
4807 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
4808 lfsck_lfsck2name(com->lc_lfsck));
4810 set = ptlrpc_prep_set();
4814 memset(lr, 0, sizeof(*lr));
4815 lr->lr_event = event;
4816 lr->lr_flags = LEF_FROM_OST;
4817 lr->lr_status = result;
4818 lr->lr_index = lfsck_dev_idx(lfsck);
4819 lr->lr_active = LFSCK_TYPE_LAYOUT;
4820 lr->lr_flags2 = lo->ll_flags;
4821 llsd->llsd_touch_gen++;
4822 spin_lock(&llsd->llsd_lock);
4823 while (!list_empty(&llsd->llsd_master_list)) {
4824 llst = list_entry(llsd->llsd_master_list.next,
4825 struct lfsck_layout_slave_target,
4827 if (llst->llst_gen == llsd->llsd_touch_gen)
4830 llst->llst_gen = llsd->llsd_touch_gen;
4831 list_move_tail(&llst->llst_list,
4832 &llsd->llsd_master_list);
4833 atomic_inc(&llst->llst_ref);
4834 spin_unlock(&llsd->llsd_lock);
4836 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4839 lfsck_layout_llst_del(llsd, llst);
4840 lfsck_layout_llst_put(llst);
4841 spin_lock(&llsd->llsd_lock);
4845 rc = lfsck_layout_async_notify(env, exp, lr, set);
4847 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4848 "notify %s for layout: rc = %d\n",
4849 lfsck_lfsck2name(lfsck),
4850 exp->exp_obd->obd_name, rc);
4852 lfsck_layout_llst_put(llst);
4853 class_export_put(exp);
4854 spin_lock(&llsd->llsd_lock);
4856 spin_unlock(&llsd->llsd_lock);
4858 ptlrpc_set_wait(env, set);
4859 ptlrpc_set_destroy(set);
4865 * \ret -ENODATA: unrecognized stripe
4866 * \ret = 0 : recognized stripe
4867 * \ret < 0 : other failures
4869 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
4870 struct lfsck_component *com,
4871 struct lu_fid *cfid,
4872 struct lu_fid *pfid, __u32 comp_id)
4874 struct lfsck_thread_info *info = lfsck_env_info(env);
4875 struct lu_buf *buf = &info->lti_big_buf;
4876 struct ost_id *oi = &info->lti_oi;
4877 struct dt_object *obj;
4878 struct lov_mds_md_v1 *lmm;
4879 struct lov_ost_data_v1 *objs;
4880 __u32 idx = pfid->f_stripe_idx;
4888 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4890 RETURN(PTR_ERR(obj));
4892 dt_read_lock(env, obj, 0);
4893 if (unlikely(dt_object_exists(obj) == 0 ||
4894 lfsck_is_dead_obj(obj)))
4895 GOTO(unlock, rc = -ENOENT);
4897 if (!S_ISREG(lfsck_object_type(obj)))
4898 GOTO(unlock, rc = -ENODATA);
4900 rc = lfsck_layout_get_lovea(env, obj, buf);
4905 magic = le32_to_cpu(lmm->lmm_magic);
4906 if (magic == LOV_MAGIC_COMP_V1) {
4907 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4908 struct lov_comp_md_entry_v1 *lcme;
4911 GOTO(unlock, rc = -ENODATA);
4913 count = le16_to_cpu(lcm->lcm_entry_count);
4914 for (i = 0; i < count; i++) {
4915 lcme = &lcm->lcm_entries[i];
4916 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
4918 le32_to_cpu(lcme->lcme_offset);
4919 magic = le32_to_cpu(lmm->lmm_magic);
4920 if (!(le32_to_cpu(lcme->lcme_flags) &
4922 GOTO(unlock, rc = -ENODATA);
4928 GOTO(unlock, rc = -ENODATA);
4932 if (magic == LOV_MAGIC_V1) {
4933 objs = &lmm->lmm_objects[0];
4935 LASSERT(magic == LOV_MAGIC_V3);
4936 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4939 fid_to_ostid(cfid, oi);
4940 count = le16_to_cpu(lmm->lmm_stripe_count);
4941 for (i = 0; i < count; i++, objs++) {
4944 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
4945 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
4946 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
4949 GOTO(unlock, rc = -ENODATA);
4952 dt_read_unlock(env, obj);
4953 lfsck_object_put(env, obj);
4959 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
4960 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
4961 * layout EA from MDT to OST. On one hand, the OST no need to understand
4962 * the layout EA structure; on the other hand, it may cause trouble when
4963 * transfer large layout EA from MDT to OST via normal OUT RPC.
4965 * \ret > 0: unrecognized stripe
4966 * \ret = 0: recognized stripe
4967 * \ret < 0: other failures
4969 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
4970 struct lfsck_component *com,
4971 struct lu_fid *cfid,
4972 struct lu_fid *pfid, __u32 comp_id)
4974 struct lfsck_instance *lfsck = com->lc_lfsck;
4975 struct obd_device *obd = lfsck->li_obd;
4976 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4977 struct obd_export *exp = NULL;
4978 struct ptlrpc_request *req = NULL;
4979 struct lfsck_request *lr;
4980 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
4984 if (unlikely(fid_is_idif(pfid)))
4987 fld_range_set_any(range);
4988 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
4990 RETURN(rc == -ENOENT ? 1 : rc);
4992 if (unlikely(!fld_range_is_mdt(range)))
4995 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
4996 if (unlikely(exp == NULL))
4999 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
5000 GOTO(out, rc = -EOPNOTSUPP);
5002 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
5004 GOTO(out, rc = -ENOMEM);
5006 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
5008 ptlrpc_request_free(req);
5013 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
5014 memset(lr, 0, sizeof(*lr));
5015 lr->lr_event = LE_PAIRS_VERIFY;
5016 lr->lr_active = LFSCK_TYPE_LAYOUT;
5017 lr->lr_fid = *cfid; /* OST-object itself FID. */
5018 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
5019 lr->lr_comp_id = comp_id;
5021 ptlrpc_request_set_replen(req);
5022 rc = ptlrpc_queue_wait(req);
5023 ptlrpc_req_finished(req);
5025 if (rc == -ENOENT || rc == -ENODATA)
5032 class_export_put(exp);
5037 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
5038 struct lfsck_component *com,
5039 struct lfsck_req_local *lrl)
5041 struct dt_object *obj;
5045 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
5047 GOTO(log, rc = PTR_ERR(obj));
5049 dt_write_lock(env, obj, 0);
5050 if (unlikely(dt_object_exists(obj) == 0 ||
5051 lfsck_is_dead_obj(obj)))
5052 GOTO(unlock, rc = 0);
5054 rc = __lfsck_layout_update_pfid(env, obj, &lrl->lrl_ff_client.ff_parent,
5055 &lrl->lrl_ff_client.ff_layout,
5056 lrl->lrl_ff_client.ff_layout_version,
5057 lrl->lrl_ff_client.ff_range,
5058 lrl->lrl_ff_client.ff_parent.f_ver);
5063 dt_write_unlock(env, obj);
5064 lfsck_object_put(env, obj);
5067 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
5068 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
5069 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
5076 static void lfsck_layout_slave_quit(const struct lu_env *env,
5077 struct lfsck_component *com);
5079 static int lfsck_layout_reset(const struct lu_env *env,
5080 struct lfsck_component *com, bool init)
5082 struct lfsck_layout *lo = com->lc_file_ram;
5085 down_write(&com->lc_sem);
5087 memset(lo, 0, com->lc_file_size);
5089 __u32 count = lo->ll_success_count;
5090 time64_t last_time = lo->ll_time_last_complete;
5092 memset(lo, 0, com->lc_file_size);
5093 lo->ll_success_count = count;
5094 lo->ll_time_last_complete = last_time;
5097 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
5098 lo->ll_status = LS_INIT;
5100 if (com->lc_lfsck->li_master) {
5101 struct lfsck_assistant_data *lad = com->lc_data;
5103 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
5104 CFS_RESET_BITMAP(lad->lad_bitmap);
5107 rc = lfsck_layout_store(env, com);
5108 if (rc == 0 && com->lc_lfsck->li_master)
5109 rc = lfsck_load_sub_trace_files(env, com,
5110 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
5111 up_write(&com->lc_sem);
5113 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
5114 lfsck_lfsck2name(com->lc_lfsck), rc);
5119 static void lfsck_layout_fail(const struct lu_env *env,
5120 struct lfsck_component *com, bool new_checked)
5122 struct lfsck_layout *lo = com->lc_file_ram;
5124 down_write(&com->lc_sem);
5126 com->lc_new_checked++;
5127 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
5128 up_write(&com->lc_sem);
5131 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
5132 struct lfsck_component *com, bool init)
5134 struct lfsck_instance *lfsck = com->lc_lfsck;
5135 struct lfsck_layout *lo = com->lc_file_ram;
5139 rc = lfsck_checkpoint_generic(env, com);
5141 return rc > 0 ? 0 : rc;
5144 down_write(&com->lc_sem);
5146 lo->ll_pos_latest_start =
5147 lfsck->li_pos_checkpoint.lp_oit_cookie;
5149 lo->ll_pos_last_checkpoint =
5150 lfsck->li_pos_checkpoint.lp_oit_cookie;
5151 lo->ll_run_time_phase1 += ktime_get_seconds() -
5152 lfsck->li_time_last_checkpoint;
5153 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5154 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5155 com->lc_new_checked = 0;
5158 rc = lfsck_layout_store(env, com);
5159 up_write(&com->lc_sem);
5161 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5162 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5163 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5168 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5169 struct lfsck_component *com, bool init)
5171 struct lfsck_instance *lfsck = com->lc_lfsck;
5172 struct lfsck_layout *lo = com->lc_file_ram;
5175 if (com->lc_new_checked == 0 && !init)
5178 down_write(&com->lc_sem);
5180 lo->ll_pos_latest_start =
5181 lfsck->li_pos_checkpoint.lp_oit_cookie;
5183 lo->ll_pos_last_checkpoint =
5184 lfsck->li_pos_checkpoint.lp_oit_cookie;
5185 lo->ll_run_time_phase1 += ktime_get_seconds() -
5186 lfsck->li_time_last_checkpoint;
5187 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5188 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5189 com->lc_new_checked = 0;
5192 rc = lfsck_layout_store(env, com);
5193 up_write(&com->lc_sem);
5195 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5196 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5197 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5202 static int lfsck_layout_prep(const struct lu_env *env,
5203 struct lfsck_component *com,
5204 struct lfsck_start *start)
5206 struct lfsck_instance *lfsck = com->lc_lfsck;
5207 struct lfsck_layout *lo = com->lc_file_ram;
5208 struct lfsck_position *pos = &com->lc_pos_start;
5210 fid_zero(&pos->lp_dir_parent);
5211 pos->lp_dir_cookie = 0;
5212 if (lo->ll_status == LS_COMPLETED ||
5213 lo->ll_status == LS_PARTIAL ||
5214 /* To handle orphan, must scan from the beginning. */
5215 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5218 rc = lfsck_layout_reset(env, com, false);
5220 rc = lfsck_set_param(env, lfsck, start, true);
5223 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5224 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5230 down_write(&com->lc_sem);
5231 lo->ll_time_latest_start = ktime_get_real_seconds();
5232 spin_lock(&lfsck->li_lock);
5233 if (lo->ll_flags & LF_SCANNED_ONCE) {
5234 if (!lfsck->li_drop_dryrun ||
5235 lo->ll_pos_first_inconsistent == 0) {
5236 lo->ll_status = LS_SCANNING_PHASE2;
5237 list_move_tail(&com->lc_link,
5238 &lfsck->li_list_double_scan);
5239 pos->lp_oit_cookie = 0;
5243 lo->ll_status = LS_SCANNING_PHASE1;
5244 lo->ll_run_time_phase1 = 0;
5245 lo->ll_run_time_phase2 = 0;
5246 lo->ll_objs_checked_phase1 = 0;
5247 lo->ll_objs_checked_phase2 = 0;
5248 lo->ll_objs_failed_phase1 = 0;
5249 lo->ll_objs_failed_phase2 = 0;
5250 for (i = 0; i < LLIT_MAX; i++)
5251 lo->ll_objs_repaired[i] = 0;
5253 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5254 fid_zero(&com->lc_fid_latest_scanned_phase2);
5257 lo->ll_status = LS_SCANNING_PHASE1;
5258 if (!lfsck->li_drop_dryrun ||
5259 lo->ll_pos_first_inconsistent == 0)
5260 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5262 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5264 spin_unlock(&lfsck->li_lock);
5265 up_write(&com->lc_sem);
5270 static int lfsck_layout_slave_prep(const struct lu_env *env,
5271 struct lfsck_component *com,
5272 struct lfsck_start_param *lsp)
5274 struct lfsck_layout_slave_data *llsd = com->lc_data;
5275 struct lfsck_instance *lfsck = com->lc_lfsck;
5276 struct lfsck_layout *lo = com->lc_file_ram;
5277 struct lfsck_start *start = lsp->lsp_start;
5280 rc = lfsck_layout_prep(env, com, start);
5284 if (lo->ll_flags & LF_CRASHED_LASTID &&
5285 list_empty(&llsd->llsd_master_list)) {
5286 LASSERT(lfsck->li_out_notify != NULL);
5288 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5289 LE_LASTID_REBUILDING);
5292 if (!lsp->lsp_index_valid)
5295 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5296 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5297 LASSERT(!llsd->llsd_rbtree_valid);
5299 down_write(&llsd->llsd_rb_rwsem);
5300 rc = lfsck_rbtree_setup(env, com);
5301 up_write(&llsd->llsd_rb_rwsem);
5304 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5305 "%llu]\n", lfsck_lfsck2name(lfsck),
5306 com->lc_pos_start.lp_oit_cookie);
5311 static int lfsck_layout_master_prep(const struct lu_env *env,
5312 struct lfsck_component *com,
5313 struct lfsck_start_param *lsp)
5318 rc = lfsck_layout_load_bitmap(env, com);
5320 rc = lfsck_layout_reset(env, com, false);
5322 rc = lfsck_set_param(env, com->lc_lfsck,
5323 lsp->lsp_start, true);
5329 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5333 rc = lfsck_start_assistant(env, com, lsp);
5338 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5339 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5340 com->lc_pos_start.lp_oit_cookie);
5345 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5346 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5347 struct lfsck_component *com,
5348 struct dt_object *parent,
5349 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5351 struct lfsck_thread_info *info = lfsck_env_info(env);
5352 struct lfsck_instance *lfsck = com->lc_lfsck;
5353 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5354 struct lfsck_layout *lo = com->lc_file_ram;
5355 struct lfsck_assistant_data *lad = com->lc_data;
5356 struct lfsck_assistant_object *lso = NULL;
5357 struct lov_ost_data_v1 *objs;
5358 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5359 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5360 struct ptlrpc_thread *athread = &lad->lad_thread;
5368 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5369 magic = le32_to_cpu(lmm->lmm_magic);
5370 if (magic == LOV_MAGIC_V1) {
5371 objs = &lmm->lmm_objects[0];
5373 LASSERT(magic == LOV_MAGIC_V3);
5374 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5377 count = le16_to_cpu(lmm->lmm_stripe_count);
5378 for (i = 0; i < count; i++, objs++) {
5379 struct lu_fid *fid = &info->lti_fid;
5380 struct ost_id *oi = &info->lti_oi;
5381 struct lfsck_layout_req *llr;
5382 struct lfsck_tgt_desc *tgt = NULL;
5383 struct dt_object *cobj = NULL;
5385 bool wakeup = false;
5387 if (unlikely(lovea_slot_is_dummy(objs)))
5390 wait_event_idle(mthread->t_ctl_waitq,
5391 lad->lad_prefetched < bk->lb_async_windows ||
5392 !thread_is_running(mthread) ||
5393 thread_is_stopped(athread));
5395 if (unlikely(!thread_is_running(mthread)) ||
5396 thread_is_stopped(athread))
5399 if (unlikely(lfsck_is_dead_obj(parent)))
5402 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5403 index = le32_to_cpu(objs->l_ost_idx);
5404 rc = ostid_to_fid(fid, oi, index);
5406 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5407 ": "DOSTID", idx %u, comp_id %u\n",
5408 lfsck_lfsck2name(lfsck),
5409 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5414 tgt = lfsck_tgt_get(ltds, index);
5415 if (unlikely(tgt == NULL)) {
5416 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5417 "did not join the layout LFSCK, comp_id %u\n",
5418 lfsck_lfsck2name(lfsck), index, comp_id);
5419 lfsck_lad_set_bitmap(env, com, index);
5423 /* There is potential deadlock race condition between object
5424 * destroy and layout LFSCK. Consider the following scenario:
5426 * 1) The LFSCK thread obtained the parent object firstly, at
5427 * that time, the parent object has not been destroyed yet.
5429 * 2) One RPC service thread destroyed the parent and all its
5430 * children objects. Because the LFSCK is referencing the
5431 * parent object, then the parent object will be marked as
5432 * dying in RAM. On the other hand, the parent object is
5433 * referencing all its children objects, then all children
5434 * objects will be marked as dying in RAM also.
5436 * 3) The LFSCK thread tries to find some child object with
5437 * the parent object referenced. Then it will find that the
5438 * child object is dying. According to the object visibility
5439 * rules: the object with dying flag cannot be returned to
5440 * others. So the LFSCK thread has to wait until the dying
5441 * object has been purged from RAM, then it can allocate a
5442 * new object (with the same FID) in RAM. Unfortunately, the
5443 * LFSCK thread itself is referencing the parent object, and
5444 * cause the parent object cannot be purged, then cause the
5445 * child object cannot be purged also. So the LFSCK thread
5446 * will fall into deadlock.
5448 cobj = lfsck_object_find_by_dev(env, tgt->ltd_tgt, fid);
5450 if (lfsck_is_dead_obj(parent)) {
5460 rc = dt_declare_attr_get(env, cobj);
5464 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
5469 struct lu_attr *attr = &info->lti_la;
5471 rc = dt_attr_get(env, parent, attr);
5475 lso = lfsck_assistant_object_init(env,
5476 lfsck_dto2fid(parent), attr,
5477 lfsck->li_pos_current.lp_oit_cookie, false);
5486 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5494 spin_lock(&lad->lad_lock);
5495 if (lad->lad_assistant_status < 0) {
5496 spin_unlock(&lad->lad_lock);
5497 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5499 RETURN(lad->lad_assistant_status);
5502 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5503 if (lad->lad_prefetched == 0)
5506 lad->lad_prefetched++;
5507 spin_unlock(&lad->lad_lock);
5509 wake_up_all(&athread->t_ctl_waitq);
5512 down_write(&com->lc_sem);
5513 com->lc_new_checked++;
5515 lfsck_layout_record_failure(env, lfsck, lo);
5516 up_write(&com->lc_sem);
5518 if (cobj != NULL && !IS_ERR(cobj))
5519 lfsck_object_put(env, cobj);
5521 if (likely(tgt != NULL))
5524 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5532 lfsck_assistant_object_put(env, lso);
5537 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5538 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5539 * list ::lad_req_list.
5541 * For each request on above list, the lfsck_layout_assistant thread compares
5542 * the OST side attribute with local attribute, if inconsistent, then repair it.
5544 * All above processing is async mode with pipeline. */
5545 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5546 struct lfsck_component *com,
5547 struct dt_object *obj)
5549 struct lfsck_thread_info *info = lfsck_env_info(env);
5550 struct ost_id *oi = &info->lti_oi;
5551 struct lfsck_layout *lo = com->lc_file_ram;
5552 struct lfsck_assistant_data *lad = com->lc_data;
5553 struct lfsck_instance *lfsck = com->lc_lfsck;
5554 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5555 struct thandle *handle = NULL;
5556 struct lu_buf *buf = &info->lti_big_buf;
5557 struct lov_mds_md_v1 *lmm = NULL;
5558 struct dt_device *dev = lfsck_obj2dev(obj);
5559 struct lustre_handle lh = { 0 };
5560 struct lu_buf ea_buf = { NULL };
5561 struct lov_comp_md_v1 *lcm = NULL;
5562 struct lov_comp_md_entry_v1 *lcme = NULL;
5567 bool locked = false;
5568 bool stripe = false;
5569 bool bad_oi = false;
5572 if (!S_ISREG(lfsck_object_type(obj)))
5575 if (lad->lad_assistant_status < 0)
5576 GOTO(out, rc = -ESRCH);
5578 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5579 lmm_oi_cpu_to_le(oi, oi);
5580 dt_read_lock(env, obj, 0);
5585 if (dt_object_exists(obj) == 0 ||
5586 lfsck_is_dead_obj(obj))
5589 rc = lfsck_layout_get_lovea(env, obj, buf);
5590 if (rc == -EINVAL || rc == -ENODATA || rc == -EOPNOTSUPP)
5591 /* Skip bad lov EA during the 1st cycle scanning, and
5592 * try to recover it via orphan in the 2nd scanning. */
5599 magic = le32_to_cpu(lmm->lmm_magic);
5600 if (magic == LOV_MAGIC_COMP_V1) {
5604 count = le16_to_cpu(lcm->lcm_entry_count);
5605 for (i = 0; i < count; i++) {
5606 lcme = &lcm->lcm_entries[i];
5607 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5608 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) != 0)
5612 GOTO(out, stripe = true);
5613 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5614 GOTO(out, stripe = true);
5618 /* Inconsistent lmm_oi, should be repaired. */
5621 if (bk->lb_param & LPF_DRYRUN) {
5622 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5624 GOTO(out, stripe = true);
5627 if (!lustre_handle_is_used(&lh)) {
5628 dt_read_unlock(env, obj);
5630 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5631 MDS_INODELOCK_LAYOUT |
5632 MDS_INODELOCK_XATTR, LCK_EX);
5636 handle = dt_trans_create(env, dev);
5638 GOTO(out, rc = PTR_ERR(handle));
5640 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
5641 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5642 LU_XATTR_REPLACE, handle);
5646 rc = dt_trans_start_local(env, dev, handle);
5650 dt_write_lock(env, obj, 0);
5656 if (magic == LOV_MAGIC_COMP_V1) {
5659 for (i = 0; i < count; i++) {
5660 lcme = &lcm->lcm_entries[i];
5661 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5668 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5669 LU_XATTR_REPLACE, handle);
5673 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5675 GOTO(out, stripe = true);
5679 if (lustre_handle_is_used(&lh))
5680 dt_write_unlock(env, obj);
5682 dt_read_unlock(env, obj);
5685 if (handle != NULL && !IS_ERR(handle))
5686 dt_trans_stop(env, dev, handle);
5688 lfsck_ibits_unlock(&lh, LCK_EX);
5691 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5692 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5693 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5694 PFID(lfsck_dto2fid(obj)), rc);
5697 if (magic == LOV_MAGIC_COMP_V1) {
5700 for (i = 0; i < count; i++) {
5701 lcme = &lcm->lcm_entries[i];
5702 if (!(le32_to_cpu(lcme->lcme_flags) &
5706 rc = lfsck_layout_scan_stripes(env, com, obj,
5707 (struct lov_mds_md_v1 *)(buf->lb_buf +
5708 le32_to_cpu(lcme->lcme_offset)),
5709 le32_to_cpu(lcme->lcme_id));
5712 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5715 down_write(&com->lc_sem);
5716 com->lc_new_checked++;
5718 lfsck_layout_record_failure(env, lfsck, lo);
5719 up_write(&com->lc_sem);
5725 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5726 struct lfsck_component *com,
5727 struct dt_object *obj)
5729 struct lfsck_instance *lfsck = com->lc_lfsck;
5730 struct lfsck_layout *lo = com->lc_file_ram;
5731 const struct lu_fid *fid = lfsck_dto2fid(obj);
5732 struct lfsck_layout_slave_data *llsd = com->lc_data;
5733 struct lfsck_layout_seq *lls;
5739 LASSERT(llsd != NULL);
5741 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5742 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5743 struct ptlrpc_thread *thread = &lfsck->li_thread;
5745 wait_event_idle_timeout(thread->t_ctl_waitq,
5746 !thread_is_running(thread),
5747 cfs_time_seconds(1));
5750 lfsck_rbtree_update_bitmap(env, com, fid, false);
5752 down_write(&com->lc_sem);
5753 if (fid_is_idif(fid))
5755 else if (!fid_is_norm(fid) ||
5756 !fid_is_for_ostobj(env, lfsck, obj, fid))
5757 GOTO(unlock, rc = 0);
5760 com->lc_new_checked++;
5762 lls = lfsck_layout_seq_lookup(llsd, seq);
5765 if (unlikely(lls == NULL))
5766 GOTO(unlock, rc = -ENOMEM);
5768 INIT_LIST_HEAD(&lls->lls_list);
5770 rc = lfsck_layout_lastid_load(env, com, lls);
5772 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5773 "load LAST_ID for %#llx: rc = %d\n",
5774 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5775 lo->ll_objs_failed_phase1++;
5780 lfsck_layout_seq_insert(llsd, lls);
5783 if (unlikely(fid_is_last_id(fid)))
5784 GOTO(unlock, rc = 0);
5786 if (fid_is_idif(fid))
5787 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5791 if (oid > lls->lls_lastid_known)
5792 lls->lls_lastid_known = oid;
5794 if (oid > lls->lls_lastid) {
5795 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
5796 /* OFD may create new objects during LFSCK scanning. */
5797 rc = lfsck_layout_lastid_reload(env, com, lls);
5798 if (unlikely(rc != 0)) {
5799 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5800 "reload LAST_ID for %#llx: rc = %d\n",
5801 lfsck_lfsck2name(com->lc_lfsck),
5807 if (oid <= lls->lls_lastid ||
5808 lo->ll_flags & LF_CRASHED_LASTID)
5809 GOTO(unlock, rc = 0);
5811 LASSERT(lfsck->li_out_notify != NULL);
5813 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5814 LE_LASTID_REBUILDING);
5815 lo->ll_flags |= LF_CRASHED_LASTID;
5817 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
5818 "LAST_ID file (2) for the sequence %#llx"
5819 ", old value %llu, known value %llu\n",
5820 lfsck_lfsck2name(lfsck), lls->lls_seq,
5821 lls->lls_lastid, oid);
5824 lls->lls_lastid = oid;
5828 GOTO(unlock, rc = 0);
5831 up_write(&com->lc_sem);
5836 static int lfsck_layout_exec_dir(const struct lu_env *env,
5837 struct lfsck_component *com,
5838 struct lfsck_assistant_object *lso,
5839 struct lu_dirent *ent, __u16 type)
5844 static int lfsck_layout_master_post(const struct lu_env *env,
5845 struct lfsck_component *com,
5846 int result, bool init)
5848 struct lfsck_instance *lfsck = com->lc_lfsck;
5849 struct lfsck_layout *lo = com->lc_file_ram;
5853 lfsck_post_generic(env, com, &result);
5855 down_write(&com->lc_sem);
5856 spin_lock(&lfsck->li_lock);
5858 lo->ll_pos_last_checkpoint =
5859 lfsck->li_pos_checkpoint.lp_oit_cookie;
5862 if (lo->ll_flags & LF_INCOMPLETE)
5863 lo->ll_status = LS_PARTIAL;
5865 lo->ll_status = LS_SCANNING_PHASE2;
5866 lo->ll_flags |= LF_SCANNED_ONCE;
5867 lo->ll_flags &= ~LF_UPGRADE;
5868 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5869 } else if (result == 0) {
5870 if (lfsck->li_status != 0)
5871 lo->ll_status = lfsck->li_status;
5873 lo->ll_status = LS_STOPPED;
5874 if (lo->ll_status != LS_PAUSED)
5875 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5877 lo->ll_status = LS_FAILED;
5878 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5880 spin_unlock(&lfsck->li_lock);
5883 lo->ll_run_time_phase1 += ktime_get_seconds() -
5884 lfsck->li_time_last_checkpoint;
5885 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5886 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5887 com->lc_new_checked = 0;
5890 rc = lfsck_layout_store(env, com);
5891 up_write(&com->lc_sem);
5893 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
5894 lfsck_lfsck2name(lfsck), rc);
5899 static int lfsck_layout_slave_post(const struct lu_env *env,
5900 struct lfsck_component *com,
5901 int result, bool init)
5903 struct lfsck_instance *lfsck = com->lc_lfsck;
5904 struct lfsck_layout *lo = com->lc_file_ram;
5908 down_write(&com->lc_sem);
5909 rc = lfsck_layout_lastid_store(env, com);
5913 LASSERT(lfsck->li_out_notify != NULL);
5915 spin_lock(&lfsck->li_lock);
5917 lo->ll_pos_last_checkpoint =
5918 lfsck->li_pos_checkpoint.lp_oit_cookie;
5921 lo->ll_status = LS_SCANNING_PHASE2;
5922 lo->ll_flags |= LF_SCANNED_ONCE;
5923 if (lo->ll_flags & LF_CRASHED_LASTID) {
5925 lo->ll_flags &= ~LF_CRASHED_LASTID;
5927 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
5928 "crashed LAST_ID files successfully\n",
5929 lfsck_lfsck2name(lfsck));
5931 lo->ll_flags &= ~LF_UPGRADE;
5932 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5933 } else if (result == 0) {
5934 if (lfsck->li_status != 0)
5935 lo->ll_status = lfsck->li_status;
5937 lo->ll_status = LS_STOPPED;
5938 if (lo->ll_status != LS_PAUSED)
5939 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5941 lo->ll_status = LS_FAILED;
5942 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5944 spin_unlock(&lfsck->li_lock);
5947 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5951 lo->ll_run_time_phase1 += ktime_get_seconds() -
5952 lfsck->li_time_last_checkpoint;
5953 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5954 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5955 com->lc_new_checked = 0;
5958 rc = lfsck_layout_store(env, com);
5959 up_write(&com->lc_sem);
5961 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
5963 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
5964 lfsck_lfsck2name(lfsck), rc);
5969 static void lfsck_layout_dump(const struct lu_env *env,
5970 struct lfsck_component *com, struct seq_file *m)
5972 struct lfsck_instance *lfsck = com->lc_lfsck;
5973 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5974 struct lfsck_layout *lo = com->lc_file_ram;
5977 down_read(&com->lc_sem);
5978 if (bk->lb_param & LPF_DRYRUN)
5979 prefix = "inconsistent";
5981 prefix = "repaired";
5983 seq_printf(m, "name: lfsck_layout\n"
5989 lfsck_status2name(lo->ll_status));
5991 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
5993 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
5995 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
5997 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
5999 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
6001 seq_printf(m, "latest_start_position: %llu\n"
6002 "last_checkpoint_position: %llu\n"
6003 "first_failure_position: %llu\n",
6004 lo->ll_pos_latest_start,
6005 lo->ll_pos_last_checkpoint,
6006 lo->ll_pos_first_inconsistent);
6008 seq_printf(m, "success_count: %u\n"
6009 "%s_dangling: %llu\n"
6010 "%s_unmatched_pair: %llu\n"
6011 "%s_multiple_referenced: %llu\n"
6013 "%s_inconsistent_owner: %llu\n"
6016 "failed_phase1: %llu\n"
6017 "failed_phase2: %llu\n",
6018 lo->ll_success_count,
6019 prefix, lo->ll_objs_repaired[LLIT_DANGLING - 1],
6020 prefix, lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
6021 prefix, lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
6022 prefix, lo->ll_objs_repaired[LLIT_ORPHAN - 1],
6023 prefix, lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
6024 prefix, lo->ll_objs_repaired[LLIT_OTHERS - 1],
6025 lo->ll_objs_skipped,
6026 lo->ll_objs_failed_phase1,
6027 lo->ll_objs_failed_phase2);
6029 if (lo->ll_status == LS_SCANNING_PHASE1) {
6030 time64_t duration = ktime_get_seconds() -
6031 lfsck->li_time_last_checkpoint;
6032 u64 checked = lo->ll_objs_checked_phase1 +
6033 com->lc_new_checked;
6034 u64 speed = checked;
6035 u64 new_checked = com->lc_new_checked;
6036 time64_t rtime = lo->ll_run_time_phase1 + duration;
6040 new_checked = div64_s64(new_checked, duration);
6042 speed = div64_s64(speed, rtime);
6043 seq_printf(m, "checked_phase1: %llu\n"
6044 "checked_phase2: %llu\n"
6045 "run_time_phase1: %lld seconds\n"
6046 "run_time_phase2: %lld seconds\n"
6047 "average_speed_phase1: %llu items/sec\n"
6048 "average_speed_phase2: N/A\n"
6049 "real-time_speed_phase1: %llu items/sec\n"
6050 "real-time_speed_phase2: N/A\n",
6052 lo->ll_objs_checked_phase2,
6054 lo->ll_run_time_phase2,
6058 if (likely(lfsck->li_di_oit)) {
6059 const struct dt_it_ops *iops =
6060 &lfsck->li_obj_oit->do_index_ops->dio_it;
6062 /* The low layer otable-based iteration position may NOT
6063 * exactly match the layout-based directory traversal
6064 * cookie. Generally, it is not a serious issue. But the
6065 * caller should NOT make assumption on that. */
6066 pos = iops->store(env, lfsck->li_di_oit);
6067 if (!lfsck->li_current_oit_processed)
6070 pos = lo->ll_pos_last_checkpoint;
6073 seq_printf(m, "current_position: %llu\n", pos);
6074 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
6075 time64_t duration = ktime_get_seconds() -
6076 com->lc_time_last_checkpoint;
6077 u64 checked = lo->ll_objs_checked_phase2 +
6078 com->lc_new_checked;
6079 u64 speed1 = lo->ll_objs_checked_phase1;
6080 u64 speed2 = checked;
6081 u64 new_checked = com->lc_new_checked;
6082 time64_t rtime = lo->ll_run_time_phase2 + duration;
6085 new_checked = div64_s64(new_checked, duration);
6086 if (lo->ll_run_time_phase1 != 0)
6087 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6089 speed2 = div64_s64(speed2, rtime);
6090 seq_printf(m, "checked_phase1: %llu\n"
6091 "checked_phase2: %llu\n"
6092 "run_time_phase1: %lld seconds\n"
6093 "run_time_phase2: %lld seconds\n"
6094 "average_speed_phase1: %llu items/sec\n"
6095 "average_speed_phase2: %llu items/sec\n"
6096 "real-time_speed_phase1: N/A\n"
6097 "real-time_speed_phase2: %llu items/sec\n"
6098 "current_position: "DFID"\n",
6099 lo->ll_objs_checked_phase1,
6101 lo->ll_run_time_phase1,
6106 PFID(&com->lc_fid_latest_scanned_phase2));
6108 __u64 speed1 = lo->ll_objs_checked_phase1;
6109 __u64 speed2 = lo->ll_objs_checked_phase2;
6111 if (lo->ll_run_time_phase1 != 0)
6112 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6113 if (lo->ll_run_time_phase2 != 0)
6114 speed2 = div64_s64(speed2, lo->ll_run_time_phase2);
6115 seq_printf(m, "checked_phase1: %llu\n"
6116 "checked_phase2: %llu\n"
6117 "run_time_phase1: %lld seconds\n"
6118 "run_time_phase2: %lld seconds\n"
6119 "average_speed_phase1: %llu items/sec\n"
6120 "average_speed_phase2: %llu objs/sec\n"
6121 "real-time_speed_phase1: N/A\n"
6122 "real-time_speed_phase2: N/A\n"
6123 "current_position: N/A\n",
6124 lo->ll_objs_checked_phase1,
6125 lo->ll_objs_checked_phase2,
6126 lo->ll_run_time_phase1,
6127 lo->ll_run_time_phase2,
6132 up_read(&com->lc_sem);
6135 static int lfsck_layout_master_double_scan(const struct lu_env *env,
6136 struct lfsck_component *com)
6138 struct lfsck_layout *lo = com->lc_file_ram;
6139 struct lfsck_assistant_data *lad = com->lc_data;
6140 struct lfsck_instance *lfsck = com->lc_lfsck;
6141 struct lfsck_tgt_descs *ltds;
6142 struct lfsck_tgt_desc *ltd;
6143 struct lfsck_tgt_desc *next;
6146 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
6148 if (thread_is_stopped(&lad->lad_thread)) {
6149 LASSERT(list_empty(&lad->lad_req_list));
6150 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6151 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6153 ltds = &lfsck->li_ost_descs;
6154 spin_lock(<ds->ltd_lock);
6155 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6156 ltd_layout_phase_list) {
6157 list_del_init(<d->ltd_layout_phase_list);
6159 spin_unlock(<ds->ltd_lock);
6161 ltds = &lfsck->li_mdt_descs;
6162 spin_lock(<ds->ltd_lock);
6163 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6164 ltd_layout_phase_list) {
6165 list_del_init(<d->ltd_layout_phase_list);
6167 spin_unlock(<ds->ltd_lock);
6173 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6174 struct lfsck_component *com)
6176 struct lfsck_instance *lfsck = com->lc_lfsck;
6177 struct lfsck_layout_slave_data *llsd = com->lc_data;
6178 struct lfsck_layout *lo = com->lc_file_ram;
6179 struct ptlrpc_thread *thread = &lfsck->li_thread;
6183 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6184 lfsck_lfsck2name(lfsck));
6186 atomic_inc(&lfsck->li_double_scan_count);
6188 if (lo->ll_flags & LF_INCOMPLETE)
6191 com->lc_new_checked = 0;
6192 com->lc_new_scanned = 0;
6193 com->lc_time_last_checkpoint = ktime_get_seconds();
6194 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6195 LFSCK_CHECKPOINT_INTERVAL;
6198 rc = lfsck_layout_slave_query_master(env, com);
6199 if (list_empty(&llsd->llsd_master_list)) {
6200 if (unlikely(!thread_is_running(thread)))
6211 rc = wait_event_idle_timeout(
6212 thread->t_ctl_waitq,
6213 !thread_is_running(thread) ||
6214 lo->ll_flags & LF_INCOMPLETE ||
6215 list_empty(&llsd->llsd_master_list),
6216 cfs_time_seconds(30));
6217 if (unlikely(!thread_is_running(thread)))
6220 if (lo->ll_flags & LF_INCOMPLETE)
6230 rc = lfsck_layout_double_scan_result(env, com, rc);
6231 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6232 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6233 lfsck_layout_slave_quit(env, com);
6234 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6235 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6237 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6238 "status %d: rc = %d\n",
6239 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6244 static void lfsck_layout_master_data_release(const struct lu_env *env,
6245 struct lfsck_component *com)
6247 struct lfsck_assistant_data *lad = com->lc_data;
6248 struct lfsck_instance *lfsck = com->lc_lfsck;
6249 struct lfsck_tgt_descs *ltds;
6250 struct lfsck_tgt_desc *ltd;
6251 struct lfsck_tgt_desc *next;
6253 LASSERT(lad != NULL);
6254 LASSERT(thread_is_init(&lad->lad_thread) ||
6255 thread_is_stopped(&lad->lad_thread));
6256 LASSERT(list_empty(&lad->lad_req_list));
6258 com->lc_data = NULL;
6260 ltds = &lfsck->li_ost_descs;
6261 spin_lock(<ds->ltd_lock);
6262 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6263 ltd_layout_phase_list) {
6264 list_del_init(<d->ltd_layout_phase_list);
6266 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6267 ltd_layout_phase_list) {
6268 list_del_init(<d->ltd_layout_phase_list);
6270 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6272 list_del_init(<d->ltd_layout_list);
6274 spin_unlock(<ds->ltd_lock);
6276 ltds = &lfsck->li_mdt_descs;
6277 spin_lock(<ds->ltd_lock);
6278 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6279 ltd_layout_phase_list) {
6280 list_del_init(<d->ltd_layout_phase_list);
6282 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6283 ltd_layout_phase_list) {
6284 list_del_init(<d->ltd_layout_phase_list);
6286 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6288 list_del_init(<d->ltd_layout_list);
6290 spin_unlock(<ds->ltd_lock);
6292 if (likely(lad->lad_bitmap != NULL))
6293 CFS_FREE_BITMAP(lad->lad_bitmap);
6298 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6299 struct lfsck_component *com)
6301 struct lfsck_layout_slave_data *llsd = com->lc_data;
6303 lfsck_layout_slave_quit(env, com);
6304 com->lc_data = NULL;
6308 static void lfsck_layout_master_quit(const struct lu_env *env,
6309 struct lfsck_component *com)
6311 struct lfsck_assistant_data *lad = com->lc_data;
6312 struct lfsck_instance *lfsck = com->lc_lfsck;
6313 struct lfsck_tgt_descs *ltds;
6314 struct lfsck_tgt_desc *ltd;
6315 struct lfsck_tgt_desc *next;
6317 LASSERT(lad != NULL);
6319 lfsck_quit_generic(env, com);
6321 LASSERT(thread_is_init(&lad->lad_thread) ||
6322 thread_is_stopped(&lad->lad_thread));
6323 LASSERT(list_empty(&lad->lad_req_list));
6325 ltds = &lfsck->li_ost_descs;
6326 spin_lock(<ds->ltd_lock);
6327 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6328 ltd_layout_phase_list) {
6329 list_del_init(<d->ltd_layout_phase_list);
6331 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6332 ltd_layout_phase_list) {
6333 list_del_init(<d->ltd_layout_phase_list);
6335 spin_unlock(<ds->ltd_lock);
6337 ltds = &lfsck->li_mdt_descs;
6338 spin_lock(<ds->ltd_lock);
6339 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6340 ltd_layout_phase_list) {
6341 list_del_init(<d->ltd_layout_phase_list);
6343 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6344 ltd_layout_phase_list) {
6345 list_del_init(<d->ltd_layout_phase_list);
6347 spin_unlock(<ds->ltd_lock);
6350 static void lfsck_layout_slave_quit(const struct lu_env *env,
6351 struct lfsck_component *com)
6353 struct lfsck_layout_slave_data *llsd = com->lc_data;
6354 struct lfsck_layout_seq *lls;
6355 struct lfsck_layout_seq *next;
6356 struct lfsck_layout_slave_target *llst;
6358 LASSERT(llsd != NULL);
6360 down_write(&com->lc_sem);
6361 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6363 list_del_init(&lls->lls_list);
6364 lfsck_object_put(env, lls->lls_lastid_obj);
6367 up_write(&com->lc_sem);
6369 spin_lock(&llsd->llsd_lock);
6370 while (!list_empty(&llsd->llsd_master_list)) {
6371 llst = list_entry(llsd->llsd_master_list.next,
6372 struct lfsck_layout_slave_target, llst_list);
6373 list_del_init(&llst->llst_list);
6374 spin_unlock(&llsd->llsd_lock);
6375 lfsck_layout_llst_put(llst);
6376 spin_lock(&llsd->llsd_lock);
6378 spin_unlock(&llsd->llsd_lock);
6380 lfsck_rbtree_cleanup(env, com);
6383 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6384 struct lfsck_component *com,
6385 struct lfsck_request *lr)
6387 struct lfsck_instance *lfsck = com->lc_lfsck;
6388 struct lfsck_layout *lo = com->lc_file_ram;
6389 struct lfsck_assistant_data *lad = com->lc_data;
6390 struct lfsck_tgt_descs *ltds;
6391 struct lfsck_tgt_desc *ltd;
6395 if (lr->lr_event == LE_PAIRS_VERIFY) {
6398 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6405 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6406 "from %s %x, status %d, flags %x, flags2 %x\n",
6407 lfsck_lfsck2name(lfsck), lr->lr_event,
6408 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6409 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6411 if (lr->lr_event != LE_PHASE1_DONE &&
6412 lr->lr_event != LE_PHASE2_DONE &&
6413 lr->lr_event != LE_PEER_EXIT)
6416 if (lr->lr_flags & LEF_FROM_OST)
6417 ltds = &lfsck->li_ost_descs;
6419 ltds = &lfsck->li_mdt_descs;
6420 spin_lock(<ds->ltd_lock);
6421 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6423 spin_unlock(<ds->ltd_lock);
6428 list_del_init(<d->ltd_layout_phase_list);
6429 switch (lr->lr_event) {
6430 case LE_PHASE1_DONE:
6431 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6432 if (lr->lr_flags2 & LF_INCOMPLETE) {
6433 if (lr->lr_flags & LEF_FROM_OST)
6434 lfsck_lad_set_bitmap(env, com,
6437 lo->ll_flags |= LF_INCOMPLETE;
6439 ltd->ltd_layout_done = 1;
6440 list_del_init(<d->ltd_layout_list);
6445 if (lr->lr_flags & LEF_FROM_OST) {
6446 if (list_empty(<d->ltd_layout_list))
6447 list_add_tail(<d->ltd_layout_list,
6448 &lad->lad_ost_list);
6449 list_add_tail(<d->ltd_layout_phase_list,
6450 &lad->lad_ost_phase2_list);
6452 if (list_empty(<d->ltd_layout_list))
6453 list_add_tail(<d->ltd_layout_list,
6454 &lad->lad_mdt_list);
6455 list_add_tail(<d->ltd_layout_phase_list,
6456 &lad->lad_mdt_phase2_list);
6459 case LE_PHASE2_DONE:
6460 ltd->ltd_layout_done = 1;
6461 if (!list_empty(<d->ltd_layout_list))
6462 list_del_init(<d->ltd_layout_list);
6464 if (lr->lr_flags2 & LF_INCOMPLETE) {
6465 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6472 ltd->ltd_layout_done = 1;
6473 list_del_init(<d->ltd_layout_list);
6474 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6475 !(lr->lr_flags & LEF_FROM_OST))
6476 lo->ll_flags |= LF_INCOMPLETE;
6481 spin_unlock(<ds->ltd_lock);
6483 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6484 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6486 memset(stop, 0, sizeof(*stop));
6487 stop->ls_status = lr->lr_status;
6488 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6489 lfsck_stop(env, lfsck->li_bottom, stop);
6490 } else if (lfsck_phase2_next_ready(lad)) {
6491 wake_up_all(&lad->lad_thread.t_ctl_waitq);
6497 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6498 struct lfsck_component *com,
6499 struct lfsck_req_local *lrl,
6504 switch (lrl->lrl_event) {
6505 case LEL_FID_ACCESSED:
6506 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6508 case LEL_PAIRS_VERIFY_LOCAL: {
6511 lrl->lrl_status = LPVS_INIT;
6512 /* Firstly, if the MDT-object which is claimed via OST-object
6513 * local stored PFID xattr recognizes the OST-object, then it
6514 * must be that the client given PFID is wrong. */
6515 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6516 &lrl->lrl_ff_local.ff_parent,
6517 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6521 lrl->lrl_status = LPVS_INCONSISTENT;
6522 /* The OST-object local stored PFID xattr is stale. We need to
6523 * check whether the MDT-object that is claimed via the client
6524 * given PFID information recognizes the OST-object or not. If
6525 * matches, then need to update the OST-object's PFID xattr. */
6526 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6527 &lrl->lrl_ff_client.ff_parent,
6528 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6530 * We are not sure whether the client given PFID information
6531 * is correct or not, do nothing to avoid improper fixing.
6534 * The client given PFID information is also invalid, we can
6535 * NOT fix the OST-object inconsistency.
6538 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6539 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6548 RETURN(-EOPNOTSUPP);
6551 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6552 struct lfsck_component *com,
6553 struct lfsck_request *lr)
6555 struct lfsck_instance *lfsck = com->lc_lfsck;
6556 struct lfsck_layout_slave_data *llsd = com->lc_data;
6557 struct lfsck_layout_slave_target *llst;
6561 switch (lr->lr_event) {
6562 case LE_CONDITIONAL_DESTROY:
6563 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6565 case LE_PHASE1_DONE: {
6566 if (lr->lr_flags2 & LF_INCOMPLETE) {
6567 struct lfsck_layout *lo = com->lc_file_ram;
6569 lo->ll_flags |= LF_INCOMPLETE;
6570 llst = lfsck_layout_llst_find_and_del(llsd,
6574 lfsck_layout_llst_put(llst);
6575 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6581 case LE_PHASE2_DONE:
6583 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6584 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6585 lr->lr_event, lr->lr_index, lr->lr_status);
6591 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6595 lfsck_layout_llst_put(llst);
6596 if (list_empty(&llsd->llsd_master_list))
6597 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6599 if (lr->lr_event == LE_PEER_EXIT &&
6600 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6601 (list_empty(&llsd->llsd_master_list) &&
6602 (lr->lr_status == LS_STOPPED ||
6603 lr->lr_status == LS_CO_STOPPED)))) {
6604 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6606 memset(stop, 0, sizeof(*stop));
6607 stop->ls_status = lr->lr_status;
6608 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6609 lfsck_stop(env, lfsck->li_bottom, stop);
6615 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6619 for (i = 0; i < LLIT_MAX; i++)
6620 *count += lo->ll_objs_repaired[i];
6623 static int lfsck_layout_query_all(const struct lu_env *env,
6624 struct lfsck_component *com,
6625 __u32 *mdts_count, __u32 *osts_count,
6628 struct lfsck_layout *lo = com->lc_file_ram;
6629 struct lfsck_tgt_descs *ltds;
6630 struct lfsck_tgt_desc *ltd;
6635 rc = lfsck_query_all(env, com);
6639 ltds = &com->lc_lfsck->li_mdt_descs;
6640 down_read(<ds->ltd_rw_sem);
6641 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6642 ltd = lfsck_ltd2tgt(ltds, idx);
6643 LASSERT(ltd != NULL);
6645 mdts_count[ltd->ltd_layout_status]++;
6646 *repaired += ltd->ltd_layout_repaired;
6648 up_read(<ds->ltd_rw_sem);
6650 ltds = &com->lc_lfsck->li_ost_descs;
6651 down_read(<ds->ltd_rw_sem);
6652 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6653 ltd = lfsck_ltd2tgt(ltds, idx);
6654 LASSERT(ltd != NULL);
6656 osts_count[ltd->ltd_layout_status]++;
6657 *repaired += ltd->ltd_layout_repaired;
6659 up_read(<ds->ltd_rw_sem);
6661 down_read(&com->lc_sem);
6662 mdts_count[lo->ll_status]++;
6663 lfsck_layout_repaired(lo, repaired);
6664 up_read(&com->lc_sem);
6669 static int lfsck_layout_query(const struct lu_env *env,
6670 struct lfsck_component *com,
6671 struct lfsck_request *req,
6672 struct lfsck_reply *rep,
6673 struct lfsck_query *que, int idx)
6675 struct lfsck_layout *lo = com->lc_file_ram;
6679 LASSERT(com->lc_lfsck->li_master);
6681 rc = lfsck_layout_query_all(env, com,
6682 que->lu_mdts_count[idx],
6683 que->lu_osts_count[idx],
6684 &que->lu_repaired[idx]);
6686 down_read(&com->lc_sem);
6687 rep->lr_status = lo->ll_status;
6688 if (req->lr_flags & LEF_QUERY_ALL)
6689 lfsck_layout_repaired(lo, &rep->lr_repaired);
6690 up_read(&com->lc_sem);
6696 /* with lfsck::li_lock held */
6697 static int lfsck_layout_slave_join(const struct lu_env *env,
6698 struct lfsck_component *com,
6699 struct lfsck_start_param *lsp)
6701 struct lfsck_instance *lfsck = com->lc_lfsck;
6702 struct lfsck_layout_slave_data *llsd = com->lc_data;
6703 struct lfsck_layout_slave_target *llst;
6704 struct lfsck_start *start = lsp->lsp_start;
6708 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6711 if (!lsp->lsp_index_valid)
6714 /* If someone is running the LFSCK without orphan handling,
6715 * it will not maintain the object accessing rbtree. So we
6716 * cannot join it for orphan handling. */
6717 if (!llsd->llsd_rbtree_valid)
6720 spin_unlock(&lfsck->li_lock);
6721 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6722 spin_lock(&lfsck->li_lock);
6723 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6724 spin_unlock(&lfsck->li_lock);
6725 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6728 lfsck_layout_llst_put(llst);
6729 spin_lock(&lfsck->li_lock);
6736 static struct lfsck_operations lfsck_layout_master_ops = {
6737 .lfsck_reset = lfsck_layout_reset,
6738 .lfsck_fail = lfsck_layout_fail,
6739 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6740 .lfsck_prep = lfsck_layout_master_prep,
6741 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6742 .lfsck_exec_dir = lfsck_layout_exec_dir,
6743 .lfsck_post = lfsck_layout_master_post,
6744 .lfsck_dump = lfsck_layout_dump,
6745 .lfsck_double_scan = lfsck_layout_master_double_scan,
6746 .lfsck_data_release = lfsck_layout_master_data_release,
6747 .lfsck_quit = lfsck_layout_master_quit,
6748 .lfsck_in_notify = lfsck_layout_master_in_notify,
6749 .lfsck_query = lfsck_layout_query,
6752 static struct lfsck_operations lfsck_layout_slave_ops = {
6753 .lfsck_reset = lfsck_layout_reset,
6754 .lfsck_fail = lfsck_layout_fail,
6755 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6756 .lfsck_prep = lfsck_layout_slave_prep,
6757 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6758 .lfsck_exec_dir = lfsck_layout_exec_dir,
6759 .lfsck_post = lfsck_layout_slave_post,
6760 .lfsck_dump = lfsck_layout_dump,
6761 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6762 .lfsck_data_release = lfsck_layout_slave_data_release,
6763 .lfsck_quit = lfsck_layout_slave_quit,
6764 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6765 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6766 .lfsck_query = lfsck_layout_query,
6767 .lfsck_join = lfsck_layout_slave_join,
6770 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6771 struct lfsck_component *com,
6772 struct lfsck_position *pos)
6774 struct lfsck_assistant_data *lad = com->lc_data;
6775 struct lfsck_layout_req *llr;
6777 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6781 if (list_empty(&lad->lad_req_list))
6784 llr = list_entry(lad->lad_req_list.next,
6785 struct lfsck_layout_req,
6787 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6790 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6791 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
6792 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
6793 .la_fill_pos = lfsck_layout_assistant_fill_pos,
6794 .la_double_scan_result = lfsck_layout_double_scan_result,
6795 .la_req_fini = lfsck_layout_assistant_req_fini,
6796 .la_sync_failures = lfsck_layout_assistant_sync_failures,
6799 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
6801 struct lfsck_component *com;
6802 struct lfsck_layout *lo;
6803 struct dt_object *root = NULL;
6804 struct dt_object *obj;
6813 INIT_LIST_HEAD(&com->lc_link);
6814 INIT_LIST_HEAD(&com->lc_link_dir);
6815 init_rwsem(&com->lc_sem);
6816 atomic_set(&com->lc_ref, 1);
6817 com->lc_lfsck = lfsck;
6818 com->lc_type = LFSCK_TYPE_LAYOUT;
6819 if (lfsck->li_master) {
6820 com->lc_ops = &lfsck_layout_master_ops;
6821 com->lc_data = lfsck_assistant_data_init(
6822 &lfsck_layout_assistant_ops,
6824 if (com->lc_data == NULL)
6825 GOTO(out, rc = -ENOMEM);
6827 for (i = 0; i < LFSCK_STF_COUNT; i++)
6828 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6830 struct lfsck_layout_slave_data *llsd;
6832 com->lc_ops = &lfsck_layout_slave_ops;
6833 OBD_ALLOC_PTR(llsd);
6835 GOTO(out, rc = -ENOMEM);
6837 INIT_LIST_HEAD(&llsd->llsd_seq_list);
6838 INIT_LIST_HEAD(&llsd->llsd_master_list);
6839 spin_lock_init(&llsd->llsd_lock);
6840 llsd->llsd_rb_root = RB_ROOT;
6841 init_rwsem(&llsd->llsd_rb_rwsem);
6842 com->lc_data = llsd;
6844 com->lc_file_size = sizeof(*lo);
6845 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6846 if (com->lc_file_ram == NULL)
6847 GOTO(out, rc = -ENOMEM);
6849 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6850 if (com->lc_file_disk == NULL)
6851 GOTO(out, rc = -ENOMEM);
6853 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6855 GOTO(out, rc = PTR_ERR(root));
6857 if (unlikely(!dt_try_as_dir(env, root)))
6858 GOTO(out, rc = -ENOTDIR);
6860 obj = local_file_find_or_create(env, lfsck->li_los, root,
6862 S_IFREG | S_IRUGO | S_IWUSR);
6864 GOTO(out, rc = PTR_ERR(obj));
6867 rc = lfsck_layout_load(env, com);
6869 rc = lfsck_layout_reset(env, com, true);
6870 } else if (rc == -ENOENT) {
6871 rc = lfsck_layout_init(env, com);
6872 } else if (lfsck->li_master) {
6873 rc = lfsck_load_sub_trace_files(env, com,
6874 &dt_lfsck_layout_dangling_features,
6875 LFSCK_LAYOUT, false);
6877 rc = lfsck_layout_reset(env, com, true);
6883 lo = com->lc_file_ram;
6884 switch (lo->ll_status) {
6890 spin_lock(&lfsck->li_lock);
6891 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6892 spin_unlock(&lfsck->li_lock);
6895 CERROR("%s: unknown lfsck_layout status %d\n",
6896 lfsck_lfsck2name(lfsck), lo->ll_status);
6898 case LS_SCANNING_PHASE1:
6899 case LS_SCANNING_PHASE2:
6900 /* No need to store the status to disk right now.
6901 * If the system crashed before the status stored,
6902 * it will be loaded back when next time. */
6903 lo->ll_status = LS_CRASHED;
6904 if (!lfsck->li_master)
6905 lo->ll_flags |= LF_INCOMPLETE;
6912 spin_lock(&lfsck->li_lock);
6913 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6914 spin_unlock(&lfsck->li_lock);
6918 if (lo->ll_flags & LF_CRASHED_LASTID) {
6919 LASSERT(lfsck->li_out_notify != NULL);
6921 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6922 LE_LASTID_REBUILDING);
6928 if (root != NULL && !IS_ERR(root))
6929 lfsck_object_put(env, root);
6932 lfsck_component_cleanup(env, com);
6933 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
6934 lfsck_lfsck2name(lfsck), rc);
6940 struct lfsck_orphan_it {
6941 struct lfsck_component *loi_com;
6942 struct lfsck_rbtree_node *loi_lrn;
6943 struct lfsck_layout_slave_target *loi_llst;
6944 struct lu_fid loi_key;
6945 struct lu_orphan_rec_v3 loi_rec;
6947 unsigned int loi_over:1;
6950 static int lfsck_fid_match_idx(const struct lu_env *env,
6951 struct lfsck_instance *lfsck,
6952 const struct lu_fid *fid, int idx)
6954 struct seq_server_site *ss;
6955 struct lu_server_fld *sf;
6956 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
6959 /* All abnormal cases will be returned to MDT0. */
6960 if (!fid_is_norm(fid)) {
6967 ss = lfsck_dev_site(lfsck);
6968 if (unlikely(ss == NULL))
6971 sf = ss->ss_server_fld;
6972 LASSERT(sf != NULL);
6974 fld_range_set_any(range);
6975 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
6979 if (!fld_range_is_mdt(range))
6982 if (range->lsr_index == idx)
6988 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
6989 struct dt_object *obj)
6991 struct dt_device *dev = lfsck_obj2dev(obj);
6992 struct thandle *handle;
6996 handle = dt_trans_create(env, dev);
7000 rc = dt_declare_ref_del(env, obj, handle);
7004 rc = dt_declare_destroy(env, obj, handle);
7008 rc = dt_trans_start_local(env, dev, handle);
7012 dt_write_lock(env, obj, 0);
7013 rc = dt_ref_del(env, obj, handle);
7015 rc = dt_destroy(env, obj, handle);
7016 dt_write_unlock(env, obj);
7021 dt_trans_stop(env, dev, handle);
7023 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
7024 PFID(lfsck_dto2fid(obj)), rc);
7029 static int lfsck_orphan_index_lookup(const struct lu_env *env,
7030 struct dt_object *dt,
7032 const struct dt_key *key)
7037 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
7038 struct dt_object *dt,
7039 const struct dt_rec *rec,
7040 const struct dt_key *key,
7041 struct thandle *handle)
7046 static int lfsck_orphan_index_insert(const struct lu_env *env,
7047 struct dt_object *dt,
7048 const struct dt_rec *rec,
7049 const struct dt_key *key,
7050 struct thandle *handle)
7055 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
7056 struct dt_object *dt,
7057 const struct dt_key *key,
7058 struct thandle *handle)
7063 static int lfsck_orphan_index_delete(const struct lu_env *env,
7064 struct dt_object *dt,
7065 const struct dt_key *key,
7066 struct thandle *handle)
7071 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
7072 struct dt_object *dt,
7075 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
7076 struct lfsck_instance *lfsck;
7077 struct lfsck_component *com = NULL;
7078 struct lfsck_layout_slave_data *llsd;
7079 struct lfsck_orphan_it *it = NULL;
7080 struct lfsck_layout *lo;
7084 lfsck = lfsck_instance_find(dev, true, false);
7085 if (unlikely(lfsck == NULL))
7086 RETURN(ERR_PTR(-ENXIO));
7088 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
7089 if (unlikely(com == NULL))
7090 GOTO(out, rc = -ENOENT);
7092 lo = com->lc_file_ram;
7093 if (lo->ll_flags & LF_INCOMPLETE)
7094 GOTO(out, rc = -ESRCH);
7096 llsd = com->lc_data;
7097 if (!llsd->llsd_rbtree_valid)
7098 GOTO(out, rc = -ESRCH);
7102 GOTO(out, rc = -ENOMEM);
7104 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
7105 if (it->loi_llst == NULL)
7106 GOTO(out, rc = -ENXIO);
7108 if (dev->dd_record_fid_accessed) {
7109 /* The first iteration against the rbtree, scan the whole rbtree
7110 * to remove the nodes which do NOT need to be handled. */
7111 down_write(&llsd->llsd_rb_rwsem);
7112 if (dev->dd_record_fid_accessed) {
7113 struct rb_node *node;
7114 struct rb_node *next;
7115 struct lfsck_rbtree_node *lrn;
7117 /* No need to record the fid accessing anymore. */
7118 dev->dd_record_fid_accessed = 0;
7120 node = rb_first(&llsd->llsd_rb_root);
7121 while (node != NULL) {
7122 next = rb_next(node);
7123 lrn = rb_entry(node, struct lfsck_rbtree_node,
7125 if (atomic_read(&lrn->lrn_known_count) <=
7126 atomic_read(&lrn->lrn_accessed_count)) {
7127 rb_erase(node, &llsd->llsd_rb_root);
7128 lfsck_rbtree_free(lrn);
7133 up_write(&llsd->llsd_rb_rwsem);
7136 /* read lock the rbtree when init, and unlock when fini */
7137 down_read(&llsd->llsd_rb_rwsem);
7145 lfsck_component_put(env, com);
7147 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
7148 lfsck_lfsck2name(lfsck), rc);
7150 lfsck_instance_put(env, lfsck);
7155 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7158 return (struct dt_it *)it;
7161 static void lfsck_orphan_it_fini(const struct lu_env *env,
7164 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7165 struct lfsck_component *com = it->loi_com;
7166 struct lfsck_layout_slave_data *llsd;
7167 struct lfsck_layout_slave_target *llst;
7170 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7171 lfsck_lfsck2name(com->lc_lfsck));
7173 llsd = com->lc_data;
7174 up_read(&llsd->llsd_rb_rwsem);
7175 llst = it->loi_llst;
7176 LASSERT(llst != NULL);
7178 /* Save the key and hash for iterate next. */
7179 llst->llst_fid = it->loi_key;
7180 llst->llst_hash = it->loi_hash;
7181 lfsck_layout_llst_put(llst);
7182 lfsck_component_put(env, com);
7188 * \retval +1: the iteration finished
7189 * \retval 0: on success, not finished
7190 * \retval -ve: on error
7192 static int lfsck_orphan_it_next(const struct lu_env *env,
7195 struct lfsck_thread_info *info = lfsck_env_info(env);
7196 struct filter_fid *ff = &info->lti_ff;
7197 struct lu_attr *la = &info->lti_la;
7198 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7199 struct lu_fid *key = &it->loi_key;
7200 struct lu_orphan_rec_v3 *rec = &it->loi_rec;
7201 struct ost_layout *ol = &rec->lor_layout;
7202 struct lfsck_component *com = it->loi_com;
7203 struct lfsck_instance *lfsck = com->lc_lfsck;
7204 struct lfsck_layout_slave_data *llsd = com->lc_data;
7205 struct dt_object *obj;
7206 struct lfsck_rbtree_node *lrn;
7210 __u32 idx = it->loi_llst->llst_index;
7220 lrn = lfsck_rbtree_search(llsd, key, &exact);
7228 key->f_seq = lrn->lrn_seq;
7229 key->f_oid = lrn->lrn_first_oid;
7234 if (unlikely(key->f_oid == 0)) {
7241 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7247 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7248 atomic_read(&lrn->lrn_accessed_count))) {
7249 struct rb_node *next = rb_next(&lrn->lrn_node);
7251 while (next != NULL) {
7252 lrn = rb_entry(next, struct lfsck_rbtree_node,
7254 if (atomic_read(&lrn->lrn_known_count) >
7255 atomic_read(&lrn->lrn_accessed_count))
7257 next = rb_next(next);
7266 key->f_seq = lrn->lrn_seq;
7267 key->f_oid = lrn->lrn_first_oid;
7271 pos = key->f_oid - lrn->lrn_first_oid;
7274 pos = find_next_bit(lrn->lrn_known_bitmap,
7275 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7276 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7277 key->f_oid = lrn->lrn_first_oid + pos;
7278 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7286 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7291 key->f_oid = lrn->lrn_first_oid + pos;
7292 obj = lfsck_object_find_bottom(env, lfsck, key);
7295 if (rc == -ENOENT) {
7302 dt_read_lock(env, obj, 0);
7303 if (dt_object_exists(obj) == 0 ||
7304 lfsck_is_dead_obj(obj)) {
7305 dt_read_unlock(env, obj);
7306 lfsck_object_put(env, obj);
7311 rc = dt_attr_get(env, obj, la);
7315 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7317 if (rc == -ENODATA) {
7318 /* For the pre-created OST-object, update the bitmap to avoid
7319 * others LFSCK (second phase) iteration to touch it again. */
7320 if (la->la_ctime == 0) {
7321 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7322 atomic_inc(&lrn->lrn_accessed_count);
7324 /* For the race between repairing dangling referenced
7325 * MDT-object and unlink the file, it may left orphan
7326 * OST-object there. Destroy it now! */
7327 if (unlikely(!(la->la_mode & S_ISUID))) {
7328 dt_read_unlock(env, obj);
7329 lfsck_layout_destroy_orphan(env, obj);
7330 lfsck_object_put(env, obj);
7334 } else if (idx == 0) {
7335 /* If the orphan OST-object has no parent information,
7336 * regard it as referenced by the MDT-object on MDT0. */
7337 fid_zero(&rec->lor_rec.lor_fid);
7338 rec->lor_rec.lor_uid = la->la_uid;
7339 rec->lor_rec.lor_gid = la->la_gid;
7340 memset(ol, 0, sizeof(*ol));
7341 rec->lor_layout_version = 0;
7347 dt_read_unlock(env, obj);
7348 lfsck_object_put(env, obj);
7353 if (rc < sizeof(struct lu_fid))
7354 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7356 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7357 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7358 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7359 * parent MDT-object's layout EA. */
7360 save = rec->lor_rec.lor_fid.f_stripe_idx;
7361 rec->lor_rec.lor_fid.f_ver = 0;
7362 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7363 /* If the orphan OST-object does not claim the MDT, then next.
7365 * If we do not know whether it matches or not, then return it
7366 * to the MDT for further check. */
7368 dt_read_unlock(env, obj);
7369 lfsck_object_put(env, obj);
7374 rec->lor_rec.lor_fid.f_stripe_idx = save;
7375 rec->lor_rec.lor_uid = la->la_uid;
7376 rec->lor_rec.lor_gid = la->la_gid;
7377 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7378 rec->lor_layout_version =
7379 le32_to_cpu(ff->ff_layout_version & ~LU_LAYOUT_RESYNC);
7380 rec->lor_range = le32_to_cpu(ff->ff_range);
7382 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7383 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7384 "COMP end %llu, layout version %u, range %u\n",
7385 lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7386 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7387 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7388 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
7389 rec->lor_layout_version, rec->lor_range);
7394 dt_read_unlock(env, obj);
7395 lfsck_object_put(env, obj);
7403 * \retval +1: locate to the exactly position
7404 * \retval 0: cannot locate to the exactly position,
7405 * call next() to move to a valid position.
7406 * \retval -ve: on error
7408 static int lfsck_orphan_it_get(const struct lu_env *env,
7410 const struct dt_key *key)
7412 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7415 it->loi_key = *(struct lu_fid *)key;
7416 rc = lfsck_orphan_it_next(env, di);
7426 static void lfsck_orphan_it_put(const struct lu_env *env,
7431 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7432 const struct dt_it *di)
7434 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7436 return (struct dt_key *)&it->loi_key;
7439 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7440 const struct dt_it *di)
7442 return sizeof(struct lu_fid);
7445 static int lfsck_orphan_it_rec(const struct lu_env *env,
7446 const struct dt_it *di,
7450 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7452 *(struct lu_orphan_rec_v3 *)rec = it->loi_rec;
7457 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7458 const struct dt_it *di)
7460 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7462 return it->loi_hash;
7466 * \retval +1: locate to the exactly position
7467 * \retval 0: cannot locate to the exactly position,
7468 * call next() to move to a valid position.
7469 * \retval -ve: on error
7471 static int lfsck_orphan_it_load(const struct lu_env *env,
7472 const struct dt_it *di,
7475 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7476 struct lfsck_layout_slave_target *llst = it->loi_llst;
7479 LASSERT(llst != NULL);
7481 if (hash != llst->llst_hash) {
7482 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7483 "iteration does not match the one when fini "
7484 "%llu, to be reset.\n",
7485 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7487 fid_zero(&llst->llst_fid);
7488 llst->llst_hash = 0;
7491 it->loi_key = llst->llst_fid;
7492 it->loi_hash = llst->llst_hash;
7493 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7503 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7504 const struct dt_it *di,
7510 static const struct dt_index_operations lfsck_orphan_index_ops = {
7511 .dio_lookup = lfsck_orphan_index_lookup,
7512 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7513 .dio_insert = lfsck_orphan_index_insert,
7514 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7515 .dio_delete = lfsck_orphan_index_delete,
7517 .init = lfsck_orphan_it_init,
7518 .fini = lfsck_orphan_it_fini,
7519 .get = lfsck_orphan_it_get,
7520 .put = lfsck_orphan_it_put,
7521 .next = lfsck_orphan_it_next,
7522 .key = lfsck_orphan_it_key,
7523 .key_size = lfsck_orphan_it_key_size,
7524 .rec = lfsck_orphan_it_rec,
7525 .store = lfsck_orphan_it_store,
7526 .load = lfsck_orphan_it_load,
7527 .key_rec = lfsck_orphan_it_key_rec,