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, BYPASS_CAPA);
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, BYPASS_CAPA);
378 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
383 lu_buf_realloc(buf, rc);
384 if (buf->lb_buf == NULL)
396 if (unlikely(buf->lb_buf == NULL)) {
397 lu_buf_alloc(buf, rc);
398 if (buf->lb_buf == NULL)
407 static int lfsck_layout_verify_header(struct lov_mds_md_v1 *lmm)
412 magic = le32_to_cpu(lmm->lmm_magic);
413 /* If magic crashed, keep it there. Sometime later, during OST-object
414 * orphan handling, if some OST-object(s) back-point to it, it can be
415 * verified and repaired. */
416 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
420 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
421 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
426 CDEBUG(D_LFSCK, "%s LOV EA magic %u on "DOSTID"\n",
427 rc == -EINVAL ? "Unknown" : "Unsupported",
433 pattern = le32_to_cpu(lmm->lmm_pattern);
434 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
435 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
438 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
439 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u on "DOSTID"\n",
440 pattern, POSTID(&oi));
448 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_CACHE_SIZE
449 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
450 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
452 struct lfsck_rbtree_node {
453 struct rb_node lrn_node;
456 atomic_t lrn_known_count;
457 atomic_t lrn_accessed_count;
458 void *lrn_known_bitmap;
459 void *lrn_accessed_bitmap;
462 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
463 __u64 seq, __u32 oid)
465 if (seq < lrn->lrn_seq)
468 if (seq > lrn->lrn_seq)
471 if (oid < lrn->lrn_first_oid)
474 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
480 /* The caller should hold llsd->llsd_rb_lock. */
481 static struct lfsck_rbtree_node *
482 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
483 const struct lu_fid *fid, bool *exact)
485 struct rb_node *node = llsd->llsd_rb_root.rb_node;
486 struct rb_node *prev = NULL;
487 struct lfsck_rbtree_node *lrn = NULL;
493 while (node != NULL) {
495 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
496 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
498 node = node->rb_left;
500 node = node->rb_right;
508 /* If there is no exactly matched one, then to the next valid one. */
511 /* The rbtree is empty. */
518 node = rb_next(prev);
520 /* The end of the rbtree. */
524 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
529 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
530 const struct lu_fid *fid)
532 struct lfsck_rbtree_node *lrn;
536 return ERR_PTR(-ENOMEM);
538 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
539 if (lrn->lrn_known_bitmap == NULL) {
542 return ERR_PTR(-ENOMEM);
545 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
546 if (lrn->lrn_accessed_bitmap == NULL) {
547 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
550 return ERR_PTR(-ENOMEM);
553 RB_CLEAR_NODE(&lrn->lrn_node);
554 lrn->lrn_seq = fid_seq(fid);
555 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
556 atomic_set(&lrn->lrn_known_count, 0);
557 atomic_set(&lrn->lrn_accessed_count, 0);
562 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
564 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
565 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
569 /* The caller should hold lock. */
570 static struct lfsck_rbtree_node *
571 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
572 struct lfsck_rbtree_node *lrn)
574 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
575 struct rb_node *parent = NULL;
576 struct lfsck_rbtree_node *tmp;
579 while (*pos != NULL) {
581 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
582 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
584 pos = &(*pos)->rb_left;
586 pos = &(*pos)->rb_right;
591 rb_link_node(&lrn->lrn_node, parent, pos);
592 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
597 extern const struct dt_index_operations lfsck_orphan_index_ops;
599 static int lfsck_rbtree_setup(const struct lu_env *env,
600 struct lfsck_component *com)
602 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
603 struct lfsck_instance *lfsck = com->lc_lfsck;
604 struct dt_device *dev = lfsck->li_bottom;
605 struct lfsck_layout_slave_data *llsd = com->lc_data;
606 struct dt_object *obj;
608 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
609 fid->f_oid = lfsck_dev_idx(lfsck);
611 obj = dt_locate(env, dev, fid);
613 RETURN(PTR_ERR(obj));
615 /* Generate an in-RAM object to stand for the layout rbtree.
616 * Scanning the layout rbtree will be via the iteration over
617 * the object. In the future, the rbtree may be written onto
618 * disk with the object.
620 * Mark the object to be as exist. */
621 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
622 obj->do_index_ops = &lfsck_orphan_index_ops;
623 llsd->llsd_rb_obj = obj;
624 llsd->llsd_rbtree_valid = 1;
625 dev->dd_record_fid_accessed = 1;
627 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
628 lfsck_lfsck2name(lfsck));
633 static void lfsck_rbtree_cleanup(const struct lu_env *env,
634 struct lfsck_component *com)
636 struct lfsck_instance *lfsck = com->lc_lfsck;
637 struct lfsck_layout_slave_data *llsd = com->lc_data;
638 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
639 struct rb_node *next;
640 struct lfsck_rbtree_node *lrn;
642 lfsck->li_bottom->dd_record_fid_accessed = 0;
643 /* Invalid the rbtree, then no others will use it. */
644 write_lock(&llsd->llsd_rb_lock);
645 llsd->llsd_rbtree_valid = 0;
646 write_unlock(&llsd->llsd_rb_lock);
648 while (node != NULL) {
649 next = rb_next(node);
650 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
651 rb_erase(node, &llsd->llsd_rb_root);
652 lfsck_rbtree_free(lrn);
656 if (llsd->llsd_rb_obj != NULL) {
657 lfsck_object_put(env, llsd->llsd_rb_obj);
658 llsd->llsd_rb_obj = NULL;
661 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
662 lfsck_lfsck2name(lfsck));
665 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
666 struct lfsck_component *com,
667 const struct lu_fid *fid,
670 struct lfsck_layout_slave_data *llsd = com->lc_data;
671 struct lfsck_rbtree_node *lrn;
677 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
680 if (!fid_is_idif(fid) && !fid_is_norm(fid))
683 read_lock(&llsd->llsd_rb_lock);
684 if (!llsd->llsd_rbtree_valid)
685 GOTO(unlock, rc = 0);
687 lrn = lfsck_rbtree_search(llsd, fid, NULL);
689 struct lfsck_rbtree_node *tmp;
693 read_unlock(&llsd->llsd_rb_lock);
694 tmp = lfsck_rbtree_new(env, fid);
696 GOTO(out, rc = PTR_ERR(tmp));
699 write_lock(&llsd->llsd_rb_lock);
700 if (!llsd->llsd_rbtree_valid) {
701 lfsck_rbtree_free(tmp);
702 GOTO(unlock, rc = 0);
705 lrn = lfsck_rbtree_insert(llsd, tmp);
707 lfsck_rbtree_free(tmp);
710 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
711 /* Any accessed object must be a known object. */
712 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
713 atomic_inc(&lrn->lrn_known_count);
714 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
715 atomic_inc(&lrn->lrn_accessed_count);
717 GOTO(unlock, rc = 0);
721 write_unlock(&llsd->llsd_rb_lock);
723 read_unlock(&llsd->llsd_rb_lock);
725 if (rc != 0 && accessed) {
726 struct lfsck_layout *lo = com->lc_file_ram;
728 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
729 "bitmap, and will cause incorrect LFSCK OST-object "
730 "handling, so disable it to cancel orphan handling "
731 "for related device. rc = %d\n",
732 lfsck_lfsck2name(com->lc_lfsck), rc);
734 lo->ll_flags |= LF_INCOMPLETE;
735 lfsck_rbtree_cleanup(env, com);
739 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
740 const struct lfsck_layout *src)
744 des->ll_magic = le32_to_cpu(src->ll_magic);
745 des->ll_status = le32_to_cpu(src->ll_status);
746 des->ll_flags = le32_to_cpu(src->ll_flags);
747 des->ll_success_count = le32_to_cpu(src->ll_success_count);
748 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
749 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
750 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
751 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
752 des->ll_time_last_checkpoint =
753 le64_to_cpu(src->ll_time_last_checkpoint);
754 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
755 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
756 des->ll_pos_first_inconsistent =
757 le64_to_cpu(src->ll_pos_first_inconsistent);
758 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
759 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
760 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
761 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
762 for (i = 0; i < LLIT_MAX; i++)
763 des->ll_objs_repaired[i] =
764 le64_to_cpu(src->ll_objs_repaired[i]);
765 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
766 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
769 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
770 const struct lfsck_layout *src)
774 des->ll_magic = cpu_to_le32(src->ll_magic);
775 des->ll_status = cpu_to_le32(src->ll_status);
776 des->ll_flags = cpu_to_le32(src->ll_flags);
777 des->ll_success_count = cpu_to_le32(src->ll_success_count);
778 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
779 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
780 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
781 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
782 des->ll_time_last_checkpoint =
783 cpu_to_le64(src->ll_time_last_checkpoint);
784 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
785 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
786 des->ll_pos_first_inconsistent =
787 cpu_to_le64(src->ll_pos_first_inconsistent);
788 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
789 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
790 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
791 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
792 for (i = 0; i < LLIT_MAX; i++)
793 des->ll_objs_repaired[i] =
794 cpu_to_le64(src->ll_objs_repaired[i]);
795 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
796 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
800 * Load the OST bitmap from the lfsck_layout trace file.
802 * \param[in] env pointer to the thread context
803 * \param[in] com pointer to the lfsck component
805 * \retval 0 for success
806 * \retval negative error number on failure or data corruption
808 static int lfsck_layout_load_bitmap(const struct lu_env *env,
809 struct lfsck_component *com)
811 struct dt_object *obj = com->lc_obj;
812 struct lfsck_assistant_data *lad = com->lc_data;
813 struct lfsck_layout *lo = com->lc_file_ram;
814 cfs_bitmap_t *bitmap = lad->lad_bitmap;
815 loff_t pos = com->lc_file_size;
821 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
823 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
825 nbits = lo->ll_bitmap_size;
827 if (unlikely(nbits < BITS_PER_LONG))
828 nbits = BITS_PER_LONG;
830 if (nbits > bitmap->size) {
831 __u32 new_bits = bitmap->size;
832 cfs_bitmap_t *new_bitmap;
834 while (new_bits < nbits)
837 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
838 if (new_bitmap == NULL)
841 lad->lad_bitmap = new_bitmap;
842 CFS_FREE_BITMAP(bitmap);
846 if (lo->ll_bitmap_size == 0) {
847 lad->lad_incomplete = 0;
848 CFS_RESET_BITMAP(bitmap);
853 size = (lo->ll_bitmap_size + 7) >> 3;
854 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
856 RETURN(rc >= 0 ? -EINVAL : rc);
858 if (cfs_bitmap_check_empty(bitmap))
859 lad->lad_incomplete = 0;
861 lad->lad_incomplete = 1;
867 * Load the layout LFSCK trace file from disk.
869 * The layout LFSCK trace file records the layout LFSCK status information
870 * and other statistics, such as how many objects have been scanned, and how
871 * many objects have been repaired, and etc. It also contains the bitmap for
872 * failed OSTs during the layout LFSCK. All these information will be loaded
873 * from disk to RAM when the layout LFSCK component setup.
875 * \param[in] env pointer to the thread context
876 * \param[in] com pointer to the lfsck component
878 * \retval positive number for file data corruption, the caller
879 * should reset the layout LFSCK trace file
880 * \retval 0 for success
881 * \retval negative error number on failure
883 static int lfsck_layout_load(const struct lu_env *env,
884 struct lfsck_component *com)
886 struct lfsck_layout *lo = com->lc_file_ram;
887 ssize_t size = com->lc_file_size;
891 rc = dt_read(env, com->lc_obj,
892 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
896 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
897 lfsck_lfsck2name(com->lc_lfsck), rc);
899 } else if (rc != size) {
900 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
901 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
905 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
906 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
907 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
908 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
909 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
917 * Store the layout LFSCK trace file on disk.
919 * The layout LFSCK trace file records the layout LFSCK status information
920 * and other statistics, such as how many objects have been scanned, and how
921 * many objects have been repaired, and etc. It also contains the bitmap for
922 * failed OSTs during the layout LFSCK. All these information will be synced
923 * from RAM to disk periodically.
925 * \param[in] env pointer to the thread context
926 * \param[in] com pointer to the lfsck component
928 * \retval 0 for success
929 * \retval negative error number on failure
931 static int lfsck_layout_store(const struct lu_env *env,
932 struct lfsck_component *com)
934 struct dt_object *obj = com->lc_obj;
935 struct lfsck_instance *lfsck = com->lc_lfsck;
936 struct lfsck_layout *lo_ram = com->lc_file_ram;
937 struct lfsck_layout *lo = com->lc_file_disk;
939 struct dt_device *dev = lfsck_obj2dev(obj);
940 cfs_bitmap_t *bitmap = NULL;
942 ssize_t size = com->lc_file_size;
947 if (lfsck->li_master) {
948 struct lfsck_assistant_data *lad = com->lc_data;
950 bitmap = lad->lad_bitmap;
951 nbits = bitmap->size;
954 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
957 lo_ram->ll_bitmap_size = nbits;
958 lfsck_layout_cpu_to_le(lo, lo_ram);
959 th = dt_trans_create(env, dev);
961 GOTO(log, rc = PTR_ERR(th));
963 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
968 if (bitmap != NULL) {
969 rc = dt_declare_record_write(env, obj,
970 lfsck_buf_get(env, bitmap->data, nbits >> 3),
976 rc = dt_trans_start_local(env, dev, th);
981 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
985 if (bitmap != NULL) {
987 rc = dt_record_write(env, obj,
988 lfsck_buf_get(env, bitmap->data, nbits >> 3),
995 dt_trans_stop(env, dev, th);
999 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1000 lfsck_lfsck2name(lfsck), rc);
1005 static int lfsck_layout_init(const struct lu_env *env,
1006 struct lfsck_component *com)
1008 struct lfsck_layout *lo = com->lc_file_ram;
1011 memset(lo, 0, com->lc_file_size);
1012 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1013 lo->ll_status = LS_INIT;
1014 down_write(&com->lc_sem);
1015 rc = lfsck_layout_store(env, com);
1016 up_write(&com->lc_sem);
1021 static int fid_is_for_ostobj(const struct lu_env *env,
1022 struct lfsck_instance *lfsck,
1023 struct dt_object *obj, const struct lu_fid *fid)
1025 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1026 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1027 struct lustre_mdt_attrs *lma;
1030 fld_range_set_any(range);
1031 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1033 if (fld_range_is_ost(range))
1039 lma = &lfsck_env_info(env)->lti_lma;
1040 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
1041 XATTR_NAME_LMA, BYPASS_CAPA);
1042 if (rc == sizeof(*lma)) {
1043 lustre_lma_swab(lma);
1045 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1048 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID, BYPASS_CAPA);
1053 static struct lfsck_layout_seq *
1054 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1056 struct lfsck_layout_seq *lls;
1058 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1059 if (lls->lls_seq == seq)
1062 if (lls->lls_seq > seq)
1070 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1071 struct lfsck_layout_seq *lls)
1073 struct lfsck_layout_seq *tmp;
1074 struct list_head *pos = &llsd->llsd_seq_list;
1076 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1077 if (lls->lls_seq < tmp->lls_seq) {
1078 pos = &tmp->lls_list;
1082 list_add_tail(&lls->lls_list, pos);
1086 lfsck_layout_lastid_create(const struct lu_env *env,
1087 struct lfsck_instance *lfsck,
1088 struct dt_object *obj)
1090 struct lfsck_thread_info *info = lfsck_env_info(env);
1091 struct lu_attr *la = &info->lti_la;
1092 struct dt_object_format *dof = &info->lti_dof;
1093 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1094 struct dt_device *dt = lfsck_obj2dev(obj);
1101 if (bk->lb_param & LPF_DRYRUN)
1104 memset(la, 0, sizeof(*la));
1105 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1106 la->la_valid = LA_MODE | LA_UID | LA_GID;
1107 memset(dof, 0, sizeof(*dof));
1108 dof->dof_type = dt_mode_to_dft(S_IFREG);
1110 th = dt_trans_create(env, dt);
1112 GOTO(log, rc = PTR_ERR(th));
1114 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1118 rc = dt_declare_record_write(env, obj,
1119 lfsck_buf_get(env, &lastid,
1125 rc = dt_trans_start_local(env, dt, th);
1129 dt_write_lock(env, obj, 0);
1130 if (likely(dt_object_exists(obj) == 0)) {
1131 rc = dt_create(env, obj, la, NULL, dof, th);
1133 rc = dt_record_write(env, obj,
1134 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1137 dt_write_unlock(env, obj);
1142 dt_trans_stop(env, dt, th);
1145 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1147 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1153 lfsck_layout_lastid_reload(const struct lu_env *env,
1154 struct lfsck_component *com,
1155 struct lfsck_layout_seq *lls)
1161 dt_read_lock(env, lls->lls_lastid_obj, 0);
1162 rc = dt_record_read(env, lls->lls_lastid_obj,
1163 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1164 dt_read_unlock(env, lls->lls_lastid_obj);
1165 if (unlikely(rc != 0))
1168 lastid = le64_to_cpu(lastid);
1169 if (lastid < lls->lls_lastid_known) {
1170 struct lfsck_instance *lfsck = com->lc_lfsck;
1171 struct lfsck_layout *lo = com->lc_file_ram;
1173 lls->lls_lastid = lls->lls_lastid_known;
1175 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1176 LASSERT(lfsck->li_out_notify != NULL);
1178 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1179 LE_LASTID_REBUILDING);
1180 lo->ll_flags |= LF_CRASHED_LASTID;
1182 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1183 "LAST_ID file (1) for the sequence "LPX64
1184 ", old value "LPU64", known value "LPU64"\n",
1185 lfsck_lfsck2name(lfsck), lls->lls_seq,
1186 lastid, lls->lls_lastid);
1188 } else if (lastid >= lls->lls_lastid) {
1189 lls->lls_lastid = lastid;
1197 lfsck_layout_lastid_store(const struct lu_env *env,
1198 struct lfsck_component *com)
1200 struct lfsck_instance *lfsck = com->lc_lfsck;
1201 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1202 struct dt_device *dt = lfsck->li_bottom;
1203 struct lfsck_layout_slave_data *llsd = com->lc_data;
1204 struct lfsck_layout_seq *lls;
1210 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1213 if (!lls->lls_dirty)
1216 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1217 "<seq> "LPX64" as <oid> "LPU64"\n",
1218 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1220 if (bk->lb_param & LPF_DRYRUN) {
1225 th = dt_trans_create(env, dt);
1228 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1229 "the LAST_ID for <seq> "LPX64"(1): rc = %d\n",
1230 lfsck_lfsck2name(com->lc_lfsck),
1235 lastid = cpu_to_le64(lls->lls_lastid);
1236 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1237 lfsck_buf_get(env, &lastid,
1243 rc = dt_trans_start_local(env, dt, th);
1247 dt_write_lock(env, lls->lls_lastid_obj, 0);
1248 rc = dt_record_write(env, lls->lls_lastid_obj,
1249 lfsck_buf_get(env, &lastid,
1250 sizeof(lastid)), &pos, th);
1251 dt_write_unlock(env, lls->lls_lastid_obj);
1256 dt_trans_stop(env, dt, th);
1259 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1260 "the LAST_ID for <seq> "LPX64"(2): rc = %d\n",
1261 lfsck_lfsck2name(com->lc_lfsck),
1270 lfsck_layout_lastid_load(const struct lu_env *env,
1271 struct lfsck_component *com,
1272 struct lfsck_layout_seq *lls)
1274 struct lfsck_instance *lfsck = com->lc_lfsck;
1275 struct lfsck_layout *lo = com->lc_file_ram;
1276 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1277 struct dt_object *obj;
1282 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1283 obj = dt_locate(env, lfsck->li_bottom, fid);
1285 RETURN(PTR_ERR(obj));
1287 /* LAST_ID crashed, to be rebuilt */
1288 if (dt_object_exists(obj) == 0) {
1289 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1290 LASSERT(lfsck->li_out_notify != NULL);
1292 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1293 LE_LASTID_REBUILDING);
1294 lo->ll_flags |= LF_CRASHED_LASTID;
1296 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1297 "LAST_ID file for sequence "LPX64"\n",
1298 lfsck_lfsck2name(lfsck), lls->lls_seq);
1300 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1302 struct l_wait_info lwi = LWI_TIMEOUT(
1303 cfs_time_seconds(cfs_fail_val),
1306 /* Some others may changed the cfs_fail_val
1307 * as zero after above check, re-check it for
1308 * sure to avoid falling into wait for ever. */
1309 if (likely(lwi.lwi_timeout > 0)) {
1310 struct ptlrpc_thread *thread =
1313 up_write(&com->lc_sem);
1314 l_wait_event(thread->t_ctl_waitq,
1315 !thread_is_running(thread),
1317 down_write(&com->lc_sem);
1322 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1324 dt_read_lock(env, obj, 0);
1325 rc = dt_read(env, obj,
1326 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1328 dt_read_unlock(env, obj);
1329 if (rc != 0 && rc != sizeof(__u64))
1330 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1332 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1333 LASSERT(lfsck->li_out_notify != NULL);
1335 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1336 LE_LASTID_REBUILDING);
1337 lo->ll_flags |= LF_CRASHED_LASTID;
1339 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1340 "LAST_ID file for the sequence "LPX64
1342 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1345 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1353 lfsck_object_put(env, obj);
1355 lls->lls_lastid_obj = obj;
1360 static void lfsck_layout_record_failure(const struct lu_env *env,
1361 struct lfsck_instance *lfsck,
1362 struct lfsck_layout *lo)
1366 lo->ll_objs_failed_phase1++;
1367 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1369 if (lo->ll_pos_first_inconsistent == 0 ||
1370 lo->ll_pos_first_inconsistent < cookie) {
1371 lo->ll_pos_first_inconsistent = cookie;
1373 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1374 "inconsistency at the pos ["LPU64"]\n",
1375 lfsck_lfsck2name(lfsck),
1376 lo->ll_pos_first_inconsistent);
1380 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1381 struct lfsck_component *com,
1384 struct lfsck_instance *lfsck = com->lc_lfsck;
1385 struct lfsck_layout *lo = com->lc_file_ram;
1387 down_write(&com->lc_sem);
1388 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1389 HALF_SEC - lfsck->li_time_last_checkpoint);
1390 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1391 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1394 if (lo->ll_flags & LF_INCOMPLETE) {
1395 lo->ll_status = LS_PARTIAL;
1397 if (lfsck->li_master) {
1398 struct lfsck_assistant_data *lad = com->lc_data;
1400 if (lad->lad_incomplete)
1401 lo->ll_status = LS_PARTIAL;
1403 lo->ll_status = LS_COMPLETED;
1405 lo->ll_status = LS_COMPLETED;
1408 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1409 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1410 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1411 lo->ll_success_count++;
1412 } else if (rc == 0) {
1413 if (lfsck->li_status != 0)
1414 lo->ll_status = lfsck->li_status;
1416 lo->ll_status = LS_STOPPED;
1418 lo->ll_status = LS_FAILED;
1421 rc = lfsck_layout_store(env, com);
1422 up_write(&com->lc_sem);
1427 static int lfsck_layout_trans_stop(const struct lu_env *env,
1428 struct dt_device *dev,
1429 struct thandle *handle, int result)
1433 handle->th_result = result;
1434 rc = dt_trans_stop(env, dev, handle);
1444 * Get the system default stripe size.
1446 * \param[in] env pointer to the thread context
1447 * \param[in] lfsck pointer to the lfsck instance
1448 * \param[out] size pointer to the default stripe size
1450 * \retval 0 for success
1451 * \retval negative error number on failure
1453 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1454 struct lfsck_instance *lfsck,
1457 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1458 struct dt_object *root;
1461 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1463 return PTR_ERR(root);
1465 /* Get the default stripe size via xattr_get on the backend root. */
1466 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1467 XATTR_NAME_LOV, BYPASS_CAPA);
1469 /* The lum->lmm_stripe_size is LE mode. The *size also
1470 * should be LE mode. So it is unnecessary to convert. */
1471 *size = lum->lmm_stripe_size;
1473 } else if (unlikely(rc == 0)) {
1477 lfsck_object_put(env, root);
1483 * \retval +1: repaired
1484 * \retval 0: did nothing
1485 * \retval -ve: on error
1487 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1488 struct thandle *handle,
1489 struct dt_object *parent,
1490 struct lu_fid *cfid,
1492 struct lov_ost_data_v1 *slot,
1493 int fl, __u32 ost_idx)
1495 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1496 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1497 struct lu_buf ea_buf;
1503 magic = le32_to_cpu(lmm->lmm_magic);
1504 count = le16_to_cpu(lmm->lmm_stripe_count);
1506 fid_to_ostid(cfid, oi);
1507 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1508 slot->l_ost_gen = cpu_to_le32(0);
1509 slot->l_ost_idx = cpu_to_le32(ost_idx);
1511 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE) {
1512 struct lov_ost_data_v1 *objs;
1515 if (magic == LOV_MAGIC_V1)
1516 objs = &lmm->lmm_objects[0];
1518 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1519 for (i = 0; i < count; i++, objs++) {
1520 if (objs != slot && lovea_slot_is_dummy(objs))
1524 /* If the @slot is the last dummy slot to be refilled,
1525 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1527 lmm->lmm_pattern &= ~cpu_to_le32(LOV_PATTERN_F_HOLE);
1530 lfsck_buf_init(&ea_buf, lmm, lov_mds_md_size(count, magic));
1531 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle,
1540 * \retval +1: repaired
1541 * \retval 0: did nothing
1542 * \retval -ve: on error
1544 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1545 struct lfsck_instance *lfsck,
1546 struct thandle *handle,
1547 struct dt_object *parent,
1548 struct lu_fid *cfid,
1549 struct lu_buf *buf, int fl,
1550 __u32 ost_idx, __u32 ea_off, bool reset)
1552 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1553 struct lov_ost_data_v1 *objs;
1559 if (fl == LU_XATTR_CREATE || reset) {
1560 __u32 pattern = LOV_PATTERN_RAID0;
1563 LASSERT(buf->lb_len >= lov_mds_md_size(count, LOV_MAGIC_V1));
1565 if (ea_off != 0 || reset) {
1566 pattern |= LOV_PATTERN_F_HOLE;
1570 memset(lmm, 0, buf->lb_len);
1571 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1572 lmm->lmm_pattern = cpu_to_le32(pattern);
1573 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1574 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1576 rc = lfsck_layout_get_def_stripesize(env, lfsck,
1577 &lmm->lmm_stripe_size);
1581 objs = &lmm->lmm_objects[ea_off];
1583 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1586 count = le16_to_cpu(lmm->lmm_stripe_count);
1587 if (magic == LOV_MAGIC_V1)
1588 objs = &lmm->lmm_objects[count];
1590 objs = &((struct lov_mds_md_v3 *)lmm)->
1593 gap = ea_off - count;
1596 LASSERT(buf->lb_len >= lov_mds_md_size(count, magic));
1599 memset(objs, 0, gap * sizeof(*objs));
1600 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1604 lmm->lmm_layout_gen =
1605 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1609 lmm->lmm_stripe_count = cpu_to_le16(count);
1610 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1613 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1614 DFID": parent "DFID", OST-index %u, stripe-index %u, fl %d, "
1615 "reset %s, %s LOV EA hole: rc = %d\n",
1616 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1617 ost_idx, ea_off, fl, reset ? "yes" : "no",
1618 hole ? "with" : "without", rc);
1623 static int __lfsck_layout_update_pfid(const struct lu_env *env,
1624 struct dt_object *child,
1625 const struct lu_fid *pfid, __u32 offset)
1627 struct dt_device *dev = lfsck_obj2dev(child);
1628 struct filter_fid *ff = &lfsck_env_info(env)->lti_new_pfid;
1629 struct thandle *handle;
1630 struct lu_buf buf = { NULL };
1633 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1634 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1635 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1636 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1637 * parent MDT-object's layout EA. */
1638 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
1639 lfsck_buf_init(&buf, ff, sizeof(struct filter_fid));
1641 handle = dt_trans_create(env, dev);
1643 RETURN(PTR_ERR(handle));
1645 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1649 rc = dt_trans_start_local(env, dev, handle);
1653 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle,
1659 dt_trans_stop(env, dev, handle);
1665 * \retval +1: repaired
1666 * \retval 0: did nothing
1667 * \retval -ve: on error
1669 static int lfsck_layout_update_pfid(const struct lu_env *env,
1670 struct lfsck_component *com,
1671 struct dt_object *parent,
1672 struct lu_fid *cfid,
1673 struct dt_device *cdev, __u32 ea_off)
1675 struct dt_object *child;
1679 child = lfsck_object_find_by_dev(env, cdev, cfid);
1681 RETURN(PTR_ERR(child));
1683 rc = __lfsck_layout_update_pfid(env, child,
1684 lu_object_fid(&parent->do_lu), ea_off);
1685 lfsck_object_put(env, child);
1687 RETURN(rc == 0 ? 1 : rc);
1691 * This function will create the MDT-object with the given (partial) LOV EA.
1693 * Under some data corruption cases, the MDT-object of the file may be lost,
1694 * but its OST-objects, or some of them are there. The layout LFSCK needs to
1695 * re-create the MDT-object with the orphan OST-object(s) information.
1697 * On the other hand, the LFSCK may has created some OST-object for repairing
1698 * dangling LOV EA reference, but as the LFSCK processing, it may find that
1699 * the old OST-object is there and should replace the former new created OST
1700 * object. Unfortunately, some others have modified such newly created object.
1701 * To keep the data (both new and old), the LFSCK will create MDT-object with
1702 * new FID to reference the original OST-object.
1704 * \param[in] env pointer to the thread context
1705 * \param[in] com pointer to the lfsck component
1706 * \param[in] ltd pointer to target device descriptor
1707 * \param[in] rec pointer to the record for the orphan OST-object
1708 * \param[in] cfid pointer to FID for the orphan OST-object
1709 * \param[in] infix additional information, such as the FID for original
1710 * MDT-object and the stripe offset in the LOV EA
1711 * \param[in] type the type for describing why the orphan MDT-object is
1712 * created. The rules are as following:
1714 * type "C": Multiple OST-objects claim the same MDT-object and the
1715 * same slot in the layout EA. Then the LFSCK will create
1716 * new MDT-object(s) to hold the conflict OST-object(s).
1718 * type "N": The orphan OST-object does not know which one was the
1719 * real parent MDT-object, so the LFSCK uses new FID for
1720 * its parent MDT-object.
1722 * type "R": The orphan OST-object knows its parent MDT-object FID,
1723 * but does not know the position (the file name) in the
1726 * type "D": The MDT-object is a directory, it may knows its parent
1727 * but because there is no valid linkEA, the LFSCK cannot
1728 * know where to put it back to the namespace.
1729 * type "O": The MDT-object has no linkEA, and there is no name
1730 * entry that references the MDT-object.
1732 * type "P": The orphan object to be created was a parent directory
1733 * of some MDT-object which linkEA shows that the @orphan
1734 * object is missing.
1736 * The orphan name will be like:
1737 * ${FID}-${infix}-${type}-${conflict_version}
1739 * \param[in] ea_off the stripe offset in the LOV EA
1741 * \retval positive on repaired something
1742 * \retval 0 if needs to repair nothing
1743 * \retval negative error number on failure
1745 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1746 struct lfsck_component *com,
1747 struct lfsck_tgt_desc *ltd,
1748 struct lu_orphan_rec *rec,
1749 struct lu_fid *cfid,
1754 struct lfsck_thread_info *info = lfsck_env_info(env);
1755 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
1756 char *name = info->lti_key;
1757 struct lu_attr *la = &info->lti_la;
1758 struct dt_object_format *dof = &info->lti_dof;
1759 struct lfsck_instance *lfsck = com->lc_lfsck;
1760 struct lu_fid *pfid = &rec->lor_fid;
1761 struct lu_fid *tfid = &info->lti_fid3;
1762 struct dt_device *dev = lfsck->li_bottom;
1763 struct dt_object *lpf = lfsck->li_lpf_obj;
1764 struct dt_object *pobj = NULL;
1765 struct dt_object *cobj = NULL;
1766 struct thandle *th = NULL;
1767 struct lu_buf *ea_buf = &info->lti_big_buf;
1768 struct lu_buf lov_buf;
1769 struct lustre_handle lh = { 0 };
1770 struct linkea_data ldata = { NULL };
1771 struct lu_buf linkea_buf;
1772 const struct lu_name *pname;
1778 if (unlikely(lpf == NULL))
1779 GOTO(log, rc = -ENXIO);
1781 /* We use two separated transactions to repair the inconsistency.
1783 * 1) create the MDT-object locally.
1784 * 2) update the OST-object's PFID EA if necessary.
1786 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
1787 * updated when the layout LFSCK run next time.
1789 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
1790 * when the layout LFSCK run next time. */
1792 if (fid_is_zero(pfid)) {
1793 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1797 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1799 GOTO(log, rc = PTR_ERR(cobj));
1802 pobj = lfsck_object_find_by_dev(env, dev, pfid);
1804 GOTO(log, rc = PTR_ERR(pobj));
1806 LASSERT(infix != NULL);
1807 LASSERT(type != NULL);
1810 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
1812 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1813 (const struct dt_key *)name, BYPASS_CAPA);
1814 if (rc != 0 && rc != -ENOENT)
1818 rc = linkea_data_new(&ldata,
1819 &lfsck_env_info(env)->lti_linkea_buf);
1823 pname = lfsck_name_get_const(env, name, strlen(name));
1824 rc = linkea_add_buf(&ldata, pname, lfsck_dto2fid(lfsck->li_lpf_obj));
1828 memset(la, 0, sizeof(*la));
1829 la->la_uid = rec->lor_uid;
1830 la->la_gid = rec->lor_gid;
1831 la->la_mode = S_IFREG | S_IRUSR;
1832 la->la_valid = LA_MODE | LA_UID | LA_GID;
1834 memset(dof, 0, sizeof(*dof));
1835 dof->dof_type = dt_mode_to_dft(S_IFREG);
1836 /* Because the dof->dof_reg.striped = 0, the LOD will not create
1837 * the stripe(s). The LFSCK will specify the LOV EA via
1838 * lfsck_layout_extend_lovea(). */
1840 size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1841 if (ea_buf->lb_len < size) {
1842 lu_buf_realloc(ea_buf, size);
1843 if (ea_buf->lb_buf == NULL)
1844 GOTO(log, rc = -ENOMEM);
1847 /* Hold update lock on the .lustre/lost+found/MDTxxxx/.
1849 * XXX: Currently, we do not grab the PDO lock as normal create cases,
1850 * because creating MDT-object for orphan OST-object is rare, we
1851 * do not much care about the performance. It can be improved in
1852 * the future when needed. */
1853 rc = lfsck_ibits_lock(env, lfsck, lpf, &lh,
1854 MDS_INODELOCK_UPDATE, LCK_EX);
1858 /* The 1st transaction. */
1859 th = dt_trans_create(env, dev);
1861 GOTO(unlock, rc = PTR_ERR(th));
1863 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1867 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
1868 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
1869 LU_XATTR_REPLACE, th);
1873 dtrec->rec_fid = pfid;
1874 dtrec->rec_type = S_IFREG;
1875 rc = dt_declare_insert(env, lpf,
1876 (const struct dt_rec *)dtrec,
1877 (const struct dt_key *)name, th);
1881 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1882 ldata.ld_leh->leh_len);
1883 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
1884 XATTR_NAME_LINK, 0, th);
1888 rc = dt_trans_start_local(env, dev, th);
1892 dt_write_lock(env, pobj, 0);
1893 rc = dt_create(env, pobj, la, NULL, dof, th);
1895 rc = lfsck_layout_extend_lovea(env, lfsck, th, pobj, cfid,
1896 &lov_buf, 0, ltd->ltd_index, ea_off, true);
1897 dt_write_unlock(env, pobj);
1901 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
1902 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1906 rc = dt_xattr_set(env, pobj, &linkea_buf,
1907 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1908 if (rc == 0 && cobj != NULL) {
1909 dt_trans_stop(env, dev, th);
1912 /* The 2nd transaction. */
1913 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ea_off);
1920 dt_trans_stop(env, dev, th);
1923 lfsck_ibits_unlock(&lh, LCK_EX);
1926 if (cobj != NULL && !IS_ERR(cobj))
1927 lfsck_object_put(env, cobj);
1928 if (pobj != NULL && !IS_ERR(pobj))
1929 lfsck_object_put(env, pobj);
1932 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
1933 "recreate the lost MDT-object: parent "DFID
1934 ", child "DFID", OST-index %u, stripe-index %u, "
1935 "infix %s, type %s: rc = %d\n",
1936 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
1937 ltd->ltd_index, ea_off, infix, type, rc);
1939 return rc >= 0 ? 1 : rc;
1942 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
1943 struct lfsck_component *com,
1944 const struct lu_fid *fid,
1947 struct lfsck_thread_info *info = lfsck_env_info(env);
1948 struct lfsck_request *lr = &info->lti_lr;
1949 struct lfsck_instance *lfsck = com->lc_lfsck;
1950 struct lfsck_tgt_desc *ltd;
1951 struct ptlrpc_request *req;
1952 struct lfsck_request *tmp;
1953 struct obd_export *exp;
1957 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
1958 if (unlikely(ltd == NULL))
1962 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
1963 GOTO(put, rc = -EOPNOTSUPP);
1965 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
1967 GOTO(put, rc = -ENOMEM);
1969 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1971 ptlrpc_request_free(req);
1976 memset(lr, 0, sizeof(*lr));
1977 lr->lr_event = LE_CONDITIONAL_DESTROY;
1978 lr->lr_active = LFSCK_TYPE_LAYOUT;
1981 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1983 ptlrpc_request_set_replen(req);
1985 rc = ptlrpc_queue_wait(req);
1986 ptlrpc_req_finished(req);
1996 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
1997 struct lfsck_component *com,
1998 struct lfsck_request *lr)
2000 struct lfsck_thread_info *info = lfsck_env_info(env);
2001 struct lu_attr *la = &info->lti_la;
2002 ldlm_policy_data_t *policy = &info->lti_policy;
2003 struct ldlm_res_id *resid = &info->lti_resid;
2004 struct lfsck_instance *lfsck = com->lc_lfsck;
2005 struct dt_device *dev = lfsck->li_bottom;
2006 struct lu_fid *fid = &lr->lr_fid;
2007 struct dt_object *obj;
2008 struct thandle *th = NULL;
2009 struct lustre_handle lh = { 0 };
2014 obj = lfsck_object_find_by_dev(env, dev, fid);
2016 RETURN(PTR_ERR(obj));
2018 dt_read_lock(env, obj, 0);
2019 if (dt_object_exists(obj) == 0 ||
2020 lfsck_is_dead_obj(obj)) {
2021 dt_read_unlock(env, obj);
2023 GOTO(put, rc = -ENOENT);
2026 /* Get obj's attr without lock firstly. */
2027 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2028 dt_read_unlock(env, obj);
2032 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2033 GOTO(put, rc = -ETXTBSY);
2035 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2036 LASSERT(lfsck->li_namespace != NULL);
2038 memset(policy, 0, sizeof(*policy));
2039 policy->l_extent.end = OBD_OBJECT_EOF;
2040 ost_fid_build_resid(fid, resid);
2041 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2042 policy, LCK_EX, &flags, ldlm_blocking_ast,
2043 ldlm_completion_ast, NULL, NULL, 0,
2044 LVB_T_NONE, NULL, &lh);
2046 GOTO(put, rc = -EIO);
2048 dt_write_lock(env, obj, 0);
2049 /* Get obj's attr within lock again. */
2050 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2054 if (la->la_ctime != 0)
2055 GOTO(unlock, rc = -ETXTBSY);
2057 th = dt_trans_create(env, dev);
2059 GOTO(unlock, rc = PTR_ERR(th));
2061 rc = dt_declare_ref_del(env, obj, th);
2065 rc = dt_declare_destroy(env, obj, th);
2069 rc = dt_trans_start_local(env, dev, th);
2073 rc = dt_ref_del(env, obj, th);
2077 rc = dt_destroy(env, obj, th);
2079 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2080 "OST-object "DFID" that was created for reparing "
2081 "dangling referenced case. But the original missing "
2082 "OST-object is found now.\n",
2083 lfsck_lfsck2name(lfsck), PFID(fid));
2088 dt_trans_stop(env, dev, th);
2091 dt_write_unlock(env, obj);
2092 ldlm_lock_decref(&lh, LCK_EX);
2095 lfsck_object_put(env, obj);
2101 * Some OST-object has occupied the specified layout EA slot.
2102 * Such OST-object may be generated by the LFSCK when repair
2103 * dangling referenced MDT-object, which can be indicated by
2104 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2105 * is true and such OST-object has not been modified yet, we
2106 * will replace it with the orphan OST-object; otherwise the
2107 * LFSCK will create new MDT-object to reference the orphan.
2109 * \retval +1: repaired
2110 * \retval 0: did nothing
2111 * \retval -ve: on error
2113 static int lfsck_layout_conflict_create(const struct lu_env *env,
2114 struct lfsck_component *com,
2115 struct lfsck_tgt_desc *ltd,
2116 struct lu_orphan_rec *rec,
2117 struct dt_object *parent,
2118 struct lu_fid *cfid,
2119 struct lu_buf *ea_buf,
2120 struct lov_ost_data_v1 *slot,
2123 struct lfsck_thread_info *info = lfsck_env_info(env);
2124 struct lu_fid *cfid2 = &info->lti_fid2;
2125 struct ost_id *oi = &info->lti_oi;
2126 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2127 struct dt_device *dev = lfsck_obj2dev(parent);
2128 struct thandle *th = NULL;
2129 struct lustre_handle lh = { 0 };
2130 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2134 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2135 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2139 /* Hold layout lock on the parent to prevent others to access. */
2140 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2141 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2146 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2148 /* If the conflict OST-obejct is not created for fixing dangling
2149 * referenced MDT-object in former LFSCK check/repair, or it has
2150 * been modified by others, then we cannot destroy it. Re-create
2151 * a new MDT-object for the orphan OST-object. */
2152 if (rc == -ETXTBSY) {
2153 /* No need the layout lock on the original parent. */
2154 lfsck_ibits_unlock(&lh, LCK_EX);
2156 fid_zero(&rec->lor_fid);
2157 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2158 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2160 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2161 info->lti_tmpbuf, "C", ea_off);
2166 if (rc != 0 && rc != -ENOENT)
2169 th = dt_trans_create(env, dev);
2171 GOTO(unlock, rc = PTR_ERR(th));
2173 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2174 LU_XATTR_REPLACE, th);
2178 rc = dt_trans_start_local(env, dev, th);
2182 dt_write_lock(env, parent, 0);
2183 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2184 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2185 LU_XATTR_REPLACE, ltd->ltd_index);
2186 dt_write_unlock(env, parent);
2191 dt_trans_stop(env, dev, th);
2194 lfsck_ibits_unlock(&lh, LCK_EX);
2197 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2198 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2199 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2200 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2201 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2204 return rc >= 0 ? 1 : rc;
2208 * \retval +1: repaired
2209 * \retval 0: did nothing
2210 * \retval -ve: on error
2212 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2213 struct lfsck_component *com,
2214 struct lfsck_tgt_desc *ltd,
2215 struct lu_orphan_rec *rec,
2216 struct dt_object *parent,
2217 struct lu_fid *cfid,
2218 __u32 ost_idx, __u32 ea_off)
2220 struct lfsck_thread_info *info = lfsck_env_info(env);
2221 struct lu_buf *buf = &info->lti_big_buf;
2222 struct lu_fid *fid = &info->lti_fid2;
2223 struct ost_id *oi = &info->lti_oi;
2224 struct lfsck_instance *lfsck = com->lc_lfsck;
2225 struct dt_device *dt = lfsck_obj2dev(parent);
2226 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2227 struct thandle *handle = NULL;
2229 struct lov_mds_md_v1 *lmm;
2230 struct lov_ost_data_v1 *objs;
2231 struct lustre_handle lh = { 0 };
2238 bool locked = false;
2241 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2242 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2245 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2246 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2247 "stripe-index %u: rc = %d\n",
2248 lfsck_lfsck2name(lfsck), PFID(cfid),
2249 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off, rc);
2256 dt_write_unlock(env, parent);
2260 if (handle != NULL) {
2261 dt_trans_stop(env, dt, handle);
2266 GOTO(unlock_layout, rc);
2269 if (buf->lb_len < lovea_size) {
2270 lu_buf_realloc(buf, lovea_size);
2271 if (buf->lb_buf == NULL)
2272 GOTO(unlock_layout, rc = -ENOMEM);
2275 if (!(bk->lb_param & LPF_DRYRUN)) {
2276 handle = dt_trans_create(env, dt);
2278 GOTO(unlock_layout, rc = PTR_ERR(handle));
2280 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2285 rc = dt_trans_start_local(env, dt, handle);
2290 dt_write_lock(env, parent, 0);
2292 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
2293 if (rc == -ERANGE) {
2294 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV,
2298 } else if (rc == -ENODATA || rc == 0) {
2299 lovea_size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2300 /* If the declared is not big enough, re-try. */
2301 if (buf->lb_len < lovea_size) {
2305 fl = LU_XATTR_CREATE;
2306 } else if (rc < 0) {
2307 GOTO(unlock_parent, rc);
2308 } else if (unlikely(buf->lb_len == 0)) {
2311 fl = LU_XATTR_REPLACE;
2315 if (fl == LU_XATTR_CREATE) {
2316 if (bk->lb_param & LPF_DRYRUN)
2317 GOTO(unlock_parent, rc = 1);
2319 LASSERT(buf->lb_len >= lovea_size);
2321 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2322 buf, fl, ost_idx, ea_off, false);
2324 GOTO(unlock_parent, rc);
2328 rc1 = lfsck_layout_verify_header(lmm);
2330 /* If the LOV EA crashed, the rebuild it. */
2331 if (rc1 == -EINVAL) {
2332 if (bk->lb_param & LPF_DRYRUN)
2333 GOTO(unlock_parent, rc = 1);
2335 LASSERT(buf->lb_len >= lovea_size);
2337 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2338 buf, fl, ost_idx, ea_off, true);
2340 GOTO(unlock_parent, rc);
2343 /* For other unknown magic/pattern, keep the current LOV EA. */
2345 GOTO(unlock_parent, rc = rc1);
2347 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2348 * been verified in lfsck_layout_verify_header() already. If some
2349 * new magic introduced in the future, then layout LFSCK needs to
2350 * be updated also. */
2351 magic = le32_to_cpu(lmm->lmm_magic);
2352 if (magic == LOV_MAGIC_V1) {
2353 objs = &lmm->lmm_objects[0];
2355 LASSERT(magic == LOV_MAGIC_V3);
2356 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2359 count = le16_to_cpu(lmm->lmm_stripe_count);
2361 GOTO(unlock_parent, rc = -EINVAL);
2364 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2365 if (count <= ea_off) {
2366 if (bk->lb_param & LPF_DRYRUN)
2367 GOTO(unlock_parent, rc = 1);
2369 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2370 /* If the declared is not big enough, re-try. */
2371 if (buf->lb_len < lovea_size) {
2376 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2377 buf, fl, ost_idx, ea_off, false);
2379 GOTO(unlock_parent, rc);
2382 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2384 for (i = 0; i < count; i++, objs++) {
2385 /* The MDT-object was created via lfsck_layout_recover_create()
2386 * by others before, and we fill the dummy layout EA. */
2387 if (lovea_slot_is_dummy(objs)) {
2391 if (bk->lb_param & LPF_DRYRUN)
2392 GOTO(unlock_parent, rc = 1);
2394 lmm->lmm_layout_gen =
2395 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2396 rc = lfsck_layout_refill_lovea(env, handle, parent,
2397 cfid, buf, objs, fl,
2400 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2401 "dummy layout slot for "DFID": parent "DFID
2402 ", OST-index %u, stripe-index %u: rc = %d\n",
2403 lfsck_lfsck2name(lfsck), PFID(cfid),
2404 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2406 GOTO(unlock_parent, rc);
2409 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2410 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2412 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2413 "invalid layout EA at the slot %d, index %u\n",
2414 lfsck_lfsck2name(lfsck),
2415 PFID(lfsck_dto2fid(parent)), i,
2416 le32_to_cpu(objs->l_ost_idx));
2418 GOTO(unlock_parent, rc);
2421 /* It should be rare case, the slot is there, but the LFSCK
2422 * does not handle it during the first-phase cycle scanning. */
2423 if (unlikely(lu_fid_eq(fid, cfid))) {
2425 GOTO(unlock_parent, rc = 0);
2427 /* Rare case that the OST-object index
2428 * does not match the parent MDT-object
2429 * layout EA. We trust the later one. */
2430 if (bk->lb_param & LPF_DRYRUN)
2431 GOTO(unlock_parent, rc = 1);
2433 dt_write_unlock(env, parent);
2435 dt_trans_stop(env, dt, handle);
2436 lfsck_ibits_unlock(&lh, LCK_EX);
2437 rc = lfsck_layout_update_pfid(env, com, parent,
2438 cfid, ltd->ltd_tgt, i);
2440 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2441 "updated OST-object's pfid for "DFID
2442 ": parent "DFID", OST-index %u, "
2443 "stripe-index %u: rc = %d\n",
2444 lfsck_lfsck2name(lfsck), PFID(cfid),
2445 PFID(lfsck_dto2fid(parent)),
2446 ltd->ltd_index, i, rc);
2453 /* The MDT-object exists, but related layout EA slot is occupied
2455 if (bk->lb_param & LPF_DRYRUN)
2456 GOTO(unlock_parent, rc = 1);
2458 dt_write_unlock(env, parent);
2460 dt_trans_stop(env, dt, handle);
2461 lfsck_ibits_unlock(&lh, LCK_EX);
2462 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2463 objs = &lmm->lmm_objects[ea_off];
2465 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2466 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2473 dt_write_unlock(env, parent);
2477 dt_trans_stop(env, dt, handle);
2480 lfsck_ibits_unlock(&lh, LCK_EX);
2485 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2486 struct lfsck_component *com,
2487 struct lfsck_tgt_desc *ltd,
2488 struct lu_orphan_rec *rec,
2489 struct lu_fid *cfid)
2491 struct lfsck_layout *lo = com->lc_file_ram;
2492 struct lu_fid *pfid = &rec->lor_fid;
2493 struct dt_object *parent = NULL;
2494 __u32 ea_off = pfid->f_stripe_idx;
2498 if (!fid_is_sane(cfid))
2499 GOTO(out, rc = -EINVAL);
2501 if (fid_is_zero(pfid)) {
2502 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2508 if (!fid_is_sane(pfid))
2509 GOTO(out, rc = -EINVAL);
2511 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2513 GOTO(out, rc = PTR_ERR(parent));
2515 if (unlikely(dt_object_remote(parent) != 0))
2516 GOTO(put, rc = -EXDEV);
2518 if (dt_object_exists(parent) == 0) {
2519 lfsck_object_put(env, parent);
2520 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2525 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2526 GOTO(put, rc = -EISDIR);
2528 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2529 ltd->ltd_index, ea_off);
2535 lfsck_object_put(env, parent);
2537 /* The layout EA is changed, need to be reloaded next time. */
2538 lu_object_put_nocache(env, &parent->do_lu);
2541 down_write(&com->lc_sem);
2542 com->lc_new_scanned++;
2543 com->lc_new_checked++;
2545 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2547 } else if (rc < 0) {
2548 lo->ll_objs_failed_phase2++;
2550 up_write(&com->lc_sem);
2555 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2556 struct lfsck_component *com,
2557 struct lfsck_tgt_desc *ltd)
2559 struct lfsck_assistant_data *lad = com->lc_data;
2560 struct lfsck_instance *lfsck = com->lc_lfsck;
2561 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2562 struct lfsck_thread_info *info = lfsck_env_info(env);
2563 struct ost_id *oi = &info->lti_oi;
2564 struct lu_fid *fid = &info->lti_fid;
2565 struct dt_object *obj;
2566 const struct dt_it_ops *iops;
2571 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
2572 "scanning for OST%04x\n",
2573 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2575 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
2576 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
2577 "scanning for OST%04x\n",
2578 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2583 ostid_set_seq(oi, FID_SEQ_IDIF);
2584 ostid_set_id(oi, 0);
2585 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
2589 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2590 if (unlikely(IS_ERR(obj)))
2591 GOTO(log, rc = PTR_ERR(obj));
2593 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_orphan_features);
2597 iops = &obj->do_index_ops->dio_it;
2598 di = iops->init(env, obj, 0, BYPASS_CAPA);
2600 GOTO(put, rc = PTR_ERR(di));
2602 rc = iops->load(env, di, 0);
2604 /* -ESRCH means that the orphan OST-objects rbtree has been
2605 * cleanup because of the OSS server restart or other errors. */
2606 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
2611 rc = iops->next(env, di);
2623 struct lu_orphan_rec *rec = &info->lti_rec;
2625 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
2626 unlikely(!thread_is_running(&lfsck->li_thread)))
2629 key = iops->key(env, di);
2630 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2631 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2633 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2634 &com->lc_fid_latest_scanned_phase2);
2635 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2638 lfsck_control_speed_by_self(com);
2640 rc = iops->next(env, di);
2641 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2648 iops->fini(env, di);
2650 lfsck_object_put(env, obj);
2653 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
2654 "scanning for OST%04x: rc = %d\n",
2655 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2657 return rc > 0 ? 0 : rc;
2660 /* For the MDT-object with dangling reference, we need to repare the
2661 * inconsistency according to the LFSCK sponsor's requirement:
2663 * 1) Keep the inconsistency there and report the inconsistency case,
2664 * then give the chance to the application to find related issues,
2665 * and the users can make the decision about how to handle it with
2666 * more human knownledge. (by default)
2668 * 2) Re-create the missing OST-object with the FID/owner information. */
2669 static int lfsck_layout_repair_dangling(const struct lu_env *env,
2670 struct lfsck_component *com,
2671 struct dt_object *parent,
2672 struct lfsck_layout_req *llr,
2673 const struct lu_attr *pla)
2675 struct lfsck_thread_info *info = lfsck_env_info(env);
2676 struct filter_fid *pfid = &info->lti_new_pfid;
2677 struct dt_object_format *dof = &info->lti_dof;
2678 struct lu_attr *cla = &info->lti_la2;
2679 struct dt_object *child = llr->llr_child;
2680 struct dt_device *dev = lfsck_obj2dev(child);
2681 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2682 struct thandle *handle;
2684 struct lustre_handle lh = { 0 };
2689 if (com->lc_lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ)
2697 memset(cla, 0, sizeof(*cla));
2698 cla->la_uid = pla->la_uid;
2699 cla->la_gid = pla->la_gid;
2700 cla->la_mode = S_IFREG | 0666;
2701 cla->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
2702 LA_ATIME | LA_MTIME | LA_CTIME;
2703 memset(dof, 0, sizeof(*dof));
2705 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2706 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2711 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2712 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2713 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2714 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2715 * parent MDT-object's layout EA. */
2716 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2717 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2719 handle = dt_trans_create(env, dev);
2721 GOTO(unlock1, rc = PTR_ERR(handle));
2723 rc = dt_declare_create(env, child, cla, NULL, dof, handle);
2727 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2728 LU_XATTR_CREATE, handle);
2732 rc = dt_trans_start_local(env, dev, handle);
2736 dt_read_lock(env, parent, 0);
2737 if (unlikely(lfsck_is_dead_obj(parent)))
2738 GOTO(unlock2, rc = 1);
2740 rc = dt_create(env, child, cla, NULL, dof, handle);
2744 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2745 handle, BYPASS_CAPA);
2750 dt_read_unlock(env, parent);
2753 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2756 lfsck_ibits_unlock(&lh, LCK_EX);
2759 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found dangling "
2760 "reference for: parent "DFID", child "DFID", OST-index %u, "
2761 "stripe-index %u, owner %u/%u. %s: rc = %d\n",
2762 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
2763 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx,
2764 llr->llr_lov_idx, pla->la_uid, pla->la_gid,
2765 create ? "Create the lost OST-object as required" :
2766 "Keep the MDT-object there by default", rc);
2771 /* If the OST-object does not recognize the MDT-object as its parent, and
2772 * there is no other MDT-object claims as its parent, then just trust the
2773 * given MDT-object as its parent. So update the OST-object filter_fid. */
2774 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2775 struct lfsck_component *com,
2776 struct dt_object *parent,
2777 struct lfsck_layout_req *llr,
2778 const struct lu_attr *pla)
2780 struct lfsck_thread_info *info = lfsck_env_info(env);
2781 struct filter_fid *pfid = &info->lti_new_pfid;
2782 struct lu_attr *tla = &info->lti_la3;
2783 struct dt_object *child = llr->llr_child;
2784 struct dt_device *dev = lfsck_obj2dev(child);
2785 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2786 struct thandle *handle;
2788 struct lustre_handle lh = { 0 };
2792 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2793 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2798 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2799 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2800 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2801 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2802 * parent MDT-object's layout EA. */
2803 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2804 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2806 handle = dt_trans_create(env, dev);
2808 GOTO(unlock1, rc = PTR_ERR(handle));
2810 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2814 tla->la_valid = LA_UID | LA_GID;
2815 tla->la_uid = pla->la_uid;
2816 tla->la_gid = pla->la_gid;
2817 rc = dt_declare_attr_set(env, child, tla, handle);
2821 rc = dt_trans_start_local(env, dev, handle);
2825 dt_write_lock(env, parent, 0);
2826 if (unlikely(lfsck_is_dead_obj(parent)))
2827 GOTO(unlock2, rc = 1);
2829 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, BYPASS_CAPA);
2839 tla->la_valid = LA_UID | LA_GID;
2840 rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
2845 dt_write_unlock(env, parent);
2848 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2851 lfsck_ibits_unlock(&lh, LCK_EX);
2854 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired unmatched "
2855 "MDT-OST pair for: parent "DFID", child "DFID", OST-index %u, "
2856 "stripe-index %u, owner %u/%u: rc = %d\n",
2857 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
2858 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
2859 pla->la_uid, pla->la_gid, rc);
2864 /* If there are more than one MDT-objects claim as the OST-object's parent,
2865 * and the OST-object only recognizes one of them, then we need to generate
2866 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
2867 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
2868 struct lfsck_component *com,
2869 struct dt_object *parent,
2870 struct lfsck_layout_req *llr,
2874 struct lfsck_thread_info *info = lfsck_env_info(env);
2875 struct dt_allocation_hint *hint = &info->lti_hint;
2876 struct dt_object_format *dof = &info->lti_dof;
2877 struct ost_id *oi = &info->lti_oi;
2878 struct lfsck_instance *lfsck = com->lc_lfsck;
2879 struct dt_device *dev = lfsck->li_bottom;
2880 struct lu_device *d =
2881 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
2882 struct lu_object *o;
2883 struct lu_object *n;
2884 struct dt_object *child = NULL;
2885 struct thandle *handle = NULL;
2886 struct lov_mds_md_v1 *lmm;
2887 struct lov_ost_data_v1 *objs;
2888 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2890 struct lustre_handle lh = { 0 };
2891 struct lu_buf ea_buf;
2897 /* We use two separated transactions to repair the inconsistency.
2899 * 1) create the child (OST-object).
2900 * 2) update the parent LOV EA according to the child's FID.
2902 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
2903 * handled as orphan when the layout LFSCK run next time.
2905 * If 1) failed, but 2) succeed, then such OST-object will be re-created
2906 * as dangling referened case when the layout LFSCK run next time. */
2908 /* The 1st transaction. */
2909 o = lu_object_anon(env, d, NULL);
2911 GOTO(log, rc = PTR_ERR(o));
2913 n = lu_object_locate(o->lo_header, d->ld_type);
2914 if (unlikely(n == NULL)) {
2915 lu_object_put_nocache(env, o);
2917 GOTO(log, rc = -EINVAL);
2920 child = container_of(n, struct dt_object, do_lu);
2921 memset(hint, 0, sizeof(*hint));
2922 la->la_valid = LA_UID | LA_GID;
2923 memset(dof, 0, sizeof(*dof));
2925 handle = dt_trans_create(env, dev);
2927 GOTO(log, rc = PTR_ERR(handle));
2929 rc = dt_declare_create(env, child, la, hint, dof, handle);
2933 rc = dt_trans_start_local(env, dev, handle);
2937 rc = dt_create(env, child, la, hint, dof, handle);
2938 dt_trans_stop(env, dev, handle);
2943 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2944 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2949 /* The 2nd transaction. */
2951 /* XXX: Generally, we should use bottom device (OSD) to update parent
2952 * LOV EA. But because the LOD-object still references the wrong
2953 * OSP-object that should be detached after the parent's LOV EA
2954 * refreshed. Unfortunately, there is no suitable API for that.
2955 * So we have to make the LOD to re-load the OSP-object(s) via
2956 * replacing the LOV EA against the LOD-object.
2958 * Once the DNE2 patches have been landed, we can replace the
2959 * LOD device with the OSD device. LU-6230. */
2961 dev = lfsck->li_next;
2962 parent = lfsck_object_locate(dev, parent);
2964 GOTO(log, rc = PTR_ERR(parent));
2966 handle = dt_trans_create(env, dev);
2968 GOTO(log, rc = PTR_ERR(handle));
2970 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2971 LU_XATTR_REPLACE, handle);
2975 rc = dt_trans_start_local(env, dev, handle);
2979 dt_write_lock(env, parent, 0);
2980 if (unlikely(lfsck_is_dead_obj(parent)))
2981 GOTO(unlock, rc = 0);
2983 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
2984 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2985 GOTO(unlock, rc = 0);
2988 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2989 * been verified in lfsck_layout_verify_header() already. If some
2990 * new magic introduced in the future, then layout LFSCK needs to
2991 * be updated also. */
2992 magic = le32_to_cpu(lmm->lmm_magic);
2993 if (magic == LOV_MAGIC_V1) {
2994 objs = &lmm->lmm_objects[llr->llr_lov_idx];
2996 LASSERT(magic == LOV_MAGIC_V3);
2998 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
3001 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3002 index = le32_to_cpu(objs->l_ost_idx);
3003 rc = ostid_to_fid(&tfid, oi, index);
3004 /* Someone changed layout during the LFSCK, no need to repair then. */
3005 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
3006 GOTO(unlock, rc = 0);
3008 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3009 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
3010 ostid_cpu_to_le(oi, &objs->l_ost_oi);
3011 objs->l_ost_gen = cpu_to_le32(0);
3012 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
3013 lfsck_buf_init(&ea_buf, lmm,
3014 lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3016 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV,
3017 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
3019 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3022 dt_write_unlock(env, parent);
3026 dt_trans_stop(env, dev, handle);
3029 lfsck_ibits_unlock(&lh, LCK_EX);
3031 lfsck_object_put(env, child);
3033 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired multiple "
3034 "references for: parent "DFID", OST-index %u, stripe-index %u, "
3035 "owner %u/%u: rc = %d\n", lfsck_lfsck2name(lfsck), PFID(pfid),
3036 llr->llr_ost_idx, llr->llr_lov_idx, la->la_uid, la->la_gid, rc);
3041 /* If the MDT-object and the OST-object have different owner information,
3042 * then trust the MDT-object, because the normal chown/chgrp handle order
3043 * is from MDT to OST, and it is possible that some chown/chgrp operation
3044 * is partly done. */
3045 static int lfsck_layout_repair_owner(const struct lu_env *env,
3046 struct lfsck_component *com,
3047 struct dt_object *parent,
3048 struct lfsck_layout_req *llr,
3049 struct lu_attr *pla)
3051 struct lfsck_thread_info *info = lfsck_env_info(env);
3052 struct lu_attr *tla = &info->lti_la3;
3053 struct dt_object *child = llr->llr_child;
3054 struct dt_device *dev = lfsck_obj2dev(child);
3055 struct thandle *handle;
3059 handle = dt_trans_create(env, dev);
3061 GOTO(log, rc = PTR_ERR(handle));
3063 tla->la_uid = pla->la_uid;
3064 tla->la_gid = pla->la_gid;
3065 tla->la_valid = LA_UID | LA_GID;
3066 rc = dt_declare_attr_set(env, child, tla, handle);
3070 rc = dt_trans_start_local(env, dev, handle);
3074 /* Use the dt_object lock to serialize with destroy and attr_set. */
3075 dt_read_lock(env, parent, 0);
3076 if (unlikely(lfsck_is_dead_obj(parent)))
3077 GOTO(unlock, rc = 1);
3079 /* Get the latest parent's owner. */
3080 rc = dt_attr_get(env, parent, tla, BYPASS_CAPA);
3084 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3085 if (unlikely(tla->la_uid != pla->la_uid ||
3086 tla->la_gid != pla->la_gid))
3087 GOTO(unlock, rc = 1);
3089 tla->la_valid = LA_UID | LA_GID;
3090 rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
3095 dt_read_unlock(env, parent);
3098 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3101 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired inconsistent "
3102 "file owner for: parent "DFID", child "DFID", OST-index %u, "
3103 "stripe-index %u, owner %u/%u: rc = %d\n",
3104 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
3105 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
3106 pla->la_uid, pla->la_gid, rc);
3111 /* Check whether the OST-object correctly back points to the
3112 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3113 static int lfsck_layout_check_parent(const struct lu_env *env,
3114 struct lfsck_component *com,
3115 struct dt_object *parent,
3116 const struct lu_fid *pfid,
3117 const struct lu_fid *cfid,
3118 const struct lu_attr *pla,
3119 const struct lu_attr *cla,
3120 struct lfsck_layout_req *llr,
3121 struct lu_buf *lov_ea, __u32 idx)
3123 struct lfsck_thread_info *info = lfsck_env_info(env);
3124 struct lu_buf *buf = &info->lti_big_buf;
3125 struct dt_object *tobj;
3126 struct lov_mds_md_v1 *lmm;
3127 struct lov_ost_data_v1 *objs;
3128 struct lustre_handle lh = { 0 };
3135 if (fid_is_zero(pfid)) {
3136 /* client never wrote. */
3137 if (cla->la_size == 0 && cla->la_blocks == 0) {
3138 if (unlikely(cla->la_uid != pla->la_uid ||
3139 cla->la_gid != pla->la_gid))
3140 RETURN (LLIT_INCONSISTENT_OWNER);
3145 RETURN(LLIT_UNMATCHED_PAIR);
3148 if (unlikely(!fid_is_sane(pfid)))
3149 RETURN(LLIT_UNMATCHED_PAIR);
3151 if (lu_fid_eq(pfid, lu_object_fid(&parent->do_lu))) {
3152 if (llr->llr_lov_idx == idx)
3155 RETURN(LLIT_UNMATCHED_PAIR);
3158 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3160 RETURN(PTR_ERR(tobj));
3162 if (dt_object_exists(tobj) == 0 ||
3163 lfsck_is_dead_obj(tobj))
3164 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3166 if (!S_ISREG(lfsck_object_type(tobj)))
3167 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3169 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3170 * remote one on another MDT. Then check whether the given OST-object
3171 * is in such layout. If yes, it is multiple referenced, otherwise it
3172 * is unmatched referenced case. */
3173 rc = lfsck_layout_get_lovea(env, tobj, buf);
3174 if (rc == 0 || rc == -ENOENT)
3175 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3181 magic = le32_to_cpu(lmm->lmm_magic);
3182 if (magic == LOV_MAGIC_V1) {
3183 objs = &lmm->lmm_objects[0];
3185 LASSERT(magic == LOV_MAGIC_V3);
3186 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3189 count = le16_to_cpu(lmm->lmm_stripe_count);
3190 for (i = 0; i < count; i++, objs++) {
3191 struct lu_fid *tfid = &info->lti_fid2;
3192 struct ost_id *oi = &info->lti_oi;
3195 if (lovea_slot_is_dummy(objs))
3198 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3199 idx2 = le32_to_cpu(objs->l_ost_idx);
3200 rc = ostid_to_fid(tfid, oi, idx2);
3202 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3203 "invalid layout EA at the slot %d, index %u\n",
3204 lfsck_lfsck2name(com->lc_lfsck),
3205 PFID(pfid), i, idx2);
3207 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3210 if (lu_fid_eq(cfid, tfid)) {
3211 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
3212 MDS_INODELOCK_UPDATE |
3213 MDS_INODELOCK_LAYOUT |
3214 MDS_INODELOCK_XATTR,
3219 dt_read_lock(env, tobj, 0);
3221 /* For local MDT-object, re-check existence
3222 * after taken the lock. */
3223 if (!dt_object_remote(tobj)) {
3224 if (dt_object_exists(tobj) == 0 ||
3225 lfsck_is_dead_obj(tobj)) {
3226 rc = LLIT_UNMATCHED_PAIR;
3229 rc = LLIT_MULTIPLE_REFERENCED;
3235 /* For migration case, the new MDT-object and old
3236 * MDT-object may reference the same OST-object at
3237 * some migration internal time.
3239 * For remote MDT-object, the local MDT may not know
3240 * whether it has been removed or not. Try checking
3241 * for a non-existent xattr to check if this object
3242 * has been been removed or not. */
3243 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
3244 XATTR_NAME_DUMMY, BYPASS_CAPA);
3245 if (unlikely(rc == -ENOENT || rc >= 0)) {
3246 rc = LLIT_UNMATCHED_PAIR;
3247 } else if (rc == -ENODATA) {
3249 rc = LLIT_MULTIPLE_REFERENCED;
3256 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3259 if (lustre_handle_is_used(&lh)) {
3260 dt_read_unlock(env, tobj);
3261 lfsck_ibits_unlock(&lh, LCK_EX);
3265 lfsck_object_put(env, tobj);
3270 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
3271 struct lfsck_component *com,
3272 struct lfsck_assistant_req *lar)
3274 struct lfsck_layout_req *llr =
3275 container_of0(lar, struct lfsck_layout_req, llr_lar);
3276 struct lfsck_layout *lo = com->lc_file_ram;
3277 struct lfsck_thread_info *info = lfsck_env_info(env);
3278 struct filter_fid_old *pea = &info->lti_old_pfid;
3279 struct lu_fid *pfid = &info->lti_fid;
3280 struct lu_buf buf = { NULL };
3281 struct dt_object *parent;
3282 struct dt_object *child = llr->llr_child;
3283 struct lu_attr *pla = &info->lti_la;
3284 struct lu_attr *cla = &info->lti_la2;
3285 struct lfsck_instance *lfsck = com->lc_lfsck;
3286 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3287 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
3292 parent = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
3294 RETURN(PTR_ERR(parent));
3296 if (unlikely(lfsck_is_dead_obj(parent)))
3297 GOTO(put_parent, rc = 0);
3299 rc = dt_attr_get(env, parent, pla, BYPASS_CAPA);
3303 rc = dt_attr_get(env, child, cla, BYPASS_CAPA);
3304 if (rc == -ENOENT) {
3305 if (unlikely(lfsck_is_dead_obj(parent)))
3306 GOTO(put_parent, rc = 0);
3308 type = LLIT_DANGLING;
3315 lfsck_buf_init(&buf, pea, sizeof(struct filter_fid_old));
3316 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID, BYPASS_CAPA);
3317 if (unlikely(rc >= 0 && rc != sizeof(struct filter_fid_old) &&
3318 rc != sizeof(struct filter_fid))) {
3319 type = LLIT_UNMATCHED_PAIR;
3323 if (rc < 0 && rc != -ENODATA)
3326 if (rc == -ENODATA) {
3329 fid_le_to_cpu(pfid, &pea->ff_parent);
3330 /* Currently, the filter_fid::ff_parent::f_ver is not the
3331 * real parent MDT-object's FID::f_ver, instead it is the
3332 * OST-object index in its parent MDT-object's layout EA. */
3333 idx = pfid->f_stripe_idx;
3337 rc = lfsck_layout_check_parent(env, com, parent, pfid,
3338 lu_object_fid(&child->do_lu),
3339 pla, cla, llr, &buf, idx);
3348 if (unlikely(cla->la_uid != pla->la_uid ||
3349 cla->la_gid != pla->la_gid)) {
3350 type = LLIT_INCONSISTENT_OWNER;
3355 if (bk->lb_param & LPF_DRYRUN) {
3356 if (type != LLIT_NONE)
3364 rc = lfsck_layout_repair_dangling(env, com, parent, llr, pla);
3366 case LLIT_UNMATCHED_PAIR:
3367 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
3370 case LLIT_MULTIPLE_REFERENCED:
3371 rc = lfsck_layout_repair_multiple_references(env, com, parent,
3374 case LLIT_INCONSISTENT_OWNER:
3375 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla);
3385 down_write(&com->lc_sem);
3387 struct lfsck_assistant_data *lad = com->lc_data;
3389 if (unlikely(lad->lad_exit)) {
3391 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
3392 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
3393 rc == -EHOSTUNREACH) {
3394 /* If cannot touch the target server,
3395 * mark the LFSCK as INCOMPLETE. */
3396 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
3397 "talk with OST %x: rc = %d\n",
3398 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
3399 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
3400 lo->ll_objs_skipped++;
3403 lfsck_layout_record_failure(env, lfsck, lo);
3405 } else if (rc > 0) {
3406 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
3407 "unknown type = %d\n", type);
3409 lo->ll_objs_repaired[type - 1]++;
3410 if (bk->lb_param & LPF_DRYRUN &&
3411 unlikely(lo->ll_pos_first_inconsistent == 0))
3412 lo->ll_pos_first_inconsistent =
3413 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
3416 up_write(&com->lc_sem);
3419 lfsck_object_put(env, parent);
3424 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
3425 struct lfsck_component *com)
3427 struct lfsck_assistant_data *lad = com->lc_data;
3428 struct lfsck_instance *lfsck = com->lc_lfsck;
3429 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3430 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
3431 struct lfsck_tgt_desc *ltd;
3435 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
3436 lfsck_lfsck2name(lfsck));
3438 spin_lock(<ds->ltd_lock);
3439 while (!list_empty(&lad->lad_ost_phase2_list)) {
3440 ltd = list_entry(lad->lad_ost_phase2_list.next,
3441 struct lfsck_tgt_desc,
3442 ltd_layout_phase_list);
3443 list_del_init(<d->ltd_layout_phase_list);
3444 if (bk->lb_param & LPF_ALL_TGT) {
3445 spin_unlock(<ds->ltd_lock);
3446 rc = lfsck_layout_scan_orphan(env, com, ltd);
3447 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3450 if (unlikely(lad->lad_exit ||
3451 !thread_is_running(&lfsck->li_thread)))
3453 spin_lock(<ds->ltd_lock);
3457 if (list_empty(&lad->lad_ost_phase1_list))
3461 spin_unlock(<ds->ltd_lock);
3463 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
3464 lfsck_lfsck2name(lfsck), rc);
3470 lfsck_layout_slave_async_interpret(const struct lu_env *env,
3471 struct ptlrpc_request *req,
3474 struct lfsck_layout_slave_async_args *llsaa = args;
3475 struct obd_export *exp = llsaa->llsaa_exp;
3476 struct lfsck_component *com = llsaa->llsaa_com;
3477 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
3478 struct lfsck_layout_slave_data *llsd = com->lc_data;
3479 struct lfsck_reply *lr = NULL;
3483 /* It is quite probably caused by target crash,
3484 * to make the LFSCK can go ahead, assume that
3485 * the target finished the LFSCK prcoessing. */
3488 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
3489 if (lr->lr_status != LS_SCANNING_PHASE1 &&
3490 lr->lr_status != LS_SCANNING_PHASE2)
3495 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
3496 "status %d\n", lfsck_lfsck2name(com->lc_lfsck),
3497 llst->llst_index, lr != NULL ? lr->lr_status : rc);
3499 lfsck_layout_llst_del(llsd, llst);
3502 lfsck_layout_llst_put(llst);
3503 lfsck_component_put(env, com);
3504 class_export_put(exp);
3509 static int lfsck_layout_async_query(const struct lu_env *env,
3510 struct lfsck_component *com,
3511 struct obd_export *exp,
3512 struct lfsck_layout_slave_target *llst,
3513 struct lfsck_request *lr,
3514 struct ptlrpc_request_set *set)
3516 struct lfsck_layout_slave_async_args *llsaa;
3517 struct ptlrpc_request *req;
3518 struct lfsck_request *tmp;
3522 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
3526 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
3528 ptlrpc_request_free(req);
3532 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3534 ptlrpc_request_set_replen(req);
3536 llsaa = ptlrpc_req_async_args(req);
3537 llsaa->llsaa_exp = exp;
3538 llsaa->llsaa_com = lfsck_component_get(com);
3539 llsaa->llsaa_llst = llst;
3540 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
3541 ptlrpc_set_add_req(set, req);
3546 static int lfsck_layout_async_notify(const struct lu_env *env,
3547 struct obd_export *exp,
3548 struct lfsck_request *lr,
3549 struct ptlrpc_request_set *set)
3551 struct ptlrpc_request *req;
3552 struct lfsck_request *tmp;
3556 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3560 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3562 ptlrpc_request_free(req);
3566 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3568 ptlrpc_request_set_replen(req);
3569 ptlrpc_set_add_req(set, req);
3575 lfsck_layout_slave_query_master(const struct lu_env *env,
3576 struct lfsck_component *com)
3578 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3579 struct lfsck_instance *lfsck = com->lc_lfsck;
3580 struct lfsck_layout_slave_data *llsd = com->lc_data;
3581 struct lfsck_layout_slave_target *llst;
3582 struct obd_export *exp;
3583 struct ptlrpc_request_set *set;
3588 set = ptlrpc_prep_set();
3590 GOTO(log, rc = -ENOMEM);
3592 memset(lr, 0, sizeof(*lr));
3593 lr->lr_event = LE_QUERY;
3594 lr->lr_active = LFSCK_TYPE_LAYOUT;
3596 llsd->llsd_touch_gen++;
3597 spin_lock(&llsd->llsd_lock);
3598 while (!list_empty(&llsd->llsd_master_list)) {
3599 llst = list_entry(llsd->llsd_master_list.next,
3600 struct lfsck_layout_slave_target,
3602 if (llst->llst_gen == llsd->llsd_touch_gen)
3605 llst->llst_gen = llsd->llsd_touch_gen;
3606 list_move_tail(&llst->llst_list,
3607 &llsd->llsd_master_list);
3608 atomic_inc(&llst->llst_ref);
3609 spin_unlock(&llsd->llsd_lock);
3611 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3614 lfsck_layout_llst_del(llsd, llst);
3615 lfsck_layout_llst_put(llst);
3616 spin_lock(&llsd->llsd_lock);
3620 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
3622 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3623 "query %s for layout: rc = %d\n",
3624 lfsck_lfsck2name(lfsck),
3625 exp->exp_obd->obd_name, rc);
3628 lfsck_layout_llst_put(llst);
3629 class_export_put(exp);
3631 spin_lock(&llsd->llsd_lock);
3633 spin_unlock(&llsd->llsd_lock);
3635 rc = ptlrpc_set_wait(set);
3636 ptlrpc_set_destroy(set);
3638 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
3641 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
3642 lfsck_lfsck2name(com->lc_lfsck), rc);
3648 lfsck_layout_slave_notify_master(const struct lu_env *env,
3649 struct lfsck_component *com,
3650 enum lfsck_events event, int result)
3652 struct lfsck_layout *lo = com->lc_file_ram;
3653 struct lfsck_instance *lfsck = com->lc_lfsck;
3654 struct lfsck_layout_slave_data *llsd = com->lc_data;
3655 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3656 struct lfsck_layout_slave_target *llst;
3657 struct obd_export *exp;
3658 struct ptlrpc_request_set *set;
3662 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
3663 lfsck_lfsck2name(com->lc_lfsck));
3665 set = ptlrpc_prep_set();
3669 memset(lr, 0, sizeof(*lr));
3670 lr->lr_event = event;
3671 lr->lr_flags = LEF_FROM_OST;
3672 lr->lr_status = result;
3673 lr->lr_index = lfsck_dev_idx(lfsck);
3674 lr->lr_active = LFSCK_TYPE_LAYOUT;
3675 lr->lr_flags2 = lo->ll_flags;
3676 llsd->llsd_touch_gen++;
3677 spin_lock(&llsd->llsd_lock);
3678 while (!list_empty(&llsd->llsd_master_list)) {
3679 llst = list_entry(llsd->llsd_master_list.next,
3680 struct lfsck_layout_slave_target,
3682 if (llst->llst_gen == llsd->llsd_touch_gen)
3685 llst->llst_gen = llsd->llsd_touch_gen;
3686 list_move_tail(&llst->llst_list,
3687 &llsd->llsd_master_list);
3688 atomic_inc(&llst->llst_ref);
3689 spin_unlock(&llsd->llsd_lock);
3691 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3694 lfsck_layout_llst_del(llsd, llst);
3695 lfsck_layout_llst_put(llst);
3696 spin_lock(&llsd->llsd_lock);
3700 rc = lfsck_layout_async_notify(env, exp, lr, set);
3702 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3703 "notify %s for layout: rc = %d\n",
3704 lfsck_lfsck2name(lfsck),
3705 exp->exp_obd->obd_name, rc);
3707 lfsck_layout_llst_put(llst);
3708 class_export_put(exp);
3709 spin_lock(&llsd->llsd_lock);
3711 spin_unlock(&llsd->llsd_lock);
3713 ptlrpc_set_wait(set);
3714 ptlrpc_set_destroy(set);
3720 * \ret -ENODATA: unrecognized stripe
3721 * \ret = 0 : recognized stripe
3722 * \ret < 0 : other failures
3724 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
3725 struct lfsck_component *com,
3726 struct lu_fid *cfid,
3727 struct lu_fid *pfid)
3729 struct lfsck_thread_info *info = lfsck_env_info(env);
3730 struct lu_buf *buf = &info->lti_big_buf;
3731 struct ost_id *oi = &info->lti_oi;
3732 struct dt_object *obj;
3733 struct lov_mds_md_v1 *lmm;
3734 struct lov_ost_data_v1 *objs;
3735 __u32 idx = pfid->f_stripe_idx;
3743 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3745 RETURN(PTR_ERR(obj));
3747 dt_read_lock(env, obj, 0);
3748 if (unlikely(dt_object_exists(obj) == 0 ||
3749 lfsck_is_dead_obj(obj)))
3750 GOTO(unlock, rc = -ENOENT);
3752 if (!S_ISREG(lfsck_object_type(obj)))
3753 GOTO(unlock, rc = -ENODATA);
3755 rc = lfsck_layout_get_lovea(env, obj, buf);
3760 GOTO(unlock, rc = -ENODATA);
3763 rc = lfsck_layout_verify_header(lmm);
3767 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
3768 * been verified in lfsck_layout_verify_header() already. If some
3769 * new magic introduced in the future, then layout LFSCK needs to
3770 * be updated also. */
3771 magic = le32_to_cpu(lmm->lmm_magic);
3772 if (magic == LOV_MAGIC_V1) {
3773 objs = &lmm->lmm_objects[0];
3775 LASSERT(magic == LOV_MAGIC_V3);
3776 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3779 fid_to_ostid(cfid, oi);
3780 count = le16_to_cpu(lmm->lmm_stripe_count);
3781 for (i = 0; i < count; i++, objs++) {
3784 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
3785 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
3786 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
3789 GOTO(unlock, rc = -ENODATA);
3792 dt_read_unlock(env, obj);
3793 lfsck_object_put(env, obj);
3799 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
3800 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
3801 * layout EA from MDT to OST. On one hand, the OST no need to understand
3802 * the layout EA structure; on the other hand, it may cause trouble when
3803 * transfer large layout EA from MDT to OST via normal OUT RPC.
3805 * \ret > 0: unrecognized stripe
3806 * \ret = 0: recognized stripe
3807 * \ret < 0: other failures
3809 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
3810 struct lfsck_component *com,
3811 struct lu_fid *cfid,
3812 struct lu_fid *pfid)
3814 struct lfsck_instance *lfsck = com->lc_lfsck;
3815 struct obd_device *obd = lfsck->li_obd;
3816 struct seq_server_site *ss = lfsck_dev_site(lfsck);
3817 struct obd_export *exp = NULL;
3818 struct ptlrpc_request *req = NULL;
3819 struct lfsck_request *lr;
3820 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
3824 if (unlikely(fid_is_idif(pfid)))
3827 fld_range_set_any(range);
3828 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
3830 RETURN(rc == -ENOENT ? 1 : rc);
3832 if (unlikely(!fld_range_is_mdt(range)))
3835 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
3836 if (unlikely(exp == NULL))
3839 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
3840 GOTO(out, rc = -EOPNOTSUPP);
3842 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3844 GOTO(out, rc = -ENOMEM);
3846 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3848 ptlrpc_request_free(req);
3853 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3854 memset(lr, 0, sizeof(*lr));
3855 lr->lr_event = LE_PAIRS_VERIFY;
3856 lr->lr_active = LFSCK_TYPE_LAYOUT;
3857 lr->lr_fid = *cfid; /* OST-object itself FID. */
3858 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
3860 ptlrpc_request_set_replen(req);
3861 rc = ptlrpc_queue_wait(req);
3862 ptlrpc_req_finished(req);
3864 if (rc == -ENOENT || rc == -ENODATA)
3871 class_export_put(exp);
3876 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
3877 struct lfsck_component *com,
3878 struct lfsck_request *lr)
3880 struct dt_object *obj;
3884 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lr->lr_fid);
3886 GOTO(log, rc = PTR_ERR(obj));
3888 dt_write_lock(env, obj, 0);
3889 if (unlikely(dt_object_exists(obj) == 0 ||
3890 lfsck_is_dead_obj(obj)))
3891 GOTO(unlock, rc = 0);
3893 rc = __lfsck_layout_update_pfid(env, obj, &lr->lr_fid2,
3899 dt_write_unlock(env, obj);
3900 lfsck_object_put(env, obj);
3903 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
3904 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
3905 PFID(&lr->lr_fid), PFID(&lr->lr_fid2), rc);
3912 static void lfsck_layout_slave_quit(const struct lu_env *env,
3913 struct lfsck_component *com);
3915 static int lfsck_layout_reset(const struct lu_env *env,
3916 struct lfsck_component *com, bool init)
3918 struct lfsck_layout *lo = com->lc_file_ram;
3921 down_write(&com->lc_sem);
3923 memset(lo, 0, com->lc_file_size);
3925 __u32 count = lo->ll_success_count;
3926 __u64 last_time = lo->ll_time_last_complete;
3928 memset(lo, 0, com->lc_file_size);
3929 lo->ll_success_count = count;
3930 lo->ll_time_last_complete = last_time;
3933 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
3934 lo->ll_status = LS_INIT;
3936 if (com->lc_lfsck->li_master) {
3937 struct lfsck_assistant_data *lad = com->lc_data;
3939 lad->lad_incomplete = 0;
3940 CFS_RESET_BITMAP(lad->lad_bitmap);
3943 rc = lfsck_layout_store(env, com);
3944 up_write(&com->lc_sem);
3946 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
3947 lfsck_lfsck2name(com->lc_lfsck), rc);
3952 static void lfsck_layout_fail(const struct lu_env *env,
3953 struct lfsck_component *com, bool new_checked)
3955 struct lfsck_layout *lo = com->lc_file_ram;
3957 down_write(&com->lc_sem);
3959 com->lc_new_checked++;
3960 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
3961 up_write(&com->lc_sem);
3964 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
3965 struct lfsck_component *com, bool init)
3967 struct lfsck_instance *lfsck = com->lc_lfsck;
3968 struct lfsck_layout *lo = com->lc_file_ram;
3972 rc = lfsck_checkpoint_generic(env, com);
3974 return rc > 0 ? 0 : rc;
3977 down_write(&com->lc_sem);
3979 lo->ll_pos_latest_start =
3980 lfsck->li_pos_checkpoint.lp_oit_cookie;
3982 lo->ll_pos_last_checkpoint =
3983 lfsck->li_pos_checkpoint.lp_oit_cookie;
3984 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3985 HALF_SEC - lfsck->li_time_last_checkpoint);
3986 lo->ll_time_last_checkpoint = cfs_time_current_sec();
3987 lo->ll_objs_checked_phase1 += com->lc_new_checked;
3988 com->lc_new_checked = 0;
3991 rc = lfsck_layout_store(env, com);
3992 up_write(&com->lc_sem);
3994 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
3995 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3996 lfsck->li_pos_current.lp_oit_cookie, rc);
4001 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
4002 struct lfsck_component *com, bool init)
4004 struct lfsck_instance *lfsck = com->lc_lfsck;
4005 struct lfsck_layout *lo = com->lc_file_ram;
4008 if (com->lc_new_checked == 0 && !init)
4011 down_write(&com->lc_sem);
4013 lo->ll_pos_latest_start =
4014 lfsck->li_pos_checkpoint.lp_oit_cookie;
4016 lo->ll_pos_last_checkpoint =
4017 lfsck->li_pos_checkpoint.lp_oit_cookie;
4018 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4019 HALF_SEC - lfsck->li_time_last_checkpoint);
4020 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4021 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4022 com->lc_new_checked = 0;
4025 rc = lfsck_layout_store(env, com);
4026 up_write(&com->lc_sem);
4028 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
4029 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
4030 lfsck->li_pos_current.lp_oit_cookie, rc);
4035 static int lfsck_layout_prep(const struct lu_env *env,
4036 struct lfsck_component *com,
4037 struct lfsck_start *start)
4039 struct lfsck_instance *lfsck = com->lc_lfsck;
4040 struct lfsck_layout *lo = com->lc_file_ram;
4041 struct lfsck_position *pos = &com->lc_pos_start;
4043 fid_zero(&pos->lp_dir_parent);
4044 pos->lp_dir_cookie = 0;
4045 if (lo->ll_status == LS_COMPLETED ||
4046 lo->ll_status == LS_PARTIAL ||
4047 /* To handle orphan, must scan from the beginning. */
4048 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
4051 rc = lfsck_layout_reset(env, com, false);
4053 rc = lfsck_set_param(env, lfsck, start, true);
4056 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
4057 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4063 down_write(&com->lc_sem);
4064 lo->ll_time_latest_start = cfs_time_current_sec();
4065 spin_lock(&lfsck->li_lock);
4066 if (lo->ll_flags & LF_SCANNED_ONCE) {
4067 if (!lfsck->li_drop_dryrun ||
4068 lo->ll_pos_first_inconsistent == 0) {
4069 lo->ll_status = LS_SCANNING_PHASE2;
4070 list_move_tail(&com->lc_link,
4071 &lfsck->li_list_double_scan);
4072 pos->lp_oit_cookie = 0;
4076 lo->ll_status = LS_SCANNING_PHASE1;
4077 lo->ll_run_time_phase1 = 0;
4078 lo->ll_run_time_phase2 = 0;
4079 lo->ll_objs_checked_phase1 = 0;
4080 lo->ll_objs_checked_phase2 = 0;
4081 lo->ll_objs_failed_phase1 = 0;
4082 lo->ll_objs_failed_phase2 = 0;
4083 for (i = 0; i < LLIT_MAX; i++)
4084 lo->ll_objs_repaired[i] = 0;
4086 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4087 fid_zero(&com->lc_fid_latest_scanned_phase2);
4090 lo->ll_status = LS_SCANNING_PHASE1;
4091 if (!lfsck->li_drop_dryrun ||
4092 lo->ll_pos_first_inconsistent == 0)
4093 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
4095 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4097 spin_unlock(&lfsck->li_lock);
4098 up_write(&com->lc_sem);
4103 static int lfsck_layout_slave_prep(const struct lu_env *env,
4104 struct lfsck_component *com,
4105 struct lfsck_start_param *lsp)
4107 struct lfsck_layout_slave_data *llsd = com->lc_data;
4108 struct lfsck_instance *lfsck = com->lc_lfsck;
4109 struct lfsck_layout *lo = com->lc_file_ram;
4110 struct lfsck_start *start = lsp->lsp_start;
4113 rc = lfsck_layout_prep(env, com, start);
4117 if (lo->ll_flags & LF_CRASHED_LASTID &&
4118 list_empty(&llsd->llsd_master_list)) {
4119 LASSERT(lfsck->li_out_notify != NULL);
4121 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4122 LE_LASTID_REBUILDING);
4125 if (!lsp->lsp_index_valid)
4128 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
4129 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
4130 LASSERT(!llsd->llsd_rbtree_valid);
4132 write_lock(&llsd->llsd_rb_lock);
4133 rc = lfsck_rbtree_setup(env, com);
4134 write_unlock(&llsd->llsd_rb_lock);
4137 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
4138 LPU64"]\n", lfsck_lfsck2name(lfsck),
4139 com->lc_pos_start.lp_oit_cookie);
4144 static int lfsck_layout_master_prep(const struct lu_env *env,
4145 struct lfsck_component *com,
4146 struct lfsck_start_param *lsp)
4151 rc = lfsck_layout_load_bitmap(env, com);
4153 rc = lfsck_layout_reset(env, com, false);
4155 rc = lfsck_set_param(env, com->lc_lfsck,
4156 lsp->lsp_start, true);
4162 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
4166 rc = lfsck_start_assistant(env, com, lsp);
4171 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
4172 LPU64"]\n", lfsck_lfsck2name(com->lc_lfsck),
4173 com->lc_pos_start.lp_oit_cookie);
4178 /* Pre-fetch the attribute for each stripe in the given layout EA. */
4179 static int lfsck_layout_scan_stripes(const struct lu_env *env,
4180 struct lfsck_component *com,
4181 struct dt_object *parent,
4182 struct lov_mds_md_v1 *lmm)
4184 struct lfsck_thread_info *info = lfsck_env_info(env);
4185 struct lfsck_instance *lfsck = com->lc_lfsck;
4186 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4187 struct lfsck_layout *lo = com->lc_file_ram;
4188 struct lfsck_assistant_data *lad = com->lc_data;
4189 struct lfsck_layout_object *llo = NULL;
4190 struct lov_ost_data_v1 *objs;
4191 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4192 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4193 struct ptlrpc_thread *athread = &lad->lad_thread;
4194 struct l_wait_info lwi = { 0 };
4202 lfsck_buf_init(&buf, &info->lti_old_pfid,
4203 sizeof(struct filter_fid_old));
4204 count = le16_to_cpu(lmm->lmm_stripe_count);
4205 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
4206 * been verified in lfsck_layout_verify_header() already. If some
4207 * new magic introduced in the future, then layout LFSCK needs to
4208 * be updated also. */
4209 magic = le32_to_cpu(lmm->lmm_magic);
4210 if (magic == LOV_MAGIC_V1) {
4211 objs = &lmm->lmm_objects[0];
4213 LASSERT(magic == LOV_MAGIC_V3);
4214 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4217 for (i = 0; i < count; i++, objs++) {
4218 struct lu_fid *fid = &info->lti_fid;
4219 struct ost_id *oi = &info->lti_oi;
4220 struct lfsck_layout_req *llr;
4221 struct lfsck_tgt_desc *tgt = NULL;
4222 struct dt_object *cobj = NULL;
4224 bool wakeup = false;
4226 if (unlikely(lovea_slot_is_dummy(objs)))
4229 l_wait_event(mthread->t_ctl_waitq,
4230 lad->lad_prefetched < bk->lb_async_windows ||
4231 !thread_is_running(mthread) ||
4232 thread_is_stopped(athread),
4235 if (unlikely(!thread_is_running(mthread)) ||
4236 thread_is_stopped(athread))
4239 if (unlikely(lfsck_is_dead_obj(parent)))
4242 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4243 index = le32_to_cpu(objs->l_ost_idx);
4244 rc = ostid_to_fid(fid, oi, index);
4246 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
4247 ": "DOSTID", idx:%u\n", lfsck_lfsck2name(lfsck),
4248 PFID(lfsck_dto2fid(parent)), POSTID(oi), index);
4252 tgt = lfsck_tgt_get(ltds, index);
4253 if (unlikely(tgt == NULL)) {
4254 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
4255 "did not join the layout LFSCK\n",
4256 lfsck_lfsck2name(lfsck), index);
4257 lfsck_lad_set_bitmap(env, com, index);
4261 /* There is potential deadlock race condition between object
4262 * destroy and layout LFSCK. Consider the following scenario:
4264 * 1) The LFSCK thread obtained the parent object firstly, at
4265 * that time, the parent object has not been destroyed yet.
4267 * 2) One RPC service thread destroyed the parent and all its
4268 * children objects. Because the LFSCK is referencing the
4269 * parent object, then the parent object will be marked as
4270 * dying in RAM. On the other hand, the parent object is
4271 * referencing all its children objects, then all children
4272 * objects will be marked as dying in RAM also.
4274 * 3) The LFSCK thread tries to find some child object with
4275 * the parent object referenced. Then it will find that the
4276 * child object is dying. According to the object visibility
4277 * rules: the object with dying flag cannot be returned to
4278 * others. So the LFSCK thread has to wait until the dying
4279 * object has been purged from RAM, then it can allocate a
4280 * new object (with the same FID) in RAM. Unfortunately, the
4281 * LFSCK thread itself is referencing the parent object, and
4282 * cause the parent object cannot be purged, then cause the
4283 * child object cannot be purged also. So the LFSCK thread
4284 * will fall into deadlock.
4286 * We introduce non-blocked version lu_object_find() to allow
4287 * the LFSCK thread to return failure immediately (instead of
4288 * wait) when it finds dying (child) object, then the LFSCK
4289 * thread can check whether the parent object is dying or not.
4290 * So avoid above deadlock. LU-5395 */
4291 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
4293 if (lfsck_is_dead_obj(parent)) {
4303 rc = dt_declare_attr_get(env, cobj, BYPASS_CAPA);
4307 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, BYPASS_CAPA);
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,
4786 "time_since_last_completed");
4790 rc = lfsck_time_dump(m, lo->ll_time_latest_start,
4791 "time_since_latest_start");
4795 rc = lfsck_time_dump(m, lo->ll_time_last_checkpoint,
4796 "time_since_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,
5733 struct lustre_capa *capa)
5738 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
5739 struct dt_object *dt,
5740 const struct dt_rec *rec,
5741 const struct dt_key *key,
5742 struct thandle *handle)
5747 static int lfsck_orphan_index_insert(const struct lu_env *env,
5748 struct dt_object *dt,
5749 const struct dt_rec *rec,
5750 const struct dt_key *key,
5751 struct thandle *handle,
5752 struct lustre_capa *capa,
5758 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
5759 struct dt_object *dt,
5760 const struct dt_key *key,
5761 struct thandle *handle)
5766 static int lfsck_orphan_index_delete(const struct lu_env *env,
5767 struct dt_object *dt,
5768 const struct dt_key *key,
5769 struct thandle *handle,
5770 struct lustre_capa *capa)
5775 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
5776 struct dt_object *dt,
5778 struct lustre_capa *capa)
5780 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
5781 struct lfsck_instance *lfsck;
5782 struct lfsck_component *com = NULL;
5783 struct lfsck_layout_slave_data *llsd;
5784 struct lfsck_orphan_it *it = NULL;
5785 struct lfsck_layout *lo;
5789 lfsck = lfsck_instance_find(dev, true, false);
5790 if (unlikely(lfsck == NULL))
5791 RETURN(ERR_PTR(-ENXIO));
5793 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
5794 if (unlikely(com == NULL))
5795 GOTO(out, rc = -ENOENT);
5797 lo = com->lc_file_ram;
5798 if (lo->ll_flags & LF_INCOMPLETE)
5799 GOTO(out, rc = -ESRCH);
5801 llsd = com->lc_data;
5802 if (!llsd->llsd_rbtree_valid)
5803 GOTO(out, rc = -ESRCH);
5807 GOTO(out, rc = -ENOMEM);
5809 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
5810 if (it->loi_llst == NULL)
5811 GOTO(out, rc = -ENXIO);
5813 if (dev->dd_record_fid_accessed) {
5814 /* The first iteration against the rbtree, scan the whole rbtree
5815 * to remove the nodes which do NOT need to be handled. */
5816 write_lock(&llsd->llsd_rb_lock);
5817 if (dev->dd_record_fid_accessed) {
5818 struct rb_node *node;
5819 struct rb_node *next;
5820 struct lfsck_rbtree_node *lrn;
5822 /* No need to record the fid accessing anymore. */
5823 dev->dd_record_fid_accessed = 0;
5825 node = rb_first(&llsd->llsd_rb_root);
5826 while (node != NULL) {
5827 next = rb_next(node);
5828 lrn = rb_entry(node, struct lfsck_rbtree_node,
5830 if (atomic_read(&lrn->lrn_known_count) <=
5831 atomic_read(&lrn->lrn_accessed_count)) {
5832 rb_erase(node, &llsd->llsd_rb_root);
5833 lfsck_rbtree_free(lrn);
5838 write_unlock(&llsd->llsd_rb_lock);
5841 /* read lock the rbtree when init, and unlock when fini */
5842 read_lock(&llsd->llsd_rb_lock);
5850 lfsck_component_put(env, com);
5852 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
5853 lfsck_lfsck2name(lfsck), rc);
5855 lfsck_instance_put(env, lfsck);
5860 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
5863 return (struct dt_it *)it;
5866 static void lfsck_orphan_it_fini(const struct lu_env *env,
5869 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5870 struct lfsck_component *com = it->loi_com;
5871 struct lfsck_layout_slave_data *llsd;
5872 struct lfsck_layout_slave_target *llst;
5875 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
5876 lfsck_lfsck2name(com->lc_lfsck));
5878 llsd = com->lc_data;
5879 read_unlock(&llsd->llsd_rb_lock);
5880 llst = it->loi_llst;
5881 LASSERT(llst != NULL);
5883 /* Save the key and hash for iterate next. */
5884 llst->llst_fid = it->loi_key;
5885 llst->llst_hash = it->loi_hash;
5886 lfsck_layout_llst_put(llst);
5887 lfsck_component_put(env, com);
5893 * \retval +1: the iteration finished
5894 * \retval 0: on success, not finished
5895 * \retval -ve: on error
5897 static int lfsck_orphan_it_next(const struct lu_env *env,
5900 struct lfsck_thread_info *info = lfsck_env_info(env);
5901 struct filter_fid_old *pfid = &info->lti_old_pfid;
5902 struct lu_attr *la = &info->lti_la;
5903 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5904 struct lu_fid *key = &it->loi_key;
5905 struct lu_orphan_rec *rec = &it->loi_rec;
5906 struct lfsck_component *com = it->loi_com;
5907 struct lfsck_instance *lfsck = com->lc_lfsck;
5908 struct lfsck_layout_slave_data *llsd = com->lc_data;
5909 struct dt_object *obj;
5910 struct lfsck_rbtree_node *lrn;
5914 __u32 idx = it->loi_llst->llst_index;
5924 lrn = lfsck_rbtree_search(llsd, key, &exact);
5932 key->f_seq = lrn->lrn_seq;
5933 key->f_oid = lrn->lrn_first_oid;
5938 if (unlikely(key->f_oid == 0)) {
5945 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
5951 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
5952 atomic_read(&lrn->lrn_accessed_count))) {
5953 struct rb_node *next = rb_next(&lrn->lrn_node);
5955 while (next != NULL) {
5956 lrn = rb_entry(next, struct lfsck_rbtree_node,
5958 if (atomic_read(&lrn->lrn_known_count) >
5959 atomic_read(&lrn->lrn_accessed_count))
5961 next = rb_next(next);
5970 key->f_seq = lrn->lrn_seq;
5971 key->f_oid = lrn->lrn_first_oid;
5975 pos = key->f_oid - lrn->lrn_first_oid;
5978 pos = find_next_bit(lrn->lrn_known_bitmap,
5979 LFSCK_RBTREE_BITMAP_WIDTH, pos);
5980 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
5981 key->f_oid = lrn->lrn_first_oid + pos;
5982 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
5990 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
5995 key->f_oid = lrn->lrn_first_oid + pos;
5996 obj = lfsck_object_find_bottom(env, lfsck, key);
5999 if (rc == -ENOENT) {
6006 dt_read_lock(env, obj, 0);
6007 if (dt_object_exists(obj) == 0 ||
6008 lfsck_is_dead_obj(obj)) {
6009 dt_read_unlock(env, obj);
6010 lfsck_object_put(env, obj);
6015 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
6019 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, pfid, sizeof(*pfid)),
6020 XATTR_NAME_FID, BYPASS_CAPA);
6021 if (rc == -ENODATA) {
6022 /* For the pre-created OST-object, update the bitmap to avoid
6023 * others LFSCK (second phase) iteration to touch it again. */
6024 if (la->la_ctime == 0) {
6025 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
6026 atomic_inc(&lrn->lrn_accessed_count);
6028 /* For the race between repairing dangling referenced
6029 * MDT-object and unlink the file, it may left orphan
6030 * OST-object there. Destroy it now! */
6031 if (unlikely(!(la->la_mode & S_ISUID))) {
6032 dt_read_unlock(env, obj);
6033 lfsck_layout_destroy_orphan(env, obj);
6034 lfsck_object_put(env, obj);
6038 } else if (idx == 0) {
6039 /* If the orphan OST-object has no parent information,
6040 * regard it as referenced by the MDT-object on MDT0. */
6041 fid_zero(&rec->lor_fid);
6042 rec->lor_uid = la->la_uid;
6043 rec->lor_gid = la->la_gid;
6047 dt_read_unlock(env, obj);
6048 lfsck_object_put(env, obj);
6056 if (rc != sizeof(struct filter_fid) &&
6057 rc != sizeof(struct filter_fid_old))
6058 GOTO(out, rc = -EINVAL);
6060 fid_le_to_cpu(&rec->lor_fid, &pfid->ff_parent);
6061 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
6062 * MDT-object's FID::f_ver, instead it is the OST-object index in its
6063 * parent MDT-object's layout EA. */
6064 save = rec->lor_fid.f_stripe_idx;
6065 rec->lor_fid.f_ver = 0;
6066 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_fid, idx);
6067 /* If the orphan OST-object does not claim the MDT, then next.
6069 * If we do not know whether it matches or not, then return it
6070 * to the MDT for further check. */
6072 dt_read_unlock(env, obj);
6073 lfsck_object_put(env, obj);
6078 rec->lor_fid.f_stripe_idx = save;
6079 rec->lor_uid = la->la_uid;
6080 rec->lor_gid = la->la_gid;
6082 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u\n",
6083 lfsck_lfsck2name(com->lc_lfsck), PFID(key), PFID(&rec->lor_fid),
6084 rec->lor_uid, rec->lor_gid);
6089 dt_read_unlock(env, obj);
6090 lfsck_object_put(env, obj);
6098 * \retval +1: locate to the exactly position
6099 * \retval 0: cannot locate to the exactly position,
6100 * call next() to move to a valid position.
6101 * \retval -ve: on error
6103 static int lfsck_orphan_it_get(const struct lu_env *env,
6105 const struct dt_key *key)
6107 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6110 it->loi_key = *(struct lu_fid *)key;
6111 rc = lfsck_orphan_it_next(env, di);
6121 static void lfsck_orphan_it_put(const struct lu_env *env,
6126 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
6127 const struct dt_it *di)
6129 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6131 return (struct dt_key *)&it->loi_key;
6134 static int lfsck_orphan_it_key_size(const struct lu_env *env,
6135 const struct dt_it *di)
6137 return sizeof(struct lu_fid);
6140 static int lfsck_orphan_it_rec(const struct lu_env *env,
6141 const struct dt_it *di,
6145 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6147 *(struct lu_orphan_rec *)rec = it->loi_rec;
6152 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
6153 const struct dt_it *di)
6155 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6157 return it->loi_hash;
6161 * \retval +1: locate to the exactly position
6162 * \retval 0: cannot locate to the exactly position,
6163 * call next() to move to a valid position.
6164 * \retval -ve: on error
6166 static int lfsck_orphan_it_load(const struct lu_env *env,
6167 const struct dt_it *di,
6170 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6171 struct lfsck_layout_slave_target *llst = it->loi_llst;
6174 LASSERT(llst != NULL);
6176 if (hash != llst->llst_hash) {
6177 CDEBUG(D_LFSCK, "%s: the given hash "LPU64" for orphan "
6178 "iteration does not match the one when fini "
6179 LPU64", to be reset.\n",
6180 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
6182 fid_zero(&llst->llst_fid);
6183 llst->llst_hash = 0;
6186 it->loi_key = llst->llst_fid;
6187 it->loi_hash = llst->llst_hash;
6188 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
6198 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
6199 const struct dt_it *di,
6205 const struct dt_index_operations lfsck_orphan_index_ops = {
6206 .dio_lookup = lfsck_orphan_index_lookup,
6207 .dio_declare_insert = lfsck_orphan_index_declare_insert,
6208 .dio_insert = lfsck_orphan_index_insert,
6209 .dio_declare_delete = lfsck_orphan_index_declare_delete,
6210 .dio_delete = lfsck_orphan_index_delete,
6212 .init = lfsck_orphan_it_init,
6213 .fini = lfsck_orphan_it_fini,
6214 .get = lfsck_orphan_it_get,
6215 .put = lfsck_orphan_it_put,
6216 .next = lfsck_orphan_it_next,
6217 .key = lfsck_orphan_it_key,
6218 .key_size = lfsck_orphan_it_key_size,
6219 .rec = lfsck_orphan_it_rec,
6220 .store = lfsck_orphan_it_store,
6221 .load = lfsck_orphan_it_load,
6222 .key_rec = lfsck_orphan_it_key_rec,