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);
721 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
722 const struct lfsck_layout *src)
726 des->ll_magic = cpu_to_le32(src->ll_magic);
727 des->ll_status = cpu_to_le32(src->ll_status);
728 des->ll_flags = cpu_to_le32(src->ll_flags);
729 des->ll_success_count = cpu_to_le32(src->ll_success_count);
730 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
731 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
732 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
733 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
734 des->ll_time_last_checkpoint =
735 cpu_to_le64(src->ll_time_last_checkpoint);
736 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
737 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
738 des->ll_pos_first_inconsistent =
739 cpu_to_le64(src->ll_pos_first_inconsistent);
740 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
741 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
742 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
743 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
744 for (i = 0; i < LLIT_MAX; i++)
745 des->ll_objs_repaired[i] =
746 cpu_to_le64(src->ll_objs_repaired[i]);
747 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
748 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
752 * Load the OST bitmap from the lfsck_layout trace file.
754 * \param[in] env pointer to the thread context
755 * \param[in] com pointer to the lfsck component
757 * \retval 0 for success
758 * \retval negative error number on failure or data corruption
760 static int lfsck_layout_load_bitmap(const struct lu_env *env,
761 struct lfsck_component *com)
763 struct dt_object *obj = com->lc_obj;
764 struct lfsck_assistant_data *lad = com->lc_data;
765 struct lfsck_layout *lo = com->lc_file_ram;
766 struct cfs_bitmap *bitmap = lad->lad_bitmap;
767 loff_t pos = com->lc_file_size;
773 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
775 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
777 nbits = lo->ll_bitmap_size;
779 if (unlikely(nbits < BITS_PER_LONG))
780 nbits = BITS_PER_LONG;
782 if (nbits > bitmap->size) {
783 __u32 new_bits = bitmap->size;
784 struct cfs_bitmap *new_bitmap;
786 while (new_bits < nbits)
789 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
790 if (new_bitmap == NULL)
793 lad->lad_bitmap = new_bitmap;
794 CFS_FREE_BITMAP(bitmap);
798 if (lo->ll_bitmap_size == 0) {
799 lad->lad_incomplete = 0;
800 CFS_RESET_BITMAP(bitmap);
805 size = (lo->ll_bitmap_size + 7) >> 3;
806 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
808 RETURN(rc >= 0 ? -EINVAL : rc);
810 if (cfs_bitmap_check_empty(bitmap))
811 lad->lad_incomplete = 0;
813 lad->lad_incomplete = 1;
819 * Load the layout LFSCK trace file from disk.
821 * The layout LFSCK trace file records the layout LFSCK status information
822 * and other statistics, such as how many objects have been scanned, and how
823 * many objects have been repaired, and etc. It also contains the bitmap for
824 * failed OSTs during the layout LFSCK. All these information will be loaded
825 * from disk to RAM when the layout LFSCK component setup.
827 * \param[in] env pointer to the thread context
828 * \param[in] com pointer to the lfsck component
830 * \retval positive number for file data corruption, the caller
831 * should reset the layout LFSCK trace file
832 * \retval 0 for success
833 * \retval negative error number on failure
835 static int lfsck_layout_load(const struct lu_env *env,
836 struct lfsck_component *com)
838 struct lfsck_layout *lo = com->lc_file_ram;
839 ssize_t size = com->lc_file_size;
843 rc = dt_read(env, com->lc_obj,
844 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
848 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
849 lfsck_lfsck2name(com->lc_lfsck), rc);
851 } else if (rc != size) {
852 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
853 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
857 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
858 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
859 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
860 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
861 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
869 * Store the layout LFSCK trace file on disk.
871 * The layout LFSCK trace file records the layout LFSCK status information
872 * and other statistics, such as how many objects have been scanned, and how
873 * many objects have been repaired, and etc. It also contains the bitmap for
874 * failed OSTs during the layout LFSCK. All these information will be synced
875 * from RAM to disk periodically.
877 * \param[in] env pointer to the thread context
878 * \param[in] com pointer to the lfsck component
880 * \retval 0 for success
881 * \retval negative error number on failure
883 static int lfsck_layout_store(const struct lu_env *env,
884 struct lfsck_component *com)
886 struct dt_object *obj = com->lc_obj;
887 struct lfsck_instance *lfsck = com->lc_lfsck;
888 struct lfsck_layout *lo_ram = com->lc_file_ram;
889 struct lfsck_layout *lo = com->lc_file_disk;
891 struct dt_device *dev = lfsck_obj2dev(obj);
892 struct cfs_bitmap *bitmap = NULL;
894 ssize_t size = com->lc_file_size;
899 if (lfsck->li_master) {
900 struct lfsck_assistant_data *lad = com->lc_data;
902 bitmap = lad->lad_bitmap;
903 nbits = bitmap->size;
906 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
909 lo_ram->ll_bitmap_size = nbits;
910 lfsck_layout_cpu_to_le(lo, lo_ram);
911 th = dt_trans_create(env, dev);
913 GOTO(log, rc = PTR_ERR(th));
915 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
920 if (bitmap != NULL) {
921 rc = dt_declare_record_write(env, obj,
922 lfsck_buf_get(env, bitmap->data, nbits >> 3),
928 rc = dt_trans_start_local(env, dev, th);
933 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
937 if (bitmap != NULL) {
939 rc = dt_record_write(env, obj,
940 lfsck_buf_get(env, bitmap->data, nbits >> 3),
947 dt_trans_stop(env, dev, th);
951 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
952 lfsck_lfsck2name(lfsck), rc);
957 static int lfsck_layout_init(const struct lu_env *env,
958 struct lfsck_component *com)
960 struct lfsck_layout *lo = com->lc_file_ram;
963 memset(lo, 0, com->lc_file_size);
964 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
965 lo->ll_status = LS_INIT;
966 down_write(&com->lc_sem);
967 rc = lfsck_layout_store(env, com);
968 up_write(&com->lc_sem);
973 static int fid_is_for_ostobj(const struct lu_env *env,
974 struct lfsck_instance *lfsck,
975 struct dt_object *obj, const struct lu_fid *fid)
977 struct seq_server_site *ss = lfsck_dev_site(lfsck);
978 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
979 struct lustre_mdt_attrs *lma;
982 fld_range_set_any(range);
983 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
985 if (fld_range_is_ost(range))
991 lma = &lfsck_env_info(env)->lti_lma;
992 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
994 if (rc == sizeof(*lma)) {
995 lustre_lma_swab(lma);
997 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1000 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1005 static struct lfsck_layout_seq *
1006 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1008 struct lfsck_layout_seq *lls;
1010 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1011 if (lls->lls_seq == seq)
1014 if (lls->lls_seq > seq)
1022 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1023 struct lfsck_layout_seq *lls)
1025 struct lfsck_layout_seq *tmp;
1026 struct list_head *pos = &llsd->llsd_seq_list;
1028 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1029 if (lls->lls_seq < tmp->lls_seq) {
1030 pos = &tmp->lls_list;
1034 list_add_tail(&lls->lls_list, pos);
1038 lfsck_layout_lastid_create(const struct lu_env *env,
1039 struct lfsck_instance *lfsck,
1040 struct dt_object *obj)
1042 struct lfsck_thread_info *info = lfsck_env_info(env);
1043 struct lu_attr *la = &info->lti_la;
1044 struct dt_object_format *dof = &info->lti_dof;
1045 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1046 struct dt_device *dt = lfsck_obj2dev(obj);
1053 if (bk->lb_param & LPF_DRYRUN)
1056 memset(la, 0, sizeof(*la));
1057 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1058 la->la_valid = LA_MODE | LA_UID | LA_GID;
1059 memset(dof, 0, sizeof(*dof));
1060 dof->dof_type = dt_mode_to_dft(S_IFREG);
1062 th = dt_trans_create(env, dt);
1064 GOTO(log, rc = PTR_ERR(th));
1066 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1070 rc = dt_declare_record_write(env, obj,
1071 lfsck_buf_get(env, &lastid,
1077 rc = dt_trans_start_local(env, dt, th);
1081 dt_write_lock(env, obj, 0);
1082 if (likely(dt_object_exists(obj) == 0)) {
1083 rc = dt_create(env, obj, la, NULL, dof, th);
1085 rc = dt_record_write(env, obj,
1086 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1089 dt_write_unlock(env, obj);
1094 dt_trans_stop(env, dt, th);
1097 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1099 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1105 lfsck_layout_lastid_reload(const struct lu_env *env,
1106 struct lfsck_component *com,
1107 struct lfsck_layout_seq *lls)
1113 dt_read_lock(env, lls->lls_lastid_obj, 0);
1114 rc = dt_record_read(env, lls->lls_lastid_obj,
1115 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1116 dt_read_unlock(env, lls->lls_lastid_obj);
1117 if (unlikely(rc != 0))
1120 lastid = le64_to_cpu(lastid);
1121 if (lastid < lls->lls_lastid_known) {
1122 struct lfsck_instance *lfsck = com->lc_lfsck;
1123 struct lfsck_layout *lo = com->lc_file_ram;
1125 lls->lls_lastid = lls->lls_lastid_known;
1127 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1128 LASSERT(lfsck->li_out_notify != NULL);
1130 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1131 LE_LASTID_REBUILDING);
1132 lo->ll_flags |= LF_CRASHED_LASTID;
1134 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1135 "LAST_ID file (1) for the sequence %#llx"
1136 ", old value %llu, known value %llu\n",
1137 lfsck_lfsck2name(lfsck), lls->lls_seq,
1138 lastid, lls->lls_lastid);
1140 } else if (lastid >= lls->lls_lastid) {
1141 lls->lls_lastid = lastid;
1149 lfsck_layout_lastid_store(const struct lu_env *env,
1150 struct lfsck_component *com)
1152 struct lfsck_instance *lfsck = com->lc_lfsck;
1153 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1154 struct dt_device *dt = lfsck->li_bottom;
1155 struct lfsck_layout_slave_data *llsd = com->lc_data;
1156 struct lfsck_layout_seq *lls;
1162 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1165 if (!lls->lls_dirty)
1168 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1169 "<seq> %#llx as <oid> %llu\n",
1170 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1172 if (bk->lb_param & LPF_DRYRUN) {
1177 th = dt_trans_create(env, dt);
1180 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1181 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1182 lfsck_lfsck2name(com->lc_lfsck),
1187 lastid = cpu_to_le64(lls->lls_lastid);
1188 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1189 lfsck_buf_get(env, &lastid,
1195 rc = dt_trans_start_local(env, dt, th);
1199 dt_write_lock(env, lls->lls_lastid_obj, 0);
1200 rc = dt_record_write(env, lls->lls_lastid_obj,
1201 lfsck_buf_get(env, &lastid,
1202 sizeof(lastid)), &pos, th);
1203 dt_write_unlock(env, lls->lls_lastid_obj);
1208 dt_trans_stop(env, dt, th);
1211 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1212 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1213 lfsck_lfsck2name(com->lc_lfsck),
1222 lfsck_layout_lastid_load(const struct lu_env *env,
1223 struct lfsck_component *com,
1224 struct lfsck_layout_seq *lls)
1226 struct lfsck_instance *lfsck = com->lc_lfsck;
1227 struct lfsck_layout *lo = com->lc_file_ram;
1228 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1229 struct dt_object *obj;
1234 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1235 obj = dt_locate(env, lfsck->li_bottom, fid);
1237 RETURN(PTR_ERR(obj));
1239 /* LAST_ID crashed, to be rebuilt */
1240 if (dt_object_exists(obj) == 0) {
1241 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1242 LASSERT(lfsck->li_out_notify != NULL);
1244 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1245 LE_LASTID_REBUILDING);
1246 lo->ll_flags |= LF_CRASHED_LASTID;
1248 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1249 "LAST_ID file for sequence %#llx\n",
1250 lfsck_lfsck2name(lfsck), lls->lls_seq);
1252 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1254 struct l_wait_info lwi = LWI_TIMEOUT(
1255 cfs_time_seconds(cfs_fail_val),
1258 /* Some others may changed the cfs_fail_val
1259 * as zero after above check, re-check it for
1260 * sure to avoid falling into wait for ever. */
1261 if (likely(lwi.lwi_timeout > 0)) {
1262 struct ptlrpc_thread *thread =
1265 up_write(&com->lc_sem);
1266 l_wait_event(thread->t_ctl_waitq,
1267 !thread_is_running(thread),
1269 down_write(&com->lc_sem);
1274 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1276 dt_read_lock(env, obj, 0);
1277 rc = dt_read(env, obj,
1278 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1280 dt_read_unlock(env, obj);
1281 if (rc != 0 && rc != sizeof(__u64))
1282 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1284 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1285 LASSERT(lfsck->li_out_notify != NULL);
1287 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1288 LE_LASTID_REBUILDING);
1289 lo->ll_flags |= LF_CRASHED_LASTID;
1291 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1292 "LAST_ID file for the sequence %#llx"
1294 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1297 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1305 lfsck_object_put(env, obj);
1307 lls->lls_lastid_obj = obj;
1312 static void lfsck_layout_record_failure(const struct lu_env *env,
1313 struct lfsck_instance *lfsck,
1314 struct lfsck_layout *lo)
1318 lo->ll_objs_failed_phase1++;
1319 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1321 if (lo->ll_pos_first_inconsistent == 0 ||
1322 lo->ll_pos_first_inconsistent < cookie) {
1323 lo->ll_pos_first_inconsistent = cookie;
1325 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1326 "inconsistency at the pos [%llu]\n",
1327 lfsck_lfsck2name(lfsck),
1328 lo->ll_pos_first_inconsistent);
1332 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1333 struct lfsck_component *com,
1336 struct lfsck_instance *lfsck = com->lc_lfsck;
1337 struct lfsck_layout *lo = com->lc_file_ram;
1339 down_write(&com->lc_sem);
1340 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1341 HALF_SEC - com->lc_time_last_checkpoint);
1342 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1343 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1346 if (lo->ll_flags & LF_INCOMPLETE) {
1347 lo->ll_status = LS_PARTIAL;
1349 if (lfsck->li_master) {
1350 struct lfsck_assistant_data *lad = com->lc_data;
1352 if (lad->lad_incomplete)
1353 lo->ll_status = LS_PARTIAL;
1355 lo->ll_status = LS_COMPLETED;
1357 lo->ll_status = LS_COMPLETED;
1360 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1361 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1362 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1363 lo->ll_success_count++;
1364 } else if (rc == 0) {
1365 if (lfsck->li_status != 0)
1366 lo->ll_status = lfsck->li_status;
1368 lo->ll_status = LS_STOPPED;
1370 lo->ll_status = LS_FAILED;
1373 rc = lfsck_layout_store(env, com);
1374 up_write(&com->lc_sem);
1379 static int lfsck_layout_trans_stop(const struct lu_env *env,
1380 struct dt_device *dev,
1381 struct thandle *handle, int result)
1385 /* XXX: If there is something worng or it needs to repair nothing,
1386 * then notify the lower to stop the modification. Currently,
1387 * we use th_result for such purpose, that may be replaced by
1388 * some rollback mechanism in the future. */
1389 handle->th_result = result;
1390 rc = dt_trans_stop(env, dev, handle);
1392 return result > 0 ? 0 : result;
1394 return rc == 0 ? 1 : rc;
1398 * Get the system default stripe size.
1400 * \param[in] env pointer to the thread context
1401 * \param[in] lfsck pointer to the lfsck instance
1402 * \param[out] size pointer to the default stripe size
1404 * \retval 0 for success
1405 * \retval negative error number on failure
1407 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1408 struct lfsck_instance *lfsck,
1411 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1412 struct dt_object *root;
1415 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1417 return PTR_ERR(root);
1419 /* Get the default stripe size via xattr_get on the backend root. */
1420 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1423 /* The lum->lmm_stripe_size is LE mode. The *size also
1424 * should be LE mode. So it is unnecessary to convert. */
1425 *size = lum->lmm_stripe_size;
1427 } else if (unlikely(rc == 0)) {
1431 lfsck_object_put(env, root);
1437 * \retval +1: repaired
1438 * \retval 0: did nothing
1439 * \retval -ve: on error
1441 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1442 struct thandle *handle,
1443 struct dt_object *parent,
1444 struct lu_fid *cfid,
1446 struct lov_ost_data_v1 *slot,
1447 int fl, __u32 ost_idx)
1449 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1450 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1451 struct lu_buf ea_buf;
1457 magic = le32_to_cpu(lmm->lmm_magic);
1458 count = le16_to_cpu(lmm->lmm_stripe_count);
1460 fid_to_ostid(cfid, oi);
1461 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1462 slot->l_ost_gen = cpu_to_le32(0);
1463 slot->l_ost_idx = cpu_to_le32(ost_idx);
1465 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE) {
1466 struct lov_ost_data_v1 *objs;
1469 if (magic == LOV_MAGIC_V1)
1470 objs = &lmm->lmm_objects[0];
1472 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1473 for (i = 0; i < count; i++, objs++) {
1474 if (objs != slot && lovea_slot_is_dummy(objs))
1478 /* If the @slot is the last dummy slot to be refilled,
1479 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1481 lmm->lmm_pattern &= ~cpu_to_le32(LOV_PATTERN_F_HOLE);
1484 lfsck_buf_init(&ea_buf, lmm, lov_mds_md_size(count, magic));
1485 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1493 * \retval +1: repaired
1494 * \retval 0: did nothing
1495 * \retval -ve: on error
1497 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1498 struct lfsck_instance *lfsck,
1499 struct thandle *handle,
1500 struct dt_object *parent,
1501 struct lu_fid *cfid,
1502 struct lu_buf *buf, int fl,
1503 __u32 ost_idx, __u32 ea_off, bool reset)
1505 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1506 struct lov_ost_data_v1 *objs;
1512 if (fl == LU_XATTR_CREATE || reset) {
1513 __u32 pattern = LOV_PATTERN_RAID0;
1516 LASSERT(buf->lb_len >= lov_mds_md_size(count, LOV_MAGIC_V1));
1518 if (ea_off != 0 || reset) {
1519 pattern |= LOV_PATTERN_F_HOLE;
1523 memset(lmm, 0, buf->lb_len);
1524 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1525 lmm->lmm_pattern = cpu_to_le32(pattern);
1526 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1527 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1529 rc = lfsck_layout_get_def_stripesize(env, lfsck,
1530 &lmm->lmm_stripe_size);
1534 objs = &lmm->lmm_objects[ea_off];
1536 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1539 count = le16_to_cpu(lmm->lmm_stripe_count);
1540 if (magic == LOV_MAGIC_V1)
1541 objs = &lmm->lmm_objects[count];
1543 objs = &((struct lov_mds_md_v3 *)lmm)->
1546 gap = ea_off - count;
1549 LASSERT(buf->lb_len >= lov_mds_md_size(count, magic));
1552 memset(objs, 0, gap * sizeof(*objs));
1553 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1557 lmm->lmm_layout_gen =
1558 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1562 lmm->lmm_stripe_count = cpu_to_le16(count);
1563 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1566 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1567 DFID": parent "DFID", OST-index %u, stripe-index %u, fl %d, "
1568 "reset %s, %s LOV EA hole: rc = %d\n",
1569 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1570 ost_idx, ea_off, fl, reset ? "yes" : "no",
1571 hole ? "with" : "without", rc);
1576 static int __lfsck_layout_update_pfid(const struct lu_env *env,
1577 struct dt_object *child,
1578 const struct lu_fid *pfid, __u32 offset)
1580 struct dt_device *dev = lfsck_obj2dev(child);
1581 struct filter_fid *ff = &lfsck_env_info(env)->lti_new_pfid;
1582 struct thandle *handle;
1583 struct lu_buf buf = { NULL };
1586 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1587 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1588 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1589 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1590 * parent MDT-object's layout EA. */
1591 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
1592 lfsck_buf_init(&buf, ff, sizeof(struct filter_fid));
1594 handle = dt_trans_create(env, dev);
1596 RETURN(PTR_ERR(handle));
1598 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1602 rc = dt_trans_start_local(env, dev, handle);
1606 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1611 dt_trans_stop(env, dev, handle);
1617 * \retval +1: repaired
1618 * \retval 0: did nothing
1619 * \retval -ve: on error
1621 static int lfsck_layout_update_pfid(const struct lu_env *env,
1622 struct lfsck_component *com,
1623 struct dt_object *parent,
1624 struct lu_fid *cfid,
1625 struct dt_device *cdev, __u32 ea_off)
1627 struct dt_object *child;
1631 child = lfsck_object_find_by_dev(env, cdev, cfid);
1633 RETURN(PTR_ERR(child));
1635 rc = __lfsck_layout_update_pfid(env, child,
1636 lu_object_fid(&parent->do_lu), ea_off);
1637 lfsck_object_put(env, child);
1639 RETURN(rc == 0 ? 1 : rc);
1643 * This function will create the MDT-object with the given (partial) LOV EA.
1645 * Under some data corruption cases, the MDT-object of the file may be lost,
1646 * but its OST-objects, or some of them are there. The layout LFSCK needs to
1647 * re-create the MDT-object with the orphan OST-object(s) information.
1649 * On the other hand, the LFSCK may has created some OST-object for repairing
1650 * dangling LOV EA reference, but as the LFSCK processing, it may find that
1651 * the old OST-object is there and should replace the former new created OST
1652 * object. Unfortunately, some others have modified such newly created object.
1653 * To keep the data (both new and old), the LFSCK will create MDT-object with
1654 * new FID to reference the original OST-object.
1656 * \param[in] env pointer to the thread context
1657 * \param[in] com pointer to the lfsck component
1658 * \param[in] ltd pointer to target device descriptor
1659 * \param[in] rec pointer to the record for the orphan OST-object
1660 * \param[in] cfid pointer to FID for the orphan OST-object
1661 * \param[in] infix additional information, such as the FID for original
1662 * MDT-object and the stripe offset in the LOV EA
1663 * \param[in] type the type for describing why the orphan MDT-object is
1664 * created. The rules are as following:
1666 * type "C": Multiple OST-objects claim the same MDT-object and the
1667 * same slot in the layout EA. Then the LFSCK will create
1668 * new MDT-object(s) to hold the conflict OST-object(s).
1670 * type "N": The orphan OST-object does not know which one was the
1671 * real parent MDT-object, so the LFSCK uses new FID for
1672 * its parent MDT-object.
1674 * type "R": The orphan OST-object knows its parent MDT-object FID,
1675 * but does not know the position (the file name) in the
1678 * type "D": The MDT-object is a directory, it may knows its parent
1679 * but because there is no valid linkEA, the LFSCK cannot
1680 * know where to put it back to the namespace.
1681 * type "O": The MDT-object has no linkEA, and there is no name
1682 * entry that references the MDT-object.
1684 * type "P": The orphan object to be created was a parent directory
1685 * of some MDT-object which linkEA shows that the @orphan
1686 * object is missing.
1688 * The orphan name will be like:
1689 * ${FID}-${infix}-${type}-${conflict_version}
1691 * \param[in] ea_off the stripe offset in the LOV EA
1693 * \retval positive on repaired something
1694 * \retval 0 if needs to repair nothing
1695 * \retval negative error number on failure
1697 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1698 struct lfsck_component *com,
1699 struct lfsck_tgt_desc *ltd,
1700 struct lu_orphan_rec *rec,
1701 struct lu_fid *cfid,
1706 struct lfsck_thread_info *info = lfsck_env_info(env);
1707 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
1708 char *name = info->lti_key;
1709 struct lu_attr *la = &info->lti_la2;
1710 struct dt_object_format *dof = &info->lti_dof;
1711 struct lfsck_instance *lfsck = com->lc_lfsck;
1712 struct lu_fid *pfid = &rec->lor_fid;
1713 struct lu_fid *tfid = &info->lti_fid3;
1714 struct dt_device *dev = lfsck->li_bottom;
1715 struct dt_object *lpf = lfsck->li_lpf_obj;
1716 struct dt_object *pobj = NULL;
1717 struct dt_object *cobj = NULL;
1718 struct thandle *th = NULL;
1719 struct lu_buf *ea_buf = &info->lti_big_buf;
1720 struct lu_buf lov_buf;
1721 struct lfsck_lock_handle *llh = &info->lti_llh;
1722 struct linkea_data ldata = { NULL };
1723 struct lu_buf linkea_buf;
1724 const struct lu_name *pname;
1730 if (unlikely(lpf == NULL))
1731 GOTO(log, rc = -ENXIO);
1733 /* We use two separated transactions to repair the inconsistency.
1735 * 1) create the MDT-object locally.
1736 * 2) update the OST-object's PFID EA if necessary.
1738 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
1739 * updated when the layout LFSCK run next time.
1741 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
1742 * when the layout LFSCK run next time. */
1744 if (fid_is_zero(pfid)) {
1745 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1749 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1751 GOTO(log, rc = PTR_ERR(cobj));
1754 pobj = lfsck_object_find_by_dev(env, dev, pfid);
1756 GOTO(log, rc = PTR_ERR(pobj));
1758 LASSERT(infix != NULL);
1759 LASSERT(type != NULL);
1761 memset(la, 0, sizeof(*la));
1762 la->la_uid = rec->lor_uid;
1763 la->la_gid = rec->lor_gid;
1764 la->la_mode = S_IFREG | S_IRUSR;
1765 la->la_valid = LA_MODE | LA_UID | LA_GID;
1767 memset(dof, 0, sizeof(*dof));
1768 dof->dof_type = dt_mode_to_dft(S_IFREG);
1769 /* Because the dof->dof_reg.striped = 0, the LOD will not create
1770 * the stripe(s). The LFSCK will specify the LOV EA via
1771 * lfsck_layout_extend_lovea(). */
1773 size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1774 if (ea_buf->lb_len < size) {
1775 lu_buf_realloc(ea_buf, size);
1776 if (ea_buf->lb_buf == NULL)
1777 GOTO(log, rc = -ENOMEM);
1782 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
1784 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1785 (const struct dt_key *)name);
1786 if (rc != 0 && rc != -ENOENT)
1790 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
1791 MDS_INODELOCK_UPDATE, LCK_PW);
1795 /* Re-check whether the name conflict with othrs after taken
1797 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1798 (const struct dt_key *)name);
1799 if (unlikely(rc == 0)) {
1807 rc = linkea_data_new(&ldata,
1808 &lfsck_env_info(env)->lti_linkea_buf);
1812 pname = lfsck_name_get_const(env, name, strlen(name));
1813 rc = linkea_add_buf(&ldata, pname, lfsck_dto2fid(lfsck->li_lpf_obj));
1817 /* The 1st transaction. */
1818 th = dt_trans_create(env, dev);
1820 GOTO(unlock, rc = PTR_ERR(th));
1822 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1826 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
1827 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
1828 LU_XATTR_REPLACE, th);
1832 dtrec->rec_fid = pfid;
1833 dtrec->rec_type = S_IFREG;
1834 rc = dt_declare_insert(env, lpf,
1835 (const struct dt_rec *)dtrec,
1836 (const struct dt_key *)name, th);
1840 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1841 ldata.ld_leh->leh_len);
1842 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
1843 XATTR_NAME_LINK, 0, th);
1847 rc = dt_trans_start_local(env, dev, th);
1851 dt_write_lock(env, pobj, 0);
1852 rc = dt_create(env, pobj, la, NULL, dof, th);
1854 rc = lfsck_layout_extend_lovea(env, lfsck, th, pobj, cfid,
1855 &lov_buf, 0, ltd->ltd_index, ea_off, true);
1856 dt_write_unlock(env, pobj);
1860 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
1861 (const struct dt_key *)name, th, 1);
1865 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
1866 if (rc == 0 && cobj != NULL) {
1867 dt_trans_stop(env, dev, th);
1870 /* The 2nd transaction. */
1871 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ea_off);
1878 dt_trans_stop(env, dev, th);
1884 if (cobj != NULL && !IS_ERR(cobj))
1885 lfsck_object_put(env, cobj);
1886 if (pobj != NULL && !IS_ERR(pobj))
1887 lfsck_object_put(env, pobj);
1890 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
1891 "recreate the lost MDT-object: parent "DFID
1892 ", child "DFID", OST-index %u, stripe-index %u, "
1893 "infix %s, type %s: rc = %d\n",
1894 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
1895 ltd->ltd_index, ea_off, infix, type, rc);
1897 return rc >= 0 ? 1 : rc;
1900 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
1901 struct lfsck_component *com,
1902 const struct lu_fid *fid,
1905 struct lfsck_thread_info *info = lfsck_env_info(env);
1906 struct lfsck_request *lr = &info->lti_lr;
1907 struct lfsck_instance *lfsck = com->lc_lfsck;
1908 struct lfsck_tgt_desc *ltd;
1909 struct ptlrpc_request *req;
1910 struct lfsck_request *tmp;
1911 struct obd_export *exp;
1915 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
1916 if (unlikely(ltd == NULL))
1920 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
1921 GOTO(put, rc = -EOPNOTSUPP);
1923 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
1925 GOTO(put, rc = -ENOMEM);
1927 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1929 ptlrpc_request_free(req);
1934 memset(lr, 0, sizeof(*lr));
1935 lr->lr_event = LE_CONDITIONAL_DESTROY;
1936 lr->lr_active = LFSCK_TYPE_LAYOUT;
1939 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1941 ptlrpc_request_set_replen(req);
1943 rc = ptlrpc_queue_wait(req);
1944 ptlrpc_req_finished(req);
1954 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
1955 struct lfsck_component *com,
1956 struct lfsck_request *lr)
1958 struct lfsck_thread_info *info = lfsck_env_info(env);
1959 struct lu_attr *la = &info->lti_la;
1960 union ldlm_policy_data *policy = &info->lti_policy;
1961 struct ldlm_res_id *resid = &info->lti_resid;
1962 struct lfsck_instance *lfsck = com->lc_lfsck;
1963 struct dt_device *dev = lfsck->li_bottom;
1964 struct lu_fid *fid = &lr->lr_fid;
1965 struct dt_object *obj;
1966 struct thandle *th = NULL;
1967 struct lustre_handle lh = { 0 };
1972 obj = lfsck_object_find_by_dev(env, dev, fid);
1974 RETURN(PTR_ERR(obj));
1976 dt_read_lock(env, obj, 0);
1977 if (dt_object_exists(obj) == 0 ||
1978 lfsck_is_dead_obj(obj)) {
1979 dt_read_unlock(env, obj);
1981 GOTO(put, rc = -ENOENT);
1984 /* Get obj's attr without lock firstly. */
1985 rc = dt_attr_get(env, obj, la);
1986 dt_read_unlock(env, obj);
1990 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
1991 GOTO(put, rc = -ETXTBSY);
1993 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
1994 LASSERT(lfsck->li_namespace != NULL);
1996 memset(policy, 0, sizeof(*policy));
1997 policy->l_extent.end = OBD_OBJECT_EOF;
1998 ost_fid_build_resid(fid, resid);
1999 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2000 policy, LCK_EX, &flags, ldlm_blocking_ast,
2001 ldlm_completion_ast, NULL, NULL, 0,
2002 LVB_T_NONE, NULL, &lh);
2004 GOTO(put, rc = -EIO);
2006 dt_write_lock(env, obj, 0);
2007 /* Get obj's attr within lock again. */
2008 rc = dt_attr_get(env, obj, la);
2012 if (la->la_ctime != 0)
2013 GOTO(unlock, rc = -ETXTBSY);
2015 th = dt_trans_create(env, dev);
2017 GOTO(unlock, rc = PTR_ERR(th));
2019 rc = dt_declare_ref_del(env, obj, th);
2023 rc = dt_declare_destroy(env, obj, th);
2027 rc = dt_trans_start_local(env, dev, th);
2031 rc = dt_ref_del(env, obj, th);
2035 rc = dt_destroy(env, obj, th);
2037 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2038 "OST-object "DFID" that was created for reparing "
2039 "dangling referenced case. But the original missing "
2040 "OST-object is found now.\n",
2041 lfsck_lfsck2name(lfsck), PFID(fid));
2046 dt_trans_stop(env, dev, th);
2049 dt_write_unlock(env, obj);
2050 ldlm_lock_decref(&lh, LCK_EX);
2053 lfsck_object_put(env, obj);
2059 * Some OST-object has occupied the specified layout EA slot.
2060 * Such OST-object may be generated by the LFSCK when repair
2061 * dangling referenced MDT-object, which can be indicated by
2062 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2063 * is true and such OST-object has not been modified yet, we
2064 * will replace it with the orphan OST-object; otherwise the
2065 * LFSCK will create new MDT-object to reference the orphan.
2067 * \retval +1: repaired
2068 * \retval 0: did nothing
2069 * \retval -ve: on error
2071 static int lfsck_layout_conflict_create(const struct lu_env *env,
2072 struct lfsck_component *com,
2073 struct lfsck_tgt_desc *ltd,
2074 struct lu_orphan_rec *rec,
2075 struct dt_object *parent,
2076 struct lu_fid *cfid,
2077 struct lu_buf *ea_buf,
2078 struct lov_ost_data_v1 *slot,
2081 struct lfsck_thread_info *info = lfsck_env_info(env);
2082 struct lu_fid *cfid2 = &info->lti_fid2;
2083 struct ost_id *oi = &info->lti_oi;
2084 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2085 struct dt_device *dev = lfsck_obj2dev(parent);
2086 struct thandle *th = NULL;
2087 struct lustre_handle lh = { 0 };
2088 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2092 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2093 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2097 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2098 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2102 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2103 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2108 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2110 /* If the conflict OST-obejct is not created for fixing dangling
2111 * referenced MDT-object in former LFSCK check/repair, or it has
2112 * been modified by others, then we cannot destroy it. Re-create
2113 * a new MDT-object for the orphan OST-object. */
2114 if (rc == -ETXTBSY) {
2115 /* No need the layout lock on the original parent. */
2116 lfsck_ibits_unlock(&lh, LCK_EX);
2118 fid_zero(&rec->lor_fid);
2119 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2120 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2122 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2123 info->lti_tmpbuf, "C", ea_off);
2128 if (rc != 0 && rc != -ENOENT)
2131 th = dt_trans_create(env, dev);
2133 GOTO(unlock, rc = PTR_ERR(th));
2135 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2136 LU_XATTR_REPLACE, th);
2140 rc = dt_trans_start_local(env, dev, th);
2144 dt_write_lock(env, parent, 0);
2145 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2146 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2147 LU_XATTR_REPLACE, ltd->ltd_index);
2148 dt_write_unlock(env, parent);
2153 dt_trans_stop(env, dev, th);
2156 lfsck_ibits_unlock(&lh, LCK_EX);
2159 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2160 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2161 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2162 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2163 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2166 return rc >= 0 ? 1 : rc;
2170 * \retval +1: repaired
2171 * \retval 0: did nothing
2172 * \retval -ve: on error
2174 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2175 struct lfsck_component *com,
2176 struct lfsck_tgt_desc *ltd,
2177 struct lu_orphan_rec *rec,
2178 struct dt_object *parent,
2179 struct lu_fid *cfid,
2180 __u32 ost_idx, __u32 ea_off)
2182 struct lfsck_thread_info *info = lfsck_env_info(env);
2183 struct lu_buf *buf = &info->lti_big_buf;
2184 struct lu_fid *fid = &info->lti_fid2;
2185 struct ost_id *oi = &info->lti_oi;
2186 struct lfsck_instance *lfsck = com->lc_lfsck;
2187 struct dt_device *dt = lfsck_obj2dev(parent);
2188 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2189 struct thandle *handle = NULL;
2191 struct lov_mds_md_v1 *lmm;
2192 struct lov_ost_data_v1 *objs;
2193 struct lustre_handle lh = { 0 };
2200 bool locked = false;
2203 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2204 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2207 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2208 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2209 "stripe-index %u: rc = %d\n",
2210 lfsck_lfsck2name(lfsck), PFID(cfid),
2211 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off, rc);
2218 dt_write_unlock(env, parent);
2222 if (handle != NULL) {
2223 dt_trans_stop(env, dt, handle);
2228 GOTO(unlock_layout, rc);
2231 if (buf->lb_len < lovea_size) {
2232 lu_buf_realloc(buf, lovea_size);
2233 if (buf->lb_buf == NULL)
2234 GOTO(unlock_layout, rc = -ENOMEM);
2237 if (!(bk->lb_param & LPF_DRYRUN)) {
2238 handle = dt_trans_create(env, dt);
2240 GOTO(unlock_layout, rc = PTR_ERR(handle));
2242 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2247 rc = dt_trans_start_local(env, dt, handle);
2252 dt_write_lock(env, parent, 0);
2254 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2255 if (rc == -ERANGE) {
2256 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2259 } else if (rc == -ENODATA || rc == 0) {
2260 lovea_size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2261 /* If the declared is not big enough, re-try. */
2262 if (buf->lb_len < lovea_size) {
2266 fl = LU_XATTR_CREATE;
2267 } else if (rc < 0) {
2268 GOTO(unlock_parent, rc);
2269 } else if (unlikely(buf->lb_len == 0)) {
2272 fl = LU_XATTR_REPLACE;
2276 if (fl == LU_XATTR_CREATE) {
2277 if (bk->lb_param & LPF_DRYRUN)
2278 GOTO(unlock_parent, rc = 1);
2280 LASSERT(buf->lb_len >= lovea_size);
2282 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2283 buf, fl, ost_idx, ea_off, false);
2285 GOTO(unlock_parent, rc);
2289 rc1 = lfsck_layout_verify_header(lmm);
2291 /* If the LOV EA crashed, the rebuild it. */
2292 if (rc1 == -EINVAL) {
2293 if (bk->lb_param & LPF_DRYRUN)
2294 GOTO(unlock_parent, rc = 1);
2296 LASSERT(buf->lb_len >= lovea_size);
2298 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2299 buf, fl, ost_idx, ea_off, true);
2301 GOTO(unlock_parent, rc);
2304 /* For other unknown magic/pattern, keep the current LOV EA. */
2306 GOTO(unlock_parent, rc = rc1);
2308 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2309 * been verified in lfsck_layout_verify_header() already. If some
2310 * new magic introduced in the future, then layout LFSCK needs to
2311 * be updated also. */
2312 magic = le32_to_cpu(lmm->lmm_magic);
2313 if (magic == LOV_MAGIC_V1) {
2314 objs = &lmm->lmm_objects[0];
2316 LASSERT(magic == LOV_MAGIC_V3);
2317 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2320 count = le16_to_cpu(lmm->lmm_stripe_count);
2322 GOTO(unlock_parent, rc = -EINVAL);
2325 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2326 if (count <= ea_off) {
2327 if (bk->lb_param & LPF_DRYRUN)
2328 GOTO(unlock_parent, rc = 1);
2330 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2331 /* If the declared is not big enough, re-try. */
2332 if (buf->lb_len < lovea_size) {
2337 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2338 buf, fl, ost_idx, ea_off, false);
2340 GOTO(unlock_parent, rc);
2343 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2345 for (i = 0; i < count; i++, objs++) {
2346 /* The MDT-object was created via lfsck_layout_recover_create()
2347 * by others before, and we fill the dummy layout EA. */
2348 if (lovea_slot_is_dummy(objs)) {
2352 if (bk->lb_param & LPF_DRYRUN)
2353 GOTO(unlock_parent, rc = 1);
2355 lmm->lmm_layout_gen =
2356 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2357 rc = lfsck_layout_refill_lovea(env, handle, parent,
2358 cfid, buf, objs, fl,
2361 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2362 "dummy layout slot for "DFID": parent "DFID
2363 ", OST-index %u, stripe-index %u: rc = %d\n",
2364 lfsck_lfsck2name(lfsck), PFID(cfid),
2365 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2367 GOTO(unlock_parent, rc);
2370 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2371 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2373 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2374 "invalid layout EA at the slot %d, index %u\n",
2375 lfsck_lfsck2name(lfsck),
2376 PFID(lfsck_dto2fid(parent)), i,
2377 le32_to_cpu(objs->l_ost_idx));
2379 GOTO(unlock_parent, rc);
2382 /* It should be rare case, the slot is there, but the LFSCK
2383 * does not handle it during the first-phase cycle scanning. */
2384 if (unlikely(lu_fid_eq(fid, cfid))) {
2386 GOTO(unlock_parent, rc = 0);
2388 /* Rare case that the OST-object index
2389 * does not match the parent MDT-object
2390 * layout EA. We trust the later one. */
2391 if (bk->lb_param & LPF_DRYRUN)
2392 GOTO(unlock_parent, rc = 1);
2394 dt_write_unlock(env, parent);
2396 dt_trans_stop(env, dt, handle);
2397 lfsck_ibits_unlock(&lh, LCK_EX);
2398 rc = lfsck_layout_update_pfid(env, com, parent,
2399 cfid, ltd->ltd_tgt, i);
2401 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2402 "updated OST-object's pfid for "DFID
2403 ": parent "DFID", OST-index %u, "
2404 "stripe-index %u: rc = %d\n",
2405 lfsck_lfsck2name(lfsck), PFID(cfid),
2406 PFID(lfsck_dto2fid(parent)),
2407 ltd->ltd_index, i, rc);
2414 /* The MDT-object exists, but related layout EA slot is occupied
2416 if (bk->lb_param & LPF_DRYRUN)
2417 GOTO(unlock_parent, rc = 1);
2419 dt_write_unlock(env, parent);
2421 dt_trans_stop(env, dt, handle);
2422 lfsck_ibits_unlock(&lh, LCK_EX);
2423 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2424 objs = &lmm->lmm_objects[ea_off];
2426 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2427 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2434 dt_write_unlock(env, parent);
2438 dt_trans_stop(env, dt, handle);
2441 lfsck_ibits_unlock(&lh, LCK_EX);
2446 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2447 struct lfsck_component *com,
2448 struct lfsck_tgt_desc *ltd,
2449 struct lu_orphan_rec *rec,
2450 struct lu_fid *cfid)
2452 struct lfsck_layout *lo = com->lc_file_ram;
2453 struct lu_fid *pfid = &rec->lor_fid;
2454 struct dt_object *parent = NULL;
2455 __u32 ea_off = pfid->f_stripe_idx;
2459 if (!fid_is_sane(cfid))
2460 GOTO(out, rc = -EINVAL);
2462 if (fid_is_zero(pfid)) {
2463 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2469 if (!fid_is_sane(pfid))
2470 GOTO(out, rc = -EINVAL);
2472 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2474 GOTO(out, rc = PTR_ERR(parent));
2476 if (unlikely(dt_object_remote(parent) != 0))
2477 GOTO(put, rc = -EXDEV);
2479 if (dt_object_exists(parent) == 0) {
2480 lfsck_object_put(env, parent);
2481 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2486 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2487 GOTO(put, rc = -EISDIR);
2489 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2490 ltd->ltd_index, ea_off);
2496 lfsck_object_put(env, parent);
2498 /* The layout EA is changed, need to be reloaded next time. */
2499 lu_object_put_nocache(env, &parent->do_lu);
2502 down_write(&com->lc_sem);
2503 com->lc_new_scanned++;
2504 com->lc_new_checked++;
2506 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2508 } else if (rc < 0) {
2509 lo->ll_objs_failed_phase2++;
2511 up_write(&com->lc_sem);
2516 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2517 struct lfsck_component *com,
2518 struct lfsck_tgt_desc *ltd)
2520 struct lfsck_assistant_data *lad = com->lc_data;
2521 struct lfsck_instance *lfsck = com->lc_lfsck;
2522 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2523 struct lfsck_thread_info *info = lfsck_env_info(env);
2524 struct ost_id *oi = &info->lti_oi;
2525 struct lu_fid *fid = &info->lti_fid;
2526 struct dt_object *obj;
2527 const struct dt_it_ops *iops;
2532 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
2533 "scanning for OST%04x\n",
2534 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2536 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
2537 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
2538 "scanning for OST%04x\n",
2539 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2544 ostid_set_seq(oi, FID_SEQ_IDIF);
2545 ostid_set_id(oi, 0);
2546 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
2550 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2551 if (unlikely(IS_ERR(obj)))
2552 GOTO(log, rc = PTR_ERR(obj));
2554 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_orphan_features);
2558 iops = &obj->do_index_ops->dio_it;
2559 di = iops->init(env, obj, 0);
2561 GOTO(put, rc = PTR_ERR(di));
2563 rc = iops->load(env, di, 0);
2565 /* -ESRCH means that the orphan OST-objects rbtree has been
2566 * cleanup because of the OSS server restart or other errors. */
2567 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
2572 rc = iops->next(env, di);
2584 struct lu_orphan_rec *rec = &info->lti_rec;
2586 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
2587 unlikely(!thread_is_running(&lfsck->li_thread)))
2590 key = iops->key(env, di);
2591 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2592 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2594 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2595 &com->lc_fid_latest_scanned_phase2);
2596 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2599 lfsck_control_speed_by_self(com);
2601 rc = iops->next(env, di);
2602 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2609 iops->fini(env, di);
2611 lfsck_object_put(env, obj);
2614 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
2615 "scanning for OST%04x: rc = %d\n",
2616 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2618 return rc > 0 ? 0 : rc;
2621 /* For the MDT-object with dangling reference, we need to repare the
2622 * inconsistency according to the LFSCK sponsor's requirement:
2624 * 1) Keep the inconsistency there and report the inconsistency case,
2625 * then give the chance to the application to find related issues,
2626 * and the users can make the decision about how to handle it with
2627 * more human knownledge. (by default)
2629 * 2) Re-create the missing OST-object with the FID/owner information. */
2630 static int lfsck_layout_repair_dangling(const struct lu_env *env,
2631 struct lfsck_component *com,
2632 struct dt_object *parent,
2633 struct lfsck_layout_req *llr,
2636 struct lfsck_thread_info *info = lfsck_env_info(env);
2637 struct filter_fid *pfid = &info->lti_new_pfid;
2638 struct dt_object_format *dof = &info->lti_dof;
2639 struct dt_object *child = llr->llr_child;
2640 struct dt_device *dev = lfsck_obj2dev(child);
2641 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2642 struct thandle *handle;
2644 struct lustre_handle lh = { 0 };
2649 if (com->lc_lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ)
2657 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2658 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2663 rc = dt_attr_get(env, parent, la);
2667 la->la_mode = S_IFREG | 0666;
2668 la->la_atime = la->la_mtime = la->la_ctime = 0;
2669 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
2670 LA_ATIME | LA_MTIME | LA_CTIME;
2671 memset(dof, 0, sizeof(*dof));
2672 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2673 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2674 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2675 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2676 * parent MDT-object's layout EA. */
2677 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2678 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2680 handle = dt_trans_create(env, dev);
2682 GOTO(unlock1, rc = PTR_ERR(handle));
2684 rc = dt_declare_create(env, child, la, NULL, dof, handle);
2688 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2689 LU_XATTR_CREATE, handle);
2693 rc = dt_trans_start_local(env, dev, handle);
2697 dt_read_lock(env, parent, 0);
2698 if (unlikely(lfsck_is_dead_obj(parent)))
2699 GOTO(unlock2, rc = 1);
2701 rc = dt_create(env, child, la, NULL, dof, handle);
2705 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2711 dt_read_unlock(env, parent);
2714 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2717 lfsck_ibits_unlock(&lh, LCK_EX);
2721 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
2722 "dangling reference for: parent "DFID", child "DFID
2723 ", OST-index %u, stripe-index %u, owner %u/%u. %s: "
2724 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2725 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2726 llr->llr_ost_idx, llr->llr_lov_idx,
2727 la->la_uid, la->la_gid,
2728 create ? "Create the lost OST-object as required" :
2729 "Keep the MDT-object there by default", rc);
2734 /* If the OST-object does not recognize the MDT-object as its parent, and
2735 * there is no other MDT-object claims as its parent, then just trust the
2736 * given MDT-object as its parent. So update the OST-object filter_fid. */
2737 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2738 struct lfsck_component *com,
2739 struct dt_object *parent,
2740 struct lfsck_layout_req *llr,
2743 struct lfsck_thread_info *info = lfsck_env_info(env);
2744 struct filter_fid *pfid = &info->lti_new_pfid;
2745 struct dt_object *child = llr->llr_child;
2746 struct dt_device *dev = lfsck_obj2dev(child);
2747 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2748 struct thandle *handle;
2750 struct lustre_handle lh = { 0 };
2754 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2755 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2760 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2761 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2762 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2763 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2764 * parent MDT-object's layout EA. */
2765 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2766 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2768 handle = dt_trans_create(env, dev);
2770 GOTO(unlock1, rc = PTR_ERR(handle));
2772 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2776 rc = dt_attr_get(env, parent, la);
2780 la->la_valid = LA_UID | LA_GID;
2781 rc = dt_declare_attr_set(env, child, la, handle);
2785 rc = dt_trans_start_local(env, dev, handle);
2789 dt_write_lock(env, parent, 0);
2790 if (unlikely(lfsck_is_dead_obj(parent)))
2791 GOTO(unlock2, rc = 1);
2793 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2797 /* Get the latest parent's owner. */
2798 rc = dt_attr_get(env, parent, la);
2802 la->la_valid = LA_UID | LA_GID;
2803 rc = dt_attr_set(env, child, la, handle);
2808 dt_write_unlock(env, parent);
2811 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2814 lfsck_ibits_unlock(&lh, LCK_EX);
2818 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
2819 "unmatched MDT-OST pair for: parent "DFID
2820 ", child "DFID", OST-index %u, stripe-index %u, "
2821 "owner %u/%u: rc = %d\n",
2822 lfsck_lfsck2name(com->lc_lfsck),
2823 PFID(lfsck_dto2fid(parent)),
2824 PFID(lfsck_dto2fid(child)),
2825 llr->llr_ost_idx, llr->llr_lov_idx,
2826 la->la_uid, la->la_gid, rc);
2831 /* If there are more than one MDT-objects claim as the OST-object's parent,
2832 * and the OST-object only recognizes one of them, then we need to generate
2833 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
2834 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
2835 struct lfsck_component *com,
2836 struct dt_object *parent,
2837 struct lfsck_layout_req *llr,
2841 struct lfsck_thread_info *info = lfsck_env_info(env);
2842 struct dt_allocation_hint *hint = &info->lti_hint;
2843 struct dt_object_format *dof = &info->lti_dof;
2844 struct ost_id *oi = &info->lti_oi;
2845 struct lfsck_instance *lfsck = com->lc_lfsck;
2846 struct dt_device *dev;
2847 struct lu_device *d =
2848 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
2849 struct lu_object *o;
2850 struct lu_object *n;
2851 struct dt_object *child = NULL;
2852 struct thandle *handle = NULL;
2853 struct lov_mds_md_v1 *lmm;
2854 struct lov_ost_data_v1 *objs;
2855 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2857 struct lustre_handle lh = { 0 };
2858 struct lu_buf ea_buf;
2864 /* We use two separated transactions to repair the inconsistency.
2866 * 1) create the child (OST-object).
2867 * 2) update the parent LOV EA according to the child's FID.
2869 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
2870 * handled as orphan when the layout LFSCK run next time.
2872 * If 1) failed, but 2) succeed, then such OST-object will be re-created
2873 * as dangling referened case when the layout LFSCK run next time. */
2875 /* The 1st transaction. */
2876 o = lu_object_anon(env, d, NULL);
2878 GOTO(log, rc = PTR_ERR(o));
2880 n = lu_object_locate(o->lo_header, d->ld_type);
2881 if (unlikely(n == NULL)) {
2882 lu_object_put_nocache(env, o);
2884 GOTO(log, rc = -EINVAL);
2887 child = container_of(n, struct dt_object, do_lu);
2888 memset(hint, 0, sizeof(*hint));
2889 rc = dt_attr_get(env, parent, la);
2893 la->la_valid = LA_UID | LA_GID;
2894 memset(dof, 0, sizeof(*dof));
2896 dev = lfsck_obj2dev(child);
2897 handle = dt_trans_create(env, dev);
2899 GOTO(log, rc = PTR_ERR(handle));
2901 rc = dt_declare_create(env, child, la, hint, dof, handle);
2905 rc = dt_trans_start_local(env, dev, handle);
2909 rc = dt_create(env, child, la, hint, dof, handle);
2910 dt_trans_stop(env, dev, handle);
2915 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2916 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2921 /* The 2nd transaction. */
2923 /* XXX: Generally, we should use bottom device (OSD) to update parent
2924 * LOV EA. But because the LOD-object still references the wrong
2925 * OSP-object that should be detached after the parent's LOV EA
2926 * refreshed. Unfortunately, there is no suitable API for that.
2927 * So we have to make the LOD to re-load the OSP-object(s) via
2928 * replacing the LOV EA against the LOD-object.
2930 * Once the DNE2 patches have been landed, we can replace the
2931 * LOD device with the OSD device. LU-6230. */
2933 dev = lfsck->li_next;
2934 parent = lfsck_object_locate(dev, parent);
2936 GOTO(log, rc = PTR_ERR(parent));
2938 handle = dt_trans_create(env, dev);
2940 GOTO(log, rc = PTR_ERR(handle));
2942 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2943 LU_XATTR_REPLACE, handle);
2947 rc = dt_trans_start_local(env, dev, handle);
2951 dt_write_lock(env, parent, 0);
2952 if (unlikely(lfsck_is_dead_obj(parent)))
2953 GOTO(unlock, rc = 0);
2955 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2956 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2957 GOTO(unlock, rc = 0);
2960 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2961 * been verified in lfsck_layout_verify_header() already. If some
2962 * new magic introduced in the future, then layout LFSCK needs to
2963 * be updated also. */
2964 magic = le32_to_cpu(lmm->lmm_magic);
2965 if (magic == LOV_MAGIC_V1) {
2966 objs = &lmm->lmm_objects[llr->llr_lov_idx];
2968 LASSERT(magic == LOV_MAGIC_V3);
2970 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
2973 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2974 index = le32_to_cpu(objs->l_ost_idx);
2975 rc = ostid_to_fid(&tfid, oi, index);
2976 /* Someone changed layout during the LFSCK, no need to repair then. */
2977 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
2978 GOTO(unlock, rc = 0);
2980 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2981 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
2982 ostid_cpu_to_le(oi, &objs->l_ost_oi);
2983 objs->l_ost_gen = cpu_to_le32(0);
2984 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
2985 lfsck_buf_init(&ea_buf, lmm,
2986 lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2988 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV,
2989 LU_XATTR_REPLACE, handle);
2991 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2994 dt_write_unlock(env, parent);
2998 dt_trans_stop(env, dev, handle);
3001 lfsck_ibits_unlock(&lh, LCK_EX);
3003 lfsck_object_put(env, child);
3006 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3007 "multiple references for: parent "DFID", OST-index %u, "
3008 "stripe-index %u, owner %u/%u: rc = %d\n",
3009 lfsck_lfsck2name(lfsck), PFID(pfid), llr->llr_ost_idx,
3010 llr->llr_lov_idx, la->la_uid, la->la_gid, rc);
3015 /* If the MDT-object and the OST-object have different owner information,
3016 * then trust the MDT-object, because the normal chown/chgrp handle order
3017 * is from MDT to OST, and it is possible that some chown/chgrp operation
3018 * is partly done. */
3019 static int lfsck_layout_repair_owner(const struct lu_env *env,
3020 struct lfsck_component *com,
3021 struct dt_object *parent,
3022 struct lfsck_layout_req *llr,
3023 struct lu_attr *pla,
3024 const struct lu_attr *cla)
3026 struct lfsck_thread_info *info = lfsck_env_info(env);
3027 struct lu_attr *tla = &info->lti_la2;
3028 struct dt_object *child = llr->llr_child;
3029 struct dt_device *dev = lfsck_obj2dev(child);
3030 struct thandle *handle;
3034 tla->la_uid = pla->la_uid;
3035 tla->la_gid = pla->la_gid;
3036 tla->la_valid = LA_UID | LA_GID;
3037 handle = dt_trans_create(env, dev);
3039 GOTO(log, rc = PTR_ERR(handle));
3041 rc = dt_declare_attr_set(env, child, tla, handle);
3045 rc = dt_trans_start_local(env, dev, handle);
3049 /* Use the dt_object lock to serialize with destroy and attr_set. */
3050 dt_read_lock(env, parent, 0);
3051 if (unlikely(lfsck_is_dead_obj(parent)))
3052 GOTO(unlock, rc = 1);
3054 /* Get the latest parent's owner. */
3055 rc = dt_attr_get(env, parent, pla);
3059 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3060 if (unlikely(tla->la_uid != pla->la_uid ||
3061 tla->la_gid != pla->la_gid))
3064 rc = dt_attr_set(env, child, tla, handle);
3069 dt_read_unlock(env, parent);
3072 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3076 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3077 "inconsistent file owner for: parent "DFID", child "DFID
3078 ", OST-index %u, stripe-index %u, old owner %u/%u, "
3079 "new owner %u/%u: rc = %d\n",
3080 lfsck_lfsck2name(com->lc_lfsck),
3081 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
3082 llr->llr_ost_idx, llr->llr_lov_idx,
3083 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
3088 /* Check whether the OST-object correctly back points to the
3089 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3090 static int lfsck_layout_check_parent(const struct lu_env *env,
3091 struct lfsck_component *com,
3092 struct lfsck_assistant_object *lso,
3093 const struct lu_fid *pfid,
3094 const struct lu_fid *cfid,
3095 const struct lu_attr *cla,
3096 struct lfsck_layout_req *llr,
3097 struct lu_buf *lov_ea, __u32 idx)
3099 struct lfsck_thread_info *info = lfsck_env_info(env);
3100 struct lu_buf *buf = &info->lti_big_buf;
3101 struct dt_object *tobj;
3102 struct lov_mds_md_v1 *lmm;
3103 struct lov_ost_data_v1 *objs;
3104 struct lustre_handle lh = { 0 };
3111 if (unlikely(!fid_is_sane(pfid)))
3112 RETURN(LLIT_UNMATCHED_PAIR);
3114 if (lu_fid_eq(pfid, &lso->lso_fid)) {
3115 if (likely(llr->llr_lov_idx == idx))
3118 RETURN(LLIT_UNMATCHED_PAIR);
3121 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3123 RETURN(PTR_ERR(tobj));
3125 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
3126 !S_ISREG(lfsck_object_type(tobj)))
3127 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3129 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3130 * remote one on another MDT. Then check whether the given OST-object
3131 * is in such layout. If yes, it is multiple referenced, otherwise it
3132 * is unmatched referenced case. */
3133 rc = lfsck_layout_get_lovea(env, tobj, buf);
3134 if (rc == 0 || rc == -ENOENT)
3135 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3141 magic = le32_to_cpu(lmm->lmm_magic);
3142 if (magic == LOV_MAGIC_V1) {
3143 objs = &lmm->lmm_objects[0];
3145 LASSERT(magic == LOV_MAGIC_V3);
3146 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3149 count = le16_to_cpu(lmm->lmm_stripe_count);
3150 for (i = 0; i < count; i++, objs++) {
3151 struct lu_fid *tfid = &info->lti_fid2;
3152 struct ost_id *oi = &info->lti_oi;
3155 if (lovea_slot_is_dummy(objs))
3158 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3159 idx2 = le32_to_cpu(objs->l_ost_idx);
3160 rc = ostid_to_fid(tfid, oi, idx2);
3162 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3163 "invalid layout EA at the slot %d, index %u\n",
3164 lfsck_lfsck2name(com->lc_lfsck),
3165 PFID(pfid), i, idx2);
3167 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3170 if (lu_fid_eq(cfid, tfid)) {
3171 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
3172 MDS_INODELOCK_UPDATE |
3173 MDS_INODELOCK_LAYOUT |
3174 MDS_INODELOCK_XATTR,
3179 dt_read_lock(env, tobj, 0);
3181 /* For local MDT-object, re-check existence
3182 * after taken the lock. */
3183 if (!dt_object_remote(tobj)) {
3184 if (dt_object_exists(tobj) == 0 ||
3185 lfsck_is_dead_obj(tobj)) {
3186 rc = LLIT_UNMATCHED_PAIR;
3189 rc = LLIT_MULTIPLE_REFERENCED;
3195 /* For migration case, the new MDT-object and old
3196 * MDT-object may reference the same OST-object at
3197 * some migration internal time.
3199 * For remote MDT-object, the local MDT may not know
3200 * whether it has been removed or not. Try checking
3201 * for a non-existent xattr to check if this object
3202 * has been been removed or not. */
3203 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
3205 if (unlikely(rc == -ENOENT || rc >= 0)) {
3206 rc = LLIT_UNMATCHED_PAIR;
3207 } else if (rc == -ENODATA) {
3209 rc = LLIT_MULTIPLE_REFERENCED;
3216 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3219 if (lustre_handle_is_used(&lh)) {
3220 dt_read_unlock(env, tobj);
3221 lfsck_ibits_unlock(&lh, LCK_EX);
3225 lfsck_object_put(env, tobj);
3230 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
3231 struct lfsck_component *com,
3232 struct lfsck_assistant_req *lar)
3234 struct lfsck_layout_req *llr =
3235 container_of0(lar, struct lfsck_layout_req, llr_lar);
3236 struct lfsck_assistant_object *lso = lar->lar_parent;
3237 struct lfsck_layout *lo = com->lc_file_ram;
3238 struct lfsck_thread_info *info = lfsck_env_info(env);
3239 struct filter_fid_old *pea = &info->lti_old_pfid;
3240 struct lu_fid *pfid = &info->lti_fid;
3241 struct lu_buf buf = { NULL };
3242 struct dt_object *parent = NULL;
3243 struct dt_object *child = llr->llr_child;
3244 struct lu_attr *pla = &lso->lso_attr;
3245 struct lu_attr *cla = &info->lti_la;
3246 struct lfsck_instance *lfsck = com->lc_lfsck;
3247 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3248 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
3256 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ASSISTANT_DIRECT, cfs_fail_val);
3258 rc = dt_attr_get(env, child, cla);
3259 if (rc == -ENOENT) {
3260 parent = lfsck_assistant_object_load(env, lfsck, lso);
3261 if (IS_ERR(parent)) {
3262 rc = PTR_ERR(parent);
3264 RETURN(rc == -ENOENT ? 0 : rc);
3267 type = LLIT_DANGLING;
3274 lfsck_buf_init(&buf, pea, sizeof(struct filter_fid_old));
3275 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
3276 if (unlikely(rc > 0 && rc != sizeof(struct filter_fid_old) &&
3277 rc != sizeof(struct filter_fid))) {
3278 type = LLIT_UNMATCHED_PAIR;
3282 if (rc < 0 && rc != -ENODATA)
3285 if (rc == 0 || rc == -ENODATA)
3286 GOTO(check_owner, rc = 0);
3288 fid_le_to_cpu(pfid, &pea->ff_parent);
3289 /* Currently, the filter_fid::ff_parent::f_ver is not the
3290 * real parent MDT-object's FID::f_ver, instead it is the
3291 * OST-object index in its parent MDT-object's layout EA. */
3292 idx = pfid->f_stripe_idx;
3294 rc = lfsck_layout_check_parent(env, com, lso, pfid,
3295 lu_object_fid(&child->do_lu),
3296 cla, llr, &buf, idx);
3306 /* Someone may has changed the owner after the parent attr pre-loaded.
3307 * It can be handled later inside the lfsck_layout_repair_owner(). */
3308 if (unlikely(cla->la_uid != pla->la_uid ||
3309 cla->la_gid != pla->la_gid)) {
3310 type = LLIT_INCONSISTENT_OWNER;
3315 if (type == LLIT_NONE)
3318 if (bk->lb_param & LPF_DRYRUN)
3321 if (parent == NULL) {
3322 parent = lfsck_assistant_object_load(env, lfsck, lso);
3323 if (IS_ERR(parent)) {
3324 rc = PTR_ERR(parent);
3335 rc = lfsck_layout_repair_dangling(env, com, parent, llr, pla);
3337 case LLIT_UNMATCHED_PAIR:
3338 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
3341 case LLIT_MULTIPLE_REFERENCED:
3342 rc = lfsck_layout_repair_multiple_references(env, com, parent,
3345 case LLIT_INCONSISTENT_OWNER:
3346 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
3356 down_write(&com->lc_sem);
3358 struct lfsck_assistant_data *lad = com->lc_data;
3360 if (unlikely(lad->lad_exit)) {
3362 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
3363 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
3364 rc == -EHOSTUNREACH) {
3365 /* If cannot touch the target server,
3366 * mark the LFSCK as INCOMPLETE. */
3367 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
3368 "talk with OST %x: rc = %d\n",
3369 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
3370 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
3371 lo->ll_objs_skipped++;
3374 lfsck_layout_record_failure(env, lfsck, lo);
3376 } else if (rc > 0) {
3377 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
3378 "unknown type = %d\n", type);
3380 lo->ll_objs_repaired[type - 1]++;
3381 if (bk->lb_param & LPF_DRYRUN &&
3382 unlikely(lo->ll_pos_first_inconsistent == 0))
3383 lo->ll_pos_first_inconsistent =
3384 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
3387 up_write(&com->lc_sem);
3389 if (parent != NULL && !IS_ERR(parent))
3390 lfsck_object_put(env, parent);
3395 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
3396 struct lfsck_component *com)
3398 struct lfsck_assistant_data *lad = com->lc_data;
3399 struct lfsck_instance *lfsck = com->lc_lfsck;
3400 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3401 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
3402 struct lfsck_tgt_desc *ltd;
3406 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
3407 lfsck_lfsck2name(lfsck));
3409 spin_lock(<ds->ltd_lock);
3410 while (!list_empty(&lad->lad_ost_phase2_list)) {
3411 ltd = list_entry(lad->lad_ost_phase2_list.next,
3412 struct lfsck_tgt_desc,
3413 ltd_layout_phase_list);
3414 list_del_init(<d->ltd_layout_phase_list);
3415 if (bk->lb_param & LPF_ALL_TGT) {
3416 spin_unlock(<ds->ltd_lock);
3417 rc = lfsck_layout_scan_orphan(env, com, ltd);
3418 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3421 if (unlikely(lad->lad_exit ||
3422 !thread_is_running(&lfsck->li_thread)))
3424 spin_lock(<ds->ltd_lock);
3428 if (list_empty(&lad->lad_ost_phase1_list))
3432 spin_unlock(<ds->ltd_lock);
3434 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
3435 lfsck_lfsck2name(lfsck), rc);
3441 lfsck_layout_slave_async_interpret(const struct lu_env *env,
3442 struct ptlrpc_request *req,
3445 struct lfsck_layout_slave_async_args *llsaa = args;
3446 struct obd_export *exp = llsaa->llsaa_exp;
3447 struct lfsck_component *com = llsaa->llsaa_com;
3448 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
3449 struct lfsck_layout_slave_data *llsd = com->lc_data;
3450 struct lfsck_reply *lr = NULL;
3454 /* It is probably caused by network trouble, or target crash,
3455 * it will try several times (depends on the obd_timeout, and
3456 * will not less than 3 times). But to make the LFSCK can go
3457 * ahead, we should not try for ever. After some try but still
3458 * hit failure, it will assume that the target exit the LFSCK
3459 * prcoessing and stop try. */
3460 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
3461 int max_try = max_t(int, obd_timeout / 30, 3);
3463 if (++(llst->llst_failures) > max_try)
3469 llst->llst_failures = 0;
3470 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
3471 if (lr->lr_status != LS_SCANNING_PHASE1 &&
3472 lr->lr_status != LS_SCANNING_PHASE2)
3477 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
3478 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
3479 llst->llst_index, lr != NULL ? lr->lr_status : rc,
3480 llst->llst_failures);
3482 lfsck_layout_llst_del(llsd, llst);
3485 lfsck_layout_llst_put(llst);
3486 lfsck_component_put(env, com);
3487 class_export_put(exp);
3492 static int lfsck_layout_async_query(const struct lu_env *env,
3493 struct lfsck_component *com,
3494 struct obd_export *exp,
3495 struct lfsck_layout_slave_target *llst,
3496 struct lfsck_request *lr,
3497 struct ptlrpc_request_set *set)
3499 struct lfsck_layout_slave_async_args *llsaa;
3500 struct ptlrpc_request *req;
3501 struct lfsck_request *tmp;
3505 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
3509 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
3511 ptlrpc_request_free(req);
3515 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3517 ptlrpc_request_set_replen(req);
3519 llsaa = ptlrpc_req_async_args(req);
3520 llsaa->llsaa_exp = exp;
3521 llsaa->llsaa_com = lfsck_component_get(com);
3522 llsaa->llsaa_llst = llst;
3523 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
3524 req->rq_allow_intr = 1;
3525 ptlrpc_set_add_req(set, req);
3530 static int lfsck_layout_async_notify(const struct lu_env *env,
3531 struct obd_export *exp,
3532 struct lfsck_request *lr,
3533 struct ptlrpc_request_set *set)
3535 struct ptlrpc_request *req;
3536 struct lfsck_request *tmp;
3540 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3544 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3546 ptlrpc_request_free(req);
3550 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3552 ptlrpc_request_set_replen(req);
3553 req->rq_allow_intr = 1;
3554 ptlrpc_set_add_req(set, req);
3560 lfsck_layout_slave_query_master(const struct lu_env *env,
3561 struct lfsck_component *com)
3563 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3564 struct lfsck_instance *lfsck = com->lc_lfsck;
3565 struct lfsck_layout_slave_data *llsd = com->lc_data;
3566 struct lfsck_layout_slave_target *llst;
3567 struct obd_export *exp;
3568 struct ptlrpc_request_set *set;
3573 set = ptlrpc_prep_set();
3575 GOTO(log, rc = -ENOMEM);
3577 memset(lr, 0, sizeof(*lr));
3578 lr->lr_event = LE_QUERY;
3579 lr->lr_active = LFSCK_TYPE_LAYOUT;
3581 llsd->llsd_touch_gen++;
3582 spin_lock(&llsd->llsd_lock);
3583 while (!list_empty(&llsd->llsd_master_list)) {
3584 llst = list_entry(llsd->llsd_master_list.next,
3585 struct lfsck_layout_slave_target,
3587 if (llst->llst_gen == llsd->llsd_touch_gen)
3590 llst->llst_gen = llsd->llsd_touch_gen;
3591 list_move_tail(&llst->llst_list,
3592 &llsd->llsd_master_list);
3593 atomic_inc(&llst->llst_ref);
3594 spin_unlock(&llsd->llsd_lock);
3596 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3599 lfsck_layout_llst_del(llsd, llst);
3600 lfsck_layout_llst_put(llst);
3601 spin_lock(&llsd->llsd_lock);
3605 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
3607 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3608 "query %s for layout: rc = %d\n",
3609 lfsck_lfsck2name(lfsck),
3610 exp->exp_obd->obd_name, rc);
3613 lfsck_layout_llst_put(llst);
3614 class_export_put(exp);
3616 spin_lock(&llsd->llsd_lock);
3618 spin_unlock(&llsd->llsd_lock);
3620 rc = ptlrpc_set_wait(set);
3621 ptlrpc_set_destroy(set);
3623 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
3626 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
3627 lfsck_lfsck2name(com->lc_lfsck), rc);
3633 lfsck_layout_slave_notify_master(const struct lu_env *env,
3634 struct lfsck_component *com,
3635 enum lfsck_events event, int result)
3637 struct lfsck_layout *lo = com->lc_file_ram;
3638 struct lfsck_instance *lfsck = com->lc_lfsck;
3639 struct lfsck_layout_slave_data *llsd = com->lc_data;
3640 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3641 struct lfsck_layout_slave_target *llst;
3642 struct obd_export *exp;
3643 struct ptlrpc_request_set *set;
3647 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
3648 lfsck_lfsck2name(com->lc_lfsck));
3650 set = ptlrpc_prep_set();
3654 memset(lr, 0, sizeof(*lr));
3655 lr->lr_event = event;
3656 lr->lr_flags = LEF_FROM_OST;
3657 lr->lr_status = result;
3658 lr->lr_index = lfsck_dev_idx(lfsck);
3659 lr->lr_active = LFSCK_TYPE_LAYOUT;
3660 lr->lr_flags2 = lo->ll_flags;
3661 llsd->llsd_touch_gen++;
3662 spin_lock(&llsd->llsd_lock);
3663 while (!list_empty(&llsd->llsd_master_list)) {
3664 llst = list_entry(llsd->llsd_master_list.next,
3665 struct lfsck_layout_slave_target,
3667 if (llst->llst_gen == llsd->llsd_touch_gen)
3670 llst->llst_gen = llsd->llsd_touch_gen;
3671 list_move_tail(&llst->llst_list,
3672 &llsd->llsd_master_list);
3673 atomic_inc(&llst->llst_ref);
3674 spin_unlock(&llsd->llsd_lock);
3676 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3679 lfsck_layout_llst_del(llsd, llst);
3680 lfsck_layout_llst_put(llst);
3681 spin_lock(&llsd->llsd_lock);
3685 rc = lfsck_layout_async_notify(env, exp, lr, set);
3687 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3688 "notify %s for layout: rc = %d\n",
3689 lfsck_lfsck2name(lfsck),
3690 exp->exp_obd->obd_name, rc);
3692 lfsck_layout_llst_put(llst);
3693 class_export_put(exp);
3694 spin_lock(&llsd->llsd_lock);
3696 spin_unlock(&llsd->llsd_lock);
3698 ptlrpc_set_wait(set);
3699 ptlrpc_set_destroy(set);
3705 * \ret -ENODATA: unrecognized stripe
3706 * \ret = 0 : recognized stripe
3707 * \ret < 0 : other failures
3709 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
3710 struct lfsck_component *com,
3711 struct lu_fid *cfid,
3712 struct lu_fid *pfid)
3714 struct lfsck_thread_info *info = lfsck_env_info(env);
3715 struct lu_buf *buf = &info->lti_big_buf;
3716 struct ost_id *oi = &info->lti_oi;
3717 struct dt_object *obj;
3718 struct lov_mds_md_v1 *lmm;
3719 struct lov_ost_data_v1 *objs;
3720 __u32 idx = pfid->f_stripe_idx;
3728 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3730 RETURN(PTR_ERR(obj));
3732 dt_read_lock(env, obj, 0);
3733 if (unlikely(dt_object_exists(obj) == 0 ||
3734 lfsck_is_dead_obj(obj)))
3735 GOTO(unlock, rc = -ENOENT);
3737 if (!S_ISREG(lfsck_object_type(obj)))
3738 GOTO(unlock, rc = -ENODATA);
3740 rc = lfsck_layout_get_lovea(env, obj, buf);
3745 GOTO(unlock, rc = -ENODATA);
3748 rc = lfsck_layout_verify_header(lmm);
3752 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
3753 * been verified in lfsck_layout_verify_header() already. If some
3754 * new magic introduced in the future, then layout LFSCK needs to
3755 * be updated also. */
3756 magic = le32_to_cpu(lmm->lmm_magic);
3757 if (magic == LOV_MAGIC_V1) {
3758 objs = &lmm->lmm_objects[0];
3760 LASSERT(magic == LOV_MAGIC_V3);
3761 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3764 fid_to_ostid(cfid, oi);
3765 count = le16_to_cpu(lmm->lmm_stripe_count);
3766 for (i = 0; i < count; i++, objs++) {
3769 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
3770 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
3771 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
3774 GOTO(unlock, rc = -ENODATA);
3777 dt_read_unlock(env, obj);
3778 lfsck_object_put(env, obj);
3784 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
3785 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
3786 * layout EA from MDT to OST. On one hand, the OST no need to understand
3787 * the layout EA structure; on the other hand, it may cause trouble when
3788 * transfer large layout EA from MDT to OST via normal OUT RPC.
3790 * \ret > 0: unrecognized stripe
3791 * \ret = 0: recognized stripe
3792 * \ret < 0: other failures
3794 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
3795 struct lfsck_component *com,
3796 struct lu_fid *cfid,
3797 struct lu_fid *pfid)
3799 struct lfsck_instance *lfsck = com->lc_lfsck;
3800 struct obd_device *obd = lfsck->li_obd;
3801 struct seq_server_site *ss = lfsck_dev_site(lfsck);
3802 struct obd_export *exp = NULL;
3803 struct ptlrpc_request *req = NULL;
3804 struct lfsck_request *lr;
3805 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
3809 if (unlikely(fid_is_idif(pfid)))
3812 fld_range_set_any(range);
3813 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
3815 RETURN(rc == -ENOENT ? 1 : rc);
3817 if (unlikely(!fld_range_is_mdt(range)))
3820 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
3821 if (unlikely(exp == NULL))
3824 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
3825 GOTO(out, rc = -EOPNOTSUPP);
3827 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3829 GOTO(out, rc = -ENOMEM);
3831 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3833 ptlrpc_request_free(req);
3838 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3839 memset(lr, 0, sizeof(*lr));
3840 lr->lr_event = LE_PAIRS_VERIFY;
3841 lr->lr_active = LFSCK_TYPE_LAYOUT;
3842 lr->lr_fid = *cfid; /* OST-object itself FID. */
3843 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
3845 ptlrpc_request_set_replen(req);
3846 rc = ptlrpc_queue_wait(req);
3847 ptlrpc_req_finished(req);
3849 if (rc == -ENOENT || rc == -ENODATA)
3856 class_export_put(exp);
3861 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
3862 struct lfsck_component *com,
3863 struct lfsck_request *lr)
3865 struct dt_object *obj;
3869 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lr->lr_fid);
3871 GOTO(log, rc = PTR_ERR(obj));
3873 dt_write_lock(env, obj, 0);
3874 if (unlikely(dt_object_exists(obj) == 0 ||
3875 lfsck_is_dead_obj(obj)))
3876 GOTO(unlock, rc = 0);
3878 rc = __lfsck_layout_update_pfid(env, obj, &lr->lr_fid2,
3884 dt_write_unlock(env, obj);
3885 lfsck_object_put(env, obj);
3888 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
3889 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
3890 PFID(&lr->lr_fid), PFID(&lr->lr_fid2), rc);
3897 static void lfsck_layout_slave_quit(const struct lu_env *env,
3898 struct lfsck_component *com);
3900 static int lfsck_layout_reset(const struct lu_env *env,
3901 struct lfsck_component *com, bool init)
3903 struct lfsck_layout *lo = com->lc_file_ram;
3906 down_write(&com->lc_sem);
3908 memset(lo, 0, com->lc_file_size);
3910 __u32 count = lo->ll_success_count;
3911 __u64 last_time = lo->ll_time_last_complete;
3913 memset(lo, 0, com->lc_file_size);
3914 lo->ll_success_count = count;
3915 lo->ll_time_last_complete = last_time;
3918 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
3919 lo->ll_status = LS_INIT;
3921 if (com->lc_lfsck->li_master) {
3922 struct lfsck_assistant_data *lad = com->lc_data;
3924 lad->lad_incomplete = 0;
3925 CFS_RESET_BITMAP(lad->lad_bitmap);
3928 rc = lfsck_layout_store(env, com);
3929 up_write(&com->lc_sem);
3931 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
3932 lfsck_lfsck2name(com->lc_lfsck), rc);
3937 static void lfsck_layout_fail(const struct lu_env *env,
3938 struct lfsck_component *com, bool new_checked)
3940 struct lfsck_layout *lo = com->lc_file_ram;
3942 down_write(&com->lc_sem);
3944 com->lc_new_checked++;
3945 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
3946 up_write(&com->lc_sem);
3949 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
3950 struct lfsck_component *com, bool init)
3952 struct lfsck_instance *lfsck = com->lc_lfsck;
3953 struct lfsck_layout *lo = com->lc_file_ram;
3957 rc = lfsck_checkpoint_generic(env, com);
3959 return rc > 0 ? 0 : rc;
3962 down_write(&com->lc_sem);
3964 lo->ll_pos_latest_start =
3965 lfsck->li_pos_checkpoint.lp_oit_cookie;
3967 lo->ll_pos_last_checkpoint =
3968 lfsck->li_pos_checkpoint.lp_oit_cookie;
3969 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3970 HALF_SEC - lfsck->li_time_last_checkpoint);
3971 lo->ll_time_last_checkpoint = cfs_time_current_sec();
3972 lo->ll_objs_checked_phase1 += com->lc_new_checked;
3973 com->lc_new_checked = 0;
3976 rc = lfsck_layout_store(env, com);
3977 up_write(&com->lc_sem);
3979 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
3980 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
3981 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
3986 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
3987 struct lfsck_component *com, bool init)
3989 struct lfsck_instance *lfsck = com->lc_lfsck;
3990 struct lfsck_layout *lo = com->lc_file_ram;
3993 if (com->lc_new_checked == 0 && !init)
3996 down_write(&com->lc_sem);
3998 lo->ll_pos_latest_start =
3999 lfsck->li_pos_checkpoint.lp_oit_cookie;
4001 lo->ll_pos_last_checkpoint =
4002 lfsck->li_pos_checkpoint.lp_oit_cookie;
4003 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4004 HALF_SEC - lfsck->li_time_last_checkpoint);
4005 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4006 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4007 com->lc_new_checked = 0;
4010 rc = lfsck_layout_store(env, com);
4011 up_write(&com->lc_sem);
4013 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
4014 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
4015 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
4020 static int lfsck_layout_prep(const struct lu_env *env,
4021 struct lfsck_component *com,
4022 struct lfsck_start *start)
4024 struct lfsck_instance *lfsck = com->lc_lfsck;
4025 struct lfsck_layout *lo = com->lc_file_ram;
4026 struct lfsck_position *pos = &com->lc_pos_start;
4028 fid_zero(&pos->lp_dir_parent);
4029 pos->lp_dir_cookie = 0;
4030 if (lo->ll_status == LS_COMPLETED ||
4031 lo->ll_status == LS_PARTIAL ||
4032 /* To handle orphan, must scan from the beginning. */
4033 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
4036 rc = lfsck_layout_reset(env, com, false);
4038 rc = lfsck_set_param(env, lfsck, start, true);
4041 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
4042 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4048 down_write(&com->lc_sem);
4049 lo->ll_time_latest_start = cfs_time_current_sec();
4050 spin_lock(&lfsck->li_lock);
4051 if (lo->ll_flags & LF_SCANNED_ONCE) {
4052 if (!lfsck->li_drop_dryrun ||
4053 lo->ll_pos_first_inconsistent == 0) {
4054 lo->ll_status = LS_SCANNING_PHASE2;
4055 list_move_tail(&com->lc_link,
4056 &lfsck->li_list_double_scan);
4057 pos->lp_oit_cookie = 0;
4061 lo->ll_status = LS_SCANNING_PHASE1;
4062 lo->ll_run_time_phase1 = 0;
4063 lo->ll_run_time_phase2 = 0;
4064 lo->ll_objs_checked_phase1 = 0;
4065 lo->ll_objs_checked_phase2 = 0;
4066 lo->ll_objs_failed_phase1 = 0;
4067 lo->ll_objs_failed_phase2 = 0;
4068 for (i = 0; i < LLIT_MAX; i++)
4069 lo->ll_objs_repaired[i] = 0;
4071 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4072 fid_zero(&com->lc_fid_latest_scanned_phase2);
4075 lo->ll_status = LS_SCANNING_PHASE1;
4076 if (!lfsck->li_drop_dryrun ||
4077 lo->ll_pos_first_inconsistent == 0)
4078 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
4080 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4082 spin_unlock(&lfsck->li_lock);
4083 up_write(&com->lc_sem);
4088 static int lfsck_layout_slave_prep(const struct lu_env *env,
4089 struct lfsck_component *com,
4090 struct lfsck_start_param *lsp)
4092 struct lfsck_layout_slave_data *llsd = com->lc_data;
4093 struct lfsck_instance *lfsck = com->lc_lfsck;
4094 struct lfsck_layout *lo = com->lc_file_ram;
4095 struct lfsck_start *start = lsp->lsp_start;
4098 rc = lfsck_layout_prep(env, com, start);
4102 if (lo->ll_flags & LF_CRASHED_LASTID &&
4103 list_empty(&llsd->llsd_master_list)) {
4104 LASSERT(lfsck->li_out_notify != NULL);
4106 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4107 LE_LASTID_REBUILDING);
4110 if (!lsp->lsp_index_valid)
4113 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
4114 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
4115 LASSERT(!llsd->llsd_rbtree_valid);
4117 write_lock(&llsd->llsd_rb_lock);
4118 rc = lfsck_rbtree_setup(env, com);
4119 write_unlock(&llsd->llsd_rb_lock);
4122 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
4123 "%llu]\n", lfsck_lfsck2name(lfsck),
4124 com->lc_pos_start.lp_oit_cookie);
4129 static int lfsck_layout_master_prep(const struct lu_env *env,
4130 struct lfsck_component *com,
4131 struct lfsck_start_param *lsp)
4136 rc = lfsck_layout_load_bitmap(env, com);
4138 rc = lfsck_layout_reset(env, com, false);
4140 rc = lfsck_set_param(env, com->lc_lfsck,
4141 lsp->lsp_start, true);
4147 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
4151 rc = lfsck_start_assistant(env, com, lsp);
4156 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
4157 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
4158 com->lc_pos_start.lp_oit_cookie);
4163 /* Pre-fetch the attribute for each stripe in the given layout EA. */
4164 static int lfsck_layout_scan_stripes(const struct lu_env *env,
4165 struct lfsck_component *com,
4166 struct dt_object *parent,
4167 struct lov_mds_md_v1 *lmm)
4169 struct lfsck_thread_info *info = lfsck_env_info(env);
4170 struct lfsck_instance *lfsck = com->lc_lfsck;
4171 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4172 struct lfsck_layout *lo = com->lc_file_ram;
4173 struct lfsck_assistant_data *lad = com->lc_data;
4174 struct lfsck_assistant_object *lso = NULL;
4175 struct lov_ost_data_v1 *objs;
4176 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4177 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4178 struct ptlrpc_thread *athread = &lad->lad_thread;
4179 struct l_wait_info lwi = { 0 };
4187 lfsck_buf_init(&buf, &info->lti_old_pfid,
4188 sizeof(struct filter_fid_old));
4189 count = le16_to_cpu(lmm->lmm_stripe_count);
4190 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
4191 * been verified in lfsck_layout_verify_header() already. If some
4192 * new magic introduced in the future, then layout LFSCK needs to
4193 * be updated also. */
4194 magic = le32_to_cpu(lmm->lmm_magic);
4195 if (magic == LOV_MAGIC_V1) {
4196 objs = &lmm->lmm_objects[0];
4198 LASSERT(magic == LOV_MAGIC_V3);
4199 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4202 for (i = 0; i < count; i++, objs++) {
4203 struct lu_fid *fid = &info->lti_fid;
4204 struct ost_id *oi = &info->lti_oi;
4205 struct lfsck_layout_req *llr;
4206 struct lfsck_tgt_desc *tgt = NULL;
4207 struct dt_object *cobj = NULL;
4209 bool wakeup = false;
4211 if (unlikely(lovea_slot_is_dummy(objs)))
4214 l_wait_event(mthread->t_ctl_waitq,
4215 lad->lad_prefetched < bk->lb_async_windows ||
4216 !thread_is_running(mthread) ||
4217 thread_is_stopped(athread),
4220 if (unlikely(!thread_is_running(mthread)) ||
4221 thread_is_stopped(athread))
4224 if (unlikely(lfsck_is_dead_obj(parent)))
4227 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4228 index = le32_to_cpu(objs->l_ost_idx);
4229 rc = ostid_to_fid(fid, oi, index);
4231 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
4232 ": "DOSTID", idx:%u\n", lfsck_lfsck2name(lfsck),
4233 PFID(lfsck_dto2fid(parent)), POSTID(oi), index);
4237 tgt = lfsck_tgt_get(ltds, index);
4238 if (unlikely(tgt == NULL)) {
4239 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
4240 "did not join the layout LFSCK\n",
4241 lfsck_lfsck2name(lfsck), index);
4242 lfsck_lad_set_bitmap(env, com, index);
4246 /* There is potential deadlock race condition between object
4247 * destroy and layout LFSCK. Consider the following scenario:
4249 * 1) The LFSCK thread obtained the parent object firstly, at
4250 * that time, the parent object has not been destroyed yet.
4252 * 2) One RPC service thread destroyed the parent and all its
4253 * children objects. Because the LFSCK is referencing the
4254 * parent object, then the parent object will be marked as
4255 * dying in RAM. On the other hand, the parent object is
4256 * referencing all its children objects, then all children
4257 * objects will be marked as dying in RAM also.
4259 * 3) The LFSCK thread tries to find some child object with
4260 * the parent object referenced. Then it will find that the
4261 * child object is dying. According to the object visibility
4262 * rules: the object with dying flag cannot be returned to
4263 * others. So the LFSCK thread has to wait until the dying
4264 * object has been purged from RAM, then it can allocate a
4265 * new object (with the same FID) in RAM. Unfortunately, the
4266 * LFSCK thread itself is referencing the parent object, and
4267 * cause the parent object cannot be purged, then cause the
4268 * child object cannot be purged also. So the LFSCK thread
4269 * will fall into deadlock.
4271 * We introduce non-blocked version lu_object_find() to allow
4272 * the LFSCK thread to return failure immediately (instead of
4273 * wait) when it finds dying (child) object, then the LFSCK
4274 * thread can check whether the parent object is dying or not.
4275 * So avoid above deadlock. LU-5395 */
4276 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
4278 if (lfsck_is_dead_obj(parent)) {
4288 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_ASSISTANT_DIRECT)) {
4289 rc = dt_declare_attr_get(env, cobj);
4293 rc = dt_declare_xattr_get(env, cobj, &buf,
4300 struct lu_attr *attr = &info->lti_la;
4302 rc = dt_attr_get(env, parent, attr);
4306 lso = lfsck_assistant_object_init(env,
4307 lfsck_dto2fid(parent), attr,
4308 lfsck->li_pos_current.lp_oit_cookie, false);
4317 llr = lfsck_layout_assistant_req_init(lso, cobj, index, i);
4324 spin_lock(&lad->lad_lock);
4325 if (lad->lad_assistant_status < 0) {
4326 spin_unlock(&lad->lad_lock);
4327 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
4329 RETURN(lad->lad_assistant_status);
4332 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
4333 if (lad->lad_prefetched == 0)
4336 lad->lad_prefetched++;
4337 spin_unlock(&lad->lad_lock);
4339 wake_up_all(&athread->t_ctl_waitq);
4342 down_write(&com->lc_sem);
4343 com->lc_new_checked++;
4345 lfsck_layout_record_failure(env, lfsck, lo);
4346 up_write(&com->lc_sem);
4348 if (cobj != NULL && !IS_ERR(cobj))
4349 lfsck_object_put(env, cobj);
4351 if (likely(tgt != NULL))
4354 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4362 lfsck_assistant_object_put(env, lso);
4367 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
4368 * the OST-object's attribute and generate an structure lfsck_layout_req on the
4369 * list ::lad_req_list.
4371 * For each request on above list, the lfsck_layout_assistant thread compares
4372 * the OST side attribute with local attribute, if inconsistent, then repair it.
4374 * All above processing is async mode with pipeline. */
4375 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
4376 struct lfsck_component *com,
4377 struct dt_object *obj)
4379 struct lfsck_thread_info *info = lfsck_env_info(env);
4380 struct ost_id *oi = &info->lti_oi;
4381 struct lfsck_layout *lo = com->lc_file_ram;
4382 struct lfsck_assistant_data *lad = com->lc_data;
4383 struct lfsck_instance *lfsck = com->lc_lfsck;
4384 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4385 struct thandle *handle = NULL;
4386 struct lu_buf *buf = &info->lti_big_buf;
4387 struct lov_mds_md_v1 *lmm = NULL;
4388 struct dt_device *dev = lfsck_obj2dev(obj);
4389 struct lustre_handle lh = { 0 };
4390 struct lu_buf ea_buf = { NULL };
4393 bool locked = false;
4394 bool stripe = false;
4395 bool bad_oi = false;
4398 if (!S_ISREG(lfsck_object_type(obj)))
4401 if (lad->lad_assistant_status < 0)
4402 GOTO(out, rc = -ESRCH);
4404 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
4405 lmm_oi_cpu_to_le(oi, oi);
4406 dt_read_lock(env, obj, 0);
4410 if (dt_object_exists(obj) == 0 ||
4411 lfsck_is_dead_obj(obj))
4414 rc = lfsck_layout_get_lovea(env, obj, buf);
4420 rc = lfsck_layout_verify_header(lmm);
4421 /* If the LOV EA crashed, then it is possible to be rebuilt later
4422 * when handle orphan OST-objects. */
4426 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0)
4427 GOTO(out, stripe = true);
4429 /* Inconsistent lmm_oi, should be repaired. */
4433 if (bk->lb_param & LPF_DRYRUN) {
4434 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4436 GOTO(out, stripe = true);
4439 if (!lustre_handle_is_used(&lh)) {
4440 dt_read_unlock(env, obj);
4442 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4443 MDS_INODELOCK_LAYOUT |
4444 MDS_INODELOCK_XATTR, LCK_EX);
4448 handle = dt_trans_create(env, dev);
4450 GOTO(out, rc = PTR_ERR(handle));
4452 lfsck_buf_init(&ea_buf, lmm, size);
4453 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4454 LU_XATTR_REPLACE, handle);
4458 rc = dt_trans_start_local(env, dev, handle);
4462 dt_write_lock(env, obj, 0);
4468 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4469 LU_XATTR_REPLACE, handle);
4473 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4475 GOTO(out, stripe = true);
4479 if (lustre_handle_is_used(&lh))
4480 dt_write_unlock(env, obj);
4482 dt_read_unlock(env, obj);
4485 if (handle != NULL && !IS_ERR(handle))
4486 dt_trans_stop(env, dev, handle);
4488 lfsck_ibits_unlock(&lh, LCK_EX);
4491 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
4492 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
4493 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
4494 PFID(lfsck_dto2fid(obj)), rc);
4497 rc = lfsck_layout_scan_stripes(env, com, obj, lmm);
4499 down_write(&com->lc_sem);
4500 com->lc_new_checked++;
4502 lfsck_layout_record_failure(env, lfsck, lo);
4503 up_write(&com->lc_sem);
4509 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
4510 struct lfsck_component *com,
4511 struct dt_object *obj)
4513 struct lfsck_instance *lfsck = com->lc_lfsck;
4514 struct lfsck_layout *lo = com->lc_file_ram;
4515 const struct lu_fid *fid = lfsck_dto2fid(obj);
4516 struct lfsck_layout_slave_data *llsd = com->lc_data;
4517 struct lfsck_layout_seq *lls;
4523 LASSERT(llsd != NULL);
4525 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
4526 cfs_fail_val == lfsck_dev_idx(lfsck)) {
4527 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
4529 struct ptlrpc_thread *thread = &lfsck->li_thread;
4531 l_wait_event(thread->t_ctl_waitq,
4532 !thread_is_running(thread),
4536 lfsck_rbtree_update_bitmap(env, com, fid, false);
4538 down_write(&com->lc_sem);
4539 if (fid_is_idif(fid))
4541 else if (!fid_is_norm(fid) ||
4542 !fid_is_for_ostobj(env, lfsck, obj, fid))
4543 GOTO(unlock, rc = 0);
4546 com->lc_new_checked++;
4548 lls = lfsck_layout_seq_lookup(llsd, seq);
4551 if (unlikely(lls == NULL))
4552 GOTO(unlock, rc = -ENOMEM);
4554 INIT_LIST_HEAD(&lls->lls_list);
4556 rc = lfsck_layout_lastid_load(env, com, lls);
4558 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4559 "load LAST_ID for %#llx: rc = %d\n",
4560 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
4561 lo->ll_objs_failed_phase1++;
4566 lfsck_layout_seq_insert(llsd, lls);
4569 if (unlikely(fid_is_last_id(fid)))
4570 GOTO(unlock, rc = 0);
4572 if (fid_is_idif(fid))
4573 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
4577 if (oid > lls->lls_lastid_known)
4578 lls->lls_lastid_known = oid;
4580 if (oid > lls->lls_lastid) {
4581 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
4582 /* OFD may create new objects during LFSCK scanning. */
4583 rc = lfsck_layout_lastid_reload(env, com, lls);
4584 if (unlikely(rc != 0)) {
4585 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4586 "reload LAST_ID for %#llx: rc = %d\n",
4587 lfsck_lfsck2name(com->lc_lfsck),
4593 if (oid <= lls->lls_lastid ||
4594 lo->ll_flags & LF_CRASHED_LASTID)
4595 GOTO(unlock, rc = 0);
4597 LASSERT(lfsck->li_out_notify != NULL);
4599 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4600 LE_LASTID_REBUILDING);
4601 lo->ll_flags |= LF_CRASHED_LASTID;
4603 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
4604 "LAST_ID file (2) for the sequence %#llx"
4605 ", old value %llu, known value %llu\n",
4606 lfsck_lfsck2name(lfsck), lls->lls_seq,
4607 lls->lls_lastid, oid);
4610 lls->lls_lastid = oid;
4614 GOTO(unlock, rc = 0);
4617 up_write(&com->lc_sem);
4622 static int lfsck_layout_exec_dir(const struct lu_env *env,
4623 struct lfsck_component *com,
4624 struct lfsck_assistant_object *lso,
4625 struct lu_dirent *ent, __u16 type)
4630 static int lfsck_layout_master_post(const struct lu_env *env,
4631 struct lfsck_component *com,
4632 int result, bool init)
4634 struct lfsck_instance *lfsck = com->lc_lfsck;
4635 struct lfsck_layout *lo = com->lc_file_ram;
4639 lfsck_post_generic(env, com, &result);
4641 down_write(&com->lc_sem);
4642 spin_lock(&lfsck->li_lock);
4644 lo->ll_pos_last_checkpoint =
4645 lfsck->li_pos_checkpoint.lp_oit_cookie;
4648 if (lo->ll_flags & LF_INCOMPLETE)
4649 lo->ll_status = LS_PARTIAL;
4651 lo->ll_status = LS_SCANNING_PHASE2;
4652 lo->ll_flags |= LF_SCANNED_ONCE;
4653 lo->ll_flags &= ~LF_UPGRADE;
4654 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4655 } else if (result == 0) {
4656 if (lfsck->li_status != 0)
4657 lo->ll_status = lfsck->li_status;
4659 lo->ll_status = LS_STOPPED;
4660 if (lo->ll_status != LS_PAUSED)
4661 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4663 lo->ll_status = LS_FAILED;
4664 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4666 spin_unlock(&lfsck->li_lock);
4669 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4670 HALF_SEC - lfsck->li_time_last_checkpoint);
4671 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4672 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4673 com->lc_new_checked = 0;
4676 rc = lfsck_layout_store(env, com);
4677 up_write(&com->lc_sem);
4679 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
4680 lfsck_lfsck2name(lfsck), rc);
4685 static int lfsck_layout_slave_post(const struct lu_env *env,
4686 struct lfsck_component *com,
4687 int result, bool init)
4689 struct lfsck_instance *lfsck = com->lc_lfsck;
4690 struct lfsck_layout *lo = com->lc_file_ram;
4694 down_write(&com->lc_sem);
4695 rc = lfsck_layout_lastid_store(env, com);
4699 LASSERT(lfsck->li_out_notify != NULL);
4701 spin_lock(&lfsck->li_lock);
4703 lo->ll_pos_last_checkpoint =
4704 lfsck->li_pos_checkpoint.lp_oit_cookie;
4707 lo->ll_status = LS_SCANNING_PHASE2;
4708 lo->ll_flags |= LF_SCANNED_ONCE;
4709 if (lo->ll_flags & LF_CRASHED_LASTID) {
4711 lo->ll_flags &= ~LF_CRASHED_LASTID;
4713 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
4714 "crashed LAST_ID files successfully\n",
4715 lfsck_lfsck2name(lfsck));
4717 lo->ll_flags &= ~LF_UPGRADE;
4718 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4719 } else if (result == 0) {
4720 if (lfsck->li_status != 0)
4721 lo->ll_status = lfsck->li_status;
4723 lo->ll_status = LS_STOPPED;
4724 if (lo->ll_status != LS_PAUSED)
4725 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4727 lo->ll_status = LS_FAILED;
4728 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4730 spin_unlock(&lfsck->li_lock);
4733 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4737 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4738 HALF_SEC - lfsck->li_time_last_checkpoint);
4739 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4740 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4741 com->lc_new_checked = 0;
4744 rc = lfsck_layout_store(env, com);
4745 up_write(&com->lc_sem);
4747 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
4749 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
4750 lfsck_lfsck2name(lfsck), rc);
4755 static void lfsck_layout_dump(const struct lu_env *env,
4756 struct lfsck_component *com, struct seq_file *m)
4758 struct lfsck_instance *lfsck = com->lc_lfsck;
4759 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4760 struct lfsck_layout *lo = com->lc_file_ram;
4762 down_read(&com->lc_sem);
4763 seq_printf(m, "name: lfsck_layout\n"
4769 lfsck_status2name(lo->ll_status));
4771 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
4773 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4775 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
4777 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
4779 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
4781 seq_printf(m, "latest_start_position: %llu\n"
4782 "last_checkpoint_position: %llu\n"
4783 "first_failure_position: %llu\n",
4784 lo->ll_pos_latest_start,
4785 lo->ll_pos_last_checkpoint,
4786 lo->ll_pos_first_inconsistent);
4788 seq_printf(m, "success_count: %u\n"
4789 "repaired_dangling: %llu\n"
4790 "repaired_unmatched_pair: %llu\n"
4791 "repaired_multiple_referenced: %llu\n"
4792 "repaired_orphan: %llu\n"
4793 "repaired_inconsistent_owner: %llu\n"
4794 "repaired_others: %llu\n"
4796 "failed_phase1: %llu\n"
4797 "failed_phase2: %llu\n",
4798 lo->ll_success_count,
4799 lo->ll_objs_repaired[LLIT_DANGLING - 1],
4800 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
4801 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
4802 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
4803 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
4804 lo->ll_objs_repaired[LLIT_OTHERS - 1],
4805 lo->ll_objs_skipped,
4806 lo->ll_objs_failed_phase1,
4807 lo->ll_objs_failed_phase2);
4809 if (lo->ll_status == LS_SCANNING_PHASE1) {
4811 const struct dt_it_ops *iops;
4812 cfs_duration_t duration = cfs_time_current() -
4813 lfsck->li_time_last_checkpoint;
4814 __u64 checked = lo->ll_objs_checked_phase1 +
4815 com->lc_new_checked;
4816 __u64 speed = checked;
4817 __u64 new_checked = com->lc_new_checked *
4818 msecs_to_jiffies(MSEC_PER_SEC);
4819 __u32 rtime = lo->ll_run_time_phase1 +
4820 cfs_duration_sec(duration + HALF_SEC);
4823 do_div(new_checked, duration);
4825 do_div(speed, rtime);
4826 seq_printf(m, "checked_phase1: %llu\n"
4827 "checked_phase2: %llu\n"
4828 "run_time_phase1: %u seconds\n"
4829 "run_time_phase2: %u seconds\n"
4830 "average_speed_phase1: %llu items/sec\n"
4831 "average_speed_phase2: N/A\n"
4832 "real-time_speed_phase1: %llu items/sec\n"
4833 "real-time_speed_phase2: N/A\n",
4835 lo->ll_objs_checked_phase2,
4837 lo->ll_run_time_phase2,
4841 LASSERT(lfsck->li_di_oit != NULL);
4843 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4845 /* The low layer otable-based iteration position may NOT
4846 * exactly match the layout-based directory traversal
4847 * cookie. Generally, it is not a serious issue. But the
4848 * caller should NOT make assumption on that. */
4849 pos = iops->store(env, lfsck->li_di_oit);
4850 if (!lfsck->li_current_oit_processed)
4852 seq_printf(m, "current_position: %llu\n", pos);
4854 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
4855 cfs_duration_t duration = cfs_time_current() -
4856 com->lc_time_last_checkpoint;
4857 __u64 checked = lo->ll_objs_checked_phase2 +
4858 com->lc_new_checked;
4859 __u64 speed1 = lo->ll_objs_checked_phase1;
4860 __u64 speed2 = checked;
4861 __u64 new_checked = com->lc_new_checked *
4862 msecs_to_jiffies(MSEC_PER_SEC);
4863 __u32 rtime = lo->ll_run_time_phase2 +
4864 cfs_duration_sec(duration + HALF_SEC);
4867 do_div(new_checked, duration);
4868 if (lo->ll_run_time_phase1 != 0)
4869 do_div(speed1, lo->ll_run_time_phase1);
4871 do_div(speed2, rtime);
4872 seq_printf(m, "checked_phase1: %llu\n"
4873 "checked_phase2: %llu\n"
4874 "run_time_phase1: %u seconds\n"
4875 "run_time_phase2: %u seconds\n"
4876 "average_speed_phase1: %llu items/sec\n"
4877 "average_speed_phase2: %llu items/sec\n"
4878 "real-time_speed_phase1: N/A\n"
4879 "real-time_speed_phase2: %llu items/sec\n"
4880 "current_position: "DFID"\n",
4881 lo->ll_objs_checked_phase1,
4883 lo->ll_run_time_phase1,
4888 PFID(&com->lc_fid_latest_scanned_phase2));
4890 __u64 speed1 = lo->ll_objs_checked_phase1;
4891 __u64 speed2 = lo->ll_objs_checked_phase2;
4893 if (lo->ll_run_time_phase1 != 0)
4894 do_div(speed1, lo->ll_run_time_phase1);
4895 if (lo->ll_run_time_phase2 != 0)
4896 do_div(speed2, lo->ll_run_time_phase2);
4897 seq_printf(m, "checked_phase1: %llu\n"
4898 "checked_phase2: %llu\n"
4899 "run_time_phase1: %u seconds\n"
4900 "run_time_phase2: %u seconds\n"
4901 "average_speed_phase1: %llu items/sec\n"
4902 "average_speed_phase2: %llu objs/sec\n"
4903 "real-time_speed_phase1: N/A\n"
4904 "real-time_speed_phase2: N/A\n"
4905 "current_position: N/A\n",
4906 lo->ll_objs_checked_phase1,
4907 lo->ll_objs_checked_phase2,
4908 lo->ll_run_time_phase1,
4909 lo->ll_run_time_phase2,
4914 up_read(&com->lc_sem);
4917 static int lfsck_layout_master_double_scan(const struct lu_env *env,
4918 struct lfsck_component *com)
4920 struct lfsck_layout *lo = com->lc_file_ram;
4921 struct lfsck_assistant_data *lad = com->lc_data;
4922 struct lfsck_instance *lfsck = com->lc_lfsck;
4923 struct lfsck_tgt_descs *ltds;
4924 struct lfsck_tgt_desc *ltd;
4925 struct lfsck_tgt_desc *next;
4928 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
4930 if (thread_is_stopped(&lad->lad_thread)) {
4931 LASSERT(list_empty(&lad->lad_req_list));
4932 LASSERT(list_empty(&lad->lad_ost_phase1_list));
4933 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4935 ltds = &lfsck->li_ost_descs;
4936 spin_lock(<ds->ltd_lock);
4937 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
4938 ltd_layout_phase_list) {
4939 list_del_init(<d->ltd_layout_phase_list);
4941 spin_unlock(<ds->ltd_lock);
4943 ltds = &lfsck->li_mdt_descs;
4944 spin_lock(<ds->ltd_lock);
4945 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4946 ltd_layout_phase_list) {
4947 list_del_init(<d->ltd_layout_phase_list);
4949 spin_unlock(<ds->ltd_lock);
4955 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
4956 struct lfsck_component *com)
4958 struct lfsck_instance *lfsck = com->lc_lfsck;
4959 struct lfsck_layout_slave_data *llsd = com->lc_data;
4960 struct lfsck_layout *lo = com->lc_file_ram;
4961 struct ptlrpc_thread *thread = &lfsck->li_thread;
4965 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
4966 lfsck_lfsck2name(lfsck));
4968 atomic_inc(&lfsck->li_double_scan_count);
4970 if (lo->ll_flags & LF_INCOMPLETE)
4973 com->lc_new_checked = 0;
4974 com->lc_new_scanned = 0;
4975 com->lc_time_last_checkpoint = cfs_time_current();
4976 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4977 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4980 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
4983 rc = lfsck_layout_slave_query_master(env, com);
4984 if (list_empty(&llsd->llsd_master_list)) {
4985 if (unlikely(!thread_is_running(thread)))
4996 rc = l_wait_event(thread->t_ctl_waitq,
4997 !thread_is_running(thread) ||
4998 lo->ll_flags & LF_INCOMPLETE ||
4999 list_empty(&llsd->llsd_master_list),
5001 if (unlikely(!thread_is_running(thread)))
5004 if (lo->ll_flags & LF_INCOMPLETE)
5007 if (rc == -ETIMEDOUT)
5010 GOTO(done, rc = (rc < 0 ? rc : 1));
5014 rc = lfsck_layout_double_scan_result(env, com, rc);
5015 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
5016 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
5017 lfsck_layout_slave_quit(env, com);
5018 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
5019 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5021 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
5022 "status %d: rc = %d\n",
5023 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
5028 static void lfsck_layout_master_data_release(const struct lu_env *env,
5029 struct lfsck_component *com)
5031 struct lfsck_assistant_data *lad = com->lc_data;
5032 struct lfsck_instance *lfsck = com->lc_lfsck;
5033 struct lfsck_tgt_descs *ltds;
5034 struct lfsck_tgt_desc *ltd;
5035 struct lfsck_tgt_desc *next;
5037 LASSERT(lad != NULL);
5038 LASSERT(thread_is_init(&lad->lad_thread) ||
5039 thread_is_stopped(&lad->lad_thread));
5040 LASSERT(list_empty(&lad->lad_req_list));
5042 com->lc_data = NULL;
5044 ltds = &lfsck->li_ost_descs;
5045 spin_lock(<ds->ltd_lock);
5046 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
5047 ltd_layout_phase_list) {
5048 list_del_init(<d->ltd_layout_phase_list);
5050 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
5051 ltd_layout_phase_list) {
5052 list_del_init(<d->ltd_layout_phase_list);
5054 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
5056 list_del_init(<d->ltd_layout_list);
5058 spin_unlock(<ds->ltd_lock);
5060 ltds = &lfsck->li_mdt_descs;
5061 spin_lock(<ds->ltd_lock);
5062 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5063 ltd_layout_phase_list) {
5064 list_del_init(<d->ltd_layout_phase_list);
5066 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5067 ltd_layout_phase_list) {
5068 list_del_init(<d->ltd_layout_phase_list);
5070 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
5072 list_del_init(<d->ltd_layout_list);
5074 spin_unlock(<ds->ltd_lock);
5076 if (likely(lad->lad_bitmap != NULL))
5077 CFS_FREE_BITMAP(lad->lad_bitmap);
5082 static void lfsck_layout_slave_data_release(const struct lu_env *env,
5083 struct lfsck_component *com)
5085 struct lfsck_layout_slave_data *llsd = com->lc_data;
5087 lfsck_layout_slave_quit(env, com);
5088 com->lc_data = NULL;
5092 static void lfsck_layout_master_quit(const struct lu_env *env,
5093 struct lfsck_component *com)
5095 struct lfsck_assistant_data *lad = com->lc_data;
5096 struct lfsck_instance *lfsck = com->lc_lfsck;
5097 struct lfsck_tgt_descs *ltds;
5098 struct lfsck_tgt_desc *ltd;
5099 struct lfsck_tgt_desc *next;
5101 LASSERT(lad != NULL);
5103 lfsck_quit_generic(env, com);
5105 LASSERT(thread_is_init(&lad->lad_thread) ||
5106 thread_is_stopped(&lad->lad_thread));
5107 LASSERT(list_empty(&lad->lad_req_list));
5109 ltds = &lfsck->li_ost_descs;
5110 spin_lock(<ds->ltd_lock);
5111 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
5112 ltd_layout_phase_list) {
5113 list_del_init(<d->ltd_layout_phase_list);
5115 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
5116 ltd_layout_phase_list) {
5117 list_del_init(<d->ltd_layout_phase_list);
5119 spin_unlock(<ds->ltd_lock);
5121 ltds = &lfsck->li_mdt_descs;
5122 spin_lock(<ds->ltd_lock);
5123 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5124 ltd_layout_phase_list) {
5125 list_del_init(<d->ltd_layout_phase_list);
5127 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5128 ltd_layout_phase_list) {
5129 list_del_init(<d->ltd_layout_phase_list);
5131 spin_unlock(<ds->ltd_lock);
5134 static void lfsck_layout_slave_quit(const struct lu_env *env,
5135 struct lfsck_component *com)
5137 struct lfsck_layout_slave_data *llsd = com->lc_data;
5138 struct lfsck_layout_seq *lls;
5139 struct lfsck_layout_seq *next;
5140 struct lfsck_layout_slave_target *llst;
5142 LASSERT(llsd != NULL);
5144 down_write(&com->lc_sem);
5145 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
5147 list_del_init(&lls->lls_list);
5148 lfsck_object_put(env, lls->lls_lastid_obj);
5151 up_write(&com->lc_sem);
5153 spin_lock(&llsd->llsd_lock);
5154 while (!list_empty(&llsd->llsd_master_list)) {
5155 llst = list_entry(llsd->llsd_master_list.next,
5156 struct lfsck_layout_slave_target, llst_list);
5157 list_del_init(&llst->llst_list);
5158 spin_unlock(&llsd->llsd_lock);
5159 lfsck_layout_llst_put(llst);
5160 spin_lock(&llsd->llsd_lock);
5162 spin_unlock(&llsd->llsd_lock);
5164 lfsck_rbtree_cleanup(env, com);
5167 static int lfsck_layout_master_in_notify(const struct lu_env *env,
5168 struct lfsck_component *com,
5169 struct lfsck_request *lr,
5172 struct lfsck_instance *lfsck = com->lc_lfsck;
5173 struct lfsck_layout *lo = com->lc_file_ram;
5174 struct lfsck_assistant_data *lad = com->lc_data;
5175 struct lfsck_tgt_descs *ltds;
5176 struct lfsck_tgt_desc *ltd;
5180 if (lr->lr_event == LE_PAIRS_VERIFY) {
5183 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
5189 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
5190 "from %s %x, status %d, flags %x, flags2 %x\n",
5191 lfsck_lfsck2name(lfsck), lr->lr_event,
5192 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
5193 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
5195 if (lr->lr_event != LE_PHASE1_DONE &&
5196 lr->lr_event != LE_PHASE2_DONE &&
5197 lr->lr_event != LE_PEER_EXIT)
5200 if (lr->lr_flags & LEF_FROM_OST)
5201 ltds = &lfsck->li_ost_descs;
5203 ltds = &lfsck->li_mdt_descs;
5204 spin_lock(<ds->ltd_lock);
5205 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
5207 spin_unlock(<ds->ltd_lock);
5212 list_del_init(<d->ltd_layout_phase_list);
5213 switch (lr->lr_event) {
5214 case LE_PHASE1_DONE:
5215 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
5216 if (lr->lr_flags2 & LF_INCOMPLETE) {
5217 if (lr->lr_flags & LEF_FROM_OST)
5218 lfsck_lad_set_bitmap(env, com,
5221 lo->ll_flags |= LF_INCOMPLETE;
5223 ltd->ltd_layout_done = 1;
5224 list_del_init(<d->ltd_layout_list);
5229 if (lr->lr_flags & LEF_FROM_OST) {
5230 if (list_empty(<d->ltd_layout_list))
5231 list_add_tail(<d->ltd_layout_list,
5232 &lad->lad_ost_list);
5233 list_add_tail(<d->ltd_layout_phase_list,
5234 &lad->lad_ost_phase2_list);
5236 if (list_empty(<d->ltd_layout_list))
5237 list_add_tail(<d->ltd_layout_list,
5238 &lad->lad_mdt_list);
5239 list_add_tail(<d->ltd_layout_phase_list,
5240 &lad->lad_mdt_phase2_list);
5243 case LE_PHASE2_DONE:
5244 ltd->ltd_layout_done = 1;
5245 if (!list_empty(<d->ltd_layout_list)) {
5246 list_del_init(<d->ltd_layout_list);
5247 if (lr->lr_flags2 & LF_INCOMPLETE) {
5248 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
5256 ltd->ltd_layout_done = 1;
5257 list_del_init(<d->ltd_layout_list);
5258 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
5259 !(lr->lr_flags & LEF_FROM_OST))
5260 lo->ll_flags |= LF_INCOMPLETE;
5265 spin_unlock(<ds->ltd_lock);
5267 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5268 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5270 memset(stop, 0, sizeof(*stop));
5271 stop->ls_status = lr->lr_status;
5272 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5273 lfsck_stop(env, lfsck->li_bottom, stop);
5274 } else if (lfsck_phase2_next_ready(lad)) {
5275 wake_up_all(&lad->lad_thread.t_ctl_waitq);
5281 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
5282 struct lfsck_component *com,
5283 struct lfsck_request *lr,
5286 struct lfsck_instance *lfsck = com->lc_lfsck;
5287 struct lfsck_layout_slave_data *llsd = com->lc_data;
5288 struct lfsck_layout_slave_target *llst;
5292 switch (lr->lr_event) {
5293 case LE_FID_ACCESSED:
5294 lfsck_rbtree_update_bitmap(env, com, &lr->lr_fid, true);
5296 case LE_CONDITIONAL_DESTROY:
5297 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
5299 case LE_PAIRS_VERIFY: {
5300 lr->lr_status = LPVS_INIT;
5301 /* Firstly, if the MDT-object which is claimed via OST-object
5302 * local stored PFID xattr recognizes the OST-object, then it
5303 * must be that the client given PFID is wrong. */
5304 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5309 lr->lr_status = LPVS_INCONSISTENT;
5310 /* The OST-object local stored PFID xattr is stale. We need to
5311 * check whether the MDT-object that is claimed via the client
5312 * given PFID information recognizes the OST-object or not. If
5313 * matches, then need to update the OST-object's PFID xattr. */
5314 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5317 * We are not sure whether the client given PFID information
5318 * is correct or not, do nothing to avoid improper fixing.
5321 * The client given PFID information is also invalid, we can
5322 * NOT fix the OST-object inconsistency.
5327 lr->lr_status = LPVS_INCONSISTENT_TOFIX;
5328 rc = lfsck_layout_slave_repair_pfid(env, com, lr);
5332 case LE_PHASE1_DONE: {
5333 if (lr->lr_flags2 & LF_INCOMPLETE) {
5334 struct lfsck_layout *lo = com->lc_file_ram;
5336 lo->ll_flags |= LF_INCOMPLETE;
5337 llst = lfsck_layout_llst_find_and_del(llsd,
5341 lfsck_layout_llst_put(llst);
5342 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5348 case LE_PHASE2_DONE:
5350 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
5351 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
5352 lr->lr_event, lr->lr_index, lr->lr_status);
5358 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
5362 lfsck_layout_llst_put(llst);
5363 if (list_empty(&llsd->llsd_master_list))
5364 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5366 if (lr->lr_event == LE_PEER_EXIT &&
5367 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
5368 (list_empty(&llsd->llsd_master_list) &&
5369 (lr->lr_status == LS_STOPPED ||
5370 lr->lr_status == LS_CO_STOPPED)))) {
5371 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5373 memset(stop, 0, sizeof(*stop));
5374 stop->ls_status = lr->lr_status;
5375 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5376 lfsck_stop(env, lfsck->li_bottom, stop);
5382 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
5386 for (i = 0; i < LLIT_MAX; i++)
5387 *count += lo->ll_objs_repaired[i];
5390 static int lfsck_layout_query_all(const struct lu_env *env,
5391 struct lfsck_component *com,
5392 __u32 *mdts_count, __u32 *osts_count,
5395 struct lfsck_layout *lo = com->lc_file_ram;
5396 struct lfsck_tgt_descs *ltds;
5397 struct lfsck_tgt_desc *ltd;
5402 rc = lfsck_query_all(env, com);
5406 ltds = &com->lc_lfsck->li_mdt_descs;
5407 down_read(<ds->ltd_rw_sem);
5408 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
5409 ltd = lfsck_ltd2tgt(ltds, idx);
5410 LASSERT(ltd != NULL);
5412 mdts_count[ltd->ltd_layout_status]++;
5413 *repaired += ltd->ltd_layout_repaired;
5415 up_read(<ds->ltd_rw_sem);
5417 ltds = &com->lc_lfsck->li_ost_descs;
5418 down_read(<ds->ltd_rw_sem);
5419 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
5420 ltd = lfsck_ltd2tgt(ltds, idx);
5421 LASSERT(ltd != NULL);
5423 osts_count[ltd->ltd_layout_status]++;
5424 *repaired += ltd->ltd_layout_repaired;
5426 up_read(<ds->ltd_rw_sem);
5428 down_read(&com->lc_sem);
5429 mdts_count[lo->ll_status]++;
5430 lfsck_layout_repaired(lo, repaired);
5431 up_read(&com->lc_sem);
5436 static int lfsck_layout_query(const struct lu_env *env,
5437 struct lfsck_component *com,
5438 struct lfsck_request *req,
5439 struct lfsck_reply *rep,
5440 struct lfsck_query *que, int idx)
5442 struct lfsck_layout *lo = com->lc_file_ram;
5446 LASSERT(com->lc_lfsck->li_master);
5448 rc = lfsck_layout_query_all(env, com,
5449 que->lu_mdts_count[idx],
5450 que->lu_osts_count[idx],
5451 &que->lu_repaired[idx]);
5453 down_read(&com->lc_sem);
5454 rep->lr_status = lo->ll_status;
5455 if (req->lr_flags & LEF_QUERY_ALL)
5456 lfsck_layout_repaired(lo, &rep->lr_repaired);
5457 up_read(&com->lc_sem);
5463 /* with lfsck::li_lock held */
5464 static int lfsck_layout_slave_join(const struct lu_env *env,
5465 struct lfsck_component *com,
5466 struct lfsck_start_param *lsp)
5468 struct lfsck_instance *lfsck = com->lc_lfsck;
5469 struct lfsck_layout_slave_data *llsd = com->lc_data;
5470 struct lfsck_layout_slave_target *llst;
5471 struct lfsck_start *start = lsp->lsp_start;
5475 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
5478 if (!lsp->lsp_index_valid)
5481 /* If someone is running the LFSCK without orphan handling,
5482 * it will not maintain the object accessing rbtree. So we
5483 * cannot join it for orphan handling. */
5484 if (!llsd->llsd_rbtree_valid)
5487 spin_unlock(&lfsck->li_lock);
5488 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5489 spin_lock(&lfsck->li_lock);
5490 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
5491 spin_unlock(&lfsck->li_lock);
5492 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
5495 lfsck_layout_llst_put(llst);
5496 spin_lock(&lfsck->li_lock);
5503 static struct lfsck_operations lfsck_layout_master_ops = {
5504 .lfsck_reset = lfsck_layout_reset,
5505 .lfsck_fail = lfsck_layout_fail,
5506 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
5507 .lfsck_prep = lfsck_layout_master_prep,
5508 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
5509 .lfsck_exec_dir = lfsck_layout_exec_dir,
5510 .lfsck_post = lfsck_layout_master_post,
5511 .lfsck_dump = lfsck_layout_dump,
5512 .lfsck_double_scan = lfsck_layout_master_double_scan,
5513 .lfsck_data_release = lfsck_layout_master_data_release,
5514 .lfsck_quit = lfsck_layout_master_quit,
5515 .lfsck_in_notify = lfsck_layout_master_in_notify,
5516 .lfsck_query = lfsck_layout_query,
5519 static struct lfsck_operations lfsck_layout_slave_ops = {
5520 .lfsck_reset = lfsck_layout_reset,
5521 .lfsck_fail = lfsck_layout_fail,
5522 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
5523 .lfsck_prep = lfsck_layout_slave_prep,
5524 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
5525 .lfsck_exec_dir = lfsck_layout_exec_dir,
5526 .lfsck_post = lfsck_layout_slave_post,
5527 .lfsck_dump = lfsck_layout_dump,
5528 .lfsck_double_scan = lfsck_layout_slave_double_scan,
5529 .lfsck_data_release = lfsck_layout_slave_data_release,
5530 .lfsck_quit = lfsck_layout_slave_quit,
5531 .lfsck_in_notify = lfsck_layout_slave_in_notify,
5532 .lfsck_query = lfsck_layout_query,
5533 .lfsck_join = lfsck_layout_slave_join,
5536 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
5537 struct lfsck_component *com,
5538 struct lfsck_position *pos)
5540 struct lfsck_assistant_data *lad = com->lc_data;
5541 struct lfsck_layout_req *llr;
5543 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
5547 if (list_empty(&lad->lad_req_list))
5550 llr = list_entry(lad->lad_req_list.next,
5551 struct lfsck_layout_req,
5553 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
5556 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
5557 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
5558 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
5559 .la_fill_pos = lfsck_layout_assistant_fill_pos,
5560 .la_double_scan_result = lfsck_layout_double_scan_result,
5561 .la_req_fini = lfsck_layout_assistant_req_fini,
5562 .la_sync_failures = lfsck_layout_assistant_sync_failures,
5565 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
5567 struct lfsck_component *com;
5568 struct lfsck_layout *lo;
5569 struct dt_object *root = NULL;
5570 struct dt_object *obj;
5578 INIT_LIST_HEAD(&com->lc_link);
5579 INIT_LIST_HEAD(&com->lc_link_dir);
5580 init_rwsem(&com->lc_sem);
5581 atomic_set(&com->lc_ref, 1);
5582 com->lc_lfsck = lfsck;
5583 com->lc_type = LFSCK_TYPE_LAYOUT;
5584 if (lfsck->li_master) {
5585 com->lc_ops = &lfsck_layout_master_ops;
5586 com->lc_data = lfsck_assistant_data_init(
5587 &lfsck_layout_assistant_ops,
5589 if (com->lc_data == NULL)
5590 GOTO(out, rc = -ENOMEM);
5592 struct lfsck_layout_slave_data *llsd;
5594 com->lc_ops = &lfsck_layout_slave_ops;
5595 OBD_ALLOC_PTR(llsd);
5597 GOTO(out, rc = -ENOMEM);
5599 INIT_LIST_HEAD(&llsd->llsd_seq_list);
5600 INIT_LIST_HEAD(&llsd->llsd_master_list);
5601 spin_lock_init(&llsd->llsd_lock);
5602 llsd->llsd_rb_root = RB_ROOT;
5603 rwlock_init(&llsd->llsd_rb_lock);
5604 com->lc_data = llsd;
5606 com->lc_file_size = sizeof(*lo);
5607 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
5608 if (com->lc_file_ram == NULL)
5609 GOTO(out, rc = -ENOMEM);
5611 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
5612 if (com->lc_file_disk == NULL)
5613 GOTO(out, rc = -ENOMEM);
5615 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
5617 GOTO(out, rc = PTR_ERR(root));
5619 if (unlikely(!dt_try_as_dir(env, root)))
5620 GOTO(out, rc = -ENOTDIR);
5622 obj = local_file_find_or_create(env, lfsck->li_los, root,
5624 S_IFREG | S_IRUGO | S_IWUSR);
5626 GOTO(out, rc = PTR_ERR(obj));
5629 rc = lfsck_layout_load(env, com);
5631 rc = lfsck_layout_reset(env, com, true);
5632 else if (rc == -ENOENT)
5633 rc = lfsck_layout_init(env, com);
5638 lo = com->lc_file_ram;
5639 switch (lo->ll_status) {
5645 spin_lock(&lfsck->li_lock);
5646 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
5647 spin_unlock(&lfsck->li_lock);
5650 CERROR("%s: unknown lfsck_layout status %d\n",
5651 lfsck_lfsck2name(lfsck), lo->ll_status);
5653 case LS_SCANNING_PHASE1:
5654 case LS_SCANNING_PHASE2:
5655 /* No need to store the status to disk right now.
5656 * If the system crashed before the status stored,
5657 * it will be loaded back when next time. */
5658 lo->ll_status = LS_CRASHED;
5659 if (!lfsck->li_master)
5660 lo->ll_flags |= LF_INCOMPLETE;
5667 spin_lock(&lfsck->li_lock);
5668 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
5669 spin_unlock(&lfsck->li_lock);
5673 if (lo->ll_flags & LF_CRASHED_LASTID) {
5674 LASSERT(lfsck->li_out_notify != NULL);
5676 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5677 LE_LASTID_REBUILDING);
5683 if (root != NULL && !IS_ERR(root))
5684 lfsck_object_put(env, root);
5687 lfsck_component_cleanup(env, com);
5688 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
5689 lfsck_lfsck2name(lfsck), rc);
5695 struct lfsck_orphan_it {
5696 struct lfsck_component *loi_com;
5697 struct lfsck_rbtree_node *loi_lrn;
5698 struct lfsck_layout_slave_target *loi_llst;
5699 struct lu_fid loi_key;
5700 struct lu_orphan_rec loi_rec;
5702 unsigned int loi_over:1;
5705 static int lfsck_fid_match_idx(const struct lu_env *env,
5706 struct lfsck_instance *lfsck,
5707 const struct lu_fid *fid, int idx)
5709 struct seq_server_site *ss;
5710 struct lu_server_fld *sf;
5711 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
5714 /* All abnormal cases will be returned to MDT0. */
5715 if (!fid_is_norm(fid)) {
5722 ss = lfsck_dev_site(lfsck);
5723 if (unlikely(ss == NULL))
5726 sf = ss->ss_server_fld;
5727 LASSERT(sf != NULL);
5729 fld_range_set_any(range);
5730 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
5734 if (!fld_range_is_mdt(range))
5737 if (range->lsr_index == idx)
5743 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
5744 struct dt_object *obj)
5746 struct dt_device *dev = lfsck_obj2dev(obj);
5747 struct thandle *handle;
5751 handle = dt_trans_create(env, dev);
5755 rc = dt_declare_ref_del(env, obj, handle);
5759 rc = dt_declare_destroy(env, obj, handle);
5763 rc = dt_trans_start_local(env, dev, handle);
5767 dt_write_lock(env, obj, 0);
5768 rc = dt_ref_del(env, obj, handle);
5770 rc = dt_destroy(env, obj, handle);
5771 dt_write_unlock(env, obj);
5776 dt_trans_stop(env, dev, handle);
5778 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
5779 PFID(lfsck_dto2fid(obj)), rc);
5784 static int lfsck_orphan_index_lookup(const struct lu_env *env,
5785 struct dt_object *dt,
5787 const struct dt_key *key)
5792 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
5793 struct dt_object *dt,
5794 const struct dt_rec *rec,
5795 const struct dt_key *key,
5796 struct thandle *handle)
5801 static int lfsck_orphan_index_insert(const struct lu_env *env,
5802 struct dt_object *dt,
5803 const struct dt_rec *rec,
5804 const struct dt_key *key,
5805 struct thandle *handle,
5811 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
5812 struct dt_object *dt,
5813 const struct dt_key *key,
5814 struct thandle *handle)
5819 static int lfsck_orphan_index_delete(const struct lu_env *env,
5820 struct dt_object *dt,
5821 const struct dt_key *key,
5822 struct thandle *handle)
5827 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
5828 struct dt_object *dt,
5831 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
5832 struct lfsck_instance *lfsck;
5833 struct lfsck_component *com = NULL;
5834 struct lfsck_layout_slave_data *llsd;
5835 struct lfsck_orphan_it *it = NULL;
5836 struct lfsck_layout *lo;
5840 lfsck = lfsck_instance_find(dev, true, false);
5841 if (unlikely(lfsck == NULL))
5842 RETURN(ERR_PTR(-ENXIO));
5844 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
5845 if (unlikely(com == NULL))
5846 GOTO(out, rc = -ENOENT);
5848 lo = com->lc_file_ram;
5849 if (lo->ll_flags & LF_INCOMPLETE)
5850 GOTO(out, rc = -ESRCH);
5852 llsd = com->lc_data;
5853 if (!llsd->llsd_rbtree_valid)
5854 GOTO(out, rc = -ESRCH);
5858 GOTO(out, rc = -ENOMEM);
5860 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
5861 if (it->loi_llst == NULL)
5862 GOTO(out, rc = -ENXIO);
5864 if (dev->dd_record_fid_accessed) {
5865 /* The first iteration against the rbtree, scan the whole rbtree
5866 * to remove the nodes which do NOT need to be handled. */
5867 write_lock(&llsd->llsd_rb_lock);
5868 if (dev->dd_record_fid_accessed) {
5869 struct rb_node *node;
5870 struct rb_node *next;
5871 struct lfsck_rbtree_node *lrn;
5873 /* No need to record the fid accessing anymore. */
5874 dev->dd_record_fid_accessed = 0;
5876 node = rb_first(&llsd->llsd_rb_root);
5877 while (node != NULL) {
5878 next = rb_next(node);
5879 lrn = rb_entry(node, struct lfsck_rbtree_node,
5881 if (atomic_read(&lrn->lrn_known_count) <=
5882 atomic_read(&lrn->lrn_accessed_count)) {
5883 rb_erase(node, &llsd->llsd_rb_root);
5884 lfsck_rbtree_free(lrn);
5889 write_unlock(&llsd->llsd_rb_lock);
5892 /* read lock the rbtree when init, and unlock when fini */
5893 read_lock(&llsd->llsd_rb_lock);
5901 lfsck_component_put(env, com);
5903 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
5904 lfsck_lfsck2name(lfsck), rc);
5906 lfsck_instance_put(env, lfsck);
5911 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
5914 return (struct dt_it *)it;
5917 static void lfsck_orphan_it_fini(const struct lu_env *env,
5920 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5921 struct lfsck_component *com = it->loi_com;
5922 struct lfsck_layout_slave_data *llsd;
5923 struct lfsck_layout_slave_target *llst;
5926 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
5927 lfsck_lfsck2name(com->lc_lfsck));
5929 llsd = com->lc_data;
5930 read_unlock(&llsd->llsd_rb_lock);
5931 llst = it->loi_llst;
5932 LASSERT(llst != NULL);
5934 /* Save the key and hash for iterate next. */
5935 llst->llst_fid = it->loi_key;
5936 llst->llst_hash = it->loi_hash;
5937 lfsck_layout_llst_put(llst);
5938 lfsck_component_put(env, com);
5944 * \retval +1: the iteration finished
5945 * \retval 0: on success, not finished
5946 * \retval -ve: on error
5948 static int lfsck_orphan_it_next(const struct lu_env *env,
5951 struct lfsck_thread_info *info = lfsck_env_info(env);
5952 struct filter_fid_old *pfid = &info->lti_old_pfid;
5953 struct lu_attr *la = &info->lti_la;
5954 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5955 struct lu_fid *key = &it->loi_key;
5956 struct lu_orphan_rec *rec = &it->loi_rec;
5957 struct lfsck_component *com = it->loi_com;
5958 struct lfsck_instance *lfsck = com->lc_lfsck;
5959 struct lfsck_layout_slave_data *llsd = com->lc_data;
5960 struct dt_object *obj;
5961 struct lfsck_rbtree_node *lrn;
5965 __u32 idx = it->loi_llst->llst_index;
5975 lrn = lfsck_rbtree_search(llsd, key, &exact);
5983 key->f_seq = lrn->lrn_seq;
5984 key->f_oid = lrn->lrn_first_oid;
5989 if (unlikely(key->f_oid == 0)) {
5996 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
6002 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
6003 atomic_read(&lrn->lrn_accessed_count))) {
6004 struct rb_node *next = rb_next(&lrn->lrn_node);
6006 while (next != NULL) {
6007 lrn = rb_entry(next, struct lfsck_rbtree_node,
6009 if (atomic_read(&lrn->lrn_known_count) >
6010 atomic_read(&lrn->lrn_accessed_count))
6012 next = rb_next(next);
6021 key->f_seq = lrn->lrn_seq;
6022 key->f_oid = lrn->lrn_first_oid;
6026 pos = key->f_oid - lrn->lrn_first_oid;
6029 pos = find_next_bit(lrn->lrn_known_bitmap,
6030 LFSCK_RBTREE_BITMAP_WIDTH, pos);
6031 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
6032 key->f_oid = lrn->lrn_first_oid + pos;
6033 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
6041 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
6046 key->f_oid = lrn->lrn_first_oid + pos;
6047 obj = lfsck_object_find_bottom(env, lfsck, key);
6050 if (rc == -ENOENT) {
6057 dt_read_lock(env, obj, 0);
6058 if (dt_object_exists(obj) == 0 ||
6059 lfsck_is_dead_obj(obj)) {
6060 dt_read_unlock(env, obj);
6061 lfsck_object_put(env, obj);
6066 rc = dt_attr_get(env, obj, la);
6070 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, pfid, sizeof(*pfid)),
6072 if (rc == -ENODATA) {
6073 /* For the pre-created OST-object, update the bitmap to avoid
6074 * others LFSCK (second phase) iteration to touch it again. */
6075 if (la->la_ctime == 0) {
6076 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
6077 atomic_inc(&lrn->lrn_accessed_count);
6079 /* For the race between repairing dangling referenced
6080 * MDT-object and unlink the file, it may left orphan
6081 * OST-object there. Destroy it now! */
6082 if (unlikely(!(la->la_mode & S_ISUID))) {
6083 dt_read_unlock(env, obj);
6084 lfsck_layout_destroy_orphan(env, obj);
6085 lfsck_object_put(env, obj);
6089 } else if (idx == 0) {
6090 /* If the orphan OST-object has no parent information,
6091 * regard it as referenced by the MDT-object on MDT0. */
6092 fid_zero(&rec->lor_fid);
6093 rec->lor_uid = la->la_uid;
6094 rec->lor_gid = la->la_gid;
6098 dt_read_unlock(env, obj);
6099 lfsck_object_put(env, obj);
6107 if (rc != sizeof(struct filter_fid) &&
6108 rc != sizeof(struct filter_fid_old))
6109 GOTO(out, rc = -EINVAL);
6111 fid_le_to_cpu(&rec->lor_fid, &pfid->ff_parent);
6112 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
6113 * MDT-object's FID::f_ver, instead it is the OST-object index in its
6114 * parent MDT-object's layout EA. */
6115 save = rec->lor_fid.f_stripe_idx;
6116 rec->lor_fid.f_ver = 0;
6117 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_fid, idx);
6118 /* If the orphan OST-object does not claim the MDT, then next.
6120 * If we do not know whether it matches or not, then return it
6121 * to the MDT for further check. */
6123 dt_read_unlock(env, obj);
6124 lfsck_object_put(env, obj);
6129 rec->lor_fid.f_stripe_idx = save;
6130 rec->lor_uid = la->la_uid;
6131 rec->lor_gid = la->la_gid;
6133 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u\n",
6134 lfsck_lfsck2name(com->lc_lfsck), PFID(key), PFID(&rec->lor_fid),
6135 rec->lor_uid, rec->lor_gid);
6140 dt_read_unlock(env, obj);
6141 lfsck_object_put(env, obj);
6149 * \retval +1: locate to the exactly position
6150 * \retval 0: cannot locate to the exactly position,
6151 * call next() to move to a valid position.
6152 * \retval -ve: on error
6154 static int lfsck_orphan_it_get(const struct lu_env *env,
6156 const struct dt_key *key)
6158 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6161 it->loi_key = *(struct lu_fid *)key;
6162 rc = lfsck_orphan_it_next(env, di);
6172 static void lfsck_orphan_it_put(const struct lu_env *env,
6177 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
6178 const struct dt_it *di)
6180 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6182 return (struct dt_key *)&it->loi_key;
6185 static int lfsck_orphan_it_key_size(const struct lu_env *env,
6186 const struct dt_it *di)
6188 return sizeof(struct lu_fid);
6191 static int lfsck_orphan_it_rec(const struct lu_env *env,
6192 const struct dt_it *di,
6196 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6198 *(struct lu_orphan_rec *)rec = it->loi_rec;
6203 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
6204 const struct dt_it *di)
6206 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6208 return it->loi_hash;
6212 * \retval +1: locate to the exactly position
6213 * \retval 0: cannot locate to the exactly position,
6214 * call next() to move to a valid position.
6215 * \retval -ve: on error
6217 static int lfsck_orphan_it_load(const struct lu_env *env,
6218 const struct dt_it *di,
6221 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6222 struct lfsck_layout_slave_target *llst = it->loi_llst;
6225 LASSERT(llst != NULL);
6227 if (hash != llst->llst_hash) {
6228 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
6229 "iteration does not match the one when fini "
6230 "%llu, to be reset.\n",
6231 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
6233 fid_zero(&llst->llst_fid);
6234 llst->llst_hash = 0;
6237 it->loi_key = llst->llst_fid;
6238 it->loi_hash = llst->llst_hash;
6239 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
6249 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
6250 const struct dt_it *di,
6256 const struct dt_index_operations lfsck_orphan_index_ops = {
6257 .dio_lookup = lfsck_orphan_index_lookup,
6258 .dio_declare_insert = lfsck_orphan_index_declare_insert,
6259 .dio_insert = lfsck_orphan_index_insert,
6260 .dio_declare_delete = lfsck_orphan_index_declare_delete,
6261 .dio_delete = lfsck_orphan_index_delete,
6263 .init = lfsck_orphan_it_init,
6264 .fini = lfsck_orphan_it_fini,
6265 .get = lfsck_orphan_it_get,
6266 .put = lfsck_orphan_it_put,
6267 .next = lfsck_orphan_it_next,
6268 .key = lfsck_orphan_it_key,
6269 .key_size = lfsck_orphan_it_key_size,
6270 .rec = lfsck_orphan_it_rec,
6271 .store = lfsck_orphan_it_store,
6272 .load = lfsck_orphan_it_load,
6273 .key_rec = lfsck_orphan_it_key_rec,