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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2012, 2017, Intel Corporation.
26 * lustre/osd-ldiskfs/osd_scrub.c
28 * Top-level entry points into osd module
30 * The OI scrub is used for rebuilding Object Index files when restores MDT from
33 * The otable based iterator scans ldiskfs inode table to feed up layer LFSCK.
35 * Author: Fan Yong <yong.fan@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LFSCK
40 #include <linux/kthread.h>
41 #include <uapi/linux/lustre/lustre_idl.h>
42 #include <lustre_disk.h>
43 #include <dt_object.h>
44 #include <linux/xattr.h>
45 #include <lustre_scrub.h>
46 #include <lustre_nodemap.h>
48 #include "osd_internal.h"
50 #include "osd_scrub.h"
52 #define OSD_OTABLE_MAX_HASH 0x00000000ffffffffULL
54 static inline int osd_scrub_has_window(struct lustre_scrub *scrub,
55 struct osd_otable_cache *ooc)
57 return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
61 * update/insert/delete the specified OI mapping (@fid @id) according to the ops
63 * \retval 1, changed nothing
64 * \retval 0, changed successfully
65 * \retval -ve, on error
67 int osd_scrub_refresh_mapping(struct osd_thread_info *info,
68 struct osd_device *dev,
69 const struct lu_fid *fid,
70 const struct osd_inode_id *id,
72 enum oi_check_flags flags, bool *exist)
78 if (dev->od_scrub.os_scrub.os_file.sf_param & SP_DRYRUN && !force)
81 /* DTO_INDEX_INSERT is enough for other two ops:
82 * delete/update, but save stack. */
83 th = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
84 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
87 CDEBUG(D_LFSCK, "%s: fail to start trans for scrub op %d "
88 DFID" => %u/%u: rc = %d\n", osd_name(dev), ops,
89 PFID(fid), id ? id->oii_ino : -1, id ? id->oii_gen : -1,
95 case DTO_INDEX_UPDATE:
96 rc = osd_oi_update(info, dev, fid, id, th, flags);
97 if (unlikely(rc == -ENOENT)) {
98 /* Some unlink thread may removed the OI mapping. */
102 case DTO_INDEX_INSERT:
103 rc = osd_oi_insert(info, dev, fid, id, th, flags, exist);
104 if (unlikely(rc == -EEXIST)) {
106 /* XXX: There are trouble things when adding OI
107 * mapping for IGIF object, which may cause
108 * multiple objects to be mapped to the same
109 * IGIF formatted FID. Consider the following
112 * 1) The MDT is upgrading from 1.8 device.
113 * The OI scrub generates IGIF FID1 for the
114 * OBJ1 and adds the OI mapping.
116 * 2) For some reason, the OI scrub does not
117 * process all the IGIF objects completely.
119 * 3) The MDT is backuped and restored against
122 * 4) When the MDT mounts up, the OI scrub will
123 * try to rebuild the OI files. For some IGIF
124 * object, OBJ2, which was not processed by the
125 * OI scrub before the backup/restore, and the
126 * new generated IGIF formatted FID may be just
127 * the FID1, the same as OBJ1.
129 * Under such case, the OI scrub cannot know how
130 * to generate new FID for the OBJ2.
132 * Currently, we do nothing for that. One possible
133 * solution is to generate new normal FID for the
136 * Anyway, it is rare, only exists in theory. */
139 case DTO_INDEX_DELETE:
140 rc = osd_oi_delete(info, dev, fid, th, flags);
142 /* It is normal that the unlink thread has removed the
143 * OI mapping already. */
148 LASSERTF(0, "Unexpected ops %d\n", ops);
152 ldiskfs_journal_stop(th);
154 CDEBUG(D_LFSCK, "%s: fail to refresh OI map for scrub op %d "
155 DFID" => %u/%u: rc = %d\n", osd_name(dev), ops,
156 PFID(fid), id ? id->oii_ino : -1, id ? id->oii_gen : -1,
163 osd_scrub_convert_ff(struct osd_thread_info *info, struct osd_device *dev,
164 struct inode *inode, const struct lu_fid *fid)
166 struct filter_fid_18_23 *ff = &info->oti_ff_old;
167 struct dentry *dentry = &info->oti_obj_dentry;
168 struct lu_fid *tfid = &info->oti_fid;
169 bool fid_18_23 = false;
175 if (dev->od_scrub.os_scrub.os_file.sf_param & SP_DRYRUN)
178 if (fid_is_idif(fid) && dev->od_index_in_idif == 0) {
179 struct ost_id *oi = &info->oti_ostid;
181 fid_to_ostid(fid, oi);
182 ostid_to_fid(tfid, oi, 0);
187 /* We want the LMA to fit into the 256-byte OST inode, so operate
189 * 1) read old XATTR_NAME_FID and save the parent FID;
190 * 2) delete the old XATTR_NAME_FID;
191 * 3) make new LMA and add it;
192 * 4) generate new XATTR_NAME_FID with the saved parent FID and add it.
194 * Making the LMA to fit into the 256-byte OST inode can save time for
195 * normal osd_check_lma() and for other OI scrub scanning in future.
196 * So it is worth to make some slow conversion here. */
197 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
198 osd_dto_credits_noquota[DTO_XATTR_SET] * 3);
201 CDEBUG(D_LFSCK, "%s: fail to start trans for convert ff "
202 DFID": rc = %d\n", osd_name(dev), PFID(tfid), rc);
206 /* 1) read old XATTR_NAME_FID and save the parent FID */
207 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
208 if (rc == sizeof(*ff)) {
209 /* 2) delete the old XATTR_NAME_FID */
210 dquot_initialize(inode);
211 rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
216 } else if (rc != -ENODATA && rc < (int)sizeof(struct filter_fid_24_29)) {
217 GOTO(stop, rc = -EINVAL);
220 /* 3) make new LMA and add it */
221 rc = osd_ea_fid_set(info, inode, tfid, LMAC_FID_ON_OST, 0);
224 /* If failed, we should try to add the old back. */
227 /* The new PFID EA will only contains ::ff_parent */
228 size = sizeof(ff->ff_parent);
231 /* 4) generate new XATTR_NAME_FID with the saved parent FID and add it*/
235 rc1 = __osd_xattr_set(info, inode, XATTR_NAME_FID, ff, size,
237 if (rc1 != 0 && rc == 0)
244 ldiskfs_journal_stop(jh);
246 CDEBUG(D_LFSCK, "%s: fail to convert ff "DFID": rc = %d\n",
247 osd_name(dev), PFID(tfid), rc);
252 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
253 struct osd_idmap_cache *oic, int val)
255 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
256 struct scrub_file *sf = &scrub->os_file;
257 struct lu_fid *fid = &oic->oic_fid;
258 struct osd_inode_id *lid = &oic->oic_lid;
259 struct osd_inode_id *lid2 = &info->oti_id;
260 struct osd_inconsistent_item *oii = NULL;
261 struct inode *inode = NULL;
262 int ops = DTO_INDEX_UPDATE;
264 bool converted = false;
268 down_write(&scrub->os_rwsem);
269 scrub->os_new_checked++;
273 if (scrub->os_in_prior)
274 oii = list_entry(oic, struct osd_inconsistent_item,
277 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
280 if (fid_is_igif(fid))
283 if (val == SCRUB_NEXT_OSTOBJ_OLD) {
284 inode = osd_iget(info, dev, lid);
287 /* Someone removed the inode. */
288 if (rc == -ENOENT || rc == -ESTALE)
293 /* The inode has been reused as EA inode, ignore it. */
294 if (unlikely(osd_is_ea_inode(inode)))
297 sf->sf_flags |= SF_UPGRADE;
298 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
299 dev->od_check_ff = 1;
300 rc = osd_scrub_convert_ff(info, dev, inode, fid);
307 if ((val == SCRUB_NEXT_NOLMA) &&
308 (!scrub->os_convert_igif || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
311 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA)) {
312 ops = DTO_INDEX_INSERT;
317 rc = osd_oi_lookup(info, dev, fid, lid2,
318 (val == SCRUB_NEXT_OSTOBJ ||
319 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
322 ops = DTO_INDEX_INSERT;
323 else if (rc != -ESTALE)
328 inode = osd_iget(info, dev, lid);
331 /* Someone removed the inode. */
332 if (rc == -ENOENT || rc == -ESTALE)
337 /* The inode has been reused as EA inode, ignore it. */
338 if (unlikely(osd_is_ea_inode(inode)))
343 case SCRUB_NEXT_NOLMA:
344 sf->sf_flags |= SF_UPGRADE;
345 if (!(sf->sf_param & SP_DRYRUN)) {
346 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
351 if (!(sf->sf_flags & SF_INCONSISTENT))
352 dev->od_igif_inoi = 0;
354 case SCRUB_NEXT_OSTOBJ:
355 sf->sf_flags |= SF_INCONSISTENT;
356 case SCRUB_NEXT_OSTOBJ_OLD:
361 } else if (osd_id_eq(lid, lid2)) {
363 sf->sf_items_updated++;
367 if (!scrub->os_partial_scan) {
368 spin_lock(&scrub->os_lock);
369 scrub->os_full_speed = 1;
370 spin_unlock(&scrub->os_lock);
372 sf->sf_flags |= SF_INCONSISTENT;
374 /* XXX: If the device is restored from file-level backup, then
375 * some IGIFs may have been already in OI files, and some
376 * may be not yet. Means upgrading from 1.8 may be partly
377 * processed, but some clients may hold some immobilized
378 * IGIFs, and use them to access related objects. Under
379 * such case, OSD does not know whether an given IGIF has
380 * been processed or to be processed, and it also cannot
381 * generate local ino#/gen# directly from the immobilized
382 * IGIF because of the backup/restore. Then force OSD to
383 * lookup the given IGIF in OI files, and if no entry,
384 * then ask the client to retry after upgrading completed.
385 * No better choice. */
386 dev->od_igif_inoi = 1;
389 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops, false,
390 (val == SCRUB_NEXT_OSTOBJ ||
391 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0,
394 if (scrub->os_in_prior)
395 sf->sf_items_updated_prior++;
397 sf->sf_items_updated++;
399 if (ops == DTO_INDEX_INSERT && val == 0 && !exist) {
400 int idx = osd_oi_fid2idx(dev, fid);
402 sf->sf_flags |= SF_RECREATED;
403 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
404 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
412 sf->sf_items_failed++;
413 if (sf->sf_pos_first_inconsistent == 0 ||
414 sf->sf_pos_first_inconsistent > lid->oii_ino)
415 sf->sf_pos_first_inconsistent = lid->oii_ino;
420 /* There may be conflict unlink during the OI scrub,
421 * if happend, then remove the new added OI mapping. */
422 if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
423 unlikely(ldiskfs_test_inode_state(inode,
424 LDISKFS_STATE_LUSTRE_DESTROY)))
425 osd_scrub_refresh_mapping(info, dev, fid, lid,
426 DTO_INDEX_DELETE, false,
427 (val == SCRUB_NEXT_OSTOBJ ||
428 val == SCRUB_NEXT_OSTOBJ_OLD) ?
429 OI_KNOWN_ON_OST : 0, NULL);
430 up_write(&scrub->os_rwsem);
436 spin_lock(&scrub->os_lock);
437 if (likely(!list_empty(&oii->oii_list)))
438 list_del(&oii->oii_list);
439 spin_unlock(&scrub->os_lock);
444 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
447 /* iteration engine */
449 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
450 struct osd_device *dev,
451 struct osd_iit_param *param,
452 struct osd_idmap_cache **oic,
455 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
456 struct osd_device *dev,
457 struct osd_iit_param *param,
458 struct osd_idmap_cache *oic,
459 bool *noslot, int rc);
461 static int osd_iit_next(struct osd_iit_param *param, __u64 *pos)
466 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
467 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
468 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
469 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
470 return SCRUB_NEXT_BREAK;
473 offset = param->offset++;
474 if (unlikely(*pos == param->gbase + offset && *pos != param->start)) {
475 /* We should NOT find the same object more than once. */
476 CERROR("%s: scan the same object multiple times at the pos: "
477 "group = %u, base = %u, offset = %u, start = %u\n",
478 osd_sb2name(param->sb), (__u32)param->bg, param->gbase,
479 offset, param->start);
483 *pos = param->gbase + offset;
488 * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
489 * \retval 0: FID-on-MDT
491 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
492 struct osd_device *dev,
495 /* XXX: The initial OI scrub will scan the top level /O to generate
496 * a small local FLDB according to the <seq>. If the given FID
497 * is in the local FLDB, then it is FID-on-OST; otherwise it's
498 * quite possible for FID-on-MDT. */
500 return SCRUB_NEXT_OSTOBJ_OLD;
505 static int osd_scrub_get_fid(struct osd_thread_info *info,
506 struct osd_device *dev, struct inode *inode,
507 struct lu_fid *fid, bool scrub)
509 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
510 bool has_lma = false;
513 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
514 &info->oti_ost_attrs);
517 if (lma->lma_compat & LMAC_NOT_IN_OI ||
518 lma->lma_incompat & LMAI_AGENT)
519 return SCRUB_NEXT_CONTINUE;
521 *fid = lma->lma_self_fid;
525 if (lma->lma_compat & LMAC_FID_ON_OST)
526 return SCRUB_NEXT_OSTOBJ;
528 if (fid_is_idif(fid))
529 return SCRUB_NEXT_OSTOBJ_OLD;
531 /* For local object. */
532 if (fid_is_internal(fid))
535 /* For external visible MDT-object with non-normal FID. */
536 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
539 /* For the object with normal FID, it may be MDT-object,
540 * or may be 2.4 OST-object, need further distinguish.
541 * Fall through to next section. */
544 if (rc == -ENODATA || rc == 0) {
545 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
548 /* It is 2.3 or older OST-object. */
549 rc = SCRUB_NEXT_OSTOBJ_OLD;
555 /* It is FID-on-OST, but we do not know how
556 * to generate its FID, ignore it directly. */
557 rc = SCRUB_NEXT_CONTINUE;
559 /* It is 2.4 or newer OST-object. */
560 rc = SCRUB_NEXT_OSTOBJ_OLD;
568 if (dev->od_scrub.os_scrub.os_convert_igif) {
569 lu_igif_build(fid, inode->i_ino,
570 inode->i_generation);
572 rc = SCRUB_NEXT_NOLMA;
576 /* It may be FID-on-OST, or may be FID for
577 * non-MDT0, anyway, we do not know how to
578 * generate its FID, ignore it directly. */
579 rc = SCRUB_NEXT_CONTINUE;
584 /* For OI scrub case only: the object has LMA but has no ff
585 * (or ff crashed). It may be MDT-object, may be OST-object
586 * with crashed ff. The last check is local FLDB. */
587 rc = osd_scrub_check_local_fldb(info, dev, fid);
593 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
594 struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
595 struct super_block *sb, bool scrub)
601 /* Not handle the backend root object and agent parent object.
602 * They are neither visible to namespace nor have OI mappings. */
603 if (unlikely(pos == osd_sb(dev)->s_root->d_inode->i_ino ||
604 is_remote_parent_ino(dev, pos)))
605 RETURN(SCRUB_NEXT_CONTINUE);
607 /* Skip project quota inode since it is greater than s_first_ino. */
608 #ifdef HAVE_PROJECT_QUOTA
609 if (ldiskfs_has_feature_project(sb) &&
610 pos == le32_to_cpu(LDISKFS_SB(sb)->s_es->s_prj_quota_inum))
611 RETURN(SCRUB_NEXT_CONTINUE);
614 osd_id_gen(lid, pos, OSD_OII_NOGEN);
615 inode = osd_iget(info, dev, lid);
618 /* The inode may be removed after bitmap searching, or the
619 * file is new created without inode initialized yet. */
620 if (rc == -ENOENT || rc == -ESTALE)
621 RETURN(SCRUB_NEXT_CONTINUE);
623 CDEBUG(D_LFSCK, "%s: fail to read inode, ino# = %u: "
624 "rc = %d\n", osd_dev2name(dev), pos, rc);
628 if (dev->od_is_ost && S_ISREG(inode->i_mode) && inode->i_nlink > 1)
629 dev->od_scrub.os_scrub.os_has_ml_file = 1;
631 /* It is an EA inode, no OI mapping for it, skip it. */
632 if (osd_is_ea_inode(inode))
633 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
636 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
637 /* Only skip it for the first OI scrub accessing. */
638 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
639 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
642 rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
651 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
652 struct osd_iit_param *param,
653 struct osd_idmap_cache **oic, const bool noslot)
655 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
657 struct osd_inode_id *lid;
660 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0)
661 wait_var_event_timeout(
663 !list_empty(&scrub->os_inconsistent_items) ||
664 kthread_should_stop(),
665 cfs_time_seconds(cfs_fail_val));
667 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
668 spin_lock(&scrub->os_lock);
669 scrub->os_running = 0;
670 spin_unlock(&scrub->os_lock);
671 return SCRUB_NEXT_CRASH;
674 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
675 return SCRUB_NEXT_FATAL;
677 if (kthread_should_stop())
678 return SCRUB_NEXT_EXIT;
680 if (!list_empty(&scrub->os_inconsistent_items)) {
681 spin_lock(&scrub->os_lock);
682 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
683 struct osd_inconsistent_item *oii;
685 oii = list_entry(scrub->os_inconsistent_items.next,
686 struct osd_inconsistent_item, oii_list);
688 *oic = &oii->oii_cache;
689 scrub->os_in_prior = 1;
690 spin_unlock(&scrub->os_lock);
694 spin_unlock(&scrub->os_lock);
698 return SCRUB_NEXT_WAIT;
700 rc = osd_iit_next(param, &scrub->os_pos_current);
704 *oic = &dev->od_scrub.os_oic;
705 fid = &(*oic)->oic_fid;
706 lid = &(*oic)->oic_lid;
707 rc = osd_iit_iget(info, dev, fid, lid,
708 scrub->os_pos_current, param->sb, true);
712 static int osd_preload_next(struct osd_thread_info *info,
713 struct osd_device *dev, struct osd_iit_param *param,
714 struct osd_idmap_cache **oic, const bool noslot)
716 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
717 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
720 if (scrub->os_running &&
721 ooc->ooc_pos_preload >= scrub->os_pos_current)
722 return SCRUB_NEXT_EXIT;
724 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
728 rc = osd_iit_iget(info, dev,
729 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
730 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
731 ooc->ooc_pos_preload, param->sb, false);
736 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
738 spin_lock(&scrub->os_lock);
739 if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
740 !list_empty(&scrub->os_inconsistent_items) ||
741 it->ooi_waiting || kthread_should_stop())
742 scrub->os_waiting = 0;
744 scrub->os_waiting = 1;
745 spin_unlock(&scrub->os_lock);
747 return !scrub->os_waiting;
750 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
751 struct osd_iit_param *param,
752 struct osd_idmap_cache *oic, bool *noslot, int rc)
754 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
755 struct scrub_file *sf = &scrub->os_file;
756 struct osd_otable_it *it = dev->od_otable_it;
757 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
760 case SCRUB_NEXT_NOSCRUB:
761 down_write(&scrub->os_rwsem);
762 scrub->os_new_checked++;
763 sf->sf_items_noscrub++;
764 up_write(&scrub->os_rwsem);
765 case SCRUB_NEXT_CONTINUE:
766 case SCRUB_NEXT_WAIT:
770 rc = osd_scrub_check_update(info, dev, oic, rc);
772 spin_lock(&scrub->os_lock);
773 scrub->os_in_prior = 0;
774 spin_unlock(&scrub->os_lock);
778 rc = scrub_checkpoint(info->oti_env, scrub);
780 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
781 "rc = %d\n", osd_scrub2name(scrub),
782 scrub->os_pos_current, rc);
783 /* Continue, as long as the scrub itself can go ahead. */
786 if (scrub->os_in_prior) {
787 spin_lock(&scrub->os_lock);
788 scrub->os_in_prior = 0;
789 spin_unlock(&scrub->os_lock);
794 if (it != NULL && it->ooi_waiting && ooc != NULL &&
795 ooc->ooc_pos_preload < scrub->os_pos_current) {
796 spin_lock(&scrub->os_lock);
799 spin_unlock(&scrub->os_lock);
802 if (rc == SCRUB_NEXT_CONTINUE)
805 if (scrub->os_full_speed || !ooc || osd_scrub_has_window(scrub, ooc)) {
811 wait_var_event(scrub, osd_scrub_wakeup(scrub, it));
813 if (!ooc || osd_scrub_has_window(scrub, ooc))
820 static int osd_preload_exec(struct osd_thread_info *info,
821 struct osd_device *dev, struct osd_iit_param *param,
822 struct osd_idmap_cache *oic, bool *noslot, int rc)
824 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
827 ooc->ooc_cached_items++;
828 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
829 ~OSD_OTABLE_IT_CACHE_MASK;
831 return rc > 0 ? 0 : rc;
834 #define SCRUB_IT_ALL 1
835 #define SCRUB_IT_CRASH 2
837 static void osd_scrub_join(const struct lu_env *env, struct osd_device *dev,
838 __u32 flags, bool inconsistent)
840 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
841 struct scrub_file *sf = &scrub->os_file;
845 LASSERT(!(flags & SS_AUTO_PARTIAL));
847 down_write(&scrub->os_rwsem);
848 spin_lock(&scrub->os_lock);
849 scrub->os_in_join = 1;
850 if (flags & SS_SET_FAILOUT)
851 sf->sf_param |= SP_FAILOUT;
852 else if (flags & SS_CLEAR_FAILOUT)
853 sf->sf_param &= ~SP_FAILOUT;
855 if (flags & SS_SET_DRYRUN)
856 sf->sf_param |= SP_DRYRUN;
857 else if (flags & SS_CLEAR_DRYRUN)
858 sf->sf_param &= ~SP_DRYRUN;
860 if (flags & SS_RESET) {
861 scrub_file_reset(scrub, dev->od_uuid,
862 inconsistent ? SF_INCONSISTENT : 0);
863 sf->sf_status = SS_SCANNING;
866 if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
867 scrub->os_full_speed = 1;
869 scrub->os_full_speed = 0;
871 if (flags & SS_AUTO_FULL) {
872 sf->sf_flags |= SF_AUTO;
873 scrub->os_full_speed = 1;
875 spin_unlock(&scrub->os_lock);
877 scrub->os_new_checked = 0;
878 if (sf->sf_pos_last_checkpoint != 0)
879 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
881 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
883 scrub->os_pos_current = sf->sf_pos_latest_start;
884 sf->sf_time_latest_start = ktime_get_real_seconds();
885 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
886 sf->sf_pos_last_checkpoint = sf->sf_pos_latest_start - 1;
887 rc = scrub_file_store(env, scrub);
889 spin_lock(&scrub->os_lock);
890 scrub->os_waiting = 0;
891 scrub->os_paused = 0;
892 scrub->os_partial_scan = 0;
893 scrub->os_in_join = 0;
894 scrub->os_full_scrub = 0;
895 spin_unlock(&scrub->os_lock);
897 up_write(&scrub->os_rwsem);
899 CDEBUG(D_LFSCK, "%s: joined in the OI scrub with flag %u: rc = %d\n",
900 osd_scrub2name(scrub), flags, rc);
905 static int osd_inode_iteration(struct osd_thread_info *info,
906 struct osd_device *dev, __u32 max, bool preload)
908 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
909 struct scrub_file *sf = &scrub->os_file;
910 osd_iit_next_policy next;
911 osd_iit_exec_policy exec;
914 struct osd_iit_param *param;
923 param = &dev->od_scrub.os_iit_param;
924 memset(param, 0, sizeof(*param));
925 param->sb = osd_sb(dev);
927 while (scrub->os_partial_scan && !scrub->os_in_join) {
928 struct osd_idmap_cache *oic = NULL;
930 rc = osd_scrub_next(info, dev, param, &oic, noslot);
932 case SCRUB_NEXT_EXIT:
934 case SCRUB_NEXT_CRASH:
935 RETURN(SCRUB_IT_CRASH);
936 case SCRUB_NEXT_FATAL:
938 case SCRUB_NEXT_WAIT: {
939 struct kstatfs *ksfs = &info->oti_ksfs;
942 if (dev->od_full_scrub_ratio == OFSR_NEVER ||
943 unlikely(sf->sf_items_updated_prior == 0))
946 if (dev->od_full_scrub_ratio == OFSR_DIRECTLY ||
947 scrub->os_full_scrub) {
948 osd_scrub_join(info->oti_env, dev,
949 SS_AUTO_FULL | SS_RESET, true);
953 rc = param->sb->s_op->statfs(param->sb->s_root, ksfs);
955 __u64 used = ksfs->f_files - ksfs->f_ffree;
957 used = div64_u64(used, sf->sf_items_updated_prior);
958 /* If we hit too much inconsistent OI
959 * mappings during the partial scan,
960 * then scan the device completely. */
961 if (used < dev->od_full_scrub_ratio) {
962 osd_scrub_join(info->oti_env, dev,
963 SS_AUTO_FULL | SS_RESET, true);
969 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) &&
973 saved_flags = sf->sf_flags;
974 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
975 SF_UPGRADE | SF_AUTO);
976 sf->sf_status = SS_COMPLETED;
979 kthread_should_stop() ||
980 !scrub->os_partial_scan ||
982 !list_empty(&scrub->os_inconsistent_items));
983 sf->sf_flags = saved_flags;
984 sf->sf_status = SS_SCANNING;
986 if (kthread_should_stop())
989 if (!scrub->os_partial_scan || scrub->os_in_join)
995 LASSERTF(rc == 0, "rc = %d\n", rc);
997 osd_scrub_exec(info, dev, param, oic, &noslot, rc);
1004 wait_var_event(scrub,
1005 kthread_should_stop() ||
1006 !scrub->os_in_join);
1008 if (kthread_should_stop())
1014 next = osd_scrub_next;
1015 exec = osd_scrub_exec;
1016 pos = &scrub->os_pos_current;
1017 count = &scrub->os_new_checked;
1018 param->start = *pos;
1019 param->bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param->sb);
1021 (*pos - 1) % LDISKFS_INODES_PER_GROUP(param->sb);
1023 1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
1025 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1027 next = osd_preload_next;
1028 exec = osd_preload_exec;
1029 pos = &ooc->ooc_pos_preload;
1030 count = &ooc->ooc_cached_items;
1031 param = &dev->od_otable_it->ooi_iit_param;
1035 limit = le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count);
1036 while (*pos <= limit && *count < max) {
1037 struct ldiskfs_group_desc *desc;
1038 bool next_group = false;
1040 desc = ldiskfs_get_group_desc(param->sb, param->bg, NULL);
1044 if (desc->bg_flags & cpu_to_le16(LDISKFS_BG_INODE_UNINIT)) {
1049 param->bitmap = ldiskfs_read_inode_bitmap(param->sb, param->bg);
1050 if (!param->bitmap) {
1051 CERROR("%s: fail to read bitmap for %u, "
1052 "scrub will stop, urgent mode\n",
1053 osd_scrub2name(scrub), (__u32)param->bg);
1058 struct osd_idmap_cache *oic = NULL;
1061 ldiskfs_itable_unused_count(param->sb, desc) >=
1062 LDISKFS_INODES_PER_GROUP(param->sb)) {
1067 rc = next(info, dev, param, &oic, noslot);
1069 case SCRUB_NEXT_BREAK:
1072 case SCRUB_NEXT_EXIT:
1073 brelse(param->bitmap);
1075 case SCRUB_NEXT_CRASH:
1076 brelse(param->bitmap);
1077 RETURN(SCRUB_IT_CRASH);
1078 case SCRUB_NEXT_FATAL:
1079 brelse(param->bitmap);
1083 rc = exec(info, dev, param, oic, &noslot, rc);
1084 } while (!rc && *pos <= limit && *count < max);
1087 if (param->bitmap) {
1088 brelse(param->bitmap);
1089 param->bitmap = NULL;
1099 param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
1100 *pos = param->gbase;
1101 param->start = *pos;
1106 RETURN(SCRUB_IT_ALL);
1112 static int osd_otable_it_preload(const struct lu_env *env,
1113 struct osd_otable_it *it)
1115 struct osd_device *dev = it->ooi_dev;
1116 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1117 struct osd_otable_cache *ooc = &it->ooi_cache;
1121 rc = osd_inode_iteration(osd_oti_get(env), dev,
1122 OSD_OTABLE_IT_CACHE_SIZE, true);
1123 if (rc == SCRUB_IT_ALL)
1124 it->ooi_all_cached = 1;
1126 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1127 spin_lock(&scrub->os_lock);
1128 scrub->os_waiting = 0;
1130 spin_unlock(&scrub->os_lock);
1133 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1136 static int osd_scan_ml_file_main(const struct lu_env *env,
1137 struct osd_device *dev);
1139 static int osd_scrub_main(void *args)
1142 struct osd_device *dev = (struct osd_device *)args;
1143 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1147 rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
1149 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
1150 osd_scrub2name(scrub), rc);
1154 rc = scrub_thread_prep(&env, scrub, dev->od_uuid,
1155 LDISKFS_FIRST_INO(osd_sb(dev)) + 1);
1157 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
1158 osd_scrub2name(scrub), rc);
1162 if (!scrub->os_full_speed && !scrub->os_partial_scan) {
1163 struct osd_otable_it *it = dev->od_otable_it;
1164 struct osd_otable_cache *ooc = &it->ooi_cache;
1166 wait_var_event(scrub,
1167 it->ooi_user_ready || kthread_should_stop());
1168 if (kthread_should_stop())
1171 scrub->os_pos_current = ooc->ooc_pos_preload;
1174 CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu\n",
1175 osd_scrub2name(scrub), scrub->os_start_flags,
1176 scrub->os_pos_current);
1178 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1179 if (unlikely(rc == SCRUB_IT_CRASH)) {
1180 spin_lock(&scrub->os_lock);
1181 scrub->os_running = 0;
1182 spin_unlock(&scrub->os_lock);
1183 GOTO(out, rc = -EINVAL);
1186 if (scrub->os_has_ml_file) {
1187 ret = osd_scan_ml_file_main(&env, dev);
1196 dev->od_igif_inoi = 1;
1197 dev->od_check_ff = 0;
1199 rc = scrub_thread_post(&env, &dev->od_scrub.os_scrub, rc);
1200 CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d\n",
1201 osd_scrub2name(scrub), scrub->os_pos_current, rc);
1204 while (!list_empty(&scrub->os_inconsistent_items)) {
1205 struct osd_inconsistent_item *oii;
1207 oii = list_entry(scrub->os_inconsistent_items.next,
1208 struct osd_inconsistent_item, oii_list);
1209 list_del_init(&oii->oii_list);
1216 spin_lock(&scrub->os_lock);
1217 scrub->os_running = 0;
1218 spin_unlock(&scrub->os_lock);
1219 if (xchg(&scrub->os_task, NULL) == NULL)
1220 /* scrub_stop() is waiting, we need to synchronize */
1221 wait_var_event(scrub, kthread_should_stop());
1226 /* initial OI scrub */
1228 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1229 struct dentry *, filldir_t filldir);
1231 #ifdef HAVE_FILLDIR_USE_CTX
1232 static int osd_ios_varfid_fill(struct dir_context *buf, const char *name,
1233 int namelen, loff_t offset, __u64 ino,
1235 static int osd_ios_lf_fill(struct dir_context *buf, const char *name,
1236 int namelen, loff_t offset, __u64 ino,
1238 static int osd_ios_dl_fill(struct dir_context *buf, const char *name,
1239 int namelen, loff_t offset, __u64 ino,
1241 static int osd_ios_uld_fill(struct dir_context *buf, const char *name,
1242 int namelen, loff_t offset, __u64 ino,
1245 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1246 loff_t offset, __u64 ino, unsigned d_type);
1247 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1248 loff_t offset, __u64 ino, unsigned d_type);
1249 static int osd_ios_dl_fill(void *buf, const char *name, int namelen,
1250 loff_t offset, __u64 ino, unsigned d_type);
1251 static int osd_ios_uld_fill(void *buf, const char *name, int namelen,
1252 loff_t offset, __u64 ino, unsigned d_type);
1256 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1257 struct dentry *dentry, filldir_t filldir);
1259 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1260 struct dentry *dentry, filldir_t filldir);
1263 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1264 struct dentry *dentry, filldir_t filldir);
1268 struct lu_fid olm_fid;
1271 scandir_t olm_scandir;
1272 filldir_t olm_filldir;
1275 /* Add the new introduced local files in the list in the future. */
1276 static const struct osd_lf_map osd_lf_maps[] = {
1279 .olm_name = CATLIST,
1281 .f_seq = FID_SEQ_LOCAL_FILE,
1282 .f_oid = LLOG_CATALOGS_OID,
1284 .olm_flags = OLF_SHOW_NAME,
1285 .olm_namelen = sizeof(CATLIST) - 1,
1290 .olm_name = MOUNT_CONFIGS_DIR,
1292 .f_seq = FID_SEQ_LOCAL_FILE,
1293 .f_oid = MGS_CONFIGS_OID,
1295 .olm_flags = OLF_SCAN_SUBITEMS,
1296 .olm_namelen = sizeof(MOUNT_CONFIGS_DIR) - 1,
1297 .olm_scandir = osd_ios_general_scan,
1298 .olm_filldir = osd_ios_varfid_fill,
1301 /* NIDTBL_VERSIONS */
1303 .olm_name = MGS_NIDTBL_DIR,
1304 .olm_flags = OLF_SCAN_SUBITEMS,
1305 .olm_namelen = sizeof(MGS_NIDTBL_DIR) - 1,
1306 .olm_scandir = osd_ios_general_scan,
1307 .olm_filldir = osd_ios_varfid_fill,
1312 .olm_name = MDT_ORPHAN_DIR,
1313 .olm_namelen = sizeof(MDT_ORPHAN_DIR) - 1,
1320 .f_seq = FID_SEQ_ROOT,
1321 .f_oid = FID_OID_ROOT,
1323 .olm_flags = OLF_SCAN_SUBITEMS | OLF_HIDE_FID,
1324 .olm_namelen = sizeof("ROOT") - 1,
1325 .olm_scandir = osd_ios_ROOT_scan,
1328 /* changelog_catalog */
1330 .olm_name = CHANGELOG_CATALOG,
1331 .olm_namelen = sizeof(CHANGELOG_CATALOG) - 1,
1334 /* changelog_users */
1336 .olm_name = CHANGELOG_USERS,
1337 .olm_namelen = sizeof(CHANGELOG_USERS) - 1,
1344 .f_seq = FID_SEQ_LOCAL_FILE,
1345 .f_oid = FLD_INDEX_OID,
1347 .olm_flags = OLF_SHOW_NAME,
1348 .olm_namelen = sizeof("fld") - 1,
1353 .olm_name = LAST_RCVD,
1355 .f_seq = FID_SEQ_LOCAL_FILE,
1356 .f_oid = LAST_RECV_OID,
1358 .olm_flags = OLF_SHOW_NAME,
1359 .olm_namelen = sizeof(LAST_RCVD) - 1,
1364 .olm_name = REPLY_DATA,
1366 .f_seq = FID_SEQ_LOCAL_FILE,
1367 .f_oid = REPLY_DATA_OID,
1369 .olm_flags = OLF_SHOW_NAME,
1370 .olm_namelen = sizeof(REPLY_DATA) - 1,
1375 .olm_name = LOV_OBJID,
1377 .f_seq = FID_SEQ_LOCAL_FILE,
1378 .f_oid = MDD_LOV_OBJ_OID,
1380 .olm_flags = OLF_SHOW_NAME,
1381 .olm_namelen = sizeof(LOV_OBJID) - 1,
1386 .olm_name = LOV_OBJSEQ,
1388 .f_seq = FID_SEQ_LOCAL_FILE,
1389 .f_oid = MDD_LOV_OBJ_OSEQ,
1391 .olm_flags = OLF_SHOW_NAME,
1392 .olm_namelen = sizeof(LOV_OBJSEQ) - 1,
1397 .olm_name = QMT_DIR,
1398 .olm_flags = OLF_SCAN_SUBITEMS,
1399 .olm_namelen = sizeof(QMT_DIR) - 1,
1400 .olm_scandir = osd_ios_general_scan,
1401 .olm_filldir = osd_ios_varfid_fill,
1406 .olm_name = QSD_DIR,
1407 .olm_flags = OLF_SCAN_SUBITEMS,
1408 .olm_namelen = sizeof(QSD_DIR) - 1,
1409 .olm_scandir = osd_ios_general_scan,
1410 .olm_filldir = osd_ios_varfid_fill,
1415 .olm_name = "seq_ctl",
1417 .f_seq = FID_SEQ_LOCAL_FILE,
1418 .f_oid = FID_SEQ_CTL_OID,
1420 .olm_flags = OLF_SHOW_NAME,
1421 .olm_namelen = sizeof("seq_ctl") - 1,
1426 .olm_name = "seq_srv",
1428 .f_seq = FID_SEQ_LOCAL_FILE,
1429 .f_oid = FID_SEQ_SRV_OID,
1431 .olm_flags = OLF_SHOW_NAME,
1432 .olm_namelen = sizeof("seq_srv") - 1,
1437 .olm_name = HEALTH_CHECK,
1439 .f_seq = FID_SEQ_LOCAL_FILE,
1440 .f_oid = OFD_HEALTH_CHECK_OID,
1442 .olm_flags = OLF_SHOW_NAME,
1443 .olm_namelen = sizeof(HEALTH_CHECK) - 1,
1448 .olm_name = LFSCK_DIR,
1449 .olm_flags = OLF_SCAN_SUBITEMS,
1450 .olm_namelen = sizeof(LFSCK_DIR) - 1,
1451 .olm_scandir = osd_ios_general_scan,
1452 .olm_filldir = osd_ios_varfid_fill,
1455 /* lfsck_bookmark */
1457 .olm_name = LFSCK_BOOKMARK,
1458 .olm_namelen = sizeof(LFSCK_BOOKMARK) - 1,
1463 .olm_name = LFSCK_LAYOUT,
1464 .olm_namelen = sizeof(LFSCK_LAYOUT) - 1,
1467 /* lfsck_namespace */
1469 .olm_name = LFSCK_NAMESPACE,
1470 .olm_namelen = sizeof(LFSCK_NAMESPACE) - 1,
1473 /* OBJECTS, upgrade from old device */
1475 .olm_name = OBJECTS,
1476 .olm_flags = OLF_SCAN_SUBITEMS,
1477 .olm_namelen = sizeof(OBJECTS) - 1,
1478 .olm_scandir = osd_ios_OBJECTS_scan,
1481 /* lquota_v2.user, upgrade from old device */
1483 .olm_name = "lquota_v2.user",
1484 .olm_namelen = sizeof("lquota_v2.user") - 1,
1487 /* lquota_v2.group, upgrade from old device */
1489 .olm_name = "lquota_v2.group",
1490 .olm_namelen = sizeof("lquota_v2.group") - 1,
1493 /* LAST_GROUP, upgrade from old device */
1495 .olm_name = "LAST_GROUP",
1497 .f_seq = FID_SEQ_LOCAL_FILE,
1498 .f_oid = OFD_LAST_GROUP_OID,
1500 .olm_flags = OLF_SHOW_NAME,
1501 .olm_namelen = sizeof("LAST_GROUP") - 1,
1504 /* committed batchid for cross-MDT operation */
1506 .olm_name = "BATCHID",
1508 .f_seq = FID_SEQ_LOCAL_FILE,
1509 .f_oid = BATCHID_COMMITTED_OID,
1511 .olm_flags = OLF_SHOW_NAME,
1512 .olm_namelen = sizeof("BATCHID") - 1,
1515 /* OSP update logs update_log{_dir} use f_seq = FID_SEQ_UPDATE_LOG{_DIR}
1516 * and f_oid = index for their log files. See lu_update_log{_dir}_fid()
1517 * for more details. */
1521 .olm_name = "update_log",
1523 .f_seq = FID_SEQ_UPDATE_LOG,
1525 .olm_flags = OLF_SHOW_NAME | OLF_IDX_IN_FID,
1526 .olm_namelen = sizeof("update_log") - 1,
1529 /* update_log_dir */
1531 .olm_name = "update_log_dir",
1533 .f_seq = FID_SEQ_UPDATE_LOG_DIR,
1535 .olm_flags = OLF_SHOW_NAME | OLF_SCAN_SUBITEMS |
1537 .olm_namelen = sizeof("update_log_dir") - 1,
1538 .olm_scandir = osd_ios_general_scan,
1539 .olm_filldir = osd_ios_uld_fill,
1544 .olm_name = "lost+found",
1546 .f_seq = FID_SEQ_LOCAL_FILE,
1547 .f_oid = OSD_LPF_OID,
1549 .olm_flags = OLF_SCAN_SUBITEMS,
1550 .olm_namelen = sizeof("lost+found") - 1,
1551 .olm_scandir = osd_ios_general_scan,
1552 .olm_filldir = osd_ios_lf_fill,
1557 .olm_name = HSM_ACTIONS,
1562 .olm_name = LUSTRE_NODEMAP_NAME,
1567 .olm_name = INDEX_BACKUP_DIR,
1569 .f_seq = FID_SEQ_LOCAL_FILE,
1570 .f_oid = INDEX_BACKUP_OID,
1572 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
1573 .olm_namelen = sizeof(INDEX_BACKUP_DIR) - 1,
1574 .olm_scandir = osd_ios_general_scan,
1575 .olm_filldir = osd_ios_varfid_fill,
1583 /* Add the new introduced files under .lustre/ in the list in the future. */
1584 static const struct osd_lf_map osd_dl_maps[] = {
1589 .f_seq = FID_SEQ_DOT_LUSTRE,
1590 .f_oid = FID_OID_DOT_LUSTRE_OBF,
1592 .olm_namelen = sizeof("fid") - 1,
1595 /* .lustre/lost+found */
1597 .olm_name = "lost+found",
1599 .f_seq = FID_SEQ_DOT_LUSTRE,
1600 .f_oid = FID_OID_DOT_LUSTRE_LPF,
1602 .olm_namelen = sizeof("lost+found") - 1,
1610 struct osd_ios_item {
1611 struct list_head oii_list;
1612 struct dentry *oii_dentry;
1613 scandir_t oii_scandir;
1614 filldir_t oii_filldir;
1617 struct osd_ios_filldir_buf {
1618 /* please keep it as first member */
1619 struct dir_context ctx;
1620 struct osd_thread_info *oifb_info;
1621 struct osd_device *oifb_dev;
1622 struct dentry *oifb_dentry;
1627 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1628 scandir_t scandir, filldir_t filldir)
1630 struct osd_ios_item *item;
1633 OBD_ALLOC_PTR(item);
1637 INIT_LIST_HEAD(&item->oii_list);
1638 item->oii_dentry = dget(dentry);
1639 item->oii_scandir = scandir;
1640 item->oii_filldir = filldir;
1641 list_add_tail(&item->oii_list, &dev->od_ios_list);
1646 static bool osd_index_need_recreate(const struct lu_env *env,
1647 struct osd_device *dev, struct inode *inode)
1649 struct osd_directory *iam = &osd_oti_get(env)->oti_iam;
1650 struct iam_container *bag = &iam->od_container;
1654 rc = iam_container_init(bag, &iam->od_descr, inode);
1658 rc = iam_container_setup(bag);
1659 iam_container_fini(bag);
1666 static void osd_ios_index_register(const struct lu_env *env,
1667 struct osd_device *osd,
1668 const struct lu_fid *fid,
1669 struct inode *inode)
1671 struct osd_directory *iam = &osd_oti_get(env)->oti_iam;
1672 struct iam_container *bag = &iam->od_container;
1673 struct super_block *sb = osd_sb(osd);
1674 struct iam_descr *descr;
1680 /* Index must be a regular file. */
1681 if (!S_ISREG(inode->i_mode))
1684 /* Index's size must be block aligned. */
1685 if (inode->i_size < sb->s_blocksize ||
1686 (inode->i_size & (sb->s_blocksize - 1)) != 0)
1689 iam_container_init(bag, &iam->od_descr, inode);
1690 rc = iam_container_setup(bag);
1694 descr = bag->ic_descr;
1695 /* May be regular file with IAM_LFIX_ROOT_MAGIC matched
1696 * coincidentally, or corrupted index object, skip it. */
1697 if (descr->id_ptr_size != 4)
1700 keysize = descr->id_key_size;
1701 recsize = descr->id_rec_size;
1702 rc = osd_index_register(osd, fid, keysize, recsize);
1707 iam_container_fini(bag);
1709 CDEBUG(D_LFSCK, "%s: index object "DFID" (%u/%u) registered\n",
1710 osd_name(osd), PFID(fid), keysize, recsize);
1713 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
1714 struct lustre_index_restore_unit *liru,
1715 void *buf, int bufsize)
1717 struct osd_thread_info *info = osd_oti_get(env);
1718 struct osd_inode_id *id = &info->oti_id;
1719 struct lu_fid *tgt_fid = &liru->liru_cfid;
1720 struct inode *bak_inode = NULL;
1721 struct ldiskfs_dir_entry_2 *de = NULL;
1722 struct buffer_head *bh = NULL;
1723 struct dentry *dentry;
1725 struct lu_fid bak_fid;
1729 lustre_fid2lbx(name, tgt_fid, bufsize);
1730 dentry = osd_child_dentry_by_inode(env, dev->od_index_backup_inode,
1731 name, strlen(name));
1732 bh = osd_ldiskfs_find_entry(dev->od_index_backup_inode,
1733 &dentry->d_name, &de, NULL, NULL);
1735 GOTO(log, rc = PTR_ERR(bh));
1737 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
1739 bak_inode = osd_iget_fid(info, dev, id, &bak_fid);
1740 if (IS_ERR(bak_inode))
1741 GOTO(log, rc = PTR_ERR(bak_inode));
1744 /* The OI mapping for index may be invalid, since it will be
1745 * re-created, not update the OI mapping, just cache it in RAM. */
1746 osd_id_gen(id, liru->liru_clid, OSD_OII_NOGEN);
1747 osd_add_oi_cache(info, dev, id, tgt_fid);
1748 rc = lustre_index_restore(env, &dev->od_dt_dev, &liru->liru_pfid,
1749 tgt_fid, &bak_fid, liru->liru_name,
1750 &dev->od_index_backup_list, &dev->od_lock,
1755 CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
1756 osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
1760 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1762 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1763 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1764 * reference the inode, or fixed if it is missing or references another inode.
1767 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1768 struct inode *parent, struct inode *inode,
1769 const struct lu_fid *fid, const char *name,
1770 int namelen, int flags)
1772 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1773 struct osd_inode_id *id = &info->oti_id;
1774 struct osd_inode_id *id2 = &info->oti_id2;
1775 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
1776 struct scrub_file *sf = &scrub->os_file;
1782 CDEBUG(D_INODE, "%s: child '%.*s' lacks inode: rc = -2\n",
1783 osd_name(dev), namelen, name);
1787 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
1788 &info->oti_ost_attrs);
1789 if (rc != 0 && rc != -ENODATA) {
1790 CDEBUG(D_LFSCK, "%s: fail to get lma for init OI scrub: "
1791 "rc = %d\n", osd_name(dev), rc);
1796 osd_id_gen(id, inode->i_ino, inode->i_generation);
1797 if (rc == -ENODATA) {
1798 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID) {
1799 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1802 if (flags & OLF_IDX_IN_FID) {
1803 LASSERT(dev->od_index >= 0);
1805 tfid.f_oid = dev->od_index;
1808 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1810 CDEBUG(D_LFSCK, "%s: fail to set LMA for init OI "
1811 "scrub: rc = %d\n", osd_name(dev), rc);
1816 if (lma->lma_compat & LMAC_NOT_IN_OI)
1819 tfid = lma->lma_self_fid;
1820 if (lma->lma_compat & LMAC_IDX_BACKUP &&
1821 osd_index_need_recreate(info->oti_env, dev, inode)) {
1822 struct lu_fid *pfid = &info->oti_fid3;
1824 if (is_root_inode(parent)) {
1825 lu_local_obj_fid(pfid, OSD_FS_ROOT_OID);
1827 rc = osd_scrub_get_fid(info, dev, parent, pfid,
1833 rc = lustre_liru_new(&dev->od_index_restore_list, pfid,
1834 &tfid, inode->i_ino, name, namelen);
1839 if (!(flags & OLF_NOT_BACKUP))
1840 osd_ios_index_register(info->oti_env, dev, &tfid,
1844 /* Since this called from iterate_dir() the inode lock will be taken */
1845 rc = osd_oi_lookup(info, dev, &tfid, id2, OI_LOCKED);
1850 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1851 DTO_INDEX_INSERT, true, 0, NULL);
1858 if (osd_id_eq_strict(id, id2))
1861 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1862 scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
1863 rc = scrub_file_store(info->oti_env, scrub);
1868 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1869 DTO_INDEX_UPDATE, true, 0, NULL);
1877 * It scans the /lost+found, and for the OST-object (with filter_fid
1878 * or filter_fid_18_23), move them back to its proper /O/<seq>/d<x>.
1880 #ifdef HAVE_FILLDIR_USE_CTX
1881 static int osd_ios_lf_fill(struct dir_context *buf,
1883 static int osd_ios_lf_fill(void *buf,
1885 const char *name, int namelen,
1886 loff_t offset, __u64 ino, unsigned d_type)
1888 struct osd_ios_filldir_buf *fill_buf =
1889 (struct osd_ios_filldir_buf *)buf;
1890 struct osd_thread_info *info = fill_buf->oifb_info;
1891 struct osd_device *dev = fill_buf->oifb_dev;
1892 struct lu_fid *fid = &info->oti_fid;
1893 struct osd_scrub *scrub = &dev->od_scrub;
1894 struct dentry *parent = fill_buf->oifb_dentry;
1895 struct dentry *child;
1896 struct inode *dir = parent->d_inode;
1897 struct inode *inode;
1901 fill_buf->oifb_items++;
1903 /* skip any '.' started names */
1907 scrub->os_lf_scanned++;
1908 child = osd_lookup_one_len(dev, name, parent, namelen);
1909 if (IS_ERR(child)) {
1910 rc = PTR_ERR(child);
1911 CDEBUG(D_LFSCK, "%s: cannot lookup child '%.*s': rc = %d\n",
1912 osd_name(dev), namelen, name, rc);
1914 } else if (!child->d_inode) {
1916 CDEBUG(D_INODE, "%s: child '%.*s' lacks inode\n",
1917 osd_name(dev), namelen, name);
1921 inode = child->d_inode;
1922 if (S_ISDIR(inode->i_mode)) {
1923 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1926 CDEBUG(D_LFSCK, "%s: cannot add child '%.*s': "
1927 "rc = %d\n", osd_name(dev), namelen, name, rc);
1931 if (!S_ISREG(inode->i_mode))
1934 rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1935 if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1936 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1938 CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1939 "/lost+found.\n", namelen, name, PFID(fid));
1940 scrub->os_lf_repaired++;
1942 CDEBUG(D_LFSCK, "%s: cannot rename for '%.*s' "
1944 osd_name(dev), namelen, name, PFID(fid), rc);
1948 /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1949 * visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1950 * can process them in furtuer. */
1956 scrub->os_lf_failed++;
1958 /* skip the failure to make the scanning to continue. */
1962 #ifdef HAVE_FILLDIR_USE_CTX
1963 static int osd_ios_varfid_fill(struct dir_context *buf,
1965 static int osd_ios_varfid_fill(void *buf,
1967 const char *name, int namelen,
1968 loff_t offset, __u64 ino, unsigned d_type)
1970 struct osd_ios_filldir_buf *fill_buf =
1971 (struct osd_ios_filldir_buf *)buf;
1972 struct osd_device *dev = fill_buf->oifb_dev;
1973 struct dentry *child;
1977 fill_buf->oifb_items++;
1979 /* skip any '.' started names */
1983 child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
1985 RETURN(PTR_ERR(child));
1987 rc = osd_ios_scan_one(fill_buf->oifb_info, dev,
1988 fill_buf->oifb_dentry->d_inode, child->d_inode,
1989 NULL, name, namelen, 0);
1990 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1991 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1992 osd_ios_varfid_fill);
1998 #ifdef HAVE_FILLDIR_USE_CTX
1999 static int osd_ios_dl_fill(struct dir_context *buf,
2001 static int osd_ios_dl_fill(void *buf,
2003 const char *name, int namelen,
2004 loff_t offset, __u64 ino, unsigned d_type)
2006 struct osd_ios_filldir_buf *fill_buf =
2007 (struct osd_ios_filldir_buf *)buf;
2008 struct osd_device *dev = fill_buf->oifb_dev;
2009 const struct osd_lf_map *map;
2010 struct dentry *child;
2014 fill_buf->oifb_items++;
2016 /* skip any '.' started names */
2020 for (map = osd_dl_maps; map->olm_name != NULL; map++) {
2021 if (map->olm_namelen != namelen)
2024 if (strncmp(map->olm_name, name, namelen) == 0)
2028 if (map->olm_name == NULL)
2031 child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2033 RETURN(PTR_ERR(child));
2035 rc = osd_ios_scan_one(fill_buf->oifb_info, dev,
2036 fill_buf->oifb_dentry->d_inode, child->d_inode,
2037 &map->olm_fid, name, namelen, map->olm_flags);
2043 #ifdef HAVE_FILLDIR_USE_CTX
2044 static int osd_ios_uld_fill(struct dir_context *buf,
2046 static int osd_ios_uld_fill(void *buf,
2048 const char *name, int namelen,
2049 loff_t offset, __u64 ino, unsigned d_type)
2051 struct osd_ios_filldir_buf *fill_buf =
2052 (struct osd_ios_filldir_buf *)buf;
2053 struct osd_device *dev = fill_buf->oifb_dev;
2054 struct dentry *child;
2059 fill_buf->oifb_items++;
2061 /* skip any non-DFID format name */
2065 child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2067 RETURN(PTR_ERR(child));
2069 /* skip the start '[' */
2070 sscanf(&name[1], SFID, RFID(&tfid));
2071 if (fid_is_sane(&tfid))
2072 rc = osd_ios_scan_one(fill_buf->oifb_info, fill_buf->oifb_dev,
2073 fill_buf->oifb_dentry->d_inode,
2074 child->d_inode, &tfid, name, namelen, 0);
2082 #ifdef HAVE_FILLDIR_USE_CTX
2083 static int osd_ios_root_fill(struct dir_context *buf,
2085 static int osd_ios_root_fill(void *buf,
2087 const char *name, int namelen,
2088 loff_t offset, __u64 ino, unsigned d_type)
2090 struct osd_ios_filldir_buf *fill_buf =
2091 (struct osd_ios_filldir_buf *)buf;
2092 struct osd_device *dev = fill_buf->oifb_dev;
2093 const struct osd_lf_map *map;
2094 struct dentry *child;
2098 fill_buf->oifb_items++;
2100 /* skip any '.' started names */
2104 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
2105 if (map->olm_namelen != namelen)
2108 if (strncmp(map->olm_name, name, namelen) == 0)
2112 if (map->olm_name == NULL)
2115 child = osd_lookup_one_len(dev, name, fill_buf->oifb_dentry, namelen);
2117 RETURN(PTR_ERR(child));
2118 else if (!child->d_inode)
2119 GOTO(out_put, rc = -ENOENT);
2121 if (!(map->olm_flags & OLF_NO_OI))
2122 rc = osd_ios_scan_one(fill_buf->oifb_info, dev,
2123 fill_buf->oifb_dentry->d_inode, child->d_inode,
2124 &map->olm_fid, name, namelen, map->olm_flags);
2125 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
2126 rc = osd_ios_new_item(dev, child, map->olm_scandir,
2135 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
2136 struct dentry *dentry, filldir_t filldir)
2138 struct osd_ios_filldir_buf buf = {
2139 .ctx.actor = filldir,
2142 .oifb_dentry = dentry
2150 path.dentry = dget(dentry);
2151 path.mnt = mntget(dev->od_mnt);
2153 filp = dentry_open(&path, O_RDONLY, current_cred());
2156 RETURN(PTR_ERR(filp));
2158 filp->f_mode |= FMODE_64BITHASH | FMODE_NONOTIFY;
2159 filp->f_flags |= O_NOATIME;
2164 rc = iterate_dir(filp, &buf.ctx);
2165 } while (rc >= 0 && buf.oifb_items > 0 &&
2166 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
2173 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
2174 struct dentry *dentry, filldir_t filldir)
2176 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2177 struct scrub_file *sf = &scrub->os_file;
2178 struct dentry *child;
2182 /* It is existing MDT0 device. We only allow the case of object without
2183 * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
2184 * can generate IGIF mode FID for the object and related OI mapping. If
2185 * it is on other MDTs, then becuase file-level backup/restore, related
2186 * OI mapping may be invalid already, we do not know which is the right
2187 * FID for the object. We only allow IGIF objects to reside on the MDT0.
2189 * XXX: For the case of object on non-MDT0 device with neither LMA nor
2190 * "fid" xattr, then something crashed. We cannot re-generate the
2191 * FID directly, instead, the OI scrub will scan the OI structure
2192 * and try to re-generate the LMA from the OI mapping. But if the
2193 * OI mapping crashed or lost also, then we have to give up under
2194 * double failure cases.
2196 spin_lock(&scrub->os_lock);
2197 scrub->os_convert_igif = 1;
2198 spin_unlock(&scrub->os_lock);
2199 child = osd_lookup_one_len_unlocked(dev, dot_lustre_name, dentry,
2200 strlen(dot_lustre_name));
2201 if (IS_ERR(child)) {
2202 if (PTR_ERR(child) != -ENOENT)
2203 RETURN(PTR_ERR(child));
2207 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
2208 * so the client will get IGIF for the ".lustre" object when
2211 * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
2212 * it does not know whether there are some old clients cached
2213 * the ".lustre" IGIF during the upgrading. Two choices:
2215 * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
2216 * It will allow the old connected clients to access the
2217 * ".lustre" with cached IGIF. But it will cause others
2218 * on the MDT failed to check "fid_is_dot_lustre()".
2220 * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
2221 * for ".lustre" in spite of whether there are some clients
2222 * cached the ".lustre" IGIF or not. It enables the check
2223 * "fid_is_dot_lustre()" on the MDT, although it will cause
2224 * that the old connected clients cannot access the ".lustre"
2225 * with the cached IGIF.
2227 * Usually, it is rare case for the old connected clients
2228 * to access the ".lustre" with cached IGIF. So we prefer
2229 * to the solution 2).
2231 inode_lock(dentry->d_inode);
2232 rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2233 child->d_inode, &LU_DOT_LUSTRE_FID,
2235 strlen(dot_lustre_name), 0);
2236 inode_unlock(dentry->d_inode);
2237 if (rc == -ENOENT) {
2239 /* It is 1.8 MDT device. */
2240 if (!(sf->sf_flags & SF_UPGRADE)) {
2241 scrub_file_reset(scrub, dev->od_uuid,
2243 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2244 rc = scrub_file_store(info->oti_env, scrub);
2248 } else if (rc == 0) {
2249 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
2258 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
2259 struct dentry *dentry, filldir_t filldir)
2261 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2262 struct scrub_file *sf = &scrub->os_file;
2263 struct dentry *child;
2267 if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
2268 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
2269 rc = scrub_file_store(info->oti_env, scrub);
2274 child = osd_lookup_one_len_unlocked(dev, ADMIN_USR, dentry,
2276 if (IS_ERR(child)) {
2277 rc = PTR_ERR(child);
2279 inode_lock(dentry->d_inode);
2280 rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2281 child->d_inode, NULL, ADMIN_USR,
2282 strlen(ADMIN_USR), 0);
2283 inode_unlock(dentry->d_inode);
2287 if (rc != 0 && rc != -ENOENT)
2290 child = osd_lookup_one_len_unlocked(dev, ADMIN_GRP, dentry,
2293 GOTO(out, rc = PTR_ERR(child));
2295 inode_lock(dentry->d_inode);
2296 rc = osd_ios_scan_one(info, dev, dentry->d_inode,
2297 child->d_inode, NULL, ADMIN_GRP,
2298 strlen(ADMIN_GRP), 0);
2299 inode_unlock(dentry->d_inode);
2302 RETURN(rc == -ENOENT ? 0 : rc);
2305 static void osd_initial_OI_scrub(struct osd_thread_info *info,
2306 struct osd_device *dev)
2308 struct osd_ios_item *item = NULL;
2309 scandir_t scandir = osd_ios_general_scan;
2310 filldir_t filldir = osd_ios_root_fill;
2311 struct dentry *dentry = osd_sb(dev)->s_root;
2312 const struct osd_lf_map *map = osd_lf_maps;
2315 /* Lookup IGIF in OI by force for initial OI scrub. */
2316 dev->od_igif_inoi = 1;
2319 /* Don't take inode_lock here since scandir() callbacks
2320 * can call VFS functions which may manully take the
2321 * inode lock itself like iterate_dir(). Since this
2322 * is the case it is best to leave the scandir()
2323 * callbacks to managing the inode lock.
2325 scandir(info, dev, dentry, filldir);
2327 dput(item->oii_dentry);
2331 if (list_empty(&dev->od_ios_list))
2334 item = list_entry(dev->od_ios_list.next,
2335 struct osd_ios_item, oii_list);
2336 list_del_init(&item->oii_list);
2338 LASSERT(item->oii_scandir != NULL);
2339 scandir = item->oii_scandir;
2340 filldir = item->oii_filldir;
2341 dentry = item->oii_dentry;
2344 /* There maybe the case that the object has been removed, but its OI
2345 * mapping is still in the OI file, such as the "CATALOGS" after MDT
2346 * file-level backup/restore. So here cleanup the stale OI mappings. */
2347 while (map->olm_name != NULL) {
2348 struct dentry *child;
2350 if (fid_is_zero(&map->olm_fid)) {
2355 child = osd_lookup_one_len_unlocked(dev, map->olm_name,
2356 osd_sb(dev)->s_root,
2358 if (PTR_ERR(child) == -ENOENT ||
2359 (!IS_ERR(child) && !child->d_inode))
2360 osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
2361 NULL, DTO_INDEX_DELETE,
2368 if (!list_empty(&dev->od_index_restore_list)) {
2371 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
2373 CERROR("%s: not enough RAM for rebuild index\n",
2376 while (!list_empty(&dev->od_index_restore_list)) {
2377 struct lustre_index_restore_unit *liru;
2379 liru = list_entry(dev->od_index_restore_list.next,
2380 struct lustre_index_restore_unit,
2382 list_del(&liru->liru_link);
2384 osd_index_restore(info->oti_env, dev, liru,
2385 buf, INDEX_BACKUP_BUFSIZE);
2386 OBD_FREE(liru, liru->liru_len);
2390 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
2396 char *osd_lf_fid2name(const struct lu_fid *fid)
2398 const struct osd_lf_map *map = osd_lf_maps;
2400 while (map->olm_name != NULL) {
2401 if (!lu_fid_eq(fid, &map->olm_fid)) {
2406 if (map->olm_flags & OLF_SHOW_NAME)
2407 return map->olm_name;
2415 /* OI scrub start/stop */
2417 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
2420 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2424 if (dev->od_dt_dev.dd_rdonly)
2427 /* od_otable_mutex: prevent curcurrent start/stop */
2428 mutex_lock(&dev->od_otable_mutex);
2429 rc = scrub_start(osd_scrub_main, scrub, dev, flags);
2430 if (rc == -EALREADY) {
2432 if ((scrub->os_file.sf_flags & SF_AUTO ||
2433 scrub->os_partial_scan) &&
2434 !(flags & SS_AUTO_PARTIAL))
2435 osd_scrub_join(env, dev, flags, false);
2437 mutex_unlock(&dev->od_otable_mutex);
2442 void osd_scrub_stop(struct osd_device *dev)
2444 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2446 /* od_otable_mutex: prevent curcurrent start/stop */
2447 mutex_lock(&dev->od_otable_mutex);
2448 spin_lock(&scrub->os_lock);
2449 scrub->os_paused = 1;
2450 spin_unlock(&scrub->os_lock);
2452 mutex_unlock(&dev->od_otable_mutex);
2455 /* OI scrub setup/cleanup */
2457 static const char osd_scrub_name[] = "OI_scrub";
2459 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
2462 struct osd_thread_info *info = osd_oti_get(env);
2463 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2464 struct lvfs_run_ctxt *ctxt = &dev->od_scrub.os_ctxt;
2465 time64_t interval = scrub->os_auto_scrub_interval;
2466 struct scrub_file *sf = &scrub->os_file;
2467 struct super_block *sb = osd_sb(dev);
2468 struct lvfs_run_ctxt saved;
2470 struct inode *inode;
2471 struct lu_fid *fid = &info->oti_fid;
2472 struct osd_inode_id *id = &info->oti_id;
2473 struct dt_object *obj;
2478 memset(&dev->od_scrub, 0, sizeof(struct osd_scrub));
2479 OBD_SET_CTXT_MAGIC(ctxt);
2480 ctxt->pwdmnt = dev->od_mnt;
2481 ctxt->pwd = dev->od_mnt->mnt_root;
2483 init_rwsem(&scrub->os_rwsem);
2484 spin_lock_init(&scrub->os_lock);
2485 INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2486 scrub->os_name = osd_name(dev);
2487 scrub->os_auto_scrub_interval = interval;
2489 push_ctxt(&saved, ctxt);
2490 filp = filp_open(osd_scrub_name, O_RDWR |
2491 (dev->od_dt_dev.dd_rdonly ? 0 : O_CREAT), 0644);
2493 pop_ctxt(&saved, ctxt);
2494 RETURN(PTR_ERR(filp));
2497 inode = file_inode(filp);
2498 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2499 if (!dev->od_dt_dev.dd_rdonly) {
2500 /* 'What the @fid is' is not imporatant, because the object
2501 * has no OI mapping, and only is visible inside the OSD.*/
2502 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2503 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2505 filp_close(filp, NULL);
2506 pop_ctxt(&saved, ctxt);
2511 osd_id_gen(id, inode->i_ino, inode->i_generation);
2512 osd_add_oi_cache(info, dev, id, fid);
2513 filp_close(filp, NULL);
2514 pop_ctxt(&saved, ctxt);
2516 obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
2517 if (IS_ERR_OR_NULL(obj))
2518 RETURN(obj ? PTR_ERR(obj) : -ENOENT);
2520 #ifndef HAVE_S_UUID_AS_UUID_T
2521 memcpy(dev->od_uuid.b, sb->s_uuid, sizeof(dev->od_uuid));
2523 uuid_copy(&dev->od_uuid, &sb->s_uuid);
2525 scrub->os_obj = obj;
2526 rc = scrub_file_load(env, scrub);
2527 if (rc == -ENOENT || rc == -EFAULT) {
2528 scrub_file_init(scrub, dev->od_uuid);
2529 /* If the "/O" dir does not exist when mount (indicated by
2530 * osd_device::od_maybe_new), neither for the "/OI_scrub",
2531 * then it is quite probably that the device is a new one,
2532 * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2534 * For the rare case that "/O" and "OI_scrub" both lost on
2535 * an old device, it can be found and cleared later.
2537 * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2538 * need to check "filter_fid_18_23" and to convert it to
2539 * "filter_fid" for each object, and all the IGIF should
2540 * have their FID mapping in OI files already. */
2541 if (dev->od_maybe_new && rc == -ENOENT)
2542 sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2544 } else if (rc < 0) {
2545 GOTO(cleanup_obj, rc);
2547 if (!uuid_equal(&sf->sf_uuid, &dev->od_uuid)) {
2549 "%s: UUID has been changed from %pU to %pU\n",
2550 osd_dev2name(dev), &sf->sf_uuid, &dev->od_uuid);
2551 scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
2554 } else if (sf->sf_status == SS_SCANNING) {
2555 sf->sf_status = SS_CRASHED;
2559 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
2560 LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
2561 osd_dev2name(dev), sf->sf_oi_count,
2563 sf->sf_oi_count = osd_oi_count;
2568 if (sf->sf_pos_last_checkpoint != 0)
2569 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2571 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2574 rc = scrub_file_store(env, scrub);
2576 GOTO(cleanup_obj, rc);
2579 /* Initialize OI files. */
2580 rc = osd_oi_init(info, dev, restored);
2582 GOTO(cleanup_obj, rc);
2584 if (!dev->od_dt_dev.dd_rdonly)
2585 osd_initial_OI_scrub(info, dev);
2587 if (sf->sf_flags & SF_UPGRADE ||
2588 !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2589 sf->sf_success_count > 0)) {
2590 dev->od_igif_inoi = 0;
2591 dev->od_check_ff = dev->od_is_ost;
2593 dev->od_igif_inoi = 1;
2594 dev->od_check_ff = 0;
2597 if (sf->sf_flags & SF_INCONSISTENT)
2598 /* The 'od_igif_inoi' will be set under the
2600 * 1) new created system, or
2601 * 2) restored from file-level backup, or
2602 * 3) the upgrading completed.
2604 * The 'od_igif_inoi' may be cleared by OI scrub
2605 * later if found that the system is upgrading. */
2606 dev->od_igif_inoi = 1;
2608 if (!dev->od_dt_dev.dd_rdonly &&
2609 dev->od_scrub.os_scrub.os_auto_scrub_interval != AS_NEVER &&
2610 ((sf->sf_status == SS_PAUSED) ||
2611 (sf->sf_status == SS_CRASHED &&
2612 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2613 SF_UPGRADE | SF_AUTO)) ||
2614 (sf->sf_status == SS_INIT &&
2615 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2617 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
2620 GOTO(cleanup_oi, rc);
2622 /* it is possible that dcache entries may keep objects after they are
2623 * deleted by OSD. While it looks safe this can cause object data to
2624 * stay until umount causing failures in tests calculating free space,
2625 * e.g. replay-ost-single. Since those dcache entries are not used
2626 * anymore let's just free them after use here */
2627 shrink_dcache_sb(sb);
2631 osd_oi_fini(info, dev);
2633 dt_object_put_nocache(env, scrub->os_obj);
2634 scrub->os_obj = NULL;
2639 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2641 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2643 LASSERT(dev->od_otable_it == NULL);
2645 if (scrub->os_obj != NULL) {
2646 osd_scrub_stop(dev);
2647 dt_object_put_nocache(env, scrub->os_obj);
2648 scrub->os_obj = NULL;
2652 /* object table based iteration APIs */
2654 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2655 struct dt_object *dt, __u32 attr)
2657 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2658 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2659 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
2660 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2661 struct osd_otable_it *it;
2666 /* od_otable_mutex: prevent curcurrent init/fini */
2667 mutex_lock(&dev->od_otable_mutex);
2668 if (dev->od_otable_it != NULL)
2669 GOTO(out, it = ERR_PTR(-EALREADY));
2673 GOTO(out, it = ERR_PTR(-ENOMEM));
2675 dev->od_otable_it = it;
2677 it->ooi_cache.ooc_consumer_idx = -1;
2678 if (flags & DOIF_OUTUSED)
2679 it->ooi_used_outside = 1;
2681 if (flags & DOIF_RESET)
2684 if (valid & DOIV_ERROR_HANDLE) {
2685 if (flags & DOIF_FAILOUT)
2686 start |= SS_SET_FAILOUT;
2688 start |= SS_CLEAR_FAILOUT;
2691 if (valid & DOIV_DRYRUN) {
2692 if (flags & DOIF_DRYRUN)
2693 start |= SS_SET_DRYRUN;
2695 start |= SS_CLEAR_DRYRUN;
2698 rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
2699 if (rc == -EALREADY) {
2700 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2701 } else if (rc < 0) {
2702 dev->od_otable_it = NULL;
2706 /* We have to start from the begining. */
2707 it->ooi_cache.ooc_pos_preload =
2708 LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2714 mutex_unlock(&dev->od_otable_mutex);
2715 return (struct dt_it *)it;
2718 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2720 struct osd_otable_it *it = (struct osd_otable_it *)di;
2721 struct osd_device *dev = it->ooi_dev;
2723 /* od_otable_mutex: prevent curcurrent init/fini */
2724 mutex_lock(&dev->od_otable_mutex);
2725 scrub_stop(&dev->od_scrub.os_scrub);
2726 LASSERT(dev->od_otable_it == it);
2728 dev->od_otable_it = NULL;
2729 mutex_unlock(&dev->od_otable_mutex);
2733 static int osd_otable_it_get(const struct lu_env *env,
2734 struct dt_it *di, const struct dt_key *key)
2739 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2744 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
2746 spin_lock(&scrub->os_lock);
2747 if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2748 scrub->os_waiting || !scrub->os_running)
2749 it->ooi_waiting = 0;
2751 it->ooi_waiting = 1;
2752 spin_unlock(&scrub->os_lock);
2754 return !it->ooi_waiting;
2757 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2759 struct osd_otable_it *it = (struct osd_otable_it *)di;
2760 struct osd_device *dev = it->ooi_dev;
2761 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2762 struct osd_otable_cache *ooc = &it->ooi_cache;
2766 LASSERT(it->ooi_user_ready);
2769 if (!scrub->os_running && !it->ooi_used_outside)
2772 if (ooc->ooc_cached_items > 0) {
2773 ooc->ooc_cached_items--;
2774 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2775 ~OSD_OTABLE_IT_CACHE_MASK;
2779 if (it->ooi_all_cached) {
2780 wait_var_event(scrub, !scrub->os_running);
2784 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2785 spin_lock(&scrub->os_lock);
2786 scrub->os_waiting = 0;
2788 spin_unlock(&scrub->os_lock);
2791 if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2792 wait_var_event(scrub, osd_otable_it_wakeup(scrub, it));
2794 if (!scrub->os_running && !it->ooi_used_outside)
2797 rc = osd_otable_it_preload(env, it);
2804 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2805 const struct dt_it *di)
2810 static int osd_otable_it_key_size(const struct lu_env *env,
2811 const struct dt_it *di)
2813 return sizeof(__u64);
2816 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2817 struct dt_rec *rec, __u32 attr)
2819 struct osd_otable_it *it = (struct osd_otable_it *)di;
2820 struct osd_otable_cache *ooc = &it->ooi_cache;
2822 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2824 /* Filter out Invald FID already. */
2825 LASSERTF(fid_is_sane((struct lu_fid *)rec),
2826 "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2827 PFID((struct lu_fid *)rec),
2828 ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2833 static __u64 osd_otable_it_store(const struct lu_env *env,
2834 const struct dt_it *di)
2836 struct osd_otable_it *it = (struct osd_otable_it *)di;
2837 struct osd_otable_cache *ooc = &it->ooi_cache;
2840 if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2841 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2843 hash = ooc->ooc_pos_preload;
2848 * Set the OSD layer iteration start position as the specified hash.
2850 static int osd_otable_it_load(const struct lu_env *env,
2851 const struct dt_it *di, __u64 hash)
2853 struct osd_otable_it *it = (struct osd_otable_it *)di;
2854 struct osd_device *dev = it->ooi_dev;
2855 struct osd_otable_cache *ooc = &it->ooi_cache;
2856 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2857 struct osd_iit_param *param = &it->ooi_iit_param;
2861 /* Forbid to set iteration position after iteration started. */
2862 if (it->ooi_user_ready)
2865 LASSERT(!scrub->os_partial_scan);
2867 if (hash > OSD_OTABLE_MAX_HASH)
2868 hash = OSD_OTABLE_MAX_HASH;
2870 /* The hash is the last checkpoint position,
2871 * we will start from the next one. */
2872 ooc->ooc_pos_preload = hash + 1;
2873 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2874 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2876 it->ooi_user_ready = 1;
2877 if (!scrub->os_full_speed)
2880 memset(param, 0, sizeof(*param));
2881 param->sb = osd_sb(dev);
2882 param->start = ooc->ooc_pos_preload;
2883 param->bg = (ooc->ooc_pos_preload - 1) /
2884 LDISKFS_INODES_PER_GROUP(param->sb);
2885 param->offset = (ooc->ooc_pos_preload - 1) %
2886 LDISKFS_INODES_PER_GROUP(param->sb);
2887 param->gbase = 1 + param->bg * LDISKFS_INODES_PER_GROUP(param->sb);
2889 /* Unplug OSD layer iteration by the first next() call. */
2890 rc = osd_otable_it_next(env, (struct dt_it *)it);
2895 static int osd_otable_it_key_rec(const struct lu_env *env,
2896 const struct dt_it *di, void *key_rec)
2901 const struct dt_index_operations osd_otable_ops = {
2903 .init = osd_otable_it_init,
2904 .fini = osd_otable_it_fini,
2905 .get = osd_otable_it_get,
2906 .put = osd_otable_it_put,
2907 .next = osd_otable_it_next,
2908 .key = osd_otable_it_key,
2909 .key_size = osd_otable_it_key_size,
2910 .rec = osd_otable_it_rec,
2911 .store = osd_otable_it_store,
2912 .load = osd_otable_it_load,
2913 .key_rec = osd_otable_it_key_rec,
2917 /* high priority inconsistent items list APIs */
2919 #define SCRUB_BAD_OIMAP_DECAY_INTERVAL 60
2921 int osd_oii_insert(struct osd_device *dev, const struct lu_fid *fid,
2922 struct osd_inode_id *id, int insert)
2924 struct osd_inconsistent_item *oii;
2925 struct osd_scrub *oscrub = &dev->od_scrub;
2926 struct lustre_scrub *lscrub = &oscrub->os_scrub;
2931 if (unlikely(oii == NULL))
2934 INIT_LIST_HEAD(&oii->oii_list);
2935 oii->oii_cache.oic_fid = *fid;
2936 oii->oii_cache.oic_lid = *id;
2937 oii->oii_cache.oic_dev = dev;
2938 oii->oii_insert = insert;
2940 spin_lock(&lscrub->os_lock);
2941 if (lscrub->os_partial_scan) {
2942 __u64 now = ktime_get_real_seconds();
2944 /* If there haven't been errors in a long time,
2945 * decay old count until either the errors are
2946 * gone or we reach the current interval. */
2947 while (unlikely(oscrub->os_bad_oimap_count > 0 &&
2948 oscrub->os_bad_oimap_time +
2949 SCRUB_BAD_OIMAP_DECAY_INTERVAL < now)) {
2950 oscrub->os_bad_oimap_count >>= 1;
2951 oscrub->os_bad_oimap_time +=
2952 SCRUB_BAD_OIMAP_DECAY_INTERVAL;
2955 oscrub->os_bad_oimap_time = now;
2956 if (++oscrub->os_bad_oimap_count >
2957 dev->od_full_scrub_threshold_rate)
2958 lscrub->os_full_scrub = 1;
2961 if (!lscrub->os_running) {
2962 spin_unlock(&lscrub->os_lock);
2967 if (list_empty(&lscrub->os_inconsistent_items))
2969 list_add_tail(&oii->oii_list, &lscrub->os_inconsistent_items);
2970 spin_unlock(&lscrub->os_lock);
2973 wake_up_var(lscrub);
2978 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2979 struct osd_inode_id *id)
2981 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
2982 struct osd_inconsistent_item *oii;
2985 spin_lock(&scrub->os_lock);
2986 list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2987 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2988 *id = oii->oii_cache.oic_lid;
2989 spin_unlock(&scrub->os_lock);
2993 spin_unlock(&scrub->os_lock);
2998 void osd_scrub_dump(struct seq_file *m, struct osd_device *dev)
3000 struct osd_scrub *scrub = &dev->od_scrub;
3002 scrub_dump(m, &scrub->os_scrub);
3003 seq_printf(m, "lf_scanned: %llu\n"
3005 "lf_failed: %llu\n",
3006 scrub->os_lf_scanned,
3007 scrub->os_scrub.os_file.sf_param & SP_DRYRUN ?
3008 "inconsistent" : "repaired",
3009 scrub->os_lf_repaired,
3010 scrub->os_lf_failed);
3013 typedef int (*scan_dir_helper_t)(const struct lu_env *env,
3014 struct osd_device *dev, struct inode *dir,
3015 struct osd_it_ea *oie);
3017 static int osd_scan_dir(const struct lu_env *env, struct osd_device *dev,
3018 struct inode *inode, scan_dir_helper_t cb)
3020 struct osd_it_ea *oie;
3025 oie = osd_it_dir_init(env, dev, inode, LUDA_TYPE);
3027 RETURN(PTR_ERR(oie));
3029 oie->oie_file->f_pos = 0;
3030 rc = osd_ldiskfs_it_fill(env, (struct dt_it *)oie);
3036 while (oie->oie_it_dirent <= oie->oie_rd_dirent) {
3037 if (!name_is_dot_or_dotdot(oie->oie_dirent->oied_name,
3038 oie->oie_dirent->oied_namelen))
3039 cb(env, dev, inode, oie);
3041 oie->oie_dirent = (void *)oie->oie_dirent +
3042 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3043 oie->oie_dirent->oied_namelen);
3045 oie->oie_it_dirent++;
3046 if (oie->oie_it_dirent <= oie->oie_rd_dirent)
3049 if (oie->oie_file->f_pos ==
3050 ldiskfs_get_htree_eof(oie->oie_file))
3053 rc = osd_ldiskfs_it_fill(env, (struct dt_it *)oie);
3062 osd_it_dir_fini(env, oie, inode);
3066 static int osd_remove_ml_file(struct osd_thread_info *info,
3067 struct osd_device *dev, struct inode *dir,
3068 struct inode *inode, struct osd_it_ea *oie)
3071 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
3072 struct dentry dentry;
3077 if (scrub->os_file.sf_param & SP_DRYRUN)
3080 th = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
3081 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3082 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3084 RETURN(PTR_ERR(th));
3086 /* Should be created by the VFS layer */
3087 dentry.d_inode = dir;
3088 dentry.d_sb = dir->i_sb;
3089 rc = osd_obj_del_entry(info, dev, &dentry, oie->oie_dirent->oied_name,
3090 oie->oie_dirent->oied_namelen, th);
3092 mark_inode_dirty(inode);
3093 ldiskfs_journal_stop(th);
3097 static int osd_scan_ml_file(const struct lu_env *env, struct osd_device *dev,
3098 struct inode *dir, struct osd_it_ea *oie)
3100 struct osd_thread_info *info = osd_oti_get(env);
3101 struct osd_inode_id id;
3102 struct inode *inode;
3103 struct osd_obj_seq *oseq;
3104 struct ost_id *ostid = &info->oti_ostid;
3105 struct lu_fid *fid = &oie->oie_dirent->oied_fid;
3111 osd_id_gen(&id, oie->oie_dirent->oied_ino, OSD_OII_NOGEN);