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 border, __u32 size)
101 return (border & (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_of(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, __u64 end,
340 magic = le32_to_cpu(lmm->lmm_magic);
341 /* If magic crashed, keep it there. Sometime later, during OST-object
342 * orphan handling, if some OST-object(s) back-point to it, it can be
343 * verified and repaired. */
344 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
347 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
352 CDEBUG(D_LFSCK, "%s LOV EA magic 0x%X for the file "DFID"\n",
353 rc == -EINVAL ? "Unknown" : "Unsupported",
354 magic, PFID(lfsck_dto2fid(obj)));
359 pattern = le32_to_cpu(lmm->lmm_pattern);
360 *dom = !!(lov_pattern(pattern) == LOV_PATTERN_MDT);
362 /* XXX: DoM file verification will be supportted via LU-11081. */
363 if (lov_pattern(pattern) == LOV_PATTERN_MDT) {
366 CDEBUG(D_LFSCK, "The DoM entry for "DFID" is not "
367 "the first component in the mirror %x/%llu\n",
368 PFID(lfsck_dto2fid(obj)), comp_id, start);
373 } else if (!lov_pattern_supported_normal_comp(lov_pattern(pattern))) {
374 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
375 DFID" in the component %x\n",
376 pattern, PFID(lfsck_dto2fid(obj)), comp_id);
381 size = le32_to_cpu(lmm->lmm_stripe_size);
382 if (!ext && end != LUSTRE_EOF && start != end &&
383 !lfsck_comp_extent_aligned(end, size)){
384 CDEBUG(D_LFSCK, "not aligned border in PFL extent range "
385 "[%llu - %llu) stripesize %u for the file "DFID
386 " at idx %d\n", start, end, size,
387 PFID(lfsck_dto2fid(obj)), comp_id);
395 static int lfsck_layout_verify_header_foreign(struct dt_object *obj,
396 struct lov_foreign_md *lfm,
399 /* magic has been verified already */
400 __u32 value_len = le32_to_cpu(lfm->lfm_length);
401 /* type and flags are not checked for instance */
403 CDEBUG(D_INFO, "foreign LOV EA, magic %x, len %u, type %x, flags %x, for file "DFID"\n",
404 le32_to_cpu(lfm->lfm_magic), value_len,
405 le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
406 PFID(lfsck_dto2fid(obj)));
408 if (len != value_len + offsetof(typeof(*lfm), lfm_value))
409 CDEBUG(D_LFSCK, "foreign LOV EA internal size %u does not match EA full size %zu for file "DFID"\n",
410 value_len, len, PFID(lfsck_dto2fid(obj)));
412 /* nothing to repair */
416 static int lfsck_layout_verify_header(struct dt_object *obj,
417 struct lov_mds_md_v1 *lmm, size_t len)
422 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1 ||
423 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_SEL) {
424 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
427 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
429 if (unlikely(count == 0)) {
430 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
431 "components count 0\n",
432 PFID(lfsck_dto2fid(obj)));
437 for (i = 0; i < count && !rc; i++) {
438 struct lov_comp_md_entry_v1 *lcme =
439 &lcm->lcm_entries[i];
440 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
441 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
442 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
443 bool ext, inited, zero;
446 if (unlikely(comp_id == LCME_ID_INVAL ||
447 comp_id > LCME_ID_MAX)) {
448 CDEBUG(D_LFSCK, "found invalid PFL ID %u "
449 "for the file "DFID" at idx %d\n",
450 comp_id, PFID(lfsck_dto2fid(obj)), i);
455 flags = le32_to_cpu(lcme->lcme_flags);
456 ext = flags & LCME_FL_EXTENSION;
457 inited = flags & LCME_FL_INIT;
458 zero = !!(start == end);
460 if ((i == 0) && zero) {
461 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu "
462 "- %llu) for "DFID"\n", i, start, end,
463 PFID(lfsck_dto2fid(obj)));
467 if ((zero && (inited || (i + 1 == count))) ||
469 CDEBUG(D_LFSCK, "invalid PFL comp %d/%d: "
470 "[%llu, %llu) for "DFID", %sinited\n",
471 i, count, start, end,
472 PFID(lfsck_dto2fid(obj)),
473 inited ? "" : "NOT ");
477 if (!ext && p_zero) {
478 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu, "
479 "%llu) for "DFID": NOT extension "
480 "after 0-length component\n", i,
481 start, end, PFID(lfsck_dto2fid(obj)));
485 if (ext && (inited || p_dom || zero)) {
486 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu, "
487 "%llu) for "DFID": %s\n", i,
488 start, end, PFID(lfsck_dto2fid(obj)),
489 inited ? "inited extension" :
490 p_dom ? "extension follows DOM" :
491 zero ? "zero length extension" : "");
495 rc = lfsck_layout_verify_header_v1v3(obj,
496 (struct lov_mds_md_v1 *)((char *)lmm +
497 le32_to_cpu(lcme->lcme_offset)), start,
498 end, comp_id, ext, &p_dom);
502 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_FOREIGN) {
503 rc = lfsck_layout_verify_header_foreign(obj,
504 (struct lov_foreign_md *)lmm,
507 rc = lfsck_layout_verify_header_v1v3(obj, lmm, 0, LUSTRE_EOF,
514 static int lfsck_layout_get_lovea(const struct lu_env *env,
515 struct dt_object *obj, struct lu_buf *buf)
521 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
523 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
525 return !rc ? -ENODATA : rc;
527 lu_buf_realloc(buf, rc);
528 if (buf->lb_buf == NULL)
535 return !rc ? -ENODATA : rc;
537 if (unlikely(buf->lb_buf == NULL)) {
538 lu_buf_alloc(buf, rc);
539 if (buf->lb_buf == NULL)
545 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf, rc);
547 return rc1 ? rc1 : rc;
550 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
551 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
552 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
554 struct lfsck_rbtree_node {
555 struct rb_node lrn_node;
558 atomic_t lrn_known_count;
559 atomic_t lrn_accessed_count;
560 void *lrn_known_bitmap;
561 void *lrn_accessed_bitmap;
564 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
565 __u64 seq, __u32 oid)
567 if (seq < lrn->lrn_seq)
570 if (seq > lrn->lrn_seq)
573 if (oid < lrn->lrn_first_oid)
576 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
582 /* The caller should hold llsd->llsd_rb_lock. */
583 static struct lfsck_rbtree_node *
584 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
585 const struct lu_fid *fid, bool *exact)
587 struct rb_node *node = llsd->llsd_rb_root.rb_node;
588 struct rb_node *prev = NULL;
589 struct lfsck_rbtree_node *lrn = NULL;
595 while (node != NULL) {
597 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
598 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
600 node = node->rb_left;
602 node = node->rb_right;
610 /* If there is no exactly matched one, then to the next valid one. */
613 /* The rbtree is empty. */
620 node = rb_next(prev);
622 /* The end of the rbtree. */
626 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
631 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
632 const struct lu_fid *fid)
634 struct lfsck_rbtree_node *lrn;
638 return ERR_PTR(-ENOMEM);
640 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
641 if (lrn->lrn_known_bitmap == NULL) {
644 return ERR_PTR(-ENOMEM);
647 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
648 if (lrn->lrn_accessed_bitmap == NULL) {
649 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
652 return ERR_PTR(-ENOMEM);
655 RB_CLEAR_NODE(&lrn->lrn_node);
656 lrn->lrn_seq = fid_seq(fid);
657 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
658 atomic_set(&lrn->lrn_known_count, 0);
659 atomic_set(&lrn->lrn_accessed_count, 0);
664 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
666 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
667 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
671 /* The caller should hold lock. */
672 static struct lfsck_rbtree_node *
673 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
674 struct lfsck_rbtree_node *lrn)
676 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
677 struct rb_node *parent = NULL;
678 struct lfsck_rbtree_node *tmp;
681 while (*pos != NULL) {
683 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
684 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
686 pos = &(*pos)->rb_left;
688 pos = &(*pos)->rb_right;
693 rb_link_node(&lrn->lrn_node, parent, pos);
694 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
699 static const struct dt_index_operations lfsck_orphan_index_ops;
701 static int lfsck_rbtree_setup(const struct lu_env *env,
702 struct lfsck_component *com)
704 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
705 struct lfsck_instance *lfsck = com->lc_lfsck;
706 struct dt_device *dev = lfsck->li_bottom;
707 struct lfsck_layout_slave_data *llsd = com->lc_data;
708 struct dt_object *obj;
710 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
711 fid->f_oid = lfsck_dev_idx(lfsck);
713 obj = dt_locate(env, dev, fid);
715 RETURN(PTR_ERR(obj));
717 /* Generate an in-RAM object to stand for the layout rbtree.
718 * Scanning the layout rbtree will be via the iteration over
719 * the object. In the future, the rbtree may be written onto
720 * disk with the object.
722 * Mark the object to be as exist. */
723 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
724 obj->do_index_ops = &lfsck_orphan_index_ops;
725 llsd->llsd_rb_obj = obj;
726 llsd->llsd_rbtree_valid = 1;
727 dev->dd_record_fid_accessed = 1;
729 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
730 lfsck_lfsck2name(lfsck));
735 static void lfsck_rbtree_cleanup(const struct lu_env *env,
736 struct lfsck_component *com)
738 struct lfsck_instance *lfsck = com->lc_lfsck;
739 struct lfsck_layout_slave_data *llsd = com->lc_data;
740 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
741 struct rb_node *next;
742 struct lfsck_rbtree_node *lrn;
744 lfsck->li_bottom->dd_record_fid_accessed = 0;
745 /* Invalid the rbtree, then no others will use it. */
746 down_write(&llsd->llsd_rb_rwsem);
747 llsd->llsd_rbtree_valid = 0;
748 up_write(&llsd->llsd_rb_rwsem);
750 while (node != NULL) {
751 next = rb_next(node);
752 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
753 rb_erase(node, &llsd->llsd_rb_root);
754 lfsck_rbtree_free(lrn);
758 if (llsd->llsd_rb_obj != NULL) {
759 lfsck_object_put(env, llsd->llsd_rb_obj);
760 llsd->llsd_rb_obj = NULL;
763 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
764 lfsck_lfsck2name(lfsck));
767 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
768 struct lfsck_component *com,
769 const struct lu_fid *fid,
772 struct lfsck_layout_slave_data *llsd = com->lc_data;
773 struct lfsck_rbtree_node *lrn;
779 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
782 if (!fid_is_idif(fid) && !fid_is_norm(fid))
785 down_read(&llsd->llsd_rb_rwsem);
786 if (!llsd->llsd_rbtree_valid)
787 GOTO(unlock, rc = 0);
789 lrn = lfsck_rbtree_search(llsd, fid, NULL);
791 struct lfsck_rbtree_node *tmp;
795 up_read(&llsd->llsd_rb_rwsem);
796 tmp = lfsck_rbtree_new(env, fid);
798 GOTO(out, rc = PTR_ERR(tmp));
801 down_write(&llsd->llsd_rb_rwsem);
802 if (!llsd->llsd_rbtree_valid) {
803 lfsck_rbtree_free(tmp);
804 GOTO(unlock, rc = 0);
807 lrn = lfsck_rbtree_insert(llsd, tmp);
809 lfsck_rbtree_free(tmp);
812 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
813 /* Any accessed object must be a known object. */
814 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
815 atomic_inc(&lrn->lrn_known_count);
816 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
817 atomic_inc(&lrn->lrn_accessed_count);
819 GOTO(unlock, rc = 0);
823 up_write(&llsd->llsd_rb_rwsem);
825 up_read(&llsd->llsd_rb_rwsem);
827 if (rc != 0 && accessed) {
828 struct lfsck_layout *lo = com->lc_file_ram;
830 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
831 "bitmap, and will cause incorrect LFSCK OST-object "
832 "handling, so disable it to cancel orphan handling "
833 "for related device. rc = %d\n",
834 lfsck_lfsck2name(com->lc_lfsck), rc);
836 lo->ll_flags |= LF_INCOMPLETE;
837 lfsck_rbtree_cleanup(env, com);
841 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
842 const struct lfsck_layout_dangling_key *src)
844 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
845 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
846 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
849 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
850 const struct lfsck_layout_dangling_key *src)
852 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
853 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
854 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
857 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
858 const struct lfsck_layout_dangling_key *src)
860 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
861 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
862 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
865 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
866 const struct lfsck_layout_dangling_key *src)
868 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
869 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
870 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
873 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
874 const struct lfsck_layout *src)
878 des->ll_magic = le32_to_cpu(src->ll_magic);
879 des->ll_status = le32_to_cpu(src->ll_status);
880 des->ll_flags = le32_to_cpu(src->ll_flags);
881 des->ll_success_count = le32_to_cpu(src->ll_success_count);
882 des->ll_run_time_phase1 = le64_to_cpu(src->ll_run_time_phase1);
883 des->ll_run_time_phase2 = le64_to_cpu(src->ll_run_time_phase2);
884 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
885 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
886 des->ll_time_last_checkpoint =
887 le64_to_cpu(src->ll_time_last_checkpoint);
888 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
889 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
890 des->ll_pos_first_inconsistent =
891 le64_to_cpu(src->ll_pos_first_inconsistent);
892 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
893 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
894 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
895 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
896 for (i = 0; i < LLIT_MAX; i++)
897 des->ll_objs_repaired[i] =
898 le64_to_cpu(src->ll_objs_repaired[i]);
899 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
900 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
901 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
902 &src->ll_lldk_latest_scanned_phase2);
905 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
906 const struct lfsck_layout *src)
910 des->ll_magic = cpu_to_le32(src->ll_magic);
911 des->ll_status = cpu_to_le32(src->ll_status);
912 des->ll_flags = cpu_to_le32(src->ll_flags);
913 des->ll_success_count = cpu_to_le32(src->ll_success_count);
914 des->ll_run_time_phase1 = cpu_to_le64(src->ll_run_time_phase1);
915 des->ll_run_time_phase2 = cpu_to_le64(src->ll_run_time_phase2);
916 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
917 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
918 des->ll_time_last_checkpoint =
919 cpu_to_le64(src->ll_time_last_checkpoint);
920 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
921 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
922 des->ll_pos_first_inconsistent =
923 cpu_to_le64(src->ll_pos_first_inconsistent);
924 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
925 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
926 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
927 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
928 for (i = 0; i < LLIT_MAX; i++)
929 des->ll_objs_repaired[i] =
930 cpu_to_le64(src->ll_objs_repaired[i]);
931 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
932 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
933 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
934 &src->ll_lldk_latest_scanned_phase2);
938 * Load the OST bitmap from the lfsck_layout trace file.
940 * \param[in] env pointer to the thread context
941 * \param[in] com pointer to the lfsck component
943 * \retval 0 for success
944 * \retval negative error number on failure or data corruption
946 static int lfsck_layout_load_bitmap(const struct lu_env *env,
947 struct lfsck_component *com)
949 struct dt_object *obj = com->lc_obj;
950 struct lfsck_assistant_data *lad = com->lc_data;
951 struct lfsck_layout *lo = com->lc_file_ram;
952 struct cfs_bitmap *bitmap = lad->lad_bitmap;
953 loff_t pos = com->lc_file_size;
959 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
961 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
963 nbits = lo->ll_bitmap_size;
965 if (unlikely(nbits < BITS_PER_LONG))
966 nbits = BITS_PER_LONG;
968 if (nbits > bitmap->size) {
969 __u32 new_bits = bitmap->size;
970 struct cfs_bitmap *new_bitmap;
972 while (new_bits < nbits)
975 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
976 if (new_bitmap == NULL)
979 lad->lad_bitmap = new_bitmap;
980 CFS_FREE_BITMAP(bitmap);
984 if (lo->ll_bitmap_size == 0) {
985 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
986 CFS_RESET_BITMAP(bitmap);
991 size = (lo->ll_bitmap_size + 7) >> 3;
992 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
994 RETURN(rc >= 0 ? -EINVAL : rc);
996 if (cfs_bitmap_check_empty(bitmap))
997 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
999 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
1005 * Load the layout LFSCK trace file from disk.
1007 * The layout LFSCK trace file records the layout LFSCK status information
1008 * and other statistics, such as how many objects have been scanned, and how
1009 * many objects have been repaired, and etc. It also contains the bitmap for
1010 * failed OSTs during the layout LFSCK. All these information will be loaded
1011 * from disk to RAM when the layout LFSCK component setup.
1013 * \param[in] env pointer to the thread context
1014 * \param[in] com pointer to the lfsck component
1016 * \retval positive number for file data corruption, the caller
1017 * should reset the layout LFSCK trace file
1018 * \retval 0 for success
1019 * \retval negative error number on failure
1021 static int lfsck_layout_load(const struct lu_env *env,
1022 struct lfsck_component *com)
1024 struct lfsck_layout *lo = com->lc_file_ram;
1025 ssize_t size = com->lc_file_size;
1029 rc = dt_read(env, com->lc_obj,
1030 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
1033 } else if (rc < 0) {
1034 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
1035 lfsck_lfsck2name(com->lc_lfsck), rc);
1037 } else if (rc != size) {
1038 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
1039 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
1043 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
1044 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
1045 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
1046 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
1047 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
1055 * Store the layout LFSCK trace file on disk.
1057 * The layout LFSCK trace file records the layout LFSCK status information
1058 * and other statistics, such as how many objects have been scanned, and how
1059 * many objects have been repaired, and etc. It also contains the bitmap for
1060 * failed OSTs during the layout LFSCK. All these information will be synced
1061 * from RAM to disk periodically.
1063 * \param[in] env pointer to the thread context
1064 * \param[in] com pointer to the lfsck component
1066 * \retval 0 for success
1067 * \retval negative error number on failure
1069 static int lfsck_layout_store(const struct lu_env *env,
1070 struct lfsck_component *com)
1072 struct dt_object *obj = com->lc_obj;
1073 struct lfsck_instance *lfsck = com->lc_lfsck;
1074 struct lfsck_layout *lo_ram = com->lc_file_ram;
1075 struct lfsck_layout *lo = com->lc_file_disk;
1077 struct dt_device *dev = lfsck_obj2dev(obj);
1078 struct cfs_bitmap *bitmap = NULL;
1080 ssize_t size = com->lc_file_size;
1085 if (lfsck->li_master) {
1086 struct lfsck_assistant_data *lad = com->lc_data;
1088 bitmap = lad->lad_bitmap;
1089 nbits = bitmap->size;
1092 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1095 lo_ram->ll_bitmap_size = nbits;
1096 lfsck_layout_cpu_to_le(lo, lo_ram);
1097 th = dt_trans_create(env, dev);
1099 GOTO(log, rc = PTR_ERR(th));
1101 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1106 if (bitmap != NULL) {
1107 rc = dt_declare_record_write(env, obj,
1108 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1114 rc = dt_trans_start_local(env, dev, th);
1119 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1123 if (bitmap != NULL) {
1125 rc = dt_record_write(env, obj,
1126 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1133 dt_trans_stop(env, dev, th);
1137 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1138 lfsck_lfsck2name(lfsck), rc);
1143 static int lfsck_layout_init(const struct lu_env *env,
1144 struct lfsck_component *com)
1146 struct lfsck_layout *lo = com->lc_file_ram;
1149 memset(lo, 0, com->lc_file_size);
1150 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1151 lo->ll_status = LS_INIT;
1152 down_write(&com->lc_sem);
1153 rc = lfsck_layout_store(env, com);
1154 if (rc == 0 && com->lc_lfsck->li_master)
1155 rc = lfsck_load_sub_trace_files(env, com,
1156 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1157 up_write(&com->lc_sem);
1162 static int fid_is_for_ostobj(const struct lu_env *env,
1163 struct lfsck_instance *lfsck,
1164 struct dt_object *obj, const struct lu_fid *fid)
1166 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1167 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1168 struct lustre_ost_attrs *loa;
1171 fld_range_set_any(range);
1172 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1174 if (fld_range_is_ost(range))
1180 loa = &lfsck_env_info(env)->lti_loa;
1181 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1183 if (rc >= (int)sizeof(struct lustre_mdt_attrs)) {
1184 lustre_lma_swab(&loa->loa_lma);
1186 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1189 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1194 static struct lfsck_layout_seq *
1195 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1197 struct lfsck_layout_seq *lls;
1199 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1200 if (lls->lls_seq == seq)
1203 if (lls->lls_seq > seq)
1211 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1212 struct lfsck_layout_seq *lls)
1214 struct lfsck_layout_seq *tmp;
1215 struct list_head *pos = &llsd->llsd_seq_list;
1217 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1218 if (lls->lls_seq < tmp->lls_seq) {
1219 pos = &tmp->lls_list;
1223 list_add_tail(&lls->lls_list, pos);
1227 lfsck_layout_lastid_create(const struct lu_env *env,
1228 struct lfsck_instance *lfsck,
1229 struct dt_object *obj)
1231 struct lfsck_thread_info *info = lfsck_env_info(env);
1232 struct lu_attr *la = &info->lti_la;
1233 struct dt_object_format *dof = &info->lti_dof;
1234 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1235 struct dt_device *dt = lfsck_obj2dev(obj);
1242 if (bk->lb_param & LPF_DRYRUN)
1245 memset(la, 0, sizeof(*la));
1246 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1247 la->la_valid = LA_MODE | LA_UID | LA_GID;
1248 memset(dof, 0, sizeof(*dof));
1249 dof->dof_type = dt_mode_to_dft(S_IFREG);
1251 th = lfsck_trans_create(env, dt, lfsck);
1253 GOTO(log, rc = PTR_ERR(th));
1255 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1259 rc = dt_declare_record_write(env, obj,
1260 lfsck_buf_get(env, &lastid,
1266 rc = dt_trans_start_local(env, dt, th);
1270 dt_write_lock(env, obj, 0);
1271 if (likely(dt_object_exists(obj) == 0)) {
1272 rc = dt_create(env, obj, la, NULL, dof, th);
1274 rc = dt_record_write(env, obj,
1275 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1278 dt_write_unlock(env, obj);
1283 dt_trans_stop(env, dt, th);
1286 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1288 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1294 lfsck_layout_lastid_reload(const struct lu_env *env,
1295 struct lfsck_component *com,
1296 struct lfsck_layout_seq *lls)
1302 dt_read_lock(env, lls->lls_lastid_obj, 0);
1303 rc = dt_record_read(env, lls->lls_lastid_obj,
1304 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1305 dt_read_unlock(env, lls->lls_lastid_obj);
1306 if (unlikely(rc != 0))
1309 lastid = le64_to_cpu(lastid);
1310 if (lastid < lls->lls_lastid_known) {
1311 struct lfsck_instance *lfsck = com->lc_lfsck;
1312 struct lfsck_layout *lo = com->lc_file_ram;
1314 lls->lls_lastid = lls->lls_lastid_known;
1316 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1317 LASSERT(lfsck->li_out_notify != NULL);
1319 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1320 LE_LASTID_REBUILDING);
1321 lo->ll_flags |= LF_CRASHED_LASTID;
1323 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1324 "LAST_ID file (1) for the sequence %#llx"
1325 ", old value %llu, known value %llu\n",
1326 lfsck_lfsck2name(lfsck), lls->lls_seq,
1327 lastid, lls->lls_lastid);
1329 } else if (lastid >= lls->lls_lastid) {
1330 lls->lls_lastid = lastid;
1338 lfsck_layout_lastid_store(const struct lu_env *env,
1339 struct lfsck_component *com)
1341 struct lfsck_instance *lfsck = com->lc_lfsck;
1342 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1343 struct dt_device *dt = lfsck->li_bottom;
1344 struct lfsck_layout_slave_data *llsd = com->lc_data;
1345 struct lfsck_layout_seq *lls;
1351 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1354 if (!lls->lls_dirty)
1357 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1358 "<seq> %#llx as <oid> %llu\n",
1359 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1361 if (bk->lb_param & LPF_DRYRUN) {
1366 th = lfsck_trans_create(env, dt, lfsck);
1369 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1370 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1371 lfsck_lfsck2name(com->lc_lfsck),
1376 lastid = cpu_to_le64(lls->lls_lastid);
1377 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1378 lfsck_buf_get(env, &lastid,
1384 rc = dt_trans_start_local(env, dt, th);
1388 dt_write_lock(env, lls->lls_lastid_obj, 0);
1389 rc = dt_record_write(env, lls->lls_lastid_obj,
1390 lfsck_buf_get(env, &lastid,
1391 sizeof(lastid)), &pos, th);
1392 dt_write_unlock(env, lls->lls_lastid_obj);
1397 dt_trans_stop(env, dt, th);
1400 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1401 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1402 lfsck_lfsck2name(com->lc_lfsck),
1411 lfsck_layout_lastid_load(const struct lu_env *env,
1412 struct lfsck_component *com,
1413 struct lfsck_layout_seq *lls)
1415 struct lfsck_instance *lfsck = com->lc_lfsck;
1416 struct lfsck_layout *lo = com->lc_file_ram;
1417 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1418 struct dt_object *obj;
1423 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1424 obj = dt_locate(env, lfsck->li_bottom, fid);
1426 RETURN(PTR_ERR(obj));
1428 /* LAST_ID crashed, to be rebuilt */
1429 if (dt_object_exists(obj) == 0) {
1430 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1431 LASSERT(lfsck->li_out_notify != NULL);
1433 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1434 LE_LASTID_REBUILDING);
1435 lo->ll_flags |= LF_CRASHED_LASTID;
1437 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1438 "LAST_ID file for sequence %#llx\n",
1439 lfsck_lfsck2name(lfsck), lls->lls_seq);
1441 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1443 struct ptlrpc_thread *thread =
1446 up_write(&com->lc_sem);
1447 wait_event_idle_timeout(
1448 thread->t_ctl_waitq,
1449 !thread_is_running(thread),
1450 cfs_time_seconds(cfs_fail_val));
1451 down_write(&com->lc_sem);
1455 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1457 dt_read_lock(env, obj, 0);
1458 rc = dt_read(env, obj,
1459 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1461 dt_read_unlock(env, obj);
1462 if (rc != 0 && rc != sizeof(__u64))
1463 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1465 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1466 LASSERT(lfsck->li_out_notify != NULL);
1468 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1469 LE_LASTID_REBUILDING);
1470 lo->ll_flags |= LF_CRASHED_LASTID;
1472 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1473 "LAST_ID file for the sequence %#llx"
1475 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1478 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1486 lfsck_object_put(env, obj);
1488 lls->lls_lastid_obj = obj;
1493 static void lfsck_layout_record_failure(const struct lu_env *env,
1494 struct lfsck_instance *lfsck,
1495 struct lfsck_layout *lo)
1499 lo->ll_objs_failed_phase1++;
1500 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1502 if (lo->ll_pos_first_inconsistent == 0 ||
1503 lo->ll_pos_first_inconsistent < cookie) {
1504 lo->ll_pos_first_inconsistent = cookie;
1506 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1507 "inconsistency at the pos [%llu]\n",
1508 lfsck_lfsck2name(lfsck),
1509 lo->ll_pos_first_inconsistent);
1513 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1514 struct lfsck_component *com,
1517 struct lfsck_instance *lfsck = com->lc_lfsck;
1518 struct lfsck_layout *lo = com->lc_file_ram;
1520 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan: rc = %d\n",
1521 lfsck_lfsck2name(lfsck), rc);
1523 down_write(&com->lc_sem);
1524 lo->ll_run_time_phase2 += ktime_get_seconds() -
1525 com->lc_time_last_checkpoint;
1526 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
1527 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1530 if (lo->ll_flags & LF_INCOMPLETE) {
1531 lo->ll_status = LS_PARTIAL;
1533 if (lfsck->li_master) {
1534 struct lfsck_assistant_data *lad = com->lc_data;
1536 if (test_bit(LAD_INCOMPLETE, &lad->lad_flags))
1537 lo->ll_status = LS_PARTIAL;
1539 lo->ll_status = LS_COMPLETED;
1541 lo->ll_status = LS_COMPLETED;
1544 lo->ll_flags &= ~LF_SCANNED_ONCE;
1545 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1546 lo->ll_flags &= ~LF_INCONSISTENT;
1547 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1548 lo->ll_success_count++;
1549 } else if (rc == 0) {
1550 if (lfsck->li_status != 0)
1551 lo->ll_status = lfsck->li_status;
1553 lo->ll_status = LS_STOPPED;
1555 lo->ll_status = LS_FAILED;
1558 rc = lfsck_layout_store(env, com);
1559 up_write(&com->lc_sem);
1561 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan result %u: rc = %d\n",
1562 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
1567 static int lfsck_layout_trans_stop(const struct lu_env *env,
1568 struct dt_device *dev,
1569 struct thandle *handle, int result)
1573 /* XXX: If there is something worng or it needs to repair nothing,
1574 * then notify the lower to stop the modification. Currently,
1575 * we use th_result for such purpose, that may be replaced by
1576 * some rollback mechanism in the future. */
1577 handle->th_result = result;
1578 rc = dt_trans_stop(env, dev, handle);
1580 return result > 0 ? 0 : result;
1582 return rc == 0 ? 1 : rc;
1585 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1586 struct lfsck_component *com,
1587 const struct lu_fid *pfid,
1588 const struct lu_fid *cfid,
1589 __u32 comp_id, __u32 ea_off,
1592 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1593 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1594 struct dt_device *dev;
1595 struct dt_object *obj;
1596 struct thandle *th = NULL;
1601 idx = lfsck_sub_trace_file_fid2idx(pfid);
1602 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1603 dev = lfsck_obj2dev(obj);
1605 fid_cpu_to_be(&key->lldk_fid, pfid);
1606 key->lldk_comp_id = cpu_to_be32(comp_id);
1607 key->lldk_ea_off = cpu_to_be32(ea_off);
1609 fid_cpu_to_be(rec, cfid);
1610 rec->f_ver = cpu_to_be32(ost_idx);
1612 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1614 th = lfsck_trans_create(env, dev, com->lc_lfsck);
1616 GOTO(unlock, rc = PTR_ERR(th));
1618 rc = dt_declare_insert(env, obj,
1619 (const struct dt_rec *)rec,
1620 (const struct dt_key *)key, th);
1624 rc = dt_trans_start_local(env, dev, th);
1628 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1629 (const struct dt_key *)key, th);
1634 if (th && !IS_ERR(th))
1635 dt_trans_stop(env, dev, th);
1637 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1639 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1640 "ea_off = %u, ost_idx = %u, into the trace file for further "
1641 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1642 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1647 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1648 struct lfsck_component *com,
1649 const struct lu_fid *fid,
1650 __u32 comp_id, __u32 ea_off)
1652 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1653 struct dt_device *dev;
1654 struct dt_object *obj;
1655 struct thandle *th = NULL;
1660 idx = lfsck_sub_trace_file_fid2idx(fid);
1661 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1662 dev = lfsck_obj2dev(obj);
1664 fid_cpu_to_be(&key->lldk_fid, fid);
1665 key->lldk_comp_id = cpu_to_be32(comp_id);
1666 key->lldk_ea_off = cpu_to_be32(ea_off);
1668 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1670 th = lfsck_trans_create(env, dev, com->lc_lfsck);
1672 GOTO(unlock, rc = PTR_ERR(th));
1674 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1678 rc = dt_trans_start_local(env, dev, th);
1682 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1687 if (th && !IS_ERR(th))
1688 dt_trans_stop(env, dev, th);
1690 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1692 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1693 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1694 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1700 * Get the system default stripe size.
1702 * \param[in] env pointer to the thread context
1703 * \param[in] lfsck pointer to the lfsck instance
1704 * \param[out] size pointer to the default stripe size
1706 * \retval 0 for success
1707 * \retval negative error number on failure
1709 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1710 struct lfsck_instance *lfsck,
1713 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1714 struct dt_object *root;
1717 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1719 return PTR_ERR(root);
1721 /* Get the default stripe size via xattr_get on the backend root. */
1722 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1725 /* The lum->lmm_stripe_size is LE mode. The *size also
1726 * should be LE mode. So it is unnecessary to convert. */
1727 *size = lum->lmm_stripe_size;
1729 } else if (unlikely(rc == 0)) {
1733 lfsck_object_put(env, root);
1739 * \retval +1: repaired
1740 * \retval 0: did nothing
1741 * \retval -ve: on error
1743 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1744 struct lfsck_instance *lfsck,
1745 struct thandle *handle,
1746 struct dt_object *parent,
1747 const struct lu_fid *cfid,
1749 struct lov_mds_md_v1 *lmm,
1750 struct lov_ost_data_v1 *slot,
1751 int fl, __u32 ost_idx, int size)
1753 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1754 struct lu_buf ea_buf;
1761 magic = le32_to_cpu(lmm->lmm_magic);
1762 pattern = le32_to_cpu(lmm->lmm_pattern);
1763 count = le16_to_cpu(lmm->lmm_stripe_count);
1765 fid_to_ostid(cfid, oi);
1766 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1767 slot->l_ost_gen = cpu_to_le32(0);
1768 slot->l_ost_idx = cpu_to_le32(ost_idx);
1770 if (pattern & LOV_PATTERN_F_HOLE) {
1771 struct lov_ost_data_v1 *objs;
1774 if (magic == LOV_MAGIC_V1)
1775 objs = &lmm->lmm_objects[0];
1777 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1778 for (i = 0; i < count; i++, objs++) {
1779 if (lovea_slot_is_dummy(objs))
1783 /* If the @slot is the last dummy slot to be refilled,
1784 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1787 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1789 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1790 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1791 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1795 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1796 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1803 static struct lov_ost_data_v1 *
1804 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1805 const struct lu_fid *pfid,
1806 __u32 stripe_size, __u32 ea_off,
1807 __u32 pattern, __u16 count)
1809 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1810 lmm->lmm_pattern = cpu_to_le32(pattern);
1811 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1812 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1813 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1814 lmm->lmm_stripe_count = cpu_to_le16(count);
1815 lmm->lmm_layout_gen = cpu_to_le16(1);
1816 memset(&lmm->lmm_objects[0], 0,
1817 sizeof(struct lov_ost_data_v1) * count);
1819 return &lmm->lmm_objects[ea_off];
1822 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1823 struct lfsck_instance *lfsck,
1824 struct ost_layout *ol,
1825 struct dt_object *parent,
1826 struct lu_buf *buf, __u32 ea_off,
1827 struct lov_mds_md_v1 **lmm,
1828 struct lov_ost_data_v1 **objs)
1831 __u32 stripe_size = ol->ol_stripe_size;
1832 __u32 pattern = LOV_PATTERN_RAID0;
1835 if (ol->ol_stripe_count != 0)
1836 count = ol->ol_stripe_count;
1840 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1841 LASSERTF(buf->lb_len >= size,
1842 "buffer len %d is less than real size %d\n",
1843 (int)buf->lb_len, size);
1845 if (stripe_size == 0) {
1848 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1854 if (ol->ol_stripe_count > 1 ||
1855 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1856 pattern |= LOV_PATTERN_F_HOLE;
1857 memset(&(*lmm)->lmm_objects[0], 0,
1858 count * sizeof(struct lov_ost_data_v1));
1861 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1862 stripe_size, ea_off, pattern, count);
1867 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1868 struct lu_orphan_rec_v3 *rec,
1869 struct dt_object *parent,
1870 struct lu_buf *buf, __u32 ea_off,
1871 struct lov_mds_md_v1 **lmm,
1872 struct lov_ost_data_v1 **objs)
1874 struct ost_layout *ol = &rec->lor_layout;
1875 struct lov_comp_md_v1 *lcm;
1876 struct lov_comp_md_entry_v1 *lcme;
1877 __u32 pattern = LOV_PATTERN_RAID0;
1878 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1879 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1880 int size = offset + lcme_size;
1882 LASSERTF(buf->lb_len >= size,
1883 "buffer len %d is less than real size %d\n",
1884 (int)buf->lb_len, size);
1887 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1888 lcm->lcm_size = cpu_to_le32(size);
1889 if (rec->lor_range) {
1890 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1892 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1893 } else if (rec->lor_layout_version) {
1894 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1896 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1898 lcm->lcm_layout_gen = cpu_to_le32(1);
1899 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1901 lcm->lcm_entry_count = cpu_to_le16(1);
1902 /* Currently, we do not know how many mirrors will be, set it as zero
1903 * at the beginning. It will be updated when more mirrors are found. */
1904 lcm->lcm_mirror_count = 0;
1906 lcme = &lcm->lcm_entries[0];
1907 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1908 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1909 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1910 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1911 lcme->lcme_offset = cpu_to_le32(offset);
1912 lcme->lcme_size = cpu_to_le32(lcme_size);
1913 lcme->lcme_layout_gen = lcm->lcm_layout_gen;
1914 if (ol->ol_stripe_count > 1)
1915 pattern |= LOV_PATTERN_F_HOLE;
1917 *lmm = buf->lb_buf + offset;
1918 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1919 ol->ol_stripe_size, ea_off,
1920 pattern, ol->ol_stripe_count);
1925 static void lfsck_layout_update_lcm(struct lov_comp_md_v1 *lcm,
1926 struct lov_comp_md_entry_v1 *lcme,
1927 __u32 version, __u32 range)
1929 struct lov_comp_md_entry_v1 *tmp;
1930 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
1931 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
1932 __u32 gen = version + range;
1935 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1936 __u16 flags = le16_to_cpu(lcm->lcm_flags);
1940 lcme->lcme_layout_gen = cpu_to_le32(gen);
1941 if (le32_to_cpu(lcm->lcm_layout_gen) < gen)
1942 lcm->lcm_layout_gen = cpu_to_le32(gen);
1945 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1946 else if (flags == LCM_FL_NONE && le16_to_cpu(lcm->lcm_mirror_count) > 0)
1947 lcm->lcm_flags = cpu_to_le16(LCM_FL_RDONLY);
1949 for (i = 0; i < count; i++) {
1950 tmp = &lcm->lcm_entries[i];
1951 if (le64_to_cpu(tmp->lcme_extent.e_end) <= start)
1954 if (le64_to_cpu(tmp->lcme_extent.e_start) >= end)
1957 if (le32_to_cpu(tmp->lcme_flags) & LCME_FL_STALE)
1960 tmp_gen = le32_to_cpu(tmp->lcme_layout_gen);
1961 /* "lcme_layout_gen == 0" but without LCME_FL_STALE flag,
1962 * then it should be the latest version of all mirrors. */
1963 if (tmp_gen == 0 || tmp_gen > gen) {
1964 lcme->lcme_flags = cpu_to_le32(
1965 le32_to_cpu(lcme->lcme_flags) | LCME_FL_STALE);
1970 tmp->lcme_flags = cpu_to_le32(
1971 le32_to_cpu(tmp->lcme_flags) | LCME_FL_STALE);
1975 static int lfsck_layout_add_comp(const struct lu_env *env,
1976 struct lfsck_instance *lfsck,
1977 struct thandle *handle,
1978 struct lu_orphan_rec_v3 *rec,
1979 struct dt_object *parent,
1980 const struct lu_fid *cfid,
1981 struct lu_buf *buf, __u32 ost_idx,
1982 __u32 ea_off, int pos, bool new_mirror)
1984 struct ost_layout *ol = &rec->lor_layout;
1985 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1986 struct lov_comp_md_entry_v1 *lcme;
1987 struct lov_mds_md_v1 *lmm;
1988 struct lov_ost_data_v1 *objs;
1989 int added = sizeof(*lcme) +
1990 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1991 int size = le32_to_cpu(lcm->lcm_size) + added;
1995 __u32 pattern = LOV_PATTERN_RAID0;
1996 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1999 lu_buf_check_and_grow(buf, size);
2000 /* set the lcm again because lu_buf_check_and_grow() may
2001 * have reallocated the buf. */
2003 lcm->lcm_size = cpu_to_le32(size);
2004 lcm->lcm_entry_count = cpu_to_le16(count + 1);
2006 le16_add_cpu(&lcm->lcm_mirror_count, 1);
2008 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
2009 * with distance of 'added'. */
2012 for (i = pos; i < count; i++) {
2013 lcme = &lcm->lcm_entries[i];
2014 size += le32_to_cpu(lcme->lcme_size);
2017 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
2018 memmove(buf->lb_buf + offset + added,
2019 buf->lb_buf + offset, size);
2023 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
2024 * of 'sizeof(struct lov_comp_md_entry_v1)' */
2026 for (i = 0; i < pos; i++) {
2027 lcme = &lcm->lcm_entries[i];
2028 size += le32_to_cpu(lcme->lcme_size);
2031 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
2032 memmove(buf->lb_buf + offset + sizeof(*lcme),
2033 buf->lb_buf + offset, size);
2036 /* 3. Recalculate the enter offset for the component [pos, count-1] */
2037 for (i = count - 1; i >= pos; i--) {
2038 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
2039 lcm->lcm_entries[i + 1].lcme_offset =
2040 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
2041 lcme_offset) + added);
2044 /* 4. Recalculate the enter offset for the component [0, pos) */
2045 for (i = 0; i < pos; i++) {
2046 lcm->lcm_entries[i].lcme_offset =
2047 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
2048 lcme_offset) + sizeof(*lcme));
2051 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
2052 /* 4. Insert the new component header (entry) at the slot 'pos'. */
2053 lcme = &lcm->lcm_entries[pos];
2054 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
2055 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2056 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
2057 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
2058 lcme->lcme_offset = cpu_to_le32(offset);
2059 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
2062 if (ol->ol_stripe_count > 1)
2063 pattern |= LOV_PATTERN_F_HOLE;
2065 lmm = buf->lb_buf + offset;
2066 /* 5. Insert teh new component body at the 'offset'. */
2067 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
2068 ol->ol_stripe_size, ea_off,
2069 pattern, ol->ol_stripe_count);
2071 /* 6. Update mirror related flags and version. */
2072 lfsck_layout_update_lcm(lcm, lcme, rec->lor_layout_version,
2075 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2076 lmm, objs, LU_XATTR_REPLACE, ost_idx,
2077 le32_to_cpu(lcm->lcm_size));
2079 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
2080 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2081 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2082 "comp_end %llu, layout version %u, range %u, "
2083 "%s LOV EA hole: rc = %d\n",
2084 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2085 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2086 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2087 rec->lor_layout_version, rec->lor_range,
2088 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2089 "with" : "without", rc);
2094 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
2095 struct lfsck_instance *lfsck,
2096 struct thandle *handle,
2097 struct ost_layout *ol,
2098 struct dt_object *parent,
2099 const struct lu_fid *cfid,
2100 struct lu_buf *buf, __u32 ost_idx,
2103 struct lov_mds_md_v1 *lmm = buf->lb_buf;
2104 struct lov_ost_data_v1 *objs;
2105 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
2106 __u32 magic = le32_to_cpu(lmm->lmm_magic);
2112 /* The original LOVEA maybe re-generated via old filter_fid, at
2113 * that time, we do not know the stripe count and stripe size. */
2114 if (ol->ol_stripe_count > count)
2115 count = ol->ol_stripe_count;
2116 if (ol->ol_stripe_size != 0 &&
2117 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
2118 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
2120 if (magic == LOV_MAGIC_V1)
2121 objs = &lmm->lmm_objects[count];
2123 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
2125 gap = ea_off - count;
2129 size = lov_mds_md_size(count, magic);
2130 LASSERTF(buf->lb_len >= size,
2131 "buffer len %d is less than real size %d\n",
2132 (int)buf->lb_len, size);
2135 memset(objs, 0, gap * sizeof(*objs));
2136 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
2139 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2140 lmm->lmm_stripe_count = cpu_to_le16(count);
2143 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2144 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
2146 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
2147 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2148 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2149 "comp_end %llu, %s LOV EA hole: rc = %d\n",
2150 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2151 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2152 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2153 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2154 "with" : "without", rc);
2160 * \retval +1: repaired
2161 * \retval 0: did nothing
2162 * \retval -ve: on error
2164 static int lfsck_layout_update_lovea(const struct lu_env *env,
2165 struct lfsck_instance *lfsck,
2166 struct thandle *handle,
2167 struct lu_orphan_rec_v3 *rec,
2168 struct dt_object *parent,
2169 const struct lu_fid *cfid,
2170 struct lu_buf *buf, int fl,
2171 __u32 ost_idx, __u32 ea_off)
2173 struct ost_layout *ol = &rec->lor_layout;
2174 struct lov_mds_md_v1 *lmm = NULL;
2175 struct lov_ost_data_v1 *objs = NULL;
2179 if (ol->ol_comp_id != 0)
2180 rc = lfsck_layout_new_comp_lovea(env, rec, parent, buf, ea_off,
2183 rc = lfsck_layout_new_v1_lovea(env, lfsck, &rec->lor_layout,
2184 parent, buf, ea_off, &lmm,
2187 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2188 buf, lmm, objs, fl, ost_idx, rc);
2190 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2191 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2192 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2193 "comp_end %llu, layout version %u, range %u, fl %d, "
2194 "%s LOV EA hole: rc = %d\n",
2195 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2196 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2197 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2198 rec->lor_layout_version, rec->lor_range, fl,
2199 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2200 "with" : "without", rc);
2205 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2206 struct lfsck_component *com,
2207 struct dt_object *child,
2208 const struct lu_fid *pfid,
2209 const struct ost_layout *ol, __u32 offset,
2210 __u32 version, __u32 range)
2212 struct dt_device *dev = lfsck_obj2dev(child);
2213 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2214 struct thandle *handle;
2215 struct lu_buf buf = { NULL };
2218 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2219 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2220 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2221 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2222 * parent MDT-object's layout EA. */
2223 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2224 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2225 ff->ff_layout_version = cpu_to_le32(version);
2226 ff->ff_range = cpu_to_le32(range);
2227 lfsck_buf_init(&buf, ff, sizeof(*ff));
2229 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
2231 RETURN(PTR_ERR(handle));
2233 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2237 rc = dt_trans_start_local(env, dev, handle);
2241 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2246 dt_trans_stop(env, dev, handle);
2252 * \retval +1: repaired
2253 * \retval 0: did nothing
2254 * \retval -ve: on error
2256 static int lfsck_layout_update_pfid(const struct lu_env *env,
2257 struct lfsck_component *com,
2258 struct dt_object *parent,
2259 struct lu_fid *cfid,
2260 struct dt_device *cdev,
2261 struct lu_orphan_rec_v3 *rec, __u32 ea_off)
2263 struct dt_object *child;
2267 child = lfsck_object_find_by_dev(env, cdev, cfid);
2269 RETURN(PTR_ERR(child));
2271 rc = __lfsck_layout_update_pfid(env, com, child,
2272 lu_object_fid(&parent->do_lu),
2273 &rec->lor_layout, ea_off,
2274 rec->lor_layout_version,
2276 lfsck_object_put(env, child);
2278 RETURN(rc == 0 ? 1 : rc);
2281 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2283 if (ol->ol_comp_id != 0)
2284 return sizeof(struct lov_comp_md_v1) +
2285 sizeof(struct lov_comp_md_entry_v1) +
2286 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2288 if (ol->ol_stripe_count != 0)
2289 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2291 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2295 * This function will create the MDT-object with the given (partial) LOV EA.
2297 * Under some data corruption cases, the MDT-object of the file may be lost,
2298 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2299 * re-create the MDT-object with the orphan OST-object(s) information.
2301 * On the other hand, the LFSCK may has created some OST-object for repairing
2302 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2303 * the old OST-object is there and should replace the former new created OST
2304 * object. Unfortunately, some others have modified such newly created object.
2305 * To keep the data (both new and old), the LFSCK will create MDT-object with
2306 * new FID to reference the original OST-object.
2308 * \param[in] env pointer to the thread context
2309 * \param[in] com pointer to the lfsck component
2310 * \param[in] ltd pointer to target device descriptor
2311 * \param[in] rec pointer to the record for the orphan OST-object
2312 * \param[in] cfid pointer to FID for the orphan OST-object
2313 * \param[in] infix additional information, such as the FID for original
2314 * MDT-object and the stripe offset in the LOV EA
2315 * \param[in] type the type for describing why the orphan MDT-object is
2316 * created. The rules are as following:
2318 * type "C": Multiple OST-objects claim the same MDT-object and the
2319 * same slot in the layout EA. Then the LFSCK will create
2320 * new MDT-object(s) to hold the conflict OST-object(s).
2322 * type "N": The orphan OST-object does not know which one was the
2323 * real parent MDT-object, so the LFSCK uses new FID for
2324 * its parent MDT-object.
2326 * type "R": The orphan OST-object knows its parent MDT-object FID,
2327 * but does not know the position (the file name) in the
2330 * type "D": The MDT-object is a directory, it may knows its parent
2331 * but because there is no valid linkEA, the LFSCK cannot
2332 * know where to put it back to the namespace.
2333 * type "O": The MDT-object has no linkEA, and there is no name
2334 * entry that references the MDT-object.
2336 * type "P": The orphan object to be created was a parent directory
2337 * of some MDT-object which linkEA shows that the @orphan
2338 * object is missing.
2340 * The orphan name will be like:
2341 * ${FID}-${infix}-${type}-${conflict_version}
2343 * \param[in] ea_off the stripe offset in the LOV EA
2345 * \retval positive on repaired something
2346 * \retval 0 if needs to repair nothing
2347 * \retval negative error number on failure
2349 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2350 struct lfsck_component *com,
2351 struct lfsck_tgt_desc *ltd,
2352 struct lu_orphan_rec_v3 *rec,
2353 struct lu_fid *cfid,
2358 struct lfsck_thread_info *info = lfsck_env_info(env);
2359 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2360 char *name = info->lti_key;
2361 struct lu_attr *la = &info->lti_la2;
2362 struct dt_object_format *dof = &info->lti_dof;
2363 struct lfsck_instance *lfsck = com->lc_lfsck;
2364 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2365 struct lu_fid *tfid = &info->lti_fid3;
2366 struct dt_device *dev = lfsck->li_bottom;
2367 struct dt_object *lpf = lfsck->li_lpf_obj;
2368 struct dt_object *pobj = NULL;
2369 struct dt_object *cobj = NULL;
2370 struct thandle *th = NULL;
2371 struct lu_buf *ea_buf = &info->lti_big_buf;
2372 struct lu_buf lov_buf;
2373 struct lfsck_lock_handle *llh = &info->lti_llh;
2374 struct linkea_data ldata = { NULL };
2375 struct lu_buf linkea_buf;
2376 const struct lu_name *pname;
2382 if (lfsck_is_dryrun(lfsck))
2385 if (unlikely(lpf == NULL))
2386 GOTO(log, rc = -ENXIO);
2388 /* We use two separated transactions to repair the inconsistency.
2390 * 1) create the MDT-object locally.
2391 * 2) update the OST-object's PFID EA if necessary.
2393 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2394 * updated when the layout LFSCK run next time.
2396 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2397 * when the layout LFSCK run next time. */
2399 if (fid_is_zero(pfid)) {
2400 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2404 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2406 GOTO(log, rc = PTR_ERR(cobj));
2409 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2411 GOTO(log, rc = PTR_ERR(pobj));
2413 LASSERT(infix != NULL);
2414 LASSERT(type != NULL);
2416 memset(la, 0, sizeof(*la));
2417 la->la_uid = rec->lor_rec.lor_uid;
2418 la->la_gid = rec->lor_rec.lor_gid;
2419 la->la_mode = S_IFREG | S_IRUSR;
2420 la->la_valid = LA_MODE | LA_UID | LA_GID;
2422 memset(dof, 0, sizeof(*dof));
2423 dof->dof_type = dt_mode_to_dft(S_IFREG);
2424 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2425 * the stripe(s). The LFSCK will specify the LOV EA via
2426 * lfsck_layout_update_lovea(). */
2428 size = lfsck_lovea_size(&rec->lor_layout, ea_off);
2429 if (ea_buf->lb_len < size) {
2430 lu_buf_realloc(ea_buf, size);
2431 if (ea_buf->lb_buf == NULL)
2432 GOTO(log, rc = -ENOMEM);
2437 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2439 rc = dt_lookup_dir(env, lfsck->li_lpf_obj, name, tfid);
2440 if (rc != 0 && rc != -ENOENT)
2444 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2445 MDS_INODELOCK_UPDATE, LCK_PW);
2449 /* Re-check whether the name conflict with othrs after taken
2451 rc = dt_lookup_dir(env, lfsck->li_lpf_obj, name, tfid);
2452 if (unlikely(rc == 0)) {
2460 pname = lfsck_name_get_const(env, name, strlen(name));
2461 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2462 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2466 /* The 1st transaction. */
2467 th = lfsck_trans_create(env, dev, lfsck);
2469 GOTO(unlock, rc = PTR_ERR(th));
2471 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2475 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2476 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2477 LU_XATTR_CREATE, th);
2481 dtrec->rec_fid = pfid;
2482 dtrec->rec_type = S_IFREG;
2483 rc = dt_declare_insert(env, lpf,
2484 (const struct dt_rec *)dtrec,
2485 (const struct dt_key *)name, th);
2489 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2490 ldata.ld_leh->leh_len);
2491 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2492 XATTR_NAME_LINK, 0, th);
2496 rc = dt_trans_start_local(env, dev, th);
2500 dt_write_lock(env, pobj, 0);
2501 rc = dt_create(env, pobj, la, NULL, dof, th);
2503 rc = lfsck_layout_update_lovea(env, lfsck, th, rec, pobj, cfid,
2504 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2505 dt_write_unlock(env, pobj);
2509 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2510 (const struct dt_key *)name, th);
2514 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2515 if (rc == 0 && cobj != NULL) {
2516 dt_trans_stop(env, dev, th);
2519 /* The 2nd transaction. */
2520 rc = __lfsck_layout_update_pfid(env, com, cobj, pfid,
2521 &rec->lor_layout, ea_off,
2522 rec->lor_layout_version,
2530 dt_trans_stop(env, dev, th);
2536 if (cobj != NULL && !IS_ERR(cobj))
2537 lfsck_object_put(env, cobj);
2538 if (pobj != NULL && !IS_ERR(pobj))
2539 lfsck_object_put(env, pobj);
2542 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2543 "recreate the lost MDT-object: parent "DFID
2544 ", child "DFID", OST-index %u, stripe-index %u, "
2545 "infix %s, type %s: rc = %d\n",
2546 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2547 ltd->ltd_index, ea_off, infix, type, rc);
2549 return rc >= 0 ? 1 : rc;
2552 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2553 struct lfsck_component *com,
2554 const struct lu_fid *fid,
2557 struct lfsck_thread_info *info = lfsck_env_info(env);
2558 struct lfsck_request *lr = &info->lti_lr;
2559 struct lfsck_instance *lfsck = com->lc_lfsck;
2560 struct lfsck_tgt_desc *ltd;
2561 struct ptlrpc_request *req;
2562 struct lfsck_request *tmp;
2563 struct obd_export *exp;
2567 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2568 if (unlikely(ltd == NULL))
2572 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2573 GOTO(put, rc = -EOPNOTSUPP);
2575 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2577 GOTO(put, rc = -ENOMEM);
2579 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2581 ptlrpc_request_free(req);
2586 memset(lr, 0, sizeof(*lr));
2587 lr->lr_event = LE_CONDITIONAL_DESTROY;
2588 lr->lr_active = LFSCK_TYPE_LAYOUT;
2591 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2593 ptlrpc_request_set_replen(req);
2595 rc = ptlrpc_queue_wait(req);
2596 ptlrpc_req_finished(req);
2606 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2607 struct lfsck_component *com,
2608 struct lfsck_request *lr)
2610 struct lfsck_thread_info *info = lfsck_env_info(env);
2611 struct lu_attr *la = &info->lti_la;
2612 union ldlm_policy_data *policy = &info->lti_policy;
2613 struct ldlm_res_id *resid = &info->lti_resid;
2614 struct lfsck_instance *lfsck = com->lc_lfsck;
2615 struct dt_device *dev = lfsck->li_bottom;
2616 struct lu_fid *fid = &lr->lr_fid;
2617 struct dt_object *obj;
2618 struct thandle *th = NULL;
2619 struct lustre_handle lh = { 0 };
2624 obj = lfsck_object_find_by_dev(env, dev, fid);
2626 RETURN(PTR_ERR(obj));
2628 dt_read_lock(env, obj, 0);
2629 if (dt_object_exists(obj) == 0 ||
2630 lfsck_is_dead_obj(obj)) {
2631 dt_read_unlock(env, obj);
2633 GOTO(put, rc = -ENOENT);
2636 /* Get obj's attr without lock firstly. */
2637 rc = dt_attr_get(env, obj, la);
2638 dt_read_unlock(env, obj);
2642 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2643 GOTO(put, rc = -ETXTBSY);
2645 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2646 LASSERT(lfsck->li_namespace != NULL);
2648 memset(policy, 0, sizeof(*policy));
2649 policy->l_extent.end = OBD_OBJECT_EOF;
2650 ost_fid_build_resid(fid, resid);
2651 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
2652 LDLM_EXTENT, policy, LCK_EX, &flags,
2653 ldlm_blocking_ast, ldlm_completion_ast,
2654 NULL, NULL, 0, LVB_T_NONE, NULL, &lh);
2656 GOTO(put, rc = -EIO);
2658 dt_write_lock(env, obj, 0);
2659 /* Get obj's attr within lock again. */
2660 rc = dt_attr_get(env, obj, la);
2664 if (la->la_ctime != 0)
2665 GOTO(unlock, rc = -ETXTBSY);
2667 th = lfsck_trans_create(env, dev, lfsck);
2669 GOTO(unlock, rc = PTR_ERR(th));
2671 rc = dt_declare_ref_del(env, obj, th);
2675 rc = dt_declare_destroy(env, obj, th);
2679 rc = dt_trans_start_local(env, dev, th);
2683 rc = dt_ref_del(env, obj, th);
2687 rc = dt_destroy(env, obj, th);
2689 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2690 "OST-object "DFID" that was created for reparing "
2691 "dangling referenced case. But the original missing "
2692 "OST-object is found now.\n",
2693 lfsck_lfsck2name(lfsck), PFID(fid));
2698 dt_trans_stop(env, dev, th);
2701 dt_write_unlock(env, obj);
2702 ldlm_lock_decref(&lh, LCK_EX);
2705 lfsck_object_put(env, obj);
2711 * Some OST-object has occupied the specified layout EA slot.
2712 * Such OST-object may be generated by the LFSCK when repair
2713 * dangling referenced MDT-object, which can be indicated by
2714 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2715 * is true and such OST-object has not been modified yet, we
2716 * will replace it with the orphan OST-object; otherwise the
2717 * LFSCK will create new MDT-object to reference the orphan.
2719 * \retval +1: repaired
2720 * \retval 0: did nothing
2721 * \retval -ve: on error
2723 static int lfsck_layout_conflict_create(const struct lu_env *env,
2724 struct lfsck_component *com,
2725 struct lfsck_tgt_desc *ltd,
2726 struct lu_orphan_rec_v3 *rec,
2727 struct dt_object *parent,
2728 struct lu_fid *cfid,
2729 struct lu_buf *ea_buf,
2730 struct lov_mds_md_v1 *lmm,
2731 struct lov_ost_data_v1 *slot,
2732 __u32 ea_off, int lovea_size)
2734 struct lfsck_thread_info *info = lfsck_env_info(env);
2735 struct lu_fid *cfid2 = &info->lti_fid2;
2736 struct ost_id *oi = &info->lti_oi;
2737 struct dt_device *dev = lfsck_obj2dev(parent);
2738 struct thandle *th = NULL;
2739 struct lustre_handle lh = { 0 };
2740 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2744 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2745 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2749 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2750 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2754 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2755 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2760 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2762 /* If the conflict OST-obejct is not created for fixing dangling
2763 * referenced MDT-object in former LFSCK check/repair, or it has
2764 * been modified by others, then we cannot destroy it. Re-create
2765 * a new MDT-object for the orphan OST-object. */
2766 if (rc == -ETXTBSY) {
2767 /* No need the layout lock on the original parent. */
2768 lfsck_ibits_unlock(&lh, LCK_EX);
2770 fid_zero(&rec->lor_rec.lor_fid);
2771 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2772 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2774 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2775 info->lti_tmpbuf, "C", ea_off);
2780 if (rc != 0 && rc != -ENOENT)
2783 if (lfsck_is_dryrun(com->lc_lfsck))
2784 GOTO(unlock, rc = 0);
2786 th = lfsck_trans_create(env, dev, com->lc_lfsck);
2788 GOTO(unlock, rc = PTR_ERR(th));
2790 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2791 LU_XATTR_REPLACE, th);
2795 rc = dt_trans_start_local(env, dev, th);
2799 dt_write_lock(env, parent, 0);
2800 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2801 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2802 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2803 ltd->ltd_index, lovea_size);
2804 dt_write_unlock(env, parent);
2809 dt_trans_stop(env, dev, th);
2812 lfsck_ibits_unlock(&lh, LCK_EX);
2815 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2816 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2817 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2818 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2819 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2822 return rc >= 0 ? 1 : rc;
2826 * \retval +1: repaired
2827 * \retval 0: did nothing
2828 * \retval -ve: on error
2830 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2831 struct lfsck_component *com,
2832 struct lfsck_tgt_desc *ltd,
2833 struct lu_orphan_rec_v3 *rec,
2834 struct dt_object *parent,
2835 struct lu_fid *cfid,
2836 __u32 ost_idx, __u32 ea_off)
2838 struct lfsck_thread_info *info = lfsck_env_info(env);
2839 struct lu_buf *buf = &info->lti_big_buf;
2840 struct lu_fid *fid = &info->lti_fid2;
2841 struct ost_id *oi = &info->lti_oi;
2842 struct lfsck_instance *lfsck = com->lc_lfsck;
2843 struct dt_device *dt = lfsck_obj2dev(parent);
2844 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2845 struct ost_layout *ol = &rec->lor_layout;
2846 struct lov_comp_md_v1 *lcm = NULL;
2847 struct lov_comp_md_entry_v1 *lcme = NULL;
2848 struct thandle *handle = NULL;
2850 struct lov_mds_md_v1 *lmm;
2851 struct lov_ost_data_v1 *objs;
2852 struct lustre_handle lh = { 0 };
2861 bool locked = false;
2862 bool new_mirror = true;
2865 if (lfsck_is_dryrun(lfsck))
2868 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2869 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2872 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2873 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2874 "stripe-index %u, comp_id %u, comp_start %llu, "
2875 "comp_end %llu, layout version %u, range %u: rc = %d\n",
2876 lfsck_lfsck2name(lfsck), PFID(cfid),
2877 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2878 ol->ol_comp_id, ol->ol_comp_start,
2879 ol->ol_comp_end, rec->lor_layout_version,
2880 rec->lor_range, rc);
2887 dt_write_unlock(env, parent);
2891 if (handle != NULL) {
2892 dt_trans_stop(env, dt, handle);
2897 GOTO(unlock_layout, rc);
2900 if (buf->lb_len < lovea_size) {
2901 lu_buf_realloc(buf, lovea_size);
2902 if (buf->lb_buf == NULL)
2903 GOTO(unlock_layout, rc = -ENOMEM);
2906 if (!(bk->lb_param & LPF_DRYRUN)) {
2907 handle = lfsck_trans_create(env, dt, lfsck);
2909 GOTO(unlock_layout, rc = PTR_ERR(handle));
2911 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2916 rc = dt_trans_start_local(env, dt, handle);
2921 dt_write_lock(env, parent, 0);
2923 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2924 if (rc == -ERANGE) {
2925 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2928 } else if (rc == -ENODATA || rc == 0) {
2929 lovea_size = lfsck_lovea_size(ol, ea_off);
2930 /* If the declared is not big enough, re-try. */
2931 if (buf->lb_len < lovea_size) {
2935 fl = LU_XATTR_CREATE;
2936 } else if (rc < 0) {
2937 GOTO(unlock_parent, rc);
2938 } else if (unlikely(buf->lb_len == 0)) {
2941 fl = LU_XATTR_REPLACE;
2945 if (fl == LU_XATTR_CREATE) {
2946 if (bk->lb_param & LPF_DRYRUN)
2947 GOTO(unlock_parent, rc = 1);
2949 LASSERT(buf->lb_len >= lovea_size);
2951 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2952 cfid, buf, fl, ost_idx, ea_off);
2954 GOTO(unlock_parent, rc);
2958 rc1 = lfsck_layout_verify_header(parent, lmm, lovea_size);
2960 /* If the LOV EA crashed, the rebuild it. */
2961 if (rc1 == -EINVAL) {
2962 if (bk->lb_param & LPF_DRYRUN)
2963 GOTO(unlock_parent, rc = 1);
2965 LASSERT(buf->lb_len >= lovea_size);
2967 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2968 cfid, buf, fl, ost_idx, ea_off);
2970 GOTO(unlock_parent, rc);
2973 /* For other unknown magic/pattern, keep the current LOV EA. */
2974 if (rc1 == -EOPNOTSUPP)
2975 GOTO(unlock_parent, rc1 = 0);
2978 GOTO(unlock_parent, rc = rc1);
2980 magic = le32_to_cpu(lmm->lmm_magic);
2981 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
2984 __u16 mirror_id0 = mirror_id_of(ol->ol_comp_id);
2987 if (bk->lb_param & LPF_DRYRUN)
2988 GOTO(unlock_parent, rc = 1);
2991 count = le16_to_cpu(lcm->lcm_entry_count);
2992 for (i = 0; i < count; pos = ++i) {
2993 lcme = &lcm->lcm_entries[i];
2994 start = le64_to_cpu(lcme->lcme_extent.e_start);
2995 end = le64_to_cpu(lcme->lcme_extent.e_end);
2996 mirror_id1 = mirror_id_of(le32_to_cpu(lcme->lcme_id));
2998 if (mirror_id0 > mirror_id1)
3001 if (mirror_id0 < mirror_id1)
3005 if (end <= ol->ol_comp_start)
3008 if (start >= ol->ol_comp_end)
3011 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
3012 magic = le32_to_cpu(lmm->lmm_magic);
3013 flags = le32_to_cpu(lcme->lcme_flags);
3017 rc = lfsck_layout_add_comp(env, lfsck, handle, rec, parent,
3018 cfid, buf, ost_idx, ea_off, pos, new_mirror);
3020 GOTO(unlock_parent, rc);
3024 count = le16_to_cpu(lmm->lmm_stripe_count);
3026 GOTO(unlock_parent, rc = -EINVAL);
3029 /* Exceed the current end of MDT-object layout EA. Then extend it. */
3030 if (count <= ea_off) {
3031 if (bk->lb_param & LPF_DRYRUN)
3032 GOTO(unlock_parent, rc = 1);
3034 lovea_size = lov_mds_md_size(ea_off + 1, magic);
3035 /* If the declared is not big enough, re-try. */
3036 if (buf->lb_len < lovea_size) {
3044 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
3045 lfsck_layout_update_lcm(lcm, lcme,
3046 rec->lor_layout_version,
3050 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
3051 parent, cfid, buf, ost_idx, ea_off);
3053 GOTO(unlock_parent, rc);
3056 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
3058 if (magic == LOV_MAGIC_V1) {
3059 objs = &lmm->lmm_objects[0];
3061 LASSERT(magic == LOV_MAGIC_V3);
3062 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3065 for (i = 0; i < count; i++, objs++) {
3066 /* The MDT-object was created via lfsck_layout_recover_create()
3067 * by others before, and we fill the dummy layout EA. */
3068 if ((lcme && !(flags & LCME_FL_INIT)) ||
3069 lovea_slot_is_dummy(objs)) {
3073 if (bk->lb_param & LPF_DRYRUN)
3074 GOTO(unlock_parent, rc = 1);
3076 lmm->lmm_layout_gen =
3077 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3081 if (le32_to_cpu(lmm->lmm_stripe_size) !=
3082 ol->ol_stripe_size ||
3083 le16_to_cpu(lmm->lmm_stripe_count) !=
3084 ol->ol_stripe_count ||
3085 le64_to_cpu(lcme->lcme_extent.e_start) !=
3086 ol->ol_comp_start ||
3087 le64_to_cpu(lcme->lcme_extent.e_end) !=
3089 CDEBUG(D_LFSCK, "%s: found invalid "
3090 "component for "DFID ": parent "DFID
3091 ", stripe-index %u, stripe_size %u, "
3092 "stripe_count %u, comp_id %u, "
3093 "comp_start %llu, comp_end %llu, "
3094 "cur_stripe_size %u, "
3095 "cur_stripe_count %u, "
3096 "cur_comp_start %llu, "
3097 "cur_comp_end %llu\n",
3098 lfsck_lfsck2name(lfsck), PFID(cfid),
3099 PFID(lfsck_dto2fid(parent)), ea_off,
3101 ol->ol_stripe_count, ol->ol_comp_id,
3102 ol->ol_comp_start, ol->ol_comp_end,
3103 le32_to_cpu(lmm->lmm_stripe_size),
3104 le16_to_cpu(lmm->lmm_stripe_count),
3105 le64_to_cpu(lcme->lcme_extent.e_start),
3106 le64_to_cpu(lcme->lcme_extent.e_end));
3108 GOTO(unlock_parent, rc = -EINVAL);
3111 lovea_size = le32_to_cpu(lcm->lcm_size);
3112 lcme->lcme_flags = cpu_to_le32(flags |
3114 lfsck_layout_update_lcm(lcm, lcme,
3115 rec->lor_layout_version,
3119 LASSERTF(buf->lb_len >= lovea_size,
3120 "buffer len %d is less than real size %d\n",
3121 (int)buf->lb_len, (int)lovea_size);
3123 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
3124 parent, cfid, buf, lmm, objs,
3125 fl, ost_idx, lovea_size);
3127 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
3128 "dummy layout slot for "DFID": parent "DFID
3129 ", OST-index %u, stripe-index %u: rc = %d\n",
3130 lfsck_lfsck2name(lfsck), PFID(cfid),
3131 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
3133 GOTO(unlock_parent, rc);
3136 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3137 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
3139 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3140 "invalid layout EA at the slot %d, index %u\n",
3141 lfsck_lfsck2name(lfsck),
3142 PFID(lfsck_dto2fid(parent)), i,
3143 le32_to_cpu(objs->l_ost_idx));
3145 GOTO(unlock_parent, rc);
3148 /* It should be rare case, the slot is there, but the LFSCK
3149 * does not handle it during the first-phase cycle scanning. */
3150 if (unlikely(lu_fid_eq(fid, cfid))) {
3152 GOTO(unlock_parent, rc = 0);
3154 /* Rare case that the OST-object index
3155 * does not match the parent MDT-object
3156 * layout EA. We trust the later one. */
3157 if (bk->lb_param & LPF_DRYRUN)
3158 GOTO(unlock_parent, rc = 1);
3160 dt_write_unlock(env, parent);
3162 dt_trans_stop(env, dt, handle);
3163 lfsck_ibits_unlock(&lh, LCK_EX);
3164 rc = lfsck_layout_update_pfid(env, com, parent,
3168 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
3169 "updated OST-object's pfid for "DFID
3170 ": parent "DFID", OST-index %u, "
3171 "stripe-index %u: rc = %d\n",
3172 lfsck_lfsck2name(lfsck), PFID(cfid),
3173 PFID(lfsck_dto2fid(parent)),
3174 ltd->ltd_index, i, rc);
3181 /* The MDT-object exists, but related layout EA slot is occupied
3183 if (bk->lb_param & LPF_DRYRUN)
3184 GOTO(unlock_parent, rc = 1);
3186 dt_write_unlock(env, parent);
3188 dt_trans_stop(env, dt, handle);
3189 lfsck_ibits_unlock(&lh, LCK_EX);
3190 if (magic == LOV_MAGIC_V1)
3191 objs = &lmm->lmm_objects[ea_off];
3193 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
3194 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
3195 buf, lmm, objs, ea_off, lovea_size);
3201 dt_write_unlock(env, parent);
3205 dt_trans_stop(env, dt, handle);
3208 lfsck_ibits_unlock(&lh, LCK_EX);
3213 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3214 struct lfsck_component *com,
3215 struct lfsck_tgt_desc *ltd,
3216 struct lu_orphan_rec_v3 *rec,
3217 struct lu_fid *cfid)
3219 struct lfsck_layout *lo = com->lc_file_ram;
3220 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3221 struct dt_object *parent = NULL;
3222 __u32 ea_off = pfid->f_stripe_idx;
3226 if (!fid_is_sane(cfid))
3227 GOTO(out, rc = -EINVAL);
3230 if (fid_is_zero(pfid)) {
3231 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3236 if (!fid_is_sane(pfid))
3237 GOTO(out, rc = -EINVAL);
3239 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3241 GOTO(out, rc = PTR_ERR(parent));
3243 if (unlikely(dt_object_remote(parent) != 0))
3244 GOTO(put, rc = -EXDEV);
3246 if (dt_object_exists(parent) == 0) {
3247 lfsck_object_put(env, parent);
3248 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3253 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3254 GOTO(put, rc = -EISDIR);
3256 /* The orphan OST-object claims to be the parent's stripe, then
3257 * related dangling record in the trace file is meaningless. */
3258 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3259 rec->lor_layout.ol_comp_id, ea_off);
3260 if (rc && rc != -ENOENT)
3263 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3264 ltd->ltd_index, ea_off);
3270 lfsck_object_put(env, parent);
3272 /* The layout EA is changed, need to be reloaded next time. */
3273 dt_object_put_nocache(env, parent);
3276 down_write(&com->lc_sem);
3277 com->lc_new_scanned++;
3278 com->lc_new_checked++;
3280 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3282 } else if (rc < 0) {
3283 lo->ll_objs_failed_phase2++;
3285 up_write(&com->lc_sem);
3290 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3291 struct lfsck_component *com,
3292 struct lfsck_tgt_desc *ltd)
3294 struct lfsck_assistant_data *lad = com->lc_data;
3295 struct lfsck_instance *lfsck = com->lc_lfsck;
3296 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3297 struct lfsck_thread_info *info = lfsck_env_info(env);
3298 struct lu_fid *fid = &info->lti_fid;
3299 struct dt_object *obj;
3300 const struct dt_it_ops *iops;
3305 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3306 "scanning for OST%04x\n",
3307 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3309 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
3310 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3311 "scanning for OST%04x\n",
3312 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3317 fid->f_seq = fid_idif_seq(0, ltd->ltd_index);
3318 fid->f_oid = fid->f_ver = 0;
3320 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3321 if (unlikely(IS_ERR(obj)))
3322 GOTO(log, rc = PTR_ERR(obj));
3324 rc = obj->do_ops->do_index_try(env, obj,
3325 &dt_lfsck_layout_orphan_features);
3329 iops = &obj->do_index_ops->dio_it;
3330 di = iops->init(env, obj, 0);
3332 GOTO(put, rc = PTR_ERR(di));
3334 rc = iops->load(env, di, 0);
3336 /* -ESRCH means that the orphan OST-objects rbtree has been
3337 * cleanup because of the OSS server restart or other errors. */
3338 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3343 rc = iops->next(env, di);
3355 struct lu_orphan_rec_v3 *rec = &info->lti_rec;
3357 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3358 unlikely(!thread_is_running(&lfsck->li_thread)))
3361 key = iops->key(env, di);
3362 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3363 /* Remote target OST may be runnning old LFSCK */
3364 memset(rec, 0, sizeof(*rec));
3365 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3367 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3368 &com->lc_fid_latest_scanned_phase2);
3369 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3372 lfsck_control_speed_by_self(com);
3374 rc = iops->next(env, di);
3375 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3382 iops->fini(env, di);
3384 lfsck_object_put(env, obj);
3387 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3388 "scanning for OST%04x: rc = %d\n",
3389 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3391 return rc > 0 ? 0 : rc;
3394 static int lfsck_lov2layout(struct lov_mds_md_v1 *lmm, struct filter_fid *ff,
3397 struct ost_layout *ol = &ff->ff_layout;
3398 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3402 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3403 ol->ol_stripe_size = lmm->lmm_stripe_size;
3404 ol->ol_stripe_count = lmm->lmm_stripe_count;
3405 ol->ol_comp_start = 0;
3406 ol->ol_comp_end = 0;
3408 ff->ff_layout_version = 0;
3410 } else if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
3411 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3412 struct lov_comp_md_entry_v1 *lcme = NULL;
3413 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3416 for (i = 0; i < count; i++) {
3417 lcme = &lcm->lcm_entries[i];
3418 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3419 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3426 /* The comp has been removed, do nothing. */
3430 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3431 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3432 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3433 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3434 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3435 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3436 ff->ff_layout_version = le32_to_cpu(lcme->lcme_layout_gen);
3439 GOTO(out, rc = -EINVAL);
3449 * Repair the MDT-object with dangling LOV EA reference.
3451 * we need to repair the inconsistency according to the users' requirement:
3453 * 1) Keep the inconsistency there and report the inconsistency case,
3454 * then give the chance to the application to find related issues,
3455 * and the users can make the decision about how to handle it with
3456 * more human knownledge. (by default)
3458 * 2) Re-create the missing OST-object with the FID/owner information.
3460 * \param[in] env pointer to the thread context
3461 * \param[in] com the layout LFSCK component
3462 * \param[in] parent the MDT-object with dangling LOV EA reference
3463 * \param[in] child the OST-object to be created
3464 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3465 * \param[in] ea_off the offset of the OST-object in the LOV EA
3466 * \param[in] ost_idx the index of OST on which the OST-object resides
3468 * \retval +1 for repair successfully
3469 * \retval 0 for did nothing
3470 * \retval negative error number on failure
3472 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3473 struct lfsck_component *com,
3474 struct dt_object *parent,
3475 struct dt_object *child,
3476 __u32 comp_id, __u32 ea_off,
3477 __u32 ost_idx, bool log)
3479 struct lfsck_thread_info *info = lfsck_env_info(env);
3480 struct filter_fid *ff = &info->lti_ff;
3481 struct dt_object_format *dof = &info->lti_dof;
3482 struct lu_attr *la = &info->lti_la;
3483 struct lfsck_instance *lfsck = com->lc_lfsck;
3484 struct dt_device *dev = lfsck_obj2dev(child);
3485 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3486 const struct lu_fid *cfid = lfsck_dto2fid(child);
3487 struct lu_buf *tbuf = &info->lti_big_buf;
3488 struct thandle *handle;
3490 struct lustre_handle lh = { 0 };
3494 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3497 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3498 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3503 rc = dt_attr_get(env, parent, la);
3507 la->la_mode = S_IFREG | 0666;
3508 la->la_atime = la->la_mtime = la->la_ctime = 0;
3509 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3510 LA_ATIME | LA_MTIME | LA_CTIME;
3511 memset(dof, 0, sizeof(*dof));
3512 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3513 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3514 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3515 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3516 * parent MDT-object's layout EA. */
3517 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3519 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3520 if (unlikely(rc == -ENODATA))
3525 rc = lfsck_lov2layout(tbuf->lb_buf, ff, comp_id);
3529 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3530 handle = lfsck_trans_create(env, dev, lfsck);
3532 GOTO(unlock1, rc = PTR_ERR(handle));
3534 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3538 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3539 LU_XATTR_CREATE, handle);
3543 rc = dt_trans_start_local(env, dev, handle);
3547 dt_read_lock(env, parent, 0);
3548 if (unlikely(lfsck_is_dead_obj(parent)))
3549 GOTO(unlock2, rc = 0);
3551 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3552 struct ost_id *oi = &info->lti_oi;
3553 struct lu_fid *tfid = &info->lti_fid2;
3554 struct lu_buf *lovea = &info->lti_big_buf;
3555 struct lov_mds_md_v1 *lmm;
3556 struct lov_ost_data_v1 *objs;
3561 rc = lfsck_layout_get_lovea(env, parent, lovea);
3562 if (unlikely(rc == -ENODATA))
3567 lmm = lovea->lb_buf;
3568 magic = le32_to_cpu(lmm->lmm_magic);
3569 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
3570 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3571 struct lov_comp_md_entry_v1 *lcme;
3572 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3575 for (i = 0; i < count; i++) {
3576 lcme = &lcm->lcm_entries[i];
3577 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3578 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3581 lmm = lovea->lb_buf +
3582 le32_to_cpu(lcme->lcme_offset);
3583 magic = le32_to_cpu(lmm->lmm_magic);
3588 /* Someone removed the component, do nothing. */
3589 GOTO(unlock2, rc = 0);
3593 count = le16_to_cpu(lmm->lmm_stripe_count);
3594 /* Someone changed the LOV EA, do nothing. */
3595 if (count <= ea_off)
3596 GOTO(unlock2, rc = 0);
3598 if (magic == LOV_MAGIC_V1) {
3599 objs = &lmm->lmm_objects[ea_off];
3601 LASSERT(magic == LOV_MAGIC_V3);
3603 objs = &((struct lov_mds_md_v3 *)lmm)->\
3604 lmm_objects[ea_off];
3607 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3608 idx2 = le32_to_cpu(objs->l_ost_idx);
3609 rc = ostid_to_fid(tfid, oi, idx2);
3610 /* Someone changed the LOV EA, do nothing. */
3611 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3615 rc = dt_create(env, child, la, NULL, dof, handle);
3619 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3625 dt_read_unlock(env, parent);
3628 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3631 lfsck_ibits_unlock(&lh, LCK_EX);
3635 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3636 "dangling reference for: parent "DFID", child "
3637 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3639 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3640 comp_id, ea_off, ost_idx,
3641 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3642 "Create the lost OST-object as required" :
3643 "Keep the MDT-object there by default", rc);
3649 * Repair the MDT-object with dangling LOV EA reference.
3651 * Prepare parameters and call __lfsck_layout_repair_dangling()
3652 * to repair the dangling LOV EA reference.
3654 * \param[in] env pointer to the thread context
3655 * \param[in] com the layout LFSCK component
3656 * \param[in] pfid the MDT-object's FID
3657 * \param[in] cfid the FID for the OST-object to be created
3658 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3659 * \param[in] ea_off the offset of the OST-object in the LOV EA
3660 * \param[in] ost_idx the index of OST on which the OST-object resides
3662 * \retval +1 for repair successfully
3663 * \retval 0 for did nothing
3664 * \retval negative error number on failure
3666 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3667 struct lfsck_component *com,
3668 const struct lu_fid *pfid,
3669 const struct lu_fid *cfid,
3670 __u32 comp_id, __u32 ea_off,
3673 struct lfsck_instance *lfsck = com->lc_lfsck;
3674 struct dt_object *parent = NULL;
3675 struct dt_object *child = NULL;
3676 struct lfsck_tgt_desc *ltd;
3680 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3682 GOTO(log, rc = PTR_ERR(parent));
3684 /* The MDT-object has been removed. */
3685 if (dt_object_exists(parent) == 0)
3688 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3689 if (unlikely(ltd == NULL))
3690 GOTO(log, rc = -ENODEV);
3692 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3694 GOTO(log, rc = PTR_ERR(child));
3696 /* The OST-object has been created. */
3697 if (unlikely(dt_object_exists(child) != 0))
3700 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3701 comp_id, ea_off, ost_idx, false);
3706 if (child != NULL && !IS_ERR(child))
3707 lfsck_object_put(env, child);
3709 if (parent != NULL && !IS_ERR(parent))
3710 lfsck_object_put(env, parent);
3713 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3714 "dangling reference for: parent "DFID", child "
3715 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3716 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3717 comp_id, ea_off, ost_idx,
3718 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3719 "Create the lost OST-object as required" :
3720 "Keep the MDT-object there by default", rc);
3725 /* If the OST-object does not recognize the MDT-object as its parent, and
3726 * there is no other MDT-object claims as its parent, then just trust the
3727 * given MDT-object as its parent. So update the OST-object filter_fid. */
3728 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3729 struct lfsck_component *com,
3730 struct dt_object *parent,
3731 struct lfsck_layout_req *llr,
3734 struct lfsck_thread_info *info = lfsck_env_info(env);
3735 struct filter_fid *ff = &info->lti_ff;
3736 struct dt_object *child = llr->llr_child;
3737 struct dt_device *dev = lfsck_obj2dev(child);
3738 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3739 struct lu_buf *tbuf = &info->lti_big_buf;
3740 struct thandle *handle;
3742 struct lustre_handle lh = { 0 };
3746 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3747 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3752 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3753 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3754 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3755 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3756 * parent MDT-object's layout EA. */
3757 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3759 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3760 if (unlikely(rc == -ENODATA))
3765 rc = lfsck_lov2layout(tbuf->lb_buf, ff, llr->llr_comp_id);
3769 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3771 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
3773 GOTO(unlock1, rc = PTR_ERR(handle));
3775 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3779 rc = dt_attr_get(env, parent, la);
3783 la->la_valid = LA_UID | LA_GID;
3784 rc = dt_declare_attr_set(env, child, la, handle);
3788 rc = dt_trans_start_local(env, dev, handle);
3792 dt_write_lock(env, parent, 0);
3793 if (unlikely(lfsck_is_dead_obj(parent)))
3794 GOTO(unlock2, rc = 1);
3796 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3800 /* Get the latest parent's owner. */
3801 rc = dt_attr_get(env, parent, la);
3805 la->la_valid = LA_UID | LA_GID;
3806 rc = dt_attr_set(env, child, la, handle);
3811 dt_write_unlock(env, parent);
3814 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3817 lfsck_ibits_unlock(&lh, LCK_EX);
3821 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3822 "unmatched MDT-OST pair for: parent "DFID
3823 ", child "DFID", comp_id %u, OST-index %u, "
3824 "stripe-index %u, owner %u/%u: rc = %d\n",
3825 lfsck_lfsck2name(com->lc_lfsck),
3826 PFID(lfsck_dto2fid(parent)),
3827 PFID(lfsck_dto2fid(child)),
3828 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3829 la->la_uid, la->la_gid, rc);
3834 /* If there are more than one MDT-objects claim as the OST-object's parent,
3835 * and the OST-object only recognizes one of them, then we need to generate
3836 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3837 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3838 struct lfsck_component *com,
3839 struct dt_object *parent,
3840 struct lfsck_layout_req *llr,
3843 struct lfsck_thread_info *info = lfsck_env_info(env);
3844 struct dt_allocation_hint *hint = &info->lti_hint;
3845 struct dt_object_format *dof = &info->lti_dof;
3846 struct ost_id *oi = &info->lti_oi;
3847 struct lu_buf *buf = &info->lti_big_buf;
3848 struct lfsck_instance *lfsck = com->lc_lfsck;
3849 struct dt_device *dev;
3850 struct lu_device *d =
3851 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3852 struct lu_object *o;
3853 struct lu_object *n;
3854 struct dt_object *child = NULL;
3855 struct thandle *handle = NULL;
3856 struct lov_mds_md_v1 *lmm;
3857 struct lov_ost_data_v1 *objs;
3858 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3860 struct lustre_handle lh = { 0 };
3866 /* We use two separated transactions to repair the inconsistency.
3868 * 1) create the child (OST-object).
3869 * 2) update the parent LOV EA according to the child's FID.
3871 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3872 * handled as orphan when the layout LFSCK run next time.
3874 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3875 * as dangling referened case when the layout LFSCK run next time. */
3877 /* The 1st transaction. */
3878 o = lu_object_anon(env, d, NULL);
3880 GOTO(log, rc = PTR_ERR(o));
3882 n = lu_object_locate(o->lo_header, d->ld_type);
3883 if (unlikely(n == NULL)) {
3884 lu_object_put_nocache(env, o);
3886 GOTO(log, rc = -EINVAL);
3889 child = container_of(n, struct dt_object, do_lu);
3890 memset(hint, 0, sizeof(*hint));
3891 rc = dt_attr_get(env, parent, la);
3895 la->la_valid = LA_UID | LA_GID;
3896 memset(dof, 0, sizeof(*dof));
3898 dev = lfsck_obj2dev(child);
3899 handle = lfsck_trans_create(env, dev, lfsck);
3901 GOTO(log, rc = PTR_ERR(handle));
3903 rc = dt_declare_create(env, child, la, hint, dof, handle);
3907 rc = dt_trans_start_local(env, dev, handle);
3911 rc = dt_create(env, child, la, hint, dof, handle);
3912 dt_trans_stop(env, dev, handle);
3917 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3918 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3923 /* The 2nd transaction. */
3925 /* XXX: Generally, we should use bottom device (OSD) to update parent
3926 * LOV EA. But because the LOD-object still references the wrong
3927 * OSP-object that should be detached after the parent's LOV EA
3928 * refreshed. Unfortunately, there is no suitable API for that.
3929 * So we have to make the LOD to re-load the OSP-object(s) via
3930 * replacing the LOV EA against the LOD-object.
3932 * Once the DNE2 patches have been landed, we can replace the
3933 * LOD device with the OSD device. LU-6230. */
3935 dev = lfsck->li_next;
3936 parent = lfsck_object_locate(dev, parent);
3938 GOTO(log, rc = PTR_ERR(parent));
3940 handle = lfsck_trans_create(env, dev, lfsck);
3942 GOTO(log, rc = PTR_ERR(handle));
3944 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3945 LU_XATTR_REPLACE, handle);
3949 rc = dt_trans_start_local(env, dev, handle);
3953 dt_write_lock(env, parent, 0);
3954 if (unlikely(lfsck_is_dead_obj(parent)))
3955 GOTO(unlock, rc = 0);
3957 rc = lfsck_layout_get_lovea(env, parent, buf);
3958 if (unlikely(rc == -ENODATA))
3964 magic = le32_to_cpu(lmm->lmm_magic);
3965 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
3966 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3967 struct lov_comp_md_entry_v1 *lcme;
3968 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3971 LASSERT(llr->llr_comp_id != 0);
3973 for (i = 0; i < count; i++) {
3974 lcme = &lcm->lcm_entries[i];
3975 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3976 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3979 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3981 le32_to_cpu(lcme->lcme_offset);
3982 magic = le32_to_cpu(lmm->lmm_magic);
3987 GOTO(unlock, rc = 0);
3991 if (magic == LOV_MAGIC_V1) {
3992 objs = &lmm->lmm_objects[llr->llr_lov_idx];
3994 LASSERT(magic == LOV_MAGIC_V3);
3996 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3999 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4000 index = le32_to_cpu(objs->l_ost_idx);
4001 rc = ostid_to_fid(&tfid, oi, index);
4002 /* Someone changed layout during the LFSCK, no need to repair then. */
4003 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
4004 GOTO(unlock, rc = 0);
4006 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
4007 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
4008 ostid_cpu_to_le(oi, &objs->l_ost_oi);
4009 objs->l_ost_gen = cpu_to_le32(0);
4010 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
4011 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
4012 LU_XATTR_REPLACE, handle);
4014 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
4017 dt_write_unlock(env, parent);
4021 dt_trans_stop(env, dev, handle);
4024 lfsck_ibits_unlock(&lh, LCK_EX);
4026 lfsck_object_put(env, child);
4029 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4030 "multiple references for: parent "DFID", comp_id %u, "
4031 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
4032 lfsck_lfsck2name(lfsck), PFID(pfid),
4033 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
4034 la->la_uid, la->la_gid, rc);
4039 /* If the MDT-object and the OST-object have different owner information,
4040 * then trust the MDT-object, because the normal chown/chgrp handle order
4041 * is from MDT to OST, and it is possible that some chown/chgrp operation
4042 * is partly done. */
4043 static int lfsck_layout_repair_owner(const struct lu_env *env,
4044 struct lfsck_component *com,
4045 struct dt_object *parent,
4046 struct lfsck_layout_req *llr,
4047 struct lu_attr *pla,
4048 const struct lu_attr *cla)
4050 struct lfsck_thread_info *info = lfsck_env_info(env);
4051 struct lu_attr *tla = &info->lti_la2;
4052 struct dt_object *child = llr->llr_child;
4053 struct dt_device *dev = lfsck_obj2dev(child);
4054 struct thandle *handle;
4056 dt_obj_version_t version;
4059 tla->la_uid = pla->la_uid;
4060 tla->la_gid = pla->la_gid;
4061 tla->la_valid = LA_UID | LA_GID;
4062 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
4064 GOTO(log, rc = PTR_ERR(handle));
4066 rc = dt_declare_attr_set(env, child, tla, handle);
4070 rc = dt_trans_start_local(env, dev, handle);
4074 /* Use the dt_object lock to serialize with destroy and attr_set. */
4075 dt_read_lock(env, parent, 0);
4076 if (unlikely(lfsck_is_dead_obj(parent)))
4077 GOTO(unlock, rc = 1);
4079 version = dt_version_get(env, child);
4080 if (version == -EOPNOTSUPP)
4083 /* Get the latest parent's owner. */
4084 rc = dt_attr_get(env, parent, pla);
4088 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
4089 if (unlikely((!version && tla->la_ctime == 0) ||
4090 tla->la_uid != pla->la_uid || tla->la_gid != pla->la_gid))
4093 rc = dt_attr_set(env, child, tla, handle);
4098 dt_read_unlock(env, parent);
4101 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
4105 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4106 "inconsistent file owner for: parent "DFID", child "DFID
4107 ", OST-index %u, stripe-index %u, old owner %u/%u, "
4108 "new owner %u/%u: rc = %d\n",
4109 lfsck_lfsck2name(com->lc_lfsck),
4110 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4111 llr->llr_ost_idx, llr->llr_lov_idx,
4112 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
4117 #define CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid, msg) \
4118 CDEBUG(D_LFSCK, "%s:("DFID"|"DFID")/"DFID":XATTR %s: %s\n", \
4119 lfsck_lfsck2name(lfsck), PFID(&lso->lso_fid), PFID(pfid), \
4120 PFID(cfid), XATTR_NAME_FID, msg);
4122 /* Check whether the OST-object correctly back points to the
4123 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
4124 static int lfsck_layout_check_parent(const struct lu_env *env,
4125 struct lfsck_component *com,
4126 struct lfsck_assistant_object *lso,
4127 struct filter_fid *ff,
4128 const struct lu_fid *cfid,
4129 const struct lu_attr *cla,
4130 struct lfsck_layout_req *llr)
4132 struct lfsck_thread_info *info = lfsck_env_info(env);
4133 struct lu_buf *buf = &info->lti_big_buf;
4134 struct lu_fid *pfid = &info->lti_fid;
4135 struct dt_object *tobj;
4136 struct lov_mds_md_v1 *lmm;
4137 struct lov_ost_data_v1 *objs;
4138 struct lustre_handle lh = { 0 };
4139 struct lfsck_instance *lfsck = com->lc_lfsck;
4147 *pfid = ff->ff_parent;
4148 idx = pfid->f_stripe_idx;
4151 if (unlikely(!fid_is_sane(pfid))) {
4152 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4153 "the parent FID is invalid");
4155 RETURN(LLIT_UNMATCHED_PAIR);
4158 if (lu_fid_eq(pfid, &lso->lso_fid)) {
4159 if (likely(llr->llr_lov_idx == idx))
4162 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4163 "the stripe index is unmatched");
4165 RETURN(LLIT_UNMATCHED_PAIR);
4168 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4170 RETURN(PTR_ERR(tobj));
4172 if (dt_object_exists(tobj) == 0) {
4173 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4174 "the parent is nonexistent");
4176 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4179 if (lfsck_is_dead_obj(tobj)) {
4180 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4181 "the parent is dead object");
4183 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4186 if (!S_ISREG(lfsck_object_type(tobj))) {
4187 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4188 "the parent is not a regular file");
4190 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4193 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
4194 * remote one on another MDT. Then check whether the given OST-object
4195 * is in such layout. If yes, it is multiple referenced, otherwise it
4196 * is unmatched referenced case. */
4197 rc = lfsck_layout_get_lovea(env, tobj, buf);
4198 if (rc == 0 || rc == -ENODATA || rc == -ENOENT) {
4199 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4200 "the parent has no stripe data");
4202 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4205 if (unlikely(rc == -EOPNOTSUPP))
4206 GOTO(out, rc = LLIT_NONE);
4212 magic = le32_to_cpu(lmm->lmm_magic);
4213 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
4214 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4215 struct lov_comp_md_entry_v1 *lcme;
4217 if (ff->ff_layout.ol_comp_id == 0) {
4218 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4219 "the parent has incorrect comp_id");
4221 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4224 count = le16_to_cpu(lcm->lcm_entry_count);
4225 for (i = 0; i < count; i++) {
4226 lcme = &lcm->lcm_entries[i];
4227 if (le32_to_cpu(lcme->lcme_id) ==
4228 ff->ff_layout.ol_comp_id) {
4230 le32_to_cpu(lcme->lcme_offset);
4231 magic = le32_to_cpu(lmm->lmm_magic);
4232 if (!(le32_to_cpu(lcme->lcme_flags) &
4234 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid,
4236 "the parent has uninitialized component");
4238 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4245 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4246 "the parent has no matched comp_id");
4248 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4252 if (magic == LOV_MAGIC_V1) {
4253 objs = &lmm->lmm_objects[0];
4255 LASSERT(magic == LOV_MAGIC_V3);
4256 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4259 count = le16_to_cpu(lmm->lmm_stripe_count);
4260 for (i = 0; i < count; i++, objs++) {
4261 struct lu_fid *tfid = &info->lti_fid2;
4262 struct ost_id *oi = &info->lti_oi;
4265 if (lovea_slot_is_dummy(objs))
4268 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4269 idx2 = le32_to_cpu(objs->l_ost_idx);
4270 rc = ostid_to_fid(tfid, oi, idx2);
4272 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4273 "invalid layout EA at the slot %d, index %u\n",
4274 lfsck_lfsck2name(com->lc_lfsck),
4275 PFID(pfid), i, idx2);
4277 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4280 if (lu_fid_eq(cfid, tfid)) {
4281 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4282 MDS_INODELOCK_UPDATE |
4283 MDS_INODELOCK_LAYOUT |
4284 MDS_INODELOCK_XATTR,
4289 dt_read_lock(env, tobj, 0);
4291 /* For local MDT-object, re-check existence
4292 * after taken the lock. */
4293 if (!dt_object_remote(tobj)) {
4294 if (dt_object_exists(tobj) == 0 ||
4295 lfsck_is_dead_obj(tobj)) {
4296 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid,
4298 "the parent doesn't exist anymore after lock");
4300 rc = LLIT_UNMATCHED_PAIR;
4302 rc = LLIT_MULTIPLE_REFERENCED;
4308 /* For migration case, the new MDT-object and old
4309 * MDT-object may reference the same OST-object at
4310 * some migration internal time.
4312 * For remote MDT-object, the local MDT may not know
4313 * whether it has been removed or not. Try checking
4314 * for a non-existent xattr to check if this object
4315 * has been been removed or not. */
4316 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4318 if (unlikely(rc == -ENOENT || rc >= 0)) {
4319 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4320 "the parent is remote object and nonexistent after lock");
4322 rc = LLIT_UNMATCHED_PAIR;
4323 } else if (rc == -ENODATA) {
4324 rc = LLIT_MULTIPLE_REFERENCED;
4331 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4332 "the parent has no matched stripe");
4334 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4337 if (lustre_handle_is_used(&lh)) {
4338 dt_read_unlock(env, tobj);
4339 lfsck_ibits_unlock(&lh, LCK_EX);
4343 lfsck_object_put(env, tobj);
4348 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4349 struct lfsck_component *com,
4350 struct lfsck_assistant_req *lar)
4352 struct lfsck_layout_req *llr =
4353 container_of(lar, struct lfsck_layout_req, llr_lar);
4354 struct lfsck_assistant_object *lso = lar->lar_parent;
4355 struct lfsck_layout *lo = com->lc_file_ram;
4356 struct lfsck_thread_info *info = lfsck_env_info(env);
4357 struct filter_fid *ff = &info->lti_ff;
4358 struct lu_buf buf = { .lb_buf = ff,
4359 .lb_len = sizeof(*ff) };
4360 struct dt_object *parent = NULL;
4361 struct dt_object *child = llr->llr_child;
4362 struct lu_attr *pla = &lso->lso_attr;
4363 struct lu_attr *cla = &info->lti_la;
4364 struct lfsck_instance *lfsck = com->lc_lfsck;
4365 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4366 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4373 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
4375 rc = dt_attr_get(env, child, cla);
4376 if (rc == -ENOENT) {
4377 parent = lfsck_assistant_object_load(env, lfsck, lso);
4378 if (IS_ERR(parent)) {
4379 rc = PTR_ERR(parent);
4381 RETURN(rc == -ENOENT ? 0 : rc);
4384 type = LLIT_DANGLING;
4391 lfsck_buf_init(&buf, ff, sizeof(*ff));
4392 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4393 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4394 CDEBUG(D_LFSCK, "%s:"DFID"/"DFID": "
4395 "the child object's %s is corrupted\n",
4396 lfsck_lfsck2name(lfsck), PFID(&lso->lso_fid),
4397 PFID(lu_object_fid(&child->do_lu)),
4400 type = LLIT_UNMATCHED_PAIR;
4404 if (rc < 0 && rc != -ENODATA)
4407 if (rc == 0 || rc == -ENODATA)
4408 GOTO(check_owner, rc = 0);
4410 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4411 rc = lfsck_layout_check_parent(env, com, lso, ff,
4412 lu_object_fid(&child->do_lu), cla, llr);
4422 /* Someone may has changed the owner after the parent attr pre-loaded.
4423 * It can be handled later inside the lfsck_layout_repair_owner(). */
4424 if (unlikely(cla->la_uid != pla->la_uid ||
4425 cla->la_gid != pla->la_gid)) {
4426 type = LLIT_INCONSISTENT_OWNER;
4431 if (type == LLIT_NONE)
4434 if (bk->lb_param & LPF_DRYRUN)
4437 if (parent == NULL) {
4438 parent = lfsck_assistant_object_load(env, lfsck, lso);
4439 if (IS_ERR(parent)) {
4440 rc = PTR_ERR(parent);
4451 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4452 rc = lfsck_layout_ins_dangling_rec(env, com,
4453 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4454 llr->llr_comp_id, llr->llr_lov_idx,
4457 rc = __lfsck_layout_repair_dangling(env, com, parent,
4464 case LLIT_UNMATCHED_PAIR:
4465 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4468 case LLIT_MULTIPLE_REFERENCED:
4469 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4472 case LLIT_INCONSISTENT_OWNER:
4473 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4483 down_write(&com->lc_sem);
4485 struct lfsck_assistant_data *lad = com->lc_data;
4487 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags))) {
4489 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4490 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4491 rc == -EHOSTUNREACH) {
4492 /* If cannot touch the target server,
4493 * mark the LFSCK as INCOMPLETE. */
4494 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4495 "talk with OST %x: rc = %d\n",
4496 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4497 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4498 lo->ll_objs_skipped++;
4501 lfsck_layout_record_failure(env, lfsck, lo);
4503 } else if (rc > 0 && (type != LLIT_DANGLING ||
4504 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4505 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4506 "unknown type = %d\n", type);
4508 lo->ll_objs_repaired[type - 1]++;
4509 if (bk->lb_param & LPF_DRYRUN &&
4510 unlikely(lo->ll_pos_first_inconsistent == 0))
4511 lo->ll_pos_first_inconsistent =
4512 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4515 up_write(&com->lc_sem);
4517 if (parent != NULL && !IS_ERR(parent))
4518 lfsck_object_put(env, parent);
4524 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4525 struct lfsck_component *com,
4526 struct dt_object *obj, bool first)
4528 struct lfsck_instance *lfsck = com->lc_lfsck;
4529 struct ptlrpc_thread *thread = &lfsck->li_thread;
4530 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4531 struct lfsck_layout *lo = com->lc_file_ram;
4532 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4535 struct lfsck_layout_dangling_key *parent =
4536 &lfsck_env_info(env)->lti_lldk;
4537 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4542 di = iops->init(env, obj, 0);
4544 RETURN(PTR_ERR(di));
4547 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4549 memset(parent, 0, sizeof(*parent));
4550 rc = iops->get(env, di, (const struct dt_key *)parent);
4555 /* The start one either has been processed or does not exist,
4557 rc = iops->next(env, di);
4563 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4564 unlikely(!thread_is_running(thread)))
4567 key = iops->key(env, di);
4576 lldk_be_to_cpu(parent,
4577 (const struct lfsck_layout_dangling_key *)key);
4578 if (!fid_is_sane(&parent->lldk_fid)) {
4583 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4585 fid_be_to_cpu(cfid, cfid);
4586 ost_idx = cfid->f_ver;
4588 if (!fid_is_sane(cfid)) {
4593 rc = lfsck_layout_repair_dangling(env, com,
4594 &parent->lldk_fid, cfid,
4595 parent->lldk_comp_id,
4596 parent->lldk_ea_off, ost_idx);
4600 down_write(&com->lc_sem);
4601 com->lc_new_checked++;
4602 com->lc_new_scanned++;
4604 lo->ll_lldk_latest_scanned_phase2 = *parent;
4607 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4609 lo->ll_objs_failed_phase2++;
4610 up_write(&com->lc_sem);
4612 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4615 if (unlikely(com->lc_time_next_checkpoint <=
4616 ktime_get_seconds()) &&
4617 com->lc_new_checked != 0) {
4618 down_write(&com->lc_sem);
4619 lo->ll_run_time_phase2 += ktime_get_seconds() -
4620 com->lc_time_last_checkpoint;
4621 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
4622 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4623 com->lc_new_checked = 0;
4624 lfsck_layout_store(env, com);
4625 up_write(&com->lc_sem);
4627 com->lc_time_last_checkpoint = ktime_get_seconds();
4628 com->lc_time_next_checkpoint =
4629 com->lc_time_last_checkpoint +
4630 LFSCK_CHECKPOINT_INTERVAL;
4633 lfsck_control_speed_by_self(com);
4634 if (unlikely(!thread_is_running(thread)))
4637 rc = iops->next(env, di);
4646 iops->fini(env, di);
4651 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4652 struct lfsck_component *com)
4654 struct lfsck_assistant_data *lad = com->lc_data;
4655 struct lfsck_instance *lfsck = com->lc_lfsck;
4656 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4657 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4658 struct lfsck_tgt_desc *ltd;
4662 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4663 lfsck_lfsck2name(lfsck));
4665 spin_lock(<ds->ltd_lock);
4666 while (!list_empty(&lad->lad_ost_phase2_list)) {
4667 ltd = list_entry(lad->lad_ost_phase2_list.next,
4668 struct lfsck_tgt_desc,
4669 ltd_layout_phase_list);
4670 list_del_init(<d->ltd_layout_phase_list);
4671 if (bk->lb_param & LPF_OST_ORPHAN) {
4672 spin_unlock(<ds->ltd_lock);
4673 rc = lfsck_layout_scan_orphan(env, com, ltd);
4674 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4677 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags) ||
4678 !thread_is_running(&lfsck->li_thread)))
4680 spin_lock(<ds->ltd_lock);
4684 if (list_empty(&lad->lad_ost_phase1_list))
4688 spin_unlock(<ds->ltd_lock);
4690 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4691 struct lfsck_layout *lo = com->lc_file_ram;
4694 com->lc_new_checked = 0;
4695 com->lc_new_scanned = 0;
4696 com->lc_time_last_checkpoint = ktime_get_seconds();
4697 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4698 LFSCK_CHECKPOINT_INTERVAL;
4700 i = lfsck_sub_trace_file_fid2idx(
4701 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4702 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4703 com->lc_sub_trace_objs[i].lsto_obj, true);
4704 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4705 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4706 com->lc_sub_trace_objs[i].lsto_obj, false);
4708 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4709 "at the No. %d trace file: rc = %d\n",
4710 lfsck_lfsck2name(lfsck), i, rc);
4713 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4714 lfsck_lfsck2name(lfsck), rc);
4720 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4721 struct ptlrpc_request *req,
4724 struct lfsck_layout_slave_async_args *llsaa = args;
4725 struct obd_export *exp = llsaa->llsaa_exp;
4726 struct lfsck_component *com = llsaa->llsaa_com;
4727 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4728 struct lfsck_layout_slave_data *llsd = com->lc_data;
4729 struct lfsck_reply *lr = NULL;
4733 /* It is probably caused by network trouble, or target crash,
4734 * it will try several times (depends on the obd_timeout, and
4735 * will not less than 3 times). But to make the LFSCK can go
4736 * ahead, we should not try for ever. After some try but still
4737 * hit failure, it will assume that the target exit the LFSCK
4738 * prcoessing and stop try. */
4739 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4740 int max_try = max_t(int, obd_timeout / 30, 3);
4742 if (++(llst->llst_failures) > max_try)
4748 llst->llst_failures = 0;
4749 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4750 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4751 lr->lr_status != LS_SCANNING_PHASE2)
4756 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4757 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4758 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4759 llst->llst_failures);
4761 lfsck_layout_llst_del(llsd, llst);
4764 lfsck_layout_llst_put(llst);
4765 lfsck_component_put(env, com);
4766 class_export_put(exp);
4771 static int lfsck_layout_async_query(const struct lu_env *env,
4772 struct lfsck_component *com,
4773 struct obd_export *exp,
4774 struct lfsck_layout_slave_target *llst,
4775 struct lfsck_request *lr,
4776 struct ptlrpc_request_set *set)
4778 struct lfsck_layout_slave_async_args *llsaa;
4779 struct ptlrpc_request *req;
4780 struct lfsck_request *tmp;
4784 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4788 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4790 ptlrpc_request_free(req);
4794 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4796 ptlrpc_request_set_replen(req);
4798 llsaa = ptlrpc_req_async_args(llsaa, req);
4799 llsaa->llsaa_exp = exp;
4800 llsaa->llsaa_com = lfsck_component_get(com);
4801 llsaa->llsaa_llst = llst;
4802 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4803 req->rq_allow_intr = 1;
4804 req->rq_no_delay = 1;
4805 ptlrpc_set_add_req(set, req);
4810 static int lfsck_layout_async_notify(const struct lu_env *env,
4811 struct obd_export *exp,
4812 struct lfsck_request *lr,
4813 struct ptlrpc_request_set *set)
4815 struct ptlrpc_request *req;
4816 struct lfsck_request *tmp;
4820 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4824 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4826 ptlrpc_request_free(req);
4830 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4832 ptlrpc_request_set_replen(req);
4833 req->rq_allow_intr = 1;
4834 req->rq_no_delay = 1;
4835 ptlrpc_set_add_req(set, req);
4841 lfsck_layout_slave_query_master(const struct lu_env *env,
4842 struct lfsck_component *com)
4844 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4845 struct lfsck_instance *lfsck = com->lc_lfsck;
4846 struct lfsck_layout_slave_data *llsd = com->lc_data;
4847 struct lfsck_layout_slave_target *llst;
4848 struct obd_export *exp;
4849 struct ptlrpc_request_set *set;
4854 set = ptlrpc_prep_set();
4856 GOTO(log, rc = -ENOMEM);
4858 memset(lr, 0, sizeof(*lr));
4859 lr->lr_event = LE_QUERY;
4860 lr->lr_active = LFSCK_TYPE_LAYOUT;
4862 llsd->llsd_touch_gen++;
4863 spin_lock(&llsd->llsd_lock);
4864 while (!list_empty(&llsd->llsd_master_list)) {
4865 llst = list_entry(llsd->llsd_master_list.next,
4866 struct lfsck_layout_slave_target,
4868 if (llst->llst_gen == llsd->llsd_touch_gen)
4871 llst->llst_gen = llsd->llsd_touch_gen;
4872 list_move_tail(&llst->llst_list,
4873 &llsd->llsd_master_list);
4874 atomic_inc(&llst->llst_ref);
4875 spin_unlock(&llsd->llsd_lock);
4877 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4880 lfsck_layout_llst_del(llsd, llst);
4881 lfsck_layout_llst_put(llst);
4882 spin_lock(&llsd->llsd_lock);
4886 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4888 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4889 "query %s for layout: rc = %d\n",
4890 lfsck_lfsck2name(lfsck),
4891 exp->exp_obd->obd_name, rc);
4894 lfsck_layout_llst_put(llst);
4895 class_export_put(exp);
4897 spin_lock(&llsd->llsd_lock);
4899 spin_unlock(&llsd->llsd_lock);
4901 rc = ptlrpc_set_wait(env, set);
4902 ptlrpc_set_destroy(set);
4904 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
4907 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
4908 lfsck_lfsck2name(com->lc_lfsck), rc);
4914 lfsck_layout_slave_notify_master(const struct lu_env *env,
4915 struct lfsck_component *com,
4916 enum lfsck_events event, int result)
4918 struct lfsck_layout *lo = com->lc_file_ram;
4919 struct lfsck_instance *lfsck = com->lc_lfsck;
4920 struct lfsck_layout_slave_data *llsd = com->lc_data;
4921 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4922 struct lfsck_layout_slave_target *llst;
4923 struct obd_export *exp;
4924 struct ptlrpc_request_set *set;
4928 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
4929 lfsck_lfsck2name(com->lc_lfsck));
4931 set = ptlrpc_prep_set();
4935 memset(lr, 0, sizeof(*lr));
4936 lr->lr_event = event;
4937 lr->lr_flags = LEF_FROM_OST;
4938 lr->lr_status = result;
4939 lr->lr_index = lfsck_dev_idx(lfsck);
4940 lr->lr_active = LFSCK_TYPE_LAYOUT;
4941 lr->lr_flags2 = lo->ll_flags;
4942 llsd->llsd_touch_gen++;
4943 spin_lock(&llsd->llsd_lock);
4944 while (!list_empty(&llsd->llsd_master_list)) {
4945 llst = list_entry(llsd->llsd_master_list.next,
4946 struct lfsck_layout_slave_target,
4948 if (llst->llst_gen == llsd->llsd_touch_gen)
4951 llst->llst_gen = llsd->llsd_touch_gen;
4952 list_move_tail(&llst->llst_list,
4953 &llsd->llsd_master_list);
4954 atomic_inc(&llst->llst_ref);
4955 spin_unlock(&llsd->llsd_lock);
4957 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4960 lfsck_layout_llst_del(llsd, llst);
4961 lfsck_layout_llst_put(llst);
4962 spin_lock(&llsd->llsd_lock);
4966 rc = lfsck_layout_async_notify(env, exp, lr, set);
4968 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4969 "notify %s for layout: rc = %d\n",
4970 lfsck_lfsck2name(lfsck),
4971 exp->exp_obd->obd_name, rc);
4973 lfsck_layout_llst_put(llst);
4974 class_export_put(exp);
4975 spin_lock(&llsd->llsd_lock);
4977 spin_unlock(&llsd->llsd_lock);
4979 ptlrpc_set_wait(env, set);
4980 ptlrpc_set_destroy(set);
4986 * \ret -ENODATA: unrecognized stripe
4987 * \ret = 0 : recognized stripe
4988 * \ret < 0 : other failures
4990 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
4991 struct lfsck_component *com,
4992 struct lu_fid *cfid,
4993 struct lu_fid *pfid, __u32 comp_id)
4995 struct lfsck_thread_info *info = lfsck_env_info(env);
4996 struct lu_buf *buf = &info->lti_big_buf;
4997 struct ost_id *oi = &info->lti_oi;
4998 struct dt_object *obj;
4999 struct lov_mds_md_v1 *lmm;
5000 struct lov_ost_data_v1 *objs;
5001 __u32 idx = pfid->f_stripe_idx;
5009 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
5011 RETURN(PTR_ERR(obj));
5013 dt_read_lock(env, obj, 0);
5014 if (unlikely(dt_object_exists(obj) == 0 ||
5015 lfsck_is_dead_obj(obj)))
5016 GOTO(unlock, rc = -ENOENT);
5018 if (!S_ISREG(lfsck_object_type(obj)))
5019 GOTO(unlock, rc = -ENODATA);
5021 rc = lfsck_layout_get_lovea(env, obj, buf);
5026 magic = le32_to_cpu(lmm->lmm_magic);
5027 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5028 struct lov_comp_md_v1 *lcm = buf->lb_buf;
5029 struct lov_comp_md_entry_v1 *lcme;
5032 GOTO(unlock, rc = -ENODATA);
5034 count = le16_to_cpu(lcm->lcm_entry_count);
5035 for (i = 0; i < count; i++) {
5036 lcme = &lcm->lcm_entries[i];
5037 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
5039 le32_to_cpu(lcme->lcme_offset);
5040 magic = le32_to_cpu(lmm->lmm_magic);
5041 if (!(le32_to_cpu(lcme->lcme_flags) &
5043 GOTO(unlock, rc = -ENODATA);
5049 GOTO(unlock, rc = -ENODATA);
5053 if (magic == LOV_MAGIC_V1) {
5054 objs = &lmm->lmm_objects[0];
5056 LASSERT(magic == LOV_MAGIC_V3);
5057 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5060 fid_to_ostid(cfid, oi);
5061 count = le16_to_cpu(lmm->lmm_stripe_count);
5062 for (i = 0; i < count; i++, objs++) {
5065 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
5066 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
5067 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
5070 GOTO(unlock, rc = -ENODATA);
5073 dt_read_unlock(env, obj);
5074 lfsck_object_put(env, obj);
5080 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
5081 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
5082 * layout EA from MDT to OST. On one hand, the OST no need to understand
5083 * the layout EA structure; on the other hand, it may cause trouble when
5084 * transfer large layout EA from MDT to OST via normal OUT RPC.
5086 * \ret > 0: unrecognized stripe
5087 * \ret = 0: recognized stripe
5088 * \ret < 0: other failures
5090 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
5091 struct lfsck_component *com,
5092 struct lu_fid *cfid,
5093 struct lu_fid *pfid, __u32 comp_id)
5095 struct lfsck_instance *lfsck = com->lc_lfsck;
5096 struct obd_device *obd = lfsck->li_obd;
5097 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5098 struct obd_export *exp = NULL;
5099 struct ptlrpc_request *req = NULL;
5100 struct lfsck_request *lr;
5101 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
5105 if (unlikely(fid_is_idif(pfid)))
5108 fld_range_set_any(range);
5109 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
5111 RETURN(rc == -ENOENT ? 1 : rc);
5113 if (unlikely(!fld_range_is_mdt(range)))
5116 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
5117 if (unlikely(exp == NULL))
5120 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
5121 GOTO(out, rc = -EOPNOTSUPP);
5123 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
5125 GOTO(out, rc = -ENOMEM);
5127 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
5129 ptlrpc_request_free(req);
5134 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
5135 memset(lr, 0, sizeof(*lr));
5136 lr->lr_event = LE_PAIRS_VERIFY;
5137 lr->lr_active = LFSCK_TYPE_LAYOUT;
5138 lr->lr_fid = *cfid; /* OST-object itself FID. */
5139 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
5140 lr->lr_comp_id = comp_id;
5142 ptlrpc_request_set_replen(req);
5143 rc = ptlrpc_queue_wait(req);
5144 ptlrpc_req_finished(req);
5146 if (rc == -ENOENT || rc == -ENODATA)
5153 class_export_put(exp);
5158 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
5159 struct lfsck_component *com,
5160 struct lfsck_req_local *lrl)
5162 struct dt_object *obj;
5166 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
5168 GOTO(log, rc = PTR_ERR(obj));
5170 dt_write_lock(env, obj, 0);
5171 if (unlikely(dt_object_exists(obj) == 0 ||
5172 lfsck_is_dead_obj(obj)))
5173 GOTO(unlock, rc = 0);
5175 rc = __lfsck_layout_update_pfid(env, com, obj,
5176 &lrl->lrl_ff_client.ff_parent,
5177 &lrl->lrl_ff_client.ff_layout,
5178 lrl->lrl_ff_client.ff_layout_version,
5179 lrl->lrl_ff_client.ff_range,
5180 lrl->lrl_ff_client.ff_parent.f_ver);
5185 dt_write_unlock(env, obj);
5186 lfsck_object_put(env, obj);
5189 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
5190 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
5191 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
5198 static void lfsck_layout_slave_quit(const struct lu_env *env,
5199 struct lfsck_component *com);
5201 static int lfsck_layout_reset(const struct lu_env *env,
5202 struct lfsck_component *com, bool init)
5204 struct lfsck_layout *lo = com->lc_file_ram;
5207 down_write(&com->lc_sem);
5209 memset(lo, 0, com->lc_file_size);
5211 __u32 count = lo->ll_success_count;
5212 time64_t last_time = lo->ll_time_last_complete;
5214 memset(lo, 0, com->lc_file_size);
5215 lo->ll_success_count = count;
5216 lo->ll_time_last_complete = last_time;
5219 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
5220 lo->ll_status = LS_INIT;
5222 if (com->lc_lfsck->li_master) {
5223 struct lfsck_assistant_data *lad = com->lc_data;
5225 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
5226 CFS_RESET_BITMAP(lad->lad_bitmap);
5229 rc = lfsck_layout_store(env, com);
5230 if (rc == 0 && com->lc_lfsck->li_master)
5231 rc = lfsck_load_sub_trace_files(env, com,
5232 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
5233 up_write(&com->lc_sem);
5235 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
5236 lfsck_lfsck2name(com->lc_lfsck), rc);
5241 static void lfsck_layout_fail(const struct lu_env *env,
5242 struct lfsck_component *com, bool new_checked)
5244 struct lfsck_layout *lo = com->lc_file_ram;
5246 down_write(&com->lc_sem);
5248 com->lc_new_checked++;
5249 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
5250 up_write(&com->lc_sem);
5253 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
5254 struct lfsck_component *com, bool init)
5256 struct lfsck_instance *lfsck = com->lc_lfsck;
5257 struct lfsck_layout *lo = com->lc_file_ram;
5261 rc = lfsck_checkpoint_generic(env, com);
5263 return rc > 0 ? 0 : rc;
5266 down_write(&com->lc_sem);
5268 lo->ll_pos_latest_start =
5269 lfsck->li_pos_checkpoint.lp_oit_cookie;
5271 lo->ll_pos_last_checkpoint =
5272 lfsck->li_pos_checkpoint.lp_oit_cookie;
5273 lo->ll_run_time_phase1 += ktime_get_seconds() -
5274 lfsck->li_time_last_checkpoint;
5275 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5276 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5277 com->lc_new_checked = 0;
5280 rc = lfsck_layout_store(env, com);
5281 up_write(&com->lc_sem);
5283 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5284 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5285 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5290 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5291 struct lfsck_component *com, bool init)
5293 struct lfsck_instance *lfsck = com->lc_lfsck;
5294 struct lfsck_layout *lo = com->lc_file_ram;
5297 if (com->lc_new_checked == 0 && !init)
5300 down_write(&com->lc_sem);
5302 lo->ll_pos_latest_start =
5303 lfsck->li_pos_checkpoint.lp_oit_cookie;
5305 lo->ll_pos_last_checkpoint =
5306 lfsck->li_pos_checkpoint.lp_oit_cookie;
5307 lo->ll_run_time_phase1 += ktime_get_seconds() -
5308 lfsck->li_time_last_checkpoint;
5309 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5310 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5311 com->lc_new_checked = 0;
5314 rc = lfsck_layout_store(env, com);
5315 up_write(&com->lc_sem);
5317 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5318 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5319 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5324 static int lfsck_layout_prep(const struct lu_env *env,
5325 struct lfsck_component *com,
5326 struct lfsck_start *start)
5328 struct lfsck_instance *lfsck = com->lc_lfsck;
5329 struct lfsck_layout *lo = com->lc_file_ram;
5330 struct lfsck_position *pos = &com->lc_pos_start;
5332 fid_zero(&pos->lp_dir_parent);
5333 pos->lp_dir_cookie = 0;
5334 if (lo->ll_status == LS_COMPLETED ||
5335 lo->ll_status == LS_PARTIAL ||
5336 /* To handle orphan, must scan from the beginning. */
5337 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5340 rc = lfsck_layout_reset(env, com, false);
5342 rc = lfsck_set_param(env, lfsck, start, true);
5345 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5346 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5352 down_write(&com->lc_sem);
5353 lo->ll_time_latest_start = ktime_get_real_seconds();
5354 spin_lock(&lfsck->li_lock);
5355 if (lo->ll_flags & LF_SCANNED_ONCE) {
5356 if (!lfsck->li_drop_dryrun ||
5357 lo->ll_pos_first_inconsistent == 0) {
5358 lo->ll_status = LS_SCANNING_PHASE2;
5359 list_move_tail(&com->lc_link,
5360 &lfsck->li_list_double_scan);
5361 pos->lp_oit_cookie = 0;
5365 lo->ll_status = LS_SCANNING_PHASE1;
5366 lo->ll_run_time_phase1 = 0;
5367 lo->ll_run_time_phase2 = 0;
5368 lo->ll_objs_checked_phase1 = 0;
5369 lo->ll_objs_checked_phase2 = 0;
5370 lo->ll_objs_failed_phase1 = 0;
5371 lo->ll_objs_failed_phase2 = 0;
5372 for (i = 0; i < LLIT_MAX; i++)
5373 lo->ll_objs_repaired[i] = 0;
5375 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5376 fid_zero(&com->lc_fid_latest_scanned_phase2);
5379 lo->ll_status = LS_SCANNING_PHASE1;
5380 if (!lfsck->li_drop_dryrun ||
5381 lo->ll_pos_first_inconsistent == 0)
5382 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5384 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5386 spin_unlock(&lfsck->li_lock);
5387 up_write(&com->lc_sem);
5392 static int lfsck_layout_slave_prep(const struct lu_env *env,
5393 struct lfsck_component *com,
5394 struct lfsck_start_param *lsp)
5396 struct lfsck_layout_slave_data *llsd = com->lc_data;
5397 struct lfsck_instance *lfsck = com->lc_lfsck;
5398 struct lfsck_layout *lo = com->lc_file_ram;
5399 struct lfsck_start *start = lsp->lsp_start;
5402 rc = lfsck_layout_prep(env, com, start);
5406 if (lo->ll_flags & LF_CRASHED_LASTID &&
5407 list_empty(&llsd->llsd_master_list)) {
5408 LASSERT(lfsck->li_out_notify != NULL);
5410 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5411 LE_LASTID_REBUILDING);
5414 if (!lsp->lsp_index_valid)
5417 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5418 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5419 LASSERT(!llsd->llsd_rbtree_valid);
5421 down_write(&llsd->llsd_rb_rwsem);
5422 rc = lfsck_rbtree_setup(env, com);
5423 up_write(&llsd->llsd_rb_rwsem);
5426 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5427 "%llu]\n", lfsck_lfsck2name(lfsck),
5428 com->lc_pos_start.lp_oit_cookie);
5433 static int lfsck_layout_master_prep(const struct lu_env *env,
5434 struct lfsck_component *com,
5435 struct lfsck_start_param *lsp)
5440 rc = lfsck_layout_load_bitmap(env, com);
5442 rc = lfsck_layout_reset(env, com, false);
5444 rc = lfsck_set_param(env, com->lc_lfsck,
5445 lsp->lsp_start, true);
5451 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5455 rc = lfsck_start_assistant(env, com, lsp);
5460 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5461 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5462 com->lc_pos_start.lp_oit_cookie);
5467 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5468 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5469 struct lfsck_component *com,
5470 struct dt_object *parent,
5471 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5473 struct lfsck_thread_info *info = lfsck_env_info(env);
5474 struct lfsck_instance *lfsck = com->lc_lfsck;
5475 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5476 struct lfsck_layout *lo = com->lc_file_ram;
5477 struct lfsck_assistant_data *lad = com->lc_data;
5478 struct lfsck_assistant_object *lso = NULL;
5479 struct lov_ost_data_v1 *objs;
5480 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5481 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5482 struct ptlrpc_thread *athread = &lad->lad_thread;
5490 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5491 magic = le32_to_cpu(lmm->lmm_magic);
5492 if (magic == LOV_MAGIC_V1) {
5493 objs = &lmm->lmm_objects[0];
5495 LASSERT(magic == LOV_MAGIC_V3);
5496 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5499 count = le16_to_cpu(lmm->lmm_stripe_count);
5500 for (i = 0; i < count; i++, objs++) {
5501 struct lu_fid *fid = &info->lti_fid;
5502 struct ost_id *oi = &info->lti_oi;
5503 struct lfsck_layout_req *llr;
5504 struct lfsck_tgt_desc *tgt = NULL;
5505 struct dt_object *cobj = NULL;
5507 bool wakeup = false;
5509 if (unlikely(lovea_slot_is_dummy(objs)))
5512 wait_event_idle(mthread->t_ctl_waitq,
5513 lad->lad_prefetched < bk->lb_async_windows ||
5514 !thread_is_running(mthread) ||
5515 thread_is_stopped(athread));
5517 if (unlikely(!thread_is_running(mthread)) ||
5518 thread_is_stopped(athread))
5521 if (unlikely(lfsck_is_dead_obj(parent)))
5524 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5525 index = le32_to_cpu(objs->l_ost_idx);
5526 rc = ostid_to_fid(fid, oi, index);
5528 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5529 ": "DOSTID", idx %u, comp_id %u\n",
5530 lfsck_lfsck2name(lfsck),
5531 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5536 tgt = lfsck_tgt_get(ltds, index);
5537 if (unlikely(tgt == NULL)) {
5538 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5539 "did not join the layout LFSCK, comp_id %u\n",
5540 lfsck_lfsck2name(lfsck), index, comp_id);
5541 lfsck_lad_set_bitmap(env, com, index);
5545 /* There is potential deadlock race condition between object
5546 * destroy and layout LFSCK. Consider the following scenario:
5548 * 1) The LFSCK thread obtained the parent object firstly, at
5549 * that time, the parent object has not been destroyed yet.
5551 * 2) One RPC service thread destroyed the parent and all its
5552 * children objects. Because the LFSCK is referencing the
5553 * parent object, then the parent object will be marked as
5554 * dying in RAM. On the other hand, the parent object is
5555 * referencing all its children objects, then all children
5556 * objects will be marked as dying in RAM also.
5558 * 3) The LFSCK thread tries to find some child object with
5559 * the parent object referenced. Then it will find that the
5560 * child object is dying. According to the object visibility
5561 * rules: the object with dying flag cannot be returned to
5562 * others. So the LFSCK thread has to wait until the dying
5563 * object has been purged from RAM, then it can allocate a
5564 * new object (with the same FID) in RAM. Unfortunately, the
5565 * LFSCK thread itself is referencing the parent object, and
5566 * cause the parent object cannot be purged, then cause the
5567 * child object cannot be purged also. So the LFSCK thread
5568 * will fall into deadlock.
5570 cobj = lfsck_object_find_by_dev(env, tgt->ltd_tgt, fid);
5572 if (lfsck_is_dead_obj(parent)) {
5582 rc = dt_declare_attr_get(env, cobj);
5586 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
5591 struct lu_attr *attr = &info->lti_la;
5593 rc = dt_attr_get(env, parent, attr);
5597 lso = lfsck_assistant_object_init(env,
5598 lfsck_dto2fid(parent), attr,
5599 lfsck->li_pos_current.lp_oit_cookie, false);
5608 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5616 spin_lock(&lad->lad_lock);
5617 if (lad->lad_assistant_status < 0) {
5618 spin_unlock(&lad->lad_lock);
5619 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5621 RETURN(lad->lad_assistant_status);
5624 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5625 if (lad->lad_prefetched == 0)
5628 lad->lad_prefetched++;
5629 spin_unlock(&lad->lad_lock);
5631 wake_up(&athread->t_ctl_waitq);
5634 down_write(&com->lc_sem);
5635 com->lc_new_checked++;
5637 lfsck_layout_record_failure(env, lfsck, lo);
5638 up_write(&com->lc_sem);
5640 if (cobj != NULL && !IS_ERR(cobj))
5641 lfsck_object_put(env, cobj);
5643 if (likely(tgt != NULL))
5646 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5654 lfsck_assistant_object_put(env, lso);
5659 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5660 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5661 * list ::lad_req_list.
5663 * For each request on above list, the lfsck_layout_assistant thread compares
5664 * the OST side attribute with local attribute, if inconsistent, then repair it.
5666 * All above processing is async mode with pipeline. */
5667 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5668 struct lfsck_component *com,
5669 struct dt_object *obj)
5671 struct lfsck_thread_info *info = lfsck_env_info(env);
5672 struct ost_id *oi = &info->lti_oi;
5673 struct lfsck_layout *lo = com->lc_file_ram;
5674 struct lfsck_assistant_data *lad = com->lc_data;
5675 struct lfsck_instance *lfsck = com->lc_lfsck;
5676 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5677 struct thandle *handle = NULL;
5678 struct lu_buf *buf = &info->lti_big_buf;
5679 struct lov_mds_md_v1 *lmm = NULL;
5680 struct dt_device *dev = lfsck_obj2dev(obj);
5681 struct lustre_handle lh = { 0 };
5682 struct lu_buf ea_buf = { NULL };
5683 struct lov_comp_md_v1 *lcm = NULL;
5684 struct lov_comp_md_entry_v1 *lcme = NULL;
5689 bool locked = false;
5690 bool stripe = false;
5691 bool bad_oi = false;
5694 if (!S_ISREG(lfsck_object_type(obj)))
5697 if (lad->lad_assistant_status < 0)
5698 GOTO(out, rc = -ESRCH);
5700 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5701 lmm_oi_cpu_to_le(oi, oi);
5702 dt_read_lock(env, obj, 0);
5707 if (dt_object_exists(obj) == 0 ||
5708 lfsck_is_dead_obj(obj))
5711 rc = lfsck_layout_get_lovea(env, obj, buf);
5712 if (rc == -EINVAL || rc == -ENODATA || rc == -EOPNOTSUPP)
5713 /* Skip bad lov EA during the 1st cycle scanning, and
5714 * try to recover it via orphan in the 2nd scanning. */
5721 magic = le32_to_cpu(lmm->lmm_magic);
5722 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5723 struct lov_mds_md_v1 *v1;
5727 count = le16_to_cpu(lcm->lcm_entry_count);
5728 for (i = 0; i < count; i++) {
5729 lcme = &lcm->lcm_entries[i];
5730 v1 = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5731 if (memcmp(oi, &v1->lmm_oi, sizeof(*oi)) != 0)
5735 GOTO(out, stripe = true);
5736 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5737 GOTO(out, stripe = true);
5741 /* Inconsistent lmm_oi, should be repaired. */
5744 if (bk->lb_param & LPF_DRYRUN) {
5745 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5747 GOTO(out, stripe = true);
5750 if (!lustre_handle_is_used(&lh)) {
5751 dt_read_unlock(env, obj);
5753 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5754 MDS_INODELOCK_LAYOUT |
5755 MDS_INODELOCK_XATTR, LCK_EX);
5759 handle = lfsck_trans_create(env, dev, lfsck);
5761 GOTO(out, rc = PTR_ERR(handle));
5763 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
5764 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5765 LU_XATTR_REPLACE, handle);
5769 rc = dt_trans_start_local(env, dev, handle);
5773 dt_write_lock(env, obj, 0);
5779 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5780 struct lov_mds_md_v1 *v1;
5783 for (i = 0; i < count; i++) {
5784 lcme = &lcm->lcm_entries[i];
5785 v1 = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5792 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5793 LU_XATTR_REPLACE, handle);
5797 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5799 GOTO(out, stripe = true);
5803 if (lustre_handle_is_used(&lh))
5804 dt_write_unlock(env, obj);
5806 dt_read_unlock(env, obj);
5809 if (handle != NULL && !IS_ERR(handle))
5810 dt_trans_stop(env, dev, handle);
5812 lfsck_ibits_unlock(&lh, LCK_EX);
5815 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5816 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5817 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5818 PFID(lfsck_dto2fid(obj)), rc);
5821 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5824 for (i = 0; i < count; i++) {
5825 lcme = &lcm->lcm_entries[i];
5826 if (!(le32_to_cpu(lcme->lcme_flags) &
5830 rc = lfsck_layout_scan_stripes(env, com, obj,
5831 (struct lov_mds_md_v1 *)(buf->lb_buf +
5832 le32_to_cpu(lcme->lcme_offset)),
5833 le32_to_cpu(lcme->lcme_id));
5836 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5839 down_write(&com->lc_sem);
5840 com->lc_new_checked++;
5842 lfsck_layout_record_failure(env, lfsck, lo);
5843 up_write(&com->lc_sem);
5849 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5850 struct lfsck_component *com,
5851 struct dt_object *obj)
5853 struct lfsck_instance *lfsck = com->lc_lfsck;
5854 struct lfsck_layout *lo = com->lc_file_ram;
5855 const struct lu_fid *fid = lfsck_dto2fid(obj);
5856 struct lfsck_layout_slave_data *llsd = com->lc_data;
5857 struct lfsck_layout_seq *lls;
5863 LASSERT(llsd != NULL);
5865 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5866 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5867 struct ptlrpc_thread *thread = &lfsck->li_thread;
5869 wait_event_idle_timeout(thread->t_ctl_waitq,
5870 !thread_is_running(thread),
5871 cfs_time_seconds(1));
5874 lfsck_rbtree_update_bitmap(env, com, fid, false);
5876 down_write(&com->lc_sem);
5877 if (fid_is_idif(fid))
5879 else if (!fid_is_norm(fid) ||
5880 !fid_is_for_ostobj(env, lfsck, obj, fid))
5881 GOTO(unlock, rc = 0);
5884 com->lc_new_checked++;
5886 lls = lfsck_layout_seq_lookup(llsd, seq);
5889 if (unlikely(lls == NULL))
5890 GOTO(unlock, rc = -ENOMEM);
5892 INIT_LIST_HEAD(&lls->lls_list);
5894 rc = lfsck_layout_lastid_load(env, com, lls);
5896 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5897 "load LAST_ID for %#llx: rc = %d\n",
5898 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5899 lo->ll_objs_failed_phase1++;
5904 lfsck_layout_seq_insert(llsd, lls);
5907 if (unlikely(fid_is_last_id(fid)))
5908 GOTO(unlock, rc = 0);
5910 if (fid_is_idif(fid))
5911 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5915 if (oid > lls->lls_lastid_known)
5916 lls->lls_lastid_known = oid;
5918 if (oid > lls->lls_lastid) {
5919 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
5920 /* OFD may create new objects during LFSCK scanning. */
5921 rc = lfsck_layout_lastid_reload(env, com, lls);
5922 if (unlikely(rc != 0)) {
5923 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5924 "reload LAST_ID for %#llx: rc = %d\n",
5925 lfsck_lfsck2name(com->lc_lfsck),
5931 if (oid <= lls->lls_lastid ||
5932 lo->ll_flags & LF_CRASHED_LASTID)
5933 GOTO(unlock, rc = 0);
5935 LASSERT(lfsck->li_out_notify != NULL);
5937 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5938 LE_LASTID_REBUILDING);
5939 lo->ll_flags |= LF_CRASHED_LASTID;
5941 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
5942 "LAST_ID file (2) for the sequence %#llx"
5943 ", old value %llu, known value %llu\n",
5944 lfsck_lfsck2name(lfsck), lls->lls_seq,
5945 lls->lls_lastid, oid);
5948 lls->lls_lastid = oid;
5952 GOTO(unlock, rc = 0);
5955 up_write(&com->lc_sem);
5960 static int lfsck_layout_exec_dir(const struct lu_env *env,
5961 struct lfsck_component *com,
5962 struct lfsck_assistant_object *lso,
5963 struct lu_dirent *ent, __u16 type)
5968 static int lfsck_layout_master_post(const struct lu_env *env,
5969 struct lfsck_component *com,
5970 int result, bool init)
5972 struct lfsck_instance *lfsck = com->lc_lfsck;
5973 struct lfsck_layout *lo = com->lc_file_ram;
5977 lfsck_post_generic(env, com, &result);
5979 down_write(&com->lc_sem);
5980 spin_lock(&lfsck->li_lock);
5982 lo->ll_pos_last_checkpoint =
5983 lfsck->li_pos_checkpoint.lp_oit_cookie;
5986 if (lo->ll_flags & LF_INCOMPLETE)
5987 lo->ll_status = LS_PARTIAL;
5989 lo->ll_status = LS_SCANNING_PHASE2;
5990 lo->ll_flags |= LF_SCANNED_ONCE;
5991 lo->ll_flags &= ~LF_UPGRADE;
5992 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5993 } else if (result == 0) {
5994 if (lfsck->li_status != 0)
5995 lo->ll_status = lfsck->li_status;
5997 lo->ll_status = LS_STOPPED;
5998 if (lo->ll_status != LS_PAUSED)
5999 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6001 lo->ll_status = LS_FAILED;
6002 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6004 spin_unlock(&lfsck->li_lock);
6007 lo->ll_run_time_phase1 += ktime_get_seconds() -
6008 lfsck->li_time_last_checkpoint;
6009 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
6010 lo->ll_objs_checked_phase1 += com->lc_new_checked;
6011 com->lc_new_checked = 0;
6014 rc = lfsck_layout_store(env, com);
6015 up_write(&com->lc_sem);
6017 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
6018 lfsck_lfsck2name(lfsck), rc);
6023 static int lfsck_layout_slave_post(const struct lu_env *env,
6024 struct lfsck_component *com,
6025 int result, bool init)
6027 struct lfsck_instance *lfsck = com->lc_lfsck;
6028 struct lfsck_layout *lo = com->lc_file_ram;
6032 down_write(&com->lc_sem);
6033 rc = lfsck_layout_lastid_store(env, com);
6037 LASSERT(lfsck->li_out_notify != NULL);
6039 spin_lock(&lfsck->li_lock);
6041 lo->ll_pos_last_checkpoint =
6042 lfsck->li_pos_checkpoint.lp_oit_cookie;
6045 lo->ll_status = LS_SCANNING_PHASE2;
6046 lo->ll_flags |= LF_SCANNED_ONCE;
6047 if (lo->ll_flags & LF_CRASHED_LASTID) {
6049 lo->ll_flags &= ~LF_CRASHED_LASTID;
6051 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
6052 "crashed LAST_ID files successfully\n",
6053 lfsck_lfsck2name(lfsck));
6055 lo->ll_flags &= ~LF_UPGRADE;
6056 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
6057 } else if (result == 0) {
6058 if (lfsck->li_status != 0)
6059 lo->ll_status = lfsck->li_status;
6061 lo->ll_status = LS_STOPPED;
6062 if (lo->ll_status != LS_PAUSED)
6063 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6065 lo->ll_status = LS_FAILED;
6066 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6068 spin_unlock(&lfsck->li_lock);
6071 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6075 lo->ll_run_time_phase1 += ktime_get_seconds() -
6076 lfsck->li_time_last_checkpoint;
6077 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
6078 lo->ll_objs_checked_phase1 += com->lc_new_checked;
6079 com->lc_new_checked = 0;
6082 rc = lfsck_layout_store(env, com);
6083 up_write(&com->lc_sem);
6085 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
6087 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
6088 lfsck_lfsck2name(lfsck), rc);
6093 static void lfsck_layout_dump(const struct lu_env *env,
6094 struct lfsck_component *com, struct seq_file *m)
6096 struct lfsck_instance *lfsck = com->lc_lfsck;
6097 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6098 struct lfsck_layout *lo = com->lc_file_ram;
6101 down_read(&com->lc_sem);
6102 if (bk->lb_param & LPF_DRYRUN)
6103 prefix = "inconsistent";
6105 prefix = "repaired";
6107 seq_printf(m, "name: lfsck_layout\n"
6113 lfsck_status2name(lo->ll_status));
6115 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
6117 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
6119 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
6121 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
6123 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
6125 seq_printf(m, "latest_start_position: %llu\n"
6126 "last_checkpoint_position: %llu\n"
6127 "first_failure_position: %llu\n",
6128 lo->ll_pos_latest_start,
6129 lo->ll_pos_last_checkpoint,
6130 lo->ll_pos_first_inconsistent);
6132 seq_printf(m, "success_count: %u\n"
6133 "%s_dangling: %llu\n"
6134 "%s_unmatched_pair: %llu\n"
6135 "%s_multiple_referenced: %llu\n"
6137 "%s_inconsistent_owner: %llu\n"
6140 "failed_phase1: %llu\n"
6141 "failed_phase2: %llu\n",
6142 lo->ll_success_count,
6143 prefix, lo->ll_objs_repaired[LLIT_DANGLING - 1],
6144 prefix, lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
6145 prefix, lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
6146 prefix, lo->ll_objs_repaired[LLIT_ORPHAN - 1],
6147 prefix, lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
6148 prefix, lo->ll_objs_repaired[LLIT_OTHERS - 1],
6149 lo->ll_objs_skipped,
6150 lo->ll_objs_failed_phase1,
6151 lo->ll_objs_failed_phase2);
6153 if (lo->ll_status == LS_SCANNING_PHASE1) {
6154 time64_t duration = ktime_get_seconds() -
6155 lfsck->li_time_last_checkpoint;
6156 u64 checked = lo->ll_objs_checked_phase1 +
6157 com->lc_new_checked;
6158 u64 speed = checked;
6159 u64 new_checked = com->lc_new_checked;
6160 time64_t rtime = lo->ll_run_time_phase1 + duration;
6164 new_checked = div64_s64(new_checked, duration);
6166 speed = div64_s64(speed, rtime);
6167 seq_printf(m, "checked_phase1: %llu\n"
6168 "checked_phase2: %llu\n"
6169 "run_time_phase1: %lld seconds\n"
6170 "run_time_phase2: %lld seconds\n"
6171 "average_speed_phase1: %llu items/sec\n"
6172 "average_speed_phase2: N/A\n"
6173 "real_time_speed_phase1: %llu items/sec\n"
6174 "real_time_speed_phase2: N/A\n",
6176 lo->ll_objs_checked_phase2,
6178 lo->ll_run_time_phase2,
6182 if (likely(lfsck->li_di_oit)) {
6183 const struct dt_it_ops *iops =
6184 &lfsck->li_obj_oit->do_index_ops->dio_it;
6186 /* The low layer otable-based iteration position may NOT
6187 * exactly match the layout-based directory traversal
6188 * cookie. Generally, it is not a serious issue. But the
6189 * caller should NOT make assumption on that. */
6190 pos = iops->store(env, lfsck->li_di_oit);
6191 if (!lfsck->li_current_oit_processed)
6194 pos = lo->ll_pos_last_checkpoint;
6197 seq_printf(m, "current_position: %llu\n", pos);
6198 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
6199 time64_t duration = ktime_get_seconds() -
6200 com->lc_time_last_checkpoint;
6201 u64 checked = lo->ll_objs_checked_phase2 +
6202 com->lc_new_checked;
6203 u64 speed1 = lo->ll_objs_checked_phase1;
6204 u64 speed2 = checked;
6205 u64 new_checked = com->lc_new_checked;
6206 time64_t rtime = lo->ll_run_time_phase2 + duration;
6209 new_checked = div64_s64(new_checked, duration);
6210 if (lo->ll_run_time_phase1 != 0)
6211 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6213 speed2 = div64_s64(speed2, rtime);
6214 seq_printf(m, "checked_phase1: %llu\n"
6215 "checked_phase2: %llu\n"
6216 "run_time_phase1: %lld seconds\n"
6217 "run_time_phase2: %lld seconds\n"
6218 "average_speed_phase1: %llu items/sec\n"
6219 "average_speed_phase2: %llu items/sec\n"
6220 "real_time_speed_phase1: N/A\n"
6221 "real_time_speed_phase2: %llu items/sec\n"
6222 "current_position: "DFID"\n",
6223 lo->ll_objs_checked_phase1,
6225 lo->ll_run_time_phase1,
6230 PFID(&com->lc_fid_latest_scanned_phase2));
6232 __u64 speed1 = lo->ll_objs_checked_phase1;
6233 __u64 speed2 = lo->ll_objs_checked_phase2;
6235 if (lo->ll_run_time_phase1 != 0)
6236 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6237 if (lo->ll_run_time_phase2 != 0)
6238 speed2 = div64_s64(speed2, lo->ll_run_time_phase2);
6239 seq_printf(m, "checked_phase1: %llu\n"
6240 "checked_phase2: %llu\n"
6241 "run_time_phase1: %lld seconds\n"
6242 "run_time_phase2: %lld seconds\n"
6243 "average_speed_phase1: %llu items/sec\n"
6244 "average_speed_phase2: %llu objs/sec\n"
6245 "real_time_speed_phase1: N/A\n"
6246 "real_time_speed_phase2: N/A\n"
6247 "current_position: N/A\n",
6248 lo->ll_objs_checked_phase1,
6249 lo->ll_objs_checked_phase2,
6250 lo->ll_run_time_phase1,
6251 lo->ll_run_time_phase2,
6256 up_read(&com->lc_sem);
6259 static int lfsck_layout_master_double_scan(const struct lu_env *env,
6260 struct lfsck_component *com)
6262 struct lfsck_layout *lo = com->lc_file_ram;
6263 struct lfsck_assistant_data *lad = com->lc_data;
6264 struct lfsck_instance *lfsck = com->lc_lfsck;
6265 struct lfsck_tgt_descs *ltds;
6266 struct lfsck_tgt_desc *ltd;
6267 struct lfsck_tgt_desc *next;
6270 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
6272 if (thread_is_stopped(&lad->lad_thread)) {
6273 LASSERT(list_empty(&lad->lad_req_list));
6274 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6275 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6277 ltds = &lfsck->li_ost_descs;
6278 spin_lock(<ds->ltd_lock);
6279 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6280 ltd_layout_phase_list) {
6281 list_del_init(<d->ltd_layout_phase_list);
6283 spin_unlock(<ds->ltd_lock);
6285 ltds = &lfsck->li_mdt_descs;
6286 spin_lock(<ds->ltd_lock);
6287 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6288 ltd_layout_phase_list) {
6289 list_del_init(<d->ltd_layout_phase_list);
6291 spin_unlock(<ds->ltd_lock);
6297 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6298 struct lfsck_component *com)
6300 struct lfsck_instance *lfsck = com->lc_lfsck;
6301 struct lfsck_layout_slave_data *llsd = com->lc_data;
6302 struct lfsck_layout *lo = com->lc_file_ram;
6303 struct ptlrpc_thread *thread = &lfsck->li_thread;
6307 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6308 lfsck_lfsck2name(lfsck));
6310 atomic_inc(&lfsck->li_double_scan_count);
6312 if (lo->ll_flags & LF_INCOMPLETE)
6315 com->lc_new_checked = 0;
6316 com->lc_new_scanned = 0;
6317 com->lc_time_last_checkpoint = ktime_get_seconds();
6318 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6319 LFSCK_CHECKPOINT_INTERVAL;
6322 rc = lfsck_layout_slave_query_master(env, com);
6323 if (list_empty(&llsd->llsd_master_list)) {
6324 if (unlikely(!thread_is_running(thread)))
6335 rc = wait_event_idle_timeout(
6336 thread->t_ctl_waitq,
6337 !thread_is_running(thread) ||
6338 lo->ll_flags & LF_INCOMPLETE ||
6339 list_empty(&llsd->llsd_master_list),
6340 cfs_time_seconds(30));
6341 if (unlikely(!thread_is_running(thread)))
6344 if (lo->ll_flags & LF_INCOMPLETE)
6354 rc = lfsck_layout_double_scan_result(env, com, rc);
6355 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6356 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6357 lfsck_layout_slave_quit(env, com);
6358 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6359 wake_up(&lfsck->li_thread.t_ctl_waitq);
6361 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6362 "status %d: rc = %d\n",
6363 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6368 static void lfsck_layout_master_data_release(const struct lu_env *env,
6369 struct lfsck_component *com)
6371 struct lfsck_assistant_data *lad = com->lc_data;
6372 struct lfsck_instance *lfsck = com->lc_lfsck;
6373 struct lfsck_tgt_descs *ltds;
6374 struct lfsck_tgt_desc *ltd;
6375 struct lfsck_tgt_desc *next;
6377 LASSERT(lad != NULL);
6378 LASSERT(thread_is_init(&lad->lad_thread) ||
6379 thread_is_stopped(&lad->lad_thread));
6380 LASSERT(list_empty(&lad->lad_req_list));
6382 com->lc_data = NULL;
6384 ltds = &lfsck->li_ost_descs;
6385 spin_lock(<ds->ltd_lock);
6386 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6387 ltd_layout_phase_list) {
6388 list_del_init(<d->ltd_layout_phase_list);
6390 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6391 ltd_layout_phase_list) {
6392 list_del_init(<d->ltd_layout_phase_list);
6394 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6396 list_del_init(<d->ltd_layout_list);
6398 spin_unlock(<ds->ltd_lock);
6400 ltds = &lfsck->li_mdt_descs;
6401 spin_lock(<ds->ltd_lock);
6402 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6403 ltd_layout_phase_list) {
6404 list_del_init(<d->ltd_layout_phase_list);
6406 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6407 ltd_layout_phase_list) {
6408 list_del_init(<d->ltd_layout_phase_list);
6410 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6412 list_del_init(<d->ltd_layout_list);
6414 spin_unlock(<ds->ltd_lock);
6416 if (likely(lad->lad_bitmap != NULL))
6417 CFS_FREE_BITMAP(lad->lad_bitmap);
6422 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6423 struct lfsck_component *com)
6425 struct lfsck_layout_slave_data *llsd = com->lc_data;
6427 lfsck_layout_slave_quit(env, com);
6428 com->lc_data = NULL;
6432 static void lfsck_layout_master_quit(const struct lu_env *env,
6433 struct lfsck_component *com)
6435 struct lfsck_assistant_data *lad = com->lc_data;
6436 struct lfsck_instance *lfsck = com->lc_lfsck;
6437 struct lfsck_tgt_descs *ltds;
6438 struct lfsck_tgt_desc *ltd;
6439 struct lfsck_tgt_desc *next;
6441 LASSERT(lad != NULL);
6443 lfsck_quit_generic(env, com);
6445 LASSERT(thread_is_init(&lad->lad_thread) ||
6446 thread_is_stopped(&lad->lad_thread));
6447 LASSERT(list_empty(&lad->lad_req_list));
6449 ltds = &lfsck->li_ost_descs;
6450 spin_lock(<ds->ltd_lock);
6451 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6452 ltd_layout_phase_list) {
6453 list_del_init(<d->ltd_layout_phase_list);
6455 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6456 ltd_layout_phase_list) {
6457 list_del_init(<d->ltd_layout_phase_list);
6459 spin_unlock(<ds->ltd_lock);
6461 ltds = &lfsck->li_mdt_descs;
6462 spin_lock(<ds->ltd_lock);
6463 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6464 ltd_layout_phase_list) {
6465 list_del_init(<d->ltd_layout_phase_list);
6467 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6468 ltd_layout_phase_list) {
6469 list_del_init(<d->ltd_layout_phase_list);
6471 spin_unlock(<ds->ltd_lock);
6474 static void lfsck_layout_slave_quit(const struct lu_env *env,
6475 struct lfsck_component *com)
6477 struct lfsck_layout_slave_data *llsd = com->lc_data;
6478 struct lfsck_layout_seq *lls;
6479 struct lfsck_layout_seq *next;
6480 struct lfsck_layout_slave_target *llst;
6482 LASSERT(llsd != NULL);
6484 down_write(&com->lc_sem);
6485 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6487 list_del_init(&lls->lls_list);
6488 lfsck_object_put(env, lls->lls_lastid_obj);
6491 up_write(&com->lc_sem);
6493 spin_lock(&llsd->llsd_lock);
6494 while (!list_empty(&llsd->llsd_master_list)) {
6495 llst = list_entry(llsd->llsd_master_list.next,
6496 struct lfsck_layout_slave_target, llst_list);
6497 list_del_init(&llst->llst_list);
6498 spin_unlock(&llsd->llsd_lock);
6499 lfsck_layout_llst_put(llst);
6500 spin_lock(&llsd->llsd_lock);
6502 spin_unlock(&llsd->llsd_lock);
6504 lfsck_rbtree_cleanup(env, com);
6507 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6508 struct lfsck_component *com,
6509 struct lfsck_request *lr)
6511 struct lfsck_instance *lfsck = com->lc_lfsck;
6512 struct lfsck_layout *lo = com->lc_file_ram;
6513 struct lfsck_assistant_data *lad = com->lc_data;
6514 struct lfsck_tgt_descs *ltds;
6515 struct lfsck_tgt_desc *ltd;
6519 if (lr->lr_event == LE_PAIRS_VERIFY) {
6522 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6529 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6530 "from %s %x, status %d, flags %x, flags2 %x\n",
6531 lfsck_lfsck2name(lfsck), lr->lr_event,
6532 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6533 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6535 if (lr->lr_event != LE_PHASE1_DONE &&
6536 lr->lr_event != LE_PHASE2_DONE &&
6537 lr->lr_event != LE_PEER_EXIT)
6540 if (lr->lr_flags & LEF_FROM_OST)
6541 ltds = &lfsck->li_ost_descs;
6543 ltds = &lfsck->li_mdt_descs;
6544 spin_lock(<ds->ltd_lock);
6545 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6547 spin_unlock(<ds->ltd_lock);
6552 list_del_init(<d->ltd_layout_phase_list);
6553 switch (lr->lr_event) {
6554 case LE_PHASE1_DONE:
6555 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6556 if (lr->lr_flags2 & LF_INCOMPLETE) {
6557 if (lr->lr_flags & LEF_FROM_OST)
6558 lfsck_lad_set_bitmap(env, com,
6561 lo->ll_flags |= LF_INCOMPLETE;
6563 ltd->ltd_layout_done = 1;
6564 list_del_init(<d->ltd_layout_list);
6569 if (lr->lr_flags & LEF_FROM_OST) {
6570 if (list_empty(<d->ltd_layout_list))
6571 list_add_tail(<d->ltd_layout_list,
6572 &lad->lad_ost_list);
6573 list_add_tail(<d->ltd_layout_phase_list,
6574 &lad->lad_ost_phase2_list);
6576 if (list_empty(<d->ltd_layout_list))
6577 list_add_tail(<d->ltd_layout_list,
6578 &lad->lad_mdt_list);
6579 list_add_tail(<d->ltd_layout_phase_list,
6580 &lad->lad_mdt_phase2_list);
6583 case LE_PHASE2_DONE:
6584 ltd->ltd_layout_done = 1;
6585 if (!list_empty(<d->ltd_layout_list))
6586 list_del_init(<d->ltd_layout_list);
6588 if (lr->lr_flags2 & LF_INCOMPLETE) {
6589 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6596 ltd->ltd_layout_done = 1;
6597 list_del_init(<d->ltd_layout_list);
6598 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6599 !(lr->lr_flags & LEF_FROM_OST))
6600 lo->ll_flags |= LF_INCOMPLETE;
6605 spin_unlock(<ds->ltd_lock);
6607 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6608 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6610 memset(stop, 0, sizeof(*stop));
6611 stop->ls_status = lr->lr_status;
6612 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6613 lfsck_stop(env, lfsck->li_bottom, stop);
6614 } else if (lfsck_phase2_next_ready(lad)) {
6615 wake_up(&lad->lad_thread.t_ctl_waitq);
6621 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6622 struct lfsck_component *com,
6623 struct lfsck_req_local *lrl,
6628 switch (lrl->lrl_event) {
6629 case LEL_FID_ACCESSED:
6630 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6632 case LEL_PAIRS_VERIFY_LOCAL: {
6635 lrl->lrl_status = LPVS_INIT;
6636 /* Firstly, if the MDT-object which is claimed via OST-object
6637 * local stored PFID xattr recognizes the OST-object, then it
6638 * must be that the client given PFID is wrong. */
6639 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6640 &lrl->lrl_ff_local.ff_parent,
6641 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6645 lrl->lrl_status = LPVS_INCONSISTENT;
6646 /* The OST-object local stored PFID xattr is stale. We need to
6647 * check whether the MDT-object that is claimed via the client
6648 * given PFID information recognizes the OST-object or not. If
6649 * matches, then need to update the OST-object's PFID xattr. */
6650 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6651 &lrl->lrl_ff_client.ff_parent,
6652 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6654 * We are not sure whether the client given PFID information
6655 * is correct or not, do nothing to avoid improper fixing.
6658 * The client given PFID information is also invalid, we can
6659 * NOT fix the OST-object inconsistency.
6662 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6663 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6672 RETURN(-EOPNOTSUPP);
6675 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6676 struct lfsck_component *com,
6677 struct lfsck_request *lr)
6679 struct lfsck_instance *lfsck = com->lc_lfsck;
6680 struct lfsck_layout_slave_data *llsd = com->lc_data;
6681 struct lfsck_layout_slave_target *llst;
6685 switch (lr->lr_event) {
6686 case LE_CONDITIONAL_DESTROY:
6687 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6689 case LE_PHASE1_DONE: {
6690 if (lr->lr_flags2 & LF_INCOMPLETE) {
6691 struct lfsck_layout *lo = com->lc_file_ram;
6693 lo->ll_flags |= LF_INCOMPLETE;
6694 llst = lfsck_layout_llst_find_and_del(llsd,
6698 lfsck_layout_llst_put(llst);
6699 wake_up(&lfsck->li_thread.t_ctl_waitq);
6705 case LE_PHASE2_DONE:
6707 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6708 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6709 lr->lr_event, lr->lr_index, lr->lr_status);
6715 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6719 lfsck_layout_llst_put(llst);
6720 if (list_empty(&llsd->llsd_master_list))
6721 wake_up(&lfsck->li_thread.t_ctl_waitq);
6723 if (lr->lr_event == LE_PEER_EXIT &&
6724 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6725 (list_empty(&llsd->llsd_master_list) &&
6726 (lr->lr_status == LS_STOPPED ||
6727 lr->lr_status == LS_CO_STOPPED)))) {
6728 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6730 memset(stop, 0, sizeof(*stop));
6731 stop->ls_status = lr->lr_status;
6732 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6733 lfsck_stop(env, lfsck->li_bottom, stop);
6739 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6743 for (i = 0; i < LLIT_MAX; i++)
6744 *count += lo->ll_objs_repaired[i];
6747 static int lfsck_layout_query_all(const struct lu_env *env,
6748 struct lfsck_component *com,
6749 __u32 *mdts_count, __u32 *osts_count,
6752 struct lfsck_layout *lo = com->lc_file_ram;
6753 struct lfsck_tgt_descs *ltds;
6754 struct lfsck_tgt_desc *ltd;
6759 rc = lfsck_query_all(env, com);
6763 ltds = &com->lc_lfsck->li_mdt_descs;
6764 down_read(<ds->ltd_rw_sem);
6765 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6766 ltd = lfsck_ltd2tgt(ltds, idx);
6767 LASSERT(ltd != NULL);
6769 mdts_count[ltd->ltd_layout_status]++;
6770 *repaired += ltd->ltd_layout_repaired;
6772 up_read(<ds->ltd_rw_sem);
6774 ltds = &com->lc_lfsck->li_ost_descs;
6775 down_read(<ds->ltd_rw_sem);
6776 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6777 ltd = lfsck_ltd2tgt(ltds, idx);
6778 LASSERT(ltd != NULL);
6780 osts_count[ltd->ltd_layout_status]++;
6781 *repaired += ltd->ltd_layout_repaired;
6783 up_read(<ds->ltd_rw_sem);
6785 down_read(&com->lc_sem);
6786 mdts_count[lo->ll_status]++;
6787 lfsck_layout_repaired(lo, repaired);
6788 up_read(&com->lc_sem);
6793 static int lfsck_layout_query(const struct lu_env *env,
6794 struct lfsck_component *com,
6795 struct lfsck_request *req,
6796 struct lfsck_reply *rep,
6797 struct lfsck_query *que, int idx)
6799 struct lfsck_layout *lo = com->lc_file_ram;
6803 LASSERT(com->lc_lfsck->li_master);
6805 rc = lfsck_layout_query_all(env, com,
6806 que->lu_mdts_count[idx],
6807 que->lu_osts_count[idx],
6808 &que->lu_repaired[idx]);
6810 down_read(&com->lc_sem);
6811 rep->lr_status = lo->ll_status;
6812 if (req->lr_flags & LEF_QUERY_ALL)
6813 lfsck_layout_repaired(lo, &rep->lr_repaired);
6814 up_read(&com->lc_sem);
6820 /* with lfsck::li_lock held */
6821 static int lfsck_layout_slave_join(const struct lu_env *env,
6822 struct lfsck_component *com,
6823 struct lfsck_start_param *lsp)
6825 struct lfsck_instance *lfsck = com->lc_lfsck;
6826 struct lfsck_layout_slave_data *llsd = com->lc_data;
6827 struct lfsck_layout_slave_target *llst;
6828 struct lfsck_start *start = lsp->lsp_start;
6832 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6835 if (!lsp->lsp_index_valid)
6838 /* If someone is running the LFSCK without orphan handling,
6839 * it will not maintain the object accessing rbtree. So we
6840 * cannot join it for orphan handling. */
6841 if (!llsd->llsd_rbtree_valid)
6844 spin_unlock(&lfsck->li_lock);
6845 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6846 spin_lock(&lfsck->li_lock);
6847 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6848 spin_unlock(&lfsck->li_lock);
6849 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6852 lfsck_layout_llst_put(llst);
6853 spin_lock(&lfsck->li_lock);
6860 static const struct lfsck_operations lfsck_layout_master_ops = {
6861 .lfsck_reset = lfsck_layout_reset,
6862 .lfsck_fail = lfsck_layout_fail,
6863 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6864 .lfsck_prep = lfsck_layout_master_prep,
6865 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6866 .lfsck_exec_dir = lfsck_layout_exec_dir,
6867 .lfsck_post = lfsck_layout_master_post,
6868 .lfsck_dump = lfsck_layout_dump,
6869 .lfsck_double_scan = lfsck_layout_master_double_scan,
6870 .lfsck_data_release = lfsck_layout_master_data_release,
6871 .lfsck_quit = lfsck_layout_master_quit,
6872 .lfsck_in_notify = lfsck_layout_master_in_notify,
6873 .lfsck_query = lfsck_layout_query,
6876 static const struct lfsck_operations lfsck_layout_slave_ops = {
6877 .lfsck_reset = lfsck_layout_reset,
6878 .lfsck_fail = lfsck_layout_fail,
6879 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6880 .lfsck_prep = lfsck_layout_slave_prep,
6881 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6882 .lfsck_exec_dir = lfsck_layout_exec_dir,
6883 .lfsck_post = lfsck_layout_slave_post,
6884 .lfsck_dump = lfsck_layout_dump,
6885 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6886 .lfsck_data_release = lfsck_layout_slave_data_release,
6887 .lfsck_quit = lfsck_layout_slave_quit,
6888 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6889 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6890 .lfsck_query = lfsck_layout_query,
6891 .lfsck_join = lfsck_layout_slave_join,
6894 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6895 struct lfsck_component *com,
6896 struct lfsck_position *pos)
6898 struct lfsck_assistant_data *lad = com->lc_data;
6899 struct lfsck_layout_req *llr;
6901 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6905 if (list_empty(&lad->lad_req_list))
6908 llr = list_entry(lad->lad_req_list.next,
6909 struct lfsck_layout_req,
6911 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6914 const struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6915 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
6916 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
6917 .la_fill_pos = lfsck_layout_assistant_fill_pos,
6918 .la_double_scan_result = lfsck_layout_double_scan_result,
6919 .la_req_fini = lfsck_layout_assistant_req_fini,
6920 .la_sync_failures = lfsck_layout_assistant_sync_failures,
6923 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
6925 struct lfsck_component *com;
6926 struct lfsck_layout *lo;
6927 struct dt_object *root = NULL;
6928 struct dt_object *obj;
6937 INIT_LIST_HEAD(&com->lc_link);
6938 INIT_LIST_HEAD(&com->lc_link_dir);
6939 init_rwsem(&com->lc_sem);
6940 atomic_set(&com->lc_ref, 1);
6941 com->lc_lfsck = lfsck;
6942 com->lc_type = LFSCK_TYPE_LAYOUT;
6943 if (lfsck->li_master) {
6944 com->lc_ops = &lfsck_layout_master_ops;
6945 com->lc_data = lfsck_assistant_data_init(
6946 &lfsck_layout_assistant_ops,
6948 if (com->lc_data == NULL)
6949 GOTO(out, rc = -ENOMEM);
6951 for (i = 0; i < LFSCK_STF_COUNT; i++)
6952 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6954 struct lfsck_layout_slave_data *llsd;
6956 com->lc_ops = &lfsck_layout_slave_ops;
6957 OBD_ALLOC_PTR(llsd);
6959 GOTO(out, rc = -ENOMEM);
6961 INIT_LIST_HEAD(&llsd->llsd_seq_list);
6962 INIT_LIST_HEAD(&llsd->llsd_master_list);
6963 spin_lock_init(&llsd->llsd_lock);
6964 llsd->llsd_rb_root = RB_ROOT;
6965 init_rwsem(&llsd->llsd_rb_rwsem);
6966 com->lc_data = llsd;
6968 com->lc_file_size = sizeof(*lo);
6969 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6970 if (com->lc_file_ram == NULL)
6971 GOTO(out, rc = -ENOMEM);
6973 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6974 if (com->lc_file_disk == NULL)
6975 GOTO(out, rc = -ENOMEM);
6977 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6979 GOTO(out, rc = PTR_ERR(root));
6981 if (unlikely(!dt_try_as_dir(env, root)))
6982 GOTO(out, rc = -ENOTDIR);
6984 obj = local_file_find_or_create(env, lfsck->li_los, root,
6986 S_IFREG | S_IRUGO | S_IWUSR);
6988 GOTO(out, rc = PTR_ERR(obj));
6991 rc = lfsck_layout_load(env, com);
6993 rc = lfsck_layout_reset(env, com, true);
6994 } else if (rc == -ENOENT) {
6995 rc = lfsck_layout_init(env, com);
6996 } else if (lfsck->li_master) {
6997 rc = lfsck_load_sub_trace_files(env, com,
6998 &dt_lfsck_layout_dangling_features,
6999 LFSCK_LAYOUT, false);
7001 rc = lfsck_layout_reset(env, com, true);
7007 lo = com->lc_file_ram;
7008 switch (lo->ll_status) {
7014 spin_lock(&lfsck->li_lock);
7015 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
7016 spin_unlock(&lfsck->li_lock);
7019 CERROR("%s: unknown lfsck_layout status %d\n",
7020 lfsck_lfsck2name(lfsck), lo->ll_status);
7022 case LS_SCANNING_PHASE1:
7023 case LS_SCANNING_PHASE2:
7024 /* No need to store the status to disk right now.
7025 * If the system crashed before the status stored,
7026 * it will be loaded back when next time. */
7027 lo->ll_status = LS_CRASHED;
7028 if (!lfsck->li_master)
7029 lo->ll_flags |= LF_INCOMPLETE;
7036 spin_lock(&lfsck->li_lock);
7037 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
7038 spin_unlock(&lfsck->li_lock);
7042 if (lo->ll_flags & LF_CRASHED_LASTID) {
7043 LASSERT(lfsck->li_out_notify != NULL);
7045 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
7046 LE_LASTID_REBUILDING);
7052 if (root != NULL && !IS_ERR(root))
7053 lfsck_object_put(env, root);
7056 lfsck_component_cleanup(env, com);
7057 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
7058 lfsck_lfsck2name(lfsck), rc);
7064 struct lfsck_orphan_it {
7065 struct lfsck_component *loi_com;
7066 struct lfsck_rbtree_node *loi_lrn;
7067 struct lfsck_layout_slave_target *loi_llst;
7068 struct lu_fid loi_key;
7069 struct lu_orphan_rec_v3 loi_rec;
7071 unsigned int loi_over:1;
7074 static int lfsck_fid_match_idx(const struct lu_env *env,
7075 struct lfsck_instance *lfsck,
7076 const struct lu_fid *fid, int idx)
7078 struct seq_server_site *ss;
7079 struct lu_server_fld *sf;
7080 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
7083 /* All abnormal cases will be returned to MDT0. */
7084 if (!fid_is_norm(fid)) {
7091 ss = lfsck_dev_site(lfsck);
7092 if (unlikely(ss == NULL))
7095 sf = ss->ss_server_fld;
7096 LASSERT(sf != NULL);
7098 fld_range_set_any(range);
7099 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
7103 if (!fld_range_is_mdt(range))
7106 if (range->lsr_index == idx)
7112 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
7113 struct lfsck_instance *lfsck,
7114 struct dt_object *obj)
7116 struct dt_device *dev = lfsck_obj2dev(obj);
7117 struct thandle *handle;
7121 handle = lfsck_trans_create(env, dev, lfsck);
7125 rc = dt_declare_ref_del(env, obj, handle);
7129 rc = dt_declare_destroy(env, obj, handle);
7133 rc = dt_trans_start_local(env, dev, handle);
7137 dt_write_lock(env, obj, 0);
7138 rc = dt_ref_del(env, obj, handle);
7140 rc = dt_destroy(env, obj, handle);
7141 dt_write_unlock(env, obj);
7146 dt_trans_stop(env, dev, handle);
7148 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
7149 PFID(lfsck_dto2fid(obj)), rc);
7154 static int lfsck_orphan_index_lookup(const struct lu_env *env,
7155 struct dt_object *dt,
7157 const struct dt_key *key)
7162 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
7163 struct dt_object *dt,
7164 const struct dt_rec *rec,
7165 const struct dt_key *key,
7166 struct thandle *handle)
7171 static int lfsck_orphan_index_insert(const struct lu_env *env,
7172 struct dt_object *dt,
7173 const struct dt_rec *rec,
7174 const struct dt_key *key,
7175 struct thandle *handle)
7180 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
7181 struct dt_object *dt,
7182 const struct dt_key *key,
7183 struct thandle *handle)
7188 static int lfsck_orphan_index_delete(const struct lu_env *env,
7189 struct dt_object *dt,
7190 const struct dt_key *key,
7191 struct thandle *handle)
7196 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
7197 struct dt_object *dt,
7200 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
7201 struct lfsck_instance *lfsck;
7202 struct lfsck_component *com = NULL;
7203 struct lfsck_layout_slave_data *llsd;
7204 struct lfsck_orphan_it *it = NULL;
7205 struct lfsck_layout *lo;
7209 lfsck = lfsck_instance_find(dev, true, false);
7210 if (unlikely(lfsck == NULL))
7211 RETURN(ERR_PTR(-ENXIO));
7213 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
7214 if (unlikely(com == NULL))
7215 GOTO(out, rc = -ENOENT);
7217 lo = com->lc_file_ram;
7218 if (lo->ll_flags & LF_INCOMPLETE)
7219 GOTO(out, rc = -ESRCH);
7221 llsd = com->lc_data;
7222 if (!llsd->llsd_rbtree_valid)
7223 GOTO(out, rc = -ESRCH);
7227 GOTO(out, rc = -ENOMEM);
7229 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
7230 if (it->loi_llst == NULL)
7231 GOTO(out, rc = -ENXIO);
7233 if (dev->dd_record_fid_accessed) {
7234 /* The first iteration against the rbtree, scan the whole rbtree
7235 * to remove the nodes which do NOT need to be handled. */
7236 down_write(&llsd->llsd_rb_rwsem);
7237 if (dev->dd_record_fid_accessed) {
7238 struct rb_node *node;
7239 struct rb_node *next;
7240 struct lfsck_rbtree_node *lrn;
7242 /* No need to record the fid accessing anymore. */
7243 dev->dd_record_fid_accessed = 0;
7245 node = rb_first(&llsd->llsd_rb_root);
7246 while (node != NULL) {
7247 next = rb_next(node);
7248 lrn = rb_entry(node, struct lfsck_rbtree_node,
7250 if (atomic_read(&lrn->lrn_known_count) <=
7251 atomic_read(&lrn->lrn_accessed_count)) {
7252 rb_erase(node, &llsd->llsd_rb_root);
7253 lfsck_rbtree_free(lrn);
7258 up_write(&llsd->llsd_rb_rwsem);
7261 /* read lock the rbtree when init, and unlock when fini */
7262 down_read(&llsd->llsd_rb_rwsem);
7270 lfsck_component_put(env, com);
7272 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
7273 lfsck_lfsck2name(lfsck), rc);
7275 lfsck_instance_put(env, lfsck);
7280 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7283 return (struct dt_it *)it;
7286 static void lfsck_orphan_it_fini(const struct lu_env *env,
7289 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7290 struct lfsck_component *com = it->loi_com;
7291 struct lfsck_layout_slave_data *llsd;
7292 struct lfsck_layout_slave_target *llst;
7295 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7296 lfsck_lfsck2name(com->lc_lfsck));
7298 llsd = com->lc_data;
7299 up_read(&llsd->llsd_rb_rwsem);
7300 llst = it->loi_llst;
7301 LASSERT(llst != NULL);
7303 /* Save the key and hash for iterate next. */
7304 llst->llst_fid = it->loi_key;
7305 llst->llst_hash = it->loi_hash;
7306 lfsck_layout_llst_put(llst);
7307 lfsck_component_put(env, com);
7313 * \retval +1: the iteration finished
7314 * \retval 0: on success, not finished
7315 * \retval -ve: on error
7317 static int lfsck_orphan_it_next(const struct lu_env *env,
7320 struct lfsck_thread_info *info = lfsck_env_info(env);
7321 struct filter_fid *ff = &info->lti_ff;
7322 struct lu_attr *la = &info->lti_la;
7323 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7324 struct lu_fid *key = &it->loi_key;
7325 struct lu_orphan_rec_v3 *rec = &it->loi_rec;
7326 struct ost_layout *ol = &rec->lor_layout;
7327 struct lfsck_component *com = it->loi_com;
7328 struct lfsck_instance *lfsck = com->lc_lfsck;
7329 struct lfsck_layout_slave_data *llsd = com->lc_data;
7330 struct dt_object *obj;
7331 struct lfsck_rbtree_node *lrn;
7335 __u32 idx = it->loi_llst->llst_index;
7345 lrn = lfsck_rbtree_search(llsd, key, &exact);
7353 key->f_seq = lrn->lrn_seq;
7354 key->f_oid = lrn->lrn_first_oid;
7359 if (unlikely(key->f_oid == 0)) {
7366 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7372 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7373 atomic_read(&lrn->lrn_accessed_count))) {
7374 struct rb_node *next = rb_next(&lrn->lrn_node);
7376 while (next != NULL) {
7377 lrn = rb_entry(next, struct lfsck_rbtree_node,
7379 if (atomic_read(&lrn->lrn_known_count) >
7380 atomic_read(&lrn->lrn_accessed_count))
7382 next = rb_next(next);
7391 key->f_seq = lrn->lrn_seq;
7392 key->f_oid = lrn->lrn_first_oid;
7396 pos = key->f_oid - lrn->lrn_first_oid;
7399 pos = find_next_bit(lrn->lrn_known_bitmap,
7400 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7401 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7402 key->f_oid = lrn->lrn_first_oid + pos;
7403 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7411 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7416 key->f_oid = lrn->lrn_first_oid + pos;
7417 obj = lfsck_object_find_bottom(env, lfsck, key);
7420 if (rc == -ENOENT) {
7427 dt_read_lock(env, obj, 0);
7428 if (dt_object_exists(obj) == 0 ||
7429 lfsck_is_dead_obj(obj)) {
7430 dt_read_unlock(env, obj);
7431 lfsck_object_put(env, obj);
7436 rc = dt_attr_get(env, obj, la);
7440 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7442 if (rc == -ENODATA) {
7443 /* For the pre-created OST-object, update the bitmap to avoid
7444 * others LFSCK (second phase) iteration to touch it again. */
7445 if (la->la_ctime == 0) {
7446 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7447 atomic_inc(&lrn->lrn_accessed_count);
7449 /* For the race between repairing dangling referenced
7450 * MDT-object and unlink the file, it may left orphan
7451 * OST-object there. Destroy it now! */
7452 if (unlikely(!(la->la_mode & S_ISUID))) {
7453 dt_read_unlock(env, obj);
7454 lfsck_layout_destroy_orphan(env, lfsck, obj);
7455 lfsck_object_put(env, obj);
7459 } else if (idx == 0) {
7460 /* If the orphan OST-object has no parent information,
7461 * regard it as referenced by the MDT-object on MDT0. */
7462 fid_zero(&rec->lor_rec.lor_fid);
7463 rec->lor_rec.lor_uid = la->la_uid;
7464 rec->lor_rec.lor_gid = la->la_gid;
7465 memset(ol, 0, sizeof(*ol));
7466 rec->lor_layout_version = 0;
7472 dt_read_unlock(env, obj);
7473 lfsck_object_put(env, obj);
7478 if (rc < sizeof(struct lu_fid))
7479 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7481 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7482 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7483 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7484 * parent MDT-object's layout EA. */
7485 save = rec->lor_rec.lor_fid.f_stripe_idx;
7486 rec->lor_rec.lor_fid.f_ver = 0;
7487 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7488 /* If the orphan OST-object does not claim the MDT, then next.
7490 * If we do not know whether it matches or not, then return it
7491 * to the MDT for further check. */
7493 dt_read_unlock(env, obj);
7494 lfsck_object_put(env, obj);
7499 rec->lor_rec.lor_fid.f_stripe_idx = save;
7500 rec->lor_rec.lor_uid = la->la_uid;
7501 rec->lor_rec.lor_gid = la->la_gid;
7502 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7503 rec->lor_layout_version =
7504 le32_to_cpu(ff->ff_layout_version & ~LU_LAYOUT_RESYNC);
7505 rec->lor_range = le32_to_cpu(ff->ff_range);
7507 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7508 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7509 "COMP end %llu, layout version %u, range %u\n",
7510 lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7511 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7512 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7513 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
7514 rec->lor_layout_version, rec->lor_range);
7519 dt_read_unlock(env, obj);
7520 lfsck_object_put(env, obj);
7528 * \retval +1: locate to the exactly position
7529 * \retval 0: cannot locate to the exactly position,
7530 * call next() to move to a valid position.
7531 * \retval -ve: on error
7533 static int lfsck_orphan_it_get(const struct lu_env *env,
7535 const struct dt_key *key)
7537 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7540 it->loi_key = *(struct lu_fid *)key;
7541 rc = lfsck_orphan_it_next(env, di);
7551 static void lfsck_orphan_it_put(const struct lu_env *env,
7556 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7557 const struct dt_it *di)
7559 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7561 return (struct dt_key *)&it->loi_key;
7564 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7565 const struct dt_it *di)
7567 return sizeof(struct lu_fid);
7570 static int lfsck_orphan_it_rec(const struct lu_env *env,
7571 const struct dt_it *di,
7575 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7577 *(struct lu_orphan_rec_v3 *)rec = it->loi_rec;
7582 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7583 const struct dt_it *di)
7585 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7587 return it->loi_hash;
7591 * \retval +1: locate to the exactly position
7592 * \retval 0: cannot locate to the exactly position,
7593 * call next() to move to a valid position.
7594 * \retval -ve: on error
7596 static int lfsck_orphan_it_load(const struct lu_env *env,
7597 const struct dt_it *di,
7600 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7601 struct lfsck_layout_slave_target *llst = it->loi_llst;
7604 LASSERT(llst != NULL);
7606 if (hash != llst->llst_hash) {
7607 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7608 "iteration does not match the one when fini "
7609 "%llu, to be reset.\n",
7610 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7612 fid_zero(&llst->llst_fid);
7613 llst->llst_hash = 0;
7616 it->loi_key = llst->llst_fid;
7617 it->loi_hash = llst->llst_hash;
7618 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7628 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7629 const struct dt_it *di,
7635 static const struct dt_index_operations lfsck_orphan_index_ops = {
7636 .dio_lookup = lfsck_orphan_index_lookup,
7637 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7638 .dio_insert = lfsck_orphan_index_insert,
7639 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7640 .dio_delete = lfsck_orphan_index_delete,
7642 .init = lfsck_orphan_it_init,
7643 .fini = lfsck_orphan_it_fini,
7644 .get = lfsck_orphan_it_get,
7645 .put = lfsck_orphan_it_put,
7646 .next = lfsck_orphan_it_next,
7647 .key = lfsck_orphan_it_key,
7648 .key_size = lfsck_orphan_it_key_size,
7649 .rec = lfsck_orphan_it_rec,
7650 .store = lfsck_orphan_it_store,
7651 .load = lfsck_orphan_it_load,
7652 .key_rec = lfsck_orphan_it_key_rec,