4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, 2017, Intel Corporation.
26 * lustre/lfsck/lfsck_layout.c
28 * Author: Fan, Yong <fan.yong@intel.com>
32 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_LFSCK
36 #include <linux/bitops.h>
37 #include <linux/rbtree.h>
39 #include <lu_object.h>
40 #include <dt_object.h>
41 #include <lustre_fid.h>
42 #include <lustre_lib.h>
43 #include <lustre_net.h>
44 #include <md_object.h>
45 #include <obd_class.h>
47 #include "lfsck_internal.h"
49 #define LFSCK_LAYOUT_MAGIC_V1 0xB173AE14
50 #define LFSCK_LAYOUT_MAGIC_V2 0xB1734D76
51 #define LFSCK_LAYOUT_MAGIC_V3 0xB17371B9
52 #define LFSCK_LAYOUT_MAGIC_V4 0xB1732FED
54 #define LFSCK_LAYOUT_MAGIC LFSCK_LAYOUT_MAGIC_V4
56 struct lfsck_layout_seq {
57 struct list_head lls_list;
60 __u64 lls_lastid_known;
61 struct dt_object *lls_lastid_obj;
62 unsigned int lls_dirty:1;
65 struct lfsck_layout_slave_target {
66 /* link into lfsck_layout_slave_data::llsd_master_list. */
67 struct list_head llst_list;
68 /* The position for next record in the rbtree for iteration. */
69 struct lu_fid llst_fid;
70 /* Dummy hash for iteration against the rbtree. */
75 /* How many times we have failed to get the master status. */
79 struct lfsck_layout_slave_data {
80 /* list for lfsck_layout_seq */
81 struct list_head llsd_seq_list;
83 /* list for the masters involve layout verification. */
84 struct list_head llsd_master_list;
87 struct dt_object *llsd_rb_obj;
88 struct rb_root llsd_rb_root;
89 struct rw_semaphore llsd_rb_rwsem;
90 unsigned int llsd_rbtree_valid:1;
93 struct lfsck_layout_slave_async_args {
94 struct obd_export *llsaa_exp;
95 struct lfsck_component *llsaa_com;
96 struct lfsck_layout_slave_target *llsaa_llst;
99 static inline bool lfsck_comp_extent_aligned(__u64 size)
101 return (size & (LOV_MIN_STRIPE_SIZE - 1)) == 0;
105 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
107 if (atomic_dec_and_test(&llst->llst_ref)) {
108 LASSERT(list_empty(&llst->llst_list));
115 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
117 struct lfsck_layout_slave_target *llst;
118 struct lfsck_layout_slave_target *tmp;
125 INIT_LIST_HEAD(&llst->llst_list);
127 llst->llst_index = index;
128 atomic_set(&llst->llst_ref, 1);
130 spin_lock(&llsd->llsd_lock);
131 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
132 if (tmp->llst_index == index) {
138 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
139 spin_unlock(&llsd->llsd_lock);
148 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
149 struct lfsck_layout_slave_target *llst)
153 spin_lock(&llsd->llsd_lock);
154 if (!list_empty(&llst->llst_list)) {
155 list_del_init(&llst->llst_list);
158 spin_unlock(&llsd->llsd_lock);
161 lfsck_layout_llst_put(llst);
164 static inline struct lfsck_layout_slave_target *
165 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
166 __u32 index, bool unlink)
168 struct lfsck_layout_slave_target *llst;
170 spin_lock(&llsd->llsd_lock);
171 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
172 if (llst->llst_index == index) {
174 list_del_init(&llst->llst_list);
176 atomic_inc(&llst->llst_ref);
177 spin_unlock(&llsd->llsd_lock);
182 spin_unlock(&llsd->llsd_lock);
187 static struct lfsck_layout_req *
188 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
189 struct dt_object *child, __u32 comp_id,
190 __u32 ost_idx, __u32 lov_idx)
192 struct lfsck_layout_req *llr;
196 return ERR_PTR(-ENOMEM);
198 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
199 llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
200 llr->llr_child = child;
201 llr->llr_comp_id = comp_id;
202 llr->llr_ost_idx = ost_idx;
203 llr->llr_lov_idx = lov_idx;
208 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
209 struct lfsck_assistant_req *lar)
211 struct lfsck_layout_req *llr =
212 container_of0(lar, struct lfsck_layout_req, llr_lar);
214 lfsck_object_put(env, llr->llr_child);
215 lfsck_assistant_object_put(env, lar->lar_parent);
220 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
221 struct ptlrpc_request *req,
225 struct lfsck_async_interpret_args *laia = args;
226 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
228 ltd->ltd_synced_failures = 1;
229 atomic_dec(laia->laia_count);
236 * Notify remote LFSCK instances about former failures.
238 * The local LFSCK instance has recorded which OSTs have ever failed to respond
239 * some LFSCK verification requests (maybe because of network issues or the OST
240 * itself trouble). During the respond gap, the OST may missed some OST-objects
241 * verification, then the OST cannot know whether related OST-objects have been
242 * referenced by related MDT-objects or not, then in the second-stage scanning,
243 * these OST-objects will be regarded as orphan, if the OST-object contains bad
244 * parent FID for back reference, then it will misguide the LFSCK to make wrong
245 * fixing for the fake orphan.
247 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
248 * it will scan the bitmap for the ever failed OSTs, and notify them that they
249 * have ever missed some OST-object verification and should skip the handling
250 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
252 * \param[in] env pointer to the thread context
253 * \param[in] com pointer to the lfsck component
254 * \param[in] lr pointer to the lfsck request
256 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
257 struct lfsck_component *com,
258 struct lfsck_request *lr)
260 struct lfsck_async_interpret_args *laia =
261 &lfsck_env_info(env)->lti_laia2;
262 struct lfsck_assistant_data *lad = com->lc_data;
263 struct lfsck_layout *lo = com->lc_file_ram;
264 struct lfsck_instance *lfsck = com->lc_lfsck;
265 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
266 struct lfsck_tgt_desc *ltd;
267 struct ptlrpc_request_set *set;
273 if (!test_bit(LAD_INCOMPLETE, &lad->lad_flags))
276 /* If the MDT has ever failed to verfiy some OST-objects,
277 * then sync failures with them firstly. */
278 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
280 atomic_set(&count, 0);
281 memset(laia, 0, sizeof(*laia));
282 laia->laia_count = &count;
283 set = ptlrpc_prep_set();
285 GOTO(out, rc = -ENOMEM);
287 down_read(<ds->ltd_rw_sem);
288 cfs_foreach_bit(lad->lad_bitmap, idx) {
289 ltd = lfsck_ltd2tgt(ltds, idx);
293 laia->laia_ltd = ltd;
294 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
295 lfsck_layout_assistant_sync_failures_interpret,
298 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
299 "notify target %x for %s phase1 done: "
300 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
301 ltd->ltd_index, lad->lad_name, rc);
308 up_read(<ds->ltd_rw_sem);
310 if (rc == 0 && atomic_read(&count) > 0)
311 rc = ptlrpc_set_wait(env, set);
313 ptlrpc_set_destroy(set);
315 if (rc == 0 && atomic_read(&count) > 0)
322 /* If failed to sync failures with the OSTs, then have to
323 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
324 * subsequent orphan OST-object handling. */
325 lo->ll_flags |= LF_INCOMPLETE;
327 lr->lr_flags2 = lo->ll_flags;
330 static int lfsck_layout_verify_header_v1v3(struct dt_object *obj,
331 struct lov_mds_md_v1 *lmm,
332 __u64 start, __u32 comp_id)
337 magic = le32_to_cpu(lmm->lmm_magic);
338 /* If magic crashed, keep it there. Sometime later, during OST-object
339 * orphan handling, if some OST-object(s) back-point to it, it can be
340 * verified and repaired. */
341 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
344 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
349 CDEBUG(D_LFSCK, "%s LOV EA magic %u for the file "DFID"\n",
350 rc == -EINVAL ? "Unknown" : "Unsupported",
351 magic, PFID(lfsck_dto2fid(obj)));
356 pattern = le32_to_cpu(lmm->lmm_pattern);
359 /* XXX: DoM file verification will be supportted via LU-11081. */
360 if (lov_pattern(pattern) == LOV_PATTERN_MDT) {
362 CDEBUG(D_LFSCK, "The DoM entry for "DFID" is not "
363 "the first component in the mirror %x/%llu\n",
364 PFID(lfsck_dto2fid(obj)), comp_id, start);
371 if (!lov_pattern_supported_normal_comp(lov_pattern(pattern))) {
372 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
373 DFID" in the component %x\n",
374 pattern, PFID(lfsck_dto2fid(obj)), comp_id);
382 static int lfsck_layout_verify_header_foreign(struct dt_object *obj,
383 struct lov_foreign_md *lfm,
386 /* magic has been verified already */
387 __u32 value_len = le32_to_cpu(lfm->lfm_length);
388 /* type and flags are not checked for instance */
390 CDEBUG(D_INFO, "foreign LOV EA, magic %x, len %u, type %x, flags %x, for file "DFID"\n",
391 le32_to_cpu(lfm->lfm_magic), value_len,
392 le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
393 PFID(lfsck_dto2fid(obj)));
395 if (len != value_len + offsetof(typeof(*lfm), lfm_value))
396 CDEBUG(D_LFSCK, "foreign LOV EA internal size %u does not match EA full size %zu for file "DFID"\n",
397 value_len, len, PFID(lfsck_dto2fid(obj)));
399 /* nothing to repair */
403 static int lfsck_layout_verify_header(struct dt_object *obj,
404 struct lov_mds_md_v1 *lmm, size_t len)
408 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
409 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
411 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
413 if (unlikely(count == 0)) {
414 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
415 "components count 0\n",
416 PFID(lfsck_dto2fid(obj)));
421 for (i = 0; i < count && !rc; i++) {
422 struct lov_comp_md_entry_v1 *lcme =
423 &lcm->lcm_entries[i];
424 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
425 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
426 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
428 if (unlikely(comp_id == LCME_ID_INVAL ||
429 comp_id > LCME_ID_MAX)) {
430 CDEBUG(D_LFSCK, "found invalid FPL ID %u "
431 "for the file "DFID" at idx %d\n",
432 comp_id, PFID(lfsck_dto2fid(obj)), i);
437 if (unlikely(start >= end ||
438 !lfsck_comp_extent_aligned(start) ||
439 (!lfsck_comp_extent_aligned(end) &&
440 end != LUSTRE_EOF))) {
441 CDEBUG(D_LFSCK, "found invalid FPL extent "
442 "range [%llu - %llu) for the file "
444 start, end, PFID(lfsck_dto2fid(obj)), i);
449 rc = lfsck_layout_verify_header_v1v3(obj,
450 (struct lov_mds_md_v1 *)((char *)lmm +
451 le32_to_cpu(lcme->lcme_offset)), start,
454 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_FOREIGN) {
455 rc = lfsck_layout_verify_header_foreign(obj,
456 (struct lov_foreign_md *)lmm,
459 rc = lfsck_layout_verify_header_v1v3(obj, lmm, 1, 0);
465 static int lfsck_layout_get_lovea(const struct lu_env *env,
466 struct dt_object *obj, struct lu_buf *buf)
472 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
474 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
476 return !rc ? -ENODATA : rc;
478 lu_buf_realloc(buf, rc);
479 if (buf->lb_buf == NULL)
486 return !rc ? -ENODATA : rc;
488 if (unlikely(buf->lb_buf == NULL)) {
489 lu_buf_alloc(buf, rc);
490 if (buf->lb_buf == NULL)
496 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf, rc);
498 return rc1 ? rc1 : rc;
501 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
502 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
503 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
505 struct lfsck_rbtree_node {
506 struct rb_node lrn_node;
509 atomic_t lrn_known_count;
510 atomic_t lrn_accessed_count;
511 void *lrn_known_bitmap;
512 void *lrn_accessed_bitmap;
515 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
516 __u64 seq, __u32 oid)
518 if (seq < lrn->lrn_seq)
521 if (seq > lrn->lrn_seq)
524 if (oid < lrn->lrn_first_oid)
527 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
533 /* The caller should hold llsd->llsd_rb_lock. */
534 static struct lfsck_rbtree_node *
535 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
536 const struct lu_fid *fid, bool *exact)
538 struct rb_node *node = llsd->llsd_rb_root.rb_node;
539 struct rb_node *prev = NULL;
540 struct lfsck_rbtree_node *lrn = NULL;
546 while (node != NULL) {
548 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
549 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
551 node = node->rb_left;
553 node = node->rb_right;
561 /* If there is no exactly matched one, then to the next valid one. */
564 /* The rbtree is empty. */
571 node = rb_next(prev);
573 /* The end of the rbtree. */
577 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
582 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
583 const struct lu_fid *fid)
585 struct lfsck_rbtree_node *lrn;
589 return ERR_PTR(-ENOMEM);
591 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
592 if (lrn->lrn_known_bitmap == NULL) {
595 return ERR_PTR(-ENOMEM);
598 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
599 if (lrn->lrn_accessed_bitmap == NULL) {
600 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
603 return ERR_PTR(-ENOMEM);
606 RB_CLEAR_NODE(&lrn->lrn_node);
607 lrn->lrn_seq = fid_seq(fid);
608 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
609 atomic_set(&lrn->lrn_known_count, 0);
610 atomic_set(&lrn->lrn_accessed_count, 0);
615 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
617 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
618 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
622 /* The caller should hold lock. */
623 static struct lfsck_rbtree_node *
624 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
625 struct lfsck_rbtree_node *lrn)
627 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
628 struct rb_node *parent = NULL;
629 struct lfsck_rbtree_node *tmp;
632 while (*pos != NULL) {
634 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
635 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
637 pos = &(*pos)->rb_left;
639 pos = &(*pos)->rb_right;
644 rb_link_node(&lrn->lrn_node, parent, pos);
645 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
650 extern const struct dt_index_operations lfsck_orphan_index_ops;
652 static int lfsck_rbtree_setup(const struct lu_env *env,
653 struct lfsck_component *com)
655 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
656 struct lfsck_instance *lfsck = com->lc_lfsck;
657 struct dt_device *dev = lfsck->li_bottom;
658 struct lfsck_layout_slave_data *llsd = com->lc_data;
659 struct dt_object *obj;
661 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
662 fid->f_oid = lfsck_dev_idx(lfsck);
664 obj = dt_locate(env, dev, fid);
666 RETURN(PTR_ERR(obj));
668 /* Generate an in-RAM object to stand for the layout rbtree.
669 * Scanning the layout rbtree will be via the iteration over
670 * the object. In the future, the rbtree may be written onto
671 * disk with the object.
673 * Mark the object to be as exist. */
674 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
675 obj->do_index_ops = &lfsck_orphan_index_ops;
676 llsd->llsd_rb_obj = obj;
677 llsd->llsd_rbtree_valid = 1;
678 dev->dd_record_fid_accessed = 1;
680 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
681 lfsck_lfsck2name(lfsck));
686 static void lfsck_rbtree_cleanup(const struct lu_env *env,
687 struct lfsck_component *com)
689 struct lfsck_instance *lfsck = com->lc_lfsck;
690 struct lfsck_layout_slave_data *llsd = com->lc_data;
691 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
692 struct rb_node *next;
693 struct lfsck_rbtree_node *lrn;
695 lfsck->li_bottom->dd_record_fid_accessed = 0;
696 /* Invalid the rbtree, then no others will use it. */
697 down_write(&llsd->llsd_rb_rwsem);
698 llsd->llsd_rbtree_valid = 0;
699 up_write(&llsd->llsd_rb_rwsem);
701 while (node != NULL) {
702 next = rb_next(node);
703 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
704 rb_erase(node, &llsd->llsd_rb_root);
705 lfsck_rbtree_free(lrn);
709 if (llsd->llsd_rb_obj != NULL) {
710 lfsck_object_put(env, llsd->llsd_rb_obj);
711 llsd->llsd_rb_obj = NULL;
714 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
715 lfsck_lfsck2name(lfsck));
718 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
719 struct lfsck_component *com,
720 const struct lu_fid *fid,
723 struct lfsck_layout_slave_data *llsd = com->lc_data;
724 struct lfsck_rbtree_node *lrn;
730 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
733 if (!fid_is_idif(fid) && !fid_is_norm(fid))
736 down_read(&llsd->llsd_rb_rwsem);
737 if (!llsd->llsd_rbtree_valid)
738 GOTO(unlock, rc = 0);
740 lrn = lfsck_rbtree_search(llsd, fid, NULL);
742 struct lfsck_rbtree_node *tmp;
746 up_read(&llsd->llsd_rb_rwsem);
747 tmp = lfsck_rbtree_new(env, fid);
749 GOTO(out, rc = PTR_ERR(tmp));
752 down_write(&llsd->llsd_rb_rwsem);
753 if (!llsd->llsd_rbtree_valid) {
754 lfsck_rbtree_free(tmp);
755 GOTO(unlock, rc = 0);
758 lrn = lfsck_rbtree_insert(llsd, tmp);
760 lfsck_rbtree_free(tmp);
763 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
764 /* Any accessed object must be a known object. */
765 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
766 atomic_inc(&lrn->lrn_known_count);
767 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
768 atomic_inc(&lrn->lrn_accessed_count);
770 GOTO(unlock, rc = 0);
774 up_write(&llsd->llsd_rb_rwsem);
776 up_read(&llsd->llsd_rb_rwsem);
778 if (rc != 0 && accessed) {
779 struct lfsck_layout *lo = com->lc_file_ram;
781 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
782 "bitmap, and will cause incorrect LFSCK OST-object "
783 "handling, so disable it to cancel orphan handling "
784 "for related device. rc = %d\n",
785 lfsck_lfsck2name(com->lc_lfsck), rc);
787 lo->ll_flags |= LF_INCOMPLETE;
788 lfsck_rbtree_cleanup(env, com);
792 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
793 const struct lfsck_layout_dangling_key *src)
795 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
796 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
797 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
800 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
801 const struct lfsck_layout_dangling_key *src)
803 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
804 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
805 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
808 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
809 const struct lfsck_layout_dangling_key *src)
811 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
812 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
813 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
816 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
817 const struct lfsck_layout_dangling_key *src)
819 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
820 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
821 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
824 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
825 const struct lfsck_layout *src)
829 des->ll_magic = le32_to_cpu(src->ll_magic);
830 des->ll_status = le32_to_cpu(src->ll_status);
831 des->ll_flags = le32_to_cpu(src->ll_flags);
832 des->ll_success_count = le32_to_cpu(src->ll_success_count);
833 des->ll_run_time_phase1 = le64_to_cpu(src->ll_run_time_phase1);
834 des->ll_run_time_phase2 = le64_to_cpu(src->ll_run_time_phase2);
835 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
836 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
837 des->ll_time_last_checkpoint =
838 le64_to_cpu(src->ll_time_last_checkpoint);
839 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
840 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
841 des->ll_pos_first_inconsistent =
842 le64_to_cpu(src->ll_pos_first_inconsistent);
843 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
844 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
845 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
846 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
847 for (i = 0; i < LLIT_MAX; i++)
848 des->ll_objs_repaired[i] =
849 le64_to_cpu(src->ll_objs_repaired[i]);
850 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
851 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
852 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
853 &src->ll_lldk_latest_scanned_phase2);
856 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
857 const struct lfsck_layout *src)
861 des->ll_magic = cpu_to_le32(src->ll_magic);
862 des->ll_status = cpu_to_le32(src->ll_status);
863 des->ll_flags = cpu_to_le32(src->ll_flags);
864 des->ll_success_count = cpu_to_le32(src->ll_success_count);
865 des->ll_run_time_phase1 = cpu_to_le64(src->ll_run_time_phase1);
866 des->ll_run_time_phase2 = cpu_to_le64(src->ll_run_time_phase2);
867 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
868 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
869 des->ll_time_last_checkpoint =
870 cpu_to_le64(src->ll_time_last_checkpoint);
871 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
872 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
873 des->ll_pos_first_inconsistent =
874 cpu_to_le64(src->ll_pos_first_inconsistent);
875 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
876 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
877 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
878 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
879 for (i = 0; i < LLIT_MAX; i++)
880 des->ll_objs_repaired[i] =
881 cpu_to_le64(src->ll_objs_repaired[i]);
882 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
883 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
884 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
885 &src->ll_lldk_latest_scanned_phase2);
889 * Load the OST bitmap from the lfsck_layout trace file.
891 * \param[in] env pointer to the thread context
892 * \param[in] com pointer to the lfsck component
894 * \retval 0 for success
895 * \retval negative error number on failure or data corruption
897 static int lfsck_layout_load_bitmap(const struct lu_env *env,
898 struct lfsck_component *com)
900 struct dt_object *obj = com->lc_obj;
901 struct lfsck_assistant_data *lad = com->lc_data;
902 struct lfsck_layout *lo = com->lc_file_ram;
903 struct cfs_bitmap *bitmap = lad->lad_bitmap;
904 loff_t pos = com->lc_file_size;
910 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
912 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
914 nbits = lo->ll_bitmap_size;
916 if (unlikely(nbits < BITS_PER_LONG))
917 nbits = BITS_PER_LONG;
919 if (nbits > bitmap->size) {
920 __u32 new_bits = bitmap->size;
921 struct cfs_bitmap *new_bitmap;
923 while (new_bits < nbits)
926 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
927 if (new_bitmap == NULL)
930 lad->lad_bitmap = new_bitmap;
931 CFS_FREE_BITMAP(bitmap);
935 if (lo->ll_bitmap_size == 0) {
936 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
937 CFS_RESET_BITMAP(bitmap);
942 size = (lo->ll_bitmap_size + 7) >> 3;
943 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
945 RETURN(rc >= 0 ? -EINVAL : rc);
947 if (cfs_bitmap_check_empty(bitmap))
948 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
950 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
956 * Load the layout LFSCK trace file from disk.
958 * The layout LFSCK trace file records the layout LFSCK status information
959 * and other statistics, such as how many objects have been scanned, and how
960 * many objects have been repaired, and etc. It also contains the bitmap for
961 * failed OSTs during the layout LFSCK. All these information will be loaded
962 * from disk to RAM when the layout LFSCK component setup.
964 * \param[in] env pointer to the thread context
965 * \param[in] com pointer to the lfsck component
967 * \retval positive number for file data corruption, the caller
968 * should reset the layout LFSCK trace file
969 * \retval 0 for success
970 * \retval negative error number on failure
972 static int lfsck_layout_load(const struct lu_env *env,
973 struct lfsck_component *com)
975 struct lfsck_layout *lo = com->lc_file_ram;
976 ssize_t size = com->lc_file_size;
980 rc = dt_read(env, com->lc_obj,
981 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
985 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
986 lfsck_lfsck2name(com->lc_lfsck), rc);
988 } else if (rc != size) {
989 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
990 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
994 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
995 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
996 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
997 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
998 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
1006 * Store the layout LFSCK trace file on disk.
1008 * The layout LFSCK trace file records the layout LFSCK status information
1009 * and other statistics, such as how many objects have been scanned, and how
1010 * many objects have been repaired, and etc. It also contains the bitmap for
1011 * failed OSTs during the layout LFSCK. All these information will be synced
1012 * from RAM to disk periodically.
1014 * \param[in] env pointer to the thread context
1015 * \param[in] com pointer to the lfsck component
1017 * \retval 0 for success
1018 * \retval negative error number on failure
1020 static int lfsck_layout_store(const struct lu_env *env,
1021 struct lfsck_component *com)
1023 struct dt_object *obj = com->lc_obj;
1024 struct lfsck_instance *lfsck = com->lc_lfsck;
1025 struct lfsck_layout *lo_ram = com->lc_file_ram;
1026 struct lfsck_layout *lo = com->lc_file_disk;
1028 struct dt_device *dev = lfsck_obj2dev(obj);
1029 struct cfs_bitmap *bitmap = NULL;
1031 ssize_t size = com->lc_file_size;
1036 if (lfsck->li_master) {
1037 struct lfsck_assistant_data *lad = com->lc_data;
1039 bitmap = lad->lad_bitmap;
1040 nbits = bitmap->size;
1043 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1046 lo_ram->ll_bitmap_size = nbits;
1047 lfsck_layout_cpu_to_le(lo, lo_ram);
1048 th = dt_trans_create(env, dev);
1050 GOTO(log, rc = PTR_ERR(th));
1052 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1057 if (bitmap != NULL) {
1058 rc = dt_declare_record_write(env, obj,
1059 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1065 rc = dt_trans_start_local(env, dev, th);
1070 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1074 if (bitmap != NULL) {
1076 rc = dt_record_write(env, obj,
1077 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1084 dt_trans_stop(env, dev, th);
1088 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1089 lfsck_lfsck2name(lfsck), rc);
1094 static int lfsck_layout_init(const struct lu_env *env,
1095 struct lfsck_component *com)
1097 struct lfsck_layout *lo = com->lc_file_ram;
1100 memset(lo, 0, com->lc_file_size);
1101 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1102 lo->ll_status = LS_INIT;
1103 down_write(&com->lc_sem);
1104 rc = lfsck_layout_store(env, com);
1105 if (rc == 0 && com->lc_lfsck->li_master)
1106 rc = lfsck_load_sub_trace_files(env, com,
1107 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1108 up_write(&com->lc_sem);
1113 static int fid_is_for_ostobj(const struct lu_env *env,
1114 struct lfsck_instance *lfsck,
1115 struct dt_object *obj, const struct lu_fid *fid)
1117 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1118 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1119 struct lustre_ost_attrs *loa;
1122 fld_range_set_any(range);
1123 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1125 if (fld_range_is_ost(range))
1131 loa = &lfsck_env_info(env)->lti_loa;
1132 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1134 if (rc >= (int)sizeof(struct lustre_mdt_attrs)) {
1135 lustre_lma_swab(&loa->loa_lma);
1137 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1140 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1145 static struct lfsck_layout_seq *
1146 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1148 struct lfsck_layout_seq *lls;
1150 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1151 if (lls->lls_seq == seq)
1154 if (lls->lls_seq > seq)
1162 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1163 struct lfsck_layout_seq *lls)
1165 struct lfsck_layout_seq *tmp;
1166 struct list_head *pos = &llsd->llsd_seq_list;
1168 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1169 if (lls->lls_seq < tmp->lls_seq) {
1170 pos = &tmp->lls_list;
1174 list_add_tail(&lls->lls_list, pos);
1178 lfsck_layout_lastid_create(const struct lu_env *env,
1179 struct lfsck_instance *lfsck,
1180 struct dt_object *obj)
1182 struct lfsck_thread_info *info = lfsck_env_info(env);
1183 struct lu_attr *la = &info->lti_la;
1184 struct dt_object_format *dof = &info->lti_dof;
1185 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1186 struct dt_device *dt = lfsck_obj2dev(obj);
1193 if (bk->lb_param & LPF_DRYRUN)
1196 memset(la, 0, sizeof(*la));
1197 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1198 la->la_valid = LA_MODE | LA_UID | LA_GID;
1199 memset(dof, 0, sizeof(*dof));
1200 dof->dof_type = dt_mode_to_dft(S_IFREG);
1202 th = dt_trans_create(env, dt);
1204 GOTO(log, rc = PTR_ERR(th));
1206 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1210 rc = dt_declare_record_write(env, obj,
1211 lfsck_buf_get(env, &lastid,
1217 rc = dt_trans_start_local(env, dt, th);
1221 dt_write_lock(env, obj, 0);
1222 if (likely(dt_object_exists(obj) == 0)) {
1223 rc = dt_create(env, obj, la, NULL, dof, th);
1225 rc = dt_record_write(env, obj,
1226 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1229 dt_write_unlock(env, obj);
1234 dt_trans_stop(env, dt, th);
1237 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1239 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1245 lfsck_layout_lastid_reload(const struct lu_env *env,
1246 struct lfsck_component *com,
1247 struct lfsck_layout_seq *lls)
1253 dt_read_lock(env, lls->lls_lastid_obj, 0);
1254 rc = dt_record_read(env, lls->lls_lastid_obj,
1255 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1256 dt_read_unlock(env, lls->lls_lastid_obj);
1257 if (unlikely(rc != 0))
1260 lastid = le64_to_cpu(lastid);
1261 if (lastid < lls->lls_lastid_known) {
1262 struct lfsck_instance *lfsck = com->lc_lfsck;
1263 struct lfsck_layout *lo = com->lc_file_ram;
1265 lls->lls_lastid = lls->lls_lastid_known;
1267 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1268 LASSERT(lfsck->li_out_notify != NULL);
1270 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1271 LE_LASTID_REBUILDING);
1272 lo->ll_flags |= LF_CRASHED_LASTID;
1274 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1275 "LAST_ID file (1) for the sequence %#llx"
1276 ", old value %llu, known value %llu\n",
1277 lfsck_lfsck2name(lfsck), lls->lls_seq,
1278 lastid, lls->lls_lastid);
1280 } else if (lastid >= lls->lls_lastid) {
1281 lls->lls_lastid = lastid;
1289 lfsck_layout_lastid_store(const struct lu_env *env,
1290 struct lfsck_component *com)
1292 struct lfsck_instance *lfsck = com->lc_lfsck;
1293 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1294 struct dt_device *dt = lfsck->li_bottom;
1295 struct lfsck_layout_slave_data *llsd = com->lc_data;
1296 struct lfsck_layout_seq *lls;
1302 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1305 if (!lls->lls_dirty)
1308 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1309 "<seq> %#llx as <oid> %llu\n",
1310 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1312 if (bk->lb_param & LPF_DRYRUN) {
1317 th = dt_trans_create(env, dt);
1320 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1321 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1322 lfsck_lfsck2name(com->lc_lfsck),
1327 lastid = cpu_to_le64(lls->lls_lastid);
1328 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1329 lfsck_buf_get(env, &lastid,
1335 rc = dt_trans_start_local(env, dt, th);
1339 dt_write_lock(env, lls->lls_lastid_obj, 0);
1340 rc = dt_record_write(env, lls->lls_lastid_obj,
1341 lfsck_buf_get(env, &lastid,
1342 sizeof(lastid)), &pos, th);
1343 dt_write_unlock(env, lls->lls_lastid_obj);
1348 dt_trans_stop(env, dt, th);
1351 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1352 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1353 lfsck_lfsck2name(com->lc_lfsck),
1362 lfsck_layout_lastid_load(const struct lu_env *env,
1363 struct lfsck_component *com,
1364 struct lfsck_layout_seq *lls)
1366 struct lfsck_instance *lfsck = com->lc_lfsck;
1367 struct lfsck_layout *lo = com->lc_file_ram;
1368 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1369 struct dt_object *obj;
1374 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1375 obj = dt_locate(env, lfsck->li_bottom, fid);
1377 RETURN(PTR_ERR(obj));
1379 /* LAST_ID crashed, to be rebuilt */
1380 if (dt_object_exists(obj) == 0) {
1381 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1382 LASSERT(lfsck->li_out_notify != NULL);
1384 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1385 LE_LASTID_REBUILDING);
1386 lo->ll_flags |= LF_CRASHED_LASTID;
1388 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1389 "LAST_ID file for sequence %#llx\n",
1390 lfsck_lfsck2name(lfsck), lls->lls_seq);
1392 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1394 struct l_wait_info lwi = LWI_TIMEOUT(
1395 cfs_time_seconds(cfs_fail_val),
1398 /* Some others may changed the cfs_fail_val
1399 * as zero after above check, re-check it for
1400 * sure to avoid falling into wait for ever. */
1401 if (likely(lwi.lwi_timeout > 0)) {
1402 struct ptlrpc_thread *thread =
1405 up_write(&com->lc_sem);
1406 l_wait_event(thread->t_ctl_waitq,
1407 !thread_is_running(thread),
1409 down_write(&com->lc_sem);
1414 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1416 dt_read_lock(env, obj, 0);
1417 rc = dt_read(env, obj,
1418 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1420 dt_read_unlock(env, obj);
1421 if (rc != 0 && rc != sizeof(__u64))
1422 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1424 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1425 LASSERT(lfsck->li_out_notify != NULL);
1427 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1428 LE_LASTID_REBUILDING);
1429 lo->ll_flags |= LF_CRASHED_LASTID;
1431 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1432 "LAST_ID file for the sequence %#llx"
1434 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1437 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1445 lfsck_object_put(env, obj);
1447 lls->lls_lastid_obj = obj;
1452 static void lfsck_layout_record_failure(const struct lu_env *env,
1453 struct lfsck_instance *lfsck,
1454 struct lfsck_layout *lo)
1458 lo->ll_objs_failed_phase1++;
1459 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1461 if (lo->ll_pos_first_inconsistent == 0 ||
1462 lo->ll_pos_first_inconsistent < cookie) {
1463 lo->ll_pos_first_inconsistent = cookie;
1465 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1466 "inconsistency at the pos [%llu]\n",
1467 lfsck_lfsck2name(lfsck),
1468 lo->ll_pos_first_inconsistent);
1472 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1473 struct lfsck_component *com,
1476 struct lfsck_instance *lfsck = com->lc_lfsck;
1477 struct lfsck_layout *lo = com->lc_file_ram;
1479 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan: rc = %d\n",
1480 lfsck_lfsck2name(lfsck), rc);
1482 down_write(&com->lc_sem);
1483 lo->ll_run_time_phase2 += ktime_get_seconds() -
1484 com->lc_time_last_checkpoint;
1485 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
1486 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1489 if (lo->ll_flags & LF_INCOMPLETE) {
1490 lo->ll_status = LS_PARTIAL;
1492 if (lfsck->li_master) {
1493 struct lfsck_assistant_data *lad = com->lc_data;
1495 if (test_bit(LAD_INCOMPLETE, &lad->lad_flags))
1496 lo->ll_status = LS_PARTIAL;
1498 lo->ll_status = LS_COMPLETED;
1500 lo->ll_status = LS_COMPLETED;
1503 lo->ll_flags &= ~LF_SCANNED_ONCE;
1504 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1505 lo->ll_flags &= ~LF_INCONSISTENT;
1506 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1507 lo->ll_success_count++;
1508 } else if (rc == 0) {
1509 if (lfsck->li_status != 0)
1510 lo->ll_status = lfsck->li_status;
1512 lo->ll_status = LS_STOPPED;
1514 lo->ll_status = LS_FAILED;
1517 rc = lfsck_layout_store(env, com);
1518 up_write(&com->lc_sem);
1520 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan result %u: rc = %d\n",
1521 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
1526 static int lfsck_layout_trans_stop(const struct lu_env *env,
1527 struct dt_device *dev,
1528 struct thandle *handle, int result)
1532 /* XXX: If there is something worng or it needs to repair nothing,
1533 * then notify the lower to stop the modification. Currently,
1534 * we use th_result for such purpose, that may be replaced by
1535 * some rollback mechanism in the future. */
1536 handle->th_result = result;
1537 rc = dt_trans_stop(env, dev, handle);
1539 return result > 0 ? 0 : result;
1541 return rc == 0 ? 1 : rc;
1544 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1545 struct lfsck_component *com,
1546 const struct lu_fid *pfid,
1547 const struct lu_fid *cfid,
1548 __u32 comp_id, __u32 ea_off,
1551 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1552 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1553 struct dt_device *dev;
1554 struct dt_object *obj;
1555 struct thandle *th = NULL;
1560 idx = lfsck_sub_trace_file_fid2idx(pfid);
1561 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1562 dev = lfsck_obj2dev(obj);
1564 fid_cpu_to_be(&key->lldk_fid, pfid);
1565 key->lldk_comp_id = cpu_to_be32(comp_id);
1566 key->lldk_ea_off = cpu_to_be32(ea_off);
1568 fid_cpu_to_be(rec, cfid);
1569 rec->f_ver = cpu_to_be32(ost_idx);
1571 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1573 th = dt_trans_create(env, dev);
1575 GOTO(unlock, rc = PTR_ERR(th));
1577 rc = dt_declare_insert(env, obj,
1578 (const struct dt_rec *)rec,
1579 (const struct dt_key *)key, th);
1583 rc = dt_trans_start_local(env, dev, th);
1587 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1588 (const struct dt_key *)key, th);
1593 if (th && !IS_ERR(th))
1594 dt_trans_stop(env, dev, th);
1596 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1598 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1599 "ea_off = %u, ost_idx = %u, into the trace file for further "
1600 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1601 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1606 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1607 struct lfsck_component *com,
1608 const struct lu_fid *fid,
1609 __u32 comp_id, __u32 ea_off)
1611 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1612 struct dt_device *dev;
1613 struct dt_object *obj;
1614 struct thandle *th = NULL;
1619 idx = lfsck_sub_trace_file_fid2idx(fid);
1620 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1621 dev = lfsck_obj2dev(obj);
1623 fid_cpu_to_be(&key->lldk_fid, fid);
1624 key->lldk_comp_id = cpu_to_be32(comp_id);
1625 key->lldk_ea_off = cpu_to_be32(ea_off);
1627 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1629 th = dt_trans_create(env, dev);
1631 GOTO(unlock, rc = PTR_ERR(th));
1633 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1637 rc = dt_trans_start_local(env, dev, th);
1641 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1646 if (th && !IS_ERR(th))
1647 dt_trans_stop(env, dev, th);
1649 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1651 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1652 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1653 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1659 * Get the system default stripe size.
1661 * \param[in] env pointer to the thread context
1662 * \param[in] lfsck pointer to the lfsck instance
1663 * \param[out] size pointer to the default stripe size
1665 * \retval 0 for success
1666 * \retval negative error number on failure
1668 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1669 struct lfsck_instance *lfsck,
1672 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1673 struct dt_object *root;
1676 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1678 return PTR_ERR(root);
1680 /* Get the default stripe size via xattr_get on the backend root. */
1681 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1684 /* The lum->lmm_stripe_size is LE mode. The *size also
1685 * should be LE mode. So it is unnecessary to convert. */
1686 *size = lum->lmm_stripe_size;
1688 } else if (unlikely(rc == 0)) {
1692 lfsck_object_put(env, root);
1698 * \retval +1: repaired
1699 * \retval 0: did nothing
1700 * \retval -ve: on error
1702 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1703 struct lfsck_instance *lfsck,
1704 struct thandle *handle,
1705 struct dt_object *parent,
1706 const struct lu_fid *cfid,
1708 struct lov_mds_md_v1 *lmm,
1709 struct lov_ost_data_v1 *slot,
1710 int fl, __u32 ost_idx, int size)
1712 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1713 struct lu_buf ea_buf;
1720 magic = le32_to_cpu(lmm->lmm_magic);
1721 pattern = le32_to_cpu(lmm->lmm_pattern);
1722 count = le16_to_cpu(lmm->lmm_stripe_count);
1724 fid_to_ostid(cfid, oi);
1725 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1726 slot->l_ost_gen = cpu_to_le32(0);
1727 slot->l_ost_idx = cpu_to_le32(ost_idx);
1729 if (pattern & LOV_PATTERN_F_HOLE) {
1730 struct lov_ost_data_v1 *objs;
1733 if (magic == LOV_MAGIC_V1)
1734 objs = &lmm->lmm_objects[0];
1736 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1737 for (i = 0; i < count; i++, objs++) {
1738 if (lovea_slot_is_dummy(objs))
1742 /* If the @slot is the last dummy slot to be refilled,
1743 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1746 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1748 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1749 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1750 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1754 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1755 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1762 static struct lov_ost_data_v1 *
1763 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1764 const struct lu_fid *pfid,
1765 __u32 stripe_size, __u32 ea_off,
1766 __u32 pattern, __u16 count)
1768 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1769 lmm->lmm_pattern = cpu_to_le32(pattern);
1770 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1771 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1772 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1773 lmm->lmm_stripe_count = cpu_to_le16(count);
1774 lmm->lmm_layout_gen = cpu_to_le16(1);
1775 memset(&lmm->lmm_objects[0], 0,
1776 sizeof(struct lov_ost_data_v1) * count);
1778 return &lmm->lmm_objects[ea_off];
1781 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1782 struct lfsck_instance *lfsck,
1783 struct ost_layout *ol,
1784 struct dt_object *parent,
1785 struct lu_buf *buf, __u32 ea_off,
1786 struct lov_mds_md_v1 **lmm,
1787 struct lov_ost_data_v1 **objs)
1790 __u32 stripe_size = ol->ol_stripe_size;
1791 __u32 pattern = LOV_PATTERN_RAID0;
1794 if (ol->ol_stripe_count != 0)
1795 count = ol->ol_stripe_count;
1799 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1800 LASSERTF(buf->lb_len >= size,
1801 "buffer len %d is less than real size %d\n",
1802 (int)buf->lb_len, size);
1804 if (stripe_size == 0) {
1807 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1813 if (ol->ol_stripe_count > 1 ||
1814 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1815 pattern |= LOV_PATTERN_F_HOLE;
1816 memset(&(*lmm)->lmm_objects[0], 0,
1817 count * sizeof(struct lov_ost_data_v1));
1820 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1821 stripe_size, ea_off, pattern, count);
1826 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1827 struct lu_orphan_rec_v3 *rec,
1828 struct dt_object *parent,
1829 struct lu_buf *buf, __u32 ea_off,
1830 struct lov_mds_md_v1 **lmm,
1831 struct lov_ost_data_v1 **objs)
1833 struct ost_layout *ol = &rec->lor_layout;
1834 struct lov_comp_md_v1 *lcm;
1835 struct lov_comp_md_entry_v1 *lcme;
1836 __u32 pattern = LOV_PATTERN_RAID0;
1837 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1838 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1839 int size = offset + lcme_size;
1841 LASSERTF(buf->lb_len >= size,
1842 "buffer len %d is less than real size %d\n",
1843 (int)buf->lb_len, size);
1846 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1847 lcm->lcm_size = cpu_to_le32(size);
1848 if (rec->lor_range) {
1849 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1851 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1852 } else if (rec->lor_layout_version) {
1853 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1855 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1857 lcm->lcm_layout_gen = cpu_to_le32(1);
1858 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1860 lcm->lcm_entry_count = cpu_to_le16(1);
1861 /* Currently, we do not know how many mirrors will be, set it as zero
1862 * at the beginning. It will be updated when more mirrors are found. */
1863 lcm->lcm_mirror_count = 0;
1865 lcme = &lcm->lcm_entries[0];
1866 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1867 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1868 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1869 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1870 lcme->lcme_offset = cpu_to_le32(offset);
1871 lcme->lcme_size = cpu_to_le32(lcme_size);
1872 lcme->lcme_layout_gen = lcm->lcm_layout_gen;
1873 if (ol->ol_stripe_count > 1)
1874 pattern |= LOV_PATTERN_F_HOLE;
1876 *lmm = buf->lb_buf + offset;
1877 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1878 ol->ol_stripe_size, ea_off,
1879 pattern, ol->ol_stripe_count);
1884 static void lfsck_layout_update_lcm(struct lov_comp_md_v1 *lcm,
1885 struct lov_comp_md_entry_v1 *lcme,
1886 __u32 version, __u32 range)
1888 struct lov_comp_md_entry_v1 *tmp;
1889 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
1890 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
1891 __u32 gen = version + range;
1894 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1895 __u16 flags = le16_to_cpu(lcm->lcm_flags);
1899 lcme->lcme_layout_gen = cpu_to_le32(gen);
1900 if (le32_to_cpu(lcm->lcm_layout_gen) < gen)
1901 lcm->lcm_layout_gen = cpu_to_le32(gen);
1904 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1905 else if (flags == LCM_FL_NONE && le16_to_cpu(lcm->lcm_mirror_count) > 0)
1906 lcm->lcm_flags = cpu_to_le16(LCM_FL_RDONLY);
1908 for (i = 0; i < count; i++) {
1909 tmp = &lcm->lcm_entries[i];
1910 if (le64_to_cpu(tmp->lcme_extent.e_end) <= start)
1913 if (le64_to_cpu(tmp->lcme_extent.e_start) >= end)
1916 if (le32_to_cpu(tmp->lcme_flags) & LCME_FL_STALE)
1919 tmp_gen = le32_to_cpu(tmp->lcme_layout_gen);
1920 /* "lcme_layout_gen == 0" but without LCME_FL_STALE flag,
1921 * then it should be the latest version of all mirrors. */
1922 if (tmp_gen == 0 || tmp_gen > gen) {
1923 lcme->lcme_flags = cpu_to_le32(
1924 le32_to_cpu(lcme->lcme_flags) | LCME_FL_STALE);
1929 tmp->lcme_flags = cpu_to_le32(
1930 le32_to_cpu(tmp->lcme_flags) | LCME_FL_STALE);
1934 static int lfsck_layout_add_comp(const struct lu_env *env,
1935 struct lfsck_instance *lfsck,
1936 struct thandle *handle,
1937 struct lu_orphan_rec_v3 *rec,
1938 struct dt_object *parent,
1939 const struct lu_fid *cfid,
1940 struct lu_buf *buf, __u32 ost_idx,
1941 __u32 ea_off, int pos, bool new_mirror)
1943 struct ost_layout *ol = &rec->lor_layout;
1944 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1945 struct lov_comp_md_entry_v1 *lcme;
1946 struct lov_mds_md_v1 *lmm;
1947 struct lov_ost_data_v1 *objs;
1948 int added = sizeof(*lcme) +
1949 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1950 int size = le32_to_cpu(lcm->lcm_size) + added;
1954 __u32 pattern = LOV_PATTERN_RAID0;
1955 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1958 lu_buf_check_and_grow(buf, size);
1959 /* set the lcm again because lu_buf_check_and_grow() may
1960 * have reallocated the buf. */
1962 lcm->lcm_size = cpu_to_le32(size);
1963 lcm->lcm_entry_count = cpu_to_le16(count + 1);
1965 le16_add_cpu(&lcm->lcm_mirror_count, 1);
1967 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
1968 * with distance of 'added'. */
1971 for (i = pos; i < count; i++) {
1972 lcme = &lcm->lcm_entries[i];
1973 size += le32_to_cpu(lcme->lcme_size);
1976 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
1977 memmove(buf->lb_buf + offset + added,
1978 buf->lb_buf + offset, size);
1982 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
1983 * of 'sizeof(struct lov_comp_md_entry_v1)' */
1985 for (i = 0; i < pos; i++) {
1986 lcme = &lcm->lcm_entries[i];
1987 size += le32_to_cpu(lcme->lcme_size);
1990 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
1991 memmove(buf->lb_buf + offset + sizeof(*lcme),
1992 buf->lb_buf + offset, size);
1995 /* 3. Recalculate the enter offset for the component [pos, count-1] */
1996 for (i = count - 1; i >= pos; i--) {
1997 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
1998 lcm->lcm_entries[i + 1].lcme_offset =
1999 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
2000 lcme_offset) + added);
2003 /* 4. Recalculate the enter offset for the component [0, pos) */
2004 for (i = 0; i < pos; i++) {
2005 lcm->lcm_entries[i].lcme_offset =
2006 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
2007 lcme_offset) + sizeof(*lcme));
2010 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
2011 /* 4. Insert the new component header (entry) at the slot 'pos'. */
2012 lcme = &lcm->lcm_entries[pos];
2013 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
2014 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2015 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
2016 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
2017 lcme->lcme_offset = cpu_to_le32(offset);
2018 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
2021 if (ol->ol_stripe_count > 1)
2022 pattern |= LOV_PATTERN_F_HOLE;
2024 lmm = buf->lb_buf + offset;
2025 /* 5. Insert teh new component body at the 'offset'. */
2026 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
2027 ol->ol_stripe_size, ea_off,
2028 pattern, ol->ol_stripe_count);
2030 /* 6. Update mirror related flags and version. */
2031 lfsck_layout_update_lcm(lcm, lcme, rec->lor_layout_version,
2034 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2035 lmm, objs, LU_XATTR_REPLACE, ost_idx,
2036 le32_to_cpu(lcm->lcm_size));
2038 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
2039 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2040 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2041 "comp_end %llu, layout version %u, range %u, "
2042 "%s LOV EA hole: rc = %d\n",
2043 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2044 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2045 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2046 rec->lor_layout_version, rec->lor_range,
2047 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2048 "with" : "without", rc);
2053 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
2054 struct lfsck_instance *lfsck,
2055 struct thandle *handle,
2056 struct ost_layout *ol,
2057 struct dt_object *parent,
2058 const struct lu_fid *cfid,
2059 struct lu_buf *buf, __u32 ost_idx,
2062 struct lov_mds_md_v1 *lmm = buf->lb_buf;
2063 struct lov_ost_data_v1 *objs;
2064 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
2065 __u32 magic = le32_to_cpu(lmm->lmm_magic);
2071 /* The original LOVEA maybe re-generated via old filter_fid, at
2072 * that time, we do not know the stripe count and stripe size. */
2073 if (ol->ol_stripe_count > count)
2074 count = ol->ol_stripe_count;
2075 if (ol->ol_stripe_size != 0 &&
2076 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
2077 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
2079 if (magic == LOV_MAGIC_V1)
2080 objs = &lmm->lmm_objects[count];
2082 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
2084 gap = ea_off - count;
2088 size = lov_mds_md_size(count, magic);
2089 LASSERTF(buf->lb_len >= size,
2090 "buffer len %d is less than real size %d\n",
2091 (int)buf->lb_len, size);
2094 memset(objs, 0, gap * sizeof(*objs));
2095 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
2098 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2099 lmm->lmm_stripe_count = cpu_to_le16(count);
2102 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2103 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
2105 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
2106 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2107 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2108 "comp_end %llu, %s LOV EA hole: rc = %d\n",
2109 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2110 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2111 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2112 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2113 "with" : "without", rc);
2119 * \retval +1: repaired
2120 * \retval 0: did nothing
2121 * \retval -ve: on error
2123 static int lfsck_layout_update_lovea(const struct lu_env *env,
2124 struct lfsck_instance *lfsck,
2125 struct thandle *handle,
2126 struct lu_orphan_rec_v3 *rec,
2127 struct dt_object *parent,
2128 const struct lu_fid *cfid,
2129 struct lu_buf *buf, int fl,
2130 __u32 ost_idx, __u32 ea_off)
2132 struct ost_layout *ol = &rec->lor_layout;
2133 struct lov_mds_md_v1 *lmm = NULL;
2134 struct lov_ost_data_v1 *objs = NULL;
2138 if (ol->ol_comp_id != 0)
2139 rc = lfsck_layout_new_comp_lovea(env, rec, parent, buf, ea_off,
2142 rc = lfsck_layout_new_v1_lovea(env, lfsck, &rec->lor_layout,
2143 parent, buf, ea_off, &lmm,
2146 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2147 buf, lmm, objs, fl, ost_idx, rc);
2149 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2150 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2151 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2152 "comp_end %llu, layout version %u, range %u, fl %d, "
2153 "%s LOV EA hole: rc = %d\n",
2154 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2155 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2156 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2157 rec->lor_layout_version, rec->lor_range, fl,
2158 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2159 "with" : "without", rc);
2164 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2165 struct dt_object *child,
2166 const struct lu_fid *pfid,
2167 const struct ost_layout *ol, __u32 offset,
2168 __u32 version, __u32 range)
2170 struct dt_device *dev = lfsck_obj2dev(child);
2171 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2172 struct thandle *handle;
2173 struct lu_buf buf = { NULL };
2176 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2177 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2178 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2179 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2180 * parent MDT-object's layout EA. */
2181 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2182 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2183 ff->ff_layout_version = cpu_to_le32(version);
2184 ff->ff_range = cpu_to_le32(range);
2185 lfsck_buf_init(&buf, ff, sizeof(*ff));
2187 handle = dt_trans_create(env, dev);
2189 RETURN(PTR_ERR(handle));
2191 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2195 rc = dt_trans_start_local(env, dev, handle);
2199 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2204 dt_trans_stop(env, dev, handle);
2210 * \retval +1: repaired
2211 * \retval 0: did nothing
2212 * \retval -ve: on error
2214 static int lfsck_layout_update_pfid(const struct lu_env *env,
2215 struct lfsck_component *com,
2216 struct dt_object *parent,
2217 struct lu_fid *cfid,
2218 struct dt_device *cdev,
2219 struct lu_orphan_rec_v3 *rec, __u32 ea_off)
2221 struct dt_object *child;
2225 child = lfsck_object_find_by_dev(env, cdev, cfid);
2227 RETURN(PTR_ERR(child));
2229 rc = __lfsck_layout_update_pfid(env, child,
2230 lu_object_fid(&parent->do_lu),
2231 &rec->lor_layout, ea_off,
2232 rec->lor_layout_version,
2234 lfsck_object_put(env, child);
2236 RETURN(rc == 0 ? 1 : rc);
2239 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2241 if (ol->ol_comp_id != 0)
2242 return sizeof(struct lov_comp_md_v1) +
2243 sizeof(struct lov_comp_md_entry_v1) +
2244 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2246 if (ol->ol_stripe_count != 0)
2247 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2249 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2253 * This function will create the MDT-object with the given (partial) LOV EA.
2255 * Under some data corruption cases, the MDT-object of the file may be lost,
2256 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2257 * re-create the MDT-object with the orphan OST-object(s) information.
2259 * On the other hand, the LFSCK may has created some OST-object for repairing
2260 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2261 * the old OST-object is there and should replace the former new created OST
2262 * object. Unfortunately, some others have modified such newly created object.
2263 * To keep the data (both new and old), the LFSCK will create MDT-object with
2264 * new FID to reference the original OST-object.
2266 * \param[in] env pointer to the thread context
2267 * \param[in] com pointer to the lfsck component
2268 * \param[in] ltd pointer to target device descriptor
2269 * \param[in] rec pointer to the record for the orphan OST-object
2270 * \param[in] cfid pointer to FID for the orphan OST-object
2271 * \param[in] infix additional information, such as the FID for original
2272 * MDT-object and the stripe offset in the LOV EA
2273 * \param[in] type the type for describing why the orphan MDT-object is
2274 * created. The rules are as following:
2276 * type "C": Multiple OST-objects claim the same MDT-object and the
2277 * same slot in the layout EA. Then the LFSCK will create
2278 * new MDT-object(s) to hold the conflict OST-object(s).
2280 * type "N": The orphan OST-object does not know which one was the
2281 * real parent MDT-object, so the LFSCK uses new FID for
2282 * its parent MDT-object.
2284 * type "R": The orphan OST-object knows its parent MDT-object FID,
2285 * but does not know the position (the file name) in the
2288 * type "D": The MDT-object is a directory, it may knows its parent
2289 * but because there is no valid linkEA, the LFSCK cannot
2290 * know where to put it back to the namespace.
2291 * type "O": The MDT-object has no linkEA, and there is no name
2292 * entry that references the MDT-object.
2294 * type "P": The orphan object to be created was a parent directory
2295 * of some MDT-object which linkEA shows that the @orphan
2296 * object is missing.
2298 * The orphan name will be like:
2299 * ${FID}-${infix}-${type}-${conflict_version}
2301 * \param[in] ea_off the stripe offset in the LOV EA
2303 * \retval positive on repaired something
2304 * \retval 0 if needs to repair nothing
2305 * \retval negative error number on failure
2307 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2308 struct lfsck_component *com,
2309 struct lfsck_tgt_desc *ltd,
2310 struct lu_orphan_rec_v3 *rec,
2311 struct lu_fid *cfid,
2316 struct lfsck_thread_info *info = lfsck_env_info(env);
2317 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2318 char *name = info->lti_key;
2319 struct lu_attr *la = &info->lti_la2;
2320 struct dt_object_format *dof = &info->lti_dof;
2321 struct lfsck_instance *lfsck = com->lc_lfsck;
2322 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2323 struct lu_fid *tfid = &info->lti_fid3;
2324 struct dt_device *dev = lfsck->li_bottom;
2325 struct dt_object *lpf = lfsck->li_lpf_obj;
2326 struct dt_object *pobj = NULL;
2327 struct dt_object *cobj = NULL;
2328 struct thandle *th = NULL;
2329 struct lu_buf *ea_buf = &info->lti_big_buf;
2330 struct lu_buf lov_buf;
2331 struct lfsck_lock_handle *llh = &info->lti_llh;
2332 struct linkea_data ldata = { NULL };
2333 struct lu_buf linkea_buf;
2334 const struct lu_name *pname;
2340 if (unlikely(lpf == NULL))
2341 GOTO(log, rc = -ENXIO);
2343 /* We use two separated transactions to repair the inconsistency.
2345 * 1) create the MDT-object locally.
2346 * 2) update the OST-object's PFID EA if necessary.
2348 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2349 * updated when the layout LFSCK run next time.
2351 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2352 * when the layout LFSCK run next time. */
2354 if (fid_is_zero(pfid)) {
2355 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2359 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2361 GOTO(log, rc = PTR_ERR(cobj));
2364 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2366 GOTO(log, rc = PTR_ERR(pobj));
2368 LASSERT(infix != NULL);
2369 LASSERT(type != NULL);
2371 memset(la, 0, sizeof(*la));
2372 la->la_uid = rec->lor_rec.lor_uid;
2373 la->la_gid = rec->lor_rec.lor_gid;
2374 la->la_mode = S_IFREG | S_IRUSR;
2375 la->la_valid = LA_MODE | LA_UID | LA_GID;
2377 memset(dof, 0, sizeof(*dof));
2378 dof->dof_type = dt_mode_to_dft(S_IFREG);
2379 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2380 * the stripe(s). The LFSCK will specify the LOV EA via
2381 * lfsck_layout_update_lovea(). */
2383 size = lfsck_lovea_size(&rec->lor_layout, ea_off);
2384 if (ea_buf->lb_len < size) {
2385 lu_buf_realloc(ea_buf, size);
2386 if (ea_buf->lb_buf == NULL)
2387 GOTO(log, rc = -ENOMEM);
2392 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2394 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2395 (const struct dt_key *)name);
2396 if (rc != 0 && rc != -ENOENT)
2400 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2401 MDS_INODELOCK_UPDATE, LCK_PW);
2405 /* Re-check whether the name conflict with othrs after taken
2407 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2408 (const struct dt_key *)name);
2409 if (unlikely(rc == 0)) {
2417 pname = lfsck_name_get_const(env, name, strlen(name));
2418 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2419 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2423 /* The 1st transaction. */
2424 th = dt_trans_create(env, dev);
2426 GOTO(unlock, rc = PTR_ERR(th));
2428 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2432 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2433 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2434 LU_XATTR_CREATE, th);
2438 dtrec->rec_fid = pfid;
2439 dtrec->rec_type = S_IFREG;
2440 rc = dt_declare_insert(env, lpf,
2441 (const struct dt_rec *)dtrec,
2442 (const struct dt_key *)name, th);
2446 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2447 ldata.ld_leh->leh_len);
2448 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2449 XATTR_NAME_LINK, 0, th);
2453 rc = dt_trans_start_local(env, dev, th);
2457 dt_write_lock(env, pobj, 0);
2458 rc = dt_create(env, pobj, la, NULL, dof, th);
2460 rc = lfsck_layout_update_lovea(env, lfsck, th, rec, pobj, cfid,
2461 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2462 dt_write_unlock(env, pobj);
2466 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2467 (const struct dt_key *)name, th);
2471 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2472 if (rc == 0 && cobj != NULL) {
2473 dt_trans_stop(env, dev, th);
2476 /* The 2nd transaction. */
2477 rc = __lfsck_layout_update_pfid(env, cobj, pfid,
2478 &rec->lor_layout, ea_off,
2479 rec->lor_layout_version,
2487 dt_trans_stop(env, dev, th);
2493 if (cobj != NULL && !IS_ERR(cobj))
2494 lfsck_object_put(env, cobj);
2495 if (pobj != NULL && !IS_ERR(pobj))
2496 lfsck_object_put(env, pobj);
2499 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2500 "recreate the lost MDT-object: parent "DFID
2501 ", child "DFID", OST-index %u, stripe-index %u, "
2502 "infix %s, type %s: rc = %d\n",
2503 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2504 ltd->ltd_index, ea_off, infix, type, rc);
2506 return rc >= 0 ? 1 : rc;
2509 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2510 struct lfsck_component *com,
2511 const struct lu_fid *fid,
2514 struct lfsck_thread_info *info = lfsck_env_info(env);
2515 struct lfsck_request *lr = &info->lti_lr;
2516 struct lfsck_instance *lfsck = com->lc_lfsck;
2517 struct lfsck_tgt_desc *ltd;
2518 struct ptlrpc_request *req;
2519 struct lfsck_request *tmp;
2520 struct obd_export *exp;
2524 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2525 if (unlikely(ltd == NULL))
2529 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2530 GOTO(put, rc = -EOPNOTSUPP);
2532 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2534 GOTO(put, rc = -ENOMEM);
2536 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2538 ptlrpc_request_free(req);
2543 memset(lr, 0, sizeof(*lr));
2544 lr->lr_event = LE_CONDITIONAL_DESTROY;
2545 lr->lr_active = LFSCK_TYPE_LAYOUT;
2548 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2550 ptlrpc_request_set_replen(req);
2552 rc = ptlrpc_queue_wait(req);
2553 ptlrpc_req_finished(req);
2563 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2564 struct lfsck_component *com,
2565 struct lfsck_request *lr)
2567 struct lfsck_thread_info *info = lfsck_env_info(env);
2568 struct lu_attr *la = &info->lti_la;
2569 union ldlm_policy_data *policy = &info->lti_policy;
2570 struct ldlm_res_id *resid = &info->lti_resid;
2571 struct lfsck_instance *lfsck = com->lc_lfsck;
2572 struct dt_device *dev = lfsck->li_bottom;
2573 struct lu_fid *fid = &lr->lr_fid;
2574 struct dt_object *obj;
2575 struct thandle *th = NULL;
2576 struct lustre_handle lh = { 0 };
2581 obj = lfsck_object_find_by_dev(env, dev, fid);
2583 RETURN(PTR_ERR(obj));
2585 dt_read_lock(env, obj, 0);
2586 if (dt_object_exists(obj) == 0 ||
2587 lfsck_is_dead_obj(obj)) {
2588 dt_read_unlock(env, obj);
2590 GOTO(put, rc = -ENOENT);
2593 /* Get obj's attr without lock firstly. */
2594 rc = dt_attr_get(env, obj, la);
2595 dt_read_unlock(env, obj);
2599 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2600 GOTO(put, rc = -ETXTBSY);
2602 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2603 LASSERT(lfsck->li_namespace != NULL);
2605 memset(policy, 0, sizeof(*policy));
2606 policy->l_extent.end = OBD_OBJECT_EOF;
2607 ost_fid_build_resid(fid, resid);
2608 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
2609 LDLM_EXTENT, policy, LCK_EX, &flags,
2610 ldlm_blocking_ast, ldlm_completion_ast,
2611 NULL, NULL, 0, LVB_T_NONE, NULL, &lh);
2613 GOTO(put, rc = -EIO);
2615 dt_write_lock(env, obj, 0);
2616 /* Get obj's attr within lock again. */
2617 rc = dt_attr_get(env, obj, la);
2621 if (la->la_ctime != 0)
2622 GOTO(unlock, rc = -ETXTBSY);
2624 th = dt_trans_create(env, dev);
2626 GOTO(unlock, rc = PTR_ERR(th));
2628 rc = dt_declare_ref_del(env, obj, th);
2632 rc = dt_declare_destroy(env, obj, th);
2636 rc = dt_trans_start_local(env, dev, th);
2640 rc = dt_ref_del(env, obj, th);
2644 rc = dt_destroy(env, obj, th);
2646 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2647 "OST-object "DFID" that was created for reparing "
2648 "dangling referenced case. But the original missing "
2649 "OST-object is found now.\n",
2650 lfsck_lfsck2name(lfsck), PFID(fid));
2655 dt_trans_stop(env, dev, th);
2658 dt_write_unlock(env, obj);
2659 ldlm_lock_decref(&lh, LCK_EX);
2662 lfsck_object_put(env, obj);
2668 * Some OST-object has occupied the specified layout EA slot.
2669 * Such OST-object may be generated by the LFSCK when repair
2670 * dangling referenced MDT-object, which can be indicated by
2671 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2672 * is true and such OST-object has not been modified yet, we
2673 * will replace it with the orphan OST-object; otherwise the
2674 * LFSCK will create new MDT-object to reference the orphan.
2676 * \retval +1: repaired
2677 * \retval 0: did nothing
2678 * \retval -ve: on error
2680 static int lfsck_layout_conflict_create(const struct lu_env *env,
2681 struct lfsck_component *com,
2682 struct lfsck_tgt_desc *ltd,
2683 struct lu_orphan_rec_v3 *rec,
2684 struct dt_object *parent,
2685 struct lu_fid *cfid,
2686 struct lu_buf *ea_buf,
2687 struct lov_mds_md_v1 *lmm,
2688 struct lov_ost_data_v1 *slot,
2689 __u32 ea_off, int lovea_size)
2691 struct lfsck_thread_info *info = lfsck_env_info(env);
2692 struct lu_fid *cfid2 = &info->lti_fid2;
2693 struct ost_id *oi = &info->lti_oi;
2694 struct dt_device *dev = lfsck_obj2dev(parent);
2695 struct thandle *th = NULL;
2696 struct lustre_handle lh = { 0 };
2697 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2701 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2702 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2706 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2707 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2711 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2712 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2717 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2719 /* If the conflict OST-obejct is not created for fixing dangling
2720 * referenced MDT-object in former LFSCK check/repair, or it has
2721 * been modified by others, then we cannot destroy it. Re-create
2722 * a new MDT-object for the orphan OST-object. */
2723 if (rc == -ETXTBSY) {
2724 /* No need the layout lock on the original parent. */
2725 lfsck_ibits_unlock(&lh, LCK_EX);
2727 fid_zero(&rec->lor_rec.lor_fid);
2728 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2729 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2731 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2732 info->lti_tmpbuf, "C", ea_off);
2737 if (rc != 0 && rc != -ENOENT)
2740 th = dt_trans_create(env, dev);
2742 GOTO(unlock, rc = PTR_ERR(th));
2744 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2745 LU_XATTR_REPLACE, th);
2749 rc = dt_trans_start_local(env, dev, th);
2753 dt_write_lock(env, parent, 0);
2754 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2755 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2756 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2757 ltd->ltd_index, lovea_size);
2758 dt_write_unlock(env, parent);
2763 dt_trans_stop(env, dev, th);
2766 lfsck_ibits_unlock(&lh, LCK_EX);
2769 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2770 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2771 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2772 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2773 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2776 return rc >= 0 ? 1 : rc;
2780 * \retval +1: repaired
2781 * \retval 0: did nothing
2782 * \retval -ve: on error
2784 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2785 struct lfsck_component *com,
2786 struct lfsck_tgt_desc *ltd,
2787 struct lu_orphan_rec_v3 *rec,
2788 struct dt_object *parent,
2789 struct lu_fid *cfid,
2790 __u32 ost_idx, __u32 ea_off)
2792 struct lfsck_thread_info *info = lfsck_env_info(env);
2793 struct lu_buf *buf = &info->lti_big_buf;
2794 struct lu_fid *fid = &info->lti_fid2;
2795 struct ost_id *oi = &info->lti_oi;
2796 struct lfsck_instance *lfsck = com->lc_lfsck;
2797 struct dt_device *dt = lfsck_obj2dev(parent);
2798 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2799 struct ost_layout *ol = &rec->lor_layout;
2800 struct lov_comp_md_v1 *lcm = NULL;
2801 struct lov_comp_md_entry_v1 *lcme = NULL;
2802 struct thandle *handle = NULL;
2804 struct lov_mds_md_v1 *lmm;
2805 struct lov_ost_data_v1 *objs;
2806 struct lustre_handle lh = { 0 };
2815 bool locked = false;
2816 bool new_mirror = true;
2819 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2820 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2823 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2824 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2825 "stripe-index %u, comp_id %u, comp_start %llu, "
2826 "comp_end %llu, layout version %u, range %u: rc = %d\n",
2827 lfsck_lfsck2name(lfsck), PFID(cfid),
2828 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2829 ol->ol_comp_id, ol->ol_comp_start,
2830 ol->ol_comp_end, rec->lor_layout_version,
2831 rec->lor_range, rc);
2838 dt_write_unlock(env, parent);
2842 if (handle != NULL) {
2843 dt_trans_stop(env, dt, handle);
2848 GOTO(unlock_layout, rc);
2851 if (buf->lb_len < lovea_size) {
2852 lu_buf_realloc(buf, lovea_size);
2853 if (buf->lb_buf == NULL)
2854 GOTO(unlock_layout, rc = -ENOMEM);
2857 if (!(bk->lb_param & LPF_DRYRUN)) {
2858 handle = dt_trans_create(env, dt);
2860 GOTO(unlock_layout, rc = PTR_ERR(handle));
2862 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2867 rc = dt_trans_start_local(env, dt, handle);
2872 dt_write_lock(env, parent, 0);
2874 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2875 if (rc == -ERANGE) {
2876 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2879 } else if (rc == -ENODATA || rc == 0) {
2880 lovea_size = lfsck_lovea_size(ol, ea_off);
2881 /* If the declared is not big enough, re-try. */
2882 if (buf->lb_len < lovea_size) {
2886 fl = LU_XATTR_CREATE;
2887 } else if (rc < 0) {
2888 GOTO(unlock_parent, rc);
2889 } else if (unlikely(buf->lb_len == 0)) {
2892 fl = LU_XATTR_REPLACE;
2896 if (fl == LU_XATTR_CREATE) {
2897 if (bk->lb_param & LPF_DRYRUN)
2898 GOTO(unlock_parent, rc = 1);
2900 LASSERT(buf->lb_len >= lovea_size);
2902 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2903 cfid, buf, fl, ost_idx, ea_off);
2905 GOTO(unlock_parent, rc);
2909 rc1 = lfsck_layout_verify_header(parent, lmm, lovea_size);
2911 /* If the LOV EA crashed, the rebuild it. */
2912 if (rc1 == -EINVAL) {
2913 if (bk->lb_param & LPF_DRYRUN)
2914 GOTO(unlock_parent, rc = 1);
2916 LASSERT(buf->lb_len >= lovea_size);
2918 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2919 cfid, buf, fl, ost_idx, ea_off);
2921 GOTO(unlock_parent, rc);
2924 /* For other unknown magic/pattern, keep the current LOV EA. */
2925 if (rc1 == -EOPNOTSUPP)
2926 GOTO(unlock_parent, rc1 = 0);
2929 GOTO(unlock_parent, rc = rc1);
2931 magic = le32_to_cpu(lmm->lmm_magic);
2932 if (magic == LOV_MAGIC_COMP_V1) {
2935 __u16 mirror_id0 = mirror_id_of(ol->ol_comp_id);
2938 if (bk->lb_param & LPF_DRYRUN)
2939 GOTO(unlock_parent, rc = 1);
2942 count = le16_to_cpu(lcm->lcm_entry_count);
2943 for (i = 0; i < count; pos = ++i) {
2944 lcme = &lcm->lcm_entries[i];
2945 start = le64_to_cpu(lcme->lcme_extent.e_start);
2946 end = le64_to_cpu(lcme->lcme_extent.e_end);
2947 mirror_id1 = mirror_id_of(le32_to_cpu(lcme->lcme_id));
2949 if (mirror_id0 > mirror_id1)
2952 if (mirror_id0 < mirror_id1)
2956 if (end <= ol->ol_comp_start)
2959 if (start >= ol->ol_comp_end)
2962 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
2963 magic = le32_to_cpu(lmm->lmm_magic);
2964 flags = le32_to_cpu(lcme->lcme_flags);
2968 rc = lfsck_layout_add_comp(env, lfsck, handle, rec, parent,
2969 cfid, buf, ost_idx, ea_off, pos, new_mirror);
2971 GOTO(unlock_parent, rc);
2975 count = le16_to_cpu(lmm->lmm_stripe_count);
2977 GOTO(unlock_parent, rc = -EINVAL);
2980 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2981 if (count <= ea_off) {
2982 if (bk->lb_param & LPF_DRYRUN)
2983 GOTO(unlock_parent, rc = 1);
2985 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2986 /* If the declared is not big enough, re-try. */
2987 if (buf->lb_len < lovea_size) {
2995 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
2996 lfsck_layout_update_lcm(lcm, lcme,
2997 rec->lor_layout_version,
3001 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
3002 parent, cfid, buf, ost_idx, ea_off);
3004 GOTO(unlock_parent, rc);
3007 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
3009 if (magic == LOV_MAGIC_V1) {
3010 objs = &lmm->lmm_objects[0];
3012 LASSERT(magic == LOV_MAGIC_V3);
3013 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3016 for (i = 0; i < count; i++, objs++) {
3017 /* The MDT-object was created via lfsck_layout_recover_create()
3018 * by others before, and we fill the dummy layout EA. */
3019 if ((lcme && !(flags & LCME_FL_INIT)) ||
3020 lovea_slot_is_dummy(objs)) {
3024 if (bk->lb_param & LPF_DRYRUN)
3025 GOTO(unlock_parent, rc = 1);
3027 lmm->lmm_layout_gen =
3028 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3032 if (le32_to_cpu(lmm->lmm_stripe_size) !=
3033 ol->ol_stripe_size ||
3034 le16_to_cpu(lmm->lmm_stripe_count) !=
3035 ol->ol_stripe_count ||
3036 le64_to_cpu(lcme->lcme_extent.e_start) !=
3037 ol->ol_comp_start ||
3038 le64_to_cpu(lcme->lcme_extent.e_end) !=
3040 CDEBUG(D_LFSCK, "%s: found invalid "
3041 "component for "DFID ": parent "DFID
3042 ", stripe-index %u, stripe_size %u, "
3043 "stripe_count %u, comp_id %u, "
3044 "comp_start %llu, comp_end %llu, "
3045 "cur_stripe_size %u, "
3046 "cur_stripe_count %u, "
3047 "cur_comp_start %llu, "
3048 "cur_comp_end %llu\n",
3049 lfsck_lfsck2name(lfsck), PFID(cfid),
3050 PFID(lfsck_dto2fid(parent)), ea_off,
3052 ol->ol_stripe_count, ol->ol_comp_id,
3053 ol->ol_comp_start, ol->ol_comp_end,
3054 le32_to_cpu(lmm->lmm_stripe_size),
3055 le16_to_cpu(lmm->lmm_stripe_count),
3056 le64_to_cpu(lcme->lcme_extent.e_start),
3057 le64_to_cpu(lcme->lcme_extent.e_end));
3059 GOTO(unlock_parent, rc = -EINVAL);
3062 lovea_size = le32_to_cpu(lcm->lcm_size);
3063 lcme->lcme_flags = cpu_to_le32(flags |
3065 lfsck_layout_update_lcm(lcm, lcme,
3066 rec->lor_layout_version,
3070 LASSERTF(buf->lb_len >= lovea_size,
3071 "buffer len %d is less than real size %d\n",
3072 (int)buf->lb_len, (int)lovea_size);
3074 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
3075 parent, cfid, buf, lmm, objs,
3076 fl, ost_idx, lovea_size);
3078 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
3079 "dummy layout slot for "DFID": parent "DFID
3080 ", OST-index %u, stripe-index %u: rc = %d\n",
3081 lfsck_lfsck2name(lfsck), PFID(cfid),
3082 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
3084 GOTO(unlock_parent, rc);
3087 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3088 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
3090 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3091 "invalid layout EA at the slot %d, index %u\n",
3092 lfsck_lfsck2name(lfsck),
3093 PFID(lfsck_dto2fid(parent)), i,
3094 le32_to_cpu(objs->l_ost_idx));
3096 GOTO(unlock_parent, rc);
3099 /* It should be rare case, the slot is there, but the LFSCK
3100 * does not handle it during the first-phase cycle scanning. */
3101 if (unlikely(lu_fid_eq(fid, cfid))) {
3103 GOTO(unlock_parent, rc = 0);
3105 /* Rare case that the OST-object index
3106 * does not match the parent MDT-object
3107 * layout EA. We trust the later one. */
3108 if (bk->lb_param & LPF_DRYRUN)
3109 GOTO(unlock_parent, rc = 1);
3111 dt_write_unlock(env, parent);
3113 dt_trans_stop(env, dt, handle);
3114 lfsck_ibits_unlock(&lh, LCK_EX);
3115 rc = lfsck_layout_update_pfid(env, com, parent,
3119 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
3120 "updated OST-object's pfid for "DFID
3121 ": parent "DFID", OST-index %u, "
3122 "stripe-index %u: rc = %d\n",
3123 lfsck_lfsck2name(lfsck), PFID(cfid),
3124 PFID(lfsck_dto2fid(parent)),
3125 ltd->ltd_index, i, rc);
3132 /* The MDT-object exists, but related layout EA slot is occupied
3134 if (bk->lb_param & LPF_DRYRUN)
3135 GOTO(unlock_parent, rc = 1);
3137 dt_write_unlock(env, parent);
3139 dt_trans_stop(env, dt, handle);
3140 lfsck_ibits_unlock(&lh, LCK_EX);
3141 if (magic == LOV_MAGIC_V1)
3142 objs = &lmm->lmm_objects[ea_off];
3144 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
3145 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
3146 buf, lmm, objs, ea_off, lovea_size);
3152 dt_write_unlock(env, parent);
3156 dt_trans_stop(env, dt, handle);
3159 lfsck_ibits_unlock(&lh, LCK_EX);
3164 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3165 struct lfsck_component *com,
3166 struct lfsck_tgt_desc *ltd,
3167 struct lu_orphan_rec_v3 *rec,
3168 struct lu_fid *cfid)
3170 struct lfsck_layout *lo = com->lc_file_ram;
3171 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3172 struct dt_object *parent = NULL;
3173 __u32 ea_off = pfid->f_stripe_idx;
3177 if (!fid_is_sane(cfid))
3178 GOTO(out, rc = -EINVAL);
3181 if (fid_is_zero(pfid)) {
3182 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3187 if (!fid_is_sane(pfid))
3188 GOTO(out, rc = -EINVAL);
3190 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3192 GOTO(out, rc = PTR_ERR(parent));
3194 if (unlikely(dt_object_remote(parent) != 0))
3195 GOTO(put, rc = -EXDEV);
3197 if (dt_object_exists(parent) == 0) {
3198 lfsck_object_put(env, parent);
3199 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3204 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3205 GOTO(put, rc = -EISDIR);
3207 /* The orphan OST-object claims to be the parent's stripe, then
3208 * related dangling record in the trace file is meaningless. */
3209 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3210 rec->lor_layout.ol_comp_id, ea_off);
3211 if (rc && rc != -ENOENT)
3214 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3215 ltd->ltd_index, ea_off);
3221 lfsck_object_put(env, parent);
3223 /* The layout EA is changed, need to be reloaded next time. */
3224 dt_object_put_nocache(env, parent);
3227 down_write(&com->lc_sem);
3228 com->lc_new_scanned++;
3229 com->lc_new_checked++;
3231 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3233 } else if (rc < 0) {
3234 lo->ll_objs_failed_phase2++;
3236 up_write(&com->lc_sem);
3241 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3242 struct lfsck_component *com,
3243 struct lfsck_tgt_desc *ltd)
3245 struct lfsck_assistant_data *lad = com->lc_data;
3246 struct lfsck_instance *lfsck = com->lc_lfsck;
3247 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3248 struct lfsck_thread_info *info = lfsck_env_info(env);
3249 struct lu_fid *fid = &info->lti_fid;
3250 struct dt_object *obj;
3251 const struct dt_it_ops *iops;
3256 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3257 "scanning for OST%04x\n",
3258 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3260 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
3261 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3262 "scanning for OST%04x\n",
3263 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3268 fid->f_seq = fid_idif_seq(0, ltd->ltd_index);
3269 fid->f_oid = fid->f_ver = 0;
3271 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3272 if (unlikely(IS_ERR(obj)))
3273 GOTO(log, rc = PTR_ERR(obj));
3275 rc = obj->do_ops->do_index_try(env, obj,
3276 &dt_lfsck_layout_orphan_features);
3280 iops = &obj->do_index_ops->dio_it;
3281 di = iops->init(env, obj, 0);
3283 GOTO(put, rc = PTR_ERR(di));
3285 rc = iops->load(env, di, 0);
3287 /* -ESRCH means that the orphan OST-objects rbtree has been
3288 * cleanup because of the OSS server restart or other errors. */
3289 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3294 rc = iops->next(env, di);
3306 struct lu_orphan_rec_v3 *rec = &info->lti_rec;
3308 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3309 unlikely(!thread_is_running(&lfsck->li_thread)))
3312 key = iops->key(env, di);
3313 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3314 /* Remote target OST may be runnning old LFSCK */
3315 memset(rec, 0, sizeof(*rec));
3316 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3318 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3319 &com->lc_fid_latest_scanned_phase2);
3320 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3323 lfsck_control_speed_by_self(com);
3325 rc = iops->next(env, di);
3326 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3333 iops->fini(env, di);
3335 lfsck_object_put(env, obj);
3338 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3339 "scanning for OST%04x: rc = %d\n",
3340 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3342 return rc > 0 ? 0 : rc;
3345 static int lfsck_lov2layout(struct lov_mds_md_v1 *lmm, struct filter_fid *ff,
3348 struct ost_layout *ol = &ff->ff_layout;
3349 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3353 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3354 ol->ol_stripe_size = lmm->lmm_stripe_size;
3355 ol->ol_stripe_count = lmm->lmm_stripe_count;
3356 ol->ol_comp_start = 0;
3357 ol->ol_comp_end = 0;
3359 ff->ff_layout_version = 0;
3361 } else if (magic == LOV_MAGIC_COMP_V1) {
3362 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3363 struct lov_comp_md_entry_v1 *lcme = NULL;
3364 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3367 for (i = 0; i < count; i++) {
3368 lcme = &lcm->lcm_entries[i];
3369 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3370 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3377 /* The comp has been removed, do nothing. */
3381 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3382 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3383 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3384 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3385 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3386 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3387 ff->ff_layout_version = le32_to_cpu(lcme->lcme_layout_gen);
3390 GOTO(out, rc = -EINVAL);
3400 * Repair the MDT-object with dangling LOV EA reference.
3402 * we need to repair the inconsistency according to the users' requirement:
3404 * 1) Keep the inconsistency there and report the inconsistency case,
3405 * then give the chance to the application to find related issues,
3406 * and the users can make the decision about how to handle it with
3407 * more human knownledge. (by default)
3409 * 2) Re-create the missing OST-object with the FID/owner information.
3411 * \param[in] env pointer to the thread context
3412 * \param[in] com the layout LFSCK component
3413 * \param[in] parent the MDT-object with dangling LOV EA reference
3414 * \param[in] child the OST-object to be created
3415 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3416 * \param[in] ea_off the offset of the OST-object in the LOV EA
3417 * \param[in] ost_idx the index of OST on which the OST-object resides
3419 * \retval +1 for repair successfully
3420 * \retval 0 for did nothing
3421 * \retval negative error number on failure
3423 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3424 struct lfsck_component *com,
3425 struct dt_object *parent,
3426 struct dt_object *child,
3427 __u32 comp_id, __u32 ea_off,
3428 __u32 ost_idx, bool log)
3430 struct lfsck_thread_info *info = lfsck_env_info(env);
3431 struct filter_fid *ff = &info->lti_ff;
3432 struct dt_object_format *dof = &info->lti_dof;
3433 struct lu_attr *la = &info->lti_la;
3434 struct lfsck_instance *lfsck = com->lc_lfsck;
3435 struct dt_device *dev = lfsck_obj2dev(child);
3436 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3437 const struct lu_fid *cfid = lfsck_dto2fid(child);
3438 struct lu_buf *tbuf = &info->lti_big_buf;
3439 struct thandle *handle;
3441 struct lustre_handle lh = { 0 };
3445 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3448 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3449 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3454 rc = dt_attr_get(env, parent, la);
3458 la->la_mode = S_IFREG | 0666;
3459 la->la_atime = la->la_mtime = la->la_ctime = 0;
3460 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3461 LA_ATIME | LA_MTIME | LA_CTIME;
3462 memset(dof, 0, sizeof(*dof));
3463 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3464 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3465 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3466 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3467 * parent MDT-object's layout EA. */
3468 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3470 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3471 if (unlikely(rc == -ENODATA))
3476 rc = lfsck_lov2layout(tbuf->lb_buf, ff, comp_id);
3480 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3481 handle = dt_trans_create(env, dev);
3483 GOTO(unlock1, rc = PTR_ERR(handle));
3485 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3489 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3490 LU_XATTR_CREATE, handle);
3494 rc = dt_trans_start_local(env, dev, handle);
3498 dt_read_lock(env, parent, 0);
3499 if (unlikely(lfsck_is_dead_obj(parent)))
3500 GOTO(unlock2, rc = 0);
3502 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3503 struct ost_id *oi = &info->lti_oi;
3504 struct lu_fid *tfid = &info->lti_fid2;
3505 struct lu_buf *lovea = &info->lti_big_buf;
3506 struct lov_mds_md_v1 *lmm;
3507 struct lov_ost_data_v1 *objs;
3512 rc = lfsck_layout_get_lovea(env, parent, lovea);
3513 if (unlikely(rc == -ENODATA))
3518 lmm = lovea->lb_buf;
3519 magic = le32_to_cpu(lmm->lmm_magic);
3520 if (magic == LOV_MAGIC_COMP_V1) {
3521 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3522 struct lov_comp_md_entry_v1 *lcme;
3523 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3526 for (i = 0; i < count; i++) {
3527 lcme = &lcm->lcm_entries[i];
3528 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3529 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3532 lmm = lovea->lb_buf +
3533 le32_to_cpu(lcme->lcme_offset);
3534 magic = le32_to_cpu(lmm->lmm_magic);
3539 /* Someone removed the component, do nothing. */
3540 GOTO(unlock2, rc = 0);
3544 count = le16_to_cpu(lmm->lmm_stripe_count);
3545 /* Someone changed the LOV EA, do nothing. */
3546 if (count <= ea_off)
3547 GOTO(unlock2, rc = 0);
3549 if (magic == LOV_MAGIC_V1) {
3550 objs = &lmm->lmm_objects[ea_off];
3552 LASSERT(magic == LOV_MAGIC_V3);
3554 objs = &((struct lov_mds_md_v3 *)lmm)->\
3555 lmm_objects[ea_off];
3558 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3559 idx2 = le32_to_cpu(objs->l_ost_idx);
3560 rc = ostid_to_fid(tfid, oi, idx2);
3561 /* Someone changed the LOV EA, do nothing. */
3562 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3566 rc = dt_create(env, child, la, NULL, dof, handle);
3570 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3576 dt_read_unlock(env, parent);
3579 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3582 lfsck_ibits_unlock(&lh, LCK_EX);
3586 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3587 "dangling reference for: parent "DFID", child "
3588 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3590 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3591 comp_id, ea_off, ost_idx,
3592 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3593 "Create the lost OST-object as required" :
3594 "Keep the MDT-object there by default", rc);
3600 * Repair the MDT-object with dangling LOV EA reference.
3602 * Prepare parameters and call __lfsck_layout_repair_dangling()
3603 * to repair the dangling LOV EA reference.
3605 * \param[in] env pointer to the thread context
3606 * \param[in] com the layout LFSCK component
3607 * \param[in] pfid the MDT-object's FID
3608 * \param[in] cfid the FID for the OST-object to be created
3609 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3610 * \param[in] ea_off the offset of the OST-object in the LOV EA
3611 * \param[in] ost_idx the index of OST on which the OST-object resides
3613 * \retval +1 for repair successfully
3614 * \retval 0 for did nothing
3615 * \retval negative error number on failure
3617 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3618 struct lfsck_component *com,
3619 const struct lu_fid *pfid,
3620 const struct lu_fid *cfid,
3621 __u32 comp_id, __u32 ea_off,
3624 struct lfsck_instance *lfsck = com->lc_lfsck;
3625 struct dt_object *parent = NULL;
3626 struct dt_object *child = NULL;
3627 struct lfsck_tgt_desc *ltd;
3631 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3633 GOTO(log, rc = PTR_ERR(parent));
3635 /* The MDT-object has been removed. */
3636 if (dt_object_exists(parent) == 0)
3639 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3640 if (unlikely(ltd == NULL))
3641 GOTO(log, rc = -ENODEV);
3643 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3645 GOTO(log, rc = PTR_ERR(child));
3647 /* The OST-object has been created. */
3648 if (unlikely(dt_object_exists(child) != 0))
3651 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3652 comp_id, ea_off, ost_idx, false);
3657 if (child != NULL && !IS_ERR(child))
3658 lfsck_object_put(env, child);
3660 if (parent != NULL && !IS_ERR(parent))
3661 lfsck_object_put(env, parent);
3664 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3665 "dangling reference for: parent "DFID", child "
3666 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3667 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3668 comp_id, ea_off, ost_idx,
3669 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3670 "Create the lost OST-object as required" :
3671 "Keep the MDT-object there by default", rc);
3676 /* If the OST-object does not recognize the MDT-object as its parent, and
3677 * there is no other MDT-object claims as its parent, then just trust the
3678 * given MDT-object as its parent. So update the OST-object filter_fid. */
3679 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3680 struct lfsck_component *com,
3681 struct dt_object *parent,
3682 struct lfsck_layout_req *llr,
3685 struct lfsck_thread_info *info = lfsck_env_info(env);
3686 struct filter_fid *ff = &info->lti_ff;
3687 struct dt_object *child = llr->llr_child;
3688 struct dt_device *dev = lfsck_obj2dev(child);
3689 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3690 struct lu_buf *tbuf = &info->lti_big_buf;
3691 struct thandle *handle;
3693 struct lustre_handle lh = { 0 };
3697 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3698 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3703 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3704 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3705 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3706 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3707 * parent MDT-object's layout EA. */
3708 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3710 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3711 if (unlikely(rc == -ENODATA))
3716 rc = lfsck_lov2layout(tbuf->lb_buf, ff, llr->llr_comp_id);
3720 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3722 handle = dt_trans_create(env, dev);
3724 GOTO(unlock1, rc = PTR_ERR(handle));
3726 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3730 rc = dt_attr_get(env, parent, la);
3734 la->la_valid = LA_UID | LA_GID;
3735 rc = dt_declare_attr_set(env, child, la, handle);
3739 rc = dt_trans_start_local(env, dev, handle);
3743 dt_write_lock(env, parent, 0);
3744 if (unlikely(lfsck_is_dead_obj(parent)))
3745 GOTO(unlock2, rc = 1);
3747 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3751 /* Get the latest parent's owner. */
3752 rc = dt_attr_get(env, parent, la);
3756 la->la_valid = LA_UID | LA_GID;
3757 rc = dt_attr_set(env, child, la, handle);
3762 dt_write_unlock(env, parent);
3765 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3768 lfsck_ibits_unlock(&lh, LCK_EX);
3772 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3773 "unmatched MDT-OST pair for: parent "DFID
3774 ", child "DFID", comp_id %u, OST-index %u, "
3775 "stripe-index %u, owner %u/%u: rc = %d\n",
3776 lfsck_lfsck2name(com->lc_lfsck),
3777 PFID(lfsck_dto2fid(parent)),
3778 PFID(lfsck_dto2fid(child)),
3779 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3780 la->la_uid, la->la_gid, rc);
3785 /* If there are more than one MDT-objects claim as the OST-object's parent,
3786 * and the OST-object only recognizes one of them, then we need to generate
3787 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3788 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3789 struct lfsck_component *com,
3790 struct dt_object *parent,
3791 struct lfsck_layout_req *llr,
3794 struct lfsck_thread_info *info = lfsck_env_info(env);
3795 struct dt_allocation_hint *hint = &info->lti_hint;
3796 struct dt_object_format *dof = &info->lti_dof;
3797 struct ost_id *oi = &info->lti_oi;
3798 struct lu_buf *buf = &info->lti_big_buf;
3799 struct lfsck_instance *lfsck = com->lc_lfsck;
3800 struct dt_device *dev;
3801 struct lu_device *d =
3802 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3803 struct lu_object *o;
3804 struct lu_object *n;
3805 struct dt_object *child = NULL;
3806 struct thandle *handle = NULL;
3807 struct lov_mds_md_v1 *lmm;
3808 struct lov_ost_data_v1 *objs;
3809 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3811 struct lustre_handle lh = { 0 };
3817 /* We use two separated transactions to repair the inconsistency.
3819 * 1) create the child (OST-object).
3820 * 2) update the parent LOV EA according to the child's FID.
3822 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3823 * handled as orphan when the layout LFSCK run next time.
3825 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3826 * as dangling referened case when the layout LFSCK run next time. */
3828 /* The 1st transaction. */
3829 o = lu_object_anon(env, d, NULL);
3831 GOTO(log, rc = PTR_ERR(o));
3833 n = lu_object_locate(o->lo_header, d->ld_type);
3834 if (unlikely(n == NULL)) {
3835 lu_object_put_nocache(env, o);
3837 GOTO(log, rc = -EINVAL);
3840 child = container_of(n, struct dt_object, do_lu);
3841 memset(hint, 0, sizeof(*hint));
3842 rc = dt_attr_get(env, parent, la);
3846 la->la_valid = LA_UID | LA_GID;
3847 memset(dof, 0, sizeof(*dof));
3849 dev = lfsck_obj2dev(child);
3850 handle = dt_trans_create(env, dev);
3852 GOTO(log, rc = PTR_ERR(handle));
3854 rc = dt_declare_create(env, child, la, hint, dof, handle);
3858 rc = dt_trans_start_local(env, dev, handle);
3862 rc = dt_create(env, child, la, hint, dof, handle);
3863 dt_trans_stop(env, dev, handle);
3868 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3869 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3874 /* The 2nd transaction. */
3876 /* XXX: Generally, we should use bottom device (OSD) to update parent
3877 * LOV EA. But because the LOD-object still references the wrong
3878 * OSP-object that should be detached after the parent's LOV EA
3879 * refreshed. Unfortunately, there is no suitable API for that.
3880 * So we have to make the LOD to re-load the OSP-object(s) via
3881 * replacing the LOV EA against the LOD-object.
3883 * Once the DNE2 patches have been landed, we can replace the
3884 * LOD device with the OSD device. LU-6230. */
3886 dev = lfsck->li_next;
3887 parent = lfsck_object_locate(dev, parent);
3889 GOTO(log, rc = PTR_ERR(parent));
3891 handle = dt_trans_create(env, dev);
3893 GOTO(log, rc = PTR_ERR(handle));
3895 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3896 LU_XATTR_REPLACE, handle);
3900 rc = dt_trans_start_local(env, dev, handle);
3904 dt_write_lock(env, parent, 0);
3905 if (unlikely(lfsck_is_dead_obj(parent)))
3906 GOTO(unlock, rc = 0);
3908 rc = lfsck_layout_get_lovea(env, parent, buf);
3909 if (unlikely(rc == -ENODATA))
3915 magic = le32_to_cpu(lmm->lmm_magic);
3916 if (magic == LOV_MAGIC_COMP_V1) {
3917 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3918 struct lov_comp_md_entry_v1 *lcme;
3919 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3922 LASSERT(llr->llr_comp_id != 0);
3924 for (i = 0; i < count; i++) {
3925 lcme = &lcm->lcm_entries[i];
3926 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3927 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3930 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3932 le32_to_cpu(lcme->lcme_offset);
3933 magic = le32_to_cpu(lmm->lmm_magic);
3938 GOTO(unlock, rc = 0);
3942 if (magic == LOV_MAGIC_V1) {
3943 objs = &lmm->lmm_objects[llr->llr_lov_idx];
3945 LASSERT(magic == LOV_MAGIC_V3);
3947 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3950 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3951 index = le32_to_cpu(objs->l_ost_idx);
3952 rc = ostid_to_fid(&tfid, oi, index);
3953 /* Someone changed layout during the LFSCK, no need to repair then. */
3954 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3955 GOTO(unlock, rc = 0);
3957 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3958 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3959 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3960 objs->l_ost_gen = cpu_to_le32(0);
3961 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3962 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3963 LU_XATTR_REPLACE, handle);
3965 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3968 dt_write_unlock(env, parent);
3972 dt_trans_stop(env, dev, handle);
3975 lfsck_ibits_unlock(&lh, LCK_EX);
3977 lfsck_object_put(env, child);
3980 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3981 "multiple references for: parent "DFID", comp_id %u, "
3982 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
3983 lfsck_lfsck2name(lfsck), PFID(pfid),
3984 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3985 la->la_uid, la->la_gid, rc);
3990 /* If the MDT-object and the OST-object have different owner information,
3991 * then trust the MDT-object, because the normal chown/chgrp handle order
3992 * is from MDT to OST, and it is possible that some chown/chgrp operation
3993 * is partly done. */
3994 static int lfsck_layout_repair_owner(const struct lu_env *env,
3995 struct lfsck_component *com,
3996 struct dt_object *parent,
3997 struct lfsck_layout_req *llr,
3998 struct lu_attr *pla,
3999 const struct lu_attr *cla)
4001 struct lfsck_thread_info *info = lfsck_env_info(env);
4002 struct lu_attr *tla = &info->lti_la2;
4003 struct dt_object *child = llr->llr_child;
4004 struct dt_device *dev = lfsck_obj2dev(child);
4005 struct thandle *handle;
4007 dt_obj_version_t version;
4010 tla->la_uid = pla->la_uid;
4011 tla->la_gid = pla->la_gid;
4012 tla->la_valid = LA_UID | LA_GID;
4013 handle = dt_trans_create(env, dev);
4015 GOTO(log, rc = PTR_ERR(handle));
4017 rc = dt_declare_attr_set(env, child, tla, handle);
4021 rc = dt_trans_start_local(env, dev, handle);
4025 /* Use the dt_object lock to serialize with destroy and attr_set. */
4026 dt_read_lock(env, parent, 0);
4027 if (unlikely(lfsck_is_dead_obj(parent)))
4028 GOTO(unlock, rc = 1);
4030 version = dt_version_get(env, child);
4031 if (version == -EOPNOTSUPP)
4034 /* Get the latest parent's owner. */
4035 rc = dt_attr_get(env, parent, pla);
4039 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
4040 if (unlikely((!version && tla->la_ctime == 0) ||
4041 tla->la_uid != pla->la_uid || tla->la_gid != pla->la_gid))
4044 rc = dt_attr_set(env, child, tla, handle);
4049 dt_read_unlock(env, parent);
4052 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
4056 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4057 "inconsistent file owner for: parent "DFID", child "DFID
4058 ", OST-index %u, stripe-index %u, old owner %u/%u, "
4059 "new owner %u/%u: rc = %d\n",
4060 lfsck_lfsck2name(com->lc_lfsck),
4061 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4062 llr->llr_ost_idx, llr->llr_lov_idx,
4063 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
4068 /* Check whether the OST-object correctly back points to the
4069 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
4070 static int lfsck_layout_check_parent(const struct lu_env *env,
4071 struct lfsck_component *com,
4072 struct lfsck_assistant_object *lso,
4073 struct filter_fid *ff,
4074 const struct lu_fid *cfid,
4075 const struct lu_attr *cla,
4076 struct lfsck_layout_req *llr)
4078 struct lfsck_thread_info *info = lfsck_env_info(env);
4079 struct lu_buf *buf = &info->lti_big_buf;
4080 struct lu_fid *pfid = &info->lti_fid;
4081 struct dt_object *tobj;
4082 struct lov_mds_md_v1 *lmm;
4083 struct lov_ost_data_v1 *objs;
4084 struct lustre_handle lh = { 0 };
4092 *pfid = ff->ff_parent;
4093 idx = pfid->f_stripe_idx;
4096 if (unlikely(!fid_is_sane(pfid)))
4097 RETURN(LLIT_UNMATCHED_PAIR);
4099 if (lu_fid_eq(pfid, &lso->lso_fid)) {
4100 if (likely(llr->llr_lov_idx == idx))
4103 RETURN(LLIT_UNMATCHED_PAIR);
4106 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4108 RETURN(PTR_ERR(tobj));
4110 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
4111 !S_ISREG(lfsck_object_type(tobj)))
4112 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4114 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
4115 * remote one on another MDT. Then check whether the given OST-object
4116 * is in such layout. If yes, it is multiple referenced, otherwise it
4117 * is unmatched referenced case. */
4118 rc = lfsck_layout_get_lovea(env, tobj, buf);
4119 if (rc == 0 || rc == -ENODATA || rc == -ENOENT)
4120 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4122 if (unlikely(rc == -EOPNOTSUPP))
4123 GOTO(out, rc = LLIT_NONE);
4129 magic = le32_to_cpu(lmm->lmm_magic);
4130 if (magic == LOV_MAGIC_COMP_V1) {
4131 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4132 struct lov_comp_md_entry_v1 *lcme;
4134 if (ff->ff_layout.ol_comp_id == 0)
4135 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4137 count = le16_to_cpu(lcm->lcm_entry_count);
4138 for (i = 0; i < count; i++) {
4139 lcme = &lcm->lcm_entries[i];
4140 if (le32_to_cpu(lcme->lcme_id) ==
4141 ff->ff_layout.ol_comp_id) {
4143 le32_to_cpu(lcme->lcme_offset);
4144 magic = le32_to_cpu(lmm->lmm_magic);
4145 if (!(le32_to_cpu(lcme->lcme_flags) &
4147 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4153 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4157 if (magic == LOV_MAGIC_V1) {
4158 objs = &lmm->lmm_objects[0];
4160 LASSERT(magic == LOV_MAGIC_V3);
4161 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4164 count = le16_to_cpu(lmm->lmm_stripe_count);
4165 for (i = 0; i < count; i++, objs++) {
4166 struct lu_fid *tfid = &info->lti_fid2;
4167 struct ost_id *oi = &info->lti_oi;
4170 if (lovea_slot_is_dummy(objs))
4173 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4174 idx2 = le32_to_cpu(objs->l_ost_idx);
4175 rc = ostid_to_fid(tfid, oi, idx2);
4177 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4178 "invalid layout EA at the slot %d, index %u\n",
4179 lfsck_lfsck2name(com->lc_lfsck),
4180 PFID(pfid), i, idx2);
4182 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4185 if (lu_fid_eq(cfid, tfid)) {
4186 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4187 MDS_INODELOCK_UPDATE |
4188 MDS_INODELOCK_LAYOUT |
4189 MDS_INODELOCK_XATTR,
4194 dt_read_lock(env, tobj, 0);
4196 /* For local MDT-object, re-check existence
4197 * after taken the lock. */
4198 if (!dt_object_remote(tobj)) {
4199 if (dt_object_exists(tobj) == 0 ||
4200 lfsck_is_dead_obj(tobj))
4201 rc = LLIT_UNMATCHED_PAIR;
4203 rc = LLIT_MULTIPLE_REFERENCED;
4208 /* For migration case, the new MDT-object and old
4209 * MDT-object may reference the same OST-object at
4210 * some migration internal time.
4212 * For remote MDT-object, the local MDT may not know
4213 * whether it has been removed or not. Try checking
4214 * for a non-existent xattr to check if this object
4215 * has been been removed or not. */
4216 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4218 if (unlikely(rc == -ENOENT || rc >= 0))
4219 rc = LLIT_UNMATCHED_PAIR;
4220 else if (rc == -ENODATA)
4221 rc = LLIT_MULTIPLE_REFERENCED;
4227 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4230 if (lustre_handle_is_used(&lh)) {
4231 dt_read_unlock(env, tobj);
4232 lfsck_ibits_unlock(&lh, LCK_EX);
4236 lfsck_object_put(env, tobj);
4241 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4242 struct lfsck_component *com,
4243 struct lfsck_assistant_req *lar)
4245 struct lfsck_layout_req *llr =
4246 container_of0(lar, struct lfsck_layout_req, llr_lar);
4247 struct lfsck_assistant_object *lso = lar->lar_parent;
4248 struct lfsck_layout *lo = com->lc_file_ram;
4249 struct lfsck_thread_info *info = lfsck_env_info(env);
4250 struct filter_fid *ff = &info->lti_ff;
4251 struct lu_buf buf = { .lb_buf = ff,
4252 .lb_len = sizeof(*ff) };
4253 struct dt_object *parent = NULL;
4254 struct dt_object *child = llr->llr_child;
4255 struct lu_attr *pla = &lso->lso_attr;
4256 struct lu_attr *cla = &info->lti_la;
4257 struct lfsck_instance *lfsck = com->lc_lfsck;
4258 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4259 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4266 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
4268 rc = dt_attr_get(env, child, cla);
4269 if (rc == -ENOENT) {
4270 parent = lfsck_assistant_object_load(env, lfsck, lso);
4271 if (IS_ERR(parent)) {
4272 rc = PTR_ERR(parent);
4274 RETURN(rc == -ENOENT ? 0 : rc);
4277 type = LLIT_DANGLING;
4284 lfsck_buf_init(&buf, ff, sizeof(*ff));
4285 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4286 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4287 type = LLIT_UNMATCHED_PAIR;
4291 if (rc < 0 && rc != -ENODATA)
4294 if (rc == 0 || rc == -ENODATA)
4295 GOTO(check_owner, rc = 0);
4297 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4298 rc = lfsck_layout_check_parent(env, com, lso, ff,
4299 lu_object_fid(&child->do_lu), cla, llr);
4309 /* Someone may has changed the owner after the parent attr pre-loaded.
4310 * It can be handled later inside the lfsck_layout_repair_owner(). */
4311 if (unlikely(cla->la_uid != pla->la_uid ||
4312 cla->la_gid != pla->la_gid)) {
4313 type = LLIT_INCONSISTENT_OWNER;
4318 if (type == LLIT_NONE)
4321 if (bk->lb_param & LPF_DRYRUN)
4324 if (parent == NULL) {
4325 parent = lfsck_assistant_object_load(env, lfsck, lso);
4326 if (IS_ERR(parent)) {
4327 rc = PTR_ERR(parent);
4338 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4339 rc = lfsck_layout_ins_dangling_rec(env, com,
4340 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4341 llr->llr_comp_id, llr->llr_lov_idx,
4344 rc = __lfsck_layout_repair_dangling(env, com, parent,
4351 case LLIT_UNMATCHED_PAIR:
4352 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4355 case LLIT_MULTIPLE_REFERENCED:
4356 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4359 case LLIT_INCONSISTENT_OWNER:
4360 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4370 down_write(&com->lc_sem);
4372 struct lfsck_assistant_data *lad = com->lc_data;
4374 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags))) {
4376 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4377 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4378 rc == -EHOSTUNREACH) {
4379 /* If cannot touch the target server,
4380 * mark the LFSCK as INCOMPLETE. */
4381 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4382 "talk with OST %x: rc = %d\n",
4383 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4384 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4385 lo->ll_objs_skipped++;
4388 lfsck_layout_record_failure(env, lfsck, lo);
4390 } else if (rc > 0 && (type != LLIT_DANGLING ||
4391 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4392 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4393 "unknown type = %d\n", type);
4395 lo->ll_objs_repaired[type - 1]++;
4396 if (bk->lb_param & LPF_DRYRUN &&
4397 unlikely(lo->ll_pos_first_inconsistent == 0))
4398 lo->ll_pos_first_inconsistent =
4399 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4402 up_write(&com->lc_sem);
4404 if (parent != NULL && !IS_ERR(parent))
4405 lfsck_object_put(env, parent);
4411 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4412 struct lfsck_component *com,
4413 struct dt_object *obj, bool first)
4415 struct lfsck_instance *lfsck = com->lc_lfsck;
4416 struct ptlrpc_thread *thread = &lfsck->li_thread;
4417 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4418 struct lfsck_layout *lo = com->lc_file_ram;
4419 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4422 struct lfsck_layout_dangling_key *parent =
4423 &lfsck_env_info(env)->lti_lldk;
4424 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4429 di = iops->init(env, obj, 0);
4431 RETURN(PTR_ERR(di));
4434 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4436 memset(parent, 0, sizeof(*parent));
4437 rc = iops->get(env, di, (const struct dt_key *)parent);
4442 /* The start one either has been processed or does not exist,
4444 rc = iops->next(env, di);
4450 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4451 unlikely(!thread_is_running(thread)))
4454 key = iops->key(env, di);
4463 lldk_be_to_cpu(parent,
4464 (const struct lfsck_layout_dangling_key *)key);
4465 if (!fid_is_sane(&parent->lldk_fid)) {
4470 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4472 fid_be_to_cpu(cfid, cfid);
4473 ost_idx = cfid->f_ver;
4475 if (!fid_is_sane(cfid)) {
4480 rc = lfsck_layout_repair_dangling(env, com,
4481 &parent->lldk_fid, cfid,
4482 parent->lldk_comp_id,
4483 parent->lldk_ea_off, ost_idx);
4487 down_write(&com->lc_sem);
4488 com->lc_new_checked++;
4489 com->lc_new_scanned++;
4491 lo->ll_lldk_latest_scanned_phase2 = *parent;
4494 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4496 lo->ll_objs_failed_phase2++;
4497 up_write(&com->lc_sem);
4499 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4502 if (unlikely(com->lc_time_next_checkpoint <=
4503 ktime_get_seconds()) &&
4504 com->lc_new_checked != 0) {
4505 down_write(&com->lc_sem);
4506 lo->ll_run_time_phase2 += ktime_get_seconds() -
4507 com->lc_time_last_checkpoint;
4508 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
4509 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4510 com->lc_new_checked = 0;
4511 lfsck_layout_store(env, com);
4512 up_write(&com->lc_sem);
4514 com->lc_time_last_checkpoint = ktime_get_seconds();
4515 com->lc_time_next_checkpoint =
4516 com->lc_time_last_checkpoint +
4517 LFSCK_CHECKPOINT_INTERVAL;
4520 lfsck_control_speed_by_self(com);
4521 if (unlikely(!thread_is_running(thread)))
4524 rc = iops->next(env, di);
4533 iops->fini(env, di);
4538 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4539 struct lfsck_component *com)
4541 struct lfsck_assistant_data *lad = com->lc_data;
4542 struct lfsck_instance *lfsck = com->lc_lfsck;
4543 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4544 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4545 struct lfsck_tgt_desc *ltd;
4549 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4550 lfsck_lfsck2name(lfsck));
4552 spin_lock(<ds->ltd_lock);
4553 while (!list_empty(&lad->lad_ost_phase2_list)) {
4554 ltd = list_entry(lad->lad_ost_phase2_list.next,
4555 struct lfsck_tgt_desc,
4556 ltd_layout_phase_list);
4557 list_del_init(<d->ltd_layout_phase_list);
4558 if (bk->lb_param & LPF_OST_ORPHAN) {
4559 spin_unlock(<ds->ltd_lock);
4560 rc = lfsck_layout_scan_orphan(env, com, ltd);
4561 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4564 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags) ||
4565 !thread_is_running(&lfsck->li_thread)))
4567 spin_lock(<ds->ltd_lock);
4571 if (list_empty(&lad->lad_ost_phase1_list))
4575 spin_unlock(<ds->ltd_lock);
4577 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4578 struct lfsck_layout *lo = com->lc_file_ram;
4581 com->lc_new_checked = 0;
4582 com->lc_new_scanned = 0;
4583 com->lc_time_last_checkpoint = ktime_get_seconds();
4584 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4585 LFSCK_CHECKPOINT_INTERVAL;
4587 i = lfsck_sub_trace_file_fid2idx(
4588 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4589 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4590 com->lc_sub_trace_objs[i].lsto_obj, true);
4591 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4592 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4593 com->lc_sub_trace_objs[i].lsto_obj, false);
4595 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4596 "at the No. %d trace file: rc = %d\n",
4597 lfsck_lfsck2name(lfsck), i, rc);
4600 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4601 lfsck_lfsck2name(lfsck), rc);
4607 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4608 struct ptlrpc_request *req,
4611 struct lfsck_layout_slave_async_args *llsaa = args;
4612 struct obd_export *exp = llsaa->llsaa_exp;
4613 struct lfsck_component *com = llsaa->llsaa_com;
4614 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4615 struct lfsck_layout_slave_data *llsd = com->lc_data;
4616 struct lfsck_reply *lr = NULL;
4620 /* It is probably caused by network trouble, or target crash,
4621 * it will try several times (depends on the obd_timeout, and
4622 * will not less than 3 times). But to make the LFSCK can go
4623 * ahead, we should not try for ever. After some try but still
4624 * hit failure, it will assume that the target exit the LFSCK
4625 * prcoessing and stop try. */
4626 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4627 int max_try = max_t(int, obd_timeout / 30, 3);
4629 if (++(llst->llst_failures) > max_try)
4635 llst->llst_failures = 0;
4636 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4637 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4638 lr->lr_status != LS_SCANNING_PHASE2)
4643 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4644 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4645 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4646 llst->llst_failures);
4648 lfsck_layout_llst_del(llsd, llst);
4651 lfsck_layout_llst_put(llst);
4652 lfsck_component_put(env, com);
4653 class_export_put(exp);
4658 static int lfsck_layout_async_query(const struct lu_env *env,
4659 struct lfsck_component *com,
4660 struct obd_export *exp,
4661 struct lfsck_layout_slave_target *llst,
4662 struct lfsck_request *lr,
4663 struct ptlrpc_request_set *set)
4665 struct lfsck_layout_slave_async_args *llsaa;
4666 struct ptlrpc_request *req;
4667 struct lfsck_request *tmp;
4671 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4675 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4677 ptlrpc_request_free(req);
4681 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4683 ptlrpc_request_set_replen(req);
4685 llsaa = ptlrpc_req_async_args(llsaa, req);
4686 llsaa->llsaa_exp = exp;
4687 llsaa->llsaa_com = lfsck_component_get(com);
4688 llsaa->llsaa_llst = llst;
4689 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4690 req->rq_allow_intr = 1;
4691 req->rq_no_delay = 1;
4692 ptlrpc_set_add_req(set, req);
4697 static int lfsck_layout_async_notify(const struct lu_env *env,
4698 struct obd_export *exp,
4699 struct lfsck_request *lr,
4700 struct ptlrpc_request_set *set)
4702 struct ptlrpc_request *req;
4703 struct lfsck_request *tmp;
4707 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4711 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4713 ptlrpc_request_free(req);
4717 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4719 ptlrpc_request_set_replen(req);
4720 req->rq_allow_intr = 1;
4721 req->rq_no_delay = 1;
4722 ptlrpc_set_add_req(set, req);
4728 lfsck_layout_slave_query_master(const struct lu_env *env,
4729 struct lfsck_component *com)
4731 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4732 struct lfsck_instance *lfsck = com->lc_lfsck;
4733 struct lfsck_layout_slave_data *llsd = com->lc_data;
4734 struct lfsck_layout_slave_target *llst;
4735 struct obd_export *exp;
4736 struct ptlrpc_request_set *set;
4741 set = ptlrpc_prep_set();
4743 GOTO(log, rc = -ENOMEM);
4745 memset(lr, 0, sizeof(*lr));
4746 lr->lr_event = LE_QUERY;
4747 lr->lr_active = LFSCK_TYPE_LAYOUT;
4749 llsd->llsd_touch_gen++;
4750 spin_lock(&llsd->llsd_lock);
4751 while (!list_empty(&llsd->llsd_master_list)) {
4752 llst = list_entry(llsd->llsd_master_list.next,
4753 struct lfsck_layout_slave_target,
4755 if (llst->llst_gen == llsd->llsd_touch_gen)
4758 llst->llst_gen = llsd->llsd_touch_gen;
4759 list_move_tail(&llst->llst_list,
4760 &llsd->llsd_master_list);
4761 atomic_inc(&llst->llst_ref);
4762 spin_unlock(&llsd->llsd_lock);
4764 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4767 lfsck_layout_llst_del(llsd, llst);
4768 lfsck_layout_llst_put(llst);
4769 spin_lock(&llsd->llsd_lock);
4773 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4775 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4776 "query %s for layout: rc = %d\n",
4777 lfsck_lfsck2name(lfsck),
4778 exp->exp_obd->obd_name, rc);
4781 lfsck_layout_llst_put(llst);
4782 class_export_put(exp);
4784 spin_lock(&llsd->llsd_lock);
4786 spin_unlock(&llsd->llsd_lock);
4788 rc = ptlrpc_set_wait(env, set);
4789 ptlrpc_set_destroy(set);
4791 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
4794 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
4795 lfsck_lfsck2name(com->lc_lfsck), rc);
4801 lfsck_layout_slave_notify_master(const struct lu_env *env,
4802 struct lfsck_component *com,
4803 enum lfsck_events event, int result)
4805 struct lfsck_layout *lo = com->lc_file_ram;
4806 struct lfsck_instance *lfsck = com->lc_lfsck;
4807 struct lfsck_layout_slave_data *llsd = com->lc_data;
4808 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4809 struct lfsck_layout_slave_target *llst;
4810 struct obd_export *exp;
4811 struct ptlrpc_request_set *set;
4815 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
4816 lfsck_lfsck2name(com->lc_lfsck));
4818 set = ptlrpc_prep_set();
4822 memset(lr, 0, sizeof(*lr));
4823 lr->lr_event = event;
4824 lr->lr_flags = LEF_FROM_OST;
4825 lr->lr_status = result;
4826 lr->lr_index = lfsck_dev_idx(lfsck);
4827 lr->lr_active = LFSCK_TYPE_LAYOUT;
4828 lr->lr_flags2 = lo->ll_flags;
4829 llsd->llsd_touch_gen++;
4830 spin_lock(&llsd->llsd_lock);
4831 while (!list_empty(&llsd->llsd_master_list)) {
4832 llst = list_entry(llsd->llsd_master_list.next,
4833 struct lfsck_layout_slave_target,
4835 if (llst->llst_gen == llsd->llsd_touch_gen)
4838 llst->llst_gen = llsd->llsd_touch_gen;
4839 list_move_tail(&llst->llst_list,
4840 &llsd->llsd_master_list);
4841 atomic_inc(&llst->llst_ref);
4842 spin_unlock(&llsd->llsd_lock);
4844 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4847 lfsck_layout_llst_del(llsd, llst);
4848 lfsck_layout_llst_put(llst);
4849 spin_lock(&llsd->llsd_lock);
4853 rc = lfsck_layout_async_notify(env, exp, lr, set);
4855 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4856 "notify %s for layout: rc = %d\n",
4857 lfsck_lfsck2name(lfsck),
4858 exp->exp_obd->obd_name, rc);
4860 lfsck_layout_llst_put(llst);
4861 class_export_put(exp);
4862 spin_lock(&llsd->llsd_lock);
4864 spin_unlock(&llsd->llsd_lock);
4866 ptlrpc_set_wait(env, set);
4867 ptlrpc_set_destroy(set);
4873 * \ret -ENODATA: unrecognized stripe
4874 * \ret = 0 : recognized stripe
4875 * \ret < 0 : other failures
4877 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
4878 struct lfsck_component *com,
4879 struct lu_fid *cfid,
4880 struct lu_fid *pfid, __u32 comp_id)
4882 struct lfsck_thread_info *info = lfsck_env_info(env);
4883 struct lu_buf *buf = &info->lti_big_buf;
4884 struct ost_id *oi = &info->lti_oi;
4885 struct dt_object *obj;
4886 struct lov_mds_md_v1 *lmm;
4887 struct lov_ost_data_v1 *objs;
4888 __u32 idx = pfid->f_stripe_idx;
4896 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4898 RETURN(PTR_ERR(obj));
4900 dt_read_lock(env, obj, 0);
4901 if (unlikely(dt_object_exists(obj) == 0 ||
4902 lfsck_is_dead_obj(obj)))
4903 GOTO(unlock, rc = -ENOENT);
4905 if (!S_ISREG(lfsck_object_type(obj)))
4906 GOTO(unlock, rc = -ENODATA);
4908 rc = lfsck_layout_get_lovea(env, obj, buf);
4913 magic = le32_to_cpu(lmm->lmm_magic);
4914 if (magic == LOV_MAGIC_COMP_V1) {
4915 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4916 struct lov_comp_md_entry_v1 *lcme;
4919 GOTO(unlock, rc = -ENODATA);
4921 count = le16_to_cpu(lcm->lcm_entry_count);
4922 for (i = 0; i < count; i++) {
4923 lcme = &lcm->lcm_entries[i];
4924 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
4926 le32_to_cpu(lcme->lcme_offset);
4927 magic = le32_to_cpu(lmm->lmm_magic);
4928 if (!(le32_to_cpu(lcme->lcme_flags) &
4930 GOTO(unlock, rc = -ENODATA);
4936 GOTO(unlock, rc = -ENODATA);
4940 if (magic == LOV_MAGIC_V1) {
4941 objs = &lmm->lmm_objects[0];
4943 LASSERT(magic == LOV_MAGIC_V3);
4944 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4947 fid_to_ostid(cfid, oi);
4948 count = le16_to_cpu(lmm->lmm_stripe_count);
4949 for (i = 0; i < count; i++, objs++) {
4952 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
4953 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
4954 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
4957 GOTO(unlock, rc = -ENODATA);
4960 dt_read_unlock(env, obj);
4961 lfsck_object_put(env, obj);
4967 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
4968 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
4969 * layout EA from MDT to OST. On one hand, the OST no need to understand
4970 * the layout EA structure; on the other hand, it may cause trouble when
4971 * transfer large layout EA from MDT to OST via normal OUT RPC.
4973 * \ret > 0: unrecognized stripe
4974 * \ret = 0: recognized stripe
4975 * \ret < 0: other failures
4977 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
4978 struct lfsck_component *com,
4979 struct lu_fid *cfid,
4980 struct lu_fid *pfid, __u32 comp_id)
4982 struct lfsck_instance *lfsck = com->lc_lfsck;
4983 struct obd_device *obd = lfsck->li_obd;
4984 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4985 struct obd_export *exp = NULL;
4986 struct ptlrpc_request *req = NULL;
4987 struct lfsck_request *lr;
4988 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
4992 if (unlikely(fid_is_idif(pfid)))
4995 fld_range_set_any(range);
4996 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
4998 RETURN(rc == -ENOENT ? 1 : rc);
5000 if (unlikely(!fld_range_is_mdt(range)))
5003 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
5004 if (unlikely(exp == NULL))
5007 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
5008 GOTO(out, rc = -EOPNOTSUPP);
5010 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
5012 GOTO(out, rc = -ENOMEM);
5014 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
5016 ptlrpc_request_free(req);
5021 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
5022 memset(lr, 0, sizeof(*lr));
5023 lr->lr_event = LE_PAIRS_VERIFY;
5024 lr->lr_active = LFSCK_TYPE_LAYOUT;
5025 lr->lr_fid = *cfid; /* OST-object itself FID. */
5026 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
5027 lr->lr_comp_id = comp_id;
5029 ptlrpc_request_set_replen(req);
5030 rc = ptlrpc_queue_wait(req);
5031 ptlrpc_req_finished(req);
5033 if (rc == -ENOENT || rc == -ENODATA)
5040 class_export_put(exp);
5045 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
5046 struct lfsck_component *com,
5047 struct lfsck_req_local *lrl)
5049 struct dt_object *obj;
5053 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
5055 GOTO(log, rc = PTR_ERR(obj));
5057 dt_write_lock(env, obj, 0);
5058 if (unlikely(dt_object_exists(obj) == 0 ||
5059 lfsck_is_dead_obj(obj)))
5060 GOTO(unlock, rc = 0);
5062 rc = __lfsck_layout_update_pfid(env, obj, &lrl->lrl_ff_client.ff_parent,
5063 &lrl->lrl_ff_client.ff_layout,
5064 lrl->lrl_ff_client.ff_layout_version,
5065 lrl->lrl_ff_client.ff_range,
5066 lrl->lrl_ff_client.ff_parent.f_ver);
5071 dt_write_unlock(env, obj);
5072 lfsck_object_put(env, obj);
5075 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
5076 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
5077 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
5084 static void lfsck_layout_slave_quit(const struct lu_env *env,
5085 struct lfsck_component *com);
5087 static int lfsck_layout_reset(const struct lu_env *env,
5088 struct lfsck_component *com, bool init)
5090 struct lfsck_layout *lo = com->lc_file_ram;
5093 down_write(&com->lc_sem);
5095 memset(lo, 0, com->lc_file_size);
5097 __u32 count = lo->ll_success_count;
5098 time64_t last_time = lo->ll_time_last_complete;
5100 memset(lo, 0, com->lc_file_size);
5101 lo->ll_success_count = count;
5102 lo->ll_time_last_complete = last_time;
5105 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
5106 lo->ll_status = LS_INIT;
5108 if (com->lc_lfsck->li_master) {
5109 struct lfsck_assistant_data *lad = com->lc_data;
5111 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
5112 CFS_RESET_BITMAP(lad->lad_bitmap);
5115 rc = lfsck_layout_store(env, com);
5116 if (rc == 0 && com->lc_lfsck->li_master)
5117 rc = lfsck_load_sub_trace_files(env, com,
5118 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
5119 up_write(&com->lc_sem);
5121 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
5122 lfsck_lfsck2name(com->lc_lfsck), rc);
5127 static void lfsck_layout_fail(const struct lu_env *env,
5128 struct lfsck_component *com, bool new_checked)
5130 struct lfsck_layout *lo = com->lc_file_ram;
5132 down_write(&com->lc_sem);
5134 com->lc_new_checked++;
5135 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
5136 up_write(&com->lc_sem);
5139 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
5140 struct lfsck_component *com, bool init)
5142 struct lfsck_instance *lfsck = com->lc_lfsck;
5143 struct lfsck_layout *lo = com->lc_file_ram;
5147 rc = lfsck_checkpoint_generic(env, com);
5149 return rc > 0 ? 0 : rc;
5152 down_write(&com->lc_sem);
5154 lo->ll_pos_latest_start =
5155 lfsck->li_pos_checkpoint.lp_oit_cookie;
5157 lo->ll_pos_last_checkpoint =
5158 lfsck->li_pos_checkpoint.lp_oit_cookie;
5159 lo->ll_run_time_phase1 += ktime_get_seconds() -
5160 lfsck->li_time_last_checkpoint;
5161 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5162 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5163 com->lc_new_checked = 0;
5166 rc = lfsck_layout_store(env, com);
5167 up_write(&com->lc_sem);
5169 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5170 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5171 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5176 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5177 struct lfsck_component *com, bool init)
5179 struct lfsck_instance *lfsck = com->lc_lfsck;
5180 struct lfsck_layout *lo = com->lc_file_ram;
5183 if (com->lc_new_checked == 0 && !init)
5186 down_write(&com->lc_sem);
5188 lo->ll_pos_latest_start =
5189 lfsck->li_pos_checkpoint.lp_oit_cookie;
5191 lo->ll_pos_last_checkpoint =
5192 lfsck->li_pos_checkpoint.lp_oit_cookie;
5193 lo->ll_run_time_phase1 += ktime_get_seconds() -
5194 lfsck->li_time_last_checkpoint;
5195 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5196 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5197 com->lc_new_checked = 0;
5200 rc = lfsck_layout_store(env, com);
5201 up_write(&com->lc_sem);
5203 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5204 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5205 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5210 static int lfsck_layout_prep(const struct lu_env *env,
5211 struct lfsck_component *com,
5212 struct lfsck_start *start)
5214 struct lfsck_instance *lfsck = com->lc_lfsck;
5215 struct lfsck_layout *lo = com->lc_file_ram;
5216 struct lfsck_position *pos = &com->lc_pos_start;
5218 fid_zero(&pos->lp_dir_parent);
5219 pos->lp_dir_cookie = 0;
5220 if (lo->ll_status == LS_COMPLETED ||
5221 lo->ll_status == LS_PARTIAL ||
5222 /* To handle orphan, must scan from the beginning. */
5223 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5226 rc = lfsck_layout_reset(env, com, false);
5228 rc = lfsck_set_param(env, lfsck, start, true);
5231 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5232 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5238 down_write(&com->lc_sem);
5239 lo->ll_time_latest_start = ktime_get_real_seconds();
5240 spin_lock(&lfsck->li_lock);
5241 if (lo->ll_flags & LF_SCANNED_ONCE) {
5242 if (!lfsck->li_drop_dryrun ||
5243 lo->ll_pos_first_inconsistent == 0) {
5244 lo->ll_status = LS_SCANNING_PHASE2;
5245 list_move_tail(&com->lc_link,
5246 &lfsck->li_list_double_scan);
5247 pos->lp_oit_cookie = 0;
5251 lo->ll_status = LS_SCANNING_PHASE1;
5252 lo->ll_run_time_phase1 = 0;
5253 lo->ll_run_time_phase2 = 0;
5254 lo->ll_objs_checked_phase1 = 0;
5255 lo->ll_objs_checked_phase2 = 0;
5256 lo->ll_objs_failed_phase1 = 0;
5257 lo->ll_objs_failed_phase2 = 0;
5258 for (i = 0; i < LLIT_MAX; i++)
5259 lo->ll_objs_repaired[i] = 0;
5261 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5262 fid_zero(&com->lc_fid_latest_scanned_phase2);
5265 lo->ll_status = LS_SCANNING_PHASE1;
5266 if (!lfsck->li_drop_dryrun ||
5267 lo->ll_pos_first_inconsistent == 0)
5268 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5270 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5272 spin_unlock(&lfsck->li_lock);
5273 up_write(&com->lc_sem);
5278 static int lfsck_layout_slave_prep(const struct lu_env *env,
5279 struct lfsck_component *com,
5280 struct lfsck_start_param *lsp)
5282 struct lfsck_layout_slave_data *llsd = com->lc_data;
5283 struct lfsck_instance *lfsck = com->lc_lfsck;
5284 struct lfsck_layout *lo = com->lc_file_ram;
5285 struct lfsck_start *start = lsp->lsp_start;
5288 rc = lfsck_layout_prep(env, com, start);
5292 if (lo->ll_flags & LF_CRASHED_LASTID &&
5293 list_empty(&llsd->llsd_master_list)) {
5294 LASSERT(lfsck->li_out_notify != NULL);
5296 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5297 LE_LASTID_REBUILDING);
5300 if (!lsp->lsp_index_valid)
5303 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5304 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5305 LASSERT(!llsd->llsd_rbtree_valid);
5307 down_write(&llsd->llsd_rb_rwsem);
5308 rc = lfsck_rbtree_setup(env, com);
5309 up_write(&llsd->llsd_rb_rwsem);
5312 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5313 "%llu]\n", lfsck_lfsck2name(lfsck),
5314 com->lc_pos_start.lp_oit_cookie);
5319 static int lfsck_layout_master_prep(const struct lu_env *env,
5320 struct lfsck_component *com,
5321 struct lfsck_start_param *lsp)
5326 rc = lfsck_layout_load_bitmap(env, com);
5328 rc = lfsck_layout_reset(env, com, false);
5330 rc = lfsck_set_param(env, com->lc_lfsck,
5331 lsp->lsp_start, true);
5337 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5341 rc = lfsck_start_assistant(env, com, lsp);
5346 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5347 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5348 com->lc_pos_start.lp_oit_cookie);
5353 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5354 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5355 struct lfsck_component *com,
5356 struct dt_object *parent,
5357 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5359 struct lfsck_thread_info *info = lfsck_env_info(env);
5360 struct lfsck_instance *lfsck = com->lc_lfsck;
5361 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5362 struct lfsck_layout *lo = com->lc_file_ram;
5363 struct lfsck_assistant_data *lad = com->lc_data;
5364 struct lfsck_assistant_object *lso = NULL;
5365 struct lov_ost_data_v1 *objs;
5366 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5367 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5368 struct ptlrpc_thread *athread = &lad->lad_thread;
5369 struct l_wait_info lwi = { 0 };
5377 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5378 magic = le32_to_cpu(lmm->lmm_magic);
5379 if (magic == LOV_MAGIC_V1) {
5380 objs = &lmm->lmm_objects[0];
5382 LASSERT(magic == LOV_MAGIC_V3);
5383 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5386 count = le16_to_cpu(lmm->lmm_stripe_count);
5387 for (i = 0; i < count; i++, objs++) {
5388 struct lu_fid *fid = &info->lti_fid;
5389 struct ost_id *oi = &info->lti_oi;
5390 struct lfsck_layout_req *llr;
5391 struct lfsck_tgt_desc *tgt = NULL;
5392 struct dt_object *cobj = NULL;
5394 bool wakeup = false;
5396 if (unlikely(lovea_slot_is_dummy(objs)))
5399 l_wait_event(mthread->t_ctl_waitq,
5400 lad->lad_prefetched < bk->lb_async_windows ||
5401 !thread_is_running(mthread) ||
5402 thread_is_stopped(athread),
5405 if (unlikely(!thread_is_running(mthread)) ||
5406 thread_is_stopped(athread))
5409 if (unlikely(lfsck_is_dead_obj(parent)))
5412 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5413 index = le32_to_cpu(objs->l_ost_idx);
5414 rc = ostid_to_fid(fid, oi, index);
5416 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5417 ": "DOSTID", idx %u, comp_id %u\n",
5418 lfsck_lfsck2name(lfsck),
5419 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5424 tgt = lfsck_tgt_get(ltds, index);
5425 if (unlikely(tgt == NULL)) {
5426 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5427 "did not join the layout LFSCK, comp_id %u\n",
5428 lfsck_lfsck2name(lfsck), index, comp_id);
5429 lfsck_lad_set_bitmap(env, com, index);
5433 /* There is potential deadlock race condition between object
5434 * destroy and layout LFSCK. Consider the following scenario:
5436 * 1) The LFSCK thread obtained the parent object firstly, at
5437 * that time, the parent object has not been destroyed yet.
5439 * 2) One RPC service thread destroyed the parent and all its
5440 * children objects. Because the LFSCK is referencing the
5441 * parent object, then the parent object will be marked as
5442 * dying in RAM. On the other hand, the parent object is
5443 * referencing all its children objects, then all children
5444 * objects will be marked as dying in RAM also.
5446 * 3) The LFSCK thread tries to find some child object with
5447 * the parent object referenced. Then it will find that the
5448 * child object is dying. According to the object visibility
5449 * rules: the object with dying flag cannot be returned to
5450 * others. So the LFSCK thread has to wait until the dying
5451 * object has been purged from RAM, then it can allocate a
5452 * new object (with the same FID) in RAM. Unfortunately, the
5453 * LFSCK thread itself is referencing the parent object, and
5454 * cause the parent object cannot be purged, then cause the
5455 * child object cannot be purged also. So the LFSCK thread
5456 * will fall into deadlock.
5458 cobj = lfsck_object_find_by_dev(env, tgt->ltd_tgt, fid);
5460 if (lfsck_is_dead_obj(parent)) {
5470 rc = dt_declare_attr_get(env, cobj);
5474 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
5479 struct lu_attr *attr = &info->lti_la;
5481 rc = dt_attr_get(env, parent, attr);
5485 lso = lfsck_assistant_object_init(env,
5486 lfsck_dto2fid(parent), attr,
5487 lfsck->li_pos_current.lp_oit_cookie, false);
5496 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5504 spin_lock(&lad->lad_lock);
5505 if (lad->lad_assistant_status < 0) {
5506 spin_unlock(&lad->lad_lock);
5507 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5509 RETURN(lad->lad_assistant_status);
5512 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5513 if (lad->lad_prefetched == 0)
5516 lad->lad_prefetched++;
5517 spin_unlock(&lad->lad_lock);
5519 wake_up_all(&athread->t_ctl_waitq);
5522 down_write(&com->lc_sem);
5523 com->lc_new_checked++;
5525 lfsck_layout_record_failure(env, lfsck, lo);
5526 up_write(&com->lc_sem);
5528 if (cobj != NULL && !IS_ERR(cobj))
5529 lfsck_object_put(env, cobj);
5531 if (likely(tgt != NULL))
5534 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5542 lfsck_assistant_object_put(env, lso);
5547 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5548 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5549 * list ::lad_req_list.
5551 * For each request on above list, the lfsck_layout_assistant thread compares
5552 * the OST side attribute with local attribute, if inconsistent, then repair it.
5554 * All above processing is async mode with pipeline. */
5555 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5556 struct lfsck_component *com,
5557 struct dt_object *obj)
5559 struct lfsck_thread_info *info = lfsck_env_info(env);
5560 struct ost_id *oi = &info->lti_oi;
5561 struct lfsck_layout *lo = com->lc_file_ram;
5562 struct lfsck_assistant_data *lad = com->lc_data;
5563 struct lfsck_instance *lfsck = com->lc_lfsck;
5564 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5565 struct thandle *handle = NULL;
5566 struct lu_buf *buf = &info->lti_big_buf;
5567 struct lov_mds_md_v1 *lmm = NULL;
5568 struct dt_device *dev = lfsck_obj2dev(obj);
5569 struct lustre_handle lh = { 0 };
5570 struct lu_buf ea_buf = { NULL };
5571 struct lov_comp_md_v1 *lcm = NULL;
5572 struct lov_comp_md_entry_v1 *lcme = NULL;
5577 bool locked = false;
5578 bool stripe = false;
5579 bool bad_oi = false;
5582 if (!S_ISREG(lfsck_object_type(obj)))
5585 if (lad->lad_assistant_status < 0)
5586 GOTO(out, rc = -ESRCH);
5588 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5589 lmm_oi_cpu_to_le(oi, oi);
5590 dt_read_lock(env, obj, 0);
5595 if (dt_object_exists(obj) == 0 ||
5596 lfsck_is_dead_obj(obj))
5599 rc = lfsck_layout_get_lovea(env, obj, buf);
5600 if (rc == -EINVAL || rc == -ENODATA || rc == -EOPNOTSUPP)
5601 /* Skip bad lov EA during the 1st cycle scanning, and
5602 * try to recover it via orphan in the 2nd scanning. */
5609 magic = le32_to_cpu(lmm->lmm_magic);
5610 if (magic == LOV_MAGIC_COMP_V1) {
5614 count = le16_to_cpu(lcm->lcm_entry_count);
5615 for (i = 0; i < count; i++) {
5616 lcme = &lcm->lcm_entries[i];
5617 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5618 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) != 0)
5622 GOTO(out, stripe = true);
5623 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5624 GOTO(out, stripe = true);
5628 /* Inconsistent lmm_oi, should be repaired. */
5631 if (bk->lb_param & LPF_DRYRUN) {
5632 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5634 GOTO(out, stripe = true);
5637 if (!lustre_handle_is_used(&lh)) {
5638 dt_read_unlock(env, obj);
5640 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5641 MDS_INODELOCK_LAYOUT |
5642 MDS_INODELOCK_XATTR, LCK_EX);
5646 handle = dt_trans_create(env, dev);
5648 GOTO(out, rc = PTR_ERR(handle));
5650 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
5651 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5652 LU_XATTR_REPLACE, handle);
5656 rc = dt_trans_start_local(env, dev, handle);
5660 dt_write_lock(env, obj, 0);
5666 if (magic == LOV_MAGIC_COMP_V1) {
5669 for (i = 0; i < count; i++) {
5670 lcme = &lcm->lcm_entries[i];
5671 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5678 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5679 LU_XATTR_REPLACE, handle);
5683 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5685 GOTO(out, stripe = true);
5689 if (lustre_handle_is_used(&lh))
5690 dt_write_unlock(env, obj);
5692 dt_read_unlock(env, obj);
5695 if (handle != NULL && !IS_ERR(handle))
5696 dt_trans_stop(env, dev, handle);
5698 lfsck_ibits_unlock(&lh, LCK_EX);
5701 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5702 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5703 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5704 PFID(lfsck_dto2fid(obj)), rc);
5707 if (magic == LOV_MAGIC_COMP_V1) {
5710 for (i = 0; i < count; i++) {
5711 lcme = &lcm->lcm_entries[i];
5712 if (!(le32_to_cpu(lcme->lcme_flags) &
5716 rc = lfsck_layout_scan_stripes(env, com, obj,
5717 (struct lov_mds_md_v1 *)(buf->lb_buf +
5718 le32_to_cpu(lcme->lcme_offset)),
5719 le32_to_cpu(lcme->lcme_id));
5722 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5725 down_write(&com->lc_sem);
5726 com->lc_new_checked++;
5728 lfsck_layout_record_failure(env, lfsck, lo);
5729 up_write(&com->lc_sem);
5735 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5736 struct lfsck_component *com,
5737 struct dt_object *obj)
5739 struct lfsck_instance *lfsck = com->lc_lfsck;
5740 struct lfsck_layout *lo = com->lc_file_ram;
5741 const struct lu_fid *fid = lfsck_dto2fid(obj);
5742 struct lfsck_layout_slave_data *llsd = com->lc_data;
5743 struct lfsck_layout_seq *lls;
5749 LASSERT(llsd != NULL);
5751 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5752 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5753 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
5755 struct ptlrpc_thread *thread = &lfsck->li_thread;
5757 l_wait_event(thread->t_ctl_waitq,
5758 !thread_is_running(thread),
5762 lfsck_rbtree_update_bitmap(env, com, fid, false);
5764 down_write(&com->lc_sem);
5765 if (fid_is_idif(fid))
5767 else if (!fid_is_norm(fid) ||
5768 !fid_is_for_ostobj(env, lfsck, obj, fid))
5769 GOTO(unlock, rc = 0);
5772 com->lc_new_checked++;
5774 lls = lfsck_layout_seq_lookup(llsd, seq);
5777 if (unlikely(lls == NULL))
5778 GOTO(unlock, rc = -ENOMEM);
5780 INIT_LIST_HEAD(&lls->lls_list);
5782 rc = lfsck_layout_lastid_load(env, com, lls);
5784 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5785 "load LAST_ID for %#llx: rc = %d\n",
5786 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5787 lo->ll_objs_failed_phase1++;
5792 lfsck_layout_seq_insert(llsd, lls);
5795 if (unlikely(fid_is_last_id(fid)))
5796 GOTO(unlock, rc = 0);
5798 if (fid_is_idif(fid))
5799 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5803 if (oid > lls->lls_lastid_known)
5804 lls->lls_lastid_known = oid;
5806 if (oid > lls->lls_lastid) {
5807 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
5808 /* OFD may create new objects during LFSCK scanning. */
5809 rc = lfsck_layout_lastid_reload(env, com, lls);
5810 if (unlikely(rc != 0)) {
5811 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5812 "reload LAST_ID for %#llx: rc = %d\n",
5813 lfsck_lfsck2name(com->lc_lfsck),
5819 if (oid <= lls->lls_lastid ||
5820 lo->ll_flags & LF_CRASHED_LASTID)
5821 GOTO(unlock, rc = 0);
5823 LASSERT(lfsck->li_out_notify != NULL);
5825 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5826 LE_LASTID_REBUILDING);
5827 lo->ll_flags |= LF_CRASHED_LASTID;
5829 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
5830 "LAST_ID file (2) for the sequence %#llx"
5831 ", old value %llu, known value %llu\n",
5832 lfsck_lfsck2name(lfsck), lls->lls_seq,
5833 lls->lls_lastid, oid);
5836 lls->lls_lastid = oid;
5840 GOTO(unlock, rc = 0);
5843 up_write(&com->lc_sem);
5848 static int lfsck_layout_exec_dir(const struct lu_env *env,
5849 struct lfsck_component *com,
5850 struct lfsck_assistant_object *lso,
5851 struct lu_dirent *ent, __u16 type)
5856 static int lfsck_layout_master_post(const struct lu_env *env,
5857 struct lfsck_component *com,
5858 int result, bool init)
5860 struct lfsck_instance *lfsck = com->lc_lfsck;
5861 struct lfsck_layout *lo = com->lc_file_ram;
5865 lfsck_post_generic(env, com, &result);
5867 down_write(&com->lc_sem);
5868 spin_lock(&lfsck->li_lock);
5870 lo->ll_pos_last_checkpoint =
5871 lfsck->li_pos_checkpoint.lp_oit_cookie;
5874 if (lo->ll_flags & LF_INCOMPLETE)
5875 lo->ll_status = LS_PARTIAL;
5877 lo->ll_status = LS_SCANNING_PHASE2;
5878 lo->ll_flags |= LF_SCANNED_ONCE;
5879 lo->ll_flags &= ~LF_UPGRADE;
5880 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5881 } else if (result == 0) {
5882 if (lfsck->li_status != 0)
5883 lo->ll_status = lfsck->li_status;
5885 lo->ll_status = LS_STOPPED;
5886 if (lo->ll_status != LS_PAUSED)
5887 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5889 lo->ll_status = LS_FAILED;
5890 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5892 spin_unlock(&lfsck->li_lock);
5895 lo->ll_run_time_phase1 += ktime_get_seconds() -
5896 lfsck->li_time_last_checkpoint;
5897 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5898 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5899 com->lc_new_checked = 0;
5902 rc = lfsck_layout_store(env, com);
5903 up_write(&com->lc_sem);
5905 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
5906 lfsck_lfsck2name(lfsck), rc);
5911 static int lfsck_layout_slave_post(const struct lu_env *env,
5912 struct lfsck_component *com,
5913 int result, bool init)
5915 struct lfsck_instance *lfsck = com->lc_lfsck;
5916 struct lfsck_layout *lo = com->lc_file_ram;
5920 down_write(&com->lc_sem);
5921 rc = lfsck_layout_lastid_store(env, com);
5925 LASSERT(lfsck->li_out_notify != NULL);
5927 spin_lock(&lfsck->li_lock);
5929 lo->ll_pos_last_checkpoint =
5930 lfsck->li_pos_checkpoint.lp_oit_cookie;
5933 lo->ll_status = LS_SCANNING_PHASE2;
5934 lo->ll_flags |= LF_SCANNED_ONCE;
5935 if (lo->ll_flags & LF_CRASHED_LASTID) {
5937 lo->ll_flags &= ~LF_CRASHED_LASTID;
5939 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
5940 "crashed LAST_ID files successfully\n",
5941 lfsck_lfsck2name(lfsck));
5943 lo->ll_flags &= ~LF_UPGRADE;
5944 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5945 } else if (result == 0) {
5946 if (lfsck->li_status != 0)
5947 lo->ll_status = lfsck->li_status;
5949 lo->ll_status = LS_STOPPED;
5950 if (lo->ll_status != LS_PAUSED)
5951 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5953 lo->ll_status = LS_FAILED;
5954 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5956 spin_unlock(&lfsck->li_lock);
5959 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5963 lo->ll_run_time_phase1 += ktime_get_seconds() -
5964 lfsck->li_time_last_checkpoint;
5965 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5966 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5967 com->lc_new_checked = 0;
5970 rc = lfsck_layout_store(env, com);
5971 up_write(&com->lc_sem);
5973 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
5975 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
5976 lfsck_lfsck2name(lfsck), rc);
5981 static void lfsck_layout_dump(const struct lu_env *env,
5982 struct lfsck_component *com, struct seq_file *m)
5984 struct lfsck_instance *lfsck = com->lc_lfsck;
5985 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5986 struct lfsck_layout *lo = com->lc_file_ram;
5989 down_read(&com->lc_sem);
5990 if (bk->lb_param & LPF_DRYRUN)
5991 prefix = "inconsistent";
5993 prefix = "repaired";
5995 seq_printf(m, "name: lfsck_layout\n"
6001 lfsck_status2name(lo->ll_status));
6003 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
6005 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
6007 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
6009 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
6011 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
6013 seq_printf(m, "latest_start_position: %llu\n"
6014 "last_checkpoint_position: %llu\n"
6015 "first_failure_position: %llu\n",
6016 lo->ll_pos_latest_start,
6017 lo->ll_pos_last_checkpoint,
6018 lo->ll_pos_first_inconsistent);
6020 seq_printf(m, "success_count: %u\n"
6021 "%s_dangling: %llu\n"
6022 "%s_unmatched_pair: %llu\n"
6023 "%s_multiple_referenced: %llu\n"
6025 "%s_inconsistent_owner: %llu\n"
6028 "failed_phase1: %llu\n"
6029 "failed_phase2: %llu\n",
6030 lo->ll_success_count,
6031 prefix, lo->ll_objs_repaired[LLIT_DANGLING - 1],
6032 prefix, lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
6033 prefix, lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
6034 prefix, lo->ll_objs_repaired[LLIT_ORPHAN - 1],
6035 prefix, lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
6036 prefix, lo->ll_objs_repaired[LLIT_OTHERS - 1],
6037 lo->ll_objs_skipped,
6038 lo->ll_objs_failed_phase1,
6039 lo->ll_objs_failed_phase2);
6041 if (lo->ll_status == LS_SCANNING_PHASE1) {
6042 time64_t duration = ktime_get_seconds() -
6043 lfsck->li_time_last_checkpoint;
6044 u64 checked = lo->ll_objs_checked_phase1 +
6045 com->lc_new_checked;
6046 u64 speed = checked;
6047 u64 new_checked = com->lc_new_checked;
6048 time64_t rtime = lo->ll_run_time_phase1 + duration;
6052 new_checked = div64_s64(new_checked, duration);
6054 speed = div64_s64(speed, rtime);
6055 seq_printf(m, "checked_phase1: %llu\n"
6056 "checked_phase2: %llu\n"
6057 "run_time_phase1: %lld seconds\n"
6058 "run_time_phase2: %lld seconds\n"
6059 "average_speed_phase1: %llu items/sec\n"
6060 "average_speed_phase2: N/A\n"
6061 "real-time_speed_phase1: %llu items/sec\n"
6062 "real-time_speed_phase2: N/A\n",
6064 lo->ll_objs_checked_phase2,
6066 lo->ll_run_time_phase2,
6070 if (likely(lfsck->li_di_oit)) {
6071 const struct dt_it_ops *iops =
6072 &lfsck->li_obj_oit->do_index_ops->dio_it;
6074 /* The low layer otable-based iteration position may NOT
6075 * exactly match the layout-based directory traversal
6076 * cookie. Generally, it is not a serious issue. But the
6077 * caller should NOT make assumption on that. */
6078 pos = iops->store(env, lfsck->li_di_oit);
6079 if (!lfsck->li_current_oit_processed)
6082 pos = lo->ll_pos_last_checkpoint;
6085 seq_printf(m, "current_position: %llu\n", pos);
6086 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
6087 time64_t duration = ktime_get_seconds() -
6088 com->lc_time_last_checkpoint;
6089 u64 checked = lo->ll_objs_checked_phase2 +
6090 com->lc_new_checked;
6091 u64 speed1 = lo->ll_objs_checked_phase1;
6092 u64 speed2 = checked;
6093 u64 new_checked = com->lc_new_checked;
6094 time64_t rtime = lo->ll_run_time_phase2 + duration;
6097 new_checked = div64_s64(new_checked, duration);
6098 if (lo->ll_run_time_phase1 != 0)
6099 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6101 speed2 = div64_s64(speed2, rtime);
6102 seq_printf(m, "checked_phase1: %llu\n"
6103 "checked_phase2: %llu\n"
6104 "run_time_phase1: %lld seconds\n"
6105 "run_time_phase2: %lld seconds\n"
6106 "average_speed_phase1: %llu items/sec\n"
6107 "average_speed_phase2: %llu items/sec\n"
6108 "real-time_speed_phase1: N/A\n"
6109 "real-time_speed_phase2: %llu items/sec\n"
6110 "current_position: "DFID"\n",
6111 lo->ll_objs_checked_phase1,
6113 lo->ll_run_time_phase1,
6118 PFID(&com->lc_fid_latest_scanned_phase2));
6120 __u64 speed1 = lo->ll_objs_checked_phase1;
6121 __u64 speed2 = lo->ll_objs_checked_phase2;
6123 if (lo->ll_run_time_phase1 != 0)
6124 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6125 if (lo->ll_run_time_phase2 != 0)
6126 speed2 = div64_s64(speed2, lo->ll_run_time_phase2);
6127 seq_printf(m, "checked_phase1: %llu\n"
6128 "checked_phase2: %llu\n"
6129 "run_time_phase1: %lld seconds\n"
6130 "run_time_phase2: %lld seconds\n"
6131 "average_speed_phase1: %llu items/sec\n"
6132 "average_speed_phase2: %llu objs/sec\n"
6133 "real-time_speed_phase1: N/A\n"
6134 "real-time_speed_phase2: N/A\n"
6135 "current_position: N/A\n",
6136 lo->ll_objs_checked_phase1,
6137 lo->ll_objs_checked_phase2,
6138 lo->ll_run_time_phase1,
6139 lo->ll_run_time_phase2,
6144 up_read(&com->lc_sem);
6147 static int lfsck_layout_master_double_scan(const struct lu_env *env,
6148 struct lfsck_component *com)
6150 struct lfsck_layout *lo = com->lc_file_ram;
6151 struct lfsck_assistant_data *lad = com->lc_data;
6152 struct lfsck_instance *lfsck = com->lc_lfsck;
6153 struct lfsck_tgt_descs *ltds;
6154 struct lfsck_tgt_desc *ltd;
6155 struct lfsck_tgt_desc *next;
6158 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
6160 if (thread_is_stopped(&lad->lad_thread)) {
6161 LASSERT(list_empty(&lad->lad_req_list));
6162 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6163 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6165 ltds = &lfsck->li_ost_descs;
6166 spin_lock(<ds->ltd_lock);
6167 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6168 ltd_layout_phase_list) {
6169 list_del_init(<d->ltd_layout_phase_list);
6171 spin_unlock(<ds->ltd_lock);
6173 ltds = &lfsck->li_mdt_descs;
6174 spin_lock(<ds->ltd_lock);
6175 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6176 ltd_layout_phase_list) {
6177 list_del_init(<d->ltd_layout_phase_list);
6179 spin_unlock(<ds->ltd_lock);
6185 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6186 struct lfsck_component *com)
6188 struct lfsck_instance *lfsck = com->lc_lfsck;
6189 struct lfsck_layout_slave_data *llsd = com->lc_data;
6190 struct lfsck_layout *lo = com->lc_file_ram;
6191 struct ptlrpc_thread *thread = &lfsck->li_thread;
6195 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6196 lfsck_lfsck2name(lfsck));
6198 atomic_inc(&lfsck->li_double_scan_count);
6200 if (lo->ll_flags & LF_INCOMPLETE)
6203 com->lc_new_checked = 0;
6204 com->lc_new_scanned = 0;
6205 com->lc_time_last_checkpoint = ktime_get_seconds();
6206 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6207 LFSCK_CHECKPOINT_INTERVAL;
6210 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
6213 rc = lfsck_layout_slave_query_master(env, com);
6214 if (list_empty(&llsd->llsd_master_list)) {
6215 if (unlikely(!thread_is_running(thread)))
6226 rc = l_wait_event(thread->t_ctl_waitq,
6227 !thread_is_running(thread) ||
6228 lo->ll_flags & LF_INCOMPLETE ||
6229 list_empty(&llsd->llsd_master_list),
6231 if (unlikely(!thread_is_running(thread)))
6234 if (lo->ll_flags & LF_INCOMPLETE)
6237 if (rc == -ETIMEDOUT)
6240 GOTO(done, rc = (rc < 0 ? rc : 1));
6244 rc = lfsck_layout_double_scan_result(env, com, rc);
6245 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6246 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6247 lfsck_layout_slave_quit(env, com);
6248 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6249 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6251 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6252 "status %d: rc = %d\n",
6253 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6258 static void lfsck_layout_master_data_release(const struct lu_env *env,
6259 struct lfsck_component *com)
6261 struct lfsck_assistant_data *lad = com->lc_data;
6262 struct lfsck_instance *lfsck = com->lc_lfsck;
6263 struct lfsck_tgt_descs *ltds;
6264 struct lfsck_tgt_desc *ltd;
6265 struct lfsck_tgt_desc *next;
6267 LASSERT(lad != NULL);
6268 LASSERT(thread_is_init(&lad->lad_thread) ||
6269 thread_is_stopped(&lad->lad_thread));
6270 LASSERT(list_empty(&lad->lad_req_list));
6272 com->lc_data = NULL;
6274 ltds = &lfsck->li_ost_descs;
6275 spin_lock(<ds->ltd_lock);
6276 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6277 ltd_layout_phase_list) {
6278 list_del_init(<d->ltd_layout_phase_list);
6280 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6281 ltd_layout_phase_list) {
6282 list_del_init(<d->ltd_layout_phase_list);
6284 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6286 list_del_init(<d->ltd_layout_list);
6288 spin_unlock(<ds->ltd_lock);
6290 ltds = &lfsck->li_mdt_descs;
6291 spin_lock(<ds->ltd_lock);
6292 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6293 ltd_layout_phase_list) {
6294 list_del_init(<d->ltd_layout_phase_list);
6296 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6297 ltd_layout_phase_list) {
6298 list_del_init(<d->ltd_layout_phase_list);
6300 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6302 list_del_init(<d->ltd_layout_list);
6304 spin_unlock(<ds->ltd_lock);
6306 if (likely(lad->lad_bitmap != NULL))
6307 CFS_FREE_BITMAP(lad->lad_bitmap);
6312 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6313 struct lfsck_component *com)
6315 struct lfsck_layout_slave_data *llsd = com->lc_data;
6317 lfsck_layout_slave_quit(env, com);
6318 com->lc_data = NULL;
6322 static void lfsck_layout_master_quit(const struct lu_env *env,
6323 struct lfsck_component *com)
6325 struct lfsck_assistant_data *lad = com->lc_data;
6326 struct lfsck_instance *lfsck = com->lc_lfsck;
6327 struct lfsck_tgt_descs *ltds;
6328 struct lfsck_tgt_desc *ltd;
6329 struct lfsck_tgt_desc *next;
6331 LASSERT(lad != NULL);
6333 lfsck_quit_generic(env, com);
6335 LASSERT(thread_is_init(&lad->lad_thread) ||
6336 thread_is_stopped(&lad->lad_thread));
6337 LASSERT(list_empty(&lad->lad_req_list));
6339 ltds = &lfsck->li_ost_descs;
6340 spin_lock(<ds->ltd_lock);
6341 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6342 ltd_layout_phase_list) {
6343 list_del_init(<d->ltd_layout_phase_list);
6345 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6346 ltd_layout_phase_list) {
6347 list_del_init(<d->ltd_layout_phase_list);
6349 spin_unlock(<ds->ltd_lock);
6351 ltds = &lfsck->li_mdt_descs;
6352 spin_lock(<ds->ltd_lock);
6353 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6354 ltd_layout_phase_list) {
6355 list_del_init(<d->ltd_layout_phase_list);
6357 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6358 ltd_layout_phase_list) {
6359 list_del_init(<d->ltd_layout_phase_list);
6361 spin_unlock(<ds->ltd_lock);
6364 static void lfsck_layout_slave_quit(const struct lu_env *env,
6365 struct lfsck_component *com)
6367 struct lfsck_layout_slave_data *llsd = com->lc_data;
6368 struct lfsck_layout_seq *lls;
6369 struct lfsck_layout_seq *next;
6370 struct lfsck_layout_slave_target *llst;
6372 LASSERT(llsd != NULL);
6374 down_write(&com->lc_sem);
6375 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6377 list_del_init(&lls->lls_list);
6378 lfsck_object_put(env, lls->lls_lastid_obj);
6381 up_write(&com->lc_sem);
6383 spin_lock(&llsd->llsd_lock);
6384 while (!list_empty(&llsd->llsd_master_list)) {
6385 llst = list_entry(llsd->llsd_master_list.next,
6386 struct lfsck_layout_slave_target, llst_list);
6387 list_del_init(&llst->llst_list);
6388 spin_unlock(&llsd->llsd_lock);
6389 lfsck_layout_llst_put(llst);
6390 spin_lock(&llsd->llsd_lock);
6392 spin_unlock(&llsd->llsd_lock);
6394 lfsck_rbtree_cleanup(env, com);
6397 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6398 struct lfsck_component *com,
6399 struct lfsck_request *lr)
6401 struct lfsck_instance *lfsck = com->lc_lfsck;
6402 struct lfsck_layout *lo = com->lc_file_ram;
6403 struct lfsck_assistant_data *lad = com->lc_data;
6404 struct lfsck_tgt_descs *ltds;
6405 struct lfsck_tgt_desc *ltd;
6409 if (lr->lr_event == LE_PAIRS_VERIFY) {
6412 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6419 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6420 "from %s %x, status %d, flags %x, flags2 %x\n",
6421 lfsck_lfsck2name(lfsck), lr->lr_event,
6422 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6423 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6425 if (lr->lr_event != LE_PHASE1_DONE &&
6426 lr->lr_event != LE_PHASE2_DONE &&
6427 lr->lr_event != LE_PEER_EXIT)
6430 if (lr->lr_flags & LEF_FROM_OST)
6431 ltds = &lfsck->li_ost_descs;
6433 ltds = &lfsck->li_mdt_descs;
6434 spin_lock(<ds->ltd_lock);
6435 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6437 spin_unlock(<ds->ltd_lock);
6442 list_del_init(<d->ltd_layout_phase_list);
6443 switch (lr->lr_event) {
6444 case LE_PHASE1_DONE:
6445 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6446 if (lr->lr_flags2 & LF_INCOMPLETE) {
6447 if (lr->lr_flags & LEF_FROM_OST)
6448 lfsck_lad_set_bitmap(env, com,
6451 lo->ll_flags |= LF_INCOMPLETE;
6453 ltd->ltd_layout_done = 1;
6454 list_del_init(<d->ltd_layout_list);
6459 if (lr->lr_flags & LEF_FROM_OST) {
6460 if (list_empty(<d->ltd_layout_list))
6461 list_add_tail(<d->ltd_layout_list,
6462 &lad->lad_ost_list);
6463 list_add_tail(<d->ltd_layout_phase_list,
6464 &lad->lad_ost_phase2_list);
6466 if (list_empty(<d->ltd_layout_list))
6467 list_add_tail(<d->ltd_layout_list,
6468 &lad->lad_mdt_list);
6469 list_add_tail(<d->ltd_layout_phase_list,
6470 &lad->lad_mdt_phase2_list);
6473 case LE_PHASE2_DONE:
6474 ltd->ltd_layout_done = 1;
6475 if (!list_empty(<d->ltd_layout_list))
6476 list_del_init(<d->ltd_layout_list);
6478 if (lr->lr_flags2 & LF_INCOMPLETE) {
6479 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6486 ltd->ltd_layout_done = 1;
6487 list_del_init(<d->ltd_layout_list);
6488 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6489 !(lr->lr_flags & LEF_FROM_OST))
6490 lo->ll_flags |= LF_INCOMPLETE;
6495 spin_unlock(<ds->ltd_lock);
6497 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6498 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6500 memset(stop, 0, sizeof(*stop));
6501 stop->ls_status = lr->lr_status;
6502 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6503 lfsck_stop(env, lfsck->li_bottom, stop);
6504 } else if (lfsck_phase2_next_ready(lad)) {
6505 wake_up_all(&lad->lad_thread.t_ctl_waitq);
6511 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6512 struct lfsck_component *com,
6513 struct lfsck_req_local *lrl,
6518 switch (lrl->lrl_event) {
6519 case LEL_FID_ACCESSED:
6520 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6522 case LEL_PAIRS_VERIFY_LOCAL: {
6525 lrl->lrl_status = LPVS_INIT;
6526 /* Firstly, if the MDT-object which is claimed via OST-object
6527 * local stored PFID xattr recognizes the OST-object, then it
6528 * must be that the client given PFID is wrong. */
6529 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6530 &lrl->lrl_ff_local.ff_parent,
6531 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6535 lrl->lrl_status = LPVS_INCONSISTENT;
6536 /* The OST-object local stored PFID xattr is stale. We need to
6537 * check whether the MDT-object that is claimed via the client
6538 * given PFID information recognizes the OST-object or not. If
6539 * matches, then need to update the OST-object's PFID xattr. */
6540 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6541 &lrl->lrl_ff_client.ff_parent,
6542 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6544 * We are not sure whether the client given PFID information
6545 * is correct or not, do nothing to avoid improper fixing.
6548 * The client given PFID information is also invalid, we can
6549 * NOT fix the OST-object inconsistency.
6552 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6553 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6562 RETURN(-EOPNOTSUPP);
6565 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6566 struct lfsck_component *com,
6567 struct lfsck_request *lr)
6569 struct lfsck_instance *lfsck = com->lc_lfsck;
6570 struct lfsck_layout_slave_data *llsd = com->lc_data;
6571 struct lfsck_layout_slave_target *llst;
6575 switch (lr->lr_event) {
6576 case LE_CONDITIONAL_DESTROY:
6577 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6579 case LE_PHASE1_DONE: {
6580 if (lr->lr_flags2 & LF_INCOMPLETE) {
6581 struct lfsck_layout *lo = com->lc_file_ram;
6583 lo->ll_flags |= LF_INCOMPLETE;
6584 llst = lfsck_layout_llst_find_and_del(llsd,
6588 lfsck_layout_llst_put(llst);
6589 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6595 case LE_PHASE2_DONE:
6597 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6598 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6599 lr->lr_event, lr->lr_index, lr->lr_status);
6605 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6609 lfsck_layout_llst_put(llst);
6610 if (list_empty(&llsd->llsd_master_list))
6611 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6613 if (lr->lr_event == LE_PEER_EXIT &&
6614 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6615 (list_empty(&llsd->llsd_master_list) &&
6616 (lr->lr_status == LS_STOPPED ||
6617 lr->lr_status == LS_CO_STOPPED)))) {
6618 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6620 memset(stop, 0, sizeof(*stop));
6621 stop->ls_status = lr->lr_status;
6622 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6623 lfsck_stop(env, lfsck->li_bottom, stop);
6629 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6633 for (i = 0; i < LLIT_MAX; i++)
6634 *count += lo->ll_objs_repaired[i];
6637 static int lfsck_layout_query_all(const struct lu_env *env,
6638 struct lfsck_component *com,
6639 __u32 *mdts_count, __u32 *osts_count,
6642 struct lfsck_layout *lo = com->lc_file_ram;
6643 struct lfsck_tgt_descs *ltds;
6644 struct lfsck_tgt_desc *ltd;
6649 rc = lfsck_query_all(env, com);
6653 ltds = &com->lc_lfsck->li_mdt_descs;
6654 down_read(<ds->ltd_rw_sem);
6655 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6656 ltd = lfsck_ltd2tgt(ltds, idx);
6657 LASSERT(ltd != NULL);
6659 mdts_count[ltd->ltd_layout_status]++;
6660 *repaired += ltd->ltd_layout_repaired;
6662 up_read(<ds->ltd_rw_sem);
6664 ltds = &com->lc_lfsck->li_ost_descs;
6665 down_read(<ds->ltd_rw_sem);
6666 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6667 ltd = lfsck_ltd2tgt(ltds, idx);
6668 LASSERT(ltd != NULL);
6670 osts_count[ltd->ltd_layout_status]++;
6671 *repaired += ltd->ltd_layout_repaired;
6673 up_read(<ds->ltd_rw_sem);
6675 down_read(&com->lc_sem);
6676 mdts_count[lo->ll_status]++;
6677 lfsck_layout_repaired(lo, repaired);
6678 up_read(&com->lc_sem);
6683 static int lfsck_layout_query(const struct lu_env *env,
6684 struct lfsck_component *com,
6685 struct lfsck_request *req,
6686 struct lfsck_reply *rep,
6687 struct lfsck_query *que, int idx)
6689 struct lfsck_layout *lo = com->lc_file_ram;
6693 LASSERT(com->lc_lfsck->li_master);
6695 rc = lfsck_layout_query_all(env, com,
6696 que->lu_mdts_count[idx],
6697 que->lu_osts_count[idx],
6698 &que->lu_repaired[idx]);
6700 down_read(&com->lc_sem);
6701 rep->lr_status = lo->ll_status;
6702 if (req->lr_flags & LEF_QUERY_ALL)
6703 lfsck_layout_repaired(lo, &rep->lr_repaired);
6704 up_read(&com->lc_sem);
6710 /* with lfsck::li_lock held */
6711 static int lfsck_layout_slave_join(const struct lu_env *env,
6712 struct lfsck_component *com,
6713 struct lfsck_start_param *lsp)
6715 struct lfsck_instance *lfsck = com->lc_lfsck;
6716 struct lfsck_layout_slave_data *llsd = com->lc_data;
6717 struct lfsck_layout_slave_target *llst;
6718 struct lfsck_start *start = lsp->lsp_start;
6722 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6725 if (!lsp->lsp_index_valid)
6728 /* If someone is running the LFSCK without orphan handling,
6729 * it will not maintain the object accessing rbtree. So we
6730 * cannot join it for orphan handling. */
6731 if (!llsd->llsd_rbtree_valid)
6734 spin_unlock(&lfsck->li_lock);
6735 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6736 spin_lock(&lfsck->li_lock);
6737 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6738 spin_unlock(&lfsck->li_lock);
6739 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6742 lfsck_layout_llst_put(llst);
6743 spin_lock(&lfsck->li_lock);
6750 static struct lfsck_operations lfsck_layout_master_ops = {
6751 .lfsck_reset = lfsck_layout_reset,
6752 .lfsck_fail = lfsck_layout_fail,
6753 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6754 .lfsck_prep = lfsck_layout_master_prep,
6755 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6756 .lfsck_exec_dir = lfsck_layout_exec_dir,
6757 .lfsck_post = lfsck_layout_master_post,
6758 .lfsck_dump = lfsck_layout_dump,
6759 .lfsck_double_scan = lfsck_layout_master_double_scan,
6760 .lfsck_data_release = lfsck_layout_master_data_release,
6761 .lfsck_quit = lfsck_layout_master_quit,
6762 .lfsck_in_notify = lfsck_layout_master_in_notify,
6763 .lfsck_query = lfsck_layout_query,
6766 static struct lfsck_operations lfsck_layout_slave_ops = {
6767 .lfsck_reset = lfsck_layout_reset,
6768 .lfsck_fail = lfsck_layout_fail,
6769 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6770 .lfsck_prep = lfsck_layout_slave_prep,
6771 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6772 .lfsck_exec_dir = lfsck_layout_exec_dir,
6773 .lfsck_post = lfsck_layout_slave_post,
6774 .lfsck_dump = lfsck_layout_dump,
6775 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6776 .lfsck_data_release = lfsck_layout_slave_data_release,
6777 .lfsck_quit = lfsck_layout_slave_quit,
6778 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6779 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6780 .lfsck_query = lfsck_layout_query,
6781 .lfsck_join = lfsck_layout_slave_join,
6784 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6785 struct lfsck_component *com,
6786 struct lfsck_position *pos)
6788 struct lfsck_assistant_data *lad = com->lc_data;
6789 struct lfsck_layout_req *llr;
6791 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6795 if (list_empty(&lad->lad_req_list))
6798 llr = list_entry(lad->lad_req_list.next,
6799 struct lfsck_layout_req,
6801 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6804 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6805 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
6806 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
6807 .la_fill_pos = lfsck_layout_assistant_fill_pos,
6808 .la_double_scan_result = lfsck_layout_double_scan_result,
6809 .la_req_fini = lfsck_layout_assistant_req_fini,
6810 .la_sync_failures = lfsck_layout_assistant_sync_failures,
6813 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
6815 struct lfsck_component *com;
6816 struct lfsck_layout *lo;
6817 struct dt_object *root = NULL;
6818 struct dt_object *obj;
6827 INIT_LIST_HEAD(&com->lc_link);
6828 INIT_LIST_HEAD(&com->lc_link_dir);
6829 init_rwsem(&com->lc_sem);
6830 atomic_set(&com->lc_ref, 1);
6831 com->lc_lfsck = lfsck;
6832 com->lc_type = LFSCK_TYPE_LAYOUT;
6833 if (lfsck->li_master) {
6834 com->lc_ops = &lfsck_layout_master_ops;
6835 com->lc_data = lfsck_assistant_data_init(
6836 &lfsck_layout_assistant_ops,
6838 if (com->lc_data == NULL)
6839 GOTO(out, rc = -ENOMEM);
6841 for (i = 0; i < LFSCK_STF_COUNT; i++)
6842 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6844 struct lfsck_layout_slave_data *llsd;
6846 com->lc_ops = &lfsck_layout_slave_ops;
6847 OBD_ALLOC_PTR(llsd);
6849 GOTO(out, rc = -ENOMEM);
6851 INIT_LIST_HEAD(&llsd->llsd_seq_list);
6852 INIT_LIST_HEAD(&llsd->llsd_master_list);
6853 spin_lock_init(&llsd->llsd_lock);
6854 llsd->llsd_rb_root = RB_ROOT;
6855 init_rwsem(&llsd->llsd_rb_rwsem);
6856 com->lc_data = llsd;
6858 com->lc_file_size = sizeof(*lo);
6859 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6860 if (com->lc_file_ram == NULL)
6861 GOTO(out, rc = -ENOMEM);
6863 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6864 if (com->lc_file_disk == NULL)
6865 GOTO(out, rc = -ENOMEM);
6867 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6869 GOTO(out, rc = PTR_ERR(root));
6871 if (unlikely(!dt_try_as_dir(env, root)))
6872 GOTO(out, rc = -ENOTDIR);
6874 obj = local_file_find_or_create(env, lfsck->li_los, root,
6876 S_IFREG | S_IRUGO | S_IWUSR);
6878 GOTO(out, rc = PTR_ERR(obj));
6881 rc = lfsck_layout_load(env, com);
6883 rc = lfsck_layout_reset(env, com, true);
6884 } else if (rc == -ENOENT) {
6885 rc = lfsck_layout_init(env, com);
6886 } else if (lfsck->li_master) {
6887 rc = lfsck_load_sub_trace_files(env, com,
6888 &dt_lfsck_layout_dangling_features,
6889 LFSCK_LAYOUT, false);
6891 rc = lfsck_layout_reset(env, com, true);
6897 lo = com->lc_file_ram;
6898 switch (lo->ll_status) {
6904 spin_lock(&lfsck->li_lock);
6905 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6906 spin_unlock(&lfsck->li_lock);
6909 CERROR("%s: unknown lfsck_layout status %d\n",
6910 lfsck_lfsck2name(lfsck), lo->ll_status);
6912 case LS_SCANNING_PHASE1:
6913 case LS_SCANNING_PHASE2:
6914 /* No need to store the status to disk right now.
6915 * If the system crashed before the status stored,
6916 * it will be loaded back when next time. */
6917 lo->ll_status = LS_CRASHED;
6918 if (!lfsck->li_master)
6919 lo->ll_flags |= LF_INCOMPLETE;
6926 spin_lock(&lfsck->li_lock);
6927 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6928 spin_unlock(&lfsck->li_lock);
6932 if (lo->ll_flags & LF_CRASHED_LASTID) {
6933 LASSERT(lfsck->li_out_notify != NULL);
6935 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6936 LE_LASTID_REBUILDING);
6942 if (root != NULL && !IS_ERR(root))
6943 lfsck_object_put(env, root);
6946 lfsck_component_cleanup(env, com);
6947 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
6948 lfsck_lfsck2name(lfsck), rc);
6954 struct lfsck_orphan_it {
6955 struct lfsck_component *loi_com;
6956 struct lfsck_rbtree_node *loi_lrn;
6957 struct lfsck_layout_slave_target *loi_llst;
6958 struct lu_fid loi_key;
6959 struct lu_orphan_rec_v3 loi_rec;
6961 unsigned int loi_over:1;
6964 static int lfsck_fid_match_idx(const struct lu_env *env,
6965 struct lfsck_instance *lfsck,
6966 const struct lu_fid *fid, int idx)
6968 struct seq_server_site *ss;
6969 struct lu_server_fld *sf;
6970 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
6973 /* All abnormal cases will be returned to MDT0. */
6974 if (!fid_is_norm(fid)) {
6981 ss = lfsck_dev_site(lfsck);
6982 if (unlikely(ss == NULL))
6985 sf = ss->ss_server_fld;
6986 LASSERT(sf != NULL);
6988 fld_range_set_any(range);
6989 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
6993 if (!fld_range_is_mdt(range))
6996 if (range->lsr_index == idx)
7002 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
7003 struct dt_object *obj)
7005 struct dt_device *dev = lfsck_obj2dev(obj);
7006 struct thandle *handle;
7010 handle = dt_trans_create(env, dev);
7014 rc = dt_declare_ref_del(env, obj, handle);
7018 rc = dt_declare_destroy(env, obj, handle);
7022 rc = dt_trans_start_local(env, dev, handle);
7026 dt_write_lock(env, obj, 0);
7027 rc = dt_ref_del(env, obj, handle);
7029 rc = dt_destroy(env, obj, handle);
7030 dt_write_unlock(env, obj);
7035 dt_trans_stop(env, dev, handle);
7037 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
7038 PFID(lfsck_dto2fid(obj)), rc);
7043 static int lfsck_orphan_index_lookup(const struct lu_env *env,
7044 struct dt_object *dt,
7046 const struct dt_key *key)
7051 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
7052 struct dt_object *dt,
7053 const struct dt_rec *rec,
7054 const struct dt_key *key,
7055 struct thandle *handle)
7060 static int lfsck_orphan_index_insert(const struct lu_env *env,
7061 struct dt_object *dt,
7062 const struct dt_rec *rec,
7063 const struct dt_key *key,
7064 struct thandle *handle)
7069 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
7070 struct dt_object *dt,
7071 const struct dt_key *key,
7072 struct thandle *handle)
7077 static int lfsck_orphan_index_delete(const struct lu_env *env,
7078 struct dt_object *dt,
7079 const struct dt_key *key,
7080 struct thandle *handle)
7085 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
7086 struct dt_object *dt,
7089 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
7090 struct lfsck_instance *lfsck;
7091 struct lfsck_component *com = NULL;
7092 struct lfsck_layout_slave_data *llsd;
7093 struct lfsck_orphan_it *it = NULL;
7094 struct lfsck_layout *lo;
7098 lfsck = lfsck_instance_find(dev, true, false);
7099 if (unlikely(lfsck == NULL))
7100 RETURN(ERR_PTR(-ENXIO));
7102 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
7103 if (unlikely(com == NULL))
7104 GOTO(out, rc = -ENOENT);
7106 lo = com->lc_file_ram;
7107 if (lo->ll_flags & LF_INCOMPLETE)
7108 GOTO(out, rc = -ESRCH);
7110 llsd = com->lc_data;
7111 if (!llsd->llsd_rbtree_valid)
7112 GOTO(out, rc = -ESRCH);
7116 GOTO(out, rc = -ENOMEM);
7118 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
7119 if (it->loi_llst == NULL)
7120 GOTO(out, rc = -ENXIO);
7122 if (dev->dd_record_fid_accessed) {
7123 /* The first iteration against the rbtree, scan the whole rbtree
7124 * to remove the nodes which do NOT need to be handled. */
7125 down_write(&llsd->llsd_rb_rwsem);
7126 if (dev->dd_record_fid_accessed) {
7127 struct rb_node *node;
7128 struct rb_node *next;
7129 struct lfsck_rbtree_node *lrn;
7131 /* No need to record the fid accessing anymore. */
7132 dev->dd_record_fid_accessed = 0;
7134 node = rb_first(&llsd->llsd_rb_root);
7135 while (node != NULL) {
7136 next = rb_next(node);
7137 lrn = rb_entry(node, struct lfsck_rbtree_node,
7139 if (atomic_read(&lrn->lrn_known_count) <=
7140 atomic_read(&lrn->lrn_accessed_count)) {
7141 rb_erase(node, &llsd->llsd_rb_root);
7142 lfsck_rbtree_free(lrn);
7147 up_write(&llsd->llsd_rb_rwsem);
7150 /* read lock the rbtree when init, and unlock when fini */
7151 down_read(&llsd->llsd_rb_rwsem);
7159 lfsck_component_put(env, com);
7161 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
7162 lfsck_lfsck2name(lfsck), rc);
7164 lfsck_instance_put(env, lfsck);
7169 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7172 return (struct dt_it *)it;
7175 static void lfsck_orphan_it_fini(const struct lu_env *env,
7178 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7179 struct lfsck_component *com = it->loi_com;
7180 struct lfsck_layout_slave_data *llsd;
7181 struct lfsck_layout_slave_target *llst;
7184 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7185 lfsck_lfsck2name(com->lc_lfsck));
7187 llsd = com->lc_data;
7188 up_read(&llsd->llsd_rb_rwsem);
7189 llst = it->loi_llst;
7190 LASSERT(llst != NULL);
7192 /* Save the key and hash for iterate next. */
7193 llst->llst_fid = it->loi_key;
7194 llst->llst_hash = it->loi_hash;
7195 lfsck_layout_llst_put(llst);
7196 lfsck_component_put(env, com);
7202 * \retval +1: the iteration finished
7203 * \retval 0: on success, not finished
7204 * \retval -ve: on error
7206 static int lfsck_orphan_it_next(const struct lu_env *env,
7209 struct lfsck_thread_info *info = lfsck_env_info(env);
7210 struct filter_fid *ff = &info->lti_ff;
7211 struct lu_attr *la = &info->lti_la;
7212 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7213 struct lu_fid *key = &it->loi_key;
7214 struct lu_orphan_rec_v3 *rec = &it->loi_rec;
7215 struct ost_layout *ol = &rec->lor_layout;
7216 struct lfsck_component *com = it->loi_com;
7217 struct lfsck_instance *lfsck = com->lc_lfsck;
7218 struct lfsck_layout_slave_data *llsd = com->lc_data;
7219 struct dt_object *obj;
7220 struct lfsck_rbtree_node *lrn;
7224 __u32 idx = it->loi_llst->llst_index;
7234 lrn = lfsck_rbtree_search(llsd, key, &exact);
7242 key->f_seq = lrn->lrn_seq;
7243 key->f_oid = lrn->lrn_first_oid;
7248 if (unlikely(key->f_oid == 0)) {
7255 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7261 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7262 atomic_read(&lrn->lrn_accessed_count))) {
7263 struct rb_node *next = rb_next(&lrn->lrn_node);
7265 while (next != NULL) {
7266 lrn = rb_entry(next, struct lfsck_rbtree_node,
7268 if (atomic_read(&lrn->lrn_known_count) >
7269 atomic_read(&lrn->lrn_accessed_count))
7271 next = rb_next(next);
7280 key->f_seq = lrn->lrn_seq;
7281 key->f_oid = lrn->lrn_first_oid;
7285 pos = key->f_oid - lrn->lrn_first_oid;
7288 pos = find_next_bit(lrn->lrn_known_bitmap,
7289 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7290 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7291 key->f_oid = lrn->lrn_first_oid + pos;
7292 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7300 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7305 key->f_oid = lrn->lrn_first_oid + pos;
7306 obj = lfsck_object_find_bottom(env, lfsck, key);
7309 if (rc == -ENOENT) {
7316 dt_read_lock(env, obj, 0);
7317 if (dt_object_exists(obj) == 0 ||
7318 lfsck_is_dead_obj(obj)) {
7319 dt_read_unlock(env, obj);
7320 lfsck_object_put(env, obj);
7325 rc = dt_attr_get(env, obj, la);
7329 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7331 if (rc == -ENODATA) {
7332 /* For the pre-created OST-object, update the bitmap to avoid
7333 * others LFSCK (second phase) iteration to touch it again. */
7334 if (la->la_ctime == 0) {
7335 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7336 atomic_inc(&lrn->lrn_accessed_count);
7338 /* For the race between repairing dangling referenced
7339 * MDT-object and unlink the file, it may left orphan
7340 * OST-object there. Destroy it now! */
7341 if (unlikely(!(la->la_mode & S_ISUID))) {
7342 dt_read_unlock(env, obj);
7343 lfsck_layout_destroy_orphan(env, obj);
7344 lfsck_object_put(env, obj);
7348 } else if (idx == 0) {
7349 /* If the orphan OST-object has no parent information,
7350 * regard it as referenced by the MDT-object on MDT0. */
7351 fid_zero(&rec->lor_rec.lor_fid);
7352 rec->lor_rec.lor_uid = la->la_uid;
7353 rec->lor_rec.lor_gid = la->la_gid;
7354 memset(ol, 0, sizeof(*ol));
7355 rec->lor_layout_version = 0;
7361 dt_read_unlock(env, obj);
7362 lfsck_object_put(env, obj);
7367 if (rc < sizeof(struct lu_fid))
7368 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7370 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7371 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7372 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7373 * parent MDT-object's layout EA. */
7374 save = rec->lor_rec.lor_fid.f_stripe_idx;
7375 rec->lor_rec.lor_fid.f_ver = 0;
7376 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7377 /* If the orphan OST-object does not claim the MDT, then next.
7379 * If we do not know whether it matches or not, then return it
7380 * to the MDT for further check. */
7382 dt_read_unlock(env, obj);
7383 lfsck_object_put(env, obj);
7388 rec->lor_rec.lor_fid.f_stripe_idx = save;
7389 rec->lor_rec.lor_uid = la->la_uid;
7390 rec->lor_rec.lor_gid = la->la_gid;
7391 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7392 rec->lor_layout_version =
7393 le32_to_cpu(ff->ff_layout_version & ~LU_LAYOUT_RESYNC);
7394 rec->lor_range = le32_to_cpu(ff->ff_range);
7396 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7397 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7398 "COMP end %llu, layout version %u, range %u\n",
7399 lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7400 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7401 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7402 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
7403 rec->lor_layout_version, rec->lor_range);
7408 dt_read_unlock(env, obj);
7409 lfsck_object_put(env, obj);
7417 * \retval +1: locate to the exactly position
7418 * \retval 0: cannot locate to the exactly position,
7419 * call next() to move to a valid position.
7420 * \retval -ve: on error
7422 static int lfsck_orphan_it_get(const struct lu_env *env,
7424 const struct dt_key *key)
7426 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7429 it->loi_key = *(struct lu_fid *)key;
7430 rc = lfsck_orphan_it_next(env, di);
7440 static void lfsck_orphan_it_put(const struct lu_env *env,
7445 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7446 const struct dt_it *di)
7448 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7450 return (struct dt_key *)&it->loi_key;
7453 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7454 const struct dt_it *di)
7456 return sizeof(struct lu_fid);
7459 static int lfsck_orphan_it_rec(const struct lu_env *env,
7460 const struct dt_it *di,
7464 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7466 *(struct lu_orphan_rec_v3 *)rec = it->loi_rec;
7471 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7472 const struct dt_it *di)
7474 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7476 return it->loi_hash;
7480 * \retval +1: locate to the exactly position
7481 * \retval 0: cannot locate to the exactly position,
7482 * call next() to move to a valid position.
7483 * \retval -ve: on error
7485 static int lfsck_orphan_it_load(const struct lu_env *env,
7486 const struct dt_it *di,
7489 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7490 struct lfsck_layout_slave_target *llst = it->loi_llst;
7493 LASSERT(llst != NULL);
7495 if (hash != llst->llst_hash) {
7496 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7497 "iteration does not match the one when fini "
7498 "%llu, to be reset.\n",
7499 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7501 fid_zero(&llst->llst_fid);
7502 llst->llst_hash = 0;
7505 it->loi_key = llst->llst_fid;
7506 it->loi_hash = llst->llst_hash;
7507 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7517 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7518 const struct dt_it *di,
7524 const struct dt_index_operations lfsck_orphan_index_ops = {
7525 .dio_lookup = lfsck_orphan_index_lookup,
7526 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7527 .dio_insert = lfsck_orphan_index_insert,
7528 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7529 .dio_delete = lfsck_orphan_index_delete,
7531 .init = lfsck_orphan_it_init,
7532 .fini = lfsck_orphan_it_fini,
7533 .get = lfsck_orphan_it_get,
7534 .put = lfsck_orphan_it_put,
7535 .next = lfsck_orphan_it_next,
7536 .key = lfsck_orphan_it_key,
7537 .key_size = lfsck_orphan_it_key_size,
7538 .rec = lfsck_orphan_it_rec,
7539 .store = lfsck_orphan_it_store,
7540 .load = lfsck_orphan_it_load,
7541 .key_rec = lfsck_orphan_it_key_rec,