4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, Intel Corporation.
26 * lustre/lfsck/lfsck_layout.c
28 * Author: Fan, Yong <fan.yong@intel.com>
32 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_LFSCK
36 #include <linux/bitops.h>
37 #include <linux/rbtree.h>
39 #include <lustre/lustre_idl.h>
40 #include <lu_object.h>
41 #include <dt_object.h>
42 #include <lustre_fid.h>
43 #include <lustre_lib.h>
44 #include <lustre_net.h>
45 #include <lustre/lustre_user.h>
46 #include <md_object.h>
47 #include <obd_class.h>
49 #include "lfsck_internal.h"
51 #define LFSCK_LAYOUT_MAGIC_V1 0xB173AE14
52 #define LFSCK_LAYOUT_MAGIC_V2 0xB1734D76
54 #define LFSCK_LAYOUT_MAGIC LFSCK_LAYOUT_MAGIC_V2
56 struct lfsck_layout_seq {
57 struct list_head lls_list;
60 __u64 lls_lastid_known;
61 struct dt_object *lls_lastid_obj;
62 unsigned int lls_dirty:1;
65 struct lfsck_layout_slave_target {
66 /* link into lfsck_layout_slave_data::llsd_master_list. */
67 struct list_head llst_list;
68 /* The position for next record in the rbtree for iteration. */
69 struct lu_fid llst_fid;
70 /* Dummy hash for iteration against the rbtree. */
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 rwlock_t llsd_rb_lock;
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;
100 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
102 if (atomic_dec_and_test(&llst->llst_ref)) {
103 LASSERT(list_empty(&llst->llst_list));
110 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
112 struct lfsck_layout_slave_target *llst;
113 struct lfsck_layout_slave_target *tmp;
120 INIT_LIST_HEAD(&llst->llst_list);
122 llst->llst_index = index;
123 atomic_set(&llst->llst_ref, 1);
125 spin_lock(&llsd->llsd_lock);
126 list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
127 if (tmp->llst_index == index) {
133 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
134 spin_unlock(&llsd->llsd_lock);
143 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
144 struct lfsck_layout_slave_target *llst)
148 spin_lock(&llsd->llsd_lock);
149 if (!list_empty(&llst->llst_list)) {
150 list_del_init(&llst->llst_list);
153 spin_unlock(&llsd->llsd_lock);
156 lfsck_layout_llst_put(llst);
159 static inline struct lfsck_layout_slave_target *
160 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
161 __u32 index, bool unlink)
163 struct lfsck_layout_slave_target *llst;
165 spin_lock(&llsd->llsd_lock);
166 list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
167 if (llst->llst_index == index) {
169 list_del_init(&llst->llst_list);
171 atomic_inc(&llst->llst_ref);
172 spin_unlock(&llsd->llsd_lock);
177 spin_unlock(&llsd->llsd_lock);
182 static struct lfsck_layout_req *
183 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
184 struct dt_object *child, __u32 ost_idx,
187 struct lfsck_layout_req *llr;
191 return ERR_PTR(-ENOMEM);
193 INIT_LIST_HEAD(&llr->llr_lar.lar_list);
194 llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
195 llr->llr_child = child;
196 llr->llr_ost_idx = ost_idx;
197 llr->llr_lov_idx = lov_idx;
202 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
203 struct lfsck_assistant_req *lar)
205 struct lfsck_layout_req *llr =
206 container_of0(lar, struct lfsck_layout_req, llr_lar);
208 lfsck_object_put(env, llr->llr_child);
209 lfsck_assistant_object_put(env, lar->lar_parent);
214 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
215 struct ptlrpc_request *req,
219 struct lfsck_async_interpret_args *laia = args;
220 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
222 ltd->ltd_synced_failures = 1;
223 atomic_dec(laia->laia_count);
230 * Notify remote LFSCK instances about former failures.
232 * The local LFSCK instance has recorded which OSTs have ever failed to respond
233 * some LFSCK verification requests (maybe because of network issues or the OST
234 * itself trouble). During the respond gap, the OST may missed some OST-objects
235 * verification, then the OST cannot know whether related OST-objects have been
236 * referenced by related MDT-objects or not, then in the second-stage scanning,
237 * these OST-objects will be regarded as orphan, if the OST-object contains bad
238 * parent FID for back reference, then it will misguide the LFSCK to make wrong
239 * fixing for the fake orphan.
241 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
242 * it will scan the bitmap for the ever failed OSTs, and notify them that they
243 * have ever missed some OST-object verification and should skip the handling
244 * for orphan OST-objects on all MDTs that are in the layout LFSCK.
246 * \param[in] env pointer to the thread context
247 * \param[in] com pointer to the lfsck component
248 * \param[in] lr pointer to the lfsck request
250 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
251 struct lfsck_component *com,
252 struct lfsck_request *lr)
254 struct lfsck_async_interpret_args *laia =
255 &lfsck_env_info(env)->lti_laia2;
256 struct lfsck_assistant_data *lad = com->lc_data;
257 struct lfsck_layout *lo = com->lc_file_ram;
258 struct lfsck_instance *lfsck = com->lc_lfsck;
259 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
260 struct lfsck_tgt_desc *ltd;
261 struct ptlrpc_request_set *set;
267 if (!lad->lad_incomplete || lo->ll_flags & LF_INCOMPLETE)
270 /* If the MDT has ever failed to verfiy some OST-objects,
271 * then sync failures with them firstly. */
272 lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
274 atomic_set(&count, 0);
275 memset(laia, 0, sizeof(*laia));
276 laia->laia_count = &count;
277 set = ptlrpc_prep_set();
279 GOTO(out, rc = -ENOMEM);
281 down_read(<ds->ltd_rw_sem);
282 cfs_foreach_bit(lad->lad_bitmap, idx) {
283 ltd = lfsck_ltd2tgt(ltds, idx);
284 LASSERT(ltd != NULL);
286 laia->laia_ltd = ltd;
287 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
288 lfsck_layout_assistant_sync_failures_interpret,
291 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
292 "notify target %x for %s phase1 done: "
293 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
294 ltd->ltd_index, lad->lad_name, rc);
301 up_read(<ds->ltd_rw_sem);
303 if (rc == 0 && atomic_read(&count) > 0)
304 rc = ptlrpc_set_wait(set);
306 ptlrpc_set_destroy(set);
308 if (rc == 0 && atomic_read(&count) > 0)
315 /* If failed to sync failures with the OSTs, then have to
316 * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
317 * subsequent orphan OST-object handling. */
318 lo->ll_flags |= LF_INCOMPLETE;
320 lr->lr_flags2 = lo->ll_flags;
323 static int lfsck_layout_get_lovea(const struct lu_env *env,
324 struct dt_object *obj, struct lu_buf *buf)
329 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
331 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
335 lu_buf_realloc(buf, rc);
336 if (buf->lb_buf == NULL)
348 if (unlikely(buf->lb_buf == NULL)) {
349 lu_buf_alloc(buf, rc);
350 if (buf->lb_buf == NULL)
359 static int lfsck_layout_verify_header(struct lov_mds_md_v1 *lmm)
364 magic = le32_to_cpu(lmm->lmm_magic);
365 /* If magic crashed, keep it there. Sometime later, during OST-object
366 * orphan handling, if some OST-object(s) back-point to it, it can be
367 * verified and repaired. */
368 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
372 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
373 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
378 CDEBUG(D_LFSCK, "%s LOV EA magic %u on "DOSTID"\n",
379 rc == -EINVAL ? "Unknown" : "Unsupported",
385 pattern = le32_to_cpu(lmm->lmm_pattern);
386 /* XXX: currently, we only support LOV_PATTERN_RAID0. */
387 if (lov_pattern(pattern) != LOV_PATTERN_RAID0) {
390 lmm_oi_le_to_cpu(&oi, &lmm->lmm_oi);
391 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u on "DOSTID"\n",
392 pattern, POSTID(&oi));
400 #define LFSCK_RBTREE_BITMAP_SIZE PAGE_CACHE_SIZE
401 #define LFSCK_RBTREE_BITMAP_WIDTH (LFSCK_RBTREE_BITMAP_SIZE << 3)
402 #define LFSCK_RBTREE_BITMAP_MASK (LFSCK_RBTREE_BITMAP_WIDTH - 1)
404 struct lfsck_rbtree_node {
405 struct rb_node lrn_node;
408 atomic_t lrn_known_count;
409 atomic_t lrn_accessed_count;
410 void *lrn_known_bitmap;
411 void *lrn_accessed_bitmap;
414 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
415 __u64 seq, __u32 oid)
417 if (seq < lrn->lrn_seq)
420 if (seq > lrn->lrn_seq)
423 if (oid < lrn->lrn_first_oid)
426 if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
432 /* The caller should hold llsd->llsd_rb_lock. */
433 static struct lfsck_rbtree_node *
434 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
435 const struct lu_fid *fid, bool *exact)
437 struct rb_node *node = llsd->llsd_rb_root.rb_node;
438 struct rb_node *prev = NULL;
439 struct lfsck_rbtree_node *lrn = NULL;
445 while (node != NULL) {
447 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
448 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
450 node = node->rb_left;
452 node = node->rb_right;
460 /* If there is no exactly matched one, then to the next valid one. */
463 /* The rbtree is empty. */
470 node = rb_next(prev);
472 /* The end of the rbtree. */
476 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
481 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
482 const struct lu_fid *fid)
484 struct lfsck_rbtree_node *lrn;
488 return ERR_PTR(-ENOMEM);
490 OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
491 if (lrn->lrn_known_bitmap == NULL) {
494 return ERR_PTR(-ENOMEM);
497 OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
498 if (lrn->lrn_accessed_bitmap == NULL) {
499 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
502 return ERR_PTR(-ENOMEM);
505 RB_CLEAR_NODE(&lrn->lrn_node);
506 lrn->lrn_seq = fid_seq(fid);
507 lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
508 atomic_set(&lrn->lrn_known_count, 0);
509 atomic_set(&lrn->lrn_accessed_count, 0);
514 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
516 OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
517 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
521 /* The caller should hold lock. */
522 static struct lfsck_rbtree_node *
523 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
524 struct lfsck_rbtree_node *lrn)
526 struct rb_node **pos = &llsd->llsd_rb_root.rb_node;
527 struct rb_node *parent = NULL;
528 struct lfsck_rbtree_node *tmp;
531 while (*pos != NULL) {
533 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
534 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
536 pos = &(*pos)->rb_left;
538 pos = &(*pos)->rb_right;
543 rb_link_node(&lrn->lrn_node, parent, pos);
544 rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
549 extern const struct dt_index_operations lfsck_orphan_index_ops;
551 static int lfsck_rbtree_setup(const struct lu_env *env,
552 struct lfsck_component *com)
554 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
555 struct lfsck_instance *lfsck = com->lc_lfsck;
556 struct dt_device *dev = lfsck->li_bottom;
557 struct lfsck_layout_slave_data *llsd = com->lc_data;
558 struct dt_object *obj;
560 fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
561 fid->f_oid = lfsck_dev_idx(lfsck);
563 obj = dt_locate(env, dev, fid);
565 RETURN(PTR_ERR(obj));
567 /* Generate an in-RAM object to stand for the layout rbtree.
568 * Scanning the layout rbtree will be via the iteration over
569 * the object. In the future, the rbtree may be written onto
570 * disk with the object.
572 * Mark the object to be as exist. */
573 obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
574 obj->do_index_ops = &lfsck_orphan_index_ops;
575 llsd->llsd_rb_obj = obj;
576 llsd->llsd_rbtree_valid = 1;
577 dev->dd_record_fid_accessed = 1;
579 CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
580 lfsck_lfsck2name(lfsck));
585 static void lfsck_rbtree_cleanup(const struct lu_env *env,
586 struct lfsck_component *com)
588 struct lfsck_instance *lfsck = com->lc_lfsck;
589 struct lfsck_layout_slave_data *llsd = com->lc_data;
590 struct rb_node *node = rb_first(&llsd->llsd_rb_root);
591 struct rb_node *next;
592 struct lfsck_rbtree_node *lrn;
594 lfsck->li_bottom->dd_record_fid_accessed = 0;
595 /* Invalid the rbtree, then no others will use it. */
596 write_lock(&llsd->llsd_rb_lock);
597 llsd->llsd_rbtree_valid = 0;
598 write_unlock(&llsd->llsd_rb_lock);
600 while (node != NULL) {
601 next = rb_next(node);
602 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
603 rb_erase(node, &llsd->llsd_rb_root);
604 lfsck_rbtree_free(lrn);
608 if (llsd->llsd_rb_obj != NULL) {
609 lfsck_object_put(env, llsd->llsd_rb_obj);
610 llsd->llsd_rb_obj = NULL;
613 CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
614 lfsck_lfsck2name(lfsck));
617 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
618 struct lfsck_component *com,
619 const struct lu_fid *fid,
622 struct lfsck_layout_slave_data *llsd = com->lc_data;
623 struct lfsck_rbtree_node *lrn;
629 if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
632 if (!fid_is_idif(fid) && !fid_is_norm(fid))
635 read_lock(&llsd->llsd_rb_lock);
636 if (!llsd->llsd_rbtree_valid)
637 GOTO(unlock, rc = 0);
639 lrn = lfsck_rbtree_search(llsd, fid, NULL);
641 struct lfsck_rbtree_node *tmp;
645 read_unlock(&llsd->llsd_rb_lock);
646 tmp = lfsck_rbtree_new(env, fid);
648 GOTO(out, rc = PTR_ERR(tmp));
651 write_lock(&llsd->llsd_rb_lock);
652 if (!llsd->llsd_rbtree_valid) {
653 lfsck_rbtree_free(tmp);
654 GOTO(unlock, rc = 0);
657 lrn = lfsck_rbtree_insert(llsd, tmp);
659 lfsck_rbtree_free(tmp);
662 idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
663 /* Any accessed object must be a known object. */
664 if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
665 atomic_inc(&lrn->lrn_known_count);
666 if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
667 atomic_inc(&lrn->lrn_accessed_count);
669 GOTO(unlock, rc = 0);
673 write_unlock(&llsd->llsd_rb_lock);
675 read_unlock(&llsd->llsd_rb_lock);
677 if (rc != 0 && accessed) {
678 struct lfsck_layout *lo = com->lc_file_ram;
680 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
681 "bitmap, and will cause incorrect LFSCK OST-object "
682 "handling, so disable it to cancel orphan handling "
683 "for related device. rc = %d\n",
684 lfsck_lfsck2name(com->lc_lfsck), rc);
686 lo->ll_flags |= LF_INCOMPLETE;
687 lfsck_rbtree_cleanup(env, com);
691 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
692 const struct lfsck_layout *src)
696 des->ll_magic = le32_to_cpu(src->ll_magic);
697 des->ll_status = le32_to_cpu(src->ll_status);
698 des->ll_flags = le32_to_cpu(src->ll_flags);
699 des->ll_success_count = le32_to_cpu(src->ll_success_count);
700 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
701 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
702 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
703 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
704 des->ll_time_last_checkpoint =
705 le64_to_cpu(src->ll_time_last_checkpoint);
706 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
707 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
708 des->ll_pos_first_inconsistent =
709 le64_to_cpu(src->ll_pos_first_inconsistent);
710 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
711 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
712 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
713 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
714 for (i = 0; i < LLIT_MAX; i++)
715 des->ll_objs_repaired[i] =
716 le64_to_cpu(src->ll_objs_repaired[i]);
717 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
718 des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
721 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
722 const struct lfsck_layout *src)
726 des->ll_magic = cpu_to_le32(src->ll_magic);
727 des->ll_status = cpu_to_le32(src->ll_status);
728 des->ll_flags = cpu_to_le32(src->ll_flags);
729 des->ll_success_count = cpu_to_le32(src->ll_success_count);
730 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
731 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
732 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
733 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
734 des->ll_time_last_checkpoint =
735 cpu_to_le64(src->ll_time_last_checkpoint);
736 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
737 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
738 des->ll_pos_first_inconsistent =
739 cpu_to_le64(src->ll_pos_first_inconsistent);
740 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
741 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
742 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
743 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
744 for (i = 0; i < LLIT_MAX; i++)
745 des->ll_objs_repaired[i] =
746 cpu_to_le64(src->ll_objs_repaired[i]);
747 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
748 des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
752 * Load the OST bitmap from the lfsck_layout trace file.
754 * \param[in] env pointer to the thread context
755 * \param[in] com pointer to the lfsck component
757 * \retval 0 for success
758 * \retval negative error number on failure or data corruption
760 static int lfsck_layout_load_bitmap(const struct lu_env *env,
761 struct lfsck_component *com)
763 struct dt_object *obj = com->lc_obj;
764 struct lfsck_assistant_data *lad = com->lc_data;
765 struct lfsck_layout *lo = com->lc_file_ram;
766 cfs_bitmap_t *bitmap = lad->lad_bitmap;
767 loff_t pos = com->lc_file_size;
773 if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
775 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
777 nbits = lo->ll_bitmap_size;
779 if (unlikely(nbits < BITS_PER_LONG))
780 nbits = BITS_PER_LONG;
782 if (nbits > bitmap->size) {
783 __u32 new_bits = bitmap->size;
784 cfs_bitmap_t *new_bitmap;
786 while (new_bits < nbits)
789 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
790 if (new_bitmap == NULL)
793 lad->lad_bitmap = new_bitmap;
794 CFS_FREE_BITMAP(bitmap);
798 if (lo->ll_bitmap_size == 0) {
799 lad->lad_incomplete = 0;
800 CFS_RESET_BITMAP(bitmap);
805 size = (lo->ll_bitmap_size + 7) >> 3;
806 rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
808 RETURN(rc >= 0 ? -EINVAL : rc);
810 if (cfs_bitmap_check_empty(bitmap))
811 lad->lad_incomplete = 0;
813 lad->lad_incomplete = 1;
819 * Load the layout LFSCK trace file from disk.
821 * The layout LFSCK trace file records the layout LFSCK status information
822 * and other statistics, such as how many objects have been scanned, and how
823 * many objects have been repaired, and etc. It also contains the bitmap for
824 * failed OSTs during the layout LFSCK. All these information will be loaded
825 * from disk to RAM when the layout LFSCK component setup.
827 * \param[in] env pointer to the thread context
828 * \param[in] com pointer to the lfsck component
830 * \retval positive number for file data corruption, the caller
831 * should reset the layout LFSCK trace file
832 * \retval 0 for success
833 * \retval negative error number on failure
835 static int lfsck_layout_load(const struct lu_env *env,
836 struct lfsck_component *com)
838 struct lfsck_layout *lo = com->lc_file_ram;
839 ssize_t size = com->lc_file_size;
843 rc = dt_read(env, com->lc_obj,
844 lfsck_buf_get(env, com->lc_file_disk, size), &pos);
848 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
849 lfsck_lfsck2name(com->lc_lfsck), rc);
851 } else if (rc != size) {
852 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
853 lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
857 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
858 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
859 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
860 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
861 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
869 * Store the layout LFSCK trace file on disk.
871 * The layout LFSCK trace file records the layout LFSCK status information
872 * and other statistics, such as how many objects have been scanned, and how
873 * many objects have been repaired, and etc. It also contains the bitmap for
874 * failed OSTs during the layout LFSCK. All these information will be synced
875 * from RAM to disk periodically.
877 * \param[in] env pointer to the thread context
878 * \param[in] com pointer to the lfsck component
880 * \retval 0 for success
881 * \retval negative error number on failure
883 static int lfsck_layout_store(const struct lu_env *env,
884 struct lfsck_component *com)
886 struct dt_object *obj = com->lc_obj;
887 struct lfsck_instance *lfsck = com->lc_lfsck;
888 struct lfsck_layout *lo_ram = com->lc_file_ram;
889 struct lfsck_layout *lo = com->lc_file_disk;
891 struct dt_device *dev = lfsck_obj2dev(obj);
892 cfs_bitmap_t *bitmap = NULL;
894 ssize_t size = com->lc_file_size;
899 if (lfsck->li_master) {
900 struct lfsck_assistant_data *lad = com->lc_data;
902 bitmap = lad->lad_bitmap;
903 nbits = bitmap->size;
906 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
909 lo_ram->ll_bitmap_size = nbits;
910 lfsck_layout_cpu_to_le(lo, lo_ram);
911 th = dt_trans_create(env, dev);
913 GOTO(log, rc = PTR_ERR(th));
915 rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
920 if (bitmap != NULL) {
921 rc = dt_declare_record_write(env, obj,
922 lfsck_buf_get(env, bitmap->data, nbits >> 3),
928 rc = dt_trans_start_local(env, dev, th);
933 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
937 if (bitmap != NULL) {
939 rc = dt_record_write(env, obj,
940 lfsck_buf_get(env, bitmap->data, nbits >> 3),
947 dt_trans_stop(env, dev, th);
951 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
952 lfsck_lfsck2name(lfsck), rc);
957 static int lfsck_layout_init(const struct lu_env *env,
958 struct lfsck_component *com)
960 struct lfsck_layout *lo = com->lc_file_ram;
963 memset(lo, 0, com->lc_file_size);
964 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
965 lo->ll_status = LS_INIT;
966 down_write(&com->lc_sem);
967 rc = lfsck_layout_store(env, com);
968 up_write(&com->lc_sem);
973 static int fid_is_for_ostobj(const struct lu_env *env,
974 struct lfsck_instance *lfsck,
975 struct dt_object *obj, const struct lu_fid *fid)
977 struct seq_server_site *ss = lfsck_dev_site(lfsck);
978 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
979 struct lustre_mdt_attrs *lma;
982 fld_range_set_any(range);
983 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
985 if (fld_range_is_ost(range))
991 lma = &lfsck_env_info(env)->lti_lma;
992 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
994 if (rc == sizeof(*lma)) {
995 lustre_lma_swab(lma);
997 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1000 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1005 static struct lfsck_layout_seq *
1006 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1008 struct lfsck_layout_seq *lls;
1010 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1011 if (lls->lls_seq == seq)
1014 if (lls->lls_seq > seq)
1022 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1023 struct lfsck_layout_seq *lls)
1025 struct lfsck_layout_seq *tmp;
1026 struct list_head *pos = &llsd->llsd_seq_list;
1028 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1029 if (lls->lls_seq < tmp->lls_seq) {
1030 pos = &tmp->lls_list;
1034 list_add_tail(&lls->lls_list, pos);
1038 lfsck_layout_lastid_create(const struct lu_env *env,
1039 struct lfsck_instance *lfsck,
1040 struct dt_object *obj)
1042 struct lfsck_thread_info *info = lfsck_env_info(env);
1043 struct lu_attr *la = &info->lti_la;
1044 struct dt_object_format *dof = &info->lti_dof;
1045 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1046 struct dt_device *dt = lfsck_obj2dev(obj);
1053 if (bk->lb_param & LPF_DRYRUN)
1056 memset(la, 0, sizeof(*la));
1057 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1058 la->la_valid = LA_MODE | LA_UID | LA_GID;
1059 memset(dof, 0, sizeof(*dof));
1060 dof->dof_type = dt_mode_to_dft(S_IFREG);
1062 th = dt_trans_create(env, dt);
1064 GOTO(log, rc = PTR_ERR(th));
1066 rc = dt_declare_create(env, obj, la, NULL, dof, th);
1070 rc = dt_declare_record_write(env, obj,
1071 lfsck_buf_get(env, &lastid,
1077 rc = dt_trans_start_local(env, dt, th);
1081 dt_write_lock(env, obj, 0);
1082 if (likely(dt_object_exists(obj) == 0)) {
1083 rc = dt_create(env, obj, la, NULL, dof, th);
1085 rc = dt_record_write(env, obj,
1086 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1089 dt_write_unlock(env, obj);
1094 dt_trans_stop(env, dt, th);
1097 CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1099 lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1105 lfsck_layout_lastid_reload(const struct lu_env *env,
1106 struct lfsck_component *com,
1107 struct lfsck_layout_seq *lls)
1113 dt_read_lock(env, lls->lls_lastid_obj, 0);
1114 rc = dt_record_read(env, lls->lls_lastid_obj,
1115 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1116 dt_read_unlock(env, lls->lls_lastid_obj);
1117 if (unlikely(rc != 0))
1120 lastid = le64_to_cpu(lastid);
1121 if (lastid < lls->lls_lastid_known) {
1122 struct lfsck_instance *lfsck = com->lc_lfsck;
1123 struct lfsck_layout *lo = com->lc_file_ram;
1125 lls->lls_lastid = lls->lls_lastid_known;
1127 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1128 LASSERT(lfsck->li_out_notify != NULL);
1130 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1131 LE_LASTID_REBUILDING);
1132 lo->ll_flags |= LF_CRASHED_LASTID;
1134 CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1135 "LAST_ID file (1) for the sequence "LPX64
1136 ", old value "LPU64", known value "LPU64"\n",
1137 lfsck_lfsck2name(lfsck), lls->lls_seq,
1138 lastid, lls->lls_lastid);
1140 } else if (lastid >= lls->lls_lastid) {
1141 lls->lls_lastid = lastid;
1149 lfsck_layout_lastid_store(const struct lu_env *env,
1150 struct lfsck_component *com)
1152 struct lfsck_instance *lfsck = com->lc_lfsck;
1153 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1154 struct dt_device *dt = lfsck->li_bottom;
1155 struct lfsck_layout_slave_data *llsd = com->lc_data;
1156 struct lfsck_layout_seq *lls;
1162 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1165 if (!lls->lls_dirty)
1168 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1169 "<seq> "LPX64" as <oid> "LPU64"\n",
1170 lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1172 if (bk->lb_param & LPF_DRYRUN) {
1177 th = dt_trans_create(env, dt);
1180 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1181 "the LAST_ID for <seq> "LPX64"(1): rc = %d\n",
1182 lfsck_lfsck2name(com->lc_lfsck),
1187 lastid = cpu_to_le64(lls->lls_lastid);
1188 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1189 lfsck_buf_get(env, &lastid,
1195 rc = dt_trans_start_local(env, dt, th);
1199 dt_write_lock(env, lls->lls_lastid_obj, 0);
1200 rc = dt_record_write(env, lls->lls_lastid_obj,
1201 lfsck_buf_get(env, &lastid,
1202 sizeof(lastid)), &pos, th);
1203 dt_write_unlock(env, lls->lls_lastid_obj);
1208 dt_trans_stop(env, dt, th);
1211 CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1212 "the LAST_ID for <seq> "LPX64"(2): rc = %d\n",
1213 lfsck_lfsck2name(com->lc_lfsck),
1222 lfsck_layout_lastid_load(const struct lu_env *env,
1223 struct lfsck_component *com,
1224 struct lfsck_layout_seq *lls)
1226 struct lfsck_instance *lfsck = com->lc_lfsck;
1227 struct lfsck_layout *lo = com->lc_file_ram;
1228 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
1229 struct dt_object *obj;
1234 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1235 obj = dt_locate(env, lfsck->li_bottom, fid);
1237 RETURN(PTR_ERR(obj));
1239 /* LAST_ID crashed, to be rebuilt */
1240 if (dt_object_exists(obj) == 0) {
1241 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1242 LASSERT(lfsck->li_out_notify != NULL);
1244 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1245 LE_LASTID_REBUILDING);
1246 lo->ll_flags |= LF_CRASHED_LASTID;
1248 CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1249 "LAST_ID file for sequence "LPX64"\n",
1250 lfsck_lfsck2name(lfsck), lls->lls_seq);
1252 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1254 struct l_wait_info lwi = LWI_TIMEOUT(
1255 cfs_time_seconds(cfs_fail_val),
1258 /* Some others may changed the cfs_fail_val
1259 * as zero after above check, re-check it for
1260 * sure to avoid falling into wait for ever. */
1261 if (likely(lwi.lwi_timeout > 0)) {
1262 struct ptlrpc_thread *thread =
1265 up_write(&com->lc_sem);
1266 l_wait_event(thread->t_ctl_waitq,
1267 !thread_is_running(thread),
1269 down_write(&com->lc_sem);
1274 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1276 dt_read_lock(env, obj, 0);
1277 rc = dt_read(env, obj,
1278 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1280 dt_read_unlock(env, obj);
1281 if (rc != 0 && rc != sizeof(__u64))
1282 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1284 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1285 LASSERT(lfsck->li_out_notify != NULL);
1287 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1288 LE_LASTID_REBUILDING);
1289 lo->ll_flags |= LF_CRASHED_LASTID;
1291 CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1292 "LAST_ID file for the sequence "LPX64
1294 lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1297 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1305 lfsck_object_put(env, obj);
1307 lls->lls_lastid_obj = obj;
1312 static void lfsck_layout_record_failure(const struct lu_env *env,
1313 struct lfsck_instance *lfsck,
1314 struct lfsck_layout *lo)
1318 lo->ll_objs_failed_phase1++;
1319 cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1321 if (lo->ll_pos_first_inconsistent == 0 ||
1322 lo->ll_pos_first_inconsistent < cookie) {
1323 lo->ll_pos_first_inconsistent = cookie;
1325 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1326 "inconsistency at the pos ["LPU64"]\n",
1327 lfsck_lfsck2name(lfsck),
1328 lo->ll_pos_first_inconsistent);
1332 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1333 struct lfsck_component *com,
1336 struct lfsck_instance *lfsck = com->lc_lfsck;
1337 struct lfsck_layout *lo = com->lc_file_ram;
1339 down_write(&com->lc_sem);
1340 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1341 HALF_SEC - com->lc_time_last_checkpoint);
1342 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1343 lo->ll_objs_checked_phase2 += com->lc_new_checked;
1346 if (lo->ll_flags & LF_INCOMPLETE) {
1347 lo->ll_status = LS_PARTIAL;
1349 if (lfsck->li_master) {
1350 struct lfsck_assistant_data *lad = com->lc_data;
1352 if (lad->lad_incomplete)
1353 lo->ll_status = LS_PARTIAL;
1355 lo->ll_status = LS_COMPLETED;
1357 lo->ll_status = LS_COMPLETED;
1360 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1361 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1362 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1363 lo->ll_success_count++;
1364 } else if (rc == 0) {
1365 if (lfsck->li_status != 0)
1366 lo->ll_status = lfsck->li_status;
1368 lo->ll_status = LS_STOPPED;
1370 lo->ll_status = LS_FAILED;
1373 rc = lfsck_layout_store(env, com);
1374 up_write(&com->lc_sem);
1379 static int lfsck_layout_trans_stop(const struct lu_env *env,
1380 struct dt_device *dev,
1381 struct thandle *handle, int result)
1385 /* XXX: If there is something worng or it needs to repair nothing,
1386 * then notify the lower to stop the modification. Currently,
1387 * we use th_result for such purpose, that may be replaced by
1388 * some rollback mechanism in the future. */
1389 handle->th_result = result;
1390 rc = dt_trans_stop(env, dev, handle);
1392 return result > 0 ? 0 : result;
1394 return rc == 0 ? 1 : rc;
1398 * Get the system default stripe size.
1400 * \param[in] env pointer to the thread context
1401 * \param[in] lfsck pointer to the lfsck instance
1402 * \param[out] size pointer to the default stripe size
1404 * \retval 0 for success
1405 * \retval negative error number on failure
1407 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1408 struct lfsck_instance *lfsck,
1411 struct lov_user_md *lum = &lfsck_env_info(env)->lti_lum;
1412 struct dt_object *root;
1415 root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1417 return PTR_ERR(root);
1419 /* Get the default stripe size via xattr_get on the backend root. */
1420 rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1423 /* The lum->lmm_stripe_size is LE mode. The *size also
1424 * should be LE mode. So it is unnecessary to convert. */
1425 *size = lum->lmm_stripe_size;
1427 } else if (unlikely(rc == 0)) {
1431 lfsck_object_put(env, root);
1437 * \retval +1: repaired
1438 * \retval 0: did nothing
1439 * \retval -ve: on error
1441 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1442 struct thandle *handle,
1443 struct dt_object *parent,
1444 struct lu_fid *cfid,
1446 struct lov_ost_data_v1 *slot,
1447 int fl, __u32 ost_idx)
1449 struct ost_id *oi = &lfsck_env_info(env)->lti_oi;
1450 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1451 struct lu_buf ea_buf;
1457 magic = le32_to_cpu(lmm->lmm_magic);
1458 count = le16_to_cpu(lmm->lmm_stripe_count);
1460 fid_to_ostid(cfid, oi);
1461 ostid_cpu_to_le(oi, &slot->l_ost_oi);
1462 slot->l_ost_gen = cpu_to_le32(0);
1463 slot->l_ost_idx = cpu_to_le32(ost_idx);
1465 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE) {
1466 struct lov_ost_data_v1 *objs;
1469 if (magic == LOV_MAGIC_V1)
1470 objs = &lmm->lmm_objects[0];
1472 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1473 for (i = 0; i < count; i++, objs++) {
1474 if (objs != slot && lovea_slot_is_dummy(objs))
1478 /* If the @slot is the last dummy slot to be refilled,
1479 * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1481 lmm->lmm_pattern &= ~cpu_to_le32(LOV_PATTERN_F_HOLE);
1484 lfsck_buf_init(&ea_buf, lmm, lov_mds_md_size(count, magic));
1485 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1493 * \retval +1: repaired
1494 * \retval 0: did nothing
1495 * \retval -ve: on error
1497 static int lfsck_layout_extend_lovea(const struct lu_env *env,
1498 struct lfsck_instance *lfsck,
1499 struct thandle *handle,
1500 struct dt_object *parent,
1501 struct lu_fid *cfid,
1502 struct lu_buf *buf, int fl,
1503 __u32 ost_idx, __u32 ea_off, bool reset)
1505 struct lov_mds_md_v1 *lmm = buf->lb_buf;
1506 struct lov_ost_data_v1 *objs;
1512 if (fl == LU_XATTR_CREATE || reset) {
1513 __u32 pattern = LOV_PATTERN_RAID0;
1516 LASSERT(buf->lb_len >= lov_mds_md_size(count, LOV_MAGIC_V1));
1518 if (ea_off != 0 || reset) {
1519 pattern |= LOV_PATTERN_F_HOLE;
1523 memset(lmm, 0, buf->lb_len);
1524 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1525 lmm->lmm_pattern = cpu_to_le32(pattern);
1526 fid_to_lmm_oi(lfsck_dto2fid(parent), &lmm->lmm_oi);
1527 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1529 rc = lfsck_layout_get_def_stripesize(env, lfsck,
1530 &lmm->lmm_stripe_size);
1534 objs = &lmm->lmm_objects[ea_off];
1536 __u32 magic = le32_to_cpu(lmm->lmm_magic);
1539 count = le16_to_cpu(lmm->lmm_stripe_count);
1540 if (magic == LOV_MAGIC_V1)
1541 objs = &lmm->lmm_objects[count];
1543 objs = &((struct lov_mds_md_v3 *)lmm)->
1546 gap = ea_off - count;
1549 LASSERT(buf->lb_len >= lov_mds_md_size(count, magic));
1552 memset(objs, 0, gap * sizeof(*objs));
1553 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
1557 lmm->lmm_layout_gen =
1558 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
1562 lmm->lmm_stripe_count = cpu_to_le16(count);
1563 rc = lfsck_layout_refill_lovea(env, handle, parent, cfid, buf, objs,
1566 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
1567 DFID": parent "DFID", OST-index %u, stripe-index %u, fl %d, "
1568 "reset %s, %s LOV EA hole: rc = %d\n",
1569 lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
1570 ost_idx, ea_off, fl, reset ? "yes" : "no",
1571 hole ? "with" : "without", rc);
1576 static int __lfsck_layout_update_pfid(const struct lu_env *env,
1577 struct dt_object *child,
1578 const struct lu_fid *pfid, __u32 offset)
1580 struct dt_device *dev = lfsck_obj2dev(child);
1581 struct filter_fid *ff = &lfsck_env_info(env)->lti_new_pfid;
1582 struct thandle *handle;
1583 struct lu_buf buf = { NULL };
1586 ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
1587 ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
1588 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
1589 * MDT-object's FID::f_ver, instead it is the OST-object index in its
1590 * parent MDT-object's layout EA. */
1591 ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
1592 lfsck_buf_init(&buf, ff, sizeof(struct filter_fid));
1594 handle = dt_trans_create(env, dev);
1596 RETURN(PTR_ERR(handle));
1598 rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1602 rc = dt_trans_start_local(env, dev, handle);
1606 rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
1611 dt_trans_stop(env, dev, handle);
1617 * \retval +1: repaired
1618 * \retval 0: did nothing
1619 * \retval -ve: on error
1621 static int lfsck_layout_update_pfid(const struct lu_env *env,
1622 struct lfsck_component *com,
1623 struct dt_object *parent,
1624 struct lu_fid *cfid,
1625 struct dt_device *cdev, __u32 ea_off)
1627 struct dt_object *child;
1631 child = lfsck_object_find_by_dev(env, cdev, cfid);
1633 RETURN(PTR_ERR(child));
1635 rc = __lfsck_layout_update_pfid(env, child,
1636 lu_object_fid(&parent->do_lu), ea_off);
1637 lfsck_object_put(env, child);
1639 RETURN(rc == 0 ? 1 : rc);
1643 * This function will create the MDT-object with the given (partial) LOV EA.
1645 * Under some data corruption cases, the MDT-object of the file may be lost,
1646 * but its OST-objects, or some of them are there. The layout LFSCK needs to
1647 * re-create the MDT-object with the orphan OST-object(s) information.
1649 * On the other hand, the LFSCK may has created some OST-object for repairing
1650 * dangling LOV EA reference, but as the LFSCK processing, it may find that
1651 * the old OST-object is there and should replace the former new created OST
1652 * object. Unfortunately, some others have modified such newly created object.
1653 * To keep the data (both new and old), the LFSCK will create MDT-object with
1654 * new FID to reference the original OST-object.
1656 * \param[in] env pointer to the thread context
1657 * \param[in] com pointer to the lfsck component
1658 * \param[in] ltd pointer to target device descriptor
1659 * \param[in] rec pointer to the record for the orphan OST-object
1660 * \param[in] cfid pointer to FID for the orphan OST-object
1661 * \param[in] infix additional information, such as the FID for original
1662 * MDT-object and the stripe offset in the LOV EA
1663 * \param[in] type the type for describing why the orphan MDT-object is
1664 * created. The rules are as following:
1666 * type "C": Multiple OST-objects claim the same MDT-object and the
1667 * same slot in the layout EA. Then the LFSCK will create
1668 * new MDT-object(s) to hold the conflict OST-object(s).
1670 * type "N": The orphan OST-object does not know which one was the
1671 * real parent MDT-object, so the LFSCK uses new FID for
1672 * its parent MDT-object.
1674 * type "R": The orphan OST-object knows its parent MDT-object FID,
1675 * but does not know the position (the file name) in the
1678 * type "D": The MDT-object is a directory, it may knows its parent
1679 * but because there is no valid linkEA, the LFSCK cannot
1680 * know where to put it back to the namespace.
1681 * type "O": The MDT-object has no linkEA, and there is no name
1682 * entry that references the MDT-object.
1684 * type "P": The orphan object to be created was a parent directory
1685 * of some MDT-object which linkEA shows that the @orphan
1686 * object is missing.
1688 * The orphan name will be like:
1689 * ${FID}-${infix}-${type}-${conflict_version}
1691 * \param[in] ea_off the stripe offset in the LOV EA
1693 * \retval positive on repaired something
1694 * \retval 0 if needs to repair nothing
1695 * \retval negative error number on failure
1697 static int lfsck_layout_recreate_parent(const struct lu_env *env,
1698 struct lfsck_component *com,
1699 struct lfsck_tgt_desc *ltd,
1700 struct lu_orphan_rec *rec,
1701 struct lu_fid *cfid,
1706 struct lfsck_thread_info *info = lfsck_env_info(env);
1707 struct dt_insert_rec *dtrec = &info->lti_dt_rec;
1708 char *name = info->lti_key;
1709 struct lu_attr *la = &info->lti_la2;
1710 struct dt_object_format *dof = &info->lti_dof;
1711 struct lfsck_instance *lfsck = com->lc_lfsck;
1712 struct lu_fid *pfid = &rec->lor_fid;
1713 struct lu_fid *tfid = &info->lti_fid3;
1714 struct dt_device *dev = lfsck->li_bottom;
1715 struct dt_object *lpf = lfsck->li_lpf_obj;
1716 struct dt_object *pobj = NULL;
1717 struct dt_object *cobj = NULL;
1718 struct thandle *th = NULL;
1719 struct lu_buf *ea_buf = &info->lti_big_buf;
1720 struct lu_buf lov_buf;
1721 struct lfsck_lock_handle *llh = &info->lti_llh;
1722 struct linkea_data ldata = { NULL };
1723 struct lu_buf linkea_buf;
1724 const struct lu_name *pname;
1730 if (unlikely(lpf == NULL))
1731 GOTO(log, rc = -ENXIO);
1733 /* We use two separated transactions to repair the inconsistency.
1735 * 1) create the MDT-object locally.
1736 * 2) update the OST-object's PFID EA if necessary.
1738 * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
1739 * updated when the layout LFSCK run next time.
1741 * If 1) failed, but 2) succeed, then such MDT-object will be re-created
1742 * when the layout LFSCK run next time. */
1744 if (fid_is_zero(pfid)) {
1745 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
1749 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
1751 GOTO(log, rc = PTR_ERR(cobj));
1754 pobj = lfsck_object_find_by_dev(env, dev, pfid);
1756 GOTO(log, rc = PTR_ERR(pobj));
1758 LASSERT(infix != NULL);
1759 LASSERT(type != NULL);
1761 memset(la, 0, sizeof(*la));
1762 la->la_uid = rec->lor_uid;
1763 la->la_gid = rec->lor_gid;
1764 la->la_mode = S_IFREG | S_IRUSR;
1765 la->la_valid = LA_MODE | LA_UID | LA_GID;
1767 memset(dof, 0, sizeof(*dof));
1768 dof->dof_type = dt_mode_to_dft(S_IFREG);
1769 /* Because the dof->dof_reg.striped = 0, the LOD will not create
1770 * the stripe(s). The LFSCK will specify the LOV EA via
1771 * lfsck_layout_extend_lovea(). */
1773 size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
1774 if (ea_buf->lb_len < size) {
1775 lu_buf_realloc(ea_buf, size);
1776 if (ea_buf->lb_buf == NULL)
1777 GOTO(log, rc = -ENOMEM);
1782 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
1784 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1785 (const struct dt_key *)name);
1786 if (rc != 0 && rc != -ENOENT)
1790 rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
1791 MDS_INODELOCK_UPDATE, LCK_PW);
1795 /* Re-check whether the name conflict with othrs after taken
1797 rc = dt_lookup(env, lfsck->li_lpf_obj, (struct dt_rec *)tfid,
1798 (const struct dt_key *)name);
1799 if (unlikely(rc == 0)) {
1807 rc = linkea_data_new(&ldata,
1808 &lfsck_env_info(env)->lti_linkea_buf);
1812 pname = lfsck_name_get_const(env, name, strlen(name));
1813 rc = linkea_add_buf(&ldata, pname, lfsck_dto2fid(lfsck->li_lpf_obj));
1817 /* The 1st transaction. */
1818 th = dt_trans_create(env, dev);
1820 GOTO(unlock, rc = PTR_ERR(th));
1822 rc = dt_declare_create(env, pobj, la, NULL, dof, th);
1826 lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
1827 rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
1828 LU_XATTR_REPLACE, th);
1832 dtrec->rec_fid = pfid;
1833 dtrec->rec_type = S_IFREG;
1834 rc = dt_declare_insert(env, lpf,
1835 (const struct dt_rec *)dtrec,
1836 (const struct dt_key *)name, th);
1840 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1841 ldata.ld_leh->leh_len);
1842 rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
1843 XATTR_NAME_LINK, 0, th);
1847 rc = dt_trans_start_local(env, dev, th);
1851 dt_write_lock(env, pobj, 0);
1852 rc = dt_create(env, pobj, la, NULL, dof, th);
1854 rc = lfsck_layout_extend_lovea(env, lfsck, th, pobj, cfid,
1855 &lov_buf, 0, ltd->ltd_index, ea_off, true);
1856 dt_write_unlock(env, pobj);
1860 rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
1861 (const struct dt_key *)name, th, 1);
1865 rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
1866 if (rc == 0 && cobj != NULL) {
1867 dt_trans_stop(env, dev, th);
1870 /* The 2nd transaction. */
1871 rc = __lfsck_layout_update_pfid(env, cobj, pfid, ea_off);
1878 dt_trans_stop(env, dev, th);
1884 if (cobj != NULL && !IS_ERR(cobj))
1885 lfsck_object_put(env, cobj);
1886 if (pobj != NULL && !IS_ERR(pobj))
1887 lfsck_object_put(env, pobj);
1890 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
1891 "recreate the lost MDT-object: parent "DFID
1892 ", child "DFID", OST-index %u, stripe-index %u, "
1893 "infix %s, type %s: rc = %d\n",
1894 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
1895 ltd->ltd_index, ea_off, infix, type, rc);
1897 return rc >= 0 ? 1 : rc;
1900 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
1901 struct lfsck_component *com,
1902 const struct lu_fid *fid,
1905 struct lfsck_thread_info *info = lfsck_env_info(env);
1906 struct lfsck_request *lr = &info->lti_lr;
1907 struct lfsck_instance *lfsck = com->lc_lfsck;
1908 struct lfsck_tgt_desc *ltd;
1909 struct ptlrpc_request *req;
1910 struct lfsck_request *tmp;
1911 struct obd_export *exp;
1915 ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
1916 if (unlikely(ltd == NULL))
1920 if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
1921 GOTO(put, rc = -EOPNOTSUPP);
1923 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
1925 GOTO(put, rc = -ENOMEM);
1927 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1929 ptlrpc_request_free(req);
1934 memset(lr, 0, sizeof(*lr));
1935 lr->lr_event = LE_CONDITIONAL_DESTROY;
1936 lr->lr_active = LFSCK_TYPE_LAYOUT;
1939 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1941 ptlrpc_request_set_replen(req);
1943 rc = ptlrpc_queue_wait(req);
1944 ptlrpc_req_finished(req);
1954 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
1955 struct lfsck_component *com,
1956 struct lfsck_request *lr)
1958 struct lfsck_thread_info *info = lfsck_env_info(env);
1959 struct lu_attr *la = &info->lti_la;
1960 union ldlm_policy_data *policy = &info->lti_policy;
1961 struct ldlm_res_id *resid = &info->lti_resid;
1962 struct lfsck_instance *lfsck = com->lc_lfsck;
1963 struct dt_device *dev = lfsck->li_bottom;
1964 struct lu_fid *fid = &lr->lr_fid;
1965 struct dt_object *obj;
1966 struct thandle *th = NULL;
1967 struct lustre_handle lh = { 0 };
1972 obj = lfsck_object_find_by_dev(env, dev, fid);
1974 RETURN(PTR_ERR(obj));
1976 dt_read_lock(env, obj, 0);
1977 if (dt_object_exists(obj) == 0 ||
1978 lfsck_is_dead_obj(obj)) {
1979 dt_read_unlock(env, obj);
1981 GOTO(put, rc = -ENOENT);
1984 /* Get obj's attr without lock firstly. */
1985 rc = dt_attr_get(env, obj, la);
1986 dt_read_unlock(env, obj);
1990 if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
1991 GOTO(put, rc = -ETXTBSY);
1993 /* Acquire extent lock on [0, EOF] to sync with all possible written. */
1994 LASSERT(lfsck->li_namespace != NULL);
1996 memset(policy, 0, sizeof(*policy));
1997 policy->l_extent.end = OBD_OBJECT_EOF;
1998 ost_fid_build_resid(fid, resid);
1999 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_EXTENT,
2000 policy, LCK_EX, &flags, ldlm_blocking_ast,
2001 ldlm_completion_ast, NULL, NULL, 0,
2002 LVB_T_NONE, NULL, &lh);
2004 GOTO(put, rc = -EIO);
2006 dt_write_lock(env, obj, 0);
2007 /* Get obj's attr within lock again. */
2008 rc = dt_attr_get(env, obj, la);
2012 if (la->la_ctime != 0)
2013 GOTO(unlock, rc = -ETXTBSY);
2015 th = dt_trans_create(env, dev);
2017 GOTO(unlock, rc = PTR_ERR(th));
2019 rc = dt_declare_ref_del(env, obj, th);
2023 rc = dt_declare_destroy(env, obj, th);
2027 rc = dt_trans_start_local(env, dev, th);
2031 rc = dt_ref_del(env, obj, th);
2035 rc = dt_destroy(env, obj, th);
2037 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2038 "OST-object "DFID" that was created for reparing "
2039 "dangling referenced case. But the original missing "
2040 "OST-object is found now.\n",
2041 lfsck_lfsck2name(lfsck), PFID(fid));
2046 dt_trans_stop(env, dev, th);
2049 dt_write_unlock(env, obj);
2050 ldlm_lock_decref(&lh, LCK_EX);
2053 lfsck_object_put(env, obj);
2059 * Some OST-object has occupied the specified layout EA slot.
2060 * Such OST-object may be generated by the LFSCK when repair
2061 * dangling referenced MDT-object, which can be indicated by
2062 * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2063 * is true and such OST-object has not been modified yet, we
2064 * will replace it with the orphan OST-object; otherwise the
2065 * LFSCK will create new MDT-object to reference the orphan.
2067 * \retval +1: repaired
2068 * \retval 0: did nothing
2069 * \retval -ve: on error
2071 static int lfsck_layout_conflict_create(const struct lu_env *env,
2072 struct lfsck_component *com,
2073 struct lfsck_tgt_desc *ltd,
2074 struct lu_orphan_rec *rec,
2075 struct dt_object *parent,
2076 struct lu_fid *cfid,
2077 struct lu_buf *ea_buf,
2078 struct lov_ost_data_v1 *slot,
2081 struct lfsck_thread_info *info = lfsck_env_info(env);
2082 struct lu_fid *cfid2 = &info->lti_fid2;
2083 struct ost_id *oi = &info->lti_oi;
2084 struct lov_mds_md_v1 *lmm = ea_buf->lb_buf;
2085 struct dt_device *dev = lfsck_obj2dev(parent);
2086 struct thandle *th = NULL;
2087 struct lustre_handle lh = { 0 };
2088 __u32 ost_idx2 = le32_to_cpu(slot->l_ost_idx);
2092 ostid_le_to_cpu(&slot->l_ost_oi, oi);
2093 rc = ostid_to_fid(cfid2, oi, ost_idx2);
2097 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2098 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2103 rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2105 /* If the conflict OST-obejct is not created for fixing dangling
2106 * referenced MDT-object in former LFSCK check/repair, or it has
2107 * been modified by others, then we cannot destroy it. Re-create
2108 * a new MDT-object for the orphan OST-object. */
2109 if (rc == -ETXTBSY) {
2110 /* No need the layout lock on the original parent. */
2111 lfsck_ibits_unlock(&lh, LCK_EX);
2113 fid_zero(&rec->lor_fid);
2114 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2115 "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2117 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2118 info->lti_tmpbuf, "C", ea_off);
2123 if (rc != 0 && rc != -ENOENT)
2126 th = dt_trans_create(env, dev);
2128 GOTO(unlock, rc = PTR_ERR(th));
2130 rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2131 LU_XATTR_REPLACE, th);
2135 rc = dt_trans_start_local(env, dev, th);
2139 dt_write_lock(env, parent, 0);
2140 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2141 rc = lfsck_layout_refill_lovea(env, th, parent, cfid, ea_buf, slot,
2142 LU_XATTR_REPLACE, ltd->ltd_index);
2143 dt_write_unlock(env, parent);
2148 dt_trans_stop(env, dev, th);
2151 lfsck_ibits_unlock(&lh, LCK_EX);
2154 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2155 "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2156 "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2157 lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2158 PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2161 return rc >= 0 ? 1 : rc;
2165 * \retval +1: repaired
2166 * \retval 0: did nothing
2167 * \retval -ve: on error
2169 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2170 struct lfsck_component *com,
2171 struct lfsck_tgt_desc *ltd,
2172 struct lu_orphan_rec *rec,
2173 struct dt_object *parent,
2174 struct lu_fid *cfid,
2175 __u32 ost_idx, __u32 ea_off)
2177 struct lfsck_thread_info *info = lfsck_env_info(env);
2178 struct lu_buf *buf = &info->lti_big_buf;
2179 struct lu_fid *fid = &info->lti_fid2;
2180 struct ost_id *oi = &info->lti_oi;
2181 struct lfsck_instance *lfsck = com->lc_lfsck;
2182 struct dt_device *dt = lfsck_obj2dev(parent);
2183 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2184 struct thandle *handle = NULL;
2186 struct lov_mds_md_v1 *lmm;
2187 struct lov_ost_data_v1 *objs;
2188 struct lustre_handle lh = { 0 };
2195 bool locked = false;
2198 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2199 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2202 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2203 "LOV EA for "DFID": parent "DFID", OST-index %u, "
2204 "stripe-index %u: rc = %d\n",
2205 lfsck_lfsck2name(lfsck), PFID(cfid),
2206 PFID(lfsck_dto2fid(parent)), ost_idx, ea_off, rc);
2213 dt_write_unlock(env, parent);
2217 if (handle != NULL) {
2218 dt_trans_stop(env, dt, handle);
2223 GOTO(unlock_layout, rc);
2226 if (buf->lb_len < lovea_size) {
2227 lu_buf_realloc(buf, lovea_size);
2228 if (buf->lb_buf == NULL)
2229 GOTO(unlock_layout, rc = -ENOMEM);
2232 if (!(bk->lb_param & LPF_DRYRUN)) {
2233 handle = dt_trans_create(env, dt);
2235 GOTO(unlock_layout, rc = PTR_ERR(handle));
2237 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2242 rc = dt_trans_start_local(env, dt, handle);
2247 dt_write_lock(env, parent, 0);
2249 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2250 if (rc == -ERANGE) {
2251 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2254 } else if (rc == -ENODATA || rc == 0) {
2255 lovea_size = lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2256 /* If the declared is not big enough, re-try. */
2257 if (buf->lb_len < lovea_size) {
2261 fl = LU_XATTR_CREATE;
2262 } else if (rc < 0) {
2263 GOTO(unlock_parent, rc);
2264 } else if (unlikely(buf->lb_len == 0)) {
2267 fl = LU_XATTR_REPLACE;
2271 if (fl == LU_XATTR_CREATE) {
2272 if (bk->lb_param & LPF_DRYRUN)
2273 GOTO(unlock_parent, rc = 1);
2275 LASSERT(buf->lb_len >= lovea_size);
2277 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2278 buf, fl, ost_idx, ea_off, false);
2280 GOTO(unlock_parent, rc);
2284 rc1 = lfsck_layout_verify_header(lmm);
2286 /* If the LOV EA crashed, the rebuild it. */
2287 if (rc1 == -EINVAL) {
2288 if (bk->lb_param & LPF_DRYRUN)
2289 GOTO(unlock_parent, rc = 1);
2291 LASSERT(buf->lb_len >= lovea_size);
2293 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2294 buf, fl, ost_idx, ea_off, true);
2296 GOTO(unlock_parent, rc);
2299 /* For other unknown magic/pattern, keep the current LOV EA. */
2301 GOTO(unlock_parent, rc = rc1);
2303 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2304 * been verified in lfsck_layout_verify_header() already. If some
2305 * new magic introduced in the future, then layout LFSCK needs to
2306 * be updated also. */
2307 magic = le32_to_cpu(lmm->lmm_magic);
2308 if (magic == LOV_MAGIC_V1) {
2309 objs = &lmm->lmm_objects[0];
2311 LASSERT(magic == LOV_MAGIC_V3);
2312 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
2315 count = le16_to_cpu(lmm->lmm_stripe_count);
2317 GOTO(unlock_parent, rc = -EINVAL);
2320 /* Exceed the current end of MDT-object layout EA. Then extend it. */
2321 if (count <= ea_off) {
2322 if (bk->lb_param & LPF_DRYRUN)
2323 GOTO(unlock_parent, rc = 1);
2325 lovea_size = lov_mds_md_size(ea_off + 1, magic);
2326 /* If the declared is not big enough, re-try. */
2327 if (buf->lb_len < lovea_size) {
2332 rc = lfsck_layout_extend_lovea(env, lfsck, handle, parent, cfid,
2333 buf, fl, ost_idx, ea_off, false);
2335 GOTO(unlock_parent, rc);
2338 LASSERTF(rc > 0, "invalid rc = %d\n", rc);
2340 for (i = 0; i < count; i++, objs++) {
2341 /* The MDT-object was created via lfsck_layout_recover_create()
2342 * by others before, and we fill the dummy layout EA. */
2343 if (lovea_slot_is_dummy(objs)) {
2347 if (bk->lb_param & LPF_DRYRUN)
2348 GOTO(unlock_parent, rc = 1);
2350 lmm->lmm_layout_gen =
2351 cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2352 rc = lfsck_layout_refill_lovea(env, handle, parent,
2353 cfid, buf, objs, fl,
2356 CDEBUG(D_LFSCK, "%s layout LFSCK assistant fill "
2357 "dummy layout slot for "DFID": parent "DFID
2358 ", OST-index %u, stripe-index %u: rc = %d\n",
2359 lfsck_lfsck2name(lfsck), PFID(cfid),
2360 PFID(lfsck_dto2fid(parent)), ost_idx, i, rc);
2362 GOTO(unlock_parent, rc);
2365 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2366 rc = ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
2368 CDEBUG(D_LFSCK, "%s: the parent "DFID" contains "
2369 "invalid layout EA at the slot %d, index %u\n",
2370 lfsck_lfsck2name(lfsck),
2371 PFID(lfsck_dto2fid(parent)), i,
2372 le32_to_cpu(objs->l_ost_idx));
2374 GOTO(unlock_parent, rc);
2377 /* It should be rare case, the slot is there, but the LFSCK
2378 * does not handle it during the first-phase cycle scanning. */
2379 if (unlikely(lu_fid_eq(fid, cfid))) {
2381 GOTO(unlock_parent, rc = 0);
2383 /* Rare case that the OST-object index
2384 * does not match the parent MDT-object
2385 * layout EA. We trust the later one. */
2386 if (bk->lb_param & LPF_DRYRUN)
2387 GOTO(unlock_parent, rc = 1);
2389 dt_write_unlock(env, parent);
2391 dt_trans_stop(env, dt, handle);
2392 lfsck_ibits_unlock(&lh, LCK_EX);
2393 rc = lfsck_layout_update_pfid(env, com, parent,
2394 cfid, ltd->ltd_tgt, i);
2396 CDEBUG(D_LFSCK, "%s layout LFSCK assistant "
2397 "updated OST-object's pfid for "DFID
2398 ": parent "DFID", OST-index %u, "
2399 "stripe-index %u: rc = %d\n",
2400 lfsck_lfsck2name(lfsck), PFID(cfid),
2401 PFID(lfsck_dto2fid(parent)),
2402 ltd->ltd_index, i, rc);
2409 /* The MDT-object exists, but related layout EA slot is occupied
2411 if (bk->lb_param & LPF_DRYRUN)
2412 GOTO(unlock_parent, rc = 1);
2414 dt_write_unlock(env, parent);
2416 dt_trans_stop(env, dt, handle);
2417 lfsck_ibits_unlock(&lh, LCK_EX);
2418 if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1)
2419 objs = &lmm->lmm_objects[ea_off];
2421 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[ea_off];
2422 rc = lfsck_layout_conflict_create(env, com, ltd, rec, parent, cfid,
2429 dt_write_unlock(env, parent);
2433 dt_trans_stop(env, dt, handle);
2436 lfsck_ibits_unlock(&lh, LCK_EX);
2441 static int lfsck_layout_scan_orphan_one(const struct lu_env *env,
2442 struct lfsck_component *com,
2443 struct lfsck_tgt_desc *ltd,
2444 struct lu_orphan_rec *rec,
2445 struct lu_fid *cfid)
2447 struct lfsck_layout *lo = com->lc_file_ram;
2448 struct lu_fid *pfid = &rec->lor_fid;
2449 struct dt_object *parent = NULL;
2450 __u32 ea_off = pfid->f_stripe_idx;
2454 if (!fid_is_sane(cfid))
2455 GOTO(out, rc = -EINVAL);
2457 if (fid_is_zero(pfid)) {
2458 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2464 if (!fid_is_sane(pfid))
2465 GOTO(out, rc = -EINVAL);
2467 parent = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
2469 GOTO(out, rc = PTR_ERR(parent));
2471 if (unlikely(dt_object_remote(parent) != 0))
2472 GOTO(put, rc = -EXDEV);
2474 if (dt_object_exists(parent) == 0) {
2475 lfsck_object_put(env, parent);
2476 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2481 if (!S_ISREG(lu_object_attr(&parent->do_lu)))
2482 GOTO(put, rc = -EISDIR);
2484 rc = lfsck_layout_recreate_lovea(env, com, ltd, rec, parent, cfid,
2485 ltd->ltd_index, ea_off);
2491 lfsck_object_put(env, parent);
2493 /* The layout EA is changed, need to be reloaded next time. */
2494 lu_object_put_nocache(env, &parent->do_lu);
2497 down_write(&com->lc_sem);
2498 com->lc_new_scanned++;
2499 com->lc_new_checked++;
2501 lo->ll_objs_repaired[LLIT_ORPHAN - 1]++;
2503 } else if (rc < 0) {
2504 lo->ll_objs_failed_phase2++;
2506 up_write(&com->lc_sem);
2511 static int lfsck_layout_scan_orphan(const struct lu_env *env,
2512 struct lfsck_component *com,
2513 struct lfsck_tgt_desc *ltd)
2515 struct lfsck_assistant_data *lad = com->lc_data;
2516 struct lfsck_instance *lfsck = com->lc_lfsck;
2517 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2518 struct lfsck_thread_info *info = lfsck_env_info(env);
2519 struct ost_id *oi = &info->lti_oi;
2520 struct lu_fid *fid = &info->lti_fid;
2521 struct dt_object *obj;
2522 const struct dt_it_ops *iops;
2527 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant starts the orphan "
2528 "scanning for OST%04x\n",
2529 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2531 if (cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
2532 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant skip the orphan "
2533 "scanning for OST%04x\n",
2534 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2539 ostid_set_seq(oi, FID_SEQ_IDIF);
2540 ostid_set_id(oi, 0);
2541 rc = ostid_to_fid(fid, oi, ltd->ltd_index);
2545 obj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, fid);
2546 if (unlikely(IS_ERR(obj)))
2547 GOTO(log, rc = PTR_ERR(obj));
2549 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_orphan_features);
2553 iops = &obj->do_index_ops->dio_it;
2554 di = iops->init(env, obj, 0);
2556 GOTO(put, rc = PTR_ERR(di));
2558 rc = iops->load(env, di, 0);
2560 /* -ESRCH means that the orphan OST-objects rbtree has been
2561 * cleanup because of the OSS server restart or other errors. */
2562 lfsck_lad_set_bitmap(env, com, ltd->ltd_index);
2567 rc = iops->next(env, di);
2579 struct lu_orphan_rec *rec = &info->lti_rec;
2581 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
2582 unlikely(!thread_is_running(&lfsck->li_thread)))
2585 key = iops->key(env, di);
2586 com->lc_fid_latest_scanned_phase2 = *(struct lu_fid *)key;
2587 rc = iops->rec(env, di, (struct dt_rec *)rec, 0);
2589 rc = lfsck_layout_scan_orphan_one(env, com, ltd, rec,
2590 &com->lc_fid_latest_scanned_phase2);
2591 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2594 lfsck_control_speed_by_self(com);
2596 rc = iops->next(env, di);
2597 } while (rc < 0 && !(bk->lb_param & LPF_FAILOUT));
2604 iops->fini(env, di);
2606 lfsck_object_put(env, obj);
2609 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant finished the orphan "
2610 "scanning for OST%04x: rc = %d\n",
2611 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
2613 return rc > 0 ? 0 : rc;
2616 /* For the MDT-object with dangling reference, we need to repare the
2617 * inconsistency according to the LFSCK sponsor's requirement:
2619 * 1) Keep the inconsistency there and report the inconsistency case,
2620 * then give the chance to the application to find related issues,
2621 * and the users can make the decision about how to handle it with
2622 * more human knownledge. (by default)
2624 * 2) Re-create the missing OST-object with the FID/owner information. */
2625 static int lfsck_layout_repair_dangling(const struct lu_env *env,
2626 struct lfsck_component *com,
2627 struct dt_object *parent,
2628 struct lfsck_layout_req *llr,
2631 struct lfsck_thread_info *info = lfsck_env_info(env);
2632 struct filter_fid *pfid = &info->lti_new_pfid;
2633 struct dt_object_format *dof = &info->lti_dof;
2634 struct dt_object *child = llr->llr_child;
2635 struct dt_device *dev = lfsck_obj2dev(child);
2636 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2637 struct thandle *handle;
2639 struct lustre_handle lh = { 0 };
2644 if (com->lc_lfsck->li_bookmark_ram.lb_param & LPF_CREATE_OSTOBJ)
2652 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2653 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2658 rc = dt_attr_get(env, parent, la);
2662 la->la_mode = S_IFREG | 0666;
2663 la->la_atime = la->la_mtime = la->la_ctime = 0;
2664 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
2665 LA_ATIME | LA_MTIME | LA_CTIME;
2666 memset(dof, 0, sizeof(*dof));
2667 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2668 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2669 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2670 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2671 * parent MDT-object's layout EA. */
2672 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2673 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2675 handle = dt_trans_create(env, dev);
2677 GOTO(unlock1, rc = PTR_ERR(handle));
2679 rc = dt_declare_create(env, child, la, NULL, dof, handle);
2683 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID,
2684 LU_XATTR_CREATE, handle);
2688 rc = dt_trans_start_local(env, dev, handle);
2692 dt_read_lock(env, parent, 0);
2693 if (unlikely(lfsck_is_dead_obj(parent)))
2694 GOTO(unlock2, rc = 1);
2696 rc = dt_create(env, child, la, NULL, dof, handle);
2700 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, LU_XATTR_CREATE,
2706 dt_read_unlock(env, parent);
2709 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2712 lfsck_ibits_unlock(&lh, LCK_EX);
2716 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant found "
2717 "dangling reference for: parent "DFID", child "DFID
2718 ", OST-index %u, stripe-index %u, owner %u/%u. %s: "
2719 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2720 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2721 llr->llr_ost_idx, llr->llr_lov_idx,
2722 la->la_uid, la->la_gid,
2723 create ? "Create the lost OST-object as required" :
2724 "Keep the MDT-object there by default", rc);
2729 /* If the OST-object does not recognize the MDT-object as its parent, and
2730 * there is no other MDT-object claims as its parent, then just trust the
2731 * given MDT-object as its parent. So update the OST-object filter_fid. */
2732 static int lfsck_layout_repair_unmatched_pair(const struct lu_env *env,
2733 struct lfsck_component *com,
2734 struct dt_object *parent,
2735 struct lfsck_layout_req *llr,
2738 struct lfsck_thread_info *info = lfsck_env_info(env);
2739 struct filter_fid *pfid = &info->lti_new_pfid;
2740 struct dt_object *child = llr->llr_child;
2741 struct dt_device *dev = lfsck_obj2dev(child);
2742 const struct lu_fid *tfid = lu_object_fid(&parent->do_lu);
2743 struct thandle *handle;
2745 struct lustre_handle lh = { 0 };
2749 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2750 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2755 pfid->ff_parent.f_seq = cpu_to_le64(tfid->f_seq);
2756 pfid->ff_parent.f_oid = cpu_to_le32(tfid->f_oid);
2757 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2758 * MDT-object's FID::f_ver, instead it is the OST-object index in its
2759 * parent MDT-object's layout EA. */
2760 pfid->ff_parent.f_stripe_idx = cpu_to_le32(llr->llr_lov_idx);
2761 buf = lfsck_buf_get(env, pfid, sizeof(struct filter_fid));
2763 handle = dt_trans_create(env, dev);
2765 GOTO(unlock1, rc = PTR_ERR(handle));
2767 rc = dt_declare_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2771 rc = dt_attr_get(env, parent, la);
2775 la->la_valid = LA_UID | LA_GID;
2776 rc = dt_declare_attr_set(env, child, la, handle);
2780 rc = dt_trans_start_local(env, dev, handle);
2784 dt_write_lock(env, parent, 0);
2785 if (unlikely(lfsck_is_dead_obj(parent)))
2786 GOTO(unlock2, rc = 1);
2788 rc = dt_xattr_set(env, child, buf, XATTR_NAME_FID, 0, handle);
2792 /* Get the latest parent's owner. */
2793 rc = dt_attr_get(env, parent, la);
2797 la->la_valid = LA_UID | LA_GID;
2798 rc = dt_attr_set(env, child, la, handle);
2803 dt_write_unlock(env, parent);
2806 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
2809 lfsck_ibits_unlock(&lh, LCK_EX);
2813 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
2814 "unmatched MDT-OST pair for: parent "DFID
2815 ", child "DFID", OST-index %u, stripe-index %u, "
2816 "owner %u/%u: rc = %d\n",
2817 lfsck_lfsck2name(com->lc_lfsck),
2818 PFID(lfsck_dto2fid(parent)),
2819 PFID(lfsck_dto2fid(child)),
2820 llr->llr_ost_idx, llr->llr_lov_idx,
2821 la->la_uid, la->la_gid, rc);
2826 /* If there are more than one MDT-objects claim as the OST-object's parent,
2827 * and the OST-object only recognizes one of them, then we need to generate
2828 * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
2829 static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
2830 struct lfsck_component *com,
2831 struct dt_object *parent,
2832 struct lfsck_layout_req *llr,
2836 struct lfsck_thread_info *info = lfsck_env_info(env);
2837 struct dt_allocation_hint *hint = &info->lti_hint;
2838 struct dt_object_format *dof = &info->lti_dof;
2839 struct ost_id *oi = &info->lti_oi;
2840 struct lfsck_instance *lfsck = com->lc_lfsck;
2841 struct dt_device *dev;
2842 struct lu_device *d =
2843 &lfsck_obj2dev(llr->llr_child)->dd_lu_dev;
2844 struct lu_object *o;
2845 struct lu_object *n;
2846 struct dt_object *child = NULL;
2847 struct thandle *handle = NULL;
2848 struct lov_mds_md_v1 *lmm;
2849 struct lov_ost_data_v1 *objs;
2850 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2852 struct lustre_handle lh = { 0 };
2853 struct lu_buf ea_buf;
2859 /* We use two separated transactions to repair the inconsistency.
2861 * 1) create the child (OST-object).
2862 * 2) update the parent LOV EA according to the child's FID.
2864 * If 1) succeed, but 2) failed or aborted, then such OST-object will be
2865 * handled as orphan when the layout LFSCK run next time.
2867 * If 1) failed, but 2) succeed, then such OST-object will be re-created
2868 * as dangling referened case when the layout LFSCK run next time. */
2870 /* The 1st transaction. */
2871 o = lu_object_anon(env, d, NULL);
2873 GOTO(log, rc = PTR_ERR(o));
2875 n = lu_object_locate(o->lo_header, d->ld_type);
2876 if (unlikely(n == NULL)) {
2877 lu_object_put_nocache(env, o);
2879 GOTO(log, rc = -EINVAL);
2882 child = container_of(n, struct dt_object, do_lu);
2883 memset(hint, 0, sizeof(*hint));
2884 rc = dt_attr_get(env, parent, la);
2888 la->la_valid = LA_UID | LA_GID;
2889 memset(dof, 0, sizeof(*dof));
2891 dev = lfsck_obj2dev(child);
2892 handle = dt_trans_create(env, dev);
2894 GOTO(log, rc = PTR_ERR(handle));
2896 rc = dt_declare_create(env, child, la, hint, dof, handle);
2900 rc = dt_trans_start_local(env, dev, handle);
2904 rc = dt_create(env, child, la, hint, dof, handle);
2905 dt_trans_stop(env, dev, handle);
2910 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2911 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2916 /* The 2nd transaction. */
2918 /* XXX: Generally, we should use bottom device (OSD) to update parent
2919 * LOV EA. But because the LOD-object still references the wrong
2920 * OSP-object that should be detached after the parent's LOV EA
2921 * refreshed. Unfortunately, there is no suitable API for that.
2922 * So we have to make the LOD to re-load the OSP-object(s) via
2923 * replacing the LOV EA against the LOD-object.
2925 * Once the DNE2 patches have been landed, we can replace the
2926 * LOD device with the OSD device. LU-6230. */
2928 dev = lfsck->li_next;
2929 parent = lfsck_object_locate(dev, parent);
2931 GOTO(log, rc = PTR_ERR(parent));
2933 handle = dt_trans_create(env, dev);
2935 GOTO(log, rc = PTR_ERR(handle));
2937 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2938 LU_XATTR_REPLACE, handle);
2942 rc = dt_trans_start_local(env, dev, handle);
2946 dt_write_lock(env, parent, 0);
2947 if (unlikely(lfsck_is_dead_obj(parent)))
2948 GOTO(unlock, rc = 0);
2950 rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2951 if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
2952 GOTO(unlock, rc = 0);
2955 /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
2956 * been verified in lfsck_layout_verify_header() already. If some
2957 * new magic introduced in the future, then layout LFSCK needs to
2958 * be updated also. */
2959 magic = le32_to_cpu(lmm->lmm_magic);
2960 if (magic == LOV_MAGIC_V1) {
2961 objs = &lmm->lmm_objects[llr->llr_lov_idx];
2963 LASSERT(magic == LOV_MAGIC_V3);
2965 &((struct lov_mds_md_v3 *)lmm)->lmm_objects[llr->llr_lov_idx];
2968 ostid_le_to_cpu(&objs->l_ost_oi, oi);
2969 index = le32_to_cpu(objs->l_ost_idx);
2970 rc = ostid_to_fid(&tfid, oi, index);
2971 /* Someone changed layout during the LFSCK, no need to repair then. */
2972 if (rc == 0 && !lu_fid_eq(&tfid, lu_object_fid(&llr->llr_child->do_lu)))
2973 GOTO(unlock, rc = 0);
2975 lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2976 fid_to_ostid(lu_object_fid(&child->do_lu), oi);
2977 ostid_cpu_to_le(oi, &objs->l_ost_oi);
2978 objs->l_ost_gen = cpu_to_le32(0);
2979 objs->l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
2980 lfsck_buf_init(&ea_buf, lmm,
2981 lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2983 rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV,
2984 LU_XATTR_REPLACE, handle);
2986 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2989 dt_write_unlock(env, parent);
2993 dt_trans_stop(env, dev, handle);
2996 lfsck_ibits_unlock(&lh, LCK_EX);
2998 lfsck_object_put(env, child);
3001 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3002 "multiple references for: parent "DFID", OST-index %u, "
3003 "stripe-index %u, owner %u/%u: rc = %d\n",
3004 lfsck_lfsck2name(lfsck), PFID(pfid), llr->llr_ost_idx,
3005 llr->llr_lov_idx, la->la_uid, la->la_gid, rc);
3010 /* If the MDT-object and the OST-object have different owner information,
3011 * then trust the MDT-object, because the normal chown/chgrp handle order
3012 * is from MDT to OST, and it is possible that some chown/chgrp operation
3013 * is partly done. */
3014 static int lfsck_layout_repair_owner(const struct lu_env *env,
3015 struct lfsck_component *com,
3016 struct dt_object *parent,
3017 struct lfsck_layout_req *llr,
3018 struct lu_attr *pla,
3019 const struct lu_attr *cla)
3021 struct lfsck_thread_info *info = lfsck_env_info(env);
3022 struct lu_attr *tla = &info->lti_la2;
3023 struct dt_object *child = llr->llr_child;
3024 struct dt_device *dev = lfsck_obj2dev(child);
3025 struct thandle *handle;
3029 tla->la_uid = pla->la_uid;
3030 tla->la_gid = pla->la_gid;
3031 tla->la_valid = LA_UID | LA_GID;
3032 handle = dt_trans_create(env, dev);
3034 GOTO(log, rc = PTR_ERR(handle));
3036 rc = dt_declare_attr_set(env, child, tla, handle);
3040 rc = dt_trans_start_local(env, dev, handle);
3044 /* Use the dt_object lock to serialize with destroy and attr_set. */
3045 dt_read_lock(env, parent, 0);
3046 if (unlikely(lfsck_is_dead_obj(parent)))
3047 GOTO(unlock, rc = 1);
3049 /* Get the latest parent's owner. */
3050 rc = dt_attr_get(env, parent, pla);
3054 /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
3055 if (unlikely(tla->la_uid != pla->la_uid ||
3056 tla->la_gid != pla->la_gid))
3059 rc = dt_attr_set(env, child, tla, handle);
3064 dt_read_unlock(env, parent);
3067 rc = lfsck_layout_trans_stop(env, dev, handle, rc);
3071 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant repaired "
3072 "inconsistent file owner for: parent "DFID", child "DFID
3073 ", OST-index %u, stripe-index %u, old owner %u/%u, "
3074 "new owner %u/%u: rc = %d\n",
3075 lfsck_lfsck2name(com->lc_lfsck),
3076 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
3077 llr->llr_ost_idx, llr->llr_lov_idx,
3078 cla->la_uid, cla->la_gid, tla->la_uid, tla->la_gid, rc);
3083 /* Check whether the OST-object correctly back points to the
3084 * MDT-object (@parent) via the XATTR_NAME_FID xattr (@pfid). */
3085 static int lfsck_layout_check_parent(const struct lu_env *env,
3086 struct lfsck_component *com,
3087 struct lfsck_assistant_object *lso,
3088 const struct lu_fid *pfid,
3089 const struct lu_fid *cfid,
3090 const struct lu_attr *cla,
3091 struct lfsck_layout_req *llr,
3092 struct lu_buf *lov_ea, __u32 idx)
3094 struct lfsck_thread_info *info = lfsck_env_info(env);
3095 struct lu_buf *buf = &info->lti_big_buf;
3096 struct dt_object *tobj;
3097 struct lov_mds_md_v1 *lmm;
3098 struct lov_ost_data_v1 *objs;
3099 struct lustre_handle lh = { 0 };
3106 if (unlikely(!fid_is_sane(pfid)))
3107 RETURN(LLIT_UNMATCHED_PAIR);
3109 if (lu_fid_eq(pfid, &lso->lso_fid)) {
3110 if (likely(llr->llr_lov_idx == idx))
3113 RETURN(LLIT_UNMATCHED_PAIR);
3116 tobj = lfsck_object_find_bottom(env, com->lc_lfsck, pfid);
3118 RETURN(PTR_ERR(tobj));
3120 if (dt_object_exists(tobj) == 0 || lfsck_is_dead_obj(tobj) ||
3121 !S_ISREG(lfsck_object_type(tobj)))
3122 GOTO(out, rc = LLIT_UNMATCHED_PAIR);