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) 2012, 2013, 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_MDS
40 #include <lustre/lustre_idl.h>
41 #include <lustre_disk.h>
42 #include <dt_object.h>
43 #include <linux/xattr.h>
45 #include "osd_internal.h"
47 #include "osd_scrub.h"
49 #define HALF_SEC (HZ >> 1)
51 #define OSD_OTABLE_MAX_HASH 0x00000000ffffffffULL
53 #define SCRUB_NEXT_BREAK 1 /* exit current loop and process next group */
54 #define SCRUB_NEXT_CONTINUE 2 /* skip current object and process next bit */
55 #define SCRUB_NEXT_EXIT 3 /* exit all the loops */
56 #define SCRUB_NEXT_WAIT 4 /* wait for free cache slot */
57 #define SCRUB_NEXT_CRASH 5 /* simulate system crash during OI scrub */
58 #define SCRUB_NEXT_FATAL 6 /* simulate failure during OI scrub */
59 #define SCRUB_NEXT_NOSCRUB 7 /* new created object, no scrub on it */
60 #define SCRUB_NEXT_NOLMA 8 /* the inode has no FID-in-LMA */
61 #define SCRUB_NEXT_OSTOBJ 9 /* for OST-object */
62 #define SCRUB_NEXT_OSTOBJ_OLD 10 /* old OST-object, no LMA or no FID-on-OST
67 static inline struct osd_device *osd_scrub2dev(struct osd_scrub *scrub)
69 return container_of0(scrub, struct osd_device, od_scrub);
72 static inline struct super_block *osd_scrub2sb(struct osd_scrub *scrub)
74 return osd_sb(osd_scrub2dev(scrub));
77 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
78 struct osd_otable_cache *ooc)
80 return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
84 * update/insert/delete the specified OI mapping (@fid @id) according to the ops
86 * \retval 1, changed nothing
87 * \retval 0, changed successfully
88 * \retval -ve, on error
90 static int osd_scrub_refresh_mapping(struct osd_thread_info *info,
91 struct osd_device *dev,
92 const struct lu_fid *fid,
93 const struct osd_inode_id *id,
95 enum oi_check_flags flags)
97 const struct lu_env *env = info->oti_env;
99 struct osd_thandle *oh;
103 if (dev->od_scrub.os_file.sf_param & SP_DRYRUN && !force)
106 th = dt_trans_create(env, &dev->od_dt_dev);
110 oh = container_of0(th, struct osd_thandle, ot_super);
111 LASSERT(oh->ot_handle == NULL);
114 case DTO_INDEX_UPDATE:
115 osd_trans_declare_op(env, oh, OSD_OT_UPDATE,
116 osd_dto_credits_noquota[DTO_INDEX_UPDATE]);
117 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
121 rc = osd_oi_update(info, dev, fid, id, th, flags);
122 if (unlikely(rc == -ENOENT)) {
123 /* Some unlink thread may removed the OI mapping. */
127 case DTO_INDEX_INSERT:
128 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
129 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
130 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
134 rc = osd_oi_insert(info, dev, fid, id, th, flags);
135 if (unlikely(rc == -EEXIST)) {
137 /* XXX: There are trouble things when adding OI
138 * mapping for IGIF object, which may cause
139 * multiple objects to be mapped to the same
140 * IGIF formatted FID. Consider the following
143 * 1) The MDT is upgrading from 1.8 device.
144 * The OI scrub generates IGIF FID1 for the
145 * OBJ1 and adds the OI mapping.
147 * 2) For some reason, the OI scrub does not
148 * process all the IGIF objects completely.
150 * 3) The MDT is backuped and restored against
153 * 4) When the MDT mounts up, the OI scrub will
154 * try to rebuild the OI files. For some IGIF
155 * object, OBJ2, which was not processed by the
156 * OI scrub before the backup/restore, and the
157 * new generated IGIF formatted FID may be just
158 * the FID1, the same as OBJ1.
160 * Under such case, the OI scrub cannot know how
161 * to generate new FID for the OBJ2.
163 * Currently, we do nothing for that. One possible
164 * solution is to generate new normal FID for the
167 * Anyway, it is rare, only exists in theory. */
170 case DTO_INDEX_DELETE:
171 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
172 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
173 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
177 rc = osd_oi_delete(info, dev, fid, th, flags);
179 /* It is normal that the unlink thread has removed the
180 * OI mapping already. */
185 LASSERTF(0, "Unexpected ops %d\n", ops);
192 dt_trans_stop(env, &dev->od_dt_dev, th);
196 /* OI_scrub file ops */
198 static void osd_scrub_file_to_cpu(struct scrub_file *des,
199 struct scrub_file *src)
201 memcpy(des->sf_uuid, src->sf_uuid, 16);
202 des->sf_flags = le64_to_cpu(src->sf_flags);
203 des->sf_magic = le32_to_cpu(src->sf_magic);
204 des->sf_status = le16_to_cpu(src->sf_status);
205 des->sf_param = le16_to_cpu(src->sf_param);
206 des->sf_time_last_complete =
207 le64_to_cpu(src->sf_time_last_complete);
208 des->sf_time_latest_start =
209 le64_to_cpu(src->sf_time_latest_start);
210 des->sf_time_last_checkpoint =
211 le64_to_cpu(src->sf_time_last_checkpoint);
212 des->sf_pos_latest_start =
213 le64_to_cpu(src->sf_pos_latest_start);
214 des->sf_pos_last_checkpoint =
215 le64_to_cpu(src->sf_pos_last_checkpoint);
216 des->sf_pos_first_inconsistent =
217 le64_to_cpu(src->sf_pos_first_inconsistent);
218 des->sf_items_checked =
219 le64_to_cpu(src->sf_items_checked);
220 des->sf_items_updated =
221 le64_to_cpu(src->sf_items_updated);
222 des->sf_items_failed =
223 le64_to_cpu(src->sf_items_failed);
224 des->sf_items_updated_prior =
225 le64_to_cpu(src->sf_items_updated_prior);
226 des->sf_run_time = le32_to_cpu(src->sf_run_time);
227 des->sf_success_count = le32_to_cpu(src->sf_success_count);
228 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
229 des->sf_internal_flags = le16_to_cpu(src->sf_internal_flags);
230 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
233 static void osd_scrub_file_to_le(struct scrub_file *des,
234 struct scrub_file *src)
236 memcpy(des->sf_uuid, src->sf_uuid, 16);
237 des->sf_flags = cpu_to_le64(src->sf_flags);
238 des->sf_magic = cpu_to_le32(src->sf_magic);
239 des->sf_status = cpu_to_le16(src->sf_status);
240 des->sf_param = cpu_to_le16(src->sf_param);
241 des->sf_time_last_complete =
242 cpu_to_le64(src->sf_time_last_complete);
243 des->sf_time_latest_start =
244 cpu_to_le64(src->sf_time_latest_start);
245 des->sf_time_last_checkpoint =
246 cpu_to_le64(src->sf_time_last_checkpoint);
247 des->sf_pos_latest_start =
248 cpu_to_le64(src->sf_pos_latest_start);
249 des->sf_pos_last_checkpoint =
250 cpu_to_le64(src->sf_pos_last_checkpoint);
251 des->sf_pos_first_inconsistent =
252 cpu_to_le64(src->sf_pos_first_inconsistent);
253 des->sf_items_checked =
254 cpu_to_le64(src->sf_items_checked);
255 des->sf_items_updated =
256 cpu_to_le64(src->sf_items_updated);
257 des->sf_items_failed =
258 cpu_to_le64(src->sf_items_failed);
259 des->sf_items_updated_prior =
260 cpu_to_le64(src->sf_items_updated_prior);
261 des->sf_run_time = cpu_to_le32(src->sf_run_time);
262 des->sf_success_count = cpu_to_le32(src->sf_success_count);
263 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
264 des->sf_internal_flags = cpu_to_le16(src->sf_internal_flags);
265 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
268 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
270 struct scrub_file *sf = &scrub->os_file;
272 memset(sf, 0, sizeof(*sf));
273 memcpy(sf->sf_uuid, uuid, 16);
274 sf->sf_magic = SCRUB_MAGIC_V1;
275 sf->sf_status = SS_INIT;
278 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
280 struct scrub_file *sf = &scrub->os_file;
282 CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
283 memcpy(sf->sf_uuid, uuid, 16);
284 sf->sf_status = SS_INIT;
285 sf->sf_flags |= flags;
287 sf->sf_time_latest_start = 0;
288 sf->sf_time_last_checkpoint = 0;
289 sf->sf_pos_latest_start = 0;
290 sf->sf_pos_last_checkpoint = 0;
291 sf->sf_pos_first_inconsistent = 0;
292 sf->sf_items_checked = 0;
293 sf->sf_items_updated = 0;
294 sf->sf_items_failed = 0;
295 sf->sf_items_updated_prior = 0;
296 sf->sf_items_noscrub = 0;
297 sf->sf_items_igif = 0;
300 static int osd_scrub_file_load(struct osd_scrub *scrub)
303 char *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
304 int len = sizeof(scrub->os_file_disk);
307 rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
309 struct scrub_file *sf = &scrub->os_file;
311 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
312 if (sf->sf_magic != SCRUB_MAGIC_V1) {
313 CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
314 name, sf->sf_magic, SCRUB_MAGIC_V1);
315 /* Process it as new scrub file. */
320 } else if (rc != 0) {
321 CERROR("%.16s: fail to load scrub file, expected = %d, "
322 "rc = %d\n", name, len, rc);
326 /* return -ENOENT for empty scrub file case. */
333 int osd_scrub_file_store(struct osd_scrub *scrub)
335 struct osd_device *dev;
338 int len = sizeof(scrub->os_file_disk);
342 dev = container_of0(scrub, struct osd_device, od_scrub);
343 credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
344 osd_dto_credits_noquota[DTO_WRITE_BLOCK];
345 jh = ldiskfs_journal_start_sb(osd_sb(dev), credits);
348 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
349 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
353 osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
354 rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
356 ldiskfs_journal_stop(jh);
358 CERROR("%.16s: fail to store scrub file, expected = %d, "
360 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
362 scrub->os_time_last_checkpoint = cfs_time_current();
363 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
364 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
369 osd_scrub_convert_ff(struct osd_thread_info *info, struct osd_device *dev,
370 struct inode *inode, const struct lu_fid *fid)
372 struct filter_fid_old *ff = &info->oti_ff;
373 struct dentry *dentry = &info->oti_obj_dentry;
377 bool removed = false;
381 if (dev->od_scrub.os_file.sf_param & SP_DRYRUN)
384 /* We want the LMA to fit into the 256-byte OST inode, so operate
386 * 1) read old XATTR_NAME_FID and save the parent FID;
387 * 2) delete the old XATTR_NAME_FID;
388 * 3) make new LMA and add it;
389 * 4) generate new XATTR_NAME_FID with the saved parent FID and add it.
391 * Making the LMA to fit into the 256-byte OST inode can save time for
392 * normal osd_check_lma() and for other OI scrub scanning in future.
393 * So it is worth to make some slow conversion here. */
394 jh = ldiskfs_journal_start_sb(osd_sb(dev),
395 osd_dto_credits_noquota[DTO_XATTR_SET] * 3);
398 CERROR("%s: fail to start trans for convert ff: "DFID
400 osd_name(dev), PFID(fid), rc);
404 /* 1) read old XATTR_NAME_FID and save the parent FID */
405 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
406 if (rc == sizeof(*ff)) {
407 /* 2) delete the old XATTR_NAME_FID */
408 ll_vfs_dq_init(inode);
409 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
414 } else if (unlikely(rc == -ENODATA)) {
416 } else if (rc != sizeof(struct filter_fid)) {
417 GOTO(stop, rc = -EINVAL);
420 /* 3) make new LMA and add it */
421 rc = osd_ea_fid_set(info, inode, fid, LMAC_FID_ON_OST, 0);
422 if (rc == 0 && reset)
423 size = sizeof(struct filter_fid);
424 else if (rc != 0 && removed)
425 /* If failed, we should try to add the old back. */
426 size = sizeof(struct filter_fid_old);
428 /* 4) generate new XATTR_NAME_FID with the saved parent FID and add it*/
432 rc1 = __osd_xattr_set(info, inode, XATTR_NAME_FID, ff, size,
434 if (rc1 != 0 && rc == 0)
441 ldiskfs_journal_stop(jh);
446 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
447 struct osd_idmap_cache *oic, int val)
449 struct osd_scrub *scrub = &dev->od_scrub;
450 struct scrub_file *sf = &scrub->os_file;
451 struct lu_fid *fid = &oic->oic_fid;
452 struct osd_inode_id *lid = &oic->oic_lid;
453 struct osd_inode_id *lid2 = &info->oti_id;
454 struct osd_inconsistent_item *oii = NULL;
455 struct inode *inode = NULL;
456 int ops = DTO_INDEX_UPDATE;
459 bool converted = false;
462 down_write(&scrub->os_rwsem);
463 scrub->os_new_checked++;
467 if (scrub->os_in_prior)
468 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
471 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
474 if (fid_is_igif(fid))
477 if (val == SCRUB_NEXT_OSTOBJ_OLD) {
478 inode = osd_iget(info, dev, lid);
481 /* Someone removed the inode. */
482 if (rc == -ENOENT || rc == -ESTALE)
487 sf->sf_flags |= SF_UPGRADE;
488 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
489 dev->od_check_ff = 1;
490 rc = osd_scrub_convert_ff(info, dev, inode, fid);
497 if ((val == SCRUB_NEXT_NOLMA) &&
498 (!scrub->os_convert_igif || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
501 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
504 rc = osd_oi_lookup(info, dev, fid, lid2,
505 (val == SCRUB_NEXT_OSTOBJ ||
506 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
513 inode = osd_iget(info, dev, lid);
516 /* Someone removed the inode. */
517 if (rc == -ENOENT || rc == -ESTALE)
523 scrub->os_full_speed = 1;
524 ops = DTO_INDEX_INSERT;
525 idx = osd_oi_fid2idx(dev, fid);
527 case SCRUB_NEXT_NOLMA:
528 sf->sf_flags |= SF_UPGRADE;
529 if (!(sf->sf_param & SP_DRYRUN)) {
530 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
535 if (!(sf->sf_flags & SF_INCONSISTENT))
536 dev->od_igif_inoi = 0;
538 case SCRUB_NEXT_OSTOBJ:
539 sf->sf_flags |= SF_INCONSISTENT;
540 case SCRUB_NEXT_OSTOBJ_OLD:
543 sf->sf_flags |= SF_RECREATED;
544 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
545 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
548 } else if (osd_id_eq(lid, lid2)) {
550 sf->sf_items_updated++;
554 scrub->os_full_speed = 1;
555 sf->sf_flags |= SF_INCONSISTENT;
557 /* XXX: If the device is restored from file-level backup, then
558 * some IGIFs may have been already in OI files, and some
559 * may be not yet. Means upgrading from 1.8 may be partly
560 * processed, but some clients may hold some immobilized
561 * IGIFs, and use them to access related objects. Under
562 * such case, OSD does not know whether an given IGIF has
563 * been processed or to be processed, and it also cannot
564 * generate local ino#/gen# directly from the immobilized
565 * IGIF because of the backup/restore. Then force OSD to
566 * lookup the given IGIF in OI files, and if no entry,
567 * then ask the client to retry after upgrading completed.
568 * No better choice. */
569 dev->od_igif_inoi = 1;
572 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops, false,
573 (val == SCRUB_NEXT_OSTOBJ ||
574 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
576 if (scrub->os_in_prior)
577 sf->sf_items_updated_prior++;
579 sf->sf_items_updated++;
581 /* The target has been changed, need to be re-loaded. */
582 lu_object_purge(info->oti_env, osd2lu_dev(dev), fid);
589 sf->sf_items_failed++;
590 if (sf->sf_pos_first_inconsistent == 0 ||
591 sf->sf_pos_first_inconsistent > lid->oii_ino)
592 sf->sf_pos_first_inconsistent = lid->oii_ino;
597 /* There may be conflict unlink during the OI scrub,
598 * if happend, then remove the new added OI mapping. */
599 if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
600 unlikely(inode->i_nlink == 0))
601 osd_scrub_refresh_mapping(info, dev, fid, lid,
602 DTO_INDEX_DELETE, false,
603 (val == SCRUB_NEXT_OSTOBJ ||
604 val == SCRUB_NEXT_OSTOBJ_OLD) ?
605 OI_KNOWN_ON_OST : 0);
606 up_write(&scrub->os_rwsem);
608 if (inode != NULL && !IS_ERR(inode))
612 LASSERT(!cfs_list_empty(&oii->oii_list));
614 spin_lock(&scrub->os_lock);
615 cfs_list_del_init(&oii->oii_list);
616 spin_unlock(&scrub->os_lock);
619 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
624 static int osd_scrub_prep(struct osd_device *dev)
626 struct osd_scrub *scrub = &dev->od_scrub;
627 struct ptlrpc_thread *thread = &scrub->os_thread;
628 struct scrub_file *sf = &scrub->os_file;
629 __u32 flags = scrub->os_start_flags;
631 bool drop_dryrun = false;
634 down_write(&scrub->os_rwsem);
635 if (flags & SS_SET_FAILOUT)
636 sf->sf_param |= SP_FAILOUT;
638 if (flags & SS_CLEAR_FAILOUT)
639 sf->sf_param &= ~SP_FAILOUT;
641 if (flags & SS_SET_DRYRUN)
642 sf->sf_param |= SP_DRYRUN;
644 if (flags & SS_CLEAR_DRYRUN && sf->sf_param & SP_DRYRUN) {
645 sf->sf_param &= ~SP_DRYRUN;
649 if (flags & SS_RESET)
650 osd_scrub_file_reset(scrub,
651 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
653 if (flags & SS_AUTO) {
654 scrub->os_full_speed = 1;
655 sf->sf_flags |= SF_AUTO;
656 /* For the case of OI scrub auto triggered, NOT dryrun. */
657 sf->sf_param &= ~SP_FAILOUT;
659 scrub->os_full_speed = 0;
662 if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
663 scrub->os_full_speed = 1;
665 scrub->os_in_prior = 0;
666 spin_lock(&scrub->os_lock);
667 scrub->os_waiting = 0;
668 scrub->os_paused = 0;
669 spin_unlock(&scrub->os_lock);
670 scrub->os_new_checked = 0;
671 if (drop_dryrun && sf->sf_pos_first_inconsistent != 0)
672 sf->sf_pos_latest_start = sf->sf_pos_first_inconsistent;
673 else if (sf->sf_pos_last_checkpoint != 0)
674 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
676 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
678 scrub->os_pos_current = sf->sf_pos_latest_start;
679 sf->sf_status = SS_SCANNING;
680 sf->sf_time_latest_start = cfs_time_current_sec();
681 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
682 rc = osd_scrub_file_store(scrub);
684 spin_lock(&scrub->os_lock);
685 thread_set_flags(thread, SVC_RUNNING);
686 spin_unlock(&scrub->os_lock);
687 wake_up_all(&thread->t_ctl_waitq);
689 up_write(&scrub->os_rwsem);
694 static int osd_scrub_checkpoint(struct osd_scrub *scrub)
696 struct scrub_file *sf = &scrub->os_file;
699 if (likely(cfs_time_before(cfs_time_current(),
700 scrub->os_time_next_checkpoint) ||
701 scrub->os_new_checked == 0))
704 down_write(&scrub->os_rwsem);
705 sf->sf_items_checked += scrub->os_new_checked;
706 scrub->os_new_checked = 0;
707 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
708 sf->sf_time_last_checkpoint = cfs_time_current_sec();
709 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
710 scrub->os_time_last_checkpoint);
711 rc = osd_scrub_file_store(scrub);
712 up_write(&scrub->os_rwsem);
717 static void osd_scrub_post(struct osd_scrub *scrub, int result)
719 struct scrub_file *sf = &scrub->os_file;
722 down_write(&scrub->os_rwsem);
723 spin_lock(&scrub->os_lock);
724 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
725 spin_unlock(&scrub->os_lock);
726 if (scrub->os_new_checked > 0) {
727 sf->sf_items_checked += scrub->os_new_checked;
728 scrub->os_new_checked = 0;
729 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
731 sf->sf_time_last_checkpoint = cfs_time_current_sec();
733 struct osd_device *dev =
734 container_of0(scrub, struct osd_device, od_scrub);
736 dev->od_igif_inoi = 1;
737 dev->od_check_ff = 0;
738 sf->sf_status = SS_COMPLETED;
739 if (!(sf->sf_param & SP_DRYRUN)) {
740 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
741 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
742 SF_UPGRADE | SF_AUTO);
744 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
745 sf->sf_success_count++;
746 } else if (result == 0) {
747 if (scrub->os_paused)
748 sf->sf_status = SS_PAUSED;
750 sf->sf_status = SS_STOPPED;
752 sf->sf_status = SS_FAILED;
754 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
755 scrub->os_time_last_checkpoint);
756 result = osd_scrub_file_store(scrub);
758 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
759 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
761 up_write(&scrub->os_rwsem);
766 /* iteration engine */
768 struct osd_iit_param {
769 struct super_block *sb;
770 struct buffer_head *bitmap;
776 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
777 struct osd_device *dev,
778 struct osd_iit_param *param,
779 struct osd_idmap_cache **oic,
782 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
783 struct osd_device *dev,
784 struct osd_iit_param *param,
785 struct osd_idmap_cache *oic,
786 int *noslot, int rc);
788 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
790 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
791 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
792 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
793 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
794 return SCRUB_NEXT_BREAK;
796 *pos = param->gbase + param->offset;
802 * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
803 * \retval 0: FID-on-MDT
805 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
806 struct osd_device *dev,
809 /* XXX: The initial OI scrub will scan the top level /O to generate
810 * a small local FLDB according to the <seq>. If the given FID
811 * is in the local FLDB, then it is FID-on-OST; otherwise it's
812 * quite possible for FID-on-MDT. */
814 return SCRUB_NEXT_OSTOBJ_OLD;
819 static int osd_scrub_get_fid(struct osd_thread_info *info,
820 struct osd_device *dev, struct inode *inode,
821 struct lu_fid *fid, bool scrub)
823 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
825 bool has_lma = false;
827 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
830 if (lma->lma_compat & LMAC_NOT_IN_OI) {
831 ldiskfs_set_inode_state(inode,
832 LDISKFS_STATE_LUSTRE_NO_OI);
833 return SCRUB_NEXT_CONTINUE;
836 *fid = lma->lma_self_fid;
837 if (fid_is_internal(&lma->lma_self_fid)) {
839 rc = SCRUB_NEXT_CONTINUE;
846 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
849 if (lma->lma_compat & LMAC_FID_ON_OST)
850 return SCRUB_NEXT_OSTOBJ;
852 if (fid_is_idif(fid) || fid_is_last_id(fid))
853 return SCRUB_NEXT_OSTOBJ_OLD;
855 if (lma->lma_incompat & LMAI_AGENT)
856 return SCRUB_NEXT_CONTINUE;
858 /* Here, it may be MDT-object, or may be 2.4 OST-object.
862 if (rc == -ENODATA || rc == 0) {
863 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
866 /* It is old 2.x (x <= 3) or 1.8 OST-object. */
867 rc = SCRUB_NEXT_OSTOBJ_OLD;
873 /* It is FID-on-OST, but we do not know how
874 * to generate its FID, ignore it directly. */
875 rc = SCRUB_NEXT_CONTINUE;
877 /* It is 2.4 OST-object. */
878 rc = SCRUB_NEXT_OSTOBJ_OLD;
886 if (dev->od_scrub.os_convert_igif) {
887 lu_igif_build(fid, inode->i_ino,
888 inode->i_generation);
890 rc = SCRUB_NEXT_NOLMA;
894 /* It may be FID-on-OST, or may be FID for
895 * non-MDT0, anyway, we do not know how to
896 * generate its FID, ignore it directly. */
897 rc = SCRUB_NEXT_CONTINUE;
902 /* For OI scrub case only: the object has LMA but has no ff
903 * (or ff crashed). It may be MDT-object, may be OST-object
904 * with crashed ff. The last check is local FLDB. */
905 rc = osd_scrub_check_local_fldb(info, dev, fid);
911 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
912 struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
913 struct super_block *sb, bool scrub)
919 osd_id_gen(lid, pos, OSD_OII_NOGEN);
920 inode = osd_iget(info, dev, lid);
923 /* The inode may be removed after bitmap searching, or the
924 * file is new created without inode initialized yet. */
925 if (rc == -ENOENT || rc == -ESTALE)
926 RETURN(SCRUB_NEXT_CONTINUE);
928 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
929 LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
933 /* If the inode has no OI mapping, then it is special locally used,
934 * should be invisible to OI scrub or up layer LFSCK. */
935 if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI))
936 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
939 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
940 /* Only skip it for the first OI scrub accessing. */
941 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
942 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
945 rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
954 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
955 struct osd_iit_param *param,
956 struct osd_idmap_cache **oic, int noslot)
958 struct osd_scrub *scrub = &dev->od_scrub;
959 struct ptlrpc_thread *thread = &scrub->os_thread;
961 struct osd_inode_id *lid;
964 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
965 struct l_wait_info lwi;
967 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
968 l_wait_event(thread->t_ctl_waitq,
969 !cfs_list_empty(&scrub->os_inconsistent_items) ||
970 !thread_is_running(thread),
974 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
975 spin_lock(&scrub->os_lock);
976 thread_set_flags(thread, SVC_STOPPING);
977 spin_unlock(&scrub->os_lock);
978 return SCRUB_NEXT_CRASH;
981 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
982 return SCRUB_NEXT_FATAL;
984 if (unlikely(!thread_is_running(thread)))
985 return SCRUB_NEXT_EXIT;
987 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
988 struct osd_inconsistent_item *oii;
990 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
991 struct osd_inconsistent_item, oii_list);
992 *oic = &oii->oii_cache;
993 scrub->os_in_prior = 1;
998 return SCRUB_NEXT_WAIT;
1000 rc = osd_iit_next(param, &scrub->os_pos_current);
1004 *oic = &scrub->os_oic;
1005 fid = &(*oic)->oic_fid;
1006 lid = &(*oic)->oic_lid;
1007 rc = osd_iit_iget(info, dev, fid, lid,
1008 scrub->os_pos_current, param->sb, true);
1012 static int osd_preload_next(struct osd_thread_info *info,
1013 struct osd_device *dev, struct osd_iit_param *param,
1014 struct osd_idmap_cache **oic, int noslot)
1016 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1017 struct osd_scrub *scrub;
1018 struct ptlrpc_thread *thread;
1021 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
1025 scrub = &dev->od_scrub;
1026 thread = &scrub->os_thread;
1027 if (thread_is_running(thread) &&
1028 ooc->ooc_pos_preload >= scrub->os_pos_current)
1029 return SCRUB_NEXT_EXIT;
1031 rc = osd_iit_iget(info, dev,
1032 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
1033 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
1034 ooc->ooc_pos_preload, param->sb, false);
1035 /* If succeed, it needs to move forward; otherwise up layer LFSCK may
1036 * ignore the failure, so it still need to skip the inode next time. */
1037 ooc->ooc_pos_preload = param->gbase + ++(param->offset);
1042 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
1044 spin_lock(&scrub->os_lock);
1045 if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
1046 !cfs_list_empty(&scrub->os_inconsistent_items) ||
1047 it->ooi_waiting || !thread_is_running(&scrub->os_thread))
1048 scrub->os_waiting = 0;
1050 scrub->os_waiting = 1;
1051 spin_unlock(&scrub->os_lock);
1053 return !scrub->os_waiting;
1056 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
1057 struct osd_iit_param *param,
1058 struct osd_idmap_cache *oic, int *noslot, int rc)
1060 struct l_wait_info lwi = { 0 };
1061 struct osd_scrub *scrub = &dev->od_scrub;
1062 struct scrub_file *sf = &scrub->os_file;
1063 struct ptlrpc_thread *thread = &scrub->os_thread;
1064 struct osd_otable_it *it = dev->od_otable_it;
1065 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
1068 case SCRUB_NEXT_CONTINUE:
1070 case SCRUB_NEXT_WAIT:
1072 case SCRUB_NEXT_NOSCRUB:
1073 down_write(&scrub->os_rwsem);
1074 scrub->os_new_checked++;
1075 sf->sf_items_noscrub++;
1076 up_write(&scrub->os_rwsem);
1080 rc = osd_scrub_check_update(info, dev, oic, rc);
1084 rc = osd_scrub_checkpoint(scrub);
1086 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
1087 LDISKFS_SB(param->sb)->s_es->s_volume_name,
1088 scrub->os_pos_current, rc);
1089 /* Continue, as long as the scrub itself can go ahead. */
1092 if (scrub->os_in_prior) {
1093 scrub->os_in_prior = 0;
1098 scrub->os_pos_current = param->gbase + ++(param->offset);
1101 if (it != NULL && it->ooi_waiting && ooc != NULL &&
1102 ooc->ooc_pos_preload < scrub->os_pos_current) {
1103 spin_lock(&scrub->os_lock);
1104 it->ooi_waiting = 0;
1105 wake_up_all(&thread->t_ctl_waitq);
1106 spin_unlock(&scrub->os_lock);
1109 if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
1112 if (osd_scrub_has_window(scrub, ooc)) {
1117 l_wait_event(thread->t_ctl_waitq,
1118 osd_scrub_wakeup(scrub, it),
1121 if (osd_scrub_has_window(scrub, ooc))
1128 static int osd_preload_exec(struct osd_thread_info *info,
1129 struct osd_device *dev, struct osd_iit_param *param,
1130 struct osd_idmap_cache *oic, int *noslot, int rc)
1132 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1135 ooc->ooc_cached_items++;
1136 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1137 ~OSD_OTABLE_IT_CACHE_MASK;
1139 return rc > 0 ? 0 : rc;
1142 #define SCRUB_IT_ALL 1
1143 #define SCRUB_IT_CRASH 2
1145 static int osd_inode_iteration(struct osd_thread_info *info,
1146 struct osd_device *dev, __u32 max, bool preload)
1148 osd_iit_next_policy next;
1149 osd_iit_exec_policy exec;
1152 struct osd_iit_param param;
1159 struct osd_scrub *scrub = &dev->od_scrub;
1161 next = osd_scrub_next;
1162 exec = osd_scrub_exec;
1163 pos = &scrub->os_pos_current;
1164 count = &scrub->os_new_checked;
1166 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1168 next = osd_preload_next;
1169 exec = osd_preload_exec;
1170 pos = &ooc->ooc_pos_preload;
1171 count = &ooc->ooc_cached_items;
1173 param.sb = osd_sb(dev);
1174 limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1176 while (*pos <= limit && *count < max) {
1177 struct osd_idmap_cache *oic = NULL;
1179 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1180 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1181 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1182 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1183 if (param.bitmap == NULL) {
1184 CERROR("%.16s: fail to read bitmap for %u, "
1185 "scrub will stop, urgent mode\n",
1186 LDISKFS_SB(param.sb)->s_es->s_volume_name,
1191 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1193 rc = next(info, dev, ¶m, &oic, noslot);
1195 case SCRUB_NEXT_BREAK:
1197 case SCRUB_NEXT_EXIT:
1198 brelse(param.bitmap);
1200 case SCRUB_NEXT_CRASH:
1201 brelse(param.bitmap);
1202 RETURN(SCRUB_IT_CRASH);
1203 case SCRUB_NEXT_FATAL:
1204 brelse(param.bitmap);
1208 rc = exec(info, dev, ¶m, oic, &noslot, rc);
1210 brelse(param.bitmap);
1216 brelse(param.bitmap);
1220 RETURN(SCRUB_IT_ALL);
1224 static int osd_otable_it_preload(const struct lu_env *env,
1225 struct osd_otable_it *it)
1227 struct osd_device *dev = it->ooi_dev;
1228 struct osd_scrub *scrub = &dev->od_scrub;
1229 struct osd_otable_cache *ooc = &it->ooi_cache;
1233 rc = osd_inode_iteration(osd_oti_get(env), dev,
1234 OSD_OTABLE_IT_CACHE_SIZE, true);
1235 if (rc == SCRUB_IT_ALL)
1236 it->ooi_all_cached = 1;
1238 CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1239 le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1240 ooc->ooc_pos_preload, rc);
1242 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1243 spin_lock(&scrub->os_lock);
1244 scrub->os_waiting = 0;
1245 wake_up_all(&scrub->os_thread.t_ctl_waitq);
1246 spin_unlock(&scrub->os_lock);
1249 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1252 static int osd_scrub_main(void *args)
1255 struct osd_device *dev = (struct osd_device *)args;
1256 struct osd_scrub *scrub = &dev->od_scrub;
1257 struct ptlrpc_thread *thread = &scrub->os_thread;
1258 struct super_block *sb = osd_sb(dev);
1262 rc = lu_env_init(&env, LCT_LOCAL);
1264 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1265 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1269 rc = osd_scrub_prep(dev);
1271 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1272 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1276 if (!scrub->os_full_speed) {
1277 struct l_wait_info lwi = { 0 };
1278 struct osd_otable_it *it = dev->od_otable_it;
1279 struct osd_otable_cache *ooc = &it->ooi_cache;
1281 l_wait_event(thread->t_ctl_waitq,
1282 it->ooi_user_ready || !thread_is_running(thread),
1284 if (unlikely(!thread_is_running(thread)))
1287 scrub->os_pos_current = ooc->ooc_pos_preload;
1290 CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1291 scrub->os_start_flags, scrub->os_pos_current);
1293 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1294 if (unlikely(rc == SCRUB_IT_CRASH))
1295 GOTO(out, rc = -EINVAL);
1299 osd_scrub_post(scrub, rc);
1300 CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1301 rc, scrub->os_pos_current);
1304 while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1305 struct osd_inconsistent_item *oii;
1307 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1308 struct osd_inconsistent_item, oii_list);
1309 cfs_list_del_init(&oii->oii_list);
1315 spin_lock(&scrub->os_lock);
1316 thread_set_flags(thread, SVC_STOPPED);
1317 wake_up_all(&thread->t_ctl_waitq);
1318 spin_unlock(&scrub->os_lock);
1322 /* initial OI scrub */
1324 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1325 struct dentry *, filldir_t filldir);
1327 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1328 loff_t offset, __u64 ino, unsigned d_type);
1329 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1330 loff_t offset, __u64 ino, unsigned d_type);
1333 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1334 struct dentry *dentry, filldir_t filldir);
1336 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1337 struct dentry *dentry, filldir_t filldir);
1340 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1341 struct dentry *dentry, filldir_t filldir);
1344 OLF_SCAN_SUBITEMS = 0x0001,
1345 OLF_HIDE_FID = 0x0002,
1346 OLF_SHOW_NAME = 0x0004,
1352 struct lu_fid olm_fid;
1354 scandir_t olm_scandir;
1355 filldir_t olm_filldir;
1358 /* Add the new introduced local files in the list in the future. */
1359 static const struct osd_lf_map osd_lf_maps[] = {
1361 { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1365 { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1366 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1367 osd_ios_varfid_fill },
1369 /* NIDTBL_VERSIONS */
1370 { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1371 osd_ios_general_scan, osd_ios_varfid_fill },
1374 { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1377 { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1378 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1380 /* changelog_catalog */
1381 { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1383 /* changelog_users */
1384 { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1387 { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1391 { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1394 /* lfsck_bookmark */
1395 { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1398 { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1402 { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1403 OLF_SHOW_NAME, NULL, NULL },
1406 { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1407 osd_ios_general_scan, osd_ios_varfid_fill },
1410 { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1411 osd_ios_general_scan, osd_ios_varfid_fill },
1414 { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1415 OLF_SHOW_NAME, NULL, NULL },
1418 { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1419 OLF_SHOW_NAME, NULL, NULL },
1422 { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1423 OLF_SHOW_NAME, NULL, NULL },
1425 /* lfsck_namespace */
1426 { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1428 /* OBJECTS, upgrade from old device */
1429 { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1431 /* lquota_v2.user, upgrade from old device */
1432 { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1434 /* lquota_v2.group, upgrade from old device */
1435 { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1437 /* LAST_GROUP, upgrade from old device */
1438 { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1439 OLF_SHOW_NAME, NULL, NULL },
1442 { "lost+found", { 0, 0, 0 }, OLF_SCAN_SUBITEMS | OLF_NO_OI,
1443 osd_ios_general_scan, osd_ios_lf_fill },
1445 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1448 struct osd_ios_item {
1449 cfs_list_t oii_list;
1450 struct dentry *oii_dentry;
1451 scandir_t oii_scandir;
1452 filldir_t oii_filldir;
1455 struct osd_ios_filldir_buf {
1456 struct osd_thread_info *oifb_info;
1457 struct osd_device *oifb_dev;
1458 struct dentry *oifb_dentry;
1461 static inline struct dentry *
1462 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1464 struct dentry *dentry;
1466 CDEBUG(D_LFSCK, "init lookup one: parent = %.*s, name = %.*s\n",
1467 parent->d_name.len, parent->d_name.name, namelen, name);
1469 dentry = ll_lookup_one_len(name, parent, namelen);
1470 if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1472 return ERR_PTR(-ENOENT);
1479 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1480 scandir_t scandir, filldir_t filldir)
1482 struct osd_ios_item *item;
1485 OBD_ALLOC_PTR(item);
1489 CFS_INIT_LIST_HEAD(&item->oii_list);
1490 item->oii_dentry = dget(dentry);
1491 item->oii_scandir = scandir;
1492 item->oii_filldir = filldir;
1493 cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1499 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1501 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1502 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1503 * reference the inode, or fixed if it is missing or references another inode.
1506 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1507 struct inode *inode, const struct lu_fid *fid, int flags)
1509 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1510 struct osd_inode_id *id = &info->oti_id;
1511 struct osd_inode_id *id2 = &info->oti_id2;
1512 struct osd_scrub *scrub = &dev->od_scrub;
1513 struct scrub_file *sf = &scrub->os_file;
1518 CDEBUG(D_LFSCK, "init scan one: ino = %ld\n", inode->i_ino);
1520 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1521 if (rc != 0 && rc != -ENODATA)
1524 osd_id_gen(id, inode->i_ino, inode->i_generation);
1525 if (rc == -ENODATA) {
1526 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1527 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1530 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1534 if (lma->lma_compat & LMAC_NOT_IN_OI)
1537 tfid = lma->lma_self_fid;
1540 rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1545 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1546 DTO_INDEX_INSERT, true, 0);
1553 if (osd_id_eq_strict(id, id2))
1556 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1557 osd_scrub_file_reset(scrub,
1558 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1560 rc = osd_scrub_file_store(scrub);
1565 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1566 DTO_INDEX_UPDATE, true, 0);
1574 * It scans the /lost+found, and for the OST-object (with filter_fid
1575 * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1577 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1578 loff_t offset, __u64 ino, unsigned d_type)
1580 struct osd_ios_filldir_buf *fill_buf = buf;
1581 struct osd_thread_info *info = fill_buf->oifb_info;
1582 struct osd_device *dev = fill_buf->oifb_dev;
1583 struct lu_fid *fid = &info->oti_fid;
1584 struct osd_scrub *scrub = &dev->od_scrub;
1585 struct dentry *parent = fill_buf->oifb_dentry;
1586 struct dentry *child;
1587 struct inode *dir = parent->d_inode;
1588 struct inode *inode;
1592 /* skip any '.' started names */
1596 scrub->os_lf_scanned++;
1597 child = osd_ios_lookup_one_len(name, parent, namelen);
1598 if (IS_ERR(child)) {
1599 CWARN("%s: cannot lookup child '%.*s': rc = %d\n",
1600 osd_name(dev), namelen, name, (int)PTR_ERR(child));
1604 inode = child->d_inode;
1605 if (S_ISDIR(inode->i_mode)) {
1606 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1609 CWARN("%s: cannot add child '%.*s': rc = %d\n",
1610 osd_name(dev), namelen, name, rc);
1614 if (!S_ISREG(inode->i_mode))
1617 rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1618 if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1619 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1621 CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1622 "/lost+found.\n", namelen, name, PFID(fid));
1623 scrub->os_lf_repaired++;
1625 CWARN("%s: cannot rename for '%.*s' "DFID": rc = %d\n",
1626 osd_name(dev), namelen, name, PFID(fid), rc);
1630 /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1631 * visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1632 * can process them in furtuer. */
1638 scrub->os_lf_failed++;
1640 /* skip the failure to make the scanning to continue. */
1644 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1645 loff_t offset, __u64 ino, unsigned d_type)
1647 struct osd_ios_filldir_buf *fill_buf = buf;
1648 struct osd_device *dev = fill_buf->oifb_dev;
1649 struct dentry *child;
1653 /* skip any '.' started names */
1657 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1659 RETURN(PTR_ERR(child));
1661 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1663 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1664 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1665 osd_ios_varfid_fill);
1671 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1672 loff_t offset, __u64 ino, unsigned d_type)
1674 struct osd_ios_filldir_buf *fill_buf = buf;
1675 struct osd_device *dev = fill_buf->oifb_dev;
1676 const struct osd_lf_map *map;
1677 struct dentry *child;
1681 /* skip any '.' started names */
1685 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1686 if (strlen(map->olm_name) != namelen)
1689 if (strncmp(map->olm_name, name, namelen) == 0)
1693 if (map->olm_name == NULL)
1696 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1698 RETURN(PTR_ERR(child));
1700 if (!(map->olm_flags & OLF_NO_OI))
1701 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1702 &map->olm_fid, map->olm_flags);
1703 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1704 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1712 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1713 struct dentry *dentry, filldir_t filldir)
1715 struct osd_ios_filldir_buf buf = { info, dev, dentry };
1716 struct file *filp = &info->oti_it_ea.oie_file;
1717 struct inode *inode = dentry->d_inode;
1718 const struct file_operations *fops = inode->i_fop;
1722 LASSERT(filldir != NULL);
1725 filp->f_dentry = dentry;
1726 filp->f_mode = FMODE_64BITHASH;
1727 filp->f_mapping = inode->i_mapping;
1729 filp->private_data = NULL;
1731 rc = fops->readdir(filp, &buf, filldir);
1732 fops->release(inode, filp);
1738 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1739 struct dentry *dentry, filldir_t filldir)
1741 struct osd_scrub *scrub = &dev->od_scrub;
1742 struct scrub_file *sf = &scrub->os_file;
1743 struct dentry *child;
1747 /* It is existing MDT0 device. We only allow the case of object without
1748 * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1749 * can generate IGIF mode FID for the object and related OI mapping. If
1750 * it is on other MDTs, then becuase file-level backup/restore, related
1751 * OI mapping may be invalid already, we do not know which is the right
1752 * FID for the object. We only allow IGIF objects to reside on the MDT0.
1754 * XXX: For the case of object on non-MDT0 device with neither LMA nor
1755 * "fid" xattr, then something crashed. We cannot re-generate the
1756 * FID directly, instead, the OI scrub will scan the OI structure
1757 * and try to re-generate the LMA from the OI mapping. But if the
1758 * OI mapping crashed or lost also, then we have to give up under
1759 * double failure cases. */
1760 scrub->os_convert_igif = 1;
1761 child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1762 strlen(dot_lustre_name));
1763 if (IS_ERR(child)) {
1764 rc = PTR_ERR(child);
1765 if (rc == -ENOENT) {
1766 /* It is 1.8 MDT device. */
1767 if (!(sf->sf_flags & SF_UPGRADE)) {
1768 osd_scrub_file_reset(scrub,
1769 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1771 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1772 rc = osd_scrub_file_store(scrub);
1778 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1779 * so the client will get IGIF for the ".lustre" object when
1782 * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1783 * it does not know whether there are some old clients cached
1784 * the ".lustre" IGIF during the upgrading. Two choices:
1786 * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1787 * It will allow the old connected clients to access the
1788 * ".lustre" with cached IGIF. But it will cause others
1789 * on the MDT failed to check "fid_is_dot_lustre()".
1791 * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1792 * for ".lustre" in spite of whether there are some clients
1793 * cached the ".lustre" IGIF or not. It enables the check
1794 * "fid_is_dot_lustre()" on the MDT, although it will cause
1795 * that the old connected clients cannot access the ".lustre"
1796 * with the cached IGIF.
1798 * Usually, it is rare case for the old connected clients
1799 * to access the ".lustre" with cached IGIF. So we prefer
1800 * to the solution 2). */
1801 rc = osd_ios_scan_one(info, dev, child->d_inode,
1802 &LU_DOT_LUSTRE_FID, 0);
1810 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1811 struct dentry *dentry, filldir_t filldir)
1813 struct osd_scrub *scrub = &dev->od_scrub;
1814 struct scrub_file *sf = &scrub->os_file;
1815 struct dentry *child;
1819 if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
1820 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1821 rc = osd_scrub_file_store(scrub);
1826 child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1827 if (!IS_ERR(child)) {
1828 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1831 rc = PTR_ERR(child);
1834 if (rc != 0 && rc != -ENOENT)
1837 child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1838 if (!IS_ERR(child)) {
1839 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1842 rc = PTR_ERR(child);
1851 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1852 struct osd_device *dev)
1854 struct osd_ios_item *item = NULL;
1855 scandir_t scandir = osd_ios_general_scan;
1856 filldir_t filldir = osd_ios_root_fill;
1857 struct dentry *dentry = osd_sb(dev)->s_root;
1858 const struct osd_lf_map *map = osd_lf_maps;
1862 /* Lookup IGIF in OI by force for initial OI scrub. */
1863 dev->od_igif_inoi = 1;
1866 rc = scandir(info, dev, dentry, filldir);
1868 dput(item->oii_dentry);
1875 if (cfs_list_empty(&dev->od_ios_list))
1878 item = cfs_list_entry(dev->od_ios_list.next,
1879 struct osd_ios_item, oii_list);
1880 cfs_list_del_init(&item->oii_list);
1882 LASSERT(item->oii_scandir != NULL);
1883 scandir = item->oii_scandir;
1884 filldir = item->oii_filldir;
1885 dentry = item->oii_dentry;
1888 while (!cfs_list_empty(&dev->od_ios_list)) {
1889 item = cfs_list_entry(dev->od_ios_list.next,
1890 struct osd_ios_item, oii_list);
1891 cfs_list_del_init(&item->oii_list);
1892 dput(item->oii_dentry);
1899 /* There maybe the case that the object has been removed, but its OI
1900 * mapping is still in the OI file, such as the "CATALOGS" after MDT
1901 * file-level backup/restore. So here cleanup the stale OI mappings. */
1902 while (map->olm_name != NULL) {
1903 struct dentry *child;
1905 if (fid_is_zero(&map->olm_fid)) {
1910 child = osd_ios_lookup_one_len(map->olm_name,
1911 osd_sb(dev)->s_root,
1912 strlen(map->olm_name));
1915 else if (PTR_ERR(child) == -ENOENT)
1916 osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1917 NULL, DTO_INDEX_DELETE,
1925 char *osd_lf_fid2name(const struct lu_fid *fid)
1927 const struct osd_lf_map *map = osd_lf_maps;
1929 while (map->olm_name != NULL) {
1930 if (!lu_fid_eq(fid, &map->olm_fid)) {
1935 if (map->olm_flags & OLF_SHOW_NAME)
1936 return map->olm_name;
1944 /* OI scrub start/stop */
1946 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1948 struct osd_scrub *scrub = &dev->od_scrub;
1949 struct ptlrpc_thread *thread = &scrub->os_thread;
1950 struct l_wait_info lwi = { 0 };
1955 /* os_lock: sync status between stop and scrub thread */
1956 spin_lock(&scrub->os_lock);
1957 if (thread_is_running(thread)) {
1958 spin_unlock(&scrub->os_lock);
1960 } else if (unlikely(thread_is_stopping(thread))) {
1961 spin_unlock(&scrub->os_lock);
1962 l_wait_event(thread->t_ctl_waitq,
1963 thread_is_stopped(thread),
1967 spin_unlock(&scrub->os_lock);
1969 if (scrub->os_file.sf_status == SS_COMPLETED)
1972 scrub->os_start_flags = flags;
1973 thread_set_flags(thread, 0);
1974 rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
1975 if (IS_ERR_VALUE(rc)) {
1976 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1977 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1981 l_wait_event(thread->t_ctl_waitq,
1982 thread_is_running(thread) || thread_is_stopped(thread),
1988 int osd_scrub_start(struct osd_device *dev)
1993 /* od_otable_mutex: prevent curcurrent start/stop */
1994 mutex_lock(&dev->od_otable_mutex);
1995 rc = do_osd_scrub_start(dev, SS_AUTO);
1996 mutex_unlock(&dev->od_otable_mutex);
1998 RETURN(rc == -EALREADY ? 0 : rc);
2001 static void do_osd_scrub_stop(struct osd_scrub *scrub)
2003 struct ptlrpc_thread *thread = &scrub->os_thread;
2004 struct l_wait_info lwi = { 0 };
2006 /* os_lock: sync status between stop and scrub thread */
2007 spin_lock(&scrub->os_lock);
2008 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2009 thread_set_flags(thread, SVC_STOPPING);
2010 spin_unlock(&scrub->os_lock);
2011 wake_up_all(&thread->t_ctl_waitq);
2012 l_wait_event(thread->t_ctl_waitq,
2013 thread_is_stopped(thread),
2015 /* Do not skip the last lock/unlock, which can guarantee that
2016 * the caller cannot return until the OI scrub thread exit. */
2017 spin_lock(&scrub->os_lock);
2019 spin_unlock(&scrub->os_lock);
2022 static void osd_scrub_stop(struct osd_device *dev)
2024 /* od_otable_mutex: prevent curcurrent start/stop */
2025 mutex_lock(&dev->od_otable_mutex);
2026 dev->od_scrub.os_paused = 1;
2027 do_osd_scrub_stop(&dev->od_scrub);
2028 mutex_unlock(&dev->od_otable_mutex);
2031 /* OI scrub setup/cleanup */
2033 static const char osd_scrub_name[] = "OI_scrub";
2035 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
2037 struct osd_thread_info *info = osd_oti_get(env);
2038 struct osd_scrub *scrub = &dev->od_scrub;
2039 struct lvfs_run_ctxt *ctxt = &scrub->os_ctxt;
2040 struct scrub_file *sf = &scrub->os_file;
2041 struct super_block *sb = osd_sb(dev);
2042 struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
2043 struct lvfs_run_ctxt saved;
2045 struct inode *inode;
2046 struct lu_fid *fid = &info->oti_fid;
2051 memset(scrub, 0, sizeof(*scrub));
2052 OBD_SET_CTXT_MAGIC(ctxt);
2053 ctxt->pwdmnt = dev->od_mnt;
2054 ctxt->pwd = dev->od_mnt->mnt_root;
2055 ctxt->fs = get_ds();
2057 init_waitqueue_head(&scrub->os_thread.t_ctl_waitq);
2058 init_rwsem(&scrub->os_rwsem);
2059 spin_lock_init(&scrub->os_lock);
2060 CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2062 push_ctxt(&saved, ctxt, NULL);
2063 filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
2065 pop_ctxt(&saved, ctxt, NULL);
2066 RETURN(PTR_ERR(filp));
2069 inode = filp->f_dentry->d_inode;
2070 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
2071 /* 'What the @fid is' is not imporatant, because the object
2072 * has no OI mapping, and only is visible inside the OSD.*/
2073 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2074 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2076 filp_close(filp, 0);
2077 pop_ctxt(&saved, ctxt, NULL);
2081 scrub->os_inode = igrab(inode);
2082 filp_close(filp, 0);
2083 pop_ctxt(&saved, ctxt, NULL);
2085 rc = osd_scrub_file_load(scrub);
2086 if (rc == -ENOENT) {
2087 osd_scrub_file_init(scrub, es->s_uuid);
2088 /* If the "/O" dir does not exist when mount (indicated by
2089 * osd_device::od_maybe_new), neither for the "/OI_scrub",
2090 * then it is quite probably that the device is a new one,
2091 * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2093 * For the rare case that "/O" and "OI_scrub" both lost on
2094 * an old device, it can be found and cleared later.
2096 * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2097 * need to check "filter_fid_old" and to convert it to
2098 * "filter_fid" for each object, and all the IGIF should
2099 * have their FID mapping in OI files already. */
2100 if (dev->od_maybe_new)
2101 sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2103 } else if (rc != 0) {
2106 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
2107 osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
2109 } else if (sf->sf_status == SS_SCANNING) {
2110 sf->sf_status = SS_CRASHED;
2115 if (sf->sf_pos_last_checkpoint != 0)
2116 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2118 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2121 rc = osd_scrub_file_store(scrub);
2126 /* Initialize OI files. */
2127 rc = osd_oi_init(info, dev);
2131 rc = osd_initial_OI_scrub(info, dev);
2133 if (sf->sf_flags & SF_UPGRADE ||
2134 !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2135 sf->sf_success_count > 0)) {
2136 dev->od_igif_inoi = 0;
2137 dev->od_check_ff = dev->od_is_ost;
2139 dev->od_igif_inoi = 1;
2140 dev->od_check_ff = 0;
2143 if (sf->sf_flags & SF_INCONSISTENT)
2144 /* The 'od_igif_inoi' will be set under the
2146 * 1) new created system, or
2147 * 2) restored from file-level backup, or
2148 * 3) the upgrading completed.
2150 * The 'od_igif_inoi' may be cleared by OI scrub
2151 * later if found that the system is upgrading. */
2152 dev->od_igif_inoi = 1;
2154 if (!dev->od_noscrub &&
2155 ((sf->sf_status == SS_PAUSED) ||
2156 (sf->sf_status == SS_CRASHED &&
2157 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2158 SF_UPGRADE | SF_AUTO)) ||
2159 (sf->sf_status == SS_INIT &&
2160 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2162 rc = osd_scrub_start(dev);
2165 /* it is possible that dcache entries may keep objects after they are
2166 * deleted by OSD. While it looks safe this can cause object data to
2167 * stay until umount causing failures in tests calculating free space,
2168 * e.g. replay-ost-single. Since those dcache entries are not used
2169 * anymore let's just free them after use here */
2170 shrink_dcache_sb(sb);
2175 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2177 struct osd_scrub *scrub = &dev->od_scrub;
2179 LASSERT(dev->od_otable_it == NULL);
2181 if (scrub->os_inode != NULL) {
2182 osd_scrub_stop(dev);
2183 iput(scrub->os_inode);
2184 scrub->os_inode = NULL;
2186 if (dev->od_oi_table != NULL)
2187 osd_oi_fini(osd_oti_get(env), dev);
2190 /* object table based iteration APIs */
2192 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2193 struct dt_object *dt, __u32 attr,
2194 struct lustre_capa *capa)
2196 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2197 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2198 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
2199 struct osd_scrub *scrub = &dev->od_scrub;
2200 struct osd_otable_it *it;
2205 /* od_otable_mutex: prevent curcurrent init/fini */
2206 mutex_lock(&dev->od_otable_mutex);
2207 if (dev->od_otable_it != NULL)
2208 GOTO(out, it = ERR_PTR(-EALREADY));
2212 GOTO(out, it = ERR_PTR(-ENOMEM));
2214 dev->od_otable_it = it;
2216 it->ooi_cache.ooc_consumer_idx = -1;
2217 if (flags & DOIF_OUTUSED)
2218 it->ooi_used_outside = 1;
2220 if (flags & DOIF_RESET)
2223 if (valid & DOIV_ERROR_HANDLE) {
2224 if (flags & DOIF_FAILOUT)
2225 start |= SS_SET_FAILOUT;
2227 start |= SS_CLEAR_FAILOUT;
2230 if (valid & DOIV_DRYRUN) {
2231 if (flags & DOIF_DRYRUN)
2232 start |= SS_SET_DRYRUN;
2234 start |= SS_CLEAR_DRYRUN;
2237 rc = do_osd_scrub_start(dev, start);
2238 if (rc < 0 && rc != -EALREADY) {
2239 dev->od_otable_it = NULL;
2241 GOTO(out, it = ERR_PTR(rc));
2244 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2249 mutex_unlock(&dev->od_otable_mutex);
2250 return (struct dt_it *)it;
2253 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2255 struct osd_otable_it *it = (struct osd_otable_it *)di;
2256 struct osd_device *dev = it->ooi_dev;
2258 /* od_otable_mutex: prevent curcurrent init/fini */
2259 mutex_lock(&dev->od_otable_mutex);
2260 do_osd_scrub_stop(&dev->od_scrub);
2261 LASSERT(dev->od_otable_it == it);
2263 dev->od_otable_it = NULL;
2264 mutex_unlock(&dev->od_otable_mutex);
2268 static int osd_otable_it_get(const struct lu_env *env,
2269 struct dt_it *di, const struct dt_key *key)
2274 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2279 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2281 spin_lock(&scrub->os_lock);
2282 if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2283 scrub->os_waiting ||
2284 !thread_is_running(&scrub->os_thread))
2285 it->ooi_waiting = 0;
2287 it->ooi_waiting = 1;
2288 spin_unlock(&scrub->os_lock);
2290 return !it->ooi_waiting;
2293 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2295 struct osd_otable_it *it = (struct osd_otable_it *)di;
2296 struct osd_device *dev = it->ooi_dev;
2297 struct osd_scrub *scrub = &dev->od_scrub;
2298 struct osd_otable_cache *ooc = &it->ooi_cache;
2299 struct ptlrpc_thread *thread = &scrub->os_thread;
2300 struct l_wait_info lwi = { 0 };
2304 LASSERT(it->ooi_user_ready);
2307 if (!thread_is_running(thread) && !it->ooi_used_outside)
2310 if (ooc->ooc_cached_items > 0) {
2311 ooc->ooc_cached_items--;
2312 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2313 ~OSD_OTABLE_IT_CACHE_MASK;
2317 if (it->ooi_all_cached) {
2318 l_wait_event(thread->t_ctl_waitq,
2319 !thread_is_running(thread),
2324 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2325 spin_lock(&scrub->os_lock);
2326 scrub->os_waiting = 0;
2327 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2328 spin_unlock(&scrub->os_lock);
2331 if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2332 l_wait_event(thread->t_ctl_waitq,
2333 osd_otable_it_wakeup(scrub, it),
2336 if (!thread_is_running(thread) && !it->ooi_used_outside)
2339 rc = osd_otable_it_preload(env, it);
2346 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2347 const struct dt_it *di)
2352 static int osd_otable_it_key_size(const struct lu_env *env,
2353 const struct dt_it *di)
2355 return sizeof(__u64);
2358 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2359 struct dt_rec *rec, __u32 attr)
2361 struct osd_otable_it *it = (struct osd_otable_it *)di;
2362 struct osd_otable_cache *ooc = &it->ooi_cache;
2364 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2366 /* Filter out Invald FID already. */
2367 LASSERTF(fid_is_sane((struct lu_fid *)rec),
2368 "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2369 PFID((struct lu_fid *)rec),
2370 ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2375 static __u64 osd_otable_it_store(const struct lu_env *env,
2376 const struct dt_it *di)
2378 struct osd_otable_it *it = (struct osd_otable_it *)di;
2379 struct osd_otable_cache *ooc = &it->ooi_cache;
2382 if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2383 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2385 hash = ooc->ooc_pos_preload;
2390 * Set the OSD layer iteration start position as the specified hash.
2392 static int osd_otable_it_load(const struct lu_env *env,
2393 const struct dt_it *di, __u64 hash)
2395 struct osd_otable_it *it = (struct osd_otable_it *)di;
2396 struct osd_device *dev = it->ooi_dev;
2397 struct osd_otable_cache *ooc = &it->ooi_cache;
2398 struct osd_scrub *scrub = &dev->od_scrub;
2402 /* Forbid to set iteration position after iteration started. */
2403 if (it->ooi_user_ready)
2406 if (hash > OSD_OTABLE_MAX_HASH)
2407 hash = OSD_OTABLE_MAX_HASH;
2409 ooc->ooc_pos_preload = hash;
2410 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2411 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2413 it->ooi_user_ready = 1;
2414 if (!scrub->os_full_speed)
2415 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2417 /* Unplug OSD layer iteration by the first next() call. */
2418 rc = osd_otable_it_next(env, (struct dt_it *)it);
2423 static int osd_otable_it_key_rec(const struct lu_env *env,
2424 const struct dt_it *di, void *key_rec)
2429 const struct dt_index_operations osd_otable_ops = {
2431 .init = osd_otable_it_init,
2432 .fini = osd_otable_it_fini,
2433 .get = osd_otable_it_get,
2434 .put = osd_otable_it_put,
2435 .next = osd_otable_it_next,
2436 .key = osd_otable_it_key,
2437 .key_size = osd_otable_it_key_size,
2438 .rec = osd_otable_it_rec,
2439 .store = osd_otable_it_store,
2440 .load = osd_otable_it_load,
2441 .key_rec = osd_otable_it_key_rec,
2445 /* high priority inconsistent items list APIs */
2447 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2450 struct osd_inconsistent_item *oii;
2451 struct osd_scrub *scrub = &dev->od_scrub;
2452 struct ptlrpc_thread *thread = &scrub->os_thread;
2457 if (unlikely(oii == NULL))
2460 CFS_INIT_LIST_HEAD(&oii->oii_list);
2461 oii->oii_cache = *oic;
2462 oii->oii_insert = insert;
2464 spin_lock(&scrub->os_lock);
2465 if (unlikely(!thread_is_running(thread))) {
2466 spin_unlock(&scrub->os_lock);
2471 if (cfs_list_empty(&scrub->os_inconsistent_items))
2473 cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2474 spin_unlock(&scrub->os_lock);
2477 wake_up_all(&thread->t_ctl_waitq);
2482 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2483 struct osd_inode_id *id)
2485 struct osd_scrub *scrub = &dev->od_scrub;
2486 struct osd_inconsistent_item *oii;
2489 spin_lock(&scrub->os_lock);
2490 cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2491 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2492 *id = oii->oii_cache.oic_lid;
2493 spin_unlock(&scrub->os_lock);
2497 spin_unlock(&scrub->os_lock);
2504 static const char *scrub_status_names[] = {
2515 static const char *scrub_flags_names[] = {
2523 static const char *scrub_param_names[] = {
2529 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2537 rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2543 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2546 rc = snprintf(*buf, *len, "%s%c", names[i],
2547 bits != 0 ? ',' : '\n');
2558 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2563 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2564 cfs_time_current_sec() - time);
2566 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2575 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2580 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2582 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2591 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2593 struct osd_scrub *scrub = &dev->od_scrub;
2594 struct scrub_file *sf = &scrub->os_file;
2601 down_read(&scrub->os_rwsem);
2602 rc = snprintf(buf, len,
2607 sf->sf_magic, (int)sf->sf_oi_count,
2608 scrub_status_names[sf->sf_status]);
2614 rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2619 rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2624 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2625 "time_since_last_completed");
2629 rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2630 "time_since_latest_start");
2634 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2635 "time_since_last_checkpoint");
2639 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2640 "latest_start_position");
2644 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2645 "last_checkpoint_position");
2649 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2650 "first_failure_position");
2654 checked = sf->sf_items_checked + scrub->os_new_checked;
2655 rc = snprintf(buf, len,
2656 "checked: "LPU64"\n"
2657 "updated: "LPU64"\n"
2659 "prior_updated: "LPU64"\n"
2660 "noscrub: "LPU64"\n"
2662 "success_count: %u\n",
2663 checked, sf->sf_items_updated, sf->sf_items_failed,
2664 sf->sf_items_updated_prior, sf->sf_items_noscrub,
2665 sf->sf_items_igif, sf->sf_success_count);
2672 if (thread_is_running(&scrub->os_thread)) {
2673 cfs_duration_t duration = cfs_time_current() -
2674 scrub->os_time_last_checkpoint;
2675 __u64 new_checked = scrub->os_new_checked * HZ;
2676 __u32 rtime = sf->sf_run_time +
2677 cfs_duration_sec(duration + HALF_SEC);
2680 do_div(new_checked, duration);
2682 do_div(speed, rtime);
2683 rc = snprintf(buf, len,
2684 "run_time: %u seconds\n"
2685 "average_speed: "LPU64" objects/sec\n"
2686 "real-time_speed: "LPU64" objects/sec\n"
2687 "current_position: %u\n"
2688 "lf_scanned: "LPU64"\n"
2689 "lf_reparied: "LPU64"\n"
2690 "lf_failed: "LPU64"\n",
2691 rtime, speed, new_checked, scrub->os_pos_current,
2692 scrub->os_lf_scanned, scrub->os_lf_repaired,
2693 scrub->os_lf_failed);
2695 if (sf->sf_run_time != 0)
2696 do_div(speed, sf->sf_run_time);
2697 rc = snprintf(buf, len,
2698 "run_time: %u seconds\n"
2699 "average_speed: "LPU64" objects/sec\n"
2700 "real-time_speed: N/A\n"
2701 "current_position: N/A\n"
2702 "lf_scanned: "LPU64"\n"
2703 "lf_reparied: "LPU64"\n"
2704 "lf_failed: "LPU64"\n",
2705 sf->sf_run_time, speed, scrub->os_lf_scanned,
2706 scrub->os_lf_repaired, scrub->os_lf_failed);
2716 up_read(&scrub->os_rwsem);