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 (!lad->lad_incomplete)
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(pattern) != LOV_PATTERN_RAID0) {
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(struct dt_object *obj,
383 struct lov_mds_md_v1 *lmm)
387 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1) {
388 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
390 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
392 if (unlikely(count == 0)) {
393 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
394 "components count 0\n",
395 PFID(lfsck_dto2fid(obj)));
400 for (i = 0; i < count && !rc; i++) {
401 struct lov_comp_md_entry_v1 *lcme =
402 &lcm->lcm_entries[i];
403 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
404 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
405 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
407 if (unlikely(comp_id == LCME_ID_INVAL ||
408 comp_id > LCME_ID_MAX)) {
409 CDEBUG(D_LFSCK, "found invalid FPL ID %u "
410 "for the file "DFID" at idx %d\n",
411 comp_id, PFID(lfsck_dto2fid(obj)), i);
416 if (unlikely(start >= end ||
417 !lfsck_comp_extent_aligned(start) ||
418 (!lfsck_comp_extent_aligned(end) &&
419 end != LUSTRE_EOF))) {
420 CDEBUG(D_LFSCK, "found invalid FPL extent "
421 "range [%llu - %llu) for the file "
423 start, end, PFID(lfsck_dto2fid(obj)), i);
428 rc = lfsck_layout_verify_header_v1v3(obj,
429 (struct lov_mds_md_v1 *)((char *)lmm +
430 le32_to_cpu(lcme->lcme_offset)), start,
434 rc = lfsck_layout_verify_header_v1v3(obj, lmm, 1, 0);
440 static int lfsck_layout_get_lovea(const struct lu_env *env,
441 struct dt_object *obj, struct lu_buf *buf)
447 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
449 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
451 return !rc ? -ENODATA : rc;
453 lu_buf_realloc(buf, rc);
454 if (buf->lb_buf == NULL)
461 return !rc ? -ENODATA : rc;
463 if (unlikely(buf->lb_buf == NULL)) {
464 lu_buf_alloc(buf, rc);
465 if (buf->lb_buf == NULL)
471 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf);
473 return rc1 ? rc1 : rc;
476 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
477 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
478 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
480 struct lfsck_rbtree_node {
481 struct rb_node lrn_node;
484 atomic_t lrn_known_count;
485 atomic_t lrn_accessed_count;
486 void *lrn_known_bitmap;
487 void *lrn_accessed_bitmap;
490 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
491 __u64 seq, __u32 oid)
493 if (seq < lrn->lrn_seq)
496 if (seq > lrn->lrn_seq)
499 if (oid < lrn->lrn_first_oid)
502 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
508 /* The caller should hold llsd->llsd_rb_lock. */
509 static struct lfsck_rbtree_node *
510 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
511 const struct lu_fid *fid, bool *exact)
513 struct rb_node *node = llsd->llsd_rb_root.rb_node;
514 struct rb_node *prev = NULL;
515 struct lfsck_rbtree_node *lrn = NULL;
521 while (node != NULL) {
523 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
524 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
526 node = node->rb_left;
528 node = node->rb_right;
536 /* If there is no exactly matched one, then to the next valid one. */
539 /* The rbtree is empty. */
546 node = rb_next(prev);
548 /* The end of the rbtree. */
552 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
557 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
558 const struct lu_fid *fid)
560 struct lfsck_rbtree_node *lrn;
564 return ERR_PTR(-ENOMEM);
566 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
567 if (lrn->lrn_known_bitmap == NULL) {
570 return ERR_PTR(-ENOMEM);
573 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
574 if (lrn->lrn_accessed_bitmap == NULL) {
575 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
578 return ERR_PTR(-ENOMEM);
581 RB_CLEAR_NODE(&lrn->lrn_node);
582 lrn->lrn_seq = fid_seq(fid);
583 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
584 atomic_set(&lrn->lrn_known_count, 0);
585 atomic_set(&lrn->lrn_accessed_count, 0);
590 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
592 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
593 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
597 /* The caller should hold lock. */
598 static struct lfsck_rbtree_node *
599 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
600 struct lfsck_rbtree_node *lrn)
602 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
603 struct rb_node *parent = NULL;
604 struct lfsck_rbtree_node *tmp;
607 while (*pos != NULL) {
609 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
610 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
612 pos = &(*pos)->rb_left;
614 pos = &(*pos)->rb_right;
619 rb_link_node(&lrn->lrn_node, parent, pos);
620 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
625 extern const struct dt_index_operations lfsck_orphan_index_ops;
627 static int lfsck_rbtree_setup(const struct lu_env *env,
628 struct lfsck_component *com)
630 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
631 struct lfsck_instance *lfsck = com->lc_lfsck;
632 struct dt_device *dev = lfsck->li_bottom;
633 struct lfsck_layout_slave_data *llsd = com->lc_data;
634 struct dt_object *obj;
636 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
637 fid->f_oid = lfsck_dev_idx(lfsck);
639 obj = dt_locate(env, dev, fid);
641 RETURN(PTR_ERR(obj));
643 /* Generate an in-RAM object to stand for the layout rbtree.
644 * Scanning the layout rbtree will be via the iteration over
645 * the object. In the future, the rbtree may be written onto
646 * disk with the object.
648 * Mark the object to be as exist. */
649 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
650 obj->do_index_ops = &lfsck_orphan_index_ops;
651 llsd->llsd_rb_obj = obj;
652 llsd->llsd_rbtree_valid = 1;
653 dev->dd_record_fid_accessed = 1;
655 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
656 lfsck_lfsck2name(lfsck));
661 static void lfsck_rbtree_cleanup(const struct lu_env *env,
662 struct lfsck_component *com)
664 struct lfsck_instance *lfsck = com->lc_lfsck;
665 struct lfsck_layout_slave_data *llsd = com->lc_data;
666 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
667 struct rb_node *next;
668 struct lfsck_rbtree_node *lrn;
670 lfsck->li_bottom->dd_record_fid_accessed = 0;
671 /* Invalid the rbtree, then no others will use it. */
672 down_write(&llsd->llsd_rb_rwsem);
673 llsd->llsd_rbtree_valid = 0;
674 up_write(&llsd->llsd_rb_rwsem);
676 while (node != NULL) {
677 next = rb_next(node);
678 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
679 rb_erase(node, &llsd->llsd_rb_root);
680 lfsck_rbtree_free(lrn);
684 if (llsd->llsd_rb_obj != NULL) {
685 lfsck_object_put(env, llsd->llsd_rb_obj);
686 llsd->llsd_rb_obj = NULL;
689 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
690 lfsck_lfsck2name(lfsck));
693 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
694 struct lfsck_component *com,
695 const struct lu_fid *fid,
698 struct lfsck_layout_slave_data *llsd = com->lc_data;
699 struct lfsck_rbtree_node *lrn;
705 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
708 if (!fid_is_idif(fid) && !fid_is_norm(fid))
711 down_read(&llsd->llsd_rb_rwsem);
712 if (!llsd->llsd_rbtree_valid)
713 GOTO(unlock, rc = 0);
715 lrn = lfsck_rbtree_search(llsd, fid, NULL);
717 struct lfsck_rbtree_node *tmp;
721 up_read(&llsd->llsd_rb_rwsem);
722 tmp = lfsck_rbtree_new(env, fid);
724 GOTO(out, rc = PTR_ERR(tmp));
727 down_write(&llsd->llsd_rb_rwsem);
728 if (!llsd->llsd_rbtree_valid) {
729 lfsck_rbtree_free(tmp);
730 GOTO(unlock, rc = 0);
733 lrn = lfsck_rbtree_insert(llsd, tmp);
735 lfsck_rbtree_free(tmp);
738 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
739 /* Any accessed object must be a known object. */
740 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
741 atomic_inc(&lrn->lrn_known_count);
742 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
743 atomic_inc(&lrn->lrn_accessed_count);
745 GOTO(unlock, rc = 0);
749 up_write(&llsd->llsd_rb_rwsem);
751 up_read(&llsd->llsd_rb_rwsem);
753 if (rc != 0 && accessed) {
754 struct lfsck_layout *lo = com->lc_file_ram;
756 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
757 "bitmap, and will cause incorrect LFSCK OST-object "
758 "handling, so disable it to cancel orphan handling "
759 "for related device. rc = %d\n",
760 lfsck_lfsck2name(com->lc_lfsck), rc);
762 lo->ll_flags |= LF_INCOMPLETE;
763 lfsck_rbtree_cleanup(env, com);
767 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
768 const struct lfsck_layout_dangling_key *src)
770 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
771 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
772 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
775 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
776 const struct lfsck_layout_dangling_key *src)
778 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
779 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
780 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
783 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
784 const struct lfsck_layout_dangling_key *src)
786 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
787 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
788 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
791 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
792 const struct lfsck_layout_dangling_key *src)
794 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
795 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
796 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
799 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
800 const struct lfsck_layout *src)
804 des->ll_magic = le32_to_cpu(src->ll_magic);
805 des->ll_status = le32_to_cpu(src->ll_status);
806 des->ll_flags = le32_to_cpu(src->ll_flags);
807 des->ll_success_count = le32_to_cpu(src->ll_success_count);
808 des->ll_run_time_phase1 = le64_to_cpu(src->ll_run_time_phase1);
809 des->ll_run_time_phase2 = le64_to_cpu(src->ll_run_time_phase2);
810 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
811 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
812 des->ll_time_last_checkpoint =
813 le64_to_cpu(src->ll_time_last_checkpoint);
814 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
815 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
816 des->ll_pos_first_inconsistent =
817 le64_to_cpu(src->ll_pos_first_inconsistent);
818 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
819 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
820 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
821 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
822 for (i = 0; i < LLIT_MAX; i++)
823 des->ll_objs_repaired[i] =
824 le64_to_cpu(src->ll_objs_repaired[i]);
825 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
826 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
827 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
828 &src->ll_lldk_latest_scanned_phase2);
831 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
832 const struct lfsck_layout *src)
836 des->ll_magic = cpu_to_le32(src->ll_magic);
837 des->ll_status = cpu_to_le32(src->ll_status);
838 des->ll_flags = cpu_to_le32(src->ll_flags);
839 des->ll_success_count = cpu_to_le32(src->ll_success_count);
840 des->ll_run_time_phase1 = cpu_to_le64(src->ll_run_time_phase1);
841 des->ll_run_time_phase2 = cpu_to_le64(src->ll_run_time_phase2);
842 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
843 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
844 des->ll_time_last_checkpoint =
845 cpu_to_le64(src->ll_time_last_checkpoint);
846 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
847 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
848 des->ll_pos_first_inconsistent =
849 cpu_to_le64(src->ll_pos_first_inconsistent);
850 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
851 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
852 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
853 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
854 for (i = 0; i < LLIT_MAX; i++)
855 des->ll_objs_repaired[i] =
856 cpu_to_le64(src->ll_objs_repaired[i]);
857 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
858 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
859 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
860 &src->ll_lldk_latest_scanned_phase2);
864 * Load the OST bitmap from the lfsck_layout trace file.
866 * \param[in] env pointer to the thread context
867 * \param[in] com pointer to the lfsck component
869 * \retval 0 for success
870 * \retval negative error number on failure or data corruption
872 static int lfsck_layout_load_bitmap(const struct lu_env *env,
873 struct lfsck_component *com)
875 struct dt_object *obj = com->lc_obj;
876 struct lfsck_assistant_data *lad = com->lc_data;
877 struct lfsck_layout *lo = com->lc_file_ram;
878 struct cfs_bitmap *bitmap = lad->lad_bitmap;
879 loff_t pos = com->lc_file_size;
885 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
887 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
889 nbits = lo->ll_bitmap_size;
891 if (unlikely(nbits < BITS_PER_LONG))
892 nbits = BITS_PER_LONG;
894 if (nbits > bitmap->size) {
895 __u32 new_bits = bitmap->size;
896 struct cfs_bitmap *new_bitmap;
898 while (new_bits < nbits)
901 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
902 if (new_bitmap == NULL)
905 lad->lad_bitmap = new_bitmap;
906 CFS_FREE_BITMAP(bitmap);
910 if (lo->ll_bitmap_size == 0) {
911 lad->lad_incomplete = 0;
912 CFS_RESET_BITMAP(bitmap);
917 size = (lo->ll_bitmap_size + 7) >> 3;
918 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
920 RETURN(rc >= 0 ? -EINVAL : rc);
922 if (cfs_bitmap_check_empty(bitmap))
923 lad->lad_incomplete = 0;
925 lad->lad_incomplete = 1;
931 * Load the layout LFSCK trace file from disk.
933 * The layout LFSCK trace file records the layout LFSCK status information
934 * and other statistics, such as how many objects have been scanned, and how
935 * many objects have been repaired, and etc. It also contains the bitmap for
936 * failed OSTs during the layout LFSCK. All these information will be loaded
937 * from disk to RAM when the layout LFSCK component setup.
939 * \param[in] env pointer to the thread context
940 * \param[in] com pointer to the lfsck component
942 * \retval positive number for file data corruption, the caller
943 * should reset the layout LFSCK trace file
944 * \retval 0 for success
945 * \retval negative error number on failure
947 static int lfsck_layout_load(const struct lu_env *env,
948 struct lfsck_component *com)
950 struct lfsck_layout *lo = com->lc_file_ram;
951 ssize_t size = com->lc_file_size;
955 rc = dt_read(env, com->lc_obj,
956 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
960 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
961 lfsck_lfsck2name(com->lc_lfsck), rc);
963 } else if (rc != size) {
964 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
965 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
969 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
970 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
971 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
972 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
973 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
981 * Store the layout LFSCK trace file on disk.
983 * The layout LFSCK trace file records the layout LFSCK status information
984 * and other statistics, such as how many objects have been scanned, and how
985 * many objects have been repaired, and etc. It also contains the bitmap for
986 * failed OSTs during the layout LFSCK. All these information will be synced
987 * from RAM to disk periodically.
989 * \param[in] env pointer to the thread context
990 * \param[in] com pointer to the lfsck component
992 * \retval 0 for success
993 * \retval negative error number on failure
995 static int lfsck_layout_store(const struct lu_env *env,
996 struct lfsck_component *com)
998 struct dt_object *obj = com->lc_obj;
999 struct lfsck_instance *lfsck = com->lc_lfsck;
1000 struct lfsck_layout *lo_ram = com->lc_file_ram;
1001 struct lfsck_layout *lo = com->lc_file_disk;
1003 struct dt_device *dev = lfsck_obj2dev(obj);
1004 struct cfs_bitmap *bitmap = NULL;
1006 ssize_t size = com->lc_file_size;
1011 if (lfsck->li_master) {
1012 struct lfsck_assistant_data *lad = com->lc_data;
1014 bitmap = lad->lad_bitmap;
1015 nbits = bitmap->size;
1018 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1021 lo_ram->ll_bitmap_size = nbits;
1022 lfsck_layout_cpu_to_le(lo, lo_ram);
1023 th = dt_trans_create(env, dev);
1025 GOTO(log, rc = PTR_ERR(th));
1027 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1032 if (bitmap != NULL) {
1033 rc = dt_declare_record_write(env, obj,
1034 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1040 rc = dt_trans_start_local(env, dev, th);
1045 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1049 if (bitmap != NULL) {
1051 rc = dt_record_write(env, obj,
1052 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1059 dt_trans_stop(env, dev, th);
1063 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1064 lfsck_lfsck2name(lfsck), rc);
1069 static int lfsck_layout_init(const struct lu_env *env,
1070 struct lfsck_component *com)
1072 struct lfsck_layout *lo = com->lc_file_ram;
1075 memset(lo, 0, com->lc_file_size);
1076 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1077 lo->ll_status = LS_INIT;
1078 down_write(&com->lc_sem);
1079 rc = lfsck_layout_store(env, com);
1080 if (rc == 0 && com->lc_lfsck->li_master)
1081 rc = lfsck_load_sub_trace_files(env, com,
1082 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1083 up_write(&com->lc_sem);
1088 static int fid_is_for_ostobj(const struct lu_env *env,
1089 struct lfsck_instance *lfsck,
1090 struct dt_object *obj, const struct lu_fid *fid)
1092 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1093 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1094 struct lustre_ost_attrs *loa;
1097 fld_range_set_any(range);
1098 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1100 if (fld_range_is_ost(range))
1106 loa = &lfsck_env_info(env)->lti_loa;
1107 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1109 if (rc >= (int)sizeof(struct lustre_mdt_attrs)) {
1110 lustre_lma_swab(&loa->loa_lma);
1112 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1115 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1120 static struct lfsck_layout_seq *
1121 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1123 struct lfsck_layout_seq *lls;
1125 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1126 if (lls->lls_seq == seq)
1129 if (lls->lls_seq > seq)
1137 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1138 struct lfsck_layout_seq *lls)
1140 struct lfsck_layout_seq *tmp;
1141 struct list_head *pos = &llsd->llsd_seq_list;
1143 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1144 if (lls->lls_seq < tmp->lls_seq) {
1145 pos = &tmp->lls_list;
1149 list_add_tail(&lls->lls_list, pos);
1153 lfsck_layout_lastid_create(const struct lu_env *env,
1154 struct lfsck_instance *lfsck,
1155 struct dt_object *obj)
1157 struct lfsck_thread_info *info = lfsck_env_info(env);
1158 struct lu_attr *la = &info->lti_la;
1159 struct dt_object_format *dof = &info->lti_dof;
1160 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1161 struct dt_device *dt = lfsck_obj2dev(obj);
1168 if (bk->lb_param & LPF_DRYRUN)
1171 memset(la, 0, sizeof(*la));
1172 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1173 la->la_valid = LA_MODE | LA_UID | LA_GID;
1174 memset(dof, 0, sizeof(*dof));
1175 dof->dof_type = dt_mode_to_dft(S_IFREG);
1177 th = dt_trans_create(env, dt);
1179 GOTO(log, rc = PTR_ERR(th));
1181 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1185 rc = dt_declare_record_write(env, obj,
1186 lfsck_buf_get(env, &lastid,
1192 rc = dt_trans_start_local(env, dt, th);
1196 dt_write_lock(env, obj, 0);
1197 if (likely(dt_object_exists(obj) == 0)) {
1198 rc = dt_create(env, obj, la, NULL, dof, th);
1200 rc = dt_record_write(env, obj,
1201 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1204 dt_write_unlock(env, obj);
1209 dt_trans_stop(env, dt, th);
1212 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1214 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1220 lfsck_layout_lastid_reload(const struct lu_env *env,
1221 struct lfsck_component *com,
1222 struct lfsck_layout_seq *lls)
1228 dt_read_lock(env, lls->lls_lastid_obj, 0);
1229 rc = dt_record_read(env, lls->lls_lastid_obj,
1230 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1231 dt_read_unlock(env, lls->lls_lastid_obj);
1232 if (unlikely(rc != 0))
1235 lastid = le64_to_cpu(lastid);
1236 if (lastid < lls->lls_lastid_known) {
1237 struct lfsck_instance *lfsck = com->lc_lfsck;
1238 struct lfsck_layout *lo = com->lc_file_ram;
1240 lls->lls_lastid = lls->lls_lastid_known;
1242 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1243 LASSERT(lfsck->li_out_notify != NULL);
1245 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1246 LE_LASTID_REBUILDING);
1247 lo->ll_flags |= LF_CRASHED_LASTID;
1249 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1250 "LAST_ID file (1) for the sequence %#llx"
1251 ", old value %llu, known value %llu\n",
1252 lfsck_lfsck2name(lfsck), lls->lls_seq,
1253 lastid, lls->lls_lastid);
1255 } else if (lastid >= lls->lls_lastid) {
1256 lls->lls_lastid = lastid;
1264 lfsck_layout_lastid_store(const struct lu_env *env,
1265 struct lfsck_component *com)
1267 struct lfsck_instance *lfsck = com->lc_lfsck;
1268 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1269 struct dt_device *dt = lfsck->li_bottom;
1270 struct lfsck_layout_slave_data *llsd = com->lc_data;
1271 struct lfsck_layout_seq *lls;
1277 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1280 if (!lls->lls_dirty)
1283 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1284 "<seq> %#llx as <oid> %llu\n",
1285 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1287 if (bk->lb_param & LPF_DRYRUN) {
1292 th = dt_trans_create(env, dt);
1295 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1296 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1297 lfsck_lfsck2name(com->lc_lfsck),
1302 lastid = cpu_to_le64(lls->lls_lastid);
1303 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1304 lfsck_buf_get(env, &lastid,
1310 rc = dt_trans_start_local(env, dt, th);
1314 dt_write_lock(env, lls->lls_lastid_obj, 0);
1315 rc = dt_record_write(env, lls->lls_lastid_obj,
1316 lfsck_buf_get(env, &lastid,
1317 sizeof(lastid)), &pos, th);
1318 dt_write_unlock(env, lls->lls_lastid_obj);
1323 dt_trans_stop(env, dt, th);
1326 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1327 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1328 lfsck_lfsck2name(com->lc_lfsck),
1337 lfsck_layout_lastid_load(const struct lu_env *env,
1338 struct lfsck_component *com,
1339 struct lfsck_layout_seq *lls)
1341 struct lfsck_instance *lfsck = com->lc_lfsck;
1342 struct lfsck_layout *lo = com->lc_file_ram;
1343 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1344 struct dt_object *obj;
1349 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1350 obj = dt_locate(env, lfsck->li_bottom, fid);
1352 RETURN(PTR_ERR(obj));
1354 /* LAST_ID crashed, to be rebuilt */
1355 if (dt_object_exists(obj) == 0) {
1356 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1357 LASSERT(lfsck->li_out_notify != NULL);
1359 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1360 LE_LASTID_REBUILDING);
1361 lo->ll_flags |= LF_CRASHED_LASTID;
1363 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1364 "LAST_ID file for sequence %#llx\n",
1365 lfsck_lfsck2name(lfsck), lls->lls_seq);
1367 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1369 struct l_wait_info lwi = LWI_TIMEOUT(
1370 cfs_time_seconds(cfs_fail_val),
1373 /* Some others may changed the cfs_fail_val
1374 * as zero after above check, re-check it for
1375 * sure to avoid falling into wait for ever. */
1376 if (likely(lwi.lwi_timeout > 0)) {
1377 struct ptlrpc_thread *thread =
1380 up_write(&com->lc_sem);
1381 l_wait_event(thread->t_ctl_waitq,
1382 !thread_is_running(thread),
1384 down_write(&com->lc_sem);
1389 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1391 dt_read_lock(env, obj, 0);
1392 rc = dt_read(env, obj,
1393 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1395 dt_read_unlock(env, obj);
1396 if (rc != 0 && rc != sizeof(__u64))
1397 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1399 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1400 LASSERT(lfsck->li_out_notify != NULL);
1402 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1403 LE_LASTID_REBUILDING);
1404 lo->ll_flags |= LF_CRASHED_LASTID;
1406 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1407 "LAST_ID file for the sequence %#llx"
1409 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1412 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1420 lfsck_object_put(env, obj);
1422 lls->lls_lastid_obj = obj;
1427 static void lfsck_layout_record_failure(const struct lu_env *env,
1428 struct lfsck_instance *lfsck,
1429 struct lfsck_layout *lo)
1433 lo->ll_objs_failed_phase1++;
1434 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1436 if (lo->ll_pos_first_inconsistent == 0 ||
1437 lo->ll_pos_first_inconsistent < cookie) {
1438 lo->ll_pos_first_inconsistent = cookie;
1440 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1441 "inconsistency at the pos [%llu]\n",
1442 lfsck_lfsck2name(lfsck),
1443 lo->ll_pos_first_inconsistent);
1447 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1448 struct lfsck_component *com,
1451 struct lfsck_instance *lfsck = com->lc_lfsck;
1452 struct lfsck_layout *lo = com->lc_file_ram;
1454 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan: rc = %d\n",
1455 lfsck_lfsck2name(lfsck), rc);
1457 down_write(&com->lc_sem);
1458 lo->ll_run_time_phase2 += ktime_get_seconds() -
1459 com->lc_time_last_checkpoint;
1460 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
1461 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1464 if (lo->ll_flags & LF_INCOMPLETE) {
1465 lo->ll_status = LS_PARTIAL;
1467 if (lfsck->li_master) {
1468 struct lfsck_assistant_data *lad = com->lc_data;
1470 if (lad->lad_incomplete)
1471 lo->ll_status = LS_PARTIAL;
1473 lo->ll_status = LS_COMPLETED;
1475 lo->ll_status = LS_COMPLETED;
1478 lo->ll_flags &= ~LF_SCANNED_ONCE;
1479 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1480 lo->ll_flags &= ~LF_INCONSISTENT;
1481 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1482 lo->ll_success_count++;
1483 } else if (rc == 0) {
1484 if (lfsck->li_status != 0)
1485 lo->ll_status = lfsck->li_status;
1487 lo->ll_status = LS_STOPPED;
1489 lo->ll_status = LS_FAILED;
1492 rc = lfsck_layout_store(env, com);
1493 up_write(&com->lc_sem);
1495 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan result %u: rc = %d\n",
1496 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
1501 static int lfsck_layout_trans_stop(const struct lu_env *env,
1502 struct dt_device *dev,
1503 struct thandle *handle, int result)
1507 /* XXX: If there is something worng or it needs to repair nothing,
1508 * then notify the lower to stop the modification. Currently,
1509 * we use th_result for such purpose, that may be replaced by
1510 * some rollback mechanism in the future. */
1511 handle->th_result = result;
1512 rc = dt_trans_stop(env, dev, handle);
1514 return result > 0 ? 0 : result;
1516 return rc == 0 ? 1 : rc;
1519 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1520 struct lfsck_component *com,
1521 const struct lu_fid *pfid,
1522 const struct lu_fid *cfid,
1523 __u32 comp_id, __u32 ea_off,
1526 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1527 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1528 struct dt_device *dev;
1529 struct dt_object *obj;
1530 struct thandle *th = NULL;
1535 idx = lfsck_sub_trace_file_fid2idx(pfid);
1536 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1537 dev = lfsck_obj2dev(obj);
1539 fid_cpu_to_be(&key->lldk_fid, pfid);
1540 key->lldk_comp_id = cpu_to_be32(comp_id);
1541 key->lldk_ea_off = cpu_to_be32(ea_off);
1543 fid_cpu_to_be(rec, cfid);
1544 rec->f_ver = cpu_to_be32(ost_idx);
1546 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1548 th = dt_trans_create(env, dev);
1550 GOTO(unlock, rc = PTR_ERR(th));
1552 rc = dt_declare_insert(env, obj,
1553 (const struct dt_rec *)rec,
1554 (const struct dt_key *)key, th);
1558 rc = dt_trans_start_local(env, dev, th);
1562 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1563 (const struct dt_key *)key, th);
1568 if (th && !IS_ERR(th))
1569 dt_trans_stop(env, dev, th);
1571 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1573 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1574 "ea_off = %u, ost_idx = %u, into the trace file for further "
1575 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1576 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1581 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1582 struct lfsck_component *com,
1583 const struct lu_fid *fid,
1584 __u32 comp_id, __u32 ea_off)
1586 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1587 struct dt_device *dev;
1588 struct dt_object *obj;
1589 struct thandle *th = NULL;
1594 idx = lfsck_sub_trace_file_fid2idx(fid);
1595 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1596 dev = lfsck_obj2dev(obj);
1598 fid_cpu_to_be(&key->lldk_fid, fid);
1599 key->lldk_comp_id = cpu_to_be32(comp_id);
1600 key->lldk_ea_off = cpu_to_be32(ea_off);
1602 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1604 th = dt_trans_create(env, dev);
1606 GOTO(unlock, rc = PTR_ERR(th));
1608 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1612 rc = dt_trans_start_local(env, dev, th);
1616 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1621 if (th && !IS_ERR(th))
1622 dt_trans_stop(env, dev, th);
1624 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1626 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1627 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1628 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1634 * Get the system default stripe size.
1636 * \param[in] env pointer to the thread context
1637 * \param[in] lfsck pointer to the lfsck instance
1638 * \param[out] size pointer to the default stripe size
1640 * \retval 0 for success
1641 * \retval negative error number on failure
1643 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1644 struct lfsck_instance *lfsck,
1647 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1648 struct dt_object *root;
1651 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1653 return PTR_ERR(root);
1655 /* Get the default stripe size via xattr_get on the backend root. */
1656 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1659 /* The lum->lmm_stripe_size is LE mode. The *size also
1660 * should be LE mode. So it is unnecessary to convert. */
1661 *size = lum->lmm_stripe_size;
1663 } else if (unlikely(rc == 0)) {
1667 lfsck_object_put(env, root);
1673 * \retval +1: repaired
1674 * \retval 0: did nothing
1675 * \retval -ve: on error
1677 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1678 struct lfsck_instance *lfsck,
1679 struct thandle *handle,
1680 struct dt_object *parent,
1681 const struct lu_fid *cfid,
1683 struct lov_mds_md_v1 *lmm,
1684 struct lov_ost_data_v1 *slot,
1685 int fl, __u32 ost_idx, int size)
1687 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1688 struct lu_buf ea_buf;
1695 magic = le32_to_cpu(lmm->lmm_magic);
1696 pattern = le32_to_cpu(lmm->lmm_pattern);
1697 count = le16_to_cpu(lmm->lmm_stripe_count);
1699 fid_to_ostid(cfid, oi);
1700 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1701 slot->l_ost_gen = cpu_to_le32(0);
1702 slot->l_ost_idx = cpu_to_le32(ost_idx);
1704 if (pattern & LOV_PATTERN_F_HOLE) {
1705 struct lov_ost_data_v1 *objs;
1708 if (magic == LOV_MAGIC_V1)
1709 objs = &lmm->lmm_objects[0];
1711 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1712 for (i = 0; i < count; i++, objs++) {
1713 if (lovea_slot_is_dummy(objs))
1717 /* If the @slot is the last dummy slot to be refilled,
1718 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1721 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1723 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1724 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1725 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1729 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1730 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1737 static struct lov_ost_data_v1 *
1738 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1739 const struct lu_fid *pfid,
1740 __u32 stripe_size, __u32 ea_off,
1741 __u32 pattern, __u16 count)
1743 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1744 lmm->lmm_pattern = cpu_to_le32(pattern);
1745 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1746 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1747 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1748 lmm->lmm_stripe_count = cpu_to_le16(count);
1749 lmm->lmm_layout_gen = cpu_to_le16(1);
1750 memset(&lmm->lmm_objects[0], 0,
1751 sizeof(struct lov_ost_data_v1) * count);
1753 return &lmm->lmm_objects[ea_off];
1756 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1757 struct lfsck_instance *lfsck,
1758 struct ost_layout *ol,
1759 struct dt_object *parent,
1760 struct lu_buf *buf, __u32 ea_off,
1761 struct lov_mds_md_v1 **lmm,
1762 struct lov_ost_data_v1 **objs)
1765 __u32 stripe_size = ol->ol_stripe_size;
1766 __u32 pattern = LOV_PATTERN_RAID0;
1769 if (ol->ol_stripe_count != 0)
1770 count = ol->ol_stripe_count;
1774 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1775 LASSERTF(buf->lb_len >= size,
1776 "buffer len %d is less than real size %d\n",
1777 (int)buf->lb_len, size);
1779 if (stripe_size == 0) {
1782 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1788 if (ol->ol_stripe_count > 1 ||
1789 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1790 pattern |= LOV_PATTERN_F_HOLE;
1791 memset(&(*lmm)->lmm_objects[0], 0,
1792 count * sizeof(struct lov_ost_data_v1));
1795 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1796 stripe_size, ea_off, pattern, count);
1801 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1802 struct lu_orphan_rec_v3 *rec,
1803 struct dt_object *parent,
1804 struct lu_buf *buf, __u32 ea_off,
1805 struct lov_mds_md_v1 **lmm,
1806 struct lov_ost_data_v1 **objs)
1808 struct ost_layout *ol = &rec->lor_layout;
1809 struct lov_comp_md_v1 *lcm;
1810 struct lov_comp_md_entry_v1 *lcme;
1811 __u32 pattern = LOV_PATTERN_RAID0;
1812 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1813 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1814 int size = offset + lcme_size;
1816 LASSERTF(buf->lb_len >= size,
1817 "buffer len %d is less than real size %d\n",
1818 (int)buf->lb_len, size);
1821 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1822 lcm->lcm_size = cpu_to_le32(size);
1823 if (rec->lor_range) {
1824 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1826 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1827 } else if (rec->lor_layout_version) {
1828 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1830 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1832 lcm->lcm_layout_gen = cpu_to_le32(1);
1833 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1835 lcm->lcm_entry_count = cpu_to_le16(1);
1836 /* Currently, we do not know how many mirrors will be, set it as zero
1837 * at the beginning. It will be updated when more mirrors are found. */
1838 lcm->lcm_mirror_count = 0;
1840 lcme = &lcm->lcm_entries[0];
1841 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1842 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1843 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1844 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1845 lcme->lcme_offset = cpu_to_le32(offset);
1846 lcme->lcme_size = cpu_to_le32(lcme_size);
1847 lcme->lcme_layout_gen = lcm->lcm_layout_gen;
1848 if (ol->ol_stripe_count > 1)
1849 pattern |= LOV_PATTERN_F_HOLE;
1851 *lmm = buf->lb_buf + offset;
1852 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1853 ol->ol_stripe_size, ea_off,
1854 pattern, ol->ol_stripe_count);
1859 static void lfsck_layout_update_lcm(struct lov_comp_md_v1 *lcm,
1860 struct lov_comp_md_entry_v1 *lcme,
1861 __u32 version, __u32 range)
1863 struct lov_comp_md_entry_v1 *tmp;
1864 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
1865 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
1866 __u32 gen = version + range;
1869 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1870 __u16 flags = le16_to_cpu(lcm->lcm_flags);
1874 lcme->lcme_layout_gen = cpu_to_le32(gen);
1875 if (le32_to_cpu(lcm->lcm_layout_gen) < gen)
1876 lcm->lcm_layout_gen = cpu_to_le32(gen);
1879 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1880 else if (flags == LCM_FL_NONE && le16_to_cpu(lcm->lcm_mirror_count) > 0)
1881 lcm->lcm_flags = cpu_to_le16(LCM_FL_RDONLY);
1883 for (i = 0; i < count; i++) {
1884 tmp = &lcm->lcm_entries[i];
1885 if (le64_to_cpu(tmp->lcme_extent.e_end) <= start)
1888 if (le64_to_cpu(tmp->lcme_extent.e_start) >= end)
1891 if (le32_to_cpu(tmp->lcme_flags) & LCME_FL_STALE)
1894 tmp_gen = le32_to_cpu(tmp->lcme_layout_gen);
1895 /* "lcme_layout_gen == 0" but without LCME_FL_STALE flag,
1896 * then it should be the latest version of all mirrors. */
1897 if (tmp_gen == 0 || tmp_gen > gen) {
1898 lcme->lcme_flags = cpu_to_le32(
1899 le32_to_cpu(lcme->lcme_flags) | LCME_FL_STALE);
1904 tmp->lcme_flags = cpu_to_le32(
1905 le32_to_cpu(tmp->lcme_flags) | LCME_FL_STALE);
1909 static int lfsck_layout_add_comp(const struct lu_env *env,
1910 struct lfsck_instance *lfsck,
1911 struct thandle *handle,
1912 struct lu_orphan_rec_v3 *rec,
1913 struct dt_object *parent,
1914 const struct lu_fid *cfid,
1915 struct lu_buf *buf, __u32 ost_idx,
1916 __u32 ea_off, int pos, bool new_mirror)
1918 struct ost_layout *ol = &rec->lor_layout;
1919 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1920 struct lov_comp_md_entry_v1 *lcme;
1921 struct lov_mds_md_v1 *lmm;
1922 struct lov_ost_data_v1 *objs;
1923 int added = sizeof(*lcme) +
1924 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1925 int size = le32_to_cpu(lcm->lcm_size) + added;
1929 __u32 pattern = LOV_PATTERN_RAID0;
1930 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1933 lu_buf_check_and_grow(buf, size);
1934 /* set the lcm again because lu_buf_check_and_grow() may
1935 * have reallocated the buf. */
1937 lcm->lcm_size = cpu_to_le32(size);
1938 lcm->lcm_entry_count = cpu_to_le16(count + 1);
1940 le16_add_cpu(&lcm->lcm_mirror_count, 1);
1942 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
1943 * with distance of 'added'. */
1946 for (i = pos; i < count; i++) {
1947 lcme = &lcm->lcm_entries[i];
1948 size += le32_to_cpu(lcme->lcme_size);
1951 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
1952 memmove(buf->lb_buf + offset + added,
1953 buf->lb_buf + offset, size);
1957 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
1958 * of 'sizeof(struct lov_comp_md_entry_v1)' */
1960 for (i = 0; i < pos; i++) {
1961 lcme = &lcm->lcm_entries[i];
1962 size += le32_to_cpu(lcme->lcme_size);
1965 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
1966 memmove(buf->lb_buf + offset + sizeof(*lcme),
1967 buf->lb_buf + offset, size);
1970 /* 3. Recalculate the enter offset for the component [pos, count-1] */
1971 for (i = count - 1; i >= pos; i--) {
1972 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
1973 lcm->lcm_entries[i + 1].lcme_offset =
1974 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
1975 lcme_offset) + added);
1978 /* 4. Recalculate the enter offset for the component [0, pos) */
1979 for (i = 0; i < pos; i++) {
1980 lcm->lcm_entries[i].lcme_offset =
1981 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
1982 lcme_offset) + sizeof(*lcme));
1985 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
1986 /* 4. Insert the new component header (entry) at the slot 'pos'. */
1987 lcme = &lcm->lcm_entries[pos];
1988 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1989 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1990 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1991 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1992 lcme->lcme_offset = cpu_to_le32(offset);
1993 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
1996 if (ol->ol_stripe_count > 1)
1997 pattern |= LOV_PATTERN_F_HOLE;
1999 lmm = buf->lb_buf + offset;
2000 /* 5. Insert teh new component body at the 'offset'. */
2001 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
2002 ol->ol_stripe_size, ea_off,
2003 pattern, ol->ol_stripe_count);
2005 /* 6. Update mirror related flags and version. */
2006 lfsck_layout_update_lcm(lcm, lcme, rec->lor_layout_version,
2009 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2010 lmm, objs, LU_XATTR_REPLACE, ost_idx,
2011 le32_to_cpu(lcm->lcm_size));
2013 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
2014 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2015 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2016 "comp_end %llu, layout version %u, range %u, "
2017 "%s LOV EA hole: rc = %d\n",
2018 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2019 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2020 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2021 rec->lor_layout_version, rec->lor_range,
2022 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2023 "with" : "without", rc);
2028 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
2029 struct lfsck_instance *lfsck,
2030 struct thandle *handle,
2031 struct ost_layout *ol,
2032 struct dt_object *parent,
2033 const struct lu_fid *cfid,
2034 struct lu_buf *buf, __u32 ost_idx,
2037 struct lov_mds_md_v1 *lmm = buf->lb_buf;
2038 struct lov_ost_data_v1 *objs;
2039 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
2040 __u32 magic = le32_to_cpu(lmm->lmm_magic);
2046 /* The original LOVEA maybe re-generated via old filter_fid, at
2047 * that time, we do not know the stripe count and stripe size. */
2048 if (ol->ol_stripe_count > count)
2049 count = ol->ol_stripe_count;
2050 if (ol->ol_stripe_size != 0 &&
2051 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
2052 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
2054 if (magic == LOV_MAGIC_V1)
2055 objs = &lmm->lmm_objects[count];
2057 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
2059 gap = ea_off - count;
2063 size = lov_mds_md_size(count, magic);
2064 LASSERTF(buf->lb_len >= size,
2065 "buffer len %d is less than real size %d\n",
2066 (int)buf->lb_len, size);
2069 memset(objs, 0, gap * sizeof(*objs));
2070 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
2073 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2074 lmm->lmm_stripe_count = cpu_to_le16(count);
2077 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2078 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
2080 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
2081 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2082 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2083 "comp_end %llu, %s LOV EA hole: rc = %d\n",
2084 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2085 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2086 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2087 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2088 "with" : "without", rc);
2094 * \retval +1: repaired
2095 * \retval 0: did nothing
2096 * \retval -ve: on error
2098 static int lfsck_layout_update_lovea(const struct lu_env *env,
2099 struct lfsck_instance *lfsck,
2100 struct thandle *handle,
2101 struct lu_orphan_rec_v3 *rec,
2102 struct dt_object *parent,
2103 const struct lu_fid *cfid,
2104 struct lu_buf *buf, int fl,
2105 __u32 ost_idx, __u32 ea_off)
2107 struct ost_layout *ol = &rec->lor_layout;
2108 struct lov_mds_md_v1 *lmm = NULL;
2109 struct lov_ost_data_v1 *objs = NULL;
2113 if (ol->ol_comp_id != 0)
2114 rc = lfsck_layout_new_comp_lovea(env, rec, parent, buf, ea_off,
2117 rc = lfsck_layout_new_v1_lovea(env, lfsck, &rec->lor_layout,
2118 parent, buf, ea_off, &lmm,
2121 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2122 buf, lmm, objs, fl, ost_idx, rc);
2124 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2125 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2126 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2127 "comp_end %llu, layout version %u, range %u, fl %d, "
2128 "%s LOV EA hole: rc = %d\n",
2129 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2130 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2131 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2132 rec->lor_layout_version, rec->lor_range, fl,
2133 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2134 "with" : "without", rc);
2139 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2140 struct dt_object *child,
2141 const struct lu_fid *pfid,
2142 const struct ost_layout *ol, __u32 offset,
2143 __u32 version, __u32 range)
2145 struct dt_device *dev = lfsck_obj2dev(child);
2146 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2147 struct thandle *handle;
2148 struct lu_buf buf = { NULL };
2151 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2152 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2153 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2154 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2155 * parent MDT-object's layout EA. */
2156 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2157 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2158 ff->ff_layout_version = cpu_to_le32(version);
2159 ff->ff_range = cpu_to_le32(range);
2160 lfsck_buf_init(&buf, ff, sizeof(*ff));
2162 handle = dt_trans_create(env, dev);
2164 RETURN(PTR_ERR(handle));
2166 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2170 rc = dt_trans_start_local(env, dev, handle);
2174 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2179 dt_trans_stop(env, dev, handle);
2185 * \retval +1: repaired
2186 * \retval 0: did nothing
2187 * \retval -ve: on error
2189 static int lfsck_layout_update_pfid(const struct lu_env *env,
2190 struct lfsck_component *com,
2191 struct dt_object *parent,
2192 struct lu_fid *cfid,
2193 struct dt_device *cdev,
2194 struct lu_orphan_rec_v3 *rec, __u32 ea_off)
2196 struct dt_object *child;
2200 child = lfsck_object_find_by_dev(env, cdev, cfid);
2202 RETURN(PTR_ERR(child));
2204 rc = __lfsck_layout_update_pfid(env, child,
2205 lu_object_fid(&parent->do_lu),
2206 &rec->lor_layout, ea_off,
2207 rec->lor_layout_version,
2209 lfsck_object_put(env, child);
2211 RETURN(rc == 0 ? 1 : rc);
2214 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2216 if (ol->ol_comp_id != 0)
2217 return sizeof(struct lov_comp_md_v1) +
2218 sizeof(struct lov_comp_md_entry_v1) +
2219 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2221 if (ol->ol_stripe_count != 0)
2222 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2224 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2228 * This function will create the MDT-object with the given (partial) LOV EA.
2230 * Under some data corruption cases, the MDT-object of the file may be lost,
2231 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2232 * re-create the MDT-object with the orphan OST-object(s) information.
2234 * On the other hand, the LFSCK may has created some OST-object for repairing
2235 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2236 * the old OST-object is there and should replace the former new created OST
2237 * object. Unfortunately, some others have modified such newly created object.
2238 * To keep the data (both new and old), the LFSCK will create MDT-object with
2239 * new FID to reference the original OST-object.
2241 * \param[in] env pointer to the thread context
2242 * \param[in] com pointer to the lfsck component
2243 * \param[in] ltd pointer to target device descriptor
2244 * \param[in] rec pointer to the record for the orphan OST-object
2245 * \param[in] cfid pointer to FID for the orphan OST-object
2246 * \param[in] infix additional information, such as the FID for original
2247 * MDT-object and the stripe offset in the LOV EA
2248 * \param[in] type the type for describing why the orphan MDT-object is
2249 * created. The rules are as following:
2251 * type "C": Multiple OST-objects claim the same MDT-object and the
2252 * same slot in the layout EA. Then the LFSCK will create
2253 * new MDT-object(s) to hold the conflict OST-object(s).
2255 * type "N": The orphan OST-object does not know which one was the
2256 * real parent MDT-object, so the LFSCK uses new FID for
2257 * its parent MDT-object.
2259 * type "R": The orphan OST-object knows its parent MDT-object FID,
2260 * but does not know the position (the file name) in the
2263 * type "D": The MDT-object is a directory, it may knows its parent
2264 * but because there is no valid linkEA, the LFSCK cannot
2265 * know where to put it back to the namespace.
2266 * type "O": The MDT-object has no linkEA, and there is no name
2267 * entry that references the MDT-object.
2269 * type "P": The orphan object to be created was a parent directory
2270 * of some MDT-object which linkEA shows that the @orphan
2271 * object is missing.
2273 * The orphan name will be like:
2274 * ${FID}-${infix}-${type}-${conflict_version}
2276 * \param[in] ea_off the stripe offset in the LOV EA
2278 * \retval positive on repaired something
2279 * \retval 0 if needs to repair nothing
2280 * \retval negative error number on failure
2282 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2283 struct lfsck_component *com,
2284 struct lfsck_tgt_desc *ltd,
2285 struct lu_orphan_rec_v3 *rec,
2286 struct lu_fid *cfid,
2291 struct lfsck_thread_info *info = lfsck_env_info(env);
2292 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2293 char *name = info->lti_key;
2294 struct lu_attr *la = &info->lti_la2;
2295 struct dt_object_format *dof = &info->lti_dof;
2296 struct lfsck_instance *lfsck = com->lc_lfsck;
2297 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2298 struct lu_fid *tfid = &info->lti_fid3;
2299 struct dt_device *dev = lfsck->li_bottom;
2300 struct dt_object *lpf = lfsck->li_lpf_obj;
2301 struct dt_object *pobj = NULL;
2302 struct dt_object *cobj = NULL;
2303 struct thandle *th = NULL;
2304 struct lu_buf *ea_buf = &info->lti_big_buf;
2305 struct lu_buf lov_buf;
2306 struct lfsck_lock_handle *llh = &info->lti_llh;
2307 struct linkea_data ldata = { NULL };
2308 struct lu_buf linkea_buf;
2309 const struct lu_name *pname;
2315 if (unlikely(lpf == NULL))
2316 GOTO(log, rc = -ENXIO);
2318 /* We use two separated transactions to repair the inconsistency.
2320 * 1) create the MDT-object locally.
2321 * 2) update the OST-object's PFID EA if necessary.
2323 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2324 * updated when the layout LFSCK run next time.
2326 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2327 * when the layout LFSCK run next time. */
2329 if (fid_is_zero(pfid)) {
2330 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2334 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2336 GOTO(log, rc = PTR_ERR(cobj));
2339 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2341 GOTO(log, rc = PTR_ERR(pobj));
2343 LASSERT(infix != NULL);
2344 LASSERT(type != NULL);
2346 memset(la, 0, sizeof(*la));
2347 la->la_uid = rec->lor_rec.lor_uid;
2348 la->la_gid = rec->lor_rec.lor_gid;
2349 la->la_mode = S_IFREG | S_IRUSR;
2350 la->la_valid = LA_MODE | LA_UID | LA_GID;
2352 memset(dof, 0, sizeof(*dof));
2353 dof->dof_type = dt_mode_to_dft(S_IFREG);
2354 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2355 * the stripe(s). The LFSCK will specify the LOV EA via
2356 * lfsck_layout_update_lovea(). */
2358 size = lfsck_lovea_size(&rec->lor_layout, ea_off);
2359 if (ea_buf->lb_len < size) {
2360 lu_buf_realloc(ea_buf, size);
2361 if (ea_buf->lb_buf == NULL)
2362 GOTO(log, rc = -ENOMEM);
2367 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2369 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2370 (const struct dt_key *)name);
2371 if (rc != 0 && rc != -ENOENT)
2375 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2376 MDS_INODELOCK_UPDATE, LCK_PW);
2380 /* Re-check whether the name conflict with othrs after taken
2382 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
2383 (const struct dt_key *)name);
2384 if (unlikely(rc == 0)) {
2392 pname = lfsck_name_get_const(env, name, strlen(name));
2393 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2394 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2398 /* The 1st transaction. */
2399 th = dt_trans_create(env, dev);
2401 GOTO(unlock, rc = PTR_ERR(th));
2403 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2407 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2408 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2409 LU_XATTR_CREATE, th);
2413 dtrec->rec_fid = pfid;
2414 dtrec->rec_type = S_IFREG;
2415 rc = dt_declare_insert(env, lpf,
2416 (const struct dt_rec *)dtrec,
2417 (const struct dt_key *)name, th);
2421 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2422 ldata.ld_leh->leh_len);
2423 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2424 XATTR_NAME_LINK, 0, th);
2428 rc = dt_trans_start_local(env, dev, th);
2432 dt_write_lock(env, pobj, 0);
2433 rc = dt_create(env, pobj, la, NULL, dof, th);
2435 rc = lfsck_layout_update_lovea(env, lfsck, th, rec, pobj, cfid,
2436 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2437 dt_write_unlock(env, pobj);
2441 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2442 (const struct dt_key *)name, th);
2446 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2447 if (rc == 0 && cobj != NULL) {
2448 dt_trans_stop(env, dev, th);
2451 /* The 2nd transaction. */
2452 rc = __lfsck_layout_update_pfid(env, cobj, pfid,
2453 &rec->lor_layout, ea_off,
2454 rec->lor_layout_version,
2462 dt_trans_stop(env, dev, th);
2468 if (cobj != NULL && !IS_ERR(cobj))
2469 lfsck_object_put(env, cobj);
2470 if (pobj != NULL && !IS_ERR(pobj))
2471 lfsck_object_put(env, pobj);
2474 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2475 "recreate the lost MDT-object: parent "DFID
2476 ", child "DFID", OST-index %u, stripe-index %u, "
2477 "infix %s, type %s: rc = %d\n",
2478 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2479 ltd->ltd_index, ea_off, infix, type, rc);
2481 return rc >= 0 ? 1 : rc;
2484 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2485 struct lfsck_component *com,
2486 const struct lu_fid *fid,
2489 struct lfsck_thread_info *info = lfsck_env_info(env);
2490 struct lfsck_request *lr = &info->lti_lr;
2491 struct lfsck_instance *lfsck = com->lc_lfsck;
2492 struct lfsck_tgt_desc *ltd;
2493 struct ptlrpc_request *req;
2494 struct lfsck_request *tmp;
2495 struct obd_export *exp;
2499 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2500 if (unlikely(ltd == NULL))
2504 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2505 GOTO(put, rc = -EOPNOTSUPP);
2507 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2509 GOTO(put, rc = -ENOMEM);
2511 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2513 ptlrpc_request_free(req);
2518 memset(lr, 0, sizeof(*lr));
2519 lr->lr_event = LE_CONDITIONAL_DESTROY;
2520 lr->lr_active = LFSCK_TYPE_LAYOUT;
2523 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2525 ptlrpc_request_set_replen(req);
2527 rc = ptlrpc_queue_wait(req);
2528 ptlrpc_req_finished(req);
2538 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2539 struct lfsck_component *com,
2540 struct lfsck_request *lr)
2542 struct lfsck_thread_info *info = lfsck_env_info(env);
2543 struct lu_attr *la = &info->lti_la;
2544 union ldlm_policy_data *policy = &info->lti_policy;
2545 struct ldlm_res_id *resid = &info->lti_resid;
2546 struct lfsck_instance *lfsck = com->lc_lfsck;
2547 struct dt_device *dev = lfsck->li_bottom;
2548 struct lu_fid *fid = &lr->lr_fid;
2549 struct dt_object *obj;
2550 struct thandle *th = NULL;
2551 struct lustre_handle lh = { 0 };
2556 obj = lfsck_object_find_by_dev(env, dev, fid);
2558 RETURN(PTR_ERR(obj));
2560 dt_read_lock(env, obj, 0);
2561 if (dt_object_exists(obj) == 0 ||
2562 lfsck_is_dead_obj(obj)) {
2563 dt_read_unlock(env, obj);
2565 GOTO(put, rc = -ENOENT);
2568 /* Get obj's attr without lock firstly. */
2569 rc = dt_attr_get(env, obj, la);
2570 dt_read_unlock(env, obj);
2574 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2575 GOTO(put, rc = -ETXTBSY);
2577 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2578 LASSERT(lfsck->li_namespace != NULL);
2580 memset(policy, 0, sizeof(*policy));
2581 policy->l_extent.end = OBD_OBJECT_EOF;
2582 ost_fid_build_resid(fid, resid);
2583 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
2584 LDLM_EXTENT, policy, LCK_EX, &flags,
2585 ldlm_blocking_ast, ldlm_completion_ast,
2586 NULL, NULL, 0, LVB_T_NONE, NULL, &lh);
2588 GOTO(put, rc = -EIO);
2590 dt_write_lock(env, obj, 0);
2591 /* Get obj's attr within lock again. */
2592 rc = dt_attr_get(env, obj, la);
2596 if (la->la_ctime != 0)
2597 GOTO(unlock, rc = -ETXTBSY);
2599 th = dt_trans_create(env, dev);
2601 GOTO(unlock, rc = PTR_ERR(th));
2603 rc = dt_declare_ref_del(env, obj, th);
2607 rc = dt_declare_destroy(env, obj, th);
2611 rc = dt_trans_start_local(env, dev, th);
2615 rc = dt_ref_del(env, obj, th);
2619 rc = dt_destroy(env, obj, th);
2621 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2622 "OST-object "DFID" that was created for reparing "
2623 "dangling referenced case. But the original missing "
2624 "OST-object is found now.\n",
2625 lfsck_lfsck2name(lfsck), PFID(fid));
2630 dt_trans_stop(env, dev, th);
2633 dt_write_unlock(env, obj);
2634 ldlm_lock_decref(&lh, LCK_EX);
2637 lfsck_object_put(env, obj);
2643 * Some OST-object has occupied the specified layout EA slot.
2644 * Such OST-object may be generated by the LFSCK when repair
2645 * dangling referenced MDT-object, which can be indicated by
2646 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2647 * is true and such OST-object has not been modified yet, we
2648 * will replace it with the orphan OST-object; otherwise the
2649 * LFSCK will create new MDT-object to reference the orphan.
2651 * \retval +1: repaired
2652 * \retval 0: did nothing
2653 * \retval -ve: on error
2655 static int lfsck_layout_conflict_create(const struct lu_env *env,
2656 struct lfsck_component *com,
2657 struct lfsck_tgt_desc *ltd,
2658 struct lu_orphan_rec_v3 *rec,
2659 struct dt_object *parent,
2660 struct lu_fid *cfid,
2661 struct lu_buf *ea_buf,
2662 struct lov_mds_md_v1 *lmm,
2663 struct lov_ost_data_v1 *slot,
2664 __u32 ea_off, int lovea_size)
2666 struct lfsck_thread_info *info = lfsck_env_info(env);
2667 struct lu_fid *cfid2 = &info->lti_fid2;
2668 struct ost_id *oi = &info->lti_oi;
2669 struct dt_device *dev = lfsck_obj2dev(parent);
2670 struct thandle *th = NULL;
2671 struct lustre_handle lh = { 0 };
2672 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2676 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2677 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2681 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2682 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2686 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2687 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2692 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2694 /* If the conflict OST-obejct is not created for fixing dangling
2695 * referenced MDT-object in former LFSCK check/repair, or it has
2696 * been modified by others, then we cannot destroy it. Re-create
2697 * a new MDT-object for the orphan OST-object. */
2698 if (rc == -ETXTBSY) {
2699 /* No need the layout lock on the original parent. */
2700 lfsck_ibits_unlock(&lh, LCK_EX);
2702 fid_zero(&rec->lor_rec.lor_fid);
2703 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2704 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2706 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2707 info->lti_tmpbuf, "C", ea_off);
2712 if (rc != 0 && rc != -ENOENT)
2715 th = dt_trans_create(env, dev);
2717 GOTO(unlock, rc = PTR_ERR(th));
2719 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2720 LU_XATTR_REPLACE, th);
2724 rc = dt_trans_start_local(env, dev, th);
2728 dt_write_lock(env, parent, 0);
2729 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2730 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2731 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2732 ltd->ltd_index, lovea_size);
2733 dt_write_unlock(env, parent);
2738 dt_trans_stop(env, dev, th);
2741 lfsck_ibits_unlock(&lh, LCK_EX);
2744 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2745 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2746 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2747 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2748 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2751 return rc >= 0 ? 1 : rc;
2755 * \retval +1: repaired
2756 * \retval 0: did nothing
2757 * \retval -ve: on error
2759 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2760 struct lfsck_component *com,
2761 struct lfsck_tgt_desc *ltd,
2762 struct lu_orphan_rec_v3 *rec,
2763 struct dt_object *parent,
2764 struct lu_fid *cfid,
2765 __u32 ost_idx, __u32 ea_off)
2767 struct lfsck_thread_info *info = lfsck_env_info(env);
2768 struct lu_buf *buf = &info->lti_big_buf;
2769 struct lu_fid *fid = &info->lti_fid2;
2770 struct ost_id *oi = &info->lti_oi;
2771 struct lfsck_instance *lfsck = com->lc_lfsck;
2772 struct dt_device *dt = lfsck_obj2dev(parent);
2773 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2774 struct ost_layout *ol = &rec->lor_layout;
2775 struct lov_comp_md_v1 *lcm = NULL;
2776 struct lov_comp_md_entry_v1 *lcme = NULL;
2777 struct thandle *handle = NULL;
2779 struct lov_mds_md_v1 *lmm;
2780 struct lov_ost_data_v1 *objs;
2781 struct lustre_handle lh = { 0 };
2790 bool locked = false;
2791 bool new_mirror = true;
2794 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2795 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2798 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2799 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2800 "stripe-index %u, comp_id %u, comp_start %llu, "
2801 "comp_end %llu, layout version %u, range %u: rc = %d\n",
2802 lfsck_lfsck2name(lfsck), PFID(cfid),
2803 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2804 ol->ol_comp_id, ol->ol_comp_start,
2805 ol->ol_comp_end, rec->lor_layout_version,
2806 rec->lor_range, rc);
2813 dt_write_unlock(env, parent);
2817 if (handle != NULL) {
2818 dt_trans_stop(env, dt, handle);
2823 GOTO(unlock_layout, rc);
2826 if (buf->lb_len < lovea_size) {
2827 lu_buf_realloc(buf, lovea_size);
2828 if (buf->lb_buf == NULL)
2829 GOTO(unlock_layout, rc = -ENOMEM);
2832 if (!(bk->lb_param & LPF_DRYRUN)) {
2833 handle = dt_trans_create(env, dt);
2835 GOTO(unlock_layout, rc = PTR_ERR(handle));
2837 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2842 rc = dt_trans_start_local(env, dt, handle);
2847 dt_write_lock(env, parent, 0);
2849 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2850 if (rc == -ERANGE) {
2851 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2854 } else if (rc == -ENODATA || rc == 0) {
2855 lovea_size = lfsck_lovea_size(ol, ea_off);
2856 /* If the declared is not big enough, re-try. */
2857 if (buf->lb_len < lovea_size) {
2861 fl = LU_XATTR_CREATE;
2862 } else if (rc < 0) {
2863 GOTO(unlock_parent, rc);
2864 } else if (unlikely(buf->lb_len == 0)) {
2867 fl = LU_XATTR_REPLACE;
2871 if (fl == LU_XATTR_CREATE) {
2872 if (bk->lb_param & LPF_DRYRUN)
2873 GOTO(unlock_parent, rc = 1);
2875 LASSERT(buf->lb_len >= lovea_size);
2877 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2878 cfid, buf, fl, ost_idx, ea_off);
2880 GOTO(unlock_parent, rc);
2884 rc1 = lfsck_layout_verify_header(parent, lmm);
2886 /* If the LOV EA crashed, the rebuild it. */
2887 if (rc1 == -EINVAL) {
2888 if (bk->lb_param & LPF_DRYRUN)
2889 GOTO(unlock_parent, rc = 1);
2891 LASSERT(buf->lb_len >= lovea_size);
2893 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2894 cfid, buf, fl, ost_idx, ea_off);
2896 GOTO(unlock_parent, rc);
2899 /* For other unknown magic/pattern, keep the current LOV EA. */
2900 if (rc1 == -EOPNOTSUPP)
2901 GOTO(unlock_parent, rc1 = 0);
2904 GOTO(unlock_parent, rc = rc1);
2906 magic = le32_to_cpu(lmm->lmm_magic);
2907 if (magic == LOV_MAGIC_COMP_V1) {
2910 __u16 mirror_id0 = mirror_id_of(ol->ol_comp_id);
2913 if (bk->lb_param & LPF_DRYRUN)
2914 GOTO(unlock_parent, rc = 1);
2917 count = le16_to_cpu(lcm->lcm_entry_count);
2918 for (i = 0; i < count; pos = ++i) {
2919 lcme = &lcm->lcm_entries[i];
2920 start = le64_to_cpu(lcme->lcme_extent.e_start);
2921 end = le64_to_cpu(lcme->lcme_extent.e_end);
2922 mirror_id1 = mirror_id_of(le32_to_cpu(lcme->lcme_id));
2924 if (mirror_id0 > mirror_id1)
2927 if (mirror_id0 < mirror_id1)
2931 if (end <= ol->ol_comp_start)
2934 if (start >= ol->ol_comp_end)
2937 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
2938 magic = le32_to_cpu(lmm->lmm_magic);
2939 flags = le32_to_cpu(lcme->lcme_flags);
2943 rc = lfsck_layout_add_comp(env, lfsck, handle, rec, parent,
2944 cfid, buf, ost_idx, ea_off, pos, new_mirror);
2946 GOTO(unlock_parent, rc);
2950 count = le16_to_cpu(lmm->lmm_stripe_count);
2952 GOTO(unlock_parent, rc = -EINVAL);
2955 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2956 if (count <= ea_off) {
2957 if (bk->lb_param & LPF_DRYRUN)
2958 GOTO(unlock_parent, rc = 1);
2960 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2961 /* If the declared is not big enough, re-try. */
2962 if (buf->lb_len < lovea_size) {
2970 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
2971 lfsck_layout_update_lcm(lcm, lcme,
2972 rec->lor_layout_version,
2976 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
2977 parent, cfid, buf, ost_idx, ea_off);
2979 GOTO(unlock_parent, rc);
2982 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2984 if (magic == LOV_MAGIC_V1) {
2985 objs = &lmm->lmm_objects[0];
2987 LASSERT(magic == LOV_MAGIC_V3);
2988 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2991 for (i = 0; i < count; i++, objs++) {
2992 /* The MDT-object was created via lfsck_layout_recover_create()
2993 * by others before, and we fill the dummy layout EA. */
2994 if ((lcme && !(flags & LCME_FL_INIT)) ||
2995 lovea_slot_is_dummy(objs)) {
2999 if (bk->lb_param & LPF_DRYRUN)
3000 GOTO(unlock_parent, rc = 1);
3002 lmm->lmm_layout_gen =
3003 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3007 if (le32_to_cpu(lmm->lmm_stripe_size) !=
3008 ol->ol_stripe_size ||
3009 le16_to_cpu(lmm->lmm_stripe_count) !=
3010 ol->ol_stripe_count ||
3011 le64_to_cpu(lcme->lcme_extent.e_start) !=
3012 ol->ol_comp_start ||
3013 le64_to_cpu(lcme->lcme_extent.e_end) !=
3015 CDEBUG(D_LFSCK, "%s: found invalid "
3016 "component for "DFID ": parent "DFID
3017 ", stripe-index %u, stripe_size %u, "
3018 "stripe_count %u, comp_id %u, "
3019 "comp_start %llu, comp_end %llu, "
3020 "cur_stripe_size %u, "
3021 "cur_stripe_count %u, "
3022 "cur_comp_start %llu, "
3023 "cur_comp_end %llu\n",
3024 lfsck_lfsck2name(lfsck), PFID(cfid),
3025 PFID(lfsck_dto2fid(parent)), ea_off,
3027 ol->ol_stripe_count, ol->ol_comp_id,
3028 ol->ol_comp_start, ol->ol_comp_end,
3029 le32_to_cpu(lmm->lmm_stripe_size),
3030 le16_to_cpu(lmm->lmm_stripe_count),
3031 le64_to_cpu(lcme->lcme_extent.e_start),
3032 le64_to_cpu(lcme->lcme_extent.e_end));
3034 GOTO(unlock_parent, rc = -EINVAL);
3037 lovea_size = le32_to_cpu(lcm->lcm_size);
3038 lcme->lcme_flags = cpu_to_le32(flags |
3040 lfsck_layout_update_lcm(lcm, lcme,
3041 rec->lor_layout_version,
3045 LASSERTF(buf->lb_len >= lovea_size,
3046 "buffer len %d is less than real size %d\n",
3047 (int)buf->lb_len, (int)lovea_size);
3049 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
3050 parent, cfid, buf, lmm, objs,
3051 fl, ost_idx, lovea_size);
3053 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
3054 "dummy layout slot for "DFID": parent "DFID
3055 ", OST-index %u, stripe-index %u: rc = %d\n",
3056 lfsck_lfsck2name(lfsck), PFID(cfid),
3057 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
3059 GOTO(unlock_parent, rc);
3062 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3063 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
3065 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3066 "invalid layout EA at the slot %d, index %u\n",
3067 lfsck_lfsck2name(lfsck),
3068 PFID(lfsck_dto2fid(parent)), i,
3069 le32_to_cpu(objs->l_ost_idx));
3071 GOTO(unlock_parent, rc);
3074 /* It should be rare case, the slot is there, but the LFSCK
3075 * does not handle it during the first-phase cycle scanning. */
3076 if (unlikely(lu_fid_eq(fid, cfid))) {
3078 GOTO(unlock_parent, rc = 0);
3080 /* Rare case that the OST-object index
3081 * does not match the parent MDT-object
3082 * layout EA. We trust the later one. */
3083 if (bk->lb_param & LPF_DRYRUN)
3084 GOTO(unlock_parent, rc = 1);
3086 dt_write_unlock(env, parent);
3088 dt_trans_stop(env, dt, handle);
3089 lfsck_ibits_unlock(&lh, LCK_EX);
3090 rc = lfsck_layout_update_pfid(env, com, parent,
3094 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
3095 "updated OST-object's pfid for "DFID
3096 ": parent "DFID", OST-index %u, "
3097 "stripe-index %u: rc = %d\n",
3098 lfsck_lfsck2name(lfsck), PFID(cfid),
3099 PFID(lfsck_dto2fid(parent)),
3100 ltd->ltd_index, i, rc);
3107 /* The MDT-object exists, but related layout EA slot is occupied
3109 if (bk->lb_param & LPF_DRYRUN)
3110 GOTO(unlock_parent, rc = 1);
3112 dt_write_unlock(env, parent);
3114 dt_trans_stop(env, dt, handle);
3115 lfsck_ibits_unlock(&lh, LCK_EX);
3116 if (magic == LOV_MAGIC_V1)
3117 objs = &lmm->lmm_objects[ea_off];
3119 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
3120 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
3121 buf, lmm, objs, ea_off, lovea_size);
3127 dt_write_unlock(env, parent);
3131 dt_trans_stop(env, dt, handle);
3134 lfsck_ibits_unlock(&lh, LCK_EX);
3139 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3140 struct lfsck_component *com,
3141 struct lfsck_tgt_desc *ltd,
3142 struct lu_orphan_rec_v3 *rec,
3143 struct lu_fid *cfid)
3145 struct lfsck_layout *lo = com->lc_file_ram;
3146 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3147 struct dt_object *parent = NULL;
3148 __u32 ea_off = pfid->f_stripe_idx;
3152 if (!fid_is_sane(cfid))
3153 GOTO(out, rc = -EINVAL);
3156 if (fid_is_zero(pfid)) {
3157 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3162 if (!fid_is_sane(pfid))
3163 GOTO(out, rc = -EINVAL);
3165 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3167 GOTO(out, rc = PTR_ERR(parent));
3169 if (unlikely(dt_object_remote(parent) != 0))
3170 GOTO(put, rc = -EXDEV);
3172 if (dt_object_exists(parent) == 0) {
3173 lfsck_object_put(env, parent);
3174 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3179 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3180 GOTO(put, rc = -EISDIR);
3182 /* The orphan OST-object claims to be the parent's stripe, then
3183 * related dangling record in the trace file is meaningless. */
3184 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3185 rec->lor_layout.ol_comp_id, ea_off);
3186 if (rc && rc != -ENOENT)
3189 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3190 ltd->ltd_index, ea_off);
3196 lfsck_object_put(env, parent);
3198 /* The layout EA is changed, need to be reloaded next time. */
3199 dt_object_put_nocache(env, parent);
3202 down_write(&com->lc_sem);
3203 com->lc_new_scanned++;
3204 com->lc_new_checked++;
3206 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3208 } else if (rc < 0) {
3209 lo->ll_objs_failed_phase2++;
3211 up_write(&com->lc_sem);
3216 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3217 struct lfsck_component *com,
3218 struct lfsck_tgt_desc *ltd)
3220 struct lfsck_assistant_data *lad = com->lc_data;
3221 struct lfsck_instance *lfsck = com->lc_lfsck;
3222 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3223 struct lfsck_thread_info *info = lfsck_env_info(env);
3224 struct lu_fid *fid = &info->lti_fid;
3225 struct dt_object *obj;
3226 const struct dt_it_ops *iops;
3231 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3232 "scanning for OST%04x\n",
3233 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3235 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
3236 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3237 "scanning for OST%04x\n",
3238 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3243 fid->f_seq = fid_idif_seq(0, ltd->ltd_index);
3244 fid->f_oid = fid->f_ver = 0;
3246 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3247 if (unlikely(IS_ERR(obj)))
3248 GOTO(log, rc = PTR_ERR(obj));
3250 rc = obj->do_ops->do_index_try(env, obj,
3251 &dt_lfsck_layout_orphan_features);
3255 iops = &obj->do_index_ops->dio_it;
3256 di = iops->init(env, obj, 0);
3258 GOTO(put, rc = PTR_ERR(di));
3260 rc = iops->load(env, di, 0);
3262 /* -ESRCH means that the orphan OST-objects rbtree has been
3263 * cleanup because of the OSS server restart or other errors. */
3264 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3269 rc = iops->next(env, di);
3281 struct lu_orphan_rec_v3 *rec = &info->lti_rec;
3283 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3284 unlikely(!thread_is_running(&lfsck->li_thread)))
3287 key = iops->key(env, di);
3288 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3289 /* Remote target OST may be runnning old LFSCK */
3290 memset(rec, 0, sizeof(*rec));
3291 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3293 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3294 &com->lc_fid_latest_scanned_phase2);
3295 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3298 lfsck_control_speed_by_self(com);
3300 rc = iops->next(env, di);
3301 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3308 iops->fini(env, di);
3310 lfsck_object_put(env, obj);
3313 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3314 "scanning for OST%04x: rc = %d\n",
3315 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3317 return rc > 0 ? 0 : rc;
3320 static int lfsck_lov2layout(struct lov_mds_md_v1 *lmm, struct filter_fid *ff,
3323 struct ost_layout *ol = &ff->ff_layout;
3324 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3328 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3329 ol->ol_stripe_size = lmm->lmm_stripe_size;
3330 ol->ol_stripe_count = lmm->lmm_stripe_count;
3331 ol->ol_comp_start = 0;
3332 ol->ol_comp_end = 0;
3334 ff->ff_layout_version = 0;
3336 } else if (magic == LOV_MAGIC_COMP_V1) {
3337 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3338 struct lov_comp_md_entry_v1 *lcme = NULL;
3339 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3342 for (i = 0; i < count; i++) {
3343 lcme = &lcm->lcm_entries[i];
3344 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3345 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3352 /* The comp has been removed, do nothing. */
3356 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3357 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3358 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3359 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3360 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3361 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3362 ff->ff_layout_version = le32_to_cpu(lcme->lcme_layout_gen);
3365 GOTO(out, rc = -EINVAL);
3375 * Repair the MDT-object with dangling LOV EA reference.
3377 * we need to repair the inconsistency according to the users' requirement:
3379 * 1) Keep the inconsistency there and report the inconsistency case,
3380 * then give the chance to the application to find related issues,
3381 * and the users can make the decision about how to handle it with
3382 * more human knownledge. (by default)
3384 * 2) Re-create the missing OST-object with the FID/owner information.
3386 * \param[in] env pointer to the thread context
3387 * \param[in] com the layout LFSCK component
3388 * \param[in] parent the MDT-object with dangling LOV EA reference
3389 * \param[in] child the OST-object to be created
3390 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3391 * \param[in] ea_off the offset of the OST-object in the LOV EA
3392 * \param[in] ost_idx the index of OST on which the OST-object resides
3394 * \retval +1 for repair successfully
3395 * \retval 0 for did nothing
3396 * \retval negative error number on failure
3398 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3399 struct lfsck_component *com,
3400 struct dt_object *parent,
3401 struct dt_object *child,
3402 __u32 comp_id, __u32 ea_off,
3403 __u32 ost_idx, bool log)
3405 struct lfsck_thread_info *info = lfsck_env_info(env);
3406 struct filter_fid *ff = &info->lti_ff;
3407 struct dt_object_format *dof = &info->lti_dof;
3408 struct lu_attr *la = &info->lti_la;
3409 struct lfsck_instance *lfsck = com->lc_lfsck;
3410 struct dt_device *dev = lfsck_obj2dev(child);
3411 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3412 const struct lu_fid *cfid = lfsck_dto2fid(child);
3413 struct lu_buf *tbuf = &info->lti_big_buf;
3414 struct thandle *handle;
3416 struct lustre_handle lh = { 0 };
3420 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3423 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3424 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3429 rc = dt_attr_get(env, parent, la);
3433 la->la_mode = S_IFREG | 0666;
3434 la->la_atime = la->la_mtime = la->la_ctime = 0;
3435 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3436 LA_ATIME | LA_MTIME | LA_CTIME;
3437 memset(dof, 0, sizeof(*dof));
3438 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3439 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3440 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3441 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3442 * parent MDT-object's layout EA. */
3443 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3445 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3446 if (unlikely(rc == -ENODATA))
3451 rc = lfsck_lov2layout(tbuf->lb_buf, ff, comp_id);
3455 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3456 handle = dt_trans_create(env, dev);
3458 GOTO(unlock1, rc = PTR_ERR(handle));
3460 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3464 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3465 LU_XATTR_CREATE, handle);
3469 rc = dt_trans_start_local(env, dev, handle);
3473 dt_read_lock(env, parent, 0);
3474 if (unlikely(lfsck_is_dead_obj(parent)))
3475 GOTO(unlock2, rc = 0);
3477 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3478 struct ost_id *oi = &info->lti_oi;
3479 struct lu_fid *tfid = &info->lti_fid2;
3480 struct lu_buf *lovea = &info->lti_big_buf;
3481 struct lov_mds_md_v1 *lmm;
3482 struct lov_ost_data_v1 *objs;
3487 rc = lfsck_layout_get_lovea(env, parent, lovea);
3488 if (unlikely(rc == -ENODATA))
3493 lmm = lovea->lb_buf;
3494 magic = le32_to_cpu(lmm->lmm_magic);
3495 if (magic == LOV_MAGIC_COMP_V1) {
3496 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3497 struct lov_comp_md_entry_v1 *lcme;
3498 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3501 for (i = 0; i < count; i++) {
3502 lcme = &lcm->lcm_entries[i];
3503 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3504 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3507 lmm = lovea->lb_buf +
3508 le32_to_cpu(lcme->lcme_offset);
3509 magic = le32_to_cpu(lmm->lmm_magic);
3514 /* Someone removed the component, do nothing. */
3515 GOTO(unlock2, rc = 0);
3519 count = le16_to_cpu(lmm->lmm_stripe_count);
3520 /* Someone changed the LOV EA, do nothing. */
3521 if (count <= ea_off)
3522 GOTO(unlock2, rc = 0);
3524 if (magic == LOV_MAGIC_V1) {
3525 objs = &lmm->lmm_objects[ea_off];
3527 LASSERT(magic == LOV_MAGIC_V3);
3529 objs = &((struct lov_mds_md_v3 *)lmm)->\
3530 lmm_objects[ea_off];
3533 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3534 idx2 = le32_to_cpu(objs->l_ost_idx);
3535 rc = ostid_to_fid(tfid, oi, idx2);
3536 /* Someone changed the LOV EA, do nothing. */
3537 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3541 rc = dt_create(env, child, la, NULL, dof, handle);
3545 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3551 dt_read_unlock(env, parent);
3554 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3557 lfsck_ibits_unlock(&lh, LCK_EX);
3561 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3562 "dangling reference for: parent "DFID", child "
3563 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3565 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3566 comp_id, ea_off, ost_idx,
3567 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3568 "Create the lost OST-object as required" :
3569 "Keep the MDT-object there by default", rc);
3575 * Repair the MDT-object with dangling LOV EA reference.
3577 * Prepare parameters and call __lfsck_layout_repair_dangling()
3578 * to repair the dangling LOV EA reference.
3580 * \param[in] env pointer to the thread context
3581 * \param[in] com the layout LFSCK component
3582 * \param[in] pfid the MDT-object's FID
3583 * \param[in] cfid the FID for the OST-object to be created
3584 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3585 * \param[in] ea_off the offset of the OST-object in the LOV EA
3586 * \param[in] ost_idx the index of OST on which the OST-object resides
3588 * \retval +1 for repair successfully
3589 * \retval 0 for did nothing
3590 * \retval negative error number on failure
3592 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3593 struct lfsck_component *com,
3594 const struct lu_fid *pfid,
3595 const struct lu_fid *cfid,
3596 __u32 comp_id, __u32 ea_off,
3599 struct lfsck_instance *lfsck = com->lc_lfsck;
3600 struct dt_object *parent = NULL;
3601 struct dt_object *child = NULL;
3602 struct lfsck_tgt_desc *ltd;
3606 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3608 GOTO(log, rc = PTR_ERR(parent));
3610 /* The MDT-object has been removed. */
3611 if (dt_object_exists(parent) == 0)
3614 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3615 if (unlikely(ltd == NULL))
3616 GOTO(log, rc = -ENODEV);
3618 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3620 GOTO(log, rc = PTR_ERR(child));
3622 /* The OST-object has been created. */
3623 if (unlikely(dt_object_exists(child) != 0))
3626 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3627 comp_id, ea_off, ost_idx, false);
3632 if (child != NULL && !IS_ERR(child))
3633 lfsck_object_put(env, child);
3635 if (parent != NULL && !IS_ERR(parent))
3636 lfsck_object_put(env, parent);
3639 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3640 "dangling reference for: parent "DFID", child "
3641 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3642 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3643 comp_id, ea_off, ost_idx,
3644 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3645 "Create the lost OST-object as required" :
3646 "Keep the MDT-object there by default", rc);
3651 /* If the OST-object does not recognize the MDT-object as its parent, and
3652 * there is no other MDT-object claims as its parent, then just trust the
3653 * given MDT-object as its parent. So update the OST-object filter_fid. */
3654 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3655 struct lfsck_component *com,
3656 struct dt_object *parent,
3657 struct lfsck_layout_req *llr,
3660 struct lfsck_thread_info *info = lfsck_env_info(env);
3661 struct filter_fid *ff = &info->lti_ff;
3662 struct dt_object *child = llr->llr_child;
3663 struct dt_device *dev = lfsck_obj2dev(child);
3664 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3665 struct lu_buf *tbuf = &info->lti_big_buf;
3666 struct thandle *handle;
3668 struct lustre_handle lh = { 0 };
3672 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3673 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3678 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3679 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3680 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3681 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3682 * parent MDT-object's layout EA. */
3683 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3685 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3686 if (unlikely(rc == -ENODATA))
3691 rc = lfsck_lov2layout(tbuf->lb_buf, ff, llr->llr_comp_id);
3695 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3697 handle = dt_trans_create(env, dev);
3699 GOTO(unlock1, rc = PTR_ERR(handle));
3701 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3705 rc = dt_attr_get(env, parent, la);
3709 la->la_valid = LA_UID | LA_GID;
3710 rc = dt_declare_attr_set(env, child, la, handle);
3714 rc = dt_trans_start_local(env, dev, handle);
3718 dt_write_lock(env, parent, 0);
3719 if (unlikely(lfsck_is_dead_obj(parent)))
3720 GOTO(unlock2, rc = 1);
3722 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3726 /* Get the latest parent's owner. */
3727 rc = dt_attr_get(env, parent, la);
3731 la->la_valid = LA_UID | LA_GID;
3732 rc = dt_attr_set(env, child, la, handle);
3737 dt_write_unlock(env, parent);
3740 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3743 lfsck_ibits_unlock(&lh, LCK_EX);
3747 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3748 "unmatched MDT-OST pair for: parent "DFID
3749 ", child "DFID", comp_id %u, OST-index %u, "
3750 "stripe-index %u, owner %u/%u: rc = %d\n",
3751 lfsck_lfsck2name(com->lc_lfsck),
3752 PFID(lfsck_dto2fid(parent)),
3753 PFID(lfsck_dto2fid(child)),
3754 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3755 la->la_uid, la->la_gid, rc);
3760 /* If there are more than one MDT-objects claim as the OST-object's parent,
3761 * and the OST-object only recognizes one of them, then we need to generate
3762 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3763 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3764 struct lfsck_component *com,
3765 struct dt_object *parent,
3766 struct lfsck_layout_req *llr,
3769 struct lfsck_thread_info *info = lfsck_env_info(env);
3770 struct dt_allocation_hint *hint = &info->lti_hint;
3771 struct dt_object_format *dof = &info->lti_dof;
3772 struct ost_id *oi = &info->lti_oi;
3773 struct lu_buf *buf = &info->lti_big_buf;
3774 struct lfsck_instance *lfsck = com->lc_lfsck;
3775 struct dt_device *dev;
3776 struct lu_device *d =
3777 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3778 struct lu_object *o;
3779 struct lu_object *n;
3780 struct dt_object *child = NULL;
3781 struct thandle *handle = NULL;
3782 struct lov_mds_md_v1 *lmm;
3783 struct lov_ost_data_v1 *objs;
3784 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3786 struct lustre_handle lh = { 0 };
3792 /* We use two separated transactions to repair the inconsistency.
3794 * 1) create the child (OST-object).
3795 * 2) update the parent LOV EA according to the child's FID.
3797 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3798 * handled as orphan when the layout LFSCK run next time.
3800 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3801 * as dangling referened case when the layout LFSCK run next time. */
3803 /* The 1st transaction. */
3804 o = lu_object_anon(env, d, NULL);
3806 GOTO(log, rc = PTR_ERR(o));
3808 n = lu_object_locate(o->lo_header, d->ld_type);
3809 if (unlikely(n == NULL)) {
3810 lu_object_put_nocache(env, o);
3812 GOTO(log, rc = -EINVAL);
3815 child = container_of(n, struct dt_object, do_lu);
3816 memset(hint, 0, sizeof(*hint));
3817 rc = dt_attr_get(env, parent, la);
3821 la->la_valid = LA_UID | LA_GID;
3822 memset(dof, 0, sizeof(*dof));
3824 dev = lfsck_obj2dev(child);
3825 handle = dt_trans_create(env, dev);
3827 GOTO(log, rc = PTR_ERR(handle));
3829 rc = dt_declare_create(env, child, la, hint, dof, handle);
3833 rc = dt_trans_start_local(env, dev, handle);
3837 rc = dt_create(env, child, la, hint, dof, handle);
3838 dt_trans_stop(env, dev, handle);
3843 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3844 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3849 /* The 2nd transaction. */
3851 /* XXX: Generally, we should use bottom device (OSD) to update parent
3852 * LOV EA. But because the LOD-object still references the wrong
3853 * OSP-object that should be detached after the parent's LOV EA
3854 * refreshed. Unfortunately, there is no suitable API for that.
3855 * So we have to make the LOD to re-load the OSP-object(s) via
3856 * replacing the LOV EA against the LOD-object.
3858 * Once the DNE2 patches have been landed, we can replace the
3859 * LOD device with the OSD device. LU-6230. */
3861 dev = lfsck->li_next;
3862 parent = lfsck_object_locate(dev, parent);
3864 GOTO(log, rc = PTR_ERR(parent));
3866 handle = dt_trans_create(env, dev);
3868 GOTO(log, rc = PTR_ERR(handle));
3870 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3871 LU_XATTR_REPLACE, handle);
3875 rc = dt_trans_start_local(env, dev, handle);
3879 dt_write_lock(env, parent, 0);
3880 if (unlikely(lfsck_is_dead_obj(parent)))
3881 GOTO(unlock, rc = 0);
3883 rc = lfsck_layout_get_lovea(env, parent, buf);
3884 if (unlikely(rc == -ENODATA))
3890 magic = le32_to_cpu(lmm->lmm_magic);
3891 if (magic == LOV_MAGIC_COMP_V1) {
3892 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3893 struct lov_comp_md_entry_v1 *lcme;
3894 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3897 LASSERT(llr->llr_comp_id != 0);
3899 for (i = 0; i < count; i++) {
3900 lcme = &lcm->lcm_entries[i];
3901 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3902 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3905 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3907 le32_to_cpu(lcme->lcme_offset);
3908 magic = le32_to_cpu(lmm->lmm_magic);
3913 GOTO(unlock, rc = 0);
3917 if (magic == LOV_MAGIC_V1) {
3918 objs = &lmm->lmm_objects[llr->llr_lov_idx];
3920 LASSERT(magic == LOV_MAGIC_V3);
3922 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3925 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3926 index = le32_to_cpu(objs->l_ost_idx);
3927 rc = ostid_to_fid(&tfid, oi, index);
3928 /* Someone changed layout during the LFSCK, no need to repair then. */
3929 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3930 GOTO(unlock, rc = 0);
3932 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3933 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3934 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3935 objs->l_ost_gen = cpu_to_le32(0);
3936 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3937 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3938 LU_XATTR_REPLACE, handle);
3940 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3943 dt_write_unlock(env, parent);
3947 dt_trans_stop(env, dev, handle);
3950 lfsck_ibits_unlock(&lh, LCK_EX);
3952 lfsck_object_put(env, child);
3955 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3956 "multiple references for: parent "DFID", comp_id %u, "
3957 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
3958 lfsck_lfsck2name(lfsck), PFID(pfid),
3959 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3960 la->la_uid, la->la_gid, rc);
3965 /* If the MDT-object and the OST-object have different owner information,
3966 * then trust the MDT-object, because the normal chown/chgrp handle order
3967 * is from MDT to OST, and it is possible that some chown/chgrp operation
3968 * is partly done. */
3969 static int lfsck_layout_repair_owner(const struct lu_env *env,
3970 struct lfsck_component *com,
3971 struct dt_object *parent,
3972 struct lfsck_layout_req *llr,
3973 struct lu_attr *pla,
3974 const struct lu_attr *cla)
3976 struct lfsck_thread_info *info = lfsck_env_info(env);
3977 struct lu_attr *tla = &info->lti_la2;
3978 struct dt_object *child = llr->llr_child;
3979 struct dt_device *dev = lfsck_obj2dev(child);
3980 struct thandle *handle;
3982 dt_obj_version_t version;
3985 tla->la_uid = pla->la_uid;
3986 tla->la_gid = pla->la_gid;
3987 tla->la_valid = LA_UID | LA_GID;
3988 handle = dt_trans_create(env, dev);
3990 GOTO(log, rc = PTR_ERR(handle));
3992 rc = dt_declare_attr_set(env, child, tla, handle);
3996 rc = dt_trans_start_local(env, dev, handle);
4000 /* Use the dt_object lock to serialize with destroy and attr_set. */
4001 dt_read_lock(env, parent, 0);
4002 if (unlikely(lfsck_is_dead_obj(parent)))
4003 GOTO(unlock, rc = 1);
4005 version = dt_version_get(env, child);
4006 if (version == -EOPNOTSUPP)
4009 /* Get the latest parent's owner. */
4010 rc = dt_attr_get(env, parent, pla);
4014 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
4015 if (unlikely((!version && tla->la_ctime == 0) ||
4016 tla->la_uid != pla->la_uid || tla->la_gid != pla->la_gid))
4019 rc = dt_attr_set(env, child, tla, handle);
4024 dt_read_unlock(env, parent);
4027 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
4031 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4032 "inconsistent file owner for: parent "DFID", child "DFID
4033 ", OST-index %u, stripe-index %u, old owner %u/%u, "
4034 "new owner %u/%u: rc = %d\n",
4035 lfsck_lfsck2name(com->lc_lfsck),
4036 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4037 llr->llr_ost_idx, llr->llr_lov_idx,
4038 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
4043 /* Check whether the OST-object correctly back points to the
4044 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
4045 static int lfsck_layout_check_parent(const struct lu_env *env,
4046 struct lfsck_component *com,
4047 struct lfsck_assistant_object *lso,
4048 struct filter_fid *ff,
4049 const struct lu_fid *cfid,
4050 const struct lu_attr *cla,
4051 struct lfsck_layout_req *llr)
4053 struct lfsck_thread_info *info = lfsck_env_info(env);
4054 struct lu_buf *buf = &info->lti_big_buf;
4055 struct lu_fid *pfid = &info->lti_fid;
4056 struct dt_object *tobj;
4057 struct lov_mds_md_v1 *lmm;
4058 struct lov_ost_data_v1 *objs;
4059 struct lustre_handle lh = { 0 };
4067 *pfid = ff->ff_parent;
4068 idx = pfid->f_stripe_idx;
4071 if (unlikely(!fid_is_sane(pfid)))
4072 RETURN(LLIT_UNMATCHED_PAIR);
4074 if (lu_fid_eq(pfid, &lso->lso_fid)) {
4075 if (likely(llr->llr_lov_idx == idx))
4078 RETURN(LLIT_UNMATCHED_PAIR);
4081 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4083 RETURN(PTR_ERR(tobj));
4085 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
4086 !S_ISREG(lfsck_object_type(tobj)))
4087 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4089 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
4090 * remote one on another MDT. Then check whether the given OST-object
4091 * is in such layout. If yes, it is multiple referenced, otherwise it
4092 * is unmatched referenced case. */
4093 rc = lfsck_layout_get_lovea(env, tobj, buf);
4094 if (rc == 0 || rc == -ENODATA || rc == -ENOENT)
4095 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4097 if (unlikely(rc == -EOPNOTSUPP))
4098 GOTO(out, rc = LLIT_NONE);
4104 magic = le32_to_cpu(lmm->lmm_magic);
4105 if (magic == LOV_MAGIC_COMP_V1) {
4106 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4107 struct lov_comp_md_entry_v1 *lcme;
4109 if (ff->ff_layout.ol_comp_id == 0)
4110 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4112 count = le16_to_cpu(lcm->lcm_entry_count);
4113 for (i = 0; i < count; i++) {
4114 lcme = &lcm->lcm_entries[i];
4115 if (le32_to_cpu(lcme->lcme_id) ==
4116 ff->ff_layout.ol_comp_id) {
4118 le32_to_cpu(lcme->lcme_offset);
4119 magic = le32_to_cpu(lmm->lmm_magic);
4120 if (!(le32_to_cpu(lcme->lcme_flags) &
4122 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4128 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4132 if (magic == LOV_MAGIC_V1) {
4133 objs = &lmm->lmm_objects[0];
4135 LASSERT(magic == LOV_MAGIC_V3);
4136 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4139 count = le16_to_cpu(lmm->lmm_stripe_count);
4140 for (i = 0; i < count; i++, objs++) {
4141 struct lu_fid *tfid = &info->lti_fid2;
4142 struct ost_id *oi = &info->lti_oi;
4145 if (lovea_slot_is_dummy(objs))
4148 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4149 idx2 = le32_to_cpu(objs->l_ost_idx);
4150 rc = ostid_to_fid(tfid, oi, idx2);
4152 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4153 "invalid layout EA at the slot %d, index %u\n",
4154 lfsck_lfsck2name(com->lc_lfsck),
4155 PFID(pfid), i, idx2);
4157 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4160 if (lu_fid_eq(cfid, tfid)) {
4161 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4162 MDS_INODELOCK_UPDATE |
4163 MDS_INODELOCK_LAYOUT |
4164 MDS_INODELOCK_XATTR,
4169 dt_read_lock(env, tobj, 0);
4171 /* For local MDT-object, re-check existence
4172 * after taken the lock. */
4173 if (!dt_object_remote(tobj)) {
4174 if (dt_object_exists(tobj) == 0 ||
4175 lfsck_is_dead_obj(tobj))
4176 rc = LLIT_UNMATCHED_PAIR;
4178 rc = LLIT_MULTIPLE_REFERENCED;
4183 /* For migration case, the new MDT-object and old
4184 * MDT-object may reference the same OST-object at
4185 * some migration internal time.
4187 * For remote MDT-object, the local MDT may not know
4188 * whether it has been removed or not. Try checking
4189 * for a non-existent xattr to check if this object
4190 * has been been removed or not. */
4191 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4193 if (unlikely(rc == -ENOENT || rc >= 0))
4194 rc = LLIT_UNMATCHED_PAIR;
4195 else if (rc == -ENODATA)
4196 rc = LLIT_MULTIPLE_REFERENCED;
4202 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4205 if (lustre_handle_is_used(&lh)) {
4206 dt_read_unlock(env, tobj);
4207 lfsck_ibits_unlock(&lh, LCK_EX);
4211 lfsck_object_put(env, tobj);
4216 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4217 struct lfsck_component *com,
4218 struct lfsck_assistant_req *lar)
4220 struct lfsck_layout_req *llr =
4221 container_of0(lar, struct lfsck_layout_req, llr_lar);
4222 struct lfsck_assistant_object *lso = lar->lar_parent;
4223 struct lfsck_layout *lo = com->lc_file_ram;
4224 struct lfsck_thread_info *info = lfsck_env_info(env);
4225 struct filter_fid *ff = &info->lti_ff;
4226 struct lu_buf buf = { .lb_buf = ff,
4227 .lb_len = sizeof(*ff) };
4228 struct dt_object *parent = NULL;
4229 struct dt_object *child = llr->llr_child;
4230 struct lu_attr *pla = &lso->lso_attr;
4231 struct lu_attr *cla = &info->lti_la;
4232 struct lfsck_instance *lfsck = com->lc_lfsck;
4233 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4234 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4241 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
4243 rc = dt_attr_get(env, child, cla);
4244 if (rc == -ENOENT) {
4245 parent = lfsck_assistant_object_load(env, lfsck, lso);
4246 if (IS_ERR(parent)) {
4247 rc = PTR_ERR(parent);
4249 RETURN(rc == -ENOENT ? 0 : rc);
4252 type = LLIT_DANGLING;
4259 lfsck_buf_init(&buf, ff, sizeof(*ff));
4260 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4261 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4262 type = LLIT_UNMATCHED_PAIR;
4266 if (rc < 0 && rc != -ENODATA)
4269 if (rc == 0 || rc == -ENODATA)
4270 GOTO(check_owner, rc = 0);
4272 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4273 rc = lfsck_layout_check_parent(env, com, lso, ff,
4274 lu_object_fid(&child->do_lu), cla, llr);
4284 /* Someone may has changed the owner after the parent attr pre-loaded.
4285 * It can be handled later inside the lfsck_layout_repair_owner(). */
4286 if (unlikely(cla->la_uid != pla->la_uid ||
4287 cla->la_gid != pla->la_gid)) {
4288 type = LLIT_INCONSISTENT_OWNER;
4293 if (type == LLIT_NONE)
4296 if (bk->lb_param & LPF_DRYRUN)
4299 if (parent == NULL) {
4300 parent = lfsck_assistant_object_load(env, lfsck, lso);
4301 if (IS_ERR(parent)) {
4302 rc = PTR_ERR(parent);
4313 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4314 rc = lfsck_layout_ins_dangling_rec(env, com,
4315 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4316 llr->llr_comp_id, llr->llr_lov_idx,
4319 rc = __lfsck_layout_repair_dangling(env, com, parent,
4326 case LLIT_UNMATCHED_PAIR:
4327 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4330 case LLIT_MULTIPLE_REFERENCED:
4331 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4334 case LLIT_INCONSISTENT_OWNER:
4335 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4345 down_write(&com->lc_sem);
4347 struct lfsck_assistant_data *lad = com->lc_data;
4349 if (unlikely(lad->lad_exit)) {
4351 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4352 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4353 rc == -EHOSTUNREACH) {
4354 /* If cannot touch the target server,
4355 * mark the LFSCK as INCOMPLETE. */
4356 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4357 "talk with OST %x: rc = %d\n",
4358 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4359 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4360 lo->ll_objs_skipped++;
4363 lfsck_layout_record_failure(env, lfsck, lo);
4365 } else if (rc > 0 && (type != LLIT_DANGLING ||
4366 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4367 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4368 "unknown type = %d\n", type);
4370 lo->ll_objs_repaired[type - 1]++;
4371 if (bk->lb_param & LPF_DRYRUN &&
4372 unlikely(lo->ll_pos_first_inconsistent == 0))
4373 lo->ll_pos_first_inconsistent =
4374 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4377 up_write(&com->lc_sem);
4379 if (parent != NULL && !IS_ERR(parent))
4380 lfsck_object_put(env, parent);
4386 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4387 struct lfsck_component *com,
4388 struct dt_object *obj, bool first)
4390 struct lfsck_instance *lfsck = com->lc_lfsck;
4391 struct ptlrpc_thread *thread = &lfsck->li_thread;
4392 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4393 struct lfsck_layout *lo = com->lc_file_ram;
4394 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4397 struct lfsck_layout_dangling_key *parent =
4398 &lfsck_env_info(env)->lti_lldk;
4399 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4404 di = iops->init(env, obj, 0);
4406 RETURN(PTR_ERR(di));
4409 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4411 memset(parent, 0, sizeof(*parent));
4412 rc = iops->get(env, di, (const struct dt_key *)parent);
4417 /* The start one either has been processed or does not exist,
4419 rc = iops->next(env, di);
4425 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4426 unlikely(!thread_is_running(thread)))
4429 key = iops->key(env, di);
4438 lldk_be_to_cpu(parent,
4439 (const struct lfsck_layout_dangling_key *)key);
4440 if (!fid_is_sane(&parent->lldk_fid)) {
4445 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4447 fid_be_to_cpu(cfid, cfid);
4448 ost_idx = cfid->f_ver;
4450 if (!fid_is_sane(cfid)) {
4455 rc = lfsck_layout_repair_dangling(env, com,
4456 &parent->lldk_fid, cfid,
4457 parent->lldk_comp_id,
4458 parent->lldk_ea_off, ost_idx);
4462 down_write(&com->lc_sem);
4463 com->lc_new_checked++;
4464 com->lc_new_scanned++;
4466 lo->ll_lldk_latest_scanned_phase2 = *parent;
4469 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4471 lo->ll_objs_failed_phase2++;
4472 up_write(&com->lc_sem);
4474 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4477 if (unlikely(com->lc_time_next_checkpoint <=
4478 ktime_get_seconds()) &&
4479 com->lc_new_checked != 0) {
4480 down_write(&com->lc_sem);
4481 lo->ll_run_time_phase2 += ktime_get_seconds() -
4482 com->lc_time_last_checkpoint;
4483 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
4484 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4485 com->lc_new_checked = 0;
4486 lfsck_layout_store(env, com);
4487 up_write(&com->lc_sem);
4489 com->lc_time_last_checkpoint = ktime_get_seconds();
4490 com->lc_time_next_checkpoint =
4491 com->lc_time_last_checkpoint +
4492 LFSCK_CHECKPOINT_INTERVAL;
4495 lfsck_control_speed_by_self(com);
4496 if (unlikely(!thread_is_running(thread)))
4499 rc = iops->next(env, di);
4508 iops->fini(env, di);
4513 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4514 struct lfsck_component *com)
4516 struct lfsck_assistant_data *lad = com->lc_data;
4517 struct lfsck_instance *lfsck = com->lc_lfsck;
4518 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4519 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4520 struct lfsck_tgt_desc *ltd;
4524 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4525 lfsck_lfsck2name(lfsck));
4527 spin_lock(<ds->ltd_lock);
4528 while (!list_empty(&lad->lad_ost_phase2_list)) {
4529 ltd = list_entry(lad->lad_ost_phase2_list.next,
4530 struct lfsck_tgt_desc,
4531 ltd_layout_phase_list);
4532 list_del_init(<d->ltd_layout_phase_list);
4533 if (bk->lb_param & LPF_OST_ORPHAN) {
4534 spin_unlock(<ds->ltd_lock);
4535 rc = lfsck_layout_scan_orphan(env, com, ltd);
4536 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4539 if (unlikely(lad->lad_exit ||
4540 !thread_is_running(&lfsck->li_thread)))
4542 spin_lock(<ds->ltd_lock);
4546 if (list_empty(&lad->lad_ost_phase1_list))
4550 spin_unlock(<ds->ltd_lock);
4552 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4553 struct lfsck_layout *lo = com->lc_file_ram;
4556 com->lc_new_checked = 0;
4557 com->lc_new_scanned = 0;
4558 com->lc_time_last_checkpoint = ktime_get_seconds();
4559 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4560 LFSCK_CHECKPOINT_INTERVAL;
4562 i = lfsck_sub_trace_file_fid2idx(
4563 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4564 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4565 com->lc_sub_trace_objs[i].lsto_obj, true);
4566 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4567 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4568 com->lc_sub_trace_objs[i].lsto_obj, false);
4570 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4571 "at the No. %d trace file: rc = %d\n",
4572 lfsck_lfsck2name(lfsck), i, rc);
4575 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4576 lfsck_lfsck2name(lfsck), rc);
4582 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4583 struct ptlrpc_request *req,
4586 struct lfsck_layout_slave_async_args *llsaa = args;
4587 struct obd_export *exp = llsaa->llsaa_exp;
4588 struct lfsck_component *com = llsaa->llsaa_com;
4589 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4590 struct lfsck_layout_slave_data *llsd = com->lc_data;
4591 struct lfsck_reply *lr = NULL;
4595 /* It is probably caused by network trouble, or target crash,
4596 * it will try several times (depends on the obd_timeout, and
4597 * will not less than 3 times). But to make the LFSCK can go
4598 * ahead, we should not try for ever. After some try but still
4599 * hit failure, it will assume that the target exit the LFSCK
4600 * prcoessing and stop try. */
4601 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4602 int max_try = max_t(int, obd_timeout / 30, 3);
4604 if (++(llst->llst_failures) > max_try)
4610 llst->llst_failures = 0;
4611 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4612 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4613 lr->lr_status != LS_SCANNING_PHASE2)
4618 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4619 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4620 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4621 llst->llst_failures);
4623 lfsck_layout_llst_del(llsd, llst);
4626 lfsck_layout_llst_put(llst);
4627 lfsck_component_put(env, com);
4628 class_export_put(exp);
4633 static int lfsck_layout_async_query(const struct lu_env *env,
4634 struct lfsck_component *com,
4635 struct obd_export *exp,
4636 struct lfsck_layout_slave_target *llst,
4637 struct lfsck_request *lr,
4638 struct ptlrpc_request_set *set)
4640 struct lfsck_layout_slave_async_args *llsaa;
4641 struct ptlrpc_request *req;
4642 struct lfsck_request *tmp;
4646 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4650 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4652 ptlrpc_request_free(req);
4656 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4658 ptlrpc_request_set_replen(req);
4660 llsaa = ptlrpc_req_async_args(req);
4661 llsaa->llsaa_exp = exp;
4662 llsaa->llsaa_com = lfsck_component_get(com);
4663 llsaa->llsaa_llst = llst;
4664 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4665 req->rq_allow_intr = 1;
4666 req->rq_no_delay = 1;
4667 ptlrpc_set_add_req(set, req);
4672 static int lfsck_layout_async_notify(const struct lu_env *env,
4673 struct obd_export *exp,
4674 struct lfsck_request *lr,
4675 struct ptlrpc_request_set *set)
4677 struct ptlrpc_request *req;
4678 struct lfsck_request *tmp;
4682 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4686 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4688 ptlrpc_request_free(req);
4692 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4694 ptlrpc_request_set_replen(req);
4695 req->rq_allow_intr = 1;
4696 req->rq_no_delay = 1;
4697 ptlrpc_set_add_req(set, req);
4703 lfsck_layout_slave_query_master(const struct lu_env *env,
4704 struct lfsck_component *com)
4706 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4707 struct lfsck_instance *lfsck = com->lc_lfsck;
4708 struct lfsck_layout_slave_data *llsd = com->lc_data;
4709 struct lfsck_layout_slave_target *llst;
4710 struct obd_export *exp;
4711 struct ptlrpc_request_set *set;
4716 set = ptlrpc_prep_set();
4718 GOTO(log, rc = -ENOMEM);
4720 memset(lr, 0, sizeof(*lr));
4721 lr->lr_event = LE_QUERY;
4722 lr->lr_active = LFSCK_TYPE_LAYOUT;
4724 llsd->llsd_touch_gen++;
4725 spin_lock(&llsd->llsd_lock);
4726 while (!list_empty(&llsd->llsd_master_list)) {
4727 llst = list_entry(llsd->llsd_master_list.next,
4728 struct lfsck_layout_slave_target,
4730 if (llst->llst_gen == llsd->llsd_touch_gen)
4733 llst->llst_gen = llsd->llsd_touch_gen;
4734 list_move_tail(&llst->llst_list,
4735 &llsd->llsd_master_list);
4736 atomic_inc(&llst->llst_ref);
4737 spin_unlock(&llsd->llsd_lock);
4739 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4742 lfsck_layout_llst_del(llsd, llst);
4743 lfsck_layout_llst_put(llst);
4744 spin_lock(&llsd->llsd_lock);
4748 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4750 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4751 "query %s for layout: rc = %d\n",
4752 lfsck_lfsck2name(lfsck),
4753 exp->exp_obd->obd_name, rc);
4756 lfsck_layout_llst_put(llst);
4757 class_export_put(exp);
4759 spin_lock(&llsd->llsd_lock);
4761 spin_unlock(&llsd->llsd_lock);
4763 rc = ptlrpc_set_wait(env, set);
4764 ptlrpc_set_destroy(set);
4766 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
4769 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
4770 lfsck_lfsck2name(com->lc_lfsck), rc);
4776 lfsck_layout_slave_notify_master(const struct lu_env *env,
4777 struct lfsck_component *com,
4778 enum lfsck_events event, int result)
4780 struct lfsck_layout *lo = com->lc_file_ram;
4781 struct lfsck_instance *lfsck = com->lc_lfsck;
4782 struct lfsck_layout_slave_data *llsd = com->lc_data;
4783 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4784 struct lfsck_layout_slave_target *llst;
4785 struct obd_export *exp;
4786 struct ptlrpc_request_set *set;
4790 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
4791 lfsck_lfsck2name(com->lc_lfsck));
4793 set = ptlrpc_prep_set();
4797 memset(lr, 0, sizeof(*lr));
4798 lr->lr_event = event;
4799 lr->lr_flags = LEF_FROM_OST;
4800 lr->lr_status = result;
4801 lr->lr_index = lfsck_dev_idx(lfsck);
4802 lr->lr_active = LFSCK_TYPE_LAYOUT;
4803 lr->lr_flags2 = lo->ll_flags;
4804 llsd->llsd_touch_gen++;
4805 spin_lock(&llsd->llsd_lock);
4806 while (!list_empty(&llsd->llsd_master_list)) {
4807 llst = list_entry(llsd->llsd_master_list.next,
4808 struct lfsck_layout_slave_target,
4810 if (llst->llst_gen == llsd->llsd_touch_gen)
4813 llst->llst_gen = llsd->llsd_touch_gen;
4814 list_move_tail(&llst->llst_list,
4815 &llsd->llsd_master_list);
4816 atomic_inc(&llst->llst_ref);
4817 spin_unlock(&llsd->llsd_lock);
4819 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4822 lfsck_layout_llst_del(llsd, llst);
4823 lfsck_layout_llst_put(llst);
4824 spin_lock(&llsd->llsd_lock);
4828 rc = lfsck_layout_async_notify(env, exp, lr, set);
4830 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4831 "notify %s for layout: rc = %d\n",
4832 lfsck_lfsck2name(lfsck),
4833 exp->exp_obd->obd_name, rc);
4835 lfsck_layout_llst_put(llst);
4836 class_export_put(exp);
4837 spin_lock(&llsd->llsd_lock);
4839 spin_unlock(&llsd->llsd_lock);
4841 ptlrpc_set_wait(env, set);
4842 ptlrpc_set_destroy(set);
4848 * \ret -ENODATA: unrecognized stripe
4849 * \ret = 0 : recognized stripe
4850 * \ret < 0 : other failures
4852 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
4853 struct lfsck_component *com,
4854 struct lu_fid *cfid,
4855 struct lu_fid *pfid, __u32 comp_id)
4857 struct lfsck_thread_info *info = lfsck_env_info(env);
4858 struct lu_buf *buf = &info->lti_big_buf;
4859 struct ost_id *oi = &info->lti_oi;
4860 struct dt_object *obj;
4861 struct lov_mds_md_v1 *lmm;
4862 struct lov_ost_data_v1 *objs;
4863 __u32 idx = pfid->f_stripe_idx;
4871 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4873 RETURN(PTR_ERR(obj));
4875 dt_read_lock(env, obj, 0);
4876 if (unlikely(dt_object_exists(obj) == 0 ||
4877 lfsck_is_dead_obj(obj)))
4878 GOTO(unlock, rc = -ENOENT);
4880 if (!S_ISREG(lfsck_object_type(obj)))
4881 GOTO(unlock, rc = -ENODATA);
4883 rc = lfsck_layout_get_lovea(env, obj, buf);
4888 magic = le32_to_cpu(lmm->lmm_magic);
4889 if (magic == LOV_MAGIC_COMP_V1) {
4890 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4891 struct lov_comp_md_entry_v1 *lcme;
4894 GOTO(unlock, rc = -ENODATA);
4896 count = le16_to_cpu(lcm->lcm_entry_count);
4897 for (i = 0; i < count; i++) {
4898 lcme = &lcm->lcm_entries[i];
4899 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
4901 le32_to_cpu(lcme->lcme_offset);
4902 magic = le32_to_cpu(lmm->lmm_magic);
4903 if (!(le32_to_cpu(lcme->lcme_flags) &
4905 GOTO(unlock, rc = -ENODATA);
4911 GOTO(unlock, rc = -ENODATA);
4915 if (magic == LOV_MAGIC_V1) {
4916 objs = &lmm->lmm_objects[0];
4918 LASSERT(magic == LOV_MAGIC_V3);
4919 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4922 fid_to_ostid(cfid, oi);
4923 count = le16_to_cpu(lmm->lmm_stripe_count);
4924 for (i = 0; i < count; i++, objs++) {
4927 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
4928 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
4929 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
4932 GOTO(unlock, rc = -ENODATA);
4935 dt_read_unlock(env, obj);
4936 lfsck_object_put(env, obj);
4942 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
4943 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
4944 * layout EA from MDT to OST. On one hand, the OST no need to understand
4945 * the layout EA structure; on the other hand, it may cause trouble when
4946 * transfer large layout EA from MDT to OST via normal OUT RPC.
4948 * \ret > 0: unrecognized stripe
4949 * \ret = 0: recognized stripe
4950 * \ret < 0: other failures
4952 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
4953 struct lfsck_component *com,
4954 struct lu_fid *cfid,
4955 struct lu_fid *pfid, __u32 comp_id)
4957 struct lfsck_instance *lfsck = com->lc_lfsck;
4958 struct obd_device *obd = lfsck->li_obd;
4959 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4960 struct obd_export *exp = NULL;
4961 struct ptlrpc_request *req = NULL;
4962 struct lfsck_request *lr;
4963 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
4967 if (unlikely(fid_is_idif(pfid)))
4970 fld_range_set_any(range);
4971 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
4973 RETURN(rc == -ENOENT ? 1 : rc);
4975 if (unlikely(!fld_range_is_mdt(range)))
4978 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
4979 if (unlikely(exp == NULL))
4982 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
4983 GOTO(out, rc = -EOPNOTSUPP);
4985 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4987 GOTO(out, rc = -ENOMEM);
4989 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4991 ptlrpc_request_free(req);
4996 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4997 memset(lr, 0, sizeof(*lr));
4998 lr->lr_event = LE_PAIRS_VERIFY;
4999 lr->lr_active = LFSCK_TYPE_LAYOUT;
5000 lr->lr_fid = *cfid; /* OST-object itself FID. */
5001 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
5002 lr->lr_comp_id = comp_id;
5004 ptlrpc_request_set_replen(req);
5005 rc = ptlrpc_queue_wait(req);
5006 ptlrpc_req_finished(req);
5008 if (rc == -ENOENT || rc == -ENODATA)
5015 class_export_put(exp);
5020 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
5021 struct lfsck_component *com,
5022 struct lfsck_req_local *lrl)
5024 struct dt_object *obj;
5028 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
5030 GOTO(log, rc = PTR_ERR(obj));
5032 dt_write_lock(env, obj, 0);
5033 if (unlikely(dt_object_exists(obj) == 0 ||
5034 lfsck_is_dead_obj(obj)))
5035 GOTO(unlock, rc = 0);
5037 rc = __lfsck_layout_update_pfid(env, obj, &lrl->lrl_ff_client.ff_parent,
5038 &lrl->lrl_ff_client.ff_layout,
5039 lrl->lrl_ff_client.ff_layout_version,
5040 lrl->lrl_ff_client.ff_range,
5041 lrl->lrl_ff_client.ff_parent.f_ver);
5046 dt_write_unlock(env, obj);
5047 lfsck_object_put(env, obj);
5050 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
5051 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
5052 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
5059 static void lfsck_layout_slave_quit(const struct lu_env *env,
5060 struct lfsck_component *com);
5062 static int lfsck_layout_reset(const struct lu_env *env,
5063 struct lfsck_component *com, bool init)
5065 struct lfsck_layout *lo = com->lc_file_ram;
5068 down_write(&com->lc_sem);
5070 memset(lo, 0, com->lc_file_size);
5072 __u32 count = lo->ll_success_count;
5073 time64_t last_time = lo->ll_time_last_complete;
5075 memset(lo, 0, com->lc_file_size);
5076 lo->ll_success_count = count;
5077 lo->ll_time_last_complete = last_time;
5080 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
5081 lo->ll_status = LS_INIT;
5083 if (com->lc_lfsck->li_master) {
5084 struct lfsck_assistant_data *lad = com->lc_data;
5086 lad->lad_incomplete = 0;
5087 CFS_RESET_BITMAP(lad->lad_bitmap);
5090 rc = lfsck_layout_store(env, com);
5091 if (rc == 0 && com->lc_lfsck->li_master)
5092 rc = lfsck_load_sub_trace_files(env, com,
5093 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
5094 up_write(&com->lc_sem);
5096 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
5097 lfsck_lfsck2name(com->lc_lfsck), rc);
5102 static void lfsck_layout_fail(const struct lu_env *env,
5103 struct lfsck_component *com, bool new_checked)
5105 struct lfsck_layout *lo = com->lc_file_ram;
5107 down_write(&com->lc_sem);
5109 com->lc_new_checked++;
5110 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
5111 up_write(&com->lc_sem);
5114 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
5115 struct lfsck_component *com, bool init)
5117 struct lfsck_instance *lfsck = com->lc_lfsck;
5118 struct lfsck_layout *lo = com->lc_file_ram;
5122 rc = lfsck_checkpoint_generic(env, com);
5124 return rc > 0 ? 0 : rc;
5127 down_write(&com->lc_sem);
5129 lo->ll_pos_latest_start =
5130 lfsck->li_pos_checkpoint.lp_oit_cookie;
5132 lo->ll_pos_last_checkpoint =
5133 lfsck->li_pos_checkpoint.lp_oit_cookie;
5134 lo->ll_run_time_phase1 += ktime_get_seconds() -
5135 lfsck->li_time_last_checkpoint;
5136 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5137 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5138 com->lc_new_checked = 0;
5141 rc = lfsck_layout_store(env, com);
5142 up_write(&com->lc_sem);
5144 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5145 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5146 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5151 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5152 struct lfsck_component *com, bool init)
5154 struct lfsck_instance *lfsck = com->lc_lfsck;
5155 struct lfsck_layout *lo = com->lc_file_ram;
5158 if (com->lc_new_checked == 0 && !init)
5161 down_write(&com->lc_sem);
5163 lo->ll_pos_latest_start =
5164 lfsck->li_pos_checkpoint.lp_oit_cookie;
5166 lo->ll_pos_last_checkpoint =
5167 lfsck->li_pos_checkpoint.lp_oit_cookie;
5168 lo->ll_run_time_phase1 += ktime_get_seconds() -
5169 lfsck->li_time_last_checkpoint;
5170 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5171 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5172 com->lc_new_checked = 0;
5175 rc = lfsck_layout_store(env, com);
5176 up_write(&com->lc_sem);
5178 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5179 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5180 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5185 static int lfsck_layout_prep(const struct lu_env *env,
5186 struct lfsck_component *com,
5187 struct lfsck_start *start)
5189 struct lfsck_instance *lfsck = com->lc_lfsck;
5190 struct lfsck_layout *lo = com->lc_file_ram;
5191 struct lfsck_position *pos = &com->lc_pos_start;
5193 fid_zero(&pos->lp_dir_parent);
5194 pos->lp_dir_cookie = 0;
5195 if (lo->ll_status == LS_COMPLETED ||
5196 lo->ll_status == LS_PARTIAL ||
5197 /* To handle orphan, must scan from the beginning. */
5198 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5201 rc = lfsck_layout_reset(env, com, false);
5203 rc = lfsck_set_param(env, lfsck, start, true);
5206 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5207 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5213 down_write(&com->lc_sem);
5214 lo->ll_time_latest_start = ktime_get_real_seconds();
5215 spin_lock(&lfsck->li_lock);
5216 if (lo->ll_flags & LF_SCANNED_ONCE) {
5217 if (!lfsck->li_drop_dryrun ||
5218 lo->ll_pos_first_inconsistent == 0) {
5219 lo->ll_status = LS_SCANNING_PHASE2;
5220 list_move_tail(&com->lc_link,
5221 &lfsck->li_list_double_scan);
5222 pos->lp_oit_cookie = 0;
5226 lo->ll_status = LS_SCANNING_PHASE1;
5227 lo->ll_run_time_phase1 = 0;
5228 lo->ll_run_time_phase2 = 0;
5229 lo->ll_objs_checked_phase1 = 0;
5230 lo->ll_objs_checked_phase2 = 0;
5231 lo->ll_objs_failed_phase1 = 0;
5232 lo->ll_objs_failed_phase2 = 0;
5233 for (i = 0; i < LLIT_MAX; i++)
5234 lo->ll_objs_repaired[i] = 0;
5236 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5237 fid_zero(&com->lc_fid_latest_scanned_phase2);
5240 lo->ll_status = LS_SCANNING_PHASE1;
5241 if (!lfsck->li_drop_dryrun ||
5242 lo->ll_pos_first_inconsistent == 0)
5243 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5245 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5247 spin_unlock(&lfsck->li_lock);
5248 up_write(&com->lc_sem);
5253 static int lfsck_layout_slave_prep(const struct lu_env *env,
5254 struct lfsck_component *com,
5255 struct lfsck_start_param *lsp)
5257 struct lfsck_layout_slave_data *llsd = com->lc_data;
5258 struct lfsck_instance *lfsck = com->lc_lfsck;
5259 struct lfsck_layout *lo = com->lc_file_ram;
5260 struct lfsck_start *start = lsp->lsp_start;
5263 rc = lfsck_layout_prep(env, com, start);
5267 if (lo->ll_flags & LF_CRASHED_LASTID &&
5268 list_empty(&llsd->llsd_master_list)) {
5269 LASSERT(lfsck->li_out_notify != NULL);
5271 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5272 LE_LASTID_REBUILDING);
5275 if (!lsp->lsp_index_valid)
5278 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5279 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5280 LASSERT(!llsd->llsd_rbtree_valid);
5282 down_write(&llsd->llsd_rb_rwsem);
5283 rc = lfsck_rbtree_setup(env, com);
5284 up_write(&llsd->llsd_rb_rwsem);
5287 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5288 "%llu]\n", lfsck_lfsck2name(lfsck),
5289 com->lc_pos_start.lp_oit_cookie);
5294 static int lfsck_layout_master_prep(const struct lu_env *env,
5295 struct lfsck_component *com,
5296 struct lfsck_start_param *lsp)
5301 rc = lfsck_layout_load_bitmap(env, com);
5303 rc = lfsck_layout_reset(env, com, false);
5305 rc = lfsck_set_param(env, com->lc_lfsck,
5306 lsp->lsp_start, true);
5312 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5316 rc = lfsck_start_assistant(env, com, lsp);
5321 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5322 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5323 com->lc_pos_start.lp_oit_cookie);
5328 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5329 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5330 struct lfsck_component *com,
5331 struct dt_object *parent,
5332 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5334 struct lfsck_thread_info *info = lfsck_env_info(env);
5335 struct lfsck_instance *lfsck = com->lc_lfsck;
5336 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5337 struct lfsck_layout *lo = com->lc_file_ram;
5338 struct lfsck_assistant_data *lad = com->lc_data;
5339 struct lfsck_assistant_object *lso = NULL;
5340 struct lov_ost_data_v1 *objs;
5341 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5342 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5343 struct ptlrpc_thread *athread = &lad->lad_thread;
5344 struct l_wait_info lwi = { 0 };
5352 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5353 magic = le32_to_cpu(lmm->lmm_magic);
5354 if (magic == LOV_MAGIC_V1) {
5355 objs = &lmm->lmm_objects[0];
5357 LASSERT(magic == LOV_MAGIC_V3);
5358 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5361 count = le16_to_cpu(lmm->lmm_stripe_count);
5362 for (i = 0; i < count; i++, objs++) {
5363 struct lu_fid *fid = &info->lti_fid;
5364 struct ost_id *oi = &info->lti_oi;
5365 struct lfsck_layout_req *llr;
5366 struct lfsck_tgt_desc *tgt = NULL;
5367 struct dt_object *cobj = NULL;
5369 bool wakeup = false;
5371 if (unlikely(lovea_slot_is_dummy(objs)))
5374 l_wait_event(mthread->t_ctl_waitq,
5375 lad->lad_prefetched < bk->lb_async_windows ||
5376 !thread_is_running(mthread) ||
5377 thread_is_stopped(athread),
5380 if (unlikely(!thread_is_running(mthread)) ||
5381 thread_is_stopped(athread))
5384 if (unlikely(lfsck_is_dead_obj(parent)))
5387 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5388 index = le32_to_cpu(objs->l_ost_idx);
5389 rc = ostid_to_fid(fid, oi, index);
5391 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5392 ": "DOSTID", idx %u, comp_id %u\n",
5393 lfsck_lfsck2name(lfsck),
5394 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5399 tgt = lfsck_tgt_get(ltds, index);
5400 if (unlikely(tgt == NULL)) {
5401 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5402 "did not join the layout LFSCK, comp_id %u\n",
5403 lfsck_lfsck2name(lfsck), index, comp_id);
5404 lfsck_lad_set_bitmap(env, com, index);
5408 /* There is potential deadlock race condition between object
5409 * destroy and layout LFSCK. Consider the following scenario:
5411 * 1) The LFSCK thread obtained the parent object firstly, at
5412 * that time, the parent object has not been destroyed yet.
5414 * 2) One RPC service thread destroyed the parent and all its
5415 * children objects. Because the LFSCK is referencing the
5416 * parent object, then the parent object will be marked as
5417 * dying in RAM. On the other hand, the parent object is
5418 * referencing all its children objects, then all children
5419 * objects will be marked as dying in RAM also.
5421 * 3) The LFSCK thread tries to find some child object with
5422 * the parent object referenced. Then it will find that the
5423 * child object is dying. According to the object visibility
5424 * rules: the object with dying flag cannot be returned to
5425 * others. So the LFSCK thread has to wait until the dying
5426 * object has been purged from RAM, then it can allocate a
5427 * new object (with the same FID) in RAM. Unfortunately, the
5428 * LFSCK thread itself is referencing the parent object, and
5429 * cause the parent object cannot be purged, then cause the
5430 * child object cannot be purged also. So the LFSCK thread
5431 * will fall into deadlock.
5433 cobj = lfsck_object_find_by_dev(env, tgt->ltd_tgt, fid);
5435 if (lfsck_is_dead_obj(parent)) {
5445 rc = dt_declare_attr_get(env, cobj);
5449 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
5454 struct lu_attr *attr = &info->lti_la;
5456 rc = dt_attr_get(env, parent, attr);
5460 lso = lfsck_assistant_object_init(env,
5461 lfsck_dto2fid(parent), attr,
5462 lfsck->li_pos_current.lp_oit_cookie, false);
5471 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5479 spin_lock(&lad->lad_lock);
5480 if (lad->lad_assistant_status < 0) {
5481 spin_unlock(&lad->lad_lock);
5482 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5484 RETURN(lad->lad_assistant_status);
5487 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5488 if (lad->lad_prefetched == 0)
5491 lad->lad_prefetched++;
5492 spin_unlock(&lad->lad_lock);
5494 wake_up_all(&athread->t_ctl_waitq);
5497 down_write(&com->lc_sem);
5498 com->lc_new_checked++;
5500 lfsck_layout_record_failure(env, lfsck, lo);
5501 up_write(&com->lc_sem);
5503 if (cobj != NULL && !IS_ERR(cobj))
5504 lfsck_object_put(env, cobj);
5506 if (likely(tgt != NULL))
5509 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5517 lfsck_assistant_object_put(env, lso);
5522 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5523 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5524 * list ::lad_req_list.
5526 * For each request on above list, the lfsck_layout_assistant thread compares
5527 * the OST side attribute with local attribute, if inconsistent, then repair it.
5529 * All above processing is async mode with pipeline. */
5530 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5531 struct lfsck_component *com,
5532 struct dt_object *obj)
5534 struct lfsck_thread_info *info = lfsck_env_info(env);
5535 struct ost_id *oi = &info->lti_oi;
5536 struct lfsck_layout *lo = com->lc_file_ram;
5537 struct lfsck_assistant_data *lad = com->lc_data;
5538 struct lfsck_instance *lfsck = com->lc_lfsck;
5539 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5540 struct thandle *handle = NULL;
5541 struct lu_buf *buf = &info->lti_big_buf;
5542 struct lov_mds_md_v1 *lmm = NULL;
5543 struct dt_device *dev = lfsck_obj2dev(obj);
5544 struct lustre_handle lh = { 0 };
5545 struct lu_buf ea_buf = { NULL };
5546 struct lov_comp_md_v1 *lcm = NULL;
5547 struct lov_comp_md_entry_v1 *lcme = NULL;
5552 bool locked = false;
5553 bool stripe = false;
5554 bool bad_oi = false;
5557 if (!S_ISREG(lfsck_object_type(obj)))
5560 if (lad->lad_assistant_status < 0)
5561 GOTO(out, rc = -ESRCH);
5563 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5564 lmm_oi_cpu_to_le(oi, oi);
5565 dt_read_lock(env, obj, 0);
5570 if (dt_object_exists(obj) == 0 ||
5571 lfsck_is_dead_obj(obj))
5574 rc = lfsck_layout_get_lovea(env, obj, buf);
5575 if (rc == -EINVAL || rc == -ENODATA || rc == -EOPNOTSUPP)
5576 /* Skip bad lov EA during the 1st cycle scanning, and
5577 * try to recover it via orphan in the 2nd scanning. */
5584 magic = le32_to_cpu(lmm->lmm_magic);
5585 if (magic == LOV_MAGIC_COMP_V1) {
5589 count = le16_to_cpu(lcm->lcm_entry_count);
5590 for (i = 0; i < count; i++) {
5591 lcme = &lcm->lcm_entries[i];
5592 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5593 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) != 0)
5597 GOTO(out, stripe = true);
5598 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5599 GOTO(out, stripe = true);
5603 /* Inconsistent lmm_oi, should be repaired. */
5606 if (bk->lb_param & LPF_DRYRUN) {
5607 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5609 GOTO(out, stripe = true);
5612 if (!lustre_handle_is_used(&lh)) {
5613 dt_read_unlock(env, obj);
5615 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5616 MDS_INODELOCK_LAYOUT |
5617 MDS_INODELOCK_XATTR, LCK_EX);
5621 handle = dt_trans_create(env, dev);
5623 GOTO(out, rc = PTR_ERR(handle));
5625 lfsck_buf_init(&ea_buf, lmm, size);
5626 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5627 LU_XATTR_REPLACE, handle);
5631 rc = dt_trans_start_local(env, dev, handle);
5635 dt_write_lock(env, obj, 0);
5641 if (magic == LOV_MAGIC_COMP_V1) {
5644 for (i = 0; i < count; i++) {
5645 lcme = &lcm->lcm_entries[i];
5646 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5653 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5654 LU_XATTR_REPLACE, handle);
5658 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5660 GOTO(out, stripe = true);
5664 if (lustre_handle_is_used(&lh))
5665 dt_write_unlock(env, obj);
5667 dt_read_unlock(env, obj);
5670 if (handle != NULL && !IS_ERR(handle))
5671 dt_trans_stop(env, dev, handle);
5673 lfsck_ibits_unlock(&lh, LCK_EX);
5676 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5677 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5678 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5679 PFID(lfsck_dto2fid(obj)), rc);
5682 if (magic == LOV_MAGIC_COMP_V1) {
5685 for (i = 0; i < count; i++) {
5686 lcme = &lcm->lcm_entries[i];
5687 if (!(le32_to_cpu(lcme->lcme_flags) &
5691 rc = lfsck_layout_scan_stripes(env, com, obj,
5692 (struct lov_mds_md_v1 *)(buf->lb_buf +
5693 le32_to_cpu(lcme->lcme_offset)),
5694 le32_to_cpu(lcme->lcme_id));
5697 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5700 down_write(&com->lc_sem);
5701 com->lc_new_checked++;
5703 lfsck_layout_record_failure(env, lfsck, lo);
5704 up_write(&com->lc_sem);
5710 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5711 struct lfsck_component *com,
5712 struct dt_object *obj)
5714 struct lfsck_instance *lfsck = com->lc_lfsck;
5715 struct lfsck_layout *lo = com->lc_file_ram;
5716 const struct lu_fid *fid = lfsck_dto2fid(obj);
5717 struct lfsck_layout_slave_data *llsd = com->lc_data;
5718 struct lfsck_layout_seq *lls;
5724 LASSERT(llsd != NULL);
5726 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5727 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5728 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
5730 struct ptlrpc_thread *thread = &lfsck->li_thread;
5732 l_wait_event(thread->t_ctl_waitq,
5733 !thread_is_running(thread),
5737 lfsck_rbtree_update_bitmap(env, com, fid, false);
5739 down_write(&com->lc_sem);
5740 if (fid_is_idif(fid))
5742 else if (!fid_is_norm(fid) ||
5743 !fid_is_for_ostobj(env, lfsck, obj, fid))
5744 GOTO(unlock, rc = 0);
5747 com->lc_new_checked++;
5749 lls = lfsck_layout_seq_lookup(llsd, seq);
5752 if (unlikely(lls == NULL))
5753 GOTO(unlock, rc = -ENOMEM);
5755 INIT_LIST_HEAD(&lls->lls_list);
5757 rc = lfsck_layout_lastid_load(env, com, lls);
5759 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5760 "load LAST_ID for %#llx: rc = %d\n",
5761 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5762 lo->ll_objs_failed_phase1++;
5767 lfsck_layout_seq_insert(llsd, lls);
5770 if (unlikely(fid_is_last_id(fid)))
5771 GOTO(unlock, rc = 0);
5773 if (fid_is_idif(fid))
5774 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5778 if (oid > lls->lls_lastid_known)
5779 lls->lls_lastid_known = oid;
5781 if (oid > lls->lls_lastid) {
5782 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
5783 /* OFD may create new objects during LFSCK scanning. */
5784 rc = lfsck_layout_lastid_reload(env, com, lls);
5785 if (unlikely(rc != 0)) {
5786 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5787 "reload LAST_ID for %#llx: rc = %d\n",
5788 lfsck_lfsck2name(com->lc_lfsck),
5794 if (oid <= lls->lls_lastid ||
5795 lo->ll_flags & LF_CRASHED_LASTID)
5796 GOTO(unlock, rc = 0);
5798 LASSERT(lfsck->li_out_notify != NULL);
5800 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5801 LE_LASTID_REBUILDING);
5802 lo->ll_flags |= LF_CRASHED_LASTID;
5804 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
5805 "LAST_ID file (2) for the sequence %#llx"
5806 ", old value %llu, known value %llu\n",
5807 lfsck_lfsck2name(lfsck), lls->lls_seq,
5808 lls->lls_lastid, oid);
5811 lls->lls_lastid = oid;
5815 GOTO(unlock, rc = 0);
5818 up_write(&com->lc_sem);
5823 static int lfsck_layout_exec_dir(const struct lu_env *env,
5824 struct lfsck_component *com,
5825 struct lfsck_assistant_object *lso,
5826 struct lu_dirent *ent, __u16 type)
5831 static int lfsck_layout_master_post(const struct lu_env *env,
5832 struct lfsck_component *com,
5833 int result, bool init)
5835 struct lfsck_instance *lfsck = com->lc_lfsck;
5836 struct lfsck_layout *lo = com->lc_file_ram;
5840 lfsck_post_generic(env, com, &result);
5842 down_write(&com->lc_sem);
5843 spin_lock(&lfsck->li_lock);
5845 lo->ll_pos_last_checkpoint =
5846 lfsck->li_pos_checkpoint.lp_oit_cookie;
5849 if (lo->ll_flags & LF_INCOMPLETE)
5850 lo->ll_status = LS_PARTIAL;
5852 lo->ll_status = LS_SCANNING_PHASE2;
5853 lo->ll_flags |= LF_SCANNED_ONCE;
5854 lo->ll_flags &= ~LF_UPGRADE;
5855 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5856 } else if (result == 0) {
5857 if (lfsck->li_status != 0)
5858 lo->ll_status = lfsck->li_status;
5860 lo->ll_status = LS_STOPPED;
5861 if (lo->ll_status != LS_PAUSED)
5862 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5864 lo->ll_status = LS_FAILED;
5865 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5867 spin_unlock(&lfsck->li_lock);
5870 lo->ll_run_time_phase1 += ktime_get_seconds() -
5871 lfsck->li_time_last_checkpoint;
5872 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5873 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5874 com->lc_new_checked = 0;
5877 rc = lfsck_layout_store(env, com);
5878 up_write(&com->lc_sem);
5880 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
5881 lfsck_lfsck2name(lfsck), rc);
5886 static int lfsck_layout_slave_post(const struct lu_env *env,
5887 struct lfsck_component *com,
5888 int result, bool init)
5890 struct lfsck_instance *lfsck = com->lc_lfsck;
5891 struct lfsck_layout *lo = com->lc_file_ram;
5895 down_write(&com->lc_sem);
5896 rc = lfsck_layout_lastid_store(env, com);
5900 LASSERT(lfsck->li_out_notify != NULL);
5902 spin_lock(&lfsck->li_lock);
5904 lo->ll_pos_last_checkpoint =
5905 lfsck->li_pos_checkpoint.lp_oit_cookie;
5908 lo->ll_status = LS_SCANNING_PHASE2;
5909 lo->ll_flags |= LF_SCANNED_ONCE;
5910 if (lo->ll_flags & LF_CRASHED_LASTID) {
5912 lo->ll_flags &= ~LF_CRASHED_LASTID;
5914 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
5915 "crashed LAST_ID files successfully\n",
5916 lfsck_lfsck2name(lfsck));
5918 lo->ll_flags &= ~LF_UPGRADE;
5919 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
5920 } else if (result == 0) {
5921 if (lfsck->li_status != 0)
5922 lo->ll_status = lfsck->li_status;
5924 lo->ll_status = LS_STOPPED;
5925 if (lo->ll_status != LS_PAUSED)
5926 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5928 lo->ll_status = LS_FAILED;
5929 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
5931 spin_unlock(&lfsck->li_lock);
5934 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5938 lo->ll_run_time_phase1 += ktime_get_seconds() -
5939 lfsck->li_time_last_checkpoint;
5940 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5941 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5942 com->lc_new_checked = 0;
5945 rc = lfsck_layout_store(env, com);
5946 up_write(&com->lc_sem);
5948 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
5950 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
5951 lfsck_lfsck2name(lfsck), rc);
5956 static void lfsck_layout_dump(const struct lu_env *env,
5957 struct lfsck_component *com, struct seq_file *m)
5959 struct lfsck_instance *lfsck = com->lc_lfsck;
5960 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5961 struct lfsck_layout *lo = com->lc_file_ram;
5964 down_read(&com->lc_sem);
5965 if (bk->lb_param & LPF_DRYRUN)
5966 prefix = "inconsistent";
5968 prefix = "repaired";
5970 seq_printf(m, "name: lfsck_layout\n"
5976 lfsck_status2name(lo->ll_status));
5978 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
5980 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
5982 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
5984 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
5986 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
5988 seq_printf(m, "latest_start_position: %llu\n"
5989 "last_checkpoint_position: %llu\n"
5990 "first_failure_position: %llu\n",
5991 lo->ll_pos_latest_start,
5992 lo->ll_pos_last_checkpoint,
5993 lo->ll_pos_first_inconsistent);
5995 seq_printf(m, "success_count: %u\n"
5996 "%s_dangling: %llu\n"
5997 "%s_unmatched_pair: %llu\n"
5998 "%s_multiple_referenced: %llu\n"
6000 "%s_inconsistent_owner: %llu\n"
6003 "failed_phase1: %llu\n"
6004 "failed_phase2: %llu\n",
6005 lo->ll_success_count,
6006 prefix, lo->ll_objs_repaired[LLIT_DANGLING - 1],
6007 prefix, lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
6008 prefix, lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
6009 prefix, lo->ll_objs_repaired[LLIT_ORPHAN - 1],
6010 prefix, lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
6011 prefix, lo->ll_objs_repaired[LLIT_OTHERS - 1],
6012 lo->ll_objs_skipped,
6013 lo->ll_objs_failed_phase1,
6014 lo->ll_objs_failed_phase2);
6016 if (lo->ll_status == LS_SCANNING_PHASE1) {
6017 time64_t duration = ktime_get_seconds() -
6018 lfsck->li_time_last_checkpoint;
6019 u64 checked = lo->ll_objs_checked_phase1 +
6020 com->lc_new_checked;
6021 u64 speed = checked;
6022 u64 new_checked = com->lc_new_checked;
6023 time64_t rtime = lo->ll_run_time_phase1 + duration;
6027 new_checked = div64_s64(new_checked, duration);
6029 speed = div64_s64(speed, rtime);
6030 seq_printf(m, "checked_phase1: %llu\n"
6031 "checked_phase2: %llu\n"
6032 "run_time_phase1: %lld seconds\n"
6033 "run_time_phase2: %lld seconds\n"
6034 "average_speed_phase1: %llu items/sec\n"
6035 "average_speed_phase2: N/A\n"
6036 "real-time_speed_phase1: %llu items/sec\n"
6037 "real-time_speed_phase2: N/A\n",
6039 lo->ll_objs_checked_phase2,
6041 lo->ll_run_time_phase2,
6045 if (likely(lfsck->li_di_oit)) {
6046 const struct dt_it_ops *iops =
6047 &lfsck->li_obj_oit->do_index_ops->dio_it;
6049 /* The low layer otable-based iteration position may NOT
6050 * exactly match the layout-based directory traversal
6051 * cookie. Generally, it is not a serious issue. But the
6052 * caller should NOT make assumption on that. */
6053 pos = iops->store(env, lfsck->li_di_oit);
6054 if (!lfsck->li_current_oit_processed)
6057 pos = lo->ll_pos_last_checkpoint;
6060 seq_printf(m, "current_position: %llu\n", pos);
6061 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
6062 time64_t duration = ktime_get_seconds() -
6063 com->lc_time_last_checkpoint;
6064 u64 checked = lo->ll_objs_checked_phase2 +
6065 com->lc_new_checked;
6066 u64 speed1 = lo->ll_objs_checked_phase1;
6067 u64 speed2 = checked;
6068 u64 new_checked = com->lc_new_checked;
6069 time64_t rtime = lo->ll_run_time_phase2 + duration;
6072 new_checked = div64_s64(new_checked, duration);
6073 if (lo->ll_run_time_phase1 != 0)
6074 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6076 speed2 = div64_s64(speed2, rtime);
6077 seq_printf(m, "checked_phase1: %llu\n"
6078 "checked_phase2: %llu\n"
6079 "run_time_phase1: %lld seconds\n"
6080 "run_time_phase2: %lld seconds\n"
6081 "average_speed_phase1: %llu items/sec\n"
6082 "average_speed_phase2: %llu items/sec\n"
6083 "real-time_speed_phase1: N/A\n"
6084 "real-time_speed_phase2: %llu items/sec\n"
6085 "current_position: "DFID"\n",
6086 lo->ll_objs_checked_phase1,
6088 lo->ll_run_time_phase1,
6093 PFID(&com->lc_fid_latest_scanned_phase2));
6095 __u64 speed1 = lo->ll_objs_checked_phase1;
6096 __u64 speed2 = lo->ll_objs_checked_phase2;
6098 if (lo->ll_run_time_phase1 != 0)
6099 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6100 if (lo->ll_run_time_phase2 != 0)
6101 speed2 = div64_s64(speed2, lo->ll_run_time_phase2);
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 objs/sec\n"
6108 "real-time_speed_phase1: N/A\n"
6109 "real-time_speed_phase2: N/A\n"
6110 "current_position: N/A\n",
6111 lo->ll_objs_checked_phase1,
6112 lo->ll_objs_checked_phase2,
6113 lo->ll_run_time_phase1,
6114 lo->ll_run_time_phase2,
6119 up_read(&com->lc_sem);
6122 static int lfsck_layout_master_double_scan(const struct lu_env *env,
6123 struct lfsck_component *com)
6125 struct lfsck_layout *lo = com->lc_file_ram;
6126 struct lfsck_assistant_data *lad = com->lc_data;
6127 struct lfsck_instance *lfsck = com->lc_lfsck;
6128 struct lfsck_tgt_descs *ltds;
6129 struct lfsck_tgt_desc *ltd;
6130 struct lfsck_tgt_desc *next;
6133 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
6135 if (thread_is_stopped(&lad->lad_thread)) {
6136 LASSERT(list_empty(&lad->lad_req_list));
6137 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6138 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6140 ltds = &lfsck->li_ost_descs;
6141 spin_lock(<ds->ltd_lock);
6142 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6143 ltd_layout_phase_list) {
6144 list_del_init(<d->ltd_layout_phase_list);
6146 spin_unlock(<ds->ltd_lock);
6148 ltds = &lfsck->li_mdt_descs;
6149 spin_lock(<ds->ltd_lock);
6150 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6151 ltd_layout_phase_list) {
6152 list_del_init(<d->ltd_layout_phase_list);
6154 spin_unlock(<ds->ltd_lock);
6160 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6161 struct lfsck_component *com)
6163 struct lfsck_instance *lfsck = com->lc_lfsck;
6164 struct lfsck_layout_slave_data *llsd = com->lc_data;
6165 struct lfsck_layout *lo = com->lc_file_ram;
6166 struct ptlrpc_thread *thread = &lfsck->li_thread;
6170 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6171 lfsck_lfsck2name(lfsck));
6173 atomic_inc(&lfsck->li_double_scan_count);
6175 if (lo->ll_flags & LF_INCOMPLETE)
6178 com->lc_new_checked = 0;
6179 com->lc_new_scanned = 0;
6180 com->lc_time_last_checkpoint = ktime_get_seconds();
6181 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6182 LFSCK_CHECKPOINT_INTERVAL;
6185 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
6188 rc = lfsck_layout_slave_query_master(env, com);
6189 if (list_empty(&llsd->llsd_master_list)) {
6190 if (unlikely(!thread_is_running(thread)))
6201 rc = l_wait_event(thread->t_ctl_waitq,
6202 !thread_is_running(thread) ||
6203 lo->ll_flags & LF_INCOMPLETE ||
6204 list_empty(&llsd->llsd_master_list),
6206 if (unlikely(!thread_is_running(thread)))
6209 if (lo->ll_flags & LF_INCOMPLETE)
6212 if (rc == -ETIMEDOUT)
6215 GOTO(done, rc = (rc < 0 ? rc : 1));
6219 rc = lfsck_layout_double_scan_result(env, com, rc);
6220 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6221 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6222 lfsck_layout_slave_quit(env, com);
6223 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6224 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6226 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6227 "status %d: rc = %d\n",
6228 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6233 static void lfsck_layout_master_data_release(const struct lu_env *env,
6234 struct lfsck_component *com)
6236 struct lfsck_assistant_data *lad = com->lc_data;
6237 struct lfsck_instance *lfsck = com->lc_lfsck;
6238 struct lfsck_tgt_descs *ltds;
6239 struct lfsck_tgt_desc *ltd;
6240 struct lfsck_tgt_desc *next;
6242 LASSERT(lad != NULL);
6243 LASSERT(thread_is_init(&lad->lad_thread) ||
6244 thread_is_stopped(&lad->lad_thread));
6245 LASSERT(list_empty(&lad->lad_req_list));
6247 com->lc_data = NULL;
6249 ltds = &lfsck->li_ost_descs;
6250 spin_lock(<ds->ltd_lock);
6251 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6252 ltd_layout_phase_list) {
6253 list_del_init(<d->ltd_layout_phase_list);
6255 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6256 ltd_layout_phase_list) {
6257 list_del_init(<d->ltd_layout_phase_list);
6259 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6261 list_del_init(<d->ltd_layout_list);
6263 spin_unlock(<ds->ltd_lock);
6265 ltds = &lfsck->li_mdt_descs;
6266 spin_lock(<ds->ltd_lock);
6267 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6268 ltd_layout_phase_list) {
6269 list_del_init(<d->ltd_layout_phase_list);
6271 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6272 ltd_layout_phase_list) {
6273 list_del_init(<d->ltd_layout_phase_list);
6275 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6277 list_del_init(<d->ltd_layout_list);
6279 spin_unlock(<ds->ltd_lock);
6281 if (likely(lad->lad_bitmap != NULL))
6282 CFS_FREE_BITMAP(lad->lad_bitmap);
6287 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6288 struct lfsck_component *com)
6290 struct lfsck_layout_slave_data *llsd = com->lc_data;
6292 lfsck_layout_slave_quit(env, com);
6293 com->lc_data = NULL;
6297 static void lfsck_layout_master_quit(const struct lu_env *env,
6298 struct lfsck_component *com)
6300 struct lfsck_assistant_data *lad = com->lc_data;
6301 struct lfsck_instance *lfsck = com->lc_lfsck;
6302 struct lfsck_tgt_descs *ltds;
6303 struct lfsck_tgt_desc *ltd;
6304 struct lfsck_tgt_desc *next;
6306 LASSERT(lad != NULL);
6308 lfsck_quit_generic(env, com);
6310 LASSERT(thread_is_init(&lad->lad_thread) ||
6311 thread_is_stopped(&lad->lad_thread));
6312 LASSERT(list_empty(&lad->lad_req_list));
6314 ltds = &lfsck->li_ost_descs;
6315 spin_lock(<ds->ltd_lock);
6316 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6317 ltd_layout_phase_list) {
6318 list_del_init(<d->ltd_layout_phase_list);
6320 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6321 ltd_layout_phase_list) {
6322 list_del_init(<d->ltd_layout_phase_list);
6324 spin_unlock(<ds->ltd_lock);
6326 ltds = &lfsck->li_mdt_descs;
6327 spin_lock(<ds->ltd_lock);
6328 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6329 ltd_layout_phase_list) {
6330 list_del_init(<d->ltd_layout_phase_list);
6332 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6333 ltd_layout_phase_list) {
6334 list_del_init(<d->ltd_layout_phase_list);
6336 spin_unlock(<ds->ltd_lock);
6339 static void lfsck_layout_slave_quit(const struct lu_env *env,
6340 struct lfsck_component *com)
6342 struct lfsck_layout_slave_data *llsd = com->lc_data;
6343 struct lfsck_layout_seq *lls;
6344 struct lfsck_layout_seq *next;
6345 struct lfsck_layout_slave_target *llst;
6347 LASSERT(llsd != NULL);
6349 down_write(&com->lc_sem);
6350 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6352 list_del_init(&lls->lls_list);
6353 lfsck_object_put(env, lls->lls_lastid_obj);
6356 up_write(&com->lc_sem);
6358 spin_lock(&llsd->llsd_lock);
6359 while (!list_empty(&llsd->llsd_master_list)) {
6360 llst = list_entry(llsd->llsd_master_list.next,
6361 struct lfsck_layout_slave_target, llst_list);
6362 list_del_init(&llst->llst_list);
6363 spin_unlock(&llsd->llsd_lock);
6364 lfsck_layout_llst_put(llst);
6365 spin_lock(&llsd->llsd_lock);
6367 spin_unlock(&llsd->llsd_lock);
6369 lfsck_rbtree_cleanup(env, com);
6372 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6373 struct lfsck_component *com,
6374 struct lfsck_request *lr)
6376 struct lfsck_instance *lfsck = com->lc_lfsck;
6377 struct lfsck_layout *lo = com->lc_file_ram;
6378 struct lfsck_assistant_data *lad = com->lc_data;
6379 struct lfsck_tgt_descs *ltds;
6380 struct lfsck_tgt_desc *ltd;
6384 if (lr->lr_event == LE_PAIRS_VERIFY) {
6387 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6394 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6395 "from %s %x, status %d, flags %x, flags2 %x\n",
6396 lfsck_lfsck2name(lfsck), lr->lr_event,
6397 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6398 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6400 if (lr->lr_event != LE_PHASE1_DONE &&
6401 lr->lr_event != LE_PHASE2_DONE &&
6402 lr->lr_event != LE_PEER_EXIT)
6405 if (lr->lr_flags & LEF_FROM_OST)
6406 ltds = &lfsck->li_ost_descs;
6408 ltds = &lfsck->li_mdt_descs;
6409 spin_lock(<ds->ltd_lock);
6410 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6412 spin_unlock(<ds->ltd_lock);
6417 list_del_init(<d->ltd_layout_phase_list);
6418 switch (lr->lr_event) {
6419 case LE_PHASE1_DONE:
6420 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6421 if (lr->lr_flags2 & LF_INCOMPLETE) {
6422 if (lr->lr_flags & LEF_FROM_OST)
6423 lfsck_lad_set_bitmap(env, com,
6426 lo->ll_flags |= LF_INCOMPLETE;
6428 ltd->ltd_layout_done = 1;
6429 list_del_init(<d->ltd_layout_list);
6434 if (lr->lr_flags & LEF_FROM_OST) {
6435 if (list_empty(<d->ltd_layout_list))
6436 list_add_tail(<d->ltd_layout_list,
6437 &lad->lad_ost_list);
6438 list_add_tail(<d->ltd_layout_phase_list,
6439 &lad->lad_ost_phase2_list);
6441 if (list_empty(<d->ltd_layout_list))
6442 list_add_tail(<d->ltd_layout_list,
6443 &lad->lad_mdt_list);
6444 list_add_tail(<d->ltd_layout_phase_list,
6445 &lad->lad_mdt_phase2_list);
6448 case LE_PHASE2_DONE:
6449 ltd->ltd_layout_done = 1;
6450 if (!list_empty(<d->ltd_layout_list))
6451 list_del_init(<d->ltd_layout_list);
6453 if (lr->lr_flags2 & LF_INCOMPLETE) {
6454 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6461 ltd->ltd_layout_done = 1;
6462 list_del_init(<d->ltd_layout_list);
6463 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6464 !(lr->lr_flags & LEF_FROM_OST))
6465 lo->ll_flags |= LF_INCOMPLETE;
6470 spin_unlock(<ds->ltd_lock);
6472 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6473 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6475 memset(stop, 0, sizeof(*stop));
6476 stop->ls_status = lr->lr_status;
6477 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6478 lfsck_stop(env, lfsck->li_bottom, stop);
6479 } else if (lfsck_phase2_next_ready(lad)) {
6480 wake_up_all(&lad->lad_thread.t_ctl_waitq);
6486 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6487 struct lfsck_component *com,
6488 struct lfsck_req_local *lrl,
6493 switch (lrl->lrl_event) {
6494 case LEL_FID_ACCESSED:
6495 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6497 case LEL_PAIRS_VERIFY_LOCAL: {
6500 lrl->lrl_status = LPVS_INIT;
6501 /* Firstly, if the MDT-object which is claimed via OST-object
6502 * local stored PFID xattr recognizes the OST-object, then it
6503 * must be that the client given PFID is wrong. */
6504 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6505 &lrl->lrl_ff_local.ff_parent,
6506 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6510 lrl->lrl_status = LPVS_INCONSISTENT;
6511 /* The OST-object local stored PFID xattr is stale. We need to
6512 * check whether the MDT-object that is claimed via the client
6513 * given PFID information recognizes the OST-object or not. If
6514 * matches, then need to update the OST-object's PFID xattr. */
6515 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6516 &lrl->lrl_ff_client.ff_parent,
6517 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6519 * We are not sure whether the client given PFID information
6520 * is correct or not, do nothing to avoid improper fixing.
6523 * The client given PFID information is also invalid, we can
6524 * NOT fix the OST-object inconsistency.
6527 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6528 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6537 RETURN(-EOPNOTSUPP);
6540 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6541 struct lfsck_component *com,
6542 struct lfsck_request *lr)
6544 struct lfsck_instance *lfsck = com->lc_lfsck;
6545 struct lfsck_layout_slave_data *llsd = com->lc_data;
6546 struct lfsck_layout_slave_target *llst;
6550 switch (lr->lr_event) {
6551 case LE_CONDITIONAL_DESTROY:
6552 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6554 case LE_PHASE1_DONE: {
6555 if (lr->lr_flags2 & LF_INCOMPLETE) {
6556 struct lfsck_layout *lo = com->lc_file_ram;
6558 lo->ll_flags |= LF_INCOMPLETE;
6559 llst = lfsck_layout_llst_find_and_del(llsd,
6563 lfsck_layout_llst_put(llst);
6564 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6570 case LE_PHASE2_DONE:
6572 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6573 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6574 lr->lr_event, lr->lr_index, lr->lr_status);
6580 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6584 lfsck_layout_llst_put(llst);
6585 if (list_empty(&llsd->llsd_master_list))
6586 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
6588 if (lr->lr_event == LE_PEER_EXIT &&
6589 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6590 (list_empty(&llsd->llsd_master_list) &&
6591 (lr->lr_status == LS_STOPPED ||
6592 lr->lr_status == LS_CO_STOPPED)))) {
6593 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6595 memset(stop, 0, sizeof(*stop));
6596 stop->ls_status = lr->lr_status;
6597 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6598 lfsck_stop(env, lfsck->li_bottom, stop);
6604 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6608 for (i = 0; i < LLIT_MAX; i++)
6609 *count += lo->ll_objs_repaired[i];
6612 static int lfsck_layout_query_all(const struct lu_env *env,
6613 struct lfsck_component *com,
6614 __u32 *mdts_count, __u32 *osts_count,
6617 struct lfsck_layout *lo = com->lc_file_ram;
6618 struct lfsck_tgt_descs *ltds;
6619 struct lfsck_tgt_desc *ltd;
6624 rc = lfsck_query_all(env, com);
6628 ltds = &com->lc_lfsck->li_mdt_descs;
6629 down_read(<ds->ltd_rw_sem);
6630 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6631 ltd = lfsck_ltd2tgt(ltds, idx);
6632 LASSERT(ltd != NULL);
6634 mdts_count[ltd->ltd_layout_status]++;
6635 *repaired += ltd->ltd_layout_repaired;
6637 up_read(<ds->ltd_rw_sem);
6639 ltds = &com->lc_lfsck->li_ost_descs;
6640 down_read(<ds->ltd_rw_sem);
6641 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
6642 ltd = lfsck_ltd2tgt(ltds, idx);
6643 LASSERT(ltd != NULL);
6645 osts_count[ltd->ltd_layout_status]++;
6646 *repaired += ltd->ltd_layout_repaired;
6648 up_read(<ds->ltd_rw_sem);
6650 down_read(&com->lc_sem);
6651 mdts_count[lo->ll_status]++;
6652 lfsck_layout_repaired(lo, repaired);
6653 up_read(&com->lc_sem);
6658 static int lfsck_layout_query(const struct lu_env *env,
6659 struct lfsck_component *com,
6660 struct lfsck_request *req,
6661 struct lfsck_reply *rep,
6662 struct lfsck_query *que, int idx)
6664 struct lfsck_layout *lo = com->lc_file_ram;
6668 LASSERT(com->lc_lfsck->li_master);
6670 rc = lfsck_layout_query_all(env, com,
6671 que->lu_mdts_count[idx],
6672 que->lu_osts_count[idx],
6673 &que->lu_repaired[idx]);
6675 down_read(&com->lc_sem);
6676 rep->lr_status = lo->ll_status;
6677 if (req->lr_flags & LEF_QUERY_ALL)
6678 lfsck_layout_repaired(lo, &rep->lr_repaired);
6679 up_read(&com->lc_sem);
6685 /* with lfsck::li_lock held */
6686 static int lfsck_layout_slave_join(const struct lu_env *env,
6687 struct lfsck_component *com,
6688 struct lfsck_start_param *lsp)
6690 struct lfsck_instance *lfsck = com->lc_lfsck;
6691 struct lfsck_layout_slave_data *llsd = com->lc_data;
6692 struct lfsck_layout_slave_target *llst;
6693 struct lfsck_start *start = lsp->lsp_start;
6697 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6700 if (!lsp->lsp_index_valid)
6703 /* If someone is running the LFSCK without orphan handling,
6704 * it will not maintain the object accessing rbtree. So we
6705 * cannot join it for orphan handling. */
6706 if (!llsd->llsd_rbtree_valid)
6709 spin_unlock(&lfsck->li_lock);
6710 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6711 spin_lock(&lfsck->li_lock);
6712 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6713 spin_unlock(&lfsck->li_lock);
6714 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6717 lfsck_layout_llst_put(llst);
6718 spin_lock(&lfsck->li_lock);
6725 static struct lfsck_operations lfsck_layout_master_ops = {
6726 .lfsck_reset = lfsck_layout_reset,
6727 .lfsck_fail = lfsck_layout_fail,
6728 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6729 .lfsck_prep = lfsck_layout_master_prep,
6730 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6731 .lfsck_exec_dir = lfsck_layout_exec_dir,
6732 .lfsck_post = lfsck_layout_master_post,
6733 .lfsck_dump = lfsck_layout_dump,
6734 .lfsck_double_scan = lfsck_layout_master_double_scan,
6735 .lfsck_data_release = lfsck_layout_master_data_release,
6736 .lfsck_quit = lfsck_layout_master_quit,
6737 .lfsck_in_notify = lfsck_layout_master_in_notify,
6738 .lfsck_query = lfsck_layout_query,
6741 static struct lfsck_operations lfsck_layout_slave_ops = {
6742 .lfsck_reset = lfsck_layout_reset,
6743 .lfsck_fail = lfsck_layout_fail,
6744 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6745 .lfsck_prep = lfsck_layout_slave_prep,
6746 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6747 .lfsck_exec_dir = lfsck_layout_exec_dir,
6748 .lfsck_post = lfsck_layout_slave_post,
6749 .lfsck_dump = lfsck_layout_dump,
6750 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6751 .lfsck_data_release = lfsck_layout_slave_data_release,
6752 .lfsck_quit = lfsck_layout_slave_quit,
6753 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6754 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6755 .lfsck_query = lfsck_layout_query,
6756 .lfsck_join = lfsck_layout_slave_join,
6759 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6760 struct lfsck_component *com,
6761 struct lfsck_position *pos)
6763 struct lfsck_assistant_data *lad = com->lc_data;
6764 struct lfsck_layout_req *llr;
6766 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6770 if (list_empty(&lad->lad_req_list))
6773 llr = list_entry(lad->lad_req_list.next,
6774 struct lfsck_layout_req,
6776 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6779 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6780 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
6781 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
6782 .la_fill_pos = lfsck_layout_assistant_fill_pos,
6783 .la_double_scan_result = lfsck_layout_double_scan_result,
6784 .la_req_fini = lfsck_layout_assistant_req_fini,
6785 .la_sync_failures = lfsck_layout_assistant_sync_failures,
6788 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
6790 struct lfsck_component *com;
6791 struct lfsck_layout *lo;
6792 struct dt_object *root = NULL;
6793 struct dt_object *obj;
6802 INIT_LIST_HEAD(&com->lc_link);
6803 INIT_LIST_HEAD(&com->lc_link_dir);
6804 init_rwsem(&com->lc_sem);
6805 atomic_set(&com->lc_ref, 1);
6806 com->lc_lfsck = lfsck;
6807 com->lc_type = LFSCK_TYPE_LAYOUT;
6808 if (lfsck->li_master) {
6809 com->lc_ops = &lfsck_layout_master_ops;
6810 com->lc_data = lfsck_assistant_data_init(
6811 &lfsck_layout_assistant_ops,
6813 if (com->lc_data == NULL)
6814 GOTO(out, rc = -ENOMEM);
6816 for (i = 0; i < LFSCK_STF_COUNT; i++)
6817 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6819 struct lfsck_layout_slave_data *llsd;
6821 com->lc_ops = &lfsck_layout_slave_ops;
6822 OBD_ALLOC_PTR(llsd);
6824 GOTO(out, rc = -ENOMEM);
6826 INIT_LIST_HEAD(&llsd->llsd_seq_list);
6827 INIT_LIST_HEAD(&llsd->llsd_master_list);
6828 spin_lock_init(&llsd->llsd_lock);
6829 llsd->llsd_rb_root = RB_ROOT;
6830 init_rwsem(&llsd->llsd_rb_rwsem);
6831 com->lc_data = llsd;
6833 com->lc_file_size = sizeof(*lo);
6834 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6835 if (com->lc_file_ram == NULL)
6836 GOTO(out, rc = -ENOMEM);
6838 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6839 if (com->lc_file_disk == NULL)
6840 GOTO(out, rc = -ENOMEM);
6842 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6844 GOTO(out, rc = PTR_ERR(root));
6846 if (unlikely(!dt_try_as_dir(env, root)))
6847 GOTO(out, rc = -ENOTDIR);
6849 obj = local_file_find_or_create(env, lfsck->li_los, root,
6851 S_IFREG | S_IRUGO | S_IWUSR);
6853 GOTO(out, rc = PTR_ERR(obj));
6856 rc = lfsck_layout_load(env, com);
6858 rc = lfsck_layout_reset(env, com, true);
6859 } else if (rc == -ENOENT) {
6860 rc = lfsck_layout_init(env, com);
6861 } else if (lfsck->li_master) {
6862 rc = lfsck_load_sub_trace_files(env, com,
6863 &dt_lfsck_layout_dangling_features,
6864 LFSCK_LAYOUT, false);
6866 rc = lfsck_layout_reset(env, com, true);
6872 lo = com->lc_file_ram;
6873 switch (lo->ll_status) {
6879 spin_lock(&lfsck->li_lock);
6880 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6881 spin_unlock(&lfsck->li_lock);
6884 CERROR("%s: unknown lfsck_layout status %d\n",
6885 lfsck_lfsck2name(lfsck), lo->ll_status);
6887 case LS_SCANNING_PHASE1:
6888 case LS_SCANNING_PHASE2:
6889 /* No need to store the status to disk right now.
6890 * If the system crashed before the status stored,
6891 * it will be loaded back when next time. */
6892 lo->ll_status = LS_CRASHED;
6893 if (!lfsck->li_master)
6894 lo->ll_flags |= LF_INCOMPLETE;
6901 spin_lock(&lfsck->li_lock);
6902 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6903 spin_unlock(&lfsck->li_lock);
6907 if (lo->ll_flags & LF_CRASHED_LASTID) {
6908 LASSERT(lfsck->li_out_notify != NULL);
6910 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6911 LE_LASTID_REBUILDING);
6917 if (root != NULL && !IS_ERR(root))
6918 lfsck_object_put(env, root);
6921 lfsck_component_cleanup(env, com);
6922 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
6923 lfsck_lfsck2name(lfsck), rc);
6929 struct lfsck_orphan_it {
6930 struct lfsck_component *loi_com;
6931 struct lfsck_rbtree_node *loi_lrn;
6932 struct lfsck_layout_slave_target *loi_llst;
6933 struct lu_fid loi_key;
6934 struct lu_orphan_rec_v3 loi_rec;
6936 unsigned int loi_over:1;
6939 static int lfsck_fid_match_idx(const struct lu_env *env,
6940 struct lfsck_instance *lfsck,
6941 const struct lu_fid *fid, int idx)
6943 struct seq_server_site *ss;
6944 struct lu_server_fld *sf;
6945 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
6948 /* All abnormal cases will be returned to MDT0. */
6949 if (!fid_is_norm(fid)) {
6956 ss = lfsck_dev_site(lfsck);
6957 if (unlikely(ss == NULL))
6960 sf = ss->ss_server_fld;
6961 LASSERT(sf != NULL);
6963 fld_range_set_any(range);
6964 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
6968 if (!fld_range_is_mdt(range))
6971 if (range->lsr_index == idx)
6977 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
6978 struct dt_object *obj)
6980 struct dt_device *dev = lfsck_obj2dev(obj);
6981 struct thandle *handle;
6985 handle = dt_trans_create(env, dev);
6989 rc = dt_declare_ref_del(env, obj, handle);
6993 rc = dt_declare_destroy(env, obj, handle);
6997 rc = dt_trans_start_local(env, dev, handle);
7001 dt_write_lock(env, obj, 0);
7002 rc = dt_ref_del(env, obj, handle);
7004 rc = dt_destroy(env, obj, handle);
7005 dt_write_unlock(env, obj);
7010 dt_trans_stop(env, dev, handle);
7012 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
7013 PFID(lfsck_dto2fid(obj)), rc);
7018 static int lfsck_orphan_index_lookup(const struct lu_env *env,
7019 struct dt_object *dt,
7021 const struct dt_key *key)
7026 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
7027 struct dt_object *dt,
7028 const struct dt_rec *rec,
7029 const struct dt_key *key,
7030 struct thandle *handle)
7035 static int lfsck_orphan_index_insert(const struct lu_env *env,
7036 struct dt_object *dt,
7037 const struct dt_rec *rec,
7038 const struct dt_key *key,
7039 struct thandle *handle)
7044 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
7045 struct dt_object *dt,
7046 const struct dt_key *key,
7047 struct thandle *handle)
7052 static int lfsck_orphan_index_delete(const struct lu_env *env,
7053 struct dt_object *dt,
7054 const struct dt_key *key,
7055 struct thandle *handle)
7060 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
7061 struct dt_object *dt,
7064 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
7065 struct lfsck_instance *lfsck;
7066 struct lfsck_component *com = NULL;
7067 struct lfsck_layout_slave_data *llsd;
7068 struct lfsck_orphan_it *it = NULL;
7069 struct lfsck_layout *lo;
7073 lfsck = lfsck_instance_find(dev, true, false);
7074 if (unlikely(lfsck == NULL))
7075 RETURN(ERR_PTR(-ENXIO));
7077 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
7078 if (unlikely(com == NULL))
7079 GOTO(out, rc = -ENOENT);
7081 lo = com->lc_file_ram;
7082 if (lo->ll_flags & LF_INCOMPLETE)
7083 GOTO(out, rc = -ESRCH);
7085 llsd = com->lc_data;
7086 if (!llsd->llsd_rbtree_valid)
7087 GOTO(out, rc = -ESRCH);
7091 GOTO(out, rc = -ENOMEM);
7093 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
7094 if (it->loi_llst == NULL)
7095 GOTO(out, rc = -ENXIO);
7097 if (dev->dd_record_fid_accessed) {
7098 /* The first iteration against the rbtree, scan the whole rbtree
7099 * to remove the nodes which do NOT need to be handled. */
7100 down_write(&llsd->llsd_rb_rwsem);
7101 if (dev->dd_record_fid_accessed) {
7102 struct rb_node *node;
7103 struct rb_node *next;
7104 struct lfsck_rbtree_node *lrn;
7106 /* No need to record the fid accessing anymore. */
7107 dev->dd_record_fid_accessed = 0;
7109 node = rb_first(&llsd->llsd_rb_root);
7110 while (node != NULL) {
7111 next = rb_next(node);
7112 lrn = rb_entry(node, struct lfsck_rbtree_node,
7114 if (atomic_read(&lrn->lrn_known_count) <=
7115 atomic_read(&lrn->lrn_accessed_count)) {
7116 rb_erase(node, &llsd->llsd_rb_root);
7117 lfsck_rbtree_free(lrn);
7122 up_write(&llsd->llsd_rb_rwsem);
7125 /* read lock the rbtree when init, and unlock when fini */
7126 down_read(&llsd->llsd_rb_rwsem);
7134 lfsck_component_put(env, com);
7136 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
7137 lfsck_lfsck2name(lfsck), rc);
7139 lfsck_instance_put(env, lfsck);
7144 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7147 return (struct dt_it *)it;
7150 static void lfsck_orphan_it_fini(const struct lu_env *env,
7153 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7154 struct lfsck_component *com = it->loi_com;
7155 struct lfsck_layout_slave_data *llsd;
7156 struct lfsck_layout_slave_target *llst;
7159 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7160 lfsck_lfsck2name(com->lc_lfsck));
7162 llsd = com->lc_data;
7163 up_read(&llsd->llsd_rb_rwsem);
7164 llst = it->loi_llst;
7165 LASSERT(llst != NULL);
7167 /* Save the key and hash for iterate next. */
7168 llst->llst_fid = it->loi_key;
7169 llst->llst_hash = it->loi_hash;
7170 lfsck_layout_llst_put(llst);
7171 lfsck_component_put(env, com);
7177 * \retval +1: the iteration finished
7178 * \retval 0: on success, not finished
7179 * \retval -ve: on error
7181 static int lfsck_orphan_it_next(const struct lu_env *env,
7184 struct lfsck_thread_info *info = lfsck_env_info(env);
7185 struct filter_fid *ff = &info->lti_ff;
7186 struct lu_attr *la = &info->lti_la;
7187 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7188 struct lu_fid *key = &it->loi_key;
7189 struct lu_orphan_rec_v3 *rec = &it->loi_rec;
7190 struct ost_layout *ol = &rec->lor_layout;
7191 struct lfsck_component *com = it->loi_com;
7192 struct lfsck_instance *lfsck = com->lc_lfsck;
7193 struct lfsck_layout_slave_data *llsd = com->lc_data;
7194 struct dt_object *obj;
7195 struct lfsck_rbtree_node *lrn;
7199 __u32 idx = it->loi_llst->llst_index;
7209 lrn = lfsck_rbtree_search(llsd, key, &exact);
7217 key->f_seq = lrn->lrn_seq;
7218 key->f_oid = lrn->lrn_first_oid;
7223 if (unlikely(key->f_oid == 0)) {
7230 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7236 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7237 atomic_read(&lrn->lrn_accessed_count))) {
7238 struct rb_node *next = rb_next(&lrn->lrn_node);
7240 while (next != NULL) {
7241 lrn = rb_entry(next, struct lfsck_rbtree_node,
7243 if (atomic_read(&lrn->lrn_known_count) >
7244 atomic_read(&lrn->lrn_accessed_count))
7246 next = rb_next(next);
7255 key->f_seq = lrn->lrn_seq;
7256 key->f_oid = lrn->lrn_first_oid;
7260 pos = key->f_oid - lrn->lrn_first_oid;
7263 pos = find_next_bit(lrn->lrn_known_bitmap,
7264 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7265 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7266 key->f_oid = lrn->lrn_first_oid + pos;
7267 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7275 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7280 key->f_oid = lrn->lrn_first_oid + pos;
7281 obj = lfsck_object_find_bottom(env, lfsck, key);
7284 if (rc == -ENOENT) {
7291 dt_read_lock(env, obj, 0);
7292 if (dt_object_exists(obj) == 0 ||
7293 lfsck_is_dead_obj(obj)) {
7294 dt_read_unlock(env, obj);
7295 lfsck_object_put(env, obj);
7300 rc = dt_attr_get(env, obj, la);
7304 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7306 if (rc == -ENODATA) {
7307 /* For the pre-created OST-object, update the bitmap to avoid
7308 * others LFSCK (second phase) iteration to touch it again. */
7309 if (la->la_ctime == 0) {
7310 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7311 atomic_inc(&lrn->lrn_accessed_count);
7313 /* For the race between repairing dangling referenced
7314 * MDT-object and unlink the file, it may left orphan
7315 * OST-object there. Destroy it now! */
7316 if (unlikely(!(la->la_mode & S_ISUID))) {
7317 dt_read_unlock(env, obj);
7318 lfsck_layout_destroy_orphan(env, obj);
7319 lfsck_object_put(env, obj);
7323 } else if (idx == 0) {
7324 /* If the orphan OST-object has no parent information,
7325 * regard it as referenced by the MDT-object on MDT0. */
7326 fid_zero(&rec->lor_rec.lor_fid);
7327 rec->lor_rec.lor_uid = la->la_uid;
7328 rec->lor_rec.lor_gid = la->la_gid;
7329 memset(ol, 0, sizeof(*ol));
7330 rec->lor_layout_version = 0;
7336 dt_read_unlock(env, obj);
7337 lfsck_object_put(env, obj);
7342 if (rc < sizeof(struct lu_fid))
7343 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7345 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7346 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7347 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7348 * parent MDT-object's layout EA. */
7349 save = rec->lor_rec.lor_fid.f_stripe_idx;
7350 rec->lor_rec.lor_fid.f_ver = 0;
7351 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7352 /* If the orphan OST-object does not claim the MDT, then next.
7354 * If we do not know whether it matches or not, then return it
7355 * to the MDT for further check. */
7357 dt_read_unlock(env, obj);
7358 lfsck_object_put(env, obj);
7363 rec->lor_rec.lor_fid.f_stripe_idx = save;
7364 rec->lor_rec.lor_uid = la->la_uid;
7365 rec->lor_rec.lor_gid = la->la_gid;
7366 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7367 rec->lor_layout_version =
7368 le32_to_cpu(ff->ff_layout_version & ~LU_LAYOUT_RESYNC);
7369 rec->lor_range = le32_to_cpu(ff->ff_range);
7371 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7372 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7373 "COMP end %llu, layout version %u, range %u\n",
7374 lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7375 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7376 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7377 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
7378 rec->lor_layout_version, rec->lor_range);
7383 dt_read_unlock(env, obj);
7384 lfsck_object_put(env, obj);
7392 * \retval +1: locate to the exactly position
7393 * \retval 0: cannot locate to the exactly position,
7394 * call next() to move to a valid position.
7395 * \retval -ve: on error
7397 static int lfsck_orphan_it_get(const struct lu_env *env,
7399 const struct dt_key *key)
7401 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7404 it->loi_key = *(struct lu_fid *)key;
7405 rc = lfsck_orphan_it_next(env, di);
7415 static void lfsck_orphan_it_put(const struct lu_env *env,
7420 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7421 const struct dt_it *di)
7423 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7425 return (struct dt_key *)&it->loi_key;
7428 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7429 const struct dt_it *di)
7431 return sizeof(struct lu_fid);
7434 static int lfsck_orphan_it_rec(const struct lu_env *env,
7435 const struct dt_it *di,
7439 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7441 *(struct lu_orphan_rec_v3 *)rec = it->loi_rec;
7446 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7447 const struct dt_it *di)
7449 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7451 return it->loi_hash;
7455 * \retval +1: locate to the exactly position
7456 * \retval 0: cannot locate to the exactly position,
7457 * call next() to move to a valid position.
7458 * \retval -ve: on error
7460 static int lfsck_orphan_it_load(const struct lu_env *env,
7461 const struct dt_it *di,
7464 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7465 struct lfsck_layout_slave_target *llst = it->loi_llst;
7468 LASSERT(llst != NULL);
7470 if (hash != llst->llst_hash) {
7471 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7472 "iteration does not match the one when fini "
7473 "%llu, to be reset.\n",
7474 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7476 fid_zero(&llst->llst_fid);
7477 llst->llst_hash = 0;
7480 it->loi_key = llst->llst_fid;
7481 it->loi_hash = llst->llst_hash;
7482 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7492 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7493 const struct dt_it *di,
7499 const struct dt_index_operations lfsck_orphan_index_ops = {
7500 .dio_lookup = lfsck_orphan_index_lookup,
7501 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7502 .dio_insert = lfsck_orphan_index_insert,
7503 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7504 .dio_delete = lfsck_orphan_index_delete,
7506 .init = lfsck_orphan_it_init,
7507 .fini = lfsck_orphan_it_fini,
7508 .get = lfsck_orphan_it_get,
7509 .put = lfsck_orphan_it_put,
7510 .next = lfsck_orphan_it_next,
7511 .key = lfsck_orphan_it_key,
7512 .key_size = lfsck_orphan_it_key_size,
7513 .rec = lfsck_orphan_it_rec,
7514 .store = lfsck_orphan_it_store,
7515 .load = lfsck_orphan_it_load,
7516 .key_rec = lfsck_orphan_it_key_rec,