4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, 2017, 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 <lu_object.h>
40 #include <dt_object.h>
41 #include <lustre_fid.h>
42 #include <lustre_lib.h>
43 #include <lustre_net.h>
44 #include <md_object.h>
45 #include <obd_class.h>
47 #include "lfsck_internal.h"
49 #define LFSCK_LAYOUT_MAGIC_V1 0xB173AE14
50 #define LFSCK_LAYOUT_MAGIC_V2 0xB1734D76
51 #define LFSCK_LAYOUT_MAGIC_V3 0xB17371B9
52 #define LFSCK_LAYOUT_MAGIC_V4 0xB1732FED
54 #define LFSCK_LAYOUT_MAGIC LFSCK_LAYOUT_MAGIC_V4
56 struct lfsck_layout_seq {
57 struct list_head lls_list;
60 __u64 lls_lastid_known;
61 struct dt_object *lls_lastid_obj;
62 unsigned int lls_dirty:1;
65 struct lfsck_layout_slave_target {
66 /* link into lfsck_layout_slave_data::llsd_master_list. */
67 struct list_head llst_list;
68 /* The position for next record in the rbtree for iteration. */
69 struct lu_fid llst_fid;
70 /* Dummy hash for iteration against the rbtree. */
75 /* How many times we have failed to get the master status. */
79 struct lfsck_layout_slave_data {
80 /* list for lfsck_layout_seq */
81 struct list_head llsd_seq_list;
83 /* list for the masters involve layout verification. */
84 struct list_head llsd_master_list;
87 struct dt_object *llsd_rb_obj;
88 struct rb_root llsd_rb_root;
89 struct rw_semaphore llsd_rb_rwsem;
90 unsigned int llsd_rbtree_valid:1;
93 struct lfsck_layout_slave_async_args {
94 struct obd_export *llsaa_exp;
95 struct lfsck_component *llsaa_com;
96 struct lfsck_layout_slave_target *llsaa_llst;
99 static inline bool lfsck_comp_extent_aligned(__u64 border, __u32 size)
101 return (border & (size - 1)) == 0;
105 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
107 if (atomic_dec_and_test(&llst->llst_ref)) {
108 LASSERT(list_empty(&llst->llst_list));
115 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
117 struct lfsck_layout_slave_target *llst;
118 struct lfsck_layout_slave_target *tmp;
125 INIT_LIST_HEAD(&llst->llst_list);
127 llst->llst_index = index;
128 atomic_set(&llst->llst_ref, 1);
130 spin_lock(&llsd->llsd_lock);
131 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
132 if (tmp->llst_index == index) {
138 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
139 spin_unlock(&llsd->llsd_lock);
148 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
149 struct lfsck_layout_slave_target *llst)
153 spin_lock(&llsd->llsd_lock);
154 if (!list_empty(&llst->llst_list)) {
155 list_del_init(&llst->llst_list);
158 spin_unlock(&llsd->llsd_lock);
161 lfsck_layout_llst_put(llst);
164 static inline struct lfsck_layout_slave_target *
165 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
166 __u32 index, bool unlink)
168 struct lfsck_layout_slave_target *llst;
170 spin_lock(&llsd->llsd_lock);
171 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
172 if (llst->llst_index == index) {
174 list_del_init(&llst->llst_list);
176 atomic_inc(&llst->llst_ref);
177 spin_unlock(&llsd->llsd_lock);
182 spin_unlock(&llsd->llsd_lock);
187 static struct lfsck_layout_req *
188 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
189 struct dt_object *child, __u32 comp_id,
190 __u32 ost_idx, __u32 lov_idx)
192 struct lfsck_layout_req *llr;
196 return ERR_PTR(-ENOMEM);
198 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
199 llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
200 llr->llr_child = child;
201 llr->llr_comp_id = comp_id;
202 llr->llr_ost_idx = ost_idx;
203 llr->llr_lov_idx = lov_idx;
208 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
209 struct lfsck_assistant_req *lar)
211 struct lfsck_layout_req *llr =
212 container_of(lar, struct lfsck_layout_req, llr_lar);
214 lfsck_object_put(env, llr->llr_child);
215 lfsck_assistant_object_put(env, lar->lar_parent);
220 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
221 struct ptlrpc_request *req,
225 struct lfsck_async_interpret_args *laia = args;
226 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
228 ltd->ltd_synced_failures = 1;
229 atomic_dec(laia->laia_count);
236 * Notify remote LFSCK instances about former failures.
238 * The local LFSCK instance has recorded which OSTs have ever failed to respond
239 * some LFSCK verification requests (maybe because of network issues or the OST
240 * itself trouble). During the respond gap, the OST may missed some OST-objects
241 * verification, then the OST cannot know whether related OST-objects have been
242 * referenced by related MDT-objects or not, then in the second-stage scanning,
243 * these OST-objects will be regarded as orphan, if the OST-object contains bad
244 * parent FID for back reference, then it will misguide the LFSCK to make wrong
245 * fixing for the fake orphan.
247 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
248 * it will scan the bitmap for the ever failed OSTs, and notify them that they
249 * have ever missed some OST-object verification and should skip the handling
250 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
252 * \param[in] env pointer to the thread context
253 * \param[in] com pointer to the lfsck component
254 * \param[in] lr pointer to the lfsck request
256 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
257 struct lfsck_component *com,
258 struct lfsck_request *lr)
260 struct lfsck_async_interpret_args *laia =
261 &lfsck_env_info(env)->lti_laia2;
262 struct lfsck_assistant_data *lad = com->lc_data;
263 struct lfsck_layout *lo = com->lc_file_ram;
264 struct lfsck_instance *lfsck = com->lc_lfsck;
265 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
266 struct lfsck_tgt_desc *ltd;
267 struct ptlrpc_request_set *set;
273 if (!test_bit(LAD_INCOMPLETE, &lad->lad_flags))
276 /* If the MDT has ever failed to verfiy some OST-objects,
277 * then sync failures with them firstly. */
278 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
280 atomic_set(&count, 0);
281 memset(laia, 0, sizeof(*laia));
282 laia->laia_count = &count;
283 set = ptlrpc_prep_set();
285 GOTO(out, rc = -ENOMEM);
287 down_read(<ds->ltd_rw_sem);
288 for_each_set_bit(idx, lad->lad_bitmap, lad->lad_bitmap_count) {
289 ltd = lfsck_ltd2tgt(ltds, idx);
293 laia->laia_ltd = ltd;
294 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
295 lfsck_layout_assistant_sync_failures_interpret,
298 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
299 "notify target %x for %s phase1 done: "
300 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
301 ltd->ltd_index, lad->lad_name, rc);
308 up_read(<ds->ltd_rw_sem);
310 if (rc == 0 && atomic_read(&count) > 0)
311 rc = ptlrpc_set_wait(env, set);
313 ptlrpc_set_destroy(set);
315 if (rc == 0 && atomic_read(&count) > 0)
322 /* If failed to sync failures with the OSTs, then have to
323 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
324 * subsequent orphan OST-object handling. */
325 lo->ll_flags |= LF_INCOMPLETE;
327 lr->lr_flags2 = lo->ll_flags;
330 static int lfsck_layout_verify_header_v1v3(struct dt_object *obj,
331 struct lov_mds_md_v1 *lmm,
332 __u64 start, __u64 end,
340 magic = le32_to_cpu(lmm->lmm_magic);
341 /* If magic crashed, keep it there. Sometime later, during OST-object
342 * orphan handling, if some OST-object(s) back-point to it, it can be
343 * verified and repaired. */
344 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
347 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
352 CDEBUG(D_LFSCK, "%s LOV EA magic 0x%X for the file "DFID"\n",
353 rc == -EINVAL ? "Unknown" : "Unsupported",
354 magic, PFID(lfsck_dto2fid(obj)));
359 pattern = le32_to_cpu(lmm->lmm_pattern);
360 *dom = !!(lov_pattern(pattern) == LOV_PATTERN_MDT);
362 /* XXX: DoM file verification will be supportted via LU-11081. */
363 if (lov_pattern(pattern) == LOV_PATTERN_MDT) {
366 CDEBUG(D_LFSCK, "The DoM entry for "DFID" is not "
367 "the first component in the mirror %x/%llu\n",
368 PFID(lfsck_dto2fid(obj)), comp_id, start);
373 } else if (!lov_pattern_supported_normal_comp(lov_pattern(pattern))) {
374 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
375 DFID" in the component %x\n",
376 pattern, PFID(lfsck_dto2fid(obj)), comp_id);
381 size = le32_to_cpu(lmm->lmm_stripe_size);
382 if (!ext && end != LUSTRE_EOF && start != end &&
383 !lfsck_comp_extent_aligned(end, size)){
384 CDEBUG(D_LFSCK, "not aligned border in PFL extent range "
385 "[%llu - %llu) stripesize %u for the file "DFID
386 " at idx %d\n", start, end, size,
387 PFID(lfsck_dto2fid(obj)), comp_id);
395 static int lfsck_layout_verify_header_foreign(struct dt_object *obj,
396 struct lov_foreign_md *lfm,
399 /* magic has been verified already */
400 __u32 value_len = le32_to_cpu(lfm->lfm_length);
401 /* type and flags are not checked for instance */
403 CDEBUG(D_INFO, "foreign LOV EA, magic %x, len %u, type %x, flags %x, for file "DFID"\n",
404 le32_to_cpu(lfm->lfm_magic), value_len,
405 le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
406 PFID(lfsck_dto2fid(obj)));
408 if (len != value_len + offsetof(typeof(*lfm), lfm_value))
409 CDEBUG(D_LFSCK, "foreign LOV EA internal size %u does not match EA full size %zu for file "DFID"\n",
410 value_len, len, PFID(lfsck_dto2fid(obj)));
412 /* nothing to repair */
416 static int lfsck_layout_verify_header(struct dt_object *obj,
417 struct lov_mds_md_v1 *lmm, size_t len)
422 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1 ||
423 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_SEL) {
424 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
427 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
429 if (unlikely(count == 0)) {
430 CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
431 "components count 0\n",
432 PFID(lfsck_dto2fid(obj)));
437 for (i = 0; i < count && !rc; i++) {
438 struct lov_comp_md_entry_v1 *lcme =
439 &lcm->lcm_entries[i];
440 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
441 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
442 __u32 comp_id = le32_to_cpu(lcme->lcme_id);
443 bool ext, inited, zero;
446 if (unlikely(comp_id == LCME_ID_INVAL ||
447 comp_id > LCME_ID_MAX)) {
448 CDEBUG(D_LFSCK, "found invalid PFL ID %u "
449 "for the file "DFID" at idx %d\n",
450 comp_id, PFID(lfsck_dto2fid(obj)), i);
455 flags = le32_to_cpu(lcme->lcme_flags);
456 ext = flags & LCME_FL_EXTENSION;
457 inited = flags & LCME_FL_INIT;
458 zero = !!(start == end);
460 if ((i == 0) && zero) {
461 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu "
462 "- %llu) for "DFID"\n", i, start, end,
463 PFID(lfsck_dto2fid(obj)));
467 if ((zero && (inited || (i + 1 == count))) ||
469 CDEBUG(D_LFSCK, "invalid PFL comp %d/%d: "
470 "[%llu, %llu) for "DFID", %sinited\n",
471 i, count, start, end,
472 PFID(lfsck_dto2fid(obj)),
473 inited ? "" : "NOT ");
477 if (!ext && p_zero) {
478 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu, "
479 "%llu) for "DFID": NOT extension "
480 "after 0-length component\n", i,
481 start, end, PFID(lfsck_dto2fid(obj)));
485 if (ext && (inited || p_dom || zero)) {
486 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu, "
487 "%llu) for "DFID": %s\n", i,
488 start, end, PFID(lfsck_dto2fid(obj)),
489 inited ? "inited extension" :
490 p_dom ? "extension follows DOM" :
491 zero ? "zero length extension" : "");
495 rc = lfsck_layout_verify_header_v1v3(obj,
496 (struct lov_mds_md_v1 *)((char *)lmm +
497 le32_to_cpu(lcme->lcme_offset)), start,
498 end, comp_id, ext, &p_dom);
502 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_FOREIGN) {
503 rc = lfsck_layout_verify_header_foreign(obj,
504 (struct lov_foreign_md *)lmm,
507 rc = lfsck_layout_verify_header_v1v3(obj, lmm, 0, LUSTRE_EOF,
514 static int lfsck_layout_get_lovea(const struct lu_env *env,
515 struct dt_object *obj, struct lu_buf *buf)
521 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
523 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
525 return !rc ? -ENODATA : rc;
527 lu_buf_realloc(buf, rc);
528 if (buf->lb_buf == NULL)
535 return !rc ? -ENODATA : rc;
537 if (unlikely(buf->lb_buf == NULL)) {
538 lu_buf_alloc(buf, rc);
539 if (buf->lb_buf == NULL)
545 rc1 = lfsck_layout_verify_header(obj, buf->lb_buf, rc);
547 return rc1 ? rc1 : rc;
550 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_SIZE
551 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
552 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
554 struct lfsck_rbtree_node {
555 struct rb_node lrn_node;
558 atomic_t lrn_known_count;
559 atomic_t lrn_accessed_count;
560 void *lrn_known_bitmap;
561 void *lrn_accessed_bitmap;
564 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
565 __u64 seq, __u32 oid)
567 if (seq < lrn->lrn_seq)
570 if (seq > lrn->lrn_seq)
573 if (oid < lrn->lrn_first_oid)
576 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
582 /* The caller should hold llsd->llsd_rb_lock. */
583 static struct lfsck_rbtree_node *
584 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
585 const struct lu_fid *fid, bool *exact)
587 struct rb_node *node = llsd->llsd_rb_root.rb_node;
588 struct rb_node *prev = NULL;
589 struct lfsck_rbtree_node *lrn = NULL;
595 while (node != NULL) {
597 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
598 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
600 node = node->rb_left;
602 node = node->rb_right;
610 /* If there is no exactly matched one, then to the next valid one. */
613 /* The rbtree is empty. */
620 node = rb_next(prev);
622 /* The end of the rbtree. */
626 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
631 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
632 const struct lu_fid *fid)
634 struct lfsck_rbtree_node *lrn;
638 return ERR_PTR(-ENOMEM);
640 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
641 if (lrn->lrn_known_bitmap == NULL) {
644 return ERR_PTR(-ENOMEM);
647 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
648 if (lrn->lrn_accessed_bitmap == NULL) {
649 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
652 return ERR_PTR(-ENOMEM);
655 RB_CLEAR_NODE(&lrn->lrn_node);
656 lrn->lrn_seq = fid_seq(fid);
657 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
658 atomic_set(&lrn->lrn_known_count, 0);
659 atomic_set(&lrn->lrn_accessed_count, 0);
664 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
666 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
667 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
671 /* The caller should hold lock. */
672 static struct lfsck_rbtree_node *
673 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
674 struct lfsck_rbtree_node *lrn)
676 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
677 struct rb_node *parent = NULL;
678 struct lfsck_rbtree_node *tmp;
681 while (*pos != NULL) {
683 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
684 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
686 pos = &(*pos)->rb_left;
688 pos = &(*pos)->rb_right;
693 rb_link_node(&lrn->lrn_node, parent, pos);
694 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
699 static const struct dt_index_operations lfsck_orphan_index_ops;
701 static int lfsck_rbtree_setup(const struct lu_env *env,
702 struct lfsck_component *com)
704 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
705 struct lfsck_instance *lfsck = com->lc_lfsck;
706 struct dt_device *dev = lfsck->li_bottom;
707 struct lfsck_layout_slave_data *llsd = com->lc_data;
708 struct dt_object *obj;
710 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
711 fid->f_oid = lfsck_dev_idx(lfsck);
713 obj = dt_locate(env, dev, fid);
715 RETURN(PTR_ERR(obj));
717 /* Generate an in-RAM object to stand for the layout rbtree.
718 * Scanning the layout rbtree will be via the iteration over
719 * the object. In the future, the rbtree may be written onto
720 * disk with the object.
722 * Mark the object to be as exist. */
723 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
724 obj->do_index_ops = &lfsck_orphan_index_ops;
725 llsd->llsd_rb_obj = obj;
726 llsd->llsd_rbtree_valid = 1;
727 dev->dd_record_fid_accessed = 1;
729 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
730 lfsck_lfsck2name(lfsck));
735 static void lfsck_rbtree_cleanup(const struct lu_env *env,
736 struct lfsck_component *com)
738 struct lfsck_instance *lfsck = com->lc_lfsck;
739 struct lfsck_layout_slave_data *llsd = com->lc_data;
740 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
741 struct rb_node *next;
742 struct lfsck_rbtree_node *lrn;
744 lfsck->li_bottom->dd_record_fid_accessed = 0;
745 /* Invalid the rbtree, then no others will use it. */
746 down_write(&llsd->llsd_rb_rwsem);
747 llsd->llsd_rbtree_valid = 0;
748 up_write(&llsd->llsd_rb_rwsem);
750 while (node != NULL) {
751 next = rb_next(node);
752 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
753 rb_erase(node, &llsd->llsd_rb_root);
754 lfsck_rbtree_free(lrn);
758 if (llsd->llsd_rb_obj != NULL) {
759 lfsck_object_put(env, llsd->llsd_rb_obj);
760 llsd->llsd_rb_obj = NULL;
763 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
764 lfsck_lfsck2name(lfsck));
767 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
768 struct lfsck_component *com,
769 const struct lu_fid *fid,
772 struct lfsck_layout_slave_data *llsd = com->lc_data;
773 struct lfsck_rbtree_node *lrn;
779 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
782 if (!fid_is_idif(fid) && !fid_is_norm(fid))
785 down_read(&llsd->llsd_rb_rwsem);
786 if (!llsd->llsd_rbtree_valid)
787 GOTO(unlock, rc = 0);
789 lrn = lfsck_rbtree_search(llsd, fid, NULL);
791 struct lfsck_rbtree_node *tmp;
795 up_read(&llsd->llsd_rb_rwsem);
796 tmp = lfsck_rbtree_new(env, fid);
798 GOTO(out, rc = PTR_ERR(tmp));
801 down_write(&llsd->llsd_rb_rwsem);
802 if (!llsd->llsd_rbtree_valid) {
803 lfsck_rbtree_free(tmp);
804 GOTO(unlock, rc = 0);
807 lrn = lfsck_rbtree_insert(llsd, tmp);
809 lfsck_rbtree_free(tmp);
812 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
813 /* Any accessed object must be a known object. */
814 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
815 atomic_inc(&lrn->lrn_known_count);
816 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
817 atomic_inc(&lrn->lrn_accessed_count);
819 GOTO(unlock, rc = 0);
823 up_write(&llsd->llsd_rb_rwsem);
825 up_read(&llsd->llsd_rb_rwsem);
827 if (rc != 0 && accessed) {
828 struct lfsck_layout *lo = com->lc_file_ram;
830 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
831 "bitmap, and will cause incorrect LFSCK OST-object "
832 "handling, so disable it to cancel orphan handling "
833 "for related device. rc = %d\n",
834 lfsck_lfsck2name(com->lc_lfsck), rc);
836 lo->ll_flags |= LF_INCOMPLETE;
837 lfsck_rbtree_cleanup(env, com);
841 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
842 const struct lfsck_layout_dangling_key *src)
844 fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
845 des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
846 des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
849 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
850 const struct lfsck_layout_dangling_key *src)
852 fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
853 des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
854 des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
857 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
858 const struct lfsck_layout_dangling_key *src)
860 fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
861 des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
862 des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
865 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
866 const struct lfsck_layout_dangling_key *src)
868 fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
869 des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
870 des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
873 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
874 const struct lfsck_layout *src)
878 des->ll_magic = le32_to_cpu(src->ll_magic);
879 des->ll_status = le32_to_cpu(src->ll_status);
880 des->ll_flags = le32_to_cpu(src->ll_flags);
881 des->ll_success_count = le32_to_cpu(src->ll_success_count);
882 des->ll_run_time_phase1 = le64_to_cpu(src->ll_run_time_phase1);
883 des->ll_run_time_phase2 = le64_to_cpu(src->ll_run_time_phase2);
884 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
885 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
886 des->ll_time_last_checkpoint =
887 le64_to_cpu(src->ll_time_last_checkpoint);
888 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
889 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
890 des->ll_pos_first_inconsistent =
891 le64_to_cpu(src->ll_pos_first_inconsistent);
892 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
893 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
894 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
895 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
896 for (i = 0; i < LLIT_MAX; i++)
897 des->ll_objs_repaired[i] =
898 le64_to_cpu(src->ll_objs_repaired[i]);
899 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
900 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
901 lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
902 &src->ll_lldk_latest_scanned_phase2);
905 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
906 const struct lfsck_layout *src)
910 des->ll_magic = cpu_to_le32(src->ll_magic);
911 des->ll_status = cpu_to_le32(src->ll_status);
912 des->ll_flags = cpu_to_le32(src->ll_flags);
913 des->ll_success_count = cpu_to_le32(src->ll_success_count);
914 des->ll_run_time_phase1 = cpu_to_le64(src->ll_run_time_phase1);
915 des->ll_run_time_phase2 = cpu_to_le64(src->ll_run_time_phase2);
916 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
917 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
918 des->ll_time_last_checkpoint =
919 cpu_to_le64(src->ll_time_last_checkpoint);
920 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
921 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
922 des->ll_pos_first_inconsistent =
923 cpu_to_le64(src->ll_pos_first_inconsistent);
924 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
925 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
926 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
927 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
928 for (i = 0; i < LLIT_MAX; i++)
929 des->ll_objs_repaired[i] =
930 cpu_to_le64(src->ll_objs_repaired[i]);
931 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
932 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
933 lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
934 &src->ll_lldk_latest_scanned_phase2);
938 * Load the OST bitmap from the lfsck_layout trace file.
940 * \param[in] env pointer to the thread context
941 * \param[in] com pointer to the lfsck component
943 * \retval 0 for success
944 * \retval negative error number on failure or data corruption
946 static int lfsck_layout_load_bitmap(const struct lu_env *env,
947 struct lfsck_component *com)
949 struct dt_object *obj = com->lc_obj;
950 struct lfsck_assistant_data *lad = com->lc_data;
951 struct lfsck_layout *lo = com->lc_file_ram;
952 unsigned long *bitmap = lad->lad_bitmap;
953 loff_t pos = com->lc_file_size;
959 if (com->lc_lfsck->li_ost_descs.ltd_tgts_mask_len > lo->ll_bitmap_size)
960 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_mask_len;
962 nbits = lo->ll_bitmap_size;
964 if (unlikely(nbits < BITS_PER_LONG))
965 nbits = BITS_PER_LONG;
967 if (nbits > lad->lad_bitmap_count) {
968 u32 new_bits = lad->lad_bitmap_count;
969 unsigned long *new_bitmap;
971 while (new_bits < nbits)
974 new_bitmap = bitmap_zalloc(new_bits, GFP_KERNEL);
975 if (new_bitmap == NULL)
978 lad->lad_bitmap = new_bitmap;
979 lad->lad_bitmap_count = new_bits;
984 if (lo->ll_bitmap_size == 0) {
985 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
986 bitmap_zero(bitmap, lad->lad_bitmap_count);
990 size = (lo->ll_bitmap_size + 7) >> 3;
991 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap, size), &pos);
993 RETURN(rc >= 0 ? -EINVAL : rc);
995 if (bitmap_empty(bitmap, lad->lad_bitmap_count))
996 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
998 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
1004 * Load the layout LFSCK trace file from disk.
1006 * The layout LFSCK trace file records the layout LFSCK status information
1007 * and other statistics, such as how many objects have been scanned, and how
1008 * many objects have been repaired, and etc. It also contains the bitmap for
1009 * failed OSTs during the layout LFSCK. All these information will be loaded
1010 * from disk to RAM when the layout LFSCK component setup.
1012 * \param[in] env pointer to the thread context
1013 * \param[in] com pointer to the lfsck component
1015 * \retval positive number for file data corruption, the caller
1016 * should reset the layout LFSCK trace file
1017 * \retval 0 for success
1018 * \retval negative error number on failure
1020 static int lfsck_layout_load(const struct lu_env *env,
1021 struct lfsck_component *com)
1023 struct lfsck_layout *lo = com->lc_file_ram;
1024 ssize_t size = com->lc_file_size;
1028 rc = dt_read(env, com->lc_obj,
1029 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
1032 } else if (rc < 0) {
1033 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
1034 lfsck_lfsck2name(com->lc_lfsck), rc);
1036 } else if (rc != size) {
1037 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
1038 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
1042 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
1043 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
1044 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
1045 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
1046 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
1054 * Store the layout LFSCK trace file on disk.
1056 * The layout LFSCK trace file records the layout LFSCK status information
1057 * and other statistics, such as how many objects have been scanned, and how
1058 * many objects have been repaired, and etc. It also contains the bitmap for
1059 * failed OSTs during the layout LFSCK. All these information will be synced
1060 * from RAM to disk periodically.
1062 * \param[in] env pointer to the thread context
1063 * \param[in] com pointer to the lfsck component
1065 * \retval 0 for success
1066 * \retval negative error number on failure
1068 static int lfsck_layout_store(const struct lu_env *env,
1069 struct lfsck_component *com)
1071 struct dt_object *obj = com->lc_obj;
1072 struct lfsck_instance *lfsck = com->lc_lfsck;
1073 struct lfsck_layout *lo_ram = com->lc_file_ram;
1074 struct lfsck_layout *lo = com->lc_file_disk;
1076 struct dt_device *dev = lfsck_obj2dev(obj);
1077 unsigned long *bitmap = NULL;
1079 ssize_t size = com->lc_file_size;
1084 if (lfsck->li_master) {
1085 struct lfsck_assistant_data *lad = com->lc_data;
1087 bitmap = lad->lad_bitmap;
1088 nbits = lad->lad_bitmap_count;
1091 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1094 lo_ram->ll_bitmap_size = nbits;
1095 lfsck_layout_cpu_to_le(lo, lo_ram);
1096 th = dt_trans_create(env, dev);
1098 GOTO(log, rc = PTR_ERR(th));
1100 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1105 if (bitmap != NULL) {
1106 rc = dt_declare_record_write(env, obj,
1107 lfsck_buf_get(env, bitmap, nbits >> 3),
1113 rc = dt_trans_start_local(env, dev, th);
1118 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1122 if (bitmap != NULL) {
1124 rc = dt_record_write(env, obj,
1125 lfsck_buf_get(env, bitmap, nbits >> 3),
1132 dt_trans_stop(env, dev, th);
1136 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1137 lfsck_lfsck2name(lfsck), rc);
1142 static int lfsck_layout_init(const struct lu_env *env,
1143 struct lfsck_component *com)
1145 struct lfsck_layout *lo = com->lc_file_ram;
1148 memset(lo, 0, com->lc_file_size);
1149 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1150 lo->ll_status = LS_INIT;
1151 down_write(&com->lc_sem);
1152 rc = lfsck_layout_store(env, com);
1153 if (rc == 0 && com->lc_lfsck->li_master)
1154 rc = lfsck_load_sub_trace_files(env, com,
1155 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1156 up_write(&com->lc_sem);
1161 static int fid_is_for_ostobj(const struct lu_env *env,
1162 struct lfsck_instance *lfsck,
1163 struct dt_object *obj, const struct lu_fid *fid)
1165 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1166 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
1167 struct lustre_ost_attrs *loa;
1170 fld_range_set_any(range);
1171 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1173 if (fld_range_is_ost(range))
1179 loa = &lfsck_env_info(env)->lti_loa;
1180 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1182 if (rc >= (int)sizeof(struct lustre_mdt_attrs)) {
1183 lustre_lma_swab(&loa->loa_lma);
1185 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1188 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1193 static struct lfsck_layout_seq *
1194 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1196 struct lfsck_layout_seq *lls;
1198 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1199 if (lls->lls_seq == seq)
1202 if (lls->lls_seq > seq)
1210 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1211 struct lfsck_layout_seq *lls)
1213 struct lfsck_layout_seq *tmp;
1214 struct list_head *pos = &llsd->llsd_seq_list;
1216 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1217 if (lls->lls_seq < tmp->lls_seq) {
1218 pos = &tmp->lls_list;
1222 list_add_tail(&lls->lls_list, pos);
1226 lfsck_layout_lastid_create(const struct lu_env *env,
1227 struct lfsck_instance *lfsck,
1228 struct dt_object *obj)
1230 struct lfsck_thread_info *info = lfsck_env_info(env);
1231 struct lu_attr *la = &info->lti_la;
1232 struct dt_object_format *dof = &info->lti_dof;
1233 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1234 struct dt_device *dt = lfsck_obj2dev(obj);
1241 if (bk->lb_param & LPF_DRYRUN)
1244 memset(la, 0, sizeof(*la));
1245 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1246 la->la_valid = LA_MODE | LA_UID | LA_GID;
1247 memset(dof, 0, sizeof(*dof));
1248 dof->dof_type = dt_mode_to_dft(S_IFREG);
1250 th = lfsck_trans_create(env, dt, lfsck);
1252 GOTO(log, rc = PTR_ERR(th));
1254 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1258 rc = dt_declare_record_write(env, obj,
1259 lfsck_buf_get(env, &lastid,
1265 rc = dt_trans_start_local(env, dt, th);
1269 dt_write_lock(env, obj, 0);
1270 if (likely(dt_object_exists(obj) == 0)) {
1271 rc = dt_create(env, obj, la, NULL, dof, th);
1273 rc = dt_record_write(env, obj,
1274 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1277 dt_write_unlock(env, obj);
1282 dt_trans_stop(env, dt, th);
1285 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1287 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1293 lfsck_layout_lastid_reload(const struct lu_env *env,
1294 struct lfsck_component *com,
1295 struct lfsck_layout_seq *lls)
1301 dt_read_lock(env, lls->lls_lastid_obj, 0);
1302 rc = dt_record_read(env, lls->lls_lastid_obj,
1303 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1304 dt_read_unlock(env, lls->lls_lastid_obj);
1305 if (unlikely(rc != 0))
1308 lastid = le64_to_cpu(lastid);
1309 if (lastid < lls->lls_lastid_known) {
1310 struct lfsck_instance *lfsck = com->lc_lfsck;
1311 struct lfsck_layout *lo = com->lc_file_ram;
1313 lls->lls_lastid = lls->lls_lastid_known;
1315 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1316 LASSERT(lfsck->li_out_notify != NULL);
1318 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1319 LE_LASTID_REBUILDING);
1320 lo->ll_flags |= LF_CRASHED_LASTID;
1322 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1323 "LAST_ID file (1) for the sequence %#llx"
1324 ", old value %llu, known value %llu\n",
1325 lfsck_lfsck2name(lfsck), lls->lls_seq,
1326 lastid, lls->lls_lastid);
1328 } else if (lastid >= lls->lls_lastid) {
1329 lls->lls_lastid = lastid;
1337 lfsck_layout_lastid_store(const struct lu_env *env,
1338 struct lfsck_component *com)
1340 struct lfsck_instance *lfsck = com->lc_lfsck;
1341 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1342 struct dt_device *dt = lfsck->li_bottom;
1343 struct lfsck_layout_slave_data *llsd = com->lc_data;
1344 struct lfsck_layout_seq *lls;
1350 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1353 if (!lls->lls_dirty)
1356 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1357 "<seq> %#llx as <oid> %llu\n",
1358 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1360 if (bk->lb_param & LPF_DRYRUN) {
1365 th = lfsck_trans_create(env, dt, lfsck);
1368 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1369 "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1370 lfsck_lfsck2name(com->lc_lfsck),
1375 lastid = cpu_to_le64(lls->lls_lastid);
1376 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1377 lfsck_buf_get(env, &lastid,
1383 rc = dt_trans_start_local(env, dt, th);
1387 dt_write_lock(env, lls->lls_lastid_obj, 0);
1388 rc = dt_record_write(env, lls->lls_lastid_obj,
1389 lfsck_buf_get(env, &lastid,
1390 sizeof(lastid)), &pos, th);
1391 dt_write_unlock(env, lls->lls_lastid_obj);
1396 dt_trans_stop(env, dt, th);
1399 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1400 "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1401 lfsck_lfsck2name(com->lc_lfsck),
1410 lfsck_layout_lastid_load(const struct lu_env *env,
1411 struct lfsck_component *com,
1412 struct lfsck_layout_seq *lls)
1414 struct lfsck_instance *lfsck = com->lc_lfsck;
1415 struct lfsck_layout *lo = com->lc_file_ram;
1416 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1417 struct dt_object *obj;
1422 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1423 obj = dt_locate(env, lfsck->li_bottom, fid);
1425 RETURN(PTR_ERR(obj));
1427 /* LAST_ID crashed, to be rebuilt */
1428 if (dt_object_exists(obj) == 0) {
1429 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1430 LASSERT(lfsck->li_out_notify != NULL);
1432 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1433 LE_LASTID_REBUILDING);
1434 lo->ll_flags |= LF_CRASHED_LASTID;
1436 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1437 "LAST_ID file for sequence %#llx\n",
1438 lfsck_lfsck2name(lfsck), lls->lls_seq);
1440 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1442 struct ptlrpc_thread *thread =
1445 up_write(&com->lc_sem);
1446 wait_event_idle_timeout(
1447 thread->t_ctl_waitq,
1448 !thread_is_running(thread),
1449 cfs_time_seconds(cfs_fail_val));
1450 down_write(&com->lc_sem);
1454 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1456 dt_read_lock(env, obj, 0);
1457 rc = dt_read(env, obj,
1458 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1460 dt_read_unlock(env, obj);
1461 if (rc != 0 && rc != sizeof(__u64))
1462 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1464 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1465 LASSERT(lfsck->li_out_notify != NULL);
1467 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1468 LE_LASTID_REBUILDING);
1469 lo->ll_flags |= LF_CRASHED_LASTID;
1471 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1472 "LAST_ID file for the sequence %#llx"
1474 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1477 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1485 lfsck_object_put(env, obj);
1487 lls->lls_lastid_obj = obj;
1492 static void lfsck_layout_record_failure(const struct lu_env *env,
1493 struct lfsck_instance *lfsck,
1494 struct lfsck_layout *lo)
1498 lo->ll_objs_failed_phase1++;
1499 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1501 if (lo->ll_pos_first_inconsistent == 0 ||
1502 lo->ll_pos_first_inconsistent < cookie) {
1503 lo->ll_pos_first_inconsistent = cookie;
1505 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1506 "inconsistency at the pos [%llu]\n",
1507 lfsck_lfsck2name(lfsck),
1508 lo->ll_pos_first_inconsistent);
1512 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1513 struct lfsck_component *com,
1516 struct lfsck_instance *lfsck = com->lc_lfsck;
1517 struct lfsck_layout *lo = com->lc_file_ram;
1519 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan: rc = %d\n",
1520 lfsck_lfsck2name(lfsck), rc);
1522 down_write(&com->lc_sem);
1523 lo->ll_run_time_phase2 += ktime_get_seconds() -
1524 com->lc_time_last_checkpoint;
1525 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
1526 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1529 if (lo->ll_flags & LF_INCOMPLETE) {
1530 lo->ll_status = LS_PARTIAL;
1532 if (lfsck->li_master) {
1533 struct lfsck_assistant_data *lad = com->lc_data;
1535 if (test_bit(LAD_INCOMPLETE, &lad->lad_flags))
1536 lo->ll_status = LS_PARTIAL;
1538 lo->ll_status = LS_COMPLETED;
1540 lo->ll_status = LS_COMPLETED;
1543 lo->ll_flags &= ~LF_SCANNED_ONCE;
1544 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1545 lo->ll_flags &= ~LF_INCONSISTENT;
1546 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1547 lo->ll_success_count++;
1548 } else if (rc == 0) {
1549 if (lfsck->li_status != 0)
1550 lo->ll_status = lfsck->li_status;
1552 lo->ll_status = LS_STOPPED;
1554 lo->ll_status = LS_FAILED;
1557 rc = lfsck_layout_store(env, com);
1558 up_write(&com->lc_sem);
1560 CDEBUG(D_LFSCK, "%s: layout LFSCK double scan result %u: rc = %d\n",
1561 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
1566 static int lfsck_layout_trans_stop(const struct lu_env *env,
1567 struct dt_device *dev,
1568 struct thandle *handle, int result)
1572 /* XXX: If there is something worng or it needs to repair nothing,
1573 * then notify the lower to stop the modification. Currently,
1574 * we use th_result for such purpose, that may be replaced by
1575 * some rollback mechanism in the future. */
1576 handle->th_result = result;
1577 rc = dt_trans_stop(env, dev, handle);
1579 return result > 0 ? 0 : result;
1581 return rc == 0 ? 1 : rc;
1584 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1585 struct lfsck_component *com,
1586 const struct lu_fid *pfid,
1587 const struct lu_fid *cfid,
1588 __u32 comp_id, __u32 ea_off,
1591 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1592 struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1593 struct dt_device *dev;
1594 struct dt_object *obj;
1595 struct thandle *th = NULL;
1600 idx = lfsck_sub_trace_file_fid2idx(pfid);
1601 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1602 dev = lfsck_obj2dev(obj);
1604 fid_cpu_to_be(&key->lldk_fid, pfid);
1605 key->lldk_comp_id = cpu_to_be32(comp_id);
1606 key->lldk_ea_off = cpu_to_be32(ea_off);
1608 fid_cpu_to_be(rec, cfid);
1609 rec->f_ver = cpu_to_be32(ost_idx);
1611 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1613 th = lfsck_trans_create(env, dev, com->lc_lfsck);
1615 GOTO(unlock, rc = PTR_ERR(th));
1617 rc = dt_declare_insert(env, obj,
1618 (const struct dt_rec *)rec,
1619 (const struct dt_key *)key, th);
1623 rc = dt_trans_start_local(env, dev, th);
1627 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1628 (const struct dt_key *)key, th);
1633 if (th && !IS_ERR(th))
1634 dt_trans_stop(env, dev, th);
1636 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1638 CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1639 "ea_off = %u, ost_idx = %u, into the trace file for further "
1640 "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1641 PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1646 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1647 struct lfsck_component *com,
1648 const struct lu_fid *fid,
1649 __u32 comp_id, __u32 ea_off)
1651 struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1652 struct dt_device *dev;
1653 struct dt_object *obj;
1654 struct thandle *th = NULL;
1659 idx = lfsck_sub_trace_file_fid2idx(fid);
1660 obj = com->lc_sub_trace_objs[idx].lsto_obj;
1661 dev = lfsck_obj2dev(obj);
1663 fid_cpu_to_be(&key->lldk_fid, fid);
1664 key->lldk_comp_id = cpu_to_be32(comp_id);
1665 key->lldk_ea_off = cpu_to_be32(ea_off);
1667 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1669 th = lfsck_trans_create(env, dev, com->lc_lfsck);
1671 GOTO(unlock, rc = PTR_ERR(th));
1673 rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1677 rc = dt_trans_start_local(env, dev, th);
1681 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1686 if (th && !IS_ERR(th))
1687 dt_trans_stop(env, dev, th);
1689 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1691 CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1692 ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1693 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1699 * Get the system default stripe size.
1701 * \param[in] env pointer to the thread context
1702 * \param[in] lfsck pointer to the lfsck instance
1703 * \param[out] size pointer to the default stripe size
1705 * \retval 0 for success
1706 * \retval negative error number on failure
1708 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1709 struct lfsck_instance *lfsck,
1712 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1713 struct dt_object *root;
1716 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1718 return PTR_ERR(root);
1720 /* Get the default stripe size via xattr_get on the backend root. */
1721 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1724 /* The lum->lmm_stripe_size is LE mode. The *size also
1725 * should be LE mode. So it is unnecessary to convert. */
1726 *size = lum->lmm_stripe_size;
1728 } else if (unlikely(rc == 0)) {
1732 lfsck_object_put(env, root);
1738 * \retval +1: repaired
1739 * \retval 0: did nothing
1740 * \retval -ve: on error
1742 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1743 struct lfsck_instance *lfsck,
1744 struct thandle *handle,
1745 struct dt_object *parent,
1746 const struct lu_fid *cfid,
1748 struct lov_mds_md_v1 *lmm,
1749 struct lov_ost_data_v1 *slot,
1750 int fl, __u32 ost_idx, int size)
1752 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1753 struct lu_buf ea_buf;
1760 magic = le32_to_cpu(lmm->lmm_magic);
1761 pattern = le32_to_cpu(lmm->lmm_pattern);
1762 count = le16_to_cpu(lmm->lmm_stripe_count);
1764 fid_to_ostid(cfid, oi);
1765 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1766 slot->l_ost_gen = cpu_to_le32(0);
1767 slot->l_ost_idx = cpu_to_le32(ost_idx);
1769 if (pattern & LOV_PATTERN_F_HOLE) {
1770 struct lov_ost_data_v1 *objs;
1773 if (magic == LOV_MAGIC_V1)
1774 objs = &lmm->lmm_objects[0];
1776 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1777 for (i = 0; i < count; i++, objs++) {
1778 if (lovea_slot_is_dummy(objs))
1782 /* If the @slot is the last dummy slot to be refilled,
1783 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1786 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1788 CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1789 ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1790 PFID(cfid), PFID(lfsck_dto2fid(parent)));
1794 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1795 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1802 static struct lov_ost_data_v1 *
1803 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1804 const struct lu_fid *pfid,
1805 __u32 stripe_size, __u32 ea_off,
1806 __u32 pattern, __u16 count)
1808 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1809 lmm->lmm_pattern = cpu_to_le32(pattern);
1810 fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1811 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1812 lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1813 lmm->lmm_stripe_count = cpu_to_le16(count);
1814 lmm->lmm_layout_gen = cpu_to_le16(1);
1815 memset(&lmm->lmm_objects[0], 0,
1816 sizeof(struct lov_ost_data_v1) * count);
1818 return &lmm->lmm_objects[ea_off];
1821 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1822 struct lfsck_instance *lfsck,
1823 struct ost_layout *ol,
1824 struct dt_object *parent,
1825 struct lu_buf *buf, __u32 ea_off,
1826 struct lov_mds_md_v1 **lmm,
1827 struct lov_ost_data_v1 **objs)
1830 __u32 stripe_size = ol->ol_stripe_size;
1831 __u32 pattern = LOV_PATTERN_RAID0;
1834 if (ol->ol_stripe_count != 0)
1835 count = ol->ol_stripe_count;
1839 size = lov_mds_md_size(count, LOV_MAGIC_V1);
1840 LASSERTF(buf->lb_len >= size,
1841 "buffer len %d is less than real size %d\n",
1842 (int)buf->lb_len, size);
1844 if (stripe_size == 0) {
1847 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1853 if (ol->ol_stripe_count > 1 ||
1854 (ol->ol_stripe_count == 0 && ea_off != 0)) {
1855 pattern |= LOV_PATTERN_F_HOLE;
1856 memset(&(*lmm)->lmm_objects[0], 0,
1857 count * sizeof(struct lov_ost_data_v1));
1860 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1861 stripe_size, ea_off, pattern, count);
1866 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1867 struct lu_orphan_rec_v3 *rec,
1868 struct dt_object *parent,
1869 struct lu_buf *buf, __u32 ea_off,
1870 struct lov_mds_md_v1 **lmm,
1871 struct lov_ost_data_v1 **objs)
1873 struct ost_layout *ol = &rec->lor_layout;
1874 struct lov_comp_md_v1 *lcm;
1875 struct lov_comp_md_entry_v1 *lcme;
1876 __u32 pattern = LOV_PATTERN_RAID0;
1877 __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1878 int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1879 int size = offset + lcme_size;
1881 LASSERTF(buf->lb_len >= size,
1882 "buffer len %d is less than real size %d\n",
1883 (int)buf->lb_len, size);
1886 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1887 lcm->lcm_size = cpu_to_le32(size);
1888 if (rec->lor_range) {
1889 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1891 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1892 } else if (rec->lor_layout_version) {
1893 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1895 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1898 * if OST doesn't provide layout version, then try
1899 * to inherit one from MDS's layout, but increment
1900 * it so the client notices and applies modified
1903 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1904 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1906 lcm->lcm_entry_count = cpu_to_le16(1);
1907 /* Currently, we do not know how many mirrors will be, set it as zero
1908 * at the beginning. It will be updated when more mirrors are found. */
1909 lcm->lcm_mirror_count = 0;
1911 lcme = &lcm->lcm_entries[0];
1912 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1913 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1914 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1915 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1916 lcme->lcme_offset = cpu_to_le32(offset);
1917 lcme->lcme_size = cpu_to_le32(lcme_size);
1918 lcme->lcme_layout_gen = lcm->lcm_layout_gen;
1919 if (ol->ol_stripe_count > 1)
1920 pattern |= LOV_PATTERN_F_HOLE;
1922 *lmm = buf->lb_buf + offset;
1923 *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1924 ol->ol_stripe_size, ea_off,
1925 pattern, ol->ol_stripe_count);
1930 static void lfsck_layout_update_lcm(struct lov_comp_md_v1 *lcm,
1931 struct lov_comp_md_entry_v1 *lcme,
1932 __u32 version, __u32 range)
1934 struct lov_comp_md_entry_v1 *tmp;
1935 __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
1936 __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
1937 __u32 gen = version + range;
1940 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1941 __u16 flags = le16_to_cpu(lcm->lcm_flags);
1945 lcme->lcme_layout_gen = cpu_to_le32(gen);
1946 if (le32_to_cpu(lcm->lcm_layout_gen) < gen)
1947 lcm->lcm_layout_gen = cpu_to_le32(gen);
1950 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1951 else if (flags == LCM_FL_NONE && le16_to_cpu(lcm->lcm_mirror_count) > 0)
1952 lcm->lcm_flags = cpu_to_le16(LCM_FL_RDONLY);
1954 for (i = 0; i < count; i++) {
1955 tmp = &lcm->lcm_entries[i];
1956 if (le64_to_cpu(tmp->lcme_extent.e_end) <= start)
1959 if (le64_to_cpu(tmp->lcme_extent.e_start) >= end)
1962 if (le32_to_cpu(tmp->lcme_flags) & LCME_FL_STALE)
1965 tmp_gen = le32_to_cpu(tmp->lcme_layout_gen);
1966 /* "lcme_layout_gen == 0" but without LCME_FL_STALE flag,
1967 * then it should be the latest version of all mirrors. */
1968 if (tmp_gen == 0 || tmp_gen > gen) {
1969 lcme->lcme_flags = cpu_to_le32(
1970 le32_to_cpu(lcme->lcme_flags) | LCME_FL_STALE);
1975 tmp->lcme_flags = cpu_to_le32(
1976 le32_to_cpu(tmp->lcme_flags) | LCME_FL_STALE);
1980 static int lfsck_layout_add_comp(const struct lu_env *env,
1981 struct lfsck_instance *lfsck,
1982 struct thandle *handle,
1983 struct lu_orphan_rec_v3 *rec,
1984 struct dt_object *parent,
1985 const struct lu_fid *cfid,
1986 struct lu_buf *buf, __u32 ost_idx,
1987 __u32 ea_off, int pos, bool new_mirror)
1989 struct ost_layout *ol = &rec->lor_layout;
1990 struct lov_comp_md_v1 *lcm = buf->lb_buf;
1991 struct lov_comp_md_entry_v1 *lcme;
1992 struct lov_mds_md_v1 *lmm;
1993 struct lov_ost_data_v1 *objs;
1994 int added = sizeof(*lcme) +
1995 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1996 int size = le32_to_cpu(lcm->lcm_size) + added;
2000 __u32 pattern = LOV_PATTERN_RAID0;
2001 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
2004 lu_buf_check_and_grow(buf, size);
2005 /* set the lcm again because lu_buf_check_and_grow() may
2006 * have reallocated the buf. */
2008 lcm->lcm_size = cpu_to_le32(size);
2009 lcm->lcm_entry_count = cpu_to_le16(count + 1);
2011 le16_add_cpu(&lcm->lcm_mirror_count, 1);
2013 /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
2014 * with distance of 'added'. */
2017 for (i = pos; i < count; i++) {
2018 lcme = &lcm->lcm_entries[i];
2019 size += le32_to_cpu(lcme->lcme_size);
2022 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
2023 memmove(buf->lb_buf + offset + added,
2024 buf->lb_buf + offset, size);
2028 /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
2029 * of 'sizeof(struct lov_comp_md_entry_v1)' */
2031 for (i = 0; i < pos; i++) {
2032 lcme = &lcm->lcm_entries[i];
2033 size += le32_to_cpu(lcme->lcme_size);
2036 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
2037 memmove(buf->lb_buf + offset + sizeof(*lcme),
2038 buf->lb_buf + offset, size);
2041 /* 3. Recalculate the enter offset for the component [pos, count-1] */
2042 for (i = count - 1; i >= pos; i--) {
2043 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
2044 lcm->lcm_entries[i + 1].lcme_offset =
2045 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
2046 lcme_offset) + added);
2049 /* 4. Recalculate the enter offset for the component [0, pos) */
2050 for (i = 0; i < pos; i++) {
2051 lcm->lcm_entries[i].lcme_offset =
2052 cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
2053 lcme_offset) + sizeof(*lcme));
2056 offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
2057 /* 4. Insert the new component header (entry) at the slot 'pos'. */
2058 lcme = &lcm->lcm_entries[pos];
2059 lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
2060 lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2061 lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
2062 lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
2063 lcme->lcme_offset = cpu_to_le32(offset);
2064 lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
2067 if (ol->ol_stripe_count > 1)
2068 pattern |= LOV_PATTERN_F_HOLE;
2070 lmm = buf->lb_buf + offset;
2071 /* 5. Insert teh new component body at the 'offset'. */
2072 objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
2073 ol->ol_stripe_size, ea_off,
2074 pattern, ol->ol_stripe_count);
2076 /* 6. Update mirror related flags and version. */
2077 lfsck_layout_update_lcm(lcm, lcme, rec->lor_layout_version,
2080 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2081 lmm, objs, LU_XATTR_REPLACE, ost_idx,
2082 le32_to_cpu(lcm->lcm_size));
2084 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
2085 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2086 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2087 "comp_end %llu, layout version %u, range %u, "
2088 "%s LOV EA hole: rc = %d\n",
2089 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2090 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2091 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2092 rec->lor_layout_version, rec->lor_range,
2093 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2094 "with" : "without", rc);
2099 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
2100 struct lfsck_instance *lfsck,
2101 struct thandle *handle,
2102 struct ost_layout *ol,
2103 struct dt_object *parent,
2104 const struct lu_fid *cfid,
2105 struct lu_buf *buf, __u32 ost_idx,
2108 struct lov_mds_md_v1 *lmm = buf->lb_buf;
2109 struct lov_ost_data_v1 *objs;
2110 __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
2111 __u32 magic = le32_to_cpu(lmm->lmm_magic);
2117 /* The original LOVEA maybe re-generated via old filter_fid, at
2118 * that time, we do not know the stripe count and stripe size. */
2119 if (ol->ol_stripe_count > count)
2120 count = ol->ol_stripe_count;
2121 if (ol->ol_stripe_size != 0 &&
2122 ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
2123 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
2125 if (magic == LOV_MAGIC_V1)
2126 objs = &lmm->lmm_objects[count];
2128 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
2130 gap = ea_off - count;
2134 size = lov_mds_md_size(count, magic);
2135 LASSERTF(buf->lb_len >= size,
2136 "buffer len %d is less than real size %d\n",
2137 (int)buf->lb_len, size);
2140 memset(objs, 0, gap * sizeof(*objs));
2141 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
2144 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2145 lmm->lmm_stripe_count = cpu_to_le16(count);
2148 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2149 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
2151 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
2152 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2153 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2154 "comp_end %llu, %s LOV EA hole: rc = %d\n",
2155 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2156 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2157 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2158 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2159 "with" : "without", rc);
2165 * \retval +1: repaired
2166 * \retval 0: did nothing
2167 * \retval -ve: on error
2169 static int lfsck_layout_update_lovea(const struct lu_env *env,
2170 struct lfsck_instance *lfsck,
2171 struct thandle *handle,
2172 struct lu_orphan_rec_v3 *rec,
2173 struct dt_object *parent,
2174 const struct lu_fid *cfid,
2175 struct lu_buf *buf, int fl,
2176 __u32 ost_idx, __u32 ea_off)
2178 struct ost_layout *ol = &rec->lor_layout;
2179 struct lov_mds_md_v1 *lmm = NULL;
2180 struct lov_ost_data_v1 *objs = NULL;
2184 if (ol->ol_comp_id != 0)
2185 rc = lfsck_layout_new_comp_lovea(env, rec, parent, buf, ea_off,
2188 rc = lfsck_layout_new_v1_lovea(env, lfsck, &rec->lor_layout,
2189 parent, buf, ea_off, &lmm,
2192 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2193 buf, lmm, objs, fl, ost_idx, rc);
2195 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2196 DFID": parent "DFID", OST-index %u, stripe-index %u, "
2197 "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2198 "comp_end %llu, layout version %u, range %u, fl %d, "
2199 "%s LOV EA hole: rc = %d\n",
2200 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2201 ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2202 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2203 rec->lor_layout_version, rec->lor_range, fl,
2204 le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2205 "with" : "without", rc);
2210 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2211 struct lfsck_component *com,
2212 struct dt_object *child,
2213 const struct lu_fid *pfid,
2214 const struct ost_layout *ol, __u32 offset,
2215 __u32 version, __u32 range)
2217 struct dt_device *dev = lfsck_obj2dev(child);
2218 struct filter_fid *ff = &lfsck_env_info(env)->lti_ff;
2219 struct thandle *handle;
2220 struct lu_buf buf = { NULL };
2223 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2224 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2225 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2226 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2227 * parent MDT-object's layout EA. */
2228 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2229 ost_layout_cpu_to_le(&ff->ff_layout, ol);
2230 ff->ff_layout_version = cpu_to_le32(version);
2231 ff->ff_range = cpu_to_le32(range);
2232 lfsck_buf_init(&buf, ff, sizeof(*ff));
2234 if (!dt_object_exists(child) || lfsck_is_dead_obj(child))
2237 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
2239 RETURN(PTR_ERR(handle));
2241 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2245 rc = dt_trans_start_local(env, dev, handle);
2249 dt_write_lock(env, child, 0);
2250 if (dt_object_exists(child) && !lfsck_is_dead_obj(child))
2251 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2252 dt_write_unlock(env, child);
2257 dt_trans_stop(env, dev, handle);
2263 * \retval +1: repaired
2264 * \retval 0: did nothing
2265 * \retval -ve: on error
2267 static int lfsck_layout_update_pfid(const struct lu_env *env,
2268 struct lfsck_component *com,
2269 struct dt_object *parent,
2270 struct lu_fid *cfid,
2271 struct dt_device *cdev,
2272 struct lu_orphan_rec_v3 *rec, __u32 ea_off)
2274 struct dt_object *child;
2278 child = lfsck_object_find_by_dev(env, cdev, cfid);
2280 RETURN(PTR_ERR(child));
2282 rc = __lfsck_layout_update_pfid(env, com, child,
2283 lu_object_fid(&parent->do_lu),
2284 &rec->lor_layout, ea_off,
2285 rec->lor_layout_version,
2287 lfsck_object_put(env, child);
2289 RETURN(rc == 0 ? 1 : rc);
2292 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2294 if (ol->ol_comp_id != 0)
2295 return sizeof(struct lov_comp_md_v1) +
2296 sizeof(struct lov_comp_md_entry_v1) +
2297 lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2299 if (ol->ol_stripe_count != 0)
2300 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2302 return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2306 * This function will create the MDT-object with the given (partial) LOV EA.
2308 * Under some data corruption cases, the MDT-object of the file may be lost,
2309 * but its OST-objects, or some of them are there. The layout LFSCK needs to
2310 * re-create the MDT-object with the orphan OST-object(s) information.
2312 * On the other hand, the LFSCK may has created some OST-object for repairing
2313 * dangling LOV EA reference, but as the LFSCK processing, it may find that
2314 * the old OST-object is there and should replace the former new created OST
2315 * object. Unfortunately, some others have modified such newly created object.
2316 * To keep the data (both new and old), the LFSCK will create MDT-object with
2317 * new FID to reference the original OST-object.
2319 * \param[in] env pointer to the thread context
2320 * \param[in] com pointer to the lfsck component
2321 * \param[in] ltd pointer to target device descriptor
2322 * \param[in] rec pointer to the record for the orphan OST-object
2323 * \param[in] cfid pointer to FID for the orphan OST-object
2324 * \param[in] infix additional information, such as the FID for original
2325 * MDT-object and the stripe offset in the LOV EA
2326 * \param[in] type the type for describing why the orphan MDT-object is
2327 * created. The rules are as following:
2329 * type "C": Multiple OST-objects claim the same MDT-object and the
2330 * same slot in the layout EA. Then the LFSCK will create
2331 * new MDT-object(s) to hold the conflict OST-object(s).
2333 * type "N": The orphan OST-object does not know which one was the
2334 * real parent MDT-object, so the LFSCK uses new FID for
2335 * its parent MDT-object.
2337 * type "R": The orphan OST-object knows its parent MDT-object FID,
2338 * but does not know the position (the file name) in the
2341 * type "D": The MDT-object is a directory, it may knows its parent
2342 * but because there is no valid linkEA, the LFSCK cannot
2343 * know where to put it back to the namespace.
2344 * type "O": The MDT-object has no linkEA, and there is no name
2345 * entry that references the MDT-object.
2347 * type "P": The orphan object to be created was a parent directory
2348 * of some MDT-object which linkEA shows that the @orphan
2349 * object is missing.
2351 * The orphan name will be like:
2352 * ${FID}-${infix}-${type}-${conflict_version}
2354 * \param[in] ea_off the stripe offset in the LOV EA
2356 * \retval positive on repaired something
2357 * \retval 0 if needs to repair nothing
2358 * \retval negative error number on failure
2360 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2361 struct lfsck_component *com,
2362 struct lfsck_tgt_desc *ltd,
2363 struct lu_orphan_rec_v3 *rec,
2364 struct lu_fid *cfid,
2369 struct lfsck_thread_info *info = lfsck_env_info(env);
2370 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
2371 char *name = info->lti_key;
2372 struct lu_attr *la = &info->lti_la2;
2373 struct dt_object_format *dof = &info->lti_dof;
2374 struct lfsck_instance *lfsck = com->lc_lfsck;
2375 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
2376 struct lu_fid *tfid = &info->lti_fid3;
2377 struct dt_device *dev = lfsck->li_bottom;
2378 struct dt_object *lpf = lfsck->li_lpf_obj;
2379 struct dt_object *pobj = NULL;
2380 struct dt_object *cobj = NULL;
2381 struct thandle *th = NULL;
2382 struct lu_buf *ea_buf = &info->lti_big_buf;
2383 struct lu_buf lov_buf;
2384 struct lfsck_lock_handle *llh = &info->lti_llh;
2385 struct linkea_data ldata = { NULL };
2386 struct lu_buf linkea_buf;
2387 const struct lu_name *pname;
2393 if (lfsck_is_dryrun(lfsck))
2396 if (unlikely(lpf == NULL))
2397 GOTO(log, rc = -ENXIO);
2399 /* We use two separated transactions to repair the inconsistency.
2401 * 1) create the MDT-object locally.
2402 * 2) update the OST-object's PFID EA if necessary.
2404 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2405 * updated when the layout LFSCK run next time.
2407 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2408 * when the layout LFSCK run next time. */
2410 if (fid_is_zero(pfid)) {
2411 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2415 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2417 GOTO(log, rc = PTR_ERR(cobj));
2420 pobj = lfsck_object_find_by_dev(env, dev, pfid);
2422 GOTO(log, rc = PTR_ERR(pobj));
2424 LASSERT(infix != NULL);
2425 LASSERT(type != NULL);
2427 memset(la, 0, sizeof(*la));
2428 la->la_uid = rec->lor_rec.lor_uid;
2429 la->la_gid = rec->lor_rec.lor_gid;
2430 la->la_mode = S_IFREG | S_IRUSR;
2431 la->la_valid = LA_MODE | LA_UID | LA_GID;
2433 memset(dof, 0, sizeof(*dof));
2434 dof->dof_type = dt_mode_to_dft(S_IFREG);
2435 /* Because the dof->dof_reg.striped = 0, the LOD will not create
2436 * the stripe(s). The LFSCK will specify the LOV EA via
2437 * lfsck_layout_update_lovea(). */
2439 size = lfsck_lovea_size(&rec->lor_layout, ea_off);
2440 if (ea_buf->lb_len < size) {
2441 lu_buf_realloc(ea_buf, size);
2442 if (ea_buf->lb_buf == NULL)
2443 GOTO(log, rc = -ENOMEM);
2448 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2450 rc = dt_lookup_dir(env, lfsck->li_lpf_obj, name, tfid);
2451 if (rc != 0 && rc != -ENOENT)
2455 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2456 MDS_INODELOCK_UPDATE, LCK_PW);
2460 /* Re-check whether the name conflict with othrs after taken
2462 rc = dt_lookup_dir(env, lfsck->li_lpf_obj, name, tfid);
2463 if (unlikely(rc == 0)) {
2471 pname = lfsck_name_get_const(env, name, strlen(name));
2472 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2473 pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2477 /* The 1st transaction. */
2478 th = lfsck_trans_create(env, dev, lfsck);
2480 GOTO(unlock, rc = PTR_ERR(th));
2482 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2486 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2487 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2488 LU_XATTR_CREATE, th);
2492 dtrec->rec_fid = pfid;
2493 dtrec->rec_type = S_IFREG;
2494 rc = dt_declare_insert(env, lpf,
2495 (const struct dt_rec *)dtrec,
2496 (const struct dt_key *)name, th);
2500 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2501 ldata.ld_leh->leh_len);
2502 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2503 XATTR_NAME_LINK, 0, th);
2507 rc = dt_trans_start_local(env, dev, th);
2511 dt_write_lock(env, pobj, 0);
2512 rc = dt_create(env, pobj, la, NULL, dof, th);
2514 rc = lfsck_layout_update_lovea(env, lfsck, th, rec, pobj, cfid,
2515 &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2516 dt_write_unlock(env, pobj);
2520 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2521 (const struct dt_key *)name, th);
2525 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2526 if (rc == 0 && cobj != NULL) {
2527 dt_trans_stop(env, dev, th);
2530 /* The 2nd transaction. */
2531 rc = __lfsck_layout_update_pfid(env, com, cobj, pfid,
2532 &rec->lor_layout, ea_off,
2533 rec->lor_layout_version,
2541 dt_trans_stop(env, dev, th);
2547 if (cobj != NULL && !IS_ERR(cobj))
2548 lfsck_object_put(env, cobj);
2549 if (pobj != NULL && !IS_ERR(pobj))
2550 lfsck_object_put(env, pobj);
2553 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2554 "recreate the lost MDT-object: parent "DFID
2555 ", child "DFID", OST-index %u, stripe-index %u, "
2556 "infix %s, type %s: rc = %d\n",
2557 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2558 ltd->ltd_index, ea_off, infix, type, rc);
2560 return rc >= 0 ? 1 : rc;
2563 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2564 struct lfsck_component *com,
2565 const struct lu_fid *fid,
2568 struct lfsck_thread_info *info = lfsck_env_info(env);
2569 struct lfsck_request *lr = &info->lti_lr;
2570 struct lfsck_instance *lfsck = com->lc_lfsck;
2571 struct lfsck_tgt_desc *ltd;
2572 struct ptlrpc_request *req;
2573 struct lfsck_request *tmp;
2574 struct obd_export *exp;
2578 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2579 if (unlikely(ltd == NULL))
2583 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2584 GOTO(put, rc = -EOPNOTSUPP);
2586 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2588 GOTO(put, rc = -ENOMEM);
2590 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2592 ptlrpc_request_free(req);
2597 memset(lr, 0, sizeof(*lr));
2598 lr->lr_event = LE_CONDITIONAL_DESTROY;
2599 lr->lr_active = LFSCK_TYPE_LAYOUT;
2602 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2604 ptlrpc_request_set_replen(req);
2606 rc = ptlrpc_queue_wait(req);
2607 ptlrpc_req_finished(req);
2617 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2618 struct lfsck_component *com,
2619 struct lfsck_request *lr)
2621 struct lfsck_thread_info *info = lfsck_env_info(env);
2622 struct lu_attr *la = &info->lti_la;
2623 union ldlm_policy_data *policy = &info->lti_policy;
2624 struct ldlm_res_id *resid = &info->lti_resid;
2625 struct lfsck_instance *lfsck = com->lc_lfsck;
2626 struct dt_device *dev = lfsck->li_bottom;
2627 struct lu_fid *fid = &lr->lr_fid;
2628 struct dt_object *obj;
2629 struct thandle *th = NULL;
2630 struct lustre_handle lh = { 0 };
2635 obj = lfsck_object_find_by_dev(env, dev, fid);
2637 RETURN(PTR_ERR(obj));
2639 dt_read_lock(env, obj, 0);
2640 if (dt_object_exists(obj) == 0 ||
2641 lfsck_is_dead_obj(obj)) {
2642 dt_read_unlock(env, obj);
2644 GOTO(put, rc = -ENOENT);
2647 /* Get obj's attr without lock firstly. */
2648 rc = dt_attr_get(env, obj, la);
2649 dt_read_unlock(env, obj);
2653 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2654 GOTO(put, rc = -ETXTBSY);
2656 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2657 LASSERT(lfsck->li_namespace != NULL);
2659 memset(policy, 0, sizeof(*policy));
2660 policy->l_extent.end = OBD_OBJECT_EOF;
2661 ost_fid_build_resid(fid, resid);
2662 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
2663 LDLM_EXTENT, policy, LCK_EX, &flags,
2664 ldlm_blocking_ast, ldlm_completion_ast,
2665 NULL, NULL, 0, LVB_T_NONE, NULL, &lh);
2667 GOTO(put, rc = -EIO);
2669 dt_write_lock(env, obj, 0);
2670 /* Get obj's attr within lock again. */
2671 rc = dt_attr_get(env, obj, la);
2675 if (la->la_ctime != 0)
2676 GOTO(unlock, rc = -ETXTBSY);
2678 th = lfsck_trans_create(env, dev, lfsck);
2680 GOTO(unlock, rc = PTR_ERR(th));
2682 rc = dt_declare_ref_del(env, obj, th);
2686 rc = dt_declare_destroy(env, obj, th);
2690 rc = dt_trans_start_local(env, dev, th);
2694 rc = dt_ref_del(env, obj, th);
2698 rc = dt_destroy(env, obj, th);
2700 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2701 "OST-object "DFID" that was created for reparing "
2702 "dangling referenced case. But the original missing "
2703 "OST-object is found now.\n",
2704 lfsck_lfsck2name(lfsck), PFID(fid));
2709 dt_trans_stop(env, dev, th);
2712 dt_write_unlock(env, obj);
2713 ldlm_lock_decref(&lh, LCK_EX);
2716 lfsck_object_put(env, obj);
2722 * Some OST-object has occupied the specified layout EA slot.
2723 * Such OST-object may be generated by the LFSCK when repair
2724 * dangling referenced MDT-object, which can be indicated by
2725 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2726 * is true and such OST-object has not been modified yet, we
2727 * will replace it with the orphan OST-object; otherwise the
2728 * LFSCK will create new MDT-object to reference the orphan.
2730 * \retval +1: repaired
2731 * \retval 0: did nothing
2732 * \retval -ve: on error
2734 static int lfsck_layout_conflict_create(const struct lu_env *env,
2735 struct lfsck_component *com,
2736 struct lfsck_tgt_desc *ltd,
2737 struct lu_orphan_rec_v3 *rec,
2738 struct dt_object *parent,
2739 struct lu_fid *cfid,
2740 struct lu_buf *ea_buf,
2741 struct lov_mds_md_v1 *lmm,
2742 struct lov_ost_data_v1 *slot,
2743 __u32 ea_off, int lovea_size)
2745 struct lfsck_thread_info *info = lfsck_env_info(env);
2746 struct lu_fid *cfid2 = &info->lti_fid2;
2747 struct ost_id *oi = &info->lti_oi;
2748 struct dt_device *dev = lfsck_obj2dev(parent);
2749 struct thandle *th = NULL;
2750 struct lustre_handle lh = { 0 };
2751 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2755 while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2756 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2760 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2761 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2765 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2766 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2771 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2773 /* If the conflict OST-obejct is not created for fixing dangling
2774 * referenced MDT-object in former LFSCK check/repair, or it has
2775 * been modified by others, then we cannot destroy it. Re-create
2776 * a new MDT-object for the orphan OST-object. */
2777 if (rc == -ETXTBSY) {
2778 /* No need the layout lock on the original parent. */
2779 lfsck_ibits_unlock(&lh, LCK_EX);
2781 fid_zero(&rec->lor_rec.lor_fid);
2782 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2783 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2785 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2786 info->lti_tmpbuf, "C", ea_off);
2791 if (rc != 0 && rc != -ENOENT)
2794 if (lfsck_is_dryrun(com->lc_lfsck))
2795 GOTO(unlock, rc = 0);
2797 th = lfsck_trans_create(env, dev, com->lc_lfsck);
2799 GOTO(unlock, rc = PTR_ERR(th));
2801 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2802 LU_XATTR_REPLACE, th);
2806 rc = dt_trans_start_local(env, dev, th);
2810 dt_write_lock(env, parent, 0);
2811 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2812 rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2813 ea_buf, lmm, slot, LU_XATTR_REPLACE,
2814 ltd->ltd_index, lovea_size);
2815 dt_write_unlock(env, parent);
2820 dt_trans_stop(env, dev, th);
2823 lfsck_ibits_unlock(&lh, LCK_EX);
2826 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2827 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2828 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2829 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2830 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2833 return rc >= 0 ? 1 : rc;
2837 * \retval +1: repaired
2838 * \retval 0: did nothing
2839 * \retval -ve: on error
2841 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2842 struct lfsck_component *com,
2843 struct lfsck_tgt_desc *ltd,
2844 struct lu_orphan_rec_v3 *rec,
2845 struct dt_object *parent,
2846 struct lu_fid *cfid,
2847 __u32 ost_idx, __u32 ea_off)
2849 struct lfsck_thread_info *info = lfsck_env_info(env);
2850 struct lu_buf *buf = &info->lti_big_buf;
2851 struct lu_fid *fid = &info->lti_fid2;
2852 struct ost_id *oi = &info->lti_oi;
2853 struct lfsck_instance *lfsck = com->lc_lfsck;
2854 struct dt_device *dt = lfsck_obj2dev(parent);
2855 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2856 struct ost_layout *ol = &rec->lor_layout;
2857 struct lov_comp_md_v1 *lcm = NULL;
2858 struct lov_comp_md_entry_v1 *lcme = NULL;
2859 struct thandle *handle = NULL;
2861 struct lov_mds_md_v1 *lmm;
2862 struct lov_ost_data_v1 *objs;
2863 struct lustre_handle lh = { 0 };
2872 bool locked = false;
2873 bool new_mirror = true;
2876 if (lfsck_is_dryrun(lfsck))
2879 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2880 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2883 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2884 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2885 "stripe-index %u, comp_id %u, comp_start %llu, "
2886 "comp_end %llu, layout version %u, range %u: rc = %d\n",
2887 lfsck_lfsck2name(lfsck), PFID(cfid),
2888 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2889 ol->ol_comp_id, ol->ol_comp_start,
2890 ol->ol_comp_end, rec->lor_layout_version,
2891 rec->lor_range, rc);
2898 dt_write_unlock(env, parent);
2902 if (handle != NULL) {
2903 dt_trans_stop(env, dt, handle);
2908 GOTO(unlock_layout, rc);
2911 if (buf->lb_len < lovea_size) {
2912 lu_buf_realloc(buf, lovea_size);
2913 if (buf->lb_buf == NULL)
2914 GOTO(unlock_layout, rc = -ENOMEM);
2917 if (!(bk->lb_param & LPF_DRYRUN)) {
2918 handle = lfsck_trans_create(env, dt, lfsck);
2920 GOTO(unlock_layout, rc = PTR_ERR(handle));
2922 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2927 rc = dt_trans_start_local(env, dt, handle);
2932 dt_write_lock(env, parent, 0);
2934 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2935 if (rc == -ERANGE) {
2936 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2939 } else if (rc == -ENODATA || rc == 0) {
2940 lovea_size = lfsck_lovea_size(ol, ea_off);
2941 /* If the declared is not big enough, re-try. */
2942 if (buf->lb_len < lovea_size) {
2946 fl = LU_XATTR_CREATE;
2947 } else if (rc < 0) {
2948 GOTO(unlock_parent, rc);
2949 } else if (unlikely(buf->lb_len == 0)) {
2952 fl = LU_XATTR_REPLACE;
2956 if (fl == LU_XATTR_CREATE) {
2957 if (bk->lb_param & LPF_DRYRUN)
2958 GOTO(unlock_parent, rc = 1);
2960 LASSERT(buf->lb_len >= lovea_size);
2962 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2963 cfid, buf, fl, ost_idx, ea_off);
2965 GOTO(unlock_parent, rc);
2969 rc1 = lfsck_layout_verify_header(parent, lmm, lovea_size);
2971 /* If the LOV EA crashed, the rebuild it. */
2972 if (rc1 == -EINVAL) {
2973 if (bk->lb_param & LPF_DRYRUN)
2974 GOTO(unlock_parent, rc = 1);
2976 LASSERT(buf->lb_len >= lovea_size);
2978 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2979 cfid, buf, fl, ost_idx, ea_off);
2981 GOTO(unlock_parent, rc);
2984 /* For other unknown magic/pattern, keep the current LOV EA. */
2985 if (rc1 == -EOPNOTSUPP)
2986 GOTO(unlock_parent, rc1 = 0);
2989 GOTO(unlock_parent, rc = rc1);
2991 magic = le32_to_cpu(lmm->lmm_magic);
2992 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
2995 __u16 mirror_id0 = mirror_id_of(ol->ol_comp_id);
2998 if (bk->lb_param & LPF_DRYRUN)
2999 GOTO(unlock_parent, rc = 1);
3002 count = le16_to_cpu(lcm->lcm_entry_count);
3003 for (i = 0; i < count; pos = ++i) {
3004 lcme = &lcm->lcm_entries[i];
3005 start = le64_to_cpu(lcme->lcme_extent.e_start);
3006 end = le64_to_cpu(lcme->lcme_extent.e_end);
3007 mirror_id1 = mirror_id_of(le32_to_cpu(lcme->lcme_id));
3009 if (mirror_id0 > mirror_id1)
3012 if (mirror_id0 < mirror_id1)
3016 if (end <= ol->ol_comp_start)
3019 if (start >= ol->ol_comp_end)
3022 lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
3023 magic = le32_to_cpu(lmm->lmm_magic);
3024 flags = le32_to_cpu(lcme->lcme_flags);
3028 rc = lfsck_layout_add_comp(env, lfsck, handle, rec, parent,
3029 cfid, buf, ost_idx, ea_off, pos, new_mirror);
3031 GOTO(unlock_parent, rc);
3035 count = le16_to_cpu(lmm->lmm_stripe_count);
3037 GOTO(unlock_parent, rc = -EINVAL);
3040 /* Exceed the current end of MDT-object layout EA. Then extend it. */
3041 if (count <= ea_off) {
3042 if (bk->lb_param & LPF_DRYRUN)
3043 GOTO(unlock_parent, rc = 1);
3045 lovea_size = lov_mds_md_size(ea_off + 1, magic);
3046 /* If the declared is not big enough, re-try. */
3047 if (buf->lb_len < lovea_size) {
3055 lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
3056 lfsck_layout_update_lcm(lcm, lcme,
3057 rec->lor_layout_version,
3061 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
3062 parent, cfid, buf, ost_idx, ea_off);
3064 GOTO(unlock_parent, rc);
3067 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
3069 if (magic == LOV_MAGIC_V1) {
3070 objs = &lmm->lmm_objects[0];
3072 LASSERT(magic == LOV_MAGIC_V3);
3073 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3076 for (i = 0; i < count; i++, objs++) {
3077 /* The MDT-object was created via lfsck_layout_recover_create()
3078 * by others before, and we fill the dummy layout EA. */
3079 if ((lcme && !(flags & LCME_FL_INIT)) ||
3080 lovea_slot_is_dummy(objs)) {
3084 if (bk->lb_param & LPF_DRYRUN)
3085 GOTO(unlock_parent, rc = 1);
3087 lmm->lmm_layout_gen =
3088 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3092 if (le32_to_cpu(lmm->lmm_stripe_size) !=
3093 ol->ol_stripe_size ||
3094 le16_to_cpu(lmm->lmm_stripe_count) !=
3095 ol->ol_stripe_count ||
3096 le64_to_cpu(lcme->lcme_extent.e_start) !=
3097 ol->ol_comp_start ||
3098 le64_to_cpu(lcme->lcme_extent.e_end) !=
3100 CDEBUG(D_LFSCK, "%s: found invalid "
3101 "component for "DFID ": parent "DFID
3102 ", stripe-index %u, stripe_size %u, "
3103 "stripe_count %u, comp_id %u, "
3104 "comp_start %llu, comp_end %llu, "
3105 "cur_stripe_size %u, "
3106 "cur_stripe_count %u, "
3107 "cur_comp_start %llu, "
3108 "cur_comp_end %llu\n",
3109 lfsck_lfsck2name(lfsck), PFID(cfid),
3110 PFID(lfsck_dto2fid(parent)), ea_off,
3112 ol->ol_stripe_count, ol->ol_comp_id,
3113 ol->ol_comp_start, ol->ol_comp_end,
3114 le32_to_cpu(lmm->lmm_stripe_size),
3115 le16_to_cpu(lmm->lmm_stripe_count),
3116 le64_to_cpu(lcme->lcme_extent.e_start),
3117 le64_to_cpu(lcme->lcme_extent.e_end));
3119 GOTO(unlock_parent, rc = -EINVAL);
3122 lovea_size = le32_to_cpu(lcm->lcm_size);
3123 lcme->lcme_flags = cpu_to_le32(flags |
3125 lfsck_layout_update_lcm(lcm, lcme,
3126 rec->lor_layout_version,
3130 LASSERTF(buf->lb_len >= lovea_size,
3131 "buffer len %d is less than real size %d\n",
3132 (int)buf->lb_len, (int)lovea_size);
3134 rc = lfsck_layout_refill_lovea(env, lfsck, handle,
3135 parent, cfid, buf, lmm, objs,
3136 fl, ost_idx, lovea_size);
3138 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
3139 "dummy layout slot for "DFID": parent "DFID
3140 ", OST-index %u, stripe-index %u: rc = %d\n",
3141 lfsck_lfsck2name(lfsck), PFID(cfid),
3142 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
3144 GOTO(unlock_parent, rc);
3147 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3148 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
3150 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
3151 "invalid layout EA at the slot %d, index %u\n",
3152 lfsck_lfsck2name(lfsck),
3153 PFID(lfsck_dto2fid(parent)), i,
3154 le32_to_cpu(objs->l_ost_idx));
3156 GOTO(unlock_parent, rc);
3159 /* It should be rare case, the slot is there, but the LFSCK
3160 * does not handle it during the first-phase cycle scanning. */
3161 if (unlikely(lu_fid_eq(fid, cfid))) {
3163 GOTO(unlock_parent, rc = 0);
3165 /* Rare case that the OST-object index
3166 * does not match the parent MDT-object
3167 * layout EA. We trust the later one. */
3168 if (bk->lb_param & LPF_DRYRUN)
3169 GOTO(unlock_parent, rc = 1);
3171 dt_write_unlock(env, parent);
3173 dt_trans_stop(env, dt, handle);
3174 lfsck_ibits_unlock(&lh, LCK_EX);
3175 rc = lfsck_layout_update_pfid(env, com, parent,
3179 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
3180 "updated OST-object's pfid for "DFID
3181 ": parent "DFID", OST-index %u, "
3182 "stripe-index %u: rc = %d\n",
3183 lfsck_lfsck2name(lfsck), PFID(cfid),
3184 PFID(lfsck_dto2fid(parent)),
3185 ltd->ltd_index, i, rc);
3192 /* The MDT-object exists, but related layout EA slot is occupied
3194 if (bk->lb_param & LPF_DRYRUN)
3195 GOTO(unlock_parent, rc = 1);
3197 dt_write_unlock(env, parent);
3199 dt_trans_stop(env, dt, handle);
3200 lfsck_ibits_unlock(&lh, LCK_EX);
3201 if (magic == LOV_MAGIC_V1)
3202 objs = &lmm->lmm_objects[ea_off];
3204 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
3205 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
3206 buf, lmm, objs, ea_off, lovea_size);
3212 dt_write_unlock(env, parent);
3216 dt_trans_stop(env, dt, handle);
3219 lfsck_ibits_unlock(&lh, LCK_EX);
3224 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
3225 struct lfsck_component *com,
3226 struct lfsck_tgt_desc *ltd,
3227 struct lu_orphan_rec_v3 *rec,
3228 struct lu_fid *cfid)
3230 struct lfsck_layout *lo = com->lc_file_ram;
3231 struct lu_fid *pfid = &rec->lor_rec.lor_fid;
3232 struct dt_object *parent = NULL;
3233 __u32 ea_off = pfid->f_stripe_idx;
3237 if (!fid_is_sane(cfid))
3238 GOTO(out, rc = -EINVAL);
3241 if (fid_is_zero(pfid)) {
3242 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3247 if (!fid_is_sane(pfid))
3248 GOTO(out, rc = -EINVAL);
3250 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
3252 GOTO(out, rc = PTR_ERR(parent));
3254 if (unlikely(dt_object_remote(parent) != 0))
3255 GOTO(put, rc = -EXDEV);
3257 if (dt_object_exists(parent) == 0) {
3258 lfsck_object_put(env, parent);
3259 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
3264 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
3265 GOTO(put, rc = -EISDIR);
3267 /* The orphan OST-object claims to be the parent's stripe, then
3268 * related dangling record in the trace file is meaningless. */
3269 rc = lfsck_layout_del_dangling_rec(env, com, pfid,
3270 rec->lor_layout.ol_comp_id, ea_off);
3271 if (rc && rc != -ENOENT)
3274 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
3275 ltd->ltd_index, ea_off);
3281 lfsck_object_put(env, parent);
3283 /* The layout EA is changed, need to be reloaded next time. */
3284 dt_object_put_nocache(env, parent);
3287 down_write(&com->lc_sem);
3288 com->lc_new_scanned++;
3289 com->lc_new_checked++;
3291 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
3293 } else if (rc < 0) {
3294 lo->ll_objs_failed_phase2++;
3296 up_write(&com->lc_sem);
3301 static int lfsck_layout_scan_orphan(const struct lu_env *env,
3302 struct lfsck_component *com,
3303 struct lfsck_tgt_desc *ltd)
3305 struct lfsck_assistant_data *lad = com->lc_data;
3306 struct lfsck_instance *lfsck = com->lc_lfsck;
3307 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3308 struct lfsck_thread_info *info = lfsck_env_info(env);
3309 struct lu_fid *fid = &info->lti_fid;
3310 struct dt_object *obj;
3311 const struct dt_it_ops *iops;
3316 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
3317 "scanning for OST%04x\n",
3318 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3320 if (test_bit(ltd->ltd_index, lad->lad_bitmap)) {
3321 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
3322 "scanning for OST%04x\n",
3323 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3328 fid->f_seq = fid_idif_seq(0, ltd->ltd_index);
3329 fid->f_oid = fid->f_ver = 0;
3331 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
3332 if (unlikely(IS_ERR(obj)))
3333 GOTO(log, rc = PTR_ERR(obj));
3335 rc = obj->do_ops->do_index_try(env, obj,
3336 &dt_lfsck_layout_orphan_features);
3340 iops = &obj->do_index_ops->dio_it;
3341 di = iops->init(env, obj, 0);
3343 GOTO(put, rc = PTR_ERR(di));
3345 rc = iops->load(env, di, 0);
3347 /* -ESRCH means that the orphan OST-objects rbtree has been
3348 * cleanup because of the OSS server restart or other errors. */
3349 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
3354 rc = iops->next(env, di);
3366 struct lu_orphan_rec_v3 *rec = &info->lti_rec;
3368 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
3369 unlikely(!thread_is_running(&lfsck->li_thread)))
3372 key = iops->key(env, di);
3373 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
3374 /* Remote target OST may be runnning old LFSCK */
3375 memset(rec, 0, sizeof(*rec));
3376 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
3378 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
3379 &com->lc_fid_latest_scanned_phase2);
3380 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
3383 lfsck_control_speed_by_self(com);
3385 rc = iops->next(env, di);
3386 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
3393 iops->fini(env, di);
3395 lfsck_object_put(env, obj);
3398 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
3399 "scanning for OST%04x: rc = %d\n",
3400 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
3402 return rc > 0 ? 0 : rc;
3405 static int lfsck_lov2layout(struct lov_mds_md_v1 *lmm, struct filter_fid *ff,
3408 struct ost_layout *ol = &ff->ff_layout;
3409 __u32 magic = le32_to_cpu(lmm->lmm_magic);
3413 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3) {
3414 ol->ol_stripe_size = lmm->lmm_stripe_size;
3415 ol->ol_stripe_count = lmm->lmm_stripe_count;
3416 ol->ol_comp_start = 0;
3417 ol->ol_comp_end = 0;
3419 ff->ff_layout_version = 0;
3421 } else if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
3422 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
3423 struct lov_comp_md_entry_v1 *lcme = NULL;
3424 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3427 for (i = 0; i < count; i++) {
3428 lcme = &lcm->lcm_entries[i];
3429 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3430 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3437 /* The comp has been removed, do nothing. */
3441 lmm = (void *)lmm + le32_to_cpu(lcme->lcme_offset);
3442 ol->ol_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
3443 ol->ol_stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
3444 ol->ol_comp_start = le64_to_cpu(lcme->lcme_extent.e_start);
3445 ol->ol_comp_end = le64_to_cpu(lcme->lcme_extent.e_end);
3446 ol->ol_comp_id = le32_to_cpu(lcme->lcme_id);
3447 ff->ff_layout_version = le32_to_cpu(lcme->lcme_layout_gen);
3450 GOTO(out, rc = -EINVAL);
3460 * Repair the MDT-object with dangling LOV EA reference.
3462 * we need to repair the inconsistency according to the users' requirement:
3464 * 1) Keep the inconsistency there and report the inconsistency case,
3465 * then give the chance to the application to find related issues,
3466 * and the users can make the decision about how to handle it with
3467 * more human knownledge. (by default)
3469 * 2) Re-create the missing OST-object with the FID/owner information.
3471 * \param[in] env pointer to the thread context
3472 * \param[in] com the layout LFSCK component
3473 * \param[in] parent the MDT-object with dangling LOV EA reference
3474 * \param[in] child the OST-object to be created
3475 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3476 * \param[in] ea_off the offset of the OST-object in the LOV EA
3477 * \param[in] ost_idx the index of OST on which the OST-object resides
3479 * \retval +1 for repair successfully
3480 * \retval 0 for did nothing
3481 * \retval negative error number on failure
3483 static int __lfsck_layout_repair_dangling(const struct lu_env *env,
3484 struct lfsck_component *com,
3485 struct dt_object *parent,
3486 struct dt_object *child,
3487 __u32 comp_id, __u32 ea_off,
3488 __u32 ost_idx, bool log)
3490 struct lfsck_thread_info *info = lfsck_env_info(env);
3491 struct filter_fid *ff = &info->lti_ff;
3492 struct dt_object_format *dof = &info->lti_dof;
3493 struct lu_attr *la = &info->lti_la;
3494 struct lfsck_instance *lfsck = com->lc_lfsck;
3495 struct dt_device *dev = lfsck_obj2dev(child);
3496 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3497 const struct lu_fid *cfid = lfsck_dto2fid(child);
3498 struct lu_buf *tbuf = &info->lti_big_buf;
3499 struct thandle *handle;
3501 struct lustre_handle lh = { 0 };
3505 if (!(lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ))
3508 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3509 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3514 rc = dt_attr_get(env, parent, la);
3518 la->la_mode = S_IFREG | 0666;
3519 la->la_atime = la->la_mtime = la->la_ctime = 0;
3520 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3521 LA_ATIME | LA_MTIME | LA_CTIME;
3522 memset(dof, 0, sizeof(*dof));
3523 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
3524 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
3525 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3526 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3527 * parent MDT-object's layout EA. */
3528 ff->ff_parent.f_stripe_idx = cpu_to_le32(ea_off);
3530 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3531 if (unlikely(rc == -ENODATA))
3536 rc = lfsck_lov2layout(tbuf->lb_buf, ff, comp_id);
3540 buf = lfsck_buf_get(env, ff, sizeof(struct filter_fid));
3541 handle = lfsck_trans_create(env, dev, lfsck);
3543 GOTO(unlock1, rc = PTR_ERR(handle));
3545 rc = dt_declare_create(env, child, la, NULL, dof, handle);
3549 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
3550 LU_XATTR_CREATE, handle);
3554 rc = dt_trans_start_local(env, dev, handle);
3558 dt_read_lock(env, parent, 0);
3559 if (unlikely(lfsck_is_dead_obj(parent)))
3560 GOTO(unlock2, rc = 0);
3562 if (lfsck->li_bookmark_ram.lb_param & LPF_DELAY_CREATE_OSTOBJ) {
3563 struct ost_id *oi = &info->lti_oi;
3564 struct lu_fid *tfid = &info->lti_fid2;
3565 struct lu_buf *lovea = &info->lti_big_buf;
3566 struct lov_mds_md_v1 *lmm;
3567 struct lov_ost_data_v1 *objs;
3572 rc = lfsck_layout_get_lovea(env, parent, lovea);
3573 if (unlikely(rc == -ENODATA))
3578 lmm = lovea->lb_buf;
3579 magic = le32_to_cpu(lmm->lmm_magic);
3580 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
3581 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3582 struct lov_comp_md_entry_v1 *lcme;
3583 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3586 for (i = 0; i < count; i++) {
3587 lcme = &lcm->lcm_entries[i];
3588 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
3589 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3592 lmm = lovea->lb_buf +
3593 le32_to_cpu(lcme->lcme_offset);
3594 magic = le32_to_cpu(lmm->lmm_magic);
3599 /* Someone removed the component, do nothing. */
3600 GOTO(unlock2, rc = 0);
3604 count = le16_to_cpu(lmm->lmm_stripe_count);
3605 /* Someone changed the LOV EA, do nothing. */
3606 if (count <= ea_off)
3607 GOTO(unlock2, rc = 0);
3609 if (magic == LOV_MAGIC_V1) {
3610 objs = &lmm->lmm_objects[ea_off];
3612 LASSERT(magic == LOV_MAGIC_V3);
3614 objs = &((struct lov_mds_md_v3 *)lmm)->\
3615 lmm_objects[ea_off];
3618 ostid_le_to_cpu(&objs->l_ost_oi, oi);
3619 idx2 = le32_to_cpu(objs->l_ost_idx);
3620 rc = ostid_to_fid(tfid, oi, idx2);
3621 /* Someone changed the LOV EA, do nothing. */
3622 if (rc != 0 || !lu_fid_eq(tfid, cfid))
3626 rc = dt_create(env, child, la, NULL, dof, handle);
3630 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
3636 dt_read_unlock(env, parent);
3639 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3642 lfsck_ibits_unlock(&lh, LCK_EX);
3646 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3647 "dangling reference for: parent "DFID", child "
3648 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: "
3650 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3651 comp_id, ea_off, ost_idx,
3652 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3653 "Create the lost OST-object as required" :
3654 "Keep the MDT-object there by default", rc);
3660 * Repair the MDT-object with dangling LOV EA reference.
3662 * Prepare parameters and call __lfsck_layout_repair_dangling()
3663 * to repair the dangling LOV EA reference.
3665 * \param[in] env pointer to the thread context
3666 * \param[in] com the layout LFSCK component
3667 * \param[in] pfid the MDT-object's FID
3668 * \param[in] cfid the FID for the OST-object to be created
3669 * \param[in] comp_id the component ID of the OST-object in the LOV EA
3670 * \param[in] ea_off the offset of the OST-object in the LOV EA
3671 * \param[in] ost_idx the index of OST on which the OST-object resides
3673 * \retval +1 for repair successfully
3674 * \retval 0 for did nothing
3675 * \retval negative error number on failure
3677 static int lfsck_layout_repair_dangling(const struct lu_env *env,
3678 struct lfsck_component *com,
3679 const struct lu_fid *pfid,
3680 const struct lu_fid *cfid,
3681 __u32 comp_id, __u32 ea_off,
3684 struct lfsck_instance *lfsck = com->lc_lfsck;
3685 struct dt_object *parent = NULL;
3686 struct dt_object *child = NULL;
3687 struct lfsck_tgt_desc *ltd;
3691 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3693 GOTO(log, rc = PTR_ERR(parent));
3695 /* The MDT-object has been removed. */
3696 if (dt_object_exists(parent) == 0)
3699 ltd = lfsck_ltd2tgt(&lfsck->li_ost_descs, ost_idx);
3700 if (unlikely(ltd == NULL))
3701 GOTO(log, rc = -ENODEV);
3703 child = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
3705 GOTO(log, rc = PTR_ERR(child));
3707 /* The OST-object has been created. */
3708 if (unlikely(dt_object_exists(child) != 0))
3711 rc = __lfsck_layout_repair_dangling(env, com, parent, child,
3712 comp_id, ea_off, ost_idx, false);
3717 if (child != NULL && !IS_ERR(child))
3718 lfsck_object_put(env, child);
3720 if (parent != NULL && !IS_ERR(parent))
3721 lfsck_object_put(env, parent);
3724 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
3725 "dangling reference for: parent "DFID", child "
3726 DFID", comp_id %u, ea_off %u, ost_idx %u, %s: rc = %d\n",
3727 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
3728 comp_id, ea_off, ost_idx,
3729 (lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ) ?
3730 "Create the lost OST-object as required" :
3731 "Keep the MDT-object there by default", rc);
3736 /* If the OST-object does not recognize the MDT-object as its parent, and
3737 * there is no other MDT-object claims as its parent, then just trust the
3738 * given MDT-object as its parent. So update the OST-object filter_fid. */
3739 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
3740 struct lfsck_component *com,
3741 struct dt_object *parent,
3742 struct lfsck_layout_req *llr,
3745 struct lfsck_thread_info *info = lfsck_env_info(env);
3746 struct filter_fid *ff = &info->lti_ff;
3747 struct dt_object *child = llr->llr_child;
3748 struct dt_device *dev = lfsck_obj2dev(child);
3749 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
3750 struct lu_buf *tbuf = &info->lti_big_buf;
3751 struct thandle *handle;
3753 struct lustre_handle lh = { 0 };
3757 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
3758 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3763 ff->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
3764 ff->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
3765 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
3766 * MDT-object's FID::f_ver, instead it is the OST-object index in its
3767 * parent MDT-object's layout EA. */
3768 ff->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
3770 rc = lfsck_layout_get_lovea(env, parent, tbuf);
3771 if (unlikely(rc == -ENODATA))
3776 rc = lfsck_lov2layout(tbuf->lb_buf, ff, llr->llr_comp_id);
3780 buf = lfsck_buf_get(env, ff, sizeof(*ff));
3782 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
3784 GOTO(unlock1, rc = PTR_ERR(handle));
3786 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3790 rc = dt_attr_get(env, parent, la);
3794 la->la_valid = LA_UID | LA_GID;
3795 rc = dt_declare_attr_set(env, child, la, handle);
3799 rc = dt_trans_start_local(env, dev, handle);
3803 dt_write_lock(env, parent, 0);
3804 if (unlikely(lfsck_is_dead_obj(parent)))
3805 GOTO(unlock2, rc = 1);
3807 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
3811 /* Get the latest parent's owner. */
3812 rc = dt_attr_get(env, parent, la);
3816 la->la_valid = LA_UID | LA_GID;
3817 rc = dt_attr_set(env, child, la, handle);
3822 dt_write_unlock(env, parent);
3825 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3828 lfsck_ibits_unlock(&lh, LCK_EX);
3832 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3833 "unmatched MDT-OST pair for: parent "DFID
3834 ", child "DFID", comp_id %u, OST-index %u, "
3835 "stripe-index %u, owner %u/%u: rc = %d\n",
3836 lfsck_lfsck2name(com->lc_lfsck),
3837 PFID(lfsck_dto2fid(parent)),
3838 PFID(lfsck_dto2fid(child)),
3839 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
3840 la->la_uid, la->la_gid, rc);
3845 /* If there are more than one MDT-objects claim as the OST-object's parent,
3846 * and the OST-object only recognizes one of them, then we need to generate
3847 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
3848 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
3849 struct lfsck_component *com,
3850 struct dt_object *parent,
3851 struct lfsck_layout_req *llr,
3854 struct lfsck_thread_info *info = lfsck_env_info(env);
3855 struct dt_allocation_hint *hint = &info->lti_hint;
3856 struct dt_object_format *dof = &info->lti_dof;
3857 struct ost_id *oi = &info->lti_oi;
3858 struct lu_buf *buf = &info->lti_big_buf;
3859 struct lfsck_instance *lfsck = com->lc_lfsck;
3860 struct dt_device *dev;
3861 struct lu_device *d =
3862 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
3863 struct lu_object *o;
3864 struct lu_object *n;
3865 struct dt_object *child = NULL;
3866 struct thandle *handle = NULL;
3867 struct lov_mds_md_v1 *lmm;
3868 struct lov_ost_data_v1 *objs;
3869 const struct lu_fid *pfid = lfsck_dto2fid(parent);
3871 struct lustre_handle lh = { 0 };
3877 /* We use two separated transactions to repair the inconsistency.
3879 * 1) create the child (OST-object).
3880 * 2) update the parent LOV EA according to the child's FID.
3882 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
3883 * handled as orphan when the layout LFSCK run next time.
3885 * If 1) failed, but 2) succeed, then such OST-object will be re-created
3886 * as dangling referened case when the layout LFSCK run next time. */
3888 /* The 1st transaction. */
3889 o = lu_object_anon(env, d, NULL);
3891 GOTO(log, rc = PTR_ERR(o));
3893 n = lu_object_locate(o->lo_header, d->ld_type);
3894 if (unlikely(n == NULL)) {
3895 lu_object_put_nocache(env, o);
3897 GOTO(log, rc = -EINVAL);
3900 child = container_of(n, struct dt_object, do_lu);
3901 memset(hint, 0, sizeof(*hint));
3902 rc = dt_attr_get(env, parent, la);
3906 la->la_valid = LA_UID | LA_GID;
3907 memset(dof, 0, sizeof(*dof));
3909 dev = lfsck_obj2dev(child);
3910 handle = lfsck_trans_create(env, dev, lfsck);
3912 GOTO(log, rc = PTR_ERR(handle));
3914 rc = dt_declare_create(env, child, la, hint, dof, handle);
3918 rc = dt_trans_start_local(env, dev, handle);
3922 rc = dt_create(env, child, la, hint, dof, handle);
3923 dt_trans_stop(env, dev, handle);
3928 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3929 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
3934 /* The 2nd transaction. */
3936 /* XXX: Generally, we should use bottom device (OSD) to update parent
3937 * LOV EA. But because the LOD-object still references the wrong
3938 * OSP-object that should be detached after the parent's LOV EA
3939 * refreshed. Unfortunately, there is no suitable API for that.
3940 * So we have to make the LOD to re-load the OSP-object(s) via
3941 * replacing the LOV EA against the LOD-object.
3943 * Once the DNE2 patches have been landed, we can replace the
3944 * LOD device with the OSD device. LU-6230. */
3946 dev = lfsck->li_next;
3947 parent = lfsck_object_locate(dev, parent);
3949 GOTO(log, rc = PTR_ERR(parent));
3951 handle = lfsck_trans_create(env, dev, lfsck);
3953 GOTO(log, rc = PTR_ERR(handle));
3955 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
3956 LU_XATTR_REPLACE, handle);
3960 rc = dt_trans_start_local(env, dev, handle);
3964 dt_write_lock(env, parent, 0);
3965 if (unlikely(lfsck_is_dead_obj(parent)))
3966 GOTO(unlock, rc = 0);
3968 rc = lfsck_layout_get_lovea(env, parent, buf);
3969 if (unlikely(rc == -ENODATA))
3975 magic = le32_to_cpu(lmm->lmm_magic);
3976 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
3977 struct lov_comp_md_v1 *lcm = buf->lb_buf;
3978 struct lov_comp_md_entry_v1 *lcme;
3979 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
3982 LASSERT(llr->llr_comp_id != 0);
3984 for (i = 0; i < count; i++) {
3985 lcme = &lcm->lcm_entries[i];
3986 if (le32_to_cpu(lcme->lcme_id) == llr->llr_comp_id) {
3987 LASSERT(le32_to_cpu(lcme->lcme_flags) &
3990 le32_add_cpu(&lcm->lcm_layout_gen, 1);
3992 le32_to_cpu(lcme->lcme_offset);
3993 magic = le32_to_cpu(lmm->lmm_magic);
3998 GOTO(unlock, rc = 0);
4002 if (magic == LOV_MAGIC_V1) {
4003 objs = &lmm->lmm_objects[llr->llr_lov_idx];
4005 LASSERT(magic == LOV_MAGIC_V3);
4007 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
4010 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4011 index = le32_to_cpu(objs->l_ost_idx);
4012 rc = ostid_to_fid(&tfid, oi, index);
4013 /* Someone changed layout during the LFSCK, no need to repair then. */
4014 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
4015 GOTO(unlock, rc = 0);
4017 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
4018 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
4019 ostid_cpu_to_le(oi, &objs->l_ost_oi);
4020 objs->l_ost_gen = cpu_to_le32(0);
4021 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
4022 rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
4023 LU_XATTR_REPLACE, handle);
4025 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
4028 dt_write_unlock(env, parent);
4032 dt_trans_stop(env, dev, handle);
4035 lfsck_ibits_unlock(&lh, LCK_EX);
4037 lfsck_object_put(env, child);
4040 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4041 "multiple references for: parent "DFID", comp_id %u, "
4042 "OST-index %u, stripe-index %u, owner %u/%u: rc = %d\n",
4043 lfsck_lfsck2name(lfsck), PFID(pfid),
4044 llr->llr_comp_id, llr->llr_ost_idx, llr->llr_lov_idx,
4045 la->la_uid, la->la_gid, rc);
4050 /* If the MDT-object and the OST-object have different owner information,
4051 * then trust the MDT-object, because the normal chown/chgrp handle order
4052 * is from MDT to OST, and it is possible that some chown/chgrp operation
4053 * is partly done. */
4054 static int lfsck_layout_repair_owner(const struct lu_env *env,
4055 struct lfsck_component *com,
4056 struct dt_object *parent,
4057 struct lfsck_layout_req *llr,
4058 struct lu_attr *pla,
4059 const struct lu_attr *cla)
4061 struct lfsck_thread_info *info = lfsck_env_info(env);
4062 struct lu_attr *tla = &info->lti_la2;
4063 struct dt_object *child = llr->llr_child;
4064 struct dt_device *dev = lfsck_obj2dev(child);
4065 struct thandle *handle;
4067 dt_obj_version_t version;
4070 tla->la_uid = pla->la_uid;
4071 tla->la_gid = pla->la_gid;
4072 tla->la_valid = LA_UID | LA_GID;
4073 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
4075 GOTO(log, rc = PTR_ERR(handle));
4077 rc = dt_declare_attr_set(env, child, tla, handle);
4081 rc = dt_trans_start_local(env, dev, handle);
4085 /* Use the dt_object lock to serialize with destroy and attr_set. */
4086 dt_read_lock(env, parent, 0);
4087 if (unlikely(lfsck_is_dead_obj(parent)))
4088 GOTO(unlock, rc = 1);
4090 version = dt_version_get(env, child);
4091 if (version == -EOPNOTSUPP)
4094 /* Get the latest parent's owner. */
4095 rc = dt_attr_get(env, parent, pla);
4099 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
4100 if (unlikely((!version && tla->la_ctime == 0) ||
4101 tla->la_uid != pla->la_uid || tla->la_gid != pla->la_gid))
4104 rc = dt_attr_set(env, child, tla, handle);
4109 dt_read_unlock(env, parent);
4112 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
4116 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
4117 "inconsistent file owner for: parent "DFID", child "DFID
4118 ", OST-index %u, stripe-index %u, old owner %u/%u, "
4119 "new owner %u/%u: rc = %d\n",
4120 lfsck_lfsck2name(com->lc_lfsck),
4121 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4122 llr->llr_ost_idx, llr->llr_lov_idx,
4123 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
4128 #define CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid, msg) \
4129 CDEBUG(D_LFSCK, "%s:("DFID"|"DFID")/"DFID":XATTR %s: %s\n", \
4130 lfsck_lfsck2name(lfsck), PFID(&lso->lso_fid), PFID(pfid), \
4131 PFID(cfid), XATTR_NAME_FID, msg);
4133 /* Check whether the OST-object correctly back points to the
4134 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
4135 static int lfsck_layout_check_parent(const struct lu_env *env,
4136 struct lfsck_component *com,
4137 struct lfsck_assistant_object *lso,
4138 struct filter_fid *ff,
4139 const struct lu_fid *cfid,
4140 const struct lu_attr *cla,
4141 struct lfsck_layout_req *llr)
4143 struct lfsck_thread_info *info = lfsck_env_info(env);
4144 struct lu_buf *buf = &info->lti_big_buf;
4145 struct lu_fid *pfid = &info->lti_fid;
4146 struct dt_object *tobj;
4147 struct lov_mds_md_v1 *lmm;
4148 struct lov_ost_data_v1 *objs;
4149 struct lustre_handle lh = { 0 };
4150 struct lfsck_instance *lfsck = com->lc_lfsck;
4158 *pfid = ff->ff_parent;
4159 idx = pfid->f_stripe_idx;
4162 if (unlikely(!fid_is_sane(pfid))) {
4163 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4164 "the parent FID is invalid");
4166 RETURN(LLIT_UNMATCHED_PAIR);
4169 if (lu_fid_eq(pfid, &lso->lso_fid)) {
4170 if (likely(llr->llr_lov_idx == idx))
4173 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4174 "the stripe index is unmatched");
4176 RETURN(LLIT_UNMATCHED_PAIR);
4179 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
4181 RETURN(PTR_ERR(tobj));
4183 if (dt_object_exists(tobj) == 0) {
4184 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4185 "the parent is nonexistent");
4187 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4190 if (lfsck_is_dead_obj(tobj)) {
4191 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4192 "the parent is dead object");
4194 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4197 if (!S_ISREG(lfsck_object_type(tobj))) {
4198 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4199 "the parent is not a regular file");
4201 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4204 /* Load the tobj's layout EA, in spite of it is a local MDT-object or
4205 * remote one on another MDT. Then check whether the given OST-object
4206 * is in such layout. If yes, it is multiple referenced, otherwise it
4207 * is unmatched referenced case. */
4208 rc = lfsck_layout_get_lovea(env, tobj, buf);
4209 if (rc == 0 || rc == -ENODATA || rc == -ENOENT) {
4210 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4211 "the parent has no stripe data");
4213 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4216 if (unlikely(rc == -EOPNOTSUPP))
4217 GOTO(out, rc = LLIT_NONE);
4223 magic = le32_to_cpu(lmm->lmm_magic);
4224 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
4225 struct lov_comp_md_v1 *lcm = buf->lb_buf;
4226 struct lov_comp_md_entry_v1 *lcme;
4228 if (ff->ff_layout.ol_comp_id == 0) {
4229 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4230 "the parent has incorrect comp_id");
4232 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4235 count = le16_to_cpu(lcm->lcm_entry_count);
4236 for (i = 0; i < count; i++) {
4237 lcme = &lcm->lcm_entries[i];
4238 if (le32_to_cpu(lcme->lcme_id) ==
4239 ff->ff_layout.ol_comp_id) {
4241 le32_to_cpu(lcme->lcme_offset);
4242 magic = le32_to_cpu(lmm->lmm_magic);
4243 if (!(le32_to_cpu(lcme->lcme_flags) &
4245 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid,
4247 "the parent has uninitialized component");
4249 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4256 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4257 "the parent has no matched comp_id");
4259 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4263 if (magic == LOV_MAGIC_V1) {
4264 objs = &lmm->lmm_objects[0];
4266 LASSERT(magic == LOV_MAGIC_V3);
4267 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
4270 count = le16_to_cpu(lmm->lmm_stripe_count);
4271 for (i = 0; i < count; i++, objs++) {
4272 struct lu_fid *tfid = &info->lti_fid2;
4273 struct ost_id *oi = &info->lti_oi;
4276 if (lovea_slot_is_dummy(objs))
4279 ostid_le_to_cpu(&objs->l_ost_oi, oi);
4280 idx2 = le32_to_cpu(objs->l_ost_idx);
4281 rc = ostid_to_fid(tfid, oi, idx2);
4283 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
4284 "invalid layout EA at the slot %d, index %u\n",
4285 lfsck_lfsck2name(com->lc_lfsck),
4286 PFID(pfid), i, idx2);
4288 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4291 if (lu_fid_eq(cfid, tfid)) {
4292 rc = lfsck_ibits_lock(env, com->lc_lfsck, tobj, &lh,
4293 MDS_INODELOCK_UPDATE |
4294 MDS_INODELOCK_LAYOUT |
4295 MDS_INODELOCK_XATTR,
4300 dt_read_lock(env, tobj, 0);
4302 /* For local MDT-object, re-check existence
4303 * after taken the lock. */
4304 if (!dt_object_remote(tobj)) {
4305 if (dt_object_exists(tobj) == 0 ||
4306 lfsck_is_dead_obj(tobj)) {
4307 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid,
4309 "the parent doesn't exist anymore after lock");
4311 rc = LLIT_UNMATCHED_PAIR;
4313 rc = LLIT_MULTIPLE_REFERENCED;
4319 /* For migration case, the new MDT-object and old
4320 * MDT-object may reference the same OST-object at
4321 * some migration internal time.
4323 * For remote MDT-object, the local MDT may not know
4324 * whether it has been removed or not. Try checking
4325 * for a non-existent xattr to check if this object
4326 * has been been removed or not. */
4327 rc = dt_xattr_get(env, tobj, &LU_BUF_NULL,
4329 if (unlikely(rc == -ENOENT || rc >= 0)) {
4330 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4331 "the parent is remote object and nonexistent after lock");
4333 rc = LLIT_UNMATCHED_PAIR;
4334 } else if (rc == -ENODATA) {
4335 rc = LLIT_MULTIPLE_REFERENCED;
4342 CDEBUG_UNMATCHED_PAIR(lfsck, lso, pfid, cfid,
4343 "the parent has no matched stripe");
4345 GOTO(out, rc = LLIT_UNMATCHED_PAIR);
4348 if (lustre_handle_is_used(&lh)) {
4349 dt_read_unlock(env, tobj);
4350 lfsck_ibits_unlock(&lh, LCK_EX);
4354 lfsck_object_put(env, tobj);
4360 * If the MDT-object has the LUSTRE_ENCRYPT_FL flag, it needs to be set
4361 * on the OST-object as well.
4363 static int lfsck_layout_repair_encflag(const struct lu_env *env,
4364 struct lfsck_component *com,
4365 struct dt_object *parent,
4366 struct lfsck_layout_req *llr)
4368 struct lfsck_thread_info *info = lfsck_env_info(env);
4369 struct lu_attr *tla = &info->lti_la2;
4370 struct dt_object *child = llr->llr_child;
4371 struct dt_device *dev = lfsck_obj2dev(child);
4372 struct thandle *handle;
4377 tla->la_valid = LA_FLAGS;
4378 tla->la_flags = LUSTRE_ENCRYPT_FL;
4379 handle = lfsck_trans_create(env, dev, com->lc_lfsck);
4381 GOTO(log, rc = PTR_ERR(handle));
4383 rc = dt_declare_attr_set(env, child, tla, handle);
4387 rc = dt_trans_start_local(env, dev, handle);
4391 /* Use the dt_object lock to serialize with destroy and attr_set. */
4392 dt_read_lock(env, parent, 0);
4393 if (unlikely(lfsck_is_dead_obj(parent)))
4394 GOTO(unlock, rc = 1);
4396 rc = dt_attr_set(env, child, tla, handle);
4400 dt_read_unlock(env, parent);
4403 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
4408 "%s: layout LFSCK assistant repair of inconsistent file enc flag for: parent "
4410 DFID", OST-index %u, stripe-index %u: rc = %d\n",
4411 lfsck_lfsck2name(com->lc_lfsck),
4412 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4413 llr->llr_ost_idx, llr->llr_lov_idx, rc);
4418 static int lfsck_layout_assistant_handler_p1(const struct lu_env *env,
4419 struct lfsck_component *com,
4420 struct lfsck_assistant_req *lar)
4422 struct lfsck_layout_req *llr =
4423 container_of(lar, struct lfsck_layout_req, llr_lar);
4424 struct lfsck_assistant_object *lso = lar->lar_parent;
4425 struct lfsck_layout *lo = com->lc_file_ram;
4426 struct lfsck_thread_info *info = lfsck_env_info(env);
4427 struct filter_fid *ff = &info->lti_ff;
4428 struct lu_buf buf = { .lb_buf = ff,
4429 .lb_len = sizeof(*ff) };
4430 struct dt_object *parent = NULL;
4431 struct dt_object *child = llr->llr_child;
4432 struct lu_attr *pla = &lso->lso_attr;
4433 struct lu_attr *cla = &info->lti_la;
4434 struct lfsck_instance *lfsck = com->lc_lfsck;
4435 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4436 enum lfsck_layout_inconsistency_type type = LLIT_NONE;
4443 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
4445 rc = dt_attr_get(env, child, cla);
4446 if (rc == -ENOENT) {
4447 parent = lfsck_assistant_object_load(env, lfsck, lso);
4448 if (IS_ERR(parent)) {
4449 rc = PTR_ERR(parent);
4451 RETURN(rc == -ENOENT ? 0 : rc);
4454 type = LLIT_DANGLING;
4461 if (!(bk->lb_param & LPF_DRYRUN) &&
4462 pla->la_valid & LA_FLAGS && pla->la_flags & LUSTRE_ENCRYPT_FL) {
4463 /* MDT-inode is encrypted */
4464 struct lu_buf lb = { .lb_buf = NULL, .lb_len = 0 };
4466 /* if OST-inode is missing encryption.c xattr, fix it */
4467 if (dt_xattr_get(env, child, &lb,
4468 LL_XATTR_NAME_ENCRYPTION_CONTEXT) >= 0)
4472 parent = lfsck_assistant_object_load(env, lfsck, lso);
4473 if (!IS_ERR_OR_NULL(parent))
4474 rc = lfsck_layout_repair_encflag(env, com, parent, llr);
4475 down_write(&com->lc_sem);
4477 lfsck_layout_record_failure(env, lfsck, lo);
4479 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
4480 up_write(&com->lc_sem);
4484 lfsck_buf_init(&buf, ff, sizeof(*ff));
4485 rc = dt_xattr_get(env, child, &buf, XATTR_NAME_FID);
4486 if (unlikely(rc > 0 && rc < sizeof(struct lu_fid))) {
4487 CDEBUG(D_LFSCK, "%s:"DFID"/"DFID": "
4488 "the child object's %s is corrupted\n",
4489 lfsck_lfsck2name(lfsck), PFID(&lso->lso_fid),
4490 PFID(lu_object_fid(&child->do_lu)),
4493 type = LLIT_UNMATCHED_PAIR;
4497 if (rc < 0 && rc != -ENODATA)
4500 if (rc == 0 || rc == -ENODATA)
4501 GOTO(check_owner, rc = 0);
4503 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
4504 rc = lfsck_layout_check_parent(env, com, lso, ff,
4505 lu_object_fid(&child->do_lu), cla, llr);
4515 /* Someone may has changed the owner after the parent attr pre-loaded.
4516 * It can be handled later inside the lfsck_layout_repair_owner(). */
4517 if (unlikely(cla->la_uid != pla->la_uid ||
4518 cla->la_gid != pla->la_gid)) {
4519 type = LLIT_INCONSISTENT_OWNER;
4524 if (type == LLIT_NONE)
4527 if (bk->lb_param & LPF_DRYRUN)
4530 if (parent == NULL) {
4531 parent = lfsck_assistant_object_load(env, lfsck, lso);
4532 if (IS_ERR(parent)) {
4533 rc = PTR_ERR(parent);
4544 if (bk->lb_param & LPF_DELAY_CREATE_OSTOBJ)
4545 rc = lfsck_layout_ins_dangling_rec(env, com,
4546 lfsck_dto2fid(parent), lfsck_dto2fid(child),
4547 llr->llr_comp_id, llr->llr_lov_idx,
4550 rc = __lfsck_layout_repair_dangling(env, com, parent,
4557 case LLIT_UNMATCHED_PAIR:
4558 rc = lfsck_layout_repair_unmatched_pair(env, com, parent,
4561 case LLIT_MULTIPLE_REFERENCED:
4562 rc = lfsck_layout_repair_multiple_references(env, com, parent,
4565 case LLIT_INCONSISTENT_OWNER:
4566 rc = lfsck_layout_repair_owner(env, com, parent, llr, pla, cla);
4576 down_write(&com->lc_sem);
4578 struct lfsck_assistant_data *lad = com->lc_data;
4580 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags))) {
4582 } else if (rc == -ENOTCONN || rc == -ESHUTDOWN ||
4583 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4584 rc == -EHOSTUNREACH) {
4585 /* If cannot touch the target server,
4586 * mark the LFSCK as INCOMPLETE. */
4587 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant fail to "
4588 "talk with OST %x: rc = %d\n",
4589 lfsck_lfsck2name(lfsck), llr->llr_ost_idx, rc);
4590 lfsck_lad_set_bitmap(env, com, llr->llr_ost_idx);
4591 lo->ll_objs_skipped++;
4594 lfsck_layout_record_failure(env, lfsck, lo);
4596 } else if (rc > 0 && (type != LLIT_DANGLING ||
4597 !(bk->lb_param & LPF_DELAY_CREATE_OSTOBJ))) {
4598 LASSERTF(type > LLIT_NONE && type <= LLIT_MAX,
4599 "unknown type = %d\n", type);
4601 lo->ll_objs_repaired[type - 1]++;
4602 if (bk->lb_param & LPF_DRYRUN &&
4603 unlikely(lo->ll_pos_first_inconsistent == 0))
4604 lo->ll_pos_first_inconsistent =
4605 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
4608 up_write(&com->lc_sem);
4610 if (parent != NULL && !IS_ERR(parent))
4611 lfsck_object_put(env, parent);
4617 lfsck_layout_double_scan_one_trace_file(const struct lu_env *env,
4618 struct lfsck_component *com,
4619 struct dt_object *obj, bool first)
4621 struct lfsck_instance *lfsck = com->lc_lfsck;
4622 struct ptlrpc_thread *thread = &lfsck->li_thread;
4623 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4624 struct lfsck_layout *lo = com->lc_file_ram;
4625 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4628 struct lfsck_layout_dangling_key *parent =
4629 &lfsck_env_info(env)->lti_lldk;
4630 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
4635 di = iops->init(env, obj, 0);
4637 RETURN(PTR_ERR(di));
4640 lldk_cpu_to_be(parent, &lo->ll_lldk_latest_scanned_phase2);
4642 memset(parent, 0, sizeof(*parent));
4643 rc = iops->get(env, di, (const struct dt_key *)parent);
4648 /* The start one either has been processed or does not exist,
4650 rc = iops->next(env, di);
4656 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
4657 unlikely(!thread_is_running(thread)))
4660 key = iops->key(env, di);
4669 lldk_be_to_cpu(parent,
4670 (const struct lfsck_layout_dangling_key *)key);
4671 if (!fid_is_sane(&parent->lldk_fid)) {
4676 rc = iops->rec(env, di, (struct dt_rec *)cfid, 0);
4678 fid_be_to_cpu(cfid, cfid);
4679 ost_idx = cfid->f_ver;
4681 if (!fid_is_sane(cfid)) {
4686 rc = lfsck_layout_repair_dangling(env, com,
4687 &parent->lldk_fid, cfid,
4688 parent->lldk_comp_id,
4689 parent->lldk_ea_off, ost_idx);
4693 down_write(&com->lc_sem);
4694 com->lc_new_checked++;
4695 com->lc_new_scanned++;
4697 lo->ll_lldk_latest_scanned_phase2 = *parent;
4700 lo->ll_objs_repaired[LLIT_DANGLING - 1]++;
4702 lo->ll_objs_failed_phase2++;
4703 up_write(&com->lc_sem);
4705 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4708 if (unlikely(com->lc_time_next_checkpoint <=
4709 ktime_get_seconds()) &&
4710 com->lc_new_checked != 0) {
4711 down_write(&com->lc_sem);
4712 lo->ll_run_time_phase2 += ktime_get_seconds() -
4713 com->lc_time_last_checkpoint;
4714 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
4715 lo->ll_objs_checked_phase2 += com->lc_new_checked;
4716 com->lc_new_checked = 0;
4717 lfsck_layout_store(env, com);
4718 up_write(&com->lc_sem);
4720 com->lc_time_last_checkpoint = ktime_get_seconds();
4721 com->lc_time_next_checkpoint =
4722 com->lc_time_last_checkpoint +
4723 LFSCK_CHECKPOINT_INTERVAL;
4726 lfsck_control_speed_by_self(com);
4727 if (unlikely(!thread_is_running(thread)))
4730 rc = iops->next(env, di);
4739 iops->fini(env, di);
4744 static int lfsck_layout_assistant_handler_p2(const struct lu_env *env,
4745 struct lfsck_component *com)
4747 struct lfsck_assistant_data *lad = com->lc_data;
4748 struct lfsck_instance *lfsck = com->lc_lfsck;
4749 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4750 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
4751 struct lfsck_tgt_desc *ltd;
4755 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan start\n",
4756 lfsck_lfsck2name(lfsck));
4758 spin_lock(<ds->ltd_lock);
4759 while (!list_empty(&lad->lad_ost_phase2_list)) {
4760 ltd = list_first_entry(&lad->lad_ost_phase2_list,
4761 struct lfsck_tgt_desc,
4762 ltd_layout_phase_list);
4763 list_del_init(<d->ltd_layout_phase_list);
4764 if (bk->lb_param & LPF_OST_ORPHAN) {
4765 spin_unlock(<ds->ltd_lock);
4766 rc = lfsck_layout_scan_orphan(env, com, ltd);
4767 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
4770 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags) ||
4771 !thread_is_running(&lfsck->li_thread)))
4773 spin_lock(<ds->ltd_lock);
4777 if (list_empty(&lad->lad_ost_phase1_list))
4781 spin_unlock(<ds->ltd_lock);
4783 if (rc == 1 && bk->lb_param & LPF_OST_ORPHAN) {
4784 struct lfsck_layout *lo = com->lc_file_ram;
4787 com->lc_new_checked = 0;
4788 com->lc_new_scanned = 0;
4789 com->lc_time_last_checkpoint = ktime_get_seconds();
4790 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4791 LFSCK_CHECKPOINT_INTERVAL;
4793 i = lfsck_sub_trace_file_fid2idx(
4794 &lo->ll_lldk_latest_scanned_phase2.lldk_fid);
4795 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4796 com->lc_sub_trace_objs[i].lsto_obj, true);
4797 while (rc > 0 && ++i < LFSCK_STF_COUNT)
4798 rc = lfsck_layout_double_scan_one_trace_file(env, com,
4799 com->lc_sub_trace_objs[i].lsto_obj, false);
4801 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan dangling stop "
4802 "at the No. %d trace file: rc = %d\n",
4803 lfsck_lfsck2name(lfsck), i, rc);
4806 CDEBUG(D_LFSCK, "%s: layout LFSCK phase2 scan stop: rc = %d\n",
4807 lfsck_lfsck2name(lfsck), rc);
4813 lfsck_layout_slave_async_interpret(const struct lu_env *env,
4814 struct ptlrpc_request *req,
4817 struct lfsck_layout_slave_async_args *llsaa = args;
4818 struct obd_export *exp = llsaa->llsaa_exp;
4819 struct lfsck_component *com = llsaa->llsaa_com;
4820 struct lfsck_layout_slave_target *llst = llsaa->llsaa_llst;
4821 struct lfsck_layout_slave_data *llsd = com->lc_data;
4822 struct lfsck_reply *lr = NULL;
4826 /* It is probably caused by network trouble, or target crash,
4827 * it will try several times (depends on the obd_timeout, and
4828 * will not less than 3 times). But to make the LFSCK can go
4829 * ahead, we should not try for ever. After some try but still
4830 * hit failure, it will assume that the target exit the LFSCK
4831 * prcoessing and stop try. */
4832 if (rc == -ENOTCONN || rc == -ESHUTDOWN) {
4833 int max_try = max_t(int, obd_timeout / 30, 3);
4835 if (++(llst->llst_failures) > max_try)
4841 llst->llst_failures = 0;
4842 lr = req_capsule_server_get(&req->rq_pill, &RMF_LFSCK_REPLY);
4843 if (lr->lr_status != LS_SCANNING_PHASE1 &&
4844 lr->lr_status != LS_SCANNING_PHASE2)
4849 CDEBUG(D_LFSCK, "%s: layout LFSCK slave gets the MDT %x "
4850 "status %d, failures_try %d\n", lfsck_lfsck2name(com->lc_lfsck),
4851 llst->llst_index, lr != NULL ? lr->lr_status : rc,
4852 llst->llst_failures);
4854 lfsck_layout_llst_del(llsd, llst);
4857 lfsck_layout_llst_put(llst);
4858 lfsck_component_put(env, com);
4859 class_export_put(exp);
4864 static int lfsck_layout_async_query(const struct lu_env *env,
4865 struct lfsck_component *com,
4866 struct obd_export *exp,
4867 struct lfsck_layout_slave_target *llst,
4868 struct lfsck_request *lr,
4869 struct ptlrpc_request_set *set)
4871 struct lfsck_layout_slave_async_args *llsaa;
4872 struct ptlrpc_request *req;
4873 struct lfsck_request *tmp;
4877 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_QUERY);
4881 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_QUERY);
4883 ptlrpc_request_free(req);
4887 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4889 ptlrpc_request_set_replen(req);
4891 llsaa = ptlrpc_req_async_args(llsaa, req);
4892 llsaa->llsaa_exp = exp;
4893 llsaa->llsaa_com = lfsck_component_get(com);
4894 llsaa->llsaa_llst = llst;
4895 req->rq_interpret_reply = lfsck_layout_slave_async_interpret;
4896 req->rq_allow_intr = 1;
4897 req->rq_no_delay = 1;
4898 ptlrpc_set_add_req(set, req);
4903 static int lfsck_layout_async_notify(const struct lu_env *env,
4904 struct obd_export *exp,
4905 struct lfsck_request *lr,
4906 struct ptlrpc_request_set *set)
4908 struct ptlrpc_request *req;
4909 struct lfsck_request *tmp;
4913 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
4917 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
4919 ptlrpc_request_free(req);
4923 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
4925 ptlrpc_request_set_replen(req);
4926 req->rq_allow_intr = 1;
4927 req->rq_no_delay = 1;
4928 ptlrpc_set_add_req(set, req);
4934 lfsck_layout_slave_query_master(const struct lu_env *env,
4935 struct lfsck_component *com)
4937 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
4938 struct lfsck_instance *lfsck = com->lc_lfsck;
4939 struct lfsck_layout_slave_data *llsd = com->lc_data;
4940 struct lfsck_layout_slave_target *llst;
4941 struct obd_export *exp;
4942 struct ptlrpc_request_set *set;
4947 set = ptlrpc_prep_set();
4949 GOTO(log, rc = -ENOMEM);
4951 memset(lr, 0, sizeof(*lr));
4952 lr->lr_event = LE_QUERY;
4953 lr->lr_active = LFSCK_TYPE_LAYOUT;
4955 llsd->llsd_touch_gen++;
4956 spin_lock(&llsd->llsd_lock);
4957 while (!list_empty(&llsd->llsd_master_list)) {
4958 llst = list_first_entry(&llsd->llsd_master_list,
4959 struct lfsck_layout_slave_target,
4961 if (llst->llst_gen == llsd->llsd_touch_gen)
4964 llst->llst_gen = llsd->llsd_touch_gen;
4965 list_move_tail(&llst->llst_list,
4966 &llsd->llsd_master_list);
4967 atomic_inc(&llst->llst_ref);
4968 spin_unlock(&llsd->llsd_lock);
4970 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
4973 lfsck_layout_llst_del(llsd, llst);
4974 lfsck_layout_llst_put(llst);
4975 spin_lock(&llsd->llsd_lock);
4979 rc = lfsck_layout_async_query(env, com, exp, llst, lr, set);
4981 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
4982 "query %s for layout: rc = %d\n",
4983 lfsck_lfsck2name(lfsck),
4984 exp->exp_obd->obd_name, rc);
4987 lfsck_layout_llst_put(llst);
4988 class_export_put(exp);
4990 spin_lock(&llsd->llsd_lock);
4992 spin_unlock(&llsd->llsd_lock);
4994 rc = ptlrpc_set_wait(env, set);
4995 ptlrpc_set_destroy(set);
4997 GOTO(log, rc = (rc1 != 0 ? rc1 : rc));
5000 CDEBUG(D_LFSCK, "%s: layout LFSCK slave queries master: rc = %d\n",
5001 lfsck_lfsck2name(com->lc_lfsck), rc);
5007 lfsck_layout_slave_notify_master(const struct lu_env *env,
5008 struct lfsck_component *com,
5009 enum lfsck_events event, int result)
5011 struct lfsck_layout *lo = com->lc_file_ram;
5012 struct lfsck_instance *lfsck = com->lc_lfsck;
5013 struct lfsck_layout_slave_data *llsd = com->lc_data;
5014 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
5015 struct lfsck_layout_slave_target *llst;
5016 struct obd_export *exp;
5017 struct ptlrpc_request_set *set;
5021 CDEBUG(D_LFSCK, "%s: layout LFSCK slave notifies master\n",
5022 lfsck_lfsck2name(com->lc_lfsck));
5024 set = ptlrpc_prep_set();
5028 memset(lr, 0, sizeof(*lr));
5029 lr->lr_event = event;
5030 lr->lr_flags = LEF_FROM_OST;
5031 lr->lr_status = result;
5032 lr->lr_index = lfsck_dev_idx(lfsck);
5033 lr->lr_active = LFSCK_TYPE_LAYOUT;
5034 lr->lr_flags2 = lo->ll_flags;
5035 llsd->llsd_touch_gen++;
5036 spin_lock(&llsd->llsd_lock);
5037 while (!list_empty(&llsd->llsd_master_list)) {
5038 llst = list_first_entry(&llsd->llsd_master_list,
5039 struct lfsck_layout_slave_target,
5041 if (llst->llst_gen == llsd->llsd_touch_gen)
5044 llst->llst_gen = llsd->llsd_touch_gen;
5045 list_move_tail(&llst->llst_list,
5046 &llsd->llsd_master_list);
5047 atomic_inc(&llst->llst_ref);
5048 spin_unlock(&llsd->llsd_lock);
5050 exp = lustre_find_lwp_by_index(lfsck->li_obd->obd_name,
5053 lfsck_layout_llst_del(llsd, llst);
5054 lfsck_layout_llst_put(llst);
5055 spin_lock(&llsd->llsd_lock);
5059 rc = lfsck_layout_async_notify(env, exp, lr, set);
5061 CDEBUG(D_LFSCK, "%s: layout LFSCK slave fail to "
5062 "notify %s for layout: rc = %d\n",
5063 lfsck_lfsck2name(lfsck),
5064 exp->exp_obd->obd_name, rc);
5066 lfsck_layout_llst_put(llst);
5067 class_export_put(exp);
5068 spin_lock(&llsd->llsd_lock);
5070 spin_unlock(&llsd->llsd_lock);
5072 ptlrpc_set_wait(env, set);
5073 ptlrpc_set_destroy(set);
5079 * \ret -ENODATA: unrecognized stripe
5080 * \ret = 0 : recognized stripe
5081 * \ret < 0 : other failures
5083 static int lfsck_layout_master_check_pairs(const struct lu_env *env,
5084 struct lfsck_component *com,
5085 struct lu_fid *cfid,
5086 struct lu_fid *pfid, __u32 comp_id)
5088 struct lfsck_thread_info *info = lfsck_env_info(env);
5089 struct lu_buf *buf = &info->lti_big_buf;
5090 struct ost_id *oi = &info->lti_oi;
5091 struct dt_object *obj;
5092 struct lov_mds_md_v1 *lmm;
5093 struct lov_ost_data_v1 *objs;
5094 __u32 idx = pfid->f_stripe_idx;
5102 obj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
5104 RETURN(PTR_ERR(obj));
5106 dt_read_lock(env, obj, 0);
5107 if (unlikely(dt_object_exists(obj) == 0 ||
5108 lfsck_is_dead_obj(obj)))
5109 GOTO(unlock, rc = -ENOENT);
5111 if (!S_ISREG(lfsck_object_type(obj)))
5112 GOTO(unlock, rc = -ENODATA);
5114 rc = lfsck_layout_get_lovea(env, obj, buf);
5119 magic = le32_to_cpu(lmm->lmm_magic);
5120 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5121 struct lov_comp_md_v1 *lcm = buf->lb_buf;
5122 struct lov_comp_md_entry_v1 *lcme;
5125 GOTO(unlock, rc = -ENODATA);
5127 count = le16_to_cpu(lcm->lcm_entry_count);
5128 for (i = 0; i < count; i++) {
5129 lcme = &lcm->lcm_entries[i];
5130 if (le32_to_cpu(lcme->lcme_id) == comp_id) {
5132 le32_to_cpu(lcme->lcme_offset);
5133 magic = le32_to_cpu(lmm->lmm_magic);
5134 if (!(le32_to_cpu(lcme->lcme_flags) &
5136 GOTO(unlock, rc = -ENODATA);
5142 GOTO(unlock, rc = -ENODATA);
5146 if (magic == LOV_MAGIC_V1) {
5147 objs = &lmm->lmm_objects[0];
5149 LASSERT(magic == LOV_MAGIC_V3);
5150 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5153 fid_to_ostid(cfid, oi);
5154 count = le16_to_cpu(lmm->lmm_stripe_count);
5155 for (i = 0; i < count; i++, objs++) {
5158 ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
5159 if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
5160 GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
5163 GOTO(unlock, rc = -ENODATA);
5166 dt_read_unlock(env, obj);
5167 lfsck_object_put(env, obj);
5173 * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
5174 * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
5175 * layout EA from MDT to OST. On one hand, the OST no need to understand
5176 * the layout EA structure; on the other hand, it may cause trouble when
5177 * transfer large layout EA from MDT to OST via normal OUT RPC.
5179 * \ret > 0: unrecognized stripe
5180 * \ret = 0: recognized stripe
5181 * \ret < 0: other failures
5183 static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
5184 struct lfsck_component *com,
5185 struct lu_fid *cfid,
5186 struct lu_fid *pfid, __u32 comp_id)
5188 struct lfsck_instance *lfsck = com->lc_lfsck;
5189 struct obd_device *obd = lfsck->li_obd;
5190 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5191 struct obd_export *exp = NULL;
5192 struct ptlrpc_request *req = NULL;
5193 struct lfsck_request *lr;
5194 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
5198 if (unlikely(fid_is_idif(pfid)))
5201 fld_range_set_any(range);
5202 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), range);
5204 RETURN(rc == -ENOENT ? 1 : rc);
5206 if (unlikely(!fld_range_is_mdt(range)))
5209 exp = lustre_find_lwp_by_index(obd->obd_name, range->lsr_index);
5210 if (unlikely(exp == NULL))
5213 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
5214 GOTO(out, rc = -EOPNOTSUPP);
5216 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
5218 GOTO(out, rc = -ENOMEM);
5220 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
5222 ptlrpc_request_free(req);
5227 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
5228 memset(lr, 0, sizeof(*lr));
5229 lr->lr_event = LE_PAIRS_VERIFY;
5230 lr->lr_active = LFSCK_TYPE_LAYOUT;
5231 lr->lr_fid = *cfid; /* OST-object itself FID. */
5232 lr->lr_fid2 = *pfid; /* The claimed parent FID. */
5233 lr->lr_comp_id = comp_id;
5235 ptlrpc_request_set_replen(req);
5236 rc = ptlrpc_queue_wait(req);
5237 ptlrpc_req_finished(req);
5239 if (rc == -ENOENT || rc == -ENODATA)
5246 class_export_put(exp);
5251 static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
5252 struct lfsck_component *com,
5253 struct lfsck_req_local *lrl)
5255 struct dt_object *obj;
5259 obj = lfsck_object_find_bottom(env, com->lc_lfsck, &lrl->lrl_fid);
5261 GOTO(log, rc = PTR_ERR(obj));
5263 rc = __lfsck_layout_update_pfid(env, com, obj,
5264 &lrl->lrl_ff_client.ff_parent,
5265 &lrl->lrl_ff_client.ff_layout,
5266 lrl->lrl_ff_client.ff_layout_version,
5267 lrl->lrl_ff_client.ff_range,
5268 lrl->lrl_ff_client.ff_parent.f_ver);
5270 lfsck_object_put(env, obj);
5273 CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
5274 ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
5275 PFID(&lrl->lrl_fid), PFID(&lrl->lrl_ff_client.ff_parent), rc);
5282 static void lfsck_layout_slave_quit(const struct lu_env *env,
5283 struct lfsck_component *com);
5285 static int lfsck_layout_reset(const struct lu_env *env,
5286 struct lfsck_component *com, bool init)
5288 struct lfsck_layout *lo = com->lc_file_ram;
5291 down_write(&com->lc_sem);
5293 memset(lo, 0, com->lc_file_size);
5295 __u32 count = lo->ll_success_count;
5296 time64_t last_time = lo->ll_time_last_complete;
5298 memset(lo, 0, com->lc_file_size);
5299 lo->ll_success_count = count;
5300 lo->ll_time_last_complete = last_time;
5303 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
5304 lo->ll_status = LS_INIT;
5306 if (com->lc_lfsck->li_master) {
5307 struct lfsck_assistant_data *lad = com->lc_data;
5309 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
5310 bitmap_zero(lad->lad_bitmap, lad->lad_bitmap_count);
5313 rc = lfsck_layout_store(env, com);
5314 if (rc == 0 && com->lc_lfsck->li_master)
5315 rc = lfsck_load_sub_trace_files(env, com,
5316 &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
5317 up_write(&com->lc_sem);
5319 CDEBUG(D_LFSCK, "%s: layout LFSCK reset: rc = %d\n",
5320 lfsck_lfsck2name(com->lc_lfsck), rc);
5325 static void lfsck_layout_fail(const struct lu_env *env,
5326 struct lfsck_component *com, bool new_checked)
5328 struct lfsck_layout *lo = com->lc_file_ram;
5330 down_write(&com->lc_sem);
5332 com->lc_new_checked++;
5333 lfsck_layout_record_failure(env, com->lc_lfsck, lo);
5334 up_write(&com->lc_sem);
5337 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
5338 struct lfsck_component *com, bool init)
5340 struct lfsck_instance *lfsck = com->lc_lfsck;
5341 struct lfsck_layout *lo = com->lc_file_ram;
5345 rc = lfsck_checkpoint_generic(env, com);
5347 return rc > 0 ? 0 : rc;
5350 down_write(&com->lc_sem);
5352 lo->ll_pos_latest_start =
5353 lfsck->li_pos_checkpoint.lp_oit_cookie;
5355 lo->ll_pos_last_checkpoint =
5356 lfsck->li_pos_checkpoint.lp_oit_cookie;
5357 lo->ll_run_time_phase1 += ktime_get_seconds() -
5358 lfsck->li_time_last_checkpoint;
5359 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5360 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5361 com->lc_new_checked = 0;
5364 rc = lfsck_layout_store(env, com);
5365 up_write(&com->lc_sem);
5367 CDEBUG(D_LFSCK, "%s: layout LFSCK master checkpoint at the pos ["
5368 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5369 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5374 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
5375 struct lfsck_component *com, bool init)
5377 struct lfsck_instance *lfsck = com->lc_lfsck;
5378 struct lfsck_layout *lo = com->lc_file_ram;
5381 if (com->lc_new_checked == 0 && !init)
5384 down_write(&com->lc_sem);
5386 lo->ll_pos_latest_start =
5387 lfsck->li_pos_checkpoint.lp_oit_cookie;
5389 lo->ll_pos_last_checkpoint =
5390 lfsck->li_pos_checkpoint.lp_oit_cookie;
5391 lo->ll_run_time_phase1 += ktime_get_seconds() -
5392 lfsck->li_time_last_checkpoint;
5393 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
5394 lo->ll_objs_checked_phase1 += com->lc_new_checked;
5395 com->lc_new_checked = 0;
5398 rc = lfsck_layout_store(env, com);
5399 up_write(&com->lc_sem);
5401 CDEBUG(D_LFSCK, "%s: layout LFSCK slave checkpoint at the pos ["
5402 "%llu], status = %d: rc = %d\n", lfsck_lfsck2name(lfsck),
5403 lfsck->li_pos_current.lp_oit_cookie, lo->ll_status, rc);
5408 static int lfsck_layout_prep(const struct lu_env *env,
5409 struct lfsck_component *com,
5410 struct lfsck_start *start)
5412 struct lfsck_instance *lfsck = com->lc_lfsck;
5413 struct lfsck_layout *lo = com->lc_file_ram;
5414 struct lfsck_position *pos = &com->lc_pos_start;
5416 fid_zero(&pos->lp_dir_parent);
5417 pos->lp_dir_cookie = 0;
5418 if (lo->ll_status == LS_COMPLETED ||
5419 lo->ll_status == LS_PARTIAL ||
5420 /* To handle orphan, must scan from the beginning. */
5421 (start != NULL && start->ls_flags & LPF_OST_ORPHAN)) {
5424 rc = lfsck_layout_reset(env, com, false);
5426 rc = lfsck_set_param(env, lfsck, start, true);
5429 CDEBUG(D_LFSCK, "%s: layout LFSCK prep failed: "
5430 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
5436 down_write(&com->lc_sem);
5437 lo->ll_time_latest_start = ktime_get_real_seconds();
5438 spin_lock(&lfsck->li_lock);
5439 if (lo->ll_flags & LF_SCANNED_ONCE) {
5440 if (!lfsck->li_drop_dryrun ||
5441 lo->ll_pos_first_inconsistent == 0) {
5442 lo->ll_status = LS_SCANNING_PHASE2;
5443 list_move_tail(&com->lc_link,
5444 &lfsck->li_list_double_scan);
5445 pos->lp_oit_cookie = 0;
5449 lo->ll_status = LS_SCANNING_PHASE1;
5450 lo->ll_run_time_phase1 = 0;
5451 lo->ll_run_time_phase2 = 0;
5452 lo->ll_objs_checked_phase1 = 0;
5453 lo->ll_objs_checked_phase2 = 0;
5454 lo->ll_objs_failed_phase1 = 0;
5455 lo->ll_objs_failed_phase2 = 0;
5456 for (i = 0; i < LLIT_MAX; i++)
5457 lo->ll_objs_repaired[i] = 0;
5459 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5460 fid_zero(&com->lc_fid_latest_scanned_phase2);
5463 lo->ll_status = LS_SCANNING_PHASE1;
5464 if (!lfsck->li_drop_dryrun ||
5465 lo->ll_pos_first_inconsistent == 0)
5466 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
5468 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
5470 spin_unlock(&lfsck->li_lock);
5471 up_write(&com->lc_sem);
5476 static int lfsck_layout_slave_prep(const struct lu_env *env,
5477 struct lfsck_component *com,
5478 struct lfsck_start_param *lsp)
5480 struct lfsck_layout_slave_data *llsd = com->lc_data;
5481 struct lfsck_instance *lfsck = com->lc_lfsck;
5482 struct lfsck_layout *lo = com->lc_file_ram;
5483 struct lfsck_start *start = lsp->lsp_start;
5486 rc = lfsck_layout_prep(env, com, start);
5490 if (lo->ll_flags & LF_CRASHED_LASTID &&
5491 list_empty(&llsd->llsd_master_list)) {
5492 LASSERT(lfsck->li_out_notify != NULL);
5494 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
5495 LE_LASTID_REBUILDING);
5498 if (!lsp->lsp_index_valid)
5501 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
5502 if (rc == 0 && start != NULL && start->ls_flags & LPF_OST_ORPHAN) {
5503 LASSERT(!llsd->llsd_rbtree_valid);
5505 down_write(&llsd->llsd_rb_rwsem);
5506 rc = lfsck_rbtree_setup(env, com);
5507 up_write(&llsd->llsd_rb_rwsem);
5510 CDEBUG(D_LFSCK, "%s: layout LFSCK slave prep done, start pos ["
5511 "%llu]\n", lfsck_lfsck2name(lfsck),
5512 com->lc_pos_start.lp_oit_cookie);
5517 static int lfsck_layout_master_prep(const struct lu_env *env,
5518 struct lfsck_component *com,
5519 struct lfsck_start_param *lsp)
5524 rc = lfsck_layout_load_bitmap(env, com);
5526 rc = lfsck_layout_reset(env, com, false);
5528 rc = lfsck_set_param(env, com->lc_lfsck,
5529 lsp->lsp_start, true);
5535 rc = lfsck_layout_prep(env, com, lsp->lsp_start);
5539 rc = lfsck_start_assistant(env, com, lsp);
5544 CDEBUG(D_LFSCK, "%s: layout LFSCK master prep done, start pos ["
5545 "%llu]\n", lfsck_lfsck2name(com->lc_lfsck),
5546 com->lc_pos_start.lp_oit_cookie);
5551 /* Pre-fetch the attribute for each stripe in the given layout EA. */
5552 static int lfsck_layout_scan_stripes(const struct lu_env *env,
5553 struct lfsck_component *com,
5554 struct dt_object *parent,
5555 struct lov_mds_md_v1 *lmm, __u32 comp_id)
5557 struct lfsck_thread_info *info = lfsck_env_info(env);
5558 struct lfsck_instance *lfsck = com->lc_lfsck;
5559 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5560 struct lfsck_layout *lo = com->lc_file_ram;
5561 struct lfsck_assistant_data *lad = com->lc_data;
5562 struct lfsck_assistant_object *lso = NULL;
5563 struct lov_ost_data_v1 *objs;
5564 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
5565 struct ptlrpc_thread *mthread = &lfsck->li_thread;
5566 struct ptlrpc_thread *athread = &lad->lad_thread;
5574 lfsck_buf_init(&buf, &info->lti_ff, sizeof(struct filter_fid));
5575 magic = le32_to_cpu(lmm->lmm_magic);
5576 if (magic == LOV_MAGIC_V1) {
5577 objs = &lmm->lmm_objects[0];
5579 LASSERT(magic == LOV_MAGIC_V3);
5580 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
5583 count = le16_to_cpu(lmm->lmm_stripe_count);
5584 for (i = 0; i < count; i++, objs++) {
5585 struct lu_fid *fid = &info->lti_fid;
5586 struct ost_id *oi = &info->lti_oi;
5587 struct lfsck_layout_req *llr;
5588 struct lfsck_tgt_desc *tgt = NULL;
5589 struct dt_object *cobj = NULL;
5591 bool wakeup = false;
5593 if (unlikely(lovea_slot_is_dummy(objs)))
5596 wait_event_idle(mthread->t_ctl_waitq,
5597 lad->lad_prefetched < bk->lb_async_windows ||
5598 !thread_is_running(mthread) ||
5599 thread_is_stopped(athread));
5601 if (unlikely(!thread_is_running(mthread)) ||
5602 thread_is_stopped(athread))
5605 if (unlikely(lfsck_is_dead_obj(parent)))
5608 ostid_le_to_cpu(&objs->l_ost_oi, oi);
5609 index = le32_to_cpu(objs->l_ost_idx);
5610 rc = ostid_to_fid(fid, oi, index);
5612 CDEBUG(D_LFSCK, "%s: get invalid layout EA for "DFID
5613 ": "DOSTID", idx %u, comp_id %u\n",
5614 lfsck_lfsck2name(lfsck),
5615 PFID(lfsck_dto2fid(parent)), POSTID(oi),
5620 tgt = lfsck_tgt_get(ltds, index);
5621 if (unlikely(tgt == NULL)) {
5622 CDEBUG(D_LFSCK, "%s: cannot talk with OST %x which "
5623 "did not join the layout LFSCK, comp_id %u\n",
5624 lfsck_lfsck2name(lfsck), index, comp_id);
5625 lfsck_lad_set_bitmap(env, com, index);
5629 /* There is potential deadlock race condition between object
5630 * destroy and layout LFSCK. Consider the following scenario:
5632 * 1) The LFSCK thread obtained the parent object firstly, at
5633 * that time, the parent object has not been destroyed yet.
5635 * 2) One RPC service thread destroyed the parent and all its
5636 * children objects. Because the LFSCK is referencing the
5637 * parent object, then the parent object will be marked as
5638 * dying in RAM. On the other hand, the parent object is
5639 * referencing all its children objects, then all children
5640 * objects will be marked as dying in RAM also.
5642 * 3) The LFSCK thread tries to find some child object with
5643 * the parent object referenced. Then it will find that the
5644 * child object is dying. According to the object visibility
5645 * rules: the object with dying flag cannot be returned to
5646 * others. So the LFSCK thread has to wait until the dying
5647 * object has been purged from RAM, then it can allocate a
5648 * new object (with the same FID) in RAM. Unfortunately, the
5649 * LFSCK thread itself is referencing the parent object, and
5650 * cause the parent object cannot be purged, then cause the
5651 * child object cannot be purged also. So the LFSCK thread
5652 * will fall into deadlock.
5654 cobj = lfsck_object_find_by_dev(env, tgt->ltd_tgt, fid);
5656 if (lfsck_is_dead_obj(parent)) {
5666 rc = dt_declare_attr_get(env, cobj);
5670 rc = dt_declare_xattr_get(env, cobj, &buf, XATTR_NAME_FID);
5675 struct lu_attr *attr = &info->lti_la;
5677 rc = dt_attr_get(env, parent, attr);
5681 lso = lfsck_assistant_object_init(env,
5682 lfsck_dto2fid(parent), attr,
5683 lfsck->li_pos_current.lp_oit_cookie, false);
5692 llr = lfsck_layout_assistant_req_init(lso, cobj, comp_id,
5700 spin_lock(&lad->lad_lock);
5701 if (lad->lad_assistant_status < 0) {
5702 spin_unlock(&lad->lad_lock);
5703 lfsck_layout_assistant_req_fini(env, &llr->llr_lar);
5705 RETURN(lad->lad_assistant_status);
5708 list_add_tail(&llr->llr_lar.lar_list, &lad->lad_req_list);
5709 if (lad->lad_prefetched == 0)
5712 lad->lad_prefetched++;
5713 spin_unlock(&lad->lad_lock);
5715 wake_up(&athread->t_ctl_waitq);
5718 down_write(&com->lc_sem);
5719 com->lc_new_checked++;
5721 lfsck_layout_record_failure(env, lfsck, lo);
5722 up_write(&com->lc_sem);
5724 if (cobj != NULL && !IS_ERR(cobj))
5725 lfsck_object_put(env, cobj);
5727 if (likely(tgt != NULL))
5730 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5738 lfsck_assistant_object_put(env, lso);
5743 /* For the given object, read its layout EA locally. For each stripe, pre-fetch
5744 * the OST-object's attribute and generate an structure lfsck_layout_req on the
5745 * list ::lad_req_list.
5747 * For each request on above list, the lfsck_layout_assistant thread compares
5748 * the OST side attribute with local attribute, if inconsistent, then repair it.
5750 * All above processing is async mode with pipeline. */
5751 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
5752 struct lfsck_component *com,
5753 struct dt_object *obj)
5755 struct lfsck_thread_info *info = lfsck_env_info(env);
5756 struct ost_id *oi = &info->lti_oi;
5757 struct lfsck_layout *lo = com->lc_file_ram;
5758 struct lfsck_assistant_data *lad = com->lc_data;
5759 struct lfsck_instance *lfsck = com->lc_lfsck;
5760 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5761 struct thandle *handle = NULL;
5762 struct lu_buf *buf = &info->lti_big_buf;
5763 struct lov_mds_md_v1 *lmm = NULL;
5764 struct dt_device *dev = lfsck_obj2dev(obj);
5765 struct lustre_handle lh = { 0 };
5766 struct lu_buf ea_buf = { NULL };
5767 struct lov_comp_md_v1 *lcm = NULL;
5768 struct lov_comp_md_entry_v1 *lcme = NULL;
5773 bool locked = false;
5774 bool stripe = false;
5775 bool bad_oi = false;
5778 if (!S_ISREG(lfsck_object_type(obj)))
5781 if (lad->lad_assistant_status < 0)
5782 GOTO(out, rc = -ESRCH);
5784 fid_to_lmm_oi(lfsck_dto2fid(obj), oi);
5785 lmm_oi_cpu_to_le(oi, oi);
5786 dt_read_lock(env, obj, 0);
5791 if (dt_object_exists(obj) == 0 ||
5792 lfsck_is_dead_obj(obj))
5795 rc = lfsck_layout_get_lovea(env, obj, buf);
5796 if (rc == -EINVAL || rc == -ENODATA || rc == -EOPNOTSUPP)
5797 /* Skip bad lov EA during the 1st cycle scanning, and
5798 * try to recover it via orphan in the 2nd scanning. */
5805 magic = le32_to_cpu(lmm->lmm_magic);
5806 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5807 struct lov_mds_md_v1 *v1;
5811 count = le16_to_cpu(lcm->lcm_entry_count);
5812 for (i = 0; i < count; i++) {
5813 lcme = &lcm->lcm_entries[i];
5814 v1 = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5815 if (memcmp(oi, &v1->lmm_oi, sizeof(*oi)) != 0)
5819 GOTO(out, stripe = true);
5820 } else if (memcmp(oi, &lmm->lmm_oi, sizeof(*oi)) == 0) {
5821 GOTO(out, stripe = true);
5825 /* Inconsistent lmm_oi, should be repaired. */
5828 if (bk->lb_param & LPF_DRYRUN) {
5829 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5831 GOTO(out, stripe = true);
5834 if (!lustre_handle_is_used(&lh)) {
5835 dt_read_unlock(env, obj);
5837 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5838 MDS_INODELOCK_LAYOUT |
5839 MDS_INODELOCK_XATTR, LCK_EX);
5843 handle = lfsck_trans_create(env, dev, lfsck);
5845 GOTO(out, rc = PTR_ERR(handle));
5847 lfsck_buf_init(&ea_buf, buf->lb_buf, size);
5848 rc = dt_declare_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5849 LU_XATTR_REPLACE, handle);
5853 rc = dt_trans_start_local(env, dev, handle);
5857 dt_write_lock(env, obj, 0);
5863 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5864 struct lov_mds_md_v1 *v1;
5867 for (i = 0; i < count; i++) {
5868 lcme = &lcm->lcm_entries[i];
5869 v1 = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
5876 rc = dt_xattr_set(env, obj, &ea_buf, XATTR_NAME_LOV,
5877 LU_XATTR_REPLACE, handle);
5881 lo->ll_objs_repaired[LLIT_OTHERS - 1]++;
5883 GOTO(out, stripe = true);
5887 if (lustre_handle_is_used(&lh))
5888 dt_write_unlock(env, obj);
5890 dt_read_unlock(env, obj);
5893 if (handle != NULL && !IS_ERR(handle))
5894 dt_trans_stop(env, dev, handle);
5896 lfsck_ibits_unlock(&lh, LCK_EX);
5899 CDEBUG(D_LFSCK, "%s: layout LFSCK master %s bad lmm_oi for "
5900 DFID": rc = %d\n", lfsck_lfsck2name(lfsck),
5901 bk->lb_param & LPF_DRYRUN ? "found" : "repaired",
5902 PFID(lfsck_dto2fid(obj)), rc);
5905 if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
5908 for (i = 0; i < count; i++) {
5909 lcme = &lcm->lcm_entries[i];
5910 if (!(le32_to_cpu(lcme->lcme_flags) &
5914 rc = lfsck_layout_scan_stripes(env, com, obj,
5915 (struct lov_mds_md_v1 *)(buf->lb_buf +
5916 le32_to_cpu(lcme->lcme_offset)),
5917 le32_to_cpu(lcme->lcme_id));
5920 rc = lfsck_layout_scan_stripes(env, com, obj, lmm, 0);
5923 down_write(&com->lc_sem);
5924 com->lc_new_checked++;
5926 lfsck_layout_record_failure(env, lfsck, lo);
5927 up_write(&com->lc_sem);
5933 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
5934 struct lfsck_component *com,
5935 struct dt_object *obj)
5937 struct lfsck_instance *lfsck = com->lc_lfsck;
5938 struct lfsck_layout *lo = com->lc_file_ram;
5939 const struct lu_fid *fid = lfsck_dto2fid(obj);
5940 struct lfsck_layout_slave_data *llsd = com->lc_data;
5941 struct lfsck_layout_seq *lls;
5947 LASSERT(llsd != NULL);
5949 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY5) &&
5950 cfs_fail_val == lfsck_dev_idx(lfsck)) {
5951 struct ptlrpc_thread *thread = &lfsck->li_thread;
5953 wait_event_idle_timeout(thread->t_ctl_waitq,
5954 !thread_is_running(thread),
5955 cfs_time_seconds(1));
5958 lfsck_rbtree_update_bitmap(env, com, fid, false);
5960 down_write(&com->lc_sem);
5961 if (fid_is_idif(fid))
5963 else if (!fid_is_norm(fid) ||
5964 !fid_is_for_ostobj(env, lfsck, obj, fid))
5965 GOTO(unlock, rc = 0);
5968 com->lc_new_checked++;
5970 lls = lfsck_layout_seq_lookup(llsd, seq);
5973 if (unlikely(lls == NULL))
5974 GOTO(unlock, rc = -ENOMEM);
5976 INIT_LIST_HEAD(&lls->lls_list);
5978 rc = lfsck_layout_lastid_load(env, com, lls);
5980 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
5981 "load LAST_ID for %#llx: rc = %d\n",
5982 lfsck_lfsck2name(com->lc_lfsck), seq, rc);
5983 lo->ll_objs_failed_phase1++;
5988 lfsck_layout_seq_insert(llsd, lls);
5991 if (unlikely(fid_is_last_id(fid)))
5992 GOTO(unlock, rc = 0);
5994 if (fid_is_idif(fid))
5995 oid = fid_idif_id(fid_seq(fid), fid_oid(fid), fid_ver(fid));
5999 if (oid > lls->lls_lastid_known)
6000 lls->lls_lastid_known = oid;
6002 if (oid > lls->lls_lastid) {
6003 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
6004 /* OFD may create new objects during LFSCK scanning. */
6005 rc = lfsck_layout_lastid_reload(env, com, lls);
6006 if (unlikely(rc != 0)) {
6007 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to "
6008 "reload LAST_ID for %#llx: rc = %d\n",
6009 lfsck_lfsck2name(com->lc_lfsck),
6015 if (oid <= lls->lls_lastid ||
6016 lo->ll_flags & LF_CRASHED_LASTID)
6017 GOTO(unlock, rc = 0);
6019 LASSERT(lfsck->li_out_notify != NULL);
6021 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6022 LE_LASTID_REBUILDING);
6023 lo->ll_flags |= LF_CRASHED_LASTID;
6025 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
6026 "LAST_ID file (2) for the sequence %#llx"
6027 ", old value %llu, known value %llu\n",
6028 lfsck_lfsck2name(lfsck), lls->lls_seq,
6029 lls->lls_lastid, oid);
6032 lls->lls_lastid = oid;
6036 GOTO(unlock, rc = 0);
6039 up_write(&com->lc_sem);
6044 static int lfsck_layout_exec_dir(const struct lu_env *env,
6045 struct lfsck_component *com,
6046 struct lfsck_assistant_object *lso,
6047 struct lu_dirent *ent, __u16 type)
6052 static int lfsck_layout_master_post(const struct lu_env *env,
6053 struct lfsck_component *com,
6054 int result, bool init)
6056 struct lfsck_instance *lfsck = com->lc_lfsck;
6057 struct lfsck_layout *lo = com->lc_file_ram;
6061 lfsck_post_generic(env, com, &result);
6063 down_write(&com->lc_sem);
6064 spin_lock(&lfsck->li_lock);
6066 lo->ll_pos_last_checkpoint =
6067 lfsck->li_pos_checkpoint.lp_oit_cookie;
6070 if (lo->ll_flags & LF_INCOMPLETE)
6071 lo->ll_status = LS_PARTIAL;
6073 lo->ll_status = LS_SCANNING_PHASE2;
6074 lo->ll_flags |= LF_SCANNED_ONCE;
6075 lo->ll_flags &= ~LF_UPGRADE;
6076 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
6077 } else if (result == 0) {
6078 if (lfsck->li_status != 0)
6079 lo->ll_status = lfsck->li_status;
6081 lo->ll_status = LS_STOPPED;
6082 if (lo->ll_status != LS_PAUSED)
6083 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6085 lo->ll_status = LS_FAILED;
6086 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6088 spin_unlock(&lfsck->li_lock);
6091 lo->ll_run_time_phase1 += ktime_get_seconds() -
6092 lfsck->li_time_last_checkpoint;
6093 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
6094 lo->ll_objs_checked_phase1 += com->lc_new_checked;
6095 com->lc_new_checked = 0;
6098 rc = lfsck_layout_store(env, com);
6099 up_write(&com->lc_sem);
6101 CDEBUG(D_LFSCK, "%s: layout LFSCK master post done: rc = %d\n",
6102 lfsck_lfsck2name(lfsck), rc);
6107 static int lfsck_layout_slave_post(const struct lu_env *env,
6108 struct lfsck_component *com,
6109 int result, bool init)
6111 struct lfsck_instance *lfsck = com->lc_lfsck;
6112 struct lfsck_layout *lo = com->lc_file_ram;
6116 down_write(&com->lc_sem);
6117 rc = lfsck_layout_lastid_store(env, com);
6121 LASSERT(lfsck->li_out_notify != NULL);
6123 spin_lock(&lfsck->li_lock);
6125 lo->ll_pos_last_checkpoint =
6126 lfsck->li_pos_checkpoint.lp_oit_cookie;
6129 lo->ll_status = LS_SCANNING_PHASE2;
6130 lo->ll_flags |= LF_SCANNED_ONCE;
6131 if (lo->ll_flags & LF_CRASHED_LASTID) {
6133 lo->ll_flags &= ~LF_CRASHED_LASTID;
6135 CDEBUG(D_LFSCK, "%s: layout LFSCK has rebuilt "
6136 "crashed LAST_ID files successfully\n",
6137 lfsck_lfsck2name(lfsck));
6139 lo->ll_flags &= ~LF_UPGRADE;
6140 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
6141 } else if (result == 0) {
6142 if (lfsck->li_status != 0)
6143 lo->ll_status = lfsck->li_status;
6145 lo->ll_status = LS_STOPPED;
6146 if (lo->ll_status != LS_PAUSED)
6147 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6149 lo->ll_status = LS_FAILED;
6150 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
6152 spin_unlock(&lfsck->li_lock);
6155 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
6159 lo->ll_run_time_phase1 += ktime_get_seconds() -
6160 lfsck->li_time_last_checkpoint;
6161 lo->ll_time_last_checkpoint = ktime_get_real_seconds();
6162 lo->ll_objs_checked_phase1 += com->lc_new_checked;
6163 com->lc_new_checked = 0;
6166 rc = lfsck_layout_store(env, com);
6167 up_write(&com->lc_sem);
6169 lfsck_layout_slave_notify_master(env, com, LE_PHASE1_DONE, result);
6171 CDEBUG(D_LFSCK, "%s: layout LFSCK slave post done: rc = %d\n",
6172 lfsck_lfsck2name(lfsck), rc);
6177 static void lfsck_layout_dump(const struct lu_env *env,
6178 struct lfsck_component *com, struct seq_file *m)
6180 struct lfsck_instance *lfsck = com->lc_lfsck;
6181 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6182 struct lfsck_layout *lo = com->lc_file_ram;
6185 down_read(&com->lc_sem);
6186 if (bk->lb_param & LPF_DRYRUN)
6187 prefix = "inconsistent";
6189 prefix = "repaired";
6191 seq_printf(m, "name: lfsck_layout\n"
6197 lfsck_status2name(lo->ll_status));
6199 lfsck_bits_dump(m, lo->ll_flags, lfsck_flags_names, "flags");
6201 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
6203 lfsck_time_dump(m, lo->ll_time_last_complete, "last_completed");
6205 lfsck_time_dump(m, lo->ll_time_latest_start, "latest_start");
6207 lfsck_time_dump(m, lo->ll_time_last_checkpoint, "last_checkpoint");
6209 seq_printf(m, "latest_start_position: %llu\n"
6210 "last_checkpoint_position: %llu\n"
6211 "first_failure_position: %llu\n",
6212 lo->ll_pos_latest_start,
6213 lo->ll_pos_last_checkpoint,
6214 lo->ll_pos_first_inconsistent);
6216 seq_printf(m, "success_count: %u\n"
6217 "%s_dangling: %llu\n"
6218 "%s_unmatched_pair: %llu\n"
6219 "%s_multiple_referenced: %llu\n"
6221 "%s_inconsistent_owner: %llu\n"
6224 "failed_phase1: %llu\n"
6225 "failed_phase2: %llu\n",
6226 lo->ll_success_count,
6227 prefix, lo->ll_objs_repaired[LLIT_DANGLING - 1],
6228 prefix, lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
6229 prefix, lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
6230 prefix, lo->ll_objs_repaired[LLIT_ORPHAN - 1],
6231 prefix, lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
6232 prefix, lo->ll_objs_repaired[LLIT_OTHERS - 1],
6233 lo->ll_objs_skipped,
6234 lo->ll_objs_failed_phase1,
6235 lo->ll_objs_failed_phase2);
6237 if (lo->ll_status == LS_SCANNING_PHASE1) {
6238 time64_t duration = ktime_get_seconds() -
6239 lfsck->li_time_last_checkpoint;
6240 u64 checked = lo->ll_objs_checked_phase1 +
6241 com->lc_new_checked;
6242 u64 speed = checked;
6243 u64 new_checked = com->lc_new_checked;
6244 time64_t rtime = lo->ll_run_time_phase1 + duration;
6248 new_checked = div64_s64(new_checked, duration);
6250 speed = div64_s64(speed, rtime);
6251 seq_printf(m, "checked_phase1: %llu\n"
6252 "checked_phase2: %llu\n"
6253 "run_time_phase1: %lld seconds\n"
6254 "run_time_phase2: %lld seconds\n"
6255 "average_speed_phase1: %llu items/sec\n"
6256 "average_speed_phase2: N/A\n"
6257 "real_time_speed_phase1: %llu items/sec\n"
6258 "real_time_speed_phase2: N/A\n",
6260 lo->ll_objs_checked_phase2,
6262 lo->ll_run_time_phase2,
6266 if (likely(lfsck->li_di_oit)) {
6267 const struct dt_it_ops *iops =
6268 &lfsck->li_obj_oit->do_index_ops->dio_it;
6270 /* The low layer otable-based iteration position may NOT
6271 * exactly match the layout-based directory traversal
6272 * cookie. Generally, it is not a serious issue. But the
6273 * caller should NOT make assumption on that. */
6274 pos = iops->store(env, lfsck->li_di_oit);
6275 if (!lfsck->li_current_oit_processed)
6278 pos = lo->ll_pos_last_checkpoint;
6281 seq_printf(m, "current_position: %llu\n", pos);
6282 } else if (lo->ll_status == LS_SCANNING_PHASE2) {
6283 time64_t duration = ktime_get_seconds() -
6284 com->lc_time_last_checkpoint;
6285 u64 checked = lo->ll_objs_checked_phase2 +
6286 com->lc_new_checked;
6287 u64 speed1 = lo->ll_objs_checked_phase1;
6288 u64 speed2 = checked;
6289 u64 new_checked = com->lc_new_checked;
6290 time64_t rtime = lo->ll_run_time_phase2 + duration;
6293 new_checked = div64_s64(new_checked, duration);
6294 if (lo->ll_run_time_phase1 != 0)
6295 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6297 speed2 = div64_s64(speed2, rtime);
6298 seq_printf(m, "checked_phase1: %llu\n"
6299 "checked_phase2: %llu\n"
6300 "run_time_phase1: %lld seconds\n"
6301 "run_time_phase2: %lld seconds\n"
6302 "average_speed_phase1: %llu items/sec\n"
6303 "average_speed_phase2: %llu items/sec\n"
6304 "real_time_speed_phase1: N/A\n"
6305 "real_time_speed_phase2: %llu items/sec\n"
6306 "current_position: "DFID"\n",
6307 lo->ll_objs_checked_phase1,
6309 lo->ll_run_time_phase1,
6314 PFID(&com->lc_fid_latest_scanned_phase2));
6316 __u64 speed1 = lo->ll_objs_checked_phase1;
6317 __u64 speed2 = lo->ll_objs_checked_phase2;
6319 if (lo->ll_run_time_phase1 != 0)
6320 speed1 = div64_s64(speed1, lo->ll_run_time_phase1);
6321 if (lo->ll_run_time_phase2 != 0)
6322 speed2 = div64_s64(speed2, lo->ll_run_time_phase2);
6323 seq_printf(m, "checked_phase1: %llu\n"
6324 "checked_phase2: %llu\n"
6325 "run_time_phase1: %lld seconds\n"
6326 "run_time_phase2: %lld seconds\n"
6327 "average_speed_phase1: %llu items/sec\n"
6328 "average_speed_phase2: %llu objs/sec\n"
6329 "real_time_speed_phase1: N/A\n"
6330 "real_time_speed_phase2: N/A\n"
6331 "current_position: N/A\n",
6332 lo->ll_objs_checked_phase1,
6333 lo->ll_objs_checked_phase2,
6334 lo->ll_run_time_phase1,
6335 lo->ll_run_time_phase2,
6340 up_read(&com->lc_sem);
6343 static int lfsck_layout_master_double_scan(const struct lu_env *env,
6344 struct lfsck_component *com)
6346 struct lfsck_layout *lo = com->lc_file_ram;
6347 struct lfsck_assistant_data *lad = com->lc_data;
6348 struct lfsck_instance *lfsck = com->lc_lfsck;
6349 struct lfsck_tgt_descs *ltds;
6350 struct lfsck_tgt_desc *ltd;
6351 struct lfsck_tgt_desc *next;
6354 rc = lfsck_double_scan_generic(env, com, lo->ll_status);
6356 if (thread_is_stopped(&lad->lad_thread)) {
6357 LASSERT(list_empty(&lad->lad_req_list));
6358 LASSERT(list_empty(&lad->lad_ost_phase1_list));
6359 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
6361 ltds = &lfsck->li_ost_descs;
6362 spin_lock(<ds->ltd_lock);
6363 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6364 ltd_layout_phase_list) {
6365 list_del_init(<d->ltd_layout_phase_list);
6367 spin_unlock(<ds->ltd_lock);
6369 ltds = &lfsck->li_mdt_descs;
6370 spin_lock(<ds->ltd_lock);
6371 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6372 ltd_layout_phase_list) {
6373 list_del_init(<d->ltd_layout_phase_list);
6375 spin_unlock(<ds->ltd_lock);
6381 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
6382 struct lfsck_component *com)
6384 struct lfsck_instance *lfsck = com->lc_lfsck;
6385 struct lfsck_layout_slave_data *llsd = com->lc_data;
6386 struct lfsck_layout *lo = com->lc_file_ram;
6387 struct ptlrpc_thread *thread = &lfsck->li_thread;
6391 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan start\n",
6392 lfsck_lfsck2name(lfsck));
6394 atomic_inc(&lfsck->li_double_scan_count);
6396 if (lo->ll_flags & LF_INCOMPLETE)
6399 com->lc_new_checked = 0;
6400 com->lc_new_scanned = 0;
6401 com->lc_time_last_checkpoint = ktime_get_seconds();
6402 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6403 LFSCK_CHECKPOINT_INTERVAL;
6406 rc = lfsck_layout_slave_query_master(env, com);
6407 if (list_empty(&llsd->llsd_master_list)) {
6408 if (unlikely(!thread_is_running(thread)))
6419 rc = wait_event_idle_timeout(
6420 thread->t_ctl_waitq,
6421 !thread_is_running(thread) ||
6422 lo->ll_flags & LF_INCOMPLETE ||
6423 list_empty(&llsd->llsd_master_list),
6424 cfs_time_seconds(30));
6425 if (unlikely(!thread_is_running(thread)))
6428 if (lo->ll_flags & LF_INCOMPLETE)
6438 rc = lfsck_layout_double_scan_result(env, com, rc);
6439 lfsck_layout_slave_notify_master(env, com, LE_PHASE2_DONE,
6440 (rc > 0 && lo->ll_flags & LF_INCOMPLETE) ? 0 : rc);
6441 lfsck_layout_slave_quit(env, com);
6442 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
6443 wake_up(&lfsck->li_thread.t_ctl_waitq);
6445 CDEBUG(D_LFSCK, "%s: layout LFSCK slave phase2 scan finished, "
6446 "status %d: rc = %d\n",
6447 lfsck_lfsck2name(lfsck), lo->ll_status, rc);
6452 static void lfsck_layout_master_data_release(const struct lu_env *env,
6453 struct lfsck_component *com)
6455 struct lfsck_assistant_data *lad = com->lc_data;
6456 struct lfsck_instance *lfsck = com->lc_lfsck;
6457 struct lfsck_tgt_descs *ltds;
6458 struct lfsck_tgt_desc *ltd;
6459 struct lfsck_tgt_desc *next;
6461 LASSERT(lad != NULL);
6462 LASSERT(thread_is_init(&lad->lad_thread) ||
6463 thread_is_stopped(&lad->lad_thread));
6464 LASSERT(list_empty(&lad->lad_req_list));
6466 com->lc_data = NULL;
6468 ltds = &lfsck->li_ost_descs;
6469 spin_lock(<ds->ltd_lock);
6470 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6471 ltd_layout_phase_list) {
6472 list_del_init(<d->ltd_layout_phase_list);
6474 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6475 ltd_layout_phase_list) {
6476 list_del_init(<d->ltd_layout_phase_list);
6478 list_for_each_entry_safe(ltd, next, &lad->lad_ost_list,
6480 list_del_init(<d->ltd_layout_list);
6482 spin_unlock(<ds->ltd_lock);
6484 ltds = &lfsck->li_mdt_descs;
6485 spin_lock(<ds->ltd_lock);
6486 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6487 ltd_layout_phase_list) {
6488 list_del_init(<d->ltd_layout_phase_list);
6490 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6491 ltd_layout_phase_list) {
6492 list_del_init(<d->ltd_layout_phase_list);
6494 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
6496 list_del_init(<d->ltd_layout_list);
6498 spin_unlock(<ds->ltd_lock);
6500 bitmap_free(lad->lad_bitmap);
6505 static void lfsck_layout_slave_data_release(const struct lu_env *env,
6506 struct lfsck_component *com)
6508 struct lfsck_layout_slave_data *llsd = com->lc_data;
6510 lfsck_layout_slave_quit(env, com);
6511 com->lc_data = NULL;
6515 static void lfsck_layout_master_quit(const struct lu_env *env,
6516 struct lfsck_component *com)
6518 struct lfsck_assistant_data *lad = com->lc_data;
6519 struct lfsck_instance *lfsck = com->lc_lfsck;
6520 struct lfsck_tgt_descs *ltds;
6521 struct lfsck_tgt_desc *ltd;
6522 struct lfsck_tgt_desc *next;
6524 LASSERT(lad != NULL);
6526 lfsck_quit_generic(env, com);
6528 LASSERT(thread_is_init(&lad->lad_thread) ||
6529 thread_is_stopped(&lad->lad_thread));
6530 LASSERT(list_empty(&lad->lad_req_list));
6532 ltds = &lfsck->li_ost_descs;
6533 spin_lock(<ds->ltd_lock);
6534 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase1_list,
6535 ltd_layout_phase_list) {
6536 list_del_init(<d->ltd_layout_phase_list);
6538 list_for_each_entry_safe(ltd, next, &lad->lad_ost_phase2_list,
6539 ltd_layout_phase_list) {
6540 list_del_init(<d->ltd_layout_phase_list);
6542 spin_unlock(<ds->ltd_lock);
6544 ltds = &lfsck->li_mdt_descs;
6545 spin_lock(<ds->ltd_lock);
6546 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
6547 ltd_layout_phase_list) {
6548 list_del_init(<d->ltd_layout_phase_list);
6550 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
6551 ltd_layout_phase_list) {
6552 list_del_init(<d->ltd_layout_phase_list);
6554 spin_unlock(<ds->ltd_lock);
6557 static void lfsck_layout_slave_quit(const struct lu_env *env,
6558 struct lfsck_component *com)
6560 struct lfsck_layout_slave_data *llsd = com->lc_data;
6561 struct lfsck_layout_seq *lls;
6562 struct lfsck_layout_seq *next;
6563 struct lfsck_layout_slave_target *llst;
6565 LASSERT(llsd != NULL);
6567 down_write(&com->lc_sem);
6568 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
6570 list_del_init(&lls->lls_list);
6571 lfsck_object_put(env, lls->lls_lastid_obj);
6574 up_write(&com->lc_sem);
6576 spin_lock(&llsd->llsd_lock);
6577 while (!list_empty(&llsd->llsd_master_list)) {
6578 llst = list_first_entry(&llsd->llsd_master_list,
6579 struct lfsck_layout_slave_target,
6581 list_del_init(&llst->llst_list);
6582 spin_unlock(&llsd->llsd_lock);
6583 lfsck_layout_llst_put(llst);
6584 spin_lock(&llsd->llsd_lock);
6586 spin_unlock(&llsd->llsd_lock);
6588 lfsck_rbtree_cleanup(env, com);
6591 static int lfsck_layout_master_in_notify(const struct lu_env *env,
6592 struct lfsck_component *com,
6593 struct lfsck_request *lr)
6595 struct lfsck_instance *lfsck = com->lc_lfsck;
6596 struct lfsck_layout *lo = com->lc_file_ram;
6597 struct lfsck_assistant_data *lad = com->lc_data;
6598 struct lfsck_tgt_descs *ltds;
6599 struct lfsck_tgt_desc *ltd;
6603 if (lr->lr_event == LE_PAIRS_VERIFY) {
6606 rc = lfsck_layout_master_check_pairs(env, com, &lr->lr_fid,
6613 CDEBUG(D_LFSCK, "%s: layout LFSCK master handles notify %u "
6614 "from %s %x, status %d, flags %x, flags2 %x\n",
6615 lfsck_lfsck2name(lfsck), lr->lr_event,
6616 (lr->lr_flags & LEF_FROM_OST) ? "OST" : "MDT",
6617 lr->lr_index, lr->lr_status, lr->lr_flags, lr->lr_flags2);
6619 if (lr->lr_event != LE_PHASE1_DONE &&
6620 lr->lr_event != LE_PHASE2_DONE &&
6621 lr->lr_event != LE_PEER_EXIT)
6624 if (lr->lr_flags & LEF_FROM_OST)
6625 ltds = &lfsck->li_ost_descs;
6627 ltds = &lfsck->li_mdt_descs;
6628 spin_lock(<ds->ltd_lock);
6629 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
6631 spin_unlock(<ds->ltd_lock);
6636 list_del_init(<d->ltd_layout_phase_list);
6637 switch (lr->lr_event) {
6638 case LE_PHASE1_DONE:
6639 if (lr->lr_status <= 0 || lr->lr_flags2 & LF_INCOMPLETE) {
6640 if (lr->lr_flags2 & LF_INCOMPLETE) {
6641 if (lr->lr_flags & LEF_FROM_OST)
6642 lfsck_lad_set_bitmap(env, com,
6645 lo->ll_flags |= LF_INCOMPLETE;
6647 ltd->ltd_layout_done = 1;
6648 list_del_init(<d->ltd_layout_list);
6653 if (lr->lr_flags & LEF_FROM_OST) {
6654 if (list_empty(<d->ltd_layout_list))
6655 list_add_tail(<d->ltd_layout_list,
6656 &lad->lad_ost_list);
6657 list_add_tail(<d->ltd_layout_phase_list,
6658 &lad->lad_ost_phase2_list);
6660 if (list_empty(<d->ltd_layout_list))
6661 list_add_tail(<d->ltd_layout_list,
6662 &lad->lad_mdt_list);
6663 list_add_tail(<d->ltd_layout_phase_list,
6664 &lad->lad_mdt_phase2_list);
6667 case LE_PHASE2_DONE:
6668 ltd->ltd_layout_done = 1;
6669 if (!list_empty(<d->ltd_layout_list))
6670 list_del_init(<d->ltd_layout_list);
6672 if (lr->lr_flags2 & LF_INCOMPLETE) {
6673 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
6680 ltd->ltd_layout_done = 1;
6681 list_del_init(<d->ltd_layout_list);
6682 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) &&
6683 !(lr->lr_flags & LEF_FROM_OST))
6684 lo->ll_flags |= LF_INCOMPLETE;
6689 spin_unlock(<ds->ltd_lock);
6691 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
6692 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6694 memset(stop, 0, sizeof(*stop));
6695 stop->ls_status = lr->lr_status;
6696 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6697 lfsck_stop(env, lfsck->li_bottom, stop);
6698 } else if (lfsck_phase2_next_ready(lad)) {
6699 wake_up(&lad->lad_thread.t_ctl_waitq);
6705 static int lfsck_layout_slave_in_notify_local(const struct lu_env *env,
6706 struct lfsck_component *com,
6707 struct lfsck_req_local *lrl,
6712 switch (lrl->lrl_event) {
6713 case LEL_FID_ACCESSED:
6714 lfsck_rbtree_update_bitmap(env, com, &lrl->lrl_fid, true);
6716 case LEL_PAIRS_VERIFY_LOCAL: {
6719 lrl->lrl_status = LPVS_INIT;
6720 /* Firstly, if the MDT-object which is claimed via OST-object
6721 * local stored PFID xattr recognizes the OST-object, then it
6722 * must be that the client given PFID is wrong. */
6723 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6724 &lrl->lrl_ff_local.ff_parent,
6725 lrl->lrl_ff_local.ff_layout.ol_comp_id);
6729 lrl->lrl_status = LPVS_INCONSISTENT;
6730 /* The OST-object local stored PFID xattr is stale. We need to
6731 * check whether the MDT-object that is claimed via the client
6732 * given PFID information recognizes the OST-object or not. If
6733 * matches, then need to update the OST-object's PFID xattr. */
6734 rc = lfsck_layout_slave_check_pairs(env, com, &lrl->lrl_fid,
6735 &lrl->lrl_ff_client.ff_parent,
6736 lrl->lrl_ff_client.ff_layout.ol_comp_id);
6738 * We are not sure whether the client given PFID information
6739 * is correct or not, do nothing to avoid improper fixing.
6742 * The client given PFID information is also invalid, we can
6743 * NOT fix the OST-object inconsistency.
6746 lrl->lrl_status = LPVS_INCONSISTENT_TOFIX;
6747 rc = lfsck_layout_slave_repair_pfid(env, com, lrl);
6756 RETURN(-EOPNOTSUPP);
6759 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
6760 struct lfsck_component *com,
6761 struct lfsck_request *lr)
6763 struct lfsck_instance *lfsck = com->lc_lfsck;
6764 struct lfsck_layout_slave_data *llsd = com->lc_data;
6765 struct lfsck_layout_slave_target *llst;
6769 switch (lr->lr_event) {
6770 case LE_CONDITIONAL_DESTROY:
6771 rc = lfsck_layout_slave_conditional_destroy(env, com, lr);
6773 case LE_PHASE1_DONE: {
6774 if (lr->lr_flags2 & LF_INCOMPLETE) {
6775 struct lfsck_layout *lo = com->lc_file_ram;
6777 lo->ll_flags |= LF_INCOMPLETE;
6778 llst = lfsck_layout_llst_find_and_del(llsd,
6782 lfsck_layout_llst_put(llst);
6783 wake_up(&lfsck->li_thread.t_ctl_waitq);
6789 case LE_PHASE2_DONE:
6791 CDEBUG(D_LFSCK, "%s: layout LFSCK slave handle notify %u "
6792 "from MDT %x, status %d\n", lfsck_lfsck2name(lfsck),
6793 lr->lr_event, lr->lr_index, lr->lr_status);
6799 llst = lfsck_layout_llst_find_and_del(llsd, lr->lr_index, true);
6803 lfsck_layout_llst_put(llst);
6804 if (list_empty(&llsd->llsd_master_list))
6805 wake_up(&lfsck->li_thread.t_ctl_waitq);
6807 if (lr->lr_event == LE_PEER_EXIT &&
6808 (lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT ||
6809 (list_empty(&llsd->llsd_master_list) &&
6810 (lr->lr_status == LS_STOPPED ||
6811 lr->lr_status == LS_CO_STOPPED)))) {
6812 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
6814 memset(stop, 0, sizeof(*stop));
6815 stop->ls_status = lr->lr_status;
6816 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
6817 lfsck_stop(env, lfsck->li_bottom, stop);
6823 static void lfsck_layout_repaired(struct lfsck_layout *lo, __u64 *count)
6827 for (i = 0; i < LLIT_MAX; i++)
6828 *count += lo->ll_objs_repaired[i];
6831 static int lfsck_layout_query_all(const struct lu_env *env,
6832 struct lfsck_component *com,
6833 __u32 *mdts_count, __u32 *osts_count,
6836 struct lfsck_layout *lo = com->lc_file_ram;
6837 struct lfsck_tgt_descs *ltds;
6838 struct lfsck_tgt_desc *ltd;
6843 rc = lfsck_query_all(env, com);
6847 ltds = &com->lc_lfsck->li_mdt_descs;
6848 down_read(<ds->ltd_rw_sem);
6849 for_each_set_bit(idx, ltds->ltd_tgts_bitmap, ltds->ltd_tgts_mask_len) {
6850 ltd = lfsck_ltd2tgt(ltds, idx);
6851 LASSERT(ltd != NULL);
6853 mdts_count[ltd->ltd_layout_status]++;
6854 *repaired += ltd->ltd_layout_repaired;
6856 up_read(<ds->ltd_rw_sem);
6858 ltds = &com->lc_lfsck->li_ost_descs;
6859 down_read(<ds->ltd_rw_sem);
6860 for_each_set_bit(idx, ltds->ltd_tgts_bitmap, ltds->ltd_tgts_mask_len) {
6861 ltd = lfsck_ltd2tgt(ltds, idx);
6862 LASSERT(ltd != NULL);
6864 osts_count[ltd->ltd_layout_status]++;
6865 *repaired += ltd->ltd_layout_repaired;
6867 up_read(<ds->ltd_rw_sem);
6869 down_read(&com->lc_sem);
6870 mdts_count[lo->ll_status]++;
6871 lfsck_layout_repaired(lo, repaired);
6872 up_read(&com->lc_sem);
6877 static int lfsck_layout_query(const struct lu_env *env,
6878 struct lfsck_component *com,
6879 struct lfsck_request *req,
6880 struct lfsck_reply *rep,
6881 struct lfsck_query *que, int idx)
6883 struct lfsck_layout *lo = com->lc_file_ram;
6887 LASSERT(com->lc_lfsck->li_master);
6889 rc = lfsck_layout_query_all(env, com,
6890 que->lu_mdts_count[idx],
6891 que->lu_osts_count[idx],
6892 &que->lu_repaired[idx]);
6894 down_read(&com->lc_sem);
6895 rep->lr_status = lo->ll_status;
6896 if (req->lr_flags & LEF_QUERY_ALL)
6897 lfsck_layout_repaired(lo, &rep->lr_repaired);
6898 up_read(&com->lc_sem);
6904 /* with lfsck::li_lock held */
6905 static int lfsck_layout_slave_join(const struct lu_env *env,
6906 struct lfsck_component *com,
6907 struct lfsck_start_param *lsp)
6909 struct lfsck_instance *lfsck = com->lc_lfsck;
6910 struct lfsck_layout_slave_data *llsd = com->lc_data;
6911 struct lfsck_layout_slave_target *llst;
6912 struct lfsck_start *start = lsp->lsp_start;
6916 if (start == NULL || !(start->ls_flags & LPF_OST_ORPHAN))
6919 if (!lsp->lsp_index_valid)
6922 /* If someone is running the LFSCK without orphan handling,
6923 * it will not maintain the object accessing rbtree. So we
6924 * cannot join it for orphan handling. */
6925 if (!llsd->llsd_rbtree_valid)
6928 spin_unlock(&lfsck->li_lock);
6929 rc = lfsck_layout_llst_add(llsd, lsp->lsp_index);
6930 spin_lock(&lfsck->li_lock);
6931 if (rc == 0 && !thread_is_running(&lfsck->li_thread)) {
6932 spin_unlock(&lfsck->li_lock);
6933 llst = lfsck_layout_llst_find_and_del(llsd, lsp->lsp_index,
6936 lfsck_layout_llst_put(llst);
6937 spin_lock(&lfsck->li_lock);
6944 static const struct lfsck_operations lfsck_layout_master_ops = {
6945 .lfsck_reset = lfsck_layout_reset,
6946 .lfsck_fail = lfsck_layout_fail,
6947 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
6948 .lfsck_prep = lfsck_layout_master_prep,
6949 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
6950 .lfsck_exec_dir = lfsck_layout_exec_dir,
6951 .lfsck_post = lfsck_layout_master_post,
6952 .lfsck_dump = lfsck_layout_dump,
6953 .lfsck_double_scan = lfsck_layout_master_double_scan,
6954 .lfsck_data_release = lfsck_layout_master_data_release,
6955 .lfsck_quit = lfsck_layout_master_quit,
6956 .lfsck_in_notify = lfsck_layout_master_in_notify,
6957 .lfsck_query = lfsck_layout_query,
6960 static const struct lfsck_operations lfsck_layout_slave_ops = {
6961 .lfsck_reset = lfsck_layout_reset,
6962 .lfsck_fail = lfsck_layout_fail,
6963 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
6964 .lfsck_prep = lfsck_layout_slave_prep,
6965 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
6966 .lfsck_exec_dir = lfsck_layout_exec_dir,
6967 .lfsck_post = lfsck_layout_slave_post,
6968 .lfsck_dump = lfsck_layout_dump,
6969 .lfsck_double_scan = lfsck_layout_slave_double_scan,
6970 .lfsck_data_release = lfsck_layout_slave_data_release,
6971 .lfsck_quit = lfsck_layout_slave_quit,
6972 .lfsck_in_notify_local = lfsck_layout_slave_in_notify_local,
6973 .lfsck_in_notify = lfsck_layout_slave_in_notify,
6974 .lfsck_query = lfsck_layout_query,
6975 .lfsck_join = lfsck_layout_slave_join,
6978 static void lfsck_layout_assistant_fill_pos(const struct lu_env *env,
6979 struct lfsck_component *com,
6980 struct lfsck_position *pos)
6982 struct lfsck_assistant_data *lad = com->lc_data;
6983 struct lfsck_layout_req *llr;
6985 if (((struct lfsck_layout *)(com->lc_file_ram))->ll_status !=
6989 if (list_empty(&lad->lad_req_list))
6992 llr = list_first_entry(&lad->lad_req_list,
6993 struct lfsck_layout_req,
6995 pos->lp_oit_cookie = llr->llr_lar.lar_parent->lso_oit_cookie - 1;
6998 const struct lfsck_assistant_operations lfsck_layout_assistant_ops = {
6999 .la_handler_p1 = lfsck_layout_assistant_handler_p1,
7000 .la_handler_p2 = lfsck_layout_assistant_handler_p2,
7001 .la_fill_pos = lfsck_layout_assistant_fill_pos,
7002 .la_double_scan_result = lfsck_layout_double_scan_result,
7003 .la_req_fini = lfsck_layout_assistant_req_fini,
7004 .la_sync_failures = lfsck_layout_assistant_sync_failures,
7007 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
7009 struct lfsck_component *com;
7010 struct lfsck_layout *lo;
7011 struct dt_object *root = NULL;
7012 struct dt_object *obj;
7021 INIT_LIST_HEAD(&com->lc_link);
7022 INIT_LIST_HEAD(&com->lc_link_dir);
7023 init_rwsem(&com->lc_sem);
7024 atomic_set(&com->lc_ref, 1);
7025 com->lc_lfsck = lfsck;
7026 com->lc_type = LFSCK_TYPE_LAYOUT;
7027 if (lfsck->li_master) {
7028 com->lc_ops = &lfsck_layout_master_ops;
7029 com->lc_data = lfsck_assistant_data_init(
7030 &lfsck_layout_assistant_ops,
7032 if (com->lc_data == NULL)
7033 GOTO(out, rc = -ENOMEM);
7035 for (i = 0; i < LFSCK_STF_COUNT; i++)
7036 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
7038 struct lfsck_layout_slave_data *llsd;
7040 com->lc_ops = &lfsck_layout_slave_ops;
7041 OBD_ALLOC_PTR(llsd);
7043 GOTO(out, rc = -ENOMEM);
7045 INIT_LIST_HEAD(&llsd->llsd_seq_list);
7046 INIT_LIST_HEAD(&llsd->llsd_master_list);
7047 spin_lock_init(&llsd->llsd_lock);
7048 llsd->llsd_rb_root = RB_ROOT;
7049 init_rwsem(&llsd->llsd_rb_rwsem);
7050 com->lc_data = llsd;
7052 com->lc_file_size = sizeof(*lo);
7053 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
7054 if (com->lc_file_ram == NULL)
7055 GOTO(out, rc = -ENOMEM);
7057 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
7058 if (com->lc_file_disk == NULL)
7059 GOTO(out, rc = -ENOMEM);
7061 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
7063 GOTO(out, rc = PTR_ERR(root));
7065 if (unlikely(!dt_try_as_dir(env, root, true)))
7066 GOTO(out, rc = -ENOTDIR);
7068 obj = local_file_find_or_create(env, lfsck->li_los, root,
7070 S_IFREG | S_IRUGO | S_IWUSR);
7072 GOTO(out, rc = PTR_ERR(obj));
7075 rc = lfsck_layout_load(env, com);
7077 rc = lfsck_layout_reset(env, com, true);
7078 } else if (rc == -ENOENT) {
7079 rc = lfsck_layout_init(env, com);
7080 } else if (lfsck->li_master) {
7081 rc = lfsck_load_sub_trace_files(env, com,
7082 &dt_lfsck_layout_dangling_features,
7083 LFSCK_LAYOUT, false);
7085 rc = lfsck_layout_reset(env, com, true);
7091 lo = com->lc_file_ram;
7092 switch (lo->ll_status) {
7098 spin_lock(&lfsck->li_lock);
7099 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
7100 spin_unlock(&lfsck->li_lock);
7103 CERROR("%s: unknown lfsck_layout status %d\n",
7104 lfsck_lfsck2name(lfsck), lo->ll_status);
7106 case LS_SCANNING_PHASE1:
7107 case LS_SCANNING_PHASE2:
7108 /* No need to store the status to disk right now.
7109 * If the system crashed before the status stored,
7110 * it will be loaded back when next time. */
7111 lo->ll_status = LS_CRASHED;
7112 if (!lfsck->li_master)
7113 lo->ll_flags |= LF_INCOMPLETE;
7120 spin_lock(&lfsck->li_lock);
7121 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
7122 spin_unlock(&lfsck->li_lock);
7126 if (lo->ll_flags & LF_CRASHED_LASTID) {
7127 LASSERT(lfsck->li_out_notify != NULL);
7129 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
7130 LE_LASTID_REBUILDING);
7136 if (root != NULL && !IS_ERR(root))
7137 lfsck_object_put(env, root);
7140 lfsck_component_cleanup(env, com);
7141 CERROR("%s: fail to init layout LFSCK component: rc = %d\n",
7142 lfsck_lfsck2name(lfsck), rc);
7148 struct lfsck_orphan_it {
7149 struct lfsck_component *loi_com;
7150 struct lfsck_rbtree_node *loi_lrn;
7151 struct lfsck_layout_slave_target *loi_llst;
7152 struct lu_fid loi_key;
7153 struct lu_orphan_rec_v3 loi_rec;
7155 unsigned int loi_over:1;
7158 static int lfsck_fid_match_idx(const struct lu_env *env,
7159 struct lfsck_instance *lfsck,
7160 const struct lu_fid *fid, int idx)
7162 struct seq_server_site *ss;
7163 struct lu_server_fld *sf;
7164 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
7167 /* All abnormal cases will be returned to MDT0. */
7168 if (!fid_is_norm(fid)) {
7175 ss = lfsck_dev_site(lfsck);
7176 if (unlikely(ss == NULL))
7179 sf = ss->ss_server_fld;
7180 LASSERT(sf != NULL);
7182 fld_range_set_any(range);
7183 rc = fld_server_lookup(env, sf, fid_seq(fid), range);
7187 if (!fld_range_is_mdt(range))
7190 if (range->lsr_index == idx)
7196 static void lfsck_layout_destroy_orphan(const struct lu_env *env,
7197 struct lfsck_instance *lfsck,
7198 struct dt_object *obj)
7200 struct dt_device *dev = lfsck_obj2dev(obj);
7201 struct thandle *handle;
7205 handle = lfsck_trans_create(env, dev, lfsck);
7209 rc = dt_declare_ref_del(env, obj, handle);
7213 rc = dt_declare_destroy(env, obj, handle);
7217 rc = dt_trans_start_local(env, dev, handle);
7221 dt_write_lock(env, obj, 0);
7222 rc = dt_ref_del(env, obj, handle);
7224 rc = dt_destroy(env, obj, handle);
7225 dt_write_unlock(env, obj);
7230 dt_trans_stop(env, dev, handle);
7232 CDEBUG(D_LFSCK, "destroy orphan OST-object "DFID": rc = %d\n",
7233 PFID(lfsck_dto2fid(obj)), rc);
7238 static int lfsck_orphan_index_lookup(const struct lu_env *env,
7239 struct dt_object *dt,
7241 const struct dt_key *key)
7246 static int lfsck_orphan_index_declare_insert(const struct lu_env *env,
7247 struct dt_object *dt,
7248 const struct dt_rec *rec,
7249 const struct dt_key *key,
7250 struct thandle *handle)
7255 static int lfsck_orphan_index_insert(const struct lu_env *env,
7256 struct dt_object *dt,
7257 const struct dt_rec *rec,
7258 const struct dt_key *key,
7259 struct thandle *handle)
7264 static int lfsck_orphan_index_declare_delete(const struct lu_env *env,
7265 struct dt_object *dt,
7266 const struct dt_key *key,
7267 struct thandle *handle)
7272 static int lfsck_orphan_index_delete(const struct lu_env *env,
7273 struct dt_object *dt,
7274 const struct dt_key *key,
7275 struct thandle *handle)
7280 static struct dt_it *lfsck_orphan_it_init(const struct lu_env *env,
7281 struct dt_object *dt,
7284 struct dt_device *dev = lu2dt_dev(dt->do_lu.lo_dev);
7285 struct lfsck_instance *lfsck;
7286 struct lfsck_component *com = NULL;
7287 struct lfsck_layout_slave_data *llsd;
7288 struct lfsck_orphan_it *it = NULL;
7289 struct lfsck_layout *lo;
7293 lfsck = lfsck_instance_find(dev, true, false);
7294 if (unlikely(lfsck == NULL))
7295 RETURN(ERR_PTR(-ENXIO));
7297 com = lfsck_component_find(lfsck, LFSCK_TYPE_LAYOUT);
7298 if (unlikely(com == NULL))
7299 GOTO(out, rc = -ENOENT);
7301 lo = com->lc_file_ram;
7302 if (lo->ll_flags & LF_INCOMPLETE)
7303 GOTO(out, rc = -ESRCH);
7305 llsd = com->lc_data;
7306 if (!llsd->llsd_rbtree_valid)
7307 GOTO(out, rc = -ESRCH);
7311 GOTO(out, rc = -ENOMEM);
7313 it->loi_llst = lfsck_layout_llst_find_and_del(llsd, attr, false);
7314 if (it->loi_llst == NULL)
7315 GOTO(out, rc = -ENXIO);
7317 if (dev->dd_record_fid_accessed) {
7318 /* The first iteration against the rbtree, scan the whole rbtree
7319 * to remove the nodes which do NOT need to be handled. */
7320 down_write(&llsd->llsd_rb_rwsem);
7321 if (dev->dd_record_fid_accessed) {
7322 struct rb_node *node;
7323 struct rb_node *next;
7324 struct lfsck_rbtree_node *lrn;
7326 /* No need to record the fid accessing anymore. */
7327 dev->dd_record_fid_accessed = 0;
7329 node = rb_first(&llsd->llsd_rb_root);
7330 while (node != NULL) {
7331 next = rb_next(node);
7332 lrn = rb_entry(node, struct lfsck_rbtree_node,
7334 if (atomic_read(&lrn->lrn_known_count) <=
7335 atomic_read(&lrn->lrn_accessed_count)) {
7336 rb_erase(node, &llsd->llsd_rb_root);
7337 lfsck_rbtree_free(lrn);
7342 up_write(&llsd->llsd_rb_rwsem);
7345 /* read lock the rbtree when init, and unlock when fini */
7346 down_read(&llsd->llsd_rb_rwsem);
7354 lfsck_component_put(env, com);
7356 CDEBUG(D_LFSCK, "%s: init the orphan iteration: rc = %d\n",
7357 lfsck_lfsck2name(lfsck), rc);
7359 lfsck_instance_put(env, lfsck);
7364 it = (struct lfsck_orphan_it *)ERR_PTR(rc);
7367 return (struct dt_it *)it;
7370 static void lfsck_orphan_it_fini(const struct lu_env *env,
7373 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7374 struct lfsck_component *com = it->loi_com;
7375 struct lfsck_layout_slave_data *llsd;
7376 struct lfsck_layout_slave_target *llst;
7379 CDEBUG(D_LFSCK, "%s: fini the orphan iteration\n",
7380 lfsck_lfsck2name(com->lc_lfsck));
7382 llsd = com->lc_data;
7383 up_read(&llsd->llsd_rb_rwsem);
7384 llst = it->loi_llst;
7385 LASSERT(llst != NULL);
7387 /* Save the key and hash for iterate next. */
7388 llst->llst_fid = it->loi_key;
7389 llst->llst_hash = it->loi_hash;
7390 lfsck_layout_llst_put(llst);
7391 lfsck_component_put(env, com);
7397 * \retval +1: the iteration finished
7398 * \retval 0: on success, not finished
7399 * \retval -ve: on error
7401 static int lfsck_orphan_it_next(const struct lu_env *env,
7404 struct lfsck_thread_info *info = lfsck_env_info(env);
7405 struct filter_fid *ff = &info->lti_ff;
7406 struct lu_attr *la = &info->lti_la;
7407 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7408 struct lu_fid *key = &it->loi_key;
7409 struct lu_orphan_rec_v3 *rec = &it->loi_rec;
7410 struct ost_layout *ol = &rec->lor_layout;
7411 struct lfsck_component *com = it->loi_com;
7412 struct lfsck_instance *lfsck = com->lc_lfsck;
7413 struct lfsck_layout_slave_data *llsd = com->lc_data;
7414 struct dt_object *obj;
7415 struct lfsck_rbtree_node *lrn;
7419 __u32 idx = it->loi_llst->llst_index;
7429 lrn = lfsck_rbtree_search(llsd, key, &exact);
7437 key->f_seq = lrn->lrn_seq;
7438 key->f_oid = lrn->lrn_first_oid;
7443 if (unlikely(key->f_oid == 0)) {
7450 lrn->lrn_first_oid + LFSCK_RBTREE_BITMAP_WIDTH) {
7456 if (unlikely(atomic_read(&lrn->lrn_known_count) <=
7457 atomic_read(&lrn->lrn_accessed_count))) {
7458 struct rb_node *next = rb_next(&lrn->lrn_node);
7460 while (next != NULL) {
7461 lrn = rb_entry(next, struct lfsck_rbtree_node,
7463 if (atomic_read(&lrn->lrn_known_count) >
7464 atomic_read(&lrn->lrn_accessed_count))
7466 next = rb_next(next);
7475 key->f_seq = lrn->lrn_seq;
7476 key->f_oid = lrn->lrn_first_oid;
7480 pos = key->f_oid - lrn->lrn_first_oid;
7483 pos = find_next_bit(lrn->lrn_known_bitmap,
7484 LFSCK_RBTREE_BITMAP_WIDTH, pos);
7485 if (pos >= LFSCK_RBTREE_BITMAP_WIDTH) {
7486 key->f_oid = lrn->lrn_first_oid + pos;
7487 if (unlikely(key->f_oid < lrn->lrn_first_oid)) {
7495 if (test_bit(pos, lrn->lrn_accessed_bitmap)) {
7500 key->f_oid = lrn->lrn_first_oid + pos;
7501 obj = lfsck_object_find_bottom(env, lfsck, key);
7504 if (rc == -ENOENT) {
7511 dt_read_lock(env, obj, 0);
7512 if (dt_object_exists(obj) == 0 ||
7513 lfsck_is_dead_obj(obj)) {
7514 dt_read_unlock(env, obj);
7515 lfsck_object_put(env, obj);
7520 rc = dt_attr_get(env, obj, la);
7524 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, ff, sizeof(*ff)),
7526 if (rc == -ENODATA) {
7527 /* For the pre-created OST-object, update the bitmap to avoid
7528 * others LFSCK (second phase) iteration to touch it again. */
7529 if (la->la_ctime == 0) {
7530 if (!test_and_set_bit(pos, lrn->lrn_accessed_bitmap))
7531 atomic_inc(&lrn->lrn_accessed_count);
7533 /* For the race between repairing dangling referenced
7534 * MDT-object and unlink the file, it may left orphan
7535 * OST-object there. Destroy it now! */
7536 if (unlikely(!(la->la_mode & S_ISUID))) {
7537 dt_read_unlock(env, obj);
7538 lfsck_layout_destroy_orphan(env, lfsck, obj);
7539 lfsck_object_put(env, obj);
7543 } else if (idx == 0) {
7544 /* If the orphan OST-object has no parent information,
7545 * regard it as referenced by the MDT-object on MDT0. */
7546 fid_zero(&rec->lor_rec.lor_fid);
7547 rec->lor_rec.lor_uid = la->la_uid;
7548 rec->lor_rec.lor_gid = la->la_gid;
7549 memset(ol, 0, sizeof(*ol));
7550 rec->lor_layout_version = 0;
7556 dt_read_unlock(env, obj);
7557 lfsck_object_put(env, obj);
7562 if (rc < sizeof(struct lu_fid))
7563 GOTO(out, rc = (rc < 0 ? rc : -EINVAL));
7565 fid_le_to_cpu(&rec->lor_rec.lor_fid, &ff->ff_parent);
7566 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
7567 * MDT-object's FID::f_ver, instead it is the OST-object index in its
7568 * parent MDT-object's layout EA. */
7569 save = rec->lor_rec.lor_fid.f_stripe_idx;
7570 rec->lor_rec.lor_fid.f_ver = 0;
7571 rc = lfsck_fid_match_idx(env, lfsck, &rec->lor_rec.lor_fid, idx);
7572 /* If the orphan OST-object does not claim the MDT, then next.
7574 * If we do not know whether it matches or not, then return it
7575 * to the MDT for further check. */
7577 dt_read_unlock(env, obj);
7578 lfsck_object_put(env, obj);
7583 rec->lor_rec.lor_fid.f_stripe_idx = save;
7584 rec->lor_rec.lor_uid = la->la_uid;
7585 rec->lor_rec.lor_gid = la->la_gid;
7586 ost_layout_le_to_cpu(ol, &ff->ff_layout);
7587 rec->lor_layout_version =
7588 le32_to_cpu(ff->ff_layout_version & ~LU_LAYOUT_RESYNC);
7589 rec->lor_range = le32_to_cpu(ff->ff_range);
7591 CDEBUG(D_LFSCK, "%s: return orphan "DFID", PFID "DFID", owner %u:%u, "
7592 "stripe size %u, stripe count %u, COMP id %u, COMP start %llu, "
7593 "COMP end %llu, layout version %u, range %u\n",
7594 lfsck_lfsck2name(com->lc_lfsck), PFID(key),
7595 PFID(&rec->lor_rec.lor_fid), rec->lor_rec.lor_uid,
7596 rec->lor_rec.lor_gid, ol->ol_stripe_size, ol->ol_stripe_count,
7597 ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
7598 rec->lor_layout_version, rec->lor_range);
7603 dt_read_unlock(env, obj);
7604 lfsck_object_put(env, obj);
7612 * \retval +1: locate to the exactly position
7613 * \retval 0: cannot locate to the exactly position,
7614 * call next() to move to a valid position.
7615 * \retval -ve: on error
7617 static int lfsck_orphan_it_get(const struct lu_env *env,
7619 const struct dt_key *key)
7621 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7624 it->loi_key = *(struct lu_fid *)key;
7625 rc = lfsck_orphan_it_next(env, di);
7635 static void lfsck_orphan_it_put(const struct lu_env *env,
7640 static struct dt_key *lfsck_orphan_it_key(const struct lu_env *env,
7641 const struct dt_it *di)
7643 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7645 return (struct dt_key *)&it->loi_key;
7648 static int lfsck_orphan_it_key_size(const struct lu_env *env,
7649 const struct dt_it *di)
7651 return sizeof(struct lu_fid);
7654 static int lfsck_orphan_it_rec(const struct lu_env *env,
7655 const struct dt_it *di,
7659 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7661 *(struct lu_orphan_rec_v3 *)rec = it->loi_rec;
7666 static __u64 lfsck_orphan_it_store(const struct lu_env *env,
7667 const struct dt_it *di)
7669 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7671 return it->loi_hash;
7675 * \retval +1: locate to the exactly position
7676 * \retval 0: cannot locate to the exactly position,
7677 * call next() to move to a valid position.
7678 * \retval -ve: on error
7680 static int lfsck_orphan_it_load(const struct lu_env *env,
7681 const struct dt_it *di,
7684 struct lfsck_orphan_it *it = (struct lfsck_orphan_it *)di;
7685 struct lfsck_layout_slave_target *llst = it->loi_llst;
7688 LASSERT(llst != NULL);
7690 if (hash != llst->llst_hash) {
7691 CDEBUG(D_LFSCK, "%s: the given hash %llu for orphan "
7692 "iteration does not match the one when fini "
7693 "%llu, to be reset.\n",
7694 lfsck_lfsck2name(it->loi_com->lc_lfsck), hash,
7696 fid_zero(&llst->llst_fid);
7697 llst->llst_hash = 0;
7700 it->loi_key = llst->llst_fid;
7701 it->loi_hash = llst->llst_hash;
7702 rc = lfsck_orphan_it_next(env, (struct dt_it *)di);
7712 static int lfsck_orphan_it_key_rec(const struct lu_env *env,
7713 const struct dt_it *di,
7719 static const struct dt_index_operations lfsck_orphan_index_ops = {
7720 .dio_lookup = lfsck_orphan_index_lookup,
7721 .dio_declare_insert = lfsck_orphan_index_declare_insert,
7722 .dio_insert = lfsck_orphan_index_insert,
7723 .dio_declare_delete = lfsck_orphan_index_declare_delete,
7724 .dio_delete = lfsck_orphan_index_delete,
7726 .init = lfsck_orphan_it_init,
7727 .fini = lfsck_orphan_it_fini,
7728 .get = lfsck_orphan_it_get,
7729 .put = lfsck_orphan_it_put,
7730 .next = lfsck_orphan_it_next,
7731 .key = lfsck_orphan_it_key,
7732 .key_size = lfsck_orphan_it_key_size,
7733 .rec = lfsck_orphan_it_rec,
7734 .store = lfsck_orphan_it_store,
7735 .load = lfsck_orphan_it_load,
7736 .key_rec = lfsck_orphan_it_key_rec,