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;
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 dev = lfsck_obj2dev(child);
2926 handle = dt_trans_create(env, dev);
2928 GOTO(log, rc = PTR_ERR(handle));
2930 rc = dt_declare_create(env, child, la, hint, dof, handle);
2934 rc = dt_trans_start(env, dev, handle);
2938 rc = dt_create(env, child, la, hint, dof, handle);
2939 dt_trans_stop(env, dev, handle);
2944 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2945 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2950 /* The 2nd transaction. */
2952 /* XXX: Generally, we should use bottom device (OSD) to update parent
2953 * LOV EA. But because the LOD-object still references the wrong
2954 * OSP-object that should be detached after the parent's LOV EA
2955 * refreshed. Unfortunately, there is no suitable API for that.
2956 * So we have to make the LOD to re-load the OSP-object(s) via
2957 * replacing the LOV EA against the LOD-object.
2959 * Once the DNE2 patches have been landed, we can replace the
2960 * LOD device with the OSD device. LU-6230. */
2962 dev = lfsck->li_next;
2963 parent = lfsck_object_locate(dev, parent);
2965 GOTO(log, rc = PTR_ERR(parent));
2967 handle = dt_trans_create(env, dev);
2969 GOTO(log, rc = PTR_ERR(handle));
2971 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2972 LU_XATTR_REPLACE, handle);
2976 rc = dt_trans_start_local(env, dev, handle);
2980 dt_write_lock(env, parent, 0);
2981 if (unlikely(lfsck_is_dead_obj(parent)))
2982 GOTO(unlock, rc = 0);
2984 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2985 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2986 GOTO(unlock, rc = 0);
2989 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2990 * been verified in lfsck_layout_verify_header() already. If some
2991 * new magic introduced in the future, then layout LFSCK needs to
2992 * be updated also. */
2993 magic = le32_to_cpu(lmm->lmm_magic);
2994 if (magic == LOV_MAGIC_V1) {
2995 objs = &lmm->lmm_objects[llr->llr_lov_idx];
2997 LASSERT(magic == LOV_MAGIC_V3);
2999 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3002 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3003 index = le32_to_cpu(objs->l_ost_idx);
3004 rc = ostid_to_fid(&tfid, oi, index);
3005 /* Someone changed layout during the LFSCK, no need to repair then. */
3006 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3007 GOTO(unlock, rc = 0);
3009 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3010 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3011 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3012 objs->l_ost_gen = cpu_to_le32(0);
3013 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3014 lfsck_buf_init(&ea_buf, lmm,
3015 lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3017 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV,
3018 LU_XATTR_REPLACE, handle);
3020 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3023 dt_write_unlock(env, parent);
3027 dt_trans_stop(env, dev, handle);
3030 lfsck_ibits_unlock(&lh, LCK_EX);
3032 lfsck_object_put(env, child);
3034 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired multiple "
3035 "references for: parent "DFID", OST-index %u, stripe-index %u, "
3036 "owner %u/%u: rc = %d\n", lfsck_lfsck2name(lfsck), PFID(pfid),
3037 llr->llr_ost_idx, llr->llr_lov_idx, la->la_uid, la->la_gid, rc);
3042 /* If the MDT-object and the OST-object have different owner information,
3043 * then trust the MDT-object, because the normal chown/chgrp handle order
3044 * is from MDT to OST, and it is possible that some chown/chgrp operation
3045 * is partly done. */
3046 static int lfsck_layout_repair_owner(const struct lu_env *env,
3047 struct lfsck_component *com,
3048 struct dt_object *parent,
3049 struct lfsck_layout_req *llr,
3050 struct lu_attr *pla)
3052 struct lfsck_thread_info *info = lfsck_env_info(env);
3053 struct lu_attr *tla = &info->lti_la3;
3054 struct dt_object *child = llr->llr_child;
3055 struct dt_device *dev = lfsck_obj2dev(child);
3056 struct thandle *handle;
3060 handle = dt_trans_create(env, dev);
3062 GOTO(log, rc = PTR_ERR(handle));
3064 tla->la_uid = pla->la_uid;
3065 tla->la_gid = pla->la_gid;
3066 tla->la_valid = LA_UID | LA_GID;
3067 rc = dt_declare_attr_set(env, child, tla, handle);
3071 rc = dt_trans_start_local(env, dev, handle);
3075 /* Use the dt_object lock to serialize with destroy and attr_set. */
3076 dt_read_lock(env, parent, 0);
3077 if (unlikely(lfsck_is_dead_obj(parent)))
3078 GOTO(unlock, rc = 1);
3080 /* Get the latest parent's owner. */
3081 rc = dt_attr_get(env, parent, tla);
3085 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3086 if (unlikely(tla->la_uid != pla->la_uid ||
3087 tla->la_gid != pla->la_gid))
3088 GOTO(unlock, rc = 1);
3090 tla->la_valid = LA_UID | LA_GID;
3091 rc = dt_attr_set(env, child, tla, handle);
3096 dt_read_unlock(env, parent);
3099 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3102 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired inconsistent "
3103 "file owner for: parent "DFID", child "DFID", OST-index %u, "
3104 "stripe-index %u, owner %u/%u: rc = %d\n",
3105 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
3106 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
3107 pla->la_uid, pla->la_gid, rc);
3112 /* Check whether the OST-object correctly back points to the
3113 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3114 static int lfsck_layout_check_parent(const struct lu_env *env,
3115 struct lfsck_component *com,
3116 struct dt_object *parent,
3117 const struct lu_fid *pfid,
3118 const struct lu_fid *cfid,
3119 const struct lu_attr *pla,
3120 const struct lu_attr *cla,
3121 struct lfsck_layout_req *llr,
3122 struct lu_buf *lov_ea, __u32 idx)
3124 struct lfsck_thread_info *info = lfsck_env_info(env);
3125 struct lu_buf *buf = &info->lti_big_buf;
3126 struct dt_object *tobj;
3127 struct lov_mds_md_v1 *lmm;
3128 struct lov_ost_data_v1 *objs;
3129 struct lustre_handle lh = { 0 };
3136 if (fid_is_zero(pfid)) {
3137 /* client never wrote. */
3138 if (cla->la_size == 0 && cla->la_blocks == 0) {
3139 if (unlikely(cla->la_uid != pla->la_uid ||
3140 cla->la_gid != pla->la_gid))
3141 RETURN (LLIT_INCONSISTENT_OWNER);
3146 RETURN(LLIT_UNMATCHED_PAIR);
3149 if (unlikely(!fid_is_sane(pfid)))
3150 RETURN(LLIT_UNMATCHED_PAIR);
3152 if (lu_fid_eq(pfid, lu_object_fid(&parent->do_lu))) {
3153 if (llr->llr_lov_idx == idx)
3156 RETURN(LLIT_UNMATCHED_PAIR);
3159 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3161 RETURN(PTR_ERR(tobj));
3163 if (dt_object_exists(tobj) == 0 ||
3164 lfsck_is_dead_obj(tobj))
3165 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3167 if (!S_ISREG(lfsck_object_type(tobj)))
3168 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3170 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3171 * remote one on another MDT. Then check whether the given OST-object
3172 * is in such layout. If yes, it is multiple referenced, otherwise it
3173 * is unmatched referenced case. */
3174 rc = lfsck_layout_get_lovea(env, tobj, buf);
3175 if (rc == 0 || rc == -ENOENT)
3176 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3182 magic = le32_to_cpu(lmm->lmm_magic);
3183 if (magic == LOV_MAGIC_V1) {
3184 objs = &lmm->lmm_objects[0];
3186 LASSERT(magic == LOV_MAGIC_V3);
3187 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3190 count = le16_to_cpu(lmm->lmm_stripe_count);
3191 for (i = 0; i < count; i++, objs++) {
3192 struct lu_fid *tfid = &info->lti_fid2;
3193 struct ost_id *oi = &info->lti_oi;
3196 if (lovea_slot_is_dummy(objs))
3199 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3200 idx2 = le32_to_cpu(objs->l_ost_idx);
3201 rc = ostid_to_fid(tfid, oi, idx2);
3203 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3204 "invalid layout EA at the slot %d, index %u\n",
3205 lfsck_lfsck2name(com->lc_lfsck),
3206 PFID(pfid), i, idx2);
3208 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3211 if (lu_fid_eq(cfid, tfid)) {
3212 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
3213 MDS_INODELOCK_UPDATE |
3214 MDS_INODELOCK_LAYOUT |
3215 MDS_INODELOCK_XATTR,
3220 dt_read_lock(env, tobj, 0);
3222 /* For local MDT-object, re-check existence
3223 * after taken the lock. */
3224 if (!dt_object_remote(tobj)) {
3225 if (dt_object_exists(tobj) == 0 ||
3226 lfsck_is_dead_obj(tobj)) {
3227 rc = LLIT_UNMATCHED_PAIR;
3230 rc = LLIT_MULTIPLE_REFERENCED;
3236 /* For migration case, the new MDT-object and old
3237 * MDT-object may reference the same OST-object at
3238 * some migration internal time.
3240 * For remote MDT-object, the local MDT may not know
3241 * whether it has been removed or not. Try checking
3242 * for a non-existent xattr to check if this object
3243 * has been been removed or not. */
3244 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
3246 if (unlikely(rc == -ENOENT || rc >= 0)) {
3247 rc = LLIT_UNMATCHED_PAIR;
3248 } else if (rc == -ENODATA) {
3250 rc = LLIT_MULTIPLE_REFERENCED;
3257 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3260 if (lustre_handle_is_used(&lh)) {
3261 dt_read_unlock(env, tobj);
3262 lfsck_ibits_unlock(&lh, LCK_EX);
3266 lfsck_object_put(env, tobj);
3271 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
3272 struct lfsck_component *com,
3273 struct lfsck_assistant_req *lar)
3275 struct lfsck_layout_req *llr =
3276 container_of0(lar, struct lfsck_layout_req, llr_lar);
3277 struct lfsck_layout *lo = com->lc_file_ram;
3278 struct lfsck_thread_info *info = lfsck_env_info(env);
3279 struct filter_fid_old *pea = &info->lti_old_pfid;
3280 struct lu_fid *pfid = &info->lti_fid;
3281 struct lu_buf buf = { NULL };
3282 struct dt_object *parent;
3283 struct dt_object *child = llr->llr_child;
3284 struct lu_attr *pla = &info->lti_la;
3285 struct lu_attr *cla = &info->lti_la2;
3286 struct lfsck_instance *lfsck = com->lc_lfsck;
3287 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3288 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
3293 parent = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
3295 RETURN(PTR_ERR(parent));
3297 if (unlikely(lfsck_is_dead_obj(parent)))
3298 GOTO(put_parent, rc = 0);
3300 rc = dt_attr_get(env, parent, pla);
3304 rc = dt_attr_get(env, child, cla);
3305 if (rc == -ENOENT) {
3306 if (unlikely(lfsck_is_dead_obj(parent)))
3307 GOTO(put_parent, rc = 0);
3309 type = LLIT_DANGLING;
3316 lfsck_buf_init(&buf, pea, sizeof(struct filter_fid_old));
3317 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
3318 if (unlikely(rc >= 0 && rc != sizeof(struct filter_fid_old) &&
3319 rc != sizeof(struct filter_fid))) {
3320 type = LLIT_UNMATCHED_PAIR;
3324 if (rc < 0 && rc != -ENODATA)
3327 if (rc == -ENODATA) {
3330 fid_le_to_cpu(pfid, &pea->ff_parent);
3331 /* Currently, the filter_fid::ff_parent::f_ver is not the
3332 * real parent MDT-object's FID::f_ver, instead it is the
3333 * OST-object index in its parent MDT-object's layout EA. */
3334 idx = pfid->f_stripe_idx;
3338 rc = lfsck_layout_check_parent(env, com, parent, pfid,
3339 lu_object_fid(&child->do_lu),
3340 pla, cla, llr, &buf, idx);
3349 if (unlikely(cla->la_uid != pla->la_uid ||
3350 cla->la_gid != pla->la_gid)) {
3351 type = LLIT_INCONSISTENT_OWNER;
3356 if (bk->lb_param & LPF_DRYRUN) {
3357 if (type != LLIT_NONE)
3365 rc = lfsck_layout_repair_dangling(env, com, parent, llr, pla);
3367 case LLIT_UNMATCHED_PAIR:
3368 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
3371 case LLIT_MULTIPLE_REFERENCED:
3372 rc = lfsck_layout_repair_multiple_references(env, com, parent,
3375 case LLIT_INCONSISTENT_OWNER:
3376 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla);
3386 down_write(&com->lc_sem);
3388 struct lfsck_assistant_data *lad = com->lc_data;
3390 if (unlikely(lad->lad_exit)) {
3392 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
3393 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
3394 rc == -EHOSTUNREACH) {
3395 /* If cannot touch the target server,
3396 * mark the LFSCK as INCOMPLETE. */
3397 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
3398 "talk with OST %x: rc = %d\n",
3399 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
3400 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
3401 lo->ll_objs_skipped++;
3404 lfsck_layout_record_failure(env, lfsck, lo);
3406 } else if (rc > 0) {
3407 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
3408 "unknown type = %d\n", type);
3410 lo->ll_objs_repaired[type - 1]++;
3411 if (bk->lb_param & LPF_DRYRUN &&
3412 unlikely(lo->ll_pos_first_inconsistent == 0))
3413 lo->ll_pos_first_inconsistent =
3414 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
3417 up_write(&com->lc_sem);
3420 lfsck_object_put(env, parent);
3425 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
3426 struct lfsck_component *com)
3428 struct lfsck_assistant_data *lad = com->lc_data;
3429 struct lfsck_instance *lfsck = com->lc_lfsck;
3430 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3431 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
3432 struct lfsck_tgt_desc *ltd;
3436 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
3437 lfsck_lfsck2name(lfsck));
3439 spin_lock(<ds->ltd_lock);
3440 while (!list_empty(&lad->lad_ost_phase2_list)) {
3441 ltd = list_entry(lad->lad_ost_phase2_list.next,
3442 struct lfsck_tgt_desc,
3443 ltd_layout_phase_list);
3444 list_del_init(<d->ltd_layout_phase_list);
3445 if (bk->lb_param & LPF_ALL_TGT) {
3446 spin_unlock(<ds->ltd_lock);
3447 rc = lfsck_layout_scan_orphan(env, com, ltd);
3448 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3451 if (unlikely(lad->lad_exit ||
3452 !thread_is_running(&lfsck->li_thread)))
3454 spin_lock(<ds->ltd_lock);
3458 if (list_empty(&lad->lad_ost_phase1_list))
3462 spin_unlock(<ds->ltd_lock);
3464 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
3465 lfsck_lfsck2name(lfsck), rc);
3471 lfsck_layout_slave_async_interpret(const struct lu_env *env,
3472 struct ptlrpc_request *req,
3475 struct lfsck_layout_slave_async_args *llsaa = args;
3476 struct obd_export *exp = llsaa->llsaa_exp;
3477 struct lfsck_component *com = llsaa->llsaa_com;
3478 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
3479 struct lfsck_layout_slave_data *llsd = com->lc_data;
3480 struct lfsck_reply *lr = NULL;
3484 /* It is quite probably caused by target crash,
3485 * to make the LFSCK can go ahead, assume that
3486 * the target finished the LFSCK prcoessing. */
3489 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
3490 if (lr->lr_status != LS_SCANNING_PHASE1 &&
3491 lr->lr_status != LS_SCANNING_PHASE2)
3496 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
3497 "status %d\n", lfsck_lfsck2name(com->lc_lfsck),
3498 llst->llst_index, lr != NULL ? lr->lr_status : rc);
3500 lfsck_layout_llst_del(llsd, llst);
3503 lfsck_layout_llst_put(llst);
3504 lfsck_component_put(env, com);
3505 class_export_put(exp);
3510 static int lfsck_layout_async_query(const struct lu_env *env,
3511 struct lfsck_component *com,
3512 struct obd_export *exp,
3513 struct lfsck_layout_slave_target *llst,
3514 struct lfsck_request *lr,
3515 struct ptlrpc_request_set *set)
3517 struct lfsck_layout_slave_async_args *llsaa;
3518 struct ptlrpc_request *req;
3519 struct lfsck_request *tmp;
3523 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
3527 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
3529 ptlrpc_request_free(req);
3533 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3535 ptlrpc_request_set_replen(req);
3537 llsaa = ptlrpc_req_async_args(req);
3538 llsaa->llsaa_exp = exp;
3539 llsaa->llsaa_com = lfsck_component_get(com);
3540 llsaa->llsaa_llst = llst;
3541 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
3542 ptlrpc_set_add_req(set, req);
3547 static int lfsck_layout_async_notify(const struct lu_env *env,
3548 struct obd_export *exp,
3549 struct lfsck_request *lr,
3550 struct ptlrpc_request_set *set)
3552 struct ptlrpc_request *req;
3553 struct lfsck_request *tmp;
3557 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3561 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3563 ptlrpc_request_free(req);
3567 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3569 ptlrpc_request_set_replen(req);
3570 ptlrpc_set_add_req(set, req);
3576 lfsck_layout_slave_query_master(const struct lu_env *env,
3577 struct lfsck_component *com)
3579 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3580 struct lfsck_instance *lfsck = com->lc_lfsck;
3581 struct lfsck_layout_slave_data *llsd = com->lc_data;
3582 struct lfsck_layout_slave_target *llst;
3583 struct obd_export *exp;
3584 struct ptlrpc_request_set *set;
3589 set = ptlrpc_prep_set();
3591 GOTO(log, rc = -ENOMEM);
3593 memset(lr, 0, sizeof(*lr));
3594 lr->lr_event = LE_QUERY;
3595 lr->lr_active = LFSCK_TYPE_LAYOUT;
3597 llsd->llsd_touch_gen++;
3598 spin_lock(&llsd->llsd_lock);
3599 while (!list_empty(&llsd->llsd_master_list)) {
3600 llst = list_entry(llsd->llsd_master_list.next,
3601 struct lfsck_layout_slave_target,
3603 if (llst->llst_gen == llsd->llsd_touch_gen)
3606 llst->llst_gen = llsd->llsd_touch_gen;
3607 list_move_tail(&llst->llst_list,
3608 &llsd->llsd_master_list);
3609 atomic_inc(&llst->llst_ref);
3610 spin_unlock(&llsd->llsd_lock);
3612 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3615 lfsck_layout_llst_del(llsd, llst);
3616 lfsck_layout_llst_put(llst);
3617 spin_lock(&llsd->llsd_lock);
3621 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
3623 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3624 "query %s for layout: rc = %d\n",
3625 lfsck_lfsck2name(lfsck),
3626 exp->exp_obd->obd_name, rc);
3629 lfsck_layout_llst_put(llst);
3630 class_export_put(exp);
3632 spin_lock(&llsd->llsd_lock);
3634 spin_unlock(&llsd->llsd_lock);
3636 rc = ptlrpc_set_wait(set);
3637 ptlrpc_set_destroy(set);
3639 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
3642 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
3643 lfsck_lfsck2name(com->lc_lfsck), rc);
3649 lfsck_layout_slave_notify_master(const struct lu_env *env,
3650 struct lfsck_component *com,
3651 enum lfsck_events event, int result)
3653 struct lfsck_layout *lo = com->lc_file_ram;
3654 struct lfsck_instance *lfsck = com->lc_lfsck;
3655 struct lfsck_layout_slave_data *llsd = com->lc_data;
3656 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3657 struct lfsck_layout_slave_target *llst;
3658 struct obd_export *exp;
3659 struct ptlrpc_request_set *set;
3663 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
3664 lfsck_lfsck2name(com->lc_lfsck));
3666 set = ptlrpc_prep_set();
3670 memset(lr, 0, sizeof(*lr));
3671 lr->lr_event = event;
3672 lr->lr_flags = LEF_FROM_OST;
3673 lr->lr_status = result;
3674 lr->lr_index = lfsck_dev_idx(lfsck);
3675 lr->lr_active = LFSCK_TYPE_LAYOUT;
3676 lr->lr_flags2 = lo->ll_flags;
3677 llsd->llsd_touch_gen++;
3678 spin_lock(&llsd->llsd_lock);
3679 while (!list_empty(&llsd->llsd_master_list)) {
3680 llst = list_entry(llsd->llsd_master_list.next,
3681 struct lfsck_layout_slave_target,
3683 if (llst->llst_gen == llsd->llsd_touch_gen)
3686 llst->llst_gen = llsd->llsd_touch_gen;
3687 list_move_tail(&llst->llst_list,
3688 &llsd->llsd_master_list);
3689 atomic_inc(&llst->llst_ref);
3690 spin_unlock(&llsd->llsd_lock);
3692 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3695 lfsck_layout_llst_del(llsd, llst);
3696 lfsck_layout_llst_put(llst);
3697 spin_lock(&llsd->llsd_lock);
3701 rc = lfsck_layout_async_notify(env, exp, lr, set);
3703 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3704 "notify %s for layout: rc = %d\n",
3705 lfsck_lfsck2name(lfsck),
3706 exp->exp_obd->obd_name, rc);
3708 lfsck_layout_llst_put(llst);
3709 class_export_put(exp);
3710 spin_lock(&llsd->llsd_lock);
3712 spin_unlock(&llsd->llsd_lock);
3714 ptlrpc_set_wait(set);
3715 ptlrpc_set_destroy(set);
3721 * \ret -ENODATA: unrecognized stripe
3722 * \ret = 0 : recognized stripe
3723 * \ret < 0 : other failures
3725 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
3726 struct lfsck_component *com,
3727 struct lu_fid *cfid,
3728 struct lu_fid *pfid)
3730 struct lfsck_thread_info *info = lfsck_env_info(env);
3731 struct lu_buf *buf = &info->lti_big_buf;
3732 struct ost_id *oi = &info->lti_oi;
3733 struct dt_object *obj;
3734 struct lov_mds_md_v1 *lmm;
3735 struct lov_ost_data_v1 *objs;
3736 __u32 idx = pfid->f_stripe_idx;
3744 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3746 RETURN(PTR_ERR(obj));
3748 dt_read_lock(env, obj, 0);
3749 if (unlikely(dt_object_exists(obj) == 0 ||
3750 lfsck_is_dead_obj(obj)))
3751 GOTO(unlock, rc = -ENOENT);
3753 if (!S_ISREG(lfsck_object_type(obj)))
3754 GOTO(unlock, rc = -ENODATA);
3756 rc = lfsck_layout_get_lovea(env, obj, buf);
3761 GOTO(unlock, rc = -ENODATA);
3764 rc = lfsck_layout_verify_header(lmm);
3768 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
3769 * been verified in lfsck_layout_verify_header() already. If some
3770 * new magic introduced in the future, then layout LFSCK needs to
3771 * be updated also. */
3772 magic = le32_to_cpu(lmm->lmm_magic);
3773 if (magic == LOV_MAGIC_V1) {
3774 objs = &lmm->lmm_objects[0];
3776 LASSERT(magic == LOV_MAGIC_V3);
3777 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3780 fid_to_ostid(cfid, oi);
3781 count = le16_to_cpu(lmm->lmm_stripe_count);
3782 for (i = 0; i < count; i++, objs++) {
3785 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
3786 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
3787 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
3790 GOTO(unlock, rc = -ENODATA);
3793 dt_read_unlock(env, obj);
3794 lfsck_object_put(env, obj);
3800 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
3801 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
3802 * layout EA from MDT to OST. On one hand, the OST no need to understand
3803 * the layout EA structure; on the other hand, it may cause trouble when
3804 * transfer large layout EA from MDT to OST via normal OUT RPC.
3806 * \ret > 0: unrecognized stripe
3807 * \ret = 0: recognized stripe
3808 * \ret < 0: other failures
3810 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
3811 struct lfsck_component *com,
3812 struct lu_fid *cfid,
3813 struct lu_fid *pfid)
3815 struct lfsck_instance *lfsck = com->lc_lfsck;
3816 struct obd_device *obd = lfsck->li_obd;
3817 struct seq_server_site *ss = lfsck_dev_site(lfsck);
3818 struct obd_export *exp = NULL;
3819 struct ptlrpc_request *req = NULL;
3820 struct lfsck_request *lr;
3821 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
3825 if (unlikely(fid_is_idif(pfid)))
3828 fld_range_set_any(range);
3829 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
3831 RETURN(rc == -ENOENT ? 1 : rc);
3833 if (unlikely(!fld_range_is_mdt(range)))
3836 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
3837 if (unlikely(exp == NULL))
3840 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
3841 GOTO(out, rc = -EOPNOTSUPP);
3843 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3845 GOTO(out, rc = -ENOMEM);
3847 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3849 ptlrpc_request_free(req);
3854 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3855 memset(lr, 0, sizeof(*lr));
3856 lr->lr_event = LE_PAIRS_VERIFY;
3857 lr->lr_active = LFSCK_TYPE_LAYOUT;
3858 lr->lr_fid = *cfid; /* OST-object itself FID. */
3859 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
3861 ptlrpc_request_set_replen(req);
3862 rc = ptlrpc_queue_wait(req);
3863 ptlrpc_req_finished(req);
3865 if (rc == -ENOENT || rc == -ENODATA)
3872 class_export_put(exp);
3877 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
3878 struct lfsck_component *com,
3879 struct lfsck_request *lr)
3881 struct dt_object *obj;
3885 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lr->lr_fid);
3887 GOTO(log, rc = PTR_ERR(obj));
3889 dt_write_lock(env, obj, 0);
3890 if (unlikely(dt_object_exists(obj) == 0 ||
3891 lfsck_is_dead_obj(obj)))
3892 GOTO(unlock, rc = 0);
3894 rc = __lfsck_layout_update_pfid(env, obj, &lr->lr_fid2,
3900 dt_write_unlock(env, obj);
3901 lfsck_object_put(env, obj);
3904 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
3905 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
3906 PFID(&lr->lr_fid), PFID(&lr->lr_fid2), rc);
3913 static void lfsck_layout_slave_quit(const struct lu_env *env,
3914 struct lfsck_component *com);
3916 static int lfsck_layout_reset(const struct lu_env *env,
3917 struct lfsck_component *com, bool init)
3919 struct lfsck_layout *lo = com->lc_file_ram;
3922 down_write(&com->lc_sem);
3924 memset(lo, 0, com->lc_file_size);
3926 __u32 count = lo->ll_success_count;
3927 __u64 last_time = lo->ll_time_last_complete;
3929 memset(lo, 0, com->lc_file_size);
3930 lo->ll_success_count = count;
3931 lo->ll_time_last_complete = last_time;
3934 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
3935 lo->ll_status = LS_INIT;
3937 if (com->lc_lfsck->li_master) {
3938 struct lfsck_assistant_data *lad = com->lc_data;
3940 lad->lad_incomplete = 0;
3941 CFS_RESET_BITMAP(lad->lad_bitmap);
3944 rc = lfsck_layout_store(env, com);
3945 up_write(&com->lc_sem);
3947 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
3948 lfsck_lfsck2name(com->lc_lfsck), rc);
3953 static void lfsck_layout_fail(const struct lu_env *env,
3954 struct lfsck_component *com, bool new_checked)
3956 struct lfsck_layout *lo = com->lc_file_ram;
3958 down_write(&com->lc_sem);
3960 com->lc_new_checked++;
3961 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
3962 up_write(&com->lc_sem);
3965 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
3966 struct lfsck_component *com, bool init)
3968 struct lfsck_instance *lfsck = com->lc_lfsck;
3969 struct lfsck_layout *lo = com->lc_file_ram;
3973 rc = lfsck_checkpoint_generic(env, com);
3975 return rc > 0 ? 0 : rc;
3978 down_write(&com->lc_sem);
3980 lo->ll_pos_latest_start =
3981 lfsck->li_pos_checkpoint.lp_oit_cookie;
3983 lo->ll_pos_last_checkpoint =
3984 lfsck->li_pos_checkpoint.lp_oit_cookie;
3985 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3986 HALF_SEC - lfsck->li_time_last_checkpoint);
3987 lo->ll_time_last_checkpoint = cfs_time_current_sec();
3988 lo->ll_objs_checked_phase1 += com->lc_new_checked;
3989 com->lc_new_checked = 0;
3992 rc = lfsck_layout_store(env, com);
3993 up_write(&com->lc_sem);
3995 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
3996 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3997 lfsck->li_pos_current.lp_oit_cookie, rc);
4002 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
4003 struct lfsck_component *com, bool init)
4005 struct lfsck_instance *lfsck = com->lc_lfsck;
4006 struct lfsck_layout *lo = com->lc_file_ram;
4009 if (com->lc_new_checked == 0 && !init)
4012 down_write(&com->lc_sem);
4014 lo->ll_pos_latest_start =
4015 lfsck->li_pos_checkpoint.lp_oit_cookie;
4017 lo->ll_pos_last_checkpoint =
4018 lfsck->li_pos_checkpoint.lp_oit_cookie;
4019 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4020 HALF_SEC - lfsck->li_time_last_checkpoint);
4021 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4022 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4023 com->lc_new_checked = 0;
4026 rc = lfsck_layout_store(env, com);
4027 up_write(&com->lc_sem);
4029 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
4030 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
4031 lfsck->li_pos_current.lp_oit_cookie, rc);
4036 static int lfsck_layout_prep(const struct lu_env *env,
4037 struct lfsck_component *com,
4038 struct lfsck_start *start)
4040 struct lfsck_instance *lfsck = com->lc_lfsck;
4041 struct lfsck_layout *lo = com->lc_file_ram;
4042 struct lfsck_position *pos = &com->lc_pos_start;
4044 fid_zero(&pos->lp_dir_parent);
4045 pos->lp_dir_cookie = 0;
4046 if (lo->ll_status == LS_COMPLETED ||
4047 lo->ll_status == LS_PARTIAL ||
4048 /* To handle orphan, must scan from the beginning. */
4049 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
4052 rc = lfsck_layout_reset(env, com, false);
4054 rc = lfsck_set_param(env, lfsck, start, true);
4057 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
4058 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4064 down_write(&com->lc_sem);
4065 lo->ll_time_latest_start = cfs_time_current_sec();
4066 spin_lock(&lfsck->li_lock);
4067 if (lo->ll_flags & LF_SCANNED_ONCE) {
4068 if (!lfsck->li_drop_dryrun ||
4069 lo->ll_pos_first_inconsistent == 0) {
4070 lo->ll_status = LS_SCANNING_PHASE2;
4071 list_move_tail(&com->lc_link,
4072 &lfsck->li_list_double_scan);
4073 pos->lp_oit_cookie = 0;
4077 lo->ll_status = LS_SCANNING_PHASE1;
4078 lo->ll_run_time_phase1 = 0;
4079 lo->ll_run_time_phase2 = 0;
4080 lo->ll_objs_checked_phase1 = 0;
4081 lo->ll_objs_checked_phase2 = 0;
4082 lo->ll_objs_failed_phase1 = 0;
4083 lo->ll_objs_failed_phase2 = 0;
4084 for (i = 0; i < LLIT_MAX; i++)
4085 lo->ll_objs_repaired[i] = 0;
4087 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4088 fid_zero(&com->lc_fid_latest_scanned_phase2);
4091 lo->ll_status = LS_SCANNING_PHASE1;
4092 if (!lfsck->li_drop_dryrun ||
4093 lo->ll_pos_first_inconsistent == 0)
4094 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
4096 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4098 spin_unlock(&lfsck->li_lock);
4099 up_write(&com->lc_sem);
4104 static int lfsck_layout_slave_prep(const struct lu_env *env,
4105 struct lfsck_component *com,
4106 struct lfsck_start_param *lsp)
4108 struct lfsck_layout_slave_data *llsd = com->lc_data;
4109 struct lfsck_instance *lfsck = com->lc_lfsck;
4110 struct lfsck_layout *lo = com->lc_file_ram;
4111 struct lfsck_start *start = lsp->lsp_start;
4114 rc = lfsck_layout_prep(env, com, start);
4118 if (lo->ll_flags & LF_CRASHED_LASTID &&
4119 list_empty(&llsd->llsd_master_list)) {
4120 LASSERT(lfsck->li_out_notify != NULL);
4122 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4123 LE_LASTID_REBUILDING);
4126 if (!lsp->lsp_index_valid)
4129 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
4130 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
4131 LASSERT(!llsd->llsd_rbtree_valid);
4133 write_lock(&llsd->llsd_rb_lock);
4134 rc = lfsck_rbtree_setup(env, com);
4135 write_unlock(&llsd->llsd_rb_lock);
4138 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
4139 LPU64"]\n", lfsck_lfsck2name(lfsck),
4140 com->lc_pos_start.lp_oit_cookie);
4145 static int lfsck_layout_master_prep(const struct lu_env *env,
4146 struct lfsck_component *com,
4147 struct lfsck_start_param *lsp)
4152 rc = lfsck_layout_load_bitmap(env, com);
4154 rc = lfsck_layout_reset(env, com, false);
4156 rc = lfsck_set_param(env, com->lc_lfsck,
4157 lsp->lsp_start, true);
4163 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
4167 rc = lfsck_start_assistant(env, com, lsp);
4172 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
4173 LPU64"]\n", lfsck_lfsck2name(com->lc_lfsck),
4174 com->lc_pos_start.lp_oit_cookie);
4179 /* Pre-fetch the attribute for each stripe in the given layout EA. */
4180 static int lfsck_layout_scan_stripes(const struct lu_env *env,
4181 struct lfsck_component *com,
4182 struct dt_object *parent,
4183 struct lov_mds_md_v1 *lmm)
4185 struct lfsck_thread_info *info = lfsck_env_info(env);
4186 struct lfsck_instance *lfsck = com->lc_lfsck;
4187 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4188 struct lfsck_layout *lo = com->lc_file_ram;
4189 struct lfsck_assistant_data *lad = com->lc_data;
4190 struct lfsck_layout_object *llo = NULL;
4191 struct lov_ost_data_v1 *objs;
4192 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4193 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4194 struct ptlrpc_thread *athread = &lad->lad_thread;
4195 struct l_wait_info lwi = { 0 };
4203 lfsck_buf_init(&buf, &info->lti_old_pfid,
4204 sizeof(struct filter_fid_old));
4205 count = le16_to_cpu(lmm->lmm_stripe_count);
4206 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
4207 * been verified in lfsck_layout_verify_header() already. If some
4208 * new magic introduced in the future, then layout LFSCK needs to
4209 * be updated also. */
4210 magic = le32_to_cpu(lmm->lmm_magic);
4211 if (magic == LOV_MAGIC_V1) {
4212 objs = &lmm->lmm_objects[0];
4214 LASSERT(magic == LOV_MAGIC_V3);
4215 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4218 for (i = 0; i < count; i++, objs++) {
4219 struct lu_fid *fid = &info->lti_fid;
4220 struct ost_id *oi = &info->lti_oi;
4221 struct lfsck_layout_req *llr;
4222 struct lfsck_tgt_desc *tgt = NULL;
4223 struct dt_object *cobj = NULL;
4225 bool wakeup = false;
4227 if (unlikely(lovea_slot_is_dummy(objs)))
4230 l_wait_event(mthread->t_ctl_waitq,
4231 lad->lad_prefetched < bk->lb_async_windows ||
4232 !thread_is_running(mthread) ||
4233 thread_is_stopped(athread),
4236 if (unlikely(!thread_is_running(mthread)) ||
4237 thread_is_stopped(athread))
4240 if (unlikely(lfsck_is_dead_obj(parent)))
4243 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4244 index = le32_to_cpu(objs->l_ost_idx);
4245 rc = ostid_to_fid(fid, oi, index);
4247 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
4248 ": "DOSTID", idx:%u\n", lfsck_lfsck2name(lfsck),
4249 PFID(lfsck_dto2fid(parent)), POSTID(oi), index);
4253 tgt = lfsck_tgt_get(ltds, index);
4254 if (unlikely(tgt == NULL)) {
4255 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
4256 "did not join the layout LFSCK\n",
4257 lfsck_lfsck2name(lfsck), index);
4258 lfsck_lad_set_bitmap(env, com, index);
4262 /* There is potential deadlock race condition between object
4263 * destroy and layout LFSCK. Consider the following scenario:
4265 * 1) The LFSCK thread obtained the parent object firstly, at
4266 * that time, the parent object has not been destroyed yet.
4268 * 2) One RPC service thread destroyed the parent and all its
4269 * children objects. Because the LFSCK is referencing the
4270 * parent object, then the parent object will be marked as
4271 * dying in RAM. On the other hand, the parent object is
4272 * referencing all its children objects, then all children
4273 * objects will be marked as dying in RAM also.
4275 * 3) The LFSCK thread tries to find some child object with
4276 * the parent object referenced. Then it will find that the
4277 * child object is dying. According to the object visibility
4278 * rules: the object with dying flag cannot be returned to
4279 * others. So the LFSCK thread has to wait until the dying
4280 * object has been purged from RAM, then it can allocate a
4281 * new object (with the same FID) in RAM. Unfortunately, the
4282 * LFSCK thread itself is referencing the parent object, and
4283 * cause the parent object cannot be purged, then cause the
4284 * child object cannot be purged also. So the LFSCK thread
4285 * will fall into deadlock.
4287 * We introduce non-blocked version lu_object_find() to allow
4288 * the LFSCK thread to return failure immediately (instead of
4289 * wait) when it finds dying (child) object, then the LFSCK
4290 * thread can check whether the parent object is dying or not.
4291 * So avoid above deadlock. LU-5395 */
4292 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
4294 if (lfsck_is_dead_obj(parent)) {
4304 rc = dt_declare_attr_get(env, cobj);
4308 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
4313 llo = lfsck_layout_object_init(env, parent,
4314 lfsck->li_pos_current.lp_oit_cookie);
4321 llr = lfsck_layout_assistant_req_init(llo,
4322 lfsck_dto2fid(parent),
4330 spin_lock(&lad->lad_lock);
4331 if (lad->lad_assistant_status < 0) {
4332 spin_unlock(&lad->lad_lock);
4333 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
4335 RETURN(lad->lad_assistant_status);
4338 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
4339 if (lad->lad_prefetched == 0)
4342 lad->lad_prefetched++;
4343 spin_unlock(&lad->lad_lock);
4345 wake_up_all(&athread->t_ctl_waitq);
4348 down_write(&com->lc_sem);
4349 com->lc_new_checked++;
4351 lfsck_layout_record_failure(env, lfsck, lo);
4352 up_write(&com->lc_sem);
4354 if (cobj != NULL && !IS_ERR(cobj))
4355 lfsck_object_put(env, cobj);
4357 if (likely(tgt != NULL))
4360 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4367 if (llo != NULL && !IS_ERR(llo))
4368 lfsck_layout_object_put(env, llo);
4373 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
4374 * the OST-object's attribute and generate an structure lfsck_layout_req on the
4375 * list ::lad_req_list.
4377 * For each request on above list, the lfsck_layout_assistant thread compares
4378 * the OST side attribute with local attribute, if inconsistent, then repair it.
4380 * All above processing is async mode with pipeline. */
4381 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
4382 struct lfsck_component *com,
4383 struct dt_object *obj)
4385 struct lfsck_thread_info *info = lfsck_env_info(env);
4386 struct ost_id *oi = &info->lti_oi;
4387 struct lfsck_layout *lo = com->lc_file_ram;
4388 struct lfsck_assistant_data *lad = com->lc_data;
4389 struct lfsck_instance *lfsck = com->lc_lfsck;
4390 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4391 struct thandle *handle = NULL;
4392 struct lu_buf *buf = &info->lti_big_buf;
4393 struct lov_mds_md_v1 *lmm = NULL;
4394 struct dt_device *dev = lfsck_obj2dev(obj);
4395 struct lustre_handle lh = { 0 };
4396 struct lu_buf ea_buf = { NULL };
4399 bool locked = false;
4400 bool stripe = false;
4401 bool bad_oi = false;
4404 if (!S_ISREG(lfsck_object_type(obj)))
4407 if (lad->lad_assistant_status < 0)
4408 GOTO(out, rc = -ESRCH);
4410 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
4411 lmm_oi_cpu_to_le(oi, oi);
4412 dt_read_lock(env, obj, 0);
4416 if (dt_object_exists(obj) == 0 ||
4417 lfsck_is_dead_obj(obj))
4420 rc = lfsck_layout_get_lovea(env, obj, buf);
4426 rc = lfsck_layout_verify_header(lmm);
4427 /* If the LOV EA crashed, then it is possible to be rebuilt later
4428 * when handle orphan OST-objects. */
4432 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0)
4433 GOTO(out, stripe = true);
4435 /* Inconsistent lmm_oi, should be repaired. */
4439 if (bk->lb_param & LPF_DRYRUN) {
4440 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4442 GOTO(out, stripe = true);
4445 if (!lustre_handle_is_used(&lh)) {
4446 dt_read_unlock(env, obj);
4448 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4449 MDS_INODELOCK_LAYOUT |
4450 MDS_INODELOCK_XATTR, LCK_EX);
4454 handle = dt_trans_create(env, dev);
4456 GOTO(out, rc = PTR_ERR(handle));
4458 lfsck_buf_init(&ea_buf, lmm, size);
4459 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4460 LU_XATTR_REPLACE, handle);
4464 rc = dt_trans_start_local(env, dev, handle);
4468 dt_write_lock(env, obj, 0);
4474 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4475 LU_XATTR_REPLACE, handle);
4479 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4481 GOTO(out, stripe = true);
4485 if (lustre_handle_is_used(&lh))
4486 dt_write_unlock(env, obj);
4488 dt_read_unlock(env, obj);
4491 if (handle != NULL && !IS_ERR(handle))
4492 dt_trans_stop(env, dev, handle);
4494 lfsck_ibits_unlock(&lh, LCK_EX);
4497 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
4498 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
4499 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
4500 PFID(lfsck_dto2fid(obj)), rc);
4503 rc = lfsck_layout_scan_stripes(env, com, obj, lmm);
4505 down_write(&com->lc_sem);
4506 com->lc_new_checked++;
4508 lfsck_layout_record_failure(env, lfsck, lo);
4509 up_write(&com->lc_sem);
4515 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
4516 struct lfsck_component *com,
4517 struct dt_object *obj)
4519 struct lfsck_instance *lfsck = com->lc_lfsck;
4520 struct lfsck_layout *lo = com->lc_file_ram;
4521 const struct lu_fid *fid = lfsck_dto2fid(obj);
4522 struct lfsck_layout_slave_data *llsd = com->lc_data;
4523 struct lfsck_layout_seq *lls;
4529 LASSERT(llsd != NULL);
4531 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
4532 cfs_fail_val == lfsck_dev_idx(lfsck)) {
4533 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
4535 struct ptlrpc_thread *thread = &lfsck->li_thread;
4537 l_wait_event(thread->t_ctl_waitq,
4538 !thread_is_running(thread),
4542 lfsck_rbtree_update_bitmap(env, com, fid, false);
4544 down_write(&com->lc_sem);
4545 if (fid_is_idif(fid))
4547 else if (!fid_is_norm(fid) ||
4548 !fid_is_for_ostobj(env, lfsck, obj, fid))
4549 GOTO(unlock, rc = 0);
4552 com->lc_new_checked++;
4554 lls = lfsck_layout_seq_lookup(llsd, seq);
4557 if (unlikely(lls == NULL))
4558 GOTO(unlock, rc = -ENOMEM);
4560 INIT_LIST_HEAD(&lls->lls_list);
4562 rc = lfsck_layout_lastid_load(env, com, lls);
4564 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4565 "load LAST_ID for "LPX64": rc = %d\n",
4566 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
4567 lo->ll_objs_failed_phase1++;
4572 lfsck_layout_seq_insert(llsd, lls);
4575 if (unlikely(fid_is_last_id(fid)))
4576 GOTO(unlock, rc = 0);
4578 if (fid_is_idif(fid))
4579 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
4583 if (oid > lls->lls_lastid_known)
4584 lls->lls_lastid_known = oid;
4586 if (oid > lls->lls_lastid) {
4587 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
4588 /* OFD may create new objects during LFSCK scanning. */
4589 rc = lfsck_layout_lastid_reload(env, com, lls);
4590 if (unlikely(rc != 0)) {
4591 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4592 "reload LAST_ID for "LPX64": rc = %d\n",
4593 lfsck_lfsck2name(com->lc_lfsck),
4599 if (oid <= lls->lls_lastid ||
4600 lo->ll_flags & LF_CRASHED_LASTID)
4601 GOTO(unlock, rc = 0);
4603 LASSERT(lfsck->li_out_notify != NULL);
4605 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4606 LE_LASTID_REBUILDING);
4607 lo->ll_flags |= LF_CRASHED_LASTID;
4609 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
4610 "LAST_ID file (2) for the sequence "LPX64
4611 ", old value "LPU64", known value "LPU64"\n",
4612 lfsck_lfsck2name(lfsck), lls->lls_seq,
4613 lls->lls_lastid, oid);
4616 lls->lls_lastid = oid;
4620 GOTO(unlock, rc = 0);
4623 up_write(&com->lc_sem);
4628 static int lfsck_layout_exec_dir(const struct lu_env *env,
4629 struct lfsck_component *com,
4630 struct lu_dirent *ent, __u16 type)
4635 static int lfsck_layout_master_post(const struct lu_env *env,
4636 struct lfsck_component *com,
4637 int result, bool init)
4639 struct lfsck_instance *lfsck = com->lc_lfsck;
4640 struct lfsck_layout *lo = com->lc_file_ram;
4644 lfsck_post_generic(env, com, &result);
4646 down_write(&com->lc_sem);
4647 spin_lock(&lfsck->li_lock);
4649 lo->ll_pos_last_checkpoint =
4650 lfsck->li_pos_checkpoint.lp_oit_cookie;
4653 if (lo->ll_flags & LF_INCOMPLETE)
4654 lo->ll_status = LS_PARTIAL;
4656 lo->ll_status = LS_SCANNING_PHASE2;
4657 lo->ll_flags |= LF_SCANNED_ONCE;
4658 lo->ll_flags &= ~LF_UPGRADE;
4659 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4660 } else if (result == 0) {
4661 if (lfsck->li_status != 0)
4662 lo->ll_status = lfsck->li_status;
4664 lo->ll_status = LS_STOPPED;
4665 if (lo->ll_status != LS_PAUSED)
4666 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4668 lo->ll_status = LS_FAILED;
4669 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4671 spin_unlock(&lfsck->li_lock);
4674 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4675 HALF_SEC - lfsck->li_time_last_checkpoint);
4676 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4677 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4678 com->lc_new_checked = 0;
4681 rc = lfsck_layout_store(env, com);
4682 up_write(&com->lc_sem);
4684 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
4685 lfsck_lfsck2name(lfsck), rc);
4690 static int lfsck_layout_slave_post(const struct lu_env *env,
4691 struct lfsck_component *com,
4692 int result, bool init)
4694 struct lfsck_instance *lfsck = com->lc_lfsck;
4695 struct lfsck_layout *lo = com->lc_file_ram;
4699 rc = lfsck_layout_lastid_store(env, com);
4703 LASSERT(lfsck->li_out_notify != NULL);
4705 down_write(&com->lc_sem);
4706 spin_lock(&lfsck->li_lock);
4708 lo->ll_pos_last_checkpoint =
4709 lfsck->li_pos_checkpoint.lp_oit_cookie;
4712 lo->ll_status = LS_SCANNING_PHASE2;
4713 lo->ll_flags |= LF_SCANNED_ONCE;
4714 if (lo->ll_flags & LF_CRASHED_LASTID) {
4716 lo->ll_flags &= ~LF_CRASHED_LASTID;
4718 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
4719 "crashed LAST_ID files successfully\n",
4720 lfsck_lfsck2name(lfsck));
4722 lo->ll_flags &= ~LF_UPGRADE;
4723 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4724 } else if (result == 0) {
4725 if (lfsck->li_status != 0)
4726 lo->ll_status = lfsck->li_status;
4728 lo->ll_status = LS_STOPPED;
4729 if (lo->ll_status != LS_PAUSED)
4730 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4732 lo->ll_status = LS_FAILED;
4733 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4735 spin_unlock(&lfsck->li_lock);
4738 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4742 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4743 HALF_SEC - lfsck->li_time_last_checkpoint);
4744 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4745 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4746 com->lc_new_checked = 0;
4749 rc = lfsck_layout_store(env, com);
4750 up_write(&com->lc_sem);
4752 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
4754 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
4755 lfsck_lfsck2name(lfsck), rc);
4760 static int lfsck_layout_dump(const struct lu_env *env,
4761 struct lfsck_component *com, struct seq_file *m)
4763 struct lfsck_instance *lfsck = com->lc_lfsck;
4764 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4765 struct lfsck_layout *lo = com->lc_file_ram;
4768 down_read(&com->lc_sem);
4769 seq_printf(m, "name: lfsck_layout\n"
4775 lfsck_status2names(lo->ll_status));
4777 rc = lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
4781 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4785 rc = lfsck_time_dump(m, lo->ll_time_last_complete,
4790 rc = lfsck_time_dump(m, lo->ll_time_latest_start,
4795 rc = lfsck_time_dump(m, lo->ll_time_last_checkpoint,
4800 seq_printf(m, "latest_start_position: "LPU64"\n"
4801 "last_checkpoint_position: "LPU64"\n"
4802 "first_failure_position: "LPU64"\n",
4803 lo->ll_pos_latest_start,
4804 lo->ll_pos_last_checkpoint,
4805 lo->ll_pos_first_inconsistent);
4807 seq_printf(m, "success_count: %u\n"
4808 "repaired_dangling: "LPU64"\n"
4809 "repaired_unmatched_pair: "LPU64"\n"
4810 "repaired_multiple_referenced: "LPU64"\n"
4811 "repaired_orphan: "LPU64"\n"
4812 "repaired_inconsistent_owner: "LPU64"\n"
4813 "repaired_others: "LPU64"\n"
4814 "skipped: "LPU64"\n"
4815 "failed_phase1: "LPU64"\n"
4816 "failed_phase2: "LPU64"\n",
4817 lo->ll_success_count,
4818 lo->ll_objs_repaired[LLIT_DANGLING - 1],
4819 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
4820 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
4821 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
4822 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
4823 lo->ll_objs_repaired[LLIT_OTHERS - 1],
4824 lo->ll_objs_skipped,
4825 lo->ll_objs_failed_phase1,
4826 lo->ll_objs_failed_phase2);
4828 if (lo->ll_status == LS_SCANNING_PHASE1) {
4830 const struct dt_it_ops *iops;
4831 cfs_duration_t duration = cfs_time_current() -
4832 lfsck->li_time_last_checkpoint;
4833 __u64 checked = lo->ll_objs_checked_phase1 +
4834 com->lc_new_checked;
4835 __u64 speed = checked;
4836 __u64 new_checked = com->lc_new_checked *
4837 msecs_to_jiffies(MSEC_PER_SEC);
4838 __u32 rtime = lo->ll_run_time_phase1 +
4839 cfs_duration_sec(duration + HALF_SEC);
4842 do_div(new_checked, duration);
4844 do_div(speed, rtime);
4845 seq_printf(m, "checked_phase1: "LPU64"\n"
4846 "checked_phase2: "LPU64"\n"
4847 "run_time_phase1: %u seconds\n"
4848 "run_time_phase2: %u seconds\n"
4849 "average_speed_phase1: "LPU64" items/sec\n"
4850 "average_speed_phase2: N/A\n"
4851 "real-time_speed_phase1: "LPU64" items/sec\n"
4852 "real-time_speed_phase2: N/A\n",
4854 lo->ll_objs_checked_phase2,
4856 lo->ll_run_time_phase2,
4860 LASSERT(lfsck->li_di_oit != NULL);
4862 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4864 /* The low layer otable-based iteration position may NOT
4865 * exactly match the layout-based directory traversal
4866 * cookie. Generally, it is not a serious issue. But the
4867 * caller should NOT make assumption on that. */
4868 pos = iops->store(env, lfsck->li_di_oit);
4869 if (!lfsck->li_current_oit_processed)
4871 seq_printf(m, "current_position: "LPU64"\n", pos);
4873 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
4874 cfs_duration_t duration = cfs_time_current() -
4875 lfsck->li_time_last_checkpoint;
4876 __u64 checked = lo->ll_objs_checked_phase2 +
4877 com->lc_new_checked;
4878 __u64 speed1 = lo->ll_objs_checked_phase1;
4879 __u64 speed2 = checked;
4880 __u64 new_checked = com->lc_new_checked *
4881 msecs_to_jiffies(MSEC_PER_SEC);
4882 __u32 rtime = lo->ll_run_time_phase2 +
4883 cfs_duration_sec(duration + HALF_SEC);
4886 do_div(new_checked, duration);
4887 if (lo->ll_run_time_phase1 != 0)
4888 do_div(speed1, lo->ll_run_time_phase1);
4890 do_div(speed2, rtime);
4891 rc = seq_printf(m, "checked_phase1: "LPU64"\n"
4892 "checked_phase2: "LPU64"\n"
4893 "run_time_phase1: %u seconds\n"
4894 "run_time_phase2: %u seconds\n"
4895 "average_speed_phase1: "LPU64" items/sec\n"
4896 "average_speed_phase2: "LPU64" items/sec\n"
4897 "real-time_speed_phase1: N/A\n"
4898 "real-time_speed_phase2: "LPU64" items/sec\n"
4899 "current_position: "DFID"\n",
4900 lo->ll_objs_checked_phase1,
4902 lo->ll_run_time_phase1,
4907 PFID(&com->lc_fid_latest_scanned_phase2));
4912 __u64 speed1 = lo->ll_objs_checked_phase1;
4913 __u64 speed2 = lo->ll_objs_checked_phase2;
4915 if (lo->ll_run_time_phase1 != 0)
4916 do_div(speed1, lo->ll_run_time_phase1);
4917 if (lo->ll_run_time_phase2 != 0)
4918 do_div(speed2, lo->ll_run_time_phase2);
4919 seq_printf(m, "checked_phase1: "LPU64"\n"
4920 "checked_phase2: "LPU64"\n"
4921 "run_time_phase1: %u seconds\n"
4922 "run_time_phase2: %u seconds\n"
4923 "average_speed_phase1: "LPU64" items/sec\n"
4924 "average_speed_phase2: "LPU64" objs/sec\n"
4925 "real-time_speed_phase1: N/A\n"
4926 "real-time_speed_phase2: N/A\n"
4927 "current_position: N/A\n",
4928 lo->ll_objs_checked_phase1,
4929 lo->ll_objs_checked_phase2,
4930 lo->ll_run_time_phase1,
4931 lo->ll_run_time_phase2,
4936 up_read(&com->lc_sem);
4941 static int lfsck_layout_master_double_scan(const struct lu_env *env,
4942 struct lfsck_component *com)
4944 struct lfsck_layout *lo = com->lc_file_ram;
4945 struct lfsck_assistant_data *lad = com->lc_data;
4946 struct lfsck_instance *lfsck = com->lc_lfsck;
4947 struct lfsck_tgt_descs *ltds;
4948 struct lfsck_tgt_desc *ltd;
4949 struct lfsck_tgt_desc *next;
4952 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
4954 if (thread_is_stopped(&lad->lad_thread)) {
4955 LASSERT(list_empty(&lad->lad_req_list));
4956 LASSERT(list_empty(&lad->lad_ost_phase1_list));
4957 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4959 ltds = &lfsck->li_ost_descs;
4960 spin_lock(<ds->ltd_lock);
4961 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
4962 ltd_layout_phase_list) {
4963 list_del_init(<d->ltd_layout_phase_list);
4965 spin_unlock(<ds->ltd_lock);
4967 ltds = &lfsck->li_mdt_descs;
4968 spin_lock(<ds->ltd_lock);
4969 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4970 ltd_layout_phase_list) {
4971 list_del_init(<d->ltd_layout_phase_list);
4973 spin_unlock(<ds->ltd_lock);
4979 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
4980 struct lfsck_component *com)
4982 struct lfsck_instance *lfsck = com->lc_lfsck;
4983 struct lfsck_layout_slave_data *llsd = com->lc_data;
4984 struct lfsck_layout *lo = com->lc_file_ram;
4985 struct ptlrpc_thread *thread = &lfsck->li_thread;
4989 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
4990 lfsck_lfsck2name(lfsck));
4992 atomic_inc(&lfsck->li_double_scan_count);
4994 if (lo->ll_flags & LF_INCOMPLETE)
4997 com->lc_new_checked = 0;
4998 com->lc_new_scanned = 0;
4999 com->lc_time_last_checkpoint = cfs_time_current();
5000 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5001 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5004 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
5007 rc = lfsck_layout_slave_query_master(env, com);
5008 if (list_empty(&llsd->llsd_master_list)) {
5009 if (unlikely(!thread_is_running(thread)))
5020 rc = l_wait_event(thread->t_ctl_waitq,
5021 !thread_is_running(thread) ||
5022 lo->ll_flags & LF_INCOMPLETE ||
5023 list_empty(&llsd->llsd_master_list),
5025 if (unlikely(!thread_is_running(thread)))
5028 if (lo->ll_flags & LF_INCOMPLETE)
5031 if (rc == -ETIMEDOUT)
5034 GOTO(done, rc = (rc < 0 ? rc : 1));
5038 rc = lfsck_layout_double_scan_result(env, com, rc);
5039 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
5040 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
5041 lfsck_layout_slave_quit(env, com);
5042 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
5043 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5045 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
5046 "status %d: rc = %d\n",
5047 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
5052 static void lfsck_layout_master_data_release(const struct lu_env *env,
5053 struct lfsck_component *com)
5055 struct lfsck_assistant_data *lad = com->lc_data;
5056 struct lfsck_instance *lfsck = com->lc_lfsck;
5057 struct lfsck_tgt_descs *ltds;
5058 struct lfsck_tgt_desc *ltd;
5059 struct lfsck_tgt_desc *next;
5061 LASSERT(lad != NULL);
5062 LASSERT(thread_is_init(&lad->lad_thread) ||
5063 thread_is_stopped(&lad->lad_thread));
5064 LASSERT(list_empty(&lad->lad_req_list));
5066 com->lc_data = NULL;
5068 ltds = &lfsck->li_ost_descs;
5069 spin_lock(<ds->ltd_lock);
5070 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
5071 ltd_layout_phase_list) {
5072 list_del_init(<d->ltd_layout_phase_list);
5074 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
5075 ltd_layout_phase_list) {
5076 list_del_init(<d->ltd_layout_phase_list);
5078 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
5080 list_del_init(<d->ltd_layout_list);
5082 spin_unlock(<ds->ltd_lock);
5084 ltds = &lfsck->li_mdt_descs;
5085 spin_lock(<ds->ltd_lock);
5086 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5087 ltd_layout_phase_list) {
5088 list_del_init(<d->ltd_layout_phase_list);
5090 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5091 ltd_layout_phase_list) {
5092 list_del_init(<d->ltd_layout_phase_list);
5094 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
5096 list_del_init(<d->ltd_layout_list);
5098 spin_unlock(<ds->ltd_lock);
5100 if (likely(lad->lad_bitmap != NULL))
5101 CFS_FREE_BITMAP(lad->lad_bitmap);
5106 static void lfsck_layout_slave_data_release(const struct lu_env *env,
5107 struct lfsck_component *com)
5109 struct lfsck_layout_slave_data *llsd = com->lc_data;
5111 lfsck_layout_slave_quit(env, com);
5112 com->lc_data = NULL;
5116 static void lfsck_layout_master_quit(const struct lu_env *env,
5117 struct lfsck_component *com)
5119 struct lfsck_assistant_data *lad = com->lc_data;
5120 struct lfsck_instance *lfsck = com->lc_lfsck;
5121 struct lfsck_tgt_descs *ltds;
5122 struct lfsck_tgt_desc *ltd;
5123 struct lfsck_tgt_desc *next;
5125 LASSERT(lad != NULL);
5127 lfsck_quit_generic(env, com);
5129 LASSERT(thread_is_init(&lad->lad_thread) ||
5130 thread_is_stopped(&lad->lad_thread));
5131 LASSERT(list_empty(&lad->lad_req_list));
5133 ltds = &lfsck->li_ost_descs;
5134 spin_lock(<ds->ltd_lock);
5135 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
5136 ltd_layout_phase_list) {
5137 list_del_init(<d->ltd_layout_phase_list);
5139 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
5140 ltd_layout_phase_list) {
5141 list_del_init(<d->ltd_layout_phase_list);
5143 spin_unlock(<ds->ltd_lock);
5145 ltds = &lfsck->li_mdt_descs;
5146 spin_lock(<ds->ltd_lock);
5147 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5148 ltd_layout_phase_list) {
5149 list_del_init(<d->ltd_layout_phase_list);
5151 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5152 ltd_layout_phase_list) {
5153 list_del_init(<d->ltd_layout_phase_list);
5155 spin_unlock(<ds->ltd_lock);
5158 static void lfsck_layout_slave_quit(const struct lu_env *env,
5159 struct lfsck_component *com)
5161 struct lfsck_layout_slave_data *llsd = com->lc_data;
5162 struct lfsck_layout_seq *lls;
5163 struct lfsck_layout_seq *next;
5164 struct lfsck_layout_slave_target *llst;
5166 LASSERT(llsd != NULL);
5168 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
5170 list_del_init(&lls->lls_list);
5171 lfsck_object_put(env, lls->lls_lastid_obj);
5175 spin_lock(&llsd->llsd_lock);
5176 while (!list_empty(&llsd->llsd_master_list)) {
5177 llst = list_entry(llsd->llsd_master_list.next,
5178 struct lfsck_layout_slave_target, llst_list);
5179 list_del_init(&llst->llst_list);
5180 spin_unlock(&llsd->llsd_lock);
5181 lfsck_layout_llst_put(llst);
5182 spin_lock(&llsd->llsd_lock);
5184 spin_unlock(&llsd->llsd_lock);
5186 lfsck_rbtree_cleanup(env, com);
5189 static int lfsck_layout_master_in_notify(const struct lu_env *env,
5190 struct lfsck_component *com,
5191 struct lfsck_request *lr,
5194 struct lfsck_instance *lfsck = com->lc_lfsck;
5195 struct lfsck_layout *lo = com->lc_file_ram;
5196 struct lfsck_assistant_data *lad = com->lc_data;
5197 struct lfsck_tgt_descs *ltds;
5198 struct lfsck_tgt_desc *ltd;
5202 if (lr->lr_event == LE_PAIRS_VERIFY) {
5205 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
5211 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
5212 "from %s %x, status %d, flags %x, flags2 %x\n",
5213 lfsck_lfsck2name(lfsck), lr->lr_event,
5214 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
5215 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
5217 if (lr->lr_event != LE_PHASE1_DONE &&
5218 lr->lr_event != LE_PHASE2_DONE &&
5219 lr->lr_event != LE_PEER_EXIT)
5222 if (lr->lr_flags & LEF_FROM_OST)
5223 ltds = &lfsck->li_ost_descs;
5225 ltds = &lfsck->li_mdt_descs;
5226 spin_lock(<ds->ltd_lock);
5227 ltd = LTD_TGT(ltds, lr->lr_index);
5229 spin_unlock(<ds->ltd_lock);
5234 list_del_init(<d->ltd_layout_phase_list);
5235 switch (lr->lr_event) {
5236 case LE_PHASE1_DONE:
5237 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
5238 if (lr->lr_flags2 & LF_INCOMPLETE) {
5239 if (lr->lr_flags & LEF_FROM_OST)
5240 lfsck_lad_set_bitmap(env, com,
5243 lo->ll_flags |= LF_INCOMPLETE;
5245 ltd->ltd_layout_done = 1;
5246 list_del_init(<d->ltd_layout_list);
5251 if (lr->lr_flags & LEF_FROM_OST) {
5252 if (list_empty(<d->ltd_layout_list))
5253 list_add_tail(<d->ltd_layout_list,
5254 &lad->lad_ost_list);
5255 list_add_tail(<d->ltd_layout_phase_list,
5256 &lad->lad_ost_phase2_list);
5258 if (list_empty(<d->ltd_layout_list))
5259 list_add_tail(<d->ltd_layout_list,
5260 &lad->lad_mdt_list);
5261 list_add_tail(<d->ltd_layout_phase_list,
5262 &lad->lad_mdt_phase2_list);
5265 case LE_PHASE2_DONE:
5266 ltd->ltd_layout_done = 1;
5267 if (!list_empty(<d->ltd_layout_list)) {
5268 list_del_init(<d->ltd_layout_list);
5269 if (lr->lr_flags2 & LF_INCOMPLETE) {
5270 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
5278 ltd->ltd_layout_done = 1;
5279 list_del_init(<d->ltd_layout_list);
5280 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
5281 !(lr->lr_flags & LEF_FROM_OST))
5282 lo->ll_flags |= LF_INCOMPLETE;
5287 spin_unlock(<ds->ltd_lock);
5289 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5290 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5292 memset(stop, 0, sizeof(*stop));
5293 stop->ls_status = lr->lr_status;
5294 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5295 lfsck_stop(env, lfsck->li_bottom, stop);
5296 } else if (lfsck_phase2_next_ready(lad)) {
5297 wake_up_all(&lad->lad_thread.t_ctl_waitq);
5303 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
5304 struct lfsck_component *com,
5305 struct lfsck_request *lr,
5308 struct lfsck_instance *lfsck = com->lc_lfsck;
5309 struct lfsck_layout_slave_data *llsd = com->lc_data;
5310 struct lfsck_layout_slave_target *llst;
5314 switch (lr->lr_event) {
5315 case LE_FID_ACCESSED:
5316 lfsck_rbtree_update_bitmap(env, com, &lr->lr_fid, true);
5318 case LE_CONDITIONAL_DESTROY:
5319 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
5321 case LE_PAIRS_VERIFY: {
5322 lr->lr_status = LPVS_INIT;
5323 /* Firstly, if the MDT-object which is claimed via OST-object
5324 * local stored PFID xattr recognizes the OST-object, then it
5325 * must be that the client given PFID is wrong. */
5326 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5331 lr->lr_status = LPVS_INCONSISTENT;
5332 /* The OST-object local stored PFID xattr is stale. We need to
5333 * check whether the MDT-object that is claimed via the client
5334 * given PFID information recognizes the OST-object or not. If
5335 * matches, then need to update the OST-object's PFID xattr. */
5336 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5339 * We are not sure whether the client given PFID information
5340 * is correct or not, do nothing to avoid improper fixing.
5343 * The client given PFID information is also invalid, we can
5344 * NOT fix the OST-object inconsistency.
5349 lr->lr_status = LPVS_INCONSISTENT_TOFIX;
5350 rc = lfsck_layout_slave_repair_pfid(env, com, lr);
5354 case LE_PHASE1_DONE: {
5355 if (lr->lr_flags2 & LF_INCOMPLETE) {
5356 struct lfsck_layout *lo = com->lc_file_ram;
5358 lo->ll_flags |= LF_INCOMPLETE;
5359 llst = lfsck_layout_llst_find_and_del(llsd,
5363 lfsck_layout_llst_put(llst);
5364 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5370 case LE_PHASE2_DONE:
5372 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
5373 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
5374 lr->lr_event, lr->lr_index, lr->lr_status);
5380 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
5384 lfsck_layout_llst_put(llst);
5385 if (list_empty(&llsd->llsd_master_list))
5386 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5388 if (lr->lr_event == LE_PEER_EXIT &&
5389 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
5390 (list_empty(&llsd->llsd_master_list) &&
5391 (lr->lr_status == LS_STOPPED ||
5392 lr->lr_status == LS_CO_STOPPED)))) {
5393 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5395 memset(stop, 0, sizeof(*stop));
5396 stop->ls_status = lr->lr_status;
5397 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5398 lfsck_stop(env, lfsck->li_bottom, stop);
5404 static int lfsck_layout_query(const struct lu_env *env,
5405 struct lfsck_component *com)
5407 struct lfsck_layout *lo = com->lc_file_ram;
5409 return lo->ll_status;
5412 /* with lfsck::li_lock held */
5413 static int lfsck_layout_slave_join(const struct lu_env *env,
5414 struct lfsck_component *com,
5415 struct lfsck_start_param *lsp)
5417 struct lfsck_instance *lfsck = com->lc_lfsck;
5418 struct lfsck_layout_slave_data *llsd = com->lc_data;
5419 struct lfsck_layout_slave_target *llst;
5420 struct lfsck_start *start = lsp->lsp_start;
5424 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
5427 if (!lsp->lsp_index_valid)
5430 /* If someone is running the LFSCK without orphan handling,
5431 * it will not maintain the object accessing rbtree. So we
5432 * cannot join it for orphan handling. */
5433 if (!llsd->llsd_rbtree_valid)
5436 spin_unlock(&lfsck->li_lock);
5437 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5438 spin_lock(&lfsck->li_lock);
5439 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
5440 spin_unlock(&lfsck->li_lock);
5441 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
5444 lfsck_layout_llst_put(llst);
5445 spin_lock(&lfsck->li_lock);
5452 static struct lfsck_operations lfsck_layout_master_ops = {
5453 .lfsck_reset = lfsck_layout_reset,
5454 .lfsck_fail = lfsck_layout_fail,
5455 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
5456 .lfsck_prep = lfsck_layout_master_prep,
5457 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
5458 .lfsck_exec_dir = lfsck_layout_exec_dir,
5459 .lfsck_post = lfsck_layout_master_post,
5460 .lfsck_dump = lfsck_layout_dump,
5461 .lfsck_double_scan = lfsck_layout_master_double_scan,
5462 .lfsck_data_release = lfsck_layout_master_data_release,
5463 .lfsck_quit = lfsck_layout_master_quit,
5464 .lfsck_in_notify = lfsck_layout_master_in_notify,
5465 .lfsck_query = lfsck_layout_query,
5468 static struct lfsck_operations lfsck_layout_slave_ops = {
5469 .lfsck_reset = lfsck_layout_reset,
5470 .lfsck_fail = lfsck_layout_fail,
5471 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
5472 .lfsck_prep = lfsck_layout_slave_prep,
5473 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
5474 .lfsck_exec_dir = lfsck_layout_exec_dir,
5475 .lfsck_post = lfsck_layout_slave_post,
5476 .lfsck_dump = lfsck_layout_dump,
5477 .lfsck_double_scan = lfsck_layout_slave_double_scan,
5478 .lfsck_data_release = lfsck_layout_slave_data_release,
5479 .lfsck_quit = lfsck_layout_slave_quit,
5480 .lfsck_in_notify = lfsck_layout_slave_in_notify,
5481 .lfsck_query = lfsck_layout_query,
5482 .lfsck_join = lfsck_layout_slave_join,
5485 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
5486 struct lfsck_component *com,
5487 struct lfsck_position *pos)
5489 struct lfsck_assistant_data *lad = com->lc_data;
5490 struct lfsck_layout_req *llr;
5492 if (list_empty(&lad->lad_req_list))
5495 llr = list_entry(lad->lad_req_list.next,
5496 struct lfsck_layout_req,
5498 pos->lp_oit_cookie = llr->llr_parent->llo_cookie - 1;
5501 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
5502 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
5503 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
5504 .la_fill_pos = lfsck_layout_assistant_fill_pos,
5505 .la_double_scan_result = lfsck_layout_double_scan_result,
5506 .la_req_fini = lfsck_layout_assistant_req_fini,
5507 .la_sync_failures = lfsck_layout_assistant_sync_failures,
5510 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
5512 struct lfsck_component *com;
5513 struct lfsck_layout *lo;
5514 struct dt_object *root = NULL;
5515 struct dt_object *obj;
5523 INIT_LIST_HEAD(&com->lc_link);
5524 INIT_LIST_HEAD(&com->lc_link_dir);
5525 init_rwsem(&com->lc_sem);
5526 atomic_set(&com->lc_ref, 1);
5527 com->lc_lfsck = lfsck;
5528 com->lc_type = LFSCK_TYPE_LAYOUT;
5529 if (lfsck->li_master) {
5530 com->lc_ops = &lfsck_layout_master_ops;
5531 com->lc_data = lfsck_assistant_data_init(
5532 &lfsck_layout_assistant_ops,
5534 if (com->lc_data == NULL)
5535 GOTO(out, rc = -ENOMEM);
5537 struct lfsck_layout_slave_data *llsd;
5539 com->lc_ops = &lfsck_layout_slave_ops;
5540 OBD_ALLOC_PTR(llsd);
5542 GOTO(out, rc = -ENOMEM);
5544 INIT_LIST_HEAD(&llsd->llsd_seq_list);
5545 INIT_LIST_HEAD(&llsd->llsd_master_list);
5546 spin_lock_init(&llsd->llsd_lock);
5547 llsd->llsd_rb_root = RB_ROOT;
5548 rwlock_init(&llsd->llsd_rb_lock);
5549 com->lc_data = llsd;
5551 com->lc_file_size = sizeof(*lo);
5552 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
5553 if (com->lc_file_ram == NULL)
5554 GOTO(out, rc = -ENOMEM);
5556 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
5557 if (com->lc_file_disk == NULL)
5558 GOTO(out, rc = -ENOMEM);
5560 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
5562 GOTO(out, rc = PTR_ERR(root));
5564 if (unlikely(!dt_try_as_dir(env, root)))
5565 GOTO(out, rc = -ENOTDIR);
5567 obj = local_file_find_or_create(env, lfsck->li_los, root,
5569 S_IFREG | S_IRUGO | S_IWUSR);
5571 GOTO(out, rc = PTR_ERR(obj));
5574 rc = lfsck_layout_load(env, com);
5576 rc = lfsck_layout_reset(env, com, true);
5577 else if (rc == -ENOENT)
5578 rc = lfsck_layout_init(env, com);
5583 lo = com->lc_file_ram;
5584 switch (lo->ll_status) {
5590 spin_lock(&lfsck->li_lock);
5591 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
5592 spin_unlock(&lfsck->li_lock);
5595 CERROR("%s: unknown lfsck_layout status %d\n",
5596 lfsck_lfsck2name(lfsck), lo->ll_status);
5598 case LS_SCANNING_PHASE1:
5599 case LS_SCANNING_PHASE2:
5600 /* No need to store the status to disk right now.
5601 * If the system crashed before the status stored,
5602 * it will be loaded back when next time. */
5603 lo->ll_status = LS_CRASHED;
5604 if (!lfsck->li_master)
5605 lo->ll_flags |= LF_INCOMPLETE;
5612 spin_lock(&lfsck->li_lock);
5613 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
5614 spin_unlock(&lfsck->li_lock);
5618 if (lo->ll_flags & LF_CRASHED_LASTID) {
5619 LASSERT(lfsck->li_out_notify != NULL);
5621 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5622 LE_LASTID_REBUILDING);
5628 if (root != NULL && !IS_ERR(root))
5629 lfsck_object_put(env, root);
5632 lfsck_component_cleanup(env, com);
5633 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
5634 lfsck_lfsck2name(lfsck), rc);
5640 struct lfsck_orphan_it {
5641 struct lfsck_component *loi_com;
5642 struct lfsck_rbtree_node *loi_lrn;
5643 struct lfsck_layout_slave_target *loi_llst;
5644 struct lu_fid loi_key;
5645 struct lu_orphan_rec loi_rec;
5647 unsigned int loi_over:1;
5650 static int lfsck_fid_match_idx(const struct lu_env *env,
5651 struct lfsck_instance *lfsck,
5652 const struct lu_fid *fid, int idx)
5654 struct seq_server_site *ss;
5655 struct lu_server_fld *sf;
5656 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
5659 /* All abnormal cases will be returned to MDT0. */
5660 if (!fid_is_norm(fid)) {
5667 ss = lfsck_dev_site(lfsck);
5668 if (unlikely(ss == NULL))
5671 sf = ss->ss_server_fld;
5672 LASSERT(sf != NULL);
5674 fld_range_set_any(range);
5675 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
5679 if (!fld_range_is_mdt(range))
5682 if (range->lsr_index == idx)
5688 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
5689 struct dt_object *obj)
5691 struct dt_device *dev = lfsck_obj2dev(obj);
5692 struct thandle *handle;
5696 handle = dt_trans_create(env, dev);
5700 rc = dt_declare_ref_del(env, obj, handle);
5704 rc = dt_declare_destroy(env, obj, handle);
5708 rc = dt_trans_start_local(env, dev, handle);
5712 dt_write_lock(env, obj, 0);
5713 rc = dt_ref_del(env, obj, handle);
5715 rc = dt_destroy(env, obj, handle);
5716 dt_write_unlock(env, obj);
5721 dt_trans_stop(env, dev, handle);
5723 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
5724 PFID(lfsck_dto2fid(obj)), rc);
5729 static int lfsck_orphan_index_lookup(const struct lu_env *env,
5730 struct dt_object *dt,
5732 const struct dt_key *key)
5737 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
5738 struct dt_object *dt,
5739 const struct dt_rec *rec,
5740 const struct dt_key *key,
5741 struct thandle *handle)
5746 static int lfsck_orphan_index_insert(const struct lu_env *env,
5747 struct dt_object *dt,
5748 const struct dt_rec *rec,
5749 const struct dt_key *key,
5750 struct thandle *handle,
5756 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
5757 struct dt_object *dt,
5758 const struct dt_key *key,
5759 struct thandle *handle)
5764 static int lfsck_orphan_index_delete(const struct lu_env *env,
5765 struct dt_object *dt,
5766 const struct dt_key *key,
5767 struct thandle *handle)
5772 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
5773 struct dt_object *dt,
5776 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
5777 struct lfsck_instance *lfsck;
5778 struct lfsck_component *com = NULL;
5779 struct lfsck_layout_slave_data *llsd;
5780 struct lfsck_orphan_it *it = NULL;
5781 struct lfsck_layout *lo;
5785 lfsck = lfsck_instance_find(dev, true, false);
5786 if (unlikely(lfsck == NULL))
5787 RETURN(ERR_PTR(-ENXIO));
5789 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
5790 if (unlikely(com == NULL))
5791 GOTO(out, rc = -ENOENT);
5793 lo = com->lc_file_ram;
5794 if (lo->ll_flags & LF_INCOMPLETE)
5795 GOTO(out, rc = -ESRCH);
5797 llsd = com->lc_data;
5798 if (!llsd->llsd_rbtree_valid)
5799 GOTO(out, rc = -ESRCH);
5803 GOTO(out, rc = -ENOMEM);
5805 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
5806 if (it->loi_llst == NULL)
5807 GOTO(out, rc = -ENXIO);
5809 if (dev->dd_record_fid_accessed) {
5810 /* The first iteration against the rbtree, scan the whole rbtree
5811 * to remove the nodes which do NOT need to be handled. */
5812 write_lock(&llsd->llsd_rb_lock);
5813 if (dev->dd_record_fid_accessed) {
5814 struct rb_node *node;
5815 struct rb_node *next;
5816 struct lfsck_rbtree_node *lrn;
5818 /* No need to record the fid accessing anymore. */
5819 dev->dd_record_fid_accessed = 0;
5821 node = rb_first(&llsd->llsd_rb_root);
5822 while (node != NULL) {
5823 next = rb_next(node);
5824 lrn = rb_entry(node, struct lfsck_rbtree_node,
5826 if (atomic_read(&lrn->lrn_known_count) <=
5827 atomic_read(&lrn->lrn_accessed_count)) {
5828 rb_erase(node, &llsd->llsd_rb_root);
5829 lfsck_rbtree_free(lrn);
5834 write_unlock(&llsd->llsd_rb_lock);
5837 /* read lock the rbtree when init, and unlock when fini */
5838 read_lock(&llsd->llsd_rb_lock);
5846 lfsck_component_put(env, com);
5848 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
5849 lfsck_lfsck2name(lfsck), rc);
5851 lfsck_instance_put(env, lfsck);
5856 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
5859 return (struct dt_it *)it;
5862 static void lfsck_orphan_it_fini(const struct lu_env *env,
5865 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5866 struct lfsck_component *com = it->loi_com;
5867 struct lfsck_layout_slave_data *llsd;
5868 struct lfsck_layout_slave_target *llst;
5871 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
5872 lfsck_lfsck2name(com->lc_lfsck));
5874 llsd = com->lc_data;
5875 read_unlock(&llsd->llsd_rb_lock);
5876 llst = it->loi_llst;
5877 LASSERT(llst != NULL);
5879 /* Save the key and hash for iterate next. */
5880 llst->llst_fid = it->loi_key;
5881 llst->llst_hash = it->loi_hash;
5882 lfsck_layout_llst_put(llst);
5883 lfsck_component_put(env, com);
5889 * \retval +1: the iteration finished
5890 * \retval 0: on success, not finished
5891 * \retval -ve: on error
5893 static int lfsck_orphan_it_next(const struct lu_env *env,
5896 struct lfsck_thread_info *info = lfsck_env_info(env);
5897 struct filter_fid_old *pfid = &info->lti_old_pfid;
5898 struct lu_attr *la = &info->lti_la;
5899 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5900 struct lu_fid *key = &it->loi_key;
5901 struct lu_orphan_rec *rec = &it->loi_rec;
5902 struct lfsck_component *com = it->loi_com;
5903 struct lfsck_instance *lfsck = com->lc_lfsck;
5904 struct lfsck_layout_slave_data *llsd = com->lc_data;
5905 struct dt_object *obj;
5906 struct lfsck_rbtree_node *lrn;
5910 __u32 idx = it->loi_llst->llst_index;
5920 lrn = lfsck_rbtree_search(llsd, key, &exact);
5928 key->f_seq = lrn->lrn_seq;
5929 key->f_oid = lrn->lrn_first_oid;
5934 if (unlikely(key->f_oid == 0)) {
5941 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
5947 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
5948 atomic_read(&lrn->lrn_accessed_count))) {
5949 struct rb_node *next = rb_next(&lrn->lrn_node);
5951 while (next != NULL) {
5952 lrn = rb_entry(next, struct lfsck_rbtree_node,
5954 if (atomic_read(&lrn->lrn_known_count) >
5955 atomic_read(&lrn->lrn_accessed_count))
5957 next = rb_next(next);
5966 key->f_seq = lrn->lrn_seq;
5967 key->f_oid = lrn->lrn_first_oid;
5971 pos = key->f_oid - lrn->lrn_first_oid;
5974 pos = find_next_bit(lrn->lrn_known_bitmap,
5975 LFSCK_RBTREE_BITMAP_WIDTH, pos);
5976 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
5977 key->f_oid = lrn->lrn_first_oid + pos;
5978 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
5986 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
5991 key->f_oid = lrn->lrn_first_oid + pos;
5992 obj = lfsck_object_find_bottom(env, lfsck, key);
5995 if (rc == -ENOENT) {
6002 dt_read_lock(env, obj, 0);
6003 if (dt_object_exists(obj) == 0 ||
6004 lfsck_is_dead_obj(obj)) {
6005 dt_read_unlock(env, obj);
6006 lfsck_object_put(env, obj);
6011 rc = dt_attr_get(env, obj, la);
6015 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, pfid, sizeof(*pfid)),
6017 if (rc == -ENODATA) {
6018 /* For the pre-created OST-object, update the bitmap to avoid
6019 * others LFSCK (second phase) iteration to touch it again. */
6020 if (la->la_ctime == 0) {
6021 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
6022 atomic_inc(&lrn->lrn_accessed_count);
6024 /* For the race between repairing dangling referenced
6025 * MDT-object and unlink the file, it may left orphan
6026 * OST-object there. Destroy it now! */
6027 if (unlikely(!(la->la_mode & S_ISUID))) {
6028 dt_read_unlock(env, obj);
6029 lfsck_layout_destroy_orphan(env, obj);
6030 lfsck_object_put(env, obj);
6034 } else if (idx == 0) {
6035 /* If the orphan OST-object has no parent information,
6036 * regard it as referenced by the MDT-object on MDT0. */
6037 fid_zero(&rec->lor_fid);
6038 rec->lor_uid = la->la_uid;
6039 rec->lor_gid = la->la_gid;
6043 dt_read_unlock(env, obj);
6044 lfsck_object_put(env, obj);
6052 if (rc != sizeof(struct filter_fid) &&
6053 rc != sizeof(struct filter_fid_old))
6054 GOTO(out, rc = -EINVAL);
6056 fid_le_to_cpu(&rec->lor_fid, &pfid->ff_parent);
6057 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
6058 * MDT-object's FID::f_ver, instead it is the OST-object index in its
6059 * parent MDT-object's layout EA. */
6060 save = rec->lor_fid.f_stripe_idx;
6061 rec->lor_fid.f_ver = 0;
6062 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_fid, idx);
6063 /* If the orphan OST-object does not claim the MDT, then next.
6065 * If we do not know whether it matches or not, then return it
6066 * to the MDT for further check. */
6068 dt_read_unlock(env, obj);
6069 lfsck_object_put(env, obj);
6074 rec->lor_fid.f_stripe_idx = save;
6075 rec->lor_uid = la->la_uid;
6076 rec->lor_gid = la->la_gid;
6078 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u\n",
6079 lfsck_lfsck2name(com->lc_lfsck), PFID(key), PFID(&rec->lor_fid),
6080 rec->lor_uid, rec->lor_gid);
6085 dt_read_unlock(env, obj);
6086 lfsck_object_put(env, obj);
6094 * \retval +1: locate to the exactly position
6095 * \retval 0: cannot locate to the exactly position,
6096 * call next() to move to a valid position.
6097 * \retval -ve: on error
6099 static int lfsck_orphan_it_get(const struct lu_env *env,
6101 const struct dt_key *key)
6103 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6106 it->loi_key = *(struct lu_fid *)key;
6107 rc = lfsck_orphan_it_next(env, di);
6117 static void lfsck_orphan_it_put(const struct lu_env *env,
6122 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
6123 const struct dt_it *di)
6125 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6127 return (struct dt_key *)&it->loi_key;
6130 static int lfsck_orphan_it_key_size(const struct lu_env *env,
6131 const struct dt_it *di)
6133 return sizeof(struct lu_fid);
6136 static int lfsck_orphan_it_rec(const struct lu_env *env,
6137 const struct dt_it *di,
6141 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6143 *(struct lu_orphan_rec *)rec = it->loi_rec;
6148 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
6149 const struct dt_it *di)
6151 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6153 return it->loi_hash;
6157 * \retval +1: locate to the exactly position
6158 * \retval 0: cannot locate to the exactly position,
6159 * call next() to move to a valid position.
6160 * \retval -ve: on error
6162 static int lfsck_orphan_it_load(const struct lu_env *env,
6163 const struct dt_it *di,
6166 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6167 struct lfsck_layout_slave_target *llst = it->loi_llst;
6170 LASSERT(llst != NULL);
6172 if (hash != llst->llst_hash) {
6173 CDEBUG(D_LFSCK, "%s: the given hash "LPU64" for orphan "
6174 "iteration does not match the one when fini "
6175 LPU64", to be reset.\n",
6176 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
6178 fid_zero(&llst->llst_fid);
6179 llst->llst_hash = 0;
6182 it->loi_key = llst->llst_fid;
6183 it->loi_hash = llst->llst_hash;
6184 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
6194 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
6195 const struct dt_it *di,
6201 const struct dt_index_operations lfsck_orphan_index_ops = {
6202 .dio_lookup = lfsck_orphan_index_lookup,
6203 .dio_declare_insert = lfsck_orphan_index_declare_insert,
6204 .dio_insert = lfsck_orphan_index_insert,
6205 .dio_declare_delete = lfsck_orphan_index_declare_delete,
6206 .dio_delete = lfsck_orphan_index_delete,
6208 .init = lfsck_orphan_it_init,
6209 .fini = lfsck_orphan_it_fini,
6210 .get = lfsck_orphan_it_get,
6211 .put = lfsck_orphan_it_put,
6212 .next = lfsck_orphan_it_next,
6213 .key = lfsck_orphan_it_key,
6214 .key_size = lfsck_orphan_it_key_size,
6215 .rec = lfsck_orphan_it_rec,
6216 .store = lfsck_orphan_it_store,
6217 .load = lfsck_orphan_it_load,
6218 .key_rec = lfsck_orphan_it_key_rec,