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>
44 #include "osd_internal.h"
46 #include "osd_scrub.h"
48 #define HALF_SEC (CFS_HZ >> 1)
50 #define OSD_OTABLE_MAX_HASH 0x00000000ffffffffULL
52 #define SCRUB_NEXT_BREAK 1 /* exit current loop and process next group */
53 #define SCRUB_NEXT_CONTINUE 2 /* skip current object and process next bit */
54 #define SCRUB_NEXT_EXIT 3 /* exit all the loops */
55 #define SCRUB_NEXT_WAIT 4 /* wait for free cache slot */
56 #define SCRUB_NEXT_CRASH 5 /* simulate system crash during OI scrub */
57 #define SCRUB_NEXT_FATAL 6 /* simulate failure during OI scrub */
58 #define SCRUB_NEXT_NOSCRUB 7 /* new created object, no scrub on it */
59 #define SCRUB_NEXT_NOLMA 8 /* the inode has no FID-in-LMA */
60 #define SCRUB_NEXT_OSTOBJ 9 /* for OST-object */
61 #define SCRUB_NEXT_OSTOBJ_OLD 10 /* old OST-object, no LMA or no FID-on-OST
66 static inline struct osd_device *osd_scrub2dev(struct osd_scrub *scrub)
68 return container_of0(scrub, struct osd_device, od_scrub);
71 static inline struct super_block *osd_scrub2sb(struct osd_scrub *scrub)
73 return osd_sb(osd_scrub2dev(scrub));
76 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
77 struct osd_otable_cache *ooc)
79 return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
83 * update/insert/delete the specified OI mapping (@fid @id) according to the ops
85 * \retval 1, changed nothing
86 * \retval 0, changed successfully
87 * \retval -ve, on error
89 static int osd_scrub_refresh_mapping(struct osd_thread_info *info,
90 struct osd_device *dev,
91 const struct lu_fid *fid,
92 const struct osd_inode_id *id,
93 int ops, enum oi_check_flags flags)
95 const struct lu_env *env = info->oti_env;
97 struct osd_thandle *oh;
101 th = dt_trans_create(env, &dev->od_dt_dev);
105 oh = container_of0(th, struct osd_thandle, ot_super);
106 LASSERT(oh->ot_handle == NULL);
109 case DTO_INDEX_UPDATE:
110 osd_trans_declare_op(env, oh, OSD_OT_UPDATE,
111 osd_dto_credits_noquota[DTO_INDEX_UPDATE]);
112 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
116 rc = osd_oi_update(info, dev, fid, id, th, flags);
117 if (unlikely(rc == -ENOENT)) {
118 /* Some unlink thread may removed the OI mapping. */
122 case DTO_INDEX_INSERT:
123 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
124 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
125 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
129 rc = osd_oi_insert(info, dev, fid, id, th, flags);
130 if (unlikely(rc == -EEXIST)) {
132 /* XXX: There are trouble things when adding OI
133 * mapping for IGIF object, which may cause
134 * multiple objects to be mapped to the same
135 * IGIF formatted FID. Consider the following
138 * 1) The MDT is upgrading from 1.8 device.
139 * The OI scrub generates IGIF FID1 for the
140 * OBJ1 and adds the OI mapping.
142 * 2) For some reason, the OI scrub does not
143 * process all the IGIF objects completely.
145 * 3) The MDT is backuped and restored against
148 * 4) When the MDT mounts up, the OI scrub will
149 * try to rebuild the OI files. For some IGIF
150 * object, OBJ2, which was not processed by the
151 * OI scrub before the backup/restore, and the
152 * new generated IGIF formatted FID may be just
153 * the FID1, the same as OBJ1.
155 * Under such case, the OI scrub cannot know how
156 * to generate new FID for the OBJ2.
158 * Currently, we do nothing for that. One possible
159 * solution is to generate new normal FID for the
162 * Anyway, it is rare, only exists in theory. */
165 case DTO_INDEX_DELETE:
166 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
167 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
168 rc = dt_trans_start_local(env, &dev->od_dt_dev, th);
172 rc = osd_oi_delete(info, dev, fid, th, flags);
174 /* It is normal that the unlink thread has removed the
175 * OI mapping already. */
180 LASSERTF(0, "Unexpected ops %d\n", ops);
187 dt_trans_stop(env, &dev->od_dt_dev, th);
191 /* OI_scrub file ops */
193 static void osd_scrub_file_to_cpu(struct scrub_file *des,
194 struct scrub_file *src)
196 memcpy(des->sf_uuid, src->sf_uuid, 16);
197 des->sf_flags = le64_to_cpu(src->sf_flags);
198 des->sf_magic = le32_to_cpu(src->sf_magic);
199 des->sf_status = le16_to_cpu(src->sf_status);
200 des->sf_param = le16_to_cpu(src->sf_param);
201 des->sf_time_last_complete =
202 le64_to_cpu(src->sf_time_last_complete);
203 des->sf_time_latest_start =
204 le64_to_cpu(src->sf_time_latest_start);
205 des->sf_time_last_checkpoint =
206 le64_to_cpu(src->sf_time_last_checkpoint);
207 des->sf_pos_latest_start =
208 le64_to_cpu(src->sf_pos_latest_start);
209 des->sf_pos_last_checkpoint =
210 le64_to_cpu(src->sf_pos_last_checkpoint);
211 des->sf_pos_first_inconsistent =
212 le64_to_cpu(src->sf_pos_first_inconsistent);
213 des->sf_items_checked =
214 le64_to_cpu(src->sf_items_checked);
215 des->sf_items_updated =
216 le64_to_cpu(src->sf_items_updated);
217 des->sf_items_failed =
218 le64_to_cpu(src->sf_items_failed);
219 des->sf_items_updated_prior =
220 le64_to_cpu(src->sf_items_updated_prior);
221 des->sf_run_time = le32_to_cpu(src->sf_run_time);
222 des->sf_success_count = le32_to_cpu(src->sf_success_count);
223 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
224 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
227 static void osd_scrub_file_to_le(struct scrub_file *des,
228 struct scrub_file *src)
230 memcpy(des->sf_uuid, src->sf_uuid, 16);
231 des->sf_flags = cpu_to_le64(src->sf_flags);
232 des->sf_magic = cpu_to_le32(src->sf_magic);
233 des->sf_status = cpu_to_le16(src->sf_status);
234 des->sf_param = cpu_to_le16(src->sf_param);
235 des->sf_time_last_complete =
236 cpu_to_le64(src->sf_time_last_complete);
237 des->sf_time_latest_start =
238 cpu_to_le64(src->sf_time_latest_start);
239 des->sf_time_last_checkpoint =
240 cpu_to_le64(src->sf_time_last_checkpoint);
241 des->sf_pos_latest_start =
242 cpu_to_le64(src->sf_pos_latest_start);
243 des->sf_pos_last_checkpoint =
244 cpu_to_le64(src->sf_pos_last_checkpoint);
245 des->sf_pos_first_inconsistent =
246 cpu_to_le64(src->sf_pos_first_inconsistent);
247 des->sf_items_checked =
248 cpu_to_le64(src->sf_items_checked);
249 des->sf_items_updated =
250 cpu_to_le64(src->sf_items_updated);
251 des->sf_items_failed =
252 cpu_to_le64(src->sf_items_failed);
253 des->sf_items_updated_prior =
254 cpu_to_le64(src->sf_items_updated_prior);
255 des->sf_run_time = cpu_to_le32(src->sf_run_time);
256 des->sf_success_count = cpu_to_le32(src->sf_success_count);
257 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
258 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
261 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
263 struct scrub_file *sf = &scrub->os_file;
265 memset(sf, 0, sizeof(*sf));
266 memcpy(sf->sf_uuid, uuid, 16);
267 sf->sf_magic = SCRUB_MAGIC_V1;
268 sf->sf_status = SS_INIT;
271 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
273 struct scrub_file *sf = &scrub->os_file;
275 CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
276 memcpy(sf->sf_uuid, uuid, 16);
277 sf->sf_status = SS_INIT;
278 sf->sf_flags |= flags;
281 sf->sf_time_latest_start = 0;
282 sf->sf_time_last_checkpoint = 0;
283 sf->sf_pos_latest_start = 0;
284 sf->sf_pos_last_checkpoint = 0;
285 sf->sf_pos_first_inconsistent = 0;
286 sf->sf_items_checked = 0;
287 sf->sf_items_updated = 0;
288 sf->sf_items_failed = 0;
289 sf->sf_items_updated_prior = 0;
290 sf->sf_items_noscrub = 0;
291 sf->sf_items_igif = 0;
294 static int osd_scrub_file_load(struct osd_scrub *scrub)
297 char *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
298 int len = sizeof(scrub->os_file_disk);
301 rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
303 struct scrub_file *sf = &scrub->os_file;
305 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
306 if (sf->sf_magic != SCRUB_MAGIC_V1) {
307 CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
308 name, sf->sf_magic, SCRUB_MAGIC_V1);
309 /* Process it as new scrub file. */
314 } else if (rc != 0) {
315 CERROR("%.16s: fail to load scrub file, expected = %d, "
316 "rc = %d\n", name, len, rc);
320 /* return -ENOENT for empty scrub file case. */
327 int osd_scrub_file_store(struct osd_scrub *scrub)
329 struct osd_device *dev;
332 int len = sizeof(scrub->os_file_disk);
336 dev = container_of0(scrub, struct osd_device, od_scrub);
337 credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
338 osd_dto_credits_noquota[DTO_WRITE_BLOCK];
339 jh = ldiskfs_journal_start_sb(osd_sb(dev), credits);
342 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
343 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
347 osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
348 rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
350 ldiskfs_journal_stop(jh);
352 CERROR("%.16s: fail to store scrub file, expected = %d, "
354 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
356 scrub->os_time_last_checkpoint = cfs_time_current();
357 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
358 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
364 static int osd_scrub_prep(struct osd_device *dev)
366 struct osd_scrub *scrub = &dev->od_scrub;
367 struct ptlrpc_thread *thread = &scrub->os_thread;
368 struct scrub_file *sf = &scrub->os_file;
369 __u32 flags = scrub->os_start_flags;
373 down_write(&scrub->os_rwsem);
374 if (flags & SS_SET_FAILOUT)
375 sf->sf_param |= SP_FAILOUT;
377 if (flags & SS_CLEAR_FAILOUT)
378 sf->sf_param &= ~SP_FAILOUT;
380 if (flags & SS_RESET)
381 osd_scrub_file_reset(scrub,
382 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
384 if (flags & SS_AUTO) {
385 scrub->os_full_speed = 1;
386 sf->sf_flags |= SF_AUTO;
388 scrub->os_full_speed = 0;
391 if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
392 scrub->os_full_speed = 1;
394 scrub->os_in_prior = 0;
395 scrub->os_waiting = 0;
396 scrub->os_paused = 0;
397 scrub->os_new_checked = 0;
398 if (sf->sf_pos_last_checkpoint != 0)
399 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
401 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
403 scrub->os_pos_current = sf->sf_pos_latest_start;
404 sf->sf_status = SS_SCANNING;
405 sf->sf_time_latest_start = cfs_time_current_sec();
406 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
407 rc = osd_scrub_file_store(scrub);
409 spin_lock(&scrub->os_lock);
410 thread_set_flags(thread, SVC_RUNNING);
411 spin_unlock(&scrub->os_lock);
412 cfs_waitq_broadcast(&thread->t_ctl_waitq);
414 up_write(&scrub->os_rwsem);
420 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
421 struct osd_idmap_cache *oic, int val)
423 struct osd_scrub *scrub = &dev->od_scrub;
424 struct scrub_file *sf = &scrub->os_file;
425 struct lu_fid *fid = &oic->oic_fid;
426 struct osd_inode_id *lid = &oic->oic_lid;
427 struct osd_inode_id *lid2 = &info->oti_id;
428 struct osd_inconsistent_item *oii = NULL;
429 struct inode *inode = NULL;
430 int ops = DTO_INDEX_UPDATE;
435 down_write(&scrub->os_rwsem);
436 scrub->os_new_checked++;
440 if (scrub->os_in_prior)
441 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
444 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
447 if (fid_is_igif(fid))
450 if (val == SCRUB_NEXT_OSTOBJ_OLD) {
451 inode = osd_iget(info, dev, lid);
454 /* Someone removed the inode. */
455 if (rc == -ENOENT || rc == -ESTALE)
460 sf->sf_flags |= SF_UPGRADE;
461 rc = osd_ea_fid_set(info, inode, fid,
467 if ((val == SCRUB_NEXT_NOLMA) &&
468 (!dev->od_handle_nolma || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
471 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
474 rc = osd_oi_lookup(info, dev, fid, lid2,
475 (val == SCRUB_NEXT_OSTOBJ ||
476 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
483 inode = osd_iget(info, dev, lid);
486 /* Someone removed the inode. */
487 if (rc == -ENOENT || rc == -ESTALE)
493 scrub->os_full_speed = 1;
494 ops = DTO_INDEX_INSERT;
495 idx = osd_oi_fid2idx(dev, fid);
497 case SCRUB_NEXT_NOLMA:
498 sf->sf_flags |= SF_UPGRADE;
499 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
503 if (!(sf->sf_flags & SF_INCONSISTENT))
504 dev->od_igif_inoi = 0;
506 case SCRUB_NEXT_OSTOBJ:
507 sf->sf_flags |= SF_INCONSISTENT;
508 case SCRUB_NEXT_OSTOBJ_OLD:
511 sf->sf_flags |= SF_RECREATED;
512 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
513 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
516 } else if (osd_id_eq(lid, lid2)) {
519 scrub->os_full_speed = 1;
520 sf->sf_flags |= SF_INCONSISTENT;
522 /* XXX: If the device is restored from file-level backup, then
523 * some IGIFs may have been already in OI files, and some
524 * may be not yet. Means upgrading from 1.8 may be partly
525 * processed, but some clients may hold some immobilized
526 * IGIFs, and use them to access related objects. Under
527 * such case, OSD does not know whether an given IGIF has
528 * been processed or to be processed, and it also cannot
529 * generate local ino#/gen# directly from the immobilized
530 * IGIF because of the backup/restore. Then force OSD to
531 * lookup the given IGIF in OI files, and if no entry,
532 * then ask the client to retry after upgrading completed.
533 * No better choice. */
534 dev->od_igif_inoi = 1;
537 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops,
538 (val == SCRUB_NEXT_OSTOBJ ||
539 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
541 if (scrub->os_in_prior)
542 sf->sf_items_updated_prior++;
544 sf->sf_items_updated++;
546 /* The target has been changed, need to be re-loaded. */
547 lu_object_purge(info->oti_env, osd2lu_dev(dev), fid);
554 sf->sf_items_failed++;
555 if (sf->sf_pos_first_inconsistent == 0 ||
556 sf->sf_pos_first_inconsistent > lid->oii_ino)
557 sf->sf_pos_first_inconsistent = lid->oii_ino;
562 /* There may be conflict unlink during the OI scrub,
563 * if happend, then remove the new added OI mapping. */
564 if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
565 unlikely(inode->i_nlink == 0))
566 osd_scrub_refresh_mapping(info, dev, fid, lid,
568 (val == SCRUB_NEXT_OSTOBJ ||
569 val == SCRUB_NEXT_OSTOBJ_OLD) ?
570 OI_KNOWN_ON_OST : 0);
571 up_write(&scrub->os_rwsem);
573 if (inode != NULL && !IS_ERR(inode))
577 LASSERT(!cfs_list_empty(&oii->oii_list));
579 spin_lock(&scrub->os_lock);
580 cfs_list_del_init(&oii->oii_list);
581 spin_unlock(&scrub->os_lock);
584 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
587 static int osd_scrub_checkpoint(struct osd_scrub *scrub)
589 struct scrub_file *sf = &scrub->os_file;
592 if (likely(cfs_time_before(cfs_time_current(),
593 scrub->os_time_next_checkpoint) ||
594 scrub->os_new_checked == 0))
597 down_write(&scrub->os_rwsem);
598 sf->sf_items_checked += scrub->os_new_checked;
599 scrub->os_new_checked = 0;
600 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
601 sf->sf_time_last_checkpoint = cfs_time_current_sec();
602 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
603 scrub->os_time_last_checkpoint);
604 rc = osd_scrub_file_store(scrub);
605 up_write(&scrub->os_rwsem);
610 static void osd_scrub_post(struct osd_scrub *scrub, int result)
612 struct scrub_file *sf = &scrub->os_file;
615 down_write(&scrub->os_rwsem);
616 spin_lock(&scrub->os_lock);
617 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
618 spin_unlock(&scrub->os_lock);
619 if (scrub->os_new_checked > 0) {
620 sf->sf_items_checked += scrub->os_new_checked;
621 scrub->os_new_checked = 0;
622 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
624 sf->sf_time_last_checkpoint = cfs_time_current_sec();
626 struct osd_device *dev =
627 container_of0(scrub, struct osd_device, od_scrub);
629 dev->od_igif_inoi = 1;
630 sf->sf_status = SS_COMPLETED;
631 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
632 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
633 SF_UPGRADE | SF_AUTO);
634 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
635 sf->sf_success_count++;
636 } else if (result == 0) {
637 if (scrub->os_paused)
638 sf->sf_status = SS_PAUSED;
640 sf->sf_status = SS_STOPPED;
642 sf->sf_status = SS_FAILED;
644 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
645 scrub->os_time_last_checkpoint);
646 result = osd_scrub_file_store(scrub);
648 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
649 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
651 up_write(&scrub->os_rwsem);
656 /* iteration engine */
658 struct osd_iit_param {
659 struct super_block *sb;
660 struct buffer_head *bitmap;
666 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
667 struct osd_device *dev,
668 struct osd_iit_param *param,
669 struct osd_idmap_cache **oic,
672 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
673 struct osd_device *dev,
674 struct osd_iit_param *param,
675 struct osd_idmap_cache *oic,
676 int *noslot, int rc);
678 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
680 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
681 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
682 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
683 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
684 return SCRUB_NEXT_BREAK;
686 *pos = param->gbase + param->offset;
692 * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
693 * \retval 0: FID-on-MDT
695 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
696 struct osd_device *dev,
699 /* XXX: The initial OI scrub will scan the top level /O to generate
700 * a small local FLDB according to the <seq>. If the given FID
701 * is in the local FLDB, then it is FID-on-OST; otherwise it's
702 * quite possible for FID-on-MDT. */
706 static int osd_scrub_get_fid(struct osd_thread_info *info,
707 struct osd_device *dev, struct inode *inode,
708 struct lu_fid *fid, bool scrub)
710 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
712 bool has_lma = false;
714 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
717 if (lma->lma_compat & LMAC_NOT_IN_OI) {
718 ldiskfs_set_inode_state(inode,
719 LDISKFS_STATE_LUSTRE_NO_OI);
720 return SCRUB_NEXT_CONTINUE;
723 *fid = lma->lma_self_fid;
724 if (fid_is_internal(&lma->lma_self_fid)) {
726 rc = SCRUB_NEXT_CONTINUE;
733 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
736 if (lma->lma_compat & LMAC_FID_ON_OST)
737 return SCRUB_NEXT_OSTOBJ;
739 if (fid_is_idif(fid) || fid_is_last_id(fid))
740 return SCRUB_NEXT_OSTOBJ_OLD;
742 if (lma->lma_incompat & LMAI_AGENT)
743 return SCRUB_NEXT_CONTINUE;
745 /* Here, it may be MDT-object, or may be 2.4 OST-object.
749 if (rc == -ENODATA || rc == 0) {
750 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
753 /* It is old 2.x (x <= 3) or 1.8 OST-object. */
754 rc = SCRUB_NEXT_OSTOBJ_OLD;
760 /* It is FID-on-OST, but we do not know how
761 * to generate its FID, ignore it directly. */
762 rc = SCRUB_NEXT_CONTINUE;
764 /* It is 2.4 OST-object. */
765 rc = SCRUB_NEXT_OSTOBJ_OLD;
773 if (dev->od_handle_nolma) {
774 lu_igif_build(fid, inode->i_ino,
775 inode->i_generation);
777 rc = SCRUB_NEXT_NOLMA;
781 /* It may be FID-on-OST, or may be FID for
782 * non-MDT0, anyway, we do not know how to
783 * generate its FID, ignore it directly. */
784 rc = SCRUB_NEXT_CONTINUE;
789 /* For OI scrub case only: the object has LMA but has no ff
790 * (or ff crashed). It may be MDT-object, may be OST-object
791 * with crashed ff. The last check is local FLDB. */
792 rc = osd_scrub_check_local_fldb(info, dev, fid);
798 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
799 struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
800 struct super_block *sb, bool scrub)
806 osd_id_gen(lid, pos, OSD_OII_NOGEN);
807 inode = osd_iget(info, dev, lid);
810 /* The inode may be removed after bitmap searching, or the
811 * file is new created without inode initialized yet. */
812 if (rc == -ENOENT || rc == -ESTALE)
813 RETURN(SCRUB_NEXT_CONTINUE);
815 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
816 LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
820 /* If the inode has no OI mapping, then it is special locally used,
821 * should be invisible to OI scrub or up layer LFSCK. */
822 if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI))
823 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
826 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
827 /* Only skip it for the first OI scrub accessing. */
828 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
829 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
832 rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
841 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
842 struct osd_iit_param *param,
843 struct osd_idmap_cache **oic, int noslot)
845 struct osd_scrub *scrub = &dev->od_scrub;
846 struct ptlrpc_thread *thread = &scrub->os_thread;
848 struct osd_inode_id *lid;
851 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
852 struct l_wait_info lwi;
854 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
855 l_wait_event(thread->t_ctl_waitq,
856 !cfs_list_empty(&scrub->os_inconsistent_items) ||
857 !thread_is_running(thread),
861 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
862 spin_lock(&scrub->os_lock);
863 thread_set_flags(thread, SVC_STOPPING);
864 spin_unlock(&scrub->os_lock);
865 return SCRUB_NEXT_CRASH;
868 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
869 return SCRUB_NEXT_FATAL;
871 if (unlikely(!thread_is_running(thread)))
872 return SCRUB_NEXT_EXIT;
874 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
875 struct osd_inconsistent_item *oii;
877 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
878 struct osd_inconsistent_item, oii_list);
879 *oic = &oii->oii_cache;
880 scrub->os_in_prior = 1;
885 return SCRUB_NEXT_WAIT;
887 rc = osd_iit_next(param, &scrub->os_pos_current);
891 *oic = &scrub->os_oic;
892 fid = &(*oic)->oic_fid;
893 lid = &(*oic)->oic_lid;
894 rc = osd_iit_iget(info, dev, fid, lid,
895 scrub->os_pos_current, param->sb, true);
899 static int osd_preload_next(struct osd_thread_info *info,
900 struct osd_device *dev, struct osd_iit_param *param,
901 struct osd_idmap_cache **oic, int noslot)
903 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
904 struct osd_scrub *scrub;
905 struct ptlrpc_thread *thread;
908 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
912 scrub = &dev->od_scrub;
913 thread = &scrub->os_thread;
914 if (thread_is_running(thread) &&
915 ooc->ooc_pos_preload >= scrub->os_pos_current)
916 return SCRUB_NEXT_EXIT;
918 rc = osd_iit_iget(info, dev,
919 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
920 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
921 ooc->ooc_pos_preload, param->sb, false);
922 /* If succeed, it needs to move forward; otherwise up layer LFSCK may
923 * ignore the failure, so it still need to skip the inode next time. */
924 ooc->ooc_pos_preload = param->gbase + ++(param->offset);
929 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
931 spin_lock(&scrub->os_lock);
932 if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
933 !cfs_list_empty(&scrub->os_inconsistent_items) ||
934 it->ooi_waiting || !thread_is_running(&scrub->os_thread))
935 scrub->os_waiting = 0;
937 scrub->os_waiting = 1;
938 spin_unlock(&scrub->os_lock);
940 return !scrub->os_waiting;
943 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
944 struct osd_iit_param *param,
945 struct osd_idmap_cache *oic, int *noslot, int rc)
947 struct l_wait_info lwi = { 0 };
948 struct osd_scrub *scrub = &dev->od_scrub;
949 struct scrub_file *sf = &scrub->os_file;
950 struct ptlrpc_thread *thread = &scrub->os_thread;
951 struct osd_otable_it *it = dev->od_otable_it;
952 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
955 case SCRUB_NEXT_CONTINUE:
957 case SCRUB_NEXT_WAIT:
959 case SCRUB_NEXT_NOSCRUB:
960 down_write(&scrub->os_rwsem);
961 scrub->os_new_checked++;
962 sf->sf_items_noscrub++;
963 up_write(&scrub->os_rwsem);
967 rc = osd_scrub_check_update(info, dev, oic, rc);
971 rc = osd_scrub_checkpoint(scrub);
973 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
974 LDISKFS_SB(param->sb)->s_es->s_volume_name,
975 scrub->os_pos_current, rc);
976 /* Continue, as long as the scrub itself can go ahead. */
979 if (scrub->os_in_prior) {
980 scrub->os_in_prior = 0;
985 scrub->os_pos_current = param->gbase + ++(param->offset);
988 if (it != NULL && it->ooi_waiting &&
989 ooc->ooc_pos_preload < scrub->os_pos_current) {
990 spin_lock(&scrub->os_lock);
992 cfs_waitq_broadcast(&thread->t_ctl_waitq);
993 spin_unlock(&scrub->os_lock);
996 if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
999 if (osd_scrub_has_window(scrub, ooc)) {
1004 l_wait_event(thread->t_ctl_waitq,
1005 osd_scrub_wakeup(scrub, it),
1008 if (osd_scrub_has_window(scrub, ooc))
1015 static int osd_preload_exec(struct osd_thread_info *info,
1016 struct osd_device *dev, struct osd_iit_param *param,
1017 struct osd_idmap_cache *oic, int *noslot, int rc)
1019 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1022 ooc->ooc_cached_items++;
1023 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1024 ~OSD_OTABLE_IT_CACHE_MASK;
1026 return rc > 0 ? 0 : rc;
1029 #define SCRUB_IT_ALL 1
1030 #define SCRUB_IT_CRASH 2
1032 static int osd_inode_iteration(struct osd_thread_info *info,
1033 struct osd_device *dev, __u32 max, bool preload)
1035 osd_iit_next_policy next;
1036 osd_iit_exec_policy exec;
1039 struct osd_iit_param param;
1046 struct osd_scrub *scrub = &dev->od_scrub;
1048 next = osd_scrub_next;
1049 exec = osd_scrub_exec;
1050 pos = &scrub->os_pos_current;
1051 count = &scrub->os_new_checked;
1053 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1055 next = osd_preload_next;
1056 exec = osd_preload_exec;
1057 pos = &ooc->ooc_pos_preload;
1058 count = &ooc->ooc_cached_items;
1060 param.sb = osd_sb(dev);
1061 limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1063 while (*pos <= limit && *count < max) {
1064 struct osd_idmap_cache *oic = NULL;
1066 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1067 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1068 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1069 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1070 if (param.bitmap == NULL) {
1071 CERROR("%.16s: fail to read bitmap for %u, "
1072 "scrub will stop, urgent mode\n",
1073 LDISKFS_SB(param.sb)->s_es->s_volume_name,
1078 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1080 rc = next(info, dev, ¶m, &oic, noslot);
1082 case SCRUB_NEXT_BREAK:
1084 case SCRUB_NEXT_EXIT:
1085 brelse(param.bitmap);
1087 case SCRUB_NEXT_CRASH:
1088 brelse(param.bitmap);
1089 RETURN(SCRUB_IT_CRASH);
1090 case SCRUB_NEXT_FATAL:
1091 brelse(param.bitmap);
1095 rc = exec(info, dev, ¶m, oic, &noslot, rc);
1097 brelse(param.bitmap);
1103 brelse(param.bitmap);
1107 RETURN(SCRUB_IT_ALL);
1111 static int osd_otable_it_preload(const struct lu_env *env,
1112 struct osd_otable_it *it)
1114 struct osd_device *dev = it->ooi_dev;
1115 struct osd_scrub *scrub = &dev->od_scrub;
1116 struct osd_otable_cache *ooc = &it->ooi_cache;
1120 rc = osd_inode_iteration(osd_oti_get(env), dev,
1121 OSD_OTABLE_IT_CACHE_SIZE, true);
1122 if (rc == SCRUB_IT_ALL)
1123 it->ooi_all_cached = 1;
1125 CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1126 le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1127 ooc->ooc_pos_preload, rc);
1129 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1130 scrub->os_waiting = 0;
1131 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1134 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1137 static int osd_scrub_main(void *args)
1140 struct osd_device *dev = (struct osd_device *)args;
1141 struct osd_scrub *scrub = &dev->od_scrub;
1142 struct ptlrpc_thread *thread = &scrub->os_thread;
1143 struct super_block *sb = osd_sb(dev);
1147 rc = lu_env_init(&env, LCT_LOCAL);
1149 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1150 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1154 rc = osd_scrub_prep(dev);
1156 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1157 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1161 if (!scrub->os_full_speed) {
1162 struct l_wait_info lwi = { 0 };
1163 struct osd_otable_it *it = dev->od_otable_it;
1164 struct osd_otable_cache *ooc = &it->ooi_cache;
1166 l_wait_event(thread->t_ctl_waitq,
1167 it->ooi_user_ready || !thread_is_running(thread),
1169 if (unlikely(!thread_is_running(thread)))
1172 scrub->os_pos_current = ooc->ooc_pos_preload;
1175 CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1176 scrub->os_start_flags, scrub->os_pos_current);
1178 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1179 if (unlikely(rc == SCRUB_IT_CRASH))
1180 GOTO(out, rc = -EINVAL);
1184 osd_scrub_post(scrub, rc);
1185 CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1186 rc, scrub->os_pos_current);
1189 while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1190 struct osd_inconsistent_item *oii;
1192 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1193 struct osd_inconsistent_item, oii_list);
1194 cfs_list_del_init(&oii->oii_list);
1200 spin_lock(&scrub->os_lock);
1201 thread_set_flags(thread, SVC_STOPPED);
1202 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1203 spin_unlock(&scrub->os_lock);
1207 /* initial OI scrub */
1209 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1210 struct dentry *, filldir_t filldir);
1212 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1213 loff_t offset, __u64 ino, unsigned d_type);
1214 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1215 loff_t offset, __u64 ino, unsigned d_type);
1218 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1219 struct dentry *dentry, filldir_t filldir);
1221 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1222 struct dentry *dentry, filldir_t filldir);
1225 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1226 struct dentry *dentry, filldir_t filldir);
1229 OLF_SCAN_SUBITEMS = 0x0001,
1230 OLF_HIDE_FID = 0x0002,
1231 OLF_SHOW_NAME = 0x0004,
1237 struct lu_fid olm_fid;
1239 scandir_t olm_scandir;
1240 filldir_t olm_filldir;
1243 /* Add the new introduced local files in the list in the future. */
1244 static const struct osd_lf_map osd_lf_maps[] = {
1246 { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1250 { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1251 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1252 osd_ios_varfid_fill },
1254 /* NIDTBL_VERSIONS */
1255 { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1256 osd_ios_general_scan, osd_ios_varfid_fill },
1259 { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1262 { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1263 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1265 /* changelog_catalog */
1266 { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1268 /* changelog_users */
1269 { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1272 { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1276 { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1279 /* lfsck_bookmark */
1280 { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1283 { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1287 { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1288 OLF_SHOW_NAME, NULL, NULL },
1291 { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1292 osd_ios_general_scan, osd_ios_varfid_fill },
1295 { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1296 osd_ios_general_scan, osd_ios_varfid_fill },
1299 { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1300 OLF_SHOW_NAME, NULL, NULL },
1303 { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1304 OLF_SHOW_NAME, NULL, NULL },
1307 { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1308 OLF_SHOW_NAME, NULL, NULL },
1310 /* lfsck_namespace */
1311 { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1313 /* OBJECTS, upgrade from old device */
1314 { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1316 /* lquota_v2.user, upgrade from old device */
1317 { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1319 /* lquota_v2.group, upgrade from old device */
1320 { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1322 /* LAST_GROUP, upgrade from old device */
1323 { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1324 OLF_SHOW_NAME, NULL, NULL },
1327 { "lost+found", { 0, 0, 0 }, OLF_SCAN_SUBITEMS | OLF_NO_OI,
1328 osd_ios_general_scan, osd_ios_lf_fill },
1330 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1333 struct osd_ios_item {
1334 cfs_list_t oii_list;
1335 struct dentry *oii_dentry;
1336 scandir_t oii_scandir;
1337 filldir_t oii_filldir;
1340 struct osd_ios_filldir_buf {
1341 struct osd_thread_info *oifb_info;
1342 struct osd_device *oifb_dev;
1343 struct dentry *oifb_dentry;
1346 static inline struct dentry *
1347 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1349 struct dentry *dentry;
1351 dentry = ll_lookup_one_len(name, parent, namelen);
1352 if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1354 return ERR_PTR(-ENOENT);
1361 osd_ios_llogname2fid(struct lu_fid *fid, const char *name, int namelen)
1366 fid->f_seq = FID_SEQ_LLOG;
1368 id = id * 10 + name[i++] - '0';
1370 fid->f_oid = id & 0x00000000ffffffffULL;
1371 fid->f_ver = id >> 32;
1375 osd_ios_Oname2fid(struct lu_fid *fid, const char *name, int namelen)
1381 seq = seq * 10 + name[i++] - '0';
1383 lu_last_id_fid(fid, seq);
1387 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1388 scandir_t scandir, filldir_t filldir)
1390 struct osd_ios_item *item;
1392 OBD_ALLOC_PTR(item);
1396 CFS_INIT_LIST_HEAD(&item->oii_list);
1397 item->oii_dentry = dget(dentry);
1398 item->oii_scandir = scandir;
1399 item->oii_filldir = filldir;
1400 cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1405 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1407 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1408 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1409 * reference the inode, or fixed if it is missing or references another inode.
1412 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1413 struct inode *inode, const struct lu_fid *fid, int flags)
1415 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1416 struct osd_inode_id *id = &info->oti_id;
1417 struct osd_inode_id *id2 = &info->oti_id2;
1418 struct osd_scrub *scrub = &dev->od_scrub;
1419 struct scrub_file *sf = &scrub->os_file;
1424 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1425 if (rc != 0 && rc != -ENODATA)
1428 osd_id_gen(id, inode->i_ino, inode->i_generation);
1429 if (rc == -ENODATA) {
1430 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1431 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1434 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1438 if (lma->lma_compat & LMAC_NOT_IN_OI)
1441 tfid = lma->lma_self_fid;
1444 rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1449 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1450 DTO_INDEX_INSERT, 0);
1457 if (osd_id_eq_strict(id, id2))
1460 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1461 osd_scrub_file_reset(scrub,
1462 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1464 rc = osd_scrub_file_store(scrub);
1469 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1470 DTO_INDEX_UPDATE, 0);
1478 * It scans the /lost+found, and for the OST-object (with filter_fid
1479 * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1481 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1482 loff_t offset, __u64 ino, unsigned d_type)
1484 struct osd_ios_filldir_buf *fill_buf = buf;
1485 struct osd_thread_info *info = fill_buf->oifb_info;
1486 struct osd_device *dev = fill_buf->oifb_dev;
1487 struct lu_fid *fid = &info->oti_fid;
1488 struct osd_scrub *scrub = &dev->od_scrub;
1489 struct dentry *parent = fill_buf->oifb_dentry;
1490 struct dentry *child;
1491 struct inode *dir = parent->d_inode;
1492 struct inode *inode;
1496 /* skip any '.' started names */
1500 scrub->os_lf_scanned++;
1501 child = osd_ios_lookup_one_len(name, parent, namelen);
1502 if (IS_ERR(child)) {
1503 CWARN("%s: cannot lookup child '%.*s': rc = %d\n",
1504 osd_name(dev), namelen, name, (int)PTR_ERR(child));
1508 inode = child->d_inode;
1509 if (S_ISDIR(inode->i_mode)) {
1510 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1513 CWARN("%s: cannot add child '%.*s': rc = %d\n",
1514 osd_name(dev), namelen, name, rc);
1518 if (!S_ISREG(inode->i_mode))
1521 rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1522 if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1523 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1525 CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1526 "/lost+found.\n", namelen, name, PFID(fid));
1527 scrub->os_lf_repaired++;
1529 CWARN("%s: cannot rename for '%.*s' "DFID": rc = %d\n",
1530 osd_name(dev), namelen, name, PFID(fid), rc);
1534 /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1535 * visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1536 * can process them in furtuer. */
1542 scrub->os_lf_failed++;
1544 /* skip the failure to make the scanning to continue. */
1548 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1549 loff_t offset, __u64 ino, unsigned d_type)
1551 struct osd_ios_filldir_buf *fill_buf = buf;
1552 struct osd_device *dev = fill_buf->oifb_dev;
1553 struct dentry *child;
1557 /* skip any '.' started names */
1561 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1563 RETURN(PTR_ERR(child));
1565 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1567 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1568 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1569 osd_ios_varfid_fill);
1575 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1576 loff_t offset, __u64 ino, unsigned d_type)
1578 struct osd_ios_filldir_buf *fill_buf = buf;
1579 struct osd_device *dev = fill_buf->oifb_dev;
1580 const struct osd_lf_map *map;
1581 struct dentry *child;
1585 /* skip any '.' started names */
1589 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1590 if (strlen(map->olm_name) != namelen)
1593 if (strncmp(map->olm_name, name, namelen) == 0)
1597 if (map->olm_name == NULL)
1600 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1602 RETURN(PTR_ERR(child));
1604 if (!(map->olm_flags & OLF_NO_OI))
1605 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1606 &map->olm_fid, map->olm_flags);
1607 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1608 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1616 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1617 struct dentry *dentry, filldir_t filldir)
1619 struct osd_ios_filldir_buf buf = { info, dev, dentry };
1620 struct file *filp = &info->oti_it_ea.oie_file;
1621 struct inode *inode = dentry->d_inode;
1622 const struct file_operations *fops = inode->i_fop;
1626 LASSERT(filldir != NULL);
1629 filp->f_dentry = dentry;
1630 filp->f_mode = FMODE_64BITHASH;
1631 filp->f_mapping = inode->i_mapping;
1633 filp->private_data = NULL;
1635 rc = fops->readdir(filp, &buf, filldir);
1636 fops->release(inode, filp);
1642 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1643 struct dentry *dentry, filldir_t filldir)
1645 struct osd_scrub *scrub = &dev->od_scrub;
1646 struct scrub_file *sf = &scrub->os_file;
1647 struct dentry *child;
1651 /* It is existing MDT0 device. We only allow the case of object without
1652 * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1653 * can generate IGIF mode FID for the object and related OI mapping. If
1654 * it is on other MDTs, then becuase file-level backup/restore, related
1655 * OI mapping may be invalid already, we do not know which is the right
1656 * FID for the object. We only allow IGIF objects to reside on the MDT0.
1658 * XXX: For the case of object on non-MDT0 device with neither LMA nor
1659 * "fid" xattr, then something crashed. We cannot re-generate the
1660 * FID directly, instead, the OI scrub will scan the OI structure
1661 * and try to re-generate the LMA from the OI mapping. But if the
1662 * OI mapping crashed or lost also, then we have to give up under
1663 * double failure cases. */
1664 dev->od_handle_nolma = 1;
1665 child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1666 strlen(dot_lustre_name));
1667 if (IS_ERR(child)) {
1668 rc = PTR_ERR(child);
1669 if (rc == -ENOENT) {
1670 /* It is 1.8 MDT device. */
1671 if (!(sf->sf_flags & SF_UPGRADE)) {
1672 osd_scrub_file_reset(scrub,
1673 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1675 rc = osd_scrub_file_store(scrub);
1681 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1682 * so the client will get IGIF for the ".lustre" object when
1685 * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1686 * it does not know whether there are some old clients cached
1687 * the ".lustre" IGIF during the upgrading. Two choices:
1689 * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1690 * It will allow the old connected clients to access the
1691 * ".lustre" with cached IGIF. But it will cause others
1692 * on the MDT failed to check "fid_is_dot_lustre()".
1694 * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1695 * for ".lustre" in spite of whether there are some clients
1696 * cached the ".lustre" IGIF or not. It enables the check
1697 * "fid_is_dot_lustre()" on the MDT, although it will cause
1698 * that the old connected clients cannot access the ".lustre"
1699 * with the cached IGIF.
1701 * Usually, it is rare case for the old connected clients
1702 * to access the ".lustre" with cached IGIF. So we prefer
1703 * to the solution 2). */
1704 rc = osd_ios_scan_one(info, dev, child->d_inode,
1705 &LU_DOT_LUSTRE_FID, 0);
1713 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1714 struct dentry *dentry, filldir_t filldir)
1716 struct dentry *child;
1720 child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1721 if (!IS_ERR(child)) {
1722 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1725 rc = PTR_ERR(child);
1728 if (rc != 0 && rc != -ENOENT)
1731 child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1732 if (!IS_ERR(child)) {
1733 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1736 rc = PTR_ERR(child);
1745 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1746 struct osd_device *dev)
1748 struct osd_ios_item *item = NULL;
1749 scandir_t scandir = osd_ios_general_scan;
1750 filldir_t filldir = osd_ios_root_fill;
1751 struct dentry *dentry = osd_sb(dev)->s_root;
1752 const struct osd_lf_map *map = osd_lf_maps;
1757 rc = scandir(info, dev, dentry, filldir);
1759 dput(item->oii_dentry);
1766 if (cfs_list_empty(&dev->od_ios_list))
1769 item = cfs_list_entry(dev->od_ios_list.next,
1770 struct osd_ios_item, oii_list);
1771 cfs_list_del_init(&item->oii_list);
1773 LASSERT(item->oii_scandir != NULL);
1774 scandir = item->oii_scandir;
1775 filldir = item->oii_filldir;
1776 dentry = item->oii_dentry;
1779 while (!cfs_list_empty(&dev->od_ios_list)) {
1780 item = cfs_list_entry(dev->od_ios_list.next,
1781 struct osd_ios_item, oii_list);
1782 cfs_list_del_init(&item->oii_list);
1783 dput(item->oii_dentry);
1790 /* There maybe the case that the object has been removed, but its OI
1791 * mapping is still in the OI file, such as the "CATALOGS" after MDT
1792 * file-level backup/restore. So here cleanup the stale OI mappings. */
1793 while (map->olm_name != NULL) {
1794 struct dentry *child;
1796 if (fid_is_zero(&map->olm_fid)) {
1801 child = osd_ios_lookup_one_len(map->olm_name,
1802 osd_sb(dev)->s_root,
1803 strlen(map->olm_name));
1806 else if (PTR_ERR(child) == -ENOENT)
1807 osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1808 NULL, DTO_INDEX_DELETE, 0);
1815 char *osd_lf_fid2name(const struct lu_fid *fid)
1817 const struct osd_lf_map *map = osd_lf_maps;
1819 while (map->olm_name != NULL) {
1820 if (!lu_fid_eq(fid, &map->olm_fid)) {
1825 if (map->olm_flags & OLF_SHOW_NAME)
1826 return map->olm_name;
1834 /* OI scrub start/stop */
1836 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1838 struct osd_scrub *scrub = &dev->od_scrub;
1839 struct ptlrpc_thread *thread = &scrub->os_thread;
1840 struct l_wait_info lwi = { 0 };
1845 /* os_lock: sync status between stop and scrub thread */
1846 spin_lock(&scrub->os_lock);
1847 if (thread_is_running(thread)) {
1848 spin_unlock(&scrub->os_lock);
1850 } else if (unlikely(thread_is_stopping(thread))) {
1851 spin_unlock(&scrub->os_lock);
1852 l_wait_event(thread->t_ctl_waitq,
1853 thread_is_stopped(thread),
1857 spin_unlock(&scrub->os_lock);
1859 if (scrub->os_file.sf_status == SS_COMPLETED)
1862 scrub->os_start_flags = flags;
1863 thread_set_flags(thread, 0);
1864 rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
1865 if (IS_ERR_VALUE(rc)) {
1866 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1867 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1871 l_wait_event(thread->t_ctl_waitq,
1872 thread_is_running(thread) || thread_is_stopped(thread),
1878 int osd_scrub_start(struct osd_device *dev)
1883 /* od_otable_mutex: prevent curcurrent start/stop */
1884 mutex_lock(&dev->od_otable_mutex);
1885 rc = do_osd_scrub_start(dev, SS_AUTO);
1886 mutex_unlock(&dev->od_otable_mutex);
1888 RETURN(rc == -EALREADY ? 0 : rc);
1891 static void do_osd_scrub_stop(struct osd_scrub *scrub)
1893 struct ptlrpc_thread *thread = &scrub->os_thread;
1894 struct l_wait_info lwi = { 0 };
1896 /* os_lock: sync status between stop and scrub thread */
1897 spin_lock(&scrub->os_lock);
1898 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1899 thread_set_flags(thread, SVC_STOPPING);
1900 spin_unlock(&scrub->os_lock);
1901 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1902 l_wait_event(thread->t_ctl_waitq,
1903 thread_is_stopped(thread),
1905 /* Do not skip the last lock/unlock, which can guarantee that
1906 * the caller cannot return until the OI scrub thread exit. */
1907 spin_lock(&scrub->os_lock);
1909 spin_unlock(&scrub->os_lock);
1912 static void osd_scrub_stop(struct osd_device *dev)
1914 /* od_otable_mutex: prevent curcurrent start/stop */
1915 mutex_lock(&dev->od_otable_mutex);
1916 dev->od_scrub.os_paused = 1;
1917 do_osd_scrub_stop(&dev->od_scrub);
1918 mutex_unlock(&dev->od_otable_mutex);
1921 /* OI scrub setup/cleanup */
1923 static const char osd_scrub_name[] = "OI_scrub";
1925 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1927 struct osd_thread_info *info = osd_oti_get(env);
1928 struct osd_scrub *scrub = &dev->od_scrub;
1929 struct lvfs_run_ctxt *ctxt = &scrub->os_ctxt;
1930 struct scrub_file *sf = &scrub->os_file;
1931 struct super_block *sb = osd_sb(dev);
1932 struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
1933 struct lvfs_run_ctxt saved;
1935 struct inode *inode;
1936 struct lu_fid *fid = &info->oti_fid;
1941 memset(scrub, 0, sizeof(*scrub));
1942 OBD_SET_CTXT_MAGIC(ctxt);
1943 ctxt->pwdmnt = dev->od_mnt;
1944 ctxt->pwd = dev->od_mnt->mnt_root;
1945 ctxt->fs = get_ds();
1947 cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
1948 init_rwsem(&scrub->os_rwsem);
1949 spin_lock_init(&scrub->os_lock);
1950 CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1952 push_ctxt(&saved, ctxt, NULL);
1953 filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
1955 pop_ctxt(&saved, ctxt, NULL);
1956 RETURN(PTR_ERR(filp));
1959 inode = filp->f_dentry->d_inode;
1960 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
1961 /* 'What the @fid is' is not imporatant, because the object
1962 * has no OI mapping, and only is visible inside the OSD.*/
1963 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1964 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
1966 filp_close(filp, 0);
1967 pop_ctxt(&saved, ctxt, NULL);
1971 scrub->os_inode = igrab(inode);
1972 filp_close(filp, 0);
1973 pop_ctxt(&saved, ctxt, NULL);
1975 rc = osd_scrub_file_load(scrub);
1976 if (rc == -ENOENT) {
1977 osd_scrub_file_init(scrub, es->s_uuid);
1979 } else if (rc != 0) {
1982 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
1983 osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
1985 } else if (sf->sf_status == SS_SCANNING) {
1986 sf->sf_status = SS_CRASHED;
1991 if (sf->sf_pos_last_checkpoint != 0)
1992 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1994 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
1997 rc = osd_scrub_file_store(scrub);
2002 /* Initialize OI files. */
2003 rc = osd_oi_init(info, dev);
2007 rc = osd_initial_OI_scrub(info, dev);
2009 if ((sf->sf_flags & SF_UPGRADE) &&
2010 !(sf->sf_flags & SF_INCONSISTENT))
2011 /* The 'od_igif_inoi' will be set after the
2012 * upgrading completed, needs NOT remount. */
2013 dev->od_igif_inoi = 0;
2015 /* The 'od_igif_inoi' will be set under the
2017 * 1) new created system, or
2018 * 2) restored from file-level backup, or
2019 * 3) the upgrading completed.
2021 * The 'od_igif_inoi' may be cleared by OI scrub
2022 * later if found that the system is upgrading. */
2023 dev->od_igif_inoi = 1;
2025 if (!dev->od_noscrub &&
2026 ((sf->sf_status == SS_PAUSED) ||
2027 (sf->sf_status == SS_CRASHED &&
2028 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2029 SF_UPGRADE | SF_AUTO)) ||
2030 (sf->sf_status == SS_INIT &&
2031 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2033 rc = osd_scrub_start(dev);
2036 /* it is possible that dcache entries may keep objects after they are
2037 * deleted by OSD. While it looks safe this can cause object data to
2038 * stay until umount causing failures in tests calculating free space,
2039 * e.g. replay-ost-single. Since those dcache entries are not used
2040 * anymore let's just free them after use here */
2041 shrink_dcache_sb(sb);
2046 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2048 struct osd_scrub *scrub = &dev->od_scrub;
2050 LASSERT(dev->od_otable_it == NULL);
2052 if (scrub->os_inode != NULL) {
2053 osd_scrub_stop(dev);
2054 iput(scrub->os_inode);
2055 scrub->os_inode = NULL;
2057 if (dev->od_oi_table != NULL)
2058 osd_oi_fini(osd_oti_get(env), dev);
2061 /* object table based iteration APIs */
2063 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2064 struct dt_object *dt, __u32 attr,
2065 struct lustre_capa *capa)
2067 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2068 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2069 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
2070 struct osd_scrub *scrub = &dev->od_scrub;
2071 struct osd_otable_it *it;
2076 /* od_otable_mutex: prevent curcurrent init/fini */
2077 mutex_lock(&dev->od_otable_mutex);
2078 if (dev->od_otable_it != NULL)
2079 GOTO(out, it = ERR_PTR(-EALREADY));
2083 GOTO(out, it = ERR_PTR(-ENOMEM));
2085 dev->od_otable_it = it;
2087 it->ooi_cache.ooc_consumer_idx = -1;
2088 if (flags & DOIF_OUTUSED)
2089 it->ooi_used_outside = 1;
2091 if (flags & DOIF_RESET)
2094 if (valid & DOIV_ERROR_HANDLE) {
2095 if (flags & DOIF_FAILOUT)
2096 start |= SS_SET_FAILOUT;
2098 start |= SS_CLEAR_FAILOUT;
2101 rc = do_osd_scrub_start(dev, start);
2102 if (rc < 0 && rc != -EALREADY) {
2103 dev->od_otable_it = NULL;
2105 GOTO(out, it = ERR_PTR(rc));
2108 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2113 mutex_unlock(&dev->od_otable_mutex);
2114 return (struct dt_it *)it;
2117 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2119 struct osd_otable_it *it = (struct osd_otable_it *)di;
2120 struct osd_device *dev = it->ooi_dev;
2122 /* od_otable_mutex: prevent curcurrent init/fini */
2123 mutex_lock(&dev->od_otable_mutex);
2124 do_osd_scrub_stop(&dev->od_scrub);
2125 LASSERT(dev->od_otable_it == it);
2127 dev->od_otable_it = NULL;
2128 mutex_unlock(&dev->od_otable_mutex);
2132 static int osd_otable_it_get(const struct lu_env *env,
2133 struct dt_it *di, const struct dt_key *key)
2138 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2143 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2145 spin_lock(&scrub->os_lock);
2146 if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2147 scrub->os_waiting ||
2148 !thread_is_running(&scrub->os_thread))
2149 it->ooi_waiting = 0;
2151 it->ooi_waiting = 1;
2152 spin_unlock(&scrub->os_lock);
2154 return !it->ooi_waiting;
2157 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2159 struct osd_otable_it *it = (struct osd_otable_it *)di;
2160 struct osd_device *dev = it->ooi_dev;
2161 struct osd_scrub *scrub = &dev->od_scrub;
2162 struct osd_otable_cache *ooc = &it->ooi_cache;
2163 struct ptlrpc_thread *thread = &scrub->os_thread;
2164 struct l_wait_info lwi = { 0 };
2168 LASSERT(it->ooi_user_ready);
2171 if (!thread_is_running(thread) && !it->ooi_used_outside)
2174 if (ooc->ooc_cached_items > 0) {
2175 ooc->ooc_cached_items--;
2176 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2177 ~OSD_OTABLE_IT_CACHE_MASK;
2181 if (it->ooi_all_cached) {
2182 l_wait_event(thread->t_ctl_waitq,
2183 !thread_is_running(thread),
2188 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2189 spin_lock(&scrub->os_lock);
2190 scrub->os_waiting = 0;
2191 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2192 spin_unlock(&scrub->os_lock);
2195 if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2196 l_wait_event(thread->t_ctl_waitq,
2197 osd_otable_it_wakeup(scrub, it),
2200 if (!thread_is_running(thread) && !it->ooi_used_outside)
2203 rc = osd_otable_it_preload(env, it);
2210 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2211 const struct dt_it *di)
2216 static int osd_otable_it_key_size(const struct lu_env *env,
2217 const struct dt_it *di)
2219 return sizeof(__u64);
2222 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2223 struct dt_rec *rec, __u32 attr)
2225 struct osd_otable_it *it = (struct osd_otable_it *)di;
2226 struct osd_otable_cache *ooc = &it->ooi_cache;
2228 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2230 /* Filter out Invald FID already. */
2231 LASSERTF(fid_is_sane((struct lu_fid *)rec),
2232 "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2233 PFID((struct lu_fid *)rec),
2234 ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2239 static __u64 osd_otable_it_store(const struct lu_env *env,
2240 const struct dt_it *di)
2242 struct osd_otable_it *it = (struct osd_otable_it *)di;
2243 struct osd_otable_cache *ooc = &it->ooi_cache;
2246 if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2247 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2249 hash = ooc->ooc_pos_preload;
2254 * Set the OSD layer iteration start position as the specified hash.
2256 static int osd_otable_it_load(const struct lu_env *env,
2257 const struct dt_it *di, __u64 hash)
2259 struct osd_otable_it *it = (struct osd_otable_it *)di;
2260 struct osd_device *dev = it->ooi_dev;
2261 struct osd_otable_cache *ooc = &it->ooi_cache;
2262 struct osd_scrub *scrub = &dev->od_scrub;
2266 /* Forbid to set iteration position after iteration started. */
2267 if (it->ooi_user_ready)
2270 if (hash > OSD_OTABLE_MAX_HASH)
2271 hash = OSD_OTABLE_MAX_HASH;
2273 ooc->ooc_pos_preload = hash;
2274 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2275 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2277 it->ooi_user_ready = 1;
2278 if (!scrub->os_full_speed)
2279 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2281 /* Unplug OSD layer iteration by the first next() call. */
2282 rc = osd_otable_it_next(env, (struct dt_it *)it);
2287 static int osd_otable_it_key_rec(const struct lu_env *env,
2288 const struct dt_it *di, void *key_rec)
2293 const struct dt_index_operations osd_otable_ops = {
2295 .init = osd_otable_it_init,
2296 .fini = osd_otable_it_fini,
2297 .get = osd_otable_it_get,
2298 .put = osd_otable_it_put,
2299 .next = osd_otable_it_next,
2300 .key = osd_otable_it_key,
2301 .key_size = osd_otable_it_key_size,
2302 .rec = osd_otable_it_rec,
2303 .store = osd_otable_it_store,
2304 .load = osd_otable_it_load,
2305 .key_rec = osd_otable_it_key_rec,
2309 /* high priority inconsistent items list APIs */
2311 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2314 struct osd_inconsistent_item *oii;
2315 struct osd_scrub *scrub = &dev->od_scrub;
2316 struct ptlrpc_thread *thread = &scrub->os_thread;
2321 if (unlikely(oii == NULL))
2324 CFS_INIT_LIST_HEAD(&oii->oii_list);
2325 oii->oii_cache = *oic;
2326 oii->oii_insert = insert;
2328 spin_lock(&scrub->os_lock);
2329 if (unlikely(!thread_is_running(thread))) {
2330 spin_unlock(&scrub->os_lock);
2335 if (cfs_list_empty(&scrub->os_inconsistent_items))
2337 cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2338 spin_unlock(&scrub->os_lock);
2341 cfs_waitq_broadcast(&thread->t_ctl_waitq);
2346 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2347 struct osd_inode_id *id)
2349 struct osd_scrub *scrub = &dev->od_scrub;
2350 struct osd_inconsistent_item *oii;
2353 spin_lock(&scrub->os_lock);
2354 cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2355 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2356 *id = oii->oii_cache.oic_lid;
2357 spin_unlock(&scrub->os_lock);
2361 spin_unlock(&scrub->os_lock);
2368 static const char *scrub_status_names[] = {
2379 static const char *scrub_flags_names[] = {
2387 static const char *scrub_param_names[] = {
2392 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2400 rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2406 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2409 rc = snprintf(*buf, *len, "%s%c", names[i],
2410 bits != 0 ? ',' : '\n');
2421 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2426 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2427 cfs_time_current_sec() - time);
2429 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2438 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2443 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2445 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2454 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2456 struct osd_scrub *scrub = &dev->od_scrub;
2457 struct scrub_file *sf = &scrub->os_file;
2464 down_read(&scrub->os_rwsem);
2465 rc = snprintf(buf, len,
2470 sf->sf_magic, (int)sf->sf_oi_count,
2471 scrub_status_names[sf->sf_status]);
2477 rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2482 rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2487 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2488 "time_since_last_completed");
2492 rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2493 "time_since_latest_start");
2497 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2498 "time_since_last_checkpoint");
2502 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2503 "latest_start_position");
2507 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2508 "last_checkpoint_position");
2512 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2513 "first_failure_position");
2517 checked = sf->sf_items_checked + scrub->os_new_checked;
2518 rc = snprintf(buf, len,
2519 "checked: "LPU64"\n"
2520 "updated: "LPU64"\n"
2522 "prior_updated: "LPU64"\n"
2523 "noscrub: "LPU64"\n"
2525 "success_count: %u\n",
2526 checked, sf->sf_items_updated, sf->sf_items_failed,
2527 sf->sf_items_updated_prior, sf->sf_items_noscrub,
2528 sf->sf_items_igif, sf->sf_success_count);
2535 if (thread_is_running(&scrub->os_thread)) {
2536 cfs_duration_t duration = cfs_time_current() -
2537 scrub->os_time_last_checkpoint;
2538 __u64 new_checked = scrub->os_new_checked * CFS_HZ;
2539 __u32 rtime = sf->sf_run_time +
2540 cfs_duration_sec(duration + HALF_SEC);
2543 do_div(new_checked, duration);
2545 do_div(speed, rtime);
2546 rc = snprintf(buf, len,
2547 "run_time: %u seconds\n"
2548 "average_speed: "LPU64" objects/sec\n"
2549 "real-time_speed: "LPU64" objects/sec\n"
2550 "current_position: %u\n"
2551 "lf_scanned: "LPU64"\n"
2552 "lf_reparied: "LPU64"\n"
2553 "lf_failed: "LPU64"\n",
2554 rtime, speed, new_checked, scrub->os_pos_current,
2555 scrub->os_lf_scanned, scrub->os_lf_repaired,
2556 scrub->os_lf_failed);
2558 if (sf->sf_run_time != 0)
2559 do_div(speed, sf->sf_run_time);
2560 rc = snprintf(buf, len,
2561 "run_time: %u seconds\n"
2562 "average_speed: "LPU64" objects/sec\n"
2563 "real-time_speed: N/A\n"
2564 "current_position: N/A\n"
2565 "lf_scanned: "LPU64"\n"
2566 "lf_reparied: "LPU64"\n"
2567 "lf_failed: "LPU64"\n",
2568 sf->sf_run_time, speed, scrub->os_lf_scanned,
2569 scrub->os_lf_repaired, scrub->os_lf_failed);
2579 up_read(&scrub->os_rwsem);