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,
94 int ops, enum oi_check_flags flags)
96 const struct lu_env *env = info->oti_env;
98 struct osd_thandle *oh;
102 th = dt_trans_create(env, &dev->od_dt_dev);
106 oh = container_of0(th, struct osd_thandle, ot_super);
107 LASSERT(oh->ot_handle == NULL);
110 case DTO_INDEX_UPDATE:
111 osd_trans_declare_op(env, oh, OSD_OT_UPDATE,
112 osd_dto_credits_noquota[DTO_INDEX_UPDATE]);
113 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
117 rc = osd_oi_update(info, dev, fid, id, th, flags);
118 if (unlikely(rc == -ENOENT)) {
119 /* Some unlink thread may removed the OI mapping. */
123 case DTO_INDEX_INSERT:
124 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
125 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
126 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
130 rc = osd_oi_insert(info, dev, fid, id, th, flags);
131 if (unlikely(rc == -EEXIST)) {
133 /* XXX: There are trouble things when adding OI
134 * mapping for IGIF object, which may cause
135 * multiple objects to be mapped to the same
136 * IGIF formatted FID. Consider the following
139 * 1) The MDT is upgrading from 1.8 device.
140 * The OI scrub generates IGIF FID1 for the
141 * OBJ1 and adds the OI mapping.
143 * 2) For some reason, the OI scrub does not
144 * process all the IGIF objects completely.
146 * 3) The MDT is backuped and restored against
149 * 4) When the MDT mounts up, the OI scrub will
150 * try to rebuild the OI files. For some IGIF
151 * object, OBJ2, which was not processed by the
152 * OI scrub before the backup/restore, and the
153 * new generated IGIF formatted FID may be just
154 * the FID1, the same as OBJ1.
156 * Under such case, the OI scrub cannot know how
157 * to generate new FID for the OBJ2.
159 * Currently, we do nothing for that. One possible
160 * solution is to generate new normal FID for the
163 * Anyway, it is rare, only exists in theory. */
166 case DTO_INDEX_DELETE:
167 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
168 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
169 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
173 rc = osd_oi_delete(info, dev, fid, th, flags);
175 /* It is normal that the unlink thread has removed the
176 * OI mapping already. */
181 LASSERTF(0, "Unexpected ops %d\n", ops);
188 dt_trans_stop(env, &dev->od_dt_dev, th);
192 /* OI_scrub file ops */
194 static void osd_scrub_file_to_cpu(struct scrub_file *des,
195 struct scrub_file *src)
197 memcpy(des->sf_uuid, src->sf_uuid, 16);
198 des->sf_flags = le64_to_cpu(src->sf_flags);
199 des->sf_magic = le32_to_cpu(src->sf_magic);
200 des->sf_status = le16_to_cpu(src->sf_status);
201 des->sf_param = le16_to_cpu(src->sf_param);
202 des->sf_time_last_complete =
203 le64_to_cpu(src->sf_time_last_complete);
204 des->sf_time_latest_start =
205 le64_to_cpu(src->sf_time_latest_start);
206 des->sf_time_last_checkpoint =
207 le64_to_cpu(src->sf_time_last_checkpoint);
208 des->sf_pos_latest_start =
209 le64_to_cpu(src->sf_pos_latest_start);
210 des->sf_pos_last_checkpoint =
211 le64_to_cpu(src->sf_pos_last_checkpoint);
212 des->sf_pos_first_inconsistent =
213 le64_to_cpu(src->sf_pos_first_inconsistent);
214 des->sf_items_checked =
215 le64_to_cpu(src->sf_items_checked);
216 des->sf_items_updated =
217 le64_to_cpu(src->sf_items_updated);
218 des->sf_items_failed =
219 le64_to_cpu(src->sf_items_failed);
220 des->sf_items_updated_prior =
221 le64_to_cpu(src->sf_items_updated_prior);
222 des->sf_run_time = le32_to_cpu(src->sf_run_time);
223 des->sf_success_count = le32_to_cpu(src->sf_success_count);
224 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
225 des->sf_internal_flags = le16_to_cpu(src->sf_internal_flags);
226 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
229 static void osd_scrub_file_to_le(struct scrub_file *des,
230 struct scrub_file *src)
232 memcpy(des->sf_uuid, src->sf_uuid, 16);
233 des->sf_flags = cpu_to_le64(src->sf_flags);
234 des->sf_magic = cpu_to_le32(src->sf_magic);
235 des->sf_status = cpu_to_le16(src->sf_status);
236 des->sf_param = cpu_to_le16(src->sf_param);
237 des->sf_time_last_complete =
238 cpu_to_le64(src->sf_time_last_complete);
239 des->sf_time_latest_start =
240 cpu_to_le64(src->sf_time_latest_start);
241 des->sf_time_last_checkpoint =
242 cpu_to_le64(src->sf_time_last_checkpoint);
243 des->sf_pos_latest_start =
244 cpu_to_le64(src->sf_pos_latest_start);
245 des->sf_pos_last_checkpoint =
246 cpu_to_le64(src->sf_pos_last_checkpoint);
247 des->sf_pos_first_inconsistent =
248 cpu_to_le64(src->sf_pos_first_inconsistent);
249 des->sf_items_checked =
250 cpu_to_le64(src->sf_items_checked);
251 des->sf_items_updated =
252 cpu_to_le64(src->sf_items_updated);
253 des->sf_items_failed =
254 cpu_to_le64(src->sf_items_failed);
255 des->sf_items_updated_prior =
256 cpu_to_le64(src->sf_items_updated_prior);
257 des->sf_run_time = cpu_to_le32(src->sf_run_time);
258 des->sf_success_count = cpu_to_le32(src->sf_success_count);
259 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
260 des->sf_internal_flags = cpu_to_le16(src->sf_internal_flags);
261 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
264 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
266 struct scrub_file *sf = &scrub->os_file;
268 memset(sf, 0, sizeof(*sf));
269 memcpy(sf->sf_uuid, uuid, 16);
270 sf->sf_magic = SCRUB_MAGIC_V1;
271 sf->sf_status = SS_INIT;
274 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
276 struct scrub_file *sf = &scrub->os_file;
278 CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
279 memcpy(sf->sf_uuid, uuid, 16);
280 sf->sf_status = SS_INIT;
281 sf->sf_flags |= flags;
284 sf->sf_time_latest_start = 0;
285 sf->sf_time_last_checkpoint = 0;
286 sf->sf_pos_latest_start = 0;
287 sf->sf_pos_last_checkpoint = 0;
288 sf->sf_pos_first_inconsistent = 0;
289 sf->sf_items_checked = 0;
290 sf->sf_items_updated = 0;
291 sf->sf_items_failed = 0;
292 sf->sf_items_updated_prior = 0;
293 sf->sf_items_noscrub = 0;
294 sf->sf_items_igif = 0;
297 static int osd_scrub_file_load(struct osd_scrub *scrub)
300 char *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
301 int len = sizeof(scrub->os_file_disk);
304 rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
306 struct scrub_file *sf = &scrub->os_file;
308 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
309 if (sf->sf_magic != SCRUB_MAGIC_V1) {
310 CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
311 name, sf->sf_magic, SCRUB_MAGIC_V1);
312 /* Process it as new scrub file. */
317 } else if (rc != 0) {
318 CERROR("%.16s: fail to load scrub file, expected = %d, "
319 "rc = %d\n", name, len, rc);
323 /* return -ENOENT for empty scrub file case. */
330 int osd_scrub_file_store(struct osd_scrub *scrub)
332 struct osd_device *dev;
335 int len = sizeof(scrub->os_file_disk);
339 dev = container_of0(scrub, struct osd_device, od_scrub);
340 credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
341 osd_dto_credits_noquota[DTO_WRITE_BLOCK];
342 jh = ldiskfs_journal_start_sb(osd_sb(dev), credits);
345 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
346 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
350 osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
351 rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
353 ldiskfs_journal_stop(jh);
355 CERROR("%.16s: fail to store scrub file, expected = %d, "
357 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
359 scrub->os_time_last_checkpoint = cfs_time_current();
360 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
361 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
367 static int osd_scrub_prep(struct osd_device *dev)
369 struct osd_scrub *scrub = &dev->od_scrub;
370 struct ptlrpc_thread *thread = &scrub->os_thread;
371 struct scrub_file *sf = &scrub->os_file;
372 __u32 flags = scrub->os_start_flags;
376 down_write(&scrub->os_rwsem);
377 if (flags & SS_SET_FAILOUT)
378 sf->sf_param |= SP_FAILOUT;
380 if (flags & SS_CLEAR_FAILOUT)
381 sf->sf_param &= ~SP_FAILOUT;
383 if (flags & SS_RESET)
384 osd_scrub_file_reset(scrub,
385 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
387 if (flags & SS_AUTO) {
388 scrub->os_full_speed = 1;
389 sf->sf_flags |= SF_AUTO;
391 scrub->os_full_speed = 0;
394 if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
395 scrub->os_full_speed = 1;
397 scrub->os_in_prior = 0;
398 scrub->os_waiting = 0;
399 scrub->os_paused = 0;
400 scrub->os_new_checked = 0;
401 if (sf->sf_pos_last_checkpoint != 0)
402 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
404 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
406 scrub->os_pos_current = sf->sf_pos_latest_start;
407 sf->sf_status = SS_SCANNING;
408 sf->sf_time_latest_start = cfs_time_current_sec();
409 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
410 rc = osd_scrub_file_store(scrub);
412 spin_lock(&scrub->os_lock);
413 thread_set_flags(thread, SVC_RUNNING);
414 spin_unlock(&scrub->os_lock);
415 cfs_waitq_broadcast(&thread->t_ctl_waitq);
417 up_write(&scrub->os_rwsem);
423 osd_scrub_convert_ff(struct osd_thread_info *info, struct osd_device *dev,
424 struct inode *inode, const struct lu_fid *fid)
426 struct filter_fid_old *ff = &info->oti_ff;
427 struct dentry *dentry = &info->oti_obj_dentry;
431 bool removed = false;
435 /* We want the LMA to fit into the 256-byte OST inode, so operate
437 * 1) read old XATTR_NAME_FID and save the parent FID;
438 * 2) delete the old XATTR_NAME_FID;
439 * 3) make new LMA and add it;
440 * 4) generate new XATTR_NAME_FID with the saved parent FID and add it.
442 * Making the LMA to fit into the 256-byte OST inode can save time for
443 * normal osd_check_lma() and for other OI scrub scanning in future.
444 * So it is worth to make some slow conversion here. */
445 jh = ldiskfs_journal_start_sb(osd_sb(dev),
446 osd_dto_credits_noquota[DTO_XATTR_SET] * 3);
449 CERROR("%s: fail to start trans for convert ff: "DFID
451 osd_name(dev), PFID(fid), rc);
455 /* 1) read old XATTR_NAME_FID and save the parent FID */
456 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
457 if (rc == sizeof(*ff)) {
458 /* 2) delete the old XATTR_NAME_FID */
459 ll_vfs_dq_init(inode);
460 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
465 } else if (unlikely(rc == -ENODATA)) {
467 } else if (rc != sizeof(struct filter_fid)) {
468 GOTO(stop, rc = -EINVAL);
471 /* 3) make new LMA and add it */
472 rc = osd_ea_fid_set(info, inode, fid, LMAC_FID_ON_OST, 0);
473 if (rc == 0 && reset)
474 size = sizeof(struct filter_fid);
475 else if (rc != 0 && removed)
476 /* If failed, we should try to add the old back. */
477 size = sizeof(struct filter_fid_old);
479 /* 4) generate new XATTR_NAME_FID with the saved parent FID and add it*/
483 rc1 = __osd_xattr_set(info, inode, XATTR_NAME_FID, ff, size,
485 if (rc1 != 0 && rc != 0)
492 ldiskfs_journal_stop(jh);
497 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
498 struct osd_idmap_cache *oic, int val)
500 struct osd_scrub *scrub = &dev->od_scrub;
501 struct scrub_file *sf = &scrub->os_file;
502 struct lu_fid *fid = &oic->oic_fid;
503 struct osd_inode_id *lid = &oic->oic_lid;
504 struct osd_inode_id *lid2 = &info->oti_id;
505 struct osd_inconsistent_item *oii = NULL;
506 struct inode *inode = NULL;
507 int ops = DTO_INDEX_UPDATE;
510 bool converted = false;
513 down_write(&scrub->os_rwsem);
514 scrub->os_new_checked++;
518 if (scrub->os_in_prior)
519 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
522 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
525 if (fid_is_igif(fid))
528 if (val == SCRUB_NEXT_OSTOBJ_OLD) {
529 inode = osd_iget(info, dev, lid);
532 /* Someone removed the inode. */
533 if (rc == -ENOENT || rc == -ESTALE)
538 sf->sf_flags |= SF_UPGRADE;
539 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
540 dev->od_check_ff = 1;
541 rc = osd_scrub_convert_ff(info, dev, inode, fid);
542 rc = osd_ea_fid_set(info, inode, fid,
550 if ((val == SCRUB_NEXT_NOLMA) &&
551 (!scrub->os_convert_igif || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
554 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
557 rc = osd_oi_lookup(info, dev, fid, lid2,
558 (val == SCRUB_NEXT_OSTOBJ ||
559 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
566 inode = osd_iget(info, dev, lid);
569 /* Someone removed the inode. */
570 if (rc == -ENOENT || rc == -ESTALE)
576 scrub->os_full_speed = 1;
577 ops = DTO_INDEX_INSERT;
578 idx = osd_oi_fid2idx(dev, fid);
580 case SCRUB_NEXT_NOLMA:
581 sf->sf_flags |= SF_UPGRADE;
582 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
586 if (!(sf->sf_flags & SF_INCONSISTENT))
587 dev->od_igif_inoi = 0;
589 case SCRUB_NEXT_OSTOBJ:
590 sf->sf_flags |= SF_INCONSISTENT;
591 case SCRUB_NEXT_OSTOBJ_OLD:
594 sf->sf_flags |= SF_RECREATED;
595 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
596 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
599 } else if (osd_id_eq(lid, lid2)) {
601 sf->sf_items_updated++;
605 scrub->os_full_speed = 1;
606 sf->sf_flags |= SF_INCONSISTENT;
608 /* XXX: If the device is restored from file-level backup, then
609 * some IGIFs may have been already in OI files, and some
610 * may be not yet. Means upgrading from 1.8 may be partly
611 * processed, but some clients may hold some immobilized
612 * IGIFs, and use them to access related objects. Under
613 * such case, OSD does not know whether an given IGIF has
614 * been processed or to be processed, and it also cannot
615 * generate local ino#/gen# directly from the immobilized
616 * IGIF because of the backup/restore. Then force OSD to
617 * lookup the given IGIF in OI files, and if no entry,
618 * then ask the client to retry after upgrading completed.
619 * No better choice. */
620 dev->od_igif_inoi = 1;
623 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops,
624 (val == SCRUB_NEXT_OSTOBJ ||
625 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
627 if (scrub->os_in_prior)
628 sf->sf_items_updated_prior++;
630 sf->sf_items_updated++;
632 /* The target has been changed, need to be re-loaded. */
633 lu_object_purge(info->oti_env, osd2lu_dev(dev), fid);
640 sf->sf_items_failed++;
641 if (sf->sf_pos_first_inconsistent == 0 ||
642 sf->sf_pos_first_inconsistent > lid->oii_ino)
643 sf->sf_pos_first_inconsistent = lid->oii_ino;
648 /* There may be conflict unlink during the OI scrub,
649 * if happend, then remove the new added OI mapping. */
650 if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
651 unlikely(inode->i_nlink == 0))
652 osd_scrub_refresh_mapping(info, dev, fid, lid,
654 (val == SCRUB_NEXT_OSTOBJ ||
655 val == SCRUB_NEXT_OSTOBJ_OLD) ?
656 OI_KNOWN_ON_OST : 0);
657 up_write(&scrub->os_rwsem);
659 if (inode != NULL && !IS_ERR(inode))
663 LASSERT(!cfs_list_empty(&oii->oii_list));
665 spin_lock(&scrub->os_lock);
666 cfs_list_del_init(&oii->oii_list);
667 spin_unlock(&scrub->os_lock);
670 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
673 static int osd_scrub_checkpoint(struct osd_scrub *scrub)
675 struct scrub_file *sf = &scrub->os_file;
678 if (likely(cfs_time_before(cfs_time_current(),
679 scrub->os_time_next_checkpoint) ||
680 scrub->os_new_checked == 0))
683 down_write(&scrub->os_rwsem);
684 sf->sf_items_checked += scrub->os_new_checked;
685 scrub->os_new_checked = 0;
686 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
687 sf->sf_time_last_checkpoint = cfs_time_current_sec();
688 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
689 scrub->os_time_last_checkpoint);
690 rc = osd_scrub_file_store(scrub);
691 up_write(&scrub->os_rwsem);
696 static void osd_scrub_post(struct osd_scrub *scrub, int result)
698 struct scrub_file *sf = &scrub->os_file;
701 down_write(&scrub->os_rwsem);
702 spin_lock(&scrub->os_lock);
703 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
704 spin_unlock(&scrub->os_lock);
705 if (scrub->os_new_checked > 0) {
706 sf->sf_items_checked += scrub->os_new_checked;
707 scrub->os_new_checked = 0;
708 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
710 sf->sf_time_last_checkpoint = cfs_time_current_sec();
712 struct osd_device *dev =
713 container_of0(scrub, struct osd_device, od_scrub);
715 dev->od_igif_inoi = 1;
716 dev->od_check_ff = 0;
717 sf->sf_status = SS_COMPLETED;
718 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
719 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
720 SF_UPGRADE | SF_AUTO);
721 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
722 sf->sf_success_count++;
723 } else if (result == 0) {
724 if (scrub->os_paused)
725 sf->sf_status = SS_PAUSED;
727 sf->sf_status = SS_STOPPED;
729 sf->sf_status = SS_FAILED;
731 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
732 scrub->os_time_last_checkpoint);
733 result = osd_scrub_file_store(scrub);
735 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
736 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
738 up_write(&scrub->os_rwsem);
743 /* iteration engine */
745 struct osd_iit_param {
746 struct super_block *sb;
747 struct buffer_head *bitmap;
753 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
754 struct osd_device *dev,
755 struct osd_iit_param *param,
756 struct osd_idmap_cache **oic,
759 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
760 struct osd_device *dev,
761 struct osd_iit_param *param,
762 struct osd_idmap_cache *oic,
763 int *noslot, int rc);
765 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
767 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
768 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
769 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
770 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
771 return SCRUB_NEXT_BREAK;
773 *pos = param->gbase + param->offset;
779 * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
780 * \retval 0: FID-on-MDT
782 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
783 struct osd_device *dev,
786 /* XXX: The initial OI scrub will scan the top level /O to generate
787 * a small local FLDB according to the <seq>. If the given FID
788 * is in the local FLDB, then it is FID-on-OST; otherwise it's
789 * quite possible for FID-on-MDT. */
793 static int osd_scrub_get_fid(struct osd_thread_info *info,
794 struct osd_device *dev, struct inode *inode,
795 struct lu_fid *fid, bool scrub)
797 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
799 bool has_lma = false;
801 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
804 if (lma->lma_compat & LMAC_NOT_IN_OI) {
805 ldiskfs_set_inode_state(inode,
806 LDISKFS_STATE_LUSTRE_NO_OI);
807 return SCRUB_NEXT_CONTINUE;
810 *fid = lma->lma_self_fid;
811 if (fid_is_internal(&lma->lma_self_fid)) {
813 rc = SCRUB_NEXT_CONTINUE;
820 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
823 if (lma->lma_compat & LMAC_FID_ON_OST)
824 return SCRUB_NEXT_OSTOBJ;
826 if (fid_is_idif(fid) || fid_is_last_id(fid))
827 return SCRUB_NEXT_OSTOBJ_OLD;
829 if (lma->lma_incompat & LMAI_AGENT)
830 return SCRUB_NEXT_CONTINUE;
832 /* Here, it may be MDT-object, or may be 2.4 OST-object.
836 if (rc == -ENODATA || rc == 0) {
837 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
840 /* It is old 2.x (x <= 3) or 1.8 OST-object. */
841 rc = SCRUB_NEXT_OSTOBJ_OLD;
847 /* It is FID-on-OST, but we do not know how
848 * to generate its FID, ignore it directly. */
849 rc = SCRUB_NEXT_CONTINUE;
851 /* It is 2.4 OST-object. */
852 rc = SCRUB_NEXT_OSTOBJ_OLD;
860 if (dev->od_scrub.os_convert_igif) {
861 lu_igif_build(fid, inode->i_ino,
862 inode->i_generation);
864 rc = SCRUB_NEXT_NOLMA;
868 /* It may be FID-on-OST, or may be FID for
869 * non-MDT0, anyway, we do not know how to
870 * generate its FID, ignore it directly. */
871 rc = SCRUB_NEXT_CONTINUE;
876 /* For OI scrub case only: the object has LMA but has no ff
877 * (or ff crashed). It may be MDT-object, may be OST-object
878 * with crashed ff. The last check is local FLDB. */
879 rc = osd_scrub_check_local_fldb(info, dev, fid);
885 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
886 struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
887 struct super_block *sb, bool scrub)
893 osd_id_gen(lid, pos, OSD_OII_NOGEN);
894 inode = osd_iget(info, dev, lid);
897 /* The inode may be removed after bitmap searching, or the
898 * file is new created without inode initialized yet. */
899 if (rc == -ENOENT || rc == -ESTALE)
900 RETURN(SCRUB_NEXT_CONTINUE);
902 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
903 LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
907 /* If the inode has no OI mapping, then it is special locally used,
908 * should be invisible to OI scrub or up layer LFSCK. */
909 if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI))
910 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
913 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
914 /* Only skip it for the first OI scrub accessing. */
915 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
916 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
919 rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
928 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
929 struct osd_iit_param *param,
930 struct osd_idmap_cache **oic, int noslot)
932 struct osd_scrub *scrub = &dev->od_scrub;
933 struct ptlrpc_thread *thread = &scrub->os_thread;
935 struct osd_inode_id *lid;
938 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
939 struct l_wait_info lwi;
941 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
942 l_wait_event(thread->t_ctl_waitq,
943 !cfs_list_empty(&scrub->os_inconsistent_items) ||
944 !thread_is_running(thread),
948 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
949 spin_lock(&scrub->os_lock);
950 thread_set_flags(thread, SVC_STOPPING);
951 spin_unlock(&scrub->os_lock);
952 return SCRUB_NEXT_CRASH;
955 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
956 return SCRUB_NEXT_FATAL;
958 if (unlikely(!thread_is_running(thread)))
959 return SCRUB_NEXT_EXIT;
961 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
962 struct osd_inconsistent_item *oii;
964 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
965 struct osd_inconsistent_item, oii_list);
966 *oic = &oii->oii_cache;
967 scrub->os_in_prior = 1;
972 return SCRUB_NEXT_WAIT;
974 rc = osd_iit_next(param, &scrub->os_pos_current);
978 *oic = &scrub->os_oic;
979 fid = &(*oic)->oic_fid;
980 lid = &(*oic)->oic_lid;
981 rc = osd_iit_iget(info, dev, fid, lid,
982 scrub->os_pos_current, param->sb, true);
986 static int osd_preload_next(struct osd_thread_info *info,
987 struct osd_device *dev, struct osd_iit_param *param,
988 struct osd_idmap_cache **oic, int noslot)
990 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
991 struct osd_scrub *scrub;
992 struct ptlrpc_thread *thread;
995 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
999 scrub = &dev->od_scrub;
1000 thread = &scrub->os_thread;
1001 if (thread_is_running(thread) &&
1002 ooc->ooc_pos_preload >= scrub->os_pos_current)
1003 return SCRUB_NEXT_EXIT;
1005 rc = osd_iit_iget(info, dev,
1006 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
1007 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
1008 ooc->ooc_pos_preload, param->sb, false);
1009 /* If succeed, it needs to move forward; otherwise up layer LFSCK may
1010 * ignore the failure, so it still need to skip the inode next time. */
1011 ooc->ooc_pos_preload = param->gbase + ++(param->offset);
1016 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
1018 spin_lock(&scrub->os_lock);
1019 if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
1020 !cfs_list_empty(&scrub->os_inconsistent_items) ||
1021 it->ooi_waiting || !thread_is_running(&scrub->os_thread))
1022 scrub->os_waiting = 0;
1024 scrub->os_waiting = 1;
1025 spin_unlock(&scrub->os_lock);
1027 return !scrub->os_waiting;
1030 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
1031 struct osd_iit_param *param,
1032 struct osd_idmap_cache *oic, int *noslot, int rc)
1034 struct l_wait_info lwi = { 0 };
1035 struct osd_scrub *scrub = &dev->od_scrub;
1036 struct scrub_file *sf = &scrub->os_file;
1037 struct ptlrpc_thread *thread = &scrub->os_thread;
1038 struct osd_otable_it *it = dev->od_otable_it;
1039 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
1042 case SCRUB_NEXT_CONTINUE:
1044 case SCRUB_NEXT_WAIT:
1046 case SCRUB_NEXT_NOSCRUB:
1047 down_write(&scrub->os_rwsem);
1048 scrub->os_new_checked++;
1049 sf->sf_items_noscrub++;
1050 up_write(&scrub->os_rwsem);
1054 rc = osd_scrub_check_update(info, dev, oic, rc);
1058 rc = osd_scrub_checkpoint(scrub);
1060 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
1061 LDISKFS_SB(param->sb)->s_es->s_volume_name,
1062 scrub->os_pos_current, rc);
1063 /* Continue, as long as the scrub itself can go ahead. */
1066 if (scrub->os_in_prior) {
1067 scrub->os_in_prior = 0;
1072 scrub->os_pos_current = param->gbase + ++(param->offset);
1075 if (it != NULL && it->ooi_waiting &&
1076 ooc->ooc_pos_preload < scrub->os_pos_current) {
1077 spin_lock(&scrub->os_lock);
1078 it->ooi_waiting = 0;
1079 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1080 spin_unlock(&scrub->os_lock);
1083 if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
1086 if (osd_scrub_has_window(scrub, ooc)) {
1091 l_wait_event(thread->t_ctl_waitq,
1092 osd_scrub_wakeup(scrub, it),
1095 if (osd_scrub_has_window(scrub, ooc))
1102 static int osd_preload_exec(struct osd_thread_info *info,
1103 struct osd_device *dev, struct osd_iit_param *param,
1104 struct osd_idmap_cache *oic, int *noslot, int rc)
1106 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1109 ooc->ooc_cached_items++;
1110 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1111 ~OSD_OTABLE_IT_CACHE_MASK;
1113 return rc > 0 ? 0 : rc;
1116 #define SCRUB_IT_ALL 1
1117 #define SCRUB_IT_CRASH 2
1119 static int osd_inode_iteration(struct osd_thread_info *info,
1120 struct osd_device *dev, __u32 max, bool preload)
1122 osd_iit_next_policy next;
1123 osd_iit_exec_policy exec;
1126 struct osd_iit_param param;
1133 struct osd_scrub *scrub = &dev->od_scrub;
1135 next = osd_scrub_next;
1136 exec = osd_scrub_exec;
1137 pos = &scrub->os_pos_current;
1138 count = &scrub->os_new_checked;
1140 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1142 next = osd_preload_next;
1143 exec = osd_preload_exec;
1144 pos = &ooc->ooc_pos_preload;
1145 count = &ooc->ooc_cached_items;
1147 param.sb = osd_sb(dev);
1148 limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1150 while (*pos <= limit && *count < max) {
1151 struct osd_idmap_cache *oic = NULL;
1153 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1154 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1155 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1156 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1157 if (param.bitmap == NULL) {
1158 CERROR("%.16s: fail to read bitmap for %u, "
1159 "scrub will stop, urgent mode\n",
1160 LDISKFS_SB(param.sb)->s_es->s_volume_name,
1165 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1167 rc = next(info, dev, ¶m, &oic, noslot);
1169 case SCRUB_NEXT_BREAK:
1171 case SCRUB_NEXT_EXIT:
1172 brelse(param.bitmap);
1174 case SCRUB_NEXT_CRASH:
1175 brelse(param.bitmap);
1176 RETURN(SCRUB_IT_CRASH);
1177 case SCRUB_NEXT_FATAL:
1178 brelse(param.bitmap);
1182 rc = exec(info, dev, ¶m, oic, &noslot, rc);
1184 brelse(param.bitmap);
1190 brelse(param.bitmap);
1194 RETURN(SCRUB_IT_ALL);
1198 static int osd_otable_it_preload(const struct lu_env *env,
1199 struct osd_otable_it *it)
1201 struct osd_device *dev = it->ooi_dev;
1202 struct osd_scrub *scrub = &dev->od_scrub;
1203 struct osd_otable_cache *ooc = &it->ooi_cache;
1207 rc = osd_inode_iteration(osd_oti_get(env), dev,
1208 OSD_OTABLE_IT_CACHE_SIZE, true);
1209 if (rc == SCRUB_IT_ALL)
1210 it->ooi_all_cached = 1;
1212 CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1213 le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1214 ooc->ooc_pos_preload, rc);
1216 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1217 scrub->os_waiting = 0;
1218 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1221 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1224 static int osd_scrub_main(void *args)
1227 struct osd_device *dev = (struct osd_device *)args;
1228 struct osd_scrub *scrub = &dev->od_scrub;
1229 struct ptlrpc_thread *thread = &scrub->os_thread;
1230 struct super_block *sb = osd_sb(dev);
1234 rc = lu_env_init(&env, LCT_LOCAL);
1236 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1237 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1241 rc = osd_scrub_prep(dev);
1243 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1244 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1248 if (!scrub->os_full_speed) {
1249 struct l_wait_info lwi = { 0 };
1250 struct osd_otable_it *it = dev->od_otable_it;
1251 struct osd_otable_cache *ooc = &it->ooi_cache;
1253 l_wait_event(thread->t_ctl_waitq,
1254 it->ooi_user_ready || !thread_is_running(thread),
1256 if (unlikely(!thread_is_running(thread)))
1259 scrub->os_pos_current = ooc->ooc_pos_preload;
1262 CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1263 scrub->os_start_flags, scrub->os_pos_current);
1265 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1266 if (unlikely(rc == SCRUB_IT_CRASH))
1267 GOTO(out, rc = -EINVAL);
1271 osd_scrub_post(scrub, rc);
1272 CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1273 rc, scrub->os_pos_current);
1276 while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1277 struct osd_inconsistent_item *oii;
1279 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1280 struct osd_inconsistent_item, oii_list);
1281 cfs_list_del_init(&oii->oii_list);
1287 spin_lock(&scrub->os_lock);
1288 thread_set_flags(thread, SVC_STOPPED);
1289 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1290 spin_unlock(&scrub->os_lock);
1294 /* initial OI scrub */
1296 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1297 struct dentry *, filldir_t filldir);
1299 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1300 loff_t offset, __u64 ino, unsigned d_type);
1301 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1302 loff_t offset, __u64 ino, unsigned d_type);
1305 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1306 struct dentry *dentry, filldir_t filldir);
1308 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1309 struct dentry *dentry, filldir_t filldir);
1312 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1313 struct dentry *dentry, filldir_t filldir);
1316 OLF_SCAN_SUBITEMS = 0x0001,
1317 OLF_HIDE_FID = 0x0002,
1318 OLF_SHOW_NAME = 0x0004,
1324 struct lu_fid olm_fid;
1326 scandir_t olm_scandir;
1327 filldir_t olm_filldir;
1330 /* Add the new introduced local files in the list in the future. */
1331 static const struct osd_lf_map osd_lf_maps[] = {
1333 { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1337 { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1338 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1339 osd_ios_varfid_fill },
1341 /* NIDTBL_VERSIONS */
1342 { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1343 osd_ios_general_scan, osd_ios_varfid_fill },
1346 { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1349 { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1350 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1352 /* changelog_catalog */
1353 { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1355 /* changelog_users */
1356 { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1359 { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1363 { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1366 /* lfsck_bookmark */
1367 { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1370 { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1374 { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1375 OLF_SHOW_NAME, NULL, NULL },
1378 { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1379 osd_ios_general_scan, osd_ios_varfid_fill },
1382 { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1383 osd_ios_general_scan, osd_ios_varfid_fill },
1386 { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1387 OLF_SHOW_NAME, NULL, NULL },
1390 { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1391 OLF_SHOW_NAME, NULL, NULL },
1394 { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1395 OLF_SHOW_NAME, NULL, NULL },
1397 /* lfsck_namespace */
1398 { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1400 /* OBJECTS, upgrade from old device */
1401 { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1403 /* lquota_v2.user, upgrade from old device */
1404 { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1406 /* lquota_v2.group, upgrade from old device */
1407 { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1409 /* LAST_GROUP, upgrade from old device */
1410 { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1411 OLF_SHOW_NAME, NULL, NULL },
1414 { "lost+found", { 0, 0, 0 }, OLF_SCAN_SUBITEMS | OLF_NO_OI,
1415 osd_ios_general_scan, osd_ios_lf_fill },
1417 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1420 struct osd_ios_item {
1421 cfs_list_t oii_list;
1422 struct dentry *oii_dentry;
1423 scandir_t oii_scandir;
1424 filldir_t oii_filldir;
1427 struct osd_ios_filldir_buf {
1428 struct osd_thread_info *oifb_info;
1429 struct osd_device *oifb_dev;
1430 struct dentry *oifb_dentry;
1433 static inline struct dentry *
1434 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1436 struct dentry *dentry;
1438 dentry = ll_lookup_one_len(name, parent, namelen);
1439 if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1441 return ERR_PTR(-ENOENT);
1448 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1449 scandir_t scandir, filldir_t filldir)
1451 struct osd_ios_item *item;
1453 OBD_ALLOC_PTR(item);
1457 CFS_INIT_LIST_HEAD(&item->oii_list);
1458 item->oii_dentry = dget(dentry);
1459 item->oii_scandir = scandir;
1460 item->oii_filldir = filldir;
1461 cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1466 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1468 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1469 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1470 * reference the inode, or fixed if it is missing or references another inode.
1473 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1474 struct inode *inode, const struct lu_fid *fid, int flags)
1476 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1477 struct osd_inode_id *id = &info->oti_id;
1478 struct osd_inode_id *id2 = &info->oti_id2;
1479 struct osd_scrub *scrub = &dev->od_scrub;
1480 struct scrub_file *sf = &scrub->os_file;
1485 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1486 if (rc != 0 && rc != -ENODATA)
1489 osd_id_gen(id, inode->i_ino, inode->i_generation);
1490 if (rc == -ENODATA) {
1491 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1492 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1495 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1499 if (lma->lma_compat & LMAC_NOT_IN_OI)
1502 tfid = lma->lma_self_fid;
1505 rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1510 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1511 DTO_INDEX_INSERT, 0);
1518 if (osd_id_eq_strict(id, id2))
1521 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1522 osd_scrub_file_reset(scrub,
1523 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1525 rc = osd_scrub_file_store(scrub);
1530 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1531 DTO_INDEX_UPDATE, 0);
1539 * It scans the /lost+found, and for the OST-object (with filter_fid
1540 * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1542 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1543 loff_t offset, __u64 ino, unsigned d_type)
1545 struct osd_ios_filldir_buf *fill_buf = buf;
1546 struct osd_thread_info *info = fill_buf->oifb_info;
1547 struct osd_device *dev = fill_buf->oifb_dev;
1548 struct lu_fid *fid = &info->oti_fid;
1549 struct osd_scrub *scrub = &dev->od_scrub;
1550 struct dentry *parent = fill_buf->oifb_dentry;
1551 struct dentry *child;
1552 struct inode *dir = parent->d_inode;
1553 struct inode *inode;
1557 /* skip any '.' started names */
1561 scrub->os_lf_scanned++;
1562 child = osd_ios_lookup_one_len(name, parent, namelen);
1563 if (IS_ERR(child)) {
1564 CWARN("%s: cannot lookup child '%.*s': rc = %d\n",
1565 osd_name(dev), namelen, name, (int)PTR_ERR(child));
1569 inode = child->d_inode;
1570 if (S_ISDIR(inode->i_mode)) {
1571 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1574 CWARN("%s: cannot add child '%.*s': rc = %d\n",
1575 osd_name(dev), namelen, name, rc);
1579 if (!S_ISREG(inode->i_mode))
1582 rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1583 if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1584 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1586 CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1587 "/lost+found.\n", namelen, name, PFID(fid));
1588 scrub->os_lf_repaired++;
1590 CWARN("%s: cannot rename for '%.*s' "DFID": rc = %d\n",
1591 osd_name(dev), namelen, name, PFID(fid), rc);
1595 /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1596 * visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1597 * can process them in furtuer. */
1603 scrub->os_lf_failed++;
1605 /* skip the failure to make the scanning to continue. */
1609 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1610 loff_t offset, __u64 ino, unsigned d_type)
1612 struct osd_ios_filldir_buf *fill_buf = buf;
1613 struct osd_device *dev = fill_buf->oifb_dev;
1614 struct dentry *child;
1618 /* skip any '.' started names */
1622 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1624 RETURN(PTR_ERR(child));
1626 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1628 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1629 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1630 osd_ios_varfid_fill);
1636 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1637 loff_t offset, __u64 ino, unsigned d_type)
1639 struct osd_ios_filldir_buf *fill_buf = buf;
1640 struct osd_device *dev = fill_buf->oifb_dev;
1641 const struct osd_lf_map *map;
1642 struct dentry *child;
1646 /* skip any '.' started names */
1650 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1651 if (strlen(map->olm_name) != namelen)
1654 if (strncmp(map->olm_name, name, namelen) == 0)
1658 if (map->olm_name == NULL)
1661 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1663 RETURN(PTR_ERR(child));
1665 if (!(map->olm_flags & OLF_NO_OI))
1666 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1667 &map->olm_fid, map->olm_flags);
1668 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1669 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1677 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1678 struct dentry *dentry, filldir_t filldir)
1680 struct osd_ios_filldir_buf buf = { info, dev, dentry };
1681 struct file *filp = &info->oti_it_ea.oie_file;
1682 struct inode *inode = dentry->d_inode;
1683 const struct file_operations *fops = inode->i_fop;
1687 LASSERT(filldir != NULL);
1690 filp->f_dentry = dentry;
1691 filp->f_mode = FMODE_64BITHASH;
1692 filp->f_mapping = inode->i_mapping;
1694 filp->private_data = NULL;
1696 rc = fops->readdir(filp, &buf, filldir);
1697 fops->release(inode, filp);
1703 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1704 struct dentry *dentry, filldir_t filldir)
1706 struct osd_scrub *scrub = &dev->od_scrub;
1707 struct scrub_file *sf = &scrub->os_file;
1708 struct dentry *child;
1712 /* It is existing MDT0 device. We only allow the case of object without
1713 * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1714 * can generate IGIF mode FID for the object and related OI mapping. If
1715 * it is on other MDTs, then becuase file-level backup/restore, related
1716 * OI mapping may be invalid already, we do not know which is the right
1717 * FID for the object. We only allow IGIF objects to reside on the MDT0.
1719 * XXX: For the case of object on non-MDT0 device with neither LMA nor
1720 * "fid" xattr, then something crashed. We cannot re-generate the
1721 * FID directly, instead, the OI scrub will scan the OI structure
1722 * and try to re-generate the LMA from the OI mapping. But if the
1723 * OI mapping crashed or lost also, then we have to give up under
1724 * double failure cases. */
1725 scrub->os_convert_igif = 1;
1726 child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1727 strlen(dot_lustre_name));
1728 if (IS_ERR(child)) {
1729 rc = PTR_ERR(child);
1730 if (rc == -ENOENT) {
1731 /* It is 1.8 MDT device. */
1732 if (!(sf->sf_flags & SF_UPGRADE)) {
1733 osd_scrub_file_reset(scrub,
1734 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1736 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1737 rc = osd_scrub_file_store(scrub);
1743 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1744 * so the client will get IGIF for the ".lustre" object when
1747 * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1748 * it does not know whether there are some old clients cached
1749 * the ".lustre" IGIF during the upgrading. Two choices:
1751 * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1752 * It will allow the old connected clients to access the
1753 * ".lustre" with cached IGIF. But it will cause others
1754 * on the MDT failed to check "fid_is_dot_lustre()".
1756 * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1757 * for ".lustre" in spite of whether there are some clients
1758 * cached the ".lustre" IGIF or not. It enables the check
1759 * "fid_is_dot_lustre()" on the MDT, although it will cause
1760 * that the old connected clients cannot access the ".lustre"
1761 * with the cached IGIF.
1763 * Usually, it is rare case for the old connected clients
1764 * to access the ".lustre" with cached IGIF. So we prefer
1765 * to the solution 2). */
1766 rc = osd_ios_scan_one(info, dev, child->d_inode,
1767 &LU_DOT_LUSTRE_FID, 0);
1775 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1776 struct dentry *dentry, filldir_t filldir)
1778 struct osd_scrub *scrub = &dev->od_scrub;
1779 struct scrub_file *sf = &scrub->os_file;
1780 struct dentry *child;
1784 if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
1785 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1786 rc = osd_scrub_file_store(scrub);
1791 child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1792 if (!IS_ERR(child)) {
1793 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1796 rc = PTR_ERR(child);
1799 if (rc != 0 && rc != -ENOENT)
1802 child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1803 if (!IS_ERR(child)) {
1804 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1807 rc = PTR_ERR(child);
1816 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1817 struct osd_device *dev)
1819 struct osd_ios_item *item = NULL;
1820 scandir_t scandir = osd_ios_general_scan;
1821 filldir_t filldir = osd_ios_root_fill;
1822 struct dentry *dentry = osd_sb(dev)->s_root;
1823 const struct osd_lf_map *map = osd_lf_maps;
1828 rc = scandir(info, dev, dentry, filldir);
1830 dput(item->oii_dentry);
1837 if (cfs_list_empty(&dev->od_ios_list))
1840 item = cfs_list_entry(dev->od_ios_list.next,
1841 struct osd_ios_item, oii_list);
1842 cfs_list_del_init(&item->oii_list);
1844 LASSERT(item->oii_scandir != NULL);
1845 scandir = item->oii_scandir;
1846 filldir = item->oii_filldir;
1847 dentry = item->oii_dentry;
1850 while (!cfs_list_empty(&dev->od_ios_list)) {
1851 item = cfs_list_entry(dev->od_ios_list.next,
1852 struct osd_ios_item, oii_list);
1853 cfs_list_del_init(&item->oii_list);
1854 dput(item->oii_dentry);
1861 /* There maybe the case that the object has been removed, but its OI
1862 * mapping is still in the OI file, such as the "CATALOGS" after MDT
1863 * file-level backup/restore. So here cleanup the stale OI mappings. */
1864 while (map->olm_name != NULL) {
1865 struct dentry *child;
1867 if (fid_is_zero(&map->olm_fid)) {
1872 child = osd_ios_lookup_one_len(map->olm_name,
1873 osd_sb(dev)->s_root,
1874 strlen(map->olm_name));
1877 else if (PTR_ERR(child) == -ENOENT)
1878 osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1879 NULL, DTO_INDEX_DELETE, 0);
1886 char *osd_lf_fid2name(const struct lu_fid *fid)
1888 const struct osd_lf_map *map = osd_lf_maps;
1890 while (map->olm_name != NULL) {
1891 if (!lu_fid_eq(fid, &map->olm_fid)) {
1896 if (map->olm_flags & OLF_SHOW_NAME)
1897 return map->olm_name;
1905 /* OI scrub start/stop */
1907 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1909 struct osd_scrub *scrub = &dev->od_scrub;
1910 struct ptlrpc_thread *thread = &scrub->os_thread;
1911 struct l_wait_info lwi = { 0 };
1916 /* os_lock: sync status between stop and scrub thread */
1917 spin_lock(&scrub->os_lock);
1918 if (thread_is_running(thread)) {
1919 spin_unlock(&scrub->os_lock);
1921 } else if (unlikely(thread_is_stopping(thread))) {
1922 spin_unlock(&scrub->os_lock);
1923 l_wait_event(thread->t_ctl_waitq,
1924 thread_is_stopped(thread),
1928 spin_unlock(&scrub->os_lock);
1930 if (scrub->os_file.sf_status == SS_COMPLETED)
1933 scrub->os_start_flags = flags;
1934 thread_set_flags(thread, 0);
1935 rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
1936 if (IS_ERR_VALUE(rc)) {
1937 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1938 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1942 l_wait_event(thread->t_ctl_waitq,
1943 thread_is_running(thread) || thread_is_stopped(thread),
1949 int osd_scrub_start(struct osd_device *dev)
1954 /* od_otable_mutex: prevent curcurrent start/stop */
1955 mutex_lock(&dev->od_otable_mutex);
1956 rc = do_osd_scrub_start(dev, SS_AUTO);
1957 mutex_unlock(&dev->od_otable_mutex);
1959 RETURN(rc == -EALREADY ? 0 : rc);
1962 static void do_osd_scrub_stop(struct osd_scrub *scrub)
1964 struct ptlrpc_thread *thread = &scrub->os_thread;
1965 struct l_wait_info lwi = { 0 };
1967 /* os_lock: sync status between stop and scrub thread */
1968 spin_lock(&scrub->os_lock);
1969 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1970 thread_set_flags(thread, SVC_STOPPING);
1971 spin_unlock(&scrub->os_lock);
1972 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1973 l_wait_event(thread->t_ctl_waitq,
1974 thread_is_stopped(thread),
1976 /* Do not skip the last lock/unlock, which can guarantee that
1977 * the caller cannot return until the OI scrub thread exit. */
1978 spin_lock(&scrub->os_lock);
1980 spin_unlock(&scrub->os_lock);
1983 static void osd_scrub_stop(struct osd_device *dev)
1985 /* od_otable_mutex: prevent curcurrent start/stop */
1986 mutex_lock(&dev->od_otable_mutex);
1987 dev->od_scrub.os_paused = 1;
1988 do_osd_scrub_stop(&dev->od_scrub);
1989 mutex_unlock(&dev->od_otable_mutex);
1992 /* OI scrub setup/cleanup */
1994 static const char osd_scrub_name[] = "OI_scrub";
1996 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1998 struct osd_thread_info *info = osd_oti_get(env);
1999 struct osd_scrub *scrub = &dev->od_scrub;
2000 struct lvfs_run_ctxt *ctxt = &scrub->os_ctxt;
2001 struct scrub_file *sf = &scrub->os_file;
2002 struct super_block *sb = osd_sb(dev);
2003 struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
2004 struct lvfs_run_ctxt saved;
2006 struct inode *inode;
2007 struct lu_fid *fid = &info->oti_fid;
2012 memset(scrub, 0, sizeof(*scrub));
2013 OBD_SET_CTXT_MAGIC(ctxt);
2014 ctxt->pwdmnt = dev->od_mnt;
2015 ctxt->pwd = dev->od_mnt->mnt_root;
2016 ctxt->fs = get_ds();
2018 cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
2019 init_rwsem(&scrub->os_rwsem);
2020 spin_lock_init(&scrub->os_lock);
2021 CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2023 push_ctxt(&saved, ctxt, NULL);
2024 filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
2026 pop_ctxt(&saved, ctxt, NULL);
2027 RETURN(PTR_ERR(filp));
2030 inode = filp->f_dentry->d_inode;
2031 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
2032 /* 'What the @fid is' is not imporatant, because the object
2033 * has no OI mapping, and only is visible inside the OSD.*/
2034 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2035 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2037 filp_close(filp, 0);
2038 pop_ctxt(&saved, ctxt, NULL);
2042 scrub->os_inode = igrab(inode);
2043 filp_close(filp, 0);
2044 pop_ctxt(&saved, ctxt, NULL);
2046 rc = osd_scrub_file_load(scrub);
2047 if (rc == -ENOENT) {
2048 osd_scrub_file_init(scrub, es->s_uuid);
2049 /* If the "/O" dir does not exist when mount (indicated by
2050 * osd_device::od_maybe_new), neither for the "/OI_scrub",
2051 * then it is quite probably that the device is a new one,
2052 * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2054 * For the rare case that "/O" and "OI_scrub" both lost on
2055 * an old device, it can be found and cleared later.
2057 * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2058 * need to check "filter_fid_old" and to convert it to
2059 * "filter_fid" for each object, and all the IGIF should
2060 * have their FID mapping in OI files already. */
2061 if (dev->od_maybe_new)
2062 sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2064 } else if (rc != 0) {
2067 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
2068 osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
2070 } else if (sf->sf_status == SS_SCANNING) {
2071 sf->sf_status = SS_CRASHED;
2076 if (sf->sf_pos_last_checkpoint != 0)
2077 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2079 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2082 rc = osd_scrub_file_store(scrub);
2087 /* Initialize OI files. */
2088 rc = osd_oi_init(info, dev);
2092 rc = osd_initial_OI_scrub(info, dev);
2094 if (sf->sf_flags & SF_UPGRADE ||
2095 !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2096 sf->sf_success_count > 0)) {
2097 dev->od_igif_inoi = 0;
2098 dev->od_check_ff = 1;
2100 dev->od_igif_inoi = 1;
2101 dev->od_check_ff = 0;
2104 if (sf->sf_flags & SF_INCONSISTENT)
2105 /* The 'od_igif_inoi' will be set under the
2107 * 1) new created system, or
2108 * 2) restored from file-level backup, or
2109 * 3) the upgrading completed.
2111 * The 'od_igif_inoi' may be cleared by OI scrub
2112 * later if found that the system is upgrading. */
2113 dev->od_igif_inoi = 1;
2115 if (!dev->od_noscrub &&
2116 ((sf->sf_status == SS_PAUSED) ||
2117 (sf->sf_status == SS_CRASHED &&
2118 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2119 SF_UPGRADE | SF_AUTO)) ||
2120 (sf->sf_status == SS_INIT &&
2121 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2123 rc = osd_scrub_start(dev);
2126 /* it is possible that dcache entries may keep objects after they are
2127 * deleted by OSD. While it looks safe this can cause object data to
2128 * stay until umount causing failures in tests calculating free space,
2129 * e.g. replay-ost-single. Since those dcache entries are not used
2130 * anymore let's just free them after use here */
2131 shrink_dcache_sb(sb);
2136 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2138 struct osd_scrub *scrub = &dev->od_scrub;
2140 LASSERT(dev->od_otable_it == NULL);
2142 if (scrub->os_inode != NULL) {
2143 osd_scrub_stop(dev);
2144 iput(scrub->os_inode);
2145 scrub->os_inode = NULL;
2147 if (dev->od_oi_table != NULL)
2148 osd_oi_fini(osd_oti_get(env), dev);
2151 /* object table based iteration APIs */
2153 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2154 struct dt_object *dt, __u32 attr,
2155 struct lustre_capa *capa)
2157 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2158 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2159 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
2160 struct osd_scrub *scrub = &dev->od_scrub;
2161 struct osd_otable_it *it;
2166 /* od_otable_mutex: prevent curcurrent init/fini */
2167 mutex_lock(&dev->od_otable_mutex);
2168 if (dev->od_otable_it != NULL)
2169 GOTO(out, it = ERR_PTR(-EALREADY));
2173 GOTO(out, it = ERR_PTR(-ENOMEM));
2175 dev->od_otable_it = it;
2177 it->ooi_cache.ooc_consumer_idx = -1;
2178 if (flags & DOIF_OUTUSED)
2179 it->ooi_used_outside = 1;
2181 if (flags & DOIF_RESET)
2184 if (valid & DOIV_ERROR_HANDLE) {
2185 if (flags & DOIF_FAILOUT)
2186 start |= SS_SET_FAILOUT;
2188 start |= SS_CLEAR_FAILOUT;
2191 rc = do_osd_scrub_start(dev, start);
2192 if (rc < 0 && rc != -EALREADY) {
2193 dev->od_otable_it = NULL;
2195 GOTO(out, it = ERR_PTR(rc));
2198 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2203 mutex_unlock(&dev->od_otable_mutex);
2204 return (struct dt_it *)it;
2207 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2209 struct osd_otable_it *it = (struct osd_otable_it *)di;
2210 struct osd_device *dev = it->ooi_dev;
2212 /* od_otable_mutex: prevent curcurrent init/fini */
2213 mutex_lock(&dev->od_otable_mutex);
2214 do_osd_scrub_stop(&dev->od_scrub);
2215 LASSERT(dev->od_otable_it == it);
2217 dev->od_otable_it = NULL;
2218 mutex_unlock(&dev->od_otable_mutex);
2222 static int osd_otable_it_get(const struct lu_env *env,
2223 struct dt_it *di, const struct dt_key *key)
2228 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2233 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2235 spin_lock(&scrub->os_lock);
2236 if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2237 scrub->os_waiting ||
2238 !thread_is_running(&scrub->os_thread))
2239 it->ooi_waiting = 0;
2241 it->ooi_waiting = 1;
2242 spin_unlock(&scrub->os_lock);
2244 return !it->ooi_waiting;
2247 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2249 struct osd_otable_it *it = (struct osd_otable_it *)di;
2250 struct osd_device *dev = it->ooi_dev;
2251 struct osd_scrub *scrub = &dev->od_scrub;
2252 struct osd_otable_cache *ooc = &it->ooi_cache;
2253 struct ptlrpc_thread *thread = &scrub->os_thread;
2254 struct l_wait_info lwi = { 0 };
2258 LASSERT(it->ooi_user_ready);
2261 if (!thread_is_running(thread) && !it->ooi_used_outside)
2264 if (ooc->ooc_cached_items > 0) {
2265 ooc->ooc_cached_items--;
2266 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2267 ~OSD_OTABLE_IT_CACHE_MASK;
2271 if (it->ooi_all_cached) {
2272 l_wait_event(thread->t_ctl_waitq,
2273 !thread_is_running(thread),
2278 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2279 spin_lock(&scrub->os_lock);
2280 scrub->os_waiting = 0;
2281 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2282 spin_unlock(&scrub->os_lock);
2285 if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2286 l_wait_event(thread->t_ctl_waitq,
2287 osd_otable_it_wakeup(scrub, it),
2290 if (!thread_is_running(thread) && !it->ooi_used_outside)
2293 rc = osd_otable_it_preload(env, it);
2300 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2301 const struct dt_it *di)
2306 static int osd_otable_it_key_size(const struct lu_env *env,
2307 const struct dt_it *di)
2309 return sizeof(__u64);
2312 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2313 struct dt_rec *rec, __u32 attr)
2315 struct osd_otable_it *it = (struct osd_otable_it *)di;
2316 struct osd_otable_cache *ooc = &it->ooi_cache;
2318 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2320 /* Filter out Invald FID already. */
2321 LASSERTF(fid_is_sane((struct lu_fid *)rec),
2322 "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2323 PFID((struct lu_fid *)rec),
2324 ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2329 static __u64 osd_otable_it_store(const struct lu_env *env,
2330 const struct dt_it *di)
2332 struct osd_otable_it *it = (struct osd_otable_it *)di;
2333 struct osd_otable_cache *ooc = &it->ooi_cache;
2336 if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2337 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2339 hash = ooc->ooc_pos_preload;
2344 * Set the OSD layer iteration start position as the specified hash.
2346 static int osd_otable_it_load(const struct lu_env *env,
2347 const struct dt_it *di, __u64 hash)
2349 struct osd_otable_it *it = (struct osd_otable_it *)di;
2350 struct osd_device *dev = it->ooi_dev;
2351 struct osd_otable_cache *ooc = &it->ooi_cache;
2352 struct osd_scrub *scrub = &dev->od_scrub;
2356 /* Forbid to set iteration position after iteration started. */
2357 if (it->ooi_user_ready)
2360 if (hash > OSD_OTABLE_MAX_HASH)
2361 hash = OSD_OTABLE_MAX_HASH;
2363 ooc->ooc_pos_preload = hash;
2364 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2365 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2367 it->ooi_user_ready = 1;
2368 if (!scrub->os_full_speed)
2369 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2371 /* Unplug OSD layer iteration by the first next() call. */
2372 rc = osd_otable_it_next(env, (struct dt_it *)it);
2377 static int osd_otable_it_key_rec(const struct lu_env *env,
2378 const struct dt_it *di, void *key_rec)
2383 const struct dt_index_operations osd_otable_ops = {
2385 .init = osd_otable_it_init,
2386 .fini = osd_otable_it_fini,
2387 .get = osd_otable_it_get,
2388 .put = osd_otable_it_put,
2389 .next = osd_otable_it_next,
2390 .key = osd_otable_it_key,
2391 .key_size = osd_otable_it_key_size,
2392 .rec = osd_otable_it_rec,
2393 .store = osd_otable_it_store,
2394 .load = osd_otable_it_load,
2395 .key_rec = osd_otable_it_key_rec,
2399 /* high priority inconsistent items list APIs */
2401 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2404 struct osd_inconsistent_item *oii;
2405 struct osd_scrub *scrub = &dev->od_scrub;
2406 struct ptlrpc_thread *thread = &scrub->os_thread;
2411 if (unlikely(oii == NULL))
2414 CFS_INIT_LIST_HEAD(&oii->oii_list);
2415 oii->oii_cache = *oic;
2416 oii->oii_insert = insert;
2418 spin_lock(&scrub->os_lock);
2419 if (unlikely(!thread_is_running(thread))) {
2420 spin_unlock(&scrub->os_lock);
2425 if (cfs_list_empty(&scrub->os_inconsistent_items))
2427 cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2428 spin_unlock(&scrub->os_lock);
2431 cfs_waitq_broadcast(&thread->t_ctl_waitq);
2436 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2437 struct osd_inode_id *id)
2439 struct osd_scrub *scrub = &dev->od_scrub;
2440 struct osd_inconsistent_item *oii;
2443 spin_lock(&scrub->os_lock);
2444 cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2445 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2446 *id = oii->oii_cache.oic_lid;
2447 spin_unlock(&scrub->os_lock);
2451 spin_unlock(&scrub->os_lock);
2458 static const char *scrub_status_names[] = {
2469 static const char *scrub_flags_names[] = {
2477 static const char *scrub_param_names[] = {
2482 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2490 rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2496 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2499 rc = snprintf(*buf, *len, "%s%c", names[i],
2500 bits != 0 ? ',' : '\n');
2511 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2516 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2517 cfs_time_current_sec() - time);
2519 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2528 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2533 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2535 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2544 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2546 struct osd_scrub *scrub = &dev->od_scrub;
2547 struct scrub_file *sf = &scrub->os_file;
2554 down_read(&scrub->os_rwsem);
2555 rc = snprintf(buf, len,
2560 sf->sf_magic, (int)sf->sf_oi_count,
2561 scrub_status_names[sf->sf_status]);
2567 rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2572 rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2577 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2578 "time_since_last_completed");
2582 rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2583 "time_since_latest_start");
2587 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2588 "time_since_last_checkpoint");
2592 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2593 "latest_start_position");
2597 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2598 "last_checkpoint_position");
2602 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2603 "first_failure_position");
2607 checked = sf->sf_items_checked + scrub->os_new_checked;
2608 rc = snprintf(buf, len,
2609 "checked: "LPU64"\n"
2610 "updated: "LPU64"\n"
2612 "prior_updated: "LPU64"\n"
2613 "noscrub: "LPU64"\n"
2615 "success_count: %u\n",
2616 checked, sf->sf_items_updated, sf->sf_items_failed,
2617 sf->sf_items_updated_prior, sf->sf_items_noscrub,
2618 sf->sf_items_igif, sf->sf_success_count);
2625 if (thread_is_running(&scrub->os_thread)) {
2626 cfs_duration_t duration = cfs_time_current() -
2627 scrub->os_time_last_checkpoint;
2628 __u64 new_checked = scrub->os_new_checked * HZ;
2629 __u32 rtime = sf->sf_run_time +
2630 cfs_duration_sec(duration + HALF_SEC);
2633 do_div(new_checked, duration);
2635 do_div(speed, rtime);
2636 rc = snprintf(buf, len,
2637 "run_time: %u seconds\n"
2638 "average_speed: "LPU64" objects/sec\n"
2639 "real-time_speed: "LPU64" objects/sec\n"
2640 "current_position: %u\n"
2641 "lf_scanned: "LPU64"\n"
2642 "lf_reparied: "LPU64"\n"
2643 "lf_failed: "LPU64"\n",
2644 rtime, speed, new_checked, scrub->os_pos_current,
2645 scrub->os_lf_scanned, scrub->os_lf_repaired,
2646 scrub->os_lf_failed);
2648 if (sf->sf_run_time != 0)
2649 do_div(speed, sf->sf_run_time);
2650 rc = snprintf(buf, len,
2651 "run_time: %u seconds\n"
2652 "average_speed: "LPU64" objects/sec\n"
2653 "real-time_speed: N/A\n"
2654 "current_position: N/A\n"
2655 "lf_scanned: "LPU64"\n"
2656 "lf_reparied: "LPU64"\n"
2657 "lf_failed: "LPU64"\n",
2658 sf->sf_run_time, speed, scrub->os_lf_scanned,
2659 scrub->os_lf_repaired, scrub->os_lf_failed);
2669 up_read(&scrub->os_rwsem);