4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, 2016, Intel Corporation.
26 * lustre/lfsck/lfsck_layout.c
28 * Author: Fan, Yong <fan.yong@intel.com>
32 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_LFSCK
36 #include <linux/bitops.h>
37 #include <linux/rbtree.h>
39 #include <lustre/lustre_idl.h>
40 #include <lu_object.h>
41 #include <dt_object.h>
42 #include <lustre_fid.h>
43 #include <lustre_lib.h>
44 #include <lustre_net.h>
45 #include <lustre/lustre_user.h>
46 #include <md_object.h>
47 #include <obd_class.h>
49 #include "lfsck_internal.h"
51 #define LFSCK_LAYOUT_MAGIC_V1 0xB173AE14
52 #define LFSCK_LAYOUT_MAGIC_V2 0xB1734D76
54 #define LFSCK_LAYOUT_MAGIC LFSCK_LAYOUT_MAGIC_V2
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 rwlock_t llsd_rb_lock;
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;
100 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
102 if (atomic_dec_and_test(&llst->llst_ref)) {
103 LASSERT(list_empty(&llst->llst_list));
110 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
112 struct lfsck_layout_slave_target *llst;
113 struct lfsck_layout_slave_target *tmp;
120 INIT_LIST_HEAD(&llst->llst_list);
122 llst->llst_index = index;
123 atomic_set(&llst->llst_ref, 1);
125 spin_lock(&llsd->llsd_lock);
126 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
127 if (tmp->llst_index == index) {
133 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
134 spin_unlock(&llsd->llsd_lock);
143 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
144 struct lfsck_layout_slave_target *llst)
148 spin_lock(&llsd->llsd_lock);
149 if (!list_empty(&llst->llst_list)) {
150 list_del_init(&llst->llst_list);
153 spin_unlock(&llsd->llsd_lock);
156 lfsck_layout_llst_put(llst);
159 static inline struct lfsck_layout_slave_target *
160 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
161 __u32 index, bool unlink)
163 struct lfsck_layout_slave_target *llst;
165 spin_lock(&llsd->llsd_lock);
166 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
167 if (llst->llst_index == index) {
169 list_del_init(&llst->llst_list);
171 atomic_inc(&llst->llst_ref);
172 spin_unlock(&llsd->llsd_lock);
177 spin_unlock(&llsd->llsd_lock);
182 static struct lfsck_layout_req *
183 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
184 struct dt_object *child, __u32 ost_idx,
187 struct lfsck_layout_req *llr;
191 return ERR_PTR(-ENOMEM);
193 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
194 llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
195 llr->llr_child = child;
196 llr->llr_ost_idx = ost_idx;
197 llr->llr_lov_idx = lov_idx;
202 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
203 struct lfsck_assistant_req *lar)
205 struct lfsck_layout_req *llr =
206 container_of0(lar, struct lfsck_layout_req, llr_lar);
208 lfsck_object_put(env, llr->llr_child);
209 lfsck_assistant_object_put(env, lar->lar_parent);
214 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
215 struct ptlrpc_request *req,
219 struct lfsck_async_interpret_args *laia = args;
220 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
222 ltd->ltd_synced_failures = 1;
223 atomic_dec(laia->laia_count);
230 * Notify remote LFSCK instances about former failures.
232 * The local LFSCK instance has recorded which OSTs have ever failed to respond
233 * some LFSCK verification requests (maybe because of network issues or the OST
234 * itself trouble). During the respond gap, the OST may missed some OST-objects
235 * verification, then the OST cannot know whether related OST-objects have been
236 * referenced by related MDT-objects or not, then in the second-stage scanning,
237 * these OST-objects will be regarded as orphan, if the OST-object contains bad
238 * parent FID for back reference, then it will misguide the LFSCK to make wrong
239 * fixing for the fake orphan.
241 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
242 * it will scan the bitmap for the ever failed OSTs, and notify them that they
243 * have ever missed some OST-object verification and should skip the handling
244 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
246 * \param[in] env pointer to the thread context
247 * \param[in] com pointer to the lfsck component
248 * \param[in] lr pointer to the lfsck request
250 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
251 struct lfsck_component *com,
252 struct lfsck_request *lr)
254 struct lfsck_async_interpret_args *laia =
255 &lfsck_env_info(env)->lti_laia2;
256 struct lfsck_assistant_data *lad = com->lc_data;
257 struct lfsck_layout *lo = com->lc_file_ram;
258 struct lfsck_instance *lfsck = com->lc_lfsck;
259 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
260 struct lfsck_tgt_desc *ltd;
261 struct ptlrpc_request_set *set;
267 if (!lad->lad_incomplete || lo->ll_flags & LF_INCOMPLETE)
270 /* If the MDT has ever failed to verfiy some OST-objects,
271 * then sync failures with them firstly. */
272 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
274 atomic_set(&count, 0);
275 memset(laia, 0, sizeof(*laia));
276 laia->laia_count = &count;
277 set = ptlrpc_prep_set();
279 GOTO(out, rc = -ENOMEM);
281 down_read(<ds->ltd_rw_sem);
282 cfs_foreach_bit(lad->lad_bitmap, idx) {
283 ltd = lfsck_ltd2tgt(ltds, idx);
284 LASSERT(ltd != NULL);
286 laia->laia_ltd = ltd;
287 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
288 lfsck_layout_assistant_sync_failures_interpret,
291 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
292 "notify target %x for %s phase1 done: "
293 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
294 ltd->ltd_index, lad->lad_name, rc);
301 up_read(<ds->ltd_rw_sem);
303 if (rc == 0 && atomic_read(&count) > 0)
304 rc = ptlrpc_set_wait(set);
306 ptlrpc_set_destroy(set);
308 if (rc == 0 && atomic_read(&count) > 0)
315 /* If failed to sync failures with the OSTs, then have to
316 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
317 * subsequent orphan OST-object handling. */
318 lo->ll_flags |= LF_INCOMPLETE;
320 lr->lr_flags2 = lo->ll_flags;
323 static int lfsck_layout_get_lovea(const struct lu_env *env,
324 struct dt_object *obj, struct lu_buf *buf)
329 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
331 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
335 lu_buf_realloc(buf, rc);
336 if (buf->lb_buf == NULL)
348 if (unlikely(buf->lb_buf == NULL)) {
349 lu_buf_alloc(buf, rc);
350 if (buf->lb_buf == NULL)
359 static int lfsck_layout_verify_header(struct lov_mds_md_v1 *lmm)
364 magic = le32_to_cpu(lmm->lmm_magic);
365 /* If magic crashed, keep it there. Sometime later, during OST-object
366 * orphan handling, if some OST-object(s) back-point to it, it can be
367 * verified and repaired. */
368 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
372 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
373 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
378 CDEBUG(D_LFSCK, "%s LOV EA magic %u on "DOSTID"\n",
379 rc == -EINVAL ? "Unknown" : "Unsupported",
385 pattern = le32_to_cpu(lmm->lmm_pattern);
386 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
387 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
390 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
391 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u on "DOSTID"\n",
392 pattern, POSTID(&oi));
400 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
401 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
402 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
404 struct lfsck_rbtree_node {
405 struct rb_node lrn_node;
408 atomic_t lrn_known_count;
409 atomic_t lrn_accessed_count;
410 void *lrn_known_bitmap;
411 void *lrn_accessed_bitmap;
414 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
415 __u64 seq, __u32 oid)
417 if (seq < lrn->lrn_seq)
420 if (seq > lrn->lrn_seq)
423 if (oid < lrn->lrn_first_oid)
426 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
432 /* The caller should hold llsd->llsd_rb_lock. */
433 static struct lfsck_rbtree_node *
434 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
435 const struct lu_fid *fid, bool *exact)
437 struct rb_node *node = llsd->llsd_rb_root.rb_node;
438 struct rb_node *prev = NULL;
439 struct lfsck_rbtree_node *lrn = NULL;
445 while (node != NULL) {
447 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
448 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
450 node = node->rb_left;
452 node = node->rb_right;
460 /* If there is no exactly matched one, then to the next valid one. */
463 /* The rbtree is empty. */
470 node = rb_next(prev);
472 /* The end of the rbtree. */
476 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
481 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
482 const struct lu_fid *fid)
484 struct lfsck_rbtree_node *lrn;
488 return ERR_PTR(-ENOMEM);
490 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
491 if (lrn->lrn_known_bitmap == NULL) {
494 return ERR_PTR(-ENOMEM);
497 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
498 if (lrn->lrn_accessed_bitmap == NULL) {
499 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
502 return ERR_PTR(-ENOMEM);
505 RB_CLEAR_NODE(&lrn->lrn_node);
506 lrn->lrn_seq = fid_seq(fid);
507 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
508 atomic_set(&lrn->lrn_known_count, 0);
509 atomic_set(&lrn->lrn_accessed_count, 0);
514 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
516 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
517 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
521 /* The caller should hold lock. */
522 static struct lfsck_rbtree_node *
523 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
524 struct lfsck_rbtree_node *lrn)
526 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
527 struct rb_node *parent = NULL;
528 struct lfsck_rbtree_node *tmp;
531 while (*pos != NULL) {
533 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
534 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
536 pos = &(*pos)->rb_left;
538 pos = &(*pos)->rb_right;
543 rb_link_node(&lrn->lrn_node, parent, pos);
544 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
549 extern const struct dt_index_operations lfsck_orphan_index_ops;
551 static int lfsck_rbtree_setup(const struct lu_env *env,
552 struct lfsck_component *com)
554 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
555 struct lfsck_instance *lfsck = com->lc_lfsck;
556 struct dt_device *dev = lfsck->li_bottom;
557 struct lfsck_layout_slave_data *llsd = com->lc_data;
558 struct dt_object *obj;
560 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
561 fid->f_oid = lfsck_dev_idx(lfsck);
563 obj = dt_locate(env, dev, fid);
565 RETURN(PTR_ERR(obj));
567 /* Generate an in-RAM object to stand for the layout rbtree.
568 * Scanning the layout rbtree will be via the iteration over
569 * the object. In the future, the rbtree may be written onto
570 * disk with the object.
572 * Mark the object to be as exist. */
573 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
574 obj->do_index_ops = &lfsck_orphan_index_ops;
575 llsd->llsd_rb_obj = obj;
576 llsd->llsd_rbtree_valid = 1;
577 dev->dd_record_fid_accessed = 1;
579 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
580 lfsck_lfsck2name(lfsck));
585 static void lfsck_rbtree_cleanup(const struct lu_env *env,
586 struct lfsck_component *com)
588 struct lfsck_instance *lfsck = com->lc_lfsck;
589 struct lfsck_layout_slave_data *llsd = com->lc_data;
590 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
591 struct rb_node *next;
592 struct lfsck_rbtree_node *lrn;
594 lfsck->li_bottom->dd_record_fid_accessed = 0;
595 /* Invalid the rbtree, then no others will use it. */
596 write_lock(&llsd->llsd_rb_lock);
597 llsd->llsd_rbtree_valid = 0;
598 write_unlock(&llsd->llsd_rb_lock);
600 while (node != NULL) {
601 next = rb_next(node);
602 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
603 rb_erase(node, &llsd->llsd_rb_root);
604 lfsck_rbtree_free(lrn);
608 if (llsd->llsd_rb_obj != NULL) {
609 lfsck_object_put(env, llsd->llsd_rb_obj);
610 llsd->llsd_rb_obj = NULL;
613 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
614 lfsck_lfsck2name(lfsck));
617 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
618 struct lfsck_component *com,
619 const struct lu_fid *fid,
622 struct lfsck_layout_slave_data *llsd = com->lc_data;
623 struct lfsck_rbtree_node *lrn;
629 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
632 if (!fid_is_idif(fid) && !fid_is_norm(fid))
635 read_lock(&llsd->llsd_rb_lock);
636 if (!llsd->llsd_rbtree_valid)
637 GOTO(unlock, rc = 0);
639 lrn = lfsck_rbtree_search(llsd, fid, NULL);
641 struct lfsck_rbtree_node *tmp;
645 read_unlock(&llsd->llsd_rb_lock);
646 tmp = lfsck_rbtree_new(env, fid);
648 GOTO(out, rc = PTR_ERR(tmp));
651 write_lock(&llsd->llsd_rb_lock);
652 if (!llsd->llsd_rbtree_valid) {
653 lfsck_rbtree_free(tmp);
654 GOTO(unlock, rc = 0);
657 lrn = lfsck_rbtree_insert(llsd, tmp);
659 lfsck_rbtree_free(tmp);
662 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
663 /* Any accessed object must be a known object. */
664 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
665 atomic_inc(&lrn->lrn_known_count);
666 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
667 atomic_inc(&lrn->lrn_accessed_count);
669 GOTO(unlock, rc = 0);
673 write_unlock(&llsd->llsd_rb_lock);
675 read_unlock(&llsd->llsd_rb_lock);
677 if (rc != 0 && accessed) {
678 struct lfsck_layout *lo = com->lc_file_ram;
680 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
681 "bitmap, and will cause incorrect LFSCK OST-object "
682 "handling, so disable it to cancel orphan handling "
683 "for related device. rc = %d\n",
684 lfsck_lfsck2name(com->lc_lfsck), rc);
686 lo->ll_flags |= LF_INCOMPLETE;
687 lfsck_rbtree_cleanup(env, com);
691 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
692 const struct lfsck_layout *src)
696 des->ll_magic = le32_to_cpu(src->ll_magic);
697 des->ll_status = le32_to_cpu(src->ll_status);
698 des->ll_flags = le32_to_cpu(src->ll_flags);
699 des->ll_success_count = le32_to_cpu(src->ll_success_count);
700 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
701 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
702 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
703 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
704 des->ll_time_last_checkpoint =
705 le64_to_cpu(src->ll_time_last_checkpoint);
706 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
707 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
708 des->ll_pos_first_inconsistent =
709 le64_to_cpu(src->ll_pos_first_inconsistent);
710 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
711 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
712 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
713 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
714 for (i = 0; i < LLIT_MAX; i++)
715 des->ll_objs_repaired[i] =
716 le64_to_cpu(src->ll_objs_repaired[i]);
717 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
718 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
719 fid_le_to_cpu(&des->ll_fid_latest_scanned_phase2,
720 &src->ll_fid_latest_scanned_phase2);
723 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
724 const struct lfsck_layout *src)
728 des->ll_magic = cpu_to_le32(src->ll_magic);
729 des->ll_status = cpu_to_le32(src->ll_status);
730 des->ll_flags = cpu_to_le32(src->ll_flags);
731 des->ll_success_count = cpu_to_le32(src->ll_success_count);
732 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
733 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
734 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
735 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
736 des->ll_time_last_checkpoint =
737 cpu_to_le64(src->ll_time_last_checkpoint);
738 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
739 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
740 des->ll_pos_first_inconsistent =
741 cpu_to_le64(src->ll_pos_first_inconsistent);
742 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
743 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
744 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
745 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
746 for (i = 0; i < LLIT_MAX; i++)
747 des->ll_objs_repaired[i] =
748 cpu_to_le64(src->ll_objs_repaired[i]);
749 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
750 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
751 fid_cpu_to_le(&des->ll_fid_latest_scanned_phase2,
752 &src->ll_fid_latest_scanned_phase2);
756 * Load the OST bitmap from the lfsck_layout trace file.
758 * \param[in] env pointer to the thread context
759 * \param[in] com pointer to the lfsck component
761 * \retval 0 for success
762 * \retval negative error number on failure or data corruption
764 static int lfsck_layout_load_bitmap(const struct lu_env *env,
765 struct lfsck_component *com)
767 struct dt_object *obj = com->lc_obj;
768 struct lfsck_assistant_data *lad = com->lc_data;
769 struct lfsck_layout *lo = com->lc_file_ram;
770 struct cfs_bitmap *bitmap = lad->lad_bitmap;
771 loff_t pos = com->lc_file_size;
777 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
779 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
781 nbits = lo->ll_bitmap_size;
783 if (unlikely(nbits < BITS_PER_LONG))
784 nbits = BITS_PER_LONG;
786 if (nbits > bitmap->size) {
787 __u32 new_bits = bitmap->size;
788 struct cfs_bitmap *new_bitmap;
790 while (new_bits < nbits)
793 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
794 if (new_bitmap == NULL)
797 lad->lad_bitmap = new_bitmap;
798 CFS_FREE_BITMAP(bitmap);
802 if (lo->ll_bitmap_size == 0) {
803 lad->lad_incomplete = 0;
804 CFS_RESET_BITMAP(bitmap);
809 size = (lo->ll_bitmap_size + 7) >> 3;
810 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
812 RETURN(rc >= 0 ? -EINVAL : rc);
814 if (cfs_bitmap_check_empty(bitmap))
815 lad->lad_incomplete = 0;
817 lad->lad_incomplete = 1;
823 * Load the layout LFSCK trace file from disk.
825 * The layout LFSCK trace file records the layout LFSCK status information
826 * and other statistics, such as how many objects have been scanned, and how
827 * many objects have been repaired, and etc. It also contains the bitmap for
828 * failed OSTs during the layout LFSCK. All these information will be loaded
829 * from disk to RAM when the layout LFSCK component setup.
831 * \param[in] env pointer to the thread context
832 * \param[in] com pointer to the lfsck component
834 * \retval positive number for file data corruption, the caller
835 * should reset the layout LFSCK trace file
836 * \retval 0 for success
837 * \retval negative error number on failure
839 static int lfsck_layout_load(const struct lu_env *env,
840 struct lfsck_component *com)
842 struct lfsck_layout *lo = com->lc_file_ram;
843 ssize_t size = com->lc_file_size;
847 rc = dt_read(env, com->lc_obj,
848 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
852 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
853 lfsck_lfsck2name(com->lc_lfsck), rc);
855 } else if (rc != size) {
856 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
857 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
861 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
862 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
863 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
864 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
865 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
873 * Store the layout LFSCK trace file on disk.
875 * The layout LFSCK trace file records the layout LFSCK status information
876 * and other statistics, such as how many objects have been scanned, and how
877 * many objects have been repaired, and etc. It also contains the bitmap for
878 * failed OSTs during the layout LFSCK. All these information will be synced
879 * from RAM to disk periodically.
881 * \param[in] env pointer to the thread context
882 * \param[in] com pointer to the lfsck component
884 * \retval 0 for success
885 * \retval negative error number on failure
887 static int lfsck_layout_store(const struct lu_env *env,
888 struct lfsck_component *com)
890 struct dt_object *obj = com->lc_obj;
891 struct lfsck_instance *lfsck = com->lc_lfsck;
892 struct lfsck_layout *lo_ram = com->lc_file_ram;
893 struct lfsck_layout *lo = com->lc_file_disk;
895 struct dt_device *dev = lfsck_obj2dev(obj);
896 struct cfs_bitmap *bitmap = NULL;
898 ssize_t size = com->lc_file_size;
903 if (lfsck->li_master) {
904 struct lfsck_assistant_data *lad = com->lc_data;
906 bitmap = lad->lad_bitmap;
907 nbits = bitmap->size;
910 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
913 lo_ram->ll_bitmap_size = nbits;
914 lfsck_layout_cpu_to_le(lo, lo_ram);
915 th = dt_trans_create(env, dev);
917 GOTO(log, rc = PTR_ERR(th));
919 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
924 if (bitmap != NULL) {
925 rc = dt_declare_record_write(env, obj,
926 lfsck_buf_get(env, bitmap->data, nbits >> 3),
932 rc = dt_trans_start_local(env, dev, th);
937 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
941 if (bitmap != NULL) {
943 rc = dt_record_write(env, obj,
944 lfsck_buf_get(env, bitmap->data, nbits >> 3),
951 dt_trans_stop(env, dev, th);
955 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
956 lfsck_lfsck2name(lfsck), rc);
961 static int lfsck_layout_init(const struct lu_env *env,
962 struct lfsck_component *com)
964 struct lfsck_layout *lo = com->lc_file_ram;
967 memset(lo, 0, com->lc_file_size);
968 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
969 lo->ll_status = LS_INIT;
970 down_write(&com->lc_sem);
971 rc = lfsck_layout_store(env, com);
972 if (rc == 0 && com->lc_lfsck->li_master)
973 rc = lfsck_load_sub_trace_files(env, com,
974 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
975 up_write(&com->lc_sem);
980 static int fid_is_for_ostobj(const struct lu_env *env,
981 struct lfsck_instance *lfsck,
982 struct dt_object *obj, const struct lu_fid *fid)
984 struct seq_server_site *ss = lfsck_dev_site(lfsck);
985 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
986 struct lustre_mdt_attrs *lma;
989 fld_range_set_any(range);
990 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
992 if (fld_range_is_ost(range))
998 lma = &lfsck_env_info(env)->lti_lma;
999 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
1001 if (rc == sizeof(*lma)) {
1002 lustre_lma_swab(lma);
1004 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1007 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1012 static struct lfsck_layout_seq *
1013 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1015 struct lfsck_layout_seq *lls;
1017 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1018 if (lls->lls_seq == seq)
1021 if (lls->lls_seq > seq)
1029 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1030 struct lfsck_layout_seq *lls)
1032 struct lfsck_layout_seq *tmp;
1033 struct list_head *pos = &llsd->llsd_seq_list;
1035 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1036 if (lls->lls_seq < tmp->lls_seq) {
1037 pos = &tmp->lls_list;
1041 list_add_tail(&lls->lls_list, pos);
1045 lfsck_layout_lastid_create(const struct lu_env *env,
1046 struct lfsck_instance *lfsck,
1047 struct dt_object *obj)
1049 struct lfsck_thread_info *info = lfsck_env_info(env);
1050 struct lu_attr *la = &info->lti_la;
1051 struct dt_object_format *dof = &info->lti_dof;
1052 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1053 struct dt_device *dt = lfsck_obj2dev(obj);
1060 if (bk->lb_param & LPF_DRYRUN)
1063 memset(la, 0, sizeof(*la));
1064 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1065 la->la_valid = LA_MODE | LA_UID | LA_GID;
1066 memset(dof, 0, sizeof(*dof));
1067 dof->dof_type = dt_mode_to_dft(S_IFREG);
1069 th = dt_trans_create(env, dt);
1071 GOTO(log, rc = PTR_ERR(th));
1073 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1077 rc = dt_declare_record_write(env, obj,
1078 lfsck_buf_get(env, &lastid,
1084 rc = dt_trans_start_local(env, dt, th);
1088 dt_write_lock(env, obj, 0);
1089 if (likely(dt_object_exists(obj) == 0)) {
1090 rc = dt_create(env, obj, la, NULL, dof, th);
1092 rc = dt_record_write(env, obj,
1093 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1096 dt_write_unlock(env, obj);
1101 dt_trans_stop(env, dt, th);
1104 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1106 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1112 lfsck_layout_lastid_reload(const struct lu_env *env,
1113 struct lfsck_component *com,
1114 struct lfsck_layout_seq *lls)
1120 dt_read_lock(env, lls->lls_lastid_obj, 0);
1121 rc = dt_record_read(env, lls->lls_lastid_obj,
1122 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1123 dt_read_unlock(env, lls->lls_lastid_obj);
1124 if (unlikely(rc != 0))
1127 lastid = le64_to_cpu(lastid);
1128 if (lastid < lls->lls_lastid_known) {
1129 struct lfsck_instance *lfsck = com->lc_lfsck;
1130 struct lfsck_layout *lo = com->lc_file_ram;
1132 lls->lls_lastid = lls->lls_lastid_known;
1134 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1135 LASSERT(lfsck->li_out_notify != NULL);
1137 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1138 LE_LASTID_REBUILDING);
1139 lo->ll_flags |= LF_CRASHED_LASTID;
1141 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1142 "LAST_ID file (1) for the sequence %#llx"
1143 ", old value %llu, known value %llu\n",
1144 lfsck_lfsck2name(lfsck), lls->lls_seq,
1145 lastid, lls->lls_lastid);
1147 } else if (lastid >= lls->lls_lastid) {
1148 lls->lls_lastid = lastid;
1156 lfsck_layout_lastid_store(const struct lu_env *env,
1157 struct lfsck_component *com)
1159 struct lfsck_instance *lfsck = com->lc_lfsck;
1160 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1161 struct dt_device *dt = lfsck->li_bottom;
1162 struct lfsck_layout_slave_data *llsd = com->lc_data;
1163 struct lfsck_layout_seq *lls;
1169 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1172 if (!lls->lls_dirty)
1175 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1176 "<seq> %#llx as <oid> %llu\n",
1177 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1179 if (bk->lb_param & LPF_DRYRUN) {
1184 th = dt_trans_create(env, dt);
1187 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1188 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1189 lfsck_lfsck2name(com->lc_lfsck),
1194 lastid = cpu_to_le64(lls->lls_lastid);
1195 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1196 lfsck_buf_get(env, &lastid,
1202 rc = dt_trans_start_local(env, dt, th);
1206 dt_write_lock(env, lls->lls_lastid_obj, 0);
1207 rc = dt_record_write(env, lls->lls_lastid_obj,
1208 lfsck_buf_get(env, &lastid,
1209 sizeof(lastid)), &pos, th);
1210 dt_write_unlock(env, lls->lls_lastid_obj);
1215 dt_trans_stop(env, dt, th);
1218 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1219 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1220 lfsck_lfsck2name(com->lc_lfsck),
1229 lfsck_layout_lastid_load(const struct lu_env *env,
1230 struct lfsck_component *com,
1231 struct lfsck_layout_seq *lls)
1233 struct lfsck_instance *lfsck = com->lc_lfsck;
1234 struct lfsck_layout *lo = com->lc_file_ram;
1235 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1236 struct dt_object *obj;
1241 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1242 obj = dt_locate(env, lfsck->li_bottom, fid);
1244 RETURN(PTR_ERR(obj));
1246 /* LAST_ID crashed, to be rebuilt */
1247 if (dt_object_exists(obj) == 0) {
1248 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1249 LASSERT(lfsck->li_out_notify != NULL);
1251 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1252 LE_LASTID_REBUILDING);
1253 lo->ll_flags |= LF_CRASHED_LASTID;
1255 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1256 "LAST_ID file for sequence %#llx\n",
1257 lfsck_lfsck2name(lfsck), lls->lls_seq);
1259 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1261 struct l_wait_info lwi = LWI_TIMEOUT(
1262 cfs_time_seconds(cfs_fail_val),
1265 /* Some others may changed the cfs_fail_val
1266 * as zero after above check, re-check it for
1267 * sure to avoid falling into wait for ever. */
1268 if (likely(lwi.lwi_timeout > 0)) {
1269 struct ptlrpc_thread *thread =
1272 up_write(&com->lc_sem);
1273 l_wait_event(thread->t_ctl_waitq,
1274 !thread_is_running(thread),
1276 down_write(&com->lc_sem);
1281 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1283 dt_read_lock(env, obj, 0);
1284 rc = dt_read(env, obj,
1285 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1287 dt_read_unlock(env, obj);
1288 if (rc != 0 && rc != sizeof(__u64))
1289 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1291 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1292 LASSERT(lfsck->li_out_notify != NULL);
1294 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1295 LE_LASTID_REBUILDING);
1296 lo->ll_flags |= LF_CRASHED_LASTID;
1298 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1299 "LAST_ID file for the sequence %#llx"
1301 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1304 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1312 lfsck_object_put(env, obj);
1314 lls->lls_lastid_obj = obj;
1319 static void lfsck_layout_record_failure(const struct lu_env *env,
1320 struct lfsck_instance *lfsck,
1321 struct lfsck_layout *lo)
1325 lo->ll_objs_failed_phase1++;
1326 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1328 if (lo->ll_pos_first_inconsistent == 0 ||
1329 lo->ll_pos_first_inconsistent < cookie) {
1330 lo->ll_pos_first_inconsistent = cookie;
1332 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1333 "inconsistency at the pos [%llu]\n",
1334 lfsck_lfsck2name(lfsck),
1335 lo->ll_pos_first_inconsistent);
1339 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1340 struct lfsck_component *com,
1343 struct lfsck_instance *lfsck = com->lc_lfsck;
1344 struct lfsck_layout *lo = com->lc_file_ram;
1346 down_write(&com->lc_sem);
1347 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1348 HALF_SEC - com->lc_time_last_checkpoint);
1349 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1350 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1353 if (lo->ll_flags & LF_INCOMPLETE) {
1354 lo->ll_status = LS_PARTIAL;
1356 if (lfsck->li_master) {
1357 struct lfsck_assistant_data *lad = com->lc_data;
1359 if (lad->lad_incomplete)
1360 lo->ll_status = LS_PARTIAL;
1362 lo->ll_status = LS_COMPLETED;
1364 lo->ll_status = LS_COMPLETED;
1367 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1368 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1369 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1370 lo->ll_success_count++;
1371 } else if (rc == 0) {
1372 if (lfsck->li_status != 0)
1373 lo->ll_status = lfsck->li_status;
1375 lo->ll_status = LS_STOPPED;
1377 lo->ll_status = LS_FAILED;
1380 rc = lfsck_layout_store(env, com);
1381 up_write(&com->lc_sem);
1386 static int lfsck_layout_trans_stop(const struct lu_env *env,
1387 struct dt_device *dev,
1388 struct thandle *handle, int result)
1392 /* XXX: If there is something worng or it needs to repair nothing,
1393 * then notify the lower to stop the modification. Currently,
1394 * we use th_result for such purpose, that may be replaced by
1395 * some rollback mechanism in the future. */
1396 handle->th_result = result;
1397 rc = dt_trans_stop(env, dev, handle);
1399 return result > 0 ? 0 : result;
1401 return rc == 0 ? 1 : rc;
1404 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1405 struct lfsck_component *com,
1406 const struct lu_fid *pfid,
1407 const struct lu_fid *cfid,
1408 __u32 ea_off, __u32 ost_idx)
1410 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
1411 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid4;
1412 struct dt_device *dev;
1413 struct dt_object *obj;
1414 struct thandle *th = NULL;
1419 idx = lfsck_sub_trace_file_fid2idx(pfid);
1420 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1421 dev = lfsck_obj2dev(obj);
1422 fid_cpu_to_be(key, pfid);
1423 key->f_ver = cpu_to_be32(ea_off);
1424 fid_cpu_to_be(rec, cfid);
1425 rec->f_ver = cpu_to_be32(ost_idx);
1427 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1429 th = dt_trans_create(env, dev);
1431 GOTO(unlock, rc = PTR_ERR(th));
1433 rc = dt_declare_insert(env, obj,
1434 (const struct dt_rec *)rec,
1435 (const struct dt_key *)key, th);
1439 rc = dt_trans_start_local(env, dev, th);
1443 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1444 (const struct dt_key *)key, th, 1);
1449 if (th != NULL && !IS_ERR(th))
1450 dt_trans_stop(env, dev, th);
1452 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1454 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", ea_off = %u, "
1455 "ost_idx = %u, into the trace file for further dangling check: "
1456 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1457 PFID(pfid), PFID(cfid), ea_off, ost_idx, rc);
1462 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1463 struct lfsck_component *com,
1464 const struct lu_fid *fid,
1467 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
1468 struct dt_device *dev;
1469 struct dt_object *obj;
1470 struct thandle *th = NULL;
1475 idx = lfsck_sub_trace_file_fid2idx(fid);
1476 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1477 dev = lfsck_obj2dev(obj);
1478 fid_cpu_to_be(key, fid);
1479 key->f_ver = cpu_to_be32(ea_off);
1481 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1483 th = dt_trans_create(env, dev);
1485 GOTO(unlock, rc = PTR_ERR(th));
1487 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1491 rc = dt_trans_start_local(env, dev, th);
1495 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1500 if (th != NULL && !IS_ERR(th))
1501 dt_trans_stop(env, dev, th);
1503 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1505 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1506 ", ea_off = %u from the trace file: rc = %d\n",
1507 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), ea_off, rc);
1513 * Get the system default stripe size.
1515 * \param[in] env pointer to the thread context
1516 * \param[in] lfsck pointer to the lfsck instance
1517 * \param[out] size pointer to the default stripe size
1519 * \retval 0 for success
1520 * \retval negative error number on failure
1522 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1523 struct lfsck_instance *lfsck,
1526 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1527 struct dt_object *root;
1530 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1532 return PTR_ERR(root);
1534 /* Get the default stripe size via xattr_get on the backend root. */
1535 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1538 /* The lum->lmm_stripe_size is LE mode. The *size also
1539 * should be LE mode. So it is unnecessary to convert. */
1540 *size = lum->lmm_stripe_size;
1542 } else if (unlikely(rc == 0)) {
1546 lfsck_object_put(env, root);
1552 * \retval +1: repaired
1553 * \retval 0: did nothing
1554 * \retval -ve: on error
1556 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1557 struct thandle *handle,
1558 struct dt_object *parent,
1559 struct lu_fid *cfid,
1561 struct lov_ost_data_v1 *slot,
1562 int fl, __u32 ost_idx)
1564 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1565 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1566 struct lu_buf ea_buf;
1572 magic = le32_to_cpu(lmm->lmm_magic);
1573 count = le16_to_cpu(lmm->lmm_stripe_count);
1575 fid_to_ostid(cfid, oi);
1576 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1577 slot->l_ost_gen = cpu_to_le32(0);
1578 slot->l_ost_idx = cpu_to_le32(ost_idx);
1580 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE) {
1581 struct lov_ost_data_v1 *objs;
1584 if (magic == LOV_MAGIC_V1)
1585 objs = &lmm->lmm_objects[0];
1587 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1588 for (i = 0; i < count; i++, objs++) {
1589 if (objs != slot && lovea_slot_is_dummy(objs))
1593 /* If the @slot is the last dummy slot to be refilled,
1594 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1596 lmm->lmm_pattern &= ~cpu_to_le32(LOV_PATTERN_F_HOLE);
1599 lfsck_buf_init(&ea_buf, lmm, lov_mds_md_size(count, magic));
1600 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1608 * \retval +1: repaired
1609 * \retval 0: did nothing
1610 * \retval -ve: on error
1612 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1613 struct lfsck_instance *lfsck,
1614 struct thandle *handle,
1615 struct dt_object *parent,
1616 struct lu_fid *cfid,
1617 struct lu_buf *buf, int fl,
1618 __u32 ost_idx, __u32 ea_off, bool reset)
1620 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1621 struct lov_ost_data_v1 *objs;
1627 if (fl == LU_XATTR_CREATE || reset) {
1628 __u32 pattern = LOV_PATTERN_RAID0;
1631 LASSERT(buf->lb_len >= lov_mds_md_size(count, LOV_MAGIC_V1));
1633 if (ea_off != 0 || reset) {
1634 pattern |= LOV_PATTERN_F_HOLE;
1638 memset(lmm, 0, buf->lb_len);
1639 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1640 lmm->lmm_pattern = cpu_to_le32(pattern);
1641 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1642 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1644 rc = lfsck_layout_get_def_stripesize(env, lfsck,
1645 &lmm->lmm_stripe_size);
1649 objs = &lmm->lmm_objects[ea_off];
1651 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1654 count = le16_to_cpu(lmm->lmm_stripe_count);
1655 if (magic == LOV_MAGIC_V1)
1656 objs = &lmm->lmm_objects[count];
1658 objs = &((struct lov_mds_md_v3 *)lmm)->
1661 gap = ea_off - count;
1664 LASSERT(buf->lb_len >= lov_mds_md_size(count, magic));
1667 memset(objs, 0, gap * sizeof(*objs));
1668 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1672 lmm->lmm_layout_gen =
1673 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1677 lmm->lmm_stripe_count = cpu_to_le16(count);
1678 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1681 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1682 DFID": parent "DFID", OST-index %u, stripe-index %u, fl %d, "
1683 "reset %s, %s LOV EA hole: rc = %d\n",
1684 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1685 ost_idx, ea_off, fl, reset ? "yes" : "no",
1686 hole ? "with" : "without", rc);
1691 static int __lfsck_layout_update_pfid(const struct lu_env *env,
1692 struct dt_object *child,
1693 const struct lu_fid *pfid, __u32 offset)
1695 struct dt_device *dev = lfsck_obj2dev(child);
1696 struct filter_fid *ff = &lfsck_env_info(env)->lti_new_pfid;
1697 struct thandle *handle;
1698 struct lu_buf buf = { NULL };
1701 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1702 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1703 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1704 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1705 * parent MDT-object's layout EA. */
1706 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
1707 lfsck_buf_init(&buf, ff, sizeof(struct filter_fid));
1709 handle = dt_trans_create(env, dev);
1711 RETURN(PTR_ERR(handle));
1713 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1717 rc = dt_trans_start_local(env, dev, handle);
1721 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1726 dt_trans_stop(env, dev, handle);
1732 * \retval +1: repaired
1733 * \retval 0: did nothing
1734 * \retval -ve: on error
1736 static int lfsck_layout_update_pfid(const struct lu_env *env,
1737 struct lfsck_component *com,
1738 struct dt_object *parent,
1739 struct lu_fid *cfid,
1740 struct dt_device *cdev, __u32 ea_off)
1742 struct dt_object *child;
1746 child = lfsck_object_find_by_dev(env, cdev, cfid);
1748 RETURN(PTR_ERR(child));
1750 rc = __lfsck_layout_update_pfid(env, child,
1751 lu_object_fid(&parent->do_lu), ea_off);
1752 lfsck_object_put(env, child);
1754 RETURN(rc == 0 ? 1 : rc);
1758 * This function will create the MDT-object with the given (partial) LOV EA.
1760 * Under some data corruption cases, the MDT-object of the file may be lost,
1761 * but its OST-objects, or some of them are there. The layout LFSCK needs to
1762 * re-create the MDT-object with the orphan OST-object(s) information.
1764 * On the other hand, the LFSCK may has created some OST-object for repairing
1765 * dangling LOV EA reference, but as the LFSCK processing, it may find that
1766 * the old OST-object is there and should replace the former new created OST
1767 * object. Unfortunately, some others have modified such newly created object.
1768 * To keep the data (both new and old), the LFSCK will create MDT-object with
1769 * new FID to reference the original OST-object.
1771 * \param[in] env pointer to the thread context
1772 * \param[in] com pointer to the lfsck component
1773 * \param[in] ltd pointer to target device descriptor
1774 * \param[in] rec pointer to the record for the orphan OST-object
1775 * \param[in] cfid pointer to FID for the orphan OST-object
1776 * \param[in] infix additional information, such as the FID for original
1777 * MDT-object and the stripe offset in the LOV EA
1778 * \param[in] type the type for describing why the orphan MDT-object is
1779 * created. The rules are as following:
1781 * type "C": Multiple OST-objects claim the same MDT-object and the
1782 * same slot in the layout EA. Then the LFSCK will create
1783 * new MDT-object(s) to hold the conflict OST-object(s).
1785 * type "N": The orphan OST-object does not know which one was the
1786 * real parent MDT-object, so the LFSCK uses new FID for
1787 * its parent MDT-object.
1789 * type "R": The orphan OST-object knows its parent MDT-object FID,
1790 * but does not know the position (the file name) in the
1793 * type "D": The MDT-object is a directory, it may knows its parent
1794 * but because there is no valid linkEA, the LFSCK cannot
1795 * know where to put it back to the namespace.
1796 * type "O": The MDT-object has no linkEA, and there is no name
1797 * entry that references the MDT-object.
1799 * type "P": The orphan object to be created was a parent directory
1800 * of some MDT-object which linkEA shows that the @orphan
1801 * object is missing.
1803 * The orphan name will be like:
1804 * ${FID}-${infix}-${type}-${conflict_version}
1806 * \param[in] ea_off the stripe offset in the LOV EA
1808 * \retval positive on repaired something
1809 * \retval 0 if needs to repair nothing
1810 * \retval negative error number on failure
1812 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1813 struct lfsck_component *com,
1814 struct lfsck_tgt_desc *ltd,
1815 struct lu_orphan_rec *rec,
1816 struct lu_fid *cfid,
1821 struct lfsck_thread_info *info = lfsck_env_info(env);
1822 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
1823 char *name = info->lti_key;
1824 struct lu_attr *la = &info->lti_la2;
1825 struct dt_object_format *dof = &info->lti_dof;
1826 struct lfsck_instance *lfsck = com->lc_lfsck;
1827 struct lu_fid *pfid = &rec->lor_fid;
1828 struct lu_fid *tfid = &info->lti_fid3;
1829 struct dt_device *dev = lfsck->li_bottom;
1830 struct dt_object *lpf = lfsck->li_lpf_obj;
1831 struct dt_object *pobj = NULL;
1832 struct dt_object *cobj = NULL;
1833 struct thandle *th = NULL;
1834 struct lu_buf *ea_buf = &info->lti_big_buf;
1835 struct lu_buf lov_buf;
1836 struct lfsck_lock_handle *llh = &info->lti_llh;
1837 struct linkea_data ldata = { NULL };
1838 struct lu_buf linkea_buf;
1839 const struct lu_name *pname;
1845 if (unlikely(lpf == NULL))
1846 GOTO(log, rc = -ENXIO);
1848 /* We use two separated transactions to repair the inconsistency.
1850 * 1) create the MDT-object locally.
1851 * 2) update the OST-object's PFID EA if necessary.
1853 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
1854 * updated when the layout LFSCK run next time.
1856 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
1857 * when the layout LFSCK run next time. */
1859 if (fid_is_zero(pfid)) {
1860 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1864 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1866 GOTO(log, rc = PTR_ERR(cobj));
1869 pobj = lfsck_object_find_by_dev(env, dev, pfid);
1871 GOTO(log, rc = PTR_ERR(pobj));
1873 LASSERT(infix != NULL);
1874 LASSERT(type != NULL);
1876 memset(la, 0, sizeof(*la));
1877 la->la_uid = rec->lor_uid;
1878 la->la_gid = rec->lor_gid;
1879 la->la_mode = S_IFREG | S_IRUSR;
1880 la->la_valid = LA_MODE | LA_UID | LA_GID;
1882 memset(dof, 0, sizeof(*dof));
1883 dof->dof_type = dt_mode_to_dft(S_IFREG);
1884 /* Because the dof->dof_reg.striped = 0, the LOD will not create
1885 * the stripe(s). The LFSCK will specify the LOV EA via
1886 * lfsck_layout_extend_lovea(). */
1888 size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1889 if (ea_buf->lb_len < size) {
1890 lu_buf_realloc(ea_buf, size);
1891 if (ea_buf->lb_buf == NULL)
1892 GOTO(log, rc = -ENOMEM);
1897 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
1899 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1900 (const struct dt_key *)name);
1901 if (rc != 0 && rc != -ENOENT)
1905 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
1906 MDS_INODELOCK_UPDATE, LCK_PW);
1910 /* Re-check whether the name conflict with othrs after taken
1912 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1913 (const struct dt_key *)name);
1914 if (unlikely(rc == 0)) {
1922 pname = lfsck_name_get_const(env, name, strlen(name));
1923 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
1924 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
1928 /* The 1st transaction. */
1929 th = dt_trans_create(env, dev);
1931 GOTO(unlock, rc = PTR_ERR(th));
1933 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1937 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
1938 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
1939 LU_XATTR_REPLACE, th);
1943 dtrec->rec_fid = pfid;
1944 dtrec->rec_type = S_IFREG;
1945 rc = dt_declare_insert(env, lpf,
1946 (const struct dt_rec *)dtrec,
1947 (const struct dt_key *)name, th);
1951 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1952 ldata.ld_leh->leh_len);
1953 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
1954 XATTR_NAME_LINK, 0, th);
1958 rc = dt_trans_start_local(env, dev, th);
1962 dt_write_lock(env, pobj, 0);
1963 rc = dt_create(env, pobj, la, NULL, dof, th);
1965 rc = lfsck_layout_extend_lovea(env, lfsck, th, pobj, cfid,
1966 &lov_buf, 0, ltd->ltd_index, ea_off, true);
1967 dt_write_unlock(env, pobj);
1971 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
1972 (const struct dt_key *)name, th, 1);
1976 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
1977 if (rc == 0 && cobj != NULL) {
1978 dt_trans_stop(env, dev, th);
1981 /* The 2nd transaction. */
1982 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ea_off);
1989 dt_trans_stop(env, dev, th);
1995 if (cobj != NULL && !IS_ERR(cobj))
1996 lfsck_object_put(env, cobj);
1997 if (pobj != NULL && !IS_ERR(pobj))
1998 lfsck_object_put(env, pobj);
2001 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2002 "recreate the lost MDT-object: parent "DFID
2003 ", child "DFID", OST-index %u, stripe-index %u, "
2004 "infix %s, type %s: rc = %d\n",
2005 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2006 ltd->ltd_index, ea_off, infix, type, rc);
2008 return rc >= 0 ? 1 : rc;
2011 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2012 struct lfsck_component *com,
2013 const struct lu_fid *fid,
2016 struct lfsck_thread_info *info = lfsck_env_info(env);
2017 struct lfsck_request *lr = &info->lti_lr;
2018 struct lfsck_instance *lfsck = com->lc_lfsck;
2019 struct lfsck_tgt_desc *ltd;
2020 struct ptlrpc_request *req;
2021 struct lfsck_request *tmp;
2022 struct obd_export *exp;
2026 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2027 if (unlikely(ltd == NULL))
2031 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2032 GOTO(put, rc = -EOPNOTSUPP);
2034 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2036 GOTO(put, rc = -ENOMEM);
2038 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2040 ptlrpc_request_free(req);
2045 memset(lr, 0, sizeof(*lr));
2046 lr->lr_event = LE_CONDITIONAL_DESTROY;
2047 lr->lr_active = LFSCK_TYPE_LAYOUT;
2050 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2052 ptlrpc_request_set_replen(req);
2054 rc = ptlrpc_queue_wait(req);
2055 ptlrpc_req_finished(req);
2065 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2066 struct lfsck_component *com,
2067 struct lfsck_request *lr)
2069 struct lfsck_thread_info *info = lfsck_env_info(env);
2070 struct lu_attr *la = &info->lti_la;
2071 union ldlm_policy_data *policy = &info->lti_policy;
2072 struct ldlm_res_id *resid = &info->lti_resid;
2073 struct lfsck_instance *lfsck = com->lc_lfsck;
2074 struct dt_device *dev = lfsck->li_bottom;
2075 struct lu_fid *fid = &lr->lr_fid;
2076 struct dt_object *obj;
2077 struct thandle *th = NULL;
2078 struct lustre_handle lh = { 0 };
2083 obj = lfsck_object_find_by_dev(env, dev, fid);
2085 RETURN(PTR_ERR(obj));
2087 dt_read_lock(env, obj, 0);
2088 if (dt_object_exists(obj) == 0 ||
2089 lfsck_is_dead_obj(obj)) {
2090 dt_read_unlock(env, obj);
2092 GOTO(put, rc = -ENOENT);
2095 /* Get obj's attr without lock firstly. */
2096 rc = dt_attr_get(env, obj, la);
2097 dt_read_unlock(env, obj);
2101 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2102 GOTO(put, rc = -ETXTBSY);
2104 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2105 LASSERT(lfsck->li_namespace != NULL);
2107 memset(policy, 0, sizeof(*policy));
2108 policy->l_extent.end = OBD_OBJECT_EOF;
2109 ost_fid_build_resid(fid, resid);
2110 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2111 policy, LCK_EX, &flags, ldlm_blocking_ast,
2112 ldlm_completion_ast, NULL, NULL, 0,
2113 LVB_T_NONE, NULL, &lh);
2115 GOTO(put, rc = -EIO);
2117 dt_write_lock(env, obj, 0);
2118 /* Get obj's attr within lock again. */
2119 rc = dt_attr_get(env, obj, la);
2123 if (la->la_ctime != 0)
2124 GOTO(unlock, rc = -ETXTBSY);
2126 th = dt_trans_create(env, dev);
2128 GOTO(unlock, rc = PTR_ERR(th));
2130 rc = dt_declare_ref_del(env, obj, th);
2134 rc = dt_declare_destroy(env, obj, th);
2138 rc = dt_trans_start_local(env, dev, th);
2142 rc = dt_ref_del(env, obj, th);
2146 rc = dt_destroy(env, obj, th);
2148 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2149 "OST-object "DFID" that was created for reparing "
2150 "dangling referenced case. But the original missing "
2151 "OST-object is found now.\n",
2152 lfsck_lfsck2name(lfsck), PFID(fid));
2157 dt_trans_stop(env, dev, th);
2160 dt_write_unlock(env, obj);
2161 ldlm_lock_decref(&lh, LCK_EX);
2164 lfsck_object_put(env, obj);
2170 * Some OST-object has occupied the specified layout EA slot.
2171 * Such OST-object may be generated by the LFSCK when repair
2172 * dangling referenced MDT-object, which can be indicated by
2173 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2174 * is true and such OST-object has not been modified yet, we
2175 * will replace it with the orphan OST-object; otherwise the
2176 * LFSCK will create new MDT-object to reference the orphan.
2178 * \retval +1: repaired
2179 * \retval 0: did nothing
2180 * \retval -ve: on error
2182 static int lfsck_layout_conflict_create(const struct lu_env *env,
2183 struct lfsck_component *com,
2184 struct lfsck_tgt_desc *ltd,
2185 struct lu_orphan_rec *rec,
2186 struct dt_object *parent,
2187 struct lu_fid *cfid,
2188 struct lu_buf *ea_buf,
2189 struct lov_ost_data_v1 *slot,
2192 struct lfsck_thread_info *info = lfsck_env_info(env);
2193 struct lu_fid *cfid2 = &info->lti_fid2;
2194 struct ost_id *oi = &info->lti_oi;
2195 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2196 struct dt_device *dev = lfsck_obj2dev(parent);
2197 struct thandle *th = NULL;
2198 struct lustre_handle lh = { 0 };
2199 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2203 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2204 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2208 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2209 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2213 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2214 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2219 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2221 /* If the conflict OST-obejct is not created for fixing dangling
2222 * referenced MDT-object in former LFSCK check/repair, or it has
2223 * been modified by others, then we cannot destroy it. Re-create
2224 * a new MDT-object for the orphan OST-object. */
2225 if (rc == -ETXTBSY) {
2226 /* No need the layout lock on the original parent. */
2227 lfsck_ibits_unlock(&lh, LCK_EX);
2229 fid_zero(&rec->lor_fid);
2230 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2231 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2233 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2234 info->lti_tmpbuf, "C", ea_off);
2239 if (rc != 0 && rc != -ENOENT)
2242 th = dt_trans_create(env, dev);
2244 GOTO(unlock, rc = PTR_ERR(th));
2246 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2247 LU_XATTR_REPLACE, th);
2251 rc = dt_trans_start_local(env, dev, th);
2255 dt_write_lock(env, parent, 0);
2256 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2257 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2258 LU_XATTR_REPLACE, ltd->ltd_index);
2259 dt_write_unlock(env, parent);
2264 dt_trans_stop(env, dev, th);
2267 lfsck_ibits_unlock(&lh, LCK_EX);
2270 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2271 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2272 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2273 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2274 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2277 return rc >= 0 ? 1 : rc;
2281 * \retval +1: repaired
2282 * \retval 0: did nothing
2283 * \retval -ve: on error
2285 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2286 struct lfsck_component *com,
2287 struct lfsck_tgt_desc *ltd,
2288 struct lu_orphan_rec *rec,
2289 struct dt_object *parent,
2290 struct lu_fid *cfid,
2291 __u32 ost_idx, __u32 ea_off)
2293 struct lfsck_thread_info *info = lfsck_env_info(env);
2294 struct lu_buf *buf = &info->lti_big_buf;
2295 struct lu_fid *fid = &info->lti_fid2;
2296 struct ost_id *oi = &info->lti_oi;
2297 struct lfsck_instance *lfsck = com->lc_lfsck;
2298 struct dt_device *dt = lfsck_obj2dev(parent);
2299 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2300 struct thandle *handle = NULL;
2302 struct lov_mds_md_v1 *lmm;
2303 struct lov_ost_data_v1 *objs;
2304 struct lustre_handle lh = { 0 };
2311 bool locked = false;
2314 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2315 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2318 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2319 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2320 "stripe-index %u: rc = %d\n",
2321 lfsck_lfsck2name(lfsck), PFID(cfid),
2322 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off, rc);
2329 dt_write_unlock(env, parent);
2333 if (handle != NULL) {
2334 dt_trans_stop(env, dt, handle);
2339 GOTO(unlock_layout, rc);
2342 if (buf->lb_len < lovea_size) {
2343 lu_buf_realloc(buf, lovea_size);
2344 if (buf->lb_buf == NULL)
2345 GOTO(unlock_layout, rc = -ENOMEM);
2348 if (!(bk->lb_param & LPF_DRYRUN)) {
2349 handle = dt_trans_create(env, dt);
2351 GOTO(unlock_layout, rc = PTR_ERR(handle));
2353 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2358 rc = dt_trans_start_local(env, dt, handle);
2363 dt_write_lock(env, parent, 0);
2365 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2366 if (rc == -ERANGE) {
2367 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2370 } else if (rc == -ENODATA || rc == 0) {
2371 lovea_size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2372 /* If the declared is not big enough, re-try. */
2373 if (buf->lb_len < lovea_size) {
2377 fl = LU_XATTR_CREATE;
2378 } else if (rc < 0) {
2379 GOTO(unlock_parent, rc);
2380 } else if (unlikely(buf->lb_len == 0)) {
2383 fl = LU_XATTR_REPLACE;
2387 if (fl == LU_XATTR_CREATE) {
2388 if (bk->lb_param & LPF_DRYRUN)
2389 GOTO(unlock_parent, rc = 1);
2391 LASSERT(buf->lb_len >= lovea_size);
2393 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2394 buf, fl, ost_idx, ea_off, false);
2396 GOTO(unlock_parent, rc);
2400 rc1 = lfsck_layout_verify_header(lmm);
2402 /* If the LOV EA crashed, the rebuild it. */
2403 if (rc1 == -EINVAL) {
2404 if (bk->lb_param & LPF_DRYRUN)
2405 GOTO(unlock_parent, rc = 1);
2407 LASSERT(buf->lb_len >= lovea_size);
2409 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2410 buf, fl, ost_idx, ea_off, true);
2412 GOTO(unlock_parent, rc);
2415 /* For other unknown magic/pattern, keep the current LOV EA. */
2417 GOTO(unlock_parent, rc = rc1);
2419 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2420 * been verified in lfsck_layout_verify_header() already. If some
2421 * new magic introduced in the future, then layout LFSCK needs to
2422 * be updated also. */
2423 magic = le32_to_cpu(lmm->lmm_magic);
2424 if (magic == LOV_MAGIC_V1) {
2425 objs = &lmm->lmm_objects[0];
2427 LASSERT(magic == LOV_MAGIC_V3);
2428 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2431 count = le16_to_cpu(lmm->lmm_stripe_count);
2433 GOTO(unlock_parent, rc = -EINVAL);
2436 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2437 if (count <= ea_off) {
2438 if (bk->lb_param & LPF_DRYRUN)
2439 GOTO(unlock_parent, rc = 1);
2441 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2442 /* If the declared is not big enough, re-try. */
2443 if (buf->lb_len < lovea_size) {
2448 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2449 buf, fl, ost_idx, ea_off, false);
2451 GOTO(unlock_parent, rc);
2454 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2456 for (i = 0; i < count; i++, objs++) {
2457 /* The MDT-object was created via lfsck_layout_recover_create()
2458 * by others before, and we fill the dummy layout EA. */
2459 if (lovea_slot_is_dummy(objs)) {
2463 if (bk->lb_param & LPF_DRYRUN)
2464 GOTO(unlock_parent, rc = 1);
2466 lmm->lmm_layout_gen =
2467 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2468 rc = lfsck_layout_refill_lovea(env, handle, parent,
2469 cfid, buf, objs, fl,
2472 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2473 "dummy layout slot for "DFID": parent "DFID
2474 ", OST-index %u, stripe-index %u: rc = %d\n",
2475 lfsck_lfsck2name(lfsck), PFID(cfid),
2476 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2478 GOTO(unlock_parent, rc);
2481 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2482 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2484 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2485 "invalid layout EA at the slot %d, index %u\n",
2486 lfsck_lfsck2name(lfsck),
2487 PFID(lfsck_dto2fid(parent)), i,
2488 le32_to_cpu(objs->l_ost_idx));
2490 GOTO(unlock_parent, rc);
2493 /* It should be rare case, the slot is there, but the LFSCK
2494 * does not handle it during the first-phase cycle scanning. */
2495 if (unlikely(lu_fid_eq(fid, cfid))) {
2497 GOTO(unlock_parent, rc = 0);
2499 /* Rare case that the OST-object index
2500 * does not match the parent MDT-object
2501 * layout EA. We trust the later one. */
2502 if (bk->lb_param & LPF_DRYRUN)
2503 GOTO(unlock_parent, rc = 1);
2505 dt_write_unlock(env, parent);
2507 dt_trans_stop(env, dt, handle);
2508 lfsck_ibits_unlock(&lh, LCK_EX);
2509 rc = lfsck_layout_update_pfid(env, com, parent,
2510 cfid, ltd->ltd_tgt, i);
2512 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2513 "updated OST-object's pfid for "DFID
2514 ": parent "DFID", OST-index %u, "
2515 "stripe-index %u: rc = %d\n",
2516 lfsck_lfsck2name(lfsck), PFID(cfid),
2517 PFID(lfsck_dto2fid(parent)),
2518 ltd->ltd_index, i, rc);
2525 /* The MDT-object exists, but related layout EA slot is occupied
2527 if (bk->lb_param & LPF_DRYRUN)
2528 GOTO(unlock_parent, rc = 1);
2530 dt_write_unlock(env, parent);
2532 dt_trans_stop(env, dt, handle);
2533 lfsck_ibits_unlock(&lh, LCK_EX);
2534 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2535 objs = &lmm->lmm_objects[ea_off];
2537 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2538 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2545 dt_write_unlock(env, parent);
2549 dt_trans_stop(env, dt, handle);
2552 lfsck_ibits_unlock(&lh, LCK_EX);
2557 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2558 struct lfsck_component *com,
2559 struct lfsck_tgt_desc *ltd,
2560 struct lu_orphan_rec *rec,
2561 struct lu_fid *cfid)
2563 struct lfsck_layout *lo = com->lc_file_ram;
2564 struct lu_fid *pfid = &rec->lor_fid;
2565 struct dt_object *parent = NULL;
2566 __u32 ea_off = pfid->f_stripe_idx;
2570 if (!fid_is_sane(cfid))
2571 GOTO(out, rc = -EINVAL);
2573 if (fid_is_zero(pfid)) {
2574 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2580 if (!fid_is_sane(pfid))
2581 GOTO(out, rc = -EINVAL);
2583 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2585 GOTO(out, rc = PTR_ERR(parent));
2587 if (unlikely(dt_object_remote(parent) != 0))
2588 GOTO(put, rc = -EXDEV);
2590 if (dt_object_exists(parent) == 0) {
2591 lfsck_object_put(env, parent);
2592 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2597 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2598 GOTO(put, rc = -EISDIR);
2600 /* The orphan OST-object claims to be the parent's stripe, then
2601 * related dangling record in the trace file is meaningless. */
2602 rc = lfsck_layout_del_dangling_rec(env, com, pfid, ea_off);
2603 if (rc != 0 && rc != -ENOENT)
2606 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2607 ltd->ltd_index, ea_off);
2613 lfsck_object_put(env, parent);
2615 /* The layout EA is changed, need to be reloaded next time. */
2616 lu_object_put_nocache(env, &parent->do_lu);
2619 down_write(&com->lc_sem);
2620 com->lc_new_scanned++;
2621 com->lc_new_checked++;
2623 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2625 } else if (rc < 0) {
2626 lo->ll_objs_failed_phase2++;
2628 up_write(&com->lc_sem);
2633 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2634 struct lfsck_component *com,
2635 struct lfsck_tgt_desc *ltd)
2637 struct lfsck_assistant_data *lad = com->lc_data;
2638 struct lfsck_instance *lfsck = com->lc_lfsck;
2639 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2640 struct lfsck_thread_info *info = lfsck_env_info(env);
2641 struct ost_id *oi = &info->lti_oi;
2642 struct lu_fid *fid = &info->lti_fid;
2643 struct dt_object *obj;
2644 const struct dt_it_ops *iops;
2649 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
2650 "scanning for OST%04x\n",
2651 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2653 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
2654 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
2655 "scanning for OST%04x\n",
2656 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2661 ostid_set_seq(oi, FID_SEQ_IDIF);
2662 ostid_set_id(oi, 0);
2663 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
2667 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2668 if (unlikely(IS_ERR(obj)))
2669 GOTO(log, rc = PTR_ERR(obj));
2671 rc = obj->do_ops->do_index_try(env, obj,
2672 &dt_lfsck_layout_orphan_features);
2676 iops = &obj->do_index_ops->dio_it;
2677 di = iops->init(env, obj, 0);
2679 GOTO(put, rc = PTR_ERR(di));
2681 rc = iops->load(env, di, 0);
2683 /* -ESRCH means that the orphan OST-objects rbtree has been
2684 * cleanup because of the OSS server restart or other errors. */
2685 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
2690 rc = iops->next(env, di);
2702 struct lu_orphan_rec *rec = &info->lti_rec;
2704 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
2705 unlikely(!thread_is_running(&lfsck->li_thread)))
2708 key = iops->key(env, di);
2709 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2710 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2712 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2713 &com->lc_fid_latest_scanned_phase2);
2714 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2717 lfsck_control_speed_by_self(com);
2719 rc = iops->next(env, di);
2720 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2727 iops->fini(env, di);
2729 lfsck_object_put(env, obj);
2732 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
2733 "scanning for OST%04x: rc = %d\n",
2734 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2736 return rc > 0 ? 0 : rc;
2740 * Repair the MDT-object with dangling LOV EA reference.
2742 * we need to repair the inconsistency according to the users' requirement:
2744 * 1) Keep the inconsistency there and report the inconsistency case,
2745 * then give the chance to the application to find related issues,
2746 * and the users can make the decision about how to handle it with
2747 * more human knownledge. (by default)
2749 * 2) Re-create the missing OST-object with the FID/owner information.
2751 * \param[in] env pointer to the thread context
2752 * \param[in] com the layout LFSCK component
2753 * \param[in] parent the MDT-object with dangling LOV EA reference
2754 * \param[in] child the OST-object to be created
2755 * \param[in] ea_off the offset of the OST-object in the LOV EA
2756 * \param[in] ost_idx the index of OST on which the OST-object resides
2758 * \retval +1 for repair successfully
2759 * \retval 0 for did nothing
2760 * \retval negative error number on failure
2762 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
2763 struct lfsck_component *com,
2764 struct dt_object *parent,
2765 struct dt_object *child,
2766 __u32 ea_off, __u32 ost_idx, bool log)
2768 struct lfsck_thread_info *info = lfsck_env_info(env);
2769 struct filter_fid *ff = &info->lti_new_pfid;
2770 struct dt_object_format *dof = &info->lti_dof;
2771 struct lu_attr *la = &info->lti_la;
2772 struct lfsck_instance *lfsck = com->lc_lfsck;
2773 struct dt_device *dev = lfsck_obj2dev(child);
2774 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2775 const struct lu_fid *cfid = lfsck_dto2fid(child);
2776 struct thandle *handle;
2778 struct lustre_handle lh = { 0 };
2782 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
2785 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2786 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2791 rc = dt_attr_get(env, parent, la);
2795 la->la_mode = S_IFREG | 0666;
2796 la->la_atime = la->la_mtime = la->la_ctime = 0;
2797 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
2798 LA_ATIME | LA_MTIME | LA_CTIME;
2799 memset(dof, 0, sizeof(*dof));
2800 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2801 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2802 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2803 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2804 * parent MDT-object's layout EA. */
2805 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
2806 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
2808 handle = dt_trans_create(env, dev);
2810 GOTO(unlock1, rc = PTR_ERR(handle));
2812 rc = dt_declare_create(env, child, la, NULL, dof, handle);
2816 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2817 LU_XATTR_CREATE, handle);
2821 rc = dt_trans_start_local(env, dev, handle);
2825 dt_read_lock(env, parent, 0);
2826 if (unlikely(lfsck_is_dead_obj(parent)))
2827 GOTO(unlock2, rc = 0);
2829 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
2830 struct ost_id *oi = &info->lti_oi;
2831 struct lu_fid *tfid = &info->lti_fid2;
2832 struct lu_buf *lovea = &info->lti_big_buf;
2833 struct lov_mds_md_v1 *lmm;
2834 struct lov_ost_data_v1 *objs;
2839 rc = lfsck_layout_get_lovea(env, parent, lovea);
2843 lmm = lovea->lb_buf;
2844 rc = lfsck_layout_verify_header(lmm);
2845 if (unlikely(rc != 0))
2848 count = le16_to_cpu(lmm->lmm_stripe_count);
2849 /* Someone changed the LOV EA, do nothing. */
2850 if (count <= ea_off)
2851 GOTO(unlock2, rc = 0);
2853 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which
2854 * has been verified in lfsck_layout_verify_header() already.
2855 * If some new magic introduced in the future, then the layout
2856 * LFSCK needs to be updated also. */
2857 magic = le32_to_cpu(lmm->lmm_magic);
2858 if (magic == LOV_MAGIC_V1) {
2859 objs = &lmm->lmm_objects[ea_off];
2861 LASSERT(magic == LOV_MAGIC_V3);
2863 objs = &((struct lov_mds_md_v3 *)lmm)->\
2864 lmm_objects[ea_off];
2867 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2868 idx2 = le32_to_cpu(objs->l_ost_idx);
2869 rc = ostid_to_fid(tfid, oi, idx2);
2870 /* Someone changed the LOV EA, do nothing. */
2871 if (rc != 0 || !lu_fid_eq(tfid, cfid))
2875 rc = dt_create(env, child, la, NULL, dof, handle);
2879 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2885 dt_read_unlock(env, parent);
2888 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2891 lfsck_ibits_unlock(&lh, LCK_EX);
2895 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
2896 "dangling reference for: parent "DFID", child "
2897 DFID", ea_off %u, ost_idx %u, %s: rc = %d\n",
2898 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2900 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
2901 "Create the lost OST-object as required" :
2902 "Keep the MDT-object there by default", rc);
2908 * Repair the MDT-object with dangling LOV EA reference.
2910 * Prepare parameters and call __lfsck_layout_repair_dangling()
2911 * to repair the dangling LOV EA reference.
2913 * \param[in] env pointer to the thread context
2914 * \param[in] com the layout LFSCK component
2915 * \param[in] pfid the MDT-object's FID
2916 * \param[in] cfid the FID for the OST-object to be created
2917 * \param[in] ea_off the offset of the OST-object in the LOV EA
2918 * \param[in] ost_idx the index of OST on which the OST-object resides
2920 * \retval +1 for repair successfully
2921 * \retval 0 for did nothing
2922 * \retval negative error number on failure
2924 static int lfsck_layout_repair_dangling(const struct lu_env *env,
2925 struct lfsck_component *com,
2926 const struct lu_fid *pfid,
2927 const struct lu_fid *cfid,
2928 __u32 ea_off, __u32 ost_idx)
2930 struct lfsck_instance *lfsck = com->lc_lfsck;
2931 struct dt_object *parent = NULL;
2932 struct dt_object *child = NULL;
2933 struct lfsck_tgt_desc *ltd;
2937 parent = lfsck_object_find_bottom(env, lfsck, pfid);
2939 GOTO(log, rc = PTR_ERR(parent));
2941 /* The MDT-object has been removed. */
2942 if (dt_object_exists(parent) == 0)
2945 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
2946 if (unlikely(ltd == NULL))
2947 GOTO(log, rc = -ENODEV);
2949 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2951 GOTO(log, rc = PTR_ERR(child));
2953 /* The OST-object has been created. */
2954 if (unlikely(dt_object_exists(child) != 0))
2957 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
2958 ea_off, ost_idx, false);
2963 if (child != NULL && !IS_ERR(child))
2964 lfsck_object_put(env, child);
2966 if (parent != NULL && !IS_ERR(parent))
2967 lfsck_object_put(env, parent);
2970 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
2971 "dangling reference for: parent "DFID", child "
2972 DFID", ea_off %u, ost_idx %u, %s: rc = %d\n",
2973 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2975 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
2976 "Create the lost OST-object as required" :
2977 "Keep the MDT-object there by default", rc);
2982 /* If the OST-object does not recognize the MDT-object as its parent, and
2983 * there is no other MDT-object claims as its parent, then just trust the
2984 * given MDT-object as its parent. So update the OST-object filter_fid. */
2985 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2986 struct lfsck_component *com,
2987 struct dt_object *parent,
2988 struct lfsck_layout_req *llr,
2991 struct lfsck_thread_info *info = lfsck_env_info(env);
2992 struct filter_fid *pfid = &info->lti_new_pfid;
2993 struct dt_object *child = llr->llr_child;
2994 struct dt_device *dev = lfsck_obj2dev(child);
2995 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2996 struct thandle *handle;
2998 struct lustre_handle lh = { 0 };
3002 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3003 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3008 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3009 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3010 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3011 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3012 * parent MDT-object's layout EA. */
3013 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3014 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
3016 handle = dt_trans_create(env, dev);
3018 GOTO(unlock1, rc = PTR_ERR(handle));
3020 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3024 rc = dt_attr_get(env, parent, la);
3028 la->la_valid = LA_UID | LA_GID;
3029 rc = dt_declare_attr_set(env, child, la, handle);
3033 rc = dt_trans_start_local(env, dev, handle);
3037 dt_write_lock(env, parent, 0);
3038 if (unlikely(lfsck_is_dead_obj(parent)))
3039 GOTO(unlock2, rc = 1);
3041 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3045 /* Get the latest parent's owner. */
3046 rc = dt_attr_get(env, parent, la);
3050 la->la_valid = LA_UID | LA_GID;
3051 rc = dt_attr_set(env, child, la, handle);
3056 dt_write_unlock(env, parent);
3059 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3062 lfsck_ibits_unlock(&lh, LCK_EX);
3066 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3067 "unmatched MDT-OST pair for: parent "DFID
3068 ", child "DFID", OST-index %u, stripe-index %u, "
3069 "owner %u/%u: rc = %d\n",
3070 lfsck_lfsck2name(com->lc_lfsck),
3071 PFID(lfsck_dto2fid(parent)),
3072 PFID(lfsck_dto2fid(child)),
3073 llr->llr_ost_idx, llr->llr_lov_idx,
3074 la->la_uid, la->la_gid, rc);
3079 /* If there are more than one MDT-objects claim as the OST-object's parent,
3080 * and the OST-object only recognizes one of them, then we need to generate
3081 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3082 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3083 struct lfsck_component *com,
3084 struct dt_object *parent,
3085 struct lfsck_layout_req *llr,
3089 struct lfsck_thread_info *info = lfsck_env_info(env);
3090 struct dt_allocation_hint *hint = &info->lti_hint;
3091 struct dt_object_format *dof = &info->lti_dof;
3092 struct ost_id *oi = &info->lti_oi;
3093 struct lfsck_instance *lfsck = com->lc_lfsck;
3094 struct dt_device *dev;
3095 struct lu_device *d =
3096 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3097 struct lu_object *o;
3098 struct lu_object *n;
3099 struct dt_object *child = NULL;
3100 struct thandle *handle = NULL;
3101 struct lov_mds_md_v1 *lmm;
3102 struct lov_ost_data_v1 *objs;
3103 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3105 struct lustre_handle lh = { 0 };
3106 struct lu_buf ea_buf;
3112 /* We use two separated transactions to repair the inconsistency.
3114 * 1) create the child (OST-object).
3115 * 2) update the parent LOV EA according to the child's FID.
3117 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3118 * handled as orphan when the layout LFSCK run next time.
3120 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3121 * as dangling referened case when the layout LFSCK run next time. */
3123 /* The 1st transaction. */
3124 o = lu_object_anon(env, d, NULL);
3126 GOTO(log, rc = PTR_ERR(o));
3128 n = lu_object_locate(o->lo_header, d->ld_type);
3129 if (unlikely(n == NULL)) {
3130 lu_object_put_nocache(env, o);