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, 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. */
77 struct lfsck_layout_slave_data {
78 /* list for lfsck_layout_seq */
79 struct list_head llsd_seq_list;
81 /* list for the masters involve layout verification. */
82 struct list_head llsd_master_list;
85 struct dt_object *llsd_rb_obj;
86 struct rb_root llsd_rb_root;
87 rwlock_t llsd_rb_lock;
88 unsigned int llsd_rbtree_valid:1;
91 struct lfsck_layout_object {
92 struct lu_attr llo_attr;
97 struct lfsck_layout_req {
98 struct lfsck_assistant_req llr_lar;
99 struct lfsck_layout_object *llr_parent;
100 struct dt_object *llr_child;
102 __u32 llr_lov_idx; /* offset in LOV EA */
105 struct lfsck_layout_slave_async_args {
106 struct obd_export *llsaa_exp;
107 struct lfsck_component *llsaa_com;
108 struct lfsck_layout_slave_target *llsaa_llst;
111 static struct lfsck_layout_object *
112 lfsck_layout_object_init(const struct lu_env *env, struct dt_object *obj,
115 struct lfsck_layout_object *llo;
120 return ERR_PTR(-ENOMEM);
122 rc = dt_attr_get(env, obj, &llo->llo_attr);
129 llo->llo_cookie = cookie;
130 atomic_set(&llo->llo_ref, 1);
136 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
138 if (atomic_dec_and_test(&llst->llst_ref)) {
139 LASSERT(list_empty(&llst->llst_list));
146 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
148 struct lfsck_layout_slave_target *llst;
149 struct lfsck_layout_slave_target *tmp;
156 INIT_LIST_HEAD(&llst->llst_list);
158 llst->llst_index = index;
159 atomic_set(&llst->llst_ref, 1);
161 spin_lock(&llsd->llsd_lock);
162 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
163 if (tmp->llst_index == index) {
169 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
170 spin_unlock(&llsd->llsd_lock);
179 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
180 struct lfsck_layout_slave_target *llst)
184 spin_lock(&llsd->llsd_lock);
185 if (!list_empty(&llst->llst_list)) {
186 list_del_init(&llst->llst_list);
189 spin_unlock(&llsd->llsd_lock);
192 lfsck_layout_llst_put(llst);
195 static inline struct lfsck_layout_slave_target *
196 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
197 __u32 index, bool unlink)
199 struct lfsck_layout_slave_target *llst;
201 spin_lock(&llsd->llsd_lock);
202 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
203 if (llst->llst_index == index) {
205 list_del_init(&llst->llst_list);
207 atomic_inc(&llst->llst_ref);
208 spin_unlock(&llsd->llsd_lock);
213 spin_unlock(&llsd->llsd_lock);
218 static inline void lfsck_layout_object_put(const struct lu_env *env,
219 struct lfsck_layout_object *llo)
221 if (atomic_dec_and_test(&llo->llo_ref))
225 static struct lfsck_layout_req *
226 lfsck_layout_assistant_req_init(struct lfsck_layout_object *parent,
227 const struct lu_fid *pfid,
228 struct dt_object *child, __u32 ost_idx,
231 struct lfsck_layout_req *llr;
235 return ERR_PTR(-ENOMEM);
237 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
238 llr->llr_lar.lar_fid = *pfid;
240 atomic_inc(&parent->llo_ref);
241 llr->llr_parent = parent;
242 llr->llr_child = child;
243 llr->llr_ost_idx = ost_idx;
244 llr->llr_lov_idx = lov_idx;
249 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
250 struct lfsck_assistant_req *lar)
252 struct lfsck_layout_req *llr =
253 container_of0(lar, struct lfsck_layout_req, llr_lar);
255 lfsck_object_put(env, llr->llr_child);
256 lfsck_layout_object_put(env, llr->llr_parent);
261 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
262 struct ptlrpc_request *req,
266 struct lfsck_async_interpret_args *laia = args;
267 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
269 ltd->ltd_synced_failures = 1;
270 atomic_dec(laia->laia_count);
277 * Notify remote LFSCK instances about former failures.
279 * The local LFSCK instance has recorded which OSTs have ever failed to respond
280 * some LFSCK verification requests (maybe because of network issues or the OST
281 * itself trouble). During the respond gap, the OST may missed some OST-objects
282 * verification, then the OST cannot know whether related OST-objects have been
283 * referenced by related MDT-objects or not, then in the second-stage scanning,
284 * these OST-objects will be regarded as orphan, if the OST-object contains bad
285 * parent FID for back reference, then it will misguide the LFSCK to make wrong
286 * fixing for the fake orphan.
288 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
289 * it will scan the bitmap for the ever failed OSTs, and notify them that they
290 * have ever missed some OST-object verification and should skip the handling
291 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
293 * \param[in] env pointer to the thread context
294 * \param[in] com pointer to the lfsck component
295 * \param[in] lr pointer to the lfsck request
297 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
298 struct lfsck_component *com,
299 struct lfsck_request *lr)
301 struct lfsck_async_interpret_args *laia =
302 &lfsck_env_info(env)->lti_laia2;
303 struct lfsck_assistant_data *lad = com->lc_data;
304 struct lfsck_layout *lo = com->lc_file_ram;
305 struct lfsck_instance *lfsck = com->lc_lfsck;
306 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
307 struct lfsck_tgt_desc *ltd;
308 struct ptlrpc_request_set *set;
314 if (!lad->lad_incomplete || lo->ll_flags & LF_INCOMPLETE)
317 /* If the MDT has ever failed to verfiy some OST-objects,
318 * then sync failures with them firstly. */
319 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
321 atomic_set(&count, 0);
322 memset(laia, 0, sizeof(*laia));
323 laia->laia_count = &count;
324 set = ptlrpc_prep_set();
326 GOTO(out, rc = -ENOMEM);
328 down_read(<ds->ltd_rw_sem);
329 cfs_foreach_bit(lad->lad_bitmap, idx) {
330 ltd = LTD_TGT(ltds, idx);
331 LASSERT(ltd != NULL);
333 laia->laia_ltd = ltd;
334 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
335 lfsck_layout_assistant_sync_failures_interpret,
338 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
339 "notify target %x for %s phase1 done: "
340 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
341 ltd->ltd_index, lad->lad_name, rc);
348 up_read(<ds->ltd_rw_sem);
350 if (rc == 0 && atomic_read(&count) > 0)
351 rc = ptlrpc_set_wait(set);
353 ptlrpc_set_destroy(set);
355 if (rc == 0 && atomic_read(&count) > 0)
362 /* If failed to sync failures with the OSTs, then have to
363 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
364 * subsequent orphan OST-object handling. */
365 lo->ll_flags |= LF_INCOMPLETE;
367 lr->lr_flags2 = lo->ll_flags;
370 static int lfsck_layout_get_lovea(const struct lu_env *env,
371 struct dt_object *obj, struct lu_buf *buf)
376 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
378 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
382 lu_buf_realloc(buf, rc);
383 if (buf->lb_buf == NULL)
395 if (unlikely(buf->lb_buf == NULL)) {
396 lu_buf_alloc(buf, rc);
397 if (buf->lb_buf == NULL)
406 static int lfsck_layout_verify_header(struct lov_mds_md_v1 *lmm)
411 magic = le32_to_cpu(lmm->lmm_magic);
412 /* If magic crashed, keep it there. Sometime later, during OST-object
413 * orphan handling, if some OST-object(s) back-point to it, it can be
414 * verified and repaired. */
415 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
419 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
420 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
425 CDEBUG(D_LFSCK, "%s LOV EA magic %u on "DOSTID"\n",
426 rc == -EINVAL ? "Unknown" : "Unsupported",
432 pattern = le32_to_cpu(lmm->lmm_pattern);
433 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
434 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
437 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
438 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u on "DOSTID"\n",
439 pattern, POSTID(&oi));
447 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_CACHE_SIZE
448 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
449 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
451 struct lfsck_rbtree_node {
452 struct rb_node lrn_node;
455 atomic_t lrn_known_count;
456 atomic_t lrn_accessed_count;
457 void *lrn_known_bitmap;
458 void *lrn_accessed_bitmap;
461 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
462 __u64 seq, __u32 oid)
464 if (seq < lrn->lrn_seq)
467 if (seq > lrn->lrn_seq)
470 if (oid < lrn->lrn_first_oid)
473 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
479 /* The caller should hold llsd->llsd_rb_lock. */
480 static struct lfsck_rbtree_node *
481 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
482 const struct lu_fid *fid, bool *exact)
484 struct rb_node *node = llsd->llsd_rb_root.rb_node;
485 struct rb_node *prev = NULL;
486 struct lfsck_rbtree_node *lrn = NULL;
492 while (node != NULL) {
494 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
495 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
497 node = node->rb_left;
499 node = node->rb_right;
507 /* If there is no exactly matched one, then to the next valid one. */
510 /* The rbtree is empty. */
517 node = rb_next(prev);
519 /* The end of the rbtree. */
523 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
528 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
529 const struct lu_fid *fid)
531 struct lfsck_rbtree_node *lrn;
535 return ERR_PTR(-ENOMEM);
537 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
538 if (lrn->lrn_known_bitmap == NULL) {
541 return ERR_PTR(-ENOMEM);
544 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
545 if (lrn->lrn_accessed_bitmap == NULL) {
546 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
549 return ERR_PTR(-ENOMEM);
552 RB_CLEAR_NODE(&lrn->lrn_node);
553 lrn->lrn_seq = fid_seq(fid);
554 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
555 atomic_set(&lrn->lrn_known_count, 0);
556 atomic_set(&lrn->lrn_accessed_count, 0);
561 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
563 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
564 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
568 /* The caller should hold lock. */
569 static struct lfsck_rbtree_node *
570 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
571 struct lfsck_rbtree_node *lrn)
573 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
574 struct rb_node *parent = NULL;
575 struct lfsck_rbtree_node *tmp;
578 while (*pos != NULL) {
580 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
581 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
583 pos = &(*pos)->rb_left;
585 pos = &(*pos)->rb_right;
590 rb_link_node(&lrn->lrn_node, parent, pos);
591 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
596 extern const struct dt_index_operations lfsck_orphan_index_ops;
598 static int lfsck_rbtree_setup(const struct lu_env *env,
599 struct lfsck_component *com)
601 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
602 struct lfsck_instance *lfsck = com->lc_lfsck;
603 struct dt_device *dev = lfsck->li_bottom;
604 struct lfsck_layout_slave_data *llsd = com->lc_data;
605 struct dt_object *obj;
607 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
608 fid->f_oid = lfsck_dev_idx(lfsck);
610 obj = dt_locate(env, dev, fid);
612 RETURN(PTR_ERR(obj));
614 /* Generate an in-RAM object to stand for the layout rbtree.
615 * Scanning the layout rbtree will be via the iteration over
616 * the object. In the future, the rbtree may be written onto
617 * disk with the object.
619 * Mark the object to be as exist. */
620 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
621 obj->do_index_ops = &lfsck_orphan_index_ops;
622 llsd->llsd_rb_obj = obj;
623 llsd->llsd_rbtree_valid = 1;
624 dev->dd_record_fid_accessed = 1;
626 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
627 lfsck_lfsck2name(lfsck));
632 static void lfsck_rbtree_cleanup(const struct lu_env *env,
633 struct lfsck_component *com)
635 struct lfsck_instance *lfsck = com->lc_lfsck;
636 struct lfsck_layout_slave_data *llsd = com->lc_data;
637 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
638 struct rb_node *next;
639 struct lfsck_rbtree_node *lrn;
641 lfsck->li_bottom->dd_record_fid_accessed = 0;
642 /* Invalid the rbtree, then no others will use it. */
643 write_lock(&llsd->llsd_rb_lock);
644 llsd->llsd_rbtree_valid = 0;
645 write_unlock(&llsd->llsd_rb_lock);
647 while (node != NULL) {
648 next = rb_next(node);
649 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
650 rb_erase(node, &llsd->llsd_rb_root);
651 lfsck_rbtree_free(lrn);
655 if (llsd->llsd_rb_obj != NULL) {
656 lfsck_object_put(env, llsd->llsd_rb_obj);
657 llsd->llsd_rb_obj = NULL;
660 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
661 lfsck_lfsck2name(lfsck));
664 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
665 struct lfsck_component *com,
666 const struct lu_fid *fid,
669 struct lfsck_layout_slave_data *llsd = com->lc_data;
670 struct lfsck_rbtree_node *lrn;
676 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
679 if (!fid_is_idif(fid) && !fid_is_norm(fid))
682 read_lock(&llsd->llsd_rb_lock);
683 if (!llsd->llsd_rbtree_valid)
684 GOTO(unlock, rc = 0);
686 lrn = lfsck_rbtree_search(llsd, fid, NULL);
688 struct lfsck_rbtree_node *tmp;
692 read_unlock(&llsd->llsd_rb_lock);
693 tmp = lfsck_rbtree_new(env, fid);
695 GOTO(out, rc = PTR_ERR(tmp));
698 write_lock(&llsd->llsd_rb_lock);
699 if (!llsd->llsd_rbtree_valid) {
700 lfsck_rbtree_free(tmp);
701 GOTO(unlock, rc = 0);
704 lrn = lfsck_rbtree_insert(llsd, tmp);
706 lfsck_rbtree_free(tmp);
709 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
710 /* Any accessed object must be a known object. */
711 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
712 atomic_inc(&lrn->lrn_known_count);
713 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
714 atomic_inc(&lrn->lrn_accessed_count);
716 GOTO(unlock, rc = 0);
720 write_unlock(&llsd->llsd_rb_lock);
722 read_unlock(&llsd->llsd_rb_lock);
724 if (rc != 0 && accessed) {
725 struct lfsck_layout *lo = com->lc_file_ram;
727 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
728 "bitmap, and will cause incorrect LFSCK OST-object "
729 "handling, so disable it to cancel orphan handling "
730 "for related device. rc = %d\n",
731 lfsck_lfsck2name(com->lc_lfsck), rc);
733 lo->ll_flags |= LF_INCOMPLETE;
734 lfsck_rbtree_cleanup(env, com);
738 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
739 const struct lfsck_layout *src)
743 des->ll_magic = le32_to_cpu(src->ll_magic);
744 des->ll_status = le32_to_cpu(src->ll_status);
745 des->ll_flags = le32_to_cpu(src->ll_flags);
746 des->ll_success_count = le32_to_cpu(src->ll_success_count);
747 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
748 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
749 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
750 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
751 des->ll_time_last_checkpoint =
752 le64_to_cpu(src->ll_time_last_checkpoint);
753 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
754 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
755 des->ll_pos_first_inconsistent =
756 le64_to_cpu(src->ll_pos_first_inconsistent);
757 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
758 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
759 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
760 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
761 for (i = 0; i < LLIT_MAX; i++)
762 des->ll_objs_repaired[i] =
763 le64_to_cpu(src->ll_objs_repaired[i]);
764 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
765 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
768 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
769 const struct lfsck_layout *src)
773 des->ll_magic = cpu_to_le32(src->ll_magic);
774 des->ll_status = cpu_to_le32(src->ll_status);
775 des->ll_flags = cpu_to_le32(src->ll_flags);
776 des->ll_success_count = cpu_to_le32(src->ll_success_count);
777 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
778 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
779 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
780 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
781 des->ll_time_last_checkpoint =
782 cpu_to_le64(src->ll_time_last_checkpoint);
783 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
784 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
785 des->ll_pos_first_inconsistent =
786 cpu_to_le64(src->ll_pos_first_inconsistent);
787 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
788 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
789 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
790 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
791 for (i = 0; i < LLIT_MAX; i++)
792 des->ll_objs_repaired[i] =
793 cpu_to_le64(src->ll_objs_repaired[i]);
794 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
795 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
799 * Load the OST bitmap from the lfsck_layout trace file.
801 * \param[in] env pointer to the thread context
802 * \param[in] com pointer to the lfsck component
804 * \retval 0 for success
805 * \retval negative error number on failure or data corruption
807 static int lfsck_layout_load_bitmap(const struct lu_env *env,
808 struct lfsck_component *com)
810 struct dt_object *obj = com->lc_obj;
811 struct lfsck_assistant_data *lad = com->lc_data;
812 struct lfsck_layout *lo = com->lc_file_ram;
813 cfs_bitmap_t *bitmap = lad->lad_bitmap;
814 loff_t pos = com->lc_file_size;
820 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
822 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
824 nbits = lo->ll_bitmap_size;
826 if (unlikely(nbits < BITS_PER_LONG))
827 nbits = BITS_PER_LONG;
829 if (nbits > bitmap->size) {
830 __u32 new_bits = bitmap->size;
831 cfs_bitmap_t *new_bitmap;
833 while (new_bits < nbits)
836 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
837 if (new_bitmap == NULL)
840 lad->lad_bitmap = new_bitmap;
841 CFS_FREE_BITMAP(bitmap);
845 if (lo->ll_bitmap_size == 0) {
846 lad->lad_incomplete = 0;
847 CFS_RESET_BITMAP(bitmap);
852 size = (lo->ll_bitmap_size + 7) >> 3;
853 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
855 RETURN(rc >= 0 ? -EINVAL : rc);
857 if (cfs_bitmap_check_empty(bitmap))
858 lad->lad_incomplete = 0;
860 lad->lad_incomplete = 1;
866 * Load the layout LFSCK trace file from disk.
868 * The layout LFSCK trace file records the layout LFSCK status information
869 * and other statistics, such as how many objects have been scanned, and how
870 * many objects have been repaired, and etc. It also contains the bitmap for
871 * failed OSTs during the layout LFSCK. All these information will be loaded
872 * from disk to RAM when the layout LFSCK component setup.
874 * \param[in] env pointer to the thread context
875 * \param[in] com pointer to the lfsck component
877 * \retval positive number for file data corruption, the caller
878 * should reset the layout LFSCK trace file
879 * \retval 0 for success
880 * \retval negative error number on failure
882 static int lfsck_layout_load(const struct lu_env *env,
883 struct lfsck_component *com)
885 struct lfsck_layout *lo = com->lc_file_ram;
886 ssize_t size = com->lc_file_size;
890 rc = dt_read(env, com->lc_obj,
891 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
895 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
896 lfsck_lfsck2name(com->lc_lfsck), rc);
898 } else if (rc != size) {
899 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
900 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
904 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
905 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
906 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
907 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
908 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
916 * Store the layout LFSCK trace file on disk.
918 * The layout LFSCK trace file records the layout LFSCK status information
919 * and other statistics, such as how many objects have been scanned, and how
920 * many objects have been repaired, and etc. It also contains the bitmap for
921 * failed OSTs during the layout LFSCK. All these information will be synced
922 * from RAM to disk periodically.
924 * \param[in] env pointer to the thread context
925 * \param[in] com pointer to the lfsck component
927 * \retval 0 for success
928 * \retval negative error number on failure
930 static int lfsck_layout_store(const struct lu_env *env,
931 struct lfsck_component *com)
933 struct dt_object *obj = com->lc_obj;
934 struct lfsck_instance *lfsck = com->lc_lfsck;
935 struct lfsck_layout *lo_ram = com->lc_file_ram;
936 struct lfsck_layout *lo = com->lc_file_disk;
938 struct dt_device *dev = lfsck_obj2dev(obj);
939 cfs_bitmap_t *bitmap = NULL;
941 ssize_t size = com->lc_file_size;
946 if (lfsck->li_master) {
947 struct lfsck_assistant_data *lad = com->lc_data;
949 bitmap = lad->lad_bitmap;
950 nbits = bitmap->size;
953 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
956 lo_ram->ll_bitmap_size = nbits;
957 lfsck_layout_cpu_to_le(lo, lo_ram);
958 th = dt_trans_create(env, dev);
960 GOTO(log, rc = PTR_ERR(th));
962 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
967 if (bitmap != NULL) {
968 rc = dt_declare_record_write(env, obj,
969 lfsck_buf_get(env, bitmap->data, nbits >> 3),
975 rc = dt_trans_start_local(env, dev, th);
980 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
984 if (bitmap != NULL) {
986 rc = dt_record_write(env, obj,
987 lfsck_buf_get(env, bitmap->data, nbits >> 3),
994 dt_trans_stop(env, dev, th);
998 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
999 lfsck_lfsck2name(lfsck), rc);
1004 static int lfsck_layout_init(const struct lu_env *env,
1005 struct lfsck_component *com)
1007 struct lfsck_layout *lo = com->lc_file_ram;
1010 memset(lo, 0, com->lc_file_size);
1011 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1012 lo->ll_status = LS_INIT;
1013 down_write(&com->lc_sem);
1014 rc = lfsck_layout_store(env, com);
1015 up_write(&com->lc_sem);
1020 static int fid_is_for_ostobj(const struct lu_env *env,
1021 struct lfsck_instance *lfsck,
1022 struct dt_object *obj, const struct lu_fid *fid)
1024 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1025 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1026 struct lustre_mdt_attrs *lma;
1029 fld_range_set_any(range);
1030 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1032 if (fld_range_is_ost(range))
1038 lma = &lfsck_env_info(env)->lti_lma;
1039 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
1041 if (rc == sizeof(*lma)) {
1042 lustre_lma_swab(lma);
1044 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1047 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1052 static struct lfsck_layout_seq *
1053 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1055 struct lfsck_layout_seq *lls;
1057 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1058 if (lls->lls_seq == seq)
1061 if (lls->lls_seq > seq)
1069 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1070 struct lfsck_layout_seq *lls)
1072 struct lfsck_layout_seq *tmp;
1073 struct list_head *pos = &llsd->llsd_seq_list;
1075 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1076 if (lls->lls_seq < tmp->lls_seq) {
1077 pos = &tmp->lls_list;
1081 list_add_tail(&lls->lls_list, pos);
1085 lfsck_layout_lastid_create(const struct lu_env *env,
1086 struct lfsck_instance *lfsck,
1087 struct dt_object *obj)
1089 struct lfsck_thread_info *info = lfsck_env_info(env);
1090 struct lu_attr *la = &info->lti_la;
1091 struct dt_object_format *dof = &info->lti_dof;
1092 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1093 struct dt_device *dt = lfsck_obj2dev(obj);
1100 if (bk->lb_param & LPF_DRYRUN)
1103 memset(la, 0, sizeof(*la));
1104 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1105 la->la_valid = LA_MODE | LA_UID | LA_GID;
1106 memset(dof, 0, sizeof(*dof));
1107 dof->dof_type = dt_mode_to_dft(S_IFREG);
1109 th = dt_trans_create(env, dt);
1111 GOTO(log, rc = PTR_ERR(th));
1113 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1117 rc = dt_declare_record_write(env, obj,
1118 lfsck_buf_get(env, &lastid,
1124 rc = dt_trans_start_local(env, dt, th);
1128 dt_write_lock(env, obj, 0);
1129 if (likely(dt_object_exists(obj) == 0)) {
1130 rc = dt_create(env, obj, la, NULL, dof, th);
1132 rc = dt_record_write(env, obj,
1133 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1136 dt_write_unlock(env, obj);
1141 dt_trans_stop(env, dt, th);
1144 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1146 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1152 lfsck_layout_lastid_reload(const struct lu_env *env,
1153 struct lfsck_component *com,
1154 struct lfsck_layout_seq *lls)
1160 dt_read_lock(env, lls->lls_lastid_obj, 0);
1161 rc = dt_record_read(env, lls->lls_lastid_obj,
1162 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1163 dt_read_unlock(env, lls->lls_lastid_obj);
1164 if (unlikely(rc != 0))
1167 lastid = le64_to_cpu(lastid);
1168 if (lastid < lls->lls_lastid_known) {
1169 struct lfsck_instance *lfsck = com->lc_lfsck;
1170 struct lfsck_layout *lo = com->lc_file_ram;
1172 lls->lls_lastid = lls->lls_lastid_known;
1174 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1175 LASSERT(lfsck->li_out_notify != NULL);
1177 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1178 LE_LASTID_REBUILDING);
1179 lo->ll_flags |= LF_CRASHED_LASTID;
1181 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1182 "LAST_ID file (1) for the sequence "LPX64
1183 ", old value "LPU64", known value "LPU64"\n",
1184 lfsck_lfsck2name(lfsck), lls->lls_seq,
1185 lastid, lls->lls_lastid);
1187 } else if (lastid >= lls->lls_lastid) {
1188 lls->lls_lastid = lastid;
1196 lfsck_layout_lastid_store(const struct lu_env *env,
1197 struct lfsck_component *com)
1199 struct lfsck_instance *lfsck = com->lc_lfsck;
1200 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1201 struct dt_device *dt = lfsck->li_bottom;
1202 struct lfsck_layout_slave_data *llsd = com->lc_data;
1203 struct lfsck_layout_seq *lls;
1209 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1212 if (!lls->lls_dirty)
1215 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1216 "<seq> "LPX64" as <oid> "LPU64"\n",
1217 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1219 if (bk->lb_param & LPF_DRYRUN) {
1224 th = dt_trans_create(env, dt);
1227 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1228 "the LAST_ID for <seq> "LPX64"(1): rc = %d\n",
1229 lfsck_lfsck2name(com->lc_lfsck),
1234 lastid = cpu_to_le64(lls->lls_lastid);
1235 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1236 lfsck_buf_get(env, &lastid,
1242 rc = dt_trans_start_local(env, dt, th);
1246 dt_write_lock(env, lls->lls_lastid_obj, 0);
1247 rc = dt_record_write(env, lls->lls_lastid_obj,
1248 lfsck_buf_get(env, &lastid,
1249 sizeof(lastid)), &pos, th);
1250 dt_write_unlock(env, lls->lls_lastid_obj);
1255 dt_trans_stop(env, dt, th);
1258 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1259 "the LAST_ID for <seq> "LPX64"(2): rc = %d\n",
1260 lfsck_lfsck2name(com->lc_lfsck),
1269 lfsck_layout_lastid_load(const struct lu_env *env,
1270 struct lfsck_component *com,
1271 struct lfsck_layout_seq *lls)
1273 struct lfsck_instance *lfsck = com->lc_lfsck;
1274 struct lfsck_layout *lo = com->lc_file_ram;
1275 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1276 struct dt_object *obj;
1281 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1282 obj = dt_locate(env, lfsck->li_bottom, fid);
1284 RETURN(PTR_ERR(obj));
1286 /* LAST_ID crashed, to be rebuilt */
1287 if (dt_object_exists(obj) == 0) {
1288 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1289 LASSERT(lfsck->li_out_notify != NULL);
1291 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1292 LE_LASTID_REBUILDING);
1293 lo->ll_flags |= LF_CRASHED_LASTID;
1295 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1296 "LAST_ID file for sequence "LPX64"\n",
1297 lfsck_lfsck2name(lfsck), lls->lls_seq);
1299 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1301 struct l_wait_info lwi = LWI_TIMEOUT(
1302 cfs_time_seconds(cfs_fail_val),
1305 /* Some others may changed the cfs_fail_val
1306 * as zero after above check, re-check it for
1307 * sure to avoid falling into wait for ever. */
1308 if (likely(lwi.lwi_timeout > 0)) {
1309 struct ptlrpc_thread *thread =
1312 up_write(&com->lc_sem);
1313 l_wait_event(thread->t_ctl_waitq,
1314 !thread_is_running(thread),
1316 down_write(&com->lc_sem);
1321 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1323 dt_read_lock(env, obj, 0);
1324 rc = dt_read(env, obj,
1325 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1327 dt_read_unlock(env, obj);
1328 if (rc != 0 && rc != sizeof(__u64))
1329 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1331 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1332 LASSERT(lfsck->li_out_notify != NULL);
1334 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1335 LE_LASTID_REBUILDING);
1336 lo->ll_flags |= LF_CRASHED_LASTID;
1338 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1339 "LAST_ID file for the sequence "LPX64
1341 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1344 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1352 lfsck_object_put(env, obj);
1354 lls->lls_lastid_obj = obj;
1359 static void lfsck_layout_record_failure(const struct lu_env *env,
1360 struct lfsck_instance *lfsck,
1361 struct lfsck_layout *lo)
1365 lo->ll_objs_failed_phase1++;
1366 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1368 if (lo->ll_pos_first_inconsistent == 0 ||
1369 lo->ll_pos_first_inconsistent < cookie) {
1370 lo->ll_pos_first_inconsistent = cookie;
1372 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1373 "inconsistency at the pos ["LPU64"]\n",
1374 lfsck_lfsck2name(lfsck),
1375 lo->ll_pos_first_inconsistent);
1379 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1380 struct lfsck_component *com,
1383 struct lfsck_instance *lfsck = com->lc_lfsck;
1384 struct lfsck_layout *lo = com->lc_file_ram;
1386 down_write(&com->lc_sem);
1387 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1388 HALF_SEC - lfsck->li_time_last_checkpoint);
1389 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1390 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1393 if (lo->ll_flags & LF_INCOMPLETE) {
1394 lo->ll_status = LS_PARTIAL;
1396 if (lfsck->li_master) {
1397 struct lfsck_assistant_data *lad = com->lc_data;
1399 if (lad->lad_incomplete)
1400 lo->ll_status = LS_PARTIAL;
1402 lo->ll_status = LS_COMPLETED;
1404 lo->ll_status = LS_COMPLETED;
1407 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1408 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1409 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1410 lo->ll_success_count++;
1411 } else if (rc == 0) {
1412 if (lfsck->li_status != 0)
1413 lo->ll_status = lfsck->li_status;
1415 lo->ll_status = LS_STOPPED;
1417 lo->ll_status = LS_FAILED;
1420 rc = lfsck_layout_store(env, com);
1421 up_write(&com->lc_sem);
1426 static int lfsck_layout_trans_stop(const struct lu_env *env,
1427 struct dt_device *dev,
1428 struct thandle *handle, int result)
1432 handle->th_result = result;
1433 rc = dt_trans_stop(env, dev, handle);
1443 * Get the system default stripe size.
1445 * \param[in] env pointer to the thread context
1446 * \param[in] lfsck pointer to the lfsck instance
1447 * \param[out] size pointer to the default stripe size
1449 * \retval 0 for success
1450 * \retval negative error number on failure
1452 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1453 struct lfsck_instance *lfsck,
1456 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1457 struct dt_object *root;
1460 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1462 return PTR_ERR(root);
1464 /* Get the default stripe size via xattr_get on the backend root. */
1465 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1468 /* The lum->lmm_stripe_size is LE mode. The *size also
1469 * should be LE mode. So it is unnecessary to convert. */
1470 *size = lum->lmm_stripe_size;
1472 } else if (unlikely(rc == 0)) {
1476 lfsck_object_put(env, root);
1482 * \retval +1: repaired
1483 * \retval 0: did nothing
1484 * \retval -ve: on error
1486 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1487 struct thandle *handle,
1488 struct dt_object *parent,
1489 struct lu_fid *cfid,
1491 struct lov_ost_data_v1 *slot,
1492 int fl, __u32 ost_idx)
1494 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1495 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1496 struct lu_buf ea_buf;
1502 magic = le32_to_cpu(lmm->lmm_magic);
1503 count = le16_to_cpu(lmm->lmm_stripe_count);
1505 fid_to_ostid(cfid, oi);
1506 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1507 slot->l_ost_gen = cpu_to_le32(0);
1508 slot->l_ost_idx = cpu_to_le32(ost_idx);
1510 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE) {
1511 struct lov_ost_data_v1 *objs;
1514 if (magic == LOV_MAGIC_V1)
1515 objs = &lmm->lmm_objects[0];
1517 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1518 for (i = 0; i < count; i++, objs++) {
1519 if (objs != slot && lovea_slot_is_dummy(objs))
1523 /* If the @slot is the last dummy slot to be refilled,
1524 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1526 lmm->lmm_pattern &= ~cpu_to_le32(LOV_PATTERN_F_HOLE);
1529 lfsck_buf_init(&ea_buf, lmm, lov_mds_md_size(count, magic));
1530 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1538 * \retval +1: repaired
1539 * \retval 0: did nothing
1540 * \retval -ve: on error
1542 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1543 struct lfsck_instance *lfsck,
1544 struct thandle *handle,
1545 struct dt_object *parent,
1546 struct lu_fid *cfid,
1547 struct lu_buf *buf, int fl,
1548 __u32 ost_idx, __u32 ea_off, bool reset)
1550 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1551 struct lov_ost_data_v1 *objs;
1557 if (fl == LU_XATTR_CREATE || reset) {
1558 __u32 pattern = LOV_PATTERN_RAID0;
1561 LASSERT(buf->lb_len >= lov_mds_md_size(count, LOV_MAGIC_V1));
1563 if (ea_off != 0 || reset) {
1564 pattern |= LOV_PATTERN_F_HOLE;
1568 memset(lmm, 0, buf->lb_len);
1569 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1570 lmm->lmm_pattern = cpu_to_le32(pattern);
1571 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1572 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1574 rc = lfsck_layout_get_def_stripesize(env, lfsck,
1575 &lmm->lmm_stripe_size);
1579 objs = &lmm->lmm_objects[ea_off];
1581 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1584 count = le16_to_cpu(lmm->lmm_stripe_count);
1585 if (magic == LOV_MAGIC_V1)
1586 objs = &lmm->lmm_objects[count];
1588 objs = &((struct lov_mds_md_v3 *)lmm)->
1591 gap = ea_off - count;
1594 LASSERT(buf->lb_len >= lov_mds_md_size(count, magic));
1597 memset(objs, 0, gap * sizeof(*objs));
1598 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1602 lmm->lmm_layout_gen =
1603 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1607 lmm->lmm_stripe_count = cpu_to_le16(count);
1608 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1611 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1612 DFID": parent "DFID", OST-index %u, stripe-index %u, fl %d, "
1613 "reset %s, %s LOV EA hole: rc = %d\n",
1614 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1615 ost_idx, ea_off, fl, reset ? "yes" : "no",
1616 hole ? "with" : "without", rc);
1621 static int __lfsck_layout_update_pfid(const struct lu_env *env,
1622 struct dt_object *child,
1623 const struct lu_fid *pfid, __u32 offset)
1625 struct dt_device *dev = lfsck_obj2dev(child);
1626 struct filter_fid *ff = &lfsck_env_info(env)->lti_new_pfid;
1627 struct thandle *handle;
1628 struct lu_buf buf = { NULL };
1631 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1632 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1633 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1634 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1635 * parent MDT-object's layout EA. */
1636 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
1637 lfsck_buf_init(&buf, ff, sizeof(struct filter_fid));
1639 handle = dt_trans_create(env, dev);
1641 RETURN(PTR_ERR(handle));
1643 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1647 rc = dt_trans_start_local(env, dev, handle);
1651 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1656 dt_trans_stop(env, dev, handle);
1662 * \retval +1: repaired
1663 * \retval 0: did nothing
1664 * \retval -ve: on error
1666 static int lfsck_layout_update_pfid(const struct lu_env *env,
1667 struct lfsck_component *com,
1668 struct dt_object *parent,
1669 struct lu_fid *cfid,
1670 struct dt_device *cdev, __u32 ea_off)
1672 struct dt_object *child;
1676 child = lfsck_object_find_by_dev(env, cdev, cfid);
1678 RETURN(PTR_ERR(child));
1680 rc = __lfsck_layout_update_pfid(env, child,
1681 lu_object_fid(&parent->do_lu), ea_off);
1682 lfsck_object_put(env, child);
1684 RETURN(rc == 0 ? 1 : rc);
1688 * This function will create the MDT-object with the given (partial) LOV EA.
1690 * Under some data corruption cases, the MDT-object of the file may be lost,
1691 * but its OST-objects, or some of them are there. The layout LFSCK needs to
1692 * re-create the MDT-object with the orphan OST-object(s) information.
1694 * On the other hand, the LFSCK may has created some OST-object for repairing
1695 * dangling LOV EA reference, but as the LFSCK processing, it may find that
1696 * the old OST-object is there and should replace the former new created OST
1697 * object. Unfortunately, some others have modified such newly created object.
1698 * To keep the data (both new and old), the LFSCK will create MDT-object with
1699 * new FID to reference the original OST-object.
1701 * \param[in] env pointer to the thread context
1702 * \param[in] com pointer to the lfsck component
1703 * \param[in] ltd pointer to target device descriptor
1704 * \param[in] rec pointer to the record for the orphan OST-object
1705 * \param[in] cfid pointer to FID for the orphan OST-object
1706 * \param[in] infix additional information, such as the FID for original
1707 * MDT-object and the stripe offset in the LOV EA
1708 * \param[in] type the type for describing why the orphan MDT-object is
1709 * created. The rules are as following:
1711 * type "C": Multiple OST-objects claim the same MDT-object and the
1712 * same slot in the layout EA. Then the LFSCK will create
1713 * new MDT-object(s) to hold the conflict OST-object(s).
1715 * type "N": The orphan OST-object does not know which one was the
1716 * real parent MDT-object, so the LFSCK uses new FID for
1717 * its parent MDT-object.
1719 * type "R": The orphan OST-object knows its parent MDT-object FID,
1720 * but does not know the position (the file name) in the
1723 * type "D": The MDT-object is a directory, it may knows its parent
1724 * but because there is no valid linkEA, the LFSCK cannot
1725 * know where to put it back to the namespace.
1726 * type "O": The MDT-object has no linkEA, and there is no name
1727 * entry that references the MDT-object.
1729 * type "P": The orphan object to be created was a parent directory
1730 * of some MDT-object which linkEA shows that the @orphan
1731 * object is missing.
1733 * The orphan name will be like:
1734 * ${FID}-${infix}-${type}-${conflict_version}
1736 * \param[in] ea_off the stripe offset in the LOV EA
1738 * \retval positive on repaired something
1739 * \retval 0 if needs to repair nothing
1740 * \retval negative error number on failure
1742 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1743 struct lfsck_component *com,
1744 struct lfsck_tgt_desc *ltd,
1745 struct lu_orphan_rec *rec,
1746 struct lu_fid *cfid,
1751 struct lfsck_thread_info *info = lfsck_env_info(env);
1752 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
1753 char *name = info->lti_key;
1754 struct lu_attr *la = &info->lti_la;
1755 struct dt_object_format *dof = &info->lti_dof;
1756 struct lfsck_instance *lfsck = com->lc_lfsck;
1757 struct lu_fid *pfid = &rec->lor_fid;
1758 struct lu_fid *tfid = &info->lti_fid3;
1759 struct dt_device *dev = lfsck->li_bottom;
1760 struct dt_object *lpf = lfsck->li_lpf_obj;
1761 struct dt_object *pobj = NULL;
1762 struct dt_object *cobj = NULL;
1763 struct thandle *th = NULL;
1764 struct lu_buf *ea_buf = &info->lti_big_buf;
1765 struct lu_buf lov_buf;
1766 struct lfsck_lock_handle *llh = &info->lti_llh;
1767 struct linkea_data ldata = { NULL };
1768 struct lu_buf linkea_buf;
1769 const struct lu_name *pname;
1775 if (unlikely(lpf == NULL))
1776 GOTO(log, rc = -ENXIO);
1778 /* We use two separated transactions to repair the inconsistency.
1780 * 1) create the MDT-object locally.
1781 * 2) update the OST-object's PFID EA if necessary.
1783 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
1784 * updated when the layout LFSCK run next time.
1786 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
1787 * when the layout LFSCK run next time. */
1789 if (fid_is_zero(pfid)) {
1790 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1794 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1796 GOTO(log, rc = PTR_ERR(cobj));
1799 pobj = lfsck_object_find_by_dev(env, dev, pfid);
1801 GOTO(log, rc = PTR_ERR(pobj));
1803 LASSERT(infix != NULL);
1804 LASSERT(type != NULL);
1806 memset(la, 0, sizeof(*la));
1807 la->la_uid = rec->lor_uid;
1808 la->la_gid = rec->lor_gid;
1809 la->la_mode = S_IFREG | S_IRUSR;
1810 la->la_valid = LA_MODE | LA_UID | LA_GID;
1812 memset(dof, 0, sizeof(*dof));
1813 dof->dof_type = dt_mode_to_dft(S_IFREG);
1814 /* Because the dof->dof_reg.striped = 0, the LOD will not create
1815 * the stripe(s). The LFSCK will specify the LOV EA via
1816 * lfsck_layout_extend_lovea(). */
1818 size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1819 if (ea_buf->lb_len < size) {
1820 lu_buf_realloc(ea_buf, size);
1821 if (ea_buf->lb_buf == NULL)
1822 GOTO(log, rc = -ENOMEM);
1827 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
1829 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1830 (const struct dt_key *)name);
1831 if (rc != 0 && rc != -ENOENT)
1835 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
1836 MDS_INODELOCK_UPDATE, LCK_PW);
1840 /* Re-check whether the name conflict with othrs after taken
1842 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1843 (const struct dt_key *)name);
1844 if (unlikely(rc == 0)) {
1852 rc = linkea_data_new(&ldata,
1853 &lfsck_env_info(env)->lti_linkea_buf);
1857 pname = lfsck_name_get_const(env, name, strlen(name));
1858 rc = linkea_add_buf(&ldata, pname, lfsck_dto2fid(lfsck->li_lpf_obj));
1862 /* The 1st transaction. */
1863 th = dt_trans_create(env, dev);
1865 GOTO(unlock, rc = PTR_ERR(th));
1867 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1871 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
1872 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
1873 LU_XATTR_REPLACE, th);
1877 dtrec->rec_fid = pfid;
1878 dtrec->rec_type = S_IFREG;
1879 rc = dt_declare_insert(env, lpf,
1880 (const struct dt_rec *)dtrec,
1881 (const struct dt_key *)name, th);
1885 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1886 ldata.ld_leh->leh_len);
1887 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
1888 XATTR_NAME_LINK, 0, th);
1892 rc = dt_trans_start_local(env, dev, th);
1896 dt_write_lock(env, pobj, 0);
1897 rc = dt_create(env, pobj, la, NULL, dof, th);
1899 rc = lfsck_layout_extend_lovea(env, lfsck, th, pobj, cfid,
1900 &lov_buf, 0, ltd->ltd_index, ea_off, true);
1901 dt_write_unlock(env, pobj);
1905 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
1906 (const struct dt_key *)name, th, 1);
1910 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
1911 if (rc == 0 && cobj != NULL) {
1912 dt_trans_stop(env, dev, th);
1915 /* The 2nd transaction. */
1916 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ea_off);
1923 dt_trans_stop(env, dev, th);
1929 if (cobj != NULL && !IS_ERR(cobj))
1930 lfsck_object_put(env, cobj);
1931 if (pobj != NULL && !IS_ERR(pobj))
1932 lfsck_object_put(env, pobj);
1935 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
1936 "recreate the lost MDT-object: parent "DFID
1937 ", child "DFID", OST-index %u, stripe-index %u, "
1938 "infix %s, type %s: rc = %d\n",
1939 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
1940 ltd->ltd_index, ea_off, infix, type, rc);
1942 return rc >= 0 ? 1 : rc;
1945 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
1946 struct lfsck_component *com,
1947 const struct lu_fid *fid,
1950 struct lfsck_thread_info *info = lfsck_env_info(env);
1951 struct lfsck_request *lr = &info->lti_lr;
1952 struct lfsck_instance *lfsck = com->lc_lfsck;
1953 struct lfsck_tgt_desc *ltd;
1954 struct ptlrpc_request *req;
1955 struct lfsck_request *tmp;
1956 struct obd_export *exp;
1960 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
1961 if (unlikely(ltd == NULL))
1965 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
1966 GOTO(put, rc = -EOPNOTSUPP);
1968 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
1970 GOTO(put, rc = -ENOMEM);
1972 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1974 ptlrpc_request_free(req);
1979 memset(lr, 0, sizeof(*lr));
1980 lr->lr_event = LE_CONDITIONAL_DESTROY;
1981 lr->lr_active = LFSCK_TYPE_LAYOUT;
1984 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1986 ptlrpc_request_set_replen(req);
1988 rc = ptlrpc_queue_wait(req);
1989 ptlrpc_req_finished(req);
1999 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2000 struct lfsck_component *com,
2001 struct lfsck_request *lr)
2003 struct lfsck_thread_info *info = lfsck_env_info(env);
2004 struct lu_attr *la = &info->lti_la;
2005 ldlm_policy_data_t *policy = &info->lti_policy;
2006 struct ldlm_res_id *resid = &info->lti_resid;
2007 struct lfsck_instance *lfsck = com->lc_lfsck;
2008 struct dt_device *dev = lfsck->li_bottom;
2009 struct lu_fid *fid = &lr->lr_fid;
2010 struct dt_object *obj;
2011 struct thandle *th = NULL;
2012 struct lustre_handle lh = { 0 };
2017 obj = lfsck_object_find_by_dev(env, dev, fid);
2019 RETURN(PTR_ERR(obj));
2021 dt_read_lock(env, obj, 0);
2022 if (dt_object_exists(obj) == 0 ||
2023 lfsck_is_dead_obj(obj)) {
2024 dt_read_unlock(env, obj);
2026 GOTO(put, rc = -ENOENT);
2029 /* Get obj's attr without lock firstly. */
2030 rc = dt_attr_get(env, obj, la);
2031 dt_read_unlock(env, obj);
2035 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2036 GOTO(put, rc = -ETXTBSY);
2038 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2039 LASSERT(lfsck->li_namespace != NULL);
2041 memset(policy, 0, sizeof(*policy));
2042 policy->l_extent.end = OBD_OBJECT_EOF;
2043 ost_fid_build_resid(fid, resid);
2044 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2045 policy, LCK_EX, &flags, ldlm_blocking_ast,
2046 ldlm_completion_ast, NULL, NULL, 0,
2047 LVB_T_NONE, NULL, &lh);
2049 GOTO(put, rc = -EIO);
2051 dt_write_lock(env, obj, 0);
2052 /* Get obj's attr within lock again. */
2053 rc = dt_attr_get(env, obj, la);
2057 if (la->la_ctime != 0)
2058 GOTO(unlock, rc = -ETXTBSY);
2060 th = dt_trans_create(env, dev);
2062 GOTO(unlock, rc = PTR_ERR(th));
2064 rc = dt_declare_ref_del(env, obj, th);
2068 rc = dt_declare_destroy(env, obj, th);
2072 rc = dt_trans_start_local(env, dev, th);
2076 rc = dt_ref_del(env, obj, th);
2080 rc = dt_destroy(env, obj, th);
2082 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2083 "OST-object "DFID" that was created for reparing "
2084 "dangling referenced case. But the original missing "
2085 "OST-object is found now.\n",
2086 lfsck_lfsck2name(lfsck), PFID(fid));
2091 dt_trans_stop(env, dev, th);
2094 dt_write_unlock(env, obj);
2095 ldlm_lock_decref(&lh, LCK_EX);
2098 lfsck_object_put(env, obj);
2104 * Some OST-object has occupied the specified layout EA slot.
2105 * Such OST-object may be generated by the LFSCK when repair
2106 * dangling referenced MDT-object, which can be indicated by
2107 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2108 * is true and such OST-object has not been modified yet, we
2109 * will replace it with the orphan OST-object; otherwise the
2110 * LFSCK will create new MDT-object to reference the orphan.
2112 * \retval +1: repaired
2113 * \retval 0: did nothing
2114 * \retval -ve: on error
2116 static int lfsck_layout_conflict_create(const struct lu_env *env,
2117 struct lfsck_component *com,
2118 struct lfsck_tgt_desc *ltd,
2119 struct lu_orphan_rec *rec,
2120 struct dt_object *parent,
2121 struct lu_fid *cfid,
2122 struct lu_buf *ea_buf,
2123 struct lov_ost_data_v1 *slot,
2126 struct lfsck_thread_info *info = lfsck_env_info(env);
2127 struct lu_fid *cfid2 = &info->lti_fid2;
2128 struct ost_id *oi = &info->lti_oi;
2129 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2130 struct dt_device *dev = lfsck_obj2dev(parent);
2131 struct thandle *th = NULL;
2132 struct lustre_handle lh = { 0 };
2133 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2137 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2138 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2142 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2143 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2148 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2150 /* If the conflict OST-obejct is not created for fixing dangling
2151 * referenced MDT-object in former LFSCK check/repair, or it has
2152 * been modified by others, then we cannot destroy it. Re-create
2153 * a new MDT-object for the orphan OST-object. */
2154 if (rc == -ETXTBSY) {
2155 /* No need the layout lock on the original parent. */
2156 lfsck_ibits_unlock(&lh, LCK_EX);
2158 fid_zero(&rec->lor_fid);
2159 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2160 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2162 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2163 info->lti_tmpbuf, "C", ea_off);
2168 if (rc != 0 && rc != -ENOENT)
2171 th = dt_trans_create(env, dev);
2173 GOTO(unlock, rc = PTR_ERR(th));
2175 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2176 LU_XATTR_REPLACE, th);
2180 rc = dt_trans_start_local(env, dev, th);
2184 dt_write_lock(env, parent, 0);
2185 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2186 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2187 LU_XATTR_REPLACE, ltd->ltd_index);
2188 dt_write_unlock(env, parent);
2193 dt_trans_stop(env, dev, th);
2196 lfsck_ibits_unlock(&lh, LCK_EX);
2199 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2200 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2201 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2202 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2203 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2206 return rc >= 0 ? 1 : rc;
2210 * \retval +1: repaired
2211 * \retval 0: did nothing
2212 * \retval -ve: on error
2214 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2215 struct lfsck_component *com,
2216 struct lfsck_tgt_desc *ltd,
2217 struct lu_orphan_rec *rec,
2218 struct dt_object *parent,
2219 struct lu_fid *cfid,
2220 __u32 ost_idx, __u32 ea_off)
2222 struct lfsck_thread_info *info = lfsck_env_info(env);
2223 struct lu_buf *buf = &info->lti_big_buf;
2224 struct lu_fid *fid = &info->lti_fid2;
2225 struct ost_id *oi = &info->lti_oi;
2226 struct lfsck_instance *lfsck = com->lc_lfsck;
2227 struct dt_device *dt = lfsck_obj2dev(parent);
2228 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2229 struct thandle *handle = NULL;
2231 struct lov_mds_md_v1 *lmm;
2232 struct lov_ost_data_v1 *objs;
2233 struct lustre_handle lh = { 0 };
2240 bool locked = false;
2243 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2244 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2247 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2248 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2249 "stripe-index %u: rc = %d\n",
2250 lfsck_lfsck2name(lfsck), PFID(cfid),
2251 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off, rc);
2258 dt_write_unlock(env, parent);
2262 if (handle != NULL) {
2263 dt_trans_stop(env, dt, handle);
2268 GOTO(unlock_layout, rc);
2271 if (buf->lb_len < lovea_size) {
2272 lu_buf_realloc(buf, lovea_size);
2273 if (buf->lb_buf == NULL)
2274 GOTO(unlock_layout, rc = -ENOMEM);
2277 if (!(bk->lb_param & LPF_DRYRUN)) {
2278 handle = dt_trans_create(env, dt);
2280 GOTO(unlock_layout, rc = PTR_ERR(handle));
2282 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2287 rc = dt_trans_start_local(env, dt, handle);
2292 dt_write_lock(env, parent, 0);
2294 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2295 if (rc == -ERANGE) {
2296 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2299 } else if (rc == -ENODATA || rc == 0) {
2300 lovea_size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2301 /* If the declared is not big enough, re-try. */
2302 if (buf->lb_len < lovea_size) {
2306 fl = LU_XATTR_CREATE;
2307 } else if (rc < 0) {
2308 GOTO(unlock_parent, rc);
2309 } else if (unlikely(buf->lb_len == 0)) {
2312 fl = LU_XATTR_REPLACE;
2316 if (fl == LU_XATTR_CREATE) {
2317 if (bk->lb_param & LPF_DRYRUN)
2318 GOTO(unlock_parent, rc = 1);
2320 LASSERT(buf->lb_len >= lovea_size);
2322 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2323 buf, fl, ost_idx, ea_off, false);
2325 GOTO(unlock_parent, rc);
2329 rc1 = lfsck_layout_verify_header(lmm);
2331 /* If the LOV EA crashed, the rebuild it. */
2332 if (rc1 == -EINVAL) {
2333 if (bk->lb_param & LPF_DRYRUN)
2334 GOTO(unlock_parent, rc = 1);
2336 LASSERT(buf->lb_len >= lovea_size);
2338 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2339 buf, fl, ost_idx, ea_off, true);
2341 GOTO(unlock_parent, rc);
2344 /* For other unknown magic/pattern, keep the current LOV EA. */
2346 GOTO(unlock_parent, rc = rc1);
2348 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2349 * been verified in lfsck_layout_verify_header() already. If some
2350 * new magic introduced in the future, then layout LFSCK needs to
2351 * be updated also. */
2352 magic = le32_to_cpu(lmm->lmm_magic);
2353 if (magic == LOV_MAGIC_V1) {
2354 objs = &lmm->lmm_objects[0];
2356 LASSERT(magic == LOV_MAGIC_V3);
2357 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2360 count = le16_to_cpu(lmm->lmm_stripe_count);
2362 GOTO(unlock_parent, rc = -EINVAL);
2365 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2366 if (count <= ea_off) {
2367 if (bk->lb_param & LPF_DRYRUN)
2368 GOTO(unlock_parent, rc = 1);
2370 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2371 /* If the declared is not big enough, re-try. */
2372 if (buf->lb_len < lovea_size) {
2377 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2378 buf, fl, ost_idx, ea_off, false);
2380 GOTO(unlock_parent, rc);
2383 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2385 for (i = 0; i < count; i++, objs++) {
2386 /* The MDT-object was created via lfsck_layout_recover_create()
2387 * by others before, and we fill the dummy layout EA. */
2388 if (lovea_slot_is_dummy(objs)) {
2392 if (bk->lb_param & LPF_DRYRUN)
2393 GOTO(unlock_parent, rc = 1);
2395 lmm->lmm_layout_gen =
2396 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2397 rc = lfsck_layout_refill_lovea(env, handle, parent,
2398 cfid, buf, objs, fl,
2401 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2402 "dummy layout slot for "DFID": parent "DFID
2403 ", OST-index %u, stripe-index %u: rc = %d\n",
2404 lfsck_lfsck2name(lfsck), PFID(cfid),
2405 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2407 GOTO(unlock_parent, rc);
2410 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2411 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2413 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2414 "invalid layout EA at the slot %d, index %u\n",
2415 lfsck_lfsck2name(lfsck),
2416 PFID(lfsck_dto2fid(parent)), i,
2417 le32_to_cpu(objs->l_ost_idx));
2419 GOTO(unlock_parent, rc);
2422 /* It should be rare case, the slot is there, but the LFSCK
2423 * does not handle it during the first-phase cycle scanning. */
2424 if (unlikely(lu_fid_eq(fid, cfid))) {
2426 GOTO(unlock_parent, rc = 0);
2428 /* Rare case that the OST-object index
2429 * does not match the parent MDT-object
2430 * layout EA. We trust the later one. */
2431 if (bk->lb_param & LPF_DRYRUN)
2432 GOTO(unlock_parent, rc = 1);
2434 dt_write_unlock(env, parent);
2436 dt_trans_stop(env, dt, handle);
2437 lfsck_ibits_unlock(&lh, LCK_EX);
2438 rc = lfsck_layout_update_pfid(env, com, parent,
2439 cfid, ltd->ltd_tgt, i);
2441 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2442 "updated OST-object's pfid for "DFID
2443 ": parent "DFID", OST-index %u, "
2444 "stripe-index %u: rc = %d\n",
2445 lfsck_lfsck2name(lfsck), PFID(cfid),
2446 PFID(lfsck_dto2fid(parent)),
2447 ltd->ltd_index, i, rc);
2454 /* The MDT-object exists, but related layout EA slot is occupied
2456 if (bk->lb_param & LPF_DRYRUN)
2457 GOTO(unlock_parent, rc = 1);
2459 dt_write_unlock(env, parent);
2461 dt_trans_stop(env, dt, handle);
2462 lfsck_ibits_unlock(&lh, LCK_EX);
2463 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2464 objs = &lmm->lmm_objects[ea_off];
2466 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2467 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2474 dt_write_unlock(env, parent);
2478 dt_trans_stop(env, dt, handle);
2481 lfsck_ibits_unlock(&lh, LCK_EX);
2486 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2487 struct lfsck_component *com,
2488 struct lfsck_tgt_desc *ltd,
2489 struct lu_orphan_rec *rec,
2490 struct lu_fid *cfid)
2492 struct lfsck_layout *lo = com->lc_file_ram;
2493 struct lu_fid *pfid = &rec->lor_fid;
2494 struct dt_object *parent = NULL;
2495 __u32 ea_off = pfid->f_stripe_idx;
2499 if (!fid_is_sane(cfid))
2500 GOTO(out, rc = -EINVAL);
2502 if (fid_is_zero(pfid)) {
2503 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2509 if (!fid_is_sane(pfid))
2510 GOTO(out, rc = -EINVAL);
2512 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2514 GOTO(out, rc = PTR_ERR(parent));
2516 if (unlikely(dt_object_remote(parent) != 0))
2517 GOTO(put, rc = -EXDEV);
2519 if (dt_object_exists(parent) == 0) {
2520 lfsck_object_put(env, parent);
2521 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2526 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2527 GOTO(put, rc = -EISDIR);
2529 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2530 ltd->ltd_index, ea_off);
2536 lfsck_object_put(env, parent);
2538 /* The layout EA is changed, need to be reloaded next time. */
2539 lu_object_put_nocache(env, &parent->do_lu);
2542 down_write(&com->lc_sem);
2543 com->lc_new_scanned++;
2544 com->lc_new_checked++;
2546 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2548 } else if (rc < 0) {
2549 lo->ll_objs_failed_phase2++;
2551 up_write(&com->lc_sem);
2556 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2557 struct lfsck_component *com,
2558 struct lfsck_tgt_desc *ltd)
2560 struct lfsck_assistant_data *lad = com->lc_data;
2561 struct lfsck_instance *lfsck = com->lc_lfsck;
2562 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2563 struct lfsck_thread_info *info = lfsck_env_info(env);
2564 struct ost_id *oi = &info->lti_oi;
2565 struct lu_fid *fid = &info->lti_fid;
2566 struct dt_object *obj;
2567 const struct dt_it_ops *iops;
2572 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
2573 "scanning for OST%04x\n",
2574 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2576 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
2577 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
2578 "scanning for OST%04x\n",
2579 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2584 ostid_set_seq(oi, FID_SEQ_IDIF);
2585 ostid_set_id(oi, 0);
2586 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
2590 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2591 if (unlikely(IS_ERR(obj)))
2592 GOTO(log, rc = PTR_ERR(obj));
2594 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_orphan_features);
2598 iops = &obj->do_index_ops->dio_it;
2599 di = iops->init(env, obj, 0);
2601 GOTO(put, rc = PTR_ERR(di));
2603 rc = iops->load(env, di, 0);
2605 /* -ESRCH means that the orphan OST-objects rbtree has been
2606 * cleanup because of the OSS server restart or other errors. */
2607 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
2612 rc = iops->next(env, di);
2624 struct lu_orphan_rec *rec = &info->lti_rec;
2626 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
2627 unlikely(!thread_is_running(&lfsck->li_thread)))
2630 key = iops->key(env, di);
2631 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2632 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2634 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2635 &com->lc_fid_latest_scanned_phase2);
2636 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2639 lfsck_control_speed_by_self(com);
2641 rc = iops->next(env, di);
2642 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2649 iops->fini(env, di);
2651 lfsck_object_put(env, obj);
2654 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
2655 "scanning for OST%04x: rc = %d\n",
2656 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2658 return rc > 0 ? 0 : rc;
2661 /* For the MDT-object with dangling reference, we need to repare the
2662 * inconsistency according to the LFSCK sponsor's requirement:
2664 * 1) Keep the inconsistency there and report the inconsistency case,
2665 * then give the chance to the application to find related issues,
2666 * and the users can make the decision about how to handle it with
2667 * more human knownledge. (by default)
2669 * 2) Re-create the missing OST-object with the FID/owner information. */
2670 static int lfsck_layout_repair_dangling(const struct lu_env *env,
2671 struct lfsck_component *com,
2672 struct dt_object *parent,
2673 struct lfsck_layout_req *llr,
2674 const struct lu_attr *pla)
2676 struct lfsck_thread_info *info = lfsck_env_info(env);
2677 struct filter_fid *pfid = &info->lti_new_pfid;
2678 struct dt_object_format *dof = &info->lti_dof;
2679 struct lu_attr *cla = &info->lti_la2;
2680 struct dt_object *child = llr->llr_child;
2681 struct dt_device *dev = lfsck_obj2dev(child);
2682 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2683 struct thandle *handle;
2685 struct lustre_handle lh = { 0 };
2690 if (com->lc_lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ)
2698 memset(cla, 0, sizeof(*cla));
2699 cla->la_uid = pla->la_uid;
2700 cla->la_gid = pla->la_gid;
2701 cla->la_mode = S_IFREG | 0666;
2702 cla->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
2703 LA_ATIME | LA_MTIME | LA_CTIME;
2704 memset(dof, 0, sizeof(*dof));
2706 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2707 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2712 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2713 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2714 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2715 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2716 * parent MDT-object's layout EA. */
2717 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2718 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2720 handle = dt_trans_create(env, dev);
2722 GOTO(unlock1, rc = PTR_ERR(handle));
2724 rc = dt_declare_create(env, child, cla, NULL, dof, handle);
2728 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2729 LU_XATTR_CREATE, handle);
2733 rc = dt_trans_start_local(env, dev, handle);
2737 dt_read_lock(env, parent, 0);
2738 if (unlikely(lfsck_is_dead_obj(parent)))
2739 GOTO(unlock2, rc = 1);
2741 rc = dt_create(env, child, cla, NULL, dof, handle);
2745 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2751 dt_read_unlock(env, parent);
2754 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2757 lfsck_ibits_unlock(&lh, LCK_EX);
2760 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found dangling "
2761 "reference for: parent "DFID", child "DFID", OST-index %u, "
2762 "stripe-index %u, owner %u/%u. %s: rc = %d\n",
2763 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
2764 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx,
2765 llr->llr_lov_idx, pla->la_uid, pla->la_gid,
2766 create ? "Create the lost OST-object as required" :
2767 "Keep the MDT-object there by default", rc);
2772 /* If the OST-object does not recognize the MDT-object as its parent, and
2773 * there is no other MDT-object claims as its parent, then just trust the
2774 * given MDT-object as its parent. So update the OST-object filter_fid. */
2775 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2776 struct lfsck_component *com,
2777 struct dt_object *parent,
2778 struct lfsck_layout_req *llr,
2779 const struct lu_attr *pla)
2781 struct lfsck_thread_info *info = lfsck_env_info(env);
2782 struct filter_fid *pfid = &info->lti_new_pfid;
2783 struct lu_attr *tla = &info->lti_la3;
2784 struct dt_object *child = llr->llr_child;
2785 struct dt_device *dev = lfsck_obj2dev(child);
2786 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2787 struct thandle *handle;
2789 struct lustre_handle lh = { 0 };
2793 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2794 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2799 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2800 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2801 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2802 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2803 * parent MDT-object's layout EA. */
2804 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2805 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2807 handle = dt_trans_create(env, dev);
2809 GOTO(unlock1, rc = PTR_ERR(handle));
2811 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2815 tla->la_valid = LA_UID | LA_GID;
2816 tla->la_uid = pla->la_uid;
2817 tla->la_gid = pla->la_gid;
2818 rc = dt_declare_attr_set(env, child, tla, handle);
2822 rc = dt_trans_start_local(env, dev, handle);
2826 dt_write_lock(env, parent, 0);
2827 if (unlikely(lfsck_is_dead_obj(parent)))
2828 GOTO(unlock2, rc = 1);
2830 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2834 /* Get the latest parent's owner. */
2835 rc = dt_attr_get(env, parent, tla);
2839 tla->la_valid = LA_UID | LA_GID;
2840 rc = dt_attr_set(env, child, tla, handle);
2845 dt_write_unlock(env, parent);
2848 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2851 lfsck_ibits_unlock(&lh, LCK_EX);
2854 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired unmatched "
2855 "MDT-OST pair for: parent "DFID", child "DFID", OST-index %u, "
2856 "stripe-index %u, owner %u/%u: rc = %d\n",
2857 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
2858 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
2859 pla->la_uid, pla->la_gid, rc);
2864 /* If there are more than one MDT-objects claim as the OST-object's parent,
2865 * and the OST-object only recognizes one of them, then we need to generate
2866 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
2867 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
2868 struct lfsck_component *com,
2869 struct dt_object *parent,
2870 struct lfsck_layout_req *llr,
2874 struct lfsck_thread_info *info = lfsck_env_info(env);
2875 struct dt_allocation_hint *hint = &info->lti_hint;
2876 struct dt_object_format *dof = &info->lti_dof;
2877 struct ost_id *oi = &info->lti_oi;
2878 struct lfsck_instance *lfsck = com->lc_lfsck;
2879 struct dt_device *dev = lfsck->li_bottom;
2880 struct lu_device *d =
2881 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
2882 struct lu_object *o;
2883 struct lu_object *n;
2884 struct dt_object *child = NULL;
2885 struct thandle *handle = NULL;
2886 struct lov_mds_md_v1 *lmm;
2887 struct lov_ost_data_v1 *objs;
2888 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2890 struct lustre_handle lh = { 0 };
2891 struct lu_buf ea_buf;
2897 /* We use two separated transactions to repair the inconsistency.
2899 * 1) create the child (OST-object).
2900 * 2) update the parent LOV EA according to the child's FID.
2902 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
2903 * handled as orphan when the layout LFSCK run next time.
2905 * If 1) failed, but 2) succeed, then such OST-object will be re-created
2906 * as dangling referened case when the layout LFSCK run next time. */
2908 /* The 1st transaction. */
2909 o = lu_object_anon(env, d, NULL);
2911 GOTO(log, rc = PTR_ERR(o));
2913 n = lu_object_locate(o->lo_header, d->ld_type);
2914 if (unlikely(n == NULL)) {
2915 lu_object_put_nocache(env, o);
2917 GOTO(log, rc = -EINVAL);
2920 child = container_of(n, struct dt_object, do_lu);
2921 memset(hint, 0, sizeof(*hint));
2922 la->la_valid = LA_UID | LA_GID;
2923 memset(dof, 0, sizeof(*dof));
2925 handle = dt_trans_create(env, dev);
2927 GOTO(log, rc = PTR_ERR(handle));
2929 rc = dt_declare_create(env, child, la, hint, dof, handle);
2933 rc = dt_trans_start_local(env, dev, handle);
2937 rc = dt_create(env, child, la, hint, dof, handle);
2938 dt_trans_stop(env, dev, handle);
2943 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2944 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2949 /* The 2nd transaction. */
2951 /* XXX: Generally, we should use bottom device (OSD) to update parent
2952 * LOV EA. But because the LOD-object still references the wrong
2953 * OSP-object that should be detached after the parent's LOV EA
2954 * refreshed. Unfortunately, there is no suitable API for that.
2955 * So we have to make the LOD to re-load the OSP-object(s) via
2956 * replacing the LOV EA against the LOD-object.
2958 * Once the DNE2 patches have been landed, we can replace the
2959 * LOD device with the OSD device. LU-6230. */
2961 dev = lfsck->li_next;
2962 parent = lfsck_object_locate(dev, parent);
2964 GOTO(log, rc = PTR_ERR(parent));
2966 handle = dt_trans_create(env, dev);
2968 GOTO(log, rc = PTR_ERR(handle));
2970 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2971 LU_XATTR_REPLACE, handle);
2975 rc = dt_trans_start_local(env, dev, handle);
2979 dt_write_lock(env, parent, 0);
2980 if (unlikely(lfsck_is_dead_obj(parent)))
2981 GOTO(unlock, rc = 0);
2983 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2984 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2985 GOTO(unlock, rc = 0);
2988 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2989 * been verified in lfsck_layout_verify_header() already. If some
2990 * new magic introduced in the future, then layout LFSCK needs to
2991 * be updated also. */
2992 magic = le32_to_cpu(lmm->lmm_magic);
2993 if (magic == LOV_MAGIC_V1) {
2994 objs = &lmm->lmm_objects[llr->llr_lov_idx];
2996 LASSERT(magic == LOV_MAGIC_V3);
2998 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3001 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3002 index = le32_to_cpu(objs->l_ost_idx);
3003 rc = ostid_to_fid(&tfid, oi, index);
3004 /* Someone changed layout during the LFSCK, no need to repair then. */
3005 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3006 GOTO(unlock, rc = 0);
3008 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3009 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3010 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3011 objs->l_ost_gen = cpu_to_le32(0);
3012 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3013 lfsck_buf_init(&ea_buf, lmm,
3014 lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3016 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV,
3017 LU_XATTR_REPLACE, handle);
3019 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3022 dt_write_unlock(env, parent);
3026 dt_trans_stop(env, dev, handle);
3029 lfsck_ibits_unlock(&lh, LCK_EX);
3031 lfsck_object_put(env, child);
3033 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired multiple "
3034 "references for: parent "DFID", OST-index %u, stripe-index %u, "
3035 "owner %u/%u: rc = %d\n", lfsck_lfsck2name(lfsck), PFID(pfid),
3036 llr->llr_ost_idx, llr->llr_lov_idx, la->la_uid, la->la_gid, rc);
3041 /* If the MDT-object and the OST-object have different owner information,
3042 * then trust the MDT-object, because the normal chown/chgrp handle order
3043 * is from MDT to OST, and it is possible that some chown/chgrp operation
3044 * is partly done. */
3045 static int lfsck_layout_repair_owner(const struct lu_env *env,
3046 struct lfsck_component *com,
3047 struct dt_object *parent,
3048 struct lfsck_layout_req *llr,
3049 struct lu_attr *pla)
3051 struct lfsck_thread_info *info = lfsck_env_info(env);
3052 struct lu_attr *tla = &info->lti_la3;
3053 struct dt_object *child = llr->llr_child;
3054 struct dt_device *dev = lfsck_obj2dev(child);
3055 struct thandle *handle;
3059 handle = dt_trans_create(env, dev);
3061 GOTO(log, rc = PTR_ERR(handle));
3063 tla->la_uid = pla->la_uid;
3064 tla->la_gid = pla->la_gid;
3065 tla->la_valid = LA_UID | LA_GID;
3066 rc = dt_declare_attr_set(env, child, tla, handle);
3070 rc = dt_trans_start_local(env, dev, handle);
3074 /* Use the dt_object lock to serialize with destroy and attr_set. */
3075 dt_read_lock(env, parent, 0);
3076 if (unlikely(lfsck_is_dead_obj(parent)))
3077 GOTO(unlock, rc = 1);
3079 /* Get the latest parent's owner. */
3080 rc = dt_attr_get(env, parent, tla);
3084 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3085 if (unlikely(tla->la_uid != pla->la_uid ||
3086 tla->la_gid != pla->la_gid))
3087 GOTO(unlock, rc = 1);
3089 tla->la_valid = LA_UID | LA_GID;
3090 rc = dt_attr_set(env, child, tla, handle);
3095 dt_read_unlock(env, parent);
3098 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3101 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired inconsistent "
3102 "file owner for: parent "DFID", child "DFID", OST-index %u, "
3103 "stripe-index %u, owner %u/%u: rc = %d\n",
3104 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
3105 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
3106 pla->la_uid, pla->la_gid, rc);
3111 /* Check whether the OST-object correctly back points to the
3112 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3113 static int lfsck_layout_check_parent(const struct lu_env *env,
3114 struct lfsck_component *com,
3115 struct dt_object *parent,
3116 const struct lu_fid *pfid,
3117 const struct lu_fid *cfid,
3118 const struct lu_attr *pla,
3119 const struct lu_attr *cla,
3120 struct lfsck_layout_req *llr,
3121 struct lu_buf *lov_ea, __u32 idx)
3123 struct lfsck_thread_info *info = lfsck_env_info(env);
3124 struct lu_buf *buf = &info->lti_big_buf;
3125 struct dt_object *tobj;
3126 struct lov_mds_md_v1 *lmm;
3127 struct lov_ost_data_v1 *objs;
3128 struct lustre_handle lh = { 0 };
3135 if (fid_is_zero(pfid)) {
3136 /* client never wrote. */
3137 if (cla->la_size == 0 && cla->la_blocks == 0) {
3138 if (unlikely(cla->la_uid != pla->la_uid ||
3139 cla->la_gid != pla->la_gid))
3140 RETURN (LLIT_INCONSISTENT_OWNER);
3145 RETURN(LLIT_UNMATCHED_PAIR);
3148 if (unlikely(!fid_is_sane(pfid)))
3149 RETURN(LLIT_UNMATCHED_PAIR);
3151 if (lu_fid_eq(pfid, lu_object_fid(&parent->do_lu))) {
3152 if (llr->llr_lov_idx == idx)
3155 RETURN(LLIT_UNMATCHED_PAIR);
3158 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3160 RETURN(PTR_ERR(tobj));
3162 if (dt_object_exists(tobj) == 0 ||
3163 lfsck_is_dead_obj(tobj))
3164 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3166 if (!S_ISREG(lfsck_object_type(tobj)))
3167 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3169 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3170 * remote one on another MDT. Then check whether the given OST-object
3171 * is in such layout. If yes, it is multiple referenced, otherwise it
3172 * is unmatched referenced case. */
3173 rc = lfsck_layout_get_lovea(env, tobj, buf);
3174 if (rc == 0 || rc == -ENOENT)
3175 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3181 magic = le32_to_cpu(lmm->lmm_magic);
3182 if (magic == LOV_MAGIC_V1) {
3183 objs = &lmm->lmm_objects[0];
3185 LASSERT(magic == LOV_MAGIC_V3);
3186 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3189 count = le16_to_cpu(lmm->lmm_stripe_count);
3190 for (i = 0; i < count; i++, objs++) {
3191 struct lu_fid *tfid = &info->lti_fid2;
3192 struct ost_id *oi = &info->lti_oi;
3195 if (lovea_slot_is_dummy(objs))
3198 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3199 idx2 = le32_to_cpu(objs->l_ost_idx);
3200 rc = ostid_to_fid(tfid, oi, idx2);
3202 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3203 "invalid layout EA at the slot %d, index %u\n",
3204 lfsck_lfsck2name(com->lc_lfsck),
3205 PFID(pfid), i, idx2);
3207 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3210 if (lu_fid_eq(cfid, tfid)) {
3211 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
3212 MDS_INODELOCK_UPDATE |
3213 MDS_INODELOCK_LAYOUT |
3214 MDS_INODELOCK_XATTR,
3219 dt_read_lock(env, tobj, 0);
3221 /* For local MDT-object, re-check existence
3222 * after taken the lock. */
3223 if (!dt_object_remote(tobj)) {
3224 if (dt_object_exists(tobj) == 0 ||
3225 lfsck_is_dead_obj(tobj)) {
3226 rc = LLIT_UNMATCHED_PAIR;
3229 rc = LLIT_MULTIPLE_REFERENCED;
3235 /* For migration case, the new MDT-object and old
3236 * MDT-object may reference the same OST-object at
3237 * some migration internal time.
3239 * For remote MDT-object, the local MDT may not know
3240 * whether it has been removed or not. Try checking
3241 * for a non-existent xattr to check if this object
3242 * has been been removed or not. */
3243 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
3245 if (unlikely(rc == -ENOENT || rc >= 0)) {
3246 rc = LLIT_UNMATCHED_PAIR;
3247 } else if (rc == -ENODATA) {
3249 rc = LLIT_MULTIPLE_REFERENCED;
3256 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3259 if (lustre_handle_is_used(&lh)) {
3260 dt_read_unlock(env, tobj);
3261 lfsck_ibits_unlock(&lh, LCK_EX);
3265 lfsck_object_put(env, tobj);
3270 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
3271 struct lfsck_component *com,
3272 struct lfsck_assistant_req *lar)
3274 struct lfsck_layout_req *llr =
3275 container_of0(lar, struct lfsck_layout_req, llr_lar);
3276 struct lfsck_layout *lo = com->lc_file_ram;
3277 struct lfsck_thread_info *info = lfsck_env_info(env);
3278 struct filter_fid_old *pea = &info->lti_old_pfid;
3279 struct lu_fid *pfid = &info->lti_fid;
3280 struct lu_buf buf = { NULL };
3281 struct dt_object *parent;
3282 struct dt_object *child = llr->llr_child;
3283 struct lu_attr *pla = &info->lti_la;
3284 struct lu_attr *cla = &info->lti_la2;
3285 struct lfsck_instance *lfsck = com->lc_lfsck;
3286 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3287 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
3292 parent = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
3294 RETURN(PTR_ERR(parent));
3296 if (unlikely(lfsck_is_dead_obj(parent)))
3297 GOTO(put_parent, rc = 0);
3299 rc = dt_attr_get(env, parent, pla);
3303 rc = dt_attr_get(env, child, cla);
3304 if (rc == -ENOENT) {
3305 if (unlikely(lfsck_is_dead_obj(parent)))
3306 GOTO(put_parent, rc = 0);
3308 type = LLIT_DANGLING;
3315 lfsck_buf_init(&buf, pea, sizeof(struct filter_fid_old));
3316 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
3317 if (unlikely(rc >= 0 && rc != sizeof(struct filter_fid_old) &&
3318 rc != sizeof(struct filter_fid))) {
3319 type = LLIT_UNMATCHED_PAIR;
3323 if (rc < 0 && rc != -ENODATA)
3326 if (rc == -ENODATA) {
3329 fid_le_to_cpu(pfid, &pea->ff_parent);
3330 /* Currently, the filter_fid::ff_parent::f_ver is not the
3331 * real parent MDT-object's FID::f_ver, instead it is the
3332 * OST-object index in its parent MDT-object's layout EA. */
3333 idx = pfid->f_stripe_idx;
3337 rc = lfsck_layout_check_parent(env, com, parent, pfid,
3338 lu_object_fid(&child->do_lu),
3339 pla, cla, llr, &buf, idx);
3348 if (unlikely(cla->la_uid != pla->la_uid ||
3349 cla->la_gid != pla->la_gid)) {
3350 type = LLIT_INCONSISTENT_OWNER;
3355 if (bk->lb_param & LPF_DRYRUN) {
3356 if (type != LLIT_NONE)
3364 rc = lfsck_layout_repair_dangling(env, com, parent, llr, pla);
3366 case LLIT_UNMATCHED_PAIR:
3367 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
3370 case LLIT_MULTIPLE_REFERENCED:
3371 rc = lfsck_layout_repair_multiple_references(env, com, parent,
3374 case LLIT_INCONSISTENT_OWNER:
3375 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla);
3385 down_write(&com->lc_sem);
3387 struct lfsck_assistant_data *lad = com->lc_data;
3389 if (unlikely(lad->lad_exit)) {
3391 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
3392 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
3393 rc == -EHOSTUNREACH) {
3394 /* If cannot touch the target server,
3395 * mark the LFSCK as INCOMPLETE. */
3396 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
3397 "talk with OST %x: rc = %d\n",
3398 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
3399 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
3400 lo->ll_objs_skipped++;
3403 lfsck_layout_record_failure(env, lfsck, lo);
3405 } else if (rc > 0) {
3406 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
3407 "unknown type = %d\n", type);
3409 lo->ll_objs_repaired[type - 1]++;
3410 if (bk->lb_param & LPF_DRYRUN &&
3411 unlikely(lo->ll_pos_first_inconsistent == 0))
3412 lo->ll_pos_first_inconsistent =
3413 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
3416 up_write(&com->lc_sem);
3419 lfsck_object_put(env, parent);
3424 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
3425 struct lfsck_component *com)
3427 struct lfsck_assistant_data *lad = com->lc_data;
3428 struct lfsck_instance *lfsck = com->lc_lfsck;
3429 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3430 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
3431 struct lfsck_tgt_desc *ltd;
3435 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
3436 lfsck_lfsck2name(lfsck));
3438 spin_lock(<ds->ltd_lock);
3439 while (!list_empty(&lad->lad_ost_phase2_list)) {
3440 ltd = list_entry(lad->lad_ost_phase2_list.next,
3441 struct lfsck_tgt_desc,
3442 ltd_layout_phase_list);
3443 list_del_init(<d->ltd_layout_phase_list);
3444 if (bk->lb_param & LPF_ALL_TGT) {
3445 spin_unlock(<ds->ltd_lock);
3446 rc = lfsck_layout_scan_orphan(env, com, ltd);
3447 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3450 if (unlikely(lad->lad_exit ||
3451 !thread_is_running(&lfsck->li_thread)))
3453 spin_lock(<ds->ltd_lock);
3457 if (list_empty(&lad->lad_ost_phase1_list))
3461 spin_unlock(<ds->ltd_lock);
3463 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
3464 lfsck_lfsck2name(lfsck), rc);
3470 lfsck_layout_slave_async_interpret(const struct lu_env *env,
3471 struct ptlrpc_request *req,
3474 struct lfsck_layout_slave_async_args *llsaa = args;
3475 struct obd_export *exp = llsaa->llsaa_exp;
3476 struct lfsck_component *com = llsaa->llsaa_com;
3477 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
3478 struct lfsck_layout_slave_data *llsd = com->lc_data;
3479 struct lfsck_reply *lr = NULL;
3483 /* It is quite probably caused by target crash,
3484 * to make the LFSCK can go ahead, assume that
3485 * the target finished the LFSCK prcoessing. */
3488 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
3489 if (lr->lr_status != LS_SCANNING_PHASE1 &&
3490 lr->lr_status != LS_SCANNING_PHASE2)
3495 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
3496 "status %d\n", lfsck_lfsck2name(com->lc_lfsck),
3497 llst->llst_index, lr != NULL ? lr->lr_status : rc);
3499 lfsck_layout_llst_del(llsd, llst);
3502 lfsck_layout_llst_put(llst);
3503 lfsck_component_put(env, com);
3504 class_export_put(exp);
3509 static int lfsck_layout_async_query(const struct lu_env *env,
3510 struct lfsck_component *com,
3511 struct obd_export *exp,
3512 struct lfsck_layout_slave_target *llst,
3513 struct lfsck_request *lr,
3514 struct ptlrpc_request_set *set)
3516 struct lfsck_layout_slave_async_args *llsaa;
3517 struct ptlrpc_request *req;
3518 struct lfsck_request *tmp;
3522 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
3526 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
3528 ptlrpc_request_free(req);
3532 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3534 ptlrpc_request_set_replen(req);
3536 llsaa = ptlrpc_req_async_args(req);
3537 llsaa->llsaa_exp = exp;
3538 llsaa->llsaa_com = lfsck_component_get(com);
3539 llsaa->llsaa_llst = llst;
3540 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
3541 ptlrpc_set_add_req(set, req);
3546 static int lfsck_layout_async_notify(const struct lu_env *env,
3547 struct obd_export *exp,
3548 struct lfsck_request *lr,
3549 struct ptlrpc_request_set *set)
3551 struct ptlrpc_request *req;
3552 struct lfsck_request *tmp;
3556 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3560 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3562 ptlrpc_request_free(req);
3566 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3568 ptlrpc_request_set_replen(req);
3569 ptlrpc_set_add_req(set, req);
3575 lfsck_layout_slave_query_master(const struct lu_env *env,
3576 struct lfsck_component *com)
3578 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3579 struct lfsck_instance *lfsck = com->lc_lfsck;
3580 struct lfsck_layout_slave_data *llsd = com->lc_data;
3581 struct lfsck_layout_slave_target *llst;
3582 struct obd_export *exp;
3583 struct ptlrpc_request_set *set;
3588 set = ptlrpc_prep_set();
3590 GOTO(log, rc = -ENOMEM);
3592 memset(lr, 0, sizeof(*lr));
3593 lr->lr_event = LE_QUERY;
3594 lr->lr_active = LFSCK_TYPE_LAYOUT;
3596 llsd->llsd_touch_gen++;
3597 spin_lock(&llsd->llsd_lock);
3598 while (!list_empty(&llsd->llsd_master_list)) {
3599 llst = list_entry(llsd->llsd_master_list.next,
3600 struct lfsck_layout_slave_target,
3602 if (llst->llst_gen == llsd->llsd_touch_gen)
3605 llst->llst_gen = llsd->llsd_touch_gen;
3606 list_move_tail(&llst->llst_list,
3607 &llsd->llsd_master_list);
3608 atomic_inc(&llst->llst_ref);
3609 spin_unlock(&llsd->llsd_lock);
3611 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3614 lfsck_layout_llst_del(llsd, llst);
3615 lfsck_layout_llst_put(llst);
3616 spin_lock(&llsd->llsd_lock);
3620 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
3622 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3623 "query %s for layout: rc = %d\n",
3624 lfsck_lfsck2name(lfsck),
3625 exp->exp_obd->obd_name, rc);
3628 lfsck_layout_llst_put(llst);
3629 class_export_put(exp);
3631 spin_lock(&llsd->llsd_lock);
3633 spin_unlock(&llsd->llsd_lock);
3635 rc = ptlrpc_set_wait(set);
3636 ptlrpc_set_destroy(set);
3638 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
3641 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
3642 lfsck_lfsck2name(com->lc_lfsck), rc);
3648 lfsck_layout_slave_notify_master(const struct lu_env *env,
3649 struct lfsck_component *com,
3650 enum lfsck_events event, int result)
3652 struct lfsck_layout *lo = com->lc_file_ram;
3653 struct lfsck_instance *lfsck = com->lc_lfsck;
3654 struct lfsck_layout_slave_data *llsd = com->lc_data;
3655 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3656 struct lfsck_layout_slave_target *llst;
3657 struct obd_export *exp;
3658 struct ptlrpc_request_set *set;
3662 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
3663 lfsck_lfsck2name(com->lc_lfsck));
3665 set = ptlrpc_prep_set();
3669 memset(lr, 0, sizeof(*lr));
3670 lr->lr_event = event;
3671 lr->lr_flags = LEF_FROM_OST;
3672 lr->lr_status = result;
3673 lr->lr_index = lfsck_dev_idx(lfsck);
3674 lr->lr_active = LFSCK_TYPE_LAYOUT;
3675 lr->lr_flags2 = lo->ll_flags;
3676 llsd->llsd_touch_gen++;
3677 spin_lock(&llsd->llsd_lock);
3678 while (!list_empty(&llsd->llsd_master_list)) {
3679 llst = list_entry(llsd->llsd_master_list.next,
3680 struct lfsck_layout_slave_target,
3682 if (llst->llst_gen == llsd->llsd_touch_gen)
3685 llst->llst_gen = llsd->llsd_touch_gen;
3686 list_move_tail(&llst->llst_list,
3687 &llsd->llsd_master_list);
3688 atomic_inc(&llst->llst_ref);
3689 spin_unlock(&llsd->llsd_lock);
3691 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3694 lfsck_layout_llst_del(llsd, llst);
3695 lfsck_layout_llst_put(llst);
3696 spin_lock(&llsd->llsd_lock);
3700 rc = lfsck_layout_async_notify(env, exp, lr, set);
3702 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3703 "notify %s for layout: rc = %d\n",
3704 lfsck_lfsck2name(lfsck),
3705 exp->exp_obd->obd_name, rc);
3707 lfsck_layout_llst_put(llst);
3708 class_export_put(exp);
3709 spin_lock(&llsd->llsd_lock);
3711 spin_unlock(&llsd->llsd_lock);
3713 ptlrpc_set_wait(set);
3714 ptlrpc_set_destroy(set);
3720 * \ret -ENODATA: unrecognized stripe
3721 * \ret = 0 : recognized stripe
3722 * \ret < 0 : other failures
3724 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
3725 struct lfsck_component *com,
3726 struct lu_fid *cfid,
3727 struct lu_fid *pfid)
3729 struct lfsck_thread_info *info = lfsck_env_info(env);
3730 struct lu_buf *buf = &info->lti_big_buf;
3731 struct ost_id *oi = &info->lti_oi;
3732 struct dt_object *obj;
3733 struct lov_mds_md_v1 *lmm;
3734 struct lov_ost_data_v1 *objs;
3735 __u32 idx = pfid->f_stripe_idx;
3743 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3745 RETURN(PTR_ERR(obj));
3747 dt_read_lock(env, obj, 0);
3748 if (unlikely(dt_object_exists(obj) == 0 ||
3749 lfsck_is_dead_obj(obj)))
3750 GOTO(unlock, rc = -ENOENT);
3752 if (!S_ISREG(lfsck_object_type(obj)))
3753 GOTO(unlock, rc = -ENODATA);
3755 rc = lfsck_layout_get_lovea(env, obj, buf);
3760 GOTO(unlock, rc = -ENODATA);
3763 rc = lfsck_layout_verify_header(lmm);
3767 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
3768 * been verified in lfsck_layout_verify_header() already. If some
3769 * new magic introduced in the future, then layout LFSCK needs to
3770 * be updated also. */
3771 magic = le32_to_cpu(lmm->lmm_magic);
3772 if (magic == LOV_MAGIC_V1) {
3773 objs = &lmm->lmm_objects[0];
3775 LASSERT(magic == LOV_MAGIC_V3);
3776 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3779 fid_to_ostid(cfid, oi);
3780 count = le16_to_cpu(lmm->lmm_stripe_count);
3781 for (i = 0; i < count; i++, objs++) {
3784 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
3785 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
3786 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
3789 GOTO(unlock, rc = -ENODATA);
3792 dt_read_unlock(env, obj);
3793 lfsck_object_put(env, obj);
3799 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
3800 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
3801 * layout EA from MDT to OST. On one hand, the OST no need to understand
3802 * the layout EA structure; on the other hand, it may cause trouble when
3803 * transfer large layout EA from MDT to OST via normal OUT RPC.
3805 * \ret > 0: unrecognized stripe
3806 * \ret = 0: recognized stripe
3807 * \ret < 0: other failures
3809 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
3810 struct lfsck_component *com,
3811 struct lu_fid *cfid,
3812 struct lu_fid *pfid)
3814 struct lfsck_instance *lfsck = com->lc_lfsck;
3815 struct obd_device *obd = lfsck->li_obd;
3816 struct seq_server_site *ss = lfsck_dev_site(lfsck);
3817 struct obd_export *exp = NULL;
3818 struct ptlrpc_request *req = NULL;
3819 struct lfsck_request *lr;
3820 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
3824 if (unlikely(fid_is_idif(pfid)))
3827 fld_range_set_any(range);
3828 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
3830 RETURN(rc == -ENOENT ? 1 : rc);
3832 if (unlikely(!fld_range_is_mdt(range)))
3835 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
3836 if (unlikely(exp == NULL))
3839 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
3840 GOTO(out, rc = -EOPNOTSUPP);
3842 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3844 GOTO(out, rc = -ENOMEM);
3846 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3848 ptlrpc_request_free(req);
3853 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3854 memset(lr, 0, sizeof(*lr));
3855 lr->lr_event = LE_PAIRS_VERIFY;
3856 lr->lr_active = LFSCK_TYPE_LAYOUT;
3857 lr->lr_fid = *cfid; /* OST-object itself FID. */
3858 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
3860 ptlrpc_request_set_replen(req);
3861 rc = ptlrpc_queue_wait(req);
3862 ptlrpc_req_finished(req);
3864 if (rc == -ENOENT || rc == -ENODATA)
3871 class_export_put(exp);
3876 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
3877 struct lfsck_component *com,
3878 struct lfsck_request *lr)
3880 struct dt_object *obj;
3884 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lr->lr_fid);
3886 GOTO(log, rc = PTR_ERR(obj));
3888 dt_write_lock(env, obj, 0);
3889 if (unlikely(dt_object_exists(obj) == 0 ||
3890 lfsck_is_dead_obj(obj)))
3891 GOTO(unlock, rc = 0);
3893 rc = __lfsck_layout_update_pfid(env, obj, &lr->lr_fid2,
3899 dt_write_unlock(env, obj);
3900 lfsck_object_put(env, obj);
3903 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
3904 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
3905 PFID(&lr->lr_fid), PFID(&lr->lr_fid2), rc);
3912 static void lfsck_layout_slave_quit(const struct lu_env *env,
3913 struct lfsck_component *com);
3915 static int lfsck_layout_reset(const struct lu_env *env,
3916 struct lfsck_component *com, bool init)
3918 struct lfsck_layout *lo = com->lc_file_ram;
3921 down_write(&com->lc_sem);
3923 memset(lo, 0, com->lc_file_size);
3925 __u32 count = lo->ll_success_count;
3926 __u64 last_time = lo->ll_time_last_complete;
3928 memset(lo, 0, com->lc_file_size);
3929 lo->ll_success_count = count;
3930 lo->ll_time_last_complete = last_time;
3933 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
3934 lo->ll_status = LS_INIT;
3936 if (com->lc_lfsck->li_master) {
3937 struct lfsck_assistant_data *lad = com->lc_data;
3939 lad->lad_incomplete = 0;
3940 CFS_RESET_BITMAP(lad->lad_bitmap);
3943 rc = lfsck_layout_store(env, com);
3944 up_write(&com->lc_sem);
3946 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
3947 lfsck_lfsck2name(com->lc_lfsck), rc);
3952 static void lfsck_layout_fail(const struct lu_env *env,
3953 struct lfsck_component *com, bool new_checked)
3955 struct lfsck_layout *lo = com->lc_file_ram;
3957 down_write(&com->lc_sem);
3959 com->lc_new_checked++;
3960 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
3961 up_write(&com->lc_sem);
3964 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
3965 struct lfsck_component *com, bool init)
3967 struct lfsck_instance *lfsck = com->lc_lfsck;
3968 struct lfsck_layout *lo = com->lc_file_ram;
3972 rc = lfsck_checkpoint_generic(env, com);
3974 return rc > 0 ? 0 : rc;
3977 down_write(&com->lc_sem);
3979 lo->ll_pos_latest_start =
3980 lfsck->li_pos_checkpoint.lp_oit_cookie;
3982 lo->ll_pos_last_checkpoint =
3983 lfsck->li_pos_checkpoint.lp_oit_cookie;
3984 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3985 HALF_SEC - lfsck->li_time_last_checkpoint);
3986 lo->ll_time_last_checkpoint = cfs_time_current_sec();
3987 lo->ll_objs_checked_phase1 += com->lc_new_checked;
3988 com->lc_new_checked = 0;
3991 rc = lfsck_layout_store(env, com);
3992 up_write(&com->lc_sem);
3994 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
3995 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3996 lfsck->li_pos_current.lp_oit_cookie, rc);
4001 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
4002 struct lfsck_component *com, bool init)
4004 struct lfsck_instance *lfsck = com->lc_lfsck;
4005 struct lfsck_layout *lo = com->lc_file_ram;
4008 if (com->lc_new_checked == 0 && !init)
4011 down_write(&com->lc_sem);
4013 lo->ll_pos_latest_start =
4014 lfsck->li_pos_checkpoint.lp_oit_cookie;
4016 lo->ll_pos_last_checkpoint =
4017 lfsck->li_pos_checkpoint.lp_oit_cookie;
4018 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4019 HALF_SEC - lfsck->li_time_last_checkpoint);
4020 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4021 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4022 com->lc_new_checked = 0;
4025 rc = lfsck_layout_store(env, com);
4026 up_write(&com->lc_sem);
4028 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
4029 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
4030 lfsck->li_pos_current.lp_oit_cookie, rc);
4035 static int lfsck_layout_prep(const struct lu_env *env,
4036 struct lfsck_component *com,
4037 struct lfsck_start *start)
4039 struct lfsck_instance *lfsck = com->lc_lfsck;
4040 struct lfsck_layout *lo = com->lc_file_ram;
4041 struct lfsck_position *pos = &com->lc_pos_start;
4043 fid_zero(&pos->lp_dir_parent);
4044 pos->lp_dir_cookie = 0;
4045 if (lo->ll_status == LS_COMPLETED ||
4046 lo->ll_status == LS_PARTIAL ||
4047 /* To handle orphan, must scan from the beginning. */
4048 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
4051 rc = lfsck_layout_reset(env, com, false);
4053 rc = lfsck_set_param(env, lfsck, start, true);
4056 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
4057 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4063 down_write(&com->lc_sem);
4064 lo->ll_time_latest_start = cfs_time_current_sec();
4065 spin_lock(&lfsck->li_lock);
4066 if (lo->ll_flags & LF_SCANNED_ONCE) {
4067 if (!lfsck->li_drop_dryrun ||
4068 lo->ll_pos_first_inconsistent == 0) {
4069 lo->ll_status = LS_SCANNING_PHASE2;
4070 list_move_tail(&com->lc_link,
4071 &lfsck->li_list_double_scan);
4072 pos->lp_oit_cookie = 0;
4076 lo->ll_status = LS_SCANNING_PHASE1;
4077 lo->ll_run_time_phase1 = 0;
4078 lo->ll_run_time_phase2 = 0;
4079 lo->ll_objs_checked_phase1 = 0;
4080 lo->ll_objs_checked_phase2 = 0;
4081 lo->ll_objs_failed_phase1 = 0;
4082 lo->ll_objs_failed_phase2 = 0;
4083 for (i = 0; i < LLIT_MAX; i++)
4084 lo->ll_objs_repaired[i] = 0;
4086 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4087 fid_zero(&com->lc_fid_latest_scanned_phase2);
4090 lo->ll_status = LS_SCANNING_PHASE1;
4091 if (!lfsck->li_drop_dryrun ||
4092 lo->ll_pos_first_inconsistent == 0)
4093 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
4095 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4097 spin_unlock(&lfsck->li_lock);
4098 up_write(&com->lc_sem);
4103 static int lfsck_layout_slave_prep(const struct lu_env *env,
4104 struct lfsck_component *com,
4105 struct lfsck_start_param *lsp)
4107 struct lfsck_layout_slave_data *llsd = com->lc_data;
4108 struct lfsck_instance *lfsck = com->lc_lfsck;
4109 struct lfsck_layout *lo = com->lc_file_ram;
4110 struct lfsck_start *start = lsp->lsp_start;
4113 rc = lfsck_layout_prep(env, com, start);
4117 if (lo->ll_flags & LF_CRASHED_LASTID &&
4118 list_empty(&llsd->llsd_master_list)) {
4119 LASSERT(lfsck->li_out_notify != NULL);
4121 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4122 LE_LASTID_REBUILDING);
4125 if (!lsp->lsp_index_valid)
4128 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
4129 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
4130 LASSERT(!llsd->llsd_rbtree_valid);
4132 write_lock(&llsd->llsd_rb_lock);
4133 rc = lfsck_rbtree_setup(env, com);
4134 write_unlock(&llsd->llsd_rb_lock);
4137 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
4138 LPU64"]\n", lfsck_lfsck2name(lfsck),
4139 com->lc_pos_start.lp_oit_cookie);
4144 static int lfsck_layout_master_prep(const struct lu_env *env,
4145 struct lfsck_component *com,
4146 struct lfsck_start_param *lsp)
4151 rc = lfsck_layout_load_bitmap(env, com);
4153 rc = lfsck_layout_reset(env, com, false);
4155 rc = lfsck_set_param(env, com->lc_lfsck,
4156 lsp->lsp_start, true);
4162 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
4166 rc = lfsck_start_assistant(env, com, lsp);
4171 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
4172 LPU64"]\n", lfsck_lfsck2name(com->lc_lfsck),
4173 com->lc_pos_start.lp_oit_cookie);
4178 /* Pre-fetch the attribute for each stripe in the given layout EA. */
4179 static int lfsck_layout_scan_stripes(const struct lu_env *env,
4180 struct lfsck_component *com,
4181 struct dt_object *parent,
4182 struct lov_mds_md_v1 *lmm)
4184 struct lfsck_thread_info *info = lfsck_env_info(env);
4185 struct lfsck_instance *lfsck = com->lc_lfsck;
4186 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4187 struct lfsck_layout *lo = com->lc_file_ram;
4188 struct lfsck_assistant_data *lad = com->lc_data;
4189 struct lfsck_layout_object *llo = NULL;
4190 struct lov_ost_data_v1 *objs;
4191 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4192 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4193 struct ptlrpc_thread *athread = &lad->lad_thread;
4194 struct l_wait_info lwi = { 0 };
4202 lfsck_buf_init(&buf, &info->lti_old_pfid,
4203 sizeof(struct filter_fid_old));
4204 count = le16_to_cpu(lmm->lmm_stripe_count);
4205 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
4206 * been verified in lfsck_layout_verify_header() already. If some
4207 * new magic introduced in the future, then layout LFSCK needs to
4208 * be updated also. */
4209 magic = le32_to_cpu(lmm->lmm_magic);
4210 if (magic == LOV_MAGIC_V1) {
4211 objs = &lmm->lmm_objects[0];
4213 LASSERT(magic == LOV_MAGIC_V3);
4214 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4217 for (i = 0; i < count; i++, objs++) {
4218 struct lu_fid *fid = &info->lti_fid;
4219 struct ost_id *oi = &info->lti_oi;
4220 struct lfsck_layout_req *llr;
4221 struct lfsck_tgt_desc *tgt = NULL;
4222 struct dt_object *cobj = NULL;
4224 bool wakeup = false;
4226 if (unlikely(lovea_slot_is_dummy(objs)))
4229 l_wait_event(mthread->t_ctl_waitq,
4230 lad->lad_prefetched < bk->lb_async_windows ||
4231 !thread_is_running(mthread) ||
4232 thread_is_stopped(athread),
4235 if (unlikely(!thread_is_running(mthread)) ||
4236 thread_is_stopped(athread))
4239 if (unlikely(lfsck_is_dead_obj(parent)))
4242 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4243 index = le32_to_cpu(objs->l_ost_idx);
4244 rc = ostid_to_fid(fid, oi, index);
4246 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
4247 ": "DOSTID", idx:%u\n", lfsck_lfsck2name(lfsck),
4248 PFID(lfsck_dto2fid(parent)), POSTID(oi), index);
4252 tgt = lfsck_tgt_get(ltds, index);
4253 if (unlikely(tgt == NULL)) {
4254 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
4255 "did not join the layout LFSCK\n",
4256 lfsck_lfsck2name(lfsck), index);
4257 lfsck_lad_set_bitmap(env, com, index);
4261 /* There is potential deadlock race condition between object
4262 * destroy and layout LFSCK. Consider the following scenario:
4264 * 1) The LFSCK thread obtained the parent object firstly, at
4265 * that time, the parent object has not been destroyed yet.
4267 * 2) One RPC service thread destroyed the parent and all its
4268 * children objects. Because the LFSCK is referencing the
4269 * parent object, then the parent object will be marked as
4270 * dying in RAM. On the other hand, the parent object is
4271 * referencing all its children objects, then all children
4272 * objects will be marked as dying in RAM also.
4274 * 3) The LFSCK thread tries to find some child object with
4275 * the parent object referenced. Then it will find that the
4276 * child object is dying. According to the object visibility
4277 * rules: the object with dying flag cannot be returned to
4278 * others. So the LFSCK thread has to wait until the dying
4279 * object has been purged from RAM, then it can allocate a
4280 * new object (with the same FID) in RAM. Unfortunately, the
4281 * LFSCK thread itself is referencing the parent object, and
4282 * cause the parent object cannot be purged, then cause the
4283 * child object cannot be purged also. So the LFSCK thread
4284 * will fall into deadlock.
4286 * We introduce non-blocked version lu_object_find() to allow
4287 * the LFSCK thread to return failure immediately (instead of
4288 * wait) when it finds dying (child) object, then the LFSCK
4289 * thread can check whether the parent object is dying or not.
4290 * So avoid above deadlock. LU-5395 */
4291 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
4293 if (lfsck_is_dead_obj(parent)) {
4303 rc = dt_declare_attr_get(env, cobj);
4307 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
4312 llo = lfsck_layout_object_init(env, parent,
4313 lfsck->li_pos_current.lp_oit_cookie);
4320 llr = lfsck_layout_assistant_req_init(llo,
4321 lfsck_dto2fid(parent),
4329 spin_lock(&lad->lad_lock);
4330 if (lad->lad_assistant_status < 0) {
4331 spin_unlock(&lad->lad_lock);
4332 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
4334 RETURN(lad->lad_assistant_status);
4337 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
4338 if (lad->lad_prefetched == 0)
4341 lad->lad_prefetched++;
4342 spin_unlock(&lad->lad_lock);
4344 wake_up_all(&athread->t_ctl_waitq);
4347 down_write(&com->lc_sem);
4348 com->lc_new_checked++;
4350 lfsck_layout_record_failure(env, lfsck, lo);
4351 up_write(&com->lc_sem);
4353 if (cobj != NULL && !IS_ERR(cobj))
4354 lfsck_object_put(env, cobj);
4356 if (likely(tgt != NULL))
4359 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4366 if (llo != NULL && !IS_ERR(llo))
4367 lfsck_layout_object_put(env, llo);
4372 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
4373 * the OST-object's attribute and generate an structure lfsck_layout_req on the
4374 * list ::lad_req_list.
4376 * For each request on above list, the lfsck_layout_assistant thread compares
4377 * the OST side attribute with local attribute, if inconsistent, then repair it.
4379 * All above processing is async mode with pipeline. */
4380 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
4381 struct lfsck_component *com,
4382 struct dt_object *obj)
4384 struct lfsck_thread_info *info = lfsck_env_info(env);
4385 struct ost_id *oi = &info->lti_oi;
4386 struct lfsck_layout *lo = com->lc_file_ram;
4387 struct lfsck_assistant_data *lad = com->lc_data;
4388 struct lfsck_instance *lfsck = com->lc_lfsck;
4389 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4390 struct thandle *handle = NULL;
4391 struct lu_buf *buf = &info->lti_big_buf;
4392 struct lov_mds_md_v1 *lmm = NULL;
4393 struct dt_device *dev = lfsck_obj2dev(obj);
4394 struct lustre_handle lh = { 0 };
4395 struct lu_buf ea_buf = { NULL };
4398 bool locked = false;
4399 bool stripe = false;
4400 bool bad_oi = false;
4403 if (!S_ISREG(lfsck_object_type(obj)))
4406 if (lad->lad_assistant_status < 0)
4407 GOTO(out, rc = -ESRCH);
4409 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
4410 lmm_oi_cpu_to_le(oi, oi);
4411 dt_read_lock(env, obj, 0);
4415 if (dt_object_exists(obj) == 0 ||
4416 lfsck_is_dead_obj(obj))
4419 rc = lfsck_layout_get_lovea(env, obj, buf);
4425 rc = lfsck_layout_verify_header(lmm);
4426 /* If the LOV EA crashed, then it is possible to be rebuilt later
4427 * when handle orphan OST-objects. */
4431 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0)
4432 GOTO(out, stripe = true);
4434 /* Inconsistent lmm_oi, should be repaired. */
4438 if (bk->lb_param & LPF_DRYRUN) {
4439 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4441 GOTO(out, stripe = true);
4444 if (!lustre_handle_is_used(&lh)) {
4445 dt_read_unlock(env, obj);
4447 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4448 MDS_INODELOCK_LAYOUT |
4449 MDS_INODELOCK_XATTR, LCK_EX);
4453 handle = dt_trans_create(env, dev);
4455 GOTO(out, rc = PTR_ERR(handle));
4457 lfsck_buf_init(&ea_buf, lmm, size);
4458 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4459 LU_XATTR_REPLACE, handle);
4463 rc = dt_trans_start_local(env, dev, handle);
4467 dt_write_lock(env, obj, 0);
4473 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4474 LU_XATTR_REPLACE, handle);
4478 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4480 GOTO(out, stripe = true);
4484 if (lustre_handle_is_used(&lh))
4485 dt_write_unlock(env, obj);
4487 dt_read_unlock(env, obj);
4490 if (handle != NULL && !IS_ERR(handle))
4491 dt_trans_stop(env, dev, handle);
4493 lfsck_ibits_unlock(&lh, LCK_EX);
4496 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
4497 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
4498 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
4499 PFID(lfsck_dto2fid(obj)), rc);
4502 rc = lfsck_layout_scan_stripes(env, com, obj, lmm);
4504 down_write(&com->lc_sem);
4505 com->lc_new_checked++;
4507 lfsck_layout_record_failure(env, lfsck, lo);
4508 up_write(&com->lc_sem);
4514 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
4515 struct lfsck_component *com,
4516 struct dt_object *obj)
4518 struct lfsck_instance *lfsck = com->lc_lfsck;
4519 struct lfsck_layout *lo = com->lc_file_ram;
4520 const struct lu_fid *fid = lfsck_dto2fid(obj);
4521 struct lfsck_layout_slave_data *llsd = com->lc_data;
4522 struct lfsck_layout_seq *lls;
4528 LASSERT(llsd != NULL);
4530 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
4531 cfs_fail_val == lfsck_dev_idx(lfsck)) {
4532 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
4534 struct ptlrpc_thread *thread = &lfsck->li_thread;
4536 l_wait_event(thread->t_ctl_waitq,
4537 !thread_is_running(thread),
4541 lfsck_rbtree_update_bitmap(env, com, fid, false);
4543 down_write(&com->lc_sem);
4544 if (fid_is_idif(fid))
4546 else if (!fid_is_norm(fid) ||
4547 !fid_is_for_ostobj(env, lfsck, obj, fid))
4548 GOTO(unlock, rc = 0);
4551 com->lc_new_checked++;
4553 lls = lfsck_layout_seq_lookup(llsd, seq);
4556 if (unlikely(lls == NULL))
4557 GOTO(unlock, rc = -ENOMEM);
4559 INIT_LIST_HEAD(&lls->lls_list);
4561 rc = lfsck_layout_lastid_load(env, com, lls);
4563 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4564 "load LAST_ID for "LPX64": rc = %d\n",
4565 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
4566 lo->ll_objs_failed_phase1++;
4571 lfsck_layout_seq_insert(llsd, lls);
4574 if (unlikely(fid_is_last_id(fid)))
4575 GOTO(unlock, rc = 0);
4577 if (fid_is_idif(fid))
4578 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
4582 if (oid > lls->lls_lastid_known)
4583 lls->lls_lastid_known = oid;
4585 if (oid > lls->lls_lastid) {
4586 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
4587 /* OFD may create new objects during LFSCK scanning. */
4588 rc = lfsck_layout_lastid_reload(env, com, lls);
4589 if (unlikely(rc != 0)) {
4590 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4591 "reload LAST_ID for "LPX64": rc = %d\n",
4592 lfsck_lfsck2name(com->lc_lfsck),
4598 if (oid <= lls->lls_lastid ||
4599 lo->ll_flags & LF_CRASHED_LASTID)
4600 GOTO(unlock, rc = 0);
4602 LASSERT(lfsck->li_out_notify != NULL);
4604 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4605 LE_LASTID_REBUILDING);
4606 lo->ll_flags |= LF_CRASHED_LASTID;
4608 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
4609 "LAST_ID file (2) for the sequence "LPX64
4610 ", old value "LPU64", known value "LPU64"\n",
4611 lfsck_lfsck2name(lfsck), lls->lls_seq,
4612 lls->lls_lastid, oid);
4615 lls->lls_lastid = oid;
4619 GOTO(unlock, rc = 0);
4622 up_write(&com->lc_sem);
4627 static int lfsck_layout_exec_dir(const struct lu_env *env,
4628 struct lfsck_component *com,
4629 struct lu_dirent *ent, __u16 type)
4634 static int lfsck_layout_master_post(const struct lu_env *env,
4635 struct lfsck_component *com,
4636 int result, bool init)
4638 struct lfsck_instance *lfsck = com->lc_lfsck;
4639 struct lfsck_layout *lo = com->lc_file_ram;
4643 lfsck_post_generic(env, com, &result);
4645 down_write(&com->lc_sem);
4646 spin_lock(&lfsck->li_lock);
4648 lo->ll_pos_last_checkpoint =
4649 lfsck->li_pos_checkpoint.lp_oit_cookie;
4652 if (lo->ll_flags & LF_INCOMPLETE)
4653 lo->ll_status = LS_PARTIAL;
4655 lo->ll_status = LS_SCANNING_PHASE2;
4656 lo->ll_flags |= LF_SCANNED_ONCE;
4657 lo->ll_flags &= ~LF_UPGRADE;
4658 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4659 } else if (result == 0) {
4660 if (lfsck->li_status != 0)
4661 lo->ll_status = lfsck->li_status;
4663 lo->ll_status = LS_STOPPED;
4664 if (lo->ll_status != LS_PAUSED)
4665 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4667 lo->ll_status = LS_FAILED;
4668 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4670 spin_unlock(&lfsck->li_lock);
4673 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4674 HALF_SEC - lfsck->li_time_last_checkpoint);
4675 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4676 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4677 com->lc_new_checked = 0;
4680 rc = lfsck_layout_store(env, com);
4681 up_write(&com->lc_sem);
4683 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
4684 lfsck_lfsck2name(lfsck), rc);
4689 static int lfsck_layout_slave_post(const struct lu_env *env,
4690 struct lfsck_component *com,
4691 int result, bool init)
4693 struct lfsck_instance *lfsck = com->lc_lfsck;
4694 struct lfsck_layout *lo = com->lc_file_ram;
4698 rc = lfsck_layout_lastid_store(env, com);
4702 LASSERT(lfsck->li_out_notify != NULL);
4704 down_write(&com->lc_sem);
4705 spin_lock(&lfsck->li_lock);
4707 lo->ll_pos_last_checkpoint =
4708 lfsck->li_pos_checkpoint.lp_oit_cookie;
4711 lo->ll_status = LS_SCANNING_PHASE2;
4712 lo->ll_flags |= LF_SCANNED_ONCE;
4713 if (lo->ll_flags & LF_CRASHED_LASTID) {
4715 lo->ll_flags &= ~LF_CRASHED_LASTID;
4717 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
4718 "crashed LAST_ID files successfully\n",
4719 lfsck_lfsck2name(lfsck));
4721 lo->ll_flags &= ~LF_UPGRADE;
4722 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4723 } else if (result == 0) {
4724 if (lfsck->li_status != 0)
4725 lo->ll_status = lfsck->li_status;
4727 lo->ll_status = LS_STOPPED;
4728 if (lo->ll_status != LS_PAUSED)
4729 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4731 lo->ll_status = LS_FAILED;
4732 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4734 spin_unlock(&lfsck->li_lock);
4737 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4741 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4742 HALF_SEC - lfsck->li_time_last_checkpoint);
4743 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4744 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4745 com->lc_new_checked = 0;
4748 rc = lfsck_layout_store(env, com);
4749 up_write(&com->lc_sem);
4751 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
4753 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
4754 lfsck_lfsck2name(lfsck), rc);
4759 static int lfsck_layout_dump(const struct lu_env *env,
4760 struct lfsck_component *com, struct seq_file *m)
4762 struct lfsck_instance *lfsck = com->lc_lfsck;
4763 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4764 struct lfsck_layout *lo = com->lc_file_ram;
4767 down_read(&com->lc_sem);
4768 seq_printf(m, "name: lfsck_layout\n"
4774 lfsck_status2names(lo->ll_status));
4776 rc = lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
4780 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4784 rc = lfsck_time_dump(m, lo->ll_time_last_complete,
4789 rc = lfsck_time_dump(m, lo->ll_time_latest_start,
4794 rc = lfsck_time_dump(m, lo->ll_time_last_checkpoint,
4799 seq_printf(m, "latest_start_position: "LPU64"\n"
4800 "last_checkpoint_position: "LPU64"\n"
4801 "first_failure_position: "LPU64"\n",
4802 lo->ll_pos_latest_start,
4803 lo->ll_pos_last_checkpoint,
4804 lo->ll_pos_first_inconsistent);
4806 seq_printf(m, "success_count: %u\n"
4807 "repaired_dangling: "LPU64"\n"
4808 "repaired_unmatched_pair: "LPU64"\n"
4809 "repaired_multiple_referenced: "LPU64"\n"
4810 "repaired_orphan: "LPU64"\n"
4811 "repaired_inconsistent_owner: "LPU64"\n"
4812 "repaired_others: "LPU64"\n"
4813 "skipped: "LPU64"\n"
4814 "failed_phase1: "LPU64"\n"
4815 "failed_phase2: "LPU64"\n",
4816 lo->ll_success_count,
4817 lo->ll_objs_repaired[LLIT_DANGLING - 1],
4818 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
4819 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
4820 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
4821 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
4822 lo->ll_objs_repaired[LLIT_OTHERS - 1],
4823 lo->ll_objs_skipped,
4824 lo->ll_objs_failed_phase1,
4825 lo->ll_objs_failed_phase2);
4827 if (lo->ll_status == LS_SCANNING_PHASE1) {
4829 const struct dt_it_ops *iops;
4830 cfs_duration_t duration = cfs_time_current() -
4831 lfsck->li_time_last_checkpoint;
4832 __u64 checked = lo->ll_objs_checked_phase1 +
4833 com->lc_new_checked;
4834 __u64 speed = checked;
4835 __u64 new_checked = com->lc_new_checked *
4836 msecs_to_jiffies(MSEC_PER_SEC);
4837 __u32 rtime = lo->ll_run_time_phase1 +
4838 cfs_duration_sec(duration + HALF_SEC);
4841 do_div(new_checked, duration);
4843 do_div(speed, rtime);
4844 seq_printf(m, "checked_phase1: "LPU64"\n"
4845 "checked_phase2: "LPU64"\n"
4846 "run_time_phase1: %u seconds\n"
4847 "run_time_phase2: %u seconds\n"
4848 "average_speed_phase1: "LPU64" items/sec\n"
4849 "average_speed_phase2: N/A\n"
4850 "real-time_speed_phase1: "LPU64" items/sec\n"
4851 "real-time_speed_phase2: N/A\n",
4853 lo->ll_objs_checked_phase2,
4855 lo->ll_run_time_phase2,
4859 LASSERT(lfsck->li_di_oit != NULL);
4861 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4863 /* The low layer otable-based iteration position may NOT
4864 * exactly match the layout-based directory traversal
4865 * cookie. Generally, it is not a serious issue. But the
4866 * caller should NOT make assumption on that. */
4867 pos = iops->store(env, lfsck->li_di_oit);
4868 if (!lfsck->li_current_oit_processed)
4870 seq_printf(m, "current_position: "LPU64"\n", pos);
4872 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
4873 cfs_duration_t duration = cfs_time_current() -
4874 lfsck->li_time_last_checkpoint;
4875 __u64 checked = lo->ll_objs_checked_phase2 +
4876 com->lc_new_checked;
4877 __u64 speed1 = lo->ll_objs_checked_phase1;
4878 __u64 speed2 = checked;
4879 __u64 new_checked = com->lc_new_checked *
4880 msecs_to_jiffies(MSEC_PER_SEC);
4881 __u32 rtime = lo->ll_run_time_phase2 +
4882 cfs_duration_sec(duration + HALF_SEC);
4885 do_div(new_checked, duration);
4886 if (lo->ll_run_time_phase1 != 0)
4887 do_div(speed1, lo->ll_run_time_phase1);
4889 do_div(speed2, rtime);
4890 rc = seq_printf(m, "checked_phase1: "LPU64"\n"
4891 "checked_phase2: "LPU64"\n"
4892 "run_time_phase1: %u seconds\n"
4893 "run_time_phase2: %u seconds\n"
4894 "average_speed_phase1: "LPU64" items/sec\n"
4895 "average_speed_phase2: "LPU64" items/sec\n"
4896 "real-time_speed_phase1: N/A\n"
4897 "real-time_speed_phase2: "LPU64" items/sec\n"
4898 "current_position: "DFID"\n",
4899 lo->ll_objs_checked_phase1,
4901 lo->ll_run_time_phase1,
4906 PFID(&com->lc_fid_latest_scanned_phase2));
4911 __u64 speed1 = lo->ll_objs_checked_phase1;
4912 __u64 speed2 = lo->ll_objs_checked_phase2;
4914 if (lo->ll_run_time_phase1 != 0)
4915 do_div(speed1, lo->ll_run_time_phase1);
4916 if (lo->ll_run_time_phase2 != 0)
4917 do_div(speed2, lo->ll_run_time_phase2);
4918 seq_printf(m, "checked_phase1: "LPU64"\n"
4919 "checked_phase2: "LPU64"\n"
4920 "run_time_phase1: %u seconds\n"
4921 "run_time_phase2: %u seconds\n"
4922 "average_speed_phase1: "LPU64" items/sec\n"
4923 "average_speed_phase2: "LPU64" objs/sec\n"
4924 "real-time_speed_phase1: N/A\n"
4925 "real-time_speed_phase2: N/A\n"
4926 "current_position: N/A\n",
4927 lo->ll_objs_checked_phase1,
4928 lo->ll_objs_checked_phase2,
4929 lo->ll_run_time_phase1,
4930 lo->ll_run_time_phase2,
4935 up_read(&com->lc_sem);
4940 static int lfsck_layout_master_double_scan(const struct lu_env *env,
4941 struct lfsck_component *com)
4943 struct lfsck_layout *lo = com->lc_file_ram;
4944 struct lfsck_assistant_data *lad = com->lc_data;
4945 struct lfsck_instance *lfsck = com->lc_lfsck;
4946 struct lfsck_tgt_descs *ltds;
4947 struct lfsck_tgt_desc *ltd;
4948 struct lfsck_tgt_desc *next;
4951 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
4953 if (thread_is_stopped(&lad->lad_thread)) {
4954 LASSERT(list_empty(&lad->lad_req_list));
4955 LASSERT(list_empty(&lad->lad_ost_phase1_list));
4956 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4958 ltds = &lfsck->li_ost_descs;
4959 spin_lock(<ds->ltd_lock);
4960 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
4961 ltd_layout_phase_list) {
4962 list_del_init(<d->ltd_layout_phase_list);
4964 spin_unlock(<ds->ltd_lock);
4966 ltds = &lfsck->li_mdt_descs;
4967 spin_lock(<ds->ltd_lock);
4968 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4969 ltd_layout_phase_list) {
4970 list_del_init(<d->ltd_layout_phase_list);
4972 spin_unlock(<ds->ltd_lock);
4978 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
4979 struct lfsck_component *com)
4981 struct lfsck_instance *lfsck = com->lc_lfsck;
4982 struct lfsck_layout_slave_data *llsd = com->lc_data;
4983 struct lfsck_layout *lo = com->lc_file_ram;
4984 struct ptlrpc_thread *thread = &lfsck->li_thread;
4988 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
4989 lfsck_lfsck2name(lfsck));
4991 atomic_inc(&lfsck->li_double_scan_count);
4993 if (lo->ll_flags & LF_INCOMPLETE)
4996 com->lc_new_checked = 0;
4997 com->lc_new_scanned = 0;
4998 com->lc_time_last_checkpoint = cfs_time_current();
4999 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5000 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5003 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
5006 rc = lfsck_layout_slave_query_master(env, com);
5007 if (list_empty(&llsd->llsd_master_list)) {
5008 if (unlikely(!thread_is_running(thread)))
5019 rc = l_wait_event(thread->t_ctl_waitq,
5020 !thread_is_running(thread) ||
5021 lo->ll_flags & LF_INCOMPLETE ||
5022 list_empty(&llsd->llsd_master_list),
5024 if (unlikely(!thread_is_running(thread)))
5027 if (lo->ll_flags & LF_INCOMPLETE)
5030 if (rc == -ETIMEDOUT)
5033 GOTO(done, rc = (rc < 0 ? rc : 1));
5037 rc = lfsck_layout_double_scan_result(env, com, rc);
5038 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
5039 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
5040 lfsck_layout_slave_quit(env, com);
5041 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
5042 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5044 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
5045 "status %d: rc = %d\n",
5046 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
5051 static void lfsck_layout_master_data_release(const struct lu_env *env,
5052 struct lfsck_component *com)
5054 struct lfsck_assistant_data *lad = com->lc_data;
5055 struct lfsck_instance *lfsck = com->lc_lfsck;
5056 struct lfsck_tgt_descs *ltds;
5057 struct lfsck_tgt_desc *ltd;
5058 struct lfsck_tgt_desc *next;
5060 LASSERT(lad != NULL);
5061 LASSERT(thread_is_init(&lad->lad_thread) ||
5062 thread_is_stopped(&lad->lad_thread));
5063 LASSERT(list_empty(&lad->lad_req_list));
5065 com->lc_data = NULL;
5067 ltds = &lfsck->li_ost_descs;
5068 spin_lock(<ds->ltd_lock);
5069 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
5070 ltd_layout_phase_list) {
5071 list_del_init(<d->ltd_layout_phase_list);
5073 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
5074 ltd_layout_phase_list) {
5075 list_del_init(<d->ltd_layout_phase_list);
5077 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
5079 list_del_init(<d->ltd_layout_list);
5081 spin_unlock(<ds->ltd_lock);
5083 ltds = &lfsck->li_mdt_descs;
5084 spin_lock(<ds->ltd_lock);
5085 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5086 ltd_layout_phase_list) {
5087 list_del_init(<d->ltd_layout_phase_list);
5089 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5090 ltd_layout_phase_list) {
5091 list_del_init(<d->ltd_layout_phase_list);
5093 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
5095 list_del_init(<d->ltd_layout_list);
5097 spin_unlock(<ds->ltd_lock);
5099 if (likely(lad->lad_bitmap != NULL))
5100 CFS_FREE_BITMAP(lad->lad_bitmap);
5105 static void lfsck_layout_slave_data_release(const struct lu_env *env,
5106 struct lfsck_component *com)
5108 struct lfsck_layout_slave_data *llsd = com->lc_data;
5110 lfsck_layout_slave_quit(env, com);
5111 com->lc_data = NULL;
5115 static void lfsck_layout_master_quit(const struct lu_env *env,
5116 struct lfsck_component *com)
5118 struct lfsck_assistant_data *lad = com->lc_data;
5119 struct lfsck_instance *lfsck = com->lc_lfsck;
5120 struct lfsck_tgt_descs *ltds;
5121 struct lfsck_tgt_desc *ltd;
5122 struct lfsck_tgt_desc *next;
5124 LASSERT(lad != NULL);
5126 lfsck_quit_generic(env, com);
5128 LASSERT(thread_is_init(&lad->lad_thread) ||
5129 thread_is_stopped(&lad->lad_thread));
5130 LASSERT(list_empty(&lad->lad_req_list));
5132 ltds = &lfsck->li_ost_descs;
5133 spin_lock(<ds->ltd_lock);
5134 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
5135 ltd_layout_phase_list) {
5136 list_del_init(<d->ltd_layout_phase_list);
5138 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
5139 ltd_layout_phase_list) {
5140 list_del_init(<d->ltd_layout_phase_list);
5142 spin_unlock(<ds->ltd_lock);
5144 ltds = &lfsck->li_mdt_descs;
5145 spin_lock(<ds->ltd_lock);
5146 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5147 ltd_layout_phase_list) {
5148 list_del_init(<d->ltd_layout_phase_list);
5150 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5151 ltd_layout_phase_list) {
5152 list_del_init(<d->ltd_layout_phase_list);
5154 spin_unlock(<ds->ltd_lock);
5157 static void lfsck_layout_slave_quit(const struct lu_env *env,
5158 struct lfsck_component *com)
5160 struct lfsck_layout_slave_data *llsd = com->lc_data;
5161 struct lfsck_layout_seq *lls;
5162 struct lfsck_layout_seq *next;
5163 struct lfsck_layout_slave_target *llst;
5165 LASSERT(llsd != NULL);
5167 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
5169 list_del_init(&lls->lls_list);
5170 lfsck_object_put(env, lls->lls_lastid_obj);
5174 spin_lock(&llsd->llsd_lock);
5175 while (!list_empty(&llsd->llsd_master_list)) {
5176 llst = list_entry(llsd->llsd_master_list.next,
5177 struct lfsck_layout_slave_target, llst_list);
5178 list_del_init(&llst->llst_list);
5179 spin_unlock(&llsd->llsd_lock);
5180 lfsck_layout_llst_put(llst);
5181 spin_lock(&llsd->llsd_lock);
5183 spin_unlock(&llsd->llsd_lock);
5185 lfsck_rbtree_cleanup(env, com);
5188 static int lfsck_layout_master_in_notify(const struct lu_env *env,
5189 struct lfsck_component *com,
5190 struct lfsck_request *lr,
5193 struct lfsck_instance *lfsck = com->lc_lfsck;
5194 struct lfsck_layout *lo = com->lc_file_ram;
5195 struct lfsck_assistant_data *lad = com->lc_data;
5196 struct lfsck_tgt_descs *ltds;
5197 struct lfsck_tgt_desc *ltd;
5201 if (lr->lr_event == LE_PAIRS_VERIFY) {
5204 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
5210 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
5211 "from %s %x, status %d, flags %x, flags2 %x\n",
5212 lfsck_lfsck2name(lfsck), lr->lr_event,
5213 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
5214 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
5216 if (lr->lr_event != LE_PHASE1_DONE &&
5217 lr->lr_event != LE_PHASE2_DONE &&
5218 lr->lr_event != LE_PEER_EXIT)
5221 if (lr->lr_flags & LEF_FROM_OST)
5222 ltds = &lfsck->li_ost_descs;
5224 ltds = &lfsck->li_mdt_descs;
5225 spin_lock(<ds->ltd_lock);
5226 ltd = LTD_TGT(ltds, lr->lr_index);
5228 spin_unlock(<ds->ltd_lock);
5233 list_del_init(<d->ltd_layout_phase_list);
5234 switch (lr->lr_event) {
5235 case LE_PHASE1_DONE:
5236 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
5237 if (lr->lr_flags2 & LF_INCOMPLETE) {
5238 if (lr->lr_flags & LEF_FROM_OST)
5239 lfsck_lad_set_bitmap(env, com,
5242 lo->ll_flags |= LF_INCOMPLETE;
5244 ltd->ltd_layout_done = 1;
5245 list_del_init(<d->ltd_layout_list);
5250 if (lr->lr_flags & LEF_FROM_OST) {
5251 if (list_empty(<d->ltd_layout_list))
5252 list_add_tail(<d->ltd_layout_list,
5253 &lad->lad_ost_list);
5254 list_add_tail(<d->ltd_layout_phase_list,
5255 &lad->lad_ost_phase2_list);
5257 if (list_empty(<d->ltd_layout_list))
5258 list_add_tail(<d->ltd_layout_list,
5259 &lad->lad_mdt_list);
5260 list_add_tail(<d->ltd_layout_phase_list,
5261 &lad->lad_mdt_phase2_list);
5264 case LE_PHASE2_DONE:
5265 ltd->ltd_layout_done = 1;
5266 if (!list_empty(<d->ltd_layout_list)) {
5267 list_del_init(<d->ltd_layout_list);
5268 if (lr->lr_flags2 & LF_INCOMPLETE) {
5269 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
5277 ltd->ltd_layout_done = 1;
5278 list_del_init(<d->ltd_layout_list);
5279 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
5280 !(lr->lr_flags & LEF_FROM_OST))
5281 lo->ll_flags |= LF_INCOMPLETE;
5286 spin_unlock(<ds->ltd_lock);
5288 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5289 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5291 memset(stop, 0, sizeof(*stop));
5292 stop->ls_status = lr->lr_status;
5293 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5294 lfsck_stop(env, lfsck->li_bottom, stop);
5295 } else if (lfsck_phase2_next_ready(lad)) {
5296 wake_up_all(&lad->lad_thread.t_ctl_waitq);
5302 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
5303 struct lfsck_component *com,
5304 struct lfsck_request *lr,
5307 struct lfsck_instance *lfsck = com->lc_lfsck;
5308 struct lfsck_layout_slave_data *llsd = com->lc_data;
5309 struct lfsck_layout_slave_target *llst;
5313 switch (lr->lr_event) {
5314 case LE_FID_ACCESSED:
5315 lfsck_rbtree_update_bitmap(env, com, &lr->lr_fid, true);
5317 case LE_CONDITIONAL_DESTROY:
5318 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
5320 case LE_PAIRS_VERIFY: {
5321 lr->lr_status = LPVS_INIT;
5322 /* Firstly, if the MDT-object which is claimed via OST-object
5323 * local stored PFID xattr recognizes the OST-object, then it
5324 * must be that the client given PFID is wrong. */
5325 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5330 lr->lr_status = LPVS_INCONSISTENT;
5331 /* The OST-object local stored PFID xattr is stale. We need to
5332 * check whether the MDT-object that is claimed via the client
5333 * given PFID information recognizes the OST-object or not. If
5334 * matches, then need to update the OST-object's PFID xattr. */
5335 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5338 * We are not sure whether the client given PFID information
5339 * is correct or not, do nothing to avoid improper fixing.
5342 * The client given PFID information is also invalid, we can
5343 * NOT fix the OST-object inconsistency.
5348 lr->lr_status = LPVS_INCONSISTENT_TOFIX;
5349 rc = lfsck_layout_slave_repair_pfid(env, com, lr);
5353 case LE_PHASE1_DONE: {
5354 if (lr->lr_flags2 & LF_INCOMPLETE) {
5355 struct lfsck_layout *lo = com->lc_file_ram;
5357 lo->ll_flags |= LF_INCOMPLETE;
5358 llst = lfsck_layout_llst_find_and_del(llsd,
5362 lfsck_layout_llst_put(llst);
5363 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5369 case LE_PHASE2_DONE:
5371 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
5372 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
5373 lr->lr_event, lr->lr_index, lr->lr_status);
5379 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
5383 lfsck_layout_llst_put(llst);
5384 if (list_empty(&llsd->llsd_master_list))
5385 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5387 if (lr->lr_event == LE_PEER_EXIT &&
5388 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
5389 (list_empty(&llsd->llsd_master_list) &&
5390 (lr->lr_status == LS_STOPPED ||
5391 lr->lr_status == LS_CO_STOPPED)))) {
5392 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5394 memset(stop, 0, sizeof(*stop));
5395 stop->ls_status = lr->lr_status;
5396 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5397 lfsck_stop(env, lfsck->li_bottom, stop);
5403 static int lfsck_layout_query(const struct lu_env *env,
5404 struct lfsck_component *com)
5406 struct lfsck_layout *lo = com->lc_file_ram;
5408 return lo->ll_status;
5411 /* with lfsck::li_lock held */
5412 static int lfsck_layout_slave_join(const struct lu_env *env,
5413 struct lfsck_component *com,
5414 struct lfsck_start_param *lsp)
5416 struct lfsck_instance *lfsck = com->lc_lfsck;
5417 struct lfsck_layout_slave_data *llsd = com->lc_data;
5418 struct lfsck_layout_slave_target *llst;
5419 struct lfsck_start *start = lsp->lsp_start;
5423 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
5426 if (!lsp->lsp_index_valid)
5429 /* If someone is running the LFSCK without orphan handling,
5430 * it will not maintain the object accessing rbtree. So we
5431 * cannot join it for orphan handling. */
5432 if (!llsd->llsd_rbtree_valid)
5435 spin_unlock(&lfsck->li_lock);
5436 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5437 spin_lock(&lfsck->li_lock);
5438 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
5439 spin_unlock(&lfsck->li_lock);
5440 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
5443 lfsck_layout_llst_put(llst);
5444 spin_lock(&lfsck->li_lock);
5451 static struct lfsck_operations lfsck_layout_master_ops = {
5452 .lfsck_reset = lfsck_layout_reset,
5453 .lfsck_fail = lfsck_layout_fail,
5454 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
5455 .lfsck_prep = lfsck_layout_master_prep,
5456 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
5457 .lfsck_exec_dir = lfsck_layout_exec_dir,
5458 .lfsck_post = lfsck_layout_master_post,
5459 .lfsck_dump = lfsck_layout_dump,
5460 .lfsck_double_scan = lfsck_layout_master_double_scan,
5461 .lfsck_data_release = lfsck_layout_master_data_release,
5462 .lfsck_quit = lfsck_layout_master_quit,
5463 .lfsck_in_notify = lfsck_layout_master_in_notify,
5464 .lfsck_query = lfsck_layout_query,
5467 static struct lfsck_operations lfsck_layout_slave_ops = {
5468 .lfsck_reset = lfsck_layout_reset,
5469 .lfsck_fail = lfsck_layout_fail,
5470 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
5471 .lfsck_prep = lfsck_layout_slave_prep,
5472 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
5473 .lfsck_exec_dir = lfsck_layout_exec_dir,
5474 .lfsck_post = lfsck_layout_slave_post,
5475 .lfsck_dump = lfsck_layout_dump,
5476 .lfsck_double_scan = lfsck_layout_slave_double_scan,
5477 .lfsck_data_release = lfsck_layout_slave_data_release,
5478 .lfsck_quit = lfsck_layout_slave_quit,
5479 .lfsck_in_notify = lfsck_layout_slave_in_notify,
5480 .lfsck_query = lfsck_layout_query,
5481 .lfsck_join = lfsck_layout_slave_join,
5484 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
5485 struct lfsck_component *com,
5486 struct lfsck_position *pos)
5488 struct lfsck_assistant_data *lad = com->lc_data;
5489 struct lfsck_layout_req *llr;
5491 if (list_empty(&lad->lad_req_list))
5494 llr = list_entry(lad->lad_req_list.next,
5495 struct lfsck_layout_req,
5497 pos->lp_oit_cookie = llr->llr_parent->llo_cookie - 1;
5500 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
5501 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
5502 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
5503 .la_fill_pos = lfsck_layout_assistant_fill_pos,
5504 .la_double_scan_result = lfsck_layout_double_scan_result,
5505 .la_req_fini = lfsck_layout_assistant_req_fini,
5506 .la_sync_failures = lfsck_layout_assistant_sync_failures,
5509 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
5511 struct lfsck_component *com;
5512 struct lfsck_layout *lo;
5513 struct dt_object *root = NULL;
5514 struct dt_object *obj;
5522 INIT_LIST_HEAD(&com->lc_link);
5523 INIT_LIST_HEAD(&com->lc_link_dir);
5524 init_rwsem(&com->lc_sem);
5525 atomic_set(&com->lc_ref, 1);
5526 com->lc_lfsck = lfsck;
5527 com->lc_type = LFSCK_TYPE_LAYOUT;
5528 if (lfsck->li_master) {
5529 com->lc_ops = &lfsck_layout_master_ops;
5530 com->lc_data = lfsck_assistant_data_init(
5531 &lfsck_layout_assistant_ops,
5533 if (com->lc_data == NULL)
5534 GOTO(out, rc = -ENOMEM);
5536 struct lfsck_layout_slave_data *llsd;
5538 com->lc_ops = &lfsck_layout_slave_ops;
5539 OBD_ALLOC_PTR(llsd);
5541 GOTO(out, rc = -ENOMEM);
5543 INIT_LIST_HEAD(&llsd->llsd_seq_list);
5544 INIT_LIST_HEAD(&llsd->llsd_master_list);
5545 spin_lock_init(&llsd->llsd_lock);
5546 llsd->llsd_rb_root = RB_ROOT;
5547 rwlock_init(&llsd->llsd_rb_lock);
5548 com->lc_data = llsd;
5550 com->lc_file_size = sizeof(*lo);
5551 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
5552 if (com->lc_file_ram == NULL)
5553 GOTO(out, rc = -ENOMEM);
5555 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
5556 if (com->lc_file_disk == NULL)
5557 GOTO(out, rc = -ENOMEM);
5559 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
5561 GOTO(out, rc = PTR_ERR(root));
5563 if (unlikely(!dt_try_as_dir(env, root)))
5564 GOTO(out, rc = -ENOTDIR);
5566 obj = local_file_find_or_create(env, lfsck->li_los, root,
5568 S_IFREG | S_IRUGO | S_IWUSR);
5570 GOTO(out, rc = PTR_ERR(obj));
5573 rc = lfsck_layout_load(env, com);
5575 rc = lfsck_layout_reset(env, com, true);
5576 else if (rc == -ENOENT)
5577 rc = lfsck_layout_init(env, com);
5582 lo = com->lc_file_ram;
5583 switch (lo->ll_status) {
5589 spin_lock(&lfsck->li_lock);
5590 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
5591 spin_unlock(&lfsck->li_lock);
5594 CERROR("%s: unknown lfsck_layout status %d\n",
5595 lfsck_lfsck2name(lfsck), lo->ll_status);
5597 case LS_SCANNING_PHASE1:
5598 case LS_SCANNING_PHASE2:
5599 /* No need to store the status to disk right now.
5600 * If the system crashed before the status stored,
5601 * it will be loaded back when next time. */
5602 lo->ll_status = LS_CRASHED;
5603 if (!lfsck->li_master)
5604 lo->ll_flags |= LF_INCOMPLETE;
5611 spin_lock(&lfsck->li_lock);
5612 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
5613 spin_unlock(&lfsck->li_lock);
5617 if (lo->ll_flags & LF_CRASHED_LASTID) {
5618 LASSERT(lfsck->li_out_notify != NULL);
5620 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5621 LE_LASTID_REBUILDING);
5627 if (root != NULL && !IS_ERR(root))
5628 lfsck_object_put(env, root);
5631 lfsck_component_cleanup(env, com);
5632 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
5633 lfsck_lfsck2name(lfsck), rc);
5639 struct lfsck_orphan_it {
5640 struct lfsck_component *loi_com;
5641 struct lfsck_rbtree_node *loi_lrn;
5642 struct lfsck_layout_slave_target *loi_llst;
5643 struct lu_fid loi_key;
5644 struct lu_orphan_rec loi_rec;
5646 unsigned int loi_over:1;
5649 static int lfsck_fid_match_idx(const struct lu_env *env,
5650 struct lfsck_instance *lfsck,
5651 const struct lu_fid *fid, int idx)
5653 struct seq_server_site *ss;
5654 struct lu_server_fld *sf;
5655 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
5658 /* All abnormal cases will be returned to MDT0. */
5659 if (!fid_is_norm(fid)) {
5666 ss = lfsck_dev_site(lfsck);
5667 if (unlikely(ss == NULL))
5670 sf = ss->ss_server_fld;
5671 LASSERT(sf != NULL);
5673 fld_range_set_any(range);
5674 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
5678 if (!fld_range_is_mdt(range))
5681 if (range->lsr_index == idx)
5687 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
5688 struct dt_object *obj)
5690 struct dt_device *dev = lfsck_obj2dev(obj);
5691 struct thandle *handle;
5695 handle = dt_trans_create(env, dev);
5699 rc = dt_declare_ref_del(env, obj, handle);
5703 rc = dt_declare_destroy(env, obj, handle);
5707 rc = dt_trans_start_local(env, dev, handle);
5711 dt_write_lock(env, obj, 0);
5712 rc = dt_ref_del(env, obj, handle);
5714 rc = dt_destroy(env, obj, handle);
5715 dt_write_unlock(env, obj);
5720 dt_trans_stop(env, dev, handle);
5722 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
5723 PFID(lfsck_dto2fid(obj)), rc);
5728 static int lfsck_orphan_index_lookup(const struct lu_env *env,
5729 struct dt_object *dt,
5731 const struct dt_key *key)
5736 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
5737 struct dt_object *dt,
5738 const struct dt_rec *rec,
5739 const struct dt_key *key,
5740 struct thandle *handle)
5745 static int lfsck_orphan_index_insert(const struct lu_env *env,
5746 struct dt_object *dt,
5747 const struct dt_rec *rec,
5748 const struct dt_key *key,
5749 struct thandle *handle,
5755 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
5756 struct dt_object *dt,
5757 const struct dt_key *key,
5758 struct thandle *handle)
5763 static int lfsck_orphan_index_delete(const struct lu_env *env,
5764 struct dt_object *dt,
5765 const struct dt_key *key,
5766 struct thandle *handle)
5771 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
5772 struct dt_object *dt,
5775 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
5776 struct lfsck_instance *lfsck;
5777 struct lfsck_component *com = NULL;
5778 struct lfsck_layout_slave_data *llsd;
5779 struct lfsck_orphan_it *it = NULL;
5780 struct lfsck_layout *lo;
5784 lfsck = lfsck_instance_find(dev, true, false);
5785 if (unlikely(lfsck == NULL))
5786 RETURN(ERR_PTR(-ENXIO));
5788 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
5789 if (unlikely(com == NULL))
5790 GOTO(out, rc = -ENOENT);
5792 lo = com->lc_file_ram;
5793 if (lo->ll_flags & LF_INCOMPLETE)
5794 GOTO(out, rc = -ESRCH);
5796 llsd = com->lc_data;
5797 if (!llsd->llsd_rbtree_valid)
5798 GOTO(out, rc = -ESRCH);
5802 GOTO(out, rc = -ENOMEM);
5804 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
5805 if (it->loi_llst == NULL)
5806 GOTO(out, rc = -ENXIO);
5808 if (dev->dd_record_fid_accessed) {
5809 /* The first iteration against the rbtree, scan the whole rbtree
5810 * to remove the nodes which do NOT need to be handled. */
5811 write_lock(&llsd->llsd_rb_lock);
5812 if (dev->dd_record_fid_accessed) {
5813 struct rb_node *node;
5814 struct rb_node *next;
5815 struct lfsck_rbtree_node *lrn;
5817 /* No need to record the fid accessing anymore. */
5818 dev->dd_record_fid_accessed = 0;
5820 node = rb_first(&llsd->llsd_rb_root);
5821 while (node != NULL) {
5822 next = rb_next(node);
5823 lrn = rb_entry(node, struct lfsck_rbtree_node,
5825 if (atomic_read(&lrn->lrn_known_count) <=
5826 atomic_read(&lrn->lrn_accessed_count)) {
5827 rb_erase(node, &llsd->llsd_rb_root);
5828 lfsck_rbtree_free(lrn);
5833 write_unlock(&llsd->llsd_rb_lock);
5836 /* read lock the rbtree when init, and unlock when fini */
5837 read_lock(&llsd->llsd_rb_lock);
5845 lfsck_component_put(env, com);
5847 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
5848 lfsck_lfsck2name(lfsck), rc);
5850 lfsck_instance_put(env, lfsck);
5855 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
5858 return (struct dt_it *)it;
5861 static void lfsck_orphan_it_fini(const struct lu_env *env,
5864 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5865 struct lfsck_component *com = it->loi_com;
5866 struct lfsck_layout_slave_data *llsd;
5867 struct lfsck_layout_slave_target *llst;
5870 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
5871 lfsck_lfsck2name(com->lc_lfsck));
5873 llsd = com->lc_data;
5874 read_unlock(&llsd->llsd_rb_lock);
5875 llst = it->loi_llst;
5876 LASSERT(llst != NULL);
5878 /* Save the key and hash for iterate next. */
5879 llst->llst_fid = it->loi_key;
5880 llst->llst_hash = it->loi_hash;
5881 lfsck_layout_llst_put(llst);
5882 lfsck_component_put(env, com);
5888 * \retval +1: the iteration finished
5889 * \retval 0: on success, not finished
5890 * \retval -ve: on error
5892 static int lfsck_orphan_it_next(const struct lu_env *env,
5895 struct lfsck_thread_info *info = lfsck_env_info(env);
5896 struct filter_fid_old *pfid = &info->lti_old_pfid;
5897 struct lu_attr *la = &info->lti_la;
5898 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5899 struct lu_fid *key = &it->loi_key;
5900 struct lu_orphan_rec *rec = &it->loi_rec;
5901 struct lfsck_component *com = it->loi_com;
5902 struct lfsck_instance *lfsck = com->lc_lfsck;
5903 struct lfsck_layout_slave_data *llsd = com->lc_data;
5904 struct dt_object *obj;
5905 struct lfsck_rbtree_node *lrn;
5909 __u32 idx = it->loi_llst->llst_index;
5919 lrn = lfsck_rbtree_search(llsd, key, &exact);
5927 key->f_seq = lrn->lrn_seq;
5928 key->f_oid = lrn->lrn_first_oid;
5933 if (unlikely(key->f_oid == 0)) {
5940 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
5946 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
5947 atomic_read(&lrn->lrn_accessed_count))) {
5948 struct rb_node *next = rb_next(&lrn->lrn_node);
5950 while (next != NULL) {
5951 lrn = rb_entry(next, struct lfsck_rbtree_node,
5953 if (atomic_read(&lrn->lrn_known_count) >
5954 atomic_read(&lrn->lrn_accessed_count))
5956 next = rb_next(next);
5965 key->f_seq = lrn->lrn_seq;
5966 key->f_oid = lrn->lrn_first_oid;
5970 pos = key->f_oid - lrn->lrn_first_oid;
5973 pos = find_next_bit(lrn->lrn_known_bitmap,
5974 LFSCK_RBTREE_BITMAP_WIDTH, pos);
5975 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
5976 key->f_oid = lrn->lrn_first_oid + pos;
5977 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
5985 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
5990 key->f_oid = lrn->lrn_first_oid + pos;
5991 obj = lfsck_object_find_bottom(env, lfsck, key);
5994 if (rc == -ENOENT) {
6001 dt_read_lock(env, obj, 0);
6002 if (dt_object_exists(obj) == 0 ||
6003 lfsck_is_dead_obj(obj)) {
6004 dt_read_unlock(env, obj);
6005 lfsck_object_put(env, obj);
6010 rc = dt_attr_get(env, obj, la);
6014 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, pfid, sizeof(*pfid)),
6016 if (rc == -ENODATA) {
6017 /* For the pre-created OST-object, update the bitmap to avoid
6018 * others LFSCK (second phase) iteration to touch it again. */
6019 if (la->la_ctime == 0) {
6020 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
6021 atomic_inc(&lrn->lrn_accessed_count);
6023 /* For the race between repairing dangling referenced
6024 * MDT-object and unlink the file, it may left orphan
6025 * OST-object there. Destroy it now! */
6026 if (unlikely(!(la->la_mode & S_ISUID))) {
6027 dt_read_unlock(env, obj);
6028 lfsck_layout_destroy_orphan(env, obj);
6029 lfsck_object_put(env, obj);
6033 } else if (idx == 0) {
6034 /* If the orphan OST-object has no parent information,
6035 * regard it as referenced by the MDT-object on MDT0. */
6036 fid_zero(&rec->lor_fid);
6037 rec->lor_uid = la->la_uid;
6038 rec->lor_gid = la->la_gid;
6042 dt_read_unlock(env, obj);
6043 lfsck_object_put(env, obj);
6051 if (rc != sizeof(struct filter_fid) &&
6052 rc != sizeof(struct filter_fid_old))
6053 GOTO(out, rc = -EINVAL);
6055 fid_le_to_cpu(&rec->lor_fid, &pfid->ff_parent);
6056 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
6057 * MDT-object's FID::f_ver, instead it is the OST-object index in its
6058 * parent MDT-object's layout EA. */
6059 save = rec->lor_fid.f_stripe_idx;
6060 rec->lor_fid.f_ver = 0;
6061 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_fid, idx);
6062 /* If the orphan OST-object does not claim the MDT, then next.
6064 * If we do not know whether it matches or not, then return it
6065 * to the MDT for further check. */
6067 dt_read_unlock(env, obj);
6068 lfsck_object_put(env, obj);
6073 rec->lor_fid.f_stripe_idx = save;
6074 rec->lor_uid = la->la_uid;
6075 rec->lor_gid = la->la_gid;
6077 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u\n",
6078 lfsck_lfsck2name(com->lc_lfsck), PFID(key), PFID(&rec->lor_fid),
6079 rec->lor_uid, rec->lor_gid);
6084 dt_read_unlock(env, obj);
6085 lfsck_object_put(env, obj);
6093 * \retval +1: locate to the exactly position
6094 * \retval 0: cannot locate to the exactly position,
6095 * call next() to move to a valid position.
6096 * \retval -ve: on error
6098 static int lfsck_orphan_it_get(const struct lu_env *env,
6100 const struct dt_key *key)
6102 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6105 it->loi_key = *(struct lu_fid *)key;
6106 rc = lfsck_orphan_it_next(env, di);
6116 static void lfsck_orphan_it_put(const struct lu_env *env,
6121 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
6122 const struct dt_it *di)
6124 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6126 return (struct dt_key *)&it->loi_key;
6129 static int lfsck_orphan_it_key_size(const struct lu_env *env,
6130 const struct dt_it *di)
6132 return sizeof(struct lu_fid);
6135 static int lfsck_orphan_it_rec(const struct lu_env *env,
6136 const struct dt_it *di,
6140 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6142 *(struct lu_orphan_rec *)rec = it->loi_rec;
6147 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
6148 const struct dt_it *di)
6150 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6152 return it->loi_hash;
6156 * \retval +1: locate to the exactly position
6157 * \retval 0: cannot locate to the exactly position,
6158 * call next() to move to a valid position.
6159 * \retval -ve: on error
6161 static int lfsck_orphan_it_load(const struct lu_env *env,
6162 const struct dt_it *di,
6165 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6166 struct lfsck_layout_slave_target *llst = it->loi_llst;
6169 LASSERT(llst != NULL);
6171 if (hash != llst->llst_hash) {
6172 CDEBUG(D_LFSCK, "%s: the given hash "LPU64" for orphan "
6173 "iteration does not match the one when fini "
6174 LPU64", to be reset.\n",
6175 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
6177 fid_zero(&llst->llst_fid);
6178 llst->llst_hash = 0;
6181 it->loi_key = llst->llst_fid;
6182 it->loi_hash = llst->llst_hash;
6183 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
6193 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
6194 const struct dt_it *di,
6200 const struct dt_index_operations lfsck_orphan_index_ops = {
6201 .dio_lookup = lfsck_orphan_index_lookup,
6202 .dio_declare_insert = lfsck_orphan_index_declare_insert,
6203 .dio_insert = lfsck_orphan_index_insert,
6204 .dio_declare_delete = lfsck_orphan_index_declare_delete,
6205 .dio_delete = lfsck_orphan_index_delete,
6207 .init = lfsck_orphan_it_init,
6208 .fini = lfsck_orphan_it_fini,
6209 .get = lfsck_orphan_it_get,
6210 .put = lfsck_orphan_it_put,
6211 .next = lfsck_orphan_it_next,
6212 .key = lfsck_orphan_it_key,
6213 .key_size = lfsck_orphan_it_key_size,
6214 .rec = lfsck_orphan_it_rec,
6215 .store = lfsck_orphan_it_store,
6216 .load = lfsck_orphan_it_load,
6217 .key_rec = lfsck_orphan_it_key_rec,