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) 2013, 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 static const char lfsck_layout_name[] = "lfsck_layout";
58 struct lfsck_layout_seq {
59 struct list_head lls_list;
62 __u64 lls_lastid_known;
63 struct dt_object *lls_lastid_obj;
64 unsigned int lls_dirty:1;
67 struct lfsck_layout_slave_target {
68 /* link into lfsck_layout_slave_data::llsd_master_list. */
69 struct list_head llst_list;
70 /* The position for next record in the rbtree for iteration. */
71 struct lu_fid llst_fid;
72 /* Dummy hash for iteration against the rbtree. */
79 struct lfsck_layout_slave_data {
80 /* list for lfsck_layout_seq */
81 struct list_head llsd_seq_list;
83 /* list for the masters involve layout verification. */
84 struct list_head llsd_master_list;
87 struct dt_object *llsd_rb_obj;
88 struct rb_root llsd_rb_root;
89 rwlock_t llsd_rb_lock;
90 unsigned int llsd_rbtree_valid:1;
93 struct lfsck_layout_object {
94 struct dt_object *llo_obj;
95 struct lu_attr llo_attr;
101 struct lfsck_layout_req {
102 struct lfsck_assistant_req llr_lar;
103 struct lfsck_layout_object *llr_parent;
104 struct dt_object *llr_child;
106 __u32 llr_lov_idx; /* offset in LOV EA */
109 struct lfsck_layout_slave_async_args {
110 struct obd_export *llsaa_exp;
111 struct lfsck_component *llsaa_com;
112 struct lfsck_layout_slave_target *llsaa_llst;
115 static struct lfsck_layout_object *
116 lfsck_layout_object_init(const struct lu_env *env, struct dt_object *obj,
117 __u64 cookie, __u16 gen)
119 struct lfsck_layout_object *llo;
124 return ERR_PTR(-ENOMEM);
126 rc = dt_attr_get(env, obj, &llo->llo_attr, BYPASS_CAPA);
133 lu_object_get(&obj->do_lu);
135 llo->llo_cookie = cookie;
136 /* The gen can be used to check whether some others have changed the
137 * file layout after LFSCK pre-fetching but before real verification. */
139 atomic_set(&llo->llo_ref, 1);
145 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
147 if (atomic_dec_and_test(&llst->llst_ref)) {
148 LASSERT(list_empty(&llst->llst_list));
155 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
157 struct lfsck_layout_slave_target *llst;
158 struct lfsck_layout_slave_target *tmp;
165 INIT_LIST_HEAD(&llst->llst_list);
167 llst->llst_index = index;
168 atomic_set(&llst->llst_ref, 1);
170 spin_lock(&llsd->llsd_lock);
171 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
172 if (tmp->llst_index == index) {
178 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
179 spin_unlock(&llsd->llsd_lock);
188 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
189 struct lfsck_layout_slave_target *llst)
193 spin_lock(&llsd->llsd_lock);
194 if (!list_empty(&llst->llst_list)) {
195 list_del_init(&llst->llst_list);
198 spin_unlock(&llsd->llsd_lock);
201 lfsck_layout_llst_put(llst);
204 static inline struct lfsck_layout_slave_target *
205 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
206 __u32 index, bool unlink)
208 struct lfsck_layout_slave_target *llst;
210 spin_lock(&llsd->llsd_lock);
211 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
212 if (llst->llst_index == index) {
214 list_del_init(&llst->llst_list);
216 atomic_inc(&llst->llst_ref);
217 spin_unlock(&llsd->llsd_lock);
222 spin_unlock(&llsd->llsd_lock);
227 static inline void lfsck_layout_object_put(const struct lu_env *env,
228 struct lfsck_layout_object *llo)
230 if (atomic_dec_and_test(&llo->llo_ref)) {
231 lfsck_object_put(env, llo->llo_obj);
236 static struct lfsck_layout_req *
237 lfsck_layout_assistant_req_init(struct lfsck_layout_object *parent,
238 struct dt_object *child, __u32 ost_idx,
241 struct lfsck_layout_req *llr;
245 return ERR_PTR(-ENOMEM);
247 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
248 atomic_inc(&parent->llo_ref);
249 llr->llr_parent = parent;
250 llr->llr_child = child;
251 llr->llr_ost_idx = ost_idx;
252 llr->llr_lov_idx = lov_idx;
257 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
258 struct lfsck_assistant_req *lar)
260 struct lfsck_layout_req *llr =
261 container_of0(lar, struct lfsck_layout_req, llr_lar);
263 lu_object_put(env, &llr->llr_child->do_lu);
264 lfsck_layout_object_put(env, llr->llr_parent);
269 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
270 struct ptlrpc_request *req,
273 struct lfsck_async_interpret_args *laia = args;
276 atomic_dec(laia->laia_count);
282 * Notify remote LFSCK instances about former failures.
284 * The local LFSCK instance has recorded which OSTs have ever failed to respond
285 * some LFSCK verification requests (maybe because of network issues or the OST
286 * itself trouble). During the respond gap, the OST may missed some OST-objects
287 * verification, then the OST cannot know whether related OST-objects have been
288 * referenced by related MDT-objects or not, then in the second-stage scanning,
289 * these OST-objects will be regarded as orphan, if the OST-object contains bad
290 * parent FID for back reference, then it will misguide the LFSCK to make wrong
291 * fixing for the fake orphan.
293 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
294 * it will scan the bitmap for the ever failed OTs, and notify them that it has
295 * ever missed some OST-object verification and should skip orphan handling for
296 * all MDTs that are in layout LFSCK.
298 * \param[in] env pointer to the thread context
299 * \param[in] com pointer to the lfsck component
300 * \param[in] lr pointer to the lfsck request
302 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
303 struct lfsck_component *com,
304 struct lfsck_request *lr)
306 struct lfsck_async_interpret_args *laia =
307 &lfsck_env_info(env)->lti_laia2;
308 struct lfsck_assistant_data *lad = com->lc_data;
309 struct lfsck_layout *lo = com->lc_file_ram;
310 struct lfsck_instance *lfsck = com->lc_lfsck;
311 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
312 struct lfsck_tgt_desc *ltd;
313 struct ptlrpc_request_set *set;
319 if (!lad->lad_incomplete || lo->ll_flags & LF_INCOMPLETE)
322 /* If the MDT has ever failed to verfiy some OST-objects,
323 * then sync failures with them firstly. */
324 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
326 atomic_set(&count, 0);
327 memset(laia, 0, sizeof(*laia));
328 laia->laia_count = &count;
329 set = ptlrpc_prep_set();
331 GOTO(out, rc = -ENOMEM);
333 down_read(<ds->ltd_rw_sem);
334 cfs_foreach_bit(lad->lad_bitmap, idx) {
335 ltd = LTD_TGT(ltds, idx);
336 LASSERT(ltd != NULL);
338 spin_lock(<ds->ltd_lock);
339 list_del_init(<d->ltd_layout_phase_list);
340 list_del_init(<d->ltd_layout_list);
341 spin_unlock(<ds->ltd_lock);
343 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
344 lfsck_layout_assistant_sync_failures_interpret,
347 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
348 "notify target %x for %s phase1 done: "
349 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
350 ltd->ltd_index, lad->lad_name, rc);
357 up_read(<ds->ltd_rw_sem);
359 if (rc == 0 && atomic_read(&count) > 0)
360 rc = ptlrpc_set_wait(set);
362 ptlrpc_set_destroy(set);
364 if (rc == 0 && atomic_read(&count) > 0)
371 /* If failed to sync failures with the OSTs, then have to
372 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
373 * subsequent orphan OST-object handling. */
374 lo->ll_flags |= LF_INCOMPLETE;
376 lr->lr_flags2 = lo->ll_flags;
379 static int lfsck_layout_get_lovea(const struct lu_env *env,
380 struct dt_object *obj, struct lu_buf *buf)
385 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV, BYPASS_CAPA);
387 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
392 lu_buf_realloc(buf, rc);
393 if (buf->lb_buf == NULL)
405 if (unlikely(buf->lb_buf == NULL)) {
406 lu_buf_alloc(buf, rc);
407 if (buf->lb_buf == NULL)
416 static int lfsck_layout_verify_header(struct lov_mds_md_v1 *lmm)
421 magic = le32_to_cpu(lmm->lmm_magic);
422 /* If magic crashed, keep it there. Sometime later, during OST-object
423 * orphan handling, if some OST-object(s) back-point to it, it can be
424 * verified and repaired. */
425 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
429 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
430 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
435 CDEBUG(D_LFSCK, "%s LOV EA magic %u on "DOSTID"\n",
436 rc == -EINVAL ? "Unknown" : "Unsupported",
442 pattern = le32_to_cpu(lmm->lmm_pattern);
443 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
444 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
447 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
448 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u on "DOSTID"\n",
449 pattern, POSTID(&oi));
457 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_CACHE_SIZE
458 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
459 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
461 struct lfsck_rbtree_node {
462 struct rb_node lrn_node;
465 atomic_t lrn_known_count;
466 atomic_t lrn_accessed_count;
467 void *lrn_known_bitmap;
468 void *lrn_accessed_bitmap;
471 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
472 __u64 seq, __u32 oid)
474 if (seq < lrn->lrn_seq)
477 if (seq > lrn->lrn_seq)
480 if (oid < lrn->lrn_first_oid)
483 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
489 /* The caller should hold llsd->llsd_rb_lock. */
490 static struct lfsck_rbtree_node *
491 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
492 const struct lu_fid *fid, bool *exact)
494 struct rb_node *node = llsd->llsd_rb_root.rb_node;
495 struct rb_node *prev = NULL;
496 struct lfsck_rbtree_node *lrn = NULL;
502 while (node != NULL) {
504 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
505 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
507 node = node->rb_left;
509 node = node->rb_right;
517 /* If there is no exactly matched one, then to the next valid one. */
520 /* The rbtree is empty. */
527 node = rb_next(prev);
529 /* The end of the rbtree. */
533 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
538 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
539 const struct lu_fid *fid)
541 struct lfsck_rbtree_node *lrn;
545 return ERR_PTR(-ENOMEM);
547 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
548 if (lrn->lrn_known_bitmap == NULL) {
551 return ERR_PTR(-ENOMEM);
554 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
555 if (lrn->lrn_accessed_bitmap == NULL) {
556 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
559 return ERR_PTR(-ENOMEM);
562 RB_CLEAR_NODE(&lrn->lrn_node);
563 lrn->lrn_seq = fid_seq(fid);
564 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
565 atomic_set(&lrn->lrn_known_count, 0);
566 atomic_set(&lrn->lrn_accessed_count, 0);
571 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
573 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
574 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
578 /* The caller should hold lock. */
579 static struct lfsck_rbtree_node *
580 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
581 struct lfsck_rbtree_node *lrn)
583 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
584 struct rb_node *parent = NULL;
585 struct lfsck_rbtree_node *tmp;
588 while (*pos != NULL) {
590 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
591 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
593 pos = &(*pos)->rb_left;
595 pos = &(*pos)->rb_right;
600 rb_link_node(&lrn->lrn_node, parent, pos);
601 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
606 extern const struct dt_index_operations lfsck_orphan_index_ops;
608 static int lfsck_rbtree_setup(const struct lu_env *env,
609 struct lfsck_component *com)
611 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
612 struct lfsck_instance *lfsck = com->lc_lfsck;
613 struct dt_device *dev = lfsck->li_bottom;
614 struct lfsck_layout_slave_data *llsd = com->lc_data;
615 struct dt_object *obj;
617 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
618 fid->f_oid = lfsck_dev_idx(dev);
620 obj = dt_locate(env, dev, fid);
622 RETURN(PTR_ERR(obj));
624 /* Generate an in-RAM object to stand for the layout rbtree.
625 * Scanning the layout rbtree will be via the iteration over
626 * the object. In the future, the rbtree may be written onto
627 * disk with the object.
629 * Mark the object to be as exist. */
630 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
631 obj->do_index_ops = &lfsck_orphan_index_ops;
632 llsd->llsd_rb_obj = obj;
633 llsd->llsd_rbtree_valid = 1;
634 dev->dd_record_fid_accessed = 1;
636 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
637 lfsck_lfsck2name(lfsck));
642 static void lfsck_rbtree_cleanup(const struct lu_env *env,
643 struct lfsck_component *com)
645 struct lfsck_instance *lfsck = com->lc_lfsck;
646 struct lfsck_layout_slave_data *llsd = com->lc_data;
647 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
648 struct rb_node *next;
649 struct lfsck_rbtree_node *lrn;
651 lfsck->li_bottom->dd_record_fid_accessed = 0;
652 /* Invalid the rbtree, then no others will use it. */
653 write_lock(&llsd->llsd_rb_lock);
654 llsd->llsd_rbtree_valid = 0;
655 write_unlock(&llsd->llsd_rb_lock);
657 while (node != NULL) {
658 next = rb_next(node);
659 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
660 rb_erase(node, &llsd->llsd_rb_root);
661 lfsck_rbtree_free(lrn);
665 if (llsd->llsd_rb_obj != NULL) {
666 lu_object_put(env, &llsd->llsd_rb_obj->do_lu);
667 llsd->llsd_rb_obj = NULL;
670 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
671 lfsck_lfsck2name(lfsck));
674 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
675 struct lfsck_component *com,
676 const struct lu_fid *fid,
679 struct lfsck_layout_slave_data *llsd = com->lc_data;
680 struct lfsck_rbtree_node *lrn;
686 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
689 if (!fid_is_idif(fid) && !fid_is_norm(fid))
692 read_lock(&llsd->llsd_rb_lock);
693 if (!llsd->llsd_rbtree_valid)
694 GOTO(unlock, rc = 0);
696 lrn = lfsck_rbtree_search(llsd, fid, NULL);
698 struct lfsck_rbtree_node *tmp;
702 read_unlock(&llsd->llsd_rb_lock);
703 tmp = lfsck_rbtree_new(env, fid);
705 GOTO(out, rc = PTR_ERR(tmp));
708 write_lock(&llsd->llsd_rb_lock);
709 if (!llsd->llsd_rbtree_valid) {
710 lfsck_rbtree_free(tmp);
711 GOTO(unlock, rc = 0);
714 lrn = lfsck_rbtree_insert(llsd, tmp);
716 lfsck_rbtree_free(tmp);
719 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
720 /* Any accessed object must be a known object. */
721 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
722 atomic_inc(&lrn->lrn_known_count);
723 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
724 atomic_inc(&lrn->lrn_accessed_count);
726 GOTO(unlock, rc = 0);
730 write_unlock(&llsd->llsd_rb_lock);
732 read_unlock(&llsd->llsd_rb_lock);
734 if (rc != 0 && accessed) {
735 struct lfsck_layout *lo = com->lc_file_ram;
737 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
738 "bitmap, and will cause incorrect LFSCK OST-object "
739 "handling, so disable it to cancel orphan handling "
740 "for related device. rc = %d\n",
741 lfsck_lfsck2name(com->lc_lfsck), rc);
743 lo->ll_flags |= LF_INCOMPLETE;
744 lfsck_rbtree_cleanup(env, com);
748 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
749 const struct lfsck_layout *src)
753 des->ll_magic = le32_to_cpu(src->ll_magic);
754 des->ll_status = le32_to_cpu(src->ll_status);
755 des->ll_flags = le32_to_cpu(src->ll_flags);
756 des->ll_success_count = le32_to_cpu(src->ll_success_count);
757 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
758 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
759 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
760 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
761 des->ll_time_last_checkpoint =
762 le64_to_cpu(src->ll_time_last_checkpoint);
763 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
764 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
765 des->ll_pos_first_inconsistent =
766 le64_to_cpu(src->ll_pos_first_inconsistent);
767 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
768 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
769 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
770 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
771 for (i = 0; i < LLIT_MAX; i++)
772 des->ll_objs_repaired[i] =
773 le64_to_cpu(src->ll_objs_repaired[i]);
774 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
775 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
778 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
779 const struct lfsck_layout *src)
783 des->ll_magic = cpu_to_le32(src->ll_magic);
784 des->ll_status = cpu_to_le32(src->ll_status);
785 des->ll_flags = cpu_to_le32(src->ll_flags);
786 des->ll_success_count = cpu_to_le32(src->ll_success_count);
787 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
788 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
789 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
790 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
791 des->ll_time_last_checkpoint =
792 cpu_to_le64(src->ll_time_last_checkpoint);
793 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
794 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
795 des->ll_pos_first_inconsistent =
796 cpu_to_le64(src->ll_pos_first_inconsistent);
797 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
798 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
799 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
800 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
801 for (i = 0; i < LLIT_MAX; i++)
802 des->ll_objs_repaired[i] =
803 cpu_to_le64(src->ll_objs_repaired[i]);
804 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
805 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
809 * Load the OST bitmap from the lfsck_layout tracing file.
811 * \param[in] env pointer to the thread context
812 * \param[in] com pointer to the lfsck component
814 * \retval positive number for data corruption
815 * \retval 0 for success
816 * \retval negative error number on failure
818 static int lfsck_layout_load_bitmap(const struct lu_env *env,
819 struct lfsck_component *com)
821 struct dt_object *obj = com->lc_obj;
822 struct lfsck_assistant_data *lad = com->lc_data;
823 struct lfsck_layout *lo = com->lc_file_ram;
824 const struct dt_body_operations *dbo = obj->do_body_ops;
825 cfs_bitmap_t *bitmap = lad->lad_bitmap;
826 loff_t pos = com->lc_file_size;
832 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
834 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
836 nbits = lo->ll_bitmap_size;
838 if (unlikely(nbits < BITS_PER_LONG))
839 nbits = BITS_PER_LONG;
841 if (nbits > bitmap->size) {
842 __u32 new_bits = bitmap->size;
843 cfs_bitmap_t *new_bitmap;
845 while (new_bits < nbits)
848 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
849 if (new_bitmap == NULL)
852 lad->lad_bitmap = new_bitmap;
853 CFS_FREE_BITMAP(bitmap);
857 if (lo->ll_bitmap_size == 0) {
858 lad->lad_incomplete = 0;
859 CFS_RESET_BITMAP(bitmap);
864 size = (lo->ll_bitmap_size + 7) >> 3;
865 rc = dbo->dbo_read(env, obj,
866 lfsck_buf_get(env, bitmap->data, size), &pos,
870 } else if (rc != size) {
871 CDEBUG(D_LFSCK, "%s: lfsck_layout bitmap size %u != %u\n",
872 lfsck_lfsck2name(com->lc_lfsck),
873 (unsigned int)size, rc);
878 if (cfs_bitmap_check_empty(bitmap))
879 lad->lad_incomplete = 0;
881 lad->lad_incomplete = 1;
887 * Load the layout LFSCK tracing file from disk.
889 * The layout LFSCK tracing file records the layout LFSCK status information
890 * and other statistics, such as how many objects have been scanned, and how
891 * many objects have been repaired, and etc. It also contains the bitmap for
892 * failed OSTs during the layout LFSCK. All these information will be loaded
893 * from disk to RAM when the layout LFSCK component setup.
895 * \param[in] env pointer to the thread context
896 * \param[in] com pointer to the lfsck component
898 * \retval positive number for file data corruption, the caller
899 * should reset the layout LFSCK tracing file
900 * \retval 0 for success
901 * \retval negative error number on failure
903 static int lfsck_layout_load(const struct lu_env *env,
904 struct lfsck_component *com)
906 struct lfsck_layout *lo = com->lc_file_ram;
907 const struct dt_body_operations *dbo = com->lc_obj->do_body_ops;
908 ssize_t size = com->lc_file_size;
912 rc = dbo->dbo_read(env, com->lc_obj,
913 lfsck_buf_get(env, com->lc_file_disk, size), &pos,
918 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
919 lfsck_lfsck2name(com->lc_lfsck), rc);
921 } else if (rc != size) {
922 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
923 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
927 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
928 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
929 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
930 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
931 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
939 * Store the layout LFSCK tracing file on disk.
941 * The layout LFSCK tracing file records the layout LFSCK status information
942 * and other statistics, such as how many objects have been scanned, and how
943 * many objects have been repaired, and etc. It also contains the bitmap for
944 * failed OSTs during the layout LFSCK. All these information will be synced
945 * from RAM to disk periodically.
947 * \param[in] env pointer to the thread context
948 * \param[in] com pointer to the lfsck component
950 * \retval 0 for success
951 * \retval negative error number on failure
953 static int lfsck_layout_store(const struct lu_env *env,
954 struct lfsck_component *com)
956 struct dt_object *obj = com->lc_obj;
957 struct lfsck_instance *lfsck = com->lc_lfsck;
958 struct lfsck_layout *lo_ram = com->lc_file_ram;
959 struct lfsck_layout *lo = com->lc_file_disk;
961 struct dt_device *dev = lfsck->li_bottom;
962 cfs_bitmap_t *bitmap = NULL;
964 ssize_t size = com->lc_file_size;
969 if (lfsck->li_master) {
970 struct lfsck_assistant_data *lad = com->lc_data;
972 bitmap = lad->lad_bitmap;
973 nbits = bitmap->size;
976 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
979 lo_ram->ll_bitmap_size = nbits;
980 lfsck_layout_cpu_to_le(lo, lo_ram);
981 th = dt_trans_create(env, dev);
983 GOTO(log, rc = PTR_ERR(th));
985 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
990 if (bitmap != NULL) {
991 rc = dt_declare_record_write(env, obj,
992 lfsck_buf_get(env, bitmap->data, nbits >> 3),
998 rc = dt_trans_start_local(env, dev, th);
1003 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1007 if (bitmap != NULL) {
1009 rc = dt_record_write(env, obj,
1010 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1017 dt_trans_stop(env, dev, th);
1021 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1022 lfsck_lfsck2name(lfsck), rc);
1027 static int lfsck_layout_init(const struct lu_env *env,
1028 struct lfsck_component *com)
1030 struct lfsck_layout *lo = com->lc_file_ram;
1033 memset(lo, 0, com->lc_file_size);
1034 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1035 lo->ll_status = LS_INIT;
1036 down_write(&com->lc_sem);
1037 rc = lfsck_layout_store(env, com);
1038 up_write(&com->lc_sem);
1043 static int fid_is_for_ostobj(const struct lu_env *env, struct dt_device *dt,
1044 struct dt_object *obj, const struct lu_fid *fid)
1046 struct seq_server_site *ss = lu_site2seq(dt->dd_lu_dev.ld_site);
1047 struct lu_seq_range range = { 0 };
1048 struct lustre_mdt_attrs *lma;
1051 fld_range_set_any(&range);
1052 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), &range);
1054 if (fld_range_is_ost(&range))
1060 lma = &lfsck_env_info(env)->lti_lma;
1061 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
1062 XATTR_NAME_LMA, BYPASS_CAPA);
1063 if (rc == sizeof(*lma)) {
1064 lustre_lma_swab(lma);
1066 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1069 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID, BYPASS_CAPA);
1074 static struct lfsck_layout_seq *
1075 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1077 struct lfsck_layout_seq *lls;
1079 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1080 if (lls->lls_seq == seq)
1083 if (lls->lls_seq > seq)
1091 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1092 struct lfsck_layout_seq *lls)
1094 struct lfsck_layout_seq *tmp;
1095 struct list_head *pos = &llsd->llsd_seq_list;
1097 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1098 if (lls->lls_seq < tmp->lls_seq) {
1099 pos = &tmp->lls_list;
1103 list_add_tail(&lls->lls_list, pos);
1107 lfsck_layout_lastid_create(const struct lu_env *env,
1108 struct lfsck_instance *lfsck,
1109 struct dt_object *obj)
1111 struct lfsck_thread_info *info = lfsck_env_info(env);
1112 struct lu_attr *la = &info->lti_la;
1113 struct dt_object_format *dof = &info->lti_dof;
1114 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1115 struct dt_device *dt = lfsck->li_bottom;
1122 if (bk->lb_param & LPF_DRYRUN)
1125 memset(la, 0, sizeof(*la));
1126 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1127 la->la_valid = LA_MODE | LA_UID | LA_GID;
1128 dof->dof_type = dt_mode_to_dft(S_IFREG);
1130 th = dt_trans_create(env, dt);
1132 GOTO(log, rc = PTR_ERR(th));
1134 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1138 rc = dt_declare_record_write(env, obj,
1139 lfsck_buf_get(env, &lastid,
1145 rc = dt_trans_start_local(env, dt, th);
1149 dt_write_lock(env, obj, 0);
1150 if (likely(dt_object_exists(obj) == 0)) {
1151 rc = dt_create(env, obj, la, NULL, dof, th);
1153 rc = dt_record_write(env, obj,
1154 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1157 dt_write_unlock(env, obj);
1162 dt_trans_stop(env, dt, th);
1165 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1167 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1173 lfsck_layout_lastid_reload(const struct lu_env *env,
1174 struct lfsck_component *com,
1175 struct lfsck_layout_seq *lls)
1181 dt_read_lock(env, lls->lls_lastid_obj, 0);
1182 rc = dt_record_read(env, lls->lls_lastid_obj,
1183 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1184 dt_read_unlock(env, lls->lls_lastid_obj);
1185 if (unlikely(rc != 0))
1188 lastid = le64_to_cpu(lastid);
1189 if (lastid < lls->lls_lastid_known) {
1190 struct lfsck_instance *lfsck = com->lc_lfsck;
1191 struct lfsck_layout *lo = com->lc_file_ram;
1193 lls->lls_lastid = lls->lls_lastid_known;
1195 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1196 LASSERT(lfsck->li_out_notify != NULL);
1198 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1199 LE_LASTID_REBUILDING);
1200 lo->ll_flags |= LF_CRASHED_LASTID;
1202 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1203 "LAST_ID file (1) for the sequence "LPX64
1204 ", old value "LPU64", known value "LPU64"\n",
1205 lfsck_lfsck2name(lfsck), lls->lls_seq,
1206 lastid, lls->lls_lastid);
1208 } else if (lastid >= lls->lls_lastid) {
1209 lls->lls_lastid = lastid;
1217 lfsck_layout_lastid_store(const struct lu_env *env,
1218 struct lfsck_component *com)
1220 struct lfsck_instance *lfsck = com->lc_lfsck;
1221 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1222 struct dt_device *dt = lfsck->li_bottom;
1223 struct lfsck_layout_slave_data *llsd = com->lc_data;
1224 struct lfsck_layout_seq *lls;
1230 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1233 if (!lls->lls_dirty)
1236 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1237 "<seq> "LPX64" as <oid> "LPU64"\n",
1238 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1240 if (bk->lb_param & LPF_DRYRUN) {
1245 th = dt_trans_create(env, dt);
1248 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1249 "the LAST_ID for <seq> "LPX64"(1): rc = %d\n",
1250 lfsck_lfsck2name(com->lc_lfsck),
1255 lastid = cpu_to_le64(lls->lls_lastid);
1256 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1257 lfsck_buf_get(env, &lastid,
1263 rc = dt_trans_start_local(env, dt, th);
1267 dt_write_lock(env, lls->lls_lastid_obj, 0);
1268 rc = dt_record_write(env, lls->lls_lastid_obj,
1269 lfsck_buf_get(env, &lastid,
1270 sizeof(lastid)), &pos, th);
1271 dt_write_unlock(env, lls->lls_lastid_obj);
1276 dt_trans_stop(env, dt, th);
1279 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1280 "the LAST_ID for <seq> "LPX64"(2): rc = %d\n",
1281 lfsck_lfsck2name(com->lc_lfsck),
1290 lfsck_layout_lastid_load(const struct lu_env *env,
1291 struct lfsck_component *com,
1292 struct lfsck_layout_seq *lls)
1294 struct lfsck_instance *lfsck = com->lc_lfsck;
1295 struct lfsck_layout *lo = com->lc_file_ram;
1296 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1297 struct dt_object *obj;
1302 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck->li_bottom));
1303 obj = dt_locate(env, lfsck->li_bottom, fid);
1305 RETURN(PTR_ERR(obj));
1307 /* LAST_ID crashed, to be rebuilt */
1308 if (dt_object_exists(obj) == 0) {
1309 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1310 LASSERT(lfsck->li_out_notify != NULL);
1312 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1313 LE_LASTID_REBUILDING);
1314 lo->ll_flags |= LF_CRASHED_LASTID;
1316 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1317 "LAST_ID file for sequence "LPX64"\n",
1318 lfsck_lfsck2name(lfsck), lls->lls_seq);
1320 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1322 struct l_wait_info lwi = LWI_TIMEOUT(
1323 cfs_time_seconds(cfs_fail_val),
1326 up_write(&com->lc_sem);
1327 l_wait_event(lfsck->li_thread.t_ctl_waitq,
1328 !thread_is_running(&lfsck->li_thread),
1330 down_write(&com->lc_sem);
1334 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1336 dt_read_lock(env, obj, 0);
1337 rc = dt_read(env, obj,
1338 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1340 dt_read_unlock(env, obj);
1341 if (rc != 0 && rc != sizeof(__u64))
1342 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1344 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1345 LASSERT(lfsck->li_out_notify != NULL);
1347 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1348 LE_LASTID_REBUILDING);
1349 lo->ll_flags |= LF_CRASHED_LASTID;
1351 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1352 "LAST_ID file for the sequence "LPX64
1354 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1357 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1365 lfsck_object_put(env, obj);
1367 lls->lls_lastid_obj = obj;
1372 static void lfsck_layout_record_failure(const struct lu_env *env,
1373 struct lfsck_instance *lfsck,
1374 struct lfsck_layout *lo)
1378 lo->ll_objs_failed_phase1++;
1379 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1381 if (lo->ll_pos_first_inconsistent == 0 ||
1382 lo->ll_pos_first_inconsistent < cookie) {
1383 lo->ll_pos_first_inconsistent = cookie;
1385 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1386 "inconsistency at the pos ["LPU64"]\n",
1387 lfsck_lfsck2name(lfsck),
1388 lo->ll_pos_first_inconsistent);
1392 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1393 struct lfsck_component *com,
1396 struct lfsck_instance *lfsck = com->lc_lfsck;
1397 struct lfsck_layout *lo = com->lc_file_ram;
1399 down_write(&com->lc_sem);
1400 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1401 HALF_SEC - lfsck->li_time_last_checkpoint);
1402 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1403 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1406 com->lc_journal = 0;
1407 if (lo->ll_flags & LF_INCOMPLETE) {
1408 lo->ll_status = LS_PARTIAL;
1410 if (lfsck->li_master) {
1411 struct lfsck_assistant_data *lad = com->lc_data;
1413 if (lad->lad_incomplete)
1414 lo->ll_status = LS_PARTIAL;
1416 lo->ll_status = LS_COMPLETED;
1418 lo->ll_status = LS_COMPLETED;
1421 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1422 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1423 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1424 lo->ll_success_count++;
1425 } else if (rc == 0) {
1426 lo->ll_status = lfsck->li_status;
1427 if (lo->ll_status == 0)
1428 lo->ll_status = LS_STOPPED;
1430 lo->ll_status = LS_FAILED;
1433 rc = lfsck_layout_store(env, com);
1434 up_write(&com->lc_sem);
1439 static int lfsck_layout_trans_stop(const struct lu_env *env,
1440 struct dt_device *dev,
1441 struct thandle *handle, int result)
1445 handle->th_result = result;
1446 rc = dt_trans_stop(env, dev, handle);
1456 * Get the system default stripe size.
1458 * \param[in] env pointer to the thread context
1459 * \param[in] lfsck pointer to the lfsck instance
1460 * \param[out] size pointer to the default stripe size
1462 * \retval 0 for success
1463 * \retval negative error number on failure
1465 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1466 struct lfsck_instance *lfsck,
1469 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1470 struct dt_object *root;
1473 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1475 return PTR_ERR(root);
1477 /* Get the default stripe size via xattr_get on the backend root. */
1478 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1479 XATTR_NAME_LOV, BYPASS_CAPA);
1481 /* The lum->lmm_stripe_size is LE mode. The *size also
1482 * should be LE mode. So it is unnecessary to convert. */
1483 *size = lum->lmm_stripe_size;
1485 } else if (unlikely(rc == 0)) {
1489 lfsck_object_put(env, root);
1495 * \retval +1: repaired
1496 * \retval 0: did nothing
1497 * \retval -ve: on error
1499 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1500 struct thandle *handle,
1501 struct dt_object *parent,
1502 struct lu_fid *cfid,
1504 struct lov_ost_data_v1 *slot,
1505 int fl, __u32 ost_idx)
1507 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1508 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1509 struct lu_buf ea_buf;
1514 magic = le32_to_cpu(lmm->lmm_magic);
1515 count = le16_to_cpu(lmm->lmm_stripe_count);
1517 fid_to_ostid(cfid, oi);
1518 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1519 slot->l_ost_gen = cpu_to_le32(0);
1520 slot->l_ost_idx = cpu_to_le32(ost_idx);
1522 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE) {
1523 struct lov_ost_data_v1 *objs;
1526 if (magic == LOV_MAGIC_V1)
1527 objs = &lmm->lmm_objects[0];
1529 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1530 for (i = 0; i < count; i++, objs++) {
1531 if (objs != slot && lovea_slot_is_dummy(objs))
1535 /* If the @slot is the last dummy slot to be refilled,
1536 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1538 lmm->lmm_pattern &= ~cpu_to_le32(LOV_PATTERN_F_HOLE);
1541 lfsck_buf_init(&ea_buf, lmm, lov_mds_md_size(count, magic));
1542 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle,
1551 * \retval +1: repaired
1552 * \retval 0: did nothing
1553 * \retval -ve: on error
1555 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1556 struct lfsck_instance *lfsck,
1557 struct thandle *handle,
1558 struct dt_object *parent,
1559 struct lu_fid *cfid,
1560 struct lu_buf *buf, int fl,
1561 __u32 ost_idx, __u32 ea_off, bool reset)
1563 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1564 struct lov_ost_data_v1 *objs;
1570 if (fl == LU_XATTR_CREATE || reset) {
1571 __u32 pattern = LOV_PATTERN_RAID0;
1574 LASSERT(buf->lb_len >= lov_mds_md_size(count, LOV_MAGIC_V1));
1576 if (ea_off != 0 || reset) {
1577 pattern |= LOV_PATTERN_F_HOLE;
1581 memset(lmm, 0, buf->lb_len);
1582 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1583 lmm->lmm_pattern = cpu_to_le32(pattern);
1584 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1585 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1587 rc = lfsck_layout_get_def_stripesize(env, lfsck,
1588 &lmm->lmm_stripe_size);
1592 objs = &lmm->lmm_objects[ea_off];
1594 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1597 count = le16_to_cpu(lmm->lmm_stripe_count);
1598 if (magic == LOV_MAGIC_V1)
1599 objs = &lmm->lmm_objects[count];
1601 objs = &((struct lov_mds_md_v3 *)lmm)->
1604 gap = ea_off - count;
1607 LASSERT(buf->lb_len >= lov_mds_md_size(count, magic));
1610 memset(objs, 0, gap * sizeof(*objs));
1611 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1615 lmm->lmm_layout_gen =
1616 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1620 lmm->lmm_stripe_count = cpu_to_le16(count);
1621 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1624 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1625 DFID": parent "DFID", OST-index %u, stripe-index %u, fl %d, "
1626 "reset %s, %s LOV EA hole: rc = %d\n",
1627 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1628 ost_idx, ea_off, fl, reset ? "yes" : "no",
1629 hole ? "with" : "without", rc);
1635 * \retval +1: repaired
1636 * \retval 0: did nothing
1637 * \retval -ve: on error
1639 static int lfsck_layout_update_pfid(const struct lu_env *env,
1640 struct lfsck_component *com,
1641 struct dt_object *parent,
1642 struct lu_fid *cfid,
1643 struct dt_device *cdev, __u32 ea_off)
1645 struct filter_fid *pfid = &lfsck_env_info(env)->lti_new_pfid;
1646 struct dt_object *child;
1647 struct thandle *handle;
1648 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
1653 child = lfsck_object_find_by_dev(env, cdev, cfid);
1655 RETURN(PTR_ERR(child));
1657 handle = dt_trans_create(env, cdev);
1659 GOTO(out, rc = PTR_ERR(handle));
1661 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
1662 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
1663 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1664 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1665 * parent MDT-object's layout EA. */
1666 pfid->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
1667 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
1669 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
1673 rc = dt_trans_start(env, cdev, handle);
1677 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle,
1680 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1683 dt_trans_stop(env, cdev, handle);
1686 lu_object_put(env, &child->do_lu);
1692 * This function will create the MDT-object with the given (partial) LOV EA.
1694 * Under some data corruption cases, the MDT-object of the file may be lost,
1695 * but its OST-objects, or some of them are there. The layout LFSCK needs to
1696 * re-create the MDT-object with the orphan OST-object(s) information.
1698 * On the other hand, the LFSCK may has created some OST-object for repairing
1699 * dangling LOV EA reference, but as the LFSCK processing, it may find that
1700 * the old OST-object is there and should replace the former new created OST
1701 * object. Unfortunately, some others have modified such newly created object.
1702 * To keep the data (both new and old), the LFSCK will create MDT-object with
1703 * new FID to reference the original OST-object.
1705 * \param[in] env pointer to the thread context
1706 * \param[in] com pointer to the lfsck component
1707 * \param[in] ltd pointer to target device descriptor
1708 * \param[in] rec pointer to the record for the orphan OST-object
1709 * \param[in] cfid pointer to FID for the orphan OST-object
1710 * \param[in] infix additional information, such as the FID for original
1711 * MDT-object and the stripe offset in the LOV EA
1712 * \param[in] type the type for describing why the orphan MDT-object is
1713 * created. The rules are as following:
1715 * type "C": Multiple OST-objects claim the same MDT-object and the
1716 * same slot in the layout EA. Then the LFSCK will create
1717 * new MDT-object(s) to hold the conflict OST-object(s).
1719 * type "N": The orphan OST-object does not know which one was the
1720 * real parent MDT-object, so the LFSCK uses new FID for
1721 * its parent MDT-object.
1723 * type "R": The orphan OST-object knows its parent MDT-object FID,
1724 * but does not know the position (the file name) in the
1727 * The orphan name will be like:
1728 * ${FID}-${infix}-${type}-${conflict_version}
1730 * \param[in] ea_off the stripe offset in the LOV EA
1732 * \retval positive on repaired something
1733 * \retval 0 if needs to repair nothing
1734 * \retval negative error number on failure
1736 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1737 struct lfsck_component *com,
1738 struct lfsck_tgt_desc *ltd,
1739 struct lu_orphan_rec *rec,
1740 struct lu_fid *cfid,
1745 struct lfsck_thread_info *info = lfsck_env_info(env);
1746 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
1747 char *name = info->lti_key;
1748 struct lu_attr *la = &info->lti_la;
1749 struct dt_object_format *dof = &info->lti_dof;
1750 struct lfsck_instance *lfsck = com->lc_lfsck;
1751 struct lu_fid *pfid = &rec->lor_fid;
1752 struct lu_fid *tfid = &info->lti_fid3;
1753 struct dt_device *next = lfsck->li_next;
1754 struct dt_object *pobj = NULL;
1755 struct dt_object *cobj = NULL;
1756 struct thandle *th = NULL;
1757 struct lu_buf pbuf = { 0 };
1758 struct lu_buf *ea_buf = &info->lti_big_buf;
1759 struct lu_buf lov_buf;
1760 struct lustre_handle lh = { 0 };
1761 struct linkea_data ldata = { 0 };
1762 struct lu_buf linkea_buf;
1763 const struct lu_name *pname;
1769 /* Create .lustre/lost+found/MDTxxxx when needed. */
1770 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1771 rc = lfsck_create_lpf(env, lfsck);
1776 if (fid_is_zero(pfid)) {
1777 struct filter_fid *ff = &info->lti_new_pfid;
1779 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1783 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1784 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1785 /* Currently, the filter_fid::ff_parent::f_ver is not the
1786 * real parent MDT-object's FID::f_ver, instead it is the
1787 * OST-object index in its parent MDT-object's layout EA. */
1788 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
1789 lfsck_buf_init(&pbuf, ff, sizeof(struct filter_fid));
1790 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1792 GOTO(log, rc = PTR_ERR(cobj));
1795 pobj = lfsck_object_find_by_dev(env, lfsck->li_bottom, pfid);
1797 GOTO(put, rc = PTR_ERR(pobj));
1799 LASSERT(infix != NULL);
1800 LASSERT(type != NULL);
1803 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
1805 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1806 (const struct dt_key *)name, BYPASS_CAPA);
1807 if (rc != 0 && rc != -ENOENT)
1811 rc = linkea_data_new(&ldata,
1812 &lfsck_env_info(env)->lti_linkea_buf);
1816 pname = lfsck_name_get_const(env, name, strlen(name));
1817 rc = linkea_add_buf(&ldata, pname, lfsck_dto2fid(lfsck->li_lpf_obj));
1821 memset(la, 0, sizeof(*la));
1822 la->la_uid = rec->lor_uid;
1823 la->la_gid = rec->lor_gid;
1824 la->la_mode = S_IFREG | S_IRUSR;
1825 la->la_valid = LA_MODE | LA_UID | LA_GID;
1827 memset(dof, 0, sizeof(*dof));
1828 dof->dof_type = dt_mode_to_dft(S_IFREG);
1830 size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1831 if (ea_buf->lb_len < size) {
1832 lu_buf_realloc(ea_buf, size);
1833 if (ea_buf->lb_buf == NULL)
1834 GOTO(put, rc = -ENOMEM);
1837 /* Hold update lock on the .lustre/lost+found/MDTxxxx/.
1839 * XXX: Currently, we do not grab the PDO lock as normal create cases,
1840 * because creating MDT-object for orphan OST-object is rare, we
1841 * do not much care about the performance. It can be improved in
1842 * the future when needed. */
1843 rc = lfsck_ibits_lock(env, lfsck, lfsck->li_lpf_obj, &lh,
1844 MDS_INODELOCK_UPDATE, LCK_EX);
1848 th = dt_trans_create(env, next);
1850 GOTO(unlock, rc = PTR_ERR(th));
1852 /* 1a. Update OST-object's parent information remotely.
1854 * If other subsequent modifications failed, then next LFSCK scanning
1855 * will process the OST-object as orphan again with known parent FID. */
1857 rc = dt_declare_xattr_set(env, cobj, &pbuf, XATTR_NAME_FID,
1863 /* 2a. Create the MDT-object locally. */
1864 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1868 /* 3a. Add layout EA for the MDT-object. */
1869 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
1870 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
1871 LU_XATTR_CREATE, th);
1875 /* 4a. Insert the MDT-object to .lustre/lost+found/MDTxxxx/ */
1876 dtrec->rec_fid = pfid;
1877 dtrec->rec_type = S_IFREG;
1878 rc = dt_declare_insert(env, lfsck->li_lpf_obj,
1879 (const struct dt_rec *)dtrec,
1880 (const struct dt_key *)name, th);
1884 /* 5a. insert linkEA for parent. */
1885 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1886 ldata.ld_leh->leh_len);
1887 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
1888 XATTR_NAME_LINK, 0, th);
1892 rc = dt_trans_start(env, next, th);
1896 /* 1b. Update OST-object's parent information remotely. */
1898 rc = dt_xattr_set(env, cobj, &pbuf, XATTR_NAME_FID, 0, th,
1904 dt_write_lock(env, pobj, 0);
1905 /* 2b. Create the MDT-object locally. */
1906 rc = dt_create(env, pobj, la, NULL, dof, th);
1908 /* 3b. Add layout EA for the MDT-object. */
1909 rc = lfsck_layout_extend_lovea(env, lfsck, th, pobj, cfid,
1910 &lov_buf, LU_XATTR_CREATE,
1911 ltd->ltd_index, ea_off, false);
1912 dt_write_unlock(env, pobj);
1916 /* 4b. Insert the MDT-object to .lustre/lost+found/MDTxxxx/ */
1917 rc = dt_insert(env, lfsck->li_lpf_obj, (const struct dt_rec *)dtrec,
1918 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1922 /* 5b. insert linkEA for parent. */
1923 rc = dt_xattr_set(env, pobj, &linkea_buf,
1924 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1929 dt_trans_stop(env, next, th);
1932 lfsck_ibits_unlock(&lh, LCK_EX);
1935 if (cobj != NULL && !IS_ERR(cobj))
1936 lu_object_put(env, &cobj->do_lu);
1937 if (pobj != NULL && !IS_ERR(pobj))
1938 lu_object_put(env, &pobj->do_lu);
1942 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
1943 "recreate the lost MDT-object: parent "DFID
1944 ", child "DFID", OST-index %u, stripe-index %u, "
1945 "infix %s, type %s: rc = %d\n",
1946 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
1947 ltd->ltd_index, ea_off, infix, type, rc);
1949 return rc >= 0 ? 1 : rc;
1952 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
1953 struct lfsck_component *com,
1954 const struct lu_fid *fid,
1957 struct lfsck_thread_info *info = lfsck_env_info(env);
1958 struct lfsck_request *lr = &info->lti_lr;
1959 struct lfsck_instance *lfsck = com->lc_lfsck;
1960 struct lfsck_tgt_desc *ltd;
1961 struct ptlrpc_request *req;
1962 struct lfsck_request *tmp;
1963 struct obd_export *exp;
1967 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
1968 if (unlikely(ltd == NULL))
1972 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
1973 GOTO(put, rc = -EOPNOTSUPP);
1975 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
1977 GOTO(put, rc = -ENOMEM);
1979 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1981 ptlrpc_request_free(req);
1986 memset(lr, 0, sizeof(*lr));
1987 lr->lr_event = LE_CONDITIONAL_DESTROY;
1988 lr->lr_active = LFSCK_TYPE_LAYOUT;
1991 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1993 ptlrpc_request_set_replen(req);
1995 rc = ptlrpc_queue_wait(req);
1996 ptlrpc_req_finished(req);
2006 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2007 struct lfsck_component *com,
2008 struct lfsck_request *lr)
2010 struct lfsck_thread_info *info = lfsck_env_info(env);
2011 struct lu_attr *la = &info->lti_la;
2012 ldlm_policy_data_t *policy = &info->lti_policy;
2013 struct ldlm_res_id *resid = &info->lti_resid;
2014 struct lfsck_instance *lfsck = com->lc_lfsck;
2015 struct dt_device *dev = lfsck->li_bottom;
2016 struct lu_fid *fid = &lr->lr_fid;
2017 struct dt_object *obj;
2018 struct thandle *th = NULL;
2019 struct lustre_handle lh = { 0 };
2024 obj = lfsck_object_find_by_dev(env, dev, fid);
2026 RETURN(PTR_ERR(obj));
2028 dt_read_lock(env, obj, 0);
2029 if (dt_object_exists(obj) == 0 ||
2030 lfsck_is_dead_obj(obj)) {
2031 dt_read_unlock(env, obj);
2033 GOTO(put, rc = -ENOENT);
2036 /* Get obj's attr without lock firstly. */
2037 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2038 dt_read_unlock(env, obj);
2042 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2043 GOTO(put, rc = -ETXTBSY);
2045 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2046 LASSERT(lfsck->li_namespace != NULL);
2048 memset(policy, 0, sizeof(*policy));
2049 policy->l_extent.end = OBD_OBJECT_EOF;
2050 ost_fid_build_resid(fid, resid);
2051 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2052 policy, LCK_EX, &flags, ldlm_blocking_ast,
2053 ldlm_completion_ast, NULL, NULL, 0,
2054 LVB_T_NONE, NULL, &lh);
2056 GOTO(put, rc = -EIO);
2058 dt_write_lock(env, obj, 0);
2059 /* Get obj's attr within lock again. */
2060 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2064 if (la->la_ctime != 0)
2065 GOTO(unlock, rc = -ETXTBSY);
2067 th = dt_trans_create(env, dev);
2069 GOTO(unlock, rc = PTR_ERR(th));
2071 rc = dt_declare_ref_del(env, obj, th);
2075 rc = dt_declare_destroy(env, obj, th);
2079 rc = dt_trans_start_local(env, dev, th);
2083 rc = dt_ref_del(env, obj, th);
2087 rc = dt_destroy(env, obj, th);
2089 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2090 "OST-object "DFID" that was created for reparing "
2091 "dangling referenced case. But the original missed "
2092 "OST-object is found now.\n",
2093 lfsck_lfsck2name(lfsck), PFID(fid));
2098 dt_trans_stop(env, dev, th);
2101 dt_write_unlock(env, obj);
2102 ldlm_lock_decref(&lh, LCK_EX);
2105 lu_object_put(env, &obj->do_lu);
2111 * Some OST-object has occupied the specified layout EA slot.
2112 * Such OST-object may be generated by the LFSCK when repair
2113 * dangling referenced MDT-object, which can be indicated by
2114 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2115 * is true and such OST-object has not been modified yet, we
2116 * will replace it with the orphan OST-object; otherwise the
2117 * LFSCK will create new MDT-object to reference the orphan.
2119 * \retval +1: repaired
2120 * \retval 0: did nothing
2121 * \retval -ve: on error
2123 static int lfsck_layout_conflict_create(const struct lu_env *env,
2124 struct lfsck_component *com,
2125 struct lfsck_tgt_desc *ltd,
2126 struct lu_orphan_rec *rec,
2127 struct dt_object *parent,
2128 struct lu_fid *cfid,
2129 struct lu_buf *ea_buf,
2130 struct lov_ost_data_v1 *slot,
2133 struct lfsck_thread_info *info = lfsck_env_info(env);
2134 struct lu_fid *cfid2 = &info->lti_fid2;
2135 struct ost_id *oi = &info->lti_oi;
2136 char *infix = info->lti_tmpbuf;
2137 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2138 struct dt_device *dev = com->lc_lfsck->li_bottom;
2139 struct thandle *th = NULL;
2140 struct lustre_handle lh = { 0 };
2141 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2145 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2146 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2150 /* Hold layout lock on the parent to prevent others to access. */
2151 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2152 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2157 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2159 /* If the conflict OST-obejct is not created for fixing dangling
2160 * referenced MDT-object in former LFSCK check/repair, or it has
2161 * been modified by others, then we cannot destroy it. Re-create
2162 * a new MDT-object for the orphan OST-object. */
2163 if (rc == -ETXTBSY) {
2164 /* No need the layout lock on the original parent. */
2165 lfsck_ibits_unlock(&lh, LCK_EX);
2167 fid_zero(&rec->lor_fid);
2168 snprintf(infix, LFSCK_TMPBUF_LEN, "-"DFID"-%x",
2169 PFID(lu_object_fid(&parent->do_lu)), ea_off);
2170 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2171 infix, "C", ea_off);
2176 if (rc != 0 && rc != -ENOENT)
2179 th = dt_trans_create(env, dev);
2181 GOTO(unlock, rc = PTR_ERR(th));
2183 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2184 LU_XATTR_REPLACE, th);
2188 rc = dt_trans_start_local(env, dev, th);
2192 dt_write_lock(env, parent, 0);
2193 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2194 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2195 LU_XATTR_REPLACE, ltd->ltd_index);
2196 dt_write_unlock(env, parent);
2201 dt_trans_stop(env, dev, th);
2204 lfsck_ibits_unlock(&lh, LCK_EX);
2207 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2208 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2209 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2210 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2211 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2214 return rc >= 0 ? 1 : rc;
2218 * \retval +1: repaired
2219 * \retval 0: did nothing
2220 * \retval -ve: on error
2222 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2223 struct lfsck_component *com,
2224 struct lfsck_tgt_desc *ltd,
2225 struct lu_orphan_rec *rec,
2226 struct dt_object *parent,
2227 struct lu_fid *cfid,
2228 __u32 ost_idx, __u32 ea_off)
2230 struct lfsck_thread_info *info = lfsck_env_info(env);
2231 struct lu_buf *buf = &info->lti_big_buf;
2232 struct lu_fid *fid = &info->lti_fid2;
2233 struct ost_id *oi = &info->lti_oi;
2234 struct lfsck_instance *lfsck = com->lc_lfsck;
2235 struct dt_device *dt = lfsck->li_bottom;
2236 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2237 struct thandle *handle = NULL;
2239 struct lov_mds_md_v1 *lmm;
2240 struct lov_ost_data_v1 *objs;
2241 struct lustre_handle lh = { 0 };
2248 bool locked = false;
2251 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2252 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2255 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2256 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2257 "stripe-index %u: rc = %d\n",
2258 lfsck_lfsck2name(lfsck), PFID(cfid),
2259 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off, rc);
2266 dt_write_unlock(env, parent);
2270 if (handle != NULL) {
2271 dt_trans_stop(env, dt, handle);
2276 GOTO(unlock_layout, rc);
2279 if (buf->lb_len < lovea_size) {
2280 lu_buf_realloc(buf, lovea_size);
2281 if (buf->lb_buf == NULL)
2282 GOTO(unlock_layout, rc = -ENOMEM);
2285 if (!(bk->lb_param & LPF_DRYRUN)) {
2286 handle = dt_trans_create(env, dt);
2288 GOTO(unlock_layout, rc = PTR_ERR(handle));
2290 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2295 rc = dt_trans_start_local(env, dt, handle);
2300 dt_write_lock(env, parent, 0);
2302 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
2303 if (rc == -ERANGE) {
2304 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV,
2308 } else if (rc == -ENODATA || rc == 0) {
2309 lovea_size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2310 /* If the declared is not big enough, re-try. */
2311 if (buf->lb_len < lovea_size) {
2315 fl = LU_XATTR_CREATE;
2316 } else if (rc < 0) {
2317 GOTO(unlock_parent, rc);
2318 } else if (unlikely(buf->lb_len == 0)) {
2321 fl = LU_XATTR_REPLACE;
2325 if (fl == LU_XATTR_CREATE) {
2326 if (bk->lb_param & LPF_DRYRUN)
2327 GOTO(unlock_parent, rc = 1);
2329 LASSERT(buf->lb_len >= lovea_size);
2331 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2332 buf, fl, ost_idx, ea_off, false);
2334 GOTO(unlock_parent, rc);
2338 rc1 = lfsck_layout_verify_header(lmm);
2340 /* If the LOV EA crashed, the rebuild it. */
2341 if (rc1 == -EINVAL) {
2342 if (bk->lb_param & LPF_DRYRUN)
2343 GOTO(unlock_parent, rc = 1);
2345 LASSERT(buf->lb_len >= lovea_size);
2347 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2348 buf, fl, ost_idx, ea_off, true);
2350 GOTO(unlock_parent, rc);
2353 /* For other unknown magic/pattern, keep the current LOV EA. */
2355 GOTO(unlock_parent, rc = rc1);
2357 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2358 * been verified in lfsck_layout_verify_header() already. If some
2359 * new magic introduced in the future, then layout LFSCK needs to
2360 * be updated also. */
2361 magic = le32_to_cpu(lmm->lmm_magic);
2362 if (magic == LOV_MAGIC_V1) {
2363 objs = &lmm->lmm_objects[0];
2365 LASSERT(magic == LOV_MAGIC_V3);
2366 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2369 count = le16_to_cpu(lmm->lmm_stripe_count);
2371 GOTO(unlock_parent, rc = -EINVAL);
2374 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2375 if (count <= ea_off) {
2376 if (bk->lb_param & LPF_DRYRUN)
2377 GOTO(unlock_parent, rc = 1);
2379 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2380 /* If the declared is not big enough, re-try. */
2381 if (buf->lb_len < lovea_size) {
2386 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2387 buf, fl, ost_idx, ea_off, false);
2389 GOTO(unlock_parent, rc);
2392 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2394 for (i = 0; i < count; i++, objs++) {
2395 /* The MDT-object was created via lfsck_layout_recover_create()
2396 * by others before, and we fill the dummy layout EA. */
2397 if (lovea_slot_is_dummy(objs)) {
2401 if (bk->lb_param & LPF_DRYRUN)
2402 GOTO(unlock_parent, rc = 1);
2404 lmm->lmm_layout_gen =
2405 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2406 rc = lfsck_layout_refill_lovea(env, handle, parent,
2407 cfid, buf, objs, fl,
2410 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2411 "dummy layout slot for "DFID": parent "DFID
2412 ", OST-index %u, stripe-index %u: rc = %d\n",
2413 lfsck_lfsck2name(lfsck), PFID(cfid),
2414 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2416 GOTO(unlock_parent, rc);
2419 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2420 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2422 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2423 "invalid layout EA at the slot %d, index %u\n",
2424 lfsck_lfsck2name(lfsck),
2425 PFID(lfsck_dto2fid(parent)), i,
2426 le32_to_cpu(objs->l_ost_idx));
2428 GOTO(unlock_parent, rc);
2431 /* It should be rare case, the slot is there, but the LFSCK
2432 * does not handle it during the first-phase cycle scanning. */
2433 if (unlikely(lu_fid_eq(fid, cfid))) {
2435 GOTO(unlock_parent, rc = 0);
2437 /* Rare case that the OST-object index
2438 * does not match the parent MDT-object
2439 * layout EA. We trust the later one. */
2440 if (bk->lb_param & LPF_DRYRUN)
2441 GOTO(unlock_parent, rc = 1);
2443 dt_write_unlock(env, parent);
2445 dt_trans_stop(env, dt, handle);
2446 lfsck_ibits_unlock(&lh, LCK_EX);
2447 rc = lfsck_layout_update_pfid(env, com, parent,
2448 cfid, ltd->ltd_tgt, i);
2450 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2451 "updated OST-object's pfid for "DFID
2452 ": parent "DFID", OST-index %u, "
2453 "stripe-index %u: rc = %d\n",
2454 lfsck_lfsck2name(lfsck), PFID(cfid),
2455 PFID(lfsck_dto2fid(parent)),
2456 ltd->ltd_index, i, rc);
2463 /* The MDT-object exists, but related layout EA slot is occupied
2465 if (bk->lb_param & LPF_DRYRUN)
2466 GOTO(unlock_parent, rc = 1);
2468 dt_write_unlock(env, parent);
2470 dt_trans_stop(env, dt, handle);
2471 lfsck_ibits_unlock(&lh, LCK_EX);
2472 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2473 objs = &lmm->lmm_objects[ea_off];
2475 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2476 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2483 dt_write_unlock(env, parent);
2487 dt_trans_stop(env, dt, handle);
2490 lfsck_ibits_unlock(&lh, LCK_EX);
2495 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2496 struct lfsck_component *com,
2497 struct lfsck_tgt_desc *ltd,
2498 struct lu_orphan_rec *rec,
2499 struct lu_fid *cfid)
2501 struct lfsck_layout *lo = com->lc_file_ram;
2502 struct lu_fid *pfid = &rec->lor_fid;
2503 struct dt_object *parent = NULL;
2504 __u32 ea_off = pfid->f_stripe_idx;
2508 if (!fid_is_sane(cfid))
2509 GOTO(out, rc = -EINVAL);
2511 if (fid_is_zero(pfid)) {
2512 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2518 if (!fid_is_sane(pfid))
2519 GOTO(out, rc = -EINVAL);
2521 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2523 GOTO(out, rc = PTR_ERR(parent));
2525 if (unlikely(dt_object_remote(parent) != 0))
2526 GOTO(put, rc = -EXDEV);
2528 if (dt_object_exists(parent) == 0) {
2529 lu_object_put(env, &parent->do_lu);
2530 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2535 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2536 GOTO(put, rc = -EISDIR);
2538 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2539 ltd->ltd_index, ea_off);
2545 lu_object_put(env, &parent->do_lu);
2547 /* The layout EA is changed, need to be reloaded next time. */
2548 lu_object_put_nocache(env, &parent->do_lu);
2551 down_write(&com->lc_sem);
2552 com->lc_new_scanned++;
2553 com->lc_new_checked++;
2555 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2557 } else if (rc < 0) {
2558 lo->ll_objs_failed_phase2++;
2560 up_write(&com->lc_sem);
2565 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2566 struct lfsck_component *com,
2567 struct lfsck_tgt_desc *ltd)
2569 struct lfsck_assistant_data *lad = com->lc_data;
2570 struct lfsck_instance *lfsck = com->lc_lfsck;
2571 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2572 struct lfsck_thread_info *info = lfsck_env_info(env);
2573 struct ost_id *oi = &info->lti_oi;
2574 struct lu_fid *fid = &info->lti_fid;
2575 struct dt_object *obj;
2576 const struct dt_it_ops *iops;
2581 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
2582 "scanning for OST%04x\n",
2583 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2585 if (lad->lad_incomplete &&
2586 cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
2587 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
2588 "scanning for OST%04x\n",
2589 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2594 ostid_set_seq(oi, FID_SEQ_IDIF);
2595 ostid_set_id(oi, 0);
2596 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
2600 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2601 if (unlikely(IS_ERR(obj)))
2602 GOTO(log, rc = PTR_ERR(obj));
2604 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_orphan_features);
2608 iops = &obj->do_index_ops->dio_it;
2609 di = iops->init(env, obj, 0, BYPASS_CAPA);
2611 GOTO(put, rc = PTR_ERR(di));
2613 rc = iops->load(env, di, 0);
2615 /* -ESRCH means that the orphan OST-objects rbtree has been
2616 * cleanup because of the OSS server restart or other errors. */
2617 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
2622 rc = iops->next(env, di);
2634 struct lu_orphan_rec *rec = &info->lti_rec;
2636 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
2638 struct ptlrpc_thread *thread = &lfsck->li_thread;
2639 struct l_wait_info lwi;
2641 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2643 l_wait_event(thread->t_ctl_waitq,
2644 !thread_is_running(thread),
2648 key = iops->key(env, di);
2649 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2650 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2652 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2653 &com->lc_fid_latest_scanned_phase2);
2654 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2657 lfsck_control_speed_by_self(com);
2659 rc = iops->next(env, di);
2660 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2667 iops->fini(env, di);
2669 lu_object_put(env, &obj->do_lu);
2672 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
2673 "scanning for OST%04x: rc = %d\n",
2674 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2676 return rc > 0 ? 0 : rc;
2679 /* For the MDT-object with dangling reference, we need to repare the
2680 * inconsistency according to the LFSCK sponsor's requirement:
2682 * 1) Keep the inconsistency there and report the inconsistency case,
2683 * then give the chance to the application to find related issues,
2684 * and the users can make the decision about how to handle it with
2685 * more human knownledge. (by default)
2687 * 2) Re-create the missed OST-object with the FID/owner information. */
2688 static int lfsck_layout_repair_dangling(const struct lu_env *env,
2689 struct lfsck_component *com,
2690 struct lfsck_layout_req *llr,
2691 const struct lu_attr *pla)
2693 struct lfsck_thread_info *info = lfsck_env_info(env);
2694 struct filter_fid *pfid = &info->lti_new_pfid;
2695 struct dt_allocation_hint *hint = &info->lti_hint;
2696 struct lu_attr *cla = &info->lti_la2;
2697 struct dt_object *parent = llr->llr_parent->llo_obj;
2698 struct dt_object *child = llr->llr_child;
2699 struct dt_device *dev = lfsck_obj2dt_dev(child);
2700 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2701 struct thandle *handle;
2703 struct lustre_handle lh = { 0 };
2708 if (com->lc_lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ)
2716 memset(cla, 0, sizeof(*cla));
2717 cla->la_uid = pla->la_uid;
2718 cla->la_gid = pla->la_gid;
2719 cla->la_mode = S_IFREG | 0666;
2720 cla->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
2721 LA_ATIME | LA_MTIME | LA_CTIME;
2723 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2724 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2729 handle = dt_trans_create(env, dev);
2731 GOTO(unlock1, rc = PTR_ERR(handle));
2733 hint->dah_parent = NULL;
2735 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2736 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2737 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2738 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2739 * parent MDT-object's layout EA. */
2740 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2741 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2743 rc = dt_declare_create(env, child, cla, hint, NULL, handle);
2747 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2748 LU_XATTR_CREATE, handle);
2752 rc = dt_trans_start(env, dev, handle);
2756 dt_read_lock(env, parent, 0);
2757 if (unlikely(lfsck_is_dead_obj(parent)))
2758 GOTO(unlock2, rc = 1);
2760 rc = dt_create(env, child, cla, hint, NULL, handle);
2764 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2765 handle, BYPASS_CAPA);
2770 dt_read_unlock(env, parent);
2773 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2776 lfsck_ibits_unlock(&lh, LCK_EX);
2779 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found dangling "
2780 "reference for: parent "DFID", child "DFID", OST-index %u, "
2781 "stripe-index %u, owner %u/%u. %s: rc = %d\n",
2782 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
2783 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx,
2784 llr->llr_lov_idx, pla->la_uid, pla->la_gid,
2785 create ? "Create the lost OST-object as required" :
2786 "Keep the MDT-object there by default", rc);
2791 /* If the OST-object does not recognize the MDT-object as its parent, and
2792 * there is no other MDT-object claims as its parent, then just trust the
2793 * given MDT-object as its parent. So update the OST-object filter_fid. */
2794 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2795 struct lfsck_component *com,
2796 struct lfsck_layout_req *llr,
2797 const struct lu_attr *pla)
2799 struct lfsck_thread_info *info = lfsck_env_info(env);
2800 struct filter_fid *pfid = &info->lti_new_pfid;
2801 struct lu_attr *tla = &info->lti_la3;
2802 struct dt_object *parent = llr->llr_parent->llo_obj;
2803 struct dt_object *child = llr->llr_child;
2804 struct dt_device *dev = lfsck_obj2dt_dev(child);
2805 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2806 struct thandle *handle;
2808 struct lustre_handle lh = { 0 };
2812 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2813 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2818 handle = dt_trans_create(env, dev);
2820 GOTO(unlock1, rc = PTR_ERR(handle));
2822 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2823 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2824 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2825 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2826 * parent MDT-object's layout EA. */
2827 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2828 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2830 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2834 tla->la_valid = LA_UID | LA_GID;
2835 tla->la_uid = pla->la_uid;
2836 tla->la_gid = pla->la_gid;
2837 rc = dt_declare_attr_set(env, child, tla, handle);
2841 rc = dt_trans_start(env, dev, handle);
2845 dt_write_lock(env, parent, 0);
2846 if (unlikely(lfsck_is_dead_obj(parent)))
2847 GOTO(unlock2, rc = 1);
2849 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle,
2854 /* Get the latest parent's owner. */
2855 rc = dt_attr_get(env, parent, tla, BYPASS_CAPA);
2859 tla->la_valid = LA_UID | LA_GID;
2860 rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
2865 dt_write_unlock(env, parent);
2868 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2871 lfsck_ibits_unlock(&lh, LCK_EX);
2874 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired unmatched "
2875 "MDT-OST pair for: parent "DFID", child "DFID", OST-index %u, "
2876 "stripe-index %u, owner %u/%u: rc = %d\n",
2877 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
2878 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
2879 pla->la_uid, pla->la_gid, rc);
2884 /* If there are more than one MDT-objects claim as the OST-object's parent,
2885 * and the OST-object only recognizes one of them, then we need to generate
2886 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
2887 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
2888 struct lfsck_component *com,
2889 struct lfsck_layout_req *llr,
2893 struct lfsck_thread_info *info = lfsck_env_info(env);
2894 struct dt_allocation_hint *hint = &info->lti_hint;
2895 struct dt_object_format *dof = &info->lti_dof;
2896 struct dt_device *pdev = com->lc_lfsck->li_next;
2897 struct ost_id *oi = &info->lti_oi;
2898 struct dt_object *parent = llr->llr_parent->llo_obj;
2899 struct dt_device *cdev = lfsck_obj2dt_dev(llr->llr_child);
2900 struct dt_object *child = NULL;
2901 struct lu_device *d = &cdev->dd_lu_dev;
2902 struct lu_object *o = NULL;
2903 struct thandle *handle;
2904 struct lov_mds_md_v1 *lmm;
2905 struct lov_ost_data_v1 *objs;
2906 struct lustre_handle lh = { 0 };
2907 struct lu_buf ea_buf;
2912 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2913 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2918 handle = dt_trans_create(env, pdev);
2920 GOTO(unlock1, rc = PTR_ERR(handle));
2922 o = lu_object_anon(env, d, NULL);
2924 GOTO(stop, rc = PTR_ERR(o));
2926 child = container_of(o, struct dt_object, do_lu);
2927 o = lu_object_locate(o->lo_header, d->ld_type);
2928 if (unlikely(o == NULL))
2929 GOTO(stop, rc = -EINVAL);
2931 child = container_of(o, struct dt_object, do_lu);
2932 la->la_valid = LA_UID | LA_GID;
2933 hint->dah_parent = NULL;
2935 dof->dof_type = DFT_REGULAR;
2936 rc = dt_declare_create(env, child, la, NULL, NULL, handle);
2940 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2941 LU_XATTR_REPLACE, handle);
2945 rc = dt_trans_start(env, pdev, handle);
2949 dt_write_lock(env, parent, 0);
2950 if (unlikely(lfsck_is_dead_obj(parent)))
2951 GOTO(unlock2, rc = 0);
2953 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
2954 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2955 GOTO(unlock2, rc = 0);
2958 /* Someone change layout during the LFSCK, no need to repair then. */
2959 if (le16_to_cpu(lmm->lmm_layout_gen) != llr->llr_parent->llo_gen)
2960 GOTO(unlock2, rc = 0);
2962 rc = dt_create(env, child, la, hint, dof, handle);
2966 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2967 * been verified in lfsck_layout_verify_header() already. If some
2968 * new magic introduced in the future, then layout LFSCK needs to
2969 * be updated also. */
2970 magic = le32_to_cpu(lmm->lmm_magic);
2971 if (magic == LOV_MAGIC_V1) {
2972 objs = &lmm->lmm_objects[0];
2974 LASSERT(magic == LOV_MAGIC_V3);
2975 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2978 lmm->lmm_layout_gen = cpu_to_le16(llr->llr_parent->llo_gen + 1);
2979 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
2980 ostid_cpu_to_le(oi, &objs[llr->llr_lov_idx].l_ost_oi);
2981 objs[llr->llr_lov_idx].l_ost_gen = cpu_to_le32(0);
2982 objs[llr->llr_lov_idx].l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
2983 lfsck_buf_init(&ea_buf, lmm,
2984 lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2986 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV,
2987 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
2989 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2992 dt_write_unlock(env, parent);
2996 lu_object_put(env, &child->do_lu);
2998 dt_trans_stop(env, pdev, handle);
3001 lfsck_ibits_unlock(&lh, LCK_EX);
3004 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired multiple "
3005 "references for: parent "DFID", OST-index %u, stripe-index %u, "
3006 "owner %u/%u: rc = %d\n",
3007 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
3008 llr->llr_ost_idx, llr->llr_lov_idx, la->la_uid, la->la_gid, rc);
3013 /* If the MDT-object and the OST-object have different owner information,
3014 * then trust the MDT-object, because the normal chown/chgrp handle order
3015 * is from MDT to OST, and it is possible that some chown/chgrp operation
3016 * is partly done. */
3017 static int lfsck_layout_repair_owner(const struct lu_env *env,
3018 struct lfsck_component *com,
3019 struct lfsck_layout_req *llr,
3020 struct lu_attr *pla)
3022 struct lfsck_thread_info *info = lfsck_env_info(env);
3023 struct lu_attr *tla = &info->lti_la3;
3024 struct dt_object *parent = llr->llr_parent->llo_obj;
3025 struct dt_object *child = llr->llr_child;
3026 struct dt_device *dev = lfsck_obj2dt_dev(child);
3027 struct thandle *handle;
3031 handle = dt_trans_create(env, dev);
3033 GOTO(log, rc = PTR_ERR(handle));
3035 tla->la_uid = pla->la_uid;
3036 tla->la_gid = pla->la_gid;
3037 tla->la_valid = LA_UID | LA_GID;
3038 rc = dt_declare_attr_set(env, child, tla, handle);
3042 rc = dt_trans_start(env, dev, handle);
3046 /* Use the dt_object lock to serialize with destroy and attr_set. */
3047 dt_read_lock(env, parent, 0);
3048 if (unlikely(lfsck_is_dead_obj(parent)))
3049 GOTO(unlock, rc = 1);
3051 /* Get the latest parent's owner. */
3052 rc = dt_attr_get(env, parent, tla, BYPASS_CAPA);
3056 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3057 if (unlikely(tla->la_uid != pla->la_uid ||
3058 tla->la_gid != pla->la_gid))
3059 GOTO(unlock, rc = 1);
3061 tla->la_valid = LA_UID | LA_GID;
3062 rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
3067 dt_read_unlock(env, parent);
3070 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3073 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired inconsistent "
3074 "file owner for: parent "DFID", child "DFID", OST-index %u, "
3075 "stripe-index %u, owner %u/%u: rc = %d\n",
3076 lfsck_lfsck2name(com->lc_lfsck), PFID(lfsck_dto2fid(parent)),
3077 PFID(lfsck_dto2fid(child)), llr->llr_ost_idx, llr->llr_lov_idx,
3078 pla->la_uid, pla->la_gid, rc);
3083 /* Check whether the OST-object correctly back points to the
3084 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3085 static int lfsck_layout_check_parent(const struct lu_env *env,
3086 struct lfsck_component *com,
3087 struct dt_object *parent,
3088 const struct lu_fid *pfid,
3089 const struct lu_fid *cfid,
3090 const struct lu_attr *pla,
3091 const struct lu_attr *cla,
3092 struct lfsck_layout_req *llr,
3093 struct lu_buf *lov_ea, __u32 idx)
3095 struct lfsck_thread_info *info = lfsck_env_info(env);
3096 struct lu_buf *buf = &info->lti_big_buf;
3097 struct dt_object *tobj;
3098 struct lov_mds_md_v1 *lmm;
3099 struct lov_ost_data_v1 *objs;
3106 if (fid_is_zero(pfid)) {
3107 /* client never wrote. */
3108 if (cla->la_size == 0 && cla->la_blocks == 0) {
3109 if (unlikely(cla->la_uid != pla->la_uid ||
3110 cla->la_gid != pla->la_gid))
3111 RETURN (LLIT_INCONSISTENT_OWNER);
3116 RETURN(LLIT_UNMATCHED_PAIR);
3119 if (unlikely(!fid_is_sane(pfid)))
3120 RETURN(LLIT_UNMATCHED_PAIR);
3122 if (lu_fid_eq(pfid, lu_object_fid(&parent->do_lu))) {
3123 if (llr->llr_lov_idx == idx)
3126 RETURN(LLIT_UNMATCHED_PAIR);
3129 tobj = lfsck_object_find(env, com->lc_lfsck, pfid);
3131 RETURN(PTR_ERR(tobj));
3133 dt_read_lock(env, tobj, 0);
3134 if (dt_object_exists(tobj) == 0 ||
3135 lfsck_is_dead_obj(tobj))
3136 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3138 if (!S_ISREG(lfsck_object_type(tobj)))
3139 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3141 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
3142 * remote one on another MDT. Then check whether the given OST-object
3143 * is in such layout. If yes, it is multiple referenced, otherwise it
3144 * is unmatched referenced case. */
3145 rc = lfsck_layout_get_lovea(env, tobj, buf);
3146 if (rc == 0 || rc == -ENOENT)
3147 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3153 magic = le32_to_cpu(lmm->lmm_magic);
3154 if (magic == LOV_MAGIC_V1) {
3155 objs = &lmm->lmm_objects[0];
3157 LASSERT(magic == LOV_MAGIC_V3);
3158 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3161 count = le16_to_cpu(lmm->lmm_stripe_count);
3162 for (i = 0; i < count; i++, objs++) {
3163 struct lu_fid *tfid = &info->lti_fid2;
3164 struct ost_id *oi = &info->lti_oi;
3167 if (lovea_slot_is_dummy(objs))
3170 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3171 idx2 = le32_to_cpu(objs->l_ost_idx);
3172 rc = ostid_to_fid(tfid, oi, idx2);
3174 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3175 "invalid layout EA at the slot %d, index %u\n",
3176 lfsck_lfsck2name(com->lc_lfsck),
3177 PFID(pfid), i, idx2);
3179 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3182 if (lu_fid_eq(cfid, tfid)) {
3185 GOTO(out, rc = LLIT_MULTIPLE_REFERENCED);
3189 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
3192 dt_read_unlock(env, tobj);
3193 lfsck_object_put(env, tobj);
3198 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
3199 struct lfsck_component *com,
3200 struct lfsck_assistant_req *lar)
3202 struct lfsck_layout_req *llr =
3203 container_of0(lar, struct lfsck_layout_req, llr_lar);
3204 struct lfsck_layout *lo = com->lc_file_ram;
3205 struct lfsck_thread_info *info = lfsck_env_info(env);
3206 struct filter_fid_old *pea = &info->lti_old_pfid;
3207 struct lu_fid *pfid = &info->lti_fid;
3208 struct lu_buf buf = { 0 };
3209 struct dt_object *parent = llr->llr_parent->llo_obj;
3210 struct dt_object *child = llr->llr_child;
3211 struct lu_attr *pla = &info->lti_la;
3212 struct lu_attr *cla = &info->lti_la2;
3213 struct lfsck_instance *lfsck = com->lc_lfsck;
3214 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3215 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
3220 if (unlikely(lfsck_is_dead_obj(parent)))
3223 rc = dt_attr_get(env, parent, pla, BYPASS_CAPA);
3227 rc = dt_attr_get(env, child, cla, BYPASS_CAPA);
3228 if (rc == -ENOENT) {
3229 if (unlikely(lfsck_is_dead_obj(parent)))
3232 type = LLIT_DANGLING;
3239 lfsck_buf_init(&buf, pea, sizeof(struct filter_fid_old));
3240 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID, BYPASS_CAPA);
3241 if (unlikely(rc >= 0 && rc != sizeof(struct filter_fid_old) &&
3242 rc != sizeof(struct filter_fid))) {
3243 type = LLIT_UNMATCHED_PAIR;
3247 if (rc < 0 && rc != -ENODATA)
3250 if (rc == -ENODATA) {
3253 fid_le_to_cpu(pfid, &pea->ff_parent);
3254 /* Currently, the filter_fid::ff_parent::f_ver is not the
3255 * real parent MDT-object's FID::f_ver, instead it is the
3256 * OST-object index in its parent MDT-object's layout EA. */
3257 idx = pfid->f_stripe_idx;
3261 rc = lfsck_layout_check_parent(env, com, parent, pfid,
3262 lu_object_fid(&child->do_lu),
3263 pla, cla, llr, &buf, idx);
3272 if (unlikely(cla->la_uid != pla->la_uid ||
3273 cla->la_gid != pla->la_gid)) {
3274 type = LLIT_INCONSISTENT_OWNER;
3279 if (bk->lb_param & LPF_DRYRUN) {
3280 if (type != LLIT_NONE)
3288 rc = lfsck_layout_repair_dangling(env, com, llr, pla);
3290 case LLIT_UNMATCHED_PAIR:
3291 rc = lfsck_layout_repair_unmatched_pair(env, com, llr, pla);
3293 case LLIT_MULTIPLE_REFERENCED:
3294 rc = lfsck_layout_repair_multiple_references(env, com, llr,
3297 case LLIT_INCONSISTENT_OWNER:
3298 rc = lfsck_layout_repair_owner(env, com, llr, pla);
3308 down_write(&com->lc_sem);
3310 struct lfsck_assistant_data *lad = com->lc_data;
3312 if (unlikely(lad->lad_exit)) {
3314 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
3315 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
3316 rc == -EHOSTUNREACH) {
3317 /* If cannot touch the target server,
3318 * mark the LFSCK as INCOMPLETE. */
3319 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
3320 "talk with OST %x: rc = %d\n",
3321 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
3322 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
3323 lo->ll_objs_skipped++;
3326 lfsck_layout_record_failure(env, lfsck, lo);
3328 } else if (rc > 0) {
3329 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
3330 "unknown type = %d\n", type);
3332 lo->ll_objs_repaired[type - 1]++;
3333 if (bk->lb_param & LPF_DRYRUN &&
3334 unlikely(lo->ll_pos_first_inconsistent == 0))
3335 lo->ll_pos_first_inconsistent =
3336 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
3339 up_write(&com->lc_sem);
3344 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
3345 struct lfsck_component *com)
3347 struct lfsck_assistant_data *lad = com->lc_data;
3348 struct lfsck_instance *lfsck = com->lc_lfsck;
3349 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3350 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
3351 struct lfsck_tgt_desc *ltd;
3355 spin_lock(<ds->ltd_lock);
3356 while (!list_empty(&lad->lad_ost_phase2_list)) {
3357 ltd = list_entry(lad->lad_ost_phase2_list.next,
3358 struct lfsck_tgt_desc,
3359 ltd_layout_phase_list);
3360 list_del_init(<d->ltd_layout_phase_list);
3361 if (bk->lb_param & LPF_ALL_TGT) {
3362 spin_unlock(<ds->ltd_lock);
3363 rc = lfsck_layout_scan_orphan(env, com, ltd);
3364 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3367 if (unlikely(lad->lad_exit ||
3368 !thread_is_running(&lfsck->li_thread)))
3370 spin_lock(<ds->ltd_lock);
3374 if (list_empty(&lad->lad_ost_phase1_list))
3378 spin_unlock(<ds->ltd_lock);
3384 lfsck_layout_slave_async_interpret(const struct lu_env *env,
3385 struct ptlrpc_request *req,
3388 struct lfsck_layout_slave_async_args *llsaa = args;
3389 struct obd_export *exp = llsaa->llsaa_exp;
3390 struct lfsck_component *com = llsaa->llsaa_com;
3391 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
3392 struct lfsck_layout_slave_data *llsd = com->lc_data;
3393 struct lfsck_reply *lr = NULL;
3397 /* It is quite probably caused by target crash,
3398 * to make the LFSCK can go ahead, assume that
3399 * the target finished the LFSCK prcoessing. */
3402 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
3403 if (lr->lr_status != LS_SCANNING_PHASE1 &&
3404 lr->lr_status != LS_SCANNING_PHASE2)
3409 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
3410 "status %d\n", lfsck_lfsck2name(com->lc_lfsck),
3411 llst->llst_index, lr != NULL ? lr->lr_status : rc);
3413 lfsck_layout_llst_del(llsd, llst);
3416 lfsck_layout_llst_put(llst);
3417 lfsck_component_put(env, com);
3418 class_export_put(exp);
3423 static int lfsck_layout_async_query(const struct lu_env *env,
3424 struct lfsck_component *com,
3425 struct obd_export *exp,
3426 struct lfsck_layout_slave_target *llst,
3427 struct lfsck_request *lr,
3428 struct ptlrpc_request_set *set)
3430 struct lfsck_layout_slave_async_args *llsaa;
3431 struct ptlrpc_request *req;
3432 struct lfsck_request *tmp;
3436 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
3440 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
3442 ptlrpc_request_free(req);
3446 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3448 ptlrpc_request_set_replen(req);
3450 llsaa = ptlrpc_req_async_args(req);
3451 llsaa->llsaa_exp = exp;
3452 llsaa->llsaa_com = lfsck_component_get(com);
3453 llsaa->llsaa_llst = llst;
3454 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
3455 ptlrpc_set_add_req(set, req);
3460 static int lfsck_layout_async_notify(const struct lu_env *env,
3461 struct obd_export *exp,
3462 struct lfsck_request *lr,
3463 struct ptlrpc_request_set *set)
3465 struct ptlrpc_request *req;
3466 struct lfsck_request *tmp;
3470 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3474 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3476 ptlrpc_request_free(req);
3480 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3482 ptlrpc_request_set_replen(req);
3483 ptlrpc_set_add_req(set, req);
3489 lfsck_layout_slave_query_master(const struct lu_env *env,
3490 struct lfsck_component *com)
3492 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3493 struct lfsck_instance *lfsck = com->lc_lfsck;
3494 struct lfsck_layout_slave_data *llsd = com->lc_data;
3495 struct lfsck_layout_slave_target *llst;
3496 struct obd_export *exp;
3497 struct ptlrpc_request_set *set;
3502 set = ptlrpc_prep_set();
3504 GOTO(log, rc = -ENOMEM);
3506 memset(lr, 0, sizeof(*lr));
3507 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
3508 lr->lr_event = LE_QUERY;
3509 lr->lr_active = LFSCK_TYPE_LAYOUT;
3511 llsd->llsd_touch_gen++;
3512 spin_lock(&llsd->llsd_lock);
3513 while (!list_empty(&llsd->llsd_master_list)) {
3514 llst = list_entry(llsd->llsd_master_list.next,
3515 struct lfsck_layout_slave_target,
3517 if (llst->llst_gen == llsd->llsd_touch_gen)
3520 llst->llst_gen = llsd->llsd_touch_gen;
3521 list_move_tail(&llst->llst_list,
3522 &llsd->llsd_master_list);
3523 atomic_inc(&llst->llst_ref);
3524 spin_unlock(&llsd->llsd_lock);
3526 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3529 lfsck_layout_llst_del(llsd, llst);
3530 lfsck_layout_llst_put(llst);
3531 spin_lock(&llsd->llsd_lock);
3535 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
3537 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3538 "query %s for layout: rc = %d\n",
3539 lfsck_lfsck2name(lfsck),
3540 exp->exp_obd->obd_name, rc);
3543 lfsck_layout_llst_put(llst);
3544 class_export_put(exp);
3546 spin_lock(&llsd->llsd_lock);
3548 spin_unlock(&llsd->llsd_lock);
3550 rc = ptlrpc_set_wait(set);
3551 ptlrpc_set_destroy(set);
3553 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
3556 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
3557 lfsck_lfsck2name(com->lc_lfsck), rc);
3563 lfsck_layout_slave_notify_master(const struct lu_env *env,
3564 struct lfsck_component *com,
3565 enum lfsck_events event, int result)
3567 struct lfsck_layout *lo = com->lc_file_ram;
3568 struct lfsck_instance *lfsck = com->lc_lfsck;
3569 struct lfsck_layout_slave_data *llsd = com->lc_data;
3570 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
3571 struct lfsck_layout_slave_target *llst;
3572 struct obd_export *exp;
3573 struct ptlrpc_request_set *set;
3577 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
3578 lfsck_lfsck2name(com->lc_lfsck));
3580 set = ptlrpc_prep_set();
3584 memset(lr, 0, sizeof(*lr));
3585 lr->lr_event = event;
3586 lr->lr_flags = LEF_FROM_OST;
3587 lr->lr_status = result;
3588 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
3589 lr->lr_active = LFSCK_TYPE_LAYOUT;
3590 lr->lr_flags2 = lo->ll_flags;
3591 llsd->llsd_touch_gen++;
3592 spin_lock(&llsd->llsd_lock);
3593 while (!list_empty(&llsd->llsd_master_list)) {
3594 llst = list_entry(llsd->llsd_master_list.next,
3595 struct lfsck_layout_slave_target,
3597 if (llst->llst_gen == llsd->llsd_touch_gen)
3600 llst->llst_gen = llsd->llsd_touch_gen;
3601 list_move_tail(&llst->llst_list,
3602 &llsd->llsd_master_list);
3603 atomic_inc(&llst->llst_ref);
3604 spin_unlock(&llsd->llsd_lock);
3606 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
3609 lfsck_layout_llst_del(llsd, llst);
3610 lfsck_layout_llst_put(llst);
3611 spin_lock(&llsd->llsd_lock);
3615 rc = lfsck_layout_async_notify(env, exp, lr, set);
3617 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
3618 "notify %s for layout: rc = %d\n",
3619 lfsck_lfsck2name(lfsck),
3620 exp->exp_obd->obd_name, rc);
3622 lfsck_layout_llst_put(llst);
3623 class_export_put(exp);
3624 spin_lock(&llsd->llsd_lock);
3626 spin_unlock(&llsd->llsd_lock);
3628 ptlrpc_set_wait(set);
3629 ptlrpc_set_destroy(set);
3635 * \ret -ENODATA: unrecognized stripe
3636 * \ret = 0 : recognized stripe
3637 * \ret < 0 : other failures
3639 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
3640 struct lfsck_component *com,
3641 struct lu_fid *cfid,
3642 struct lu_fid *pfid)
3644 struct lfsck_thread_info *info = lfsck_env_info(env);
3645 struct lu_buf *buf = &info->lti_big_buf;
3646 struct ost_id *oi = &info->lti_oi;
3647 struct dt_object *obj;
3648 struct lov_mds_md_v1 *lmm;
3649 struct lov_ost_data_v1 *objs;
3650 __u32 idx = pfid->f_stripe_idx;
3658 obj = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3660 RETURN(PTR_ERR(obj));
3662 dt_read_lock(env, obj, 0);
3663 if (unlikely(dt_object_exists(obj) == 0 ||
3664 lfsck_is_dead_obj(obj)))
3665 GOTO(unlock, rc = -ENOENT);
3667 if (!S_ISREG(lfsck_object_type(obj)))
3668 GOTO(unlock, rc = -ENODATA);
3670 rc = lfsck_layout_get_lovea(env, obj, buf);
3675 GOTO(unlock, rc = -ENODATA);
3678 rc = lfsck_layout_verify_header(lmm);
3682 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
3683 * been verified in lfsck_layout_verify_header() already. If some
3684 * new magic introduced in the future, then layout LFSCK needs to
3685 * be updated also. */
3686 magic = le32_to_cpu(lmm->lmm_magic);
3687 if (magic == LOV_MAGIC_V1) {
3688 objs = &lmm->lmm_objects[0];
3690 LASSERT(magic == LOV_MAGIC_V3);
3691 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3694 fid_to_ostid(cfid, oi);
3695 count = le16_to_cpu(lmm->lmm_stripe_count);
3696 for (i = 0; i < count; i++, objs++) {
3699 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
3700 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
3701 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
3704 GOTO(unlock, rc = -ENODATA);
3707 dt_read_unlock(env, obj);
3708 lu_object_put(env, &obj->do_lu);
3714 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
3715 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
3716 * layout EA from MDT to OST. On one hand, the OST no need to understand
3717 * the layout EA structure; on the other hand, it may cause trouble when
3718 * transfer large layout EA from MDT to OST via normal OUT RPC.
3720 * \ret > 0: unrecognized stripe
3721 * \ret = 0: recognized stripe
3722 * \ret < 0: other failures
3724 static int lfsck_layout_slave_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_instance *lfsck = com->lc_lfsck;
3730 struct obd_device *obd = lfsck->li_obd;
3731 struct seq_server_site *ss =
3732 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
3733 struct obd_export *exp = NULL;
3734 struct ptlrpc_request *req = NULL;
3735 struct lfsck_request *lr;
3736 struct lu_seq_range range = { 0 };
3740 if (unlikely(fid_is_idif(pfid)))
3743 fld_range_set_any(&range);
3744 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), &range);
3746 RETURN(rc == -ENOENT ? 1 : rc);
3748 if (unlikely(!fld_range_is_mdt(&range)))
3751 exp = lustre_find_lwp_by_index(obd->obd_name, range.lsr_index);
3752 if (unlikely(exp == NULL))
3755 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
3756 GOTO(out, rc = -EOPNOTSUPP);
3758 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
3760 GOTO(out, rc = -ENOMEM);
3762 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
3764 ptlrpc_request_free(req);
3769 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
3770 memset(lr, 0, sizeof(*lr));
3771 lr->lr_event = LE_PAIRS_VERIFY;
3772 lr->lr_active = LFSCK_TYPE_LAYOUT;
3773 lr->lr_fid = *cfid; /* OST-object itself FID. */
3774 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
3776 ptlrpc_request_set_replen(req);
3777 rc = ptlrpc_queue_wait(req);
3778 ptlrpc_req_finished(req);
3780 if (rc == -ENOENT || rc == -ENODATA)
3787 class_export_put(exp);
3792 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
3793 struct lfsck_component *com,
3794 struct lfsck_request *lr)
3796 struct lfsck_thread_info *info = lfsck_env_info(env);
3797 struct filter_fid *ff = &info->lti_new_pfid;
3799 struct dt_device *dev = com->lc_lfsck->li_bottom;
3800 struct dt_object *obj;
3801 struct thandle *th = NULL;
3805 obj = lfsck_object_find_by_dev(env, dev, &lr->lr_fid);
3807 GOTO(log, rc = PTR_ERR(obj));
3809 fid_cpu_to_le(&ff->ff_parent, &lr->lr_fid2);
3810 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3811 dt_write_lock(env, obj, 0);
3812 if (unlikely(dt_object_exists(obj) == 0 ||
3813 lfsck_is_dead_obj(obj)))
3814 GOTO(unlock, rc = 0);
3816 th = dt_trans_create(env, dev);
3818 GOTO(unlock, rc = PTR_ERR(th));
3820 rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_FID, 0, th);
3824 rc = dt_trans_start_local(env, dev, th);
3828 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_FID, 0, th, BYPASS_CAPA);
3833 dt_trans_stop(env, dev, th);
3836 dt_write_unlock(env, obj);
3837 lu_object_put(env, &obj->do_lu);
3840 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
3841 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
3842 PFID(&lr->lr_fid), PFID(&lr->lr_fid2), rc);
3849 static int lfsck_layout_reset(const struct lu_env *env,
3850 struct lfsck_component *com, bool init)
3852 struct lfsck_layout *lo = com->lc_file_ram;
3855 down_write(&com->lc_sem);
3857 memset(lo, 0, com->lc_file_size);
3859 __u32 count = lo->ll_success_count;
3860 __u64 last_time = lo->ll_time_last_complete;
3862 memset(lo, 0, com->lc_file_size);
3863 lo->ll_success_count = count;
3864 lo->ll_time_last_complete = last_time;
3867 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
3868 lo->ll_status = LS_INIT;
3870 if (com->lc_lfsck->li_master) {
3871 struct lfsck_assistant_data *lad = com->lc_data;
3873 lad->lad_incomplete = 0;
3874 CFS_RESET_BITMAP(lad->lad_bitmap);
3877 rc = lfsck_layout_store(env, com);
3878 up_write(&com->lc_sem);
3880 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
3881 lfsck_lfsck2name(com->lc_lfsck), rc);
3886 static void lfsck_layout_fail(const struct lu_env *env,
3887 struct lfsck_component *com, bool new_checked)
3889 struct lfsck_layout *lo = com->lc_file_ram;
3891 down_write(&com->lc_sem);
3893 com->lc_new_checked++;
3894 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
3895 up_write(&com->lc_sem);
3898 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
3899 struct lfsck_component *com, bool init)
3901 struct lfsck_instance *lfsck = com->lc_lfsck;
3902 struct lfsck_layout *lo = com->lc_file_ram;
3906 rc = lfsck_checkpoint_generic(env, com);
3908 return rc > 0 ? 0 : rc;
3911 down_write(&com->lc_sem);
3913 lo->ll_pos_latest_start =
3914 lfsck->li_pos_checkpoint.lp_oit_cookie;
3916 lo->ll_pos_last_checkpoint =
3917 lfsck->li_pos_checkpoint.lp_oit_cookie;
3918 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3919 HALF_SEC - lfsck->li_time_last_checkpoint);
3920 lo->ll_time_last_checkpoint = cfs_time_current_sec();
3921 lo->ll_objs_checked_phase1 += com->lc_new_checked;
3922 com->lc_new_checked = 0;
3925 rc = lfsck_layout_store(env, com);
3926 up_write(&com->lc_sem);
3928 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
3929 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3930 lfsck->li_pos_current.lp_oit_cookie, rc);
3935 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
3936 struct lfsck_component *com, bool init)
3938 struct lfsck_instance *lfsck = com->lc_lfsck;
3939 struct lfsck_layout *lo = com->lc_file_ram;
3942 if (com->lc_new_checked == 0 && !init)
3945 down_write(&com->lc_sem);
3947 lo->ll_pos_latest_start =
3948 lfsck->li_pos_checkpoint.lp_oit_cookie;
3950 lo->ll_pos_last_checkpoint =
3951 lfsck->li_pos_checkpoint.lp_oit_cookie;
3952 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3953 HALF_SEC - lfsck->li_time_last_checkpoint);
3954 lo->ll_time_last_checkpoint = cfs_time_current_sec();
3955 lo->ll_objs_checked_phase1 += com->lc_new_checked;
3956 com->lc_new_checked = 0;
3959 rc = lfsck_layout_store(env, com);
3960 up_write(&com->lc_sem);
3962 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
3963 LPU64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3964 lfsck->li_pos_current.lp_oit_cookie, rc);
3969 static int lfsck_layout_prep(const struct lu_env *env,
3970 struct lfsck_component *com,
3971 struct lfsck_start *start)
3973 struct lfsck_instance *lfsck = com->lc_lfsck;
3974 struct lfsck_layout *lo = com->lc_file_ram;
3975 struct lfsck_position *pos = &com->lc_pos_start;
3977 fid_zero(&pos->lp_dir_parent);
3978 pos->lp_dir_cookie = 0;
3979 if (lo->ll_status == LS_COMPLETED ||
3980 lo->ll_status == LS_PARTIAL ||
3981 /* To handle orphan, must scan from the beginning. */
3982 (start != NULL && start->ls_flags & LPF_ORPHAN)) {
3985 rc = lfsck_layout_reset(env, com, false);
3987 rc = lfsck_set_param(env, lfsck, start, true);
3990 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
3991 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3997 down_write(&com->lc_sem);
3998 lo->ll_time_latest_start = cfs_time_current_sec();
3999 spin_lock(&lfsck->li_lock);
4000 if (lo->ll_flags & LF_SCANNED_ONCE) {
4001 if (!lfsck->li_drop_dryrun ||
4002 lo->ll_pos_first_inconsistent == 0) {
4003 lo->ll_status = LS_SCANNING_PHASE2;
4004 list_move_tail(&com->lc_link,
4005 &lfsck->li_list_double_scan);
4006 pos->lp_oit_cookie = 0;
4010 lo->ll_status = LS_SCANNING_PHASE1;
4011 lo->ll_run_time_phase1 = 0;
4012 lo->ll_run_time_phase2 = 0;
4013 lo->ll_objs_checked_phase1 = 0;
4014 lo->ll_objs_checked_phase2 = 0;
4015 lo->ll_objs_failed_phase1 = 0;
4016 lo->ll_objs_failed_phase2 = 0;
4017 for (i = 0; i < LLIT_MAX; i++)
4018 lo->ll_objs_repaired[i] = 0;
4020 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4021 fid_zero(&com->lc_fid_latest_scanned_phase2);
4024 lo->ll_status = LS_SCANNING_PHASE1;
4025 if (!lfsck->li_drop_dryrun ||
4026 lo->ll_pos_first_inconsistent == 0)
4027 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
4029 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
4031 spin_unlock(&lfsck->li_lock);
4032 up_write(&com->lc_sem);
4037 static int lfsck_layout_slave_prep(const struct lu_env *env,
4038 struct lfsck_component *com,
4039 struct lfsck_start_param *lsp)
4041 struct lfsck_layout_slave_data *llsd = com->lc_data;
4042 struct lfsck_instance *lfsck = com->lc_lfsck;
4043 struct lfsck_layout *lo = com->lc_file_ram;
4044 struct lfsck_start *start = lsp->lsp_start;
4047 rc = lfsck_layout_prep(env, com, start);
4051 if (lo->ll_flags & LF_CRASHED_LASTID &&
4052 list_empty(&llsd->llsd_master_list)) {
4053 LASSERT(lfsck->li_out_notify != NULL);
4055 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4056 LE_LASTID_REBUILDING);
4059 if (!lsp->lsp_index_valid)
4062 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
4063 if (rc == 0 && start != NULL && start->ls_flags & LPF_ORPHAN) {
4064 LASSERT(!llsd->llsd_rbtree_valid);
4066 write_lock(&llsd->llsd_rb_lock);
4067 rc = lfsck_rbtree_setup(env, com);
4068 write_unlock(&llsd->llsd_rb_lock);
4071 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
4072 LPU64"]\n", lfsck_lfsck2name(lfsck),
4073 com->lc_pos_start.lp_oit_cookie);
4078 static int lfsck_layout_master_prep(const struct lu_env *env,
4079 struct lfsck_component *com,
4080 struct lfsck_start_param *lsp)
4085 rc = lfsck_layout_load_bitmap(env, com);
4087 rc = lfsck_layout_reset(env, com, false);
4089 rc = lfsck_set_param(env, com->lc_lfsck,
4090 lsp->lsp_start, true);
4096 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
4100 rc = lfsck_start_assistant(env, com, lsp);
4105 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
4106 LPU64"\n", lfsck_lfsck2name(com->lc_lfsck),
4107 com->lc_pos_start.lp_oit_cookie);
4112 /* Pre-fetch the attribute for each stripe in the given layout EA. */
4113 static int lfsck_layout_scan_stripes(const struct lu_env *env,
4114 struct lfsck_component *com,
4115 struct dt_object *parent,
4116 struct lov_mds_md_v1 *lmm)
4118 struct lfsck_thread_info *info = lfsck_env_info(env);
4119 struct lfsck_instance *lfsck = com->lc_lfsck;
4120 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4121 struct lfsck_layout *lo = com->lc_file_ram;
4122 struct lfsck_assistant_data *lad = com->lc_data;
4123 struct lfsck_layout_object *llo = NULL;
4124 struct lov_ost_data_v1 *objs;
4125 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4126 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4127 struct ptlrpc_thread *athread = &lad->lad_thread;
4128 struct l_wait_info lwi = { 0 };
4137 lfsck_buf_init(&buf, &info->lti_old_pfid,
4138 sizeof(struct filter_fid_old));
4139 count = le16_to_cpu(lmm->lmm_stripe_count);
4140 gen = le16_to_cpu(lmm->lmm_layout_gen);
4141 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
4142 * been verified in lfsck_layout_verify_header() already. If some
4143 * new magic introduced in the future, then layout LFSCK needs to
4144 * be updated also. */
4145 magic = le32_to_cpu(lmm->lmm_magic);
4146 if (magic == LOV_MAGIC_V1) {
4147 objs = &lmm->lmm_objects[0];
4149 LASSERT(magic == LOV_MAGIC_V3);
4150 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4153 for (i = 0; i < count; i++, objs++) {
4154 struct lu_fid *fid = &info->lti_fid;
4155 struct ost_id *oi = &info->lti_oi;
4156 struct lfsck_layout_req *llr;
4157 struct lfsck_tgt_desc *tgt = NULL;
4158 struct dt_object *cobj = NULL;
4160 bool wakeup = false;
4162 if (unlikely(lovea_slot_is_dummy(objs)))
4165 l_wait_event(mthread->t_ctl_waitq,
4166 bk->lb_async_windows == 0 ||
4167 lad->lad_prefetched < bk->lb_async_windows ||
4168 !thread_is_running(mthread) ||
4169 thread_is_stopped(athread),
4172 if (unlikely(!thread_is_running(mthread)) ||
4173 thread_is_stopped(athread))
4176 if (unlikely(lfsck_is_dead_obj(parent)))
4179 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4180 index = le32_to_cpu(objs->l_ost_idx);
4181 rc = ostid_to_fid(fid, oi, index);
4183 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
4184 ": "DOSTID", idx:%u\n", lfsck_lfsck2name(lfsck),
4185 PFID(lfsck_dto2fid(parent)), POSTID(oi), index);
4189 tgt = lfsck_tgt_get(ltds, index);
4190 if (unlikely(tgt == NULL)) {
4191 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
4192 "did not join the layout LFSCK\n",
4193 lfsck_lfsck2name(lfsck), index);
4194 lfsck_lad_set_bitmap(env, com, index);
4198 /* There is potential deadlock race condition between object
4199 * destroy and layout LFSCK. Consider the following scenario:
4201 * 1) The LFSCK thread obtained the parent object firstly, at
4202 * that time, the parent object has not been destroyed yet.
4204 * 2) One RPC service thread destroyed the parent and all its
4205 * children objects. Because the LFSCK is referencing the
4206 * parent object, then the parent object will be marked as
4207 * dying in RAM. On the other hand, the parent object is
4208 * referencing all its children objects, then all children
4209 * objects will be marked as dying in RAM also.
4211 * 3) The LFSCK thread tries to find some child object with
4212 * the parent object referenced. Then it will find that the
4213 * child object is dying. According to the object visibility
4214 * rules: the object with dying flag cannot be returned to
4215 * others. So the LFSCK thread has to wait until the dying
4216 * object has been purged from RAM, then it can allocate a
4217 * new object (with the same FID) in RAM. Unfortunately, the
4218 * LFSCK thread itself is referencing the parent object, and
4219 * cause the parent object cannot be purged, then cause the
4220 * child object cannot be purged also. So the LFSCK thread
4221 * will fall into deadlock.
4223 * We introduce non-blocked version lu_object_find() to allow
4224 * the LFSCK thread to return failure immediately (instead of
4225 * wait) when it finds dying (child) object, then the LFSCK
4226 * thread can check whether the parent object is dying or not.
4227 * So avoid above deadlock. LU-5395 */
4228 cobj = lfsck_object_find_by_dev_nowait(env, tgt->ltd_tgt, fid);
4230 if (lfsck_is_dead_obj(parent)) {
4240 rc = dt_declare_attr_get(env, cobj, BYPASS_CAPA);
4244 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID,
4250 llo = lfsck_layout_object_init(env, parent,
4251 lfsck->li_pos_current.lp_oit_cookie, gen);
4258 llr = lfsck_layout_assistant_req_init(llo, cobj, index, i);
4265 spin_lock(&lad->lad_lock);
4266 if (lad->lad_assistant_status < 0) {
4267 spin_unlock(&lad->lad_lock);
4268 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
4270 RETURN(lad->lad_assistant_status);
4273 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
4274 if (lad->lad_prefetched == 0)
4277 lad->lad_prefetched++;
4278 spin_unlock(&lad->lad_lock);
4280 wake_up_all(&athread->t_ctl_waitq);
4283 down_write(&com->lc_sem);
4284 com->lc_new_checked++;
4286 lfsck_layout_record_failure(env, lfsck, lo);
4287 up_write(&com->lc_sem);
4289 if (cobj != NULL && !IS_ERR(cobj))
4290 lu_object_put(env, &cobj->do_lu);
4292 if (likely(tgt != NULL))
4295 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4302 if (llo != NULL && !IS_ERR(llo))
4303 lfsck_layout_object_put(env, llo);
4308 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
4309 * the OST-object's attribute and generate an structure lfsck_layout_req on the
4310 * list ::lad_req_list.
4312 * For each request on above list, the lfsck_layout_assistant thread compares
4313 * the OST side attribute with local attribute, if inconsistent, then repair it.
4315 * All above processing is async mode with pipeline. */
4316 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
4317 struct lfsck_component *com,
4318 struct dt_object *obj)
4320 struct lfsck_thread_info *info = lfsck_env_info(env);
4321 struct ost_id *oi = &info->lti_oi;
4322 struct lfsck_layout *lo = com->lc_file_ram;
4323 struct lfsck_assistant_data *lad = com->lc_data;
4324 struct lfsck_instance *lfsck = com->lc_lfsck;
4325 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4326 struct thandle *handle = NULL;
4327 struct lu_buf *buf = &info->lti_big_buf;
4328 struct lov_mds_md_v1 *lmm = NULL;
4329 struct dt_device *dev = lfsck->li_bottom;
4330 struct lustre_handle lh = { 0 };
4331 struct lu_buf ea_buf = { 0 };
4334 bool locked = false;
4335 bool stripe = false;
4336 bool bad_oi = false;
4339 if (!S_ISREG(lfsck_object_type(obj)))
4342 if (lad->lad_assistant_status < 0)
4343 GOTO(out, rc = -ESRCH);
4345 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
4346 lmm_oi_cpu_to_le(oi, oi);
4347 dt_read_lock(env, obj, 0);
4351 if (dt_object_exists(obj) == 0 ||
4352 lfsck_is_dead_obj(obj))
4355 rc = lfsck_layout_get_lovea(env, obj, buf);
4361 rc = lfsck_layout_verify_header(lmm);
4362 /* If the LOV EA crashed, then it is possible to be rebuilt later
4363 * when handle orphan OST-objects. */
4367 if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0)
4368 GOTO(out, stripe = true);
4370 /* Inconsistent lmm_oi, should be repaired. */
4374 if (bk->lb_param & LPF_DRYRUN) {
4375 down_write(&com->lc_sem);
4376 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4377 up_write(&com->lc_sem);
4379 GOTO(out, stripe = true);
4382 if (!lustre_handle_is_used(&lh)) {
4383 dt_read_unlock(env, obj);
4385 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4386 MDS_INODELOCK_LAYOUT |
4387 MDS_INODELOCK_XATTR, LCK_EX);
4391 handle = dt_trans_create(env, dev);
4393 GOTO(out, rc = PTR_ERR(handle));
4395 lfsck_buf_init(&ea_buf, lmm, size);
4396 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4397 LU_XATTR_REPLACE, handle);
4401 rc = dt_trans_start_local(env, dev, handle);
4405 dt_write_lock(env, obj, 0);
4411 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
4412 LU_XATTR_REPLACE, handle, BYPASS_CAPA);
4416 down_write(&com->lc_sem);
4417 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4418 up_write(&com->lc_sem);
4420 GOTO(out, stripe = true);
4424 if (lustre_handle_is_used(&lh))
4425 dt_write_unlock(env, obj);
4427 dt_read_unlock(env, obj);
4430 if (handle != NULL && !IS_ERR(handle))
4431 dt_trans_stop(env, dev, handle);
4433 lfsck_ibits_unlock(&lh, LCK_EX);
4436 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
4437 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
4438 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
4439 PFID(lfsck_dto2fid(obj)), rc);
4442 rc = lfsck_layout_scan_stripes(env, com, obj, lmm);
4444 down_write(&com->lc_sem);
4445 com->lc_new_checked++;
4447 lfsck_layout_record_failure(env, lfsck, lo);
4448 up_write(&com->lc_sem);
4454 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
4455 struct lfsck_component *com,
4456 struct dt_object *obj)
4458 struct lfsck_instance *lfsck = com->lc_lfsck;
4459 struct lfsck_layout *lo = com->lc_file_ram;
4460 const struct lu_fid *fid = lfsck_dto2fid(obj);
4461 struct lfsck_layout_slave_data *llsd = com->lc_data;
4462 struct lfsck_layout_seq *lls;
4468 LASSERT(llsd != NULL);
4470 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
4471 cfs_fail_val == lfsck_dev_idx(lfsck->li_bottom)) {
4472 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(1),
4474 struct ptlrpc_thread *thread = &lfsck->li_thread;
4476 l_wait_event(thread->t_ctl_waitq,
4477 !thread_is_running(thread),
4481 lfsck_rbtree_update_bitmap(env, com, fid, false);
4483 down_write(&com->lc_sem);
4484 if (fid_is_idif(fid))
4486 else if (!fid_is_norm(fid) ||
4487 !fid_is_for_ostobj(env, lfsck->li_next, obj, fid))
4488 GOTO(unlock, rc = 0);
4491 com->lc_new_checked++;
4493 lls = lfsck_layout_seq_lookup(llsd, seq);
4496 if (unlikely(lls == NULL))
4497 GOTO(unlock, rc = -ENOMEM);
4499 INIT_LIST_HEAD(&lls->lls_list);
4501 rc = lfsck_layout_lastid_load(env, com, lls);
4503 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4504 "load LAST_ID for "LPX64": rc = %d\n",
4505 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
4506 lo->ll_objs_failed_phase1++;
4511 lfsck_layout_seq_insert(llsd, lls);
4514 if (unlikely(fid_is_last_id(fid)))
4515 GOTO(unlock, rc = 0);
4517 if (fid_is_idif(fid))
4518 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
4522 if (oid > lls->lls_lastid_known)
4523 lls->lls_lastid_known = oid;
4525 if (oid > lls->lls_lastid) {
4526 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
4527 /* OFD may create new objects during LFSCK scanning. */
4528 rc = lfsck_layout_lastid_reload(env, com, lls);
4529 if (unlikely(rc != 0)) {
4530 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
4531 "reload LAST_ID for "LPX64": rc = %d\n",
4532 lfsck_lfsck2name(com->lc_lfsck),
4538 if (oid <= lls->lls_lastid ||
4539 lo->ll_flags & LF_CRASHED_LASTID)
4540 GOTO(unlock, rc = 0);
4542 LASSERT(lfsck->li_out_notify != NULL);
4544 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4545 LE_LASTID_REBUILDING);
4546 lo->ll_flags |= LF_CRASHED_LASTID;
4548 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
4549 "LAST_ID file (2) for the sequence "LPX64
4550 ", old value "LPU64", known value "LPU64"\n",
4551 lfsck_lfsck2name(lfsck), lls->lls_seq,
4552 lls->lls_lastid, oid);
4555 lls->lls_lastid = oid;
4559 GOTO(unlock, rc = 0);
4562 up_write(&com->lc_sem);
4567 static int lfsck_layout_exec_dir(const struct lu_env *env,
4568 struct lfsck_component *com,
4569 struct lu_dirent *ent, __u16 type)
4574 static int lfsck_layout_master_post(const struct lu_env *env,
4575 struct lfsck_component *com,
4576 int result, bool init)
4578 struct lfsck_instance *lfsck = com->lc_lfsck;
4579 struct lfsck_layout *lo = com->lc_file_ram;
4583 lfsck_post_generic(env, com, &result);
4585 down_write(&com->lc_sem);
4586 spin_lock(&lfsck->li_lock);
4588 lo->ll_pos_last_checkpoint =
4589 lfsck->li_pos_checkpoint.lp_oit_cookie;
4592 if (lo->ll_flags & LF_INCOMPLETE)
4593 lo->ll_status = LS_PARTIAL;
4595 lo->ll_status = LS_SCANNING_PHASE2;
4596 lo->ll_flags |= LF_SCANNED_ONCE;
4597 lo->ll_flags &= ~LF_UPGRADE;
4598 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4599 } else if (result == 0) {
4600 lo->ll_status = lfsck->li_status;
4601 if (lo->ll_status == 0)
4602 lo->ll_status = LS_STOPPED;
4603 if (lo->ll_status != LS_PAUSED) {
4604 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4607 lo->ll_status = LS_FAILED;
4608 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4610 spin_unlock(&lfsck->li_lock);
4613 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4614 HALF_SEC - lfsck->li_time_last_checkpoint);
4615 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4616 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4617 com->lc_new_checked = 0;
4620 rc = lfsck_layout_store(env, com);
4621 up_write(&com->lc_sem);
4623 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
4624 lfsck_lfsck2name(lfsck), rc);
4629 static int lfsck_layout_slave_post(const struct lu_env *env,
4630 struct lfsck_component *com,
4631 int result, bool init)
4633 struct lfsck_instance *lfsck = com->lc_lfsck;
4634 struct lfsck_layout *lo = com->lc_file_ram;
4638 rc = lfsck_layout_lastid_store(env, com);
4642 LASSERT(lfsck->li_out_notify != NULL);
4644 down_write(&com->lc_sem);
4645 spin_lock(&lfsck->li_lock);
4647 lo->ll_pos_last_checkpoint =
4648 lfsck->li_pos_checkpoint.lp_oit_cookie;
4651 if (lo->ll_flags & LF_INCOMPLETE)
4652 lo->ll_status = LS_PARTIAL;
4654 lo->ll_status = LS_SCANNING_PHASE2;
4655 lo->ll_flags |= LF_SCANNED_ONCE;
4656 if (lo->ll_flags & LF_CRASHED_LASTID) {
4658 lo->ll_flags &= ~LF_CRASHED_LASTID;
4660 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
4661 "crashed LAST_ID files successfully\n",
4662 lfsck_lfsck2name(lfsck));
4664 lo->ll_flags &= ~LF_UPGRADE;
4665 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4666 } else if (result == 0) {
4667 lo->ll_status = lfsck->li_status;
4668 if (lo->ll_status == 0)
4669 lo->ll_status = LS_STOPPED;
4670 if (lo->ll_status != LS_PAUSED)
4671 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4673 lo->ll_status = LS_FAILED;
4674 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4676 spin_unlock(&lfsck->li_lock);
4679 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
4683 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4684 HALF_SEC - lfsck->li_time_last_checkpoint);
4685 lo->ll_time_last_checkpoint = cfs_time_current_sec();
4686 lo->ll_objs_checked_phase1 += com->lc_new_checked;
4687 com->lc_new_checked = 0;
4690 rc = lfsck_layout_store(env, com);
4691 up_write(&com->lc_sem);
4693 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
4696 lfsck_rbtree_cleanup(env, com);
4698 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
4699 lfsck_lfsck2name(lfsck), rc);
4704 static int lfsck_layout_dump(const struct lu_env *env,
4705 struct lfsck_component *com, struct seq_file *m)
4707 struct lfsck_instance *lfsck = com->lc_lfsck;
4708 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4709 struct lfsck_layout *lo = com->lc_file_ram;
4712 down_read(&com->lc_sem);
4713 seq_printf(m, "name: lfsck_layout\n"
4719 lfsck_status2names(lo->ll_status));
4721 rc = lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
4725 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4729 rc = lfsck_time_dump(m, lo->ll_time_last_complete,
4730 "time_since_last_completed");
4734 rc = lfsck_time_dump(m, lo->ll_time_latest_start,
4735 "time_since_latest_start");
4739 rc = lfsck_time_dump(m, lo->ll_time_last_checkpoint,
4740 "time_since_last_checkpoint");
4744 seq_printf(m, "latest_start_position: "LPU64"\n"
4745 "last_checkpoint_position: "LPU64"\n"
4746 "first_failure_position: "LPU64"\n",
4747 lo->ll_pos_latest_start,
4748 lo->ll_pos_last_checkpoint,
4749 lo->ll_pos_first_inconsistent);
4751 seq_printf(m, "success_count: %u\n"
4752 "repaired_dangling: "LPU64"\n"
4753 "repaired_unmatched_pair: "LPU64"\n"
4754 "repaired_multiple_referenced: "LPU64"\n"
4755 "repaired_orphan: "LPU64"\n"
4756 "repaired_inconsistent_owner: "LPU64"\n"
4757 "repaired_others: "LPU64"\n"
4758 "skipped: "LPU64"\n"
4759 "failed_phase1: "LPU64"\n"
4760 "failed_phase2: "LPU64"\n",
4761 lo->ll_success_count,
4762 lo->ll_objs_repaired[LLIT_DANGLING - 1],
4763 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
4764 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
4765 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
4766 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
4767 lo->ll_objs_repaired[LLIT_OTHERS - 1],
4768 lo->ll_objs_skipped,
4769 lo->ll_objs_failed_phase1,
4770 lo->ll_objs_failed_phase2);
4772 if (lo->ll_status == LS_SCANNING_PHASE1) {
4774 const struct dt_it_ops *iops;
4775 cfs_duration_t duration = cfs_time_current() -
4776 lfsck->li_time_last_checkpoint;
4777 __u64 checked = lo->ll_objs_checked_phase1 +
4778 com->lc_new_checked;
4779 __u64 speed = checked;
4780 __u64 new_checked = com->lc_new_checked * HZ;
4781 __u32 rtime = lo->ll_run_time_phase1 +
4782 cfs_duration_sec(duration + HALF_SEC);
4785 do_div(new_checked, duration);
4787 do_div(speed, rtime);
4788 seq_printf(m, "checked_phase1: "LPU64"\n"
4789 "checked_phase2: "LPU64"\n"
4790 "run_time_phase1: %u seconds\n"
4791 "run_time_phase2: %u seconds\n"
4792 "average_speed_phase1: "LPU64" items/sec\n"
4793 "average_speed_phase2: N/A\n"
4794 "real-time_speed_phase1: "LPU64" items/sec\n"
4795 "real-time_speed_phase2: N/A\n",
4797 lo->ll_objs_checked_phase2,
4799 lo->ll_run_time_phase2,
4803 LASSERT(lfsck->li_di_oit != NULL);
4805 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4807 /* The low layer otable-based iteration position may NOT
4808 * exactly match the layout-based directory traversal
4809 * cookie. Generally, it is not a serious issue. But the
4810 * caller should NOT make assumption on that. */
4811 pos = iops->store(env, lfsck->li_di_oit);
4812 if (!lfsck->li_current_oit_processed)
4814 seq_printf(m, "current_position: "LPU64"\n", pos);
4816 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
4817 cfs_duration_t duration = cfs_time_current() -
4818 lfsck->li_time_last_checkpoint;
4819 __u64 checked = lo->ll_objs_checked_phase2 +
4820 com->lc_new_checked;
4821 __u64 speed1 = lo->ll_objs_checked_phase1;
4822 __u64 speed2 = checked;
4823 __u64 new_checked = com->lc_new_checked * HZ;
4824 __u32 rtime = lo->ll_run_time_phase2 +
4825 cfs_duration_sec(duration + HALF_SEC);
4828 do_div(new_checked, duration);
4829 if (lo->ll_run_time_phase1 != 0)
4830 do_div(speed1, lo->ll_run_time_phase1);
4832 do_div(speed2, rtime);
4833 rc = seq_printf(m, "checked_phase1: "LPU64"\n"
4834 "checked_phase2: "LPU64"\n"
4835 "run_time_phase1: %u seconds\n"
4836 "run_time_phase2: %u seconds\n"
4837 "average_speed_phase1: "LPU64" items/sec\n"
4838 "average_speed_phase2: "LPU64" items/sec\n"
4839 "real-time_speed_phase1: N/A\n"
4840 "real-time_speed_phase2: "LPU64" items/sec\n"
4841 "current_position: "DFID"\n",
4842 lo->ll_objs_checked_phase1,
4844 lo->ll_run_time_phase1,
4849 PFID(&com->lc_fid_latest_scanned_phase2));
4854 __u64 speed1 = lo->ll_objs_checked_phase1;
4855 __u64 speed2 = lo->ll_objs_checked_phase2;
4857 if (lo->ll_run_time_phase1 != 0)
4858 do_div(speed1, lo->ll_run_time_phase1);
4859 if (lo->ll_run_time_phase2 != 0)
4860 do_div(speed2, lo->ll_run_time_phase2);
4861 seq_printf(m, "checked_phase1: "LPU64"\n"
4862 "checked_phase2: "LPU64"\n"
4863 "run_time_phase1: %u seconds\n"
4864 "run_time_phase2: %u seconds\n"
4865 "average_speed_phase1: "LPU64" items/sec\n"
4866 "average_speed_phase2: "LPU64" objs/sec\n"
4867 "real-time_speed_phase1: N/A\n"
4868 "real-time_speed_phase2: N/A\n"
4869 "current_position: N/A\n",
4870 lo->ll_objs_checked_phase1,
4871 lo->ll_objs_checked_phase2,
4872 lo->ll_run_time_phase1,
4873 lo->ll_run_time_phase2,
4878 up_read(&com->lc_sem);
4883 static int lfsck_layout_master_double_scan(const struct lu_env *env,
4884 struct lfsck_component *com)
4886 struct lfsck_layout *lo = com->lc_file_ram;
4888 return lfsck_double_scan_generic(env, com, lo->ll_status);
4891 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
4892 struct lfsck_component *com)
4894 struct lfsck_instance *lfsck = com->lc_lfsck;
4895 struct lfsck_layout_slave_data *llsd = com->lc_data;
4896 struct lfsck_layout *lo = com->lc_file_ram;
4897 struct ptlrpc_thread *thread = &lfsck->li_thread;
4901 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2)) {
4902 lfsck_rbtree_cleanup(env, com);
4903 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE, 0);
4907 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
4908 lfsck_lfsck2name(lfsck));
4910 atomic_inc(&lfsck->li_double_scan_count);
4912 com->lc_new_checked = 0;
4913 com->lc_new_scanned = 0;
4914 com->lc_time_last_checkpoint = cfs_time_current();
4915 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4916 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4919 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(30),
4922 rc = lfsck_layout_slave_query_master(env, com);
4923 if (list_empty(&llsd->llsd_master_list)) {
4924 if (unlikely(!thread_is_running(thread)))
4935 rc = l_wait_event(thread->t_ctl_waitq,
4936 !thread_is_running(thread) ||
4937 list_empty(&llsd->llsd_master_list),
4939 if (unlikely(!thread_is_running(thread)))
4942 if (rc == -ETIMEDOUT)
4945 GOTO(done, rc = (rc < 0 ? rc : 1));
4949 rc = lfsck_layout_double_scan_result(env, com, rc);
4951 lfsck_rbtree_cleanup(env, com);
4952 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE, rc);
4953 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
4954 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
4956 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
4957 "status %d: rc = %d\n",
4958 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
4963 static void lfsck_layout_master_data_release(const struct lu_env *env,
4964 struct lfsck_component *com)
4966 struct lfsck_assistant_data *lad = com->lc_data;
4967 struct lfsck_instance *lfsck = com->lc_lfsck;
4968 struct lfsck_tgt_descs *ltds;
4969 struct lfsck_tgt_desc *ltd;
4970 struct lfsck_tgt_desc *next;
4972 LASSERT(lad != NULL);
4973 LASSERT(thread_is_init(&lad->lad_thread) ||
4974 thread_is_stopped(&lad->lad_thread));
4975 LASSERT(list_empty(&lad->lad_req_list));
4977 com->lc_data = NULL;
4979 ltds = &lfsck->li_ost_descs;
4980 spin_lock(<ds->ltd_lock);
4981 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
4982 ltd_layout_phase_list) {
4983 list_del_init(<d->ltd_layout_phase_list);
4985 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
4986 ltd_layout_phase_list) {
4987 list_del_init(<d->ltd_layout_phase_list);
4989 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
4991 list_del_init(<d->ltd_layout_list);
4993 spin_unlock(<ds->ltd_lock);
4995 ltds = &lfsck->li_mdt_descs;
4996 spin_lock(<ds->ltd_lock);
4997 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4998 ltd_layout_phase_list) {
4999 list_del_init(<d->ltd_layout_phase_list);
5001 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5002 ltd_layout_phase_list) {
5003 list_del_init(<d->ltd_layout_phase_list);
5005 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
5007 list_del_init(<d->ltd_layout_list);
5009 spin_unlock(<ds->ltd_lock);
5011 CFS_FREE_BITMAP(lad->lad_bitmap);
5016 static void lfsck_layout_slave_data_release(const struct lu_env *env,
5017 struct lfsck_component *com)
5019 struct lfsck_layout_slave_data *llsd = com->lc_data;
5020 struct lfsck_layout_seq *lls;
5021 struct lfsck_layout_seq *next;
5022 struct lfsck_layout_slave_target *llst;
5023 struct lfsck_layout_slave_target *tmp;
5025 LASSERT(llsd != NULL);
5027 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
5029 list_del_init(&lls->lls_list);
5030 lfsck_object_put(env, lls->lls_lastid_obj);
5034 list_for_each_entry_safe(llst, tmp, &llsd->llsd_master_list,
5036 list_del_init(&llst->llst_list);
5040 lfsck_rbtree_cleanup(env, com);
5041 com->lc_data = NULL;
5045 static void lfsck_layout_slave_quit(const struct lu_env *env,
5046 struct lfsck_component *com)
5048 lfsck_rbtree_cleanup(env, com);
5051 static int lfsck_layout_master_in_notify(const struct lu_env *env,
5052 struct lfsck_component *com,
5053 struct lfsck_request *lr)
5055 struct lfsck_instance *lfsck = com->lc_lfsck;
5056 struct lfsck_layout *lo = com->lc_file_ram;
5057 struct lfsck_assistant_data *lad = com->lc_data;
5058 struct lfsck_tgt_descs *ltds;
5059 struct lfsck_tgt_desc *ltd;
5063 if (lr->lr_event == LE_PAIRS_VERIFY) {
5066 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
5072 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
5073 "from %s %x, status %d\n", lfsck_lfsck2name(lfsck),
5074 lr->lr_event, (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
5075 lr->lr_index, lr->lr_status);
5077 if (lr->lr_event != LE_PHASE1_DONE &&
5078 lr->lr_event != LE_PHASE2_DONE &&
5079 lr->lr_event != LE_PEER_EXIT)
5082 if (lr->lr_flags & LEF_FROM_OST)
5083 ltds = &lfsck->li_ost_descs;
5085 ltds = &lfsck->li_mdt_descs;
5086 spin_lock(<ds->ltd_lock);
5087 ltd = LTD_TGT(ltds, lr->lr_index);
5089 spin_unlock(<ds->ltd_lock);
5094 list_del_init(<d->ltd_layout_phase_list);
5095 switch (lr->lr_event) {
5096 case LE_PHASE1_DONE:
5097 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
5098 if (lr->lr_flags2 & LF_INCOMPLETE) {
5099 if (lr->lr_flags & LEF_FROM_OST)
5100 lfsck_lad_set_bitmap(env, com,
5103 lo->ll_flags |= LF_INCOMPLETE;
5105 ltd->ltd_layout_done = 1;
5106 list_del_init(<d->ltd_layout_list);
5111 if (lr->lr_flags & LEF_FROM_OST) {
5112 if (list_empty(<d->ltd_layout_list))
5113 list_add_tail(<d->ltd_layout_list,
5114 &lad->lad_ost_list);
5115 list_add_tail(<d->ltd_layout_phase_list,
5116 &lad->lad_ost_phase2_list);
5118 if (list_empty(<d->ltd_layout_list))
5119 list_add_tail(<d->ltd_layout_list,
5120 &lad->lad_mdt_list);
5121 list_add_tail(<d->ltd_layout_phase_list,
5122 &lad->lad_mdt_phase2_list);
5125 case LE_PHASE2_DONE:
5126 ltd->ltd_layout_done = 1;
5127 list_del_init(<d->ltd_layout_list);
5131 ltd->ltd_layout_done = 1;
5132 list_del_init(<d->ltd_layout_list);
5133 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
5134 !(lr->lr_flags & LEF_FROM_OST))
5135 lo->ll_flags |= LF_INCOMPLETE;
5140 spin_unlock(<ds->ltd_lock);
5142 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5143 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5145 memset(stop, 0, sizeof(*stop));
5146 stop->ls_status = lr->lr_status;
5147 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5148 lfsck_stop(env, lfsck->li_bottom, stop);
5149 } else if (lfsck_phase2_next_ready(lad)) {
5150 wake_up_all(&lad->lad_thread.t_ctl_waitq);
5156 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
5157 struct lfsck_component *com,
5158 struct lfsck_request *lr)
5160 struct lfsck_instance *lfsck = com->lc_lfsck;
5161 struct lfsck_layout_slave_data *llsd = com->lc_data;
5162 struct lfsck_layout_slave_target *llst;
5166 switch (lr->lr_event) {
5167 case LE_FID_ACCESSED:
5168 lfsck_rbtree_update_bitmap(env, com, &lr->lr_fid, true);
5170 case LE_CONDITIONAL_DESTROY:
5171 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
5173 case LE_PAIRS_VERIFY: {
5174 lr->lr_status = LPVS_INIT;
5175 /* Firstly, if the MDT-object which is claimed via OST-object
5176 * local stored PFID xattr recognizes the OST-object, then it
5177 * must be that the client given PFID is wrong. */
5178 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5183 lr->lr_status = LPVS_INCONSISTENT;
5184 /* The OST-object local stored PFID xattr is stale. We need to
5185 * check whether the MDT-object that is claimed via the client
5186 * given PFID information recognizes the OST-object or not. If
5187 * matches, then need to update the OST-object's PFID xattr. */
5188 rc = lfsck_layout_slave_check_pairs(env, com, &lr->lr_fid,
5191 * We are not sure whether the client given PFID information
5192 * is correct or not, do nothing to avoid improper fixing.
5195 * The client given PFID information is also invalid, we can
5196 * NOT fix the OST-object inconsistency.
5201 lr->lr_status = LPVS_INCONSISTENT_TOFIX;
5202 rc = lfsck_layout_slave_repair_pfid(env, com, lr);
5206 case LE_PHASE1_DONE: {
5207 if (lr->lr_flags2 & LF_INCOMPLETE) {
5208 struct lfsck_layout *lo = com->lc_file_ram;
5210 lo->ll_flags |= LF_INCOMPLETE;
5211 llst = lfsck_layout_llst_find_and_del(llsd,
5215 lfsck_layout_llst_put(llst);
5216 if (list_empty(&llsd->llsd_master_list))
5218 &lfsck->li_thread.t_ctl_waitq);
5224 case LE_PHASE2_DONE:
5226 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
5227 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
5228 lr->lr_event, lr->lr_index, lr->lr_status);
5234 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
5238 lfsck_layout_llst_put(llst);
5239 if (list_empty(&llsd->llsd_master_list))
5240 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
5242 if (lr->lr_event == LE_PEER_EXIT &&
5243 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
5244 (list_empty(&llsd->llsd_master_list) &&
5245 (lr->lr_status == LS_STOPPED ||
5246 lr->lr_status == LS_CO_STOPPED)))) {
5247 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5249 memset(stop, 0, sizeof(*stop));
5250 stop->ls_status = lr->lr_status;
5251 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5252 lfsck_stop(env, lfsck->li_bottom, stop);
5258 static int lfsck_layout_query(const struct lu_env *env,
5259 struct lfsck_component *com)
5261 struct lfsck_layout *lo = com->lc_file_ram;
5263 return lo->ll_status;
5266 /* with lfsck::li_lock held */
5267 static int lfsck_layout_slave_join(const struct lu_env *env,
5268 struct lfsck_component *com,
5269 struct lfsck_start_param *lsp)
5271 struct lfsck_instance *lfsck = com->lc_lfsck;
5272 struct lfsck_layout_slave_data *llsd = com->lc_data;
5273 struct lfsck_layout_slave_target *llst;
5274 struct lfsck_start *start = lsp->lsp_start;
5278 if (start == NULL || !(start->ls_flags & LPF_ORPHAN))
5281 if (!lsp->lsp_index_valid)
5284 /* If someone is running the LFSCK without orphan handling,
5285 * it will not maintain the object accessing rbtree. So we
5286 * cannot join it for orphan handling. */
5287 if (!llsd->llsd_rbtree_valid)
5290 spin_unlock(&lfsck->li_lock);
5291 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5292 spin_lock(&lfsck->li_lock);
5293 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
5294 spin_unlock(&lfsck->li_lock);
5295 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
5298 lfsck_layout_llst_put(llst);
5299 spin_lock(&lfsck->li_lock);
5306 static struct lfsck_operations lfsck_layout_master_ops = {
5307 .lfsck_reset = lfsck_layout_reset,
5308 .lfsck_fail = lfsck_layout_fail,
5309 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
5310 .lfsck_prep = lfsck_layout_master_prep,
5311 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
5312 .lfsck_exec_dir = lfsck_layout_exec_dir,
5313 .lfsck_post = lfsck_layout_master_post,
5314 .lfsck_dump = lfsck_layout_dump,
5315 .lfsck_double_scan = lfsck_layout_master_double_scan,
5316 .lfsck_data_release = lfsck_layout_master_data_release,
5317 .lfsck_quit = lfsck_quit_generic,
5318 .lfsck_in_notify = lfsck_layout_master_in_notify,
5319 .lfsck_query = lfsck_layout_query,
5322 static struct lfsck_operations lfsck_layout_slave_ops = {
5323 .lfsck_reset = lfsck_layout_reset,
5324 .lfsck_fail = lfsck_layout_fail,
5325 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
5326 .lfsck_prep = lfsck_layout_slave_prep,
5327 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
5328 .lfsck_exec_dir = lfsck_layout_exec_dir,
5329 .lfsck_post = lfsck_layout_slave_post,
5330 .lfsck_dump = lfsck_layout_dump,
5331 .lfsck_double_scan = lfsck_layout_slave_double_scan,
5332 .lfsck_data_release = lfsck_layout_slave_data_release,
5333 .lfsck_quit = lfsck_layout_slave_quit,
5334 .lfsck_in_notify = lfsck_layout_slave_in_notify,
5335 .lfsck_query = lfsck_layout_query,
5336 .lfsck_join = lfsck_layout_slave_join,
5339 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
5340 struct lfsck_component *com,
5341 struct lfsck_position *pos)
5343 struct lfsck_assistant_data *lad = com->lc_data;
5344 struct lfsck_layout_req *llr;
5346 if (list_empty(&lad->lad_req_list))
5349 llr = list_entry(lad->lad_req_list.next,
5350 struct lfsck_layout_req,
5352 pos->lp_oit_cookie = llr->llr_parent->llo_cookie - 1;
5355 struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
5356 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
5357 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
5358 .la_fill_pos = lfsck_layout_assistant_fill_pos,
5359 .la_double_scan_result = lfsck_layout_double_scan_result,
5360 .la_req_fini = lfsck_layout_assistant_req_fini,
5361 .la_sync_failures = lfsck_layout_assistant_sync_failures,
5364 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
5366 struct lfsck_component *com;
5367 struct lfsck_layout *lo;
5368 struct dt_object *root = NULL;
5369 struct dt_object *obj;
5377 INIT_LIST_HEAD(&com->lc_link);
5378 INIT_LIST_HEAD(&com->lc_link_dir);
5379 init_rwsem(&com->lc_sem);
5380 atomic_set(&com->lc_ref, 1);
5381 com->lc_lfsck = lfsck;
5382 com->lc_type = LFSCK_TYPE_LAYOUT;
5383 if (lfsck->li_master) {
5384 com->lc_ops = &lfsck_layout_master_ops;
5385 com->lc_data = lfsck_assistant_data_init(
5386 &lfsck_layout_assistant_ops,
5388 if (com->lc_data == NULL)
5389 GOTO(out, rc = -ENOMEM);
5391 struct lfsck_layout_slave_data *llsd;
5393 com->lc_ops = &lfsck_layout_slave_ops;
5394 OBD_ALLOC_PTR(llsd);
5396 GOTO(out, rc = -ENOMEM);
5398 INIT_LIST_HEAD(&llsd->llsd_seq_list);
5399 INIT_LIST_HEAD(&llsd->llsd_master_list);
5400 spin_lock_init(&llsd->llsd_lock);
5401 llsd->llsd_rb_root = RB_ROOT;
5402 rwlock_init(&llsd->llsd_rb_lock);
5403 com->lc_data = llsd;
5405 com->lc_file_size = sizeof(*lo);
5406 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
5407 if (com->lc_file_ram == NULL)
5408 GOTO(out, rc = -ENOMEM);
5410 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
5411 if (com->lc_file_disk == NULL)
5412 GOTO(out, rc = -ENOMEM);
5414 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
5416 GOTO(out, rc = PTR_ERR(root));
5418 if (unlikely(!dt_try_as_dir(env, root)))
5419 GOTO(out, rc = -ENOTDIR);
5421 obj = local_file_find_or_create(env, lfsck->li_los, root,
5423 S_IFREG | S_IRUGO | S_IWUSR);
5425 GOTO(out, rc = PTR_ERR(obj));
5428 rc = lfsck_layout_load(env, com);
5430 rc = lfsck_layout_reset(env, com, true);
5431 else if (rc == -ENOENT)
5432 rc = lfsck_layout_init(env, com);
5437 lo = com->lc_file_ram;
5438 switch (lo->ll_status) {
5444 spin_lock(&lfsck->li_lock);
5445 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
5446 spin_unlock(&lfsck->li_lock);
5449 CERROR("%s: unknown lfsck_layout status %d\n",
5450 lfsck_lfsck2name(lfsck), lo->ll_status);
5452 case LS_SCANNING_PHASE1:
5453 case LS_SCANNING_PHASE2:
5454 /* No need to store the status to disk right now.
5455 * If the system crashed before the status stored,
5456 * it will be loaded back when next time. */
5457 lo->ll_status = LS_CRASHED;
5458 if (!lfsck->li_master)
5459 lo->ll_flags |= LF_INCOMPLETE;
5466 spin_lock(&lfsck->li_lock);
5467 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
5468 spin_unlock(&lfsck->li_lock);
5472 if (lo->ll_flags & LF_CRASHED_LASTID) {
5473 LASSERT(lfsck->li_out_notify != NULL);
5475 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5476 LE_LASTID_REBUILDING);
5482 if (root != NULL && !IS_ERR(root))
5483 lu_object_put(env, &root->do_lu);
5486 lfsck_component_cleanup(env, com);
5487 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
5488 lfsck_lfsck2name(lfsck), rc);
5494 struct lfsck_orphan_it {
5495 struct lfsck_component *loi_com;
5496 struct lfsck_rbtree_node *loi_lrn;
5497 struct lfsck_layout_slave_target *loi_llst;
5498 struct lu_fid loi_key;
5499 struct lu_orphan_rec loi_rec;
5501 unsigned int loi_over:1;
5504 static int lfsck_fid_match_idx(const struct lu_env *env,
5505 struct lfsck_instance *lfsck,
5506 const struct lu_fid *fid, int idx)
5508 struct seq_server_site *ss;
5509 struct lu_server_fld *sf;
5510 struct lu_seq_range range = { 0 };
5513 /* All abnormal cases will be returned to MDT0. */
5514 if (!fid_is_norm(fid)) {
5521 ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
5522 if (unlikely(ss == NULL))
5525 sf = ss->ss_server_fld;
5526 LASSERT(sf != NULL);
5528 fld_range_set_any(&range);
5529 rc = fld_server_lookup(env, sf, fid_seq(fid), &range);
5533 if (!fld_range_is_mdt(&range))
5536 if (range.lsr_index == idx)
5542 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
5543 struct dt_device *dev,
5544 struct dt_object *obj)
5546 struct thandle *handle;
5550 handle = dt_trans_create(env, dev);
5554 rc = dt_declare_ref_del(env, obj, handle);
5558 rc = dt_declare_destroy(env, obj, handle);
5562 rc = dt_trans_start_local(env, dev, handle);
5566 dt_write_lock(env, obj, 0);
5567 rc = dt_ref_del(env, obj, handle);
5569 rc = dt_destroy(env, obj, handle);
5570 dt_write_unlock(env, obj);
5575 dt_trans_stop(env, dev, handle);
5577 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
5578 PFID(lfsck_dto2fid(obj)), rc);
5583 static int lfsck_orphan_index_lookup(const struct lu_env *env,
5584 struct dt_object *dt,
5586 const struct dt_key *key,
5587 struct lustre_capa *capa)
5592 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
5593 struct dt_object *dt,
5594 const struct dt_rec *rec,
5595 const struct dt_key *key,
5596 struct thandle *handle)
5601 static int lfsck_orphan_index_insert(const struct lu_env *env,
5602 struct dt_object *dt,
5603 const struct dt_rec *rec,
5604 const struct dt_key *key,
5605 struct thandle *handle,
5606 struct lustre_capa *capa,
5612 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
5613 struct dt_object *dt,
5614 const struct dt_key *key,
5615 struct thandle *handle)
5620 static int lfsck_orphan_index_delete(const struct lu_env *env,
5621 struct dt_object *dt,
5622 const struct dt_key *key,
5623 struct thandle *handle,
5624 struct lustre_capa *capa)
5629 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
5630 struct dt_object *dt,
5632 struct lustre_capa *capa)
5634 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
5635 struct lfsck_instance *lfsck;
5636 struct lfsck_component *com = NULL;
5637 struct lfsck_layout_slave_data *llsd;
5638 struct lfsck_orphan_it *it = NULL;
5639 struct lfsck_layout *lo;
5643 lfsck = lfsck_instance_find(dev, true, false);
5644 if (unlikely(lfsck == NULL))
5645 RETURN(ERR_PTR(-ENXIO));
5647 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
5648 if (unlikely(com == NULL))
5649 GOTO(out, rc = -ENOENT);
5651 lo = com->lc_file_ram;
5652 if (lo->ll_flags & LF_INCOMPLETE)
5653 GOTO(out, rc = -ESRCH);
5655 llsd = com->lc_data;
5656 if (!llsd->llsd_rbtree_valid)
5657 GOTO(out, rc = -ESRCH);
5661 GOTO(out, rc = -ENOMEM);
5663 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
5664 if (it->loi_llst == NULL)
5665 GOTO(out, rc = -ENXIO);
5667 if (dev->dd_record_fid_accessed) {
5668 /* The first iteration against the rbtree, scan the whole rbtree
5669 * to remove the nodes which do NOT need to be handled. */
5670 write_lock(&llsd->llsd_rb_lock);
5671 if (dev->dd_record_fid_accessed) {
5672 struct rb_node *node;
5673 struct rb_node *next;
5674 struct lfsck_rbtree_node *lrn;
5676 /* No need to record the fid accessing anymore. */
5677 dev->dd_record_fid_accessed = 0;
5679 node = rb_first(&llsd->llsd_rb_root);
5680 while (node != NULL) {
5681 next = rb_next(node);
5682 lrn = rb_entry(node, struct lfsck_rbtree_node,
5684 if (atomic_read(&lrn->lrn_known_count) <=
5685 atomic_read(&lrn->lrn_accessed_count)) {
5686 rb_erase(node, &llsd->llsd_rb_root);
5687 lfsck_rbtree_free(lrn);
5692 write_unlock(&llsd->llsd_rb_lock);
5695 /* read lock the rbtree when init, and unlock when fini */
5696 read_lock(&llsd->llsd_rb_lock);
5704 lfsck_component_put(env, com);
5706 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
5707 lfsck_lfsck2name(lfsck), rc);
5709 lfsck_instance_put(env, lfsck);
5714 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
5717 return (struct dt_it *)it;
5720 static void lfsck_orphan_it_fini(const struct lu_env *env,
5723 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5724 struct lfsck_component *com = it->loi_com;
5725 struct lfsck_layout_slave_data *llsd;
5726 struct lfsck_layout_slave_target *llst;
5729 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
5730 lfsck_lfsck2name(com->lc_lfsck));
5732 llsd = com->lc_data;
5733 read_unlock(&llsd->llsd_rb_lock);
5734 llst = it->loi_llst;
5735 LASSERT(llst != NULL);
5737 /* Save the key and hash for iterate next. */
5738 llst->llst_fid = it->loi_key;
5739 llst->llst_hash = it->loi_hash;
5740 lfsck_layout_llst_put(llst);
5741 lfsck_component_put(env, com);
5747 * \retval +1: the iteration finished
5748 * \retval 0: on success, not finished
5749 * \retval -ve: on error
5751 static int lfsck_orphan_it_next(const struct lu_env *env,
5754 struct lfsck_thread_info *info = lfsck_env_info(env);
5755 struct filter_fid_old *pfid = &info->lti_old_pfid;
5756 struct lu_attr *la = &info->lti_la;
5757 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5758 struct lu_fid *key = &it->loi_key;
5759 struct lu_orphan_rec *rec = &it->loi_rec;
5760 struct lfsck_component *com = it->loi_com;
5761 struct lfsck_instance *lfsck = com->lc_lfsck;
5762 struct lfsck_layout_slave_data *llsd = com->lc_data;
5763 struct dt_object *obj;
5764 struct lfsck_rbtree_node *lrn;
5768 __u32 idx = it->loi_llst->llst_index;
5778 lrn = lfsck_rbtree_search(llsd, key, &exact);
5786 key->f_seq = lrn->lrn_seq;
5787 key->f_oid = lrn->lrn_first_oid;
5792 if (unlikely(key->f_oid == 0)) {
5799 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
5805 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
5806 atomic_read(&lrn->lrn_accessed_count))) {
5807 struct rb_node *next = rb_next(&lrn->lrn_node);
5809 while (next != NULL) {
5810 lrn = rb_entry(next, struct lfsck_rbtree_node,
5812 if (atomic_read(&lrn->lrn_known_count) >
5813 atomic_read(&lrn->lrn_accessed_count))
5815 next = rb_next(next);
5824 key->f_seq = lrn->lrn_seq;
5825 key->f_oid = lrn->lrn_first_oid;
5829 pos = key->f_oid - lrn->lrn_first_oid;
5832 pos = find_next_bit(lrn->lrn_known_bitmap,
5833 LFSCK_RBTREE_BITMAP_WIDTH, pos);
5834 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
5835 key->f_oid = lrn->lrn_first_oid + pos;
5836 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
5844 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
5849 key->f_oid = lrn->lrn_first_oid + pos;
5850 obj = lfsck_object_find(env, lfsck, key);
5853 if (rc == -ENOENT) {
5860 dt_read_lock(env, obj, 0);
5861 if (dt_object_exists(obj) == 0 ||
5862 lfsck_is_dead_obj(obj)) {
5863 dt_read_unlock(env, obj);
5864 lfsck_object_put(env, obj);
5869 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
5873 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, pfid, sizeof(*pfid)),
5874 XATTR_NAME_FID, BYPASS_CAPA);
5875 if (rc == -ENODATA) {
5876 /* For the pre-created OST-object, update the bitmap to avoid
5877 * others LFSCK (second phase) iteration to touch it again. */
5878 if (la->la_ctime == 0) {
5879 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
5880 atomic_inc(&lrn->lrn_accessed_count);
5882 /* For the race between repairing dangling referenced
5883 * MDT-object and unlink the file, it may left orphan
5884 * OST-object there. Destroy it now! */
5885 if (unlikely(!(la->la_mode & S_ISUID))) {
5886 dt_read_unlock(env, obj);
5887 lfsck_layout_destroy_orphan(env,
5890 lfsck_object_put(env, obj);
5894 } else if (idx == 0) {
5895 /* If the orphan OST-object has no parent information,
5896 * regard it as referenced by the MDT-object on MDT0. */
5897 fid_zero(&rec->lor_fid);
5898 rec->lor_uid = la->la_uid;
5899 rec->lor_gid = la->la_gid;
5903 dt_read_unlock(env, obj);
5904 lfsck_object_put(env, obj);
5912 if (rc != sizeof(struct filter_fid) &&
5913 rc != sizeof(struct filter_fid_old))
5914 GOTO(out, rc = -EINVAL);
5916 fid_le_to_cpu(&rec->lor_fid, &pfid->ff_parent);
5917 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
5918 * MDT-object's FID::f_ver, instead it is the OST-object index in its
5919 * parent MDT-object's layout EA. */
5920 save = rec->lor_fid.f_stripe_idx;
5921 rec->lor_fid.f_ver = 0;
5922 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_fid, idx);
5923 /* If the orphan OST-object does not claim the MDT, then next.
5925 * If we do not know whether it matches or not, then return it
5926 * to the MDT for further check. */
5928 dt_read_unlock(env, obj);
5929 lfsck_object_put(env, obj);
5934 rec->lor_fid.f_stripe_idx = save;
5935 rec->lor_uid = la->la_uid;
5936 rec->lor_gid = la->la_gid;
5938 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u\n",
5939 lfsck_lfsck2name(com->lc_lfsck), PFID(key), PFID(&rec->lor_fid),
5940 rec->lor_uid, rec->lor_gid);
5945 dt_read_unlock(env, obj);
5946 lfsck_object_put(env, obj);
5954 * \retval +1: locate to the exactly position
5955 * \retval 0: cannot locate to the exactly position,
5956 * call next() to move to a valid position.
5957 * \retval -ve: on error
5959 static int lfsck_orphan_it_get(const struct lu_env *env,
5961 const struct dt_key *key)
5963 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5966 it->loi_key = *(struct lu_fid *)key;
5967 rc = lfsck_orphan_it_next(env, di);
5977 static void lfsck_orphan_it_put(const struct lu_env *env,
5982 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
5983 const struct dt_it *di)
5985 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
5987 return (struct dt_key *)&it->loi_key;
5990 static int lfsck_orphan_it_key_size(const struct lu_env *env,
5991 const struct dt_it *di)
5993 return sizeof(struct lu_fid);
5996 static int lfsck_orphan_it_rec(const struct lu_env *env,
5997 const struct dt_it *di,
6001 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6003 *(struct lu_orphan_rec *)rec = it->loi_rec;
6008 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
6009 const struct dt_it *di)
6011 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6013 return it->loi_hash;
6017 * \retval +1: locate to the exactly position
6018 * \retval 0: cannot locate to the exactly position,
6019 * call next() to move to a valid position.
6020 * \retval -ve: on error
6022 static int lfsck_orphan_it_load(const struct lu_env *env,
6023 const struct dt_it *di,
6026 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
6027 struct lfsck_layout_slave_target *llst = it->loi_llst;
6030 LASSERT(llst != NULL);
6032 if (hash != llst->llst_hash) {
6033 CDEBUG(D_LFSCK, "%s: the given hash "LPU64" for orphan "
6034 "iteration does not match the one when fini "
6035 LPU64", to be reset.\n",
6036 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
6038 fid_zero(&llst->llst_fid);
6039 llst->llst_hash = 0;
6042 it->loi_key = llst->llst_fid;
6043 it->loi_hash = llst->llst_hash;
6044 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
6054 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
6055 const struct dt_it *di,
6061 const struct dt_index_operations lfsck_orphan_index_ops = {
6062 .dio_lookup = lfsck_orphan_index_lookup,
6063 .dio_declare_insert = lfsck_orphan_index_declare_insert,
6064 .dio_insert = lfsck_orphan_index_insert,
6065 .dio_declare_delete = lfsck_orphan_index_declare_delete,
6066 .dio_delete = lfsck_orphan_index_delete,
6068 .init = lfsck_orphan_it_init,
6069 .fini = lfsck_orphan_it_fini,
6070 .get = lfsck_orphan_it_get,
6071 .put = lfsck_orphan_it_put,
6072 .next = lfsck_orphan_it_next,
6073 .key = lfsck_orphan_it_key,
6074 .key_size = lfsck_orphan_it_key_size,
6075 .rec = lfsck_orphan_it_rec,
6076 .store = lfsck_orphan_it_store,
6077 .load = lfsck_orphan_it_load,
6078 .key_rec = lfsck_orphan_it_key_rec,